]>
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
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"
41 #include "isisd/isis_lsp.h"
42 #include "isisd/isis_constants.h"
43 #include "isisd/isis_adjacency.h"
44 #include "isisd/isis_dynhn.h"
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 */
51 /* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */
56 * This converts the isonet to its printable format
58 const char *isonet_print(const uint8_t *from
, int len
)
68 sprintf(pos
, "%02x", *(from
+ i
));
71 if (i
== (len
- 1)) { /* No dot at the end of address */
72 sprintf(pos
, "%02x", *(from
+ i
));
75 sprintf(pos
, "%02x.", *(from
+ i
));
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
89 int dotformat2buff(uint8_t *buff
, const char *dotted
)
92 const char *pos
= dotted
;
97 dotlen
= strlen(dotted
);
99 /* this can't be an iso net, its too long */
103 while ((pos
- dotted
) < dotlen
&& len
< 20) {
105 /* we expect the . at 2, and than every 5 */
106 if ((pos
- dotted
) != nextdotpos
) {
114 /* we must have at least two chars left here */
115 if (dotlen
- (pos
- dotted
) < 2) {
120 if ((isxdigit((int)*pos
)) && (isxdigit((int)*(pos
+ 1)))) {
121 memcpy(number
, pos
, 2);
128 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
136 * conversion of XXXX.XXXX.XXXX to memory
138 int sysid2buff(uint8_t *buff
, const char *dotted
)
141 const char *pos
= dotted
;
145 // surely not a sysid_string if not 14 length
146 if (strlen(dotted
) != 14) {
150 while (len
< ISIS_SYS_ID_LEN
) {
152 /* the . is not positioned correctly */
153 if (((pos
- dotted
) != 4) && ((pos
- dotted
) != 9)) {
160 if ((isxdigit((int)*pos
)) && (isxdigit((int)*(pos
+ 1)))) {
161 memcpy(number
, pos
, 2);
168 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
175 const char *nlpid2str(uint8_t nlpid
)
190 snprintf(buf
, sizeof(buf
), "%" PRIu8
, nlpid
);
196 * converts the nlpids struct (filled by TLV #129)
200 char *nlpid2string(struct nlpids
*nlpids
)
202 char *pos
= nlpidstring
;
205 for (i
= 0; i
< nlpids
->count
; i
++) {
206 pos
+= sprintf(pos
, "%s", nlpid2str(nlpids
->nlpids
[i
]));
207 if (nlpids
->count
- i
> 1)
208 pos
+= sprintf(pos
, ", ");
217 * Returns 0 on error, IS-IS Circuit Type on ok
219 int string2circuit_t(const char *str
)
225 if (!strcmp(str
, "level-1"))
228 if (!strcmp(str
, "level-2-only") || !strcmp(str
, "level-2"))
231 if (!strcmp(str
, "level-1-2"))
232 return IS_LEVEL_1_AND_2
;
237 const char *circuit_state2string(int state
)
253 const char *circuit_type2string(int type
)
259 case CIRCUIT_T_BROADCAST
:
261 case CIRCUIT_T_LOOPBACK
:
269 const char *circuit_t2string(int circuit_t
)
276 case IS_LEVEL_1_AND_2
:
282 return NULL
; /* not reached */
285 const char *syst2string(int type
)
288 case ISIS_SYSTYPE_ES
:
290 case ISIS_SYSTYPE_IS
:
292 case ISIS_SYSTYPE_L1_IS
:
294 case ISIS_SYSTYPE_L2_IS
:
300 return NULL
; /* not reached */
304 * Print functions - we print to static vars
306 const char *snpa_print(const uint8_t *from
)
308 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
311 const char *sysid_print(const uint8_t *from
)
313 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
316 const char *rawlspid_print(const uint8_t *from
)
318 return isis_format_id(from
, 8);
321 #define FORMAT_ID_SIZE sizeof("0000.0000.0000.00-00")
322 const char *isis_format_id(const uint8_t *id
, size_t len
)
324 #define FORMAT_BUF_COUNT 4
325 static char buf_ring
[FORMAT_BUF_COUNT
][FORMAT_ID_SIZE
];
326 static size_t cur_buf
= 0;
331 if (cur_buf
>= FORMAT_BUF_COUNT
)
334 rv
= buf_ring
[cur_buf
];
337 snprintf(rv
, FORMAT_ID_SIZE
, "unknown");
342 snprintf(rv
, FORMAT_ID_SIZE
, "Short ID");
346 snprintf(rv
, FORMAT_ID_SIZE
, "%02x%02x.%02x%02x.%02x%02x", id
[0], id
[1],
347 id
[2], id
[3], id
[4], id
[5]);
350 snprintf(rv
+ 14, FORMAT_ID_SIZE
- 14, ".%02x", id
[6]);
352 snprintf(rv
+ 17, FORMAT_ID_SIZE
- 17, "-%02x", id
[7]);
357 const char *time2string(uint32_t time
)
359 char *pos
= datestring
;
365 if (time
/ SECS_PER_YEAR
)
366 pos
+= sprintf(pos
, "%uY", time
/ SECS_PER_YEAR
);
367 rest
= time
% SECS_PER_YEAR
;
368 if (rest
/ SECS_PER_MONTH
)
369 pos
+= sprintf(pos
, "%uM", rest
/ SECS_PER_MONTH
);
370 rest
= rest
% SECS_PER_MONTH
;
371 if (rest
/ SECS_PER_WEEK
)
372 pos
+= sprintf(pos
, "%uw", rest
/ SECS_PER_WEEK
);
373 rest
= rest
% SECS_PER_WEEK
;
374 if (rest
/ SECS_PER_DAY
)
375 pos
+= sprintf(pos
, "%ud", rest
/ SECS_PER_DAY
);
376 rest
= rest
% SECS_PER_DAY
;
377 if (rest
/ SECS_PER_HOUR
)
378 pos
+= sprintf(pos
, "%uh", rest
/ SECS_PER_HOUR
);
379 rest
= rest
% SECS_PER_HOUR
;
380 if (rest
/ SECS_PER_MINUTE
)
381 pos
+= sprintf(pos
, "%um", rest
/ SECS_PER_MINUTE
);
382 rest
= rest
% SECS_PER_MINUTE
;
384 pos
+= sprintf(pos
, "%us", rest
);
392 * routine to decrement a timer by a random
395 * first argument is the timer and the second is
398 unsigned long isis_jitter(unsigned long timer
, unsigned long jitter
)
408 * randomizing just the percent value provides
409 * no good random numbers - hence the spread
410 * to RANDOM_SPREAD (100000), which is ok as
411 * most IS-IS timers are no longer than 16 bit
414 j
= 1 + (int)((RANDOM_SPREAD
* random()) / (RAND_MAX
+ 1.0));
416 k
= timer
- (timer
* (100 - jitter
)) / 100;
418 timer
= timer
- (k
* j
/ RANDOM_SPREAD
);
423 struct in_addr
newprefix2inaddr(uint8_t *prefix_start
, uint8_t prefix_masklen
)
425 memset(&new_prefix
, 0, sizeof(new_prefix
));
426 memcpy(&new_prefix
, prefix_start
,
427 (prefix_masklen
& 0x3F)
428 ? ((((prefix_masklen
& 0x3F) - 1) >> 3) + 1)
434 * Returns the dynamic hostname associated with the passed system ID.
435 * If no dynamic hostname found then returns formatted system ID.
437 const char *print_sys_hostname(const uint8_t *sysid
)
439 struct isis_dynhn
*dyn
;
444 /* For our system ID return our host name */
445 if (memcmp(sysid
, isis
->sysid
, ISIS_SYS_ID_LEN
) == 0)
446 return cmd_hostname_get();
448 dyn
= dynhn_find_by_id(sysid
);
450 return dyn
->hostname
;
452 return sysid_print(sysid
);
456 * This function is a generic utility that logs data of given length.
457 * Move this to a shared lib so that any protocol can use it.
459 void zlog_dump_data(void *data
, int len
)
466 char hexstr
[16 * 3 + 5];
467 char charstr
[16 * 1 + 5];
470 memset(bytestr
, 0, sizeof(bytestr
));
471 memset(addrstr
, 0, sizeof(addrstr
));
472 memset(hexstr
, 0, sizeof(hexstr
));
473 memset(charstr
, 0, sizeof(charstr
));
475 for (i
= 1; i
<= len
; i
++) {
480 /* store address for this line */
482 snprintf(addrstr
, sizeof(addrstr
), "%p", p
);
484 /* store hex str (for left side) */
485 snprintf(bytestr
, sizeof(bytestr
), "%02X ", *p
);
486 strncat(hexstr
, bytestr
, sizeof(hexstr
) - strlen(hexstr
) - 1);
488 /* store char str (for right side) */
489 snprintf(bytestr
, sizeof(bytestr
), "%c", c
);
490 strncat(charstr
, bytestr
,
491 sizeof(charstr
) - strlen(charstr
) - 1);
495 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
,
499 } else if ((i
% 8) == 0) {
500 /* half line: add whitespaces */
502 sizeof(hexstr
) - strlen(hexstr
) - 1);
503 strncat(charstr
, " ",
504 sizeof(charstr
) - strlen(charstr
) - 1);
509 /* print rest of buffer if not empty */
510 if (strlen(hexstr
) > 0)
511 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
, charstr
);
515 void log_multiline(int priority
, const char *prefix
, const char *format
, ...)
521 va_start(ap
, format
);
522 p
= vasnprintfrr(MTYPE_TMP
, shortbuf
, sizeof(shortbuf
), format
, ap
);
528 char *saveptr
= NULL
;
529 for (char *line
= strtok_r(p
, "\n", &saveptr
); line
;
530 line
= strtok_r(NULL
, "\n", &saveptr
)) {
531 zlog(priority
, "%s%s", prefix
, line
);
538 void vty_multiline(struct vty
*vty
, const char *prefix
, const char *format
, ...)
544 va_start(ap
, format
);
545 p
= vasnprintfrr(MTYPE_TMP
, shortbuf
, sizeof(shortbuf
), format
, ap
);
551 char *saveptr
= NULL
;
552 for (char *line
= strtok_r(p
, "\n", &saveptr
); line
;
553 line
= strtok_r(NULL
, "\n", &saveptr
)) {
554 vty_out(vty
, "%s%s\n", prefix
, line
);
561 void vty_out_timestr(struct vty
*vty
, time_t uptime
)
564 time_t difftime
= time(NULL
);
566 tm
= gmtime(&difftime
);
568 if (difftime
< ONE_DAY_SECOND
)
569 vty_out(vty
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
571 else if (difftime
< ONE_WEEK_SECOND
)
572 vty_out(vty
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
575 vty_out(vty
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
576 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
577 vty_out(vty
, " ago");