summaryrefslogtreecommitdiff
path: root/isisd/isis_misc.c
blob: cf558e83f53795e42c940d8fa415e453441ba2c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * IS-IS Rout(e)ing protocol - isis_misc.h
 *                             Miscellanous routines
 *
 * Copyright (C) 2001,2002   Sampo Saaristo
 *                           Tampere University of Technology      
 *                           Institute of Communications Engineering
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public Licenseas published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
 * more details.

 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */


#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <zebra.h>
#include <net/ethernet.h>
#include <sys/utsname.h>

#include "stream.h"
#include "vty.h"
#include "hash.h"
#include "if.h"
#include "command.h"

#include "isisd/dict.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_common.h"
#include "isisd/isis_circuit.h"
#include "isisd/isisd.h"
#include "isisd/isis_misc.h"

#include "isisd/isis_tlv.h"
#include "isisd/isis_lsp.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_adjacency.h"

/*
 * This converts the isonet to its printable format
 */
char * isonet_print (u_char *from, int len) {
  int i = 0;
  char *pos = isonet;

  if(!from)
    return "unknown";

  while (i < len) {
    if (i & 1) {
      sprintf ( pos, "%02x", *(from + i));
      pos += 2;
    } else {
      if (i == 0) { /* if the area addr is just one byte, eg. 47. */
        sprintf ( pos, "%02x", *(from + i));
        pos += 2;
      } else {
        sprintf ( pos, "%02x.", *(from + i));
        pos += 3;
      }
    }
    i++;
  }
  *(pos) = '\0';
  return isonet;
}

/*
 * Returns 0 on error, length of buff on ok
 * extract dot from the dotted str, and insert all the number in a buff 
 */
int
dotformat2buff (u_char *buff, u_char *dotted)
{
  int dotlen, len = 0;
  u_char *pos = dotted;
  u_char number[3];
  int nextdotpos = 2;

  number[2] = '\0';
  dotlen = strlen(dotted);
  if (dotlen > 50) {
    /* this can't be an iso net, its too long */
    return 0;
  }

  while ( (pos - dotted) < dotlen && len < 20 ) {
    if (*pos == '.') {
      /* we expect the . at 2, and than every 5 */
      if ((pos - dotted) != nextdotpos) {
        len = 0;
        break;
      }
      nextdotpos += 5;
      pos++;
      continue;
    }
    /* we must have at least two chars left here */
    if (dotlen - (pos - dotted) < 2) {
      len = 0;
      break;
    }

    if ((isxdigit((int)*pos)) && (isxdigit((int)*(pos+1)))){
      memcpy (number, pos ,2);
      pos+=2;
    } else {
      len = 0;
      break;
    }

    *(buff + len) = (char)strtol(number, NULL, 16);
    len++;
  }
  
  return len;
}
/*
 * conversion of XXXX.XXXX.XXXX to memory
 */
int
sysid2buff (u_char *buff, u_char *dotted)
 {
  int len = 0;
  u_char *pos = dotted;
  u_char number[3];

  number[2] = '\0';
  // surely not a sysid_string if not 14 length
  if (strlen(dotted) != 14) {
    return 0;
  }

  while ( len < ISIS_SYS_ID_LEN ) {
    if (*pos == '.') {
      /* the . is not positioned correctly */
      if (((pos - dotted) !=4) && ((pos - dotted) != 9)) { 
        len = 0;
        break;
      }
      pos++;
      continue;
    }
    if ((isxdigit((int)*pos)) && (isxdigit((int)*(pos+1)))){
      memcpy (number, pos ,2);
      pos+=2;
    } else {
      len = 0;
      break;
    }

    *(buff + len) = (char)strtol(number, NULL, 16);
    len++;
  }

  return len;

}

/*
 * converts the nlpids struct (filled by TLV #129)
 * into a string
 */

char *
nlpid2string (struct nlpids *nlpids) {
  char *pos = nlpidstring;
  int i;

  for (i=0;i<nlpids->count;i++) {
    switch (nlpids->nlpids[i]) {
    case NLPID_IP:
      pos += sprintf (pos, "IPv4");
      break;
    case NLPID_IPV6:
      pos += sprintf (pos, "IPv6");
      break;
    default:
      pos += sprintf (pos, "unknown");
      break;
    }
    if (nlpids->count-i>1)
      pos += sprintf (pos, ", ");
    
  }

  *(pos) = '\0';
  
  return nlpidstring;
}

/*
 *  supports the given af ?
 */
int 
speaks (struct nlpids *nlpids, int family)
{
  int i, speaks = 0;
  
  if (nlpids == (struct nlpids*)NULL)
    return speaks;
  for (i = 0;i < nlpids->count; i++) {
    if (family == AF_INET && nlpids->nlpids[i] == NLPID_IP)
      speaks = 1;
    if (family == AF_INET6 && nlpids->nlpids[i] == NLPID_IPV6)
      speaks = 1;
  }

  return speaks;
}


/*
 * Returns 0 on error, IS-IS Circuit Type on ok
 */
int 
string2circuit_t (u_char *str)
{
  
  if (!str)
    return 0;
  
  if (!strcmp(str,"level-1"))
    return IS_LEVEL_1;

  if (!strcmp(str,"level-2-only") || !strcmp(str,"level-2"))
    return IS_LEVEL_2;
  
  if (!strcmp(str,"level-1-2"))
    return IS_LEVEL_1_AND_2;

  return 0;
}

