Date: Mon, 27 Jul 1998 02:40:01 -0700 (PDT) From: Ruslan Ermilov <ru@ucb.crimea.ua> To: freebsd-bugs@FreeBSD.ORG Subject: Re: bin/6832: [PATCH] Allows PINGing from any address on multihomed hosts Message-ID: <199807270940.CAA28564@freefall.freebsd.org>
index | next in thread | raw e-mail
The following reply was made to PR bin/6832; it has been noted by GNATS.
From: Ruslan Ermilov <ru@ucb.crimea.ua>
To: freebsd-gnats-submit@freebsd.org, ru@ucb.crimea.ua
Cc: Subject: Re: bin/6832: [PATCH] Allows PINGing from any address on multihomed hosts
Date: Mon, 27 Jul 1998 12:33:04 +0300
Hi!
I've slightly modified my patch, since the repository
files were changed in RELENG_2_2 branch.
Here is a new patch:
Index: ping.c
===================================================================
RCS file: /usr/FreeBSD-CVS/src/sbin/ping/ping.c,v
retrieving revision 1.8.2.17
diff -u -r1.8.2.17 ping.c
--- ping.c 1998/07/17 20:13:45 1.8.2.17
+++ ping.c 1998/07/27 08:56:25
@@ -141,6 +141,7 @@
char BSPACE = '\b'; /* characters written for flood */
char DOT = '.';
char *hostname;
+char *shostname;
int ident; /* process id to identify our packets */
int uid; /* cached uid for micro-optimization */
@@ -184,7 +185,7 @@
{
struct timeval last, intvl;
struct hostent *hp;
- struct sockaddr_in *to;
+ struct sockaddr_in *to, sin;
struct termios ts;
register int i;
int ch, hold, packlen, preload, sockerrno, almost_done = 0;
@@ -192,6 +193,7 @@
unsigned char ttl, loop;
u_char *datap, *packet;
char *target, hnamebuf[MAXHOSTNAMELEN];
+ char *source = NULL, snamebuf[MAXHOSTNAMELEN];
char *ep;
u_long ultmp;
#ifdef IP_OPTIONS
@@ -217,7 +219,7 @@
preload = 0;
datap = &outpack[8 + PHDR_LEN];
- while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:v")) != -1) {
+ while ((ch = getopt(argc, argv, "I:LQRS:T:c:adfi:l:np:qrs:v")) != -1) {
switch(ch) {
case 'a':
options |= F_AUDIBLE;
@@ -301,6 +303,9 @@
optarg);
datalen = ultmp;
break;
+ case 'S':
+ source = optarg;
+ break;
case 'T': /* multicast TTL */
ultmp = strtoul(optarg, &ep, 0);
if (*ep || ep == optarg || ultmp > 255)
@@ -321,6 +326,29 @@
usage();
target = argv[optind];
+ if (source) {
+ bzero((char *)&sin, sizeof(sin));
+ sin.sin_family = AF_INET;
+ if (inet_aton(source, &sin.sin_addr) != 0) {
+ shostname = source;
+ } else {
+ hp = gethostbyname2(source, AF_INET);
+ if (!hp)
+ errx(EX_NOHOST, "cannot resolve %s: %s",
+ source, hstrerror(h_errno));
+
+ sin.sin_len = sizeof sin;
+ if (hp->h_length > sizeof(sin.sin_addr))
+ errx(1,"gethostbyname2 returned an illegal address");
+ memcpy(&sin.sin_addr, hp->h_addr_list[0], sizeof sin.sin_addr);
+ (void)strncpy(snamebuf, hp->h_name, sizeof(snamebuf) - 1);
+ snamebuf[sizeof(snamebuf) - 1] = '\0';
+ shostname = snamebuf;
+ }
+ if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1)
+ err(1, "bind");
+ }
+
bzero((char *)&whereto, sizeof(struct sockaddr));
to = (struct sockaddr_in *)&whereto;
to->sin_family = AF_INET;
@@ -429,11 +457,13 @@
(void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
sizeof(hold));
- if (to->sin_family == AF_INET)
- (void)printf("PING %s (%s): %d data bytes\n", hostname,
- inet_ntoa(to->sin_addr),
- datalen);
- else
+ if (to->sin_family == AF_INET) {
+ (void)printf("PING %s (%s)", hostname,
+ inet_ntoa(to->sin_addr));
+ if (source)
+ (void)printf(" from %s", shostname);
+ (void)printf(": %d data bytes\n", datalen);
+ } else
(void)printf("PING %s: %d data bytes\n", hostname, datalen);
/*
@@ -1269,8 +1299,9 @@
static void
usage()
{
- fprintf(stderr, "%s\n%s\n",
+ fprintf(stderr, "%s\n%s\n%s\n",
"usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-p pattern]",
-" [-s packetsize] [host | [-L] [-I iface] [-T ttl] mcast-group]");
+" [-s packetsize] [-S src_addr]",
+" [host | [-L] [-I iface] [-T ttl] mcast-group]");
exit(EX_USAGE);
}
Index: ping.8
===================================================================
RCS file: /usr/FreeBSD-CVS/src/sbin/ping/ping.8,v
retrieving revision 1.3.2.7
diff -u -r1.3.2.7 ping.8
--- ping.8 1998/07/17 20:13:44 1.3.2.7
+++ ping.8 1998/07/27 08:15:20
@@ -48,6 +48,7 @@
.Op Fl l Ar preload
.Op Fl p Ar pattern
.Op Fl s Ar packetsize
+.Op Fl S Ar src_addr
.Bo
.Ar host |
.Op Fl L
@@ -197,6 +198,13 @@
with the 8 bytes of
.Tn ICMP
header data.
+.It Fl S Ar src_addr
+Use the following IP address as the source address in outgoing packets.
+On hosts with more than one IP address, this option can be used to
+force the source address to be something other than the IP address
+of the interface the probe packet is sent on. If the IP address
+is not one of this machine's interface addresses, an error is
+returned and nothing is sent.
.It Fl T Ar ttl
Set the IP Time To Live for multicasted packets.
This flag only applies if the ping destination is a multicast address.
Regards,
--
Ruslan Ermilov Sysadmin and DBA of the
ru@ucb.crimea.ua United Commercial Bank
+380.652.247.647 Simferopol, Ukraine
http://www.FreeBSD.org The Power To Serve
http://www.oracle.com Enabling The Information Age
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199807270940.CAA28564>
