]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_misc.c
Merge pull request #5280 from qlyoung/doc-clean-topotest-json
[mirror_frr.git] / isisd / isis_misc.c
1 /*
2 * IS-IS Rout(e)ing protocol - isis_misc.h
3 * Miscellanous routines
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public Licenseas published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <zebra.h>
25
26 #include "printfrr.h"
27 #include "stream.h"
28 #include "vty.h"
29 #include "hash.h"
30 #include "if.h"
31 #include "command.h"
32
33 #include "isisd/isis_constants.h"
34 #include "isisd/isis_common.h"
35 #include "isisd/isis_flags.h"
36 #include "isisd/isis_circuit.h"
37 #include "isisd/isis_csm.h"
38 #include "isisd/isisd.h"
39 #include "isisd/isis_misc.h"
40
41 #include "isisd/isis_lsp.h"
42 #include "isisd/isis_constants.h"
43 #include "isisd/isis_adjacency.h"
44 #include "isisd/isis_dynhn.h"
45
46 /* staticly assigned vars for printing purposes */
47 struct in_addr new_prefix;
48 /* len of xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx */
49 /* + place for #0 termination */
50 char isonet[51];
51 /* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */
52 char datestring[20];
53 char nlpidstring[30];
54
55 /*
56 * This converts the isonet to its printable format
57 */
58 const char *isonet_print(const uint8_t *from, int len)
59 {
60 int i = 0;
61 char *pos = isonet;
62
63 if (!from)
64 return "unknown";
65
66 while (i < len) {
67 if (i & 1) {
68 sprintf(pos, "%02x", *(from + i));
69 pos += 2;
70 } else {
71 if (i == (len - 1)) { /* No dot at the end of address */
72 sprintf(pos, "%02x", *(from + i));
73 pos += 2;
74 } else {
75 sprintf(pos, "%02x.", *(from + i));
76 pos += 3;
77 }
78 }
79 i++;
80 }
81 *(pos) = '\0';
82 return isonet;
83 }
84
85 /*
86 * Returns 0 on error, length of buff on ok
87 * extract dot from the dotted str, and insert all the number in a buff
88 */
89 int dotformat2buff(uint8_t *buff, const char *dotted)
90 {
91 int dotlen, len = 0;
92 const char *pos = dotted;
93 uint8_t number[3];
94 int nextdotpos = 2;
95
96 number[2] = '\0';
97 dotlen = strlen(dotted);
98 if (dotlen > 50) {
99 /* this can't be an iso net, its too long */
100 return 0;
101 }
102
103 while ((pos - dotted) < dotlen && len < 20) {
104 if (*pos == '.') {
105 /* we expect the . at 2, and than every 5 */
106 if ((pos - dotted) != nextdotpos) {
107 len = 0;
108 break;
109 }
110 nextdotpos += 5;
111 pos++;
112 continue;
113 }
114 /* we must have at least two chars left here */
115 if (dotlen - (pos - dotted) < 2) {
116 len = 0;
117 break;
118 }
119
120 if ((isxdigit((unsigned char)*pos)) &&
121 (isxdigit((unsigned char)*(pos + 1)))) {
122 memcpy(number, pos, 2);
123 pos += 2;
124 } else {
125 len = 0;
126 break;
127 }
128
129 *(buff + len) = (char)strtol((char *)number, NULL, 16);
130 len++;
131 }
132
133 return len;
134 }
135
136 /*
137 * conversion of XXXX.XXXX.XXXX to memory
138 */
139 int sysid2buff(uint8_t *buff, const char *dotted)
140 {
141 int len = 0;
142 const char *pos = dotted;
143 uint8_t number[3];
144
145 number[2] = '\0';
146 // surely not a sysid_string if not 14 length
147 if (strlen(dotted) != 14) {
148 return 0;
149 }
150
151 while (len < ISIS_SYS_ID_LEN) {
152 if (*pos == '.') {
153 /* the . is not positioned correctly */
154 if (((pos - dotted) != 4) && ((pos - dotted) != 9)) {
155 len = 0;
156 break;
157 }
158 pos++;
159 continue;
160 }
161 if ((isxdigit((unsigned char)*pos)) &&
162 (isxdigit((unsigned char)*(pos + 1)))) {
163 memcpy(number, pos, 2);
164 pos += 2;
165 } else {
166 len = 0;
167 break;
168 }
169
170 *(buff + len) = (char)strtol((char *)number, NULL, 16);
171 len++;
172 }
173
174 return len;
175 }
176
177 const char *nlpid2str(uint8_t nlpid)
178 {
179 static char buf[4];
180 switch (nlpid) {
181 case NLPID_IP:
182 return "IPv4";
183 case NLPID_IPV6:
184 return "IPv6";
185 case NLPID_SNAP:
186 return "SNAP";
187 case NLPID_CLNP:
188 return "CLNP";
189 case NLPID_ESIS:
190 return "ES-IS";
191 default:
192 snprintf(buf, sizeof(buf), "%" PRIu8, nlpid);
193 return buf;
194 }
195 }
196
197 /*
198 * converts the nlpids struct (filled by TLV #129)
199 * into a string
200 */
201
202 char *nlpid2string(struct nlpids *nlpids)
203 {
204 char *pos = nlpidstring;
205 int i;
206
207 for (i = 0; i < nlpids->count; i++) {
208 pos += sprintf(pos, "%s", nlpid2str(nlpids->nlpids[i]));
209 if (nlpids->count - i > 1)
210 pos += sprintf(pos, ", ");
211 }
212
213 *(pos) = '\0';
214
215 return nlpidstring;
216 }
217
218 /*
219 * Returns 0 on error, IS-IS Circuit Type on ok
220 */
221 int string2circuit_t(const char *str)
222 {
223
224 if (!str)
225 return 0;
226
227 if (!strcmp(str, "level-1"))
228 return IS_LEVEL_1;
229
230 if (!strcmp(str, "level-2-only") || !strcmp(str, "level-2"))
231 return IS_LEVEL_2;
232
233 if (!strcmp(str, "level-1-2"))
234 return IS_LEVEL_1_AND_2;
235
236 return 0;
237 }
238
239 const char *circuit_state2string(int state)
240 {
241
242 switch (state) {
243 case C_STATE_INIT:
244 return "Init";
245 case C_STATE_CONF:
246 return "Config";
247 case C_STATE_UP:
248 return "Up";
249 default:
250 return "Unknown";
251 }
252 return NULL;
253 }
254
255 const char *circuit_type2string(int type)
256 {
257
258 switch (type) {
259 case CIRCUIT_T_P2P:
260 return "p2p";
261 case CIRCUIT_T_BROADCAST:
262 return "lan";
263 case CIRCUIT_T_LOOPBACK:
264 return "loopback";
265 default:
266 return "Unknown";
267 }
268 return NULL;
269 }
270
271 const char *circuit_t2string(int circuit_t)
272 {
273 switch (circuit_t) {
274 case IS_LEVEL_1:
275 return "L1";
276 case IS_LEVEL_2:
277 return "L2";
278 case IS_LEVEL_1_AND_2:
279 return "L1L2";
280 default:
281 return "??";
282 }
283
284 return NULL; /* not reached */
285 }
286
287 const char *syst2string(int type)
288 {
289 switch (type) {
290 case ISIS_SYSTYPE_ES:
291 return "ES";
292 case ISIS_SYSTYPE_IS:
293 return "IS";
294 case ISIS_SYSTYPE_L1_IS:
295 return "1";
296 case ISIS_SYSTYPE_L2_IS:
297 return "2";
298 default:
299 return "??";
300 }
301
302 return NULL; /* not reached */
303 }
304
305 /*
306 * Print functions - we print to static vars
307 */
308 const char *snpa_print(const uint8_t *from)
309 {
310 return isis_format_id(from, ISIS_SYS_ID_LEN);
311 }
312
313 const char *sysid_print(const uint8_t *from)
314 {
315 return isis_format_id(from, ISIS_SYS_ID_LEN);
316 }
317
318 const char *rawlspid_print(const uint8_t *from)
319 {
320 return isis_format_id(from, 8);
321 }
322
323 #define FORMAT_ID_SIZE sizeof("0000.0000.0000.00-00")
324 const char *isis_format_id(const uint8_t *id, size_t len)
325 {
326 #define FORMAT_BUF_COUNT 4
327 static char buf_ring[FORMAT_BUF_COUNT][FORMAT_ID_SIZE];
328 static size_t cur_buf = 0;
329
330 char *rv;
331
332 cur_buf++;
333 if (cur_buf >= FORMAT_BUF_COUNT)
334 cur_buf = 0;
335
336 rv = buf_ring[cur_buf];
337
338 if (!id) {
339 snprintf(rv, FORMAT_ID_SIZE, "unknown");
340 return rv;
341 }
342
343 if (len < 6) {
344 snprintf(rv, FORMAT_ID_SIZE, "Short ID");
345 return rv;
346 }
347
348 snprintf(rv, FORMAT_ID_SIZE, "%02x%02x.%02x%02x.%02x%02x", id[0], id[1],
349 id[2], id[3], id[4], id[5]);
350
351 if (len > 6)
352 snprintf(rv + 14, FORMAT_ID_SIZE - 14, ".%02x", id[6]);
353 if (len > 7)
354 snprintf(rv + 17, FORMAT_ID_SIZE - 17, "-%02x", id[7]);
355
356 return rv;
357 }
358
359 const char *time2string(uint32_t time)
360 {
361 char *pos = datestring;
362 uint32_t rest;
363
364 if (time == 0)
365 return "-";
366
367 if (time / SECS_PER_YEAR)
368 pos += sprintf(pos, "%uY", time / SECS_PER_YEAR);
369 rest = time % SECS_PER_YEAR;
370 if (rest / SECS_PER_MONTH)
371 pos += sprintf(pos, "%uM", rest / SECS_PER_MONTH);
372 rest = rest % SECS_PER_MONTH;
373 if (rest / SECS_PER_WEEK)
374 pos += sprintf(pos, "%uw", rest / SECS_PER_WEEK);
375 rest = rest % SECS_PER_WEEK;
376 if (rest / SECS_PER_DAY)
377 pos += sprintf(pos, "%ud", rest / SECS_PER_DAY);
378 rest = rest % SECS_PER_DAY;
379 if (rest / SECS_PER_HOUR)
380 pos += sprintf(pos, "%uh", rest / SECS_PER_HOUR);
381 rest = rest % SECS_PER_HOUR;
382 if (rest / SECS_PER_MINUTE)
383 pos += sprintf(pos, "%um", rest / SECS_PER_MINUTE);
384 rest = rest % SECS_PER_MINUTE;
385 if (rest)
386 pos += sprintf(pos, "%us", rest);
387
388 *(pos) = 0;
389
390 return datestring;
391 }
392
393 /*
394 * routine to decrement a timer by a random
395 * number
396 *
397 * first argument is the timer and the second is
398 * the jitter
399 */
400 unsigned long isis_jitter(unsigned long timer, unsigned long jitter)
401 {
402 int j, k;
403
404 if (jitter >= 100)
405 return timer;
406
407 if (timer == 1)
408 return timer;
409 /*
410 * randomizing just the percent value provides
411 * no good random numbers - hence the spread
412 * to RANDOM_SPREAD (100000), which is ok as
413 * most IS-IS timers are no longer than 16 bit
414 */
415
416 j = 1 + (int)((RANDOM_SPREAD * random()) / (RAND_MAX + 1.0));
417
418 k = timer - (timer * (100 - jitter)) / 100;
419
420 timer = timer - (k * j / RANDOM_SPREAD);
421
422 return timer;
423 }
424
425 struct in_addr newprefix2inaddr(uint8_t *prefix_start, uint8_t prefix_masklen)
426 {
427 memset(&new_prefix, 0, sizeof(new_prefix));
428 memcpy(&new_prefix, prefix_start,
429 (prefix_masklen & 0x3F)
430 ? ((((prefix_masklen & 0x3F) - 1) >> 3) + 1)
431 : 0);
432 return new_prefix;
433 }
434
435 /*
436 * Returns the dynamic hostname associated with the passed system ID.
437 * If no dynamic hostname found then returns formatted system ID.
438 */
439 const char *print_sys_hostname(const uint8_t *sysid)
440 {
441 struct isis_dynhn *dyn;
442
443 if (!sysid)
444 return "nullsysid";
445
446 /* For our system ID return our host name */
447 if (memcmp(sysid, isis->sysid, ISIS_SYS_ID_LEN) == 0)
448 return cmd_hostname_get();
449
450 dyn = dynhn_find_by_id(sysid);
451 if (dyn)
452 return dyn->hostname;
453
454 return sysid_print(sysid);
455 }
456
457 /*
458 * This function is a generic utility that logs data of given length.
459 * Move this to a shared lib so that any protocol can use it.
460 */
461 void zlog_dump_data(void *data, int len)
462 {
463 int i;
464 unsigned char *p;
465 unsigned char c;
466 char bytestr[4];
467 char addrstr[10];
468 char hexstr[16 * 3 + 5];
469 char charstr[16 * 1 + 5];
470
471 p = data;
472 memset(bytestr, 0, sizeof(bytestr));
473 memset(addrstr, 0, sizeof(addrstr));
474 memset(hexstr, 0, sizeof(hexstr));
475 memset(charstr, 0, sizeof(charstr));
476
477 for (i = 1; i <= len; i++) {
478 c = *p;
479 if (isalnum(c) == 0)
480 c = '.';
481
482 /* store address for this line */
483 if ((i % 16) == 1)
484 snprintf(addrstr, sizeof(addrstr), "%p", p);
485
486 /* store hex str (for left side) */
487 snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
488 strncat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1);
489
490 /* store char str (for right side) */
491 snprintf(bytestr, sizeof(bytestr), "%c", c);
492 strncat(charstr, bytestr,
493 sizeof(charstr) - strlen(charstr) - 1);
494
495 if ((i % 16) == 0) {
496 /* line completed */
497 zlog_debug("[%8.8s] %-50.50s %s", addrstr, hexstr,
498 charstr);
499 hexstr[0] = 0;
500 charstr[0] = 0;
501 } else if ((i % 8) == 0) {
502 /* half line: add whitespaces */
503 strncat(hexstr, " ",
504 sizeof(hexstr) - strlen(hexstr) - 1);
505 strncat(charstr, " ",
506 sizeof(charstr) - strlen(charstr) - 1);
507 }
508 p++; /* next byte */
509 }
510
511 /* print rest of buffer if not empty */
512 if (strlen(hexstr) > 0)
513 zlog_debug("[%8.8s] %-50.50s %s", addrstr, hexstr, charstr);
514 return;
515 }
516
517 void log_multiline(int priority, const char *prefix, const char *format, ...)
518 {
519 char shortbuf[256];
520 va_list ap;
521 char *p;
522
523 va_start(ap, format);
524 p = vasnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
525 va_end(ap);
526
527 if (!p)
528 return;
529
530 char *saveptr = NULL;
531 for (char *line = strtok_r(p, "\n", &saveptr); line;
532 line = strtok_r(NULL, "\n", &saveptr)) {
533 zlog(priority, "%s%s", prefix, line);
534 }
535
536 if (p != shortbuf)
537 XFREE(MTYPE_TMP, p);
538 }
539
540 void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...)
541 {
542 char shortbuf[256];
543 va_list ap;
544 char *p;
545
546 va_start(ap, format);
547 p = vasnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
548 va_end(ap);
549
550 if (!p)
551 return;
552
553 char *saveptr = NULL;
554 for (char *line = strtok_r(p, "\n", &saveptr); line;
555 line = strtok_r(NULL, "\n", &saveptr)) {
556 vty_out(vty, "%s%s\n", prefix, line);
557 }
558
559 if (p != shortbuf)
560 XFREE(MTYPE_TMP, p);
561 }
562
563 void vty_out_timestr(struct vty *vty, time_t uptime)
564 {
565 struct tm *tm;
566 time_t difftime = time(NULL);
567 difftime -= uptime;
568 tm = gmtime(&difftime);
569
570 if (difftime < ONE_DAY_SECOND)
571 vty_out(vty, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
572 tm->tm_sec);
573 else if (difftime < ONE_WEEK_SECOND)
574 vty_out(vty, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
575 tm->tm_min);
576 else
577 vty_out(vty, "%02dw%dd%02dh", tm->tm_yday / 7,
578 tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
579 vty_out(vty, " ago");
580 }