const char *
circuit_t2string (int circuit_t)
{
  switch (circuit_t) {
  case IS_LEVEL_1:
    return "L1";
  case IS_LEVEL_2:
    return "L2";
  case IS_LEVEL_1_AND_2:
    return "L1L2";
  default:
    return "??";
  }

  return NULL; /* not reached */
}

const char *
syst2string (int type)
{
  switch (type) {
  case ISIS_SYSTYPE_ES:
    return "ES";
  case ISIS_SYSTYPE_IS:
    return "IS";
  case ISIS_SYSTYPE_L1_IS:
    return "1";
  case ISIS_SYSTYPE_L2_IS:
    return "2";
  default:
    return "??";
  }

  return NULL; /* not reached */
}

/*
 * Print functions - we print to static vars
 */
char *
snpa_print (u_char *from)
{
  int i = 0;
  u_char *pos = snpa;

  if(!from)
    return "unknown";
  
  while (i < ETH_ALEN - 1) {
    if (i & 1) {
      sprintf ( pos, "%02x.", *(from + i));
      pos += 3;
    } else {
      sprintf ( pos, "%02x", *(from + i));
      pos += 2;

    }
    i++;
  }

  sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
  pos += 2;
  *(pos) = '\0';

  return snpa;
}

char *
sysid_print (u_char *from)
{
  int i = 0;
  char *pos = sysid;

  if(!from)
    return "unknown";

  while (i < ISIS_SYS_ID_LEN - 1) {
    if (i & 1) {
      sprintf ( pos, "%02x.", *(from + i));
      pos += 3;
    } else {
      sprintf ( pos, "%02x", *(from + i));
      pos += 2;
    
    } 
    i++;
  }

  sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
  pos += 2;
  *(pos) = '\0';
  
  return sysid;
}

char *
rawlspid_print (u_char *from)
{
  char *pos = lspid;
  if(!from)
    return "unknown";
  memcpy(pos, sysid_print(from), 15);
  pos += 14;
  sprintf (pos, ".%02x", LSP_PSEUDO_ID(from));
  pos += 3;
  sprintf (pos, "-%02x", LSP_FRAGMENT(from));
  pos += 3;

  *(pos) = '\0';

  return lspid;
}

char *
time2string (u_int32_t time) {
  char *pos = datestring; 
  u_int32_t rest;

  if (time==0)
    return "-";

  if(time/SECS_PER_YEAR)
    pos += sprintf (pos, "%uY",time/SECS_PER_YEAR);
  rest=time%SECS_PER_YEAR;    
  if(rest/SECS_PER_MONTH)
    pos += sprintf (pos, "%uM",rest/SECS_PER_MONTH);
  rest=rest%SECS_PER_MONTH;  
  if(rest/SECS_PER_WEEK)
    pos += sprintf (pos, "%uw",rest/SECS_PER_WEEK);
  rest=rest%SECS_PER_WEEK;    
  if(rest/SECS_PER_DAY)
    pos += sprintf (pos, "%ud",rest/SECS_PER_DAY);
  rest=rest%SECS_PER_DAY;    
  if(rest/SECS_PER_HOUR)
    pos += sprintf (pos, "%uh",rest/SECS_PER_HOUR);
  rest=rest%SECS_PER_HOUR;    
  if(rest/SECS_PER_MINUTE)
    pos += sprintf (pos, "%um",rest/SECS_PER_MINUTE);
  rest=rest%SECS_PER_MINUTE;    
  if(rest)
    pos += sprintf (pos, "%us",rest);   
 
  *(pos) = 0;

  return datestring;
}

/*
 * routine to decrement a timer by a random
 * number
 *
 * first argument is the timer and the second is
 * the jitter
 */
unsigned long 
isis_jitter (unsigned long timer, unsigned long jitter)
{
  int j,k;

  if (jitter>=100)
    return timer;

  if (timer == 1)
    return timer;
  /* 
   * randomizing just the percent value provides
   * no good random numbers - hence the spread
   * to RANDOM_SPREAD (100000), which is ok as
   * most IS-IS timers are no longer than 16 bit
   */

  j = 1 + (int) ((RANDOM_SPREAD * rand()) / (RAND_MAX + 1.0 )); 

  k = timer - (timer * (100 - jitter))/100;

  timer = timer - (k * j / RANDOM_SPREAD);

  return timer;
}

struct in_addr
newprefix2inaddr (u_char *prefix_start, u_char prefix_masklen) 
{
  memset(&new_prefix, 0, sizeof (new_prefix));
  memcpy(&new_prefix, prefix_start, (prefix_masklen & 0x3F) ? 
	 ((((prefix_masklen & 0x3F)-1)>>3)+1) : 0);
  return new_prefix;
}

/*
 * Returns host.name if any, otherwise
 * it returns the system hostname.
 */
const char *
unix_hostname(void)
{
  static struct utsname names;
  const char *hostname;
  extern struct host host;

  hostname = host.name;
  if (!hostname) { 
    uname(&names);
    hostname = names.nodename;
  }

  return hostname;
}