]>
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
34 #include "isisd/isis_constants.h"
35 #include "isisd/isis_common.h"
36 #include "isisd/isis_flags.h"
37 #include "isisd/isis_circuit.h"
38 #include "isisd/isis_csm.h"
39 #include "isisd/isisd.h"
40 #include "isisd/isis_misc.h"
42 #include "isisd/isis_lsp.h"
43 #include "isisd/isis_constants.h"
44 #include "isisd/isis_adjacency.h"
45 #include "isisd/isis_dynhn.h"
47 /* staticly assigned vars for printing purposes */
48 struct in_addr new_prefix
;
49 /* len of xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx */
50 /* + place for #0 termination */
52 /* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */
57 * This converts the isonet to its printable format
59 const char *isonet_print(const uint8_t *from
, int len
)
69 sprintf(pos
, "%02x", *(from
+ i
));
72 if (i
== (len
- 1)) { /* No dot at the end of address */
73 sprintf(pos
, "%02x", *(from
+ i
));
76 sprintf(pos
, "%02x.", *(from
+ i
));
87 * Returns 0 on error, length of buff on ok
88 * extract dot from the dotted str, and insert all the number in a buff
90 int dotformat2buff(uint8_t *buff
, const char *dotted
)
93 const char *pos
= dotted
;
98 dotlen
= strlen(dotted
);
100 /* this can't be an iso net, its too long */
104 while ((pos
- dotted
) < dotlen
&& len
< 20) {
106 /* we expect the . at 2, and than every 5 */
107 if ((pos
- dotted
) != nextdotpos
) {
115 /* we must have at least two chars left here */
116 if (dotlen
- (pos
- dotted
) < 2) {
121 if ((isxdigit((unsigned char)*pos
)) &&
122 (isxdigit((unsigned char)*(pos
+ 1)))) {
123 memcpy(number
, pos
, 2);
130 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
138 * conversion of XXXX.XXXX.XXXX to memory
140 int sysid2buff(uint8_t *buff
, const char *dotted
)
143 const char *pos
= dotted
;
147 // surely not a sysid_string if not 14 length
148 if (strlen(dotted
) != 14) {
152 while (len
< ISIS_SYS_ID_LEN
) {
154 /* the . is not positioned correctly */
155 if (((pos
- dotted
) != 4) && ((pos
- dotted
) != 9)) {
162 if ((isxdigit((unsigned char)*pos
)) &&
163 (isxdigit((unsigned char)*(pos
+ 1)))) {
164 memcpy(number
, pos
, 2);
171 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
178 const char *nlpid2str(uint8_t nlpid
)
193 snprintf(buf
, sizeof(buf
), "%" PRIu8
, nlpid
);
199 * converts the nlpids struct (filled by TLV #129)
203 char *nlpid2string(struct nlpids
*nlpids
)
205 char *pos
= nlpidstring
;
208 for (i
= 0; i
< nlpids
->count
; i
++) {
209 pos
+= sprintf(pos
, "%s", nlpid2str(nlpids
->nlpids
[i
]));
210 if (nlpids
->count
- i
> 1)
211 pos
+= sprintf(pos
, ", ");
220 * Returns 0 on error, IS-IS Circuit Type on ok
222 int string2circuit_t(const char *str
)
228 if (!strcmp(str
, "level-1"))
231 if (!strcmp(str
, "level-2-only") || !strcmp(str
, "level-2"))
234 if (!strcmp(str
, "level-1-2"))
235 return IS_LEVEL_1_AND_2
;
240 const char *circuit_state2string(int state
)
256 const char *circuit_type2string(int type
)
262 case CIRCUIT_T_BROADCAST
:
264 case CIRCUIT_T_LOOPBACK
:
272 const char *circuit_t2string(int circuit_t
)
279 case IS_LEVEL_1_AND_2
:
285 return NULL
; /* not reached */
288 const char *syst2string(int type
)
291 case ISIS_SYSTYPE_ES
:
293 case ISIS_SYSTYPE_IS
:
295 case ISIS_SYSTYPE_L1_IS
:
297 case ISIS_SYSTYPE_L2_IS
:
303 return NULL
; /* not reached */
307 * Print functions - we print to static vars
309 const char *snpa_print(const uint8_t *from
)
311 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
314 const char *sysid_print(const uint8_t *from
)
316 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
319 const char *rawlspid_print(const uint8_t *from
)
321 return isis_format_id(from
, 8);
324 #define FORMAT_ID_SIZE sizeof("0000.0000.0000.00-00")
325 const char *isis_format_id(const uint8_t *id
, size_t len
)
327 #define FORMAT_BUF_COUNT 4
328 static char buf_ring
[FORMAT_BUF_COUNT
][FORMAT_ID_SIZE
];
329 static size_t cur_buf
= 0;
334 if (cur_buf
>= FORMAT_BUF_COUNT
)
337 rv
= buf_ring
[cur_buf
];
340 snprintf(rv
, FORMAT_ID_SIZE
, "unknown");
345 snprintf(rv
, FORMAT_ID_SIZE
, "Short ID");
349 snprintf(rv
, FORMAT_ID_SIZE
, "%02x%02x.%02x%02x.%02x%02x", id
[0], id
[1],
350 id
[2], id
[3], id
[4], id
[5]);
353 snprintf(rv
+ 14, FORMAT_ID_SIZE
- 14, ".%02x", id
[6]);
355 snprintf(rv
+ 17, FORMAT_ID_SIZE
- 17, "-%02x", id
[7]);
360 const char *time2string(uint32_t time
)
362 char *pos
= datestring
;
368 if (time
/ SECS_PER_YEAR
)
369 pos
+= sprintf(pos
, "%uY", time
/ SECS_PER_YEAR
);
370 rest
= time
% SECS_PER_YEAR
;
371 if (rest
/ SECS_PER_MONTH
)
372 pos
+= sprintf(pos
, "%uM", rest
/ SECS_PER_MONTH
);
373 rest
= rest
% SECS_PER_MONTH
;
374 if (rest
/ SECS_PER_WEEK
)
375 pos
+= sprintf(pos
, "%uw", rest
/ SECS_PER_WEEK
);
376 rest
= rest
% SECS_PER_WEEK
;
377 if (rest
/ SECS_PER_DAY
)
378 pos
+= sprintf(pos
, "%ud", rest
/ SECS_PER_DAY
);
379 rest
= rest
% SECS_PER_DAY
;
380 if (rest
/ SECS_PER_HOUR
)
381 pos
+= sprintf(pos
, "%uh", rest
/ SECS_PER_HOUR
);
382 rest
= rest
% SECS_PER_HOUR
;
383 if (rest
/ SECS_PER_MINUTE
)
384 pos
+= sprintf(pos
, "%um", rest
/ SECS_PER_MINUTE
);
385 rest
= rest
% SECS_PER_MINUTE
;
387 pos
+= sprintf(pos
, "%us", rest
);
395 * routine to decrement a timer by a random
398 * first argument is the timer and the second is
401 unsigned long isis_jitter(unsigned long timer
, unsigned long jitter
)
411 * randomizing just the percent value provides
412 * no good random numbers - hence the spread
413 * to RANDOM_SPREAD (100000), which is ok as
414 * most IS-IS timers are no longer than 16 bit
417 j
= 1 + (int)((RANDOM_SPREAD
* frr_weak_random()) / (RAND_MAX
+ 1.0));
419 k
= timer
- (timer
* (100 - jitter
)) / 100;
421 timer
= timer
- (k
* j
/ RANDOM_SPREAD
);
426 struct in_addr
newprefix2inaddr(uint8_t *prefix_start
, uint8_t prefix_masklen
)
428 memset(&new_prefix
, 0, sizeof(new_prefix
));
429 memcpy(&new_prefix
, prefix_start
,
430 (prefix_masklen
& 0x3F)
431 ? ((((prefix_masklen
& 0x3F) - 1) >> 3) + 1)
437 * Returns the dynamic hostname associated with the passed system ID.
438 * If no dynamic hostname found then returns formatted system ID.
440 const char *print_sys_hostname(const uint8_t *sysid
)
442 struct isis_dynhn
*dyn
;
447 /* For our system ID return our host name */
448 if (memcmp(sysid
, isis
->sysid
, ISIS_SYS_ID_LEN
) == 0)
449 return cmd_hostname_get();
451 dyn
= dynhn_find_by_id(sysid
);
453 return dyn
->hostname
;
455 return sysid_print(sysid
);
459 * This function is a generic utility that logs data of given length.
460 * Move this to a shared lib so that any protocol can use it.
462 void zlog_dump_data(void *data
, int len
)
469 char hexstr
[16 * 3 + 5];
470 char charstr
[16 * 1 + 5];
473 memset(bytestr
, 0, sizeof(bytestr
));
474 memset(addrstr
, 0, sizeof(addrstr
));
475 memset(hexstr
, 0, sizeof(hexstr
));
476 memset(charstr
, 0, sizeof(charstr
));
478 for (i
= 1; i
<= len
; i
++) {
483 /* store address for this line */
485 snprintf(addrstr
, sizeof(addrstr
), "%p", p
);
487 /* store hex str (for left side) */
488 snprintf(bytestr
, sizeof(bytestr
), "%02X ", *p
);
489 strncat(hexstr
, bytestr
, sizeof(hexstr
) - strlen(hexstr
) - 1);
491 /* store char str (for right side) */
492 snprintf(bytestr
, sizeof(bytestr
), "%c", c
);
493 strncat(charstr
, bytestr
,
494 sizeof(charstr
) - strlen(charstr
) - 1);
498 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
,
502 } else if ((i
% 8) == 0) {
503 /* half line: add whitespaces */
505 sizeof(hexstr
) - strlen(hexstr
) - 1);
506 strncat(charstr
, " ",
507 sizeof(charstr
) - strlen(charstr
) - 1);
512 /* print rest of buffer if not empty */
513 if (strlen(hexstr
) > 0)
514 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
, charstr
);
518 void log_multiline(int priority
, const char *prefix
, const char *format
, ...)
524 va_start(ap
, format
);
525 p
= vasnprintfrr(MTYPE_TMP
, shortbuf
, sizeof(shortbuf
), format
, ap
);
531 char *saveptr
= NULL
;
532 for (char *line
= strtok_r(p
, "\n", &saveptr
); line
;
533 line
= strtok_r(NULL
, "\n", &saveptr
)) {
534 zlog(priority
, "%s%s", prefix
, line
);
541 void vty_multiline(struct vty
*vty
, const char *prefix
, const char *format
, ...)
547 va_start(ap
, format
);
548 p
= vasnprintfrr(MTYPE_TMP
, shortbuf
, sizeof(shortbuf
), format
, ap
);
554 char *saveptr
= NULL
;
555 for (char *line
= strtok_r(p
, "\n", &saveptr
); line
;
556 line
= strtok_r(NULL
, "\n", &saveptr
)) {
557 vty_out(vty
, "%s%s\n", prefix
, line
);
564 void vty_out_timestr(struct vty
*vty
, time_t uptime
)
566 time_t difftime
= time(NULL
);
567 char buf
[MONOTIME_STRLEN
];
571 frrtime_to_interval(difftime
, buf
, sizeof(buf
));
573 vty_out(vty
, "%s ago", buf
);