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