Merge "Rely on the kernel to check the ident for us."
diff --git a/ping.c b/ping.c
index 7f7ef82..bf69fb0 100644
--- a/ping.c
+++ b/ping.c
@@ -94,7 +94,6 @@
 int optlen = 0;
 int settos = 0;			/* Set TOS, Precendence or other QOS options */
 int icmp_sock;			/* socket file descriptor */
-int using_ping_socket = 0;
 u_char outpack[0x10000];
 int maxpacket = sizeof(outpack);
 
@@ -125,7 +124,7 @@
 {
 	struct hostent *hp;
 	int ch, hold, packlen;
-	int socket_errno;
+	int socket_errno = 0;
 	u_char *packet;
 	char *target;
 #ifdef USE_IDN
@@ -148,16 +147,14 @@
 	setlocale(LC_ALL, "");
 #endif
 
-	enable_capability_raw();
-
 	icmp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
-	if (icmp_sock != -1)
-		using_ping_socket = 1;
-	else
+	if (icmp_sock < 0) {
+		enable_capability_raw();
 		icmp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
-	socket_errno = errno;
-
-	disable_capability_raw();
+		socket_errno = errno;
+		disable_capability_raw();
+		using_ping_socket = 0;
+	}
 
 	source.sin_family = AF_INET;
 
@@ -468,32 +465,10 @@
 		}
 	}
 
-	if (!using_ping_socket) {
-		if ((options&F_STRICTSOURCE) &&
-		    bind(icmp_sock, (struct sockaddr*)&source, sizeof(source)) == -1) {
-			perror("bind");
-			exit(2);
-		}
-	} else {
-		struct sockaddr_in sa;
-		socklen_t sl;
-
-		sa.sin_family = AF_INET;
-		sa.sin_port = 0;
-		sa.sin_addr.s_addr = (options&F_STRICTSOURCE) ?
-			source.sin_addr.s_addr : 0;
-		sl = sizeof(sa);
-
-		if (bind(icmp_sock, (struct sockaddr *) &sa, sl) == -1) {
-			perror("bind");
-			exit(2);
-		}
-
-		if (getsockname(icmp_sock, (struct sockaddr *) &sa, &sl) == -1) {
-			perror("getsockname");
-			exit(2);
-		}
-		ident = sa.sin_port;
+	if ((options&F_STRICTSOURCE) &&
+	    bind(icmp_sock, (struct sockaddr*)&source, sizeof(source)) == -1) {
+		perror("bind");
+		exit(2);
 	}
 
 	if (!using_ping_socket) {
diff --git a/ping_common.c b/ping_common.c
index 124270a..1710d14 100644
--- a/ping_common.c
+++ b/ping_common.c
@@ -13,6 +13,7 @@
 __u16 acked;
 
 struct rcvd_table rcvd_tbl;
+int using_ping_socket = 1;
 
 
 /* counters */
@@ -59,7 +60,7 @@
 char *hostname;
 int uid;
 uid_t euid;
-int ident;			/* process id to identify our packets */
+int ident = 0;			/* process id to identify our packets */
 
 static int screen_width = INT_MAX;
 
@@ -677,7 +678,7 @@
 			*p++ = i;
 	}
 
-	if (!ident)
+	if (!using_ping_socket)
 		ident = htons(getpid() & 0xFFFF);
 
 	set_signal(SIGINT, sigexit);
@@ -837,7 +838,7 @@
 			}
 
 			/* See? ... someone runs another ping on this host. */
-			if (not_ours)
+			if (not_ours && !using_ping_socket)
 				install_filter();
 
 			/* If nothing is in flight, "break" returns us to pinger. */
@@ -1074,6 +1075,6 @@
 }
 
 inline int is_ours(uint16_t id) {
-	return id == ident;
+	return using_ping_socket || id == ident;
 }
 
diff --git a/ping_common.h b/ping_common.h
index f917315..f9330aa 100644
--- a/ping_common.h
+++ b/ping_common.h
@@ -100,6 +100,7 @@
 };
 
 extern struct rcvd_table rcvd_tbl;
+extern int using_ping_socket;
 
 #define	A(bit)	(rcvd_tbl.bitmap[(bit) >> BITMAP_SHIFT])	/* identify word in array */
 #define	B(bit)	(((bitmap_t)1) << ((bit) & ((1 << BITMAP_SHIFT) - 1)))	/* identify bit in word */