rtquery.c revision 1.1.1.4 1 /*-
2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 char copyright[] =
35 "@(#) Copyright (c) 1982, 1986, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
37
38 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
39 static char sccsid[] = "@(#)query.c 8.1 (Berkeley) 6/5/93";
40 #elif defined(__NetBSD__)
41 static char rcsid[] = "$NetBSD: rtquery.c,v 1.1.1.4 1998/06/02 17:41:28 thorpej Exp $";
42 #endif
43 #ident "$Revision: 1.1.1.4 $"
44
45 #include <sys/param.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/time.h>
49 #include <netinet/in.h>
50 #define RIPVERSION RIPv2
51 #include <protocols/routed.h>
52 #include <arpa/inet.h>
53 #include <netdb.h>
54 #include <errno.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #ifdef sgi
60 #include <strings.h>
61 #include <bstring.h>
62 #endif
63
64 #ifndef sgi
65 #define _HAVE_SIN_LEN
66 #endif
67
68 #define MD5_DIGEST_LEN 16
69 typedef struct {
70 u_int32_t state[4]; /* state (ABCD) */
71 u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
72 unsigned char buffer[64]; /* input buffer */
73 } MD5_CTX;
74 extern void MD5Init(MD5_CTX*);
75 extern void MD5Update(MD5_CTX*, u_char*, u_int);
76 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
77
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 s;
83
84 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 *default_argv[2] = {"localhost", 0};
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:")) != EOF)
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 bcopy(hp->h_addr, &OMSG.rip_nets[0].n_dst,
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) {
258 usage: fprintf(stderr, "%s: [-np1] [-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 if (argc == 0) {
266 argc = 1;
267 argv = default_argv;
268 }
269
270 s = socket(AF_INET, SOCK_DGRAM, 0);
271 if (s < 0) {
272 perror("socket");
273 exit(2);
274 }
275
276 /* be prepared to receive a lot of routes */
277 for (bsize = 127*1024; ; bsize -= 1024) {
278 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
279 &bsize, sizeof(bsize)) == 0)
280 break;
281 if (bsize <= 4*1024) {
282 perror("setsockopt SO_RCVBUF");
283 break;
284 }
285 }
286
287 if (trace)
288 trace_loop(argv);
289 else
290 query_loop(argv, argc);
291 /* NOTREACHED */
292 }
293
294
295 /* tell the target hosts about tracing
296 */
297 static void
298 trace_loop(char *argv[])
299 {
300 struct sockaddr_in myaddr;
301 int res;
302
303 if (geteuid() != 0) {
304 (void)fprintf(stderr, "-t requires UID 0\n");
305 exit(1);
306 }
307
308 if (ripv2) {
309 OMSG.rip_vers = RIPv2;
310 } else {
311 OMSG.rip_vers = RIPv1;
312 }
313
314 bzero(&myaddr, sizeof(myaddr));
315 myaddr.sin_family = AF_INET;
316 #ifdef _HAVE_SIN_LEN
317 myaddr.sin_len = sizeof(myaddr);
318 #endif
319 myaddr.sin_port = htons(IPPORT_RESERVED-1);
320 while (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
321 if (errno != EADDRINUSE
322 || myaddr.sin_port == 0) {
323 perror("bind");
324 exit(2);
325 }
326 myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
327 }
328
329 res = 1;
330 while (*argv != 0) {
331 if (out(*argv++) <= 0)
332 res = 0;
333 }
334 exit(res);
335 }
336
337
338 /* query all of the listed hosts
339 */
340 static void
341 query_loop(char *argv[], int argc)
342 {
343 # define NA0 (OMSG.rip_auths[0])
344 # define NA2 (OMSG.rip_auths[2])
345 struct seen {
346 struct seen *next;
347 struct in_addr addr;
348 } *seen, *sp;
349 int answered = 0;
350 int cc;
351 fd_set bits;
352 struct timeval now, delay;
353 struct sockaddr_in from;
354 int fromlen;
355 MD5_CTX md5_ctx;
356
357
358 OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
359 if (ripv2) {
360 OMSG.rip_vers = RIPv2;
361 if (auth_type == RIP_AUTH_PW) {
362 OMSG.rip_nets[1] = OMSG.rip_nets[0];
363 NA0.a_family = RIP_AF_AUTH;
364 NA0.a_type = RIP_AUTH_PW;
365 bcopy(passwd, NA0.au.au_pw,
366 RIP_AUTH_PW_LEN);
367 omsg_len += sizeof(OMSG.rip_nets[0]);
368
369 } else if (auth_type == RIP_AUTH_MD5) {
370 OMSG.rip_nets[1] = OMSG.rip_nets[0];
371 NA0.a_family = RIP_AF_AUTH;
372 NA0.a_type = RIP_AUTH_MD5;
373 NA0.au.a_md5.md5_keyid = (int8_t)keyid;
374 NA0.au.a_md5.md5_auth_len = RIP_AUTH_PW_LEN;
375 NA0.au.a_md5.md5_seqno = 0;
376 NA0.au.a_md5.md5_pkt_len = sizeof(OMSG.rip_nets[1]);
377 NA2.a_family = RIP_AF_AUTH;
378 NA2.a_type = 1;
379 bcopy(passwd, NA2.au.au_pw, sizeof(NA2.au.au_pw));
380 MD5Init(&md5_ctx);
381 MD5Update(&md5_ctx, (u_char *)&NA0,
382 (char *)(&NA2+1) - (char *)&NA0);
383 MD5Final(NA2.au.au_pw, &md5_ctx);
384 omsg_len += 2*sizeof(OMSG.rip_nets[0]);
385 }
386
387 } else {
388 OMSG.rip_vers = RIPv1;
389 OMSG.rip_nets[0].n_mask = 0;
390 }
391
392 /* ask the first (valid) host */
393 seen = 0;
394 while (0 > out(*argv++)) {
395 if (*argv == 0)
396 exit(-1);
397 answered++;
398 }
399
400 FD_ZERO(&bits);
401 for (;;) {
402 FD_SET(s, &bits);
403 delay.tv_sec = 0;
404 delay.tv_usec = STIME;
405 cc = select(s+1, &bits, 0,0, &delay);
406 if (cc > 0) {
407 fromlen = sizeof(from);
408 cc = recvfrom(s, imsg_buf.packet,
409 sizeof(imsg_buf.packet), 0,
410 (struct sockaddr *)&from, &fromlen);
411 if (cc < 0) {
412 perror("recvfrom");
413 exit(1);
414 }
415 /* count the distinct responding hosts.
416 * You cannot match responding hosts with
417 * addresses to which queries were transmitted,
418 * because a router might respond with a
419 * different source address.
420 */
421 for (sp = seen; sp != 0; sp = sp->next) {
422 if (sp->addr.s_addr == from.sin_addr.s_addr)
423 break;
424 }
425 if (sp == 0) {
426 sp = malloc(sizeof(*sp));
427 sp->addr = from.sin_addr;
428 sp->next = seen;
429 seen = sp;
430 answered++;
431 }
432
433 rip_input(&from, cc);
434 continue;
435 }
436
437 if (cc < 0) {
438 if ( errno == EINTR)
439 continue;
440 perror("select");
441 exit(1);
442 }
443
444 /* After a pause in responses, probe another host.
445 * This reduces the intermingling of answers.
446 */
447 while (*argv != 0 && 0 > out(*argv++))
448 answered++;
449
450 /* continue until no more packets arrive
451 * or we have heard from all hosts
452 */
453 if (answered >= argc)
454 break;
455
456 /* or until we have waited a long time
457 */
458 if (gettimeofday(&now, 0) < 0) {
459 perror("gettimeofday(now)");
460 exit(1);
461 }
462 if (sent.tv_sec + wtime <= now.tv_sec)
463 break;
464 }
465
466 /* fail if there was no answer */
467 exit (answered >= argc ? 0 : 1);
468 }
469
470
471 /* send to one host
472 */
473 static int
474 out(char *host)
475 {
476 struct sockaddr_in router;
477 struct hostent *hp;
478
479 if (gettimeofday(&sent, 0) < 0) {
480 perror("gettimeofday(sent)");
481 return -1;
482 }
483
484 bzero(&router, sizeof(router));
485 router.sin_family = AF_INET;
486 #ifdef _HAVE_SIN_LEN
487 router.sin_len = sizeof(router);
488 #endif
489 if (!inet_aton(host, &router.sin_addr)) {
490 hp = gethostbyname(host);
491 if (hp == 0) {
492 herror(host);
493 return -1;
494 }
495 bcopy(hp->h_addr, &router.sin_addr, sizeof(router.sin_addr));
496 }
497 router.sin_port = htons(RIP_PORT);
498
499 if (sendto(s, &omsg_buf, omsg_len, 0,
500 (struct sockaddr *)&router, sizeof(router)) < 0) {
501 perror(host);
502 return -1;
503 }
504
505 return 0;
506 }
507
508
509 /*
510 * Convert string to printable characters
511 */
512 static char *
513 qstring(u_char *s, int len)
514 {
515 static char buf[8*20+1];
516 char *p;
517 u_char *s2, c;
518
519
520 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
521 c = *s++;
522 if (c == '\0') {
523 for (s2 = s+1; s2 < &s[len]; s2++) {
524 if (*s2 != '\0')
525 break;
526 }
527 if (s2 >= &s[len])
528 goto exit;
529 }
530
531 if (c >= ' ' && c < 0x7f && c != '\\') {
532 *p++ = c;
533 continue;
534 }
535 *p++ = '\\';
536 switch (c) {
537 case '\\':
538 *p++ = '\\';
539 break;
540 case '\n':
541 *p++= 'n';
542 break;
543 case '\r':
544 *p++= 'r';
545 break;
546 case '\t':
547 *p++ = 't';
548 break;
549 case '\b':
550 *p++ = 'b';
551 break;
552 default:
553 p += sprintf(p,"%o",c);
554 break;
555 }
556 }
557 exit:
558 *p = '\0';
559 return buf;
560 }
561
562
563 /*
564 * Handle an incoming RIP packet.
565 */
566 static void
567 rip_input(struct sockaddr_in *from,
568 int size)
569 {
570 struct netinfo *n, *lim;
571 struct in_addr in;
572 char *name;
573 char net_buf[80];
574 u_int mask, dmask;
575 char *sp;
576 int i;
577 struct hostent *hp;
578 struct netent *np;
579 struct netauth *na;
580
581
582 if (nflag) {
583 printf("%s:", inet_ntoa(from->sin_addr));
584 } else {
585 hp = gethostbyaddr((char*)&from->sin_addr,
586 sizeof(struct in_addr), AF_INET);
587 if (hp == 0) {
588 printf("%s:",
589 inet_ntoa(from->sin_addr));
590 } else {
591 printf("%s (%s):", hp->h_name,
592 inet_ntoa(from->sin_addr));
593 }
594 }
595 if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
596 printf("\n unexpected response type %d\n", IMSG.rip_cmd);
597 return;
598 }
599 printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
600 (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
601 size);
602 if (size > MAXPACKETSIZE) {
603 if (size > sizeof(imsg_buf) - sizeof(*n)) {
604 printf(" at least %d bytes too long\n",
605 size-MAXPACKETSIZE);
606 size = sizeof(imsg_buf) - sizeof(*n);
607 } else {
608 printf(" %d bytes too long\n",
609 size-MAXPACKETSIZE);
610 }
611 } else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
612 printf(" response of bad length=%d\n", size);
613 }
614
615 n = IMSG.rip_nets;
616 lim = (struct netinfo *)((char*)n + size) - 1;
617 for (; n <= lim; n++) {
618 name = "";
619 if (n->n_family == RIP_AF_INET) {
620 in.s_addr = n->n_dst;
621 (void)strcpy(net_buf, inet_ntoa(in));
622
623 mask = ntohl(n->n_mask);
624 dmask = mask & -mask;
625 if (mask != 0) {
626 sp = &net_buf[strlen(net_buf)];
627 if (IMSG.rip_vers == RIPv1) {
628 (void)sprintf(sp," mask=%#x ? ",mask);
629 mask = 0;
630 } else if (mask + dmask == 0) {
631 for (i = 0;
632 (i != 32
633 && ((1<<i)&mask) == 0);
634 i++)
635 continue;
636 (void)sprintf(sp, "/%d",32-i);
637 } else {
638 (void)sprintf(sp," (mask %#x)", mask);
639 }
640 }
641
642 if (!nflag) {
643 if (mask == 0) {
644 mask = std_mask(in.s_addr);
645 if ((ntohl(in.s_addr) & ~mask) != 0)
646 mask = 0;
647 }
648 /* Without a netmask, do not worry about
649 * whether the destination is a host or a
650 * network. Try both and use the first name
651 * we get.
652 *
653 * If we have a netmask we can make a
654 * good guess.
655 */
656 if ((in.s_addr & ~mask) == 0) {
657 np = getnetbyaddr((long)in.s_addr,
658 AF_INET);
659 if (np != 0)
660 name = np->n_name;
661 else if (in.s_addr == 0)
662 name = "default";
663 }
664 if (name[0] == '\0'
665 && ((in.s_addr & ~mask) != 0
666 || mask == 0xffffffff)) {
667 hp = gethostbyaddr((char*)&in,
668 sizeof(in),
669 AF_INET);
670 if (hp != 0)
671 name = hp->h_name;
672 }
673 }
674
675 } else if (n->n_family == RIP_AF_AUTH) {
676 na = (struct netauth*)n;
677 if (na->a_type == RIP_AUTH_PW
678 && n == IMSG.rip_nets) {
679 (void)printf(" Password Authentication:"
680 " \"%s\"\n",
681 qstring(na->au.au_pw,
682 RIP_AUTH_PW_LEN));
683 continue;
684 }
685
686 if (na->a_type == RIP_AUTH_MD5
687 && n == IMSG.rip_nets) {
688 (void)printf(" MD5 Authentication"
689 " len=%d KeyID=%d"
690 " seqno=%d"
691 " rsvd=%#x,%#x\n",
692 na->au.a_md5.md5_pkt_len,
693 na->au.a_md5.md5_keyid,
694 na->au.a_md5.md5_seqno,
695 na->au.a_md5.rsvd[0],
696 na->au.a_md5.rsvd[1]);
697 continue;
698 }
699 (void)printf(" Authentication type %d: ",
700 ntohs(na->a_type));
701 for (i = 0; i < sizeof(na->au.au_pw); i++)
702 (void)printf("%02x ", na->au.au_pw[i]);
703 putc('\n', stdout);
704 continue;
705
706 } else {
707 (void)sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
708 ntohs(n->n_family),
709 (char)(n->n_dst >> 24),
710 (char)(n->n_dst >> 16),
711 (char)(n->n_dst >> 8),
712 (char)n->n_dst);
713 }
714
715 (void)printf(" %-18s metric %2d %-10s",
716 net_buf, ntohl(n->n_metric), name);
717
718 if (n->n_nhop != 0) {
719 in.s_addr = n->n_nhop;
720 if (nflag)
721 hp = 0;
722 else
723 hp = gethostbyaddr((char*)&in, sizeof(in),
724 AF_INET);
725 (void)printf(" nhop=%-15s%s",
726 (hp != 0) ? hp->h_name : inet_ntoa(in),
727 (IMSG.rip_vers == RIPv1) ? " ?" : "");
728 }
729 if (n->n_tag != 0)
730 (void)printf(" tag=%#x%s", n->n_tag,
731 (IMSG.rip_vers == RIPv1) ? " ?" : "");
732 putc('\n', stdout);
733 }
734 }
735
736
737 /* Return the classical netmask for an IP address.
738 */
739 static u_int
740 std_mask(u_int addr) /* in network order */
741 {
742 NTOHL(addr); /* was a host, not a network */
743
744 if (addr == 0) /* default route has mask 0 */
745 return 0;
746 if (IN_CLASSA(addr))
747 return IN_CLASSA_NET;
748 if (IN_CLASSB(addr))
749 return IN_CLASSB_NET;
750 return IN_CLASSC_NET;
751 }
752
753
754 /* get a network number as a name or a number, with an optional "/xx"
755 * netmask.
756 */
757 static int /* 0=bad */
758 getnet(char *name,
759 struct netinfo *rt)
760 {
761 int i;
762 struct netent *nentp;
763 u_int mask;
764 struct in_addr in;
765 char hname[MAXHOSTNAMELEN+1];
766 char *mname, *p;
767
768
769 /* Detect and separate "1.2.3.4/24"
770 */
771 if (0 != (mname = rindex(name,'/'))) {
772 i = (int)(mname - name);
773 if (i > sizeof(hname)-1) /* name too long */
774 return 0;
775 bcopy(name, hname, i);
776 hname[i] = '\0';
777 mname++;
778 name = hname;
779 }
780
781 nentp = getnetbyname(name);
782 if (nentp != 0) {
783 in.s_addr = nentp->n_net;
784 } else if (inet_aton(name, &in) == 1) {
785 NTOHL(in.s_addr);
786 } else {
787 return 0;
788 }
789
790 if (mname == 0) {
791 mask = std_mask(in.s_addr);
792 if ((~mask & in.s_addr) != 0)
793 mask = 0xffffffff;
794 } else {
795 mask = (u_int)strtoul(mname, &p, 0);
796 if (*p != '\0' || mask > 32)
797 return 0;
798 mask = 0xffffffff << (32-mask);
799 }
800
801 rt->n_dst = htonl(in.s_addr);
802 rt->n_family = RIP_AF_INET;
803 rt->n_mask = htonl(mask);
804 return 1;
805 }
806
807
808 /* strtok(), but honoring backslash
809 */
810 static int /* -1=bad */
811 parse_quote(char **linep,
812 char *delims,
813 char *delimp,
814 char *buf,
815 int lim)
816 {
817 char c, *pc, *p;
818
819
820 pc = *linep;
821 if (*pc == '\0')
822 return -1;
823
824 for (;;) {
825 if (lim == 0)
826 return -1;
827 c = *pc++;
828 if (c == '\0')
829 break;
830
831 if (c == '\\' && pc != '\0') {
832 if ((c = *pc++) == 'n') {
833 c = '\n';
834 } else if (c == 'r') {
835 c = '\r';
836 } else if (c == 't') {
837 c = '\t';
838 } else if (c == 'b') {
839 c = '\b';
840 } else if (c >= '0' && c <= '7') {
841 c -= '0';
842 if (*pc >= '0' && *pc <= '7') {
843 c = (c<<3)+(*pc++ - '0');
844 if (*pc >= '0' && *pc <= '7')
845 c = (c<<3)+(*pc++ - '0');
846 }
847 }
848
849 } else {
850 for (p = delims; *p != '\0'; ++p) {
851 if (*p == c)
852 goto exit;
853 }
854 }
855
856 *buf++ = c;
857 --lim;
858 }
859 exit:
860 if (delimp != 0)
861 *delimp = c;
862 *linep = pc-1;
863 if (lim != 0)
864 *buf = '\0';
865 return 0;
866 }
867