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