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