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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #define FRR_DEFINE_DESC_TABLE
35 /* for printstack on solaris */
36 #ifdef HAVE_UCONTEXT_H
40 DEFINE_MTYPE_STATIC(LIB
, ZLOG
, "Logging")
42 static int logfile_fd
= -1; /* Used in signal handler. */
44 struct zlog
*zlog_default
= NULL
;
46 const char *zlog_priority
[] = {
47 "emergencies", "alerts", "critical", "errors", "warnings",
48 "notifications", "informational", "debugging", NULL
,
54 * glibc has declared that the return value from write *must* not be
56 * gcc see's this problem and issues a warning for the line.
58 * Why is this a big deal you say? Because both of them are right
59 * and if you have -Werror enabled then all calls to write
60 * generate a build error and the build stops.
62 * clang has helpfully allowed this construct:
64 * to tell the compiler yeah I know it has a return value
65 * I don't care about it at this time.
66 * gcc doesn't have this ability.
68 * This code was written such that it didn't care about the
69 * return value from write. At this time do I want
70 * to go through and fix and test this code for correctness.
71 * So just wrapper the bad behavior and move on.
73 static void write_wrapper(int fd
, const void *buf
, size_t count
)
75 if (write(fd
, buf
, count
) <= 0)
82 * Looks up a message in a message list by key.
84 * If the message is not found, returns the provided error message.
86 * Terminates when it hits a struct message that's all zeros.
88 * @param mz the message list
89 * @param kz the message key
90 * @param nf the message to return if not found
93 const char *lookup_msg(const struct message
*mz
, int kz
, const char *nf
)
95 static struct message nt
= {0};
96 const char *rz
= nf
? nf
: "(no message found)";
97 const struct message
*pnt
;
98 for (pnt
= mz
; memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
100 rz
= pnt
->str
? pnt
->str
: rz
;
106 /* For time string format. */
107 size_t quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
114 struct timeval clock
;
116 gettimeofday(&clock
, NULL
);
118 /* first, we update the cache if the time has changed */
119 if (cache
.last
!= clock
.tv_sec
) {
121 cache
.last
= clock
.tv_sec
;
122 tm
= localtime(&cache
.last
);
123 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
124 "%Y/%m/%d %H:%M:%S", tm
);
126 /* note: it's not worth caching the subsecond part, because
127 chances are that back-to-back calls are not sufficiently close
129 for the clock not to have ticked forward */
131 if (buflen
> cache
.len
) {
132 memcpy(buf
, cache
.buf
, cache
.len
);
133 if ((timestamp_precision
> 0)
134 && (buflen
> cache
.len
+ 1 + timestamp_precision
)) {
135 /* should we worry about locale issues? */
136 static const int divisor
[] = {0, 100000, 10000, 1000,
139 char *p
= buf
+ cache
.len
+ 1
140 + (prec
= timestamp_precision
);
143 /* this is unlikely to happen, but protect anyway */
148 clock
.tv_usec
/= divisor
[prec
];
150 *p
-- = '0' + (clock
.tv_usec
% 10);
152 } while (--prec
> 0);
154 return cache
.len
+ 1 + timestamp_precision
;
156 buf
[cache
.len
] = '\0';
164 /* Utility routine for current time printing. */
165 static void time_print(FILE *fp
, struct timestamp_control
*ctl
)
167 if (!ctl
->already_rendered
) {
168 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
170 ctl
->already_rendered
= 1;
172 fprintf(fp
, "%s ", ctl
->buf
);
176 /* va_list version of zlog. */
177 void vzlog(int priority
, const char *format
, va_list args
)
180 int original_errno
= errno
;
181 struct timestamp_control tsctl
;
182 tsctl
.already_rendered
= 0;
183 struct zlog
*zl
= zlog_default
;
185 /* When zlog_default is also NULL, use stderr for logging. */
188 time_print(stderr
, &tsctl
);
189 fprintf(stderr
, "%s: ", "unknown");
190 vfprintf(stderr
, format
, args
);
191 fprintf(stderr
, "\n");
194 /* In this case we return at here. */
195 errno
= original_errno
;
198 tsctl
.precision
= zl
->timestamp_precision
;
201 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
]) {
204 vsyslog(priority
| zlog_default
->facility
, format
, ac
);
209 sprintf(proto_str
, "%s[%d]: ", zl
->protoname
, zl
->instance
);
211 sprintf(proto_str
, "%s: ", zl
->protoname
);
214 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
) {
216 time_print(zl
->fp
, &tsctl
);
217 if (zl
->record_priority
)
218 fprintf(zl
->fp
, "%s: ", zlog_priority
[priority
]);
219 fprintf(zl
->fp
, "%s", proto_str
);
221 vfprintf(zl
->fp
, format
, ac
);
223 fprintf(zl
->fp
, "\n");
228 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
]) {
230 time_print(stdout
, &tsctl
);
231 if (zl
->record_priority
)
232 fprintf(stdout
, "%s: ", zlog_priority
[priority
]);
233 fprintf(stdout
, "%s", proto_str
);
235 vfprintf(stdout
, format
, ac
);
237 fprintf(stdout
, "\n");
241 /* Terminal monitor. */
242 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
243 vty_log((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
244 proto_str
, format
, &tsctl
, args
);
246 errno
= original_errno
;
249 int vzlog_test(int priority
)
251 struct zlog
*zl
= zlog_default
;
253 /* When zlog_default is also NULL, use stderr for logging. */
259 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
]) {
264 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
) {
269 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
]) {
273 /* Terminal monitor. */
274 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
280 static char *str_append(char *dst
, int len
, const char *src
)
282 while ((len
-- > 0) && *src
)
287 static char *num_append(char *s
, int len
, u_long x
)
293 return str_append(s
, len
, "0");
294 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
295 while (x
&& (t
> buf
)) {
296 *--t
= '0' + (x
% 10);
299 return str_append(s
, len
, t
);
302 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
303 static char *hex_append(char *s
, int len
, u_long x
)
309 return str_append(s
, len
, "0");
310 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
311 while (x
&& (t
> buf
)) {
313 *--t
= ((cc
< 10) ? ('0' + cc
) : ('a' + cc
- 10));
316 return str_append(s
, len
, t
);
320 /* Needs to be enhanced to support Solaris. */
321 static int syslog_connect(void)
328 struct sockaddr_un addr
;
330 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
332 addr
.sun_family
= AF_UNIX
;
334 #define SYSLOG_SOCKET_PATH _PATH_LOG
336 #define SYSLOG_SOCKET_PATH "/dev/log"
338 s
= str_append(addr
.sun_path
, sizeof(addr
.sun_path
),
340 #undef SYSLOG_SOCKET_PATH
342 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
350 static void syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
352 static int syslog_fd
= -1;
353 char buf
[sizeof("<1234567890>ripngd[1234567890]: ") + msglen
+ 50];
356 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
359 #define LOC s,buf+sizeof(buf)-s
361 s
= str_append(LOC
, "<");
362 s
= num_append(LOC
, priority
);
363 s
= str_append(LOC
, ">");
364 /* forget about the timestamp, too difficult in a signal handler */
365 s
= str_append(LOC
, zlog_default
->ident
);
366 if (zlog_default
->syslog_options
& LOG_PID
) {
367 s
= str_append(LOC
, "[");
368 s
= num_append(LOC
, getpid());
369 s
= str_append(LOC
, "]");
371 s
= str_append(LOC
, ": ");
372 s
= str_append(LOC
, msg
);
373 write_wrapper(syslog_fd
, buf
, s
- buf
);
377 static int open_crashlog(void)
379 #define CRASHLOG_PREFIX "/var/tmp/quagga."
380 #define CRASHLOG_SUFFIX "crashlog"
381 if (zlog_default
&& zlog_default
->ident
) {
382 /* Avoid strlen since it is not async-signal-safe. */
386 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
389 char buf
[sizeof(CRASHLOG_PREFIX
) + ilen
390 + sizeof(CRASHLOG_SUFFIX
) + 3];
392 #define LOC s,buf+sizeof(buf)-s
393 s
= str_append(LOC
, CRASHLOG_PREFIX
);
394 s
= str_append(LOC
, zlog_default
->ident
);
395 s
= str_append(LOC
, ".");
396 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
399 return open(buf
, O_WRONLY
| O_CREAT
| O_EXCL
,
403 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
,
404 O_WRONLY
| O_CREAT
| O_EXCL
, LOGFILE_MASK
);
405 #undef CRASHLOG_SUFFIX
406 #undef CRASHLOG_PREFIX
409 /* Note: the goal here is to use only async-signal-safe functions. */
410 void zlog_signal(int signo
, const char *action
413 siginfo_t
*siginfo
, void *program_counter
418 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
421 char *msgstart
= buf
;
422 #define LOC s,buf+sizeof(buf)-s
426 s
= str_append(LOC
, zlog_default
->protoname
);
431 s
= str_append(LOC
, "Received signal ");
432 s
= num_append(LOC
, signo
);
433 s
= str_append(LOC
, " at ");
434 s
= num_append(LOC
, now
);
436 s
= str_append(LOC
, " (si_addr 0x");
437 s
= hex_append(LOC
, (u_long
)(siginfo
->si_addr
));
438 if (program_counter
) {
439 s
= str_append(LOC
, ", PC 0x");
440 s
= hex_append(LOC
, (u_long
)program_counter
);
442 s
= str_append(LOC
, "); ");
443 #else /* SA_SIGINFO */
444 s
= str_append(LOC
, "; ");
445 #endif /* SA_SIGINFO */
446 s
= str_append(LOC
, action
);
447 if (s
< buf
+ sizeof(buf
))
450 /* N.B. implicit priority is most severe */
453 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
454 /* If no file logging configured, try to write to fallback log file. */
455 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
460 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
462 /* Remove trailing '\n' for monitor and syslog */
464 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
465 vty_log_fixed(buf
, s
- buf
);
466 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
467 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
472 zlog_backtrace_sigsafe(PRI
,
482 s
= str_append(LOC
, "no thread information available\n");
484 s
= str_append(LOC
, "in thread ");
485 s
= str_append(LOC
, thread_current
->funcname
);
486 s
= str_append(LOC
, " scheduled from ");
487 s
= str_append(LOC
, thread_current
->schedfrom
);
488 s
= str_append(LOC
, ":");
489 s
= num_append(LOC
, thread_current
->schedfrom_line
);
490 s
= str_append(LOC
, "\n");
493 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
494 /* If no file logging configured, try to write to fallback log file. */
500 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
502 /* Remove trailing '\n' for monitor and syslog */
504 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
505 vty_log_fixed(buf
, s
- buf
);
506 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
507 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
516 /* Log a backtrace using only async-signal-safe functions.
517 Needs to be enhanced to support syslog logging. */
518 void zlog_backtrace_sigsafe(int priority
, void *program_counter
)
520 #ifdef HAVE_STACK_TRACE
521 static const char pclabel
[] = "Program counter: ";
525 char *s
, **bt
= NULL
;
526 #define LOC s,buf+sizeof(buf)-s
528 #ifdef HAVE_GLIBC_BACKTRACE
529 size
= backtrace(array
, array_size(array
));
530 if (size
<= 0 || (size_t)size
> array_size(array
))
535 if (program_counter) { \
536 write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \
537 backtrace_symbols_fd(&program_counter, 1, FD); \
539 write_wrapper(FD, buf, s - buf); \
540 backtrace_symbols_fd(array, size, FD); \
542 #elif defined(HAVE_PRINTSTACK)
545 if (program_counter) \
546 write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \
547 write_wrapper((FD), buf, s - buf); \
550 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
553 s
= str_append(LOC
, "Backtrace for ");
554 s
= num_append(LOC
, size
);
555 s
= str_append(LOC
, " stack frames:\n");
557 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
562 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
564 /* Remove trailing '\n' for monitor and syslog */
566 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
567 vty_log_fixed(buf
, s
- buf
);
568 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
569 syslog_sigsafe(priority
| zlog_default
->facility
, buf
,
573 #ifdef HAVE_GLIBC_BACKTRACE
574 bt
= backtrace_symbols(array
, size
);
576 /* Just print the function addresses. */
577 for (i
= 0; i
< size
; i
++) {
580 s
= str_append(LOC
, bt
[i
]);
582 s
= str_append(LOC
, "[bt ");
583 s
= num_append(LOC
, i
);
584 s
= str_append(LOC
, "] 0x");
585 s
= hex_append(LOC
, (u_long
)(array
[i
]));
589 <= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
590 vty_log_fixed(buf
, s
- buf
);
592 <= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
605 #endif /* HAVE_STRACK_TRACE */
608 void zlog_backtrace(int priority
)
610 #ifndef HAVE_GLIBC_BACKTRACE
611 zlog(priority
, "No backtrace available on this platform.");
617 size
= backtrace(array
, array_size(array
));
618 if (size
<= 0 || (size_t)size
> array_size(array
)) {
620 "Cannot get backtrace, returned invalid # of frames %d "
621 "(valid range is between 1 and %lu)",
622 size
, (unsigned long)(array_size(array
)));
625 zlog(priority
, "Backtrace for %d stack frames:", size
);
626 if (!(strings
= backtrace_symbols(array
, size
))) {
627 zlog_err("Cannot get backtrace symbols (out of memory?)");
628 for (i
= 0; i
< size
; i
++)
629 zlog(priority
, "[bt %d] %p", i
, array
[i
]);
631 for (i
= 0; i
< size
; i
++)
632 zlog(priority
, "[bt %d] %s", i
, strings
[i
]);
635 #endif /* HAVE_GLIBC_BACKTRACE */
638 void zlog(int priority
, const char *format
, ...)
642 va_start(args
, format
);
643 vzlog(priority
, format
, args
);
647 #define ZLOG_FUNC(FUNCNAME, PRIORITY) \
648 void FUNCNAME(const char *format, ...) \
651 va_start(args, format); \
652 vzlog(PRIORITY, format, args); \
656 ZLOG_FUNC(zlog_err
, LOG_ERR
)
658 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
660 ZLOG_FUNC(zlog_info
, LOG_INFO
)
662 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
664 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
668 void zlog_thread_info(int log_level
)
672 "Current thread function %s, scheduled from "
674 thread_current
->funcname
, thread_current
->schedfrom
,
675 thread_current
->schedfrom_line
);
677 zlog(log_level
, "Current thread not known/applicable");
680 void _zlog_assert_failed(const char *assertion
, const char *file
,
681 unsigned int line
, const char *function
)
683 /* Force fallback file logging? */
684 if (zlog_default
&& !zlog_default
->fp
685 && ((logfile_fd
= open_crashlog()) >= 0)
686 && ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
687 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
688 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
689 assertion
, file
, line
, (function
? function
: "?"));
690 zlog_backtrace(LOG_CRIT
);
691 zlog_thread_info(LOG_CRIT
);
692 log_memstats_stderr("log");
696 void memory_oom(size_t size
, const char *name
)
699 "out of memory: failed to allocate %zu bytes for %s"
702 zlog_backtrace(LOG_ERR
);
706 /* Open log stream */
707 void openzlog(const char *progname
, const char *protoname
, u_short instance
,
708 int syslog_flags
, int syslog_facility
)
713 zl
= XCALLOC(MTYPE_ZLOG
, sizeof(struct zlog
));
715 zl
->ident
= progname
;
716 zl
->protoname
= protoname
;
717 zl
->instance
= instance
;
718 zl
->facility
= syslog_facility
;
719 zl
->syslog_options
= syslog_flags
;
721 /* Set default logging levels. */
722 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
723 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
724 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
725 zl
->default_lvl
= LOG_DEBUG
;
727 openlog(progname
, syslog_flags
, zl
->facility
);
733 struct zlog
*zl
= zlog_default
;
740 if (zl
->filename
!= NULL
)
741 XFREE(MTYPE_ZLOG
, zl
->filename
);
743 XFREE(MTYPE_ZLOG
, zl
);
747 const char *zlog_protoname(void)
749 return zlog_default
? zlog_default
->protoname
: "NONE";
752 /* Called from command.c. */
753 void zlog_set_level(zlog_dest_t dest
, int log_level
)
755 zlog_default
->maxlvl
[dest
] = log_level
;
758 int zlog_set_file(const char *filename
, int log_level
)
760 struct zlog
*zl
= zlog_default
;
764 /* There is opend file. */
768 oldumask
= umask(0777 & ~LOGFILE_MASK
);
769 fp
= fopen(filename
, "a");
775 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
776 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
778 logfile_fd
= fileno(fp
);
783 /* Reset opend file. */
784 int zlog_reset_file(void)
786 struct zlog
*zl
= zlog_default
;
792 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
795 XFREE(MTYPE_ZLOG
, zl
->filename
);
801 /* Reopen log file. */
802 int zlog_rotate(void)
804 struct zlog
*zl
= zlog_default
;
811 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
812 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
818 oldumask
= umask(0777 & ~LOGFILE_MASK
);
819 zl
->fp
= fopen(zl
->filename
, "a");
822 if (zl
->fp
== NULL
) {
824 "Log rotate failed: cannot open file %s for append: %s",
825 zl
->filename
, safe_strerror(save_errno
));
828 logfile_fd
= fileno(zl
->fp
);
829 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
835 /* Wrapper around strerror to handle case where it returns NULL. */
836 const char *safe_strerror(int errnum
)
838 const char *s
= strerror(errnum
);
839 return (s
!= NULL
) ? s
: "Unknown error";
842 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
843 static const struct zebra_desc_table command_types
[] = {
844 DESC_ENTRY(ZEBRA_INTERFACE_ADD
),
845 DESC_ENTRY(ZEBRA_INTERFACE_DELETE
),
846 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD
),
847 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE
),
848 DESC_ENTRY(ZEBRA_INTERFACE_UP
),
849 DESC_ENTRY(ZEBRA_INTERFACE_DOWN
),
850 DESC_ENTRY(ZEBRA_IPV4_ROUTE_ADD
),
851 DESC_ENTRY(ZEBRA_IPV4_ROUTE_DELETE
),
852 DESC_ENTRY(ZEBRA_IPV6_ROUTE_ADD
),
853 DESC_ENTRY(ZEBRA_IPV6_ROUTE_DELETE
),
854 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD
),
855 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE
),
856 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
857 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
858 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD
),
859 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE
),
860 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE
),
861 DESC_ENTRY(ZEBRA_HELLO
),
862 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER
),
863 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER
),
864 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE
),
865 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
866 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
867 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
868 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER
),
869 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER
),
870 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE
),
871 DESC_ENTRY(ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
),
872 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER
),
873 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER
),
874 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE
),
875 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY
),
876 DESC_ENTRY(ZEBRA_REDISTRIBUTE_IPV4_ADD
),
877 DESC_ENTRY(ZEBRA_REDISTRIBUTE_IPV4_DEL
),
878 DESC_ENTRY(ZEBRA_REDISTRIBUTE_IPV6_ADD
),
879 DESC_ENTRY(ZEBRA_REDISTRIBUTE_IPV6_DEL
),
880 DESC_ENTRY(ZEBRA_VRF_UNREGISTER
),
881 DESC_ENTRY(ZEBRA_VRF_ADD
),
882 DESC_ENTRY(ZEBRA_VRF_DELETE
),
883 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE
),
884 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER
),
885 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV
),
886 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV
),
887 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
888 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS
),
889 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD
),
890 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE
),
891 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_ADD
),
892 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_DELETE
),
893 DESC_ENTRY(ZEBRA_IPV6_NEXTHOP_ADD
),
894 DESC_ENTRY(ZEBRA_IPV6_NEXTHOP_DELETE
),
895 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS
),
896 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT
),
897 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK
),
898 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK
),
899 DESC_ENTRY(ZEBRA_PW_ADD
),
900 DESC_ENTRY(ZEBRA_PW_DELETE
),
901 DESC_ENTRY(ZEBRA_PW_SET
),
902 DESC_ENTRY(ZEBRA_PW_UNSET
),
903 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE
),
907 static const struct zebra_desc_table unknown
= {0, "unknown", '?'};
909 static const struct zebra_desc_table
*zroute_lookup(u_int zroute
)
913 if (zroute
>= array_size(route_types
)) {
914 zlog_err("unknown zebra route type: %u", zroute
);
917 if (zroute
== route_types
[zroute
].type
)
918 return &route_types
[zroute
];
919 for (i
= 0; i
< array_size(route_types
); i
++) {
920 if (zroute
== route_types
[i
].type
) {
922 "internal error: route type table out of order "
923 "while searching for %u, please notify developers",
925 return &route_types
[i
];
928 zlog_err("internal error: cannot find route type %u in table!", zroute
);
932 const char *zebra_route_string(u_int zroute
)
934 return zroute_lookup(zroute
)->string
;
937 char zebra_route_char(u_int zroute
)
939 return zroute_lookup(zroute
)->chr
;
942 const char *zserv_command_string(unsigned int command
)
944 if (command
>= array_size(command_types
)) {
945 zlog_err("unknown zserv command type: %u", command
);
946 return unknown
.string
;
948 return command_types
[command
].string
;
951 int proto_name2num(const char *s
)
955 for (i
= 0; i
< array_size(route_types
); ++i
)
956 if (strcasecmp(s
, route_types
[i
].string
) == 0)
957 return route_types
[i
].type
;
961 int proto_redistnum(int afi
, const char *s
)
967 if (strmatch(s
, "kernel"))
968 return ZEBRA_ROUTE_KERNEL
;
969 else if (strmatch(s
, "connected"))
970 return ZEBRA_ROUTE_CONNECT
;
971 else if (strmatch(s
, "static"))
972 return ZEBRA_ROUTE_STATIC
;
973 else if (strmatch(s
, "rip"))
974 return ZEBRA_ROUTE_RIP
;
975 else if (strmatch(s
, "ospf"))
976 return ZEBRA_ROUTE_OSPF
;
977 else if (strmatch(s
, "isis"))
978 return ZEBRA_ROUTE_ISIS
;
979 else if (strmatch(s
, "bgp"))
980 return ZEBRA_ROUTE_BGP
;
981 else if (strmatch(s
, "table"))
982 return ZEBRA_ROUTE_TABLE
;
983 else if (strmatch(s
, "vnc"))
984 return ZEBRA_ROUTE_VNC
;
985 else if (strmatch(s
, "vnc-direct"))
986 return ZEBRA_ROUTE_VNC_DIRECT
;
987 else if (strmatch(s
, "nhrp"))
988 return ZEBRA_ROUTE_NHRP
;
990 if (afi
== AFI_IP6
) {
991 if (strmatch(s
, "kernel"))
992 return ZEBRA_ROUTE_KERNEL
;
993 else if (strmatch(s
, "connected"))
994 return ZEBRA_ROUTE_CONNECT
;
995 else if (strmatch(s
, "static"))
996 return ZEBRA_ROUTE_STATIC
;
997 else if (strmatch(s
, "ripng"))
998 return ZEBRA_ROUTE_RIPNG
;
999 else if (strmatch(s
, "ospf6"))
1000 return ZEBRA_ROUTE_OSPF6
;
1001 else if (strmatch(s
, "isis"))
1002 return ZEBRA_ROUTE_ISIS
;
1003 else if (strmatch(s
, "bgp"))
1004 return ZEBRA_ROUTE_BGP
;
1005 else if (strmatch(s
, "table"))
1006 return ZEBRA_ROUTE_TABLE
;
1007 else if (strmatch(s
, "vnc"))
1008 return ZEBRA_ROUTE_VNC
;
1009 else if (strmatch(s
, "vnc-direct"))
1010 return ZEBRA_ROUTE_VNC_DIRECT
;
1011 else if (strmatch(s
, "nhrp"))
1012 return ZEBRA_ROUTE_NHRP
;
1017 void zlog_hexdump(const void *mem
, unsigned int len
)
1019 unsigned long i
= 0;
1021 unsigned int columns
= 8;
1022 char buf
[(len
* 4) + ((len
/ 4) * 20) + 30];
1025 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0);
1028 if (i
% columns
== 0)
1029 s
+= sprintf(s
, "0x%016lx: ", (unsigned long)mem
+ i
);
1031 /* print hex data */
1033 s
+= sprintf(s
, "%02x ", 0xFF & ((const char *)mem
)[i
]);
1035 /* end of block, just aligning for ASCII dump */
1037 s
+= sprintf(s
, " ");
1039 /* print ASCII dump */
1040 if (i
% columns
== (columns
- 1)) {
1041 for (j
= i
- (columns
- 1); j
<= i
; j
++) {
1042 if (j
>= len
) /* end of block, not really
1044 s
+= sprintf(s
, " ");
1047 isprint((int)((const char *)mem
)
1048 [j
])) /* printable char
1052 0xFF & ((const char *)mem
)[j
]);
1054 else /* other char */
1055 s
+= sprintf(s
, ".");
1057 s
+= sprintf(s
, "\n");
1060 zlog_debug("\n%s", buf
);
1063 const char *zlog_sanitize(char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1065 const char *inbuf
= in
;
1066 char *pos
= buf
, *end
= buf
+ bufsz
;
1067 const char *iend
= inbuf
+ inlen
;
1069 memset(buf
, 0, bufsz
);
1070 for (; inbuf
< iend
; inbuf
++) {
1071 /* don't write partial escape sequence */
1076 snprintf(pos
, end
- pos
, "\\n");
1077 else if (*inbuf
== '\r')
1078 snprintf(pos
, end
- pos
, "\\r");
1079 else if (*inbuf
== '\t')
1080 snprintf(pos
, end
- pos
, "\\t");
1081 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1082 snprintf(pos
, end
- pos
, "\\x%02hhx", *inbuf
);