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