echoes.c revision b1950ae7f19f46c05848de83f268c06a2b8ea482
1/*
2@! # TEST TYPE(S): Concurrency, Load stress
3@! # TESTCASE DESCRIPTION:
4@! # 	Purpose: to send packets from the file to echo protocol on remote
5@! #		 machine and read the echoing packets back and compare them
6@! # 	Design: Connect to echo protocol on the remote machine
7@! #		read from the file and send the file to remote machine
8@! #		read the echoing  packets and store them in a file
9@! #		repeat until file exhausted.
10@! #		compare result
11@! #
12@! # SPEC. EXEC. REQS: May require multiple of this test to run
13@! #		       to target machines from multiple machine in order
14@! #		       to create stress condition
15@! # 			echoes <REMOTE HOST> <echofile> <number of process>
16*/
17#include <stdio.h>
18#include <netdb.h>
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <curses.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25
26main (int argc,char *argv[],char *env[])
27{
28
29	int	i,j,k,wait_stat,pid,finish;
30	struct	servent *sp;
31	struct 	hostent *hp;
32	struct  {
33		char	resultfile[35];
34		int	pid;
35		int	status;
36	}echo_struc[200];
37
38	hp = gethostbyname(argv[1]);
39	if ((sp=getservbyname("echo","tcp"))==NULL) {
40		printf("ERROR service is not available\n");
41		perror("echo");
42		exit(1);
43	}
44	i=atoi(argv[3]);
45	j=0;
46	while ( i-- > 0 )  {
47		echo_struc[j].status=0;
48		switch (pid=fork())  {
49		case 0:
50			getfilename(argv[2],echo_struc[j].resultfile,j);
51			echofile(sp,hp,echo_struc[j].resultfile,argv[2]);
52			break;
53		case -1:
54			printf("ERROR when forking a new process\n");
55			perror("echo");
56			exit(1);
57			break;
58		default:
59			echo_struc[j].pid=pid;
60			echo_struc[j].status=1;
61			j++;
62			break;
63		}
64	}
65	finish=atoi(argv[3]);
66	i=finish;
67	while (finish != 0) {
68		if ((pid=wait(&wait_stat)) == -1) {
69			printf("ERROR in wait process\n");
70			perror("echo");
71			exit(1);
72		}
73		if (wait_stat==0)
74			for (j=0;j<i;j++) {
75				if(echo_struc[j].pid==pid) {
76					echo_struc[j].status=2;
77					finish--;
78					j=i;
79				}
80			}
81		else {
82			for (k=0;k < i;k++)
83				if (echo_struc[k].status==1) {
84					kill(echo_struc[k].pid,9);
85				}
86			exit(1);
87		}
88	}
89	exit(0);
90}
91
92echofile (struct servent *sp, struct hostent *hp, char *resultfile, char *orgfile)
93{
94	int	n;
95	int	port;
96	char	wr_buffer[BUFSIZ];
97	char	rd_buffer[BUFSIZ];
98	struct	in_addr	hostaddr;
99	struct sockaddr_in sa;
100#ifdef DEBUG
101	struct	sockaddr_in address;
102	u_short	addrlen;
103	u_short	portnum;
104#endif
105	int	s;
106	int	finish;
107	int	fdw,fdr;
108	int	nread,nwrite;
109	int	pid;
110	int	count;
111#ifdef 	DEBUG
112	printf("Create socket .....\n");
113#endif
114	pid=getpid();
115	if ((s=socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
116		printf("ERROR occured during socket operation(%d)\n",pid);
117		perror("echo:socket");
118		cleanup(s);
119		exit(1);
120	}
121	port=sp->s_port;
122	bcopy(hp->h_addr_list[0],&hostaddr,sizeof(struct in_addr));
123	bzero((char *)&sa,sizeof (sa));
124	sa.sin_port=port;
125	sa.sin_family=AF_INET;
126	sa.sin_addr=hostaddr;
127
128#ifdef 	DEBUG
129	printf("sizeof (hostaddr)=%d\n",sizeof (hostaddr));
130	printf("port=%d hostaddr=%x", ntohs(port), hostaddr);
131	printf("Connect .......\n");
132#endif
133	if (connect(s,(struct sockaddr *) &sa,sizeof(sa))==-1) {
134		printf ("ERROR occured during connect socket operation(%d)\n",pid);
135		perror("echo:connect");
136		cleanup(s);
137		exit(1);
138	}
139#ifdef DEBUG
140	addrlen=sizeof(address);
141	printf("addrlen=%d\n",addrlen);
142	printf("hp->h_length=%d\n",hp->h_length);
143	printf("hp->h_addrtype=%d\n",hp->h_addrtype);
144	printf("hp->h_addr=%d\n",inet_ntoa(hp->h_addr));
145	if (getsockname(s,&address,&addrlen) == -1 ) {
146		printf ("ERROR occured during getsockname(%d)\n",pid);
147		perror("echo");
148		cleanup(s);
149		exit(1);
150	}
151	portnum=ntohs(address.sin_port);
152	printf ("local port is: %d\n",portnum);
153	if (getpeername(s,&address,&addrlen) == -1) {
154		printf ("ERROR occured during getpeername(%d)\n",pid);
155		perror("echo");
156		cleanup(s);
157		exit(1);
158	}
159	portnum=ntohs(address.sin_port);
160	/*	printf ("remote address is: %d\n",portnum);
161*/
162#endif
163	if ((fdr=open(orgfile,O_RDONLY)) < 0 ) {
164		printf("ERROR when opening the input file(%d)\n",pid);
165		perror("echo:orginal file");
166		cleanup(s);
167		exit(1);
168	}
169	if ((fdw=creat(resultfile,0644)) < 0 ) {
170		printf("ERROR when opening the temporary temp file(%d)\n",pid);
171		perror("echo:resultfile");
172		cleanup(s);
173		exit(1);
174	}
175	finish=FALSE;
176	count=0;
177	while (!finish) {
178		if ((nwrite=read(fdr,wr_buffer,BUFSIZ))==-1) {
179			printf("ERROR when reading input file(%d)\n",pid);
180			perror("echo:orginal file");
181			cleanup(s);
182			exit(1);
183		}
184		if (nwrite==0)
185			finish=TRUE;
186		else {
187			count++;
188			if((n=write(s,wr_buffer,nwrite))!=nwrite) {
189				printf("ERROR during write to socket(%d)\n",pid);
190				perror("echo:socket write");
191				cleanup(s);
192				exit(1);
193			}
194#ifdef 	DEBUG
195/*
196			printf("Writing .......%d\n",count);
197*/
198#endif
199			while (nwrite!=0) {
200				if((nread=read(s,rd_buffer,BUFSIZ))==-1) {
201					printf("read size:%d\n",n);
202					printf("ERROR during read from socket(%d)\n",pid);
203					perror("echo:socket read");
204					cleanup(s);
205					exit(1);
206				}
207#ifdef 	DEBUG
208/*
209				printf("Reading .......    %d\n",count);
210*/
211#endif
212				if((n=write(fdw,rd_buffer,nread))!=nread) {
213					printf("read size:%d\n",n);
214					printf("ERROR during write to result file(%d)\n",pid);
215					perror("echo:result file");
216					cleanup(s);
217					exit(1);
218				}
219				nwrite-=nread;
220			}
221
222		}/* end of else */
223	} /* end of while */
224	if ((n=close(s)) == -1) {
225		printf("ERROR in closing socket(%d)\n",pid);
226		perror("echo");
227		exit(1);
228	}
229	if ((n=close(fdr)) == -1) {
230		printf("ERROR in closing input file(%d)\n",pid);
231		perror("echo");
232		exit(1);
233	}
234	if ((n=close(fdw) ) == -1) {
235		printf("ERROR in closing temp file(%d)\n",pid);
236		perror("echo");
237		exit(1);
238	}
239	if (checkfile(orgfile,resultfile)) {
240		printf("ERROR input file and output file are not equal(%d)\n",pid);
241		exit(1);
242	}
243	printf("Finish ....%d\n",pid);
244	exit(0);
245}
246
247getfilename(char *strptr, char* filename, int j)
248{
249	int 	i;
250	char	s[10],*sptr=&s[0];
251
252	strcpy(filename,strptr);
253	itoa(j,s);
254	strcat(filename,s);
255}
256
257checkfile(char *file1, char *file2)
258{
259	int	n;
260	struct	stat buffer,*bufptr=&buffer;
261	stat(file1,bufptr);
262	n=bufptr->st_size;
263#ifdef 	DEBUG
264	/*	printf("%s size= %d \n",file1,n);
265*/
266#endif
267	stat(file2,bufptr);
268#ifdef 	DEBUG
269	/*	printf("%s size= %d \n",file2,bufptr->st_size);
270*/
271#endif
272	if(n != buffer.st_size)
273		return(TRUE);
274	else return(FALSE);
275}
276
277itoa(int n, char s[])
278{
279	int i, sign;
280
281	if ((sign = n) < 0)
282		n = -n;
283	i = 0;
284	do {
285		s[i++] = n % 10 + '0';
286	} while ((n /= 10) > 0);
287	if (sign < 0)
288		s[i++] = '-';
289	s[i] = '\0';
290	reverse(s);
291}
292
293reverse(char s[])
294{
295	int c, i, j;
296
297	for (i=0, j=strlen(s)-1; i < j; i++, j--) {
298		c = s[i];
299		s[i] = s[j];
300		s[j] = c;
301	}
302}
303cleanup(int s)
304{
305close(s);
306}
307