Imported Upstream version 20090930
[ltp-debian.git] / testcases / network / ipv6 / sendfile6 / testsf_s6.c
bloba7ae05ce4ddd855a4ee9565766f84902c971bb11
1 /*
2 * Server for the sendfile test program
3 * Syntax: testsf_s <own IP addr>
4 */
6 #include <stdio.h>
7 #include <netdb.h>
8 #include <sys/types.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/file.h>
12 #include <errno.h>
13 #include <sys/signal.h>
14 #include <netinet/in.h>
15 #include <sys/socket.h>
17 #define LISTEN_BACKLOG 10
20 void *
21 test_sig(sig)
22 int sig;
25 int status;
27 wait(&status);
33 main(argc, argv)
34 int argc;
35 char *argv[];
38 struct sockaddr_in6 sa, *ap;
39 struct sockaddr_in6 from;
40 struct addrinfo *hp;
41 struct addrinfo hints;
42 int s, fd, as, rc;
43 char *lp;
44 char *number;
45 int i, clen, pid;
46 int flags, nonblocking;
47 int nbytes, flen,gai,count;
48 char rbuf[81];
49 int chunks=0;
50 off_t *offset;
51 char nbuf[81];
52 int port;
54 /* open socket */
55 if ((s = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
56 printf("socket error = %d\n", errno);
57 exit(1);
60 signal(SIGCHLD, SIG_IGN); /* ignore signals from children */
62 memset(&hints, 0, sizeof(hints));
63 hints.ai_family = PF_INET6;
64 if ((gai=getaddrinfo(argv[1], NULL, &hints, &hp))!=0) {
65 fprintf(stderr, "Unknown subject address %s: %s\n",argv[1], gai_strerror(gai));
66 exit(1);
68 if (!hp || !hp->ai_addr || hp->ai_addr->sa_family != AF_INET6) {
69 fprintf(stderr, "getaddrinfo failed");
70 exit(1);
74 /* server IP and port */
75 memcpy(&sa, hp->ai_addr, hp->ai_addrlen);
76 port=atoi(argv[2]);
77 sa.sin6_port = htons(port);
79 /* bind IP and port to socket */
80 if ( bind(s, (struct sockaddr_in6*) &sa, sizeof(sa) ) < 0 ) {
81 printf("bind error = %d\n", errno);
82 close(s);
83 exit(1);
86 /* start to listen socket */
87 if ( listen(s, LISTEN_BACKLOG ) < 0 ) {
88 printf("listen error = %d\n", errno);
89 close(s);
90 exit(1);
93 /* process connections */
94 while(1) {
95 /* accept a connection from a client */
96 clen = sizeof(from);
97 if ((as = accept(s, &from, &clen )) < 0 ) {
98 printf("accept error = %d\n", errno);
99 if (errno == EINTR)
100 continue;
101 close(s);
102 exit(1);
105 ap = (struct sockaddr_in6 *)&from;
107 /* create a process to manage the connection */
108 if ((pid = fork()) < 0) {
109 printf("fork error = %d\n", errno);
110 close(as);
111 exit(1);
113 if (pid > 0) { /* parent, go back to accept */
114 close(as);
115 continue;
120 /* child process to manage a connection */
122 close(s); /* close service socket */
124 /* get client request information */
125 if ((nbytes = read(as, rbuf, 80)) <= 0) {
126 printf("socket read error = %d\n", errno);
127 close(as);
128 exit(1);
130 rbuf[nbytes] = '\0'; /* null terminate the info */
131 lp = &rbuf[0];
133 /* start with file length, '=' will start the filename */
134 flen = 0;
135 count = 0;
136 number = &nbuf[0];
137 while (*lp != '=') { /* convert ascii to integer */
138 nbuf[count] = *lp;
139 count++;
140 lp++;
142 nbuf[count] = '\0';
143 flen = strtol(number, (char **)NULL, 10);
144 printf("flen is %d.\n",flen);
146 /* the file name */
147 lp++;
149 printf("The file to send is %s\n", lp);
150 /* open requested file to send */
151 if ((fd = open(lp, O_RDONLY)) < 0) {
152 printf("file open error = %d\n", errno);
153 close(as);
154 exit(1);
156 offset=NULL;
157 errno=0;
158 do { /* send file parts until EOF */
159 if ((rc = sendfile(as, fd, offset, flen)) != flen) {
160 if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
161 printf("sendfile error = %d, rc = %d\n", errno, rc);
162 close(as);
163 close(fd);
164 exit(-1);
167 chunks++;
168 } while (rc != 0);
169 printf("File %s sent in %d parts\n", lp, chunks);
172 close(as); /* close connection */
173 close(fd); /* close requested file */
174 exit(0);
179 close(s); /* close parent socket (never reached because of the while(1)) */