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
[] = {
46 "emergencies", "alerts", "critical", "errors", "warnings",
47 "notifications", "informational", "debugging", NULL
,
53 * glibc has declared that the return value from write *must* not be
55 * gcc see's this problem and issues a warning for the line.
57 * Why is this a big deal you say? Because both of them are right
58 * and if you have -Werror enabled then all calls to write
59 * generate a build error and the build stops.
61 * clang has helpfully allowed this construct:
63 * to tell the compiler yeah I know it has a return value
64 * I don't care about it at this time.
65 * gcc doesn't have this ability.
67 * This code was written such that it didn't care about the
68 * return value from write. At this time do I want
69 * to go through and fix and test this code for correctness.
70 * So just wrapper the bad behavior and move on.
72 static void write_wrapper(int fd
, const void *buf
, size_t count
)
74 if (write(fd
, buf
, count
) <= 0)
81 * Looks up a message in a message list by key.
83 * If the message is not found, returns the provided error message.
85 * Terminates when it hits a struct message that's all zeros.
87 * @param mz the message list
88 * @param kz the message key
89 * @param nf the message to return if not found
92 const char *lookup_msg(const struct message
*mz
, int kz
, const char *nf
)
94 static struct message nt
= {0};
95 const char *rz
= nf
? nf
: "(no message found)";
96 const struct message
*pnt
;
97 for (pnt
= mz
; memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
99 rz
= pnt
->str
? pnt
->str
: rz
;
105 /* For time string format. */
106 size_t quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
113 struct timeval clock
;
115 gettimeofday(&clock
, NULL
);
117 /* first, we update the cache if the time has changed */
118 if (cache
.last
!= clock
.tv_sec
) {
120 cache
.last
= clock
.tv_sec
;
121 tm
= localtime(&cache
.last
);
122 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
123 "%Y/%m/%d %H:%M:%S", tm
);
125 /* note: it's not worth caching the subsecond part, because
126 chances are that back-to-back calls are not sufficiently close
128 for the clock not to have ticked forward */
130 if (buflen
> cache
.len
) {
131 memcpy(buf
, cache
.buf
, cache
.len
);
132 if ((timestamp_precision
> 0)
133 && (buflen
> cache
.len
+ 1 + timestamp_precision
)) {
134 /* should we worry about locale issues? */
135 static const int divisor
[] = {0, 100000, 10000, 1000,
138 char *p
= buf
+ cache
.len
+ 1
139 + (prec
= timestamp_precision
);
142 /* this is unlikely to happen, but protect anyway */
147 clock
.tv_usec
/= divisor
[prec
];
149 *p
-- = '0' + (clock
.tv_usec
% 10);
151 } while (--prec
> 0);
153 return cache
.len
+ 1 + timestamp_precision
;
155 buf
[cache
.len
] = '\0';
163 /* Utility routine for current time printing. */
164 static void time_print(FILE *fp
, struct timestamp_control
*ctl
)
166 if (!ctl
->already_rendered
) {
167 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
169 ctl
->already_rendered
= 1;
171 fprintf(fp
, "%s ", ctl
->buf
);
175 /* va_list version of zlog. */
176 void vzlog(int priority
, const char *format
, va_list args
)
179 int original_errno
= errno
;
180 struct timestamp_control tsctl
;
181 tsctl
.already_rendered
= 0;
182 struct zlog
*zl
= zlog_default
;
184 /* When zlog_default is also NULL, use stderr for logging. */
187 time_print(stderr
, &tsctl
);
188 fprintf(stderr
, "%s: ", "unknown");
189 vfprintf(stderr
, format
, args
);
190 fprintf(stderr
, "\n");
193 /* In this case we return at here. */
194 errno
= original_errno
;
197 tsctl
.precision
= zl
->timestamp_precision
;
200 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
]) {
203 vsyslog(priority
| zlog_default
->facility
, format
, ac
);
208 sprintf(proto_str
, "%s[%d]: ", zl
->protoname
, zl
->instance
);
210 sprintf(proto_str
, "%s: ", zl
->protoname
);
213 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
) {
215 time_print(zl
->fp
, &tsctl
);
216 if (zl
->record_priority
)
217 fprintf(zl
->fp
, "%s: ", zlog_priority
[priority
]);
218 fprintf(zl
->fp
, "%s", proto_str
);
220 vfprintf(zl
->fp
, format
, ac
);
222 fprintf(zl
->fp
, "\n");
227 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
]) {
229 time_print(stdout
, &tsctl
);
230 if (zl
->record_priority
)
231 fprintf(stdout
, "%s: ", zlog_priority
[priority
]);
232 fprintf(stdout
, "%s", proto_str
);
234 vfprintf(stdout
, format
, ac
);
236 fprintf(stdout
, "\n");
240 /* Terminal monitor. */
241 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
242 vty_log((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
243 proto_str
, format
, &tsctl
, args
);
245 errno
= original_errno
;
248 int vzlog_test(int priority
)
250 struct zlog
*zl
= zlog_default
;
252 /* When zlog_default is also NULL, use stderr for logging. */
258 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
]) {
263 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
) {
268 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
]) {
272 /* Terminal monitor. */
273 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
279 static char *str_append(char *dst
, int len
, const char *src
)
281 while ((len
-- > 0) && *src
)
286 static char *num_append(char *s
, int len
, u_long x
)
292 return str_append(s
, len
, "0");
293 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
294 while (x
&& (t
> buf
)) {
295 *--t
= '0' + (x
% 10);
298 return str_append(s
, len
, t
);
301 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
302 static char *hex_append(char *s
, int len
, u_long x
)
308 return str_append(s
, len
, "0");
309 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
310 while (x
&& (t
> buf
)) {
312 *--t
= ((cc
< 10) ? ('0' + cc
) : ('a' + cc
- 10));
315 return str_append(s
, len
, t
);
319 /* Needs to be enhanced to support Solaris. */
320 static int syslog_connect(void)
327 struct sockaddr_un addr
;
329 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
331 addr
.sun_family
= AF_UNIX
;
333 #define SYSLOG_SOCKET_PATH _PATH_LOG
335 #define SYSLOG_SOCKET_PATH "/dev/log"
337 s
= str_append(addr
.sun_path
, sizeof(addr
.sun_path
),
339 #undef SYSLOG_SOCKET_PATH
341 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
349 static void syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
351 static int syslog_fd
= -1;
352 char buf
[sizeof("<1234567890>ripngd[1234567890]: ") + msglen
+ 50];
355 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
358 #define LOC s,buf+sizeof(buf)-s
360 s
= str_append(LOC
, "<");
361 s
= num_append(LOC
, priority
);
362 s
= str_append(LOC
, ">");
363 /* forget about the timestamp, too difficult in a signal handler */
364 s
= str_append(LOC
, zlog_default
->ident
);
365 if (zlog_default
->syslog_options
& LOG_PID
) {
366 s
= str_append(LOC
, "[");
367 s
= num_append(LOC
, getpid());
368 s
= str_append(LOC
, "]");
370 s
= str_append(LOC
, ": ");
371 s
= str_append(LOC
, msg
);
372 write_wrapper(syslog_fd
, buf
, s
- buf
);
376 static int open_crashlog(void)
378 #define CRASHLOG_PREFIX "/var/tmp/quagga."
379 #define CRASHLOG_SUFFIX "crashlog"
380 if (zlog_default
&& zlog_default
->ident
) {
381 /* Avoid strlen since it is not async-signal-safe. */
385 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
388 char buf
[sizeof(CRASHLOG_PREFIX
) + ilen
389 + sizeof(CRASHLOG_SUFFIX
) + 3];
391 #define LOC s,buf+sizeof(buf)-s
392 s
= str_append(LOC
, CRASHLOG_PREFIX
);
393 s
= str_append(LOC
, zlog_default
->ident
);
394 s
= str_append(LOC
, ".");
395 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
398 return open(buf
, O_WRONLY
| O_CREAT
| O_EXCL
,
402 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
,
403 O_WRONLY
| O_CREAT
| O_EXCL
, LOGFILE_MASK
);
404 #undef CRASHLOG_SUFFIX
405 #undef CRASHLOG_PREFIX
408 /* Note: the goal here is to use only async-signal-safe functions. */
409 void zlog_signal(int signo
, const char *action
412 siginfo_t
*siginfo
, void *program_counter
417 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
420 char *msgstart
= buf
;
421 #define LOC s,buf+sizeof(buf)-s
425 s
= str_append(LOC
, zlog_default
->protoname
);
430 s
= str_append(LOC
, "Received signal ");
431 s
= num_append(LOC
, signo
);
432 s
= str_append(LOC
, " at ");
433 s
= num_append(LOC
, now
);
435 s
= str_append(LOC
, " (si_addr 0x");
436 s
= hex_append(LOC
, (u_long
)(siginfo
->si_addr
));
437 if (program_counter
) {
438 s
= str_append(LOC
, ", PC 0x");
439 s
= hex_append(LOC
, (u_long
)program_counter
);
441 s
= str_append(LOC
, "); ");
442 #else /* SA_SIGINFO */
443 s
= str_append(LOC
, "; ");
444 #endif /* SA_SIGINFO */
445 s
= str_append(LOC
, action
);
446 if (s
< buf
+ sizeof(buf
))
449 /* N.B. implicit priority is most severe */
452 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
453 /* If no file logging configured, try to write to fallback log file. */
454 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
459 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
461 /* Remove trailing '\n' for monitor and syslog */
463 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
464 vty_log_fixed(buf
, s
- buf
);
465 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
466 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
471 zlog_backtrace_sigsafe(PRI
,
481 tc
= pthread_getspecific(thread_current
);
483 s
= str_append(LOC
, "no thread information available\n");
485 s
= str_append(LOC
, "in thread ");
486 s
= str_append(LOC
, tc
->funcname
);
487 s
= str_append(LOC
, " scheduled from ");
488 s
= str_append(LOC
, tc
->schedfrom
);
489 s
= str_append(LOC
, ":");
490 s
= num_append(LOC
, tc
->schedfrom_line
);
491 s
= str_append(LOC
, "\n");
494 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
495 /* If no file logging configured, try to write to fallback log file. */
501 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
503 /* Remove trailing '\n' for monitor and syslog */
505 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
506 vty_log_fixed(buf
, s
- buf
);
507 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
508 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
517 /* Log a backtrace using only async-signal-safe functions.
518 Needs to be enhanced to support syslog logging. */
519 void zlog_backtrace_sigsafe(int priority
, void *program_counter
)
521 #ifdef HAVE_STACK_TRACE
522 static const char pclabel
[] = "Program counter: ";
526 char *s
, **bt
= NULL
;
527 #define LOC s,buf+sizeof(buf)-s
529 #ifdef HAVE_GLIBC_BACKTRACE
530 size
= backtrace(array
, array_size(array
));
531 if (size
<= 0 || (size_t)size
> array_size(array
))
536 if (program_counter) { \
537 write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \
538 backtrace_symbols_fd(&program_counter, 1, FD); \
540 write_wrapper(FD, buf, s - buf); \
541 backtrace_symbols_fd(array, size, FD); \
543 #elif defined(HAVE_PRINTSTACK)
546 if (program_counter) \
547 write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \
548 write_wrapper((FD), buf, s - buf); \
551 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
554 s
= str_append(LOC
, "Backtrace for ");
555 s
= num_append(LOC
, size
);
556 s
= str_append(LOC
, " stack frames:\n");
558 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
563 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
565 /* Remove trailing '\n' for monitor and syslog */
567 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
568 vty_log_fixed(buf
, s
- buf
);
569 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
570 syslog_sigsafe(priority
| zlog_default
->facility
, buf
,
574 #ifdef HAVE_GLIBC_BACKTRACE
575 bt
= backtrace_symbols(array
, size
);
577 /* Just print the function addresses. */
578 for (i
= 0; i
< size
; i
++) {
581 s
= str_append(LOC
, bt
[i
]);
583 s
= str_append(LOC
, "[bt ");
584 s
= num_append(LOC
, i
);
585 s
= str_append(LOC
, "] 0x");
586 s
= hex_append(LOC
, (u_long
)(array
[i
]));
590 <= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
591 vty_log_fixed(buf
, s
- buf
);
593 <= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
606 #endif /* HAVE_STRACK_TRACE */
609 void zlog_backtrace(int priority
)
611 #ifndef HAVE_GLIBC_BACKTRACE
612 zlog(priority
, "No backtrace available on this platform.");
618 size
= backtrace(array
, array_size(array
));
619 if (size
<= 0 || (size_t)size
> array_size(array
)) {
621 "Cannot get backtrace, returned invalid # of frames %d "
622 "(valid range is between 1 and %lu)",
623 size
, (unsigned long)(array_size(array
)));
626 zlog(priority
, "Backtrace for %d stack frames:", size
);
627 if (!(strings
= backtrace_symbols(array
, size
))) {
628 zlog_err("Cannot get backtrace symbols (out of memory?)");
629 for (i
= 0; i
< size
; i
++)
630 zlog(priority
, "[bt %d] %p", i
, array
[i
]);
632 for (i
= 0; i
< size
; i
++)
633 zlog(priority
, "[bt %d] %s", i
, strings
[i
]);
636 #endif /* HAVE_GLIBC_BACKTRACE */
639 void zlog(int priority
, const char *format
, ...)
643 va_start(args
, format
);
644 vzlog(priority
, format
, args
);
648 #define ZLOG_FUNC(FUNCNAME, PRIORITY) \
649 void FUNCNAME(const char *format, ...) \
652 va_start(args, format); \
653 vzlog(PRIORITY, format, args); \
657 ZLOG_FUNC(zlog_err
, LOG_ERR
)
659 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
661 ZLOG_FUNC(zlog_info
, LOG_INFO
)
663 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
665 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
669 void zlog_thread_info(int log_level
)
672 tc
= pthread_getspecific(thread_current
);
676 "Current thread function %s, scheduled from "
678 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
680 zlog(log_level
, "Current thread not known/applicable");
683 void _zlog_assert_failed(const char *assertion
, const char *file
,
684 unsigned int line
, const char *function
)
686 /* Force fallback file logging? */
687 if (zlog_default
&& !zlog_default
->fp
688 && ((logfile_fd
= open_crashlog()) >= 0)
689 && ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
690 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
691 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
692 assertion
, file
, line
, (function
? function
: "?"));
693 zlog_backtrace(LOG_CRIT
);
694 zlog_thread_info(LOG_CRIT
);
695 log_memstats_stderr("log");
699 void memory_oom(size_t size
, const char *name
)
702 "out of memory: failed to allocate %zu bytes for %s"
705 zlog_backtrace(LOG_ERR
);
709 /* Open log stream */
710 void openzlog(const char *progname
, const char *protoname
, u_short instance
,
711 int syslog_flags
, int syslog_facility
)
716 zl
= XCALLOC(MTYPE_ZLOG
, sizeof(struct zlog
));
718 zl
->ident
= progname
;
719 zl
->protoname
= protoname
;
720 zl
->instance
= instance
;
721 zl
->facility
= syslog_facility
;
722 zl
->syslog_options
= syslog_flags
;
724 /* Set default logging levels. */
725 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
726 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
727 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
728 zl
->default_lvl
= LOG_DEBUG
;
730 openlog(progname
, syslog_flags
, zl
->facility
);
733 #ifdef HAVE_GLIBC_BACKTRACE
734 /* work around backtrace() using lazily resolved dynamically linked
735 * symbols, which will otherwise cause funny breakage in the SEGV
737 * (particularly, the dynamic linker can call malloc(), which uses locks
738 * in programs linked with -pthread, thus can deadlock.) */
740 backtrace(bt
, array_size(bt
));
741 free(backtrace_symbols(bt
, 0));
742 backtrace_symbols_fd(bt
, 0, 0);
748 struct zlog
*zl
= zlog_default
;
755 if (zl
->filename
!= NULL
)
756 XFREE(MTYPE_ZLOG
, zl
->filename
);
758 XFREE(MTYPE_ZLOG
, zl
);
762 /* Called from command.c. */
763 void zlog_set_level(zlog_dest_t dest
, int log_level
)
765 zlog_default
->maxlvl
[dest
] = log_level
;
768 int zlog_set_file(const char *filename
, int log_level
)
770 struct zlog
*zl
= zlog_default
;
774 /* There is opend file. */
778 oldumask
= umask(0777 & ~LOGFILE_MASK
);
779 fp
= fopen(filename
, "a");
785 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
786 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
788 logfile_fd
= fileno(fp
);
793 /* Reset opend file. */
794 int zlog_reset_file(void)
796 struct zlog
*zl
= zlog_default
;
802 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
805 XFREE(MTYPE_ZLOG
, zl
->filename
);
811 /* Reopen log file. */
812 int zlog_rotate(void)
814 struct zlog
*zl
= zlog_default
;
821 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
822 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
828 oldumask
= umask(0777 & ~LOGFILE_MASK
);
829 zl
->fp
= fopen(zl
->filename
, "a");
832 if (zl
->fp
== NULL
) {
834 "Log rotate failed: cannot open file %s for append: %s",
835 zl
->filename
, safe_strerror(save_errno
));
838 logfile_fd
= fileno(zl
->fp
);
839 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
845 /* Wrapper around strerror to handle case where it returns NULL. */
846 const char *safe_strerror(int errnum
)
848 const char *s
= strerror(errnum
);
849 return (s
!= NULL
) ? s
: "Unknown error";
852 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
853 static const struct zebra_desc_table command_types
[] = {
854 DESC_ENTRY(ZEBRA_INTERFACE_ADD
),
855 DESC_ENTRY(ZEBRA_INTERFACE_DELETE
),
856 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD
),
857 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE
),
858 DESC_ENTRY(ZEBRA_INTERFACE_UP
),
859 DESC_ENTRY(ZEBRA_INTERFACE_DOWN
),
860 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER
),
861 DESC_ENTRY(ZEBRA_IPV4_ROUTE_ADD
),
862 DESC_ENTRY(ZEBRA_IPV4_ROUTE_DELETE
),
863 DESC_ENTRY(ZEBRA_IPV6_ROUTE_ADD
),
864 DESC_ENTRY(ZEBRA_IPV6_ROUTE_DELETE
),
865 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD
),
866 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE
),
867 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
868 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
869 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD
),
870 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE
),
871 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE
),
872 DESC_ENTRY(ZEBRA_HELLO
),
873 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER
),
874 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER
),
875 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE
),
876 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
877 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
878 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
879 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER
),
880 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER
),
881 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE
),
882 DESC_ENTRY(ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
),
883 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER
),
884 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER
),
885 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE
),
886 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY
),
887 DESC_ENTRY(ZEBRA_REDISTRIBUTE_IPV4_ADD
),
888 DESC_ENTRY(ZEBRA_REDISTRIBUTE_IPV4_DEL
),
889 DESC_ENTRY(ZEBRA_REDISTRIBUTE_IPV6_ADD
),
890 DESC_ENTRY(ZEBRA_REDISTRIBUTE_IPV6_DEL
),
891 DESC_ENTRY(ZEBRA_VRF_UNREGISTER
),
892 DESC_ENTRY(ZEBRA_VRF_ADD
),
893 DESC_ENTRY(ZEBRA_VRF_DELETE
),
894 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE
),
895 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER
),
896 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV
),
897 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV
),
898 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
899 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS
),
900 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD
),
901 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE
),
902 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_ADD
),
903 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_DELETE
),
904 DESC_ENTRY(ZEBRA_IPV6_NEXTHOP_ADD
),
905 DESC_ENTRY(ZEBRA_IPV6_NEXTHOP_DELETE
),
906 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS
),
907 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT
),
908 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK
),
909 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK
),
910 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI
),
911 DESC_ENTRY(ZEBRA_VNI_ADD
),
912 DESC_ENTRY(ZEBRA_VNI_DEL
),
913 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD
),
914 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL
),
915 DESC_ENTRY(ZEBRA_MACIP_ADD
),
916 DESC_ENTRY(ZEBRA_MACIP_DEL
),
917 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD
),
918 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL
),
922 static const struct zebra_desc_table unknown
= {0, "unknown", '?'};
924 static const struct zebra_desc_table
*zroute_lookup(u_int zroute
)
928 if (zroute
>= array_size(route_types
)) {
929 zlog_err("unknown zebra route type: %u", zroute
);
932 if (zroute
== route_types
[zroute
].type
)
933 return &route_types
[zroute
];
934 for (i
= 0; i
< array_size(route_types
); i
++) {
935 if (zroute
== route_types
[i
].type
) {
937 "internal error: route type table out of order "
938 "while searching for %u, please notify developers",
940 return &route_types
[i
];
943 zlog_err("internal error: cannot find route type %u in table!", zroute
);
947 const char *zebra_route_string(u_int zroute
)
949 return zroute_lookup(zroute
)->string
;
952 char zebra_route_char(u_int zroute
)
954 return zroute_lookup(zroute
)->chr
;
957 const char *zserv_command_string(unsigned int command
)
959 if (command
>= array_size(command_types
)) {
960 zlog_err("unknown zserv command type: %u", command
);
961 return unknown
.string
;
963 return command_types
[command
].string
;
966 int proto_name2num(const char *s
)
970 for (i
= 0; i
< array_size(route_types
); ++i
)
971 if (strcasecmp(s
, route_types
[i
].string
) == 0)
972 return route_types
[i
].type
;
976 int proto_redistnum(int afi
, const char *s
)
982 if (strmatch(s
, "kernel"))
983 return ZEBRA_ROUTE_KERNEL
;
984 else if (strmatch(s
, "connected"))
985 return ZEBRA_ROUTE_CONNECT
;
986 else if (strmatch(s
, "static"))
987 return ZEBRA_ROUTE_STATIC
;
988 else if (strmatch(s
, "rip"))
989 return ZEBRA_ROUTE_RIP
;
990 else if (strmatch(s
, "eigrp"))
991 return ZEBRA_ROUTE_EIGRP
;
992 else if (strmatch(s
, "ospf"))
993 return ZEBRA_ROUTE_OSPF
;
994 else if (strmatch(s
, "isis"))
995 return ZEBRA_ROUTE_ISIS
;
996 else if (strmatch(s
, "bgp"))
997 return ZEBRA_ROUTE_BGP
;
998 else if (strmatch(s
, "table"))
999 return ZEBRA_ROUTE_TABLE
;
1000 else if (strmatch(s
, "vnc"))
1001 return ZEBRA_ROUTE_VNC
;
1002 else if (strmatch(s
, "vnc-direct"))
1003 return ZEBRA_ROUTE_VNC_DIRECT
;
1004 else if (strmatch(s
, "nhrp"))
1005 return ZEBRA_ROUTE_NHRP
;
1006 else if (strmatch(s
, "babel"))
1007 return ZEBRA_ROUTE_BABEL
;
1009 if (afi
== AFI_IP6
) {
1010 if (strmatch(s
, "kernel"))
1011 return ZEBRA_ROUTE_KERNEL
;
1012 else if (strmatch(s
, "connected"))
1013 return ZEBRA_ROUTE_CONNECT
;
1014 else if (strmatch(s
, "static"))
1015 return ZEBRA_ROUTE_STATIC
;
1016 else if (strmatch(s
, "ripng"))
1017 return ZEBRA_ROUTE_RIPNG
;
1018 else if (strmatch(s
, "ospf6"))
1019 return ZEBRA_ROUTE_OSPF6
;
1020 else if (strmatch(s
, "isis"))
1021 return ZEBRA_ROUTE_ISIS
;
1022 else if (strmatch(s
, "bgp"))
1023 return ZEBRA_ROUTE_BGP
;
1024 else if (strmatch(s
, "table"))
1025 return ZEBRA_ROUTE_TABLE
;
1026 else if (strmatch(s
, "vnc"))
1027 return ZEBRA_ROUTE_VNC
;
1028 else if (strmatch(s
, "vnc-direct"))
1029 return ZEBRA_ROUTE_VNC_DIRECT
;
1030 else if (strmatch(s
, "nhrp"))
1031 return ZEBRA_ROUTE_NHRP
;
1032 else if (strmatch(s
, "babel"))
1033 return ZEBRA_ROUTE_BABEL
;
1038 void zlog_hexdump(const void *mem
, unsigned int len
)
1040 unsigned long i
= 0;
1042 unsigned int columns
= 8;
1043 char buf
[(len
* 4) + ((len
/ 4) * 20) + 30];
1046 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0);
1049 if (i
% columns
== 0)
1050 s
+= sprintf(s
, "0x%016lx: ", (unsigned long)mem
+ i
);
1052 /* print hex data */
1054 s
+= sprintf(s
, "%02x ", 0xFF & ((const char *)mem
)[i
]);
1056 /* end of block, just aligning for ASCII dump */
1058 s
+= sprintf(s
, " ");
1060 /* print ASCII dump */
1061 if (i
% columns
== (columns
- 1)) {
1062 for (j
= i
- (columns
- 1); j
<= i
; j
++) {
1063 if (j
>= len
) /* end of block, not really
1065 s
+= sprintf(s
, " ");
1068 isprint((int)((const char *)mem
)
1069 [j
])) /* printable char
1073 0xFF & ((const char *)mem
)[j
]);
1075 else /* other char */
1076 s
+= sprintf(s
, ".");
1078 s
+= sprintf(s
, "\n");
1081 zlog_debug("\n%s", buf
);
1084 const char *zlog_sanitize(char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1086 const char *inbuf
= in
;
1087 char *pos
= buf
, *end
= buf
+ bufsz
;
1088 const char *iend
= inbuf
+ inlen
;
1090 memset(buf
, 0, bufsz
);
1091 for (; inbuf
< iend
; inbuf
++) {
1092 /* don't write partial escape sequence */
1097 snprintf(pos
, end
- pos
, "\\n");
1098 else if (*inbuf
== '\r')
1099 snprintf(pos
, end
- pos
, "\\r");
1100 else if (*inbuf
== '\t')
1101 snprintf(pos
, end
- pos
, "\\t");
1102 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1103 snprintf(pos
, end
- pos
, "\\x%02hhx", *inbuf
);