kernel - Update swapcache manual page, fix breakage in last commit
[dragonfly.git] / sbin / routed / rtquery / rtquery.c
blobd3743a3b2694238ffcc44e96d2c833a766665271
1 /*-
2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
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 acknowledgment:
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.
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.
33 * $FreeBSD: src/sbin/routed/rtquery/rtquery.c,v 1.13 1999/08/28 00:14:21 peter Exp $
34 * $DragonFly: src/sbin/routed/rtquery/rtquery.c,v 1.4 2004/12/18 21:43:46 swildner Exp $
37 char copyright[] =
38 "@(#) Copyright (c) 1982, 1986, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/time.h>
46 #include <netinet/in.h>
47 #define RIPVERSION RIPv2
48 #include <protocols/routed.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51 #include <errno.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #ifdef sgi
57 #include <strings.h>
58 #include <bstring.h>
59 #endif
61 #if !defined(sgi) && !defined(__NetBSD__)
62 static char sccsid[] __attribute__((unused))= "@(#)query.c 8.1 (Berkeley) 6/5/93";
63 #elif defined(__NetBSD__)
64 __RCSID("$NetBSD: rtquery.c,v 1.10 1999/02/23 10:47:41 christos Exp $");
65 #endif
67 #ifndef sgi
68 #define _HAVE_SIN_LEN
69 #endif
71 #define MD5_DIGEST_LEN 16
72 typedef struct {
73 u_int32_t state[4]; /* state (ABCD) */
74 u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
75 unsigned char buffer[64]; /* input buffer */
76 } MD5_CTX;
77 extern void MD5Init(MD5_CTX*);
78 extern void MD5Update(MD5_CTX*, u_char*, u_int);
79 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
82 #define WTIME 15 /* Time to wait for all responses */
83 #define STIME (250*1000) /* usec to wait for another response */
85 int soc;
87 const char *pgmname;
89 union {
90 struct rip rip;
91 char packet[MAXPACKETSIZE+MAXPATHLEN];
92 } omsg_buf;
93 #define OMSG omsg_buf.rip
94 int omsg_len = sizeof(struct rip);
96 union {
97 struct rip rip;
98 char packet[MAXPACKETSIZE+1024];
99 } imsg_buf;
100 #define IMSG imsg_buf.rip
102 int nflag; /* numbers, no names */
103 int pflag; /* play the `gated` game */
104 int ripv2 = 1; /* use RIP version 2 */
105 int wtime = WTIME;
106 int rflag; /* 1=ask about a particular route */
107 int trace, not_trace; /* send trace command or not */
108 int auth_type = RIP_AUTH_NONE;
109 char passwd[RIP_AUTH_PW_LEN];
110 u_long keyid;
112 struct timeval sent; /* when query sent */
114 static char localhost_str[] = "localhost";
115 static char *default_argv[] = {localhost_str, 0};
117 static void rip_input(struct sockaddr_in*, int);
118 static int out(const char *);
119 static void trace_loop(char *argv[]) __attribute((__noreturn__));
120 static void query_loop(char *argv[], int) __attribute((__noreturn__));
121 static int getnet(char *, struct netinfo *);
122 static u_int std_mask(u_int);
123 static int parse_quote(char **, const char *, char *, char *, int);
124 static void usage(void);
128 main(int argc,
129 char *argv[])
131 int ch, bsize;
132 char *p, *options, *value, delim;
133 const char *result;
135 OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
136 OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
137 OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
139 pgmname = argv[0];
140 while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
141 switch (ch) {
142 case 'n':
143 not_trace = 1;
144 nflag = 1;
145 break;
147 case 'p':
148 not_trace = 1;
149 pflag = 1;
150 break;
152 case '1':
153 ripv2 = 0;
154 break;
156 case 'w':
157 not_trace = 1;
158 wtime = (int)strtoul(optarg, &p, 0);
159 if (*p != '\0'
160 || wtime <= 0)
161 usage();
162 break;
164 case 'r':
165 not_trace = 1;
166 if (rflag)
167 usage();
168 rflag = getnet(optarg, &OMSG.rip_nets[0]);
169 if (!rflag) {
170 struct hostent *hp = gethostbyname(optarg);
171 if (hp == 0) {
172 fprintf(stderr, "%s: %s:",
173 pgmname, optarg);
174 herror(0);
175 exit(1);
177 memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
178 sizeof(OMSG.rip_nets[0].n_dst));
179 OMSG.rip_nets[0].n_family = RIP_AF_INET;
180 OMSG.rip_nets[0].n_mask = -1;
181 rflag = 1;
183 break;
185 case 't':
186 trace = 1;
187 options = optarg;
188 while (*options != '\0') {
189 /* messy complications to make -W -Wall happy */
190 static char on_str[] = "on";
191 static char more_str[] = "more";
192 static char off_str[] = "off";
193 static char dump_str[] = "dump";
194 static char *traceopts[] = {
195 # define TRACE_ON 0
196 on_str,
197 # define TRACE_MORE 1
198 more_str,
199 # define TRACE_OFF 2
200 off_str,
201 # define TRACE_DUMP 3
202 dump_str,
205 result = "";
206 switch (getsubopt(&options,traceopts,&value)) {
207 case TRACE_ON:
208 OMSG.rip_cmd = RIPCMD_TRACEON;
209 if (!value
210 || strlen(value) > MAXPATHLEN)
211 usage();
212 result = value;
213 break;
214 case TRACE_MORE:
215 if (value)
216 usage();
217 OMSG.rip_cmd = RIPCMD_TRACEON;
218 break;
219 case TRACE_OFF:
220 if (value)
221 usage();
222 OMSG.rip_cmd = RIPCMD_TRACEOFF;
223 break;
224 case TRACE_DUMP:
225 if (value)
226 usage();
227 OMSG.rip_cmd = RIPCMD_TRACEON;
228 result = "dump/../table";
229 break;
230 default:
231 usage();
233 strcpy((char*)OMSG.rip_tracefile, result);
234 omsg_len += strlen(result) - sizeof(OMSG.ripun);
236 break;
238 case 'a':
239 not_trace = 1;
240 p = strchr(optarg,'=');
241 if (!p)
242 usage();
243 *p++ = '\0';
244 if (!strcasecmp("passwd",optarg))
245 auth_type = RIP_AUTH_PW;
246 else if (!strcasecmp("md5_passwd",optarg))
247 auth_type = RIP_AUTH_MD5;
248 else
249 usage();
250 if (0 > parse_quote(&p,"|",&delim,
251 passwd, sizeof(passwd)))
252 usage();
253 if (auth_type == RIP_AUTH_MD5
254 && delim == '|') {
255 keyid = strtoul(p+1,&p,0);
256 if (keyid > 255 || *p != '\0')
257 usage();
258 } else if (delim != '\0') {
259 usage();
261 break;
263 default:
264 usage();
266 argv += optind;
267 argc -= optind;
268 if (not_trace && trace)
269 usage();
270 if (argc == 0) {
271 argc = 1;
272 argv = default_argv;
275 soc = socket(AF_INET, SOCK_DGRAM, 0);
276 if (soc < 0) {
277 perror("socket");
278 exit(2);
281 /* be prepared to receive a lot of routes */
282 for (bsize = 127*1024; ; bsize -= 1024) {
283 if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
284 &bsize, sizeof(bsize)) == 0)
285 break;
286 if (bsize <= 4*1024) {
287 perror("setsockopt SO_RCVBUF");
288 break;
292 if (trace)
293 trace_loop(argv);
294 else
295 query_loop(argv, argc);
296 /* NOTREACHED */
297 return 0;
301 static void
302 usage(void)
304 fprintf(stderr,
305 "usage: rtquery [-np1] [-r tgt_rt] [-w wtime]"
306 " [-a type=passwd] host1 [host2 ...]\n"
307 "\trtquery -t {on=filename|more|off|dump}"
308 " host1 [host2 ...]\n");
309 exit(1);
313 /* tell the target hosts about tracing
315 static void
316 trace_loop(char *argv[])
318 struct sockaddr_in myaddr;
319 int res;
321 if (geteuid() != 0) {
322 fprintf(stderr, "-t requires UID 0\n");
323 exit(1);
326 if (ripv2) {
327 OMSG.rip_vers = RIPv2;
328 } else {
329 OMSG.rip_vers = RIPv1;
332 memset(&myaddr, 0, sizeof(myaddr));
333 myaddr.sin_family = AF_INET;
334 #ifdef _HAVE_SIN_LEN
335 myaddr.sin_len = sizeof(myaddr);
336 #endif
337 myaddr.sin_port = htons(IPPORT_RESERVED-1);
338 while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
339 if (errno != EADDRINUSE
340 || myaddr.sin_port == 0) {
341 perror("bind");
342 exit(2);
344 myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
347 res = 1;
348 while (*argv != 0) {
349 if (out(*argv++) <= 0)
350 res = 0;
352 exit(res);
356 /* query all of the listed hosts
358 static void
359 query_loop(char *argv[], int argc)
361 # define NA0 (OMSG.rip_auths[0])
362 # define NA2 (OMSG.rip_auths[2])
363 struct seen {
364 struct seen *next;
365 struct in_addr addr;
366 } *seen, *sp;
367 int answered = 0;
368 int cc;
369 fd_set bits;
370 struct timeval now, delay;
371 struct sockaddr_in from;
372 int fromlen;
373 MD5_CTX md5_ctx;
376 OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
377 if (ripv2) {
378 OMSG.rip_vers = RIPv2;
379 if (auth_type == RIP_AUTH_PW) {
380 OMSG.rip_nets[1] = OMSG.rip_nets[0];
381 NA0.a_family = RIP_AF_AUTH;
382 NA0.a_type = RIP_AUTH_PW;
383 memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
384 omsg_len += sizeof(OMSG.rip_nets[0]);
386 } else if (auth_type == RIP_AUTH_MD5) {
387 OMSG.rip_nets[1] = OMSG.rip_nets[0];
388 NA0.a_family = RIP_AF_AUTH;
389 NA0.a_type = RIP_AUTH_MD5;
390 NA0.au.a_md5.md5_keyid = (int8_t)keyid;
391 NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
392 NA0.au.a_md5.md5_seqno = 0;
393 cc = (char *)&NA2-(char *)&OMSG;
394 NA0.au.a_md5.md5_pkt_len = htons(cc);
395 NA2.a_family = RIP_AF_AUTH;
396 NA2.a_type = htons(1);
397 MD5Init(&md5_ctx);
398 MD5Update(&md5_ctx,
399 (u_char *)&OMSG, cc);
400 MD5Update(&md5_ctx,
401 (u_char *)passwd, RIP_AUTH_MD5_LEN);
402 MD5Final(NA2.au.au_pw, &md5_ctx);
403 omsg_len += 2*sizeof(OMSG.rip_nets[0]);
406 } else {
407 OMSG.rip_vers = RIPv1;
408 OMSG.rip_nets[0].n_mask = 0;
411 /* ask the first (valid) host */
412 seen = 0;
413 while (0 > out(*argv++)) {
414 if (*argv == 0)
415 exit(-1);
416 answered++;
419 FD_ZERO(&bits);
420 for (;;) {
421 FD_SET(soc, &bits);
422 delay.tv_sec = 0;
423 delay.tv_usec = STIME;
424 cc = select(soc+1, &bits, 0,0, &delay);
425 if (cc > 0) {
426 fromlen = sizeof(from);
427 cc = recvfrom(soc, imsg_buf.packet,
428 sizeof(imsg_buf.packet), 0,
429 (struct sockaddr *)&from, &fromlen);
430 if (cc < 0) {
431 perror("recvfrom");
432 exit(1);
434 /* count the distinct responding hosts.
435 * You cannot match responding hosts with
436 * addresses to which queries were transmitted,
437 * because a router might respond with a
438 * different source address.
440 for (sp = seen; sp != 0; sp = sp->next) {
441 if (sp->addr.s_addr == from.sin_addr.s_addr)
442 break;
444 if (sp == 0) {
445 sp = malloc(sizeof(*sp));
446 if (sp == 0) {
447 fprintf(stderr,
448 "rtquery: malloc failed\n");
449 exit(1);
451 sp->addr = from.sin_addr;
452 sp->next = seen;
453 seen = sp;
454 answered++;
457 rip_input(&from, cc);
458 continue;
461 if (cc < 0) {
462 if (errno == EINTR)
463 continue;
464 perror("select");
465 exit(1);
468 /* After a pause in responses, probe another host.
469 * This reduces the intermingling of answers.
471 while (*argv != 0 && 0 > out(*argv++))
472 answered++;
474 /* continue until no more packets arrive
475 * or we have heard from all hosts
477 if (answered >= argc)
478 break;
480 /* or until we have waited a long time
482 if (gettimeofday(&now, 0) < 0) {
483 perror("gettimeofday(now)");
484 exit(1);
486 if (sent.tv_sec + wtime <= now.tv_sec)
487 break;
490 /* fail if there was no answer */
491 exit (answered >= argc ? 0 : 1);
495 /* send to one host
497 static int
498 out(const char *host)
500 struct sockaddr_in router;
501 struct hostent *hp;
503 if (gettimeofday(&sent, 0) < 0) {
504 perror("gettimeofday(sent)");
505 return -1;
508 memset(&router, 0, sizeof(router));
509 router.sin_family = AF_INET;
510 #ifdef _HAVE_SIN_LEN
511 router.sin_len = sizeof(router);
512 #endif
513 if (!inet_aton(host, &router.sin_addr)) {
514 hp = gethostbyname(host);
515 if (hp == 0) {
516 herror(host);
517 return -1;
519 memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
521 router.sin_port = htons(RIP_PORT);
523 if (sendto(soc, &omsg_buf, omsg_len, 0,
524 (struct sockaddr *)&router, sizeof(router)) < 0) {
525 perror(host);
526 return -1;
529 return 0;
534 * Convert string to printable characters
536 static char *
537 qstring(u_char *s, int len)
539 static char buf[8*20+1];
540 char *p;
541 u_char *s2, c;
544 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
545 c = *s++;
546 if (c == '\0') {
547 for (s2 = s+1; s2 < &s[len]; s2++) {
548 if (*s2 != '\0')
549 break;
551 if (s2 >= &s[len])
552 goto exit;
555 if (c >= ' ' && c < 0x7f && c != '\\') {
556 *p++ = c;
557 continue;
559 *p++ = '\\';
560 switch (c) {
561 case '\\':
562 *p++ = '\\';
563 break;
564 case '\n':
565 *p++= 'n';
566 break;
567 case '\r':
568 *p++= 'r';
569 break;
570 case '\t':
571 *p++ = 't';
572 break;
573 case '\b':
574 *p++ = 'b';
575 break;
576 default:
577 p += sprintf(p,"%o",c);
578 break;
581 exit:
582 *p = '\0';
583 return buf;
588 * Handle an incoming RIP packet.
590 static void
591 rip_input(struct sockaddr_in *from,
592 int size)
594 struct netinfo *n, *lim;
595 struct in_addr in;
596 const char *name;
597 char net_buf[80];
598 u_char hash[RIP_AUTH_MD5_LEN];
599 MD5_CTX md5_ctx;
600 u_char md5_authed = 0;
601 u_int mask, dmask;
602 char *sp;
603 int i;
604 struct hostent *hp;
605 struct netent *np;
606 struct netauth *na;
609 if (nflag) {
610 printf("%s:", inet_ntoa(from->sin_addr));
611 } else {
612 hp = gethostbyaddr(&from->sin_addr, sizeof(struct in_addr),
613 AF_INET);
614 if (hp == 0) {
615 printf("%s:",
616 inet_ntoa(from->sin_addr));
617 } else {
618 printf("%s (%s):", hp->h_name,
619 inet_ntoa(from->sin_addr));
622 if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
623 printf("\n unexpected response type %d\n", IMSG.rip_cmd);
624 return;
626 printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
627 (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
628 size);
629 if (size > MAXPACKETSIZE) {
630 if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
631 printf(" at least %d bytes too long\n",
632 size-MAXPACKETSIZE);
633 size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
634 } else {
635 printf(" %d bytes too long\n",
636 size-MAXPACKETSIZE);
638 } else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
639 printf(" response of bad length=%d\n", size);
642 n = IMSG.rip_nets;
643 lim = (struct netinfo *)((char*)n + size) - 1;
644 for (; n <= lim; n++) {
645 name = "";
646 if (n->n_family == RIP_AF_INET) {
647 in.s_addr = n->n_dst;
648 strcpy(net_buf, inet_ntoa(in));
650 mask = ntohl(n->n_mask);
651 dmask = mask & -mask;
652 if (mask != 0) {
653 sp = &net_buf[strlen(net_buf)];
654 if (IMSG.rip_vers == RIPv1) {
655 sprintf(sp," mask=%#x ? ",mask);
656 mask = 0;
657 } else if (mask + dmask == 0) {
658 for (i = 0;
659 (i != 32
660 && ((1<<i)&mask) == 0);
661 i++)
662 continue;
663 sprintf(sp, "/%d",32-i);
664 } else {
665 sprintf(sp," (mask %#x)", mask);
669 if (!nflag) {
670 if (mask == 0) {
671 mask = std_mask(in.s_addr);
672 if ((ntohl(in.s_addr) & ~mask) != 0)
673 mask = 0;
675 /* Without a netmask, do not worry about
676 * whether the destination is a host or a
677 * network. Try both and use the first name
678 * we get.
680 * If we have a netmask we can make a
681 * good guess.
683 if ((in.s_addr & ~mask) == 0) {
684 np = getnetbyaddr((long)in.s_addr,
685 AF_INET);
686 if (np != 0)
687 name = np->n_name;
688 else if (in.s_addr == 0)
689 name = "default";
691 if (name[0] == '\0'
692 && ((in.s_addr & ~mask) != 0
693 || mask == 0xffffffff)) {
694 hp = gethostbyaddr(&in, sizeof(in),
695 AF_INET);
696 if (hp != 0)
697 name = hp->h_name;
701 } else if (n->n_family == RIP_AF_AUTH) {
702 na = (struct netauth*)n;
703 if (na->a_type == RIP_AUTH_PW
704 && n == IMSG.rip_nets) {
705 printf(" Password Authentication: \"%s\"\n",
706 qstring(na->au.au_pw, RIP_AUTH_PW_LEN));
707 continue;
710 if (na->a_type == RIP_AUTH_MD5
711 && n == IMSG.rip_nets) {
712 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;
726 printf(" Authentication type %d: ", ntohs(na->a_type));
727 for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
728 printf("%02x ", na->au.au_pw[i]);
729 putc('\n', stdout);
730 if (md5_authed && n+1 > lim
731 && na->a_type == ntohs(1)) {
732 MD5Init(&md5_ctx);
733 MD5Update(&md5_ctx, (u_char *)&IMSG,
734 (char *)na-(char *)&IMSG);
735 MD5Update(&md5_ctx, (u_char *)passwd,
736 RIP_AUTH_MD5_LEN);
737 MD5Final(hash, &md5_ctx);
738 printf(" %s hash\n",
739 memcmp(hash, na->au.au_pw, sizeof(hash))
740 ? "WRONG" : "correct");
742 continue;
744 } else {
745 sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
746 ntohs(n->n_family),
747 (char)(n->n_dst >> 24),
748 (char)(n->n_dst >> 16),
749 (char)(n->n_dst >> 8),
750 (char)n->n_dst);
753 printf(" %-18s metric %2d %-10s",
754 net_buf, (int)ntohl(n->n_metric), name);
756 if (n->n_nhop != 0) {
757 in.s_addr = n->n_nhop;
758 if (nflag)
759 hp = 0;
760 else
761 hp = gethostbyaddr(&in, sizeof(in), AF_INET);
762 printf(" nhop=%-15s%s",
763 (hp != 0) ? hp->h_name : inet_ntoa(in),
764 (IMSG.rip_vers == RIPv1) ? " ?" : "");
766 if (n->n_tag != 0)
767 printf(" tag=%#x%s", n->n_tag,
768 (IMSG.rip_vers == RIPv1) ? " ?" : "");
769 putc('\n', stdout);
774 /* Return the classical netmask for an IP address.
776 static u_int
777 std_mask(u_int addr) /* in network order */
779 addr = ntohl(addr); /* was a host, not a network */
781 if (addr == 0) /* default route has mask 0 */
782 return 0;
783 if (IN_CLASSA(addr))
784 return IN_CLASSA_NET;
785 if (IN_CLASSB(addr))
786 return IN_CLASSB_NET;
787 return IN_CLASSC_NET;
791 /* get a network number as a name or a number, with an optional "/xx"
792 * netmask.
794 static int /* 0=bad */
795 getnet(char *name,
796 struct netinfo *rt)
798 int i;
799 struct netent *nentp;
800 u_int mask;
801 struct in_addr in;
802 char hname[MAXHOSTNAMELEN+1];
803 char *mname, *p;
806 /* Detect and separate "1.2.3.4/24"
808 if (0 != (mname = strrchr(name,'/'))) {
809 i = (int)(mname - name);
810 if (i > (int)sizeof(hname)-1) /* name too long */
811 return 0;
812 memmove(hname, name, i);
813 hname[i] = '\0';
814 mname++;
815 name = hname;
818 nentp = getnetbyname(name);
819 if (nentp != 0) {
820 in.s_addr = nentp->n_net;
821 } else if (inet_aton(name, &in) == 1) {
822 in.s_addr = ntohl(in.s_addr);
823 } else {
824 return 0;
827 if (mname == 0) {
828 mask = std_mask(in.s_addr);
829 if ((~mask & in.s_addr) != 0)
830 mask = 0xffffffff;
831 } else {
832 mask = (u_int)strtoul(mname, &p, 0);
833 if (*p != '\0' || mask > 32)
834 return 0;
835 mask = 0xffffffff << (32-mask);
838 rt->n_dst = htonl(in.s_addr);
839 rt->n_family = RIP_AF_INET;
840 rt->n_mask = htonl(mask);
841 return 1;
845 /* strtok(), but honoring backslash
847 static int /* -1=bad */
848 parse_quote(char **linep,
849 const char *delims,
850 char *delimp,
851 char *buf,
852 int lim)
854 char c, *pc;
855 const char *p;
858 pc = *linep;
859 if (*pc == '\0')
860 return -1;
862 for (;;) {
863 if (lim == 0)
864 return -1;
865 c = *pc++;
866 if (c == '\0')
867 break;
869 if (c == '\\' && *pc != '\0') {
870 if ((c = *pc++) == 'n') {
871 c = '\n';
872 } else if (c == 'r') {
873 c = '\r';
874 } else if (c == 't') {
875 c = '\t';
876 } else if (c == 'b') {
877 c = '\b';
878 } else if (c >= '0' && c <= '7') {
879 c -= '0';
880 if (*pc >= '0' && *pc <= '7') {
881 c = (c<<3)+(*pc++ - '0');
882 if (*pc >= '0' && *pc <= '7')
883 c = (c<<3)+(*pc++ - '0');
887 } else {
888 for (p = delims; *p != '\0'; ++p) {
889 if (*p == c)
890 goto exit;
894 *buf++ = c;
895 --lim;
897 exit:
898 if (delimp != 0)
899 *delimp = c;
900 *linep = pc-1;
901 if (lim != 0)
902 *buf = '\0';
903 return 0;