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
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
;
45 * This must be kept in the same order as the
48 const char *zlog_proto_names
[] =
66 const char *zlog_priority
[] =
82 * glibc has declared that the return value from write *must* not be
84 * gcc see's this problem and issues a warning for the line.
86 * Why is this a big deal you say? Because both of them are right
87 * and if you have -Werror enabled then all calls to write
88 * generate a build error and the build stops.
90 * clang has helpfully allowed this construct:
92 * to tell the compiler yeah I know it has a return value
93 * I don't care about it at this time.
94 * gcc doesn't have this ability.
96 * This code was written such that it didn't care about the
97 * return value from write. At this time do I want
98 * to go through and fix and test this code for correctness.
99 * So just wrapper the bad behavior and move on.
101 static void write_wrapper (int fd
, const void *buf
, size_t count
)
103 if (write (fd
, buf
, count
) <= 0)
109 /* For time string format. */
112 quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
119 struct timeval clock
;
121 /* would it be sufficient to use global 'recent_time' here? I fear not... */
122 gettimeofday(&clock
, NULL
);
124 /* first, we update the cache if the time has changed */
125 if (cache
.last
!= clock
.tv_sec
)
128 cache
.last
= clock
.tv_sec
;
129 tm
= localtime(&cache
.last
);
130 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
131 "%Y/%m/%d %H:%M:%S", tm
);
133 /* note: it's not worth caching the subsecond part, because
134 chances are that back-to-back calls are not sufficiently close together
135 for the clock not to have ticked forward */
137 if (buflen
> cache
.len
)
139 memcpy(buf
, cache
.buf
, cache
.len
);
140 if ((timestamp_precision
> 0) &&
141 (buflen
> cache
.len
+1+timestamp_precision
))
143 /* should we worry about locale issues? */
144 static const int divisor
[] = {0, 100000, 10000, 1000, 100, 10, 1};
146 char *p
= buf
+cache
.len
+1+(prec
= timestamp_precision
);
149 /* this is unlikely to happen, but protect anyway */
154 clock
.tv_usec
/= divisor
[prec
];
157 *p
-- = '0'+(clock
.tv_usec
% 10);
162 return cache
.len
+1+timestamp_precision
;
164 buf
[cache
.len
] = '\0';
172 /* Utility routine for current time printing. */
174 time_print(FILE *fp
, struct timestamp_control
*ctl
)
176 if (!ctl
->already_rendered
)
178 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
179 ctl
->already_rendered
= 1;
181 fprintf(fp
, "%s ", ctl
->buf
);
185 /* va_list version of zlog. */
187 vzlog (struct zlog
*zl
, int priority
, const char *format
, va_list args
)
190 int original_errno
= errno
;
191 struct timestamp_control tsctl
;
192 tsctl
.already_rendered
= 0;
194 /* If zlog is not specified, use default one. */
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]: ", zlog_proto_names
[zl
->protocol
], zl
->instance
);
226 sprintf (proto_str
, "%s: ", zlog_proto_names
[zl
->protocol
]);
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 (struct zlog
*zl
, int priority
)
269 /* If zlog is not specified, use default one. */
273 /* When zlog_default is also NULL, use stderr for logging. */
280 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
286 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
292 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
297 /* Terminal monitor. */
298 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
305 str_append(char *dst
, int len
, const char *src
)
307 while ((len
-- > 0) && *src
)
313 num_append(char *s
, int len
, u_long x
)
319 return str_append(s
,len
,"0");
320 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
321 while (x
&& (t
> buf
))
326 return str_append(s
,len
,t
);
329 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
331 hex_append(char *s
, int len
, u_long x
)
337 return str_append(s
,len
,"0");
338 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
339 while (x
&& (t
> buf
))
342 *--t
= ((cc
< 10) ? ('0'+cc
) : ('a'+cc
-10));
345 return str_append(s
,len
,t
);
349 /* Needs to be enhanced to support Solaris. */
358 struct sockaddr_un addr
;
360 if ((fd
= socket(AF_UNIX
,SOCK_DGRAM
,0)) < 0)
362 addr
.sun_family
= AF_UNIX
;
364 #define SYSLOG_SOCKET_PATH _PATH_LOG
366 #define SYSLOG_SOCKET_PATH "/dev/log"
368 s
= str_append(addr
.sun_path
,sizeof(addr
.sun_path
),SYSLOG_SOCKET_PATH
);
369 #undef SYSLOG_SOCKET_PATH
371 if (connect(fd
,(struct sockaddr
*)&addr
,sizeof(addr
)) < 0)
381 syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
383 static int syslog_fd
= -1;
384 char buf
[sizeof("<1234567890>ripngd[1234567890]: ")+msglen
+50];
387 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
390 #define LOC s,buf+sizeof(buf)-s
392 s
= str_append(LOC
,"<");
393 s
= num_append(LOC
,priority
);
394 s
= str_append(LOC
,">");
395 /* forget about the timestamp, too difficult in a signal handler */
396 s
= str_append(LOC
,zlog_default
->ident
);
397 if (zlog_default
->syslog_options
& LOG_PID
)
399 s
= str_append(LOC
,"[");
400 s
= num_append(LOC
,getpid());
401 s
= str_append(LOC
,"]");
403 s
= str_append(LOC
,": ");
404 s
= str_append(LOC
,msg
);
405 write_wrapper (syslog_fd
,buf
,s
-buf
);
412 #define CRASHLOG_PREFIX "/var/tmp/quagga."
413 #define CRASHLOG_SUFFIX "crashlog"
414 if (zlog_default
&& zlog_default
->ident
)
416 /* Avoid strlen since it is not async-signal-safe. */
420 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
423 char buf
[sizeof(CRASHLOG_PREFIX
)+ilen
+sizeof(CRASHLOG_SUFFIX
)+3];
425 #define LOC s,buf+sizeof(buf)-s
426 s
= str_append(LOC
, CRASHLOG_PREFIX
);
427 s
= str_append(LOC
, zlog_default
->ident
);
428 s
= str_append(LOC
, ".");
429 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
432 return open(buf
, O_WRONLY
|O_CREAT
|O_EXCL
, LOGFILE_MASK
);
435 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
, O_WRONLY
|O_CREAT
|O_EXCL
,
437 #undef CRASHLOG_SUFFIX
438 #undef CRASHLOG_PREFIX
441 /* Note: the goal here is to use only async-signal-safe functions. */
443 zlog_signal(int signo
, const char *action
445 , siginfo_t
*siginfo
, void *program_counter
450 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
452 char *msgstart
= buf
;
453 #define LOC s,buf+sizeof(buf)-s
458 s
= str_append(LOC
,zlog_proto_names
[zlog_default
->protocol
]);
463 s
= str_append(LOC
,"Received signal ");
464 s
= num_append(LOC
,signo
);
465 s
= str_append(LOC
," at ");
466 s
= num_append(LOC
,now
);
468 s
= str_append(LOC
," (si_addr 0x");
469 s
= hex_append(LOC
,(u_long
)(siginfo
->si_addr
));
472 s
= str_append(LOC
,", PC 0x");
473 s
= hex_append(LOC
,(u_long
)program_counter
);
475 s
= str_append(LOC
,"); ");
476 #else /* SA_SIGINFO */
477 s
= str_append(LOC
,"; ");
478 #endif /* SA_SIGINFO */
479 s
= str_append(LOC
,action
);
480 if (s
< buf
+sizeof(buf
))
483 /* N.B. implicit priority is most severe */
486 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
487 /* If no file logging configured, try to write to fallback log file. */
488 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
494 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
496 /* Remove trailing '\n' for monitor and syslog */
498 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
499 vty_log_fixed(buf
,s
-buf
);
500 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
501 syslog_sigsafe(PRI
|zlog_default
->facility
,msgstart
,s
-msgstart
);
505 zlog_backtrace_sigsafe(PRI
,
515 s
= str_append (LOC
, "no thread information available\n");
518 s
= str_append (LOC
, "in thread ");
519 s
= str_append (LOC
, thread_current
->funcname
);
520 s
= str_append (LOC
, " scheduled from ");
521 s
= str_append (LOC
, thread_current
->schedfrom
);
522 s
= str_append (LOC
, ":");
523 s
= num_append (LOC
, thread_current
->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(NULL
, 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(NULL
, 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(NULL
, priority
, "[bt %d] %p",i
,array
[i
]);
664 for (i
= 0; i
< size
; i
++)
665 zlog(NULL
, priority
, "[bt %d] %s",i
,strings
[i
]);
668 #endif /* HAVE_GLIBC_BACKTRACE */
672 zlog (struct zlog
*zl
, int priority
, const char *format
, ...)
676 va_start(args
, format
);
677 vzlog (zl
, priority
, format
, args
);
681 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
683 FUNCNAME(const char *format, ...) \
686 va_start(args, format); \
687 vzlog (NULL, 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 zlog(NULL
, log_level
, "Current thread function %s, scheduled from "
707 "file %s, line %u", thread_current
->funcname
,
708 thread_current
->schedfrom
, thread_current
->schedfrom_line
);
710 zlog(NULL
, log_level
, "Current thread not known/applicable");
714 _zlog_assert_failed (const char *assertion
, const char *file
,
715 unsigned int line
, const char *function
)
717 /* Force fallback file logging? */
718 if (zlog_default
&& !zlog_default
->fp
&&
719 ((logfile_fd
= open_crashlog()) >= 0) &&
720 ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
721 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
722 zlog(NULL
, LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
723 assertion
,file
,line
,(function
? function
: "?"));
724 zlog_backtrace(LOG_CRIT
);
725 zlog_thread_info(LOG_CRIT
);
726 log_memstats_stderr ("log");
731 memory_oom (size_t size
, const char *name
)
733 zlog_err("out of memory: failed to allocate %zu bytes for %s"
734 "object", size
, name
);
735 zlog_backtrace(LOG_ERR
);
739 /* Open log stream */
741 openzlog (const char *progname
, zlog_proto_t protocol
, u_short instance
,
742 int syslog_flags
, int syslog_facility
)
747 zl
= XCALLOC(MTYPE_ZLOG
, sizeof (struct zlog
));
749 zl
->ident
= progname
;
750 zl
->protocol
= protocol
;
751 zl
->instance
= instance
;
752 zl
->facility
= syslog_facility
;
753 zl
->syslog_options
= syslog_flags
;
755 /* Set default logging levels. */
756 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
757 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
758 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
759 zl
->default_lvl
= LOG_DEBUG
;
761 openlog (progname
, syslog_flags
, zl
->facility
);
767 closezlog (struct zlog
*zl
)
774 if (zl
->filename
!= NULL
)
775 XFREE(MTYPE_ZLOG
, zl
->filename
);
777 XFREE (MTYPE_ZLOG
, zl
);
780 /* Called from command.c. */
782 zlog_set_level (struct zlog
*zl
, zlog_dest_t dest
, int log_level
)
787 zl
->maxlvl
[dest
] = log_level
;
791 zlog_set_file (struct zlog
*zl
, const char *filename
, int log_level
)
796 /* There is opend file. */
797 zlog_reset_file (zl
);
799 /* Set default zl. */
804 oldumask
= umask (0777 & ~LOGFILE_MASK
);
805 fp
= fopen (filename
, "a");
811 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
812 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
814 logfile_fd
= fileno(fp
);
819 /* Reset opend file. */
821 zlog_reset_file (struct zlog
*zl
)
830 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
833 XFREE(MTYPE_ZLOG
, zl
->filename
);
839 /* Reopen log file. */
841 zlog_rotate (struct zlog
*zl
)
852 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
853 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
860 oldumask
= umask (0777 & ~LOGFILE_MASK
);
861 zl
->fp
= fopen (zl
->filename
, "a");
866 zlog_err("Log rotate failed: cannot open file %s for append: %s",
867 zl
->filename
, safe_strerror(save_errno
));
870 logfile_fd
= fileno(zl
->fp
);
871 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
877 /* Message lookup function. */
879 lookup (const struct message
*mes
, int key
)
881 const struct message
*pnt
;
883 for (pnt
= mes
; pnt
->key
!= 0; pnt
++)
890 /* Older/faster version of message lookup function, but requires caller to pass
891 * in the array size (instead of relying on a 0 key to terminate the search).
893 * The return value is the message string if found, or the 'none' pointer
894 * provided otherwise.
897 mes_lookup (const struct message
*meslist
, int max
, int index
,
898 const char *none
, const char *mesname
)
900 int pos
= index
- meslist
[0].key
;
902 /* first check for best case: index is in range and matches the key
903 * value in that slot.
904 * NB: key numbering might be offset from 0. E.g. protocol constants
907 if ((pos
>= 0) && (pos
< max
)
908 && (meslist
[pos
].key
== index
))
909 return meslist
[pos
].str
;
911 /* fall back to linear search */
915 for (i
= 0; i
< max
; i
++, meslist
++)
917 if (meslist
->key
== index
)
919 const char *str
= (meslist
->str
? meslist
->str
: none
);
921 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
922 index
, str
, mesname
, i
, max
);
927 zlog_err("message index %d not found in %s (max is %d)", index
, mesname
, max
);
932 /* Wrapper around strerror to handle case where it returns NULL. */
934 safe_strerror(int errnum
)
936 const char *s
= strerror(errnum
);
937 return (s
!= NULL
) ? s
: "Unknown error";
940 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
941 static const struct zebra_desc_table command_types
[] = {
942 DESC_ENTRY (ZEBRA_INTERFACE_ADD
),
943 DESC_ENTRY (ZEBRA_INTERFACE_DELETE
),
944 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD
),
945 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE
),
946 DESC_ENTRY (ZEBRA_INTERFACE_UP
),
947 DESC_ENTRY (ZEBRA_INTERFACE_DOWN
),
948 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD
),
949 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE
),
950 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD
),
951 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE
),
952 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD
),
953 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE
),
954 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
955 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
956 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD
),
957 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE
),
958 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE
),
959 DESC_ENTRY (ZEBRA_HELLO
),
960 DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER
),
961 DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER
),
962 DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE
),
963 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
964 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
965 DESC_ENTRY (ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
966 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_REGISTER
),
967 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_UNREGISTER
),
968 DESC_ENTRY (ZEBRA_IMPORT_CHECK_UPDATE
),
969 DESC_ENTRY (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
),
970 DESC_ENTRY (ZEBRA_BFD_DEST_REGISTER
),
971 DESC_ENTRY (ZEBRA_BFD_DEST_DEREGISTER
),
972 DESC_ENTRY (ZEBRA_BFD_DEST_UPDATE
),
973 DESC_ENTRY (ZEBRA_BFD_DEST_REPLAY
),
974 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_ADD
),
975 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_DEL
),
976 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_ADD
),
977 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_DEL
),
978 DESC_ENTRY (ZEBRA_VRF_UNREGISTER
),
979 DESC_ENTRY (ZEBRA_VRF_ADD
),
980 DESC_ENTRY (ZEBRA_VRF_DELETE
),
981 DESC_ENTRY (ZEBRA_INTERFACE_VRF_UPDATE
),
982 DESC_ENTRY (ZEBRA_BFD_CLIENT_REGISTER
),
983 DESC_ENTRY (ZEBRA_INTERFACE_ENABLE_RADV
),
984 DESC_ENTRY (ZEBRA_INTERFACE_DISABLE_RADV
),
985 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
986 DESC_ENTRY (ZEBRA_INTERFACE_LINK_PARAMS
),
987 DESC_ENTRY (ZEBRA_MPLS_LABELS_ADD
),
988 DESC_ENTRY (ZEBRA_MPLS_LABELS_DELETE
),
989 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_ADD
),
990 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_DELETE
),
991 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_ADD
),
992 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_DELETE
),
996 static const struct zebra_desc_table unknown
= { 0, "unknown", '?' };
998 static const struct zebra_desc_table
*
999 zroute_lookup(u_int zroute
)
1003 if (zroute
>= array_size(route_types
))
1005 zlog_err("unknown zebra route type: %u", zroute
);
1008 if (zroute
== route_types
[zroute
].type
)
1009 return &route_types
[zroute
];
1010 for (i
= 0; i
< array_size(route_types
); i
++)
1012 if (zroute
== route_types
[i
].type
)
1014 zlog_warn("internal error: route type table out of order "
1015 "while searching for %u, please notify developers", zroute
);
1016 return &route_types
[i
];
1019 zlog_err("internal error: cannot find route type %u in table!", zroute
);
1024 zebra_route_string(u_int zroute
)
1026 return zroute_lookup(zroute
)->string
;
1030 zebra_route_char(u_int zroute
)
1032 return zroute_lookup(zroute
)->chr
;
1036 zserv_command_string (unsigned int command
)
1038 if (command
>= array_size(command_types
))
1040 zlog_err ("unknown zserv command type: %u", command
);
1041 return unknown
.string
;
1043 return command_types
[command
].string
;
1047 proto_name2num(const char *s
)
1051 for (i
=0; i
<array_size(route_types
); ++i
)
1052 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1053 return route_types
[i
].type
;
1058 proto_redistnum(int afi
, const char *s
)
1065 if (strncmp (s
, "k", 1) == 0)
1066 return ZEBRA_ROUTE_KERNEL
;
1067 else if (strncmp (s
, "c", 1) == 0)
1068 return ZEBRA_ROUTE_CONNECT
;
1069 else if (strncmp (s
, "s", 1) == 0)
1070 return ZEBRA_ROUTE_STATIC
;
1071 else if (strncmp (s
, "r", 1) == 0)
1072 return ZEBRA_ROUTE_RIP
;
1073 else if (strncmp (s
, "o", 1) == 0)
1074 return ZEBRA_ROUTE_OSPF
;
1075 else if (strncmp (s
, "i", 1) == 0)
1076 return ZEBRA_ROUTE_ISIS
;
1077 else if (strncmp (s
, "bg", 2) == 0)
1078 return ZEBRA_ROUTE_BGP
;
1079 else if (strncmp (s
, "ta", 2) == 0)
1080 return ZEBRA_ROUTE_TABLE
;
1081 else if (strcmp (s
, "vnc-direct") == 0)
1082 return ZEBRA_ROUTE_VNC_DIRECT
;
1083 else if (strcmp (s
, "vnc") == 0)
1084 return ZEBRA_ROUTE_VNC
;
1088 if (strncmp (s
, "k", 1) == 0)
1089 return ZEBRA_ROUTE_KERNEL
;
1090 else if (strncmp (s
, "c", 1) == 0)
1091 return ZEBRA_ROUTE_CONNECT
;
1092 else if (strncmp (s
, "s", 1) == 0)
1093 return ZEBRA_ROUTE_STATIC
;
1094 else if (strncmp (s
, "r", 1) == 0)
1095 return ZEBRA_ROUTE_RIPNG
;
1096 else if (strncmp (s
, "o", 1) == 0)
1097 return ZEBRA_ROUTE_OSPF6
;
1098 else if (strncmp (s
, "i", 1) == 0)
1099 return ZEBRA_ROUTE_ISIS
;
1100 else if (strncmp (s
, "bg", 2) == 0)
1101 return ZEBRA_ROUTE_BGP
;
1102 else if (strncmp (s
, "ta", 2) == 0)
1103 return ZEBRA_ROUTE_TABLE
;
1104 else if (strcmp (s
, "vnc-direct") == 0)
1105 return ZEBRA_ROUTE_VNC_DIRECT
;
1106 else if (strcmp (s
, "vnc") == 0)
1107 return ZEBRA_ROUTE_VNC
;
1113 zlog_hexdump (const void *mem
, unsigned int len
) {
1114 unsigned long i
= 0;
1116 unsigned int columns
= 8;
1117 char buf
[(len
* 4) + ((len
/4) * 20) + 30];
1120 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0); i
++)
1123 if (i
% columns
== 0)
1124 s
+= sprintf(s
, "0x%016lx: ", (unsigned long)mem
+ i
);
1126 /* print hex data */
1128 s
+= sprintf(s
, "%02x ", 0xFF & ((const char*)mem
)[i
]);
1130 /* end of block, just aligning for ASCII dump */
1132 s
+= sprintf(s
, " ");
1134 /* print ASCII dump */
1135 if (i
% columns
== (columns
- 1))
1137 for (j
= i
- (columns
- 1); j
<= i
; j
++)
1139 if (j
>= len
) /* end of block, not really printing */
1140 s
+= sprintf(s
, " ");
1142 else if(isprint((int)((const char *)mem
)[j
])) /* printable char */
1143 s
+= sprintf(s
, "%c", 0xFF & ((const char *)mem
)[j
]);
1145 else /* other char */
1146 s
+= sprintf(s
, ".");
1148 s
+= sprintf(s
, "\n");
1151 zlog_debug("\n%s", buf
);