* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)query.c 5.6 (Berkeley) %G%";
#include <protocols/routed.h>
#define WTIME 5 /* Time to wait for all responses */
#define STIME 500000 /* usec to wait for another response */
char packet
[MAXPACKETSIZE
];
int fromlen
= sizeof(from
);
struct timeval shorttime
;
printf("usage: query [ -n ] hosts...\n");
s
= socket(AF_INET
, SOCK_DGRAM
, 0);
* Listen for returning packets;
* may be more than one packet per host.
bzero(&shorttime
, sizeof(shorttime
));
shorttime
.tv_usec
= STIME
;
signal(SIGALRM
, timeout
);
while ((count
> 0 && !timedout
) ||
select(20, &bits
, 0, 0, &shorttime
) > 0) {
cc
= recvfrom(s
, packet
, sizeof (packet
), 0,
struct sockaddr_in router
;
register struct rip
*msg
= (struct rip
*)packet
;
bzero((char *)&router
, sizeof (router
));
router
.sin_family
= AF_INET
;
router
.sin_addr
.s_addr
= inet_addr(host
);
if (router
.sin_addr
.s_addr
== -1) {
hp
= gethostbyname(host
);
printf("%s: unknown\n", host
);
bcopy(hp
->h_addr
, &router
.sin_addr
, hp
->h_length
);
sp
= getservbyname("router", "udp");
printf("udp/router: service unknown\n");
router
.sin_port
= sp
->s_port
;
msg
->rip_cmd
= RIPCMD_REQUEST
;
msg
->rip_vers
= RIPVERSION
;
msg
->rip_nets
[0].rip_dst
.sa_family
= htons(AF_UNSPEC
);
msg
->rip_nets
[0].rip_metric
= htonl(HOPCNT_INFINITY
);
if (sendto(s
, packet
, sizeof (struct rip
), 0,
&router
, sizeof(router
)) < 0)
* Handle an incoming routing packet.
struct sockaddr_in
*from
;
register struct rip
*msg
= (struct rip
*)packet
;
if (msg
->rip_cmd
!= RIPCMD_RESPONSE
)
printf("%d bytes from ", size
);
printf("%s:\n", inet_ntoa(from
->sin_addr
));
hp
= gethostbyaddr(&from
->sin_addr
, sizeof (struct in_addr
),
name
= hp
== 0 ? "???" : hp
->h_name
;
printf("%s(%s):\n", name
, inet_ntoa(from
->sin_addr
));
register struct sockaddr_in
*sin
;
if (size
< sizeof (struct netinfo
))
ntohs(n
->rip_dst
.sa_family
);
n
->rip_metric
= ntohl(n
->rip_metric
);
sin
= (struct sockaddr_in
*)&n
->rip_dst
;
net
= inet_netof(sin
->sin_addr
);
subnet
= inet_subnetof(sin
->sin_addr
);
lna
= inet_lnaof(sin
->sin_addr
);
np
= getnetbyaddr(net
, AF_INET
);
} else if ((lna
& 0xff) == 0 &&
(np
= getnetbyaddr(subnet
, AF_INET
))) {
struct in_addr subnaddr
, inet_makeaddr();
subnaddr
= inet_makeaddr(subnet
, INADDR_ANY
);
if (bcmp(&sin
->sin_addr
, &subnaddr
,
hp
= gethostbyaddr(&sin
->sin_addr
,
sizeof (struct in_addr
), AF_INET
);
printf("\t%s(%s), metric %d\n", name
,
inet_ntoa(sin
->sin_addr
), n
->rip_metric
);
printf("\t%s, metric %d\n",
inet_ntoa(sin
->sin_addr
), n
->rip_metric
);
size
-= sizeof (struct netinfo
), n
++;
* Return the possible subnetwork number from an internet address.
* SHOULD FIND OUT WHETHER THIS IS A LOCAL NETWORK BEFORE LOOKING
* INSIDE OF THE HOST PART. We can only believe this if we have other
* information (e.g., we can find a name for this number).
register u_long i
= ntohl(in
.s_addr
);
return ((i
& IN_CLASSB_NET
) >> IN_CLASSB_NSHIFT
);
return ((i
& IN_CLASSC_NET
) >> IN_CLASSC_NSHIFT
);
return ((i
& 0xffffffc0) >> 28);