blob: abfd35bc5e246d513a0400ed4e5d9308f0bdbd36 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs274a4a42004-12-07 15:39:31 +00002 * Logging of zebra
paul718e3742002-12-13 20:15:29 +00003 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
Everton Marques871dbcf2009-08-11 15:43:05 -03004 * Portions Copyright (c) 2008 Everton da Silva Marques <everton.marques@gmail.com>
paul718e3742002-12-13 20:15:29 +00005 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
David Lampartere0ca5fd2009-09-16 01:52:42 +020024#define QUAGGA_DEFINE_DESC_TABLE
25
paul718e3742002-12-13 20:15:29 +000026#include <zebra.h>
27
28#include "log.h"
29#include "memory.h"
30#include "command.h"
ajs7d149b82004-11-28 23:00:01 +000031#ifndef SUNOS_5
32#include <sys/un.h>
33#endif
Paul Jakma30a22312008-08-15 14:05:22 +010034/* for printstack on solaris */
35#ifdef HAVE_UCONTEXT_H
36#include <ucontext.h>
37#endif
paul718e3742002-12-13 20:15:29 +000038
ajsc4c7d0c2005-02-03 19:22:05 +000039static int logfile_fd = -1; /* Used in signal handler. */
ajs1e221352005-02-03 16:42:40 +000040
paul718e3742002-12-13 20:15:29 +000041struct zlog *zlog_default = NULL;
42
43const char *zlog_proto_names[] =
44{
45 "NONE",
46 "DEFAULT",
47 "ZEBRA",
48 "RIP",
49 "BGP",
50 "OSPF",
51 "RIPNG",
Paul Jakma57345092011-12-25 17:52:09 +010052 "BABEL",
paul718e3742002-12-13 20:15:29 +000053 "OSPF6",
jardin9e867fe2003-12-23 08:56:18 +000054 "ISIS",
Everton Marques871dbcf2009-08-11 15:43:05 -030055 "PIM",
paul718e3742002-12-13 20:15:29 +000056 "MASC",
57 NULL,
58};
59
60const char *zlog_priority[] =
61{
62 "emergencies",
63 "alerts",
64 "critical",
65 "errors",
66 "warnings",
67 "notifications",
68 "informational",
69 "debugging",
70 NULL,
71};
72
73
David Lamparter6b0655a2014-06-04 06:53:35 +020074
paul718e3742002-12-13 20:15:29 +000075/* For time string format. */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000076
77size_t
78quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
79{
80 static struct {
81 time_t last;
82 size_t len;
83 char buf[28];
84 } cache;
85 struct timeval clock;
86
87 /* would it be sufficient to use global 'recent_time' here? I fear not... */
88 gettimeofday(&clock, NULL);
89
90 /* first, we update the cache if the time has changed */
91 if (cache.last != clock.tv_sec)
92 {
93 struct tm *tm;
94 cache.last = clock.tv_sec;
95 tm = localtime(&cache.last);
96 cache.len = strftime(cache.buf, sizeof(cache.buf),
97 "%Y/%m/%d %H:%M:%S", tm);
98 }
99 /* note: it's not worth caching the subsecond part, because
100 chances are that back-to-back calls are not sufficiently close together
101 for the clock not to have ticked forward */
102
103 if (buflen > cache.len)
104 {
105 memcpy(buf, cache.buf, cache.len);
106 if ((timestamp_precision > 0) &&
107 (buflen > cache.len+1+timestamp_precision))
108 {
109 /* should we worry about locale issues? */
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000110 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
111 int prec;
112 char *p = buf+cache.len+1+(prec = timestamp_precision);
113 *p-- = '\0';
114 while (prec > 6)
115 /* this is unlikely to happen, but protect anyway */
116 {
117 *p-- = '0';
118 prec--;
119 }
120 clock.tv_usec /= divisor[prec];
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000121 do
122 {
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000123 *p-- = '0'+(clock.tv_usec % 10);
124 clock.tv_usec /= 10;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000125 }
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000126 while (--prec > 0);
127 *p = '.';
128 return cache.len+1+timestamp_precision;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000129 }
130 buf[cache.len] = '\0';
131 return cache.len;
132 }
133 if (buflen > 0)
134 buf[0] = '\0';
135 return 0;
136}
paul718e3742002-12-13 20:15:29 +0000137
138/* Utility routine for current time printing. */
139static void
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000140time_print(FILE *fp, struct timestamp_control *ctl)
paul718e3742002-12-13 20:15:29 +0000141{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000142 if (!ctl->already_rendered)
143 {
144 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
145 ctl->already_rendered = 1;
146 }
147 fprintf(fp, "%s ", ctl->buf);
paul718e3742002-12-13 20:15:29 +0000148}
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000149
David Lamparter6b0655a2014-06-04 06:53:35 +0200150
paul718e3742002-12-13 20:15:29 +0000151/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +0000152static void
153vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +0000154{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000155 struct timestamp_control tsctl;
156 tsctl.already_rendered = 0;
157
paul718e3742002-12-13 20:15:29 +0000158 /* If zlog is not specified, use default one. */
159 if (zl == NULL)
160 zl = zlog_default;
161
162 /* When zlog_default is also NULL, use stderr for logging. */
163 if (zl == NULL)
164 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000165 tsctl.precision = 0;
166 time_print(stderr, &tsctl);
paul718e3742002-12-13 20:15:29 +0000167 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000168 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000169 fprintf (stderr, "\n");
170 fflush (stderr);
171
172 /* In this case we return at here. */
173 return;
174 }
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000175 tsctl.precision = zl->timestamp_precision;
paul718e3742002-12-13 20:15:29 +0000176
paul718e3742002-12-13 20:15:29 +0000177 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000178 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000179 {
180 va_list ac;
181 va_copy(ac, args);
182 vsyslog (priority|zlog_default->facility, format, ac);
183 va_end(ac);
184 }
paul718e3742002-12-13 20:15:29 +0000185
186 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000187 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000188 {
ajsd246bd92004-11-23 17:35:08 +0000189 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000190 time_print (zl->fp, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000191 if (zl->record_priority)
192 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000193 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000194 va_copy(ac, args);
195 vfprintf (zl->fp, format, ac);
196 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000197 fprintf (zl->fp, "\n");
198 fflush (zl->fp);
199 }
200
201 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000202 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000203 {
ajsd246bd92004-11-23 17:35:08 +0000204 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000205 time_print (stdout, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000206 if (zl->record_priority)
207 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000208 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000209 va_copy(ac, args);
210 vfprintf (stdout, format, ac);
211 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000212 fprintf (stdout, "\n");
213 fflush (stdout);
214 }
215
paul718e3742002-12-13 20:15:29 +0000216 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000217 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
218 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000219 zlog_proto_names[zl->protocol], format, &tsctl, args);
paul718e3742002-12-13 20:15:29 +0000220}
221
ajs59a06a92004-11-23 18:19:14 +0000222static char *
223str_append(char *dst, int len, const char *src)
224{
225 while ((len-- > 0) && *src)
226 *dst++ = *src++;
227 return dst;
228}
229
230static char *
231num_append(char *s, int len, u_long x)
232{
233 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000234 char *t;
ajs59a06a92004-11-23 18:19:14 +0000235
ajs7d149b82004-11-28 23:00:01 +0000236 if (!x)
237 return str_append(s,len,"0");
238 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000239 while (x && (t > buf))
240 {
241 *--t = '0'+(x % 10);
242 x /= 10;
243 }
244 return str_append(s,len,t);
245}
246
Paul Jakmafb66b292006-05-28 08:26:15 +0000247#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
ajs7d149b82004-11-28 23:00:01 +0000248static char *
249hex_append(char *s, int len, u_long x)
250{
251 char buf[30];
252 char *t;
253
254 if (!x)
255 return str_append(s,len,"0");
256 *(t = &buf[sizeof(buf)-1]) = '\0';
257 while (x && (t > buf))
258 {
259 u_int cc = (x % 16);
260 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
261 x /= 16;
262 }
263 return str_append(s,len,t);
264}
ajs31364272005-01-18 22:18:59 +0000265#endif
ajs7d149b82004-11-28 23:00:01 +0000266
ajs7d149b82004-11-28 23:00:01 +0000267/* Needs to be enhanced to support Solaris. */
268static int
269syslog_connect(void)
270{
271#ifdef SUNOS_5
272 return -1;
273#else
274 int fd;
275 char *s;
276 struct sockaddr_un addr;
277
278 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
279 return -1;
280 addr.sun_family = AF_UNIX;
281#ifdef _PATH_LOG
282#define SYSLOG_SOCKET_PATH _PATH_LOG
283#else
284#define SYSLOG_SOCKET_PATH "/dev/log"
285#endif
286 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
287#undef SYSLOG_SOCKET_PATH
288 *s = '\0';
289 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
290 {
291 close(fd);
292 return -1;
293 }
294 return fd;
295#endif
296}
297
298static void
299syslog_sigsafe(int priority, const char *msg, size_t msglen)
300{
ajs1e221352005-02-03 16:42:40 +0000301 static int syslog_fd = -1;
ajs7d149b82004-11-28 23:00:01 +0000302 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
303 char *s;
304
305 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
306 return;
307
308#define LOC s,buf+sizeof(buf)-s
309 s = buf;
310 s = str_append(LOC,"<");
311 s = num_append(LOC,priority);
312 s = str_append(LOC,">");
313 /* forget about the timestamp, too difficult in a signal handler */
314 s = str_append(LOC,zlog_default->ident);
315 if (zlog_default->syslog_options & LOG_PID)
316 {
317 s = str_append(LOC,"[");
318 s = num_append(LOC,getpid());
319 s = str_append(LOC,"]");
320 }
321 s = str_append(LOC,": ");
322 s = str_append(LOC,msg);
323 write(syslog_fd,buf,s-buf);
324#undef LOC
325}
326
ajs1e221352005-02-03 16:42:40 +0000327static int
328open_crashlog(void)
329{
330#define CRASHLOG_PREFIX "/var/tmp/quagga."
331#define CRASHLOG_SUFFIX "crashlog"
332 if (zlog_default && zlog_default->ident)
333 {
334 /* Avoid strlen since it is not async-signal-safe. */
335 const char *p;
336 size_t ilen;
337
338 for (p = zlog_default->ident, ilen = 0; *p; p++)
339 ilen++;
340 {
341 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
342 char *s = buf;
343#define LOC s,buf+sizeof(buf)-s
344 s = str_append(LOC, CRASHLOG_PREFIX);
345 s = str_append(LOC, zlog_default->ident);
346 s = str_append(LOC, ".");
347 s = str_append(LOC, CRASHLOG_SUFFIX);
348#undef LOC
349 *s = '\0';
350 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
351 }
352 }
353 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
354 LOGFILE_MASK);
355#undef CRASHLOG_SUFFIX
356#undef CRASHLOG_PREFIX
357}
358
ajs7d149b82004-11-28 23:00:01 +0000359/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000360void
ajs31364272005-01-18 22:18:59 +0000361zlog_signal(int signo, const char *action
362#ifdef SA_SIGINFO
363 , siginfo_t *siginfo, void *program_counter
364#endif
365 )
ajs59a06a92004-11-23 18:19:14 +0000366{
367 time_t now;
ajs40abf232005-01-12 17:27:27 +0000368 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000369 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000370 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000371#define LOC s,buf+sizeof(buf)-s
372
373 time(&now);
374 if (zlog_default)
375 {
376 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
377 *s++ = ':';
378 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000379 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000380 }
381 s = str_append(LOC,"Received signal ");
382 s = num_append(LOC,signo);
383 s = str_append(LOC," at ");
384 s = num_append(LOC,now);
ajs31364272005-01-18 22:18:59 +0000385#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000386 s = str_append(LOC," (si_addr 0x");
387 s = hex_append(LOC,(u_long)(siginfo->si_addr));
388 if (program_counter)
389 {
390 s = str_append(LOC,", PC 0x");
391 s = hex_append(LOC,(u_long)program_counter);
392 }
393 s = str_append(LOC,"); ");
ajs31364272005-01-18 22:18:59 +0000394#else /* SA_SIGINFO */
395 s = str_append(LOC,"; ");
396#endif /* SA_SIGINFO */
ajs59a06a92004-11-23 18:19:14 +0000397 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000398 if (s < buf+sizeof(buf))
399 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000400
ajs274a4a42004-12-07 15:39:31 +0000401 /* N.B. implicit priority is most severe */
ajs1e221352005-02-03 16:42:40 +0000402#define PRI LOG_CRIT
ajs274a4a42004-12-07 15:39:31 +0000403
ajs1e221352005-02-03 16:42:40 +0000404#define DUMP(FD) write(FD, buf, s-buf);
405 /* If no file logging configured, try to write to fallback log file. */
ajsc4c7d0c2005-02-03 19:22:05 +0000406 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
407 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000408 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000409 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000410 else
411 {
ajs274a4a42004-12-07 15:39:31 +0000412 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000413 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000414 /* Remove trailing '\n' for monitor and syslog */
415 *--s = '\0';
416 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
417 vty_log_fixed(buf,s-buf);
418 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
419 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000420 }
421#undef DUMP
422
ajs31364272005-01-18 22:18:59 +0000423 zlog_backtrace_sigsafe(PRI,
424#ifdef SA_SIGINFO
425 program_counter
426#else
427 NULL
428#endif
429 );
David Lamparter615f9f12013-11-18 23:52:02 +0100430
431 s = buf;
432 if (!thread_current)
433 s = str_append (LOC, "no thread information available\n");
434 else
435 {
436 s = str_append (LOC, "in thread ");
437 s = str_append (LOC, thread_current->funcname);
438 s = str_append (LOC, " scheduled from ");
439 s = str_append (LOC, thread_current->schedfrom);
440 s = str_append (LOC, ":");
441 s = num_append (LOC, thread_current->schedfrom_line);
442 s = str_append (LOC, "\n");
443 }
444
445#define DUMP(FD) write(FD, buf, s-buf);
446 /* If no file logging configured, try to write to fallback log file. */
447 if (logfile_fd >= 0)
448 DUMP(logfile_fd)
449 if (!zlog_default)
450 DUMP(STDERR_FILENO)
451 else
452 {
453 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
454 DUMP(STDOUT_FILENO)
455 /* Remove trailing '\n' for monitor and syslog */
456 *--s = '\0';
457 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
458 vty_log_fixed(buf,s-buf);
459 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
460 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
461 }
462#undef DUMP
463
ajs274a4a42004-12-07 15:39:31 +0000464#undef PRI
ajs063ee522004-11-26 18:11:14 +0000465#undef LOC
466}
ajs59a06a92004-11-23 18:19:14 +0000467
ajs063ee522004-11-26 18:11:14 +0000468/* Log a backtrace using only async-signal-safe functions.
469 Needs to be enhanced to support syslog logging. */
470void
ajs239c26f2005-01-17 15:22:28 +0000471zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000472{
Paul Jakmafb66b292006-05-28 08:26:15 +0000473#ifdef HAVE_STACK_TRACE
ajs239c26f2005-01-17 15:22:28 +0000474 static const char pclabel[] = "Program counter: ";
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700475 void *array[64];
ajs063ee522004-11-26 18:11:14 +0000476 int size;
477 char buf[100];
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700478 char *s, **bt = NULL;
ajs063ee522004-11-26 18:11:14 +0000479#define LOC s,buf+sizeof(buf)-s
480
Paul Jakmafb66b292006-05-28 08:26:15 +0000481#ifdef HAVE_GLIBC_BACKTRACE
David Lamparter4d474fa2013-11-19 15:00:06 +0100482 size = backtrace(array, array_size(array));
483 if (size <= 0 || (size_t)size > array_size(array))
ajs063ee522004-11-26 18:11:14 +0000484 return;
ajs59a06a92004-11-23 18:19:14 +0000485
ajs1e221352005-02-03 16:42:40 +0000486#define DUMP(FD) { \
ajs239c26f2005-01-17 15:22:28 +0000487 if (program_counter) \
488 { \
ajs1e221352005-02-03 16:42:40 +0000489 write(FD, pclabel, sizeof(pclabel)-1); \
490 backtrace_symbols_fd(&program_counter, 1, FD); \
ajs239c26f2005-01-17 15:22:28 +0000491 } \
ajs1e221352005-02-03 16:42:40 +0000492 write(FD, buf, s-buf); \
493 backtrace_symbols_fd(array, size, FD); \
ajs59a06a92004-11-23 18:19:14 +0000494}
Paul Jakmafb66b292006-05-28 08:26:15 +0000495#elif defined(HAVE_PRINTSTACK)
496#define DUMP(FD) { \
497 if (program_counter) \
498 write((FD), pclabel, sizeof(pclabel)-1); \
499 write((FD), buf, s-buf); \
500 printstack((FD)); \
501}
502#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
503
504 s = buf;
505 s = str_append(LOC,"Backtrace for ");
506 s = num_append(LOC,size);
507 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000508
ajsc4c7d0c2005-02-03 19:22:05 +0000509 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
510 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000511 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000512 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000513 else
514 {
ajs274a4a42004-12-07 15:39:31 +0000515 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000516 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000517 /* Remove trailing '\n' for monitor and syslog */
518 *--s = '\0';
519 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
520 vty_log_fixed(buf,s-buf);
521 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
522 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
523 {
524 int i;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700525#ifdef HAVE_GLIBC_BACKTRACE
526 bt = backtrace_symbols(array, size);
527#endif
ajs274a4a42004-12-07 15:39:31 +0000528 /* Just print the function addresses. */
529 for (i = 0; i < size; i++)
530 {
531 s = buf;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700532 if (bt)
533 s = str_append(LOC, bt[i]);
534 else {
535 s = str_append(LOC,"[bt ");
536 s = num_append(LOC,i);
537 s = str_append(LOC,"] 0x");
538 s = hex_append(LOC,(u_long)(array[i]));
539 }
ajs274a4a42004-12-07 15:39:31 +0000540 *s = '\0';
541 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
542 vty_log_fixed(buf,s-buf);
543 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000544 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000545 }
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700546 if (bt)
547 free(bt);
ajs274a4a42004-12-07 15:39:31 +0000548 }
ajs59a06a92004-11-23 18:19:14 +0000549 }
550#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000551#undef LOC
Paul Jakmafb66b292006-05-28 08:26:15 +0000552#endif /* HAVE_STRACK_TRACE */
ajs063ee522004-11-26 18:11:14 +0000553}
554
555void
556zlog_backtrace(int priority)
557{
558#ifndef HAVE_GLIBC_BACKTRACE
559 zlog(NULL, priority, "No backtrace available on this platform.");
560#else
561 void *array[20];
562 int size, i;
563 char **strings;
564
David Lamparter4d474fa2013-11-19 15:00:06 +0100565 size = backtrace(array, array_size(array));
566 if (size <= 0 || (size_t)size > array_size(array))
ajs063ee522004-11-26 18:11:14 +0000567 {
568 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000569 "(valid range is between 1 and %lu)",
Balaji.G837d16c2012-09-26 14:09:10 +0530570 size, (unsigned long)(array_size(array)));
ajs063ee522004-11-26 18:11:14 +0000571 return;
572 }
573 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
574 if (!(strings = backtrace_symbols(array, size)))
575 {
576 zlog_err("Cannot get backtrace symbols (out of memory?)");
577 for (i = 0; i < size; i++)
578 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
579 }
580 else
581 {
582 for (i = 0; i < size; i++)
583 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
584 free(strings);
585 }
586#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000587}
588
paul718e3742002-12-13 20:15:29 +0000589void
590zlog (struct zlog *zl, int priority, const char *format, ...)
591{
ajsd246bd92004-11-23 17:35:08 +0000592 va_list args;
paul718e3742002-12-13 20:15:29 +0000593
ajsd246bd92004-11-23 17:35:08 +0000594 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000595 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000596 va_end (args);
paul718e3742002-12-13 20:15:29 +0000597}
598
ajsd246bd92004-11-23 17:35:08 +0000599#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
600void \
601FUNCNAME(const char *format, ...) \
602{ \
603 va_list args; \
604 va_start(args, format); \
605 vzlog (NULL, PRIORITY, format, args); \
606 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000607}
608
ajsd246bd92004-11-23 17:35:08 +0000609ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000610
ajsd246bd92004-11-23 17:35:08 +0000611ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000612
ajsd246bd92004-11-23 17:35:08 +0000613ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000614
ajsd246bd92004-11-23 17:35:08 +0000615ZLOG_FUNC(zlog_notice, LOG_NOTICE)
616
617ZLOG_FUNC(zlog_debug, LOG_DEBUG)
618
619#undef ZLOG_FUNC
620
621#define PLOG_FUNC(FUNCNAME,PRIORITY) \
622void \
623FUNCNAME(struct zlog *zl, const char *format, ...) \
624{ \
625 va_list args; \
626 va_start(args, format); \
627 vzlog (zl, PRIORITY, format, args); \
628 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000629}
630
ajsd246bd92004-11-23 17:35:08 +0000631PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000632
ajsd246bd92004-11-23 17:35:08 +0000633PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000634
ajsd246bd92004-11-23 17:35:08 +0000635PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000636
ajsd246bd92004-11-23 17:35:08 +0000637PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000638
ajsd246bd92004-11-23 17:35:08 +0000639PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000640
ajsd246bd92004-11-23 17:35:08 +0000641#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000642
David Lamparter615f9f12013-11-18 23:52:02 +0100643void zlog_thread_info (int log_level)
644{
645 if (thread_current)
646 zlog(NULL, log_level, "Current thread function %s, scheduled from "
647 "file %s, line %u", thread_current->funcname,
648 thread_current->schedfrom, thread_current->schedfrom_line);
649 else
650 zlog(NULL, log_level, "Current thread not known/applicable");
651}
652
ajscee3df12004-11-24 17:14:49 +0000653void
654_zlog_assert_failed (const char *assertion, const char *file,
655 unsigned int line, const char *function)
656{
ajsc4c7d0c2005-02-03 19:22:05 +0000657 /* Force fallback file logging? */
658 if (zlog_default && !zlog_default->fp &&
659 ((logfile_fd = open_crashlog()) >= 0) &&
660 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
661 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
ajs1e221352005-02-03 16:42:40 +0000662 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
663 assertion,file,line,(function ? function : "?"));
664 zlog_backtrace(LOG_CRIT);
David Lamparter615f9f12013-11-18 23:52:02 +0100665 zlog_thread_info(LOG_CRIT);
ajscee3df12004-11-24 17:14:49 +0000666 abort();
667}
668
David Lamparter6b0655a2014-06-04 06:53:35 +0200669
paul718e3742002-12-13 20:15:29 +0000670/* Open log stream */
671struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000672openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000673 int syslog_flags, int syslog_facility)
674{
675 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000676 u_int i;
paul718e3742002-12-13 20:15:29 +0000677
ajs274a4a42004-12-07 15:39:31 +0000678 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000679
680 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000681 zl->protocol = protocol;
682 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000683 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000684
ajs274a4a42004-12-07 15:39:31 +0000685 /* Set default logging levels. */
Balaji.G837d16c2012-09-26 14:09:10 +0530686 for (i = 0; i < array_size(zl->maxlvl); i++)
ajs274a4a42004-12-07 15:39:31 +0000687 zl->maxlvl[i] = ZLOG_DISABLED;
688 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
689 zl->default_lvl = LOG_DEBUG;
690
paul718e3742002-12-13 20:15:29 +0000691 openlog (progname, syslog_flags, zl->facility);
692
693 return zl;
694}
695
696void
697closezlog (struct zlog *zl)
698{
699 closelog();
Chris Caputo228da422009-07-18 05:44:03 +0000700
701 if (zl->fp != NULL)
702 fclose (zl->fp);
paul718e3742002-12-13 20:15:29 +0000703
Tom Goff7e69d992010-11-10 13:01:17 -0800704 if (zl->filename != NULL)
705 free (zl->filename);
706
paul718e3742002-12-13 20:15:29 +0000707 XFREE (MTYPE_ZLOG, zl);
708}
709
710/* Called from command.c. */
711void
ajs274a4a42004-12-07 15:39:31 +0000712zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000713{
714 if (zl == NULL)
715 zl = zlog_default;
716
ajs274a4a42004-12-07 15:39:31 +0000717 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000718}
719
720int
ajs274a4a42004-12-07 15:39:31 +0000721zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000722{
723 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000724 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000725
726 /* There is opend file. */
727 zlog_reset_file (zl);
728
729 /* Set default zl. */
730 if (zl == NULL)
731 zl = zlog_default;
732
733 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000734 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000735 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000736 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000737 if (fp == NULL)
738 return 0;
paul718e3742002-12-13 20:15:29 +0000739
740 /* Set flags. */
741 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000742 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000743 zl->fp = fp;
ajsc4c7d0c2005-02-03 19:22:05 +0000744 logfile_fd = fileno(fp);
paul718e3742002-12-13 20:15:29 +0000745
746 return 1;
747}
748
749/* Reset opend file. */
750int
751zlog_reset_file (struct zlog *zl)
752{
753 if (zl == NULL)
754 zl = zlog_default;
755
paul718e3742002-12-13 20:15:29 +0000756 if (zl->fp)
757 fclose (zl->fp);
758 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000759 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000760 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000761
762 if (zl->filename)
763 free (zl->filename);
764 zl->filename = NULL;
765
766 return 1;
767}
768
769/* Reopen log file. */
770int
771zlog_rotate (struct zlog *zl)
772{
ajs274a4a42004-12-07 15:39:31 +0000773 int level;
paul718e3742002-12-13 20:15:29 +0000774
775 if (zl == NULL)
776 zl = zlog_default;
777
778 if (zl->fp)
779 fclose (zl->fp);
780 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000781 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000782 level = zl->maxlvl[ZLOG_DEST_FILE];
783 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000784
785 if (zl->filename)
786 {
gdtaa593d52003-12-22 20:15:53 +0000787 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000788 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000789
790 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000791 zl->fp = fopen (zl->filename, "a");
792 save_errno = errno;
793 umask(oldumask);
794 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000795 {
ajs274a4a42004-12-07 15:39:31 +0000796 zlog_err("Log rotate failed: cannot open file %s for append: %s",
797 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000798 return -1;
799 }
ajsc4c7d0c2005-02-03 19:22:05 +0000800 logfile_fd = fileno(zl->fp);
ajs274a4a42004-12-07 15:39:31 +0000801 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000802 }
803
804 return 1;
805}
David Lamparter6b0655a2014-06-04 06:53:35 +0200806
paul718e3742002-12-13 20:15:29 +0000807/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000808const char *
Stephen Hemminger1423c802008-08-14 17:59:25 +0100809lookup (const struct message *mes, int key)
paul718e3742002-12-13 20:15:29 +0000810{
Stephen Hemminger1423c802008-08-14 17:59:25 +0100811 const struct message *pnt;
paul718e3742002-12-13 20:15:29 +0000812
813 for (pnt = mes; pnt->key != 0; pnt++)
814 if (pnt->key == key)
815 return pnt->str;
816
817 return "";
818}
819
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000820/* Older/faster version of message lookup function, but requires caller to pass
Paul Jakma11486b52008-02-28 23:26:02 +0000821 * in the array size (instead of relying on a 0 key to terminate the search).
822 *
823 * The return value is the message string if found, or the 'none' pointer
824 * provided otherwise.
825 */
hasso8c328f12004-10-05 21:01:23 +0000826const char *
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400827mes_lookup (const struct message *meslist, int max, int index,
828 const char *none, const char *mesname)
paul718e3742002-12-13 20:15:29 +0000829{
Paul Jakma11486b52008-02-28 23:26:02 +0000830 int pos = index - meslist[0].key;
831
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000832 /* first check for best case: index is in range and matches the key
Paul Jakma11486b52008-02-28 23:26:02 +0000833 * value in that slot.
834 * NB: key numbering might be offset from 0. E.g. protocol constants
835 * often start at 1.
836 */
837 if ((pos >= 0) && (pos < max)
838 && (meslist[pos].key == index))
839 return meslist[pos].str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000840
841 /* fall back to linear search */
842 {
843 int i;
844
845 for (i = 0; i < max; i++, meslist++)
846 {
847 if (meslist->key == index)
848 {
Paul Jakma11486b52008-02-28 23:26:02 +0000849 const char *str = (meslist->str ? meslist->str : none);
850
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400851 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
852 index, str, mesname, i, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000853 return str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000854 }
855 }
856 }
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400857 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000858 assert (none);
859 return none;
paul718e3742002-12-13 20:15:29 +0000860}
ajsca359762004-11-19 23:40:16 +0000861
862/* Wrapper around strerror to handle case where it returns NULL. */
863const char *
864safe_strerror(int errnum)
865{
866 const char *s = strerror(errnum);
867 return (s != NULL) ? s : "Unknown error";
868}
ajsf52d13c2005-10-01 17:38:06 +0000869
Paul Jakmad6d672a2006-05-15 16:56:51 +0000870#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
871static const struct zebra_desc_table command_types[] = {
872 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
873 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
874 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
875 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
876 DESC_ENTRY (ZEBRA_INTERFACE_UP),
877 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
878 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
879 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
880 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
881 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
882 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
883 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
884 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
885 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
886 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
887 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
888 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
889 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
890 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
891 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
892 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
893 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
Denis Ovsienko4c783762012-01-21 22:50:19 +0400894 DESC_ENTRY (ZEBRA_HELLO),
Paul Jakmad6d672a2006-05-15 16:56:51 +0000895};
896#undef DESC_ENTRY
897
898static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
899
900static const struct zebra_desc_table *
ajsf52d13c2005-10-01 17:38:06 +0000901zroute_lookup(u_int zroute)
902{
ajsf52d13c2005-10-01 17:38:06 +0000903 u_int i;
904
Balaji.G837d16c2012-09-26 14:09:10 +0530905 if (zroute >= array_size(route_types))
ajsf52d13c2005-10-01 17:38:06 +0000906 {
907 zlog_err("unknown zebra route type: %u", zroute);
908 return &unknown;
909 }
Paul Jakmad6d672a2006-05-15 16:56:51 +0000910 if (zroute == route_types[zroute].type)
ajsf52d13c2005-10-01 17:38:06 +0000911 return &route_types[zroute];
Balaji.G837d16c2012-09-26 14:09:10 +0530912 for (i = 0; i < array_size(route_types); i++)
ajsf52d13c2005-10-01 17:38:06 +0000913 {
Paul Jakmad6d672a2006-05-15 16:56:51 +0000914 if (zroute == route_types[i].type)
ajsf52d13c2005-10-01 17:38:06 +0000915 {
916 zlog_warn("internal error: route type table out of order "
917 "while searching for %u, please notify developers", zroute);
918 return &route_types[i];
919 }
920 }
921 zlog_err("internal error: cannot find route type %u in table!", zroute);
922 return &unknown;
923}
924
925const char *
926zebra_route_string(u_int zroute)
927{
928 return zroute_lookup(zroute)->string;
929}
930
931char
932zebra_route_char(u_int zroute)
933{
934 return zroute_lookup(zroute)->chr;
935}
Paul Jakmad6d672a2006-05-15 16:56:51 +0000936
937const char *
938zserv_command_string (unsigned int command)
939{
Balaji.G837d16c2012-09-26 14:09:10 +0530940 if (command >= array_size(command_types))
Paul Jakmad6d672a2006-05-15 16:56:51 +0000941 {
942 zlog_err ("unknown zserv command type: %u", command);
943 return unknown.string;
944 }
945 return command_types[command].string;
946}
Paul Jakma7514fb72007-05-02 16:05:35 +0000947
Paul Jakma7514fb72007-05-02 16:05:35 +0000948int
949proto_name2num(const char *s)
950{
951 unsigned i;
952
Balaji.G837d16c2012-09-26 14:09:10 +0530953 for (i=0; i<array_size(route_types); ++i)
Paul Jakma7514fb72007-05-02 16:05:35 +0000954 if (strcasecmp(s, route_types[i].string) == 0)
955 return route_types[i].type;
956 return -1;
957}
David Lampartere0ca5fd2009-09-16 01:52:42 +0200958
David Lampartere0ca5fd2009-09-16 01:52:42 +0200959int
960proto_redistnum(int afi, const char *s)
961{
962 if (! s)
963 return -1;
964
965 if (afi == AFI_IP)
966 {
967 if (strncmp (s, "k", 1) == 0)
968 return ZEBRA_ROUTE_KERNEL;
969 else if (strncmp (s, "c", 1) == 0)
970 return ZEBRA_ROUTE_CONNECT;
971 else if (strncmp (s, "s", 1) == 0)
972 return ZEBRA_ROUTE_STATIC;
973 else if (strncmp (s, "r", 1) == 0)
974 return ZEBRA_ROUTE_RIP;
975 else if (strncmp (s, "o", 1) == 0)
976 return ZEBRA_ROUTE_OSPF;
977 else if (strncmp (s, "i", 1) == 0)
978 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400979 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200980 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400981 else if (strncmp (s, "ba", 2) == 0)
982 return ZEBRA_ROUTE_BABEL;
David Lampartere0ca5fd2009-09-16 01:52:42 +0200983 }
984 if (afi == AFI_IP6)
985 {
986 if (strncmp (s, "k", 1) == 0)
987 return ZEBRA_ROUTE_KERNEL;
988 else if (strncmp (s, "c", 1) == 0)
989 return ZEBRA_ROUTE_CONNECT;
990 else if (strncmp (s, "s", 1) == 0)
991 return ZEBRA_ROUTE_STATIC;
992 else if (strncmp (s, "r", 1) == 0)
993 return ZEBRA_ROUTE_RIPNG;
994 else if (strncmp (s, "o", 1) == 0)
995 return ZEBRA_ROUTE_OSPF6;
996 else if (strncmp (s, "i", 1) == 0)
997 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400998 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200999 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +04001000 else if (strncmp (s, "ba", 2) == 0)
1001 return ZEBRA_ROUTE_BABEL;
David Lampartere0ca5fd2009-09-16 01:52:42 +02001002 }
1003 return -1;
1004}