Code:
Xping(int argc, char *argv[])
{
struct timeval timeout;
struct hostent *hp;
struct sockaddr_in *to;
struct protoent *proto;
struct in_addr ifaddr;
int i;
int ch, fdmask, hold, packlen, preload;
u_char *datap, *packet;
char *target, hnamebuf[MAXHOSTNAMELEN];
u_char ttl, loop;
int am_i_root;
#ifdef IP_OPTIONS
char rspace[3 + 4 * NROUTES + 1]; /* record route space */
#endif
static char *null = NULL;
__environ = &null;
am_i_root = (getuid()==0);
/*
* Pull this stuff up front so we can drop root if desired.
*/
if (!(proto = getprotobyname("icmp"))) {
(void)fprintf(stderr, "ping: unknown protocol icmp.\n");
exit(2);
}
if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) {
if (errno==EPERM) {
fprintf(stderr, "ping: ping must run as root\n");
}
else perror("ping: socket");
exit(2);
}
#ifdef SAFE_TO_DROP_ROOT
setuid(getuid());
#endif
preload = 0;
datap = &outpack[8 + sizeof(struct timeval)];
while ((ch = getopt(argc, argv, "I:LRc:dfh:i:l:np:qrs:t:v")) != EOF)
switch(ch) {
case 'c':
npackets = atoi(optarg);
if (npackets <= 0) {
(void)fprintf(stderr,
"ping: bad number of packets to transmit.\n");
exit(2);
}
break;
case 'd':
options |= F_SO_DEBUG;
break;
case 'f':
if (!am_i_root) {
(void)fprintf(stderr,
"ping: %s\n", strerror(EPERM));
exit(2);
}
options |= F_FLOOD;
setbuf(stdout, NULL);
break;
case 'i': /* wait between sending packets */
interval = atoi(optarg);
if (interval <= 0) {
(void)fprintf(stderr,
"ping: bad timing interval.\n");
exit(2);
}
options |= F_INTERVAL;
break;
case 'l':
if (!am_i_root) {
(void)fprintf(stderr,
"ping: %s\n", strerror(EPERM));
exit(2);
}
preload = atoi(optarg);
if (preload < 0) {
(void)fprintf(stderr,
"ping: bad preload value.\n");
exit(2);
}
break;
case 'n':
options |= F_NUMERIC;
break;
case 'p': /* fill buffer with user pattern */
options |= F_PINGFILLED;
fill(datap, optarg);
break;
case 'q':
options |= F_QUIET;
break;
case 'R':
options |= F_RROUTE;
break;
case 'r':
options |= F_SO_DONTROUTE;
break;
case 's': /* size of packet to send */
datalen = atoi(optarg);
if (datalen > MAXPACKET) {
(void)fprintf(stderr,
"ping: packet size too large.\n");
exit(2);
}
if (datalen <= 0) {
(void)fprintf(stderr,
"ping: illegal packet size.\n");
exit(2);
}
break;
case 'v':
options |= F_VERBOSE;
break;
case 'L':
moptions |= MULTICAST_NOLOOP;
loop = 0;
break;
case 't':
moptions |= MULTICAST_TTL;
i = atoi(optarg);
if (i < 0 || i > 255) {
printf("ttl %u out of range\n", i);
exit(2);
}
ttl = i;
break;
case 'I':
moptions |= MULTICAST_IF;
{
int i1, i2, i3, i4;
char junk;
if (sscanf(optarg, "%u.%u.%u.%u%c",
&i1, &i2, &i3, &i4, &junk) != 4) {
printf("bad interface address '%s'\n",
optarg);
exit(2);
}
ifaddr.s_addr = (i1<<24)|(i2<<16)|(i3<<8)|i4;
ifaddr.s_addr = htonl(ifaddr.s_addr);
}
break;
default:
usage();
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
target = *argv;
memset(&whereto, 0, sizeof(struct sockaddr));
to = (struct sockaddr_in *)&whereto;
to->sin_family = AF_INET;
if (inet_aton(target, &to->sin_addr)) {
hostname = target;
}
else {
hp = gethostbyname(target);
if (!hp) {
(void)fprintf(stderr,
"ping: unknown host %s\n", target);
exit(2);
}
to->sin_family = hp->h_addrtype;
if (hp->h_length > (int)sizeof(to->sin_addr)) {
hp->h_length = sizeof(to->sin_addr);
}
memcpy(&to->sin_addr, hp->h_addr, hp->h_length);
(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
hostname = hnamebuf;
}
if (options & F_FLOOD && options & F_INTERVAL) {
(void)fprintf(stderr,
"ping: -f and -i incompatible options.\n");
exit(2);
}
if (datalen >= (int)sizeof(struct timeval)) /* can we time transfer */
timing = 1;
packlen = datalen + MAXIPLEN + MAXICMPLEN;
packet = malloc((u_int)packlen);
if (!packet) {
(void)fprintf(stderr, "ping: out of memory.\n");
exit(2);
}
if (!(options & F_PINGFILLED))
for (i = 8; i < datalen; ++i)
*datap++ = i;
ident = getpid() & 0xFFFF;
hold = 1;
if (options & F_SO_DEBUG)
(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
sizeof(hold));
if (options & F_SO_DONTROUTE)
(void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
sizeof(hold));
/* this is necessary for broadcast pings to work */
setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&hold, sizeof(hold));
/* record route option */
if (options & F_RROUTE) {
#ifdef IP_OPTIONS
memset(rspace, 0, sizeof(rspace));
rspace[IPOPT_OPTVAL] = IPOPT_RR;
rspace[IPOPT_OLEN] = sizeof(rspace)-1;
rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
sizeof(rspace)) < 0) {
perror("ping: record route");
exit(2);
}
#else
(void)fprintf(stderr,
"ping: record route not available in this implementation.\n");
exit(2);
#endif /* IP_OPTIONS */
}
/*
* When pinging the broadcast address, you can get a lot of answers.
* Doing something so evil is useful if you are trying to stress the
* ethernet, or just want to fill the arp cache to get some stuff for
* /etc/ethers.
*/
hold = 48 * 1024;
(void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
sizeof(hold));
/*#if 0*/
if (moptions & MULTICAST_NOLOOP) {
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP,
&loop, 1) == -1) {
perror ("can't disable multicast loopback");
exit(92);
}
}
if (moptions & MULTICAST_TTL) {
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
&ttl, 1) == -1) {
perror ("can't set multicast time-to-live");
exit(93);
}
}
if (moptions & MULTICAST_IF) {
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
&ifaddr, sizeof(ifaddr)) == -1) {
perror ("can't set multicast source interface");
exit(94);
}
}
/*#endif*/
if (to->sin_family == AF_INET)
(void)printf("PING %s (%s): %d data bytes\n", hostname,
inet_ntoa(*(struct in_addr *)&to->sin_addr.s_addr),
datalen);
else
(void)printf("PING %s: %d data bytes\n", hostname, datalen);
(void)signal(SIGINT, finish);
(void)signal(SIGALRM, catcher);
while (preload--) /* fire off them quickies */
pinger();
if ((options & F_FLOOD) == 0)
catcher(0); /* start things going */
for (;;) {
struct sockaddr_in from;
register int cc;
size_t fromlen;
if (options & F_FLOOD) {
pinger();
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
fdmask = 1 << s;
if (select(s + 1, (fd_set *)&fdmask, (fd_set *)NULL,
(fd_set *)NULL, &timeout) < 1)
continue;
}
fromlen = sizeof(from);
if ((cc = recvfrom(s, (char *)packet, packlen, 0,
(struct sockaddr *)&from, &fromlen)) < 0) {
if (errno == EINTR)
continue;
perror("ping: recvfrom");
continue;
}
pr_pack((char *)packet, cc, &from);
if (npackets && nreceived >= npackets)
break;
}
finish(0);
/* NOTREACHED */
return(avg);