blob: cf558e83f53795e42c940d8fa415e453441ba2c7 [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
2 * IS-IS Rout(e)ing protocol - isis_misc.h
3 * Miscellanous routines
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
8 *
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.
13 *
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.
18
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.
22 */
23
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <time.h>
28#include <ctype.h>
29#include <zebra.h>
30#include <net/ethernet.h>
jardin9e867fe2003-12-23 08:56:18 +000031#include <sys/utsname.h>
jardineb5d44e2003-12-23 08:09:43 +000032
33#include "stream.h"
34#include "vty.h"
35#include "hash.h"
36#include "if.h"
jardin9e867fe2003-12-23 08:56:18 +000037#include "command.h"
jardineb5d44e2003-12-23 08:09:43 +000038
39#include "isisd/dict.h"
40#include "isisd/isis_constants.h"
41#include "isisd/isis_common.h"
42#include "isisd/isis_circuit.h"
43#include "isisd/isisd.h"
44#include "isisd/isis_misc.h"
45
46#include "isisd/isis_tlv.h"
47#include "isisd/isis_lsp.h"
48#include "isisd/isis_constants.h"
49#include "isisd/isis_adjacency.h"
50
51/*
52 * This converts the isonet to its printable format
53 */
54char * isonet_print (u_char *from, int len) {
55 int i = 0;
56 char *pos = isonet;
57
58 if(!from)
59 return "unknown";
60
61 while (i < len) {
62 if (i & 1) {
63 sprintf ( pos, "%02x", *(from + i));
64 pos += 2;
65 } else {
66 if (i == 0) { /* if the area addr is just one byte, eg. 47. */
67 sprintf ( pos, "%02x", *(from + i));
68 pos += 2;
69 } else {
70 sprintf ( pos, "%02x.", *(from + i));
71 pos += 3;
72 }
73 }
74 i++;
75 }
76 *(pos) = '\0';
77 return isonet;
78}
79
80/*
81 * Returns 0 on error, length of buff on ok
82 * extract dot from the dotted str, and insert all the number in a buff
83 */
84int
85dotformat2buff (u_char *buff, u_char *dotted)
86{
87 int dotlen, len = 0;
88 u_char *pos = dotted;
89 u_char number[3];
90 int nextdotpos = 2;
91
92 number[2] = '\0';
93 dotlen = strlen(dotted);
94 if (dotlen > 50) {
95 /* this can't be an iso net, its too long */
96 return 0;
97 }
98
99 while ( (pos - dotted) < dotlen && len < 20 ) {
100 if (*pos == '.') {
101 /* we expect the . at 2, and than every 5 */
102 if ((pos - dotted) != nextdotpos) {
103 len = 0;
104 break;
105 }
106 nextdotpos += 5;
107 pos++;
108 continue;
109 }
110 /* we must have at least two chars left here */
111 if (dotlen - (pos - dotted) < 2) {
112 len = 0;
113 break;
114 }
115
116 if ((isxdigit((int)*pos)) && (isxdigit((int)*(pos+1)))){
117 memcpy (number, pos ,2);
118 pos+=2;
119 } else {
120 len = 0;
121 break;
122 }
123
124 *(buff + len) = (char)strtol(number, NULL, 16);
125 len++;
126 }
127
128 return len;
129}
130/*
131 * conversion of XXXX.XXXX.XXXX to memory
132 */
133int
134sysid2buff (u_char *buff, u_char *dotted)
135 {
136 int len = 0;
137 u_char *pos = dotted;
138 u_char number[3];
139
140 number[2] = '\0';
141 // surely not a sysid_string if not 14 length
142 if (strlen(dotted) != 14) {
143 return 0;
144 }
145
146 while ( len < ISIS_SYS_ID_LEN ) {
147 if (*pos == '.') {
148 /* the . is not positioned correctly */
149 if (((pos - dotted) !=4) && ((pos - dotted) != 9)) {
150 len = 0;
151 break;
152 }
153 pos++;
154 continue;
155 }
156 if ((isxdigit((int)*pos)) && (isxdigit((int)*(pos+1)))){
157 memcpy (number, pos ,2);
158 pos+=2;
159 } else {
160 len = 0;
161 break;
162 }
163
164 *(buff + len) = (char)strtol(number, NULL, 16);
165 len++;
166 }
167
168 return len;
169
170}
171
172/*
173 * converts the nlpids struct (filled by TLV #129)
174 * into a string
175 */
176
177char *
178nlpid2string (struct nlpids *nlpids) {
179 char *pos = nlpidstring;
180 int i;
181
182 for (i=0;i<nlpids->count;i++) {
183 switch (nlpids->nlpids[i]) {
184 case NLPID_IP:
185 pos += sprintf (pos, "IPv4");
186 break;
187 case NLPID_IPV6:
188 pos += sprintf (pos, "IPv6");
189 break;
190 default:
191 pos += sprintf (pos, "unknown");
192 break;
193 }
194 if (nlpids->count-i>1)
195 pos += sprintf (pos, ", ");
196
197 }
198
199 *(pos) = '\0';
200
201 return nlpidstring;
202}
203
204/*
205 * supports the given af ?
206 */
207int
208speaks (struct nlpids *nlpids, int family)
209{
210 int i, speaks = 0;
211
212 if (nlpids == (struct nlpids*)NULL)
213 return speaks;
214 for (i = 0;i < nlpids->count; i++) {
215 if (family == AF_INET && nlpids->nlpids[i] == NLPID_IP)
216 speaks = 1;
217 if (family == AF_INET6 && nlpids->nlpids[i] == NLPID_IPV6)
218 speaks = 1;
219 }
220
221 return speaks;
222}
223
224
225/*
226 * Returns 0 on error, IS-IS Circuit Type on ok
227 */
228int
229string2circuit_t (u_char *str)
230{
231
232 if (!str)
233 return 0;
234
235 if (!strcmp(str,"level-1"))
236 return IS_LEVEL_1;
237
238 if (!strcmp(str,"level-2-only") || !strcmp(str,"level-2"))
239 return IS_LEVEL_2;
240
241 if (!strcmp(str,"level-1-2"))
242 return IS_LEVEL_1_AND_2;
243
244 return 0;
245}
246
247const char *
248circuit_t2string (int circuit_t)
249{
250 switch (circuit_t) {
251 case IS_LEVEL_1:
252 return "L1";
253 case IS_LEVEL_2:
254 return "L2";
255 case IS_LEVEL_1_AND_2:
256 return "L1L2";
257 default:
258 return "??";
259 }
260
261 return NULL; /* not reached */
262}
263
264const char *
265syst2string (int type)
266{
267 switch (type) {
268 case ISIS_SYSTYPE_ES:
269 return "ES";
270 case ISIS_SYSTYPE_IS:
271 return "IS";
272 case ISIS_SYSTYPE_L1_IS:
273 return "1";
274 case ISIS_SYSTYPE_L2_IS:
275 return "2";
276 default:
277 return "??";
278 }
279
280 return NULL; /* not reached */
281}
282
283/*
284 * Print functions - we print to static vars
285 */
286char *
287snpa_print (u_char *from)
288{
289 int i = 0;
290 u_char *pos = snpa;
291
292 if(!from)
293 return "unknown";
294
295 while (i < ETH_ALEN - 1) {
296 if (i & 1) {
297 sprintf ( pos, "%02x.", *(from + i));
298 pos += 3;
299 } else {
300 sprintf ( pos, "%02x", *(from + i));
301 pos += 2;
302
303 }
304 i++;
305 }
306
307 sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
308 pos += 2;
309 *(pos) = '\0';
310
311 return snpa;
312}
313
314char *
315sysid_print (u_char *from)
316{
317 int i = 0;
318 char *pos = sysid;
319
320 if(!from)
321 return "unknown";
322
323 while (i < ISIS_SYS_ID_LEN - 1) {
324 if (i & 1) {
325 sprintf ( pos, "%02x.", *(from + i));
326 pos += 3;
327 } else {
328 sprintf ( pos, "%02x", *(from + i));
329 pos += 2;
330
331 }
332 i++;
333 }
334
335 sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
336 pos += 2;
337 *(pos) = '\0';
338
339 return sysid;
340}
341
342char *
343rawlspid_print (u_char *from)
344{
345 char *pos = lspid;
346 if(!from)
347 return "unknown";
348 memcpy(pos, sysid_print(from), 15);
349 pos += 14;
350 sprintf (pos, ".%02x", LSP_PSEUDO_ID(from));
351 pos += 3;
352 sprintf (pos, "-%02x", LSP_FRAGMENT(from));
353 pos += 3;
354
355 *(pos) = '\0';
356
357 return lspid;
358}
359
360char *
361time2string (u_int32_t time) {
362 char *pos = datestring;
363 u_int32_t rest;
364
365 if (time==0)
366 return "-";
367
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;
386 if(rest)
387 pos += sprintf (pos, "%us",rest);
388
389 *(pos) = 0;
390
391 return datestring;
392}
393
394/*
395 * routine to decrement a timer by a random
396 * number
397 *
398 * first argument is the timer and the second is
399 * the jitter
400 */
401unsigned long
402isis_jitter (unsigned long timer, unsigned long jitter)
403{
404 int j,k;
405
406 if (jitter>=100)
407 return timer;
408
409 if (timer == 1)
410 return timer;
411 /*
412 * randomizing just the percent value provides
413 * no good random numbers - hence the spread
414 * to RANDOM_SPREAD (100000), which is ok as
415 * most IS-IS timers are no longer than 16 bit
416 */
417
418 j = 1 + (int) ((RANDOM_SPREAD * rand()) / (RAND_MAX + 1.0 ));
419
420 k = timer - (timer * (100 - jitter))/100;
421
422 timer = timer - (k * j / RANDOM_SPREAD);
423
424 return timer;
425}
426
427struct in_addr
428newprefix2inaddr (u_char *prefix_start, u_char prefix_masklen)
429{
430 memset(&new_prefix, 0, sizeof (new_prefix));
431 memcpy(&new_prefix, prefix_start, (prefix_masklen & 0x3F) ?
432 ((((prefix_masklen & 0x3F)-1)>>3)+1) : 0);
433 return new_prefix;
434}
435
jardin9e867fe2003-12-23 08:56:18 +0000436/*
437 * Returns host.name if any, otherwise
438 * it returns the system hostname.
439 */
440const char *
441unix_hostname(void)
442{
443 static struct utsname names;
444 const char *hostname;
445 extern struct host host;
jardineb5d44e2003-12-23 08:09:43 +0000446
jardin9e867fe2003-12-23 08:56:18 +0000447 hostname = host.name;
448 if (!hostname) {
449 uname(&names);
450 hostname = names.nodename;
451 }
jardineb5d44e2003-12-23 08:09:43 +0000452
jardin9e867fe2003-12-23 08:56:18 +0000453 return hostname;
454}