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