ping_aux.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <netinet/in_systm.h>
#include <netdb.h>
#include <stdlib.h>
#include <ifaddrlist.h>
#include "ping.h"
/*
* IPv4 source routing option.
* In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
* is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
*/
struct ip_sourceroute {
/* up to 9 IPv4 addresses */
};
extern char *pr_name(char *, int);
static void pr_options(uchar_t *, int);
extern char *pr_protocol(int);
static char *pr_type(int);
extern void schedule_sigalrm();
extern void send_scheduled_probe();
extern void sigalrm_handler();
struct in_addr *);
/*
* Set IPv4 options
*/
void
{
int req_size;
char *bufp;
int optsize = ROUTE_SIZE;
struct ip_sourceroute *srp;
struct ip_timestamp *tsp;
int i;
if (gw_count > 0) {
/* 3 = 1 (code) + 1 (len) + 1 (ptr) of src route opt. */
progname);
}
for (i = 0; i < gw_count; i++) {
&srp->ipsr_addrs[i],
sizeof (struct in_addr));
}
}
/* do we send a timestamp option? */
if (ts_option) {
if (optsize < IPOPT_MINOFF) {
"%s: no room for timestamp option\n",
progname);
}
/* LINTED */
req_size = IPOPT_MINOFF +
2 * sizeof (struct ipt_ta);
/*
* Note: BSD/4.X is broken in their check so we
* have to bump up this number by at least one.
*/
req_size++;
"timestamp option\n", progname);
}
sizeof (struct in_addr));
sizeof (struct in_addr));
}
}
/* do we send a record route option? */
if (rr_option) {
if (optsize < IPOPT_MINOFF) {
"%s: no room for record route option\n",
progname);
}
/*
* Format of record route option is same as source
* route option.
*/
}
/* Round up to 4 byte boundary */
if (optsize & 0x3)
0) {
}
}
}
/*
* Check out the packet to see if it came from us. This logic is necessary
* because ALL readers of the ICMP socket get a copy of ALL ICMP packets
* which arrive ('tis only fair). This permits multiple copies of this
* program to be run without having intermingled output (or statistics!).
*/
void
{
union any_in_addr dst_addr;
struct sockaddr_in *from;
/* this reply same as where */
/* we're sending currently? */
/* probe all with npackets>0 */
/* and we received reply for */
/* the last probe sent to */
/* targetaddr */
int cc_left;
char tmp_buf[INET6_ADDRSTRLEN];
static char *unreach[] = {
"Net Unreachable",
"Host Unreachable",
"Protocol Unreachable",
"Port Unreachable",
"Fragmentation needed and DF set",
"Source Route Failed",
/* The following are from RFC1700 */
"Net Unknown",
"Host Unknown",
"Source Host Isolated",
"Dest Net Prohibited",
"Dest Host Prohibited",
"Net Unreachable for TOS",
"Host Unreachable for TOS",
"Communication Administratively Prohibited",
"Host Precedence Violation",
"Precedence Cutoff in Effect"
};
static char *redirect[] = {
"Net",
"Host",
"TOS Net",
"TOS Host"
};
static char *timexceed[] = {
"Time exceeded in transit",
"Time exceeded during reassembly"
};
int i;
/* decompose msghdr into useful pieces */
/* LINTED */
/* LINTED */
if (verbose) {
}
return;
}
/* LINTED */
/*
* Assume that we are running on a pre-4.3BSD system
* such as SunOS before 4.0
*/
/* LINTED */
}
case ICMP_UNREACH:
/* check if we have enough of the packet to work on */
if (verbose) {
Printf("packet too short (%d bytes) from %s\n",
AF_INET));
}
return;
}
/* get the UDP packet */
/* LINTED */
/* check to see if this is what we sent */
use_udp) {
} else {
}
if (valid_reply) {
/*
* For this valid reply, if we are still sending to
* this target IP address, we'd like to do some
* updates to targetaddr, so hold SIGALRMs.
*/
nreceived++;
if (reply_matched_current_target) {
/*
* Determine if stats, probe-all, and
* npackets != 0, and this is the reply for
* the last probe we sent to current target
* address.
*/
} else {
/*
* If it's just probe_all and we just received
* a reply from a target address we were
* probing and had timed out (now we are probing
* some other target address), we ignore
* this reply.
*/
/*
* Only if it's verbose, we get a
* message regarding this reply,
* otherwise we are done here.
*/
if (!verbose) {
return;
}
}
}
}
/* stats mode doesn't print 'alive' messages */
if (valid_reply && !stats) {
/*
* if we are still sending to the same target address,
* then stop it, because we know it's alive.
*/
if (reply_matched_current_target) {
(void) alarm(0); /* cancel alarm */
}
if (!probe_all) {
} else {
if (nflag) {
} else {
Printf("%s (%s) is alive\n",
}
}
if (reply_matched_current_target) {
/*
* Let's get things going again, but now
* ping will start sending to next target IP
* address.
*/
}
return;
} else {
/*
* If we are not moving to next targetaddr, let's
* release the SIGALRM now. We don't want to stall in
* the middle of probing a targetaddr if the pr_name()
* call (see below) takes longer.
*/
/* else, we'll release it later */
}
if (valid_reply) {
Printf("ICMP %d Unreachable from gateway %s\n",
} else {
Printf("ICMP %s from gateway %s\n",
}
}
}
/* if we are timing and the reply has a timeval */
/* LINTED */
sizeof (struct udphdr));
}
if (print_newline)
(void) putchar('\n');
/*
* If it's stats, probe-all, npackets > 0, and we received reply
* for the last probe sent to this target address, then we
* don't need to wait anymore, let's move on to next target
* address, now!
*/
if (last_reply_from_targetaddr) {
(void) alarm(0); /* cancel alarm */
}
break;
case ICMP_REDIRECT:
if (verbose) {
Printf("packet too short (%d bytes) from %s\n",
AF_INET));
}
return;
}
Printf("ICMP %d redirect from gateway %s\n",
} else {
Printf("ICMP %s redirect from gateway %s\n",
}
Printf(" to %s",
Printf(" for %s\n",
}
break;
case ICMP_ECHOREPLY:
if (!use_udp && !use_icmp_ts)
else
} else {
return;
}
if (valid_reply) {
/*
* For this valid reply, if we are still sending to
* this target IP address, we'd like to do some
* updates to targetaddr, so hold SIGALRMs.
*/
nreceived++;
if (reply_matched_current_target) {
/*
* Determine if stats, probe-all, and
* npackets != 0, and this is the reply for
* the last probe we sent to current target
* address.
*/
(MAX_ICMP_SEQ + 1) ==
} else {
/*
* If it's just probe_all and we just received
* a reply from a target address we were
* probing and had timed out (now we are probing
* some other target address), we ignore
* this reply.
*/
/*
* Only if it's verbose, we get a
* message regarding this reply,
* otherwise we are done here.
*/
if (!verbose) {
return;
}
}
}
}
if (!stats && valid_reply) {
/*
* if we are still sending to the same target address,
* then stop it, because we know it's alive.
*/
if (reply_matched_current_target) {
(void) alarm(0); /* cancel alarm */
}
if (!probe_all) {
} else {
/*
* If we are using send_reply, the real
* target address is not the src address of the
* replies. Use icmp_seq to find out where this
* probe was sent to.
*/
if (send_reply) {
(void) find_dstaddr(
} else {
}
if (nflag) {
} else {
Printf("%s (%s) is alive\n",
}
}
if (reply_matched_current_target) {
/*
* Let's get things going again, but now
* ping will start sending to next target IP
* address.
*/
}
return;
} else {
/*
* If we are not moving to next targetaddr, let's
* release the SIGALRM now. We don't want to stall in
* the middle of probing a targetaddr if the pr_name()
* call (see below) takes longer.
*/
/* else, we'll release it later */
}
/*
* If we are using send_reply, the real target address is
* not the src address of the replies. Use icmp_seq to find out
* where this probe was sent to.
*/
if (send_reply) {
} else {
}
/* LINTED */
}
(void) putchar('\n');
/*
* If it's stats, probe-all, npackets > 0, and we received reply
* for the last probe sent to this target address, then we
* don't need to wait anymore, let's move on to next target
* address, now!
*/
if (last_reply_from_targetaddr) {
(void) alarm(0); /* cancel alarm */
}
break;
case ICMP_SOURCEQUENCH:
if (verbose) {
Printf("packet too short (%d bytes) from %s\n",
AF_INET));
}
return;
}
Printf("ICMP Source Quench from %s\n",
/*
* if it's a UDP or TCP packet, we need at least first
*/
/* LINTED */
}
(void) putchar('\n');
}
break;
case ICMP_PARAMPROB:
if (verbose) {
Printf("packet too short (%d bytes) from %s\n",
AF_INET));
}
return;
}
case ICMP_PARAMPROB_OPTABSENT:
Printf("ICMP Missing a Required Option "
"parameter problem from %s\n",
break;
case ICMP_PARAMPROB_BADLENGTH:
Printf("ICMP Bad Length parameter problem "
"from %s\n",
Printf(" (value 0x%x)",
}
break;
case 0:
default:
Printf("ICMP Parameter Problem from %s\n",
Printf(" (value 0x%x)",
}
break;
}
/*
* if it's a UDP or TCP packet, we need at least first
*/
/* LINTED */
}
(void) putchar('\n');
}
break;
case ICMP_TIMXCEED:
if (verbose) {
Printf("packet too short (%d bytes) from %s\n",
AF_INET));
}
return;
}
Printf("ICMP %d time exceeded from %s\n",
} else {
Printf("ICMP %s from %s\n",
}
/* LINTED */
}
(void) putchar('\n');
}
break;
case ICMP_TSTAMPREPLY:
/* the packet should have enough space to store timestamps */
if (verbose) {
Printf("packet too short (%d bytes) from %s\n",
AF_INET));
}
return;
}
if (use_icmp_ts)
else
} else {
return;
}
if (valid_reply) {
/*
* For this valid reply, if we are still sending to
* this target IP address, we'd like to do some
* updates to targetaddr, so hold SIGALRMs.
*/
nreceived++;
if (reply_matched_current_target) {
/*
* Determine if stats, probe-all, and
* npackets != 0, and this is the reply for
* the last probe we sent to current target
* address.
*/
(MAX_ICMP_SEQ + 1) ==
} else {
/*
* If it's just probe_all and we just received
* a reply from a target address we were
* probing and had timed out (now we are probing
* some other target address), we ignore
* this reply.
*/
/*
* Only if it's verbose, we get a
* message regarding this reply,
* otherwise we are done here.
*/
if (!verbose) {
return;
}
}
}
}
if (!stats && valid_reply) {
/*
* if we are still sending to the same target address,
* then stop it, because we know it's alive.
*/
if (reply_matched_current_target) {
(void) alarm(0); /* cancel alarm */
}
if (!probe_all) {
} else {
/*
* If we are using send_reply, the real
* target address is not the src address of the
* replies. Use icmp_seq to find out where this
* probe was sent to.
*/
if (send_reply) {
(void) find_dstaddr(
} else {
}
if (nflag) {
} else {
Printf("%s (%s) is alive\n",
}
}
if (reply_matched_current_target) {
/*
* Let's get things going again, but now
* ping will start sending to next target IP
* address.
*/
}
return;
} else {
/*
* If we are not moving to next targetaddr, let's
* release the SIGALRM now. We don't want to stall in
* the middle of probing a targetaddr if the pr_name()
* call (see below) takes longer.
*/
/* else, we'll release it later */
}
/*
* If we are using send_reply, the real target address is
* not the src address of the replies. Use icmp_seq to find out
* where this probe was sent to.
*/
if (send_reply) {
} else {
}
Printf("orig = %lu, recv = %lu, xmit = %lu ",
if (valid_reply) {
/*
* icp->icmp_otime is the time passed since midnight.
* Therefore we need to adjust tv value, which is
* the time passed since Jan 1, 1970.
*/
if (triptime < 0)
}
(void) putchar('\n');
/*
* If it's stats, probe-all, npackets > 0, and we received reply
* for the last probe sent to this target address, then we
* don't need to wait anymore, let's move on to next target
* address, now!
*/
if (last_reply_from_targetaddr) {
(void) alarm(0); /* cancel alarm */
}
break;
case ICMP_ROUTERADVERT:
case ICMP_ROUTERSOLICIT:
/* Router discovery messages */
return;
case ICMP_ECHO:
case ICMP_TSTAMP:
case ICMP_IREQ:
case ICMP_MASKREQ:
/* These were never passed out from the SunOS 4.X kernel. */
return;
case ICMP_IREQREPLY:
case ICMP_MASKREPLY:
/* Replies for information and address mask requests */
return;
default:
if (verbose) {
Printf("icmp_type=%d (%s) ",
for (i = 0; i < 12; i++) {
Printf("x%2.2x: x%8.8x\n",
}
}
break;
}
/* if verbose and there exists IP options */
}
/*
* Print out the ip options.
*/
static void
{
int curlength;
Printf(" IP options: ");
while (optlength > 0) {
switch (*opt) {
case IPOPT_EOL:
optlength = 0;
break;
case IPOPT_NOP:
opt++;
optlength--;
continue;
case IPOPT_RR:
Printf(" <record route> ");
break;
case IPOPT_TS:
Printf(" <time stamp> ");
break;
case IPOPT_SECURITY:
Printf(" <security>");
break;
case IPOPT_LSRR:
Printf(" <loose source route> ");
break;
case IPOPT_SATID:
Printf(" <stream id>");
break;
case IPOPT_SSRR:
Printf(" <strict source route> ");
break;
default:
break;
}
/*
* Following most options comes a length field
*/
}
(void) putchar('\n');
}
/*
* Print out a recorded route option. If rrflag is _B_TRUE, it prints record
*/
static void
{
struct ip_sourceroute *rrp;
int sr_index = 0;
/* data starts at offset 3 */
length -= 3;
while (length > 0) {
/*
* Let's see if we are examining the addr pointed by ipsr_ptr
*/
rrflag) {
Printf(" (End of record)");
break;
}
Printf("(Current)");
}
sr_index++;
if (length > 0)
Printf(", ");
}
}
/*
* Print out a timestamp option.
*/
static void
{
struct ip_timestamp *tsp;
int ts_index = 0;
/* LINTED */
case IPOPT_TS_TSONLY:
break;
case IPOPT_TS_TSANDADDR:
break;
case IPOPT_TS_PRESPEC:
case 3:
break;
default:
return;
}
/* data starts at offset 4 */
length -= 4;
while (length > 0) {
break;
/* the minimum value of ipt_ptr is 5 */
Printf(" (End of record)");
break;
}
if (address_present) {
} else {
}
Printf("(Current)");
ts_index++;
if (length > 0)
Printf(", ");
}
}
/*
* Convert an ICMP "type" field to a printable string.
*/
static char *
{
static struct icmptype_table ttab[] = {
{ICMP_ECHOREPLY, "Echo Reply"},
{1, "ICMP 1"},
{2, "ICMP 2"},
{ICMP_UNREACH, "Dest Unreachable"},
{ICMP_SOURCEQUENCH, "Source Quench"},
{ICMP_REDIRECT, "Redirect"},
{6, "ICMP 6"},
{7, "ICMP 7"},
{ICMP_ECHO, "Echo"},
{ICMP_ROUTERADVERT, "Router Advertisement"},
{ICMP_ROUTERSOLICIT, "Router Solicitation"},
{ICMP_TIMXCEED, "Time Exceeded"},
{ICMP_PARAMPROB, "Parameter Problem"},
{ICMP_TSTAMP, "Timestamp"},
{ICMP_TSTAMPREPLY, "Timestamp Reply"},
{ICMP_IREQ, "Info Request"},
{ICMP_IREQREPLY, "Info Reply"},
{ICMP_MASKREQ, "Netmask Request"},
{ICMP_MASKREPLY, "Netmask Reply"}
};
int i;
}
return ("OUT-OF-RANGE");
}