From davidt@yadt.co.uk  Sun May 26 08:49:14 2002
Return-Path: <davidt@yadt.co.uk>
Received: from mail.yadt.co.uk (yadt.demon.co.uk [158.152.4.134])
	by hub.freebsd.org (Postfix) with SMTP id B9BA437B446
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 26 May 2002 08:49:05 -0700 (PDT)
Received: (qmail 97649 invoked from network); 26 May 2002 15:49:03 -0000
Received: from unknown (HELO mail.gattaca.yadt.co.uk) (qmailr@10.0.0.2)
  by yadt.demon.co.uk with SMTP; 26 May 2002 15:49:03 -0000
Received: (qmail 26858 invoked by uid 1000); 26 May 2002 15:49:02 -0000
Message-Id: <20020526154902.26857.qmail@gattaca.yadt.co.uk>
Date: 26 May 2002 15:49:02 -0000
From: David Taylor <davidt@yadt.co.uk>
Reply-To: David Taylor <davidt@yadt.co.uk>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: ping -o option (exit after one reply)
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         38573
>Category:       bin
>Synopsis:       ping -o option (exit after one reply)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Sun May 26 08:50:01 PDT 2002
>Closed-Date:    Tue Oct 01 20:24:45 PDT 2002
>Last-Modified:  Tue Oct 01 20:24:45 PDT 2002
>Originator:     David Taylor
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
>Environment:
System: FreeBSD gattaca.yadt.co.uk 5.0-CURRENT FreeBSD 5.0-CURRENT #7: Tue Feb 12 22:04:28 GMT 2002 root@:/usr/obj/usr/src/sys/GATTACA i386

>Description:

Whilst trying to make a script written for NetBSD run on FreeBSD, I
discovered the script used a non-existant (on FreeBSD) ping option ``-o''.

ping -o will exit after receiving a single reply packet, which is useful
for trying to see if a host/link is alive at all (which is what the script
was attempting to to do :).

I checked cvsweb, and discovered the changes required to implement -o were
pretty simple, so created the patch below, and decided it might be useful
to the FreeBSD community.

>How-To-Repeat:
	N/A

>Fix:

--- ping_o_option.diff begins here ---
Index: ping.8
===================================================================
RCS file: /usr/ncvs/src/sbin/ping/ping.8,v
retrieving revision 1.34
diff -u -r1.34 ping.8
--- ping.8	7 Feb 2002 16:47:22 -0000	1.34
+++ ping.8	26 May 2002 15:36:23 -0000
@@ -42,7 +42,7 @@
 packets to network hosts
 .Sh SYNOPSIS
 .Nm
-.Op Fl AQRadfnqrv
+.Op Fl AQRadfnoqrv
 .Op Fl c Ar count
 .Op Fl i Ar wait
 .Op Fl l Ar preload
@@ -159,6 +159,8 @@
 .It Fl n
 Numeric output only.
 No attempt will be made to lookup symbolic names for host addresses.
+.It Fl o
+Exit successfully after receiving one reply packet.
 .It Fl p Ar pattern
 You may specify up to 16
 .Dq pad
Index: ping.c
===================================================================
RCS file: /usr/ncvs/src/sbin/ping/ping.c,v
retrieving revision 1.66
diff -u -r1.66 ping.c
--- ping.c	2 Apr 2002 10:15:32 -0000	1.66
+++ ping.c	26 May 2002 15:38:49 -0000
@@ -136,6 +136,7 @@
 #endif /*IPSEC*/
 #define	F_TTL		0x8000
 #define	F_MISSED	0x10000
+#define	F_ONCE		0x20000
 
 /*
  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
@@ -241,7 +242,7 @@
 
 	datap = &outpack[8 + PHDR_LEN];
 	while ((ch = getopt(argc, argv,
-		"AI:LQRS:T:c:adfi:l:m:np:qrs:t:v"
+		"AI:LQRS:T:c:adfi:l:m:nop:qrs:t:v"
 #ifdef IPSEC
 #ifdef IPSEC_POLICY_IPSEC
 		"P:"
@@ -319,6 +320,9 @@
 		case 'n':
 			options |= F_NUMERIC;
 			break;
+		case 'o':
+			options |= F_ONCE;
+			break;
 		case 'p':		/* fill buffer with user pattern */
 			options |= F_PINGFILLED;
 			fill((char *)datap, optarg);
@@ -687,7 +691,8 @@
 				t = &now;
 			}
 			pr_pack((char *)packet, cc, &from, t);
-			if (npackets && nreceived >= npackets)
+			if (((options & F_ONCE) && nreceived) ||
+			    (npackets && nreceived >= npackets))
 				break;
 		}
 		if (n == 0 || options & F_FLOOD) {
@@ -1444,7 +1449,7 @@
 usage()
 {
 	(void)fprintf(stderr, "%s\n%s\n%s\n",
-"usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-m ttl]",
+"usage: ping [-QRadfnoqrv] [-c count] [-i wait] [-l preload] [-m ttl]",
 "            [-p pattern] "
 #ifdef IPSEC
 #ifdef IPSEC_POLICY_IPSEC
--- ping_o_option.diff ends here ---


>Release-Note:
>Audit-Trail:

From: Dima Dorfman <dima@trit.org>
To: David Taylor <davidt@yadt.co.uk>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/38573: ping -o option (exit after one reply) 
Date: Sun, 26 May 2002 23:07:33 +0000

 David Taylor <davidt@yadt.co.uk> wrote:
 > ping -o will exit after receiving a single reply packet, which is useful
 > for trying to see if a host/link is alive at all (which is what the script
 > was attempting to to do :).
 
 Isn't this the same as `ping -c 1`?

From: David Taylor <davidt@yadt.co.uk>
To: Dima Dorfman <dima@trit.org>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/38573: ping -o option (exit after one reply)
Date: Mon, 27 May 2002 08:19:57 +0100

 On Sun, 26 May 2002, Dima Dorfman wrote:
 > David Taylor <davidt@yadt.co.uk> wrote:
 > > ping -o will exit after receiving a single reply packet, which is useful
 > > for trying to see if a host/link is alive at all (which is what the script
 > > was attempting to to do :).
 > 
 > Isn't this the same as `ping -c 1`?
 
 No, ping -c 1 will exit after *sending* one packet, whether it is received
 or not.
 
 ping -o -c 10 will send (up to) 10 packets, exiting sucessfully after
 receiving the first reply, or exit(2)'ing if it fails to receive a reply
 from any packet.
 
 -- 
 David Taylor
 davidt@yadt.co.uk
 "The future just ain't what it used to be"
State-Changed-From-To: open->closed 
State-Changed-By: dd 
State-Changed-When: Tue Oct 1 20:24:29 PDT 2002 
State-Changed-Why:  
Committed to -current, thanks!  I'll MFC this after 4.7. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=38573 
>Unformatted:
