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; if not, write to the Free Software Foundation, Inc.,
21
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30
#include <net/ethernet.h>
31
#include <sys/utsname.h>
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"
46
#include "isisd/isis_tlv.h"
47
#include "isisd/isis_lsp.h"
48
#include "isisd/isis_constants.h"
49
#include "isisd/isis_adjacency.h"
52
* This converts the isonet to its printable format
54
char * isonet_print (u_char *from, int len) {
63
sprintf ( pos, "%02x", *(from + i));
66
if (i == (len - 1)) { /* No dot at the end of address */
67
sprintf ( pos, "%02x", *(from + i));
70
sprintf ( pos, "%02x.", *(from + i));
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
85
dotformat2buff (u_char *buff, u_char *dotted)
93
dotlen = strlen(dotted);
95
/* this can't be an iso net, its too long */
99
while ( (pos - dotted) < dotlen && len < 20 ) {
101
/* we expect the . at 2, and than every 5 */
102
if ((pos - dotted) != nextdotpos) {
110
/* we must have at least two chars left here */
111
if (dotlen - (pos - dotted) < 2) {
116
if ((isxdigit((int)*pos)) && (isxdigit((int)*(pos+1)))){
117
memcpy (number, pos ,2);
124
*(buff + len) = (char)strtol(number, NULL, 16);
131
* conversion of XXXX.XXXX.XXXX to memory
134
sysid2buff (u_char *buff, u_char *dotted)
137
u_char *pos = dotted;
141
// surely not a sysid_string if not 14 length
142
if (strlen(dotted) != 14) {
146
while ( len < ISIS_SYS_ID_LEN ) {
148
/* the . is not positioned correctly */
149
if (((pos - dotted) !=4) && ((pos - dotted) != 9)) {
156
if ((isxdigit((int)*pos)) && (isxdigit((int)*(pos+1)))){
157
memcpy (number, pos ,2);
164
*(buff + len) = (char)strtol(number, NULL, 16);
173
* converts the nlpids struct (filled by TLV #129)
178
nlpid2string (struct nlpids *nlpids) {
179
char *pos = nlpidstring;
182
for (i=0;i<nlpids->count;i++) {
183
switch (nlpids->nlpids[i]) {
185
pos += sprintf (pos, "IPv4");
188
pos += sprintf (pos, "IPv6");
191
pos += sprintf (pos, "SNAP");
194
pos += sprintf (pos, "CLNP");
197
pos += sprintf (pos, "ES-IS");
200
pos += sprintf (pos, "unknown");
203
if (nlpids->count-i>1)
204
pos += sprintf (pos, ", ");
214
* supports the given af ?
217
speaks (struct nlpids *nlpids, int family)
221
if (nlpids == (struct nlpids*)NULL)
223
for (i = 0;i < nlpids->count; i++) {
224
if (family == AF_INET && nlpids->nlpids[i] == NLPID_IP)
226
if (family == AF_INET6 && nlpids->nlpids[i] == NLPID_IPV6)
235
* Returns 0 on error, IS-IS Circuit Type on ok
238
string2circuit_t (u_char *str)
244
if (!strcmp(str,"level-1"))
247
if (!strcmp(str,"level-2-only") || !strcmp(str,"level-2"))
250
if (!strcmp(str,"level-1-2"))
251
return IS_LEVEL_1_AND_2;
257
circuit_t2string (int circuit_t)
264
case IS_LEVEL_1_AND_2:
270
return NULL; /* not reached */
274
syst2string (int type)
277
case ISIS_SYSTYPE_ES:
279
case ISIS_SYSTYPE_IS:
281
case ISIS_SYSTYPE_L1_IS:
283
case ISIS_SYSTYPE_L2_IS:
289
return NULL; /* not reached */
293
* Print functions - we print to static vars
296
snpa_print (u_char *from)
304
while (i < ETH_ALEN - 1) {
306
sprintf ( pos, "%02x.", *(from + i));
309
sprintf ( pos, "%02x", *(from + i));
316
sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
324
sysid_print (u_char *from)
332
while (i < ISIS_SYS_ID_LEN - 1) {
334
sprintf ( pos, "%02x.", *(from + i));
337
sprintf ( pos, "%02x", *(from + i));
344
sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
352
rawlspid_print (u_char *from)
357
memcpy(pos, sysid_print(from), 15);
359
sprintf (pos, ".%02x", LSP_PSEUDO_ID(from));
361
sprintf (pos, "-%02x", LSP_FRAGMENT(from));
370
time2string (u_int32_t time) {
371
char *pos = datestring;
377
if(time/SECS_PER_YEAR)
378
pos += sprintf (pos, "%uY",time/SECS_PER_YEAR);
379
rest=time%SECS_PER_YEAR;
380
if(rest/SECS_PER_MONTH)
381
pos += sprintf (pos, "%uM",rest/SECS_PER_MONTH);
382
rest=rest%SECS_PER_MONTH;
383
if(rest/SECS_PER_WEEK)
384
pos += sprintf (pos, "%uw",rest/SECS_PER_WEEK);
385
rest=rest%SECS_PER_WEEK;
386
if(rest/SECS_PER_DAY)
387
pos += sprintf (pos, "%ud",rest/SECS_PER_DAY);
388
rest=rest%SECS_PER_DAY;
389
if(rest/SECS_PER_HOUR)
390
pos += sprintf (pos, "%uh",rest/SECS_PER_HOUR);
391
rest=rest%SECS_PER_HOUR;
392
if(rest/SECS_PER_MINUTE)
393
pos += sprintf (pos, "%um",rest/SECS_PER_MINUTE);
394
rest=rest%SECS_PER_MINUTE;
396
pos += sprintf (pos, "%us",rest);
404
* routine to decrement a timer by a random
407
* first argument is the timer and the second is
411
isis_jitter (unsigned long timer, unsigned long jitter)
421
* randomizing just the percent value provides
422
* no good random numbers - hence the spread
423
* to RANDOM_SPREAD (100000), which is ok as
424
* most IS-IS timers are no longer than 16 bit
427
j = 1 + (int) ((RANDOM_SPREAD * rand()) / (RAND_MAX + 1.0 ));
429
k = timer - (timer * (100 - jitter))/100;
431
timer = timer - (k * j / RANDOM_SPREAD);
437
newprefix2inaddr (u_char *prefix_start, u_char prefix_masklen)
439
memset(&new_prefix, 0, sizeof (new_prefix));
440
memcpy(&new_prefix, prefix_start, (prefix_masklen & 0x3F) ?
441
((((prefix_masklen & 0x3F)-1)>>3)+1) : 0);
446
* Returns host.name if any, otherwise
447
* it returns the system hostname.
452
static struct utsname names;
453
const char *hostname;
454
extern struct host host;
456
hostname = host.name;
459
hostname = names.nodename;