Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 2 Jun 1998 16:45:31 +0300 (EEST)
From:      Ruslan Ermilov <ru@ucb.crimea.ua>
To:        FreeBSD-gnats-submit@FreeBSD.ORG
Subject:   bin/6832: [PATCH] Allows PINGing from any address on multihomed hosts
Message-ID:  <199806021345.QAA10870@relay.ucb.crimea.ua>

next in thread | raw e-mail | index | archive | help

>Number:         6832
>Category:       bin
>Synopsis:       [PATCH] Allows PINGing from any address on multihomed hosts
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:
>Keywords:
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jun  2 06:50:00 PDT 1998
>Last-Modified:
>Originator:     Ruslan Ermilov
>Organization:
United Commercial Bank
>Release:        FreeBSD 2.2.6-STABLE i386
>Environment:

	Fresh -stable

>Description:

	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).

>How-To-Repeat:


>Fix:
	
Index: ping.8
===================================================================
RCS file: /usr/FreeBSD-CVS/src/sbin/ping/ping.8,v
retrieving revision 1.3.2.6
diff -u -r1.3.2.6 ping.8
--- ping.8	1997/09/14 19:40:03	1.3.2.6
+++ ping.8	1998/06/02 13:15:12
@@ -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
@@ -194,6 +195,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.
Index: ping.c
===================================================================
RCS file: /usr/FreeBSD-CVS/src/sbin/ping/ping.c,v
retrieving revision 1.8.2.16
diff -u -r1.8.2.16 ping.c
--- ping.c	1998/05/25 20:21:34	1.8.2.16
+++ ping.c	1998/06/02 12:25:05
@@ -184,14 +184,14 @@
 {
 	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;
 	struct in_addr ifaddr;
 	unsigned char ttl, loop;
 	u_char *datap, *packet;
-	char *target, hnamebuf[MAXHOSTNAMELEN];
+	char *source = NULL, *target, hnamebuf[MAXHOSTNAMELEN];
 	char *ep;
 	u_long ultmp;
 #ifdef IP_OPTIONS
@@ -217,7 +217,7 @@
 	preload = 0;
 
 	datap = &outpack[8 + sizeof(struct timeval)];
-	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 +301,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)
@@ -322,6 +325,24 @@
 		usage(argv[0]);
 	target = argv[optind];
 
+	if (source) {
+		bzero((char *)&sin, sizeof(sin));
+		sin.sin_family = AF_INET;
+		if (inet_aton(source, &sin.sin_addr) == 0) {
+			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);
+		}
+		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;
@@ -1251,6 +1272,7 @@
 	fprintf(stderr,
 		"usage: %s [-QRadfnqrv] [-c count] [-i wait] [-l preload] "
 		"[-p pattern]\n       [-s packetsize] "
+		"[-S src_addr]\n       "
 		"[host | [-L] [-I iface] [-T ttl] mcast-group]\n",
 		argv0);
 	exit(EX_USAGE);
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199806021345.QAA10870>