xref: /aosp_15_r20/external/ltp/testcases/network/sockets/ltpClient.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /* -*- linux-c -*- */
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2000
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
8*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
9*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
10*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
13*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
14*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
18*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
19*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker /*
24*49cdfc7eSAndroid Build Coastguard Worker  * ltpClient.c
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * LTP Network Socket Test Client
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  */
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/ip_icmp.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <arpa/inet.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <netdb.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <resolv.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker #define LOCAL_UDP_SERVER_PORT   10000
47*49cdfc7eSAndroid Build Coastguard Worker #define LOCAL_TCP_SERVER_PORT   10001
48*49cdfc7eSAndroid Build Coastguard Worker #define LOCAL_MCAST_SERVER_PORT 10002
49*49cdfc7eSAndroid Build Coastguard Worker #define MAX_MSG_LEN             256
50*49cdfc7eSAndroid Build Coastguard Worker #define TIMETOLIVE              10
51*49cdfc7eSAndroid Build Coastguard Worker #define PACKETSIZE	            64
52*49cdfc7eSAndroid Build Coastguard Worker #define NET_ERROR               -1
53*49cdfc7eSAndroid Build Coastguard Worker #define PACKET_LEN              1024	/* 1K should be plenty */
54*49cdfc7eSAndroid Build Coastguard Worker #define TRUE                    1
55*49cdfc7eSAndroid Build Coastguard Worker #define FALSE                   0
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker struct protoent *protocol = NULL;
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker struct packet {
60*49cdfc7eSAndroid Build Coastguard Worker 	struct icmphdr hdr;
61*49cdfc7eSAndroid Build Coastguard Worker 	char msg[PACKETSIZE - sizeof(struct icmphdr)];
62*49cdfc7eSAndroid Build Coastguard Worker };
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker /*
65*49cdfc7eSAndroid Build Coastguard Worker *   Function Prototypes
66*49cdfc7eSAndroid Build Coastguard Worker */
67*49cdfc7eSAndroid Build Coastguard Worker int ltp_run_ping_tests(char *hostName);
68*49cdfc7eSAndroid Build Coastguard Worker int ltp_run_traceroute_tests(char *hostName);
69*49cdfc7eSAndroid Build Coastguard Worker void ping_network(struct sockaddr_in *rawAddr, int pid);
70*49cdfc7eSAndroid Build Coastguard Worker void output_to_display(void *netPacket, int bytes, int pid);
71*49cdfc7eSAndroid Build Coastguard Worker unsigned short checksum(void *netPacket, int len);
72*49cdfc7eSAndroid Build Coastguard Worker int network_listener(char *hostname, int pid);
73*49cdfc7eSAndroid Build Coastguard Worker void ltp_traceroute(struct sockaddr_in *rawTraceAddr, char *hostName, int pid);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker /*******************************************************************
76*49cdfc7eSAndroid Build Coastguard Worker *  Function: Main
77*49cdfc7eSAndroid Build Coastguard Worker *
78*49cdfc7eSAndroid Build Coastguard Worker *  Main will run the tests in this order.
79*49cdfc7eSAndroid Build Coastguard Worker *       UDP, TCP and Multicast will be run first, if multicast is enabled.
80*49cdfc7eSAndroid Build Coastguard Worker *       If multicast is not enabled, the UDP/TCP tests will continue.
81*49cdfc7eSAndroid Build Coastguard Worker *       Once those tests complete, the ping and then traceroute tests will run.
82*49cdfc7eSAndroid Build Coastguard Worker *
83*49cdfc7eSAndroid Build Coastguard Worker ********************************************************************/
main(int argc,char * argv[])84*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	int udpSocketHandle, tcpSocketHandle, mcastSocketHandle, rc, i;
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in udpClientAddr,
90*49cdfc7eSAndroid Build Coastguard Worker 	    udpRemoteServerAddr,
91*49cdfc7eSAndroid Build Coastguard Worker 	    tcpClientAddr,
92*49cdfc7eSAndroid Build Coastguard Worker 	    tcpRemoteServerAddr, mcastClientAddr, mcastRemoteServerAddr;
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	struct hostent *hostEntry;
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	char hostName[MAX_MSG_LEN],
97*49cdfc7eSAndroid Build Coastguard Worker 	    progName[MAX_MSG_LEN], traceName[MAX_MSG_LEN], multiCast = TRUE;
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	unsigned char ttl = 1;
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	mcastSocketHandle = -1;
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	/* check command line args */
104*49cdfc7eSAndroid Build Coastguard Worker 	if (argc < 4) {
105*49cdfc7eSAndroid Build Coastguard Worker 		printf
106*49cdfc7eSAndroid Build Coastguard Worker 		    ("usage :<server-hostname> <trace-hostName> <data1> ... <dataN> \n");
107*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
108*49cdfc7eSAndroid Build Coastguard Worker 	}
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	strncpy(progName, argv[0], MAX_MSG_LEN);
111*49cdfc7eSAndroid Build Coastguard Worker 	strncpy(hostName, argv[1], MAX_MSG_LEN);
112*49cdfc7eSAndroid Build Coastguard Worker 	strncpy(traceName, argv[2], MAX_MSG_LEN);
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	/* get server IP address (no check if input is IP address or DNS name */
115*49cdfc7eSAndroid Build Coastguard Worker 	hostEntry = gethostbyname(hostName);
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	if (hostEntry == NULL) {
118*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: unknown host passed'%s' \n", progName, hostName);
119*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
120*49cdfc7eSAndroid Build Coastguard Worker 	}
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	printf("%s: sending data to '%s' (IP : %s) \n", progName,
123*49cdfc7eSAndroid Build Coastguard Worker 	       hostEntry->h_name,
124*49cdfc7eSAndroid Build Coastguard Worker 	       inet_ntoa(*(struct in_addr *)hostEntry->h_addr_list[0]));
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 	/* Setup UDP data packets */
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 	udpRemoteServerAddr.sin_family = hostEntry->h_addrtype;
129*49cdfc7eSAndroid Build Coastguard Worker 	memcpy((char *)&udpRemoteServerAddr.sin_addr.s_addr,
130*49cdfc7eSAndroid Build Coastguard Worker 	       hostEntry->h_addr_list[0], hostEntry->h_length);
131*49cdfc7eSAndroid Build Coastguard Worker 	udpRemoteServerAddr.sin_port = htons(LOCAL_UDP_SERVER_PORT);
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 	/* Setup TCP data packets */
134*49cdfc7eSAndroid Build Coastguard Worker 
135*49cdfc7eSAndroid Build Coastguard Worker 	tcpRemoteServerAddr.sin_family = hostEntry->h_addrtype;
136*49cdfc7eSAndroid Build Coastguard Worker 	memcpy((char *)&tcpRemoteServerAddr.sin_addr.s_addr,
137*49cdfc7eSAndroid Build Coastguard Worker 	       hostEntry->h_addr_list[0], hostEntry->h_length);
138*49cdfc7eSAndroid Build Coastguard Worker 	tcpRemoteServerAddr.sin_port = htons(LOCAL_TCP_SERVER_PORT);
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	/* Setup multiCast data packets */
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 	mcastRemoteServerAddr.sin_family = hostEntry->h_addrtype;
143*49cdfc7eSAndroid Build Coastguard Worker 	memcpy((char *)&mcastRemoteServerAddr.sin_addr.s_addr,
144*49cdfc7eSAndroid Build Coastguard Worker 	       hostEntry->h_addr_list[0], hostEntry->h_length);
145*49cdfc7eSAndroid Build Coastguard Worker 	mcastRemoteServerAddr.sin_port = htons(LOCAL_MCAST_SERVER_PORT);
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	/* socket creation */
148*49cdfc7eSAndroid Build Coastguard Worker 	udpSocketHandle = socket(AF_INET, SOCK_DGRAM, 0);
149*49cdfc7eSAndroid Build Coastguard Worker 	tcpSocketHandle = socket(AF_INET, SOCK_STREAM, 0);
150*49cdfc7eSAndroid Build Coastguard Worker 
151*49cdfc7eSAndroid Build Coastguard Worker 	if (udpSocketHandle < 0) {
152*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: Error: cannot open UDP socket \n", progName);
153*49cdfc7eSAndroid Build Coastguard Worker 	}
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker 	if (tcpSocketHandle < 0) {
156*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: Error: cannot open TCP socket \n", progName);
157*49cdfc7eSAndroid Build Coastguard Worker 	}
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker 	/* bind any UDP port */
160*49cdfc7eSAndroid Build Coastguard Worker 	udpClientAddr.sin_family = AF_INET;
161*49cdfc7eSAndroid Build Coastguard Worker 	udpClientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
162*49cdfc7eSAndroid Build Coastguard Worker 	udpClientAddr.sin_port = htons(0);
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	/* bind any TCP port */
165*49cdfc7eSAndroid Build Coastguard Worker 	tcpClientAddr.sin_family = AF_INET;
166*49cdfc7eSAndroid Build Coastguard Worker 	tcpClientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
167*49cdfc7eSAndroid Build Coastguard Worker 	tcpClientAddr.sin_port = htons(0);
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	if (udpSocketHandle > 0) {
170*49cdfc7eSAndroid Build Coastguard Worker 		rc = bind(udpSocketHandle, (struct sockaddr *)&udpClientAddr,
171*49cdfc7eSAndroid Build Coastguard Worker 			  sizeof(udpClientAddr));
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 		if (rc < 0) {
174*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s: Error: cannot bind UDP port\n", progName);
175*49cdfc7eSAndroid Build Coastguard Worker 		}
176*49cdfc7eSAndroid Build Coastguard Worker 	}
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker 	if (tcpSocketHandle > 0) {
179*49cdfc7eSAndroid Build Coastguard Worker 		rc = bind(tcpSocketHandle, (struct sockaddr *)&tcpClientAddr,
180*49cdfc7eSAndroid Build Coastguard Worker 			  sizeof(tcpClientAddr));
181*49cdfc7eSAndroid Build Coastguard Worker 
182*49cdfc7eSAndroid Build Coastguard Worker 		if (rc < 0) {
183*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s: Error: cannot bind TCP port\n", progName);
184*49cdfc7eSAndroid Build Coastguard Worker 		} else {
185*49cdfc7eSAndroid Build Coastguard Worker 			/* connect to server */
186*49cdfc7eSAndroid Build Coastguard Worker 			rc = connect(tcpSocketHandle,
187*49cdfc7eSAndroid Build Coastguard Worker 				     (struct sockaddr *)&tcpRemoteServerAddr,
188*49cdfc7eSAndroid Build Coastguard Worker 				     sizeof(tcpRemoteServerAddr));
189*49cdfc7eSAndroid Build Coastguard Worker 
190*49cdfc7eSAndroid Build Coastguard Worker 			if (rc < 0) {
191*49cdfc7eSAndroid Build Coastguard Worker 				printf
192*49cdfc7eSAndroid Build Coastguard Worker 				    ("Error: cannot connect tp TCP Server \n");
193*49cdfc7eSAndroid Build Coastguard Worker 			}
194*49cdfc7eSAndroid Build Coastguard Worker 		}
195*49cdfc7eSAndroid Build Coastguard Worker 	}
196*49cdfc7eSAndroid Build Coastguard Worker 
197*49cdfc7eSAndroid Build Coastguard Worker 	/* check given address is multicast */
198*49cdfc7eSAndroid Build Coastguard Worker 	if (!IN_MULTICAST(ntohl(mcastRemoteServerAddr.sin_addr.s_addr))) {
199*49cdfc7eSAndroid Build Coastguard Worker 		printf
200*49cdfc7eSAndroid Build Coastguard Worker 		    ("%s : Hostname [%s] passed [%s] is not a multicast server\n",
201*49cdfc7eSAndroid Build Coastguard Worker 		     progName, hostName,
202*49cdfc7eSAndroid Build Coastguard Worker 		     inet_ntoa(mcastRemoteServerAddr.sin_addr));
203*49cdfc7eSAndroid Build Coastguard Worker 		printf("The multiCast Server will not be started \n");
204*49cdfc7eSAndroid Build Coastguard Worker 		multiCast = FALSE;
205*49cdfc7eSAndroid Build Coastguard Worker 	} else {
206*49cdfc7eSAndroid Build Coastguard Worker 		/* create socket */
207*49cdfc7eSAndroid Build Coastguard Worker 		mcastSocketHandle = socket(AF_INET, SOCK_DGRAM, 0);
208*49cdfc7eSAndroid Build Coastguard Worker 		if (mcastSocketHandle < 0) {
209*49cdfc7eSAndroid Build Coastguard Worker 			printf("Error: %s : cannot open mulitCast socket\n",
210*49cdfc7eSAndroid Build Coastguard Worker 			       progName);
211*49cdfc7eSAndroid Build Coastguard Worker 			multiCast = FALSE;
212*49cdfc7eSAndroid Build Coastguard Worker 		}
213*49cdfc7eSAndroid Build Coastguard Worker 
214*49cdfc7eSAndroid Build Coastguard Worker 		/* bind any port number */
215*49cdfc7eSAndroid Build Coastguard Worker 		mcastClientAddr.sin_family = AF_INET;
216*49cdfc7eSAndroid Build Coastguard Worker 		mcastClientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
217*49cdfc7eSAndroid Build Coastguard Worker 		mcastClientAddr.sin_port = htons(0);
218*49cdfc7eSAndroid Build Coastguard Worker 
219*49cdfc7eSAndroid Build Coastguard Worker 		if (bind
220*49cdfc7eSAndroid Build Coastguard Worker 		    (mcastSocketHandle, (struct sockaddr *)&mcastClientAddr,
221*49cdfc7eSAndroid Build Coastguard Worker 		     sizeof(mcastClientAddr)) < 0) {
222*49cdfc7eSAndroid Build Coastguard Worker 			printf("Error: binding multiCast socket");
223*49cdfc7eSAndroid Build Coastguard Worker 			multiCast = FALSE;
224*49cdfc7eSAndroid Build Coastguard Worker 		}
225*49cdfc7eSAndroid Build Coastguard Worker 
226*49cdfc7eSAndroid Build Coastguard Worker 		if (setsockopt
227*49cdfc7eSAndroid Build Coastguard Worker 		    (mcastSocketHandle, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
228*49cdfc7eSAndroid Build Coastguard Worker 		     sizeof(ttl)) < 0) {
229*49cdfc7eSAndroid Build Coastguard Worker 			printf("Error: %s : cannot set ttl = %d \n", progName,
230*49cdfc7eSAndroid Build Coastguard Worker 			       ttl);
231*49cdfc7eSAndroid Build Coastguard Worker 			multiCast = FALSE;
232*49cdfc7eSAndroid Build Coastguard Worker 		}
233*49cdfc7eSAndroid Build Coastguard Worker 
234*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s : sending data on multicast group '%s' (%s)\n",
235*49cdfc7eSAndroid Build Coastguard Worker 		       progName, hostEntry->h_name,
236*49cdfc7eSAndroid Build Coastguard Worker 		       inet_ntoa(*(struct in_addr *)hostEntry->h_addr_list[0]));
237*49cdfc7eSAndroid Build Coastguard Worker 
238*49cdfc7eSAndroid Build Coastguard Worker 	}
239*49cdfc7eSAndroid Build Coastguard Worker 
240*49cdfc7eSAndroid Build Coastguard Worker 	/* Skip over the program and hostnames and just send data */
241*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 3; i < argc; i++) {
242*49cdfc7eSAndroid Build Coastguard Worker 
243*49cdfc7eSAndroid Build Coastguard Worker 		if (udpSocketHandle > 0) {
244*49cdfc7eSAndroid Build Coastguard Worker 			rc = sendto(udpSocketHandle, argv[i],
245*49cdfc7eSAndroid Build Coastguard Worker 				    strlen(argv[i]) + 1, 0,
246*49cdfc7eSAndroid Build Coastguard Worker 				    (struct sockaddr *)&udpRemoteServerAddr,
247*49cdfc7eSAndroid Build Coastguard Worker 				    sizeof(udpRemoteServerAddr));
248*49cdfc7eSAndroid Build Coastguard Worker 
249*49cdfc7eSAndroid Build Coastguard Worker 			if (rc < 0) {
250*49cdfc7eSAndroid Build Coastguard Worker 				printf("%s: cannot send UDP data %d \n",
251*49cdfc7eSAndroid Build Coastguard Worker 				       progName, i - 1);
252*49cdfc7eSAndroid Build Coastguard Worker 				close(udpSocketHandle);
253*49cdfc7eSAndroid Build Coastguard Worker 			} else {
254*49cdfc7eSAndroid Build Coastguard Worker 				printf("%s: UDP data%u sent (%s)\n", progName,
255*49cdfc7eSAndroid Build Coastguard Worker 				       i - 1, argv[i]);
256*49cdfc7eSAndroid Build Coastguard Worker 			}
257*49cdfc7eSAndroid Build Coastguard Worker 		} else {
258*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s UDP Socket not open for send \n", hostName);
259*49cdfc7eSAndroid Build Coastguard Worker 		}
260*49cdfc7eSAndroid Build Coastguard Worker 
261*49cdfc7eSAndroid Build Coastguard Worker 		if (tcpSocketHandle > 0) {
262*49cdfc7eSAndroid Build Coastguard Worker 			rc = send(tcpSocketHandle, argv[i], strlen(argv[i]) + 1,
263*49cdfc7eSAndroid Build Coastguard Worker 				  0);
264*49cdfc7eSAndroid Build Coastguard Worker 
265*49cdfc7eSAndroid Build Coastguard Worker 			if (rc < 0) {
266*49cdfc7eSAndroid Build Coastguard Worker 				printf("cannot send TCP data ");
267*49cdfc7eSAndroid Build Coastguard Worker 				close(tcpSocketHandle);
268*49cdfc7eSAndroid Build Coastguard Worker 
269*49cdfc7eSAndroid Build Coastguard Worker 			} else {
270*49cdfc7eSAndroid Build Coastguard Worker 				printf("%s: TCP data%u sent (%s)\n", progName,
271*49cdfc7eSAndroid Build Coastguard Worker 				       i - 1, argv[i]);
272*49cdfc7eSAndroid Build Coastguard Worker 			}
273*49cdfc7eSAndroid Build Coastguard Worker 		} else {
274*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s TCP Socket not open for send \n", hostName);
275*49cdfc7eSAndroid Build Coastguard Worker 		}
276*49cdfc7eSAndroid Build Coastguard Worker 
277*49cdfc7eSAndroid Build Coastguard Worker 		if (multiCast) {
278*49cdfc7eSAndroid Build Coastguard Worker 			rc = sendto(mcastSocketHandle, argv[i],
279*49cdfc7eSAndroid Build Coastguard Worker 				    strlen(argv[i]) + 1, 0,
280*49cdfc7eSAndroid Build Coastguard Worker 				    (struct sockaddr *)&mcastRemoteServerAddr,
281*49cdfc7eSAndroid Build Coastguard Worker 				    sizeof(mcastRemoteServerAddr));
282*49cdfc7eSAndroid Build Coastguard Worker 
283*49cdfc7eSAndroid Build Coastguard Worker 			if (rc < 0) {
284*49cdfc7eSAndroid Build Coastguard Worker 				printf("%s : cannot send multiCast data %d\n",
285*49cdfc7eSAndroid Build Coastguard Worker 				       progName, i - 1);
286*49cdfc7eSAndroid Build Coastguard Worker 				close(mcastSocketHandle);
287*49cdfc7eSAndroid Build Coastguard Worker 				multiCast = FALSE;
288*49cdfc7eSAndroid Build Coastguard Worker 			}
289*49cdfc7eSAndroid Build Coastguard Worker 		}
290*49cdfc7eSAndroid Build Coastguard Worker 	}
291*49cdfc7eSAndroid Build Coastguard Worker 
292*49cdfc7eSAndroid Build Coastguard Worker 	sleep(5);
293*49cdfc7eSAndroid Build Coastguard Worker 
294*49cdfc7eSAndroid Build Coastguard Worker 	ltp_run_traceroute_tests(traceName);
295*49cdfc7eSAndroid Build Coastguard Worker 
296*49cdfc7eSAndroid Build Coastguard Worker 	ltp_run_ping_tests(hostName);
297*49cdfc7eSAndroid Build Coastguard Worker 
298*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
299*49cdfc7eSAndroid Build Coastguard Worker 
300*49cdfc7eSAndroid Build Coastguard Worker }
301*49cdfc7eSAndroid Build Coastguard Worker 
302*49cdfc7eSAndroid Build Coastguard Worker /*****************************************************************************
303*49cdfc7eSAndroid Build Coastguard Worker * Function: ltp_run_traceroute_tests - host look up and start traceroute processes
304*49cdfc7eSAndroid Build Coastguard Worker *
305*49cdfc7eSAndroid Build Coastguard Worker ******************************************************************************/
ltp_run_traceroute_tests(char * hostName)306*49cdfc7eSAndroid Build Coastguard Worker int ltp_run_traceroute_tests(char *hostName)
307*49cdfc7eSAndroid Build Coastguard Worker {
308*49cdfc7eSAndroid Build Coastguard Worker 
309*49cdfc7eSAndroid Build Coastguard Worker 	struct hostent *hostEntry;
310*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in rawTraceAddr;
311*49cdfc7eSAndroid Build Coastguard Worker 	int pid = -1;
312*49cdfc7eSAndroid Build Coastguard Worker 
313*49cdfc7eSAndroid Build Coastguard Worker 	pid = getpid();
314*49cdfc7eSAndroid Build Coastguard Worker 
315*49cdfc7eSAndroid Build Coastguard Worker 	protocol = getprotobyname("ICMP");
316*49cdfc7eSAndroid Build Coastguard Worker 	hostEntry = gethostbyname(hostName);
317*49cdfc7eSAndroid Build Coastguard Worker 
318*49cdfc7eSAndroid Build Coastguard Worker 	memset(&rawTraceAddr, 0, sizeof(rawTraceAddr));
319*49cdfc7eSAndroid Build Coastguard Worker 
320*49cdfc7eSAndroid Build Coastguard Worker 	rawTraceAddr.sin_family = hostEntry->h_addrtype;
321*49cdfc7eSAndroid Build Coastguard Worker 	rawTraceAddr.sin_port = 0;
322*49cdfc7eSAndroid Build Coastguard Worker 	rawTraceAddr.sin_addr.s_addr = *(long *)hostEntry->h_addr;
323*49cdfc7eSAndroid Build Coastguard Worker 
324*49cdfc7eSAndroid Build Coastguard Worker 	ltp_traceroute(&rawTraceAddr, hostName, pid);
325*49cdfc7eSAndroid Build Coastguard Worker 
326*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
327*49cdfc7eSAndroid Build Coastguard Worker }
328*49cdfc7eSAndroid Build Coastguard Worker 
329*49cdfc7eSAndroid Build Coastguard Worker /**********************************************************************
330*49cdfc7eSAndroid Build Coastguard Worker * Function: ltp_run_ping_tests - host look up and start ping processes
331*49cdfc7eSAndroid Build Coastguard Worker *
332*49cdfc7eSAndroid Build Coastguard Worker ***********************************************************************/
ltp_run_ping_tests(char * hostName)333*49cdfc7eSAndroid Build Coastguard Worker int ltp_run_ping_tests(char *hostName)
334*49cdfc7eSAndroid Build Coastguard Worker {
335*49cdfc7eSAndroid Build Coastguard Worker 
336*49cdfc7eSAndroid Build Coastguard Worker 	struct hostent *hostEntry;
337*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in rawAddr;
338*49cdfc7eSAndroid Build Coastguard Worker 	int pid = -1;
339*49cdfc7eSAndroid Build Coastguard Worker 
340*49cdfc7eSAndroid Build Coastguard Worker 	pid = getpid();
341*49cdfc7eSAndroid Build Coastguard Worker 
342*49cdfc7eSAndroid Build Coastguard Worker 	protocol = getprotobyname("ICMP");
343*49cdfc7eSAndroid Build Coastguard Worker 	hostEntry = gethostbyname(hostName);
344*49cdfc7eSAndroid Build Coastguard Worker 
345*49cdfc7eSAndroid Build Coastguard Worker 	memset(&rawAddr, 0, sizeof(rawAddr));
346*49cdfc7eSAndroid Build Coastguard Worker 
347*49cdfc7eSAndroid Build Coastguard Worker 	rawAddr.sin_family = hostEntry->h_addrtype;
348*49cdfc7eSAndroid Build Coastguard Worker 	rawAddr.sin_port = 0;
349*49cdfc7eSAndroid Build Coastguard Worker 	rawAddr.sin_addr.s_addr = *(long *)hostEntry->h_addr;
350*49cdfc7eSAndroid Build Coastguard Worker 
351*49cdfc7eSAndroid Build Coastguard Worker 	if (fork() == 0) {
352*49cdfc7eSAndroid Build Coastguard Worker 		network_listener(hostName, pid);
353*49cdfc7eSAndroid Build Coastguard Worker 	} else {
354*49cdfc7eSAndroid Build Coastguard Worker 		ping_network(&rawAddr, pid);
355*49cdfc7eSAndroid Build Coastguard Worker 
356*49cdfc7eSAndroid Build Coastguard Worker 	}
357*49cdfc7eSAndroid Build Coastguard Worker 
358*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
359*49cdfc7eSAndroid Build Coastguard Worker }
360*49cdfc7eSAndroid Build Coastguard Worker 
361*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************
362*49cdfc7eSAndroid Build Coastguard Worker * Function: network_listener - separate process to listen for and collect messages
363*49cdfc7eSAndroid Build Coastguard Worker *
364*49cdfc7eSAndroid Build Coastguard Worker *******************************************************************************/
network_listener(char * hostName,int pid)365*49cdfc7eSAndroid Build Coastguard Worker int network_listener(char *hostName, int pid)
366*49cdfc7eSAndroid Build Coastguard Worker {
367*49cdfc7eSAndroid Build Coastguard Worker 
368*49cdfc7eSAndroid Build Coastguard Worker 	int rawSocket, count, value = TIMETOLIVE;
369*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in rawAddr;
370*49cdfc7eSAndroid Build Coastguard Worker 	unsigned char packet[PACKET_LEN];
371*49cdfc7eSAndroid Build Coastguard Worker 
372*49cdfc7eSAndroid Build Coastguard Worker 	rawSocket = socket(PF_INET, SOCK_RAW, protocol->p_proto);
373*49cdfc7eSAndroid Build Coastguard Worker 	count = 0;
374*49cdfc7eSAndroid Build Coastguard Worker 
375*49cdfc7eSAndroid Build Coastguard Worker 	if (rawSocket < 0) {
376*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: Error: cannot open RAW socket \n", hostName);
377*49cdfc7eSAndroid Build Coastguard Worker 		return (NET_ERROR);
378*49cdfc7eSAndroid Build Coastguard Worker 	}
379*49cdfc7eSAndroid Build Coastguard Worker 
380*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {		/* loop forever */
381*49cdfc7eSAndroid Build Coastguard Worker 
382*49cdfc7eSAndroid Build Coastguard Worker 		int bytes;
383*49cdfc7eSAndroid Build Coastguard Worker 		socklen_t len = sizeof(rawAddr);
384*49cdfc7eSAndroid Build Coastguard Worker 
385*49cdfc7eSAndroid Build Coastguard Worker 		memset(packet, 0, sizeof(packet));
386*49cdfc7eSAndroid Build Coastguard Worker 
387*49cdfc7eSAndroid Build Coastguard Worker 		bytes =
388*49cdfc7eSAndroid Build Coastguard Worker 		    recvfrom(rawSocket, packet, sizeof(packet), 0,
389*49cdfc7eSAndroid Build Coastguard Worker 			     (struct sockaddr *)&rawAddr, &len);
390*49cdfc7eSAndroid Build Coastguard Worker 
391*49cdfc7eSAndroid Build Coastguard Worker 		if (bytes > 0)
392*49cdfc7eSAndroid Build Coastguard Worker 			output_to_display(packet, bytes, pid);
393*49cdfc7eSAndroid Build Coastguard Worker 		else {
394*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s : cannot receive data\n", hostName);
395*49cdfc7eSAndroid Build Coastguard Worker 			break;
396*49cdfc7eSAndroid Build Coastguard Worker 		}
397*49cdfc7eSAndroid Build Coastguard Worker 		count++;
398*49cdfc7eSAndroid Build Coastguard Worker 
399*49cdfc7eSAndroid Build Coastguard Worker 		if (value == count) {
400*49cdfc7eSAndroid Build Coastguard Worker 			printf("Exiting the network_listener...\n");
401*49cdfc7eSAndroid Build Coastguard Worker 		}
402*49cdfc7eSAndroid Build Coastguard Worker 	}
403*49cdfc7eSAndroid Build Coastguard Worker 
404*49cdfc7eSAndroid Build Coastguard Worker 	close(rawSocket);
405*49cdfc7eSAndroid Build Coastguard Worker 	return (0);
406*49cdfc7eSAndroid Build Coastguard Worker }
407*49cdfc7eSAndroid Build Coastguard Worker 
408*49cdfc7eSAndroid Build Coastguard Worker /****************************************************************
409*49cdfc7eSAndroid Build Coastguard Worker * Function: checksum - standard 1s complement checksum
410*49cdfc7eSAndroid Build Coastguard Worker *
411*49cdfc7eSAndroid Build Coastguard Worker *****************************************************************/
checksum(void * netPacket,int len)412*49cdfc7eSAndroid Build Coastguard Worker unsigned short checksum(void *netPacket, int len)
413*49cdfc7eSAndroid Build Coastguard Worker {
414*49cdfc7eSAndroid Build Coastguard Worker 
415*49cdfc7eSAndroid Build Coastguard Worker 	unsigned short *packetPtr = netPacket, result;
416*49cdfc7eSAndroid Build Coastguard Worker 
417*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int sum = 0;
418*49cdfc7eSAndroid Build Coastguard Worker 
419*49cdfc7eSAndroid Build Coastguard Worker 	for (sum = 0; len > 1; len -= 2) {
420*49cdfc7eSAndroid Build Coastguard Worker 		sum += *packetPtr++;
421*49cdfc7eSAndroid Build Coastguard Worker 	}
422*49cdfc7eSAndroid Build Coastguard Worker 
423*49cdfc7eSAndroid Build Coastguard Worker 	if (len == 1) {
424*49cdfc7eSAndroid Build Coastguard Worker 		sum += *(unsigned char *)packetPtr;
425*49cdfc7eSAndroid Build Coastguard Worker 	}
426*49cdfc7eSAndroid Build Coastguard Worker 
427*49cdfc7eSAndroid Build Coastguard Worker 	sum = (sum >> 16) + (sum & 0xFFFF);
428*49cdfc7eSAndroid Build Coastguard Worker 	sum += (sum >> 16);
429*49cdfc7eSAndroid Build Coastguard Worker 
430*49cdfc7eSAndroid Build Coastguard Worker 	result = ~sum;
431*49cdfc7eSAndroid Build Coastguard Worker 
432*49cdfc7eSAndroid Build Coastguard Worker 	return result;
433*49cdfc7eSAndroid Build Coastguard Worker }
434*49cdfc7eSAndroid Build Coastguard Worker 
435*49cdfc7eSAndroid Build Coastguard Worker /*****************************************************************
436*49cdfc7eSAndroid Build Coastguard Worker * Function: output_to_display - Output to display info. from the
437*49cdfc7eSAndroid Build Coastguard Worker *                               listener
438*49cdfc7eSAndroid Build Coastguard Worker ******************************************************************/
output_to_display(void * netPacket,int bytes,int pid)439*49cdfc7eSAndroid Build Coastguard Worker void output_to_display(void *netPacket, int bytes, int pid)
440*49cdfc7eSAndroid Build Coastguard Worker {
441*49cdfc7eSAndroid Build Coastguard Worker 
442*49cdfc7eSAndroid Build Coastguard Worker 	int i;
443*49cdfc7eSAndroid Build Coastguard Worker 	struct iphdr *ip = netPacket;
444*49cdfc7eSAndroid Build Coastguard Worker 	struct icmphdr *icmpPtr = netPacket + ip->ihl * 4;
445*49cdfc7eSAndroid Build Coastguard Worker 	struct in_addr tmp_addr;
446*49cdfc7eSAndroid Build Coastguard Worker 
447*49cdfc7eSAndroid Build Coastguard Worker 	printf
448*49cdfc7eSAndroid Build Coastguard Worker 	    ("\n************** -- Ping Tests - **********************************************\n");
449*49cdfc7eSAndroid Build Coastguard Worker 
450*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < bytes; i++) {
451*49cdfc7eSAndroid Build Coastguard Worker 		if (!(i & 15)) {
452*49cdfc7eSAndroid Build Coastguard Worker 			printf("\n[%d]:  ", i);
453*49cdfc7eSAndroid Build Coastguard Worker 		}
454*49cdfc7eSAndroid Build Coastguard Worker 
455*49cdfc7eSAndroid Build Coastguard Worker 		printf("[%d] ", ((unsigned char *)netPacket)[i]);
456*49cdfc7eSAndroid Build Coastguard Worker 	}
457*49cdfc7eSAndroid Build Coastguard Worker 
458*49cdfc7eSAndroid Build Coastguard Worker 	printf("\n");
459*49cdfc7eSAndroid Build Coastguard Worker 
460*49cdfc7eSAndroid Build Coastguard Worker 	tmp_addr.s_addr = ip->saddr;
461*49cdfc7eSAndroid Build Coastguard Worker 
462*49cdfc7eSAndroid Build Coastguard Worker 	printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ",
463*49cdfc7eSAndroid Build Coastguard Worker 	       ip->version, ip->ihl * 4, ntohs(ip->tot_len), ip->protocol,
464*49cdfc7eSAndroid Build Coastguard Worker 	       ip->ttl, inet_ntoa(tmp_addr));
465*49cdfc7eSAndroid Build Coastguard Worker 
466*49cdfc7eSAndroid Build Coastguard Worker 	tmp_addr.s_addr = ip->daddr;
467*49cdfc7eSAndroid Build Coastguard Worker 	printf("dst=%s\n", inet_ntoa(tmp_addr));
468*49cdfc7eSAndroid Build Coastguard Worker 
469*49cdfc7eSAndroid Build Coastguard Worker 	if (icmpPtr->un.echo.id == pid) {
470*49cdfc7eSAndroid Build Coastguard Worker 
471*49cdfc7eSAndroid Build Coastguard Worker 		printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n\n",
472*49cdfc7eSAndroid Build Coastguard Worker 		       icmpPtr->type, icmpPtr->code, ntohs(icmpPtr->checksum),
473*49cdfc7eSAndroid Build Coastguard Worker 		       icmpPtr->un.echo.id, icmpPtr->un.echo.sequence);
474*49cdfc7eSAndroid Build Coastguard Worker 
475*49cdfc7eSAndroid Build Coastguard Worker 	}
476*49cdfc7eSAndroid Build Coastguard Worker }
477*49cdfc7eSAndroid Build Coastguard Worker 
478*49cdfc7eSAndroid Build Coastguard Worker /***********************************************************************
479*49cdfc7eSAndroid Build Coastguard Worker * Function: ping_network - Build a message and send it.
480*49cdfc7eSAndroid Build Coastguard Worker *
481*49cdfc7eSAndroid Build Coastguard Worker *
482*49cdfc7eSAndroid Build Coastguard Worker ***********************************************************************/
ping_network(struct sockaddr_in * rawAddr,int pid)483*49cdfc7eSAndroid Build Coastguard Worker void ping_network(struct sockaddr_in *rawAddr, int pid)
484*49cdfc7eSAndroid Build Coastguard Worker {
485*49cdfc7eSAndroid Build Coastguard Worker 
486*49cdfc7eSAndroid Build Coastguard Worker 	const int value = TIMETOLIVE;
487*49cdfc7eSAndroid Build Coastguard Worker 	int i, rawSocket, count = 1;
488*49cdfc7eSAndroid Build Coastguard Worker 
489*49cdfc7eSAndroid Build Coastguard Worker 	struct packet rawPacket;
490*49cdfc7eSAndroid Build Coastguard Worker 
491*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in r_addr;
492*49cdfc7eSAndroid Build Coastguard Worker 
493*49cdfc7eSAndroid Build Coastguard Worker 	rawSocket = socket(PF_INET, SOCK_RAW, protocol->p_proto);
494*49cdfc7eSAndroid Build Coastguard Worker 
495*49cdfc7eSAndroid Build Coastguard Worker 	if (rawSocket < 0) {
496*49cdfc7eSAndroid Build Coastguard Worker 		printf("Error: cannot open RAW socket %d\n", rawSocket);
497*49cdfc7eSAndroid Build Coastguard Worker 		return;
498*49cdfc7eSAndroid Build Coastguard Worker 	}
499*49cdfc7eSAndroid Build Coastguard Worker 
500*49cdfc7eSAndroid Build Coastguard Worker 	if (setsockopt(rawSocket, SOL_IP, IP_TTL, &value, sizeof(value)) != 0) {
501*49cdfc7eSAndroid Build Coastguard Worker 		printf("ERROR: Setting TimeToLive option");
502*49cdfc7eSAndroid Build Coastguard Worker 	} else {
503*49cdfc7eSAndroid Build Coastguard Worker 		printf
504*49cdfc7eSAndroid Build Coastguard Worker 		    ("The test will run for [%d] iterations -- Ctrl-C to interupt \n",
505*49cdfc7eSAndroid Build Coastguard Worker 		     value);
506*49cdfc7eSAndroid Build Coastguard Worker 		sleep(3);
507*49cdfc7eSAndroid Build Coastguard Worker 	}
508*49cdfc7eSAndroid Build Coastguard Worker 
509*49cdfc7eSAndroid Build Coastguard Worker 	if (fcntl(rawSocket, F_SETFL, O_NONBLOCK) != 0) {
510*49cdfc7eSAndroid Build Coastguard Worker 		printf("ERROR: Failed request nonblocking I/O");
511*49cdfc7eSAndroid Build Coastguard Worker 	}
512*49cdfc7eSAndroid Build Coastguard Worker 
513*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
514*49cdfc7eSAndroid Build Coastguard Worker 
515*49cdfc7eSAndroid Build Coastguard Worker 		socklen_t msgLength = sizeof(r_addr);
516*49cdfc7eSAndroid Build Coastguard Worker 
517*49cdfc7eSAndroid Build Coastguard Worker 		printf("Message ID #:%d \n", count);
518*49cdfc7eSAndroid Build Coastguard Worker 
519*49cdfc7eSAndroid Build Coastguard Worker 		if (recvfrom
520*49cdfc7eSAndroid Build Coastguard Worker 		    (rawSocket, &rawPacket, sizeof(rawPacket), 0,
521*49cdfc7eSAndroid Build Coastguard Worker 		     (struct sockaddr *)&r_addr, &msgLength) > 0) {
522*49cdfc7eSAndroid Build Coastguard Worker 			printf("*** -- Message Received -- ***\n");
523*49cdfc7eSAndroid Build Coastguard Worker 		}
524*49cdfc7eSAndroid Build Coastguard Worker 
525*49cdfc7eSAndroid Build Coastguard Worker 		memset(&rawPacket, 0, sizeof(rawPacket));
526*49cdfc7eSAndroid Build Coastguard Worker 
527*49cdfc7eSAndroid Build Coastguard Worker 		rawPacket.hdr.type = ICMP_ECHO;
528*49cdfc7eSAndroid Build Coastguard Worker 		rawPacket.hdr.un.echo.id = pid;
529*49cdfc7eSAndroid Build Coastguard Worker 
530*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < sizeof(rawPacket.msg) - 1; i++) {
531*49cdfc7eSAndroid Build Coastguard Worker 			rawPacket.msg[i] = i + '0';
532*49cdfc7eSAndroid Build Coastguard Worker 		}
533*49cdfc7eSAndroid Build Coastguard Worker 
534*49cdfc7eSAndroid Build Coastguard Worker 		rawPacket.msg[i] = 0;
535*49cdfc7eSAndroid Build Coastguard Worker 		rawPacket.hdr.un.echo.sequence = count++;
536*49cdfc7eSAndroid Build Coastguard Worker 		rawPacket.hdr.checksum =
537*49cdfc7eSAndroid Build Coastguard Worker 		    checksum(&rawPacket, sizeof(rawPacket));
538*49cdfc7eSAndroid Build Coastguard Worker 
539*49cdfc7eSAndroid Build Coastguard Worker 		if (sendto
540*49cdfc7eSAndroid Build Coastguard Worker 		    (rawSocket, &rawPacket, sizeof(rawPacket), 0,
541*49cdfc7eSAndroid Build Coastguard Worker 		     (struct sockaddr *)rawAddr, sizeof(*rawAddr)) <= 0)
542*49cdfc7eSAndroid Build Coastguard Worker 			printf("ERROR: sendto failed !!");
543*49cdfc7eSAndroid Build Coastguard Worker 
544*49cdfc7eSAndroid Build Coastguard Worker 		sleep(1);
545*49cdfc7eSAndroid Build Coastguard Worker 
546*49cdfc7eSAndroid Build Coastguard Worker 		if (value == count) {
547*49cdfc7eSAndroid Build Coastguard Worker 			printf("Exiting ping test...\n");
548*49cdfc7eSAndroid Build Coastguard Worker 			break;
549*49cdfc7eSAndroid Build Coastguard Worker 		}
550*49cdfc7eSAndroid Build Coastguard Worker 	}
551*49cdfc7eSAndroid Build Coastguard Worker 
552*49cdfc7eSAndroid Build Coastguard Worker 	close(rawSocket);
553*49cdfc7eSAndroid Build Coastguard Worker }
554*49cdfc7eSAndroid Build Coastguard Worker 
555*49cdfc7eSAndroid Build Coastguard Worker /**********************************************************************
556*49cdfc7eSAndroid Build Coastguard Worker *  Function: ltp_traceroute
557*49cdfc7eSAndroid Build Coastguard Worker *                      try to reach the destination
558*49cdfc7eSAndroid Build Coastguard Worker *                      while outputting hops along the route
559*49cdfc7eSAndroid Build Coastguard Worker ***********************************************************************/
ltp_traceroute(struct sockaddr_in * rawTraceAddr,char * hostName,int pid)560*49cdfc7eSAndroid Build Coastguard Worker void ltp_traceroute(struct sockaddr_in *rawTraceAddr, char *hostName, int pid)
561*49cdfc7eSAndroid Build Coastguard Worker {
562*49cdfc7eSAndroid Build Coastguard Worker 
563*49cdfc7eSAndroid Build Coastguard Worker 	const int flag = TRUE;
564*49cdfc7eSAndroid Build Coastguard Worker 	int TimeToLive = 0;
565*49cdfc7eSAndroid Build Coastguard Worker 	int i, rawTraceSocket, count = 1;
566*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t length;
567*49cdfc7eSAndroid Build Coastguard Worker 	struct packet rawTracePacket;
568*49cdfc7eSAndroid Build Coastguard Worker 	unsigned char tracePacket[PACKET_LEN];
569*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in rawReceiveAddr;
570*49cdfc7eSAndroid Build Coastguard Worker 	struct hostent *hostEntry2;
571*49cdfc7eSAndroid Build Coastguard Worker 	struct in_addr tmp_addr;
572*49cdfc7eSAndroid Build Coastguard Worker 
573*49cdfc7eSAndroid Build Coastguard Worker 	printf
574*49cdfc7eSAndroid Build Coastguard Worker 	    ("\n************** -- Trace Route Tests - **********************************************\n");
575*49cdfc7eSAndroid Build Coastguard Worker 
576*49cdfc7eSAndroid Build Coastguard Worker 	rawTraceSocket = socket(PF_INET, SOCK_RAW, protocol->p_proto);
577*49cdfc7eSAndroid Build Coastguard Worker 
578*49cdfc7eSAndroid Build Coastguard Worker 	if (rawTraceSocket < 0) {
579*49cdfc7eSAndroid Build Coastguard Worker 		printf("Error: cannot open RAW socket %d\n", rawTraceSocket);
580*49cdfc7eSAndroid Build Coastguard Worker 		return;
581*49cdfc7eSAndroid Build Coastguard Worker 	}
582*49cdfc7eSAndroid Build Coastguard Worker 
583*49cdfc7eSAndroid Build Coastguard Worker 	if (setsockopt(rawTraceSocket, SOL_IP, SO_ERROR, &flag, sizeof(flag)) !=
584*49cdfc7eSAndroid Build Coastguard Worker 	    0)
585*49cdfc7eSAndroid Build Coastguard Worker 		printf("ERROR: Setting socket options");
586*49cdfc7eSAndroid Build Coastguard Worker 
587*49cdfc7eSAndroid Build Coastguard Worker 	do {
588*49cdfc7eSAndroid Build Coastguard Worker 		struct iphdr *ip;
589*49cdfc7eSAndroid Build Coastguard Worker 		length = sizeof(rawReceiveAddr);
590*49cdfc7eSAndroid Build Coastguard Worker 
591*49cdfc7eSAndroid Build Coastguard Worker 		TimeToLive++;
592*49cdfc7eSAndroid Build Coastguard Worker 		if (setsockopt
593*49cdfc7eSAndroid Build Coastguard Worker 		    (rawTraceSocket, SOL_IP, IP_TTL, &TimeToLive,
594*49cdfc7eSAndroid Build Coastguard Worker 		     sizeof(TimeToLive)) != 0) {
595*49cdfc7eSAndroid Build Coastguard Worker 			printf("ERROR: Setting TimeToLive option");
596*49cdfc7eSAndroid Build Coastguard Worker 		}
597*49cdfc7eSAndroid Build Coastguard Worker 
598*49cdfc7eSAndroid Build Coastguard Worker 		memset(&rawTracePacket, 0, sizeof(rawTracePacket));
599*49cdfc7eSAndroid Build Coastguard Worker 
600*49cdfc7eSAndroid Build Coastguard Worker 		rawTracePacket.hdr.type = ICMP_ECHO;
601*49cdfc7eSAndroid Build Coastguard Worker 		rawTracePacket.hdr.un.echo.id = pid;
602*49cdfc7eSAndroid Build Coastguard Worker 
603*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < sizeof(rawTracePacket.msg) - 1; i++) {
604*49cdfc7eSAndroid Build Coastguard Worker 			rawTracePacket.msg[i] = i + '0';
605*49cdfc7eSAndroid Build Coastguard Worker 		}
606*49cdfc7eSAndroid Build Coastguard Worker 
607*49cdfc7eSAndroid Build Coastguard Worker 		rawTracePacket.msg[i] = 0;
608*49cdfc7eSAndroid Build Coastguard Worker 		rawTracePacket.hdr.un.echo.sequence = count++;
609*49cdfc7eSAndroid Build Coastguard Worker 		rawTracePacket.hdr.checksum =
610*49cdfc7eSAndroid Build Coastguard Worker 		    checksum(&rawTracePacket, sizeof(rawTracePacket));
611*49cdfc7eSAndroid Build Coastguard Worker 
612*49cdfc7eSAndroid Build Coastguard Worker 		if (sendto
613*49cdfc7eSAndroid Build Coastguard Worker 		    (rawTraceSocket, &rawTracePacket, sizeof(rawTracePacket), 0,
614*49cdfc7eSAndroid Build Coastguard Worker 		     (struct sockaddr *)rawTraceAddr,
615*49cdfc7eSAndroid Build Coastguard Worker 		     sizeof(*rawTraceAddr)) <= 0) {
616*49cdfc7eSAndroid Build Coastguard Worker 			printf("ERROR: sendto failed !!");
617*49cdfc7eSAndroid Build Coastguard Worker 		}
618*49cdfc7eSAndroid Build Coastguard Worker 		sleep(1);
619*49cdfc7eSAndroid Build Coastguard Worker 
620*49cdfc7eSAndroid Build Coastguard Worker 		if (recvfrom
621*49cdfc7eSAndroid Build Coastguard Worker 		    (rawTraceSocket, tracePacket, sizeof(tracePacket),
622*49cdfc7eSAndroid Build Coastguard Worker 		     MSG_DONTWAIT, (struct sockaddr *)&rawReceiveAddr,
623*49cdfc7eSAndroid Build Coastguard Worker 		     &length) > 0) {
624*49cdfc7eSAndroid Build Coastguard Worker 			ip = (void *)tracePacket;
625*49cdfc7eSAndroid Build Coastguard Worker 
626*49cdfc7eSAndroid Build Coastguard Worker 			tmp_addr.s_addr = ip->saddr;
627*49cdfc7eSAndroid Build Coastguard Worker 			printf("Host IP:#%d: %s \n", count - 1,
628*49cdfc7eSAndroid Build Coastguard Worker 			       inet_ntoa(tmp_addr));
629*49cdfc7eSAndroid Build Coastguard Worker 
630*49cdfc7eSAndroid Build Coastguard Worker 			hostEntry2 =
631*49cdfc7eSAndroid Build Coastguard Worker 			    gethostbyaddr((void *)&rawReceiveAddr, length,
632*49cdfc7eSAndroid Build Coastguard Worker 					  rawReceiveAddr.sin_family);
633*49cdfc7eSAndroid Build Coastguard Worker 
634*49cdfc7eSAndroid Build Coastguard Worker 			if (hostEntry2 != NULL)
635*49cdfc7eSAndroid Build Coastguard Worker 				printf("(%s)\n", hostEntry2->h_name);
636*49cdfc7eSAndroid Build Coastguard Worker 			else
637*49cdfc7eSAndroid Build Coastguard Worker 				perror("Name: ");
638*49cdfc7eSAndroid Build Coastguard Worker 		} else {
639*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s : data send complete...\n", hostName);
640*49cdfc7eSAndroid Build Coastguard Worker 			break;
641*49cdfc7eSAndroid Build Coastguard Worker 		}
642*49cdfc7eSAndroid Build Coastguard Worker 
643*49cdfc7eSAndroid Build Coastguard Worker 	}
644*49cdfc7eSAndroid Build Coastguard Worker 	while (rawReceiveAddr.sin_addr.s_addr != rawTraceAddr->sin_addr.s_addr);
645*49cdfc7eSAndroid Build Coastguard Worker 
646*49cdfc7eSAndroid Build Coastguard Worker 	printf
647*49cdfc7eSAndroid Build Coastguard Worker 	    ("\n************** -- End Trace Route Tests - ******************************************\n");
648*49cdfc7eSAndroid Build Coastguard Worker 
649*49cdfc7eSAndroid Build Coastguard Worker 	close(rawTraceSocket);
650*49cdfc7eSAndroid Build Coastguard Worker }
651