Bug 6832 - [PATCH] Allows PINGing from any address on multihomed hosts
Summary: [PATCH] Allows PINGing from any address on multihomed hosts
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 2.2.6-STABLE
Hardware: Any Any
: Normal Affects Only Me
Assignee: Warner Losh
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 1998-06-02 14:50 UTC by Ruslan Ermilov
Modified: 1999-03-21 02:58 UTC (History)
0 users

See Also:


Attachments
file.diff (3.15 KB, patch)
1998-06-02 14:50 UTC, Ruslan Ermilov
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ruslan Ermilov 1998-06-02 14:50:00 UTC
	The patch below allows to ping from any address on the multihomed host.
	The man page is also updated, the text was cutted from traceroute(8).
Comment 1 Poul-Henning Kamp freebsd_committer freebsd_triage 1998-06-06 10:22:04 UTC
State Changed
From-To: open->suspended

awaiting committer 

Comment 2 Ruslan Ermilov 1998-07-27 10:33:04 UTC
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
Comment 3 Warner Losh freebsd_committer freebsd_triage 1998-07-27 22:02:58 UTC
Responsible Changed
From-To: freebsd-bugs->imp

I'll fix this. 
Comment 4 Warner Losh freebsd_committer freebsd_triage 1999-01-06 07:54:36 UTC
State Changed
From-To: suspended->closed

I've fixed this. 

Comment 5 Ruslan Ermilov 1999-01-06 08:37:56 UTC
On Tue, Jan 05, 1999 at 11:54:51PM -0800, Warner Losh wrote:
> Synopsis: [PATCH] Allows PINGing from any address on multihomed hosts
> 
> State-Changed-From-To: suspended->closed
> State-Changed-By: imp
> State-Changed-When: Wed Jan 6 00:54:36 MST 1999
> State-Changed-Why: 
> I've fixed this.

The ping.8 is only partially updated, compare
the original patch and repository commit!

And, will this patch come to RELENG_2_2 sometime?

BR,
-- 
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
Comment 6 Warner Losh 1999-01-06 17:39:42 UTC
In message <19990106103756.A26584@ucb.crimea.ua> Ruslan Ermilov writes:
: The ping.8 is only partially updated, compare
: the original patch and repository commit!

Dang.  I thought I'd caught that.  Will do again.

: And, will this patch come to RELENG_2_2 sometime?

I hadn't planned on it, but since you asked specifically I'll do what
I can to merge.  I'd ask that if you are running -stable to test it
and let me know if i broke anything, since I no longer have any stable
systems to test on.

Warner
Comment 7 Warner Losh freebsd_committer freebsd_triage 1999-01-06 17:52:20 UTC
State Changed
From-To: closed->feedback

Man page commit incomplete and a request to merge into 2.2.x. 

Comment 8 Ruslan Ermilov 1999-01-10 07:50:39 UTC
On Wed, Jan 06, 1999 at 10:39:42AM -0700, Warner Losh wrote:
> In message <19990106103756.A26584@ucb.crimea.ua> Ruslan Ermilov writes:
> : The ping.8 is only partially updated, compare
> : the original patch and repository commit!
> 
> Dang.  I thought I'd caught that.  Will do again.
> 
> : And, will this patch come to RELENG_2_2 sometime?
> 
> I hadn't planned on it, but since you asked specifically I'll do what
> I can to merge.  I'd ask that if you are running -stable to test it
> and let me know if i broke anything, since I no longer have any stable
> systems to test on.
> 
> Warner

I regularly upgrade my system (once per month), and I live with this
patch for a 3+ months without having any problems.
Currently, my `uname -r` says: 2.2.8-STABLE.

Best 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
Comment 9 Warner Losh freebsd_committer freebsd_triage 1999-03-21 02:57:42 UTC
State Changed
From-To: feedback->closed

I fixed this and the orignator sayid to close it :-)