1*4db4c628Sdholland /* $NetBSD: rtquery.c,v 1.25 2014/03/23 05:36:58 dholland Exp $ */
2fc1a5246Sthorpej
3b1e0bd39Sthorpej /*-
4b1e0bd39Sthorpej * Copyright (c) 1982, 1986, 1993
5b1e0bd39Sthorpej * The Regents of the University of California. All rights reserved.
6b1e0bd39Sthorpej *
7b1e0bd39Sthorpej * Redistribution and use in source and binary forms, with or without
8b1e0bd39Sthorpej * modification, are permitted provided that the following conditions
9b1e0bd39Sthorpej * are met:
10b1e0bd39Sthorpej * 1. Redistributions of source code must retain the above copyright
11b1e0bd39Sthorpej * notice, this list of conditions and the following disclaimer.
12b1e0bd39Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
13b1e0bd39Sthorpej * notice, this list of conditions and the following disclaimer in the
14b1e0bd39Sthorpej * documentation and/or other materials provided with the distribution.
15b1e0bd39Sthorpej * 3. All advertising materials mentioning features or use of this software
1694b2d428Schristos * must display the following acknowledgment:
17b1e0bd39Sthorpej * This product includes software developed by the University of
18b1e0bd39Sthorpej * California, Berkeley and its contributors.
19b1e0bd39Sthorpej * 4. Neither the name of the University nor the names of its contributors
20b1e0bd39Sthorpej * may be used to endorse or promote products derived from this software
21b1e0bd39Sthorpej * without specific prior written permission.
22b1e0bd39Sthorpej *
23b1e0bd39Sthorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24b1e0bd39Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b1e0bd39Sthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b1e0bd39Sthorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27b1e0bd39Sthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b1e0bd39Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b1e0bd39Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b1e0bd39Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b1e0bd39Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b1e0bd39Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b1e0bd39Sthorpej * SUCH DAMAGE.
34b1e0bd39Sthorpej */
35b1e0bd39Sthorpej
36f93fe60aSchristos #include <sys/cdefs.h>
37b1e0bd39Sthorpej #include <sys/param.h>
38b1e0bd39Sthorpej #include <sys/protosw.h>
39b1e0bd39Sthorpej #include <sys/socket.h>
40b1e0bd39Sthorpej #include <sys/time.h>
41b1e0bd39Sthorpej #include <netinet/in.h>
42b1e0bd39Sthorpej #define RIPVERSION RIPv2
43b1e0bd39Sthorpej #include <protocols/routed.h>
44b1e0bd39Sthorpej #include <arpa/inet.h>
45b1e0bd39Sthorpej #include <netdb.h>
46b1e0bd39Sthorpej #include <errno.h>
47b1e0bd39Sthorpej #include <unistd.h>
48b1e0bd39Sthorpej #include <stdio.h>
49b1e0bd39Sthorpej #include <stdlib.h>
50b1e0bd39Sthorpej #include <string.h>
51b1e0bd39Sthorpej #ifdef sgi
52b1e0bd39Sthorpej #include <strings.h>
53b1e0bd39Sthorpej #include <bstring.h>
54b1e0bd39Sthorpej #endif
55b1e0bd39Sthorpej
568b0f9554Sperry #define UNUSED __unused
574fea751dSchristos #ifndef __RCSID
584fea751dSchristos #define __RCSID(_s) static const char rcsid[] UNUSED = _s
594fea751dSchristos #endif
604fea751dSchristos #ifndef __COPYRIGHT
614fea751dSchristos #define __COPYRIGHT(_s) static const char copyright[] UNUSED = _s
624fea751dSchristos #endif
636543a91fSlukem __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
646543a91fSlukem The Regents of the University of California. All rights reserved.");
654fea751dSchristos #ifdef __NetBSD__
66*4db4c628Sdholland __RCSID("$NetBSD: rtquery.c,v 1.25 2014/03/23 05:36:58 dholland Exp $");
674fea751dSchristos #elif defined(__FreeBSD__)
684fea751dSchristos __RCSID("$FreeBSD$");
694fea751dSchristos #else
70f93fe60aSchristos __RCSID("Revision: 2.26 ");
71f93fe60aSchristos #ident "Revision: 2.26 "
724fea751dSchristos #endif
73c849d97eSthorpej
74b1e0bd39Sthorpej #ifndef sgi
75b1e0bd39Sthorpej #define _HAVE_SIN_LEN
76b1e0bd39Sthorpej #endif
77b1e0bd39Sthorpej
78f93fe60aSchristos #ifdef __NetBSD__
79f93fe60aSchristos #include <md5.h>
80f93fe60aSchristos #else
81f93fe60aSchristos #define MD5_DIGEST_LEN 16
82f93fe60aSchristos typedef struct {
83f93fe60aSchristos u_int32_t state[4]; /* state (ABCD) */
84f93fe60aSchristos u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
85f93fe60aSchristos unsigned char buffer[64]; /* input buffer */
86f93fe60aSchristos } MD5_CTX;
87f93fe60aSchristos extern void MD5Init(MD5_CTX*);
88f93fe60aSchristos extern void MD5Update(MD5_CTX*, u_char*, u_int);
89f93fe60aSchristos extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
90f93fe60aSchristos #endif
91f93fe60aSchristos
92f93fe60aSchristos
93b1e0bd39Sthorpej #define WTIME 15 /* Time to wait for all responses */
94b1e0bd39Sthorpej #define STIME (250*1000) /* usec to wait for another response */
95b1e0bd39Sthorpej
96756b1291Schristos int soc;
97b1e0bd39Sthorpej
98756b1291Schristos const char *pgmname;
99b1e0bd39Sthorpej
100b1e0bd39Sthorpej union {
101b1e0bd39Sthorpej struct rip rip;
102b1e0bd39Sthorpej char packet[MAXPACKETSIZE+MAXPATHLEN];
103b1e0bd39Sthorpej } omsg_buf;
104b1e0bd39Sthorpej #define OMSG omsg_buf.rip
105b1e0bd39Sthorpej int omsg_len = sizeof(struct rip);
106b1e0bd39Sthorpej
107b1e0bd39Sthorpej union {
108b1e0bd39Sthorpej struct rip rip;
109b1e0bd39Sthorpej char packet[MAXPACKETSIZE+1024];
110b1e0bd39Sthorpej } imsg_buf;
111b1e0bd39Sthorpej #define IMSG imsg_buf.rip
112b1e0bd39Sthorpej
113b1e0bd39Sthorpej int nflag; /* numbers, no names */
114b1e0bd39Sthorpej int pflag; /* play the `gated` game */
115b1e0bd39Sthorpej int ripv2 = 1; /* use RIP version 2 */
116b1e0bd39Sthorpej int wtime = WTIME;
117b1e0bd39Sthorpej int rflag; /* 1=ask about a particular route */
118e7512e5aSchristos int trace, not_trace; /* send trace command or not */
119e7512e5aSchristos int auth_type = RIP_AUTH_NONE;
120e7512e5aSchristos char passwd[RIP_AUTH_PW_LEN];
121e7512e5aSchristos u_long keyid;
122b1e0bd39Sthorpej
123b1e0bd39Sthorpej struct timeval sent; /* when query sent */
124b1e0bd39Sthorpej
125756b1291Schristos static char localhost_str[] = "localhost";
126756b1291Schristos static char *default_argv[] = {localhost_str, 0};
1276d8ef4dfSthorpej
128b1e0bd39Sthorpej static void rip_input(struct sockaddr_in*, int);
129756b1291Schristos static int out(const char *);
1308b0f9554Sperry static void trace_loop(char *argv[]) __dead;
1318b0f9554Sperry static void query_loop(char *argv[], int) __dead;
132b1e0bd39Sthorpej static int getnet(char *, struct netinfo *);
133b1e0bd39Sthorpej static u_int std_mask(u_int);
134756b1291Schristos static int parse_quote(char **, const char *, char *, char *, int);
135baa8e84bSjoerg __dead static void usage(void);
136b1e0bd39Sthorpej
137b1e0bd39Sthorpej
1386ea8e66dSmrg int
main(int argc,char * argv[])139b1e0bd39Sthorpej main(int argc,
140b1e0bd39Sthorpej char *argv[])
141b1e0bd39Sthorpej {
142b1e0bd39Sthorpej int ch, bsize;
143e7512e5aSchristos char *p, *options, *value, delim;
144756b1291Schristos const char *result;
145b1e0bd39Sthorpej
146084c0528Smrg delim = 0; /* XXX gcc */
147084c0528Smrg
148b1e0bd39Sthorpej OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
149b1e0bd39Sthorpej OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
150b1e0bd39Sthorpej OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
151b1e0bd39Sthorpej
152b1e0bd39Sthorpej pgmname = argv[0];
1533f50343aSlukem while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
154b1e0bd39Sthorpej switch (ch) {
155b1e0bd39Sthorpej case 'n':
156b1e0bd39Sthorpej not_trace = 1;
157b1e0bd39Sthorpej nflag = 1;
158b1e0bd39Sthorpej break;
159b1e0bd39Sthorpej
160b1e0bd39Sthorpej case 'p':
161b1e0bd39Sthorpej not_trace = 1;
162b1e0bd39Sthorpej pflag = 1;
163b1e0bd39Sthorpej break;
164b1e0bd39Sthorpej
165b1e0bd39Sthorpej case '1':
166b1e0bd39Sthorpej ripv2 = 0;
167b1e0bd39Sthorpej break;
168b1e0bd39Sthorpej
169b1e0bd39Sthorpej case 'w':
170b1e0bd39Sthorpej not_trace = 1;
171b1e0bd39Sthorpej wtime = (int)strtoul(optarg, &p, 0);
172b1e0bd39Sthorpej if (*p != '\0'
173b1e0bd39Sthorpej || wtime <= 0)
17494b2d428Schristos usage();
175b1e0bd39Sthorpej break;
176b1e0bd39Sthorpej
177b1e0bd39Sthorpej case 'r':
178b1e0bd39Sthorpej not_trace = 1;
179b1e0bd39Sthorpej if (rflag)
18094b2d428Schristos usage();
181b1e0bd39Sthorpej rflag = getnet(optarg, &OMSG.rip_nets[0]);
182b1e0bd39Sthorpej if (!rflag) {
183b1e0bd39Sthorpej struct hostent *hp = gethostbyname(optarg);
184b1e0bd39Sthorpej if (hp == 0) {
185b1e0bd39Sthorpej fprintf(stderr, "%s: %s:",
186b1e0bd39Sthorpej pgmname, optarg);
187b1e0bd39Sthorpej herror(0);
188b1e0bd39Sthorpej exit(1);
189b1e0bd39Sthorpej }
19094b2d428Schristos memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
191b1e0bd39Sthorpej sizeof(OMSG.rip_nets[0].n_dst));
192b1e0bd39Sthorpej OMSG.rip_nets[0].n_family = RIP_AF_INET;
193b1e0bd39Sthorpej OMSG.rip_nets[0].n_mask = -1;
194b1e0bd39Sthorpej rflag = 1;
195b1e0bd39Sthorpej }
196b1e0bd39Sthorpej break;
197b1e0bd39Sthorpej
198b1e0bd39Sthorpej case 't':
199b1e0bd39Sthorpej trace = 1;
200b1e0bd39Sthorpej options = optarg;
201b1e0bd39Sthorpej while (*options != '\0') {
202756b1291Schristos /* messy complications to make -W -Wall happy */
203756b1291Schristos static char on_str[] = "on";
204756b1291Schristos static char more_str[] = "more";
205756b1291Schristos static char off_str[] = "off";
206756b1291Schristos static char dump_str[] = "dump";
207756b1291Schristos static char *traceopts[] = {
208b1e0bd39Sthorpej # define TRACE_ON 0
209756b1291Schristos on_str,
210b1e0bd39Sthorpej # define TRACE_MORE 1
211756b1291Schristos more_str,
212b1e0bd39Sthorpej # define TRACE_OFF 2
213756b1291Schristos off_str,
2144d3fba59Schristos # define TRACE_DUMP 3
215756b1291Schristos dump_str,
216b1e0bd39Sthorpej 0
217b1e0bd39Sthorpej };
218756b1291Schristos result = "";
219b1e0bd39Sthorpej switch (getsubopt(&options,traceopts,&value)) {
220b1e0bd39Sthorpej case TRACE_ON:
221b1e0bd39Sthorpej OMSG.rip_cmd = RIPCMD_TRACEON;
222b1e0bd39Sthorpej if (!value
223b1e0bd39Sthorpej || strlen(value) > MAXPATHLEN)
22494b2d428Schristos usage();
225756b1291Schristos result = value;
226b1e0bd39Sthorpej break;
227b1e0bd39Sthorpej case TRACE_MORE:
228b1e0bd39Sthorpej if (value)
22994b2d428Schristos usage();
230b1e0bd39Sthorpej OMSG.rip_cmd = RIPCMD_TRACEON;
231b1e0bd39Sthorpej break;
232b1e0bd39Sthorpej case TRACE_OFF:
233b1e0bd39Sthorpej if (value)
23494b2d428Schristos usage();
235b1e0bd39Sthorpej OMSG.rip_cmd = RIPCMD_TRACEOFF;
2364d3fba59Schristos break;
2374d3fba59Schristos case TRACE_DUMP:
2384d3fba59Schristos if (value)
23994b2d428Schristos usage();
2404d3fba59Schristos OMSG.rip_cmd = RIPCMD_TRACEON;
241756b1291Schristos result = "dump/../table";
242b1e0bd39Sthorpej break;
243b1e0bd39Sthorpej default:
24494b2d428Schristos usage();
245b1e0bd39Sthorpej }
246756b1291Schristos strcpy((char*)OMSG.rip_tracefile, result);
247756b1291Schristos omsg_len += strlen(result) - sizeof(OMSG.ripun);
248b1e0bd39Sthorpej }
249b1e0bd39Sthorpej break;
250b1e0bd39Sthorpej
251e7512e5aSchristos case 'a':
252e7512e5aSchristos not_trace = 1;
253e7512e5aSchristos p = strchr(optarg,'=');
254e7512e5aSchristos if (!p)
25594b2d428Schristos usage();
256e7512e5aSchristos *p++ = '\0';
257e7512e5aSchristos if (!strcasecmp("passwd",optarg))
258e7512e5aSchristos auth_type = RIP_AUTH_PW;
259e7512e5aSchristos else if (!strcasecmp("md5_passwd",optarg))
260e7512e5aSchristos auth_type = RIP_AUTH_MD5;
261e7512e5aSchristos else
26294b2d428Schristos usage();
263e7512e5aSchristos if (0 > parse_quote(&p,"|",&delim,
264e7512e5aSchristos passwd, sizeof(passwd)))
26594b2d428Schristos usage();
266e7512e5aSchristos if (auth_type == RIP_AUTH_MD5
267e7512e5aSchristos && delim == '|') {
268e7512e5aSchristos keyid = strtoul(p+1,&p,0);
269e7512e5aSchristos if (keyid > 255 || *p != '\0')
27094b2d428Schristos usage();
271e7512e5aSchristos } else if (delim != '\0') {
27294b2d428Schristos usage();
273e7512e5aSchristos }
274e7512e5aSchristos break;
275e7512e5aSchristos
276b1e0bd39Sthorpej default:
27794b2d428Schristos usage();
278b1e0bd39Sthorpej }
279b1e0bd39Sthorpej argv += optind;
280b1e0bd39Sthorpej argc -= optind;
28194b2d428Schristos if (not_trace && trace)
28294b2d428Schristos usage();
2836d8ef4dfSthorpej if (argc == 0) {
2846d8ef4dfSthorpej argc = 1;
2856d8ef4dfSthorpej argv = default_argv;
2866d8ef4dfSthorpej }
287b1e0bd39Sthorpej
288756b1291Schristos soc = socket(AF_INET, SOCK_DGRAM, 0);
289756b1291Schristos if (soc < 0) {
290b1e0bd39Sthorpej perror("socket");
291b1e0bd39Sthorpej exit(2);
292b1e0bd39Sthorpej }
293b1e0bd39Sthorpej
294b1e0bd39Sthorpej /* be prepared to receive a lot of routes */
295b1e0bd39Sthorpej for (bsize = 127*1024; ; bsize -= 1024) {
296756b1291Schristos if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
297b1e0bd39Sthorpej &bsize, sizeof(bsize)) == 0)
298b1e0bd39Sthorpej break;
299b1e0bd39Sthorpej if (bsize <= 4*1024) {
300b1e0bd39Sthorpej perror("setsockopt SO_RCVBUF");
301b1e0bd39Sthorpej break;
302b1e0bd39Sthorpej }
303b1e0bd39Sthorpej }
304b1e0bd39Sthorpej
305b1e0bd39Sthorpej if (trace)
306b1e0bd39Sthorpej trace_loop(argv);
307b1e0bd39Sthorpej else
308b1e0bd39Sthorpej query_loop(argv, argc);
309b1e0bd39Sthorpej /* NOTREACHED */
31094b2d428Schristos return 0;
31194b2d428Schristos }
31294b2d428Schristos
31394b2d428Schristos
31494b2d428Schristos static void
usage(void)31594b2d428Schristos usage(void)
31694b2d428Schristos {
31794b2d428Schristos fprintf(stderr,
31894b2d428Schristos "usage: rtquery [-np1] [-r tgt_rt] [-w wtime]"
31994b2d428Schristos " [-a type=passwd] host1 [host2 ...]\n"
32094b2d428Schristos "\trtquery -t {on=filename|more|off|dump}"
32194b2d428Schristos " host1 [host2 ...]\n");
32294b2d428Schristos exit(1);
323b1e0bd39Sthorpej }
324b1e0bd39Sthorpej
325b1e0bd39Sthorpej
326b1e0bd39Sthorpej /* tell the target hosts about tracing
327b1e0bd39Sthorpej */
328b1e0bd39Sthorpej static void
trace_loop(char * argv[])329b1e0bd39Sthorpej trace_loop(char *argv[])
330b1e0bd39Sthorpej {
331b1e0bd39Sthorpej struct sockaddr_in myaddr;
332b1e0bd39Sthorpej int res;
333b1e0bd39Sthorpej
334b1e0bd39Sthorpej if (geteuid() != 0) {
335b1e0bd39Sthorpej (void)fprintf(stderr, "-t requires UID 0\n");
336b1e0bd39Sthorpej exit(1);
337b1e0bd39Sthorpej }
338b1e0bd39Sthorpej
339b1e0bd39Sthorpej if (ripv2) {
340b1e0bd39Sthorpej OMSG.rip_vers = RIPv2;
341b1e0bd39Sthorpej } else {
342b1e0bd39Sthorpej OMSG.rip_vers = RIPv1;
343b1e0bd39Sthorpej }
344b1e0bd39Sthorpej
3453f50343aSlukem memset(&myaddr, 0, sizeof(myaddr));
346b1e0bd39Sthorpej myaddr.sin_family = AF_INET;
347b1e0bd39Sthorpej #ifdef _HAVE_SIN_LEN
348b1e0bd39Sthorpej myaddr.sin_len = sizeof(myaddr);
349b1e0bd39Sthorpej #endif
350b1e0bd39Sthorpej myaddr.sin_port = htons(IPPORT_RESERVED-1);
351756b1291Schristos while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
352b1e0bd39Sthorpej if (errno != EADDRINUSE
353b1e0bd39Sthorpej || myaddr.sin_port == 0) {
354b1e0bd39Sthorpej perror("bind");
355b1e0bd39Sthorpej exit(2);
356b1e0bd39Sthorpej }
357cad376bdSchristos myaddr.sin_port = ntohs(myaddr.sin_port)-1;
358cad376bdSchristos myaddr.sin_port = htons(myaddr.sin_port);
359b1e0bd39Sthorpej }
360b1e0bd39Sthorpej
361b1e0bd39Sthorpej res = 1;
362b1e0bd39Sthorpej while (*argv != 0) {
363b1e0bd39Sthorpej if (out(*argv++) <= 0)
364b1e0bd39Sthorpej res = 0;
365b1e0bd39Sthorpej }
366b1e0bd39Sthorpej exit(res);
367b1e0bd39Sthorpej }
368b1e0bd39Sthorpej
369b1e0bd39Sthorpej
370b1e0bd39Sthorpej /* query all of the listed hosts
371b1e0bd39Sthorpej */
372b1e0bd39Sthorpej static void
query_loop(char * argv[],int argc)373b1e0bd39Sthorpej query_loop(char *argv[], int argc)
374b1e0bd39Sthorpej {
37552ebde65Schristos struct netauth *na = OMSG.rip_auths;
37652ebde65Schristos # define NA0 (na[0])
37752ebde65Schristos # define NA2 (na[2])
378b1e0bd39Sthorpej struct seen {
379b1e0bd39Sthorpej struct seen *next;
380b1e0bd39Sthorpej struct in_addr addr;
381b1e0bd39Sthorpej } *seen, *sp;
382b1e0bd39Sthorpej int answered = 0;
383b1e0bd39Sthorpej int cc;
384b1e0bd39Sthorpej fd_set bits;
385b1e0bd39Sthorpej struct timeval now, delay;
386b1e0bd39Sthorpej struct sockaddr_in from;
387bc2e62d3Smrg socklen_t fromlen;
388e7512e5aSchristos MD5_CTX md5_ctx;
389b1e0bd39Sthorpej
390b1e0bd39Sthorpej
391b1e0bd39Sthorpej OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
392b1e0bd39Sthorpej if (ripv2) {
393b1e0bd39Sthorpej OMSG.rip_vers = RIPv2;
394e7512e5aSchristos if (auth_type == RIP_AUTH_PW) {
39552ebde65Schristos na[1] = na[0];
396e7512e5aSchristos NA0.a_family = RIP_AF_AUTH;
397e7512e5aSchristos NA0.a_type = RIP_AUTH_PW;
39894b2d428Schristos memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
399e7512e5aSchristos omsg_len += sizeof(OMSG.rip_nets[0]);
400e7512e5aSchristos
401e7512e5aSchristos } else if (auth_type == RIP_AUTH_MD5) {
40252ebde65Schristos na[1] = na[0];
403e7512e5aSchristos NA0.a_family = RIP_AF_AUTH;
404e7512e5aSchristos NA0.a_type = RIP_AUTH_MD5;
405e7512e5aSchristos NA0.au.a_md5.md5_keyid = (int8_t)keyid;
406f93fe60aSchristos NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_KEY_LEN;
407e7512e5aSchristos NA0.au.a_md5.md5_seqno = 0;
40894b2d428Schristos cc = (char *)&NA2-(char *)&OMSG;
40994b2d428Schristos NA0.au.a_md5.md5_pkt_len = htons(cc);
410e7512e5aSchristos NA2.a_family = RIP_AF_AUTH;
41194b2d428Schristos NA2.a_type = htons(1);
412e7512e5aSchristos MD5Init(&md5_ctx);
41394b2d428Schristos MD5Update(&md5_ctx,
41494b2d428Schristos (u_char *)&OMSG, cc);
41594b2d428Schristos MD5Update(&md5_ctx,
416f93fe60aSchristos (u_char *)passwd, RIP_AUTH_MD5_HASH_LEN);
417e7512e5aSchristos MD5Final(NA2.au.au_pw, &md5_ctx);
418e7512e5aSchristos omsg_len += 2*sizeof(OMSG.rip_nets[0]);
419e7512e5aSchristos }
420e7512e5aSchristos
421b1e0bd39Sthorpej } else {
422b1e0bd39Sthorpej OMSG.rip_vers = RIPv1;
423b1e0bd39Sthorpej OMSG.rip_nets[0].n_mask = 0;
424b1e0bd39Sthorpej }
425b1e0bd39Sthorpej
426b1e0bd39Sthorpej /* ask the first (valid) host */
427b1e0bd39Sthorpej seen = 0;
428b1e0bd39Sthorpej while (0 > out(*argv++)) {
429b1e0bd39Sthorpej if (*argv == 0)
43014dbdf55Swiz exit(1);
431b1e0bd39Sthorpej answered++;
432b1e0bd39Sthorpej }
433b1e0bd39Sthorpej
434b1e0bd39Sthorpej FD_ZERO(&bits);
435b1e0bd39Sthorpej for (;;) {
436756b1291Schristos FD_SET(soc, &bits);
437b1e0bd39Sthorpej delay.tv_sec = 0;
438b1e0bd39Sthorpej delay.tv_usec = STIME;
439756b1291Schristos cc = select(soc+1, &bits, 0,0, &delay);
440b1e0bd39Sthorpej if (cc > 0) {
441b1e0bd39Sthorpej fromlen = sizeof(from);
442756b1291Schristos cc = recvfrom(soc, imsg_buf.packet,
443b1e0bd39Sthorpej sizeof(imsg_buf.packet), 0,
444b1e0bd39Sthorpej (struct sockaddr *)&from, &fromlen);
445b1e0bd39Sthorpej if (cc < 0) {
446b1e0bd39Sthorpej perror("recvfrom");
447b1e0bd39Sthorpej exit(1);
448b1e0bd39Sthorpej }
449b1e0bd39Sthorpej /* count the distinct responding hosts.
450b1e0bd39Sthorpej * You cannot match responding hosts with
451b1e0bd39Sthorpej * addresses to which queries were transmitted,
452b1e0bd39Sthorpej * because a router might respond with a
453b1e0bd39Sthorpej * different source address.
454b1e0bd39Sthorpej */
455b1e0bd39Sthorpej for (sp = seen; sp != 0; sp = sp->next) {
456b1e0bd39Sthorpej if (sp->addr.s_addr == from.sin_addr.s_addr)
457b1e0bd39Sthorpej break;
458b1e0bd39Sthorpej }
459b1e0bd39Sthorpej if (sp == 0) {
460b1e0bd39Sthorpej sp = malloc(sizeof(*sp));
46194b2d428Schristos if (sp == 0) {
46294b2d428Schristos fprintf(stderr,
46394b2d428Schristos "rtquery: malloc failed\n");
46494b2d428Schristos exit(1);
46594b2d428Schristos }
466b1e0bd39Sthorpej sp->addr = from.sin_addr;
467b1e0bd39Sthorpej sp->next = seen;
468b1e0bd39Sthorpej seen = sp;
469b1e0bd39Sthorpej answered++;
470b1e0bd39Sthorpej }
471b1e0bd39Sthorpej
472b1e0bd39Sthorpej rip_input(&from, cc);
473b1e0bd39Sthorpej continue;
474b1e0bd39Sthorpej }
475b1e0bd39Sthorpej
476b1e0bd39Sthorpej if (cc < 0) {
477b1e0bd39Sthorpej if (errno == EINTR)
478b1e0bd39Sthorpej continue;
479b1e0bd39Sthorpej perror("select");
480b1e0bd39Sthorpej exit(1);
481b1e0bd39Sthorpej }
482b1e0bd39Sthorpej
483b1e0bd39Sthorpej /* After a pause in responses, probe another host.
484b1e0bd39Sthorpej * This reduces the intermingling of answers.
485b1e0bd39Sthorpej */
486b1e0bd39Sthorpej while (*argv != 0 && 0 > out(*argv++))
487b1e0bd39Sthorpej answered++;
488b1e0bd39Sthorpej
489b1e0bd39Sthorpej /* continue until no more packets arrive
490b1e0bd39Sthorpej * or we have heard from all hosts
491b1e0bd39Sthorpej */
492b1e0bd39Sthorpej if (answered >= argc)
493b1e0bd39Sthorpej break;
494b1e0bd39Sthorpej
495b1e0bd39Sthorpej /* or until we have waited a long time
496b1e0bd39Sthorpej */
497b1e0bd39Sthorpej if (gettimeofday(&now, 0) < 0) {
498b1e0bd39Sthorpej perror("gettimeofday(now)");
499b1e0bd39Sthorpej exit(1);
500b1e0bd39Sthorpej }
501b1e0bd39Sthorpej if (sent.tv_sec + wtime <= now.tv_sec)
502b1e0bd39Sthorpej break;
503b1e0bd39Sthorpej }
504b1e0bd39Sthorpej
505b1e0bd39Sthorpej /* fail if there was no answer */
506b1e0bd39Sthorpej exit (answered >= argc ? 0 : 1);
507b1e0bd39Sthorpej }
508b1e0bd39Sthorpej
509b1e0bd39Sthorpej
510e7512e5aSchristos /* send to one host
511b1e0bd39Sthorpej */
512b1e0bd39Sthorpej static int
out(const char * host)513756b1291Schristos out(const char *host)
514b1e0bd39Sthorpej {
515b1e0bd39Sthorpej struct sockaddr_in router;
516b1e0bd39Sthorpej struct hostent *hp;
517b1e0bd39Sthorpej
518b1e0bd39Sthorpej if (gettimeofday(&sent, 0) < 0) {
519b1e0bd39Sthorpej perror("gettimeofday(sent)");
520b1e0bd39Sthorpej return -1;
521b1e0bd39Sthorpej }
522b1e0bd39Sthorpej
5233f50343aSlukem memset(&router, 0, sizeof(router));
524b1e0bd39Sthorpej router.sin_family = AF_INET;
525b1e0bd39Sthorpej #ifdef _HAVE_SIN_LEN
526b1e0bd39Sthorpej router.sin_len = sizeof(router);
527b1e0bd39Sthorpej #endif
528b1e0bd39Sthorpej if (!inet_aton(host, &router.sin_addr)) {
529b1e0bd39Sthorpej hp = gethostbyname(host);
530b1e0bd39Sthorpej if (hp == 0) {
531b1e0bd39Sthorpej herror(host);
532b1e0bd39Sthorpej return -1;
533b1e0bd39Sthorpej }
53494b2d428Schristos memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
535b1e0bd39Sthorpej }
536b1e0bd39Sthorpej router.sin_port = htons(RIP_PORT);
537b1e0bd39Sthorpej
538756b1291Schristos if (sendto(soc, &omsg_buf, omsg_len, 0,
539b1e0bd39Sthorpej (struct sockaddr *)&router, sizeof(router)) < 0) {
540b1e0bd39Sthorpej perror(host);
541b1e0bd39Sthorpej return -1;
542b1e0bd39Sthorpej }
543b1e0bd39Sthorpej
544b1e0bd39Sthorpej return 0;
545b1e0bd39Sthorpej }
546b1e0bd39Sthorpej
547b1e0bd39Sthorpej
548b1e0bd39Sthorpej /*
549e7512e5aSchristos * Convert string to printable characters
550e7512e5aSchristos */
551e7512e5aSchristos static char *
qstring(u_char * s,int len)552e7512e5aSchristos qstring(u_char *s, int len)
553e7512e5aSchristos {
554e7512e5aSchristos static char buf[8*20+1];
555*4db4c628Sdholland size_t bufpos;
556e7512e5aSchristos u_char *s2, c;
557e7512e5aSchristos
558e7512e5aSchristos
559*4db4c628Sdholland for (bufpos = 0; len != 0 && bufpos < sizeof(buf) - 1; len--) {
560e7512e5aSchristos c = *s++;
561e7512e5aSchristos if (c == '\0') {
562e7512e5aSchristos for (s2 = s+1; s2 < &s[len]; s2++) {
563e7512e5aSchristos if (*s2 != '\0')
564e7512e5aSchristos break;
565e7512e5aSchristos }
566e7512e5aSchristos if (s2 >= &s[len])
567e7512e5aSchristos goto exit;
568e7512e5aSchristos }
569e7512e5aSchristos
570e7512e5aSchristos if (c >= ' ' && c < 0x7f && c != '\\') {
571*4db4c628Sdholland buf[bufpos++] = c;
572e7512e5aSchristos continue;
573e7512e5aSchristos }
574*4db4c628Sdholland if (bufpos >= sizeof(buf) - 2) {
575*4db4c628Sdholland /* too long */
576*4db4c628Sdholland break;
577*4db4c628Sdholland }
578*4db4c628Sdholland buf[bufpos++] = '\\';
579e7512e5aSchristos switch (c) {
580e7512e5aSchristos case '\\':
581*4db4c628Sdholland buf[bufpos++] = '\\';
582e7512e5aSchristos break;
583e7512e5aSchristos case '\n':
584*4db4c628Sdholland buf[bufpos++] = 'n';
585e7512e5aSchristos break;
586e7512e5aSchristos case '\r':
587*4db4c628Sdholland buf[bufpos++] = 'r';
588e7512e5aSchristos break;
589e7512e5aSchristos case '\t':
590*4db4c628Sdholland buf[bufpos++] = 't';
591e7512e5aSchristos break;
592e7512e5aSchristos case '\b':
593*4db4c628Sdholland buf[bufpos++] = 'b';
594e7512e5aSchristos break;
595e7512e5aSchristos default:
596*4db4c628Sdholland bufpos += snprintf(buf + bufpos, sizeof(buf) - bufpos,
597*4db4c628Sdholland "%o", c);
598e7512e5aSchristos break;
599e7512e5aSchristos }
600e7512e5aSchristos }
601e7512e5aSchristos exit:
602*4db4c628Sdholland buf[bufpos] = '\0';
603e7512e5aSchristos return buf;
604e7512e5aSchristos }
605e7512e5aSchristos
606e7512e5aSchristos
607e7512e5aSchristos /*
608b1e0bd39Sthorpej * Handle an incoming RIP packet.
609b1e0bd39Sthorpej */
610b1e0bd39Sthorpej static void
rip_input(struct sockaddr_in * from,int size)611b1e0bd39Sthorpej rip_input(struct sockaddr_in *from,
612b1e0bd39Sthorpej int size)
613b1e0bd39Sthorpej {
614b1e0bd39Sthorpej struct netinfo *n, *lim;
615b1e0bd39Sthorpej struct in_addr in;
616756b1291Schristos const char *name;
617b1e0bd39Sthorpej char net_buf[80];
618f93fe60aSchristos u_char hash[RIP_AUTH_MD5_KEY_LEN];
61994b2d428Schristos MD5_CTX md5_ctx;
62094b2d428Schristos u_char md5_authed = 0;
621b1e0bd39Sthorpej u_int mask, dmask;
622*4db4c628Sdholland size_t spos;
623b1e0bd39Sthorpej int i;
624b1e0bd39Sthorpej struct hostent *hp;
625b1e0bd39Sthorpej struct netent *np;
626e7512e5aSchristos struct netauth *na;
627b1e0bd39Sthorpej
628b1e0bd39Sthorpej
629b1e0bd39Sthorpej if (nflag) {
630b1e0bd39Sthorpej printf("%s:", inet_ntoa(from->sin_addr));
631b1e0bd39Sthorpej } else {
632b1e0bd39Sthorpej hp = gethostbyaddr((char*)&from->sin_addr,
633b1e0bd39Sthorpej sizeof(struct in_addr), AF_INET);
634b1e0bd39Sthorpej if (hp == 0) {
635b1e0bd39Sthorpej printf("%s:",
636b1e0bd39Sthorpej inet_ntoa(from->sin_addr));
637b1e0bd39Sthorpej } else {
638b1e0bd39Sthorpej printf("%s (%s):", hp->h_name,
639b1e0bd39Sthorpej inet_ntoa(from->sin_addr));
640b1e0bd39Sthorpej }
641b1e0bd39Sthorpej }
642b1e0bd39Sthorpej if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
643b1e0bd39Sthorpej printf("\n unexpected response type %d\n", IMSG.rip_cmd);
644b1e0bd39Sthorpej return;
645b1e0bd39Sthorpej }
646b1e0bd39Sthorpej printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
647b1e0bd39Sthorpej (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
648b1e0bd39Sthorpej size);
649b1e0bd39Sthorpej if (size > MAXPACKETSIZE) {
650756b1291Schristos if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
651b1e0bd39Sthorpej printf(" at least %d bytes too long\n",
652b1e0bd39Sthorpej size-MAXPACKETSIZE);
653756b1291Schristos size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
654b1e0bd39Sthorpej } else {
655b1e0bd39Sthorpej printf(" %d bytes too long\n",
656b1e0bd39Sthorpej size-MAXPACKETSIZE);
657b1e0bd39Sthorpej }
658b1e0bd39Sthorpej } else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
659b1e0bd39Sthorpej printf(" response of bad length=%d\n", size);
660b1e0bd39Sthorpej }
661b1e0bd39Sthorpej
662b1e0bd39Sthorpej n = IMSG.rip_nets;
663b1e0bd39Sthorpej lim = (struct netinfo *)((char*)n + size) - 1;
664b1e0bd39Sthorpej for (; n <= lim; n++) {
665b1e0bd39Sthorpej name = "";
666b1e0bd39Sthorpej if (n->n_family == RIP_AF_INET) {
667b1e0bd39Sthorpej in.s_addr = n->n_dst;
668b1e0bd39Sthorpej (void)strcpy(net_buf, inet_ntoa(in));
669b1e0bd39Sthorpej
670b1e0bd39Sthorpej mask = ntohl(n->n_mask);
671b1e0bd39Sthorpej dmask = mask & -mask;
672b1e0bd39Sthorpej if (mask != 0) {
673*4db4c628Sdholland spos = strlen(net_buf);
674b1e0bd39Sthorpej if (IMSG.rip_vers == RIPv1) {
675*4db4c628Sdholland (void)snprintf(net_buf + spos,
676*4db4c628Sdholland sizeof(net_buf) - spos,
677*4db4c628Sdholland " mask=%#x ? ", mask);
678b1e0bd39Sthorpej mask = 0;
679b1e0bd39Sthorpej } else if (mask + dmask == 0) {
680b1e0bd39Sthorpej for (i = 0;
681b1e0bd39Sthorpej (i != 32
682b1e0bd39Sthorpej && ((1<<i)&mask) == 0);
683b1e0bd39Sthorpej i++)
684b1e0bd39Sthorpej continue;
685*4db4c628Sdholland (void)snprintf(net_buf + spos,
686*4db4c628Sdholland sizeof(net_buf) - spos,
687*4db4c628Sdholland "/%d", 32-i);
688b1e0bd39Sthorpej } else {
689*4db4c628Sdholland (void)snprintf(net_buf + spos,
690*4db4c628Sdholland sizeof(net_buf) - spos,
691*4db4c628Sdholland " (mask %#x)", mask);
692b1e0bd39Sthorpej }
693b1e0bd39Sthorpej }
694b1e0bd39Sthorpej
695b1e0bd39Sthorpej if (!nflag) {
696b1e0bd39Sthorpej if (mask == 0) {
697b1e0bd39Sthorpej mask = std_mask(in.s_addr);
698b1e0bd39Sthorpej if ((ntohl(in.s_addr) & ~mask) != 0)
699b1e0bd39Sthorpej mask = 0;
700b1e0bd39Sthorpej }
701b1e0bd39Sthorpej /* Without a netmask, do not worry about
702b1e0bd39Sthorpej * whether the destination is a host or a
703b1e0bd39Sthorpej * network. Try both and use the first name
704b1e0bd39Sthorpej * we get.
705b1e0bd39Sthorpej *
706b1e0bd39Sthorpej * If we have a netmask we can make a
707b1e0bd39Sthorpej * good guess.
708b1e0bd39Sthorpej */
709b1e0bd39Sthorpej if ((in.s_addr & ~mask) == 0) {
710b1e0bd39Sthorpej np = getnetbyaddr((long)in.s_addr,
711b1e0bd39Sthorpej AF_INET);
712b1e0bd39Sthorpej if (np != 0)
713b1e0bd39Sthorpej name = np->n_name;
714b1e0bd39Sthorpej else if (in.s_addr == 0)
715b1e0bd39Sthorpej name = "default";
716b1e0bd39Sthorpej }
717b1e0bd39Sthorpej if (name[0] == '\0'
718b1e0bd39Sthorpej && ((in.s_addr & ~mask) != 0
719b1e0bd39Sthorpej || mask == 0xffffffff)) {
720b1e0bd39Sthorpej hp = gethostbyaddr((char*)&in,
721b1e0bd39Sthorpej sizeof(in),
722b1e0bd39Sthorpej AF_INET);
723b1e0bd39Sthorpej if (hp != 0)
724b1e0bd39Sthorpej name = hp->h_name;
725b1e0bd39Sthorpej }
726b1e0bd39Sthorpej }
727b1e0bd39Sthorpej
728b1e0bd39Sthorpej } else if (n->n_family == RIP_AF_AUTH) {
729e7512e5aSchristos na = (struct netauth*)n;
730e7512e5aSchristos if (na->a_type == RIP_AUTH_PW
731e7512e5aSchristos && n == IMSG.rip_nets) {
732e7512e5aSchristos (void)printf(" Password Authentication:"
733e7512e5aSchristos " \"%s\"\n",
734e7512e5aSchristos qstring(na->au.au_pw,
735e7512e5aSchristos RIP_AUTH_PW_LEN));
736e7512e5aSchristos continue;
737e7512e5aSchristos }
738e7512e5aSchristos
739e7512e5aSchristos if (na->a_type == RIP_AUTH_MD5
740e7512e5aSchristos && n == IMSG.rip_nets) {
74194b2d428Schristos (void)printf(" MD5 Auth"
742e7512e5aSchristos " len=%d KeyID=%d"
74394b2d428Schristos " auth_len=%d"
74494b2d428Schristos " seqno=%#x"
745e7512e5aSchristos " rsvd=%#x,%#x\n",
74694b2d428Schristos ntohs(na->au.a_md5.md5_pkt_len),
747e7512e5aSchristos na->au.a_md5.md5_keyid,
74894b2d428Schristos na->au.a_md5.md5_auth_len,
749e9769e05Schristos (int)ntohl(na->au.a_md5.md5_seqno),
750e7512e5aSchristos na->au.a_md5.rsvd[0],
751e7512e5aSchristos na->au.a_md5.rsvd[1]);
75294b2d428Schristos md5_authed = 1;
753e7512e5aSchristos continue;
754e7512e5aSchristos }
755e7512e5aSchristos (void)printf(" Authentication type %d: ",
756e7512e5aSchristos ntohs(na->a_type));
757756b1291Schristos for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
758e7512e5aSchristos (void)printf("%02x ", na->au.au_pw[i]);
759b1e0bd39Sthorpej putc('\n', stdout);
76094b2d428Schristos if (md5_authed && n+1 > lim
76194b2d428Schristos && na->a_type == ntohs(1)) {
76294b2d428Schristos MD5Init(&md5_ctx);
76394b2d428Schristos MD5Update(&md5_ctx, (u_char *)&IMSG,
764f93fe60aSchristos (char *)na-(char *)&IMSG
765f93fe60aSchristos +RIP_AUTH_MD5_HASH_XTRA);
76694b2d428Schristos MD5Update(&md5_ctx, (u_char *)passwd,
767f93fe60aSchristos RIP_AUTH_MD5_KEY_LEN);
76894b2d428Schristos MD5Final(hash, &md5_ctx);
76994b2d428Schristos (void)printf(" %s hash\n",
77094b2d428Schristos memcmp(hash, na->au.au_pw,
77194b2d428Schristos sizeof(hash))
77294b2d428Schristos ? "WRONG" : "correct");
77394b2d428Schristos }
774b1e0bd39Sthorpej continue;
775b1e0bd39Sthorpej
776b1e0bd39Sthorpej } else {
777*4db4c628Sdholland (void)snprintf(net_buf, sizeof(net_buf),
778*4db4c628Sdholland "(af %#x) %d.%d.%d.%d",
779b1e0bd39Sthorpej ntohs(n->n_family),
780f93fe60aSchristos (u_char)(n->n_dst >> 24),
781f93fe60aSchristos (u_char)(n->n_dst >> 16),
782f93fe60aSchristos (u_char)(n->n_dst >> 8),
783f93fe60aSchristos (u_char)n->n_dst);
784b1e0bd39Sthorpej }
785b1e0bd39Sthorpej
786b1e0bd39Sthorpej (void)printf(" %-18s metric %2d %-10s",
787e9769e05Schristos net_buf, (int)ntohl(n->n_metric), name);
788b1e0bd39Sthorpej
789b1e0bd39Sthorpej if (n->n_nhop != 0) {
790b1e0bd39Sthorpej in.s_addr = n->n_nhop;
791b1e0bd39Sthorpej if (nflag)
792b1e0bd39Sthorpej hp = 0;
793b1e0bd39Sthorpej else
794b1e0bd39Sthorpej hp = gethostbyaddr((char*)&in, sizeof(in),
795b1e0bd39Sthorpej AF_INET);
796b1e0bd39Sthorpej (void)printf(" nhop=%-15s%s",
797b1e0bd39Sthorpej (hp != 0) ? hp->h_name : inet_ntoa(in),
798b1e0bd39Sthorpej (IMSG.rip_vers == RIPv1) ? " ?" : "");
799b1e0bd39Sthorpej }
800b1e0bd39Sthorpej if (n->n_tag != 0)
801b1e0bd39Sthorpej (void)printf(" tag=%#x%s", n->n_tag,
802b1e0bd39Sthorpej (IMSG.rip_vers == RIPv1) ? " ?" : "");
803b1e0bd39Sthorpej putc('\n', stdout);
804b1e0bd39Sthorpej }
805b1e0bd39Sthorpej }
806b1e0bd39Sthorpej
807b1e0bd39Sthorpej
808b1e0bd39Sthorpej /* Return the classical netmask for an IP address.
809b1e0bd39Sthorpej */
810b1e0bd39Sthorpej static u_int
std_mask(u_int addr)811b1e0bd39Sthorpej std_mask(u_int addr) /* in network order */
812b1e0bd39Sthorpej {
813cad376bdSchristos addr = ntohl(addr); /* was a host, not a network */
814b1e0bd39Sthorpej
815b1e0bd39Sthorpej if (addr == 0) /* default route has mask 0 */
816b1e0bd39Sthorpej return 0;
817b1e0bd39Sthorpej if (IN_CLASSA(addr))
818b1e0bd39Sthorpej return IN_CLASSA_NET;
819b1e0bd39Sthorpej if (IN_CLASSB(addr))
820b1e0bd39Sthorpej return IN_CLASSB_NET;
821b1e0bd39Sthorpej return IN_CLASSC_NET;
822b1e0bd39Sthorpej }
823b1e0bd39Sthorpej
824b1e0bd39Sthorpej
825b1e0bd39Sthorpej /* get a network number as a name or a number, with an optional "/xx"
826b1e0bd39Sthorpej * netmask.
827b1e0bd39Sthorpej */
828b1e0bd39Sthorpej static int /* 0=bad */
getnet(char * name,struct netinfo * rt)829b1e0bd39Sthorpej getnet(char *name,
830b1e0bd39Sthorpej struct netinfo *rt)
831b1e0bd39Sthorpej {
832b1e0bd39Sthorpej int i;
833b1e0bd39Sthorpej struct netent *nentp;
834b1e0bd39Sthorpej u_int mask;
835b1e0bd39Sthorpej struct in_addr in;
836b1e0bd39Sthorpej char hname[MAXHOSTNAMELEN+1];
837b1e0bd39Sthorpej char *mname, *p;
838b1e0bd39Sthorpej
839b1e0bd39Sthorpej
840b1e0bd39Sthorpej /* Detect and separate "1.2.3.4/24"
841b1e0bd39Sthorpej */
8423f50343aSlukem if (0 != (mname = strrchr(name,'/'))) {
843b1e0bd39Sthorpej i = (int)(mname - name);
844756b1291Schristos if (i > (int)sizeof(hname)-1) /* name too long */
845b1e0bd39Sthorpej return 0;
8463f50343aSlukem memmove(hname, name, i);
847b1e0bd39Sthorpej hname[i] = '\0';
848b1e0bd39Sthorpej mname++;
849b1e0bd39Sthorpej name = hname;
850b1e0bd39Sthorpej }
851b1e0bd39Sthorpej
852b1e0bd39Sthorpej nentp = getnetbyname(name);
853b1e0bd39Sthorpej if (nentp != 0) {
854b1e0bd39Sthorpej in.s_addr = nentp->n_net;
855b1e0bd39Sthorpej } else if (inet_aton(name, &in) == 1) {
856cad376bdSchristos in.s_addr = ntohl(in.s_addr);
857b1e0bd39Sthorpej } else {
858b1e0bd39Sthorpej return 0;
859b1e0bd39Sthorpej }
860b1e0bd39Sthorpej
861b1e0bd39Sthorpej if (mname == 0) {
862b1e0bd39Sthorpej mask = std_mask(in.s_addr);
863b1e0bd39Sthorpej if ((~mask & in.s_addr) != 0)
864b1e0bd39Sthorpej mask = 0xffffffff;
865b1e0bd39Sthorpej } else {
866b1e0bd39Sthorpej mask = (u_int)strtoul(mname, &p, 0);
867b1e0bd39Sthorpej if (*p != '\0' || mask > 32)
868b1e0bd39Sthorpej return 0;
869b1e0bd39Sthorpej mask = 0xffffffff << (32-mask);
870b1e0bd39Sthorpej }
871b1e0bd39Sthorpej
872b1e0bd39Sthorpej rt->n_dst = htonl(in.s_addr);
873b1e0bd39Sthorpej rt->n_family = RIP_AF_INET;
874b1e0bd39Sthorpej rt->n_mask = htonl(mask);
875b1e0bd39Sthorpej return 1;
876b1e0bd39Sthorpej }
877e7512e5aSchristos
878e7512e5aSchristos
879e7512e5aSchristos /* strtok(), but honoring backslash
880e7512e5aSchristos */
881e7512e5aSchristos static int /* -1=bad */
parse_quote(char ** linep,const char * delims,char * delimp,char * buf,int lim)882e7512e5aSchristos parse_quote(char **linep,
883756b1291Schristos const char *delims,
884e7512e5aSchristos char *delimp,
885e7512e5aSchristos char *buf,
886e7512e5aSchristos int lim)
887e7512e5aSchristos {
888756b1291Schristos char c, *pc;
889756b1291Schristos const char *p;
890e7512e5aSchristos
891e7512e5aSchristos
892e7512e5aSchristos pc = *linep;
893e7512e5aSchristos if (*pc == '\0')
894e7512e5aSchristos return -1;
895e7512e5aSchristos
896e7512e5aSchristos for (;;) {
897e7512e5aSchristos if (lim == 0)
898e7512e5aSchristos return -1;
899e7512e5aSchristos c = *pc++;
900e7512e5aSchristos if (c == '\0')
901e7512e5aSchristos break;
902e7512e5aSchristos
90394b2d428Schristos if (c == '\\' && *pc != '\0') {
904e7512e5aSchristos if ((c = *pc++) == 'n') {
905e7512e5aSchristos c = '\n';
906e7512e5aSchristos } else if (c == 'r') {
907e7512e5aSchristos c = '\r';
908e7512e5aSchristos } else if (c == 't') {
909e7512e5aSchristos c = '\t';
910e7512e5aSchristos } else if (c == 'b') {
911e7512e5aSchristos c = '\b';
912e7512e5aSchristos } else if (c >= '0' && c <= '7') {
913e7512e5aSchristos c -= '0';
914e7512e5aSchristos if (*pc >= '0' && *pc <= '7') {
915e7512e5aSchristos c = (c<<3)+(*pc++ - '0');
916e7512e5aSchristos if (*pc >= '0' && *pc <= '7')
917e7512e5aSchristos c = (c<<3)+(*pc++ - '0');
918e7512e5aSchristos }
919e7512e5aSchristos }
920e7512e5aSchristos
921e7512e5aSchristos } else {
922e7512e5aSchristos for (p = delims; *p != '\0'; ++p) {
923e7512e5aSchristos if (*p == c)
924e7512e5aSchristos goto exit;
925e7512e5aSchristos }
926e7512e5aSchristos }
927e7512e5aSchristos
928e7512e5aSchristos *buf++ = c;
929e7512e5aSchristos --lim;
930e7512e5aSchristos }
931e7512e5aSchristos exit:
932e7512e5aSchristos if (delimp != 0)
933e7512e5aSchristos *delimp = c;
934e7512e5aSchristos *linep = pc-1;
935e7512e5aSchristos if (lim != 0)
936e7512e5aSchristos *buf = '\0';
937e7512e5aSchristos return 0;
938e7512e5aSchristos }
939