src/vfer_test_client.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 2005, 2006, Internet2
00003  * Legal conditions are in file LICENSE
00004  * (MD5 = c434f2e53b8089d8b4d0172c7ce07360).
00005  */
00006 /**
00007  * @file   vfer_test_client.c
00008  * @author Ivan Beschastnikh
00009  * @brief  Test: A connection client that connects to the server and send a small buffer.
00010  *
00011  * This test tests c_control.c and api.c by setting up a socket for
00012  * connection, connecting to a server and sending the server a
00013  * constant size buffer(arg to program) a variable number of times
00014  * (arg to program), sleeping in between each for some time (also arg
00015  * to program).
00016  *
00017  * -     08/05/05        ivan            Added extra cmd line options to control interval, size of frames, number of frames
00018  * -     07/30/05        ivan            Added to the CVS tree (old test_client.c retired)
00019  */
00020 
00021 #include <stdio.h>
00022 #include <netdb.h>
00023 #include <sys/types.h>
00024 #include <sys/socket.h>
00025 #include <arpa/inet.h>
00026 #include <sys/select.h>
00027 #include "vfer_tests.h"
00028 #include "vfer_api.h"
00029 
00030 vfer_fd connect_skt;
00031 int port;
00032 int num_frames;
00033 int frame_size;
00034 int ms_interval;
00035 
00036 /*
00037  * main()
00038  */
00039 int main(int argc, char** argv) {
00040 
00041         struct sockaddr_in client_sa;
00042         struct sockaddr_in server_sa;
00043         char hostname_buf[256];
00044         char hostname[INET_ADDRSTRLEN];
00045         struct hostent* h;
00046         char *buffer;
00047         struct timeval interval;
00048         int i;
00049         int ret;
00050 
00051         if (argc != 6) {
00052                 printf("usage: %s [server_ip] [server_port] [num_frames] [frame_size] [interval]\n", argv[0]);
00053                 printf("\tserver_ip  : server ip address (do not use hostname or the address 127.0.0.1)\n");
00054                 printf("\tserver_port: server port to connect to\n");
00055                 printf("\tnum_frames : total number of frames to send (< 0 => no bound)\n");
00056                 printf("\tframe_size : size of each frame to send\n");
00057                 printf("\tinternval  : interval in milliseconds between each frames\n");
00058                 return -1;
00059         }
00060 
00061         TEST_INIT("TEST_CLIENT", "test_client.c", stdout, stdout);
00062         vfer_debug(stdout, stdout, "scap");
00063         
00064         port            = atoi(argv[2]);
00065         num_frames      = atoi(argv[3]);
00066         if ((frame_size = atoi(argv[4])) < 0) {
00067                 TEST_PRINT("main","ERROR: negative frame size");
00068                 return -1;
00069         }
00070         if ((ms_interval        = atoi(argv[5])) < 0) {
00071                 TEST_PRINT("main","ERROR: invalid interval");
00072                 return -1;
00073         }
00074 
00075         if ((buffer = calloc(1, frame_size)) == NULL) {
00076                 TEST_PRINT("main","ERROR: calloc failed");
00077                 return -1;
00078         }
00079 
00080         /* find out local addr */
00081         if (gethostname(hostname_buf, 256) == -1) {
00082                 perror("gethostname");
00083                 return -1;
00084         }
00085 
00086         if ((h = gethostbyname((const char*)hostname_buf)) == NULL) {
00087                 perror("gethostbyname");
00088                 return -1;
00089         }
00090 
00091         if (h->h_addr_list != 0) {
00092                 inet_ntop(h->h_addrtype, (h->h_addr_list)[0], hostname, sizeof(hostname));
00093         }
00094         
00095         TEST_PRINT("main","my ip: %s", hostname);
00096 
00097         /* setup sockaddr structure for client */
00098         bzero((char*)(&client_sa), sizeof(client_sa));
00099         client_sa.sin_port = htons(0);
00100         client_sa.sin_family = AF_INET;
00101         if (inet_pton(AF_INET, hostname, &client_sa.sin_addr) != 1) {
00102                 TEST_PRINT("main","inet_pton failed");
00103                 return -1;
00104         }
00105 
00106         /* setup sockaddr structure for server */
00107         bzero((char*)(&server_sa), sizeof(server_sa));
00108         server_sa.sin_family = AF_INET;
00109         server_sa.sin_port = htons(port);
00110 
00111         if (inet_pton(AF_INET, argv[1], &server_sa.sin_addr) != 1) {
00112                 TEST_PRINT("main","inet_pton failed");
00113                 return -1;
00114         }
00115         
00116         if ((connect_skt = vfer_socket (SOCK_DGRAM)) < 0) {        
00117                 TEST_PRINT("main","vfer_socket failed");
00118                 return -1;
00119         }
00120 
00121         if ((ret = vfer_bind  (connect_skt, (struct sockaddr*) &client_sa, sizeof(client_sa))) < 0) {
00122                 TEST_PRINT("main","vfer_bind failed [%s]", vfer_errortext(ret));
00123                 return -1;
00124         }
00125 
00126         if ((ret = vfer_connect (connect_skt, (SA*) (&server_sa), sizeof(server_sa))) < 0) {
00127                 TEST_PRINT("main","vfer_connect failed [%s]", vfer_errortext(ret));
00128                 return -1;
00129         }
00130 
00131         TEST_PRINT("main","connected!");
00132         if (num_frames < 0) {
00133                 while (1) {
00134                         if ((ret = vfer_send (connect_skt, buffer, frame_size)) <= 0) {
00135                                 TEST_PRINT("main","vfer_send returned[%d] [%s]", ret, vfer_errortext(ret));
00136                                 vfer_close(connect_skt);
00137                                 return -1;
00138                         }
00139                         interval.tv_sec = 0;
00140                         interval.tv_usec = ms_interval * 1000;
00141                         select(0,0,0,0,&interval);
00142                 }
00143         } else {
00144                 for (i = 0; i < num_frames; i++) {
00145                         TEST_PRINT("main","sending frame[%d]", i);
00146                         if ((ret = vfer_send (connect_skt, buffer, frame_size)) <= 0) {
00147                                 TEST_PRINT("main","vfer_send returned[%d] [%s] on frame[%d]", ret, vfer_errortext(ret), i);
00148                                 vfer_close(connect_skt);
00149                                 return -1;
00150                         }
00151                         if (i != num_frames - 1) {
00152                                 interval.tv_sec = 0;
00153                                 interval.tv_usec = ms_interval * 1000;
00154                                 select(0,0,0,0,&interval);
00155                         }
00156                 }
00157         }
00158         sleep(10);              /* allow packets to be processed */
00159         vfer_close(connect_skt);
00160         return 0;
00161 } /* main() */          

Generated on Tue Aug 8 16:07:19 2006 for VFER by  doxygen 1.4.7