Rely on the kernel to check the ident for us.

The code path that uses the ping socket still keeps track of the
ident value in the packet. This is unnecessary because the ident
value is overwritten by the kernel when sending packets and is
checked by the kernel when receiving packets.

Instead just rely on the fact that the kernel checks the ident
value on received packets filters out non-matching replies.  This
allows us to remove the code that calls bind() and getsockname()
just to get the ident value from the socket.

Bug: 9469682
Change-Id: I5041316c6e5262a4de2b547c40bd77cfac29a5b9
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) {