jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 24 | #include <zebra.h> |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 25 | |
| 26 | #include "stream.h" |
| 27 | #include "vty.h" |
| 28 | #include "hash.h" |
| 29 | #include "if.h" |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 30 | #include "command.h" |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 31 | |
| 32 | #include "isisd/dict.h" |
| 33 | #include "isisd/isis_constants.h" |
| 34 | #include "isisd/isis_common.h" |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 35 | #include "isisd/isis_flags.h" |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 36 | #include "isisd/isis_circuit.h" |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 37 | #include "isisd/isis_csm.h" |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 38 | #include "isisd/isisd.h" |
| 39 | #include "isisd/isis_misc.h" |
| 40 | |
| 41 | #include "isisd/isis_tlv.h" |
| 42 | #include "isisd/isis_lsp.h" |
| 43 | #include "isisd/isis_constants.h" |
| 44 | #include "isisd/isis_adjacency.h" |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 45 | #include "isisd/isis_dynhn.h" |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 46 | |
hasso | 73d1aea | 2004-09-24 10:45:28 +0000 | [diff] [blame] | 47 | /* staticly assigned vars for printing purposes */ |
| 48 | struct in_addr new_prefix; |
| 49 | /* len of xxxx.xxxx.xxxx + place for #0 termination */ |
| 50 | char sysid[15]; |
| 51 | /* len of xxxx.xxxx.xxxx + place for #0 termination */ |
| 52 | char snpa[15]; |
| 53 | /* len of xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx */ |
| 54 | char isonet[51]; |
| 55 | /* + place for #0 termination */ |
| 56 | /* len of xxxx.xxxx.xxxx.xx.xx + place for #0 termination */ |
| 57 | char lspid[21]; |
| 58 | /* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */ |
| 59 | char datestring[20]; |
| 60 | char nlpidstring[30]; |
| 61 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 62 | /* |
| 63 | * This converts the isonet to its printable format |
| 64 | */ |
hasso | 1cd8084 | 2004-10-07 20:07:40 +0000 | [diff] [blame] | 65 | const char * |
Christian Franke | 77277a1 | 2015-11-10 18:04:43 +0100 | [diff] [blame] | 66 | isonet_print (const u_char * from, int len) |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 67 | { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 68 | int i = 0; |
| 69 | char *pos = isonet; |
| 70 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 71 | if (!from) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 72 | return "unknown"; |
| 73 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 74 | while (i < len) |
| 75 | { |
| 76 | if (i & 1) |
| 77 | { |
| 78 | sprintf (pos, "%02x", *(from + i)); |
| 79 | pos += 2; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | if (i == (len - 1)) |
| 84 | { /* No dot at the end of address */ |
| 85 | sprintf (pos, "%02x", *(from + i)); |
| 86 | pos += 2; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | sprintf (pos, "%02x.", *(from + i)); |
| 91 | pos += 3; |
| 92 | } |
| 93 | } |
| 94 | i++; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 95 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 96 | *(pos) = '\0'; |
| 97 | return isonet; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Returns 0 on error, length of buff on ok |
| 102 | * extract dot from the dotted str, and insert all the number in a buff |
| 103 | */ |
| 104 | int |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 105 | dotformat2buff (u_char * buff, const char * dotted) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 106 | { |
| 107 | int dotlen, len = 0; |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 108 | const char *pos = dotted; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 109 | u_char number[3]; |
| 110 | int nextdotpos = 2; |
| 111 | |
| 112 | number[2] = '\0'; |
Paul Jakma | 41b36e9 | 2006-12-08 01:09:50 +0000 | [diff] [blame] | 113 | dotlen = strlen(dotted); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 114 | if (dotlen > 50) |
| 115 | { |
| 116 | /* this can't be an iso net, its too long */ |
| 117 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 118 | } |
| 119 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 120 | while ((pos - dotted) < dotlen && len < 20) |
| 121 | { |
| 122 | if (*pos == '.') |
| 123 | { |
| 124 | /* we expect the . at 2, and than every 5 */ |
| 125 | if ((pos - dotted) != nextdotpos) |
| 126 | { |
| 127 | len = 0; |
| 128 | break; |
| 129 | } |
| 130 | nextdotpos += 5; |
| 131 | pos++; |
| 132 | continue; |
| 133 | } |
| 134 | /* we must have at least two chars left here */ |
| 135 | if (dotlen - (pos - dotted) < 2) |
| 136 | { |
| 137 | len = 0; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | if ((isxdigit ((int) *pos)) && (isxdigit ((int) *(pos + 1)))) |
| 142 | { |
| 143 | memcpy (number, pos, 2); |
| 144 | pos += 2; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | len = 0; |
| 149 | break; |
| 150 | } |
| 151 | |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 152 | *(buff + len) = (char) strtol ((char *)number, NULL, 16); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 153 | len++; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 154 | } |
| 155 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 156 | return len; |
| 157 | } |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 158 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 159 | /* |
| 160 | * conversion of XXXX.XXXX.XXXX to memory |
| 161 | */ |
| 162 | int |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 163 | sysid2buff (u_char * buff, const char * dotted) |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 164 | { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 165 | int len = 0; |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 166 | const char *pos = dotted; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 167 | u_char number[3]; |
| 168 | |
| 169 | number[2] = '\0'; |
| 170 | // surely not a sysid_string if not 14 length |
Paul Jakma | 41b36e9 | 2006-12-08 01:09:50 +0000 | [diff] [blame] | 171 | if (strlen (dotted) != 14) |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 172 | { |
| 173 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 174 | } |
| 175 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 176 | while (len < ISIS_SYS_ID_LEN) |
| 177 | { |
| 178 | if (*pos == '.') |
| 179 | { |
| 180 | /* the . is not positioned correctly */ |
| 181 | if (((pos - dotted) != 4) && ((pos - dotted) != 9)) |
| 182 | { |
| 183 | len = 0; |
| 184 | break; |
| 185 | } |
| 186 | pos++; |
| 187 | continue; |
| 188 | } |
| 189 | if ((isxdigit ((int) *pos)) && (isxdigit ((int) *(pos + 1)))) |
| 190 | { |
| 191 | memcpy (number, pos, 2); |
| 192 | pos += 2; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | len = 0; |
| 197 | break; |
| 198 | } |
| 199 | |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 200 | *(buff + len) = (char) strtol ((char *)number, NULL, 16); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 201 | len++; |
| 202 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 203 | |
| 204 | return len; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * converts the nlpids struct (filled by TLV #129) |
| 210 | * into a string |
| 211 | */ |
| 212 | |
| 213 | char * |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 214 | nlpid2string (struct nlpids *nlpids) |
| 215 | { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 216 | char *pos = nlpidstring; |
| 217 | int i; |
| 218 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 219 | for (i = 0; i < nlpids->count; i++) |
| 220 | { |
| 221 | switch (nlpids->nlpids[i]) |
| 222 | { |
| 223 | case NLPID_IP: |
| 224 | pos += sprintf (pos, "IPv4"); |
| 225 | break; |
| 226 | case NLPID_IPV6: |
| 227 | pos += sprintf (pos, "IPv6"); |
| 228 | break; |
| 229 | case NLPID_SNAP: |
| 230 | pos += sprintf (pos, "SNAP"); |
| 231 | break; |
| 232 | case NLPID_CLNP: |
| 233 | pos += sprintf (pos, "CLNP"); |
| 234 | break; |
| 235 | case NLPID_ESIS: |
| 236 | pos += sprintf (pos, "ES-IS"); |
| 237 | break; |
| 238 | default: |
| 239 | pos += sprintf (pos, "unknown"); |
| 240 | break; |
| 241 | } |
| 242 | if (nlpids->count - i > 1) |
| 243 | pos += sprintf (pos, ", "); |
| 244 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 245 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 246 | |
| 247 | *(pos) = '\0'; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 248 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 249 | return nlpidstring; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * supports the given af ? |
| 254 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 255 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 256 | speaks (struct nlpids *nlpids, int family) |
| 257 | { |
| 258 | int i, speaks = 0; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 259 | |
| 260 | if (nlpids == (struct nlpids *) NULL) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 261 | return speaks; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 262 | for (i = 0; i < nlpids->count; i++) |
| 263 | { |
| 264 | if (family == AF_INET && nlpids->nlpids[i] == NLPID_IP) |
| 265 | speaks = 1; |
| 266 | if (family == AF_INET6 && nlpids->nlpids[i] == NLPID_IPV6) |
| 267 | speaks = 1; |
| 268 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 269 | |
| 270 | return speaks; |
| 271 | } |
| 272 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 273 | /* |
| 274 | * Returns 0 on error, IS-IS Circuit Type on ok |
| 275 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 276 | int |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 277 | string2circuit_t (const char * str) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 278 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 279 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 280 | if (!str) |
| 281 | return 0; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 282 | |
Paul Jakma | 41b36e9 | 2006-12-08 01:09:50 +0000 | [diff] [blame] | 283 | if (!strcmp (str, "level-1")) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 284 | return IS_LEVEL_1; |
| 285 | |
Paul Jakma | 41b36e9 | 2006-12-08 01:09:50 +0000 | [diff] [blame] | 286 | if (!strcmp (str, "level-2-only") || !strcmp (str, "level-2")) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 287 | return IS_LEVEL_2; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 288 | |
Paul Jakma | 41b36e9 | 2006-12-08 01:09:50 +0000 | [diff] [blame] | 289 | if (!strcmp (str, "level-1-2")) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 290 | return IS_LEVEL_1_AND_2; |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | const char * |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 296 | circuit_state2string (int state) |
| 297 | { |
| 298 | |
| 299 | switch (state) |
| 300 | { |
| 301 | case C_STATE_INIT: |
| 302 | return "Init"; |
| 303 | case C_STATE_CONF: |
| 304 | return "Config"; |
| 305 | case C_STATE_UP: |
| 306 | return "Up"; |
| 307 | default: |
| 308 | return "Unknown"; |
| 309 | } |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | const char * |
| 314 | circuit_type2string (int type) |
| 315 | { |
| 316 | |
| 317 | switch (type) |
| 318 | { |
| 319 | case CIRCUIT_T_P2P: |
| 320 | return "p2p"; |
| 321 | case CIRCUIT_T_BROADCAST: |
| 322 | return "lan"; |
| 323 | case CIRCUIT_T_LOOPBACK: |
| 324 | return "loopback"; |
| 325 | default: |
| 326 | return "Unknown"; |
| 327 | } |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | const char * |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 332 | circuit_t2string (int circuit_t) |
| 333 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 334 | switch (circuit_t) |
| 335 | { |
| 336 | case IS_LEVEL_1: |
| 337 | return "L1"; |
| 338 | case IS_LEVEL_2: |
| 339 | return "L2"; |
| 340 | case IS_LEVEL_1_AND_2: |
| 341 | return "L1L2"; |
| 342 | default: |
| 343 | return "??"; |
| 344 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 345 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 346 | return NULL; /* not reached */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | const char * |
| 350 | syst2string (int type) |
| 351 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 352 | switch (type) |
| 353 | { |
| 354 | case ISIS_SYSTYPE_ES: |
| 355 | return "ES"; |
| 356 | case ISIS_SYSTYPE_IS: |
| 357 | return "IS"; |
| 358 | case ISIS_SYSTYPE_L1_IS: |
| 359 | return "1"; |
| 360 | case ISIS_SYSTYPE_L2_IS: |
| 361 | return "2"; |
| 362 | default: |
| 363 | return "??"; |
| 364 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 365 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 366 | return NULL; /* not reached */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Print functions - we print to static vars |
| 371 | */ |
hasso | 1cd8084 | 2004-10-07 20:07:40 +0000 | [diff] [blame] | 372 | const char * |
Christian Franke | 77277a1 | 2015-11-10 18:04:43 +0100 | [diff] [blame] | 373 | snpa_print (const u_char * from) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 374 | { |
| 375 | int i = 0; |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 376 | u_char *pos = (u_char *)snpa; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 377 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 378 | if (!from) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 379 | return "unknown"; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 380 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 381 | while (i < ETH_ALEN - 1) |
| 382 | { |
| 383 | if (i & 1) |
| 384 | { |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 385 | sprintf ((char *)pos, "%02x.", *(from + i)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 386 | pos += 3; |
| 387 | } |
| 388 | else |
| 389 | { |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 390 | sprintf ((char *)pos, "%02x", *(from + i)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 391 | pos += 2; |
| 392 | |
| 393 | } |
| 394 | i++; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 395 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 396 | |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 397 | sprintf ((char *)pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1))); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 398 | pos += 2; |
| 399 | *(pos) = '\0'; |
| 400 | |
| 401 | return snpa; |
| 402 | } |
| 403 | |
hasso | 1cd8084 | 2004-10-07 20:07:40 +0000 | [diff] [blame] | 404 | const char * |
Christian Franke | 77277a1 | 2015-11-10 18:04:43 +0100 | [diff] [blame] | 405 | sysid_print (const u_char * from) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 406 | { |
| 407 | int i = 0; |
| 408 | char *pos = sysid; |
| 409 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 410 | if (!from) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 411 | return "unknown"; |
| 412 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 413 | while (i < ISIS_SYS_ID_LEN - 1) |
| 414 | { |
| 415 | if (i & 1) |
| 416 | { |
| 417 | sprintf (pos, "%02x.", *(from + i)); |
| 418 | pos += 3; |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | sprintf (pos, "%02x", *(from + i)); |
| 423 | pos += 2; |
| 424 | |
| 425 | } |
| 426 | i++; |
| 427 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 428 | |
| 429 | sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1))); |
| 430 | pos += 2; |
| 431 | *(pos) = '\0'; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 432 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 433 | return sysid; |
| 434 | } |
| 435 | |
hasso | 1cd8084 | 2004-10-07 20:07:40 +0000 | [diff] [blame] | 436 | const char * |
Christian Franke | 77277a1 | 2015-11-10 18:04:43 +0100 | [diff] [blame] | 437 | rawlspid_print (const u_char * from) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 438 | { |
| 439 | char *pos = lspid; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 440 | if (!from) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 441 | return "unknown"; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 442 | memcpy (pos, sysid_print (from), 15); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 443 | pos += 14; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 444 | sprintf (pos, ".%02x", LSP_PSEUDO_ID (from)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 445 | pos += 3; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 446 | sprintf (pos, "-%02x", LSP_FRAGMENT (from)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 447 | pos += 3; |
| 448 | |
| 449 | *(pos) = '\0'; |
| 450 | |
| 451 | return lspid; |
| 452 | } |
| 453 | |
hasso | 1cd8084 | 2004-10-07 20:07:40 +0000 | [diff] [blame] | 454 | const char * |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 455 | time2string (u_int32_t time) |
| 456 | { |
| 457 | char *pos = datestring; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 458 | u_int32_t rest; |
| 459 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 460 | if (time == 0) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 461 | return "-"; |
| 462 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 463 | if (time / SECS_PER_YEAR) |
| 464 | pos += sprintf (pos, "%uY", time / SECS_PER_YEAR); |
| 465 | rest = time % SECS_PER_YEAR; |
| 466 | if (rest / SECS_PER_MONTH) |
| 467 | pos += sprintf (pos, "%uM", rest / SECS_PER_MONTH); |
| 468 | rest = rest % SECS_PER_MONTH; |
| 469 | if (rest / SECS_PER_WEEK) |
| 470 | pos += sprintf (pos, "%uw", rest / SECS_PER_WEEK); |
| 471 | rest = rest % SECS_PER_WEEK; |
| 472 | if (rest / SECS_PER_DAY) |
| 473 | pos += sprintf (pos, "%ud", rest / SECS_PER_DAY); |
| 474 | rest = rest % SECS_PER_DAY; |
| 475 | if (rest / SECS_PER_HOUR) |
| 476 | pos += sprintf (pos, "%uh", rest / SECS_PER_HOUR); |
| 477 | rest = rest % SECS_PER_HOUR; |
| 478 | if (rest / SECS_PER_MINUTE) |
| 479 | pos += sprintf (pos, "%um", rest / SECS_PER_MINUTE); |
| 480 | rest = rest % SECS_PER_MINUTE; |
| 481 | if (rest) |
| 482 | pos += sprintf (pos, "%us", rest); |
| 483 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 484 | *(pos) = 0; |
| 485 | |
| 486 | return datestring; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * routine to decrement a timer by a random |
| 491 | * number |
| 492 | * |
| 493 | * first argument is the timer and the second is |
| 494 | * the jitter |
| 495 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 496 | unsigned long |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 497 | isis_jitter (unsigned long timer, unsigned long jitter) |
| 498 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 499 | int j, k; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 500 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 501 | if (jitter >= 100) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 502 | return timer; |
| 503 | |
| 504 | if (timer == 1) |
| 505 | return timer; |
| 506 | /* |
| 507 | * randomizing just the percent value provides |
| 508 | * no good random numbers - hence the spread |
| 509 | * to RANDOM_SPREAD (100000), which is ok as |
| 510 | * most IS-IS timers are no longer than 16 bit |
| 511 | */ |
| 512 | |
Donald Sharp | f31bab4 | 2015-06-19 19:26:19 -0400 | [diff] [blame] | 513 | j = 1 + (int) ((RANDOM_SPREAD * random ()) / (RAND_MAX + 1.0)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 514 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 515 | k = timer - (timer * (100 - jitter)) / 100; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 516 | |
| 517 | timer = timer - (k * j / RANDOM_SPREAD); |
| 518 | |
| 519 | return timer; |
| 520 | } |
| 521 | |
| 522 | struct in_addr |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 523 | newprefix2inaddr (u_char * prefix_start, u_char prefix_masklen) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 524 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 525 | memset (&new_prefix, 0, sizeof (new_prefix)); |
| 526 | memcpy (&new_prefix, prefix_start, (prefix_masklen & 0x3F) ? |
| 527 | ((((prefix_masklen & 0x3F) - 1) >> 3) + 1) : 0); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 528 | return new_prefix; |
| 529 | } |
| 530 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 531 | /* |
| 532 | * Returns host.name if any, otherwise |
| 533 | * it returns the system hostname. |
| 534 | */ |
| 535 | const char * |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 536 | unix_hostname (void) |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 537 | { |
| 538 | static struct utsname names; |
| 539 | const char *hostname; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 540 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 541 | hostname = host.name; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 542 | if (!hostname) |
| 543 | { |
| 544 | uname (&names); |
| 545 | hostname = names.nodename; |
| 546 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 547 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 548 | return hostname; |
| 549 | } |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * Returns the dynamic hostname associated with the passed system ID. |
| 553 | * If no dynamic hostname found then returns formatted system ID. |
| 554 | */ |
| 555 | const char * |
Christian Franke | 77277a1 | 2015-11-10 18:04:43 +0100 | [diff] [blame] | 556 | print_sys_hostname (const u_char *sysid) |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 557 | { |
| 558 | struct isis_dynhn *dyn; |
| 559 | |
| 560 | if (!sysid) |
| 561 | return "nullsysid"; |
| 562 | |
| 563 | /* For our system ID return our host name */ |
| 564 | if (memcmp(sysid, isis->sysid, ISIS_SYS_ID_LEN) == 0) |
| 565 | return unix_hostname(); |
| 566 | |
| 567 | dyn = dynhn_find_by_id (sysid); |
| 568 | if (dyn) |
| 569 | return (const char *)dyn->name.name; |
| 570 | |
| 571 | return sysid_print (sysid); |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * This function is a generic utility that logs data of given length. |
| 576 | * Move this to a shared lib so that any protocol can use it. |
| 577 | */ |
| 578 | void |
| 579 | zlog_dump_data (void *data, int len) |
| 580 | { |
| 581 | int i; |
| 582 | unsigned char *p; |
| 583 | unsigned char c; |
| 584 | char bytestr[4]; |
| 585 | char addrstr[10]; |
| 586 | char hexstr[ 16*3 + 5]; |
| 587 | char charstr[16*1 + 5]; |
| 588 | |
| 589 | p = data; |
| 590 | memset (bytestr, 0, sizeof(bytestr)); |
| 591 | memset (addrstr, 0, sizeof(addrstr)); |
| 592 | memset (hexstr, 0, sizeof(hexstr)); |
| 593 | memset (charstr, 0, sizeof(charstr)); |
| 594 | |
| 595 | for (i = 1; i <= len; i++) |
| 596 | { |
| 597 | c = *p; |
| 598 | if (isalnum (c) == 0) |
| 599 | c = '.'; |
| 600 | |
| 601 | /* store address for this line */ |
| 602 | if ((i % 16) == 1) |
| 603 | snprintf (addrstr, sizeof(addrstr), "%p", p); |
| 604 | |
| 605 | /* store hex str (for left side) */ |
| 606 | snprintf (bytestr, sizeof (bytestr), "%02X ", *p); |
| 607 | strncat (hexstr, bytestr, sizeof (hexstr) - strlen (hexstr) - 1); |
| 608 | |
| 609 | /* store char str (for right side) */ |
| 610 | snprintf (bytestr, sizeof (bytestr), "%c", c); |
| 611 | strncat (charstr, bytestr, sizeof (charstr) - strlen (charstr) - 1); |
| 612 | |
| 613 | if ((i % 16) == 0) |
| 614 | { |
| 615 | /* line completed */ |
| 616 | zlog_debug ("[%8.8s] %-50.50s %s", addrstr, hexstr, charstr); |
| 617 | hexstr[0] = 0; |
| 618 | charstr[0] = 0; |
| 619 | } |
| 620 | else if ((i % 8) == 0) |
| 621 | { |
| 622 | /* half line: add whitespaces */ |
| 623 | strncat (hexstr, " ", sizeof (hexstr) - strlen (hexstr) - 1); |
| 624 | strncat (charstr, " ", sizeof (charstr) - strlen (charstr) - 1); |
| 625 | } |
| 626 | p++; /* next byte */ |
| 627 | } |
| 628 | |
| 629 | /* print rest of buffer if not empty */ |
| 630 | if (strlen (hexstr) > 0) |
| 631 | zlog_debug ("[%8.8s] %-50.50s %s", addrstr, hexstr, charstr); |
| 632 | return; |
| 633 | } |