zebra: remove unused function to fix warning
[jleu-quagga.git] / isisd / isis_misc.c
blob6b565bcbe23a5b2b0659f8afa96e72e88b2883fc
1 /*
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)
12 * any later version.
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
17 * more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <zebra.h>
26 #include "stream.h"
27 #include "vty.h"
28 #include "hash.h"
29 #include "if.h"
30 #include "command.h"
32 #include "isisd/dict.h"
33 #include "isisd/isis_constants.h"
34 #include "isisd/isis_common.h"
35 #include "isisd/isis_circuit.h"
36 #include "isisd/isisd.h"
37 #include "isisd/isis_misc.h"
39 #include "isisd/isis_tlv.h"
40 #include "isisd/isis_lsp.h"
41 #include "isisd/isis_constants.h"
42 #include "isisd/isis_adjacency.h"
44 /* staticly assigned vars for printing purposes */
45 struct in_addr new_prefix;
46 /* len of xxxx.xxxx.xxxx + place for #0 termination */
47 char sysid[15];
48 /* len of xxxx.xxxx.xxxx + place for #0 termination */
49 char snpa[15];
50 /* len of xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx */
51 char isonet[51];
52 /* + place for #0 termination */
53 /* len of xxxx.xxxx.xxxx.xx.xx + place for #0 termination */
54 char lspid[21];
55 /* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */
56 char datestring[20];
57 char nlpidstring[30];
60 * This converts the isonet to its printable format
62 const char *
63 isonet_print (u_char * from, int len)
65 int i = 0;
66 char *pos = isonet;
68 if (!from)
69 return "unknown";
71 while (i < len)
73 if (i & 1)
75 sprintf (pos, "%02x", *(from + i));
76 pos += 2;
78 else
80 if (i == (len - 1))
81 { /* No dot at the end of address */
82 sprintf (pos, "%02x", *(from + i));
83 pos += 2;
85 else
87 sprintf (pos, "%02x.", *(from + i));
88 pos += 3;
91 i++;
93 *(pos) = '\0';
94 return isonet;
98 * Returns 0 on error, length of buff on ok
99 * extract dot from the dotted str, and insert all the number in a buff
102 dotformat2buff (u_char * buff, const u_char * dotted)
104 int dotlen, len = 0;
105 const u_char *pos = dotted;
106 u_char number[3];
107 int nextdotpos = 2;
109 number[2] = '\0';
110 dotlen = strlen(dotted);
111 if (dotlen > 50)
113 /* this can't be an iso net, its too long */
114 return 0;
117 while ((pos - dotted) < dotlen && len < 20)
119 if (*pos == '.')
121 /* we expect the . at 2, and than every 5 */
122 if ((pos - dotted) != nextdotpos)
124 len = 0;
125 break;
127 nextdotpos += 5;
128 pos++;
129 continue;
131 /* we must have at least two chars left here */
132 if (dotlen - (pos - dotted) < 2)
134 len = 0;
135 break;
138 if ((isxdigit ((int) *pos)) && (isxdigit ((int) *(pos + 1))))
140 memcpy (number, pos, 2);
141 pos += 2;
143 else
145 len = 0;
146 break;
149 *(buff + len) = (char) strtol ((char *)number, NULL, 16);
150 len++;
153 return len;
157 * conversion of XXXX.XXXX.XXXX to memory
160 sysid2buff (u_char * buff, const u_char * dotted)
162 int len = 0;
163 const u_char *pos = dotted;
164 u_char number[3];
166 number[2] = '\0';
167 // surely not a sysid_string if not 14 length
168 if (strlen (dotted) != 14)
170 return 0;
173 while (len < ISIS_SYS_ID_LEN)
175 if (*pos == '.')
177 /* the . is not positioned correctly */
178 if (((pos - dotted) != 4) && ((pos - dotted) != 9))
180 len = 0;
181 break;
183 pos++;
184 continue;
186 if ((isxdigit ((int) *pos)) && (isxdigit ((int) *(pos + 1))))
188 memcpy (number, pos, 2);
189 pos += 2;
191 else
193 len = 0;
194 break;
197 *(buff + len) = (char) strtol ((char *)number, NULL, 16);
198 len++;
201 return len;
206 * converts the nlpids struct (filled by TLV #129)
207 * into a string
210 char *
211 nlpid2string (struct nlpids *nlpids)
213 char *pos = nlpidstring;
214 int i;
216 for (i = 0; i < nlpids->count; i++)
218 switch (nlpids->nlpids[i])
220 case NLPID_IP:
221 pos += sprintf (pos, "IPv4");
222 break;
223 case NLPID_IPV6:
224 pos += sprintf (pos, "IPv6");
225 break;
226 case NLPID_SNAP:
227 pos += sprintf (pos, "SNAP");
228 break;
229 case NLPID_CLNP:
230 pos += sprintf (pos, "CLNP");
231 break;
232 case NLPID_ESIS:
233 pos += sprintf (pos, "ES-IS");
234 break;
235 default:
236 pos += sprintf (pos, "unknown");
237 break;
239 if (nlpids->count - i > 1)
240 pos += sprintf (pos, ", ");
244 *(pos) = '\0';
246 return nlpidstring;
250 * supports the given af ?
253 speaks (struct nlpids *nlpids, int family)
255 int i, speaks = 0;
257 if (nlpids == (struct nlpids *) NULL)
258 return speaks;
259 for (i = 0; i < nlpids->count; i++)
261 if (family == AF_INET && nlpids->nlpids[i] == NLPID_IP)
262 speaks = 1;
263 if (family == AF_INET6 && nlpids->nlpids[i] == NLPID_IPV6)
264 speaks = 1;
267 return speaks;
271 * Returns 0 on error, IS-IS Circuit Type on ok
274 string2circuit_t (const u_char * str)
277 if (!str)
278 return 0;
280 if (!strcmp (str, "level-1"))
281 return IS_LEVEL_1;
283 if (!strcmp (str, "level-2-only") || !strcmp (str, "level-2"))
284 return IS_LEVEL_2;
286 if (!strcmp (str, "level-1-2"))
287 return IS_LEVEL_1_AND_2;
289 return 0;
292 const char *
293 circuit_t2string (int circuit_t)
295 switch (circuit_t)
297 case IS_LEVEL_1:
298 return "L1";
299 case IS_LEVEL_2:
300 return "L2";
301 case IS_LEVEL_1_AND_2:
302 return "L1L2";
303 default:
304 return "??";
307 return NULL; /* not reached */
310 const char *
311 syst2string (int type)
313 switch (type)
315 case ISIS_SYSTYPE_ES:
316 return "ES";
317 case ISIS_SYSTYPE_IS:
318 return "IS";
319 case ISIS_SYSTYPE_L1_IS:
320 return "1";
321 case ISIS_SYSTYPE_L2_IS:
322 return "2";
323 default:
324 return "??";
327 return NULL; /* not reached */
331 * Print functions - we print to static vars
333 const char *
334 snpa_print (u_char * from)
336 int i = 0;
337 u_char *pos = (u_char *)snpa;
339 if (!from)
340 return "unknown";
342 while (i < ETH_ALEN - 1)
344 if (i & 1)
346 sprintf ((char *)pos, "%02x.", *(from + i));
347 pos += 3;
349 else
351 sprintf ((char *)pos, "%02x", *(from + i));
352 pos += 2;
355 i++;
358 sprintf ((char *)pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
359 pos += 2;
360 *(pos) = '\0';
362 return snpa;
365 const char *
366 sysid_print (u_char * from)
368 int i = 0;
369 char *pos = sysid;
371 if (!from)
372 return "unknown";
374 while (i < ISIS_SYS_ID_LEN - 1)
376 if (i & 1)
378 sprintf (pos, "%02x.", *(from + i));
379 pos += 3;
381 else
383 sprintf (pos, "%02x", *(from + i));
384 pos += 2;
387 i++;
390 sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
391 pos += 2;
392 *(pos) = '\0';
394 return sysid;
397 const char *
398 rawlspid_print (u_char * from)
400 char *pos = lspid;
401 if (!from)
402 return "unknown";
403 memcpy (pos, sysid_print (from), 15);
404 pos += 14;
405 sprintf (pos, ".%02x", LSP_PSEUDO_ID (from));
406 pos += 3;
407 sprintf (pos, "-%02x", LSP_FRAGMENT (from));
408 pos += 3;
410 *(pos) = '\0';
412 return lspid;
415 const char *
416 time2string (u_int32_t time)
418 char *pos = datestring;
419 u_int32_t rest;
421 if (time == 0)
422 return "-";
424 if (time / SECS_PER_YEAR)
425 pos += sprintf (pos, "%uY", time / SECS_PER_YEAR);
426 rest = time % SECS_PER_YEAR;
427 if (rest / SECS_PER_MONTH)
428 pos += sprintf (pos, "%uM", rest / SECS_PER_MONTH);
429 rest = rest % SECS_PER_MONTH;
430 if (rest / SECS_PER_WEEK)
431 pos += sprintf (pos, "%uw", rest / SECS_PER_WEEK);
432 rest = rest % SECS_PER_WEEK;
433 if (rest / SECS_PER_DAY)
434 pos += sprintf (pos, "%ud", rest / SECS_PER_DAY);
435 rest = rest % SECS_PER_DAY;
436 if (rest / SECS_PER_HOUR)
437 pos += sprintf (pos, "%uh", rest / SECS_PER_HOUR);
438 rest = rest % SECS_PER_HOUR;
439 if (rest / SECS_PER_MINUTE)
440 pos += sprintf (pos, "%um", rest / SECS_PER_MINUTE);
441 rest = rest % SECS_PER_MINUTE;
442 if (rest)
443 pos += sprintf (pos, "%us", rest);
445 *(pos) = 0;
447 return datestring;
451 * routine to decrement a timer by a random
452 * number
454 * first argument is the timer and the second is
455 * the jitter
457 unsigned long
458 isis_jitter (unsigned long timer, unsigned long jitter)
460 int j, k;
462 if (jitter >= 100)
463 return timer;
465 if (timer == 1)
466 return timer;
468 * randomizing just the percent value provides
469 * no good random numbers - hence the spread
470 * to RANDOM_SPREAD (100000), which is ok as
471 * most IS-IS timers are no longer than 16 bit
474 j = 1 + (int) ((RANDOM_SPREAD * rand ()) / (RAND_MAX + 1.0));
476 k = timer - (timer * (100 - jitter)) / 100;
478 timer = timer - (k * j / RANDOM_SPREAD);
480 return timer;
483 struct in_addr
484 newprefix2inaddr (u_char * prefix_start, u_char prefix_masklen)
486 memset (&new_prefix, 0, sizeof (new_prefix));
487 memcpy (&new_prefix, prefix_start, (prefix_masklen & 0x3F) ?
488 ((((prefix_masklen & 0x3F) - 1) >> 3) + 1) : 0);
489 return new_prefix;
493 * Returns host.name if any, otherwise
494 * it returns the system hostname.
496 const char *
497 unix_hostname (void)
499 static struct utsname names;
500 const char *hostname;
501 extern struct host host;
503 hostname = host.name;
504 if (!hostname)
506 uname (&names);
507 hostname = names.nodename;
510 return hostname;