]>
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/dict.h"
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((int)*pos
)) && (isxdigit((int)*(pos
+ 1)))) {
122 memcpy(number
, pos
, 2);
129 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
137 * conversion of XXXX.XXXX.XXXX to memory
139 int sysid2buff(uint8_t *buff
, const char *dotted
)
142 const char *pos
= dotted
;
146 // surely not a sysid_string if not 14 length
147 if (strlen(dotted
) != 14) {
151 while (len
< ISIS_SYS_ID_LEN
) {
153 /* the . is not positioned correctly */
154 if (((pos
- dotted
) != 4) && ((pos
- dotted
) != 9)) {
161 if ((isxdigit((int)*pos
)) && (isxdigit((int)*(pos
+ 1)))) {
162 memcpy(number
, pos
, 2);
169 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
176 const char *nlpid2str(uint8_t nlpid
)
191 snprintf(buf
, sizeof(buf
), "%" PRIu8
, nlpid
);
197 * converts the nlpids struct (filled by TLV #129)
201 char *nlpid2string(struct nlpids
*nlpids
)
203 char *pos
= nlpidstring
;
206 for (i
= 0; i
< nlpids
->count
; i
++) {
207 pos
+= sprintf(pos
, "%s", nlpid2str(nlpids
->nlpids
[i
]));
208 if (nlpids
->count
- i
> 1)
209 pos
+= sprintf(pos
, ", ");
218 * Returns 0 on error, IS-IS Circuit Type on ok
220 int string2circuit_t(const char *str
)
226 if (!strcmp(str
, "level-1"))
229 if (!strcmp(str
, "level-2-only") || !strcmp(str
, "level-2"))
232 if (!strcmp(str
, "level-1-2"))
233 return IS_LEVEL_1_AND_2
;
238 const char *circuit_state2string(int state
)
254 const char *circuit_type2string(int type
)
260 case CIRCUIT_T_BROADCAST
:
262 case CIRCUIT_T_LOOPBACK
:
270 const char *circuit_t2string(int circuit_t
)
277 case IS_LEVEL_1_AND_2
:
283 return NULL
; /* not reached */
286 const char *syst2string(int type
)
289 case ISIS_SYSTYPE_ES
:
291 case ISIS_SYSTYPE_IS
:
293 case ISIS_SYSTYPE_L1_IS
:
295 case ISIS_SYSTYPE_L2_IS
:
301 return NULL
; /* not reached */
305 * Print functions - we print to static vars
307 const char *snpa_print(const uint8_t *from
)
309 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
312 const char *sysid_print(const uint8_t *from
)
314 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
317 const char *rawlspid_print(const uint8_t *from
)
319 return isis_format_id(from
, 8);
322 #define FORMAT_ID_SIZE sizeof("0000.0000.0000.00-00")
323 const char *isis_format_id(const uint8_t *id
, size_t len
)
325 #define FORMAT_BUF_COUNT 4
326 static char buf_ring
[FORMAT_BUF_COUNT
][FORMAT_ID_SIZE
];
327 static size_t cur_buf
= 0;
332 if (cur_buf
>= FORMAT_BUF_COUNT
)
335 rv
= buf_ring
[cur_buf
];
338 snprintf(rv
, FORMAT_ID_SIZE
, "unknown");
343 snprintf(rv
, FORMAT_ID_SIZE
, "Short ID");
347 snprintf(rv
, FORMAT_ID_SIZE
, "%02x%02x.%02x%02x.%02x%02x", id
[0], id
[1],
348 id
[2], id
[3], id
[4], id
[5]);
351 snprintf(rv
+ 14, FORMAT_ID_SIZE
- 14, ".%02x", id
[6]);
353 snprintf(rv
+ 17, FORMAT_ID_SIZE
- 17, "-%02x", id
[7]);
358 const char *time2string(uint32_t time
)
360 char *pos
= datestring
;
366 if (time
/ SECS_PER_YEAR
)
367 pos
+= sprintf(pos
, "%uY", time
/ SECS_PER_YEAR
);
368 rest
= time
% SECS_PER_YEAR
;
369 if (rest
/ SECS_PER_MONTH
)
370 pos
+= sprintf(pos
, "%uM", rest
/ SECS_PER_MONTH
);
371 rest
= rest
% SECS_PER_MONTH
;
372 if (rest
/ SECS_PER_WEEK
)
373 pos
+= sprintf(pos
, "%uw", rest
/ SECS_PER_WEEK
);
374 rest
= rest
% SECS_PER_WEEK
;
375 if (rest
/ SECS_PER_DAY
)
376 pos
+= sprintf(pos
, "%ud", rest
/ SECS_PER_DAY
);
377 rest
= rest
% SECS_PER_DAY
;
378 if (rest
/ SECS_PER_HOUR
)
379 pos
+= sprintf(pos
, "%uh", rest
/ SECS_PER_HOUR
);
380 rest
= rest
% SECS_PER_HOUR
;
381 if (rest
/ SECS_PER_MINUTE
)
382 pos
+= sprintf(pos
, "%um", rest
/ SECS_PER_MINUTE
);
383 rest
= rest
% SECS_PER_MINUTE
;
385 pos
+= sprintf(pos
, "%us", rest
);
393 * routine to decrement a timer by a random
396 * first argument is the timer and the second is
399 unsigned long isis_jitter(unsigned long timer
, unsigned long jitter
)
409 * randomizing just the percent value provides
410 * no good random numbers - hence the spread
411 * to RANDOM_SPREAD (100000), which is ok as
412 * most IS-IS timers are no longer than 16 bit
415 j
= 1 + (int)((RANDOM_SPREAD
* random()) / (RAND_MAX
+ 1.0));
417 k
= timer
- (timer
* (100 - jitter
)) / 100;
419 timer
= timer
- (k
* j
/ RANDOM_SPREAD
);
424 struct in_addr
newprefix2inaddr(uint8_t *prefix_start
, uint8_t prefix_masklen
)
426 memset(&new_prefix
, 0, sizeof(new_prefix
));
427 memcpy(&new_prefix
, prefix_start
,
428 (prefix_masklen
& 0x3F)
429 ? ((((prefix_masklen
& 0x3F) - 1) >> 3) + 1)
435 * Returns the dynamic hostname associated with the passed system ID.
436 * If no dynamic hostname found then returns formatted system ID.
438 const char *print_sys_hostname(const uint8_t *sysid
)
440 struct isis_dynhn
*dyn
;
445 /* For our system ID return our host name */
446 if (memcmp(sysid
, isis
->sysid
, ISIS_SYS_ID_LEN
) == 0)
447 return cmd_hostname_get();
449 dyn
= dynhn_find_by_id(sysid
);
451 return dyn
->hostname
;
453 return sysid_print(sysid
);
457 * This function is a generic utility that logs data of given length.
458 * Move this to a shared lib so that any protocol can use it.
460 void zlog_dump_data(void *data
, int len
)
467 char hexstr
[16 * 3 + 5];
468 char charstr
[16 * 1 + 5];
471 memset(bytestr
, 0, sizeof(bytestr
));
472 memset(addrstr
, 0, sizeof(addrstr
));
473 memset(hexstr
, 0, sizeof(hexstr
));
474 memset(charstr
, 0, sizeof(charstr
));
476 for (i
= 1; i
<= len
; i
++) {
481 /* store address for this line */
483 snprintf(addrstr
, sizeof(addrstr
), "%p", p
);
485 /* store hex str (for left side) */
486 snprintf(bytestr
, sizeof(bytestr
), "%02X ", *p
);
487 strncat(hexstr
, bytestr
, sizeof(hexstr
) - strlen(hexstr
) - 1);
489 /* store char str (for right side) */
490 snprintf(bytestr
, sizeof(bytestr
), "%c", c
);
491 strncat(charstr
, bytestr
,
492 sizeof(charstr
) - strlen(charstr
) - 1);
496 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
,
500 } else if ((i
% 8) == 0) {
501 /* half line: add whitespaces */
503 sizeof(hexstr
) - strlen(hexstr
) - 1);
504 strncat(charstr
, " ",
505 sizeof(charstr
) - strlen(charstr
) - 1);
510 /* print rest of buffer if not empty */
511 if (strlen(hexstr
) > 0)
512 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
, charstr
);
516 static char *qasprintf(const char *format
, va_list ap
)
524 size
= vsnprintf(p
, size
, format
, ap
);
532 p
= XMALLOC(MTYPE_TMP
, size
);
534 size
= vsnprintf(p
, size
, format
, aq
);
545 void log_multiline(int priority
, const char *prefix
, const char *format
, ...)
550 va_start(ap
, format
);
551 p
= qasprintf(format
, ap
);
557 char *saveptr
= NULL
;
558 for (char *line
= strtok_r(p
, "\n", &saveptr
); line
;
559 line
= strtok_r(NULL
, "\n", &saveptr
)) {
560 zlog(priority
, "%s%s", prefix
, line
);
566 void vty_multiline(struct vty
*vty
, const char *prefix
, const char *format
, ...)
571 va_start(ap
, format
);
572 p
= qasprintf(format
, ap
);
578 char *saveptr
= NULL
;
579 for (char *line
= strtok_r(p
, "\n", &saveptr
); line
;
580 line
= strtok_r(NULL
, "\n", &saveptr
)) {
581 vty_out(vty
, "%s%s\n", prefix
, line
);
587 void vty_out_timestr(struct vty
*vty
, time_t uptime
)
590 time_t difftime
= time(NULL
);
592 tm
= gmtime(&difftime
);
594 if (difftime
< ONE_DAY_SECOND
)
595 vty_out(vty
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
597 else if (difftime
< ONE_WEEK_SECOND
)
598 vty_out(vty
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
601 vty_out(vty
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
602 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
603 vty_out(vty
, " ago");