1 /* $NetBSD: rtquery.c,v 1.15 2001/04/06 11:13:46 wiz Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgment: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/protosw.h> 38 #include <sys/socket.h> 39 #include <sys/time.h> 40 #include <netinet/in.h> 41 #define RIPVERSION RIPv2 42 #include <protocols/routed.h> 43 #include <arpa/inet.h> 44 #include <netdb.h> 45 #include <errno.h> 46 #include <unistd.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #ifdef sgi 51 #include <strings.h> 52 #include <bstring.h> 53 #endif 54 55 #define UNUSED __attribute__((unused)) 56 #ifndef __RCSID 57 #define __RCSID(_s) static const char rcsid[] UNUSED = _s 58 #endif 59 #ifndef __COPYRIGHT 60 #define __COPYRIGHT(_s) static const char copyright[] UNUSED = _s 61 #endif 62 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n" 63 "The Regents of the University of California." 64 " All rights reserved.\n"); 65 #ifdef __NetBSD__ 66 __RCSID("$NetBSD: rtquery.c,v 1.15 2001/04/06 11:13:46 wiz Exp $"); 67 #elif defined(__FreeBSD__) 68 __RCSID("$FreeBSD$"); 69 #else 70 __RCSID("Revision: 2.23 "); 71 #ident "Revision: 2.23 " 72 #endif 73 #include <md5.h> 74 75 #ifndef sgi 76 #define _HAVE_SIN_LEN 77 #endif 78 79 #define WTIME 15 /* Time to wait for all responses */ 80 #define STIME (250*1000) /* usec to wait for another response */ 81 82 int soc; 83 84 const char *pgmname; 85 86 union { 87 struct rip rip; 88 char packet[MAXPACKETSIZE+MAXPATHLEN]; 89 } omsg_buf; 90 #define OMSG omsg_buf.rip 91 int omsg_len = sizeof(struct rip); 92 93 union { 94 struct rip rip; 95 char packet[MAXPACKETSIZE+1024]; 96 } imsg_buf; 97 #define IMSG imsg_buf.rip 98 99 int nflag; /* numbers, no names */ 100 int pflag; /* play the `gated` game */ 101 int ripv2 = 1; /* use RIP version 2 */ 102 int wtime = WTIME; 103 int rflag; /* 1=ask about a particular route */ 104 int trace, not_trace; /* send trace command or not */ 105 int auth_type = RIP_AUTH_NONE; 106 char passwd[RIP_AUTH_PW_LEN]; 107 u_long keyid; 108 109 struct timeval sent; /* when query sent */ 110 111 static char localhost_str[] = "localhost"; 112 static char *default_argv[] = {localhost_str, 0}; 113 114 static void rip_input(struct sockaddr_in*, int); 115 static int out(const char *); 116 static void trace_loop(char *argv[]) __attribute((__noreturn__)); 117 static void query_loop(char *argv[], int) __attribute((__noreturn__)); 118 static int getnet(char *, struct netinfo *); 119 static u_int std_mask(u_int); 120 static int parse_quote(char **, const char *, char *, char *, int); 121 static void usage(void); 122 123 124 int 125 main(int argc, 126 char *argv[]) 127 { 128 int ch, bsize; 129 char *p, *options, *value, delim; 130 const char *result; 131 132 OMSG.rip_nets[0].n_dst = RIP_DEFAULT; 133 OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC; 134 OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY); 135 136 pgmname = argv[0]; 137 while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1) 138 switch (ch) { 139 case 'n': 140 not_trace = 1; 141 nflag = 1; 142 break; 143 144 case 'p': 145 not_trace = 1; 146 pflag = 1; 147 break; 148 149 case '1': 150 ripv2 = 0; 151 break; 152 153 case 'w': 154 not_trace = 1; 155 wtime = (int)strtoul(optarg, &p, 0); 156 if (*p != '\0' 157 || wtime <= 0) 158 usage(); 159 break; 160 161 case 'r': 162 not_trace = 1; 163 if (rflag) 164 usage(); 165 rflag = getnet(optarg, &OMSG.rip_nets[0]); 166 if (!rflag) { 167 struct hostent *hp = gethostbyname(optarg); 168 if (hp == 0) { 169 fprintf(stderr, "%s: %s:", 170 pgmname, optarg); 171 herror(0); 172 exit(1); 173 } 174 memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr, 175 sizeof(OMSG.rip_nets[0].n_dst)); 176 OMSG.rip_nets[0].n_family = RIP_AF_INET; 177 OMSG.rip_nets[0].n_mask = -1; 178 rflag = 1; 179 } 180 break; 181 182 case 't': 183 trace = 1; 184 options = optarg; 185 while (*options != '\0') { 186 /* messy complications to make -W -Wall happy */ 187 static char on_str[] = "on"; 188 static char more_str[] = "more"; 189 static char off_str[] = "off"; 190 static char dump_str[] = "dump"; 191 static char *traceopts[] = { 192 # define TRACE_ON 0 193 on_str, 194 # define TRACE_MORE 1 195 more_str, 196 # define TRACE_OFF 2 197 off_str, 198 # define TRACE_DUMP 3 199 dump_str, 200 0 201 }; 202 result = ""; 203 switch (getsubopt(&options,traceopts,&value)) { 204 case TRACE_ON: 205 OMSG.rip_cmd = RIPCMD_TRACEON; 206 if (!value 207 || strlen(value) > MAXPATHLEN) 208 usage(); 209 result = value; 210 break; 211 case TRACE_MORE: 212 if (value) 213 usage(); 214 OMSG.rip_cmd = RIPCMD_TRACEON; 215 break; 216 case TRACE_OFF: 217 if (value) 218 usage(); 219 OMSG.rip_cmd = RIPCMD_TRACEOFF; 220 break; 221 case TRACE_DUMP: 222 if (value) 223 usage(); 224 OMSG.rip_cmd = RIPCMD_TRACEON; 225 result = "dump/../table"; 226 break; 227 default: 228 usage(); 229 } 230 strcpy((char*)OMSG.rip_tracefile, result); 231 omsg_len += strlen(result) - sizeof(OMSG.ripun); 232 } 233 break; 234 235 case 'a': 236 not_trace = 1; 237 p = strchr(optarg,'='); 238 if (!p) 239 usage(); 240 *p++ = '\0'; 241 if (!strcasecmp("passwd",optarg)) 242 auth_type = RIP_AUTH_PW; 243 else if (!strcasecmp("md5_passwd",optarg)) 244 auth_type = RIP_AUTH_MD5; 245 else 246 usage(); 247 if (0 > parse_quote(&p,"|",&delim, 248 passwd, sizeof(passwd))) 249 usage(); 250 if (auth_type == RIP_AUTH_MD5 251 && delim == '|') { 252 keyid = strtoul(p+1,&p,0); 253 if (keyid > 255 || *p != '\0') 254 usage(); 255 } else if (delim != '\0') { 256 usage(); 257 } 258 break; 259 260 default: 261 usage(); 262 } 263 argv += optind; 264 argc -= optind; 265 if (not_trace && trace) 266 usage(); 267 if (argc == 0) { 268 argc = 1; 269 argv = default_argv; 270 } 271 272 soc = socket(AF_INET, SOCK_DGRAM, 0); 273 if (soc < 0) { 274 perror("socket"); 275 exit(2); 276 } 277 278 /* be prepared to receive a lot of routes */ 279 for (bsize = 127*1024; ; bsize -= 1024) { 280 if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF, 281 &bsize, sizeof(bsize)) == 0) 282 break; 283 if (bsize <= 4*1024) { 284 perror("setsockopt SO_RCVBUF"); 285 break; 286 } 287 } 288 289 if (trace) 290 trace_loop(argv); 291 else 292 query_loop(argv, argc); 293 /* NOTREACHED */ 294 return 0; 295 } 296 297 298 static void 299 usage(void) 300 { 301 fprintf(stderr, 302 "usage: rtquery [-np1] [-r tgt_rt] [-w wtime]" 303 " [-a type=passwd] host1 [host2 ...]\n" 304 "\trtquery -t {on=filename|more|off|dump}" 305 " host1 [host2 ...]\n"); 306 exit(1); 307 } 308 309 310 /* tell the target hosts about tracing 311 */ 312 static void 313 trace_loop(char *argv[]) 314 { 315 struct sockaddr_in myaddr; 316 int res; 317 318 if (geteuid() != 0) { 319 (void)fprintf(stderr, "-t requires UID 0\n"); 320 exit(1); 321 } 322 323 if (ripv2) { 324 OMSG.rip_vers = RIPv2; 325 } else { 326 OMSG.rip_vers = RIPv1; 327 } 328 329 memset(&myaddr, 0, sizeof(myaddr)); 330 myaddr.sin_family = AF_INET; 331 #ifdef _HAVE_SIN_LEN 332 myaddr.sin_len = sizeof(myaddr); 333 #endif 334 myaddr.sin_port = htons(IPPORT_RESERVED-1); 335 while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { 336 if (errno != EADDRINUSE 337 || myaddr.sin_port == 0) { 338 perror("bind"); 339 exit(2); 340 } 341 myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1); 342 } 343 344 res = 1; 345 while (*argv != 0) { 346 if (out(*argv++) <= 0) 347 res = 0; 348 } 349 exit(res); 350 } 351 352 353 /* query all of the listed hosts 354 */ 355 static void 356 query_loop(char *argv[], int argc) 357 { 358 # define NA0 (OMSG.rip_auths[0]) 359 # define NA2 (OMSG.rip_auths[2]) 360 struct seen { 361 struct seen *next; 362 struct in_addr addr; 363 } *seen, *sp; 364 int answered = 0; 365 int cc; 366 fd_set bits; 367 struct timeval now, delay; 368 struct sockaddr_in from; 369 int fromlen; 370 MD5_CTX md5_ctx; 371 372 373 OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST; 374 if (ripv2) { 375 OMSG.rip_vers = RIPv2; 376 if (auth_type == RIP_AUTH_PW) { 377 OMSG.rip_nets[1] = OMSG.rip_nets[0]; 378 NA0.a_family = RIP_AF_AUTH; 379 NA0.a_type = RIP_AUTH_PW; 380 memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN); 381 omsg_len += sizeof(OMSG.rip_nets[0]); 382 383 } else if (auth_type == RIP_AUTH_MD5) { 384 OMSG.rip_nets[1] = OMSG.rip_nets[0]; 385 NA0.a_family = RIP_AF_AUTH; 386 NA0.a_type = RIP_AUTH_MD5; 387 NA0.au.a_md5.md5_keyid = (int8_t)keyid; 388 NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN; 389 NA0.au.a_md5.md5_seqno = 0; 390 cc = (char *)&NA2-(char *)&OMSG; 391 NA0.au.a_md5.md5_pkt_len = htons(cc); 392 NA2.a_family = RIP_AF_AUTH; 393 NA2.a_type = htons(1); 394 MD5Init(&md5_ctx); 395 MD5Update(&md5_ctx, 396 (u_char *)&OMSG, cc); 397 MD5Update(&md5_ctx, 398 (u_char *)passwd, RIP_AUTH_MD5_LEN); 399 MD5Final(NA2.au.au_pw, &md5_ctx); 400 omsg_len += 2*sizeof(OMSG.rip_nets[0]); 401 } 402 403 } else { 404 OMSG.rip_vers = RIPv1; 405 OMSG.rip_nets[0].n_mask = 0; 406 } 407 408 /* ask the first (valid) host */ 409 seen = 0; 410 while (0 > out(*argv++)) { 411 if (*argv == 0) 412 exit(1); 413 answered++; 414 } 415 416 FD_ZERO(&bits); 417 for (;;) { 418 FD_SET(soc, &bits); 419 delay.tv_sec = 0; 420 delay.tv_usec = STIME; 421 cc = select(soc+1, &bits, 0,0, &delay); 422 if (cc > 0) { 423 fromlen = sizeof(from); 424 cc = recvfrom(soc, imsg_buf.packet, 425 sizeof(imsg_buf.packet), 0, 426 (struct sockaddr *)&from, &fromlen); 427 if (cc < 0) { 428 perror("recvfrom"); 429 exit(1); 430 } 431 /* count the distinct responding hosts. 432 * You cannot match responding hosts with 433 * addresses to which queries were transmitted, 434 * because a router might respond with a 435 * different source address. 436 */ 437 for (sp = seen; sp != 0; sp = sp->next) { 438 if (sp->addr.s_addr == from.sin_addr.s_addr) 439 break; 440 } 441 if (sp == 0) { 442 sp = malloc(sizeof(*sp)); 443 if (sp == 0) { 444 fprintf(stderr, 445 "rtquery: malloc failed\n"); 446 exit(1); 447 } 448 sp->addr = from.sin_addr; 449 sp->next = seen; 450 seen = sp; 451 answered++; 452 } 453 454 rip_input(&from, cc); 455 continue; 456 } 457 458 if (cc < 0) { 459 if (errno == EINTR) 460 continue; 461 perror("select"); 462 exit(1); 463 } 464 465 /* After a pause in responses, probe another host. 466 * This reduces the intermingling of answers. 467 */ 468 while (*argv != 0 && 0 > out(*argv++)) 469 answered++; 470 471 /* continue until no more packets arrive 472 * or we have heard from all hosts 473 */ 474 if (answered >= argc) 475 break; 476 477 /* or until we have waited a long time 478 */ 479 if (gettimeofday(&now, 0) < 0) { 480 perror("gettimeofday(now)"); 481 exit(1); 482 } 483 if (sent.tv_sec + wtime <= now.tv_sec) 484 break; 485 } 486 487 /* fail if there was no answer */ 488 exit (answered >= argc ? 0 : 1); 489 } 490 491 492 /* send to one host 493 */ 494 static int 495 out(const char *host) 496 { 497 struct sockaddr_in router; 498 struct hostent *hp; 499 500 if (gettimeofday(&sent, 0) < 0) { 501 perror("gettimeofday(sent)"); 502 return -1; 503 } 504 505 memset(&router, 0, sizeof(router)); 506 router.sin_family = AF_INET; 507 #ifdef _HAVE_SIN_LEN 508 router.sin_len = sizeof(router); 509 #endif 510 if (!inet_aton(host, &router.sin_addr)) { 511 hp = gethostbyname(host); 512 if (hp == 0) { 513 herror(host); 514 return -1; 515 } 516 memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr)); 517 } 518 router.sin_port = htons(RIP_PORT); 519 520 if (sendto(soc, &omsg_buf, omsg_len, 0, 521 (struct sockaddr *)&router, sizeof(router)) < 0) { 522 perror(host); 523 return -1; 524 } 525 526 return 0; 527 } 528 529 530 /* 531 * Convert string to printable characters 532 */ 533 static char * 534 qstring(u_char *s, int len) 535 { 536 static char buf[8*20+1]; 537 char *p; 538 u_char *s2, c; 539 540 541 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) { 542 c = *s++; 543 if (c == '\0') { 544 for (s2 = s+1; s2 < &s[len]; s2++) { 545 if (*s2 != '\0') 546 break; 547 } 548 if (s2 >= &s[len]) 549 goto exit; 550 } 551 552 if (c >= ' ' && c < 0x7f && c != '\\') { 553 *p++ = c; 554 continue; 555 } 556 *p++ = '\\'; 557 switch (c) { 558 case '\\': 559 *p++ = '\\'; 560 break; 561 case '\n': 562 *p++= 'n'; 563 break; 564 case '\r': 565 *p++= 'r'; 566 break; 567 case '\t': 568 *p++ = 't'; 569 break; 570 case '\b': 571 *p++ = 'b'; 572 break; 573 default: 574 p += sprintf(p,"%o",c); 575 break; 576 } 577 } 578 exit: 579 *p = '\0'; 580 return buf; 581 } 582 583 584 /* 585 * Handle an incoming RIP packet. 586 */ 587 static void 588 rip_input(struct sockaddr_in *from, 589 int size) 590 { 591 struct netinfo *n, *lim; 592 struct in_addr in; 593 const char *name; 594 char net_buf[80]; 595 u_char hash[RIP_AUTH_MD5_LEN]; 596 MD5_CTX md5_ctx; 597 u_char md5_authed = 0; 598 u_int mask, dmask; 599 char *sp; 600 int i; 601 struct hostent *hp; 602 struct netent *np; 603 struct netauth *na; 604 605 606 if (nflag) { 607 printf("%s:", inet_ntoa(from->sin_addr)); 608 } else { 609 hp = gethostbyaddr((char*)&from->sin_addr, 610 sizeof(struct in_addr), AF_INET); 611 if (hp == 0) { 612 printf("%s:", 613 inet_ntoa(from->sin_addr)); 614 } else { 615 printf("%s (%s):", hp->h_name, 616 inet_ntoa(from->sin_addr)); 617 } 618 } 619 if (IMSG.rip_cmd != RIPCMD_RESPONSE) { 620 printf("\n unexpected response type %d\n", IMSG.rip_cmd); 621 return; 622 } 623 printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers, 624 (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "", 625 size); 626 if (size > MAXPACKETSIZE) { 627 if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) { 628 printf(" at least %d bytes too long\n", 629 size-MAXPACKETSIZE); 630 size = (int)sizeof(imsg_buf) - (int)sizeof(*n); 631 } else { 632 printf(" %d bytes too long\n", 633 size-MAXPACKETSIZE); 634 } 635 } else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) { 636 printf(" response of bad length=%d\n", size); 637 } 638 639 n = IMSG.rip_nets; 640 lim = (struct netinfo *)((char*)n + size) - 1; 641 for (; n <= lim; n++) { 642 name = ""; 643 if (n->n_family == RIP_AF_INET) { 644 in.s_addr = n->n_dst; 645 (void)strcpy(net_buf, inet_ntoa(in)); 646 647 mask = ntohl(n->n_mask); 648 dmask = mask & -mask; 649 if (mask != 0) { 650 sp = &net_buf[strlen(net_buf)]; 651 if (IMSG.rip_vers == RIPv1) { 652 (void)sprintf(sp," mask=%#x ? ",mask); 653 mask = 0; 654 } else if (mask + dmask == 0) { 655 for (i = 0; 656 (i != 32 657 && ((1<<i)&mask) == 0); 658 i++) 659 continue; 660 (void)sprintf(sp, "/%d",32-i); 661 } else { 662 (void)sprintf(sp," (mask %#x)", mask); 663 } 664 } 665 666 if (!nflag) { 667 if (mask == 0) { 668 mask = std_mask(in.s_addr); 669 if ((ntohl(in.s_addr) & ~mask) != 0) 670 mask = 0; 671 } 672 /* Without a netmask, do not worry about 673 * whether the destination is a host or a 674 * network. Try both and use the first name 675 * we get. 676 * 677 * If we have a netmask we can make a 678 * good guess. 679 */ 680 if ((in.s_addr & ~mask) == 0) { 681 np = getnetbyaddr((long)in.s_addr, 682 AF_INET); 683 if (np != 0) 684 name = np->n_name; 685 else if (in.s_addr == 0) 686 name = "default"; 687 } 688 if (name[0] == '\0' 689 && ((in.s_addr & ~mask) != 0 690 || mask == 0xffffffff)) { 691 hp = gethostbyaddr((char*)&in, 692 sizeof(in), 693 AF_INET); 694 if (hp != 0) 695 name = hp->h_name; 696 } 697 } 698 699 } else if (n->n_family == RIP_AF_AUTH) { 700 na = (struct netauth*)n; 701 if (na->a_type == RIP_AUTH_PW 702 && n == IMSG.rip_nets) { 703 (void)printf(" Password Authentication:" 704 " \"%s\"\n", 705 qstring(na->au.au_pw, 706 RIP_AUTH_PW_LEN)); 707 continue; 708 } 709 710 if (na->a_type == RIP_AUTH_MD5 711 && n == IMSG.rip_nets) { 712 (void)printf(" MD5 Auth" 713 " len=%d KeyID=%d" 714 " auth_len=%d" 715 " seqno=%#x" 716 " rsvd=%#x,%#x\n", 717 ntohs(na->au.a_md5.md5_pkt_len), 718 na->au.a_md5.md5_keyid, 719 na->au.a_md5.md5_auth_len, 720 (int)ntohl(na->au.a_md5.md5_seqno), 721 na->au.a_md5.rsvd[0], 722 na->au.a_md5.rsvd[1]); 723 md5_authed = 1; 724 continue; 725 } 726 (void)printf(" Authentication type %d: ", 727 ntohs(na->a_type)); 728 for (i = 0; i < (int)sizeof(na->au.au_pw); i++) 729 (void)printf("%02x ", na->au.au_pw[i]); 730 putc('\n', stdout); 731 if (md5_authed && n+1 > lim 732 && na->a_type == ntohs(1)) { 733 MD5Init(&md5_ctx); 734 MD5Update(&md5_ctx, (u_char *)&IMSG, 735 (char *)na-(char *)&IMSG); 736 MD5Update(&md5_ctx, (u_char *)passwd, 737 RIP_AUTH_MD5_LEN); 738 MD5Final(hash, &md5_ctx); 739 (void)printf(" %s hash\n", 740 memcmp(hash, na->au.au_pw, 741 sizeof(hash)) 742 ? "WRONG" : "correct"); 743 } 744 continue; 745 746 } else { 747 (void)sprintf(net_buf, "(af %#x) %d.%d.%d.%d", 748 ntohs(n->n_family), 749 (unsigned char)(n->n_dst >> 24), 750 (unsigned char)(n->n_dst >> 16), 751 (unsigned char)(n->n_dst >> 8), 752 (unsigned char)n->n_dst); 753 } 754 755 (void)printf(" %-18s metric %2d %-10s", 756 net_buf, (int)ntohl(n->n_metric), name); 757 758 if (n->n_nhop != 0) { 759 in.s_addr = n->n_nhop; 760 if (nflag) 761 hp = 0; 762 else 763 hp = gethostbyaddr((char*)&in, sizeof(in), 764 AF_INET); 765 (void)printf(" nhop=%-15s%s", 766 (hp != 0) ? hp->h_name : inet_ntoa(in), 767 (IMSG.rip_vers == RIPv1) ? " ?" : ""); 768 } 769 if (n->n_tag != 0) 770 (void)printf(" tag=%#x%s", n->n_tag, 771 (IMSG.rip_vers == RIPv1) ? " ?" : ""); 772 putc('\n', stdout); 773 } 774 } 775 776 777 /* Return the classical netmask for an IP address. 778 */ 779 static u_int 780 std_mask(u_int addr) /* in network order */ 781 { 782 NTOHL(addr); /* was a host, not a network */ 783 784 if (addr == 0) /* default route has mask 0 */ 785 return 0; 786 if (IN_CLASSA(addr)) 787 return IN_CLASSA_NET; 788 if (IN_CLASSB(addr)) 789 return IN_CLASSB_NET; 790 return IN_CLASSC_NET; 791 } 792 793 794 /* get a network number as a name or a number, with an optional "/xx" 795 * netmask. 796 */ 797 static int /* 0=bad */ 798 getnet(char *name, 799 struct netinfo *rt) 800 { 801 int i; 802 struct netent *nentp; 803 u_int mask; 804 struct in_addr in; 805 char hname[MAXHOSTNAMELEN+1]; 806 char *mname, *p; 807 808 809 /* Detect and separate "1.2.3.4/24" 810 */ 811 if (0 != (mname = strrchr(name,'/'))) { 812 i = (int)(mname - name); 813 if (i > (int)sizeof(hname)-1) /* name too long */ 814 return 0; 815 memmove(hname, name, i); 816 hname[i] = '\0'; 817 mname++; 818 name = hname; 819 } 820 821 nentp = getnetbyname(name); 822 if (nentp != 0) { 823 in.s_addr = nentp->n_net; 824 } else if (inet_aton(name, &in) == 1) { 825 NTOHL(in.s_addr); 826 } else { 827 return 0; 828 } 829 830 if (mname == 0) { 831 mask = std_mask(in.s_addr); 832 if ((~mask & in.s_addr) != 0) 833 mask = 0xffffffff; 834 } else { 835 mask = (u_int)strtoul(mname, &p, 0); 836 if (*p != '\0' || mask > 32) 837 return 0; 838 mask = 0xffffffff << (32-mask); 839 } 840 841 rt->n_dst = htonl(in.s_addr); 842 rt->n_family = RIP_AF_INET; 843 rt->n_mask = htonl(mask); 844 return 1; 845 } 846 847 848 /* strtok(), but honoring backslash 849 */ 850 static int /* -1=bad */ 851 parse_quote(char **linep, 852 const char *delims, 853 char *delimp, 854 char *buf, 855 int lim) 856 { 857 char c, *pc; 858 const char *p; 859 860 861 pc = *linep; 862 if (*pc == '\0') 863 return -1; 864 865 for (;;) { 866 if (lim == 0) 867 return -1; 868 c = *pc++; 869 if (c == '\0') 870 break; 871 872 if (c == '\\' && *pc != '\0') { 873 if ((c = *pc++) == 'n') { 874 c = '\n'; 875 } else if (c == 'r') { 876 c = '\r'; 877 } else if (c == 't') { 878 c = '\t'; 879 } else if (c == 'b') { 880 c = '\b'; 881 } else if (c >= '0' && c <= '7') { 882 c -= '0'; 883 if (*pc >= '0' && *pc <= '7') { 884 c = (c<<3)+(*pc++ - '0'); 885 if (*pc >= '0' && *pc <= '7') 886 c = (c<<3)+(*pc++ - '0'); 887 } 888 } 889 890 } else { 891 for (p = delims; *p != '\0'; ++p) { 892 if (*p == c) 893 goto exit; 894 } 895 } 896 897 *buf++ = c; 898 --lim; 899 } 900 exit: 901 if (delimp != 0) 902 *delimp = c; 903 *linep = pc-1; 904 if (lim != 0) 905 *buf = '\0'; 906 return 0; 907 } 908