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 QUAGGA_DEFINE_DESC_TABLE
33 /* for printstack on solaris */
34 #ifdef HAVE_UCONTEXT_H
38 DEFINE_MTYPE_STATIC(LIB
, ZLOG
, "Logging")
40 static int logfile_fd
= -1; /* Used in signal handler. */
42 struct zlog
*zlog_default
= NULL
;
44 const char *zlog_proto_names
[] =
61 const char *zlog_priority
[] =
77 * glibc has declared that the return value from write *must* not be
79 * gcc see's this problem and issues a warning for the line.
81 * Why is this a big deal you say? Because both of them are right
82 * and if you have -Werror enabled then all calls to write
83 * generate a build error and the build stops.
85 * clang has helpfully allowed this construct:
87 * to tell the compiler yeah I know it has a return value
88 * I don't care about it at this time.
89 * gcc doesn't have this ability.
91 * This code was written such that it didn't care about the
92 * return value from write. At this time do I want
93 * to go through and fix and test this code for correctness.
94 * So just wrapper the bad behavior and move on.
96 static void write_wrapper (int fd
, const void *buf
, size_t count
)
98 if (write (fd
, buf
, count
) <= 0)
104 /* For time string format. */
107 quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
114 struct timeval clock
;
116 /* would it be sufficient to use global 'recent_time' here? I fear not... */
117 gettimeofday(&clock
, NULL
);
119 /* first, we update the cache if the time has changed */
120 if (cache
.last
!= clock
.tv_sec
)
123 cache
.last
= clock
.tv_sec
;
124 tm
= localtime(&cache
.last
);
125 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
126 "%Y/%m/%d %H:%M:%S", tm
);
128 /* note: it's not worth caching the subsecond part, because
129 chances are that back-to-back calls are not sufficiently close together
130 for the clock not to have ticked forward */
132 if (buflen
> cache
.len
)
134 memcpy(buf
, cache
.buf
, cache
.len
);
135 if ((timestamp_precision
> 0) &&
136 (buflen
> cache
.len
+1+timestamp_precision
))
138 /* should we worry about locale issues? */
139 static const int divisor
[] = {0, 100000, 10000, 1000, 100, 10, 1};
141 char *p
= buf
+cache
.len
+1+(prec
= timestamp_precision
);
144 /* this is unlikely to happen, but protect anyway */
149 clock
.tv_usec
/= divisor
[prec
];
152 *p
-- = '0'+(clock
.tv_usec
% 10);
157 return cache
.len
+1+timestamp_precision
;
159 buf
[cache
.len
] = '\0';
167 /* Utility routine for current time printing. */
169 time_print(FILE *fp
, struct timestamp_control
*ctl
)
171 if (!ctl
->already_rendered
)
173 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
174 ctl
->already_rendered
= 1;
176 fprintf(fp
, "%s ", ctl
->buf
);
180 /* va_list version of zlog. */
182 vzlog (struct zlog
*zl
, int priority
, const char *format
, va_list args
)
185 int original_errno
= errno
;
186 struct timestamp_control tsctl
;
187 tsctl
.already_rendered
= 0;
189 /* If zlog is not specified, use default one. */
193 /* When zlog_default is also NULL, use stderr for logging. */
197 time_print(stderr
, &tsctl
);
198 fprintf (stderr
, "%s: ", "unknown");
199 vfprintf (stderr
, format
, args
);
200 fprintf (stderr
, "\n");
203 /* In this case we return at here. */
204 errno
= original_errno
;
207 tsctl
.precision
= zl
->timestamp_precision
;
210 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
214 vsyslog (priority
|zlog_default
->facility
, format
, ac
);
219 sprintf (proto_str
, "%s[%d]: ", zlog_proto_names
[zl
->protocol
], zl
->instance
);
221 sprintf (proto_str
, "%s: ", zlog_proto_names
[zl
->protocol
]);
224 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
227 time_print (zl
->fp
, &tsctl
);
228 if (zl
->record_priority
)
229 fprintf (zl
->fp
, "%s: ", zlog_priority
[priority
]);
230 fprintf (zl
->fp
, "%s", proto_str
);
232 vfprintf (zl
->fp
, format
, ac
);
234 fprintf (zl
->fp
, "\n");
239 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
242 time_print (stdout
, &tsctl
);
243 if (zl
->record_priority
)
244 fprintf (stdout
, "%s: ", zlog_priority
[priority
]);
245 fprintf (stdout
, "%s", proto_str
);
247 vfprintf (stdout
, format
, ac
);
249 fprintf (stdout
, "\n");
253 /* Terminal monitor. */
254 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
255 vty_log ((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
256 proto_str
, format
, &tsctl
, args
);
258 errno
= original_errno
;
262 str_append(char *dst
, int len
, const char *src
)
264 while ((len
-- > 0) && *src
)
270 num_append(char *s
, int len
, u_long x
)
276 return str_append(s
,len
,"0");
277 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
278 while (x
&& (t
> buf
))
283 return str_append(s
,len
,t
);
286 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
288 hex_append(char *s
, int len
, u_long x
)
294 return str_append(s
,len
,"0");
295 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
296 while (x
&& (t
> buf
))
299 *--t
= ((cc
< 10) ? ('0'+cc
) : ('a'+cc
-10));
302 return str_append(s
,len
,t
);
306 /* Needs to be enhanced to support Solaris. */
315 struct sockaddr_un addr
;
317 if ((fd
= socket(AF_UNIX
,SOCK_DGRAM
,0)) < 0)
319 addr
.sun_family
= AF_UNIX
;
321 #define SYSLOG_SOCKET_PATH _PATH_LOG
323 #define SYSLOG_SOCKET_PATH "/dev/log"
325 s
= str_append(addr
.sun_path
,sizeof(addr
.sun_path
),SYSLOG_SOCKET_PATH
);
326 #undef SYSLOG_SOCKET_PATH
328 if (connect(fd
,(struct sockaddr
*)&addr
,sizeof(addr
)) < 0)
338 syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
340 static int syslog_fd
= -1;
341 char buf
[sizeof("<1234567890>ripngd[1234567890]: ")+msglen
+50];
344 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
347 #define LOC s,buf+sizeof(buf)-s
349 s
= str_append(LOC
,"<");
350 s
= num_append(LOC
,priority
);
351 s
= str_append(LOC
,">");
352 /* forget about the timestamp, too difficult in a signal handler */
353 s
= str_append(LOC
,zlog_default
->ident
);
354 if (zlog_default
->syslog_options
& LOG_PID
)
356 s
= str_append(LOC
,"[");
357 s
= num_append(LOC
,getpid());
358 s
= str_append(LOC
,"]");
360 s
= str_append(LOC
,": ");
361 s
= str_append(LOC
,msg
);
362 write_wrapper (syslog_fd
,buf
,s
-buf
);
369 #define CRASHLOG_PREFIX "/var/tmp/quagga."
370 #define CRASHLOG_SUFFIX "crashlog"
371 if (zlog_default
&& zlog_default
->ident
)
373 /* Avoid strlen since it is not async-signal-safe. */
377 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
380 char buf
[sizeof(CRASHLOG_PREFIX
)+ilen
+sizeof(CRASHLOG_SUFFIX
)+3];
382 #define LOC s,buf+sizeof(buf)-s
383 s
= str_append(LOC
, CRASHLOG_PREFIX
);
384 s
= str_append(LOC
, zlog_default
->ident
);
385 s
= str_append(LOC
, ".");
386 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
389 return open(buf
, O_WRONLY
|O_CREAT
|O_EXCL
, LOGFILE_MASK
);
392 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
, O_WRONLY
|O_CREAT
|O_EXCL
,
394 #undef CRASHLOG_SUFFIX
395 #undef CRASHLOG_PREFIX
398 /* Note: the goal here is to use only async-signal-safe functions. */
400 zlog_signal(int signo
, const char *action
402 , siginfo_t
*siginfo
, void *program_counter
407 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
409 char *msgstart
= buf
;
410 #define LOC s,buf+sizeof(buf)-s
415 s
= str_append(LOC
,zlog_proto_names
[zlog_default
->protocol
]);
420 s
= str_append(LOC
,"Received signal ");
421 s
= num_append(LOC
,signo
);
422 s
= str_append(LOC
," at ");
423 s
= num_append(LOC
,now
);
425 s
= str_append(LOC
," (si_addr 0x");
426 s
= hex_append(LOC
,(u_long
)(siginfo
->si_addr
));
429 s
= str_append(LOC
,", PC 0x");
430 s
= hex_append(LOC
,(u_long
)program_counter
);
432 s
= str_append(LOC
,"); ");
433 #else /* SA_SIGINFO */
434 s
= str_append(LOC
,"; ");
435 #endif /* SA_SIGINFO */
436 s
= str_append(LOC
,action
);
437 if (s
< buf
+sizeof(buf
))
440 /* N.B. implicit priority is most severe */
443 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
444 /* If no file logging configured, try to write to fallback log file. */
445 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
451 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
453 /* Remove trailing '\n' for monitor and syslog */
455 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
456 vty_log_fixed(buf
,s
-buf
);
457 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
458 syslog_sigsafe(PRI
|zlog_default
->facility
,msgstart
,s
-msgstart
);
462 zlog_backtrace_sigsafe(PRI
,
472 s
= str_append (LOC
, "no thread information available\n");
475 s
= str_append (LOC
, "in thread ");
476 s
= str_append (LOC
, thread_current
->funcname
);
477 s
= str_append (LOC
, " scheduled from ");
478 s
= str_append (LOC
, thread_current
->schedfrom
);
479 s
= str_append (LOC
, ":");
480 s
= num_append (LOC
, thread_current
->schedfrom_line
);
481 s
= str_append (LOC
, "\n");
484 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
485 /* If no file logging configured, try to write to fallback log file. */
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
);
507 /* Log a backtrace using only async-signal-safe functions.
508 Needs to be enhanced to support syslog logging. */
510 zlog_backtrace_sigsafe(int priority
, void *program_counter
)
512 #ifdef HAVE_STACK_TRACE
513 static const char pclabel
[] = "Program counter: ";
517 char *s
, **bt
= NULL
;
518 #define LOC s,buf+sizeof(buf)-s
520 #ifdef HAVE_GLIBC_BACKTRACE
521 size
= backtrace(array
, array_size(array
));
522 if (size
<= 0 || (size_t)size
> array_size(array
))
526 if (program_counter) \
528 write_wrapper(FD, pclabel, sizeof(pclabel)-1); \
529 backtrace_symbols_fd(&program_counter, 1, FD); \
531 write_wrapper(FD, buf, s-buf); \
532 backtrace_symbols_fd(array, size, FD); \
534 #elif defined(HAVE_PRINTSTACK)
536 if (program_counter) \
537 write_wrapper((FD), pclabel, sizeof(pclabel)-1); \
538 write_wrapper((FD), buf, s-buf); \
541 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
544 s
= str_append(LOC
,"Backtrace for ");
545 s
= num_append(LOC
,size
);
546 s
= str_append(LOC
," stack frames:\n");
548 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
554 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
556 /* Remove trailing '\n' for monitor and syslog */
558 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
559 vty_log_fixed(buf
,s
-buf
);
560 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
561 syslog_sigsafe(priority
|zlog_default
->facility
,buf
,s
-buf
);
564 #ifdef HAVE_GLIBC_BACKTRACE
565 bt
= backtrace_symbols(array
, size
);
567 /* Just print the function addresses. */
568 for (i
= 0; i
< size
; i
++)
572 s
= str_append(LOC
, bt
[i
]);
574 s
= str_append(LOC
,"[bt ");
575 s
= num_append(LOC
,i
);
576 s
= str_append(LOC
,"] 0x");
577 s
= hex_append(LOC
,(u_long
)(array
[i
]));
580 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
581 vty_log_fixed(buf
,s
-buf
);
582 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
583 syslog_sigsafe(priority
|zlog_default
->facility
,buf
,s
-buf
);
591 #endif /* HAVE_STRACK_TRACE */
595 zlog_backtrace(int priority
)
597 #ifndef HAVE_GLIBC_BACKTRACE
598 zlog(NULL
, priority
, "No backtrace available on this platform.");
604 size
= backtrace(array
, array_size(array
));
605 if (size
<= 0 || (size_t)size
> array_size(array
))
607 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
608 "(valid range is between 1 and %lu)",
609 size
, (unsigned long)(array_size(array
)));
612 zlog(NULL
, priority
, "Backtrace for %d stack frames:", size
);
613 if (!(strings
= backtrace_symbols(array
, size
)))
615 zlog_err("Cannot get backtrace symbols (out of memory?)");
616 for (i
= 0; i
< size
; i
++)
617 zlog(NULL
, priority
, "[bt %d] %p",i
,array
[i
]);
621 for (i
= 0; i
< size
; i
++)
622 zlog(NULL
, priority
, "[bt %d] %s",i
,strings
[i
]);
625 #endif /* HAVE_GLIBC_BACKTRACE */
629 zlog (struct zlog
*zl
, int priority
, const char *format
, ...)
633 va_start(args
, format
);
634 vzlog (zl
, priority
, format
, args
);
638 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
640 FUNCNAME(const char *format, ...) \
643 va_start(args, format); \
644 vzlog (NULL, PRIORITY, format, args); \
648 ZLOG_FUNC(zlog_err
, LOG_ERR
)
650 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
652 ZLOG_FUNC(zlog_info
, LOG_INFO
)
654 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
656 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
660 void zlog_thread_info (int log_level
)
663 zlog(NULL
, log_level
, "Current thread function %s, scheduled from "
664 "file %s, line %u", thread_current
->funcname
,
665 thread_current
->schedfrom
, thread_current
->schedfrom_line
);
667 zlog(NULL
, log_level
, "Current thread not known/applicable");
671 _zlog_assert_failed (const char *assertion
, const char *file
,
672 unsigned int line
, const char *function
)
674 /* Force fallback file logging? */
675 if (zlog_default
&& !zlog_default
->fp
&&
676 ((logfile_fd
= open_crashlog()) >= 0) &&
677 ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
678 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
679 zlog(NULL
, LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
680 assertion
,file
,line
,(function
? function
: "?"));
681 zlog_backtrace(LOG_CRIT
);
682 zlog_thread_info(LOG_CRIT
);
687 memory_oom (size_t size
, const char *name
)
689 zlog_err("out of memory: failed to allocate %zu bytes for %s"
690 "object", size
, name
);
691 zlog_backtrace(LOG_ERR
);
695 /* Open log stream */
697 openzlog (const char *progname
, zlog_proto_t protocol
, u_short instance
,
698 int syslog_flags
, int syslog_facility
)
703 zl
= XCALLOC(MTYPE_ZLOG
, sizeof (struct zlog
));
705 zl
->ident
= progname
;
706 zl
->protocol
= protocol
;
707 zl
->instance
= instance
;
708 zl
->facility
= syslog_facility
;
709 zl
->syslog_options
= syslog_flags
;
711 /* Set default logging levels. */
712 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
713 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
714 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
715 zl
->default_lvl
= LOG_DEBUG
;
717 openlog (progname
, syslog_flags
, zl
->facility
);
723 closezlog (struct zlog
*zl
)
730 if (zl
->filename
!= NULL
)
731 XFREE(MTYPE_ZLOG
, zl
->filename
);
733 XFREE (MTYPE_ZLOG
, zl
);
736 /* Called from command.c. */
738 zlog_set_level (struct zlog
*zl
, zlog_dest_t dest
, int log_level
)
743 zl
->maxlvl
[dest
] = log_level
;
747 zlog_set_file (struct zlog
*zl
, const char *filename
, int log_level
)
752 /* There is opend file. */
753 zlog_reset_file (zl
);
755 /* Set default zl. */
760 oldumask
= umask (0777 & ~LOGFILE_MASK
);
761 fp
= fopen (filename
, "a");
767 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
768 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
770 logfile_fd
= fileno(fp
);
775 /* Reset opend file. */
777 zlog_reset_file (struct zlog
*zl
)
786 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
789 XFREE(MTYPE_ZLOG
, zl
->filename
);
795 /* Reopen log file. */
797 zlog_rotate (struct zlog
*zl
)
808 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
809 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
816 oldumask
= umask (0777 & ~LOGFILE_MASK
);
817 zl
->fp
= fopen (zl
->filename
, "a");
822 zlog_err("Log rotate failed: cannot open file %s for append: %s",
823 zl
->filename
, safe_strerror(save_errno
));
826 logfile_fd
= fileno(zl
->fp
);
827 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
833 /* Message lookup function. */
835 lookup (const struct message
*mes
, int key
)
837 const struct message
*pnt
;
839 for (pnt
= mes
; pnt
->key
!= 0; pnt
++)
846 /* Older/faster version of message lookup function, but requires caller to pass
847 * in the array size (instead of relying on a 0 key to terminate the search).
849 * The return value is the message string if found, or the 'none' pointer
850 * provided otherwise.
853 mes_lookup (const struct message
*meslist
, int max
, int index
,
854 const char *none
, const char *mesname
)
856 int pos
= index
- meslist
[0].key
;
858 /* first check for best case: index is in range and matches the key
859 * value in that slot.
860 * NB: key numbering might be offset from 0. E.g. protocol constants
863 if ((pos
>= 0) && (pos
< max
)
864 && (meslist
[pos
].key
== index
))
865 return meslist
[pos
].str
;
867 /* fall back to linear search */
871 for (i
= 0; i
< max
; i
++, meslist
++)
873 if (meslist
->key
== index
)
875 const char *str
= (meslist
->str
? meslist
->str
: none
);
877 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
878 index
, str
, mesname
, i
, max
);
883 zlog_err("message index %d not found in %s (max is %d)", index
, mesname
, max
);
888 /* Wrapper around strerror to handle case where it returns NULL. */
890 safe_strerror(int errnum
)
892 const char *s
= strerror(errnum
);
893 return (s
!= NULL
) ? s
: "Unknown error";
896 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
897 static const struct zebra_desc_table command_types
[] = {
898 DESC_ENTRY (ZEBRA_INTERFACE_ADD
),
899 DESC_ENTRY (ZEBRA_INTERFACE_DELETE
),
900 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD
),
901 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE
),
902 DESC_ENTRY (ZEBRA_INTERFACE_UP
),
903 DESC_ENTRY (ZEBRA_INTERFACE_DOWN
),
904 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD
),
905 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE
),
906 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD
),
907 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE
),
908 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD
),
909 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE
),
910 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
911 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
912 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD
),
913 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE
),
914 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE
),
915 DESC_ENTRY (ZEBRA_HELLO
),
916 DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER
),
917 DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER
),
918 DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE
),
919 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
920 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
921 DESC_ENTRY (ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
922 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_REGISTER
),
923 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_UNREGISTER
),
924 DESC_ENTRY (ZEBRA_IMPORT_CHECK_UPDATE
),
925 DESC_ENTRY (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
),
926 DESC_ENTRY (ZEBRA_BFD_DEST_REGISTER
),
927 DESC_ENTRY (ZEBRA_BFD_DEST_DEREGISTER
),
928 DESC_ENTRY (ZEBRA_BFD_DEST_UPDATE
),
929 DESC_ENTRY (ZEBRA_BFD_DEST_REPLAY
),
930 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_ADD
),
931 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_DEL
),
932 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_ADD
),
933 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_DEL
),
934 DESC_ENTRY (ZEBRA_VRF_UNREGISTER
),
935 DESC_ENTRY (ZEBRA_VRF_ADD
),
936 DESC_ENTRY (ZEBRA_VRF_DELETE
),
937 DESC_ENTRY (ZEBRA_INTERFACE_VRF_UPDATE
),
938 DESC_ENTRY (ZEBRA_BFD_CLIENT_REGISTER
),
939 DESC_ENTRY (ZEBRA_INTERFACE_ENABLE_RADV
),
940 DESC_ENTRY (ZEBRA_INTERFACE_DISABLE_RADV
),
941 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
942 DESC_ENTRY (ZEBRA_MPLS_LABELS_ADD
),
943 DESC_ENTRY (ZEBRA_MPLS_LABELS_DELETE
),
947 static const struct zebra_desc_table unknown
= { 0, "unknown", '?' };
949 static const struct zebra_desc_table
*
950 zroute_lookup(u_int zroute
)
954 if (zroute
>= array_size(route_types
))
956 zlog_err("unknown zebra route type: %u", zroute
);
959 if (zroute
== route_types
[zroute
].type
)
960 return &route_types
[zroute
];
961 for (i
= 0; i
< array_size(route_types
); i
++)
963 if (zroute
== route_types
[i
].type
)
965 zlog_warn("internal error: route type table out of order "
966 "while searching for %u, please notify developers", zroute
);
967 return &route_types
[i
];
970 zlog_err("internal error: cannot find route type %u in table!", zroute
);
975 zebra_route_string(u_int zroute
)
977 return zroute_lookup(zroute
)->string
;
981 zebra_route_char(u_int zroute
)
983 return zroute_lookup(zroute
)->chr
;
987 zserv_command_string (unsigned int command
)
989 if (command
>= array_size(command_types
))
991 zlog_err ("unknown zserv command type: %u", command
);
992 return unknown
.string
;
994 return command_types
[command
].string
;
998 proto_name2num(const char *s
)
1002 for (i
=0; i
<array_size(route_types
); ++i
)
1003 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1004 return route_types
[i
].type
;
1009 proto_redistnum(int afi
, const char *s
)
1016 if (strncmp (s
, "k", 1) == 0)
1017 return ZEBRA_ROUTE_KERNEL
;
1018 else if (strncmp (s
, "c", 1) == 0)
1019 return ZEBRA_ROUTE_CONNECT
;
1020 else if (strncmp (s
, "s", 1) == 0)
1021 return ZEBRA_ROUTE_STATIC
;
1022 else if (strncmp (s
, "r", 1) == 0)
1023 return ZEBRA_ROUTE_RIP
;
1024 else if (strncmp (s
, "o", 1) == 0)
1025 return ZEBRA_ROUTE_OSPF
;
1026 else if (strncmp (s
, "i", 1) == 0)
1027 return ZEBRA_ROUTE_ISIS
;
1028 else if (strncmp (s
, "bg", 2) == 0)
1029 return ZEBRA_ROUTE_BGP
;
1030 else if (strncmp (s
, "ta", 2) == 0)
1031 return ZEBRA_ROUTE_TABLE
;
1035 if (strncmp (s
, "k", 1) == 0)
1036 return ZEBRA_ROUTE_KERNEL
;
1037 else if (strncmp (s
, "c", 1) == 0)
1038 return ZEBRA_ROUTE_CONNECT
;
1039 else if (strncmp (s
, "s", 1) == 0)
1040 return ZEBRA_ROUTE_STATIC
;
1041 else if (strncmp (s
, "r", 1) == 0)
1042 return ZEBRA_ROUTE_RIPNG
;
1043 else if (strncmp (s
, "o", 1) == 0)
1044 return ZEBRA_ROUTE_OSPF6
;
1045 else if (strncmp (s
, "i", 1) == 0)
1046 return ZEBRA_ROUTE_ISIS
;
1047 else if (strncmp (s
, "bg", 2) == 0)
1048 return ZEBRA_ROUTE_BGP
;
1049 else if (strncmp (s
, "ta", 2) == 0)
1050 return ZEBRA_ROUTE_TABLE
;
1056 zlog_hexdump (const void *mem
, unsigned int len
) {
1057 unsigned long i
= 0;
1059 unsigned int columns
= 8;
1060 char buf
[(len
* 4) + ((len
/4) * 20) + 30];
1063 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0); i
++)
1066 if (i
% columns
== 0)
1067 s
+= sprintf(s
, "0x%016lx: ", (unsigned long)mem
+ i
);
1069 /* print hex data */
1071 s
+= sprintf(s
, "%02x ", 0xFF & ((const char*)mem
)[i
]);
1073 /* end of block, just aligning for ASCII dump */
1075 s
+= sprintf(s
, " ");
1077 /* print ASCII dump */
1078 if (i
% columns
== (columns
- 1))
1080 for (j
= i
- (columns
- 1); j
<= i
; j
++)
1082 if (j
>= len
) /* end of block, not really printing */
1083 s
+= sprintf(s
, " ");
1085 else if(isprint((int)((const char *)mem
)[j
])) /* printable char */
1086 s
+= sprintf(s
, "%c", 0xFF & ((const char *)mem
)[j
]);
1088 else /* other char */
1089 s
+= sprintf(s
, ".");
1091 s
+= sprintf(s
, "\n");
1094 zlog_debug("\n%s", buf
);