xref: /aosp_15_r20/external/ltp/testcases/network/sockets/ltpServer.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  * ltpServer.c
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * LTP Network Socket Test Server
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  */
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <arpa/inet.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <netdb.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker #define LOCAL_UDP_SERVER_PORT   10000
42*49cdfc7eSAndroid Build Coastguard Worker #define LOCAL_TCP_SERVER_PORT   10001
43*49cdfc7eSAndroid Build Coastguard Worker #define LOCAL_MULTI_SERVER_PORT 10002
44*49cdfc7eSAndroid Build Coastguard Worker #define MAX_MSG_LEN             256
45*49cdfc7eSAndroid Build Coastguard Worker #define MAX_HOSTNAME_LEN        256
46*49cdfc7eSAndroid Build Coastguard Worker #define ERROR -1
47*49cdfc7eSAndroid Build Coastguard Worker #define END_LINE                0x0A
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker int udpSocketHandle,
50*49cdfc7eSAndroid Build Coastguard Worker     rc, msg_bytes, tcpSocketHandle, newTcpSocketHandle, multiSocketHandle;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker socklen_t udpClientLen, tcpClientLen, multiClientLen;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in udpClientAddr,
55*49cdfc7eSAndroid Build Coastguard Worker     udpServerAddr,
56*49cdfc7eSAndroid Build Coastguard Worker     tcpClientAddr, tcpServerAddr, multiClientAddr, multiServerAddr;
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker struct ip_mreq multiCastReq;
59*49cdfc7eSAndroid Build Coastguard Worker struct in_addr multiCastAddr;
60*49cdfc7eSAndroid Build Coastguard Worker struct hostent *hostEntry;
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker char message[MAX_MSG_LEN];
63*49cdfc7eSAndroid Build Coastguard Worker char hostname[MAX_HOSTNAME_LEN];
64*49cdfc7eSAndroid Build Coastguard Worker char ServerProg[MAX_HOSTNAME_LEN];
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker void *ltp_udp_server_queue(void *);
67*49cdfc7eSAndroid Build Coastguard Worker void *ltp_tcp_server_queue(void *);
68*49cdfc7eSAndroid Build Coastguard Worker void *ltp_multi_server_queue(void *);
69*49cdfc7eSAndroid Build Coastguard Worker int tcp_receive_buffer(int, char *);
70*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])71*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t udp_server_queue, tcp_server_queue, multi_server_queue;
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	pthread_attr_t udpthread_attr, tcpthread_attr, multithread_attr;
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	if (argc != 2) {
79*49cdfc7eSAndroid Build Coastguard Worker 		printf
80*49cdfc7eSAndroid Build Coastguard Worker 		    ("Server arguments : %s <multiCast I.P.address/hostname>\n",
81*49cdfc7eSAndroid Build Coastguard Worker 		     argv[0]);
82*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
83*49cdfc7eSAndroid Build Coastguard Worker 	}
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	strncpy(hostname, argv[1], 255);
86*49cdfc7eSAndroid Build Coastguard Worker 	strncpy(ServerProg, argv[0], 255);
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	/* get mcast address to listen to */
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	hostEntry = gethostbyname(argv[1]);
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	if (hostEntry == NULL) {
93*49cdfc7eSAndroid Build Coastguard Worker 		printf("Server %s : You need to pass a multiCast group '%s'\n",
94*49cdfc7eSAndroid Build Coastguard Worker 		       argv[0], argv[1]);
95*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
96*49cdfc7eSAndroid Build Coastguard Worker 	}
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	memcpy(&multiCastAddr, hostEntry->h_addr_list[0], hostEntry->h_length);
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	/* check given address is multicast */
101*49cdfc7eSAndroid Build Coastguard Worker 	if (!IN_MULTICAST(ntohl(multiCastAddr.s_addr))) {
102*49cdfc7eSAndroid Build Coastguard Worker 		printf
103*49cdfc7eSAndroid Build Coastguard Worker 		    ("%s : Hostname [%s] passed [%s] is not a multicast server\n",
104*49cdfc7eSAndroid Build Coastguard Worker 		     argv[0], hostname, inet_ntoa(multiCastAddr));
105*49cdfc7eSAndroid Build Coastguard Worker 		printf("The multiCast Server will not be started \n");
106*49cdfc7eSAndroid Build Coastguard Worker 	} else {
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 		/* create multiCast socket */
109*49cdfc7eSAndroid Build Coastguard Worker 		multiSocketHandle = socket(AF_INET, SOCK_DGRAM, 0);
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 		if (multiSocketHandle < 0) {
112*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s : cannot create multiCast socket\n",
113*49cdfc7eSAndroid Build Coastguard Worker 			       argv[0]);
114*49cdfc7eSAndroid Build Coastguard Worker 		} else {
115*49cdfc7eSAndroid Build Coastguard Worker 			/* bind multiCast port */
116*49cdfc7eSAndroid Build Coastguard Worker 			multiServerAddr.sin_family = AF_INET;
117*49cdfc7eSAndroid Build Coastguard Worker 			multiServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
118*49cdfc7eSAndroid Build Coastguard Worker 			multiServerAddr.sin_port =
119*49cdfc7eSAndroid Build Coastguard Worker 			    htons(LOCAL_MULTI_SERVER_PORT);
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 			if (bind
122*49cdfc7eSAndroid Build Coastguard Worker 			    (multiSocketHandle,
123*49cdfc7eSAndroid Build Coastguard Worker 			     (struct sockaddr *)&multiServerAddr,
124*49cdfc7eSAndroid Build Coastguard Worker 			     sizeof(multiServerAddr)) < 0) {
125*49cdfc7eSAndroid Build Coastguard Worker 				printf("%s : cannot bind Multicast port %d \n",
126*49cdfc7eSAndroid Build Coastguard Worker 				       argv[0], LOCAL_MULTI_SERVER_PORT);
127*49cdfc7eSAndroid Build Coastguard Worker 			} else {
128*49cdfc7eSAndroid Build Coastguard Worker 				/* join multicast group */
129*49cdfc7eSAndroid Build Coastguard Worker 				multiCastReq.imr_multiaddr.s_addr =
130*49cdfc7eSAndroid Build Coastguard Worker 				    multiCastAddr.s_addr;
131*49cdfc7eSAndroid Build Coastguard Worker 				multiCastReq.imr_interface.s_addr =
132*49cdfc7eSAndroid Build Coastguard Worker 				    htonl(INADDR_ANY);
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 				rc = setsockopt(multiSocketHandle, IPPROTO_IP,
135*49cdfc7eSAndroid Build Coastguard Worker 						IP_ADD_MEMBERSHIP,
136*49cdfc7eSAndroid Build Coastguard Worker 						(void *)&multiCastReq,
137*49cdfc7eSAndroid Build Coastguard Worker 						sizeof(multiCastReq));
138*49cdfc7eSAndroid Build Coastguard Worker 				if (rc < 0) {
139*49cdfc7eSAndroid Build Coastguard Worker 					printf
140*49cdfc7eSAndroid Build Coastguard Worker 					    ("%s : cannot join multicast group '%s'",
141*49cdfc7eSAndroid Build Coastguard Worker 					     argv[0], inet_ntoa(multiCastAddr));
142*49cdfc7eSAndroid Build Coastguard Worker 				} else {
143*49cdfc7eSAndroid Build Coastguard Worker 					printf
144*49cdfc7eSAndroid Build Coastguard Worker 					    ("%s : listening to mgroup %s:%d\n",
145*49cdfc7eSAndroid Build Coastguard Worker 					     argv[0], inet_ntoa(multiCastAddr),
146*49cdfc7eSAndroid Build Coastguard Worker 					     LOCAL_MULTI_SERVER_PORT);
147*49cdfc7eSAndroid Build Coastguard Worker 				}
148*49cdfc7eSAndroid Build Coastguard Worker 			}
149*49cdfc7eSAndroid Build Coastguard Worker 		}
150*49cdfc7eSAndroid Build Coastguard Worker 
151*49cdfc7eSAndroid Build Coastguard Worker 		rc = pthread_attr_init(&multithread_attr);
152*49cdfc7eSAndroid Build Coastguard Worker 		rc = pthread_create(&multi_server_queue, &multithread_attr,
153*49cdfc7eSAndroid Build Coastguard Worker 				    ltp_multi_server_queue, NULL);
154*49cdfc7eSAndroid Build Coastguard Worker 	}
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker 	/* udp socket creation */
157*49cdfc7eSAndroid Build Coastguard Worker 	udpSocketHandle = socket(AF_INET, SOCK_DGRAM, 0);
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker 	if (udpSocketHandle < 0) {
160*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: cannot open socket \n", argv[0]);
161*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
162*49cdfc7eSAndroid Build Coastguard Worker 	}
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	/* tcp socket creation */
165*49cdfc7eSAndroid Build Coastguard Worker 	tcpSocketHandle = socket(AF_INET, SOCK_STREAM, 0);
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 	if (tcpSocketHandle < 0) {
168*49cdfc7eSAndroid Build Coastguard Worker 		printf("Error: cannot open socket %d \n", tcpSocketHandle);
169*49cdfc7eSAndroid Build Coastguard Worker 		return ERROR;
170*49cdfc7eSAndroid Build Coastguard Worker 	}
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker 	/* bind local udp server port */
173*49cdfc7eSAndroid Build Coastguard Worker 	udpServerAddr.sin_family = AF_INET;
174*49cdfc7eSAndroid Build Coastguard Worker 	udpServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
175*49cdfc7eSAndroid Build Coastguard Worker 	udpServerAddr.sin_port = htons(LOCAL_UDP_SERVER_PORT);
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker 	rc = bind(udpSocketHandle, (struct sockaddr *)&udpServerAddr,
178*49cdfc7eSAndroid Build Coastguard Worker 		  sizeof(udpServerAddr));
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 	if (rc < 0) {
181*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: Error binding port number %d \n",
182*49cdfc7eSAndroid Build Coastguard Worker 		       argv[0], LOCAL_UDP_SERVER_PORT);
183*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
184*49cdfc7eSAndroid Build Coastguard Worker 	} else {
185*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: bound port number %d \n",
186*49cdfc7eSAndroid Build Coastguard Worker 		       argv[0], LOCAL_UDP_SERVER_PORT);
187*49cdfc7eSAndroid Build Coastguard Worker 	}
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker 	/* bind local tcp server port */
190*49cdfc7eSAndroid Build Coastguard Worker 	tcpServerAddr.sin_family = AF_INET;
191*49cdfc7eSAndroid Build Coastguard Worker 	tcpServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
192*49cdfc7eSAndroid Build Coastguard Worker 	tcpServerAddr.sin_port = htons(LOCAL_TCP_SERVER_PORT);
193*49cdfc7eSAndroid Build Coastguard Worker 
194*49cdfc7eSAndroid Build Coastguard Worker 	rc = bind(tcpSocketHandle, (struct sockaddr *)&tcpServerAddr,
195*49cdfc7eSAndroid Build Coastguard Worker 		  sizeof(tcpServerAddr));
196*49cdfc7eSAndroid Build Coastguard Worker 
197*49cdfc7eSAndroid Build Coastguard Worker 	if (rc < 0) {
198*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: Error binding port number %d \n",
199*49cdfc7eSAndroid Build Coastguard Worker 		       argv[0], LOCAL_TCP_SERVER_PORT);
200*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
201*49cdfc7eSAndroid Build Coastguard Worker 	} else {
202*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: bound port number %d \n",
203*49cdfc7eSAndroid Build Coastguard Worker 		       argv[0], LOCAL_TCP_SERVER_PORT);
204*49cdfc7eSAndroid Build Coastguard Worker 	}
205*49cdfc7eSAndroid Build Coastguard Worker 
206*49cdfc7eSAndroid Build Coastguard Worker 	rc = pthread_attr_init(&udpthread_attr);
207*49cdfc7eSAndroid Build Coastguard Worker 	rc = pthread_create(&udp_server_queue, &udpthread_attr,
208*49cdfc7eSAndroid Build Coastguard Worker 			    ltp_udp_server_queue, NULL);
209*49cdfc7eSAndroid Build Coastguard Worker 
210*49cdfc7eSAndroid Build Coastguard Worker 	rc = pthread_attr_init(&tcpthread_attr);
211*49cdfc7eSAndroid Build Coastguard Worker 	rc = pthread_create(&tcp_server_queue, &tcpthread_attr,
212*49cdfc7eSAndroid Build Coastguard Worker 			    ltp_tcp_server_queue, NULL);
213*49cdfc7eSAndroid Build Coastguard Worker 
214*49cdfc7eSAndroid Build Coastguard Worker 	while (1) ;
215*49cdfc7eSAndroid Build Coastguard Worker 
216*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
217*49cdfc7eSAndroid Build Coastguard Worker }
218*49cdfc7eSAndroid Build Coastguard Worker 
219*49cdfc7eSAndroid Build Coastguard Worker /*
220*49cdfc7eSAndroid Build Coastguard Worker  * Function:     ltp_udp_server_queue
221*49cdfc7eSAndroid Build Coastguard Worker  * Description:  This function grabs the udp message from the queue and outputs to stdio
222*49cdfc7eSAndroid Build Coastguard Worker  */
ltp_udp_server_queue(void * junk)223*49cdfc7eSAndroid Build Coastguard Worker void *ltp_udp_server_queue(void *junk)
224*49cdfc7eSAndroid Build Coastguard Worker {
225*49cdfc7eSAndroid Build Coastguard Worker 
226*49cdfc7eSAndroid Build Coastguard Worker 	printf("%s: waiting for data on port UDP %u\n",
227*49cdfc7eSAndroid Build Coastguard Worker 	       hostname, LOCAL_UDP_SERVER_PORT);
228*49cdfc7eSAndroid Build Coastguard Worker 
229*49cdfc7eSAndroid Build Coastguard Worker 	/* server infinite loop */
230*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
231*49cdfc7eSAndroid Build Coastguard Worker 
232*49cdfc7eSAndroid Build Coastguard Worker 		/* init buffer */
233*49cdfc7eSAndroid Build Coastguard Worker 		memset(message, 0, MAX_MSG_LEN);
234*49cdfc7eSAndroid Build Coastguard Worker 
235*49cdfc7eSAndroid Build Coastguard Worker 		/* receive message */
236*49cdfc7eSAndroid Build Coastguard Worker 		udpClientLen = sizeof(udpClientAddr);
237*49cdfc7eSAndroid Build Coastguard Worker 
238*49cdfc7eSAndroid Build Coastguard Worker 		msg_bytes =
239*49cdfc7eSAndroid Build Coastguard Worker 		    recvfrom(udpSocketHandle, message, MAX_MSG_LEN, 0,
240*49cdfc7eSAndroid Build Coastguard Worker 			     (struct sockaddr *)&udpClientAddr, &udpClientLen);
241*49cdfc7eSAndroid Build Coastguard Worker 
242*49cdfc7eSAndroid Build Coastguard Worker 		printf("msg_bytes:%d \n", msg_bytes);
243*49cdfc7eSAndroid Build Coastguard Worker 
244*49cdfc7eSAndroid Build Coastguard Worker 		if (msg_bytes < 0) {
245*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s: Error receiving data \n", hostname);
246*49cdfc7eSAndroid Build Coastguard Worker 		} else {
247*49cdfc7eSAndroid Build Coastguard Worker 			/* print message */
248*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s: from %s:UDP%u : %s \n",
249*49cdfc7eSAndroid Build Coastguard Worker 			       hostname, inet_ntoa(udpClientAddr.sin_addr),
250*49cdfc7eSAndroid Build Coastguard Worker 			       ntohs(udpClientAddr.sin_port), message);
251*49cdfc7eSAndroid Build Coastguard Worker 		}
252*49cdfc7eSAndroid Build Coastguard Worker 
253*49cdfc7eSAndroid Build Coastguard Worker 	}			/* end of server infinite loop */
254*49cdfc7eSAndroid Build Coastguard Worker 
255*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
256*49cdfc7eSAndroid Build Coastguard Worker 
257*49cdfc7eSAndroid Build Coastguard Worker }
258*49cdfc7eSAndroid Build Coastguard Worker 
259*49cdfc7eSAndroid Build Coastguard Worker /*
260*49cdfc7eSAndroid Build Coastguard Worker  * Function:     ltp_tcp_server_queue
261*49cdfc7eSAndroid Build Coastguard Worker  * Description:  This function grabs the tcp message from the queue and outputs to stdio
262*49cdfc7eSAndroid Build Coastguard Worker  */
ltp_tcp_server_queue(void * junk)263*49cdfc7eSAndroid Build Coastguard Worker void *ltp_tcp_server_queue(void *junk)
264*49cdfc7eSAndroid Build Coastguard Worker {
265*49cdfc7eSAndroid Build Coastguard Worker 
266*49cdfc7eSAndroid Build Coastguard Worker 	listen(tcpSocketHandle, 5);
267*49cdfc7eSAndroid Build Coastguard Worker 
268*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
269*49cdfc7eSAndroid Build Coastguard Worker 
270*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s: waiting for data on port TCP %u\n", hostname,
271*49cdfc7eSAndroid Build Coastguard Worker 		       LOCAL_TCP_SERVER_PORT);
272*49cdfc7eSAndroid Build Coastguard Worker 
273*49cdfc7eSAndroid Build Coastguard Worker 		tcpClientLen = sizeof(tcpClientAddr);
274*49cdfc7eSAndroid Build Coastguard Worker 		newTcpSocketHandle =
275*49cdfc7eSAndroid Build Coastguard Worker 		    accept(tcpSocketHandle, (struct sockaddr *)&tcpClientAddr,
276*49cdfc7eSAndroid Build Coastguard Worker 			   &tcpClientLen);
277*49cdfc7eSAndroid Build Coastguard Worker 
278*49cdfc7eSAndroid Build Coastguard Worker 		if (newTcpSocketHandle < 0) {
279*49cdfc7eSAndroid Build Coastguard Worker 			printf("cannot accept TCP connection ");
280*49cdfc7eSAndroid Build Coastguard Worker 			break;
281*49cdfc7eSAndroid Build Coastguard Worker 		}
282*49cdfc7eSAndroid Build Coastguard Worker 
283*49cdfc7eSAndroid Build Coastguard Worker 		/* init line */
284*49cdfc7eSAndroid Build Coastguard Worker 		memset(message, 0x0, MAX_MSG_LEN);
285*49cdfc7eSAndroid Build Coastguard Worker 
286*49cdfc7eSAndroid Build Coastguard Worker 		/* receive segments */
287*49cdfc7eSAndroid Build Coastguard Worker 		while (tcp_receive_buffer(newTcpSocketHandle, message) != ERROR) {
288*49cdfc7eSAndroid Build Coastguard Worker 
289*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s: received from %s:TCP%d : %s\n", hostname,
290*49cdfc7eSAndroid Build Coastguard Worker 			       inet_ntoa(tcpClientAddr.sin_addr),
291*49cdfc7eSAndroid Build Coastguard Worker 			       ntohs(tcpClientAddr.sin_port), message);
292*49cdfc7eSAndroid Build Coastguard Worker 
293*49cdfc7eSAndroid Build Coastguard Worker 			/* init line */
294*49cdfc7eSAndroid Build Coastguard Worker 			memset(message, 0x0, MAX_MSG_LEN);
295*49cdfc7eSAndroid Build Coastguard Worker 
296*49cdfc7eSAndroid Build Coastguard Worker 		}		/* while (read_line) */
297*49cdfc7eSAndroid Build Coastguard Worker 		printf("looping in TCP \n");
298*49cdfc7eSAndroid Build Coastguard Worker 
299*49cdfc7eSAndroid Build Coastguard Worker 	}			/* while (1) */
300*49cdfc7eSAndroid Build Coastguard Worker 
301*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
302*49cdfc7eSAndroid Build Coastguard Worker 
303*49cdfc7eSAndroid Build Coastguard Worker }
304*49cdfc7eSAndroid Build Coastguard Worker 
305*49cdfc7eSAndroid Build Coastguard Worker /*
306*49cdfc7eSAndroid Build Coastguard Worker  * Function:     tcp_receive_buffer
307*49cdfc7eSAndroid Build Coastguard Worker  * Description:  This function grabs the message from the tcp queue and
308*49cdfc7eSAndroid Build Coastguard Worker  *               returns it to the calling function in the buffer.
309*49cdfc7eSAndroid Build Coastguard Worker  */
tcp_receive_buffer(int newSocket,char * return_buffer)310*49cdfc7eSAndroid Build Coastguard Worker int tcp_receive_buffer(int newSocket, char *return_buffer)
311*49cdfc7eSAndroid Build Coastguard Worker {
312*49cdfc7eSAndroid Build Coastguard Worker 
313*49cdfc7eSAndroid Build Coastguard Worker 	static int bytes_received = 0;
314*49cdfc7eSAndroid Build Coastguard Worker 	static char message_received[MAX_MSG_LEN];
315*49cdfc7eSAndroid Build Coastguard Worker 	static int count = 0;
316*49cdfc7eSAndroid Build Coastguard Worker 	int offset;
317*49cdfc7eSAndroid Build Coastguard Worker 
318*49cdfc7eSAndroid Build Coastguard Worker 	offset = 0;
319*49cdfc7eSAndroid Build Coastguard Worker 
320*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
321*49cdfc7eSAndroid Build Coastguard Worker 
322*49cdfc7eSAndroid Build Coastguard Worker 		if (bytes_received == 0) {
323*49cdfc7eSAndroid Build Coastguard Worker 			/* read data from socket */
324*49cdfc7eSAndroid Build Coastguard Worker 
325*49cdfc7eSAndroid Build Coastguard Worker 			memset(message_received, 0x0, MAX_MSG_LEN);	/* init buffer */
326*49cdfc7eSAndroid Build Coastguard Worker 
327*49cdfc7eSAndroid Build Coastguard Worker 			count =
328*49cdfc7eSAndroid Build Coastguard Worker 			    recvfrom(newSocket, message_received, MAX_MSG_LEN,
329*49cdfc7eSAndroid Build Coastguard Worker 				     0, (struct sockaddr *)&tcpClientAddr,
330*49cdfc7eSAndroid Build Coastguard Worker 				     &tcpClientLen);
331*49cdfc7eSAndroid Build Coastguard Worker 
332*49cdfc7eSAndroid Build Coastguard Worker 			if (count < 0) {
333*49cdfc7eSAndroid Build Coastguard Worker 				perror(" cannot receive data ");
334*49cdfc7eSAndroid Build Coastguard Worker 				return ERROR;
335*49cdfc7eSAndroid Build Coastguard Worker 			} else if (count == 0) {
336*49cdfc7eSAndroid Build Coastguard Worker 				printf(" connection closed by client\n");
337*49cdfc7eSAndroid Build Coastguard Worker 				close(newSocket);
338*49cdfc7eSAndroid Build Coastguard Worker 				if (count) {
339*49cdfc7eSAndroid Build Coastguard Worker 				}
340*49cdfc7eSAndroid Build Coastguard Worker 				return ERROR;
341*49cdfc7eSAndroid Build Coastguard Worker 			}
342*49cdfc7eSAndroid Build Coastguard Worker 		}
343*49cdfc7eSAndroid Build Coastguard Worker 
344*49cdfc7eSAndroid Build Coastguard Worker 		/* Check for new data read on socket or */
345*49cdfc7eSAndroid Build Coastguard Worker 		/* if still more data in buffer       */
346*49cdfc7eSAndroid Build Coastguard Worker 
347*49cdfc7eSAndroid Build Coastguard Worker 		/* copy line into 'return_buffer' */
348*49cdfc7eSAndroid Build Coastguard Worker 		while (*(message_received + bytes_received) != END_LINE
349*49cdfc7eSAndroid Build Coastguard Worker 		       && bytes_received < count) {
350*49cdfc7eSAndroid Build Coastguard Worker 			memcpy(return_buffer + offset,
351*49cdfc7eSAndroid Build Coastguard Worker 			       message_received + bytes_received, 1);
352*49cdfc7eSAndroid Build Coastguard Worker 			offset++;
353*49cdfc7eSAndroid Build Coastguard Worker 			bytes_received++;
354*49cdfc7eSAndroid Build Coastguard Worker 		}
355*49cdfc7eSAndroid Build Coastguard Worker 
356*49cdfc7eSAndroid Build Coastguard Worker 		/* end of line + end of buffer => return line */
357*49cdfc7eSAndroid Build Coastguard Worker 		if (bytes_received == count - 1) {
358*49cdfc7eSAndroid Build Coastguard Worker 			/* set last byte to END_LINE */
359*49cdfc7eSAndroid Build Coastguard Worker 			*(return_buffer + offset) = END_LINE;
360*49cdfc7eSAndroid Build Coastguard Worker 			bytes_received = 0;
361*49cdfc7eSAndroid Build Coastguard Worker 			return ++offset;
362*49cdfc7eSAndroid Build Coastguard Worker 		}
363*49cdfc7eSAndroid Build Coastguard Worker 
364*49cdfc7eSAndroid Build Coastguard Worker 		/* end of line but still some data in buffer => return line */
365*49cdfc7eSAndroid Build Coastguard Worker 		if (bytes_received < count - 1) {
366*49cdfc7eSAndroid Build Coastguard Worker 			/* set last byte to END_LINE */
367*49cdfc7eSAndroid Build Coastguard Worker 			*(return_buffer + offset) = END_LINE;
368*49cdfc7eSAndroid Build Coastguard Worker 			bytes_received++;
369*49cdfc7eSAndroid Build Coastguard Worker 			return ++offset;
370*49cdfc7eSAndroid Build Coastguard Worker 		}
371*49cdfc7eSAndroid Build Coastguard Worker 
372*49cdfc7eSAndroid Build Coastguard Worker 		/* end of buffer but line is not ended => */
373*49cdfc7eSAndroid Build Coastguard Worker 		/*  wait for more data to arrive on socket */
374*49cdfc7eSAndroid Build Coastguard Worker 		if (bytes_received == count) {
375*49cdfc7eSAndroid Build Coastguard Worker 			bytes_received = 0;
376*49cdfc7eSAndroid Build Coastguard Worker 			return offset;
377*49cdfc7eSAndroid Build Coastguard Worker 		}
378*49cdfc7eSAndroid Build Coastguard Worker 
379*49cdfc7eSAndroid Build Coastguard Worker 	}			/* while */
380*49cdfc7eSAndroid Build Coastguard Worker 
381*49cdfc7eSAndroid Build Coastguard Worker }
382*49cdfc7eSAndroid Build Coastguard Worker 
383*49cdfc7eSAndroid Build Coastguard Worker /*
384*49cdfc7eSAndroid Build Coastguard Worker  * Function:     ltp_multi_server_queue
385*49cdfc7eSAndroid Build Coastguard Worker  * Description:  This function grabs the multiCast message from the queue and outputs to stdio
386*49cdfc7eSAndroid Build Coastguard Worker  */
ltp_multi_server_queue(void * junk)387*49cdfc7eSAndroid Build Coastguard Worker void *ltp_multi_server_queue(void *junk)
388*49cdfc7eSAndroid Build Coastguard Worker {
389*49cdfc7eSAndroid Build Coastguard Worker 
390*49cdfc7eSAndroid Build Coastguard Worker 	printf("%s: waiting for data on port Multicast %u\n",
391*49cdfc7eSAndroid Build Coastguard Worker 	       hostname, LOCAL_MULTI_SERVER_PORT);
392*49cdfc7eSAndroid Build Coastguard Worker 
393*49cdfc7eSAndroid Build Coastguard Worker 	/* infinite server loop */
394*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
395*49cdfc7eSAndroid Build Coastguard Worker 
396*49cdfc7eSAndroid Build Coastguard Worker 		multiClientLen = sizeof(multiClientAddr);
397*49cdfc7eSAndroid Build Coastguard Worker 
398*49cdfc7eSAndroid Build Coastguard Worker 		msg_bytes =
399*49cdfc7eSAndroid Build Coastguard Worker 		    recvfrom(multiSocketHandle, message, MAX_MSG_LEN, 0,
400*49cdfc7eSAndroid Build Coastguard Worker 			     (struct sockaddr *)&multiClientAddr,
401*49cdfc7eSAndroid Build Coastguard Worker 			     &multiClientLen);
402*49cdfc7eSAndroid Build Coastguard Worker 
403*49cdfc7eSAndroid Build Coastguard Worker 		if (msg_bytes < 0) {
404*49cdfc7eSAndroid Build Coastguard Worker 			printf("%s : cannot receive data\n", hostname);
405*49cdfc7eSAndroid Build Coastguard Worker 			continue;
406*49cdfc7eSAndroid Build Coastguard Worker 		}
407*49cdfc7eSAndroid Build Coastguard Worker 
408*49cdfc7eSAndroid Build Coastguard Worker 		printf("%s : from %s:%d on %s : %s\n", ServerProg,
409*49cdfc7eSAndroid Build Coastguard Worker 		       inet_ntoa(multiClientAddr.sin_addr),
410*49cdfc7eSAndroid Build Coastguard Worker 		       ntohs(multiClientAddr.sin_port), hostname, message);
411*49cdfc7eSAndroid Build Coastguard Worker 
412*49cdfc7eSAndroid Build Coastguard Worker 	}			/* end of infinite server loop */
413*49cdfc7eSAndroid Build Coastguard Worker 
414*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
415*49cdfc7eSAndroid Build Coastguard Worker 
416*49cdfc7eSAndroid Build Coastguard Worker }
417