fix for net_test_setup.sh script for hplibmod.ko path, adding some net_test tools
[keystone-rtos/netapi.git] / ti / runtime / netapi / tools / udpif.c
1 /*
2  *
3  * udpIf.c - provides udp 
4  *
5  */
6 //#define BENCHMARK
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #include <sys/time.h>
16 int nSent=0;
18 #ifdef BENCHMARK
19 struct timeval stime,mtime,etime;
20 #endif
22 #define SENDSZ 1396
23 int SendSize=SENDSZ;
25 #define MAIN_NEEDED
26 #ifdef MAIN_NEEDED
27 /******************************************************************************
28     main
29 *******************************************************************************/
30 /* argv[1] = S for Send, R for Receive
31    argv[2] = fileName
32    argv[3] = port
33    argv[4] = ipAddress
34    argv[5] = send size
35 */
36 main(argc,argv)
37 int argc;
38 char **argv;
39 {
40 int  sending = 0;
41 char fileName[256];
42 int  port;
43 char ipAddress[64];
44     if (argc < 4) 
45         {
46         printf ("usage:udpIf S|R fileName port [ipAddress] [send size]\n");
47         exit(-1);
48         }
49     sending = (*argv[1] == 'S') ? 1 : 0;
50     strcpy(fileName,argv[2]);
51     port = atoi(argv[3]);
52     if (argc >= 5)
53         strcpy(ipAddress,argv[4]);
54     else
55         strcpy(ipAddress,"");
56     if (argc ==6) SendSize=atoi(argv[5]);
57     if (SendSize > SENDSZ) SendSize=SENDSZ;
58     udpif(sending,fileName,port,ipAddress);
59 }
60 #endif
61 char startstring[]="start";
62 char endstring[]="done";
63 /******************************************************************************
64     udpif
65 *******************************************************************************/
66 udpif(sending,fileName,port,ipAddress)
67 int  sending;
68 char *fileName;
69 int  port;
70 char *ipAddress;
71 {
72 struct sockaddr_in addr,fraddr;
73 int    addrlen, fd, cnt;
74 socklen_t fraddrlen;
75 char message[SENDSZ+3];
76 int  numTries = 0;
77 #define MAX_TRIES 5
78 int rc;
79 int sstate=0;
80 int total, rtotal;
81 total=rtotal=0; 
82     if (sending)
83         {    
84         int inFile;
85         inFile = open(fileName,O_RDONLY);
86         if (inFile < 0)
87             {
88             printf ("Can't read %s in tcpIf\n",fileName);
89             exit(-1);
90             }
91         fd = socket(AF_INET, SOCK_DGRAM, 0);
92         printf ("socket is %d\n",fd);
93         if (fd < 0) 
94             {
95             perror("socket");
96             exit(1);
97             }
98         bzero(&addr, sizeof(addr));
99         addr.sin_family = AF_INET;
100         addr.sin_addr.s_addr = INADDR_ANY;
101         addr.sin_port = 40000;
102         addrlen = sizeof(addr);
103         rc = bind(fd, &addr, sizeof(addr));
104         printf ("Sender:bind rc is %d\n",rc);
105         if (rc < 0) 
106             {
107             perror("bind");
108             exit(1);
109             }
110         addr.sin_addr.s_addr = inet_addr(ipAddress);
111         addr.sin_port = htons(port);
112         printf("dbg: %x %x\n", addr.sin_addr.s_addr, addr.sin_port);
113 #ifdef BENCHMARK
114                sendto(fd,
115                      startstring,
116                      strlen(startstring)+1,
117                      0,&addr,sizeof(addr));
118 #endif
119         while(1)
120             {
121             char *mPtr;
122             int len;
123             int count = read(inFile,message,/*4096*/ /*SENDSZ*/SendSize);
124 #ifndef BENCHMARK
125             //printf ("Sender:in count is %d\n",count);
126 #endif
127             if (count == 0) 
128                 {
129                 close(inFile);
130 #ifdef BENCHMARK
131                sendto(fd,
132                      endstring,
133                      strlen(endstring)+1,
134                      0,&addr,sizeof(addr));
135                                      
136  sendto(fd,
137                      endstring,
138                      strlen(endstring)+1,
139                      0,&addr,sizeof(addr));
140  sendto(fd,
141                      endstring,
142                      strlen(endstring)+1,
143                      0,&addr,sizeof(addr));
144                printf("sending done %d\n",nSent);
145                sleep(1);
146 #endif
147                 printf("sending done %d\n",nSent);
149                 break;
150                 }
151             mPtr = message;
152             len = count==-1 ? 5: count;
153             do {
154                 cnt = sendto(fd, 
155                      mPtr,
156                      len,
157                      0,&addr,sizeof(addr));
158 #ifndef BENCHMARK
159                // printf ("Sender:sendto count is %d\n",cnt);
160 #endif
161                 if (cnt < 0) 
162                     {
163                     close(inFile);
164                     perror("sendto");
165                     exit(1);
166                     }
167                 nSent+=1;
168                 mPtr += cnt;
169                 len -= cnt;
170                 } while(len > 0);
171             }
172         } 
173     else 
174         { 
175         /* receive */
176         int outFile;
177         int newSocket;
178         int rc;
179         fd = socket(AF_INET, SOCK_DGRAM, 0);
180         printf ("socket is %d\n",fd);
181         if (fd < 0) 
182             {
183             perror("socket");
184             exit(1);
185             }
186         bzero(&addr, sizeof(addr));
187         addr.sin_family = AF_INET;
188         addr.sin_addr.s_addr = htonl(INADDR_ANY);
189         addr.sin_port = htons(port);
190         addrlen = sizeof(addr);
191         rc = bind(fd, &addr, sizeof(addr));
192         printf ("Receiver:bind is %d\n",rc);
193         if (rc < 0) 
194             {
195             perror("bind");
196             exit(1);
197             }
198         /*outFile = open(fileName,O_WRONLY | O_CREAT);
199         if (outFile < 0)
200             {
201             printf ("Can't write to %s in tcpIf\n",fileName);
202             exit(-1);
203             }*/
204        memset(&fraddr,0,sizeof(fraddr));
205        fraddr.sin_family= AF_INET;
206        fraddrlen = sizeof(fraddr); 
207 #ifdef BENCHMARK
208         gettimeofday(&stime, 0                  );
209         printf("rcv start @ %d: %d\n",stime.tv_sec, stime.tv_usec);
211 #endif
212        while(1)
213             {
214 #ifdef BENCHMARK
216         gettimeofday(&mtime, 0                  );
217         if(rtotal >= 10000000) 
218         {
219         printf("rcv mtime @ %d: %d %d %d\n",mtime.tv_sec, mtime.tv_usec,total, rtotal);
220         rtotal=0;
221         }
222 #endif
223             cnt = recvfrom(fd, 
224                        message, 
225                        sizeof(message), 
226                        0,
227                        NULL,
228                        &fraddrlen);
229             //printf ("Receiver:recv count = %d\n",cnt);
230             if (cnt == 0) 
231                 {
232                 /*close(outFile);*/
233                 continue;
234                 }
235             if (cnt < 0) 
236                 {
237                 /*close(outFile);*/
238                 perror("recvfrom");
239                 exit(1);
240                 }
241            rtotal+=cnt;
242            total+=cnt;
243  
244            message[cnt+1] = '\0';
245 #ifndef BENCHMARK
246             printf("rcvd %s\n",message);
247 #else
248             if (strcmp("done",message)==0)
249             {
250                 time_t blah1;
251                 suseconds_t blah2;
252                 gettimeofday(&etime, 0                  );
253                 blah1= (etime.tv_usec>stime.tv_usec) ?
254                                etime.tv_sec-stime.tv_sec :
255                                etime.tv_sec-stime.tv_sec-1;
256                 blah2 = (etime.tv_usec>stime.tv_usec) ?
257                                      etime.tv_usec-stime.tv_usec :
258                                      1000000-stime.tv_usec+etime.tv_usec;
259                 printf("DONE @ %d: %d {%d %d}\n",etime.tv_sec, etime.tv_usec,
260                                                  blah1, blah2 );
261                 gettimeofday(&etime, 0                  );
262             }
263             if (strcmp("start",message)==0)
264             {
265                 gettimeofday(&stime, 0                  );
266                 printf("START @ %d: %d\n",stime.tv_sec, stime.tv_usec);
267             }
269 #endif
270             }
271         }