]>
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((unsigned char)*pos
)) &&
121 (isxdigit((unsigned char)*(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((unsigned char)*pos
)) &&
162 (isxdigit((unsigned char)*(pos
+ 1)))) {
163 memcpy(number
, pos
, 2);
170 *(buff
+ len
) = (char)strtol((char *)number
, NULL
, 16);
177 const char *nlpid2str(uint8_t nlpid
)
192 snprintf(buf
, sizeof(buf
), "%" PRIu8
, nlpid
);
198 * converts the nlpids struct (filled by TLV #129)
202 char *nlpid2string(struct nlpids
*nlpids
)
204 char *pos
= nlpidstring
;
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
, ", ");
219 * Returns 0 on error, IS-IS Circuit Type on ok
221 int string2circuit_t(const char *str
)
227 if (!strcmp(str
, "level-1"))
230 if (!strcmp(str
, "level-2-only") || !strcmp(str
, "level-2"))
233 if (!strcmp(str
, "level-1-2"))
234 return IS_LEVEL_1_AND_2
;
239 const char *circuit_state2string(int state
)
255 const char *circuit_type2string(int type
)
261 case CIRCUIT_T_BROADCAST
:
263 case CIRCUIT_T_LOOPBACK
:
271 const char *circuit_t2string(int circuit_t
)
278 case IS_LEVEL_1_AND_2
:
284 return NULL
; /* not reached */
287 const char *syst2string(int type
)
290 case ISIS_SYSTYPE_ES
:
292 case ISIS_SYSTYPE_IS
:
294 case ISIS_SYSTYPE_L1_IS
:
296 case ISIS_SYSTYPE_L2_IS
:
302 return NULL
; /* not reached */
306 * Print functions - we print to static vars
308 const char *snpa_print(const uint8_t *from
)
310 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
313 const char *sysid_print(const uint8_t *from
)
315 return isis_format_id(from
, ISIS_SYS_ID_LEN
);
318 const char *rawlspid_print(const uint8_t *from
)
320 return isis_format_id(from
, 8);
323 #define FORMAT_ID_SIZE sizeof("0000.0000.0000.00-00")
324 const char *isis_format_id(const uint8_t *id
, size_t len
)
326 #define FORMAT_BUF_COUNT 4
327 static char buf_ring
[FORMAT_BUF_COUNT
][FORMAT_ID_SIZE
];
328 static size_t cur_buf
= 0;
333 if (cur_buf
>= FORMAT_BUF_COUNT
)
336 rv
= buf_ring
[cur_buf
];
339 snprintf(rv
, FORMAT_ID_SIZE
, "unknown");
344 snprintf(rv
, FORMAT_ID_SIZE
, "Short ID");
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]);
352 snprintf(rv
+ 14, FORMAT_ID_SIZE
- 14, ".%02x", id
[6]);
354 snprintf(rv
+ 17, FORMAT_ID_SIZE
- 17, "-%02x", id
[7]);
359 const char *time2string(uint32_t time
)
361 char *pos
= datestring
;
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
;
386 pos
+= sprintf(pos
, "%us", rest
);
394 * routine to decrement a timer by a random
397 * first argument is the timer and the second is
400 unsigned long isis_jitter(unsigned long timer
, unsigned long jitter
)
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
416 j
= 1 + (int)((RANDOM_SPREAD
* random()) / (RAND_MAX
+ 1.0));
418 k
= timer
- (timer
* (100 - jitter
)) / 100;
420 timer
= timer
- (k
* j
/ RANDOM_SPREAD
);
425 struct in_addr
newprefix2inaddr(uint8_t *prefix_start
, uint8_t prefix_masklen
)
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)
436 * Returns the dynamic hostname associated with the passed system ID.
437 * If no dynamic hostname found then returns formatted system ID.
439 const char *print_sys_hostname(const uint8_t *sysid
)
441 struct isis_dynhn
*dyn
;
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();
450 dyn
= dynhn_find_by_id(sysid
);
452 return dyn
->hostname
;
454 return sysid_print(sysid
);
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.
461 void zlog_dump_data(void *data
, int len
)
468 char hexstr
[16 * 3 + 5];
469 char charstr
[16 * 1 + 5];
472 memset(bytestr
, 0, sizeof(bytestr
));
473 memset(addrstr
, 0, sizeof(addrstr
));
474 memset(hexstr
, 0, sizeof(hexstr
));
475 memset(charstr
, 0, sizeof(charstr
));
477 for (i
= 1; i
<= len
; i
++) {
482 /* store address for this line */
484 snprintf(addrstr
, sizeof(addrstr
), "%p", p
);
486 /* store hex str (for left side) */
487 snprintf(bytestr
, sizeof(bytestr
), "%02X ", *p
);
488 strncat(hexstr
, bytestr
, sizeof(hexstr
) - strlen(hexstr
) - 1);
490 /* store char str (for right side) */
491 snprintf(bytestr
, sizeof(bytestr
), "%c", c
);
492 strncat(charstr
, bytestr
,
493 sizeof(charstr
) - strlen(charstr
) - 1);
497 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
,
501 } else if ((i
% 8) == 0) {
502 /* half line: add whitespaces */
504 sizeof(hexstr
) - strlen(hexstr
) - 1);
505 strncat(charstr
, " ",
506 sizeof(charstr
) - strlen(charstr
) - 1);
511 /* print rest of buffer if not empty */
512 if (strlen(hexstr
) > 0)
513 zlog_debug("[%8.8s] %-50.50s %s", addrstr
, hexstr
, charstr
);
517 void log_multiline(int priority
, const char *prefix
, const char *format
, ...)
523 va_start(ap
, format
);
524 p
= vasnprintfrr(MTYPE_TMP
, shortbuf
, sizeof(shortbuf
), format
, ap
);
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
);
540 void vty_multiline(struct vty
*vty
, const char *prefix
, const char *format
, ...)
546 va_start(ap
, format
);
547 p
= vasnprintfrr(MTYPE_TMP
, shortbuf
, sizeof(shortbuf
), format
, ap
);
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
);
563 void vty_out_timestr(struct vty
*vty
, time_t uptime
)
566 time_t difftime
= time(NULL
);
569 gmtime_r(&difftime
, &tm
);
571 if (difftime
< ONE_DAY_SECOND
)
572 vty_out(vty
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
574 else if (difftime
< ONE_WEEK_SECOND
)
575 vty_out(vty
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
578 vty_out(vty
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
579 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
580 vty_out(vty
, " ago");