3 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #define FRR_DEFINE_DESC_TABLE
34 /* for printstack on solaris */
35 #ifdef HAVE_UCONTEXT_H
39 DEFINE_MTYPE_STATIC(LIB
, ZLOG
, "Logging")
41 static int logfile_fd
= -1; /* Used in signal handler. */
43 struct zlog
*zlog_default
= NULL
;
45 const char *zlog_priority
[] =
61 * glibc has declared that the return value from write *must* not be
63 * gcc see's this problem and issues a warning for the line.
65 * Why is this a big deal you say? Because both of them are right
66 * and if you have -Werror enabled then all calls to write
67 * generate a build error and the build stops.
69 * clang has helpfully allowed this construct:
71 * to tell the compiler yeah I know it has a return value
72 * I don't care about it at this time.
73 * gcc doesn't have this ability.
75 * This code was written such that it didn't care about the
76 * return value from write. At this time do I want
77 * to go through and fix and test this code for correctness.
78 * So just wrapper the bad behavior and move on.
80 static void write_wrapper (int fd
, const void *buf
, size_t count
)
82 if (write (fd
, buf
, count
) <= 0)
89 * Looks up a message in a message list by key.
91 * If the message is not found, returns the provided error message.
93 * Terminates when it hits a struct message that's all zeros.
95 * @param mz the message list
96 * @param kz the message key
97 * @param nf the message to return if not found
101 lookup_msg(const struct message
*mz
, int kz
, const char *nf
)
103 static struct message nt
= { 0 };
104 const char *rz
= nf
? nf
: "(no message found)";
105 const struct message
*pnt
;
106 for (pnt
= mz
; memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
107 if (pnt
->key
== kz
) {
108 rz
= pnt
->str
? pnt
->str
: rz
;
114 /* For time string format. */
116 quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
123 struct timeval clock
;
125 gettimeofday(&clock
, NULL
);
127 /* first, we update the cache if the time has changed */
128 if (cache
.last
!= clock
.tv_sec
)
131 cache
.last
= clock
.tv_sec
;
132 tm
= localtime(&cache
.last
);
133 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
134 "%Y/%m/%d %H:%M:%S", tm
);
136 /* note: it's not worth caching the subsecond part, because
137 chances are that back-to-back calls are not sufficiently close together
138 for the clock not to have ticked forward */
140 if (buflen
> cache
.len
)
142 memcpy(buf
, cache
.buf
, cache
.len
);
143 if ((timestamp_precision
> 0) &&
144 (buflen
> cache
.len
+1+timestamp_precision
))
146 /* should we worry about locale issues? */
147 static const int divisor
[] = {0, 100000, 10000, 1000, 100, 10, 1};
149 char *p
= buf
+cache
.len
+1+(prec
= timestamp_precision
);
152 /* this is unlikely to happen, but protect anyway */
157 clock
.tv_usec
/= divisor
[prec
];
160 *p
-- = '0'+(clock
.tv_usec
% 10);
165 return cache
.len
+1+timestamp_precision
;
167 buf
[cache
.len
] = '\0';
175 /* Utility routine for current time printing. */
177 time_print(FILE *fp
, struct timestamp_control
*ctl
)
179 if (!ctl
->already_rendered
)
181 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
182 ctl
->already_rendered
= 1;
184 fprintf(fp
, "%s ", ctl
->buf
);
188 /* va_list version of zlog. */
190 vzlog (int priority
, const char *format
, va_list args
)
193 int original_errno
= errno
;
194 struct timestamp_control tsctl
;
195 tsctl
.already_rendered
= 0;
196 struct zlog
*zl
= zlog_default
;
198 /* When zlog_default is also NULL, use stderr for logging. */
202 time_print(stderr
, &tsctl
);
203 fprintf (stderr
, "%s: ", "unknown");
204 vfprintf (stderr
, format
, args
);
205 fprintf (stderr
, "\n");
208 /* In this case we return at here. */
209 errno
= original_errno
;
212 tsctl
.precision
= zl
->timestamp_precision
;
215 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
219 vsyslog (priority
|zlog_default
->facility
, format
, ac
);
224 sprintf (proto_str
, "%s[%d]: ", zl
->protoname
, zl
->instance
);
226 sprintf (proto_str
, "%s: ", zl
->protoname
);
229 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
232 time_print (zl
->fp
, &tsctl
);
233 if (zl
->record_priority
)
234 fprintf (zl
->fp
, "%s: ", zlog_priority
[priority
]);
235 fprintf (zl
->fp
, "%s", proto_str
);
237 vfprintf (zl
->fp
, format
, ac
);
239 fprintf (zl
->fp
, "\n");
244 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
247 time_print (stdout
, &tsctl
);
248 if (zl
->record_priority
)
249 fprintf (stdout
, "%s: ", zlog_priority
[priority
]);
250 fprintf (stdout
, "%s", proto_str
);
252 vfprintf (stdout
, format
, ac
);
254 fprintf (stdout
, "\n");
258 /* Terminal monitor. */
259 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
260 vty_log ((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
261 proto_str
, format
, &tsctl
, args
);
263 errno
= original_errno
;
267 vzlog_test (int priority
)
269 struct zlog
*zl
= zlog_default
;
271 /* When zlog_default is also NULL, use stderr for logging. */
278 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
284 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
290 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
295 /* Terminal monitor. */
296 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
303 str_append(char *dst
, int len
, const char *src
)
305 while ((len
-- > 0) && *src
)
311 num_append(char *s
, int len
, u_long x
)
317 return str_append(s
,len
,"0");
318 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
319 while (x
&& (t
> buf
))
324 return str_append(s
,len
,t
);
327 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
329 hex_append(char *s
, int len
, u_long x
)
335 return str_append(s
,len
,"0");
336 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
337 while (x
&& (t
> buf
))
340 *--t
= ((cc
< 10) ? ('0'+cc
) : ('a'+cc
-10));
343 return str_append(s
,len
,t
);
347 /* Needs to be enhanced to support Solaris. */
356 struct sockaddr_un addr
;
358 if ((fd
= socket(AF_UNIX
,SOCK_DGRAM
,0)) < 0)
360 addr
.sun_family
= AF_UNIX
;
362 #define SYSLOG_SOCKET_PATH _PATH_LOG
364 #define SYSLOG_SOCKET_PATH "/dev/log"
366 s
= str_append(addr
.sun_path
,sizeof(addr
.sun_path
),SYSLOG_SOCKET_PATH
);
367 #undef SYSLOG_SOCKET_PATH
369 if (connect(fd
,(struct sockaddr
*)&addr
,sizeof(addr
)) < 0)
379 syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
381 static int syslog_fd
= -1;
382 char buf
[sizeof("<1234567890>ripngd[1234567890]: ")+msglen
+50];
385 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
388 #define LOC s,buf+sizeof(buf)-s
390 s
= str_append(LOC
,"<");
391 s
= num_append(LOC
,priority
);
392 s
= str_append(LOC
,">");
393 /* forget about the timestamp, too difficult in a signal handler */
394 s
= str_append(LOC
,zlog_default
->ident
);
395 if (zlog_default
->syslog_options
& LOG_PID
)
397 s
= str_append(LOC
,"[");
398 s
= num_append(LOC
,getpid());
399 s
= str_append(LOC
,"]");
401 s
= str_append(LOC
,": ");
402 s
= str_append(LOC
,msg
);
403 write_wrapper (syslog_fd
,buf
,s
-buf
);
410 #define CRASHLOG_PREFIX "/var/tmp/quagga."
411 #define CRASHLOG_SUFFIX "crashlog"
412 if (zlog_default
&& zlog_default
->ident
)
414 /* Avoid strlen since it is not async-signal-safe. */
418 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
421 char buf
[sizeof(CRASHLOG_PREFIX
)+ilen
+sizeof(CRASHLOG_SUFFIX
)+3];
423 #define LOC s,buf+sizeof(buf)-s
424 s
= str_append(LOC
, CRASHLOG_PREFIX
);
425 s
= str_append(LOC
, zlog_default
->ident
);
426 s
= str_append(LOC
, ".");
427 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
430 return open(buf
, O_WRONLY
|O_CREAT
|O_EXCL
, LOGFILE_MASK
);
433 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
, O_WRONLY
|O_CREAT
|O_EXCL
,
435 #undef CRASHLOG_SUFFIX
436 #undef CRASHLOG_PREFIX
439 /* Note: the goal here is to use only async-signal-safe functions. */
441 zlog_signal(int signo
, const char *action
443 , siginfo_t
*siginfo
, void *program_counter
448 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
450 char *msgstart
= buf
;
451 #define LOC s,buf+sizeof(buf)-s
456 s
= str_append(LOC
,zlog_default
->protoname
);
461 s
= str_append(LOC
,"Received signal ");
462 s
= num_append(LOC
,signo
);
463 s
= str_append(LOC
," at ");
464 s
= num_append(LOC
,now
);
466 s
= str_append(LOC
," (si_addr 0x");
467 s
= hex_append(LOC
,(u_long
)(siginfo
->si_addr
));
470 s
= str_append(LOC
,", PC 0x");
471 s
= hex_append(LOC
,(u_long
)program_counter
);
473 s
= str_append(LOC
,"); ");
474 #else /* SA_SIGINFO */
475 s
= str_append(LOC
,"; ");
476 #endif /* SA_SIGINFO */
477 s
= str_append(LOC
,action
);
478 if (s
< buf
+sizeof(buf
))
481 /* N.B. implicit priority is most severe */
484 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
485 /* If no file logging configured, try to write to fallback log file. */
486 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
492 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
494 /* Remove trailing '\n' for monitor and syslog */
496 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
497 vty_log_fixed(buf
,s
-buf
);
498 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
499 syslog_sigsafe(PRI
|zlog_default
->facility
,msgstart
,s
-msgstart
);
503 zlog_backtrace_sigsafe(PRI
,
513 tc
= pthread_getspecific (thread_current
);
515 s
= str_append (LOC
, "no thread information available\n");
518 s
= str_append (LOC
, "in thread ");
519 s
= str_append (LOC
, tc
->funcname
);
520 s
= str_append (LOC
, " scheduled from ");
521 s
= str_append (LOC
, tc
->schedfrom
);
522 s
= str_append (LOC
, ":");
523 s
= num_append (LOC
, tc
->schedfrom_line
);
524 s
= str_append (LOC
, "\n");
527 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
528 /* If no file logging configured, try to write to fallback log file. */
535 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
537 /* Remove trailing '\n' for monitor and syslog */
539 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
540 vty_log_fixed(buf
,s
-buf
);
541 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
542 syslog_sigsafe(PRI
|zlog_default
->facility
,msgstart
,s
-msgstart
);
550 /* Log a backtrace using only async-signal-safe functions.
551 Needs to be enhanced to support syslog logging. */
553 zlog_backtrace_sigsafe(int priority
, void *program_counter
)
555 #ifdef HAVE_STACK_TRACE
556 static const char pclabel
[] = "Program counter: ";
560 char *s
, **bt
= NULL
;
561 #define LOC s,buf+sizeof(buf)-s
563 #ifdef HAVE_GLIBC_BACKTRACE
564 size
= backtrace(array
, array_size(array
));
565 if (size
<= 0 || (size_t)size
> array_size(array
))
569 if (program_counter) \
571 write_wrapper(FD, pclabel, sizeof(pclabel)-1); \
572 backtrace_symbols_fd(&program_counter, 1, FD); \
574 write_wrapper(FD, buf, s-buf); \
575 backtrace_symbols_fd(array, size, FD); \
577 #elif defined(HAVE_PRINTSTACK)
579 if (program_counter) \
580 write_wrapper((FD), pclabel, sizeof(pclabel)-1); \
581 write_wrapper((FD), buf, s-buf); \
584 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
587 s
= str_append(LOC
,"Backtrace for ");
588 s
= num_append(LOC
,size
);
589 s
= str_append(LOC
," stack frames:\n");
591 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
597 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
599 /* Remove trailing '\n' for monitor and syslog */
601 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
602 vty_log_fixed(buf
,s
-buf
);
603 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
604 syslog_sigsafe(priority
|zlog_default
->facility
,buf
,s
-buf
);
607 #ifdef HAVE_GLIBC_BACKTRACE
608 bt
= backtrace_symbols(array
, size
);
610 /* Just print the function addresses. */
611 for (i
= 0; i
< size
; i
++)
615 s
= str_append(LOC
, bt
[i
]);
617 s
= str_append(LOC
,"[bt ");
618 s
= num_append(LOC
,i
);
619 s
= str_append(LOC
,"] 0x");
620 s
= hex_append(LOC
,(u_long
)(array
[i
]));
623 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
624 vty_log_fixed(buf
,s
-buf
);
625 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
626 syslog_sigsafe(priority
|zlog_default
->facility
,buf
,s
-buf
);
634 #endif /* HAVE_STRACK_TRACE */
638 zlog_backtrace(int priority
)
640 #ifndef HAVE_GLIBC_BACKTRACE
641 zlog(priority
, "No backtrace available on this platform.");
647 size
= backtrace(array
, array_size(array
));
648 if (size
<= 0 || (size_t)size
> array_size(array
))
650 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
651 "(valid range is between 1 and %lu)",
652 size
, (unsigned long)(array_size(array
)));
655 zlog(priority
, "Backtrace for %d stack frames:", size
);
656 if (!(strings
= backtrace_symbols(array
, size
)))
658 zlog_err("Cannot get backtrace symbols (out of memory?)");
659 for (i
= 0; i
< size
; i
++)
660 zlog(priority
, "[bt %d] %p",i
,array
[i
]);
664 for (i
= 0; i
< size
; i
++)
665 zlog(priority
, "[bt %d] %s",i
,strings
[i
]);
668 #endif /* HAVE_GLIBC_BACKTRACE */
672 zlog (int priority
, const char *format
, ...)
676 va_start(args
, format
);
677 vzlog (priority
, format
, args
);
681 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
683 FUNCNAME(const char *format, ...) \
686 va_start(args, format); \
687 vzlog (PRIORITY, format, args); \
691 ZLOG_FUNC(zlog_err
, LOG_ERR
)
693 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
695 ZLOG_FUNC(zlog_info
, LOG_INFO
)
697 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
699 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
703 void zlog_thread_info (int log_level
)
706 tc
= pthread_getspecific (thread_current
);
709 zlog(log_level
, "Current thread function %s, scheduled from "
710 "file %s, line %u", tc
->funcname
,
711 tc
->schedfrom
, tc
->schedfrom_line
);
713 zlog(log_level
, "Current thread not known/applicable");
717 _zlog_assert_failed (const char *assertion
, const char *file
,
718 unsigned int line
, const char *function
)
720 /* Force fallback file logging? */
721 if (zlog_default
&& !zlog_default
->fp
&&
722 ((logfile_fd
= open_crashlog()) >= 0) &&
723 ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
724 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
725 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
726 assertion
,file
,line
,(function
? function
: "?"));
727 zlog_backtrace(LOG_CRIT
);
728 zlog_thread_info(LOG_CRIT
);
729 log_memstats_stderr ("log");
734 memory_oom (size_t size
, const char *name
)
736 zlog_err("out of memory: failed to allocate %zu bytes for %s"
737 "object", size
, name
);
738 zlog_backtrace(LOG_ERR
);
742 /* Open log stream */
744 openzlog (const char *progname
, const char *protoname
, u_short instance
,
745 int syslog_flags
, int syslog_facility
)
750 zl
= XCALLOC(MTYPE_ZLOG
, sizeof (struct zlog
));
752 zl
->ident
= progname
;
753 zl
->protoname
= protoname
;
754 zl
->instance
= instance
;
755 zl
->facility
= syslog_facility
;
756 zl
->syslog_options
= syslog_flags
;
758 /* Set default logging levels. */
759 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
760 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
761 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
762 zl
->default_lvl
= LOG_DEBUG
;
764 openlog (progname
, syslog_flags
, zl
->facility
);
767 #ifdef HAVE_GLIBC_BACKTRACE
768 /* work around backtrace() using lazily resolved dynamically linked
769 * symbols, which will otherwise cause funny breakage in the SEGV handler.
770 * (particularly, the dynamic linker can call malloc(), which uses locks
771 * in programs linked with -pthread, thus can deadlock.) */
773 backtrace (bt
, array_size(bt
));
774 free (backtrace_symbols (bt
, 0));
775 backtrace_symbols_fd (bt
, 0, 0);
782 struct zlog
*zl
= zlog_default
;
789 if (zl
->filename
!= NULL
)
790 XFREE(MTYPE_ZLOG
, zl
->filename
);
792 XFREE (MTYPE_ZLOG
, zl
);
796 /* Called from command.c. */
798 zlog_set_level (zlog_dest_t dest
, int log_level
)
800 zlog_default
->maxlvl
[dest
] = log_level
;
804 zlog_set_file (const char *filename
, int log_level
)
806 struct zlog
*zl
= zlog_default
;
810 /* There is opend file. */
814 oldumask
= umask (0777 & ~LOGFILE_MASK
);
815 fp
= fopen (filename
, "a");
821 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
822 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
824 logfile_fd
= fileno(fp
);
829 /* Reset opend file. */
831 zlog_reset_file (void)
833 struct zlog
*zl
= zlog_default
;
839 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
842 XFREE(MTYPE_ZLOG
, zl
->filename
);
848 /* Reopen log file. */
852 struct zlog
*zl
= zlog_default
;
859 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
860 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
867 oldumask
= umask (0777 & ~LOGFILE_MASK
);
868 zl
->fp
= fopen (zl
->filename
, "a");
873 zlog_err("Log rotate failed: cannot open file %s for append: %s",
874 zl
->filename
, safe_strerror(save_errno
));
877 logfile_fd
= fileno(zl
->fp
);
878 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
884 /* Wrapper around strerror to handle case where it returns NULL. */
886 safe_strerror(int errnum
)
888 const char *s
= strerror(errnum
);
889 return (s
!= NULL
) ? s
: "Unknown error";
892 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
893 static const struct zebra_desc_table command_types
[] = {
894 DESC_ENTRY (ZEBRA_INTERFACE_ADD
),
895 DESC_ENTRY (ZEBRA_INTERFACE_DELETE
),
896 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD
),
897 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE
),
898 DESC_ENTRY (ZEBRA_INTERFACE_UP
),
899 DESC_ENTRY (ZEBRA_INTERFACE_DOWN
),
900 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD
),
901 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE
),
902 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD
),
903 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE
),
904 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD
),
905 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE
),
906 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
907 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
908 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD
),
909 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE
),
910 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE
),
911 DESC_ENTRY (ZEBRA_HELLO
),
912 DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER
),
913 DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER
),
914 DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE
),
915 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
916 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
917 DESC_ENTRY (ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
918 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_REGISTER
),
919 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_UNREGISTER
),
920 DESC_ENTRY (ZEBRA_IMPORT_CHECK_UPDATE
),
921 DESC_ENTRY (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
),
922 DESC_ENTRY (ZEBRA_BFD_DEST_REGISTER
),
923 DESC_ENTRY (ZEBRA_BFD_DEST_DEREGISTER
),
924 DESC_ENTRY (ZEBRA_BFD_DEST_UPDATE
),
925 DESC_ENTRY (ZEBRA_BFD_DEST_REPLAY
),
926 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_ADD
),
927 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_DEL
),
928 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_ADD
),
929 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_DEL
),
930 DESC_ENTRY (ZEBRA_VRF_UNREGISTER
),
931 DESC_ENTRY (ZEBRA_VRF_ADD
),
932 DESC_ENTRY (ZEBRA_VRF_DELETE
),
933 DESC_ENTRY (ZEBRA_INTERFACE_VRF_UPDATE
),
934 DESC_ENTRY (ZEBRA_BFD_CLIENT_REGISTER
),
935 DESC_ENTRY (ZEBRA_INTERFACE_ENABLE_RADV
),
936 DESC_ENTRY (ZEBRA_INTERFACE_DISABLE_RADV
),
937 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
938 DESC_ENTRY (ZEBRA_INTERFACE_LINK_PARAMS
),
939 DESC_ENTRY (ZEBRA_MPLS_LABELS_ADD
),
940 DESC_ENTRY (ZEBRA_MPLS_LABELS_DELETE
),
941 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_ADD
),
942 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_DELETE
),
943 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_ADD
),
944 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_DELETE
),
945 DESC_ENTRY (ZEBRA_IPMR_ROUTE_STATS
),
946 DESC_ENTRY (ZEBRA_LABEL_MANAGER_CONNECT
),
947 DESC_ENTRY (ZEBRA_GET_LABEL_CHUNK
),
948 DESC_ENTRY (ZEBRA_RELEASE_LABEL_CHUNK
),
949 DESC_ENTRY (ZEBRA_ADVERTISE_ALL_VNI
),
950 DESC_ENTRY (ZEBRA_VNI_ADD
),
951 DESC_ENTRY (ZEBRA_VNI_DEL
),
952 DESC_ENTRY (ZEBRA_REMOTE_VTEP_ADD
),
953 DESC_ENTRY (ZEBRA_REMOTE_VTEP_DEL
),
954 DESC_ENTRY (ZEBRA_MACIP_ADD
),
955 DESC_ENTRY (ZEBRA_MACIP_DEL
),
956 DESC_ENTRY (ZEBRA_REMOTE_MACIP_ADD
),
957 DESC_ENTRY (ZEBRA_REMOTE_MACIP_DEL
),
961 static const struct zebra_desc_table unknown
= { 0, "unknown", '?' };
963 static const struct zebra_desc_table
*
964 zroute_lookup(u_int zroute
)
968 if (zroute
>= array_size(route_types
))
970 zlog_err("unknown zebra route type: %u", zroute
);
973 if (zroute
== route_types
[zroute
].type
)
974 return &route_types
[zroute
];
975 for (i
= 0; i
< array_size(route_types
); i
++)
977 if (zroute
== route_types
[i
].type
)
979 zlog_warn("internal error: route type table out of order "
980 "while searching for %u, please notify developers", zroute
);
981 return &route_types
[i
];
984 zlog_err("internal error: cannot find route type %u in table!", zroute
);
989 zebra_route_string(u_int zroute
)
991 return zroute_lookup(zroute
)->string
;
995 zebra_route_char(u_int zroute
)
997 return zroute_lookup(zroute
)->chr
;
1001 zserv_command_string (unsigned int command
)
1003 if (command
>= array_size(command_types
))
1005 zlog_err ("unknown zserv command type: %u", command
);
1006 return unknown
.string
;
1008 return command_types
[command
].string
;
1012 proto_name2num(const char *s
)
1016 for (i
=0; i
<array_size(route_types
); ++i
)
1017 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1018 return route_types
[i
].type
;
1023 proto_redistnum(int afi
, const char *s
)
1030 if (strmatch (s
, "kernel"))
1031 return ZEBRA_ROUTE_KERNEL
;
1032 else if (strmatch (s
, "connected"))
1033 return ZEBRA_ROUTE_CONNECT
;
1034 else if (strmatch (s
, "static"))
1035 return ZEBRA_ROUTE_STATIC
;
1036 else if (strmatch (s
, "rip"))
1037 return ZEBRA_ROUTE_RIP
;
1038 else if (strmatch (s
, "eigrp"))
1039 return ZEBRA_ROUTE_EIGRP
;
1040 else if (strmatch (s
, "ospf"))
1041 return ZEBRA_ROUTE_OSPF
;
1042 else if (strmatch (s
, "isis"))
1043 return ZEBRA_ROUTE_ISIS
;
1044 else if (strmatch (s
, "bgp"))
1045 return ZEBRA_ROUTE_BGP
;
1046 else if (strmatch (s
, "table"))
1047 return ZEBRA_ROUTE_TABLE
;
1048 else if (strmatch (s
, "vnc"))
1049 return ZEBRA_ROUTE_VNC
;
1050 else if (strmatch (s
, "vnc-direct"))
1051 return ZEBRA_ROUTE_VNC_DIRECT
;
1052 else if (strmatch (s
, "nhrp"))
1053 return ZEBRA_ROUTE_NHRP
;
1054 else if (strmatch (s
, "babel"))
1055 return ZEBRA_ROUTE_BABEL
;
1059 if (strmatch (s
, "kernel"))
1060 return ZEBRA_ROUTE_KERNEL
;
1061 else if (strmatch (s
, "connected"))
1062 return ZEBRA_ROUTE_CONNECT
;
1063 else if (strmatch (s
, "static"))
1064 return ZEBRA_ROUTE_STATIC
;
1065 else if (strmatch (s
, "ripng"))
1066 return ZEBRA_ROUTE_RIPNG
;
1067 else if (strmatch (s
, "ospf6"))
1068 return ZEBRA_ROUTE_OSPF6
;
1069 else if (strmatch (s
, "isis"))
1070 return ZEBRA_ROUTE_ISIS
;
1071 else if (strmatch (s
, "bgp"))
1072 return ZEBRA_ROUTE_BGP
;
1073 else if (strmatch (s
, "table"))
1074 return ZEBRA_ROUTE_TABLE
;
1075 else if (strmatch (s
, "vnc"))
1076 return ZEBRA_ROUTE_VNC
;
1077 else if (strmatch (s
, "vnc-direct"))
1078 return ZEBRA_ROUTE_VNC_DIRECT
;
1079 else if (strmatch (s
, "nhrp"))
1080 return ZEBRA_ROUTE_NHRP
;
1081 else if (strmatch (s
, "babel"))
1082 return ZEBRA_ROUTE_BABEL
;
1088 zlog_hexdump (const void *mem
, unsigned int len
) {
1089 unsigned long i
= 0;
1091 unsigned int columns
= 8;
1092 char buf
[(len
* 4) + ((len
/4) * 20) + 30];
1095 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0); i
++)
1098 if (i
% columns
== 0)
1099 s
+= sprintf(s
, "0x%016lx: ", (unsigned long)mem
+ i
);
1101 /* print hex data */
1103 s
+= sprintf(s
, "%02x ", 0xFF & ((const char*)mem
)[i
]);
1105 /* end of block, just aligning for ASCII dump */
1107 s
+= sprintf(s
, " ");
1109 /* print ASCII dump */
1110 if (i
% columns
== (columns
- 1))
1112 for (j
= i
- (columns
- 1); j
<= i
; j
++)
1114 if (j
>= len
) /* end of block, not really printing */
1115 s
+= sprintf(s
, " ");
1117 else if(isprint((int)((const char *)mem
)[j
])) /* printable char */
1118 s
+= sprintf(s
, "%c", 0xFF & ((const char *)mem
)[j
]);
1120 else /* other char */
1121 s
+= sprintf(s
, ".");
1123 s
+= sprintf(s
, "\n");
1126 zlog_debug("\n%s", buf
);
1130 zlog_sanitize (char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1132 const char *inbuf
= in
;
1133 char *pos
= buf
, *end
= buf
+ bufsz
;
1134 const char *iend
= inbuf
+ inlen
;
1136 memset (buf
, 0, bufsz
);
1137 for (; inbuf
< iend
; inbuf
++)
1139 /* don't write partial escape sequence */
1144 snprintf (pos
, end
- pos
, "\\n");
1145 else if (*inbuf
== '\r')
1146 snprintf (pos
, end
- pos
, "\\r");
1147 else if (*inbuf
== '\t')
1148 snprintf (pos
, end
- pos
, "\\t");
1149 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1150 snprintf (pos
, end
- pos
, "\\x%02hhx", *inbuf
);
1154 pos
+= strlen (pos
);