]>
git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_misc.c
2 * IS-IS Rout(e)ing protocol - isis_misc.h
3 * Miscellanous routines
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
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)
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
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
32 #include "isisd/isis_constants.h"
33 #include "isisd/isis_common.h"
34 #include "isisd/isis_flags.h"
35 #include "isisd/isis_circuit.h"
36 #include "isisd/isis_csm.h"
37 #include "isisd/isisd.h"
38 #include "isisd/isis_misc.h"
40 #include "isisd/isis_lsp.h"
41 #include "isisd/isis_constants.h"
42 #include "isisd/isis_adjacency.h"
43 #include "isisd/isis_dynhn.h"
45 /* staticly assigned vars for printing purposes */
46 struct in_addr new_prefix
;
47 /* len of xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx */
48 /* + place for #0 termination */
50 /* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */
55 * This converts the isonet to its printable format
57 const char *isonet_print(const uint8_t *from
, int len
)
67 sprintf(pos
, "%02x", *(from
+ i
));
70 if (i
== (len
- 1)) { /* No dot at the end of address */
71 sprintf(pos
, "%02x", *(from
+ i
));
74 sprintf(pos
, "%02x.", *(from
+ i
));
85 * Returns 0 on error, length of buff on ok
86 * extract dot from the dotted str, and insert all the number in a buff
88 int dotformat2buff(uint8_t *buff
, const char *dotted
)
91 const char *pos
= dotted
;
96 dotlen
= strlen(dotted
);
98 /* this can't be an iso net, its too long */
102 while ((pos
- dotted
) < dotlen
&& len
< 20) {
104 /* we expect the . at 2, and than every 5 */
105 if ((pos
- dotted
) != nextdotpos
) {
113 /* we must have at least two chars left here */
114 if (dotlen
- (pos
- dotted
) < 2) {
119 if ((isxdigit((int)*pos
)) && (isxdigit((int)*(pos
+ 1)))) {
120 memcpy(number
, pos
, 2);
127 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
135 * conversion of XXXX.XXXX.XXXX to memory
137 int sysid2buff(uint8_t *buff
, const char *dotted
)
140 const char *pos
= dotted
;
144 // surely not a sysid_string if not 14 length
145 if (strlen(dotted
) != 14) {
149 while (len
< ISIS_SYS_ID_LEN
) {
151 /* the . is not positioned correctly */
152 if (((pos
- dotted
) != 4) && ((pos
- dotted
) != 9)) {
159 if ((isxdigit((int)*pos
)) && (isxdigit((int)*(pos
+ 1)))) {
160 memcpy(number
, pos
, 2);
167 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
174 const char *nlpid2str(uint8_t nlpid
)
189 snprintf(buf
, sizeof(buf
), "%" PRIu8
, nlpid
);
195 * converts the nlpids struct (filled by TLV #129)
199 char *nlpid2string(struct nlpids
*nlpids
)
201 char *pos
= nlpidstring
;
204 for (i
= 0; i
< nlpids
->count
; i
++) {
205 pos
+= sprintf(pos
, "%s", nlpid2str(nlpids
->nlpids
[i
]));
206 if (nlpids
->count
- i
> 1)
207 pos
+= sprintf(pos
, ", ");
216 * Returns 0 on error, IS-IS Circuit Type on ok
218 int string2circuit_t(const char *str
)
224 if (!strcmp(str
, "level-1"))
227 if (!strcmp(str
, "level-2-only") || !strcmp(str
, "level-2"))
230 if (!strcmp(str
, "level-1-2"))
231 return IS_LEVEL_1_AND_2
;
236 const char *circuit_state2string(int state
)
252 const char *circuit_type2string(int type
)
258 case CIRCUIT_T_BROADCAST
:
260 case CIRCUIT_T_LOOPBACK
:
268 const char *circuit_t2string(int circuit_t
)
275 case IS_LEVEL_1_AND_2
:
281 return NULL
; /* not reached */
284 const char *syst2string(int type
)
287 case ISIS_SYSTYPE_ES
:
289 case ISIS_SYSTYPE_IS
:
291 case ISIS_SYSTYPE_L1_IS
:
293 case ISIS_SYSTYPE_L2_IS
:
299 return NULL
; /* not reached */
303 * Print functions - we print to static vars
305 const char *snpa_print(const uint8_t *from
)
307 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
310 const char *sysid_print(const uint8_t *from
)
312 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
315 const char *rawlspid_print(const uint8_t *from
)
317 return isis_format_id(from
, 8);
320 #define FORMAT_ID_SIZE sizeof("0000.0000.0000.00-00")
321 const char *isis_format_id(const uint8_t *id
, size_t len
)
323 #define FORMAT_BUF_COUNT 4
324 static char buf_ring
[FORMAT_BUF_COUNT
][FORMAT_ID_SIZE
];
325 static size_t cur_buf
= 0;
330 if (cur_buf
>= FORMAT_BUF_COUNT
)
333 rv
= buf_ring
[cur_buf
];
336 snprintf(rv
, FORMAT_ID_SIZE
, "unknown");
341 snprintf(rv
, FORMAT_ID_SIZE
, "Short ID");
345 snprintf(rv
, FORMAT_ID_SIZE
, "%02x%02x.%02x%02x.%02x%02x", id
[0], id
[1],
346 id
[2], id
[3], id
[4], id
[5]);
349 snprintf(rv
+ 14, FORMAT_ID_SIZE
- 14, ".%02x", id
[6]);
351 snprintf(rv
+ 17, FORMAT_ID_SIZE
- 17, "-%02x", id
[7]);
356 const char *time2string(uint32_t time
)
358 char *pos
= datestring
;
364 if (time
/ SECS_PER_YEAR
)
365 pos
+= sprintf(pos
, "%uY", time
/ SECS_PER_YEAR
);
366 rest
= time
% SECS_PER_YEAR
;
367 if (rest
/ SECS_PER_MONTH
)
368 pos
+= sprintf(pos
, "%uM", rest
/ SECS_PER_MONTH
);
369 rest
= rest
% SECS_PER_MONTH
;
370 if (rest
/ SECS_PER_WEEK
)
371 pos
+= sprintf(pos
, "%uw", rest
/ SECS_PER_WEEK
);
372 rest
= rest
% SECS_PER_WEEK
;
373 if (rest
/ SECS_PER_DAY
)
374 pos
+= sprintf(pos
, "%ud", rest
/ SECS_PER_DAY
);
375 rest
= rest
% SECS_PER_DAY
;
376 if (rest
/ SECS_PER_HOUR
)
377 pos
+= sprintf(pos
, "%uh", rest
/ SECS_PER_HOUR
);
378 rest
= rest
% SECS_PER_HOUR
;
379 if (rest
/ SECS_PER_MINUTE
)
380 pos
+= sprintf(pos
, "%um", rest
/ SECS_PER_MINUTE
);
381 rest
= rest
% SECS_PER_MINUTE
;
383 pos
+= sprintf(pos
, "%us", rest
);
391 * routine to decrement a timer by a random
394 * first argument is the timer and the second is
397 unsigned long isis_jitter(unsigned long timer
, unsigned long jitter
)
407 * randomizing just the percent value provides
408 * no good random numbers - hence the spread
409 * to RANDOM_SPREAD (100000), which is ok as
410 * most IS-IS timers are no longer than 16 bit
413 j
= 1 + (int)((RANDOM_SPREAD
* random()) / (RAND_MAX
+ 1.0));
415 k
= timer
- (timer
* (100 - jitter
)) / 100;
417 timer
= timer
- (k
* j
/ RANDOM_SPREAD
);
422 struct in_addr
newprefix2inaddr(uint8_t *prefix_start
, uint8_t prefix_masklen
)
424 memset(&new_prefix
, 0, sizeof(new_prefix
));
425 memcpy(&new_prefix
, prefix_start
,
426 (prefix_masklen
& 0x3F)
427 ? ((((prefix_masklen
& 0x3F) - 1) >> 3) + 1)
433 * Returns the dynamic hostname associated with the passed system ID.
434 * If no dynamic hostname found then returns formatted system ID.
436 const char *print_sys_hostname(const uint8_t *sysid
)
438 struct isis_dynhn
*dyn
;
443 /* For our system ID return our host name */
444 if (memcmp(sysid
, isis
->sysid
, ISIS_SYS_ID_LEN
) == 0)
445 return cmd_hostname_get();
447 dyn
= dynhn_find_by_id(sysid
);
449 return dyn
->hostname
;
451 return sysid_print(sysid
);
455 * This function is a generic utility that logs data of given length.
456 * Move this to a shared lib so that any protocol can use it.
458 void zlog_dump_data(void *data
, int len
)
465 char hexstr
[16 * 3 + 5];
466 char charstr
[16 * 1 + 5];
469 memset(bytestr
, 0, sizeof(bytestr
));
470 memset(addrstr
, 0, sizeof(addrstr
));
471 memset(hexstr
, 0, sizeof(hexstr
));
472 memset(charstr
, 0, sizeof(charstr
));
474 for (i
= 1; i
<= len
; i
++) {
479 /* store address for this line */
481 snprintf(addrstr
, sizeof(addrstr
), "%p", p
);
483 /* store hex str (for left side) */
484 snprintf(bytestr
, sizeof(bytestr
), "%02X ", *p
);
485 strncat(hexstr
, bytestr
, sizeof(hexstr
) - strlen(hexstr
) - 1);
487 /* store char str (for right side) */
488 snprintf(bytestr
, sizeof(bytestr
), "%c", c
);
489 strncat(charstr
, bytestr
,
490 sizeof(charstr
) - strlen(charstr
) - 1);
494 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
,
498 } else if ((i
% 8) == 0) {
499 /* half line: add whitespaces */
501 sizeof(hexstr
) - strlen(hexstr
) - 1);
502 strncat(charstr
, " ",
503 sizeof(charstr
) - strlen(charstr
) - 1);
508 /* print rest of buffer if not empty */
509 if (strlen(hexstr
) > 0)
510 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
, charstr
);
514 static char *qasprintf(const char *format
, va_list ap
)
522 size
= vsnprintf(p
, size
, format
, ap
);
530 p
= XMALLOC(MTYPE_TMP
, size
);
532 size
= vsnprintf(p
, size
, format
, aq
);
543 void log_multiline(int priority
, const char *prefix
, const char *format
, ...)
548 va_start(ap
, format
);
549 p
= qasprintf(format
, ap
);
555 char *saveptr
= NULL
;
556 for (char *line
= strtok_r(p
, "\n", &saveptr
); line
;
557 line
= strtok_r(NULL
, "\n", &saveptr
)) {
558 zlog(priority
, "%s%s", prefix
, line
);
564 void vty_multiline(struct vty
*vty
, const char *prefix
, const char *format
, ...)
569 va_start(ap
, format
);
570 p
= qasprintf(format
, ap
);
576 char *saveptr
= NULL
;
577 for (char *line
= strtok_r(p
, "\n", &saveptr
); line
;
578 line
= strtok_r(NULL
, "\n", &saveptr
)) {
579 vty_out(vty
, "%s%s\n", prefix
, line
);
585 void vty_out_timestr(struct vty
*vty
, time_t uptime
)
588 time_t difftime
= time(NULL
);
590 tm
= gmtime(&difftime
);
592 if (difftime
< ONE_DAY_SECOND
)
593 vty_out(vty
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
595 else if (difftime
< ONE_WEEK_SECOND
)
596 vty_out(vty
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
599 vty_out(vty
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
600 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
601 vty_out(vty
, " ago");