1996-05-30 16:19:16 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1982, 1986, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
1999-05-02 10:51:54 +00:00
|
|
|
* must display the following acknowledgment:
|
1996-05-30 16:19:16 +00:00
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1999-05-02 13:14:16 +00:00
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1996-05-30 16:19:16 +00:00
|
|
|
*/
|
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
char copyright[] =
|
1996-05-30 16:19:16 +00:00
|
|
|
"@(#) Copyright (c) 1982, 1986, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
#include <sys/cdefs.h>
|
1996-05-30 16:19:16 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#define RIPVERSION RIPv2
|
|
|
|
#include <protocols/routed.h>
|
|
|
|
#include <arpa/inet.h>
|
1998-01-14 07:17:12 +00:00
|
|
|
#include <netdb.h>
|
1996-05-30 16:19:16 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef sgi
|
|
|
|
#include <strings.h>
|
|
|
|
#include <bstring.h>
|
|
|
|
#endif
|
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
#if !defined(sgi) && !defined(__NetBSD__)
|
|
|
|
static char sccsid[] __attribute__((unused))= "@(#)query.c 8.1 (Berkeley) 6/5/93";
|
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
__RCSID("$NetBSD: rtquery.c,v 1.10 1999/02/23 10:47:41 christos Exp $");
|
|
|
|
#endif
|
1999-08-28 00:22:10 +00:00
|
|
|
#ident "$Revision: 1.12 $"
|
1999-05-02 10:51:54 +00:00
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
#ifndef sgi
|
|
|
|
#define _HAVE_SIN_LEN
|
|
|
|
#endif
|
|
|
|
|
1996-11-19 20:23:47 +00:00
|
|
|
#define MD5_DIGEST_LEN 16
|
|
|
|
typedef struct {
|
|
|
|
u_int32_t state[4]; /* state (ABCD) */
|
|
|
|
u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
|
|
|
|
unsigned char buffer[64]; /* input buffer */
|
|
|
|
} MD5_CTX;
|
|
|
|
extern void MD5Init(MD5_CTX*);
|
|
|
|
extern void MD5Update(MD5_CTX*, u_char*, u_int);
|
|
|
|
extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
|
|
|
|
|
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
#define WTIME 15 /* Time to wait for all responses */
|
|
|
|
#define STIME (250*1000) /* usec to wait for another response */
|
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
int soc;
|
1996-05-30 16:19:16 +00:00
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
const char *pgmname;
|
1996-05-30 16:19:16 +00:00
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
union {
|
|
|
|
struct rip rip;
|
|
|
|
char packet[MAXPACKETSIZE+MAXPATHLEN];
|
|
|
|
} omsg_buf;
|
|
|
|
#define OMSG omsg_buf.rip
|
|
|
|
int omsg_len = sizeof(struct rip);
|
|
|
|
|
|
|
|
union {
|
1996-05-30 16:19:16 +00:00
|
|
|
struct rip rip;
|
1996-07-22 20:56:54 +00:00
|
|
|
char packet[MAXPACKETSIZE+1024];
|
|
|
|
} imsg_buf;
|
|
|
|
#define IMSG imsg_buf.rip
|
1996-05-30 16:19:16 +00:00
|
|
|
|
|
|
|
int nflag; /* numbers, no names */
|
|
|
|
int pflag; /* play the `gated` game */
|
|
|
|
int ripv2 = 1; /* use RIP version 2 */
|
|
|
|
int wtime = WTIME;
|
|
|
|
int rflag; /* 1=ask about a particular route */
|
1996-11-19 20:23:47 +00:00
|
|
|
int trace, not_trace; /* send trace command or not */
|
|
|
|
int auth_type = RIP_AUTH_NONE;
|
|
|
|
char passwd[RIP_AUTH_PW_LEN];
|
|
|
|
u_long keyid;
|
1996-05-30 16:19:16 +00:00
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
struct timeval sent; /* when query sent */
|
1996-05-30 16:19:16 +00:00
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
static char localhost_str[] = "localhost";
|
|
|
|
static char *default_argv[] = {localhost_str, 0};
|
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
static void rip_input(struct sockaddr_in*, int);
|
1999-05-02 10:51:54 +00:00
|
|
|
static int out(const char *);
|
|
|
|
static void trace_loop(char *argv[]) __attribute((__noreturn__));
|
|
|
|
static void query_loop(char *argv[], int) __attribute((__noreturn__));
|
1996-05-30 16:19:16 +00:00
|
|
|
static int getnet(char *, struct netinfo *);
|
|
|
|
static u_int std_mask(u_int);
|
1999-05-02 10:51:54 +00:00
|
|
|
static int parse_quote(char **, const char *, char *, char *, int);
|
1998-07-28 06:36:31 +00:00
|
|
|
static void usage(void);
|
1996-05-30 16:19:16 +00:00
|
|
|
|
|
|
|
|
1998-07-28 06:36:31 +00:00
|
|
|
int
|
1996-05-30 16:19:16 +00:00
|
|
|
main(int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
1996-07-22 20:56:54 +00:00
|
|
|
int ch, bsize;
|
1996-11-19 20:23:47 +00:00
|
|
|
char *p, *options, *value, delim;
|
1999-05-02 10:51:54 +00:00
|
|
|
const char *result;
|
1996-05-30 16:19:16 +00:00
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
|
|
|
|
OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
|
|
|
|
OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
|
1996-05-30 16:19:16 +00:00
|
|
|
|
|
|
|
pgmname = argv[0];
|
1997-03-29 03:33:12 +00:00
|
|
|
while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
|
1996-05-30 16:19:16 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'n':
|
1996-07-22 20:56:54 +00:00
|
|
|
not_trace = 1;
|
1996-05-30 16:19:16 +00:00
|
|
|
nflag = 1;
|
|
|
|
break;
|
1996-07-22 20:56:54 +00:00
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
case 'p':
|
1996-07-22 20:56:54 +00:00
|
|
|
not_trace = 1;
|
1996-05-30 16:19:16 +00:00
|
|
|
pflag = 1;
|
|
|
|
break;
|
1996-07-22 20:56:54 +00:00
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
case '1':
|
|
|
|
ripv2 = 0;
|
|
|
|
break;
|
1996-07-22 20:56:54 +00:00
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
case 'w':
|
1996-07-22 20:56:54 +00:00
|
|
|
not_trace = 1;
|
1996-05-30 16:19:16 +00:00
|
|
|
wtime = (int)strtoul(optarg, &p, 0);
|
|
|
|
if (*p != '\0'
|
|
|
|
|| wtime <= 0)
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-05-30 16:19:16 +00:00
|
|
|
break;
|
1996-07-22 20:56:54 +00:00
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
case 'r':
|
1996-07-22 20:56:54 +00:00
|
|
|
not_trace = 1;
|
1996-05-30 16:19:16 +00:00
|
|
|
if (rflag)
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-07-22 20:56:54 +00:00
|
|
|
rflag = getnet(optarg, &OMSG.rip_nets[0]);
|
|
|
|
if (!rflag) {
|
|
|
|
struct hostent *hp = gethostbyname(optarg);
|
|
|
|
if (hp == 0) {
|
|
|
|
fprintf(stderr, "%s: %s:",
|
|
|
|
pgmname, optarg);
|
|
|
|
herror(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-05-02 10:51:54 +00:00
|
|
|
memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
|
|
|
|
sizeof(OMSG.rip_nets[0].n_dst));
|
1996-08-27 16:13:00 +00:00
|
|
|
OMSG.rip_nets[0].n_family = RIP_AF_INET;
|
1996-07-22 20:56:54 +00:00
|
|
|
OMSG.rip_nets[0].n_mask = -1;
|
|
|
|
rflag = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
trace = 1;
|
|
|
|
options = optarg;
|
|
|
|
while (*options != '\0') {
|
1999-05-02 10:51:54 +00:00
|
|
|
/* messy complications to make -W -Wall happy */
|
|
|
|
static char on_str[] = "on";
|
|
|
|
static char more_str[] = "more";
|
|
|
|
static char off_str[] = "off";
|
|
|
|
static char dump_str[] = "dump";
|
|
|
|
static char *traceopts[] = {
|
1996-07-22 20:56:54 +00:00
|
|
|
# define TRACE_ON 0
|
1999-05-02 10:51:54 +00:00
|
|
|
on_str,
|
1996-07-22 20:56:54 +00:00
|
|
|
# define TRACE_MORE 1
|
1999-05-02 10:51:54 +00:00
|
|
|
more_str,
|
1996-07-22 20:56:54 +00:00
|
|
|
# define TRACE_OFF 2
|
1999-05-02 10:51:54 +00:00
|
|
|
off_str,
|
1996-09-16 16:51:32 +00:00
|
|
|
# define TRACE_DUMP 3
|
1999-05-02 10:51:54 +00:00
|
|
|
dump_str,
|
1996-07-22 20:56:54 +00:00
|
|
|
0
|
|
|
|
};
|
1999-05-02 10:51:54 +00:00
|
|
|
result = "";
|
1996-07-22 20:56:54 +00:00
|
|
|
switch (getsubopt(&options,traceopts,&value)) {
|
|
|
|
case TRACE_ON:
|
|
|
|
OMSG.rip_cmd = RIPCMD_TRACEON;
|
|
|
|
if (!value
|
|
|
|
|| strlen(value) > MAXPATHLEN)
|
1999-05-02 10:51:54 +00:00
|
|
|
usage();
|
|
|
|
result = value;
|
1996-07-22 20:56:54 +00:00
|
|
|
break;
|
|
|
|
case TRACE_MORE:
|
|
|
|
if (value)
|
1999-05-02 10:51:54 +00:00
|
|
|
usage();
|
1996-07-22 20:56:54 +00:00
|
|
|
OMSG.rip_cmd = RIPCMD_TRACEON;
|
|
|
|
break;
|
|
|
|
case TRACE_OFF:
|
|
|
|
if (value)
|
1999-05-02 10:51:54 +00:00
|
|
|
usage();
|
1996-07-22 20:56:54 +00:00
|
|
|
OMSG.rip_cmd = RIPCMD_TRACEOFF;
|
1996-09-16 16:51:32 +00:00
|
|
|
break;
|
|
|
|
case TRACE_DUMP:
|
|
|
|
if (value)
|
1999-05-02 10:51:54 +00:00
|
|
|
usage();
|
1996-09-16 16:51:32 +00:00
|
|
|
OMSG.rip_cmd = RIPCMD_TRACEON;
|
1999-05-02 10:51:54 +00:00
|
|
|
result = "dump/../table";
|
1996-07-22 20:56:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-07-22 20:56:54 +00:00
|
|
|
}
|
1999-05-02 10:51:54 +00:00
|
|
|
strcpy((char*)OMSG.rip_tracefile, result);
|
|
|
|
omsg_len += strlen(result) - sizeof(OMSG.ripun);
|
1996-07-22 20:56:54 +00:00
|
|
|
}
|
1996-05-30 16:19:16 +00:00
|
|
|
break;
|
1996-07-22 20:56:54 +00:00
|
|
|
|
1996-11-19 20:23:47 +00:00
|
|
|
case 'a':
|
|
|
|
not_trace = 1;
|
|
|
|
p = strchr(optarg,'=');
|
|
|
|
if (!p)
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-11-19 20:23:47 +00:00
|
|
|
*p++ = '\0';
|
|
|
|
if (!strcasecmp("passwd",optarg))
|
|
|
|
auth_type = RIP_AUTH_PW;
|
|
|
|
else if (!strcasecmp("md5_passwd",optarg))
|
|
|
|
auth_type = RIP_AUTH_MD5;
|
|
|
|
else
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-11-19 20:23:47 +00:00
|
|
|
if (0 > parse_quote(&p,"|",&delim,
|
1999-05-02 10:51:54 +00:00
|
|
|
passwd, sizeof(passwd)))
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-11-19 20:23:47 +00:00
|
|
|
if (auth_type == RIP_AUTH_MD5
|
|
|
|
&& delim == '|') {
|
|
|
|
keyid = strtoul(p+1,&p,0);
|
|
|
|
if (keyid > 255 || *p != '\0')
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-11-19 20:23:47 +00:00
|
|
|
} else if (delim != '\0') {
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-11-19 20:23:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
default:
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
1999-05-02 10:51:54 +00:00
|
|
|
if (not_trace && trace)
|
1998-07-28 06:36:31 +00:00
|
|
|
usage();
|
1999-05-02 10:51:54 +00:00
|
|
|
if (argc == 0) {
|
|
|
|
argc = 1;
|
|
|
|
argv = default_argv;
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
soc = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (soc < 0) {
|
1999-05-05 20:28:54 +00:00
|
|
|
perror("socket");
|
|
|
|
exit(2);
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
1996-07-22 20:56:54 +00:00
|
|
|
|
|
|
|
/* be prepared to receive a lot of routes */
|
1996-05-30 16:19:16 +00:00
|
|
|
for (bsize = 127*1024; ; bsize -= 1024) {
|
1999-05-02 10:51:54 +00:00
|
|
|
if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
|
1996-05-30 16:19:16 +00:00
|
|
|
&bsize, sizeof(bsize)) == 0)
|
|
|
|
break;
|
|
|
|
if (bsize <= 4*1024) {
|
1999-05-05 20:28:54 +00:00
|
|
|
perror("setsockopt SO_RCVBUF");
|
1996-05-30 16:19:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
if (trace)
|
|
|
|
trace_loop(argv);
|
|
|
|
else
|
|
|
|
query_loop(argv, argc);
|
|
|
|
/* NOTREACHED */
|
1999-05-02 10:51:54 +00:00
|
|
|
return 0;
|
1996-07-22 20:56:54 +00:00
|
|
|
}
|
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
|
1998-07-28 06:36:31 +00:00
|
|
|
static void
|
1999-05-02 10:51:54 +00:00
|
|
|
usage(void)
|
1998-07-28 06:36:31 +00:00
|
|
|
{
|
1999-05-02 10:51:54 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"usage: rtquery [-np1] [-r tgt_rt] [-w wtime]"
|
|
|
|
" [-a type=passwd] host1 [host2 ...]\n"
|
|
|
|
"\trtquery -t {on=filename|more|off|dump}"
|
|
|
|
" host1 [host2 ...]\n");
|
1998-07-28 06:36:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-07-22 20:56:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* tell the target hosts about tracing
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
trace_loop(char *argv[])
|
|
|
|
{
|
|
|
|
struct sockaddr_in myaddr;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (geteuid() != 0) {
|
|
|
|
(void)fprintf(stderr, "-t requires UID 0\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ripv2) {
|
|
|
|
OMSG.rip_vers = RIPv2;
|
|
|
|
} else {
|
|
|
|
OMSG.rip_vers = RIPv1;
|
|
|
|
}
|
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
memset(&myaddr, 0, sizeof(myaddr));
|
1996-07-22 20:56:54 +00:00
|
|
|
myaddr.sin_family = AF_INET;
|
|
|
|
#ifdef _HAVE_SIN_LEN
|
|
|
|
myaddr.sin_len = sizeof(myaddr);
|
|
|
|
#endif
|
|
|
|
myaddr.sin_port = htons(IPPORT_RESERVED-1);
|
1999-05-02 10:51:54 +00:00
|
|
|
while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
|
1996-07-22 20:56:54 +00:00
|
|
|
if (errno != EADDRINUSE
|
|
|
|
|| myaddr.sin_port == 0) {
|
1999-05-05 20:28:54 +00:00
|
|
|
perror("bind");
|
|
|
|
exit(2);
|
1996-07-22 20:56:54 +00:00
|
|
|
}
|
|
|
|
myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = 1;
|
|
|
|
while (*argv != 0) {
|
|
|
|
if (out(*argv++) <= 0)
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
exit(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* query all of the listed hosts
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
query_loop(char *argv[], int argc)
|
|
|
|
{
|
1996-11-19 20:23:47 +00:00
|
|
|
# define NA0 (OMSG.rip_auths[0])
|
|
|
|
# define NA2 (OMSG.rip_auths[2])
|
1996-07-22 20:56:54 +00:00
|
|
|
struct seen {
|
|
|
|
struct seen *next;
|
|
|
|
struct in_addr addr;
|
|
|
|
} *seen, *sp;
|
|
|
|
int answered = 0;
|
|
|
|
int cc;
|
|
|
|
fd_set bits;
|
|
|
|
struct timeval now, delay;
|
|
|
|
struct sockaddr_in from;
|
|
|
|
int fromlen;
|
1996-11-19 20:23:47 +00:00
|
|
|
MD5_CTX md5_ctx;
|
1996-07-22 20:56:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
|
|
|
|
if (ripv2) {
|
|
|
|
OMSG.rip_vers = RIPv2;
|
1996-11-19 20:23:47 +00:00
|
|
|
if (auth_type == RIP_AUTH_PW) {
|
|
|
|
OMSG.rip_nets[1] = OMSG.rip_nets[0];
|
|
|
|
NA0.a_family = RIP_AF_AUTH;
|
|
|
|
NA0.a_type = RIP_AUTH_PW;
|
1999-05-02 10:51:54 +00:00
|
|
|
memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
|
1996-11-19 20:23:47 +00:00
|
|
|
omsg_len += sizeof(OMSG.rip_nets[0]);
|
|
|
|
|
|
|
|
} else if (auth_type == RIP_AUTH_MD5) {
|
|
|
|
OMSG.rip_nets[1] = OMSG.rip_nets[0];
|
|
|
|
NA0.a_family = RIP_AF_AUTH;
|
|
|
|
NA0.a_type = RIP_AUTH_MD5;
|
|
|
|
NA0.au.a_md5.md5_keyid = (int8_t)keyid;
|
1999-05-02 10:51:54 +00:00
|
|
|
NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
|
1996-11-19 20:23:47 +00:00
|
|
|
NA0.au.a_md5.md5_seqno = 0;
|
1999-05-02 10:51:54 +00:00
|
|
|
cc = (char *)&NA2-(char *)&OMSG;
|
|
|
|
NA0.au.a_md5.md5_pkt_len = htons(cc);
|
1996-11-19 20:23:47 +00:00
|
|
|
NA2.a_family = RIP_AF_AUTH;
|
1999-05-02 10:51:54 +00:00
|
|
|
NA2.a_type = htons(1);
|
1996-11-19 20:23:47 +00:00
|
|
|
MD5Init(&md5_ctx);
|
1999-05-02 10:51:54 +00:00
|
|
|
MD5Update(&md5_ctx,
|
|
|
|
(u_char *)&OMSG, cc);
|
|
|
|
MD5Update(&md5_ctx,
|
|
|
|
(u_char *)passwd, RIP_AUTH_MD5_LEN);
|
1996-11-19 20:23:47 +00:00
|
|
|
MD5Final(NA2.au.au_pw, &md5_ctx);
|
|
|
|
omsg_len += 2*sizeof(OMSG.rip_nets[0]);
|
|
|
|
}
|
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
} else {
|
|
|
|
OMSG.rip_vers = RIPv1;
|
|
|
|
OMSG.rip_nets[0].n_mask = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ask the first (valid) host */
|
1996-05-30 16:19:16 +00:00
|
|
|
seen = 0;
|
1996-07-22 20:56:54 +00:00
|
|
|
while (0 > out(*argv++)) {
|
|
|
|
if (*argv == 0)
|
|
|
|
exit(-1);
|
1996-05-30 16:19:16 +00:00
|
|
|
answered++;
|
1996-07-22 20:56:54 +00:00
|
|
|
}
|
1996-05-30 16:19:16 +00:00
|
|
|
|
|
|
|
FD_ZERO(&bits);
|
|
|
|
for (;;) {
|
1999-05-02 10:51:54 +00:00
|
|
|
FD_SET(soc, &bits);
|
1996-05-30 16:19:16 +00:00
|
|
|
delay.tv_sec = 0;
|
|
|
|
delay.tv_usec = STIME;
|
1999-05-02 10:51:54 +00:00
|
|
|
cc = select(soc+1, &bits, 0,0, &delay);
|
1996-05-30 16:19:16 +00:00
|
|
|
if (cc > 0) {
|
|
|
|
fromlen = sizeof(from);
|
1999-05-02 10:51:54 +00:00
|
|
|
cc = recvfrom(soc, imsg_buf.packet,
|
1996-07-22 20:56:54 +00:00
|
|
|
sizeof(imsg_buf.packet), 0,
|
1996-05-30 16:19:16 +00:00
|
|
|
(struct sockaddr *)&from, &fromlen);
|
|
|
|
if (cc < 0) {
|
1999-05-05 20:28:54 +00:00
|
|
|
perror("recvfrom");
|
|
|
|
exit(1);
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
|
|
|
/* count the distinct responding hosts.
|
|
|
|
* You cannot match responding hosts with
|
|
|
|
* addresses to which queries were transmitted,
|
|
|
|
* because a router might respond with a
|
|
|
|
* different source address.
|
|
|
|
*/
|
|
|
|
for (sp = seen; sp != 0; sp = sp->next) {
|
|
|
|
if (sp->addr.s_addr == from.sin_addr.s_addr)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sp == 0) {
|
|
|
|
sp = malloc(sizeof(*sp));
|
1999-05-02 10:51:54 +00:00
|
|
|
if (sp == 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"rtquery: malloc failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
1996-05-30 16:19:16 +00:00
|
|
|
sp->addr = from.sin_addr;
|
|
|
|
sp->next = seen;
|
|
|
|
seen = sp;
|
|
|
|
answered++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rip_input(&from, cc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cc < 0) {
|
1998-01-14 07:17:12 +00:00
|
|
|
if (errno == EINTR)
|
1996-05-30 16:19:16 +00:00
|
|
|
continue;
|
1999-05-05 20:28:54 +00:00
|
|
|
perror("select");
|
|
|
|
exit(1);
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* After a pause in responses, probe another host.
|
|
|
|
* This reduces the intermingling of answers.
|
|
|
|
*/
|
1996-07-22 20:56:54 +00:00
|
|
|
while (*argv != 0 && 0 > out(*argv++))
|
1996-05-30 16:19:16 +00:00
|
|
|
answered++;
|
|
|
|
|
|
|
|
/* continue until no more packets arrive
|
|
|
|
* or we have heard from all hosts
|
|
|
|
*/
|
|
|
|
if (answered >= argc)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* or until we have waited a long time
|
|
|
|
*/
|
|
|
|
if (gettimeofday(&now, 0) < 0) {
|
1999-05-05 20:28:54 +00:00
|
|
|
perror("gettimeofday(now)");
|
|
|
|
exit(1);
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
1996-07-22 20:56:54 +00:00
|
|
|
if (sent.tv_sec + wtime <= now.tv_sec)
|
1996-05-30 16:19:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fail if there was no answer */
|
|
|
|
exit (answered >= argc ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-11-19 20:23:47 +00:00
|
|
|
/* send to one host
|
1996-05-30 16:19:16 +00:00
|
|
|
*/
|
|
|
|
static int
|
1999-05-02 10:51:54 +00:00
|
|
|
out(const char *host)
|
1996-05-30 16:19:16 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in router;
|
|
|
|
struct hostent *hp;
|
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
if (gettimeofday(&sent, 0) < 0) {
|
1999-05-05 20:28:54 +00:00
|
|
|
perror("gettimeofday(sent)");
|
|
|
|
return -1;
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
memset(&router, 0, sizeof(router));
|
1996-05-30 16:19:16 +00:00
|
|
|
router.sin_family = AF_INET;
|
|
|
|
#ifdef _HAVE_SIN_LEN
|
|
|
|
router.sin_len = sizeof(router);
|
|
|
|
#endif
|
1996-07-22 20:56:54 +00:00
|
|
|
if (!inet_aton(host, &router.sin_addr)) {
|
1996-05-30 16:19:16 +00:00
|
|
|
hp = gethostbyname(host);
|
|
|
|
if (hp == 0) {
|
1996-07-22 20:56:54 +00:00
|
|
|
herror(host);
|
1996-05-30 16:19:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1999-05-02 10:51:54 +00:00
|
|
|
memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
|
|
|
router.sin_port = htons(RIP_PORT);
|
|
|
|
|
1999-05-02 10:51:54 +00:00
|
|
|
if (sendto(soc, &omsg_buf, omsg_len, 0,
|
1996-05-30 16:19:16 +00:00
|
|
|
(struct sockaddr *)&router, sizeof(router)) < 0) {
|
1999-05-05 20:28:54 +00:00
|
|
|
perror(host);
|
|
|
|
return -1;
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-11-19 20:23:47 +00:00
|
|
|
/*
|
|
|
|
* Convert string to printable characters
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
qstring(u_char *s, int len)
|
|
|
|
{
|
|
|
|
static char buf[8*20+1];
|
|
|
|
char *p;
|
|
|
|
u_char *s2, c;
|
|
|
|
|
|
|
|
|
|
|
|
for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
|
|
|
|
c = *s++;
|
|
|
|
if (c == '\0') {
|
|
|
|
for (s2 = s+1; s2 < &s[len]; s2++) {
|
|
|
|
if (*s2 != '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (s2 >= &s[len])
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c >= ' ' && c < 0x7f && c != '\\') {
|
|
|
|
*p++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*p++ = '\\';
|
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
*p++ = '\\';
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
*p++= 'n';
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
*p++= 'r';
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
*p++ = 't';
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
*p++ = 'b';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p += sprintf(p,"%o",c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
*p = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-05-30 16:19:16 +00:00
|
|
|
/*
|
|
|
|
* Handle an incoming RIP packet.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
rip_input(struct sockaddr_in *from,
|
|
|
|
int size)
|
|
|
|
{
|
|
|
|
struct netinfo *n, *lim;
|
|
|
|
struct in_addr in;
|
1999-05-02 10:51:54 +00:00
|
|
|
const char *name;
|
1996-05-30 16:19:16 +00:00
|
|
|
char net_buf[80];
|
1999-05-02 10:51:54 +00:00
|
|
|
u_char hash[RIP_AUTH_MD5_LEN];
|
|
|
|
MD5_CTX md5_ctx;
|
|
|
|
u_char md5_authed = 0;
|
1996-05-30 16:19:16 +00:00
|
|
|
u_int mask, dmask;
|
|
|
|
char *sp;
|
|
|
|
int i;
|
|
|
|
struct hostent *hp;
|
|
|
|
struct netent *np;
|
1996-11-19 20:23:47 +00:00
|
|
|
struct netauth *na;
|
1996-05-30 16:19:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (nflag) {
|
|
|
|
printf("%s:", inet_ntoa(from->sin_addr));
|
|
|
|
} else {
|
|
|
|
hp = gethostbyaddr((char*)&from->sin_addr,
|
|
|
|
sizeof(struct in_addr), AF_INET);
|
|
|
|
if (hp == 0) {
|
|
|
|
printf("%s:",
|
|
|
|
inet_ntoa(from->sin_addr));
|
|
|
|
} else {
|
|
|
|
printf("%s (%s):", hp->h_name,
|
|
|
|
inet_ntoa(from->sin_addr));
|
|
|
|
}
|
|
|
|
}
|
1996-07-22 20:56:54 +00:00
|
|
|
if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
|
|
|
|
printf("\n unexpected response type %d\n", IMSG.rip_cmd);
|
1996-05-30 16:19:16 +00:00
|
|
|
return;
|
|
|
|
}
|
1996-07-22 20:56:54 +00:00
|
|
|
printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
|
|
|
|
(IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
|
1996-05-30 16:19:16 +00:00
|
|
|
size);
|
|
|
|
if (size > MAXPACKETSIZE) {
|
1999-05-02 10:51:54 +00:00
|
|
|
if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
|
1996-05-30 16:19:16 +00:00
|
|
|
printf(" at least %d bytes too long\n",
|
|
|
|
size-MAXPACKETSIZE);
|
1999-05-02 10:51:54 +00:00
|
|
|
size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
|
1996-05-30 16:19:16 +00:00
|
|
|
} else {
|
|
|
|
printf(" %d bytes too long\n",
|
|
|
|
size-MAXPACKETSIZE);
|
|
|
|
}
|
|
|
|
} else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
|
|
|
|
printf(" response of bad length=%d\n", size);
|
|
|
|
}
|
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
n = IMSG.rip_nets;
|
1996-05-30 16:19:16 +00:00
|
|
|
lim = (struct netinfo *)((char*)n + size) - 1;
|
|
|
|
for (; n <= lim; n++) {
|
|
|
|
name = "";
|
|
|
|
if (n->n_family == RIP_AF_INET) {
|
|
|
|
in.s_addr = n->n_dst;
|
|
|
|
(void)strcpy(net_buf, inet_ntoa(in));
|
|
|
|
|
|
|
|
mask = ntohl(n->n_mask);
|
|
|
|
dmask = mask & -mask;
|
|
|
|
if (mask != 0) {
|
|
|
|
sp = &net_buf[strlen(net_buf)];
|
1996-07-22 20:56:54 +00:00
|
|
|
if (IMSG.rip_vers == RIPv1) {
|
1996-05-30 16:19:16 +00:00
|
|
|
(void)sprintf(sp," mask=%#x ? ",mask);
|
|
|
|
mask = 0;
|
|
|
|
} else if (mask + dmask == 0) {
|
|
|
|
for (i = 0;
|
|
|
|
(i != 32
|
|
|
|
&& ((1<<i)&mask) == 0);
|
|
|
|
i++)
|
|
|
|
continue;
|
|
|
|
(void)sprintf(sp, "/%d",32-i);
|
|
|
|
} else {
|
|
|
|
(void)sprintf(sp," (mask %#x)", mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nflag) {
|
|
|
|
if (mask == 0) {
|
|
|
|
mask = std_mask(in.s_addr);
|
|
|
|
if ((ntohl(in.s_addr) & ~mask) != 0)
|
|
|
|
mask = 0;
|
|
|
|
}
|
|
|
|
/* Without a netmask, do not worry about
|
|
|
|
* whether the destination is a host or a
|
|
|
|
* network. Try both and use the first name
|
|
|
|
* we get.
|
|
|
|
*
|
|
|
|
* If we have a netmask we can make a
|
|
|
|
* good guess.
|
|
|
|
*/
|
|
|
|
if ((in.s_addr & ~mask) == 0) {
|
1996-07-22 20:56:54 +00:00
|
|
|
np = getnetbyaddr((long)in.s_addr,
|
|
|
|
AF_INET);
|
1996-05-30 16:19:16 +00:00
|
|
|
if (np != 0)
|
|
|
|
name = np->n_name;
|
|
|
|
else if (in.s_addr == 0)
|
|
|
|
name = "default";
|
|
|
|
}
|
|
|
|
if (name[0] == '\0'
|
1996-07-22 20:56:54 +00:00
|
|
|
&& ((in.s_addr & ~mask) != 0
|
|
|
|
|| mask == 0xffffffff)) {
|
1996-05-30 16:19:16 +00:00
|
|
|
hp = gethostbyaddr((char*)&in,
|
|
|
|
sizeof(in),
|
|
|
|
AF_INET);
|
|
|
|
if (hp != 0)
|
|
|
|
name = hp->h_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (n->n_family == RIP_AF_AUTH) {
|
1996-11-19 20:23:47 +00:00
|
|
|
na = (struct netauth*)n;
|
|
|
|
if (na->a_type == RIP_AUTH_PW
|
|
|
|
&& n == IMSG.rip_nets) {
|
|
|
|
(void)printf(" Password Authentication:"
|
|
|
|
" \"%s\"\n",
|
|
|
|
qstring(na->au.au_pw,
|
|
|
|
RIP_AUTH_PW_LEN));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (na->a_type == RIP_AUTH_MD5
|
|
|
|
&& n == IMSG.rip_nets) {
|
1999-05-02 10:51:54 +00:00
|
|
|
(void)printf(" MD5 Auth"
|
1996-11-19 20:23:47 +00:00
|
|
|
" len=%d KeyID=%d"
|
1999-05-02 10:51:54 +00:00
|
|
|
" auth_len=%d"
|
|
|
|
" seqno=%#x"
|
1996-11-19 20:23:47 +00:00
|
|
|
" rsvd=%#x,%#x\n",
|
1999-05-02 10:51:54 +00:00
|
|
|
ntohs(na->au.a_md5.md5_pkt_len),
|
1996-11-19 20:23:47 +00:00
|
|
|
na->au.a_md5.md5_keyid,
|
1999-05-02 10:51:54 +00:00
|
|
|
na->au.a_md5.md5_auth_len,
|
|
|
|
(int)ntohl(na->au.a_md5.md5_seqno),
|
1996-11-19 20:23:47 +00:00
|
|
|
na->au.a_md5.rsvd[0],
|
|
|
|
na->au.a_md5.rsvd[1]);
|
1999-05-02 10:51:54 +00:00
|
|
|
md5_authed = 1;
|
1996-11-19 20:23:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void)printf(" Authentication type %d: ",
|
|
|
|
ntohs(na->a_type));
|
1999-05-02 10:51:54 +00:00
|
|
|
for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
|
1996-11-19 20:23:47 +00:00
|
|
|
(void)printf("%02x ", na->au.au_pw[i]);
|
1996-05-30 16:19:16 +00:00
|
|
|
putc('\n', stdout);
|
1999-05-02 10:51:54 +00:00
|
|
|
if (md5_authed && n+1 > lim
|
|
|
|
&& na->a_type == ntohs(1)) {
|
|
|
|
MD5Init(&md5_ctx);
|
|
|
|
MD5Update(&md5_ctx, (u_char *)&IMSG,
|
|
|
|
(char *)na-(char *)&IMSG);
|
|
|
|
MD5Update(&md5_ctx, (u_char *)passwd,
|
|
|
|
RIP_AUTH_MD5_LEN);
|
|
|
|
MD5Final(hash, &md5_ctx);
|
|
|
|
(void)printf(" %s hash\n",
|
|
|
|
memcmp(hash, na->au.au_pw,
|
|
|
|
sizeof(hash))
|
|
|
|
? "WRONG" : "correct");
|
|
|
|
}
|
1996-05-30 16:19:16 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
(void)sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
|
1996-08-27 16:13:00 +00:00
|
|
|
ntohs(n->n_family),
|
1996-05-30 16:19:16 +00:00
|
|
|
(char)(n->n_dst >> 24),
|
|
|
|
(char)(n->n_dst >> 16),
|
|
|
|
(char)(n->n_dst >> 8),
|
|
|
|
(char)n->n_dst);
|
|
|
|
}
|
|
|
|
|
1996-07-22 20:56:54 +00:00
|
|
|
(void)printf(" %-18s metric %2d %-10s",
|
1999-05-02 10:51:54 +00:00
|
|
|
net_buf, (int)ntohl(n->n_metric), name);
|
1996-05-30 16:19:16 +00:00
|
|
|
|
|
|
|
if (n->n_nhop != 0) {
|
|
|
|
in.s_addr = n->n_nhop;
|
|
|
|
if (nflag)
|
|
|
|
hp = 0;
|
|
|
|
else
|
|
|
|
hp = gethostbyaddr((char*)&in, sizeof(in),
|
|
|
|
AF_INET);
|
1996-07-22 20:56:54 +00:00
|
|
|
(void)printf(" nhop=%-15s%s",
|
1996-05-30 16:19:16 +00:00
|
|
|
(hp != 0) ? hp->h_name : inet_ntoa(in),
|
1996-07-22 20:56:54 +00:00
|
|
|
(IMSG.rip_vers == RIPv1) ? " ?" : "");
|
1996-05-30 16:19:16 +00:00
|
|
|
}
|
|
|
|
if (n->n_tag != 0)
|
|
|
|
(void)printf(" tag=%#x%s", n->n_tag,
|
1996-07-22 20:56:54 +00:00
|
|
|
(IMSG.rip_vers == RIPv1) ? " ?" : "");
|
1996-05-30 16:19:16 +00:00
|
|
|
putc('\n', stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the classical netmask for an IP address.
|
|
|
|
*/
|
|
|
|
static u_int
|
1996-08-27 16:13:00 +00:00
|
|
|
std_mask(u_int addr) /* in network order */
|
1996-05-30 16:19:16 +00:00
|
|
|
{
|
1996-08-27 16:13:00 +00:00
|
|
|
NTOHL(addr); /* was a host, not a network */
|
1996-05-30 16:19:16 +00:00
|
|
|
|
1996-08-27 16:13:00 +00:00
|
|
|
if (addr == 0) /* default route has mask 0 */
|
1996-05-30 16:19:16 +00:00
|
|
|
return 0;
|
|
|
|
if (IN_CLASSA(addr))
|
|
|
|
return IN_CLASSA_NET;
|
|
|
|
if (IN_CLASSB(addr))
|
|
|
|
return IN_CLASSB_NET;
|
|
|
|
return IN_CLASSC_NET;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get a network number as a name or a number, with an optional "/xx"
|
|
|
|
* netmask.
|
|
|
|
*/
|
|
|
|
static int /* 0=bad */
|
|
|
|
getnet(char *name,
|
|
|
|
struct netinfo *rt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct netent *nentp;
|
|
|
|
u_int mask;
|
|
|
|
struct in_addr in;
|
|
|
|
char hname[MAXHOSTNAMELEN+1];
|
|
|
|
char *mname, *p;
|
|
|
|
|
|
|
|
|
|
|
|
/* Detect and separate "1.2.3.4/24"
|
|
|
|
*/
|
1999-05-02 10:51:54 +00:00
|
|
|
if (0 != (mname = strrchr(name,'/'))) {
|
1996-05-30 16:19:16 +00:00
|
|
|
i = (int)(mname - name);
|
1999-05-02 10:51:54 +00:00
|
|
|
if (i > (int)sizeof(hname)-1) /* name too long */
|
1996-05-30 16:19:16 +00:00
|
|
|
return 0;
|
1999-05-02 10:51:54 +00:00
|
|
|
memmove(hname, name, i);
|
1996-05-30 16:19:16 +00:00
|
|
|
hname[i] = '\0';
|
|
|
|
mname++;
|
|
|
|
name = hname;
|
|
|
|
}
|
|
|
|
|
|
|
|
nentp = getnetbyname(name);
|
|
|
|
if (nentp != 0) {
|
|
|
|
in.s_addr = nentp->n_net;
|
|
|
|
} else if (inet_aton(name, &in) == 1) {
|
|
|
|
NTOHL(in.s_addr);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mname == 0) {
|
|
|
|
mask = std_mask(in.s_addr);
|
1996-08-27 16:13:00 +00:00
|
|
|
if ((~mask & in.s_addr) != 0)
|
|
|
|
mask = 0xffffffff;
|
1996-05-30 16:19:16 +00:00
|
|
|
} else {
|
|
|
|
mask = (u_int)strtoul(mname, &p, 0);
|
|
|
|
if (*p != '\0' || mask > 32)
|
|
|
|
return 0;
|
|
|
|
mask = 0xffffffff << (32-mask);
|
|
|
|
}
|
|
|
|
|
1996-08-27 16:13:00 +00:00
|
|
|
rt->n_dst = htonl(in.s_addr);
|
|
|
|
rt->n_family = RIP_AF_INET;
|
1996-05-30 16:19:16 +00:00
|
|
|
rt->n_mask = htonl(mask);
|
|
|
|
return 1;
|
|
|
|
}
|
1996-11-19 20:23:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* strtok(), but honoring backslash
|
|
|
|
*/
|
|
|
|
static int /* -1=bad */
|
|
|
|
parse_quote(char **linep,
|
1999-05-02 10:51:54 +00:00
|
|
|
const char *delims,
|
1996-11-19 20:23:47 +00:00
|
|
|
char *delimp,
|
|
|
|
char *buf,
|
|
|
|
int lim)
|
|
|
|
{
|
1999-05-02 10:51:54 +00:00
|
|
|
char c, *pc;
|
|
|
|
const char *p;
|
1996-11-19 20:23:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
pc = *linep;
|
|
|
|
if (*pc == '\0')
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (lim == 0)
|
|
|
|
return -1;
|
|
|
|
c = *pc++;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
|
1998-07-22 05:49:36 +00:00
|
|
|
if (c == '\\' && *pc != '\0') {
|
1996-11-19 20:23:47 +00:00
|
|
|
if ((c = *pc++) == 'n') {
|
|
|
|
c = '\n';
|
|
|
|
} else if (c == 'r') {
|
|
|
|
c = '\r';
|
|
|
|
} else if (c == 't') {
|
|
|
|
c = '\t';
|
|
|
|
} else if (c == 'b') {
|
|
|
|
c = '\b';
|
|
|
|
} else if (c >= '0' && c <= '7') {
|
|
|
|
c -= '0';
|
|
|
|
if (*pc >= '0' && *pc <= '7') {
|
|
|
|
c = (c<<3)+(*pc++ - '0');
|
|
|
|
if (*pc >= '0' && *pc <= '7')
|
|
|
|
c = (c<<3)+(*pc++ - '0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for (p = delims; *p != '\0'; ++p) {
|
|
|
|
if (*p == c)
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*buf++ = c;
|
|
|
|
--lim;
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
if (delimp != 0)
|
|
|
|
*delimp = c;
|
|
|
|
*linep = pc-1;
|
|
|
|
if (lim != 0)
|
|
|
|
*buf = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|