testsf_c.c revision ef77253961f909f87e82e6d2b620e87af33e9665
1
2/*
3 * Client for the send_file test program
4 * Syntax: testsf_c <server IP addr> <port> <client_filename> <server_filename> <file-length>
5 */
6
7#include <stdio.h>
8#include <netdb.h>
9#include <sys/types.h>
10#include <stdlib.h>
11#include <string.h>
12#include <sys/file.h>
13#include <arpa/inet.h>
14#include <netinet/in.h>
15#include <sys/socket.h>
16#include <errno.h>
17#include "test.h"
18#include "usctest.h"
19#include "netdefs.h"
20
21int TST_TOTAL = 1;
22extern int Tst_count;
23
24#if INET6
25char *TCID = "sendfile6_client";
26#else
27char *TCID = "sendfile_client";
28#endif
29
30int
31main (int argc, char *argv[])
32{
33	sai_t sai;
34	int s, fd;
35	int nbyte;
36	char *serv_fname, *clnt_fname;
37	char rbuf[PATH_MAX];
38	int nlen, gai;
39	struct  addrinfo *hp;
40	struct  addrinfo hints;
41	int port;
42
43	if (argc != 6) {
44		tst_resm(TBROK, "usage: server-ip port client-file server-file file-len");
45		tst_exit();
46	}
47
48	int i;
49	for (i = 0;  i < argc; i++)
50		printf("i=%d: %s\n", i, *(argv+i));
51
52	/* open socket to server */
53	if ((s = socket(AFI, SOCK_STREAM, 0)) < 0) {
54		tst_resm(TBROK, "socket error = %d\n", errno);
55		tst_exit();
56	}
57
58	clnt_fname = argv[3]; /* filename to create */
59	serv_fname = argv[4]; /* filename to request */
60
61	/* prepare to copy file from server to local machine */
62	if ((fd = open(clnt_fname, O_CREAT | O_TRUNC | O_WRONLY, 0777)) < 0) {
63		tst_resm(TBROK, "file open error = %d\n", errno);
64		close(s);
65		tst_exit();
66	}
67
68	/* initialize request info: */
69	rbuf[0] = '\0';
70	/* The request will be done in the form: `file-size=requested-file'. */
71	snprintf(rbuf, PATH_MAX, "%s=%s", argv[5], serv_fname);
72
73	memset(&hints, 0, sizeof(hints));
74	hints.ai_family = PFI;
75	if ((gai = getaddrinfo(argv[1], NULL, &hints, &hp)) != 0) {
76		tst_resm(TBROK, "Unknown subject address %s: %s\n",
77				argv[1], gai_strerror(gai));
78	}
79	if (!hp || !hp->ai_addr || hp->ai_addr->sa_family != AFI) {
80		tst_resm(TBROK, "getaddrinfo failed");
81		tst_exit();
82	}
83
84	tst_resm(TINFO, "rbuf => %s\n", rbuf);
85	/* initialize server info to make the connection */
86	memcpy(&sai, hp->ai_addr, hp->ai_addrlen);
87	port = atoi(argv[2]);
88
89#if INET6
90	sai.sin6_port = htons(port);
91#else
92	sai.sin_port = htons(port);
93#endif
94
95	if (connect(s, (sa_t*) &sai, sizeof(sai) ) < 0) {
96		tst_resm(TBROK, "connect error = %d\n", errno);
97		close(s);
98		exit(1);
99	}
100
101	/* send request info to server */
102	if ((nbyte = write(s, rbuf, strlen(rbuf))) <= 0) {
103		tst_resm(TBROK, "socket write  error = %d\n", errno);
104		close(s);
105		exit(1);
106	}
107
108	tst_resm(TINFO, "client write %d bytes to server with contents %s\n",
109			nbyte, rbuf);
110
111	nlen = 0; /* init size of info received */
112	rbuf[0] = '\0';
113	/* read until an EOF is encountered. */
114	while ((nbyte = read(s, rbuf, PATH_MAX)) > 0) {
115		nlen += nbyte;
116		if (write(fd, rbuf, nbyte) != nbyte) {
117			tst_resm(TBROK, "Error writing to file %s on client\n",
118					clnt_fname);
119			tst_exit();
120		}
121	}
122
123	tst_res(TINFO, "Asking for remote file: %s", serv_fname);
124
125	tst_resm(TINFO, "File %s received\n", argv[4]);
126
127	close(s);
128	close(fd);
129
130	return 0;
131
132}
133