]> git.proxmox.com Git - mirror_frr.git/blob - lib/log.c
This patch changes BGP from only listening mode for BFD status updates to interactive...
[mirror_frr.git] / lib / log.c
1 /*
2 * Logging of zebra
3 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * later version.
11 *
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.
16 *
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
20 * 02111-1307, USA.
21 */
22
23 #define QUAGGA_DEFINE_DESC_TABLE
24
25 #include <zebra.h>
26
27 #include "log.h"
28 #include "memory.h"
29 #include "command.h"
30 #ifndef SUNOS_5
31 #include <sys/un.h>
32 #endif
33 /* for printstack on solaris */
34 #ifdef HAVE_UCONTEXT_H
35 #include <ucontext.h>
36 #endif
37
38 static int logfile_fd = -1; /* Used in signal handler. */
39
40 struct zlog *zlog_default = NULL;
41
42 const char *zlog_proto_names[] =
43 {
44 "NONE",
45 "DEFAULT",
46 "ZEBRA",
47 "RIP",
48 "BGP",
49 "OSPF",
50 "RIPNG",
51 "BABEL",
52 "OSPF6",
53 "ISIS",
54 "MASC",
55 NULL,
56 };
57
58 const char *zlog_priority[] =
59 {
60 "emergencies",
61 "alerts",
62 "critical",
63 "errors",
64 "warnings",
65 "notifications",
66 "informational",
67 "debugging",
68 NULL,
69 };
70
71
72
73 /* For time string format. */
74
75 size_t
76 quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
77 {
78 static struct {
79 time_t last;
80 size_t len;
81 char buf[28];
82 } cache;
83 struct timeval clock;
84
85 /* would it be sufficient to use global 'recent_time' here? I fear not... */
86 gettimeofday(&clock, NULL);
87
88 /* first, we update the cache if the time has changed */
89 if (cache.last != clock.tv_sec)
90 {
91 struct tm *tm;
92 cache.last = clock.tv_sec;
93 tm = localtime(&cache.last);
94 cache.len = strftime(cache.buf, sizeof(cache.buf),
95 "%Y/%m/%d %H:%M:%S", tm);
96 }
97 /* note: it's not worth caching the subsecond part, because
98 chances are that back-to-back calls are not sufficiently close together
99 for the clock not to have ticked forward */
100
101 if (buflen > cache.len)
102 {
103 memcpy(buf, cache.buf, cache.len);
104 if ((timestamp_precision > 0) &&
105 (buflen > cache.len+1+timestamp_precision))
106 {
107 /* should we worry about locale issues? */
108 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
109 int prec;
110 char *p = buf+cache.len+1+(prec = timestamp_precision);
111 *p-- = '\0';
112 while (prec > 6)
113 /* this is unlikely to happen, but protect anyway */
114 {
115 *p-- = '0';
116 prec--;
117 }
118 clock.tv_usec /= divisor[prec];
119 do
120 {
121 *p-- = '0'+(clock.tv_usec % 10);
122 clock.tv_usec /= 10;
123 }
124 while (--prec > 0);
125 *p = '.';
126 return cache.len+1+timestamp_precision;
127 }
128 buf[cache.len] = '\0';
129 return cache.len;
130 }
131 if (buflen > 0)
132 buf[0] = '\0';
133 return 0;
134 }
135
136 /* Utility routine for current time printing. */
137 static void
138 time_print(FILE *fp, struct timestamp_control *ctl)
139 {
140 if (!ctl->already_rendered)
141 {
142 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
143 ctl->already_rendered = 1;
144 }
145 fprintf(fp, "%s ", ctl->buf);
146 }
147
148
149 /* va_list version of zlog. */
150 static void
151 vzlog (struct zlog *zl, int priority, const char *format, va_list args)
152 {
153 char proto_str[32];
154 struct timestamp_control tsctl;
155 tsctl.already_rendered = 0;
156
157 /* If zlog is not specified, use default one. */
158 if (zl == NULL)
159 zl = zlog_default;
160
161 /* When zlog_default is also NULL, use stderr for logging. */
162 if (zl == NULL)
163 {
164 tsctl.precision = 0;
165 time_print(stderr, &tsctl);
166 fprintf (stderr, "%s: ", "unknown");
167 vfprintf (stderr, format, args);
168 fprintf (stderr, "\n");
169 fflush (stderr);
170
171 /* In this case we return at here. */
172 return;
173 }
174 tsctl.precision = zl->timestamp_precision;
175
176 /* Syslog output */
177 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
178 {
179 va_list ac;
180 va_copy(ac, args);
181 vsyslog (priority|zlog_default->facility, format, ac);
182 va_end(ac);
183 }
184
185 if (zl->instance)
186 sprintf (proto_str, "%s[%d]: ", zlog_proto_names[zl->protocol], zl->instance);
187 else
188 sprintf (proto_str, "%s: ", zlog_proto_names[zl->protocol]);
189
190 /* File output. */
191 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
192 {
193 va_list ac;
194 time_print (zl->fp, &tsctl);
195 if (zl->record_priority)
196 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
197 fprintf (zl->fp, "%s", proto_str);
198 va_copy(ac, args);
199 vfprintf (zl->fp, format, ac);
200 va_end(ac);
201 fprintf (zl->fp, "\n");
202 fflush (zl->fp);
203 }
204
205 /* stdout output. */
206 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
207 {
208 va_list ac;
209 time_print (stdout, &tsctl);
210 if (zl->record_priority)
211 fprintf (stdout, "%s: ", zlog_priority[priority]);
212 fprintf (stdout, "%s", proto_str);
213 va_copy(ac, args);
214 vfprintf (stdout, format, ac);
215 va_end(ac);
216 fprintf (stdout, "\n");
217 fflush (stdout);
218 }
219
220 /* Terminal monitor. */
221 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
222 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
223 proto_str, format, &tsctl, args);
224 }
225
226 static char *
227 str_append(char *dst, int len, const char *src)
228 {
229 while ((len-- > 0) && *src)
230 *dst++ = *src++;
231 return dst;
232 }
233
234 static char *
235 num_append(char *s, int len, u_long x)
236 {
237 char buf[30];
238 char *t;
239
240 if (!x)
241 return str_append(s,len,"0");
242 *(t = &buf[sizeof(buf)-1]) = '\0';
243 while (x && (t > buf))
244 {
245 *--t = '0'+(x % 10);
246 x /= 10;
247 }
248 return str_append(s,len,t);
249 }
250
251 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
252 static char *
253 hex_append(char *s, int len, u_long x)
254 {
255 char buf[30];
256 char *t;
257
258 if (!x)
259 return str_append(s,len,"0");
260 *(t = &buf[sizeof(buf)-1]) = '\0';
261 while (x && (t > buf))
262 {
263 u_int cc = (x % 16);
264 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
265 x /= 16;
266 }
267 return str_append(s,len,t);
268 }
269 #endif
270
271 /* Needs to be enhanced to support Solaris. */
272 static int
273 syslog_connect(void)
274 {
275 #ifdef SUNOS_5
276 return -1;
277 #else
278 int fd;
279 char *s;
280 struct sockaddr_un addr;
281
282 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
283 return -1;
284 addr.sun_family = AF_UNIX;
285 #ifdef _PATH_LOG
286 #define SYSLOG_SOCKET_PATH _PATH_LOG
287 #else
288 #define SYSLOG_SOCKET_PATH "/dev/log"
289 #endif
290 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
291 #undef SYSLOG_SOCKET_PATH
292 *s = '\0';
293 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
294 {
295 close(fd);
296 return -1;
297 }
298 return fd;
299 #endif
300 }
301
302 static void
303 syslog_sigsafe(int priority, const char *msg, size_t msglen)
304 {
305 static int syslog_fd = -1;
306 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
307 char *s;
308
309 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
310 return;
311
312 #define LOC s,buf+sizeof(buf)-s
313 s = buf;
314 s = str_append(LOC,"<");
315 s = num_append(LOC,priority);
316 s = str_append(LOC,">");
317 /* forget about the timestamp, too difficult in a signal handler */
318 s = str_append(LOC,zlog_default->ident);
319 if (zlog_default->syslog_options & LOG_PID)
320 {
321 s = str_append(LOC,"[");
322 s = num_append(LOC,getpid());
323 s = str_append(LOC,"]");
324 }
325 s = str_append(LOC,": ");
326 s = str_append(LOC,msg);
327 write(syslog_fd,buf,s-buf);
328 #undef LOC
329 }
330
331 static int
332 open_crashlog(void)
333 {
334 #define CRASHLOG_PREFIX "/var/tmp/quagga."
335 #define CRASHLOG_SUFFIX "crashlog"
336 if (zlog_default && zlog_default->ident)
337 {
338 /* Avoid strlen since it is not async-signal-safe. */
339 const char *p;
340 size_t ilen;
341
342 for (p = zlog_default->ident, ilen = 0; *p; p++)
343 ilen++;
344 {
345 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
346 char *s = buf;
347 #define LOC s,buf+sizeof(buf)-s
348 s = str_append(LOC, CRASHLOG_PREFIX);
349 s = str_append(LOC, zlog_default->ident);
350 s = str_append(LOC, ".");
351 s = str_append(LOC, CRASHLOG_SUFFIX);
352 #undef LOC
353 *s = '\0';
354 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
355 }
356 }
357 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
358 LOGFILE_MASK);
359 #undef CRASHLOG_SUFFIX
360 #undef CRASHLOG_PREFIX
361 }
362
363 /* Note: the goal here is to use only async-signal-safe functions. */
364 void
365 zlog_signal(int signo, const char *action
366 #ifdef SA_SIGINFO
367 , siginfo_t *siginfo, void *program_counter
368 #endif
369 )
370 {
371 time_t now;
372 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
373 char *s = buf;
374 char *msgstart = buf;
375 #define LOC s,buf+sizeof(buf)-s
376
377 time(&now);
378 if (zlog_default)
379 {
380 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
381 *s++ = ':';
382 *s++ = ' ';
383 msgstart = s;
384 }
385 s = str_append(LOC,"Received signal ");
386 s = num_append(LOC,signo);
387 s = str_append(LOC," at ");
388 s = num_append(LOC,now);
389 #ifdef SA_SIGINFO
390 s = str_append(LOC," (si_addr 0x");
391 s = hex_append(LOC,(u_long)(siginfo->si_addr));
392 if (program_counter)
393 {
394 s = str_append(LOC,", PC 0x");
395 s = hex_append(LOC,(u_long)program_counter);
396 }
397 s = str_append(LOC,"); ");
398 #else /* SA_SIGINFO */
399 s = str_append(LOC,"; ");
400 #endif /* SA_SIGINFO */
401 s = str_append(LOC,action);
402 if (s < buf+sizeof(buf))
403 *s++ = '\n';
404
405 /* N.B. implicit priority is most severe */
406 #define PRI LOG_CRIT
407
408 #define DUMP(FD) write(FD, buf, s-buf);
409 /* If no file logging configured, try to write to fallback log file. */
410 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
411 DUMP(logfile_fd)
412 if (!zlog_default)
413 DUMP(STDERR_FILENO)
414 else
415 {
416 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
417 DUMP(STDOUT_FILENO)
418 /* Remove trailing '\n' for monitor and syslog */
419 *--s = '\0';
420 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
421 vty_log_fixed(buf,s-buf);
422 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
423 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
424 }
425 #undef DUMP
426
427 zlog_backtrace_sigsafe(PRI,
428 #ifdef SA_SIGINFO
429 program_counter
430 #else
431 NULL
432 #endif
433 );
434 #undef PRI
435 #undef LOC
436 }
437
438 /* Log a backtrace using only async-signal-safe functions.
439 Needs to be enhanced to support syslog logging. */
440 void
441 zlog_backtrace_sigsafe(int priority, void *program_counter)
442 {
443 #ifdef HAVE_STACK_TRACE
444 static const char pclabel[] = "Program counter: ";
445 void *array[64];
446 int size;
447 char buf[100];
448 char *s, **bt = NULL;
449 #define LOC s,buf+sizeof(buf)-s
450
451 #ifdef HAVE_GLIBC_BACKTRACE
452 size = backtrace(array, array_size(array));
453 if (size <= 0 || (size_t)size > array_size(array))
454 return;
455
456 #define DUMP(FD) { \
457 if (program_counter) \
458 { \
459 write(FD, pclabel, sizeof(pclabel)-1); \
460 backtrace_symbols_fd(&program_counter, 1, FD); \
461 } \
462 write(FD, buf, s-buf); \
463 backtrace_symbols_fd(array, size, FD); \
464 }
465 #elif defined(HAVE_PRINTSTACK)
466 #define DUMP(FD) { \
467 if (program_counter) \
468 write((FD), pclabel, sizeof(pclabel)-1); \
469 write((FD), buf, s-buf); \
470 printstack((FD)); \
471 }
472 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
473
474 s = buf;
475 s = str_append(LOC,"Backtrace for ");
476 s = num_append(LOC,size);
477 s = str_append(LOC," stack frames:\n");
478
479 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
480 DUMP(logfile_fd)
481 if (!zlog_default)
482 DUMP(STDERR_FILENO)
483 else
484 {
485 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
486 DUMP(STDOUT_FILENO)
487 /* Remove trailing '\n' for monitor and syslog */
488 *--s = '\0';
489 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
490 vty_log_fixed(buf,s-buf);
491 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
492 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
493 {
494 int i;
495 #ifdef HAVE_GLIBC_BACKTRACE
496 bt = backtrace_symbols(array, size);
497 #endif
498 /* Just print the function addresses. */
499 for (i = 0; i < size; i++)
500 {
501 s = buf;
502 if (bt)
503 s = str_append(LOC, bt[i]);
504 else {
505 s = str_append(LOC,"[bt ");
506 s = num_append(LOC,i);
507 s = str_append(LOC,"] 0x");
508 s = hex_append(LOC,(u_long)(array[i]));
509 }
510 *s = '\0';
511 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
512 vty_log_fixed(buf,s-buf);
513 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
514 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
515 }
516 if (bt)
517 free(bt);
518 }
519 }
520 #undef DUMP
521 #undef LOC
522 #endif /* HAVE_STRACK_TRACE */
523 }
524
525 void
526 zlog_backtrace(int priority)
527 {
528 #ifndef HAVE_GLIBC_BACKTRACE
529 zlog(NULL, priority, "No backtrace available on this platform.");
530 #else
531 void *array[20];
532 int size, i;
533 char **strings;
534
535 size = backtrace(array, array_size(array));
536 if (size <= 0 || (size_t)size > array_size(array))
537 {
538 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
539 "(valid range is between 1 and %lu)",
540 size, (unsigned long)(array_size(array)));
541 return;
542 }
543 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
544 if (!(strings = backtrace_symbols(array, size)))
545 {
546 zlog_err("Cannot get backtrace symbols (out of memory?)");
547 for (i = 0; i < size; i++)
548 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
549 }
550 else
551 {
552 for (i = 0; i < size; i++)
553 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
554 free(strings);
555 }
556 #endif /* HAVE_GLIBC_BACKTRACE */
557 }
558
559 void
560 zlog (struct zlog *zl, int priority, const char *format, ...)
561 {
562 va_list args;
563
564 va_start(args, format);
565 vzlog (zl, priority, format, args);
566 va_end (args);
567 }
568
569 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
570 void \
571 FUNCNAME(const char *format, ...) \
572 { \
573 va_list args; \
574 va_start(args, format); \
575 vzlog (NULL, PRIORITY, format, args); \
576 va_end(args); \
577 }
578
579 ZLOG_FUNC(zlog_err, LOG_ERR)
580
581 ZLOG_FUNC(zlog_warn, LOG_WARNING)
582
583 ZLOG_FUNC(zlog_info, LOG_INFO)
584
585 ZLOG_FUNC(zlog_notice, LOG_NOTICE)
586
587 ZLOG_FUNC(zlog_debug, LOG_DEBUG)
588
589 #undef ZLOG_FUNC
590
591 void
592 _zlog_assert_failed (const char *assertion, const char *file,
593 unsigned int line, const char *function)
594 {
595 /* Force fallback file logging? */
596 if (zlog_default && !zlog_default->fp &&
597 ((logfile_fd = open_crashlog()) >= 0) &&
598 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
599 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
600 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
601 assertion,file,line,(function ? function : "?"));
602 zlog_backtrace(LOG_CRIT);
603 abort();
604 }
605
606
607 /* Open log stream */
608 struct zlog *
609 openzlog (const char *progname, zlog_proto_t protocol, u_short instance,
610 int syslog_flags, int syslog_facility)
611 {
612 struct zlog *zl;
613 u_int i;
614
615 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
616
617 zl->ident = progname;
618 zl->protocol = protocol;
619 zl->instance = instance;
620 zl->facility = syslog_facility;
621 zl->syslog_options = syslog_flags;
622
623 /* Set default logging levels. */
624 for (i = 0; i < array_size(zl->maxlvl); i++)
625 zl->maxlvl[i] = ZLOG_DISABLED;
626 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
627 zl->default_lvl = LOG_DEBUG;
628
629 openlog (progname, syslog_flags, zl->facility);
630
631 return zl;
632 }
633
634 void
635 closezlog (struct zlog *zl)
636 {
637 closelog();
638
639 if (zl->fp != NULL)
640 fclose (zl->fp);
641
642 if (zl->filename != NULL)
643 free (zl->filename);
644
645 XFREE (MTYPE_ZLOG, zl);
646 }
647
648 /* Called from command.c. */
649 void
650 zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
651 {
652 if (zl == NULL)
653 zl = zlog_default;
654
655 zl->maxlvl[dest] = log_level;
656 }
657
658 int
659 zlog_set_file (struct zlog *zl, const char *filename, int log_level)
660 {
661 FILE *fp;
662 mode_t oldumask;
663
664 /* There is opend file. */
665 zlog_reset_file (zl);
666
667 /* Set default zl. */
668 if (zl == NULL)
669 zl = zlog_default;
670
671 /* Open file. */
672 oldumask = umask (0777 & ~LOGFILE_MASK);
673 fp = fopen (filename, "a");
674 umask(oldumask);
675 if (fp == NULL)
676 return 0;
677
678 /* Set flags. */
679 zl->filename = strdup (filename);
680 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
681 zl->fp = fp;
682 logfile_fd = fileno(fp);
683
684 return 1;
685 }
686
687 /* Reset opend file. */
688 int
689 zlog_reset_file (struct zlog *zl)
690 {
691 if (zl == NULL)
692 zl = zlog_default;
693
694 if (zl->fp)
695 fclose (zl->fp);
696 zl->fp = NULL;
697 logfile_fd = -1;
698 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
699
700 if (zl->filename)
701 free (zl->filename);
702 zl->filename = NULL;
703
704 return 1;
705 }
706
707 /* Reopen log file. */
708 int
709 zlog_rotate (struct zlog *zl)
710 {
711 int level;
712
713 if (zl == NULL)
714 zl = zlog_default;
715
716 if (zl->fp)
717 fclose (zl->fp);
718 zl->fp = NULL;
719 logfile_fd = -1;
720 level = zl->maxlvl[ZLOG_DEST_FILE];
721 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
722
723 if (zl->filename)
724 {
725 mode_t oldumask;
726 int save_errno;
727
728 oldumask = umask (0777 & ~LOGFILE_MASK);
729 zl->fp = fopen (zl->filename, "a");
730 save_errno = errno;
731 umask(oldumask);
732 if (zl->fp == NULL)
733 {
734 zlog_err("Log rotate failed: cannot open file %s for append: %s",
735 zl->filename, safe_strerror(save_errno));
736 return -1;
737 }
738 logfile_fd = fileno(zl->fp);
739 zl->maxlvl[ZLOG_DEST_FILE] = level;
740 }
741
742 return 1;
743 }
744
745 /* Message lookup function. */
746 const char *
747 lookup (const struct message *mes, int key)
748 {
749 const struct message *pnt;
750
751 for (pnt = mes; pnt->key != 0; pnt++)
752 if (pnt->key == key)
753 return pnt->str;
754
755 return "";
756 }
757
758 /* Older/faster version of message lookup function, but requires caller to pass
759 * in the array size (instead of relying on a 0 key to terminate the search).
760 *
761 * The return value is the message string if found, or the 'none' pointer
762 * provided otherwise.
763 */
764 const char *
765 mes_lookup (const struct message *meslist, int max, int index,
766 const char *none, const char *mesname)
767 {
768 int pos = index - meslist[0].key;
769
770 /* first check for best case: index is in range and matches the key
771 * value in that slot.
772 * NB: key numbering might be offset from 0. E.g. protocol constants
773 * often start at 1.
774 */
775 if ((pos >= 0) && (pos < max)
776 && (meslist[pos].key == index))
777 return meslist[pos].str;
778
779 /* fall back to linear search */
780 {
781 int i;
782
783 for (i = 0; i < max; i++, meslist++)
784 {
785 if (meslist->key == index)
786 {
787 const char *str = (meslist->str ? meslist->str : none);
788
789 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
790 index, str, mesname, i, max);
791 return str;
792 }
793 }
794 }
795 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
796 assert (none);
797 return none;
798 }
799
800 /* Wrapper around strerror to handle case where it returns NULL. */
801 const char *
802 safe_strerror(int errnum)
803 {
804 const char *s = strerror(errnum);
805 return (s != NULL) ? s : "Unknown error";
806 }
807
808 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
809 static const struct zebra_desc_table command_types[] = {
810 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
811 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
812 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
813 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
814 DESC_ENTRY (ZEBRA_INTERFACE_UP),
815 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
816 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
817 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
818 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
819 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
820 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
821 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
822 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
823 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
824 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
825 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
826 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
827 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
828 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
829 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
830 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
831 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
832 DESC_ENTRY (ZEBRA_HELLO),
833 DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER),
834 DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER),
835 DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE),
836 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_ADD),
837 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE),
838 DESC_ENTRY (ZEBRA_IMPORT_CHECK_UPDATE),
839 DESC_ENTRY (ZEBRA_INTERFACE_BFD_DEST_DOWN),
840 DESC_ENTRY (ZEBRA_BFD_DEST_REGISTER),
841 DESC_ENTRY (ZEBRA_BFD_DEST_DEREGISTER),
842 DESC_ENTRY (ZEBRA_BFD_DEST_UPDATE),
843 };
844 #undef DESC_ENTRY
845
846 static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
847
848 static const struct zebra_desc_table *
849 zroute_lookup(u_int zroute)
850 {
851 u_int i;
852
853 if (zroute >= array_size(route_types))
854 {
855 zlog_err("unknown zebra route type: %u", zroute);
856 return &unknown;
857 }
858 if (zroute == route_types[zroute].type)
859 return &route_types[zroute];
860 for (i = 0; i < array_size(route_types); i++)
861 {
862 if (zroute == route_types[i].type)
863 {
864 zlog_warn("internal error: route type table out of order "
865 "while searching for %u, please notify developers", zroute);
866 return &route_types[i];
867 }
868 }
869 zlog_err("internal error: cannot find route type %u in table!", zroute);
870 return &unknown;
871 }
872
873 const char *
874 zebra_route_string(u_int zroute)
875 {
876 return zroute_lookup(zroute)->string;
877 }
878
879 char
880 zebra_route_char(u_int zroute)
881 {
882 return zroute_lookup(zroute)->chr;
883 }
884
885 const char *
886 zserv_command_string (unsigned int command)
887 {
888 if (command >= array_size(command_types))
889 {
890 zlog_err ("unknown zserv command type: %u", command);
891 return unknown.string;
892 }
893 return command_types[command].string;
894 }
895
896 int
897 proto_name2num(const char *s)
898 {
899 unsigned i;
900
901 for (i=0; i<array_size(route_types); ++i)
902 if (strcasecmp(s, route_types[i].string) == 0)
903 return route_types[i].type;
904 return -1;
905 }
906
907 int
908 proto_redistnum(int afi, const char *s)
909 {
910 if (! s)
911 return -1;
912
913 if (afi == AFI_IP)
914 {
915 if (strncmp (s, "k", 1) == 0)
916 return ZEBRA_ROUTE_KERNEL;
917 else if (strncmp (s, "c", 1) == 0)
918 return ZEBRA_ROUTE_CONNECT;
919 else if (strncmp (s, "s", 1) == 0)
920 return ZEBRA_ROUTE_STATIC;
921 else if (strncmp (s, "r", 1) == 0)
922 return ZEBRA_ROUTE_RIP;
923 else if (strncmp (s, "o", 1) == 0)
924 return ZEBRA_ROUTE_OSPF;
925 else if (strncmp (s, "i", 1) == 0)
926 return ZEBRA_ROUTE_ISIS;
927 else if (strncmp (s, "bg", 2) == 0)
928 return ZEBRA_ROUTE_BGP;
929 else if (strncmp (s, "ba", 2) == 0)
930 return ZEBRA_ROUTE_BABEL;
931 else if (strncmp (s, "t", 1) == 0)
932 return ZEBRA_ROUTE_TABLE;
933 }
934 if (afi == AFI_IP6)
935 {
936 if (strncmp (s, "k", 1) == 0)
937 return ZEBRA_ROUTE_KERNEL;
938 else if (strncmp (s, "c", 1) == 0)
939 return ZEBRA_ROUTE_CONNECT;
940 else if (strncmp (s, "s", 1) == 0)
941 return ZEBRA_ROUTE_STATIC;
942 else if (strncmp (s, "r", 1) == 0)
943 return ZEBRA_ROUTE_RIPNG;
944 else if (strncmp (s, "o", 1) == 0)
945 return ZEBRA_ROUTE_OSPF6;
946 else if (strncmp (s, "i", 1) == 0)
947 return ZEBRA_ROUTE_ISIS;
948 else if (strncmp (s, "bg", 2) == 0)
949 return ZEBRA_ROUTE_BGP;
950 else if (strncmp (s, "ba", 2) == 0)
951 return ZEBRA_ROUTE_BABEL;
952 else if (strncmp (s, "t", 1) == 0)
953 return ZEBRA_ROUTE_TABLE;
954 }
955 return -1;
956 }