]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/common/v2_0/tests/tcp_lo_select.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / common / v2_0 / tests / tcp_lo_select.c
1 //==========================================================================
2 //
3 //      tests/tcp_lo_test.c
4 // 
5 //      Simple TCP throughput test 
6 //
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 //
12 // Portions of this software may have been derived from OpenBSD or other sources
13 // and are covered by the appropriate copyright disclaimers included herein.
14 //
15 // -------------------------------------------
16 //
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
20 //
21 // Author(s):    sorin@netappi.com 
22 // Contributors: gthomas,sorin@netappi.com 
23 // Date:         2000-05-24
24
25
26 // Network throughput test code
27
28 #include <network.h>
29
30 #include <cyg/infra/testcase.h>
31
32 #ifndef CYGPKG_LIBC_STDIO
33 #define perror(s) diag_printf(#s ": %s\n", strerror(errno))
34 #endif
35
36 #define SOURCE_PORT1 9990
37 #define SOURCE_PORT2   9991
38
39 #define NUM_BUF 1024
40 #define MAX_BUF 8192
41 static unsigned char data_buf1[MAX_BUF];
42 static unsigned char data_buf2[MAX_BUF];
43 static unsigned char data_buf_write1[MAX_BUF]="Client 1 is alive. You may continue ....";
44 static unsigned char data_buf_write2[MAX_BUF]="Client 2 is alive. You may continue ....";
45
46
47 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x10000)
48
49 static char stack_server[STACK_SIZE];
50 static cyg_thread server_thread_data;
51 static cyg_handle_t server_thread_handle;
52
53 static char stack_client1[STACK_SIZE];
54 static cyg_thread client1_thread_data; 
55 static cyg_handle_t client1_thread_handle;  
56
57 static char stack_client2[STACK_SIZE];
58 static cyg_thread client2_thread_data;
59 static cyg_handle_t client2_thread_handle;
60
61
62 #define MAIN_THREAD_PRIORITY     CYGPKG_NET_THREAD_PRIORITY-4
63
64 void
65 pexit(char *s)
66 {
67     CYG_TEST_FAIL_FINISH( s );
68 }
69
70
71 #ifndef max
72 #define max(a,b) (((a) > (b)) ? (a) : (b))
73 #endif
74
75 void server(void)
76 {
77     int s_s1, e_s1, s_s2, e_s2;
78     struct sockaddr_in e_s1_addr,e_s2_addr,local;
79     fd_set in_fds;
80     socklen_t len;
81     int num;
82     
83     char *hello_string=" Hello eCos network \n";
84     diag_printf("TCP SERVER:");
85     diag_printf(hello_string);
86
87     s_s1 = socket(AF_INET, SOCK_STREAM, 0);
88     if (s_s1 < 0) {
89         pexit("stream socket");
90     }   
91     memset(&local, 0, sizeof(local));
92     local.sin_family = AF_INET;
93     local.sin_len = sizeof(local);
94     local.sin_port = ntohs(SOURCE_PORT1);
95     local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
96     if(bind(s_s1, (struct sockaddr *) &local, sizeof(local)) < 0) {
97         pexit("bind /source_1/ error");
98     }
99     listen(s_s1, SOMAXCONN);
100
101     s_s2 = socket(AF_INET, SOCK_STREAM, 0);    
102     if (s_s2 < 0) {    
103         pexit("stream socket");
104     }  
105     memset(&local, 0, sizeof(local));
106     local.sin_family = AF_INET;
107     local.sin_len = sizeof(local);
108     local.sin_port = ntohs(SOURCE_PORT2);
109     local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
110     if(bind(s_s2, (struct sockaddr *) &local, sizeof(local)) < 0) {    
111         pexit("bind /source_2/ error");
112     }
113     listen(s_s2, SOMAXCONN);    
114
115    
116     e_s1 = 0; e_s2 = 0; 
117
118     
119     while (true) {
120         FD_ZERO(&in_fds);
121         FD_SET(s_s1, &in_fds);
122         FD_SET(s_s2, &in_fds);
123         num = select ( max(s_s1,s_s2)+1, &in_fds,0,0,0);
124         if (FD_ISSET(s_s1,&in_fds)) {
125                 len = sizeof(e_s1_addr);
126                 if ((e_s1 = accept(s_s1,(struct sockaddr *)&e_s1_addr,&len))<0) 
127                         {
128                         pexit("accept /source_1/");
129                         }
130         diag_printf("TCP SERVER connection from %s: %d\n",
131                inet_ntoa(e_s1_addr.sin_addr),ntohs(e_s1_addr.sin_port));
132         }
133         if (FD_ISSET(s_s2,&in_fds)) {
134                 len = sizeof(e_s2_addr);
135                 if ((e_s2 = accept(s_s2,(struct sockaddr *)&e_s2_addr,&len))<0)
136                         {
137                         pexit("accept /source_2/");
138                         }
139         diag_printf("TCP SERVER connection from %s: %d\n",
140                inet_ntoa(e_s2_addr.sin_addr), ntohs(e_s2_addr.sin_port));
141         }
142
143         if ((e_s1 != 0) && ( e_s2 != 0)) {
144             break;
145             }
146      
147         }   /* while (true) */ 
148
149      if ((len = read(e_s1, data_buf1, MAX_BUF)) < 0  )
150         {
151                 perror("I/O error");
152         }
153    diag_printf("SERVER : %s\n",data_buf1);
154
155      if ((len = read(e_s2, data_buf2, MAX_BUF)) < 0  )    
156         {
157                 perror("I/O error");
158         }
159    diag_printf("SERVER : %s\n",data_buf2);
160     
161 }
162
163 void client1(void)
164 {
165     int s_source;
166     struct sockaddr_in local;
167     int len;
168
169     diag_printf("client 1 :started\n");
170     
171     s_source = socket(AF_INET, SOCK_STREAM, 0);
172     if (s_source < 0) {
173         pexit("stream socket");
174     }
175     memset(&local, 0, sizeof(local));
176     local.sin_family = AF_INET;
177     local.sin_len = sizeof(local);
178     local.sin_port = htons(SOURCE_PORT1);
179     local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
180     
181     if (connect(s_source, (struct sockaddr *)&local, sizeof(local)) < 0) {
182         pexit("Can't connect to target");
183     }
184     
185     if ((len = write(s_source,data_buf_write1,40)) < 0) {
186         CYG_TEST_FAIL_FINISH("Error writing buffer");
187     } 
188 }
189
190 void client2(void)
191 {
192     int s_source;
193     struct sockaddr_in local;
194     int len;
195     
196     diag_printf("client 2 :started\n");
197     
198     s_source = socket(AF_INET, SOCK_STREAM, 0);
199     if (s_source < 0) {
200         pexit("stream socket");
201     }
202     memset(&local, 0, sizeof(local));
203     local.sin_family = AF_INET;
204     local.sin_len = sizeof(local);
205     local.sin_port = htons(SOURCE_PORT2);
206     local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
207    
208     if (connect(s_source, (struct sockaddr *)&local, sizeof(local)) < 0) {
209         pexit("Can't connect to target");
210     }
211     
212     if ((len = write(s_source,data_buf_write2,40)) < 0) {
213         CYG_TEST_FAIL_FINISH("Error writing buffer");
214     }
215 }
216
217
218 void
219 tcp_server(cyg_addrword_t param)
220 {
221     init_all_network_interfaces();
222     diag_printf("Start TCP server - test\n");
223     cyg_thread_resume(client1_thread_handle);   // Start it
224     cyg_thread_resume(client2_thread_handle);   // Start it
225 #if NLOOP > 0
226     server();
227     CYG_TEST_PASS_FINISH("Server returned OK");
228 #endif
229     CYG_TEST_NA( "No loopback devs" );
230 }
231
232 void
233 tcp_client_1(cyg_addrword_t param)
234 {
235     diag_printf("Start TCP client 1 - test\n");
236 #if NLOOP > 0
237     client1(); 
238 #endif
239 }
240
241 void
242 tcp_client_2(cyg_addrword_t param)
243 {
244     diag_printf("Start TCP client 2 - test\n");
245 #if NLOOP > 0
246     client2();
247 #endif
248 }
249
250
251 void
252 cyg_start(void)
253 {
254     CYG_TEST_INIT();
255
256     cyg_thread_create(MAIN_THREAD_PRIORITY,     // Priority
257                       tcp_server,               // entry
258                       0,                        // entry parameter
259                       "TCP loopback server",    // Name
260                       &stack_server[0],         // Stack
261                       STACK_SIZE,               // Size
262                       &server_thread_handle,    // Handle
263                       &server_thread_data       // Thread data structure
264             );
265     cyg_thread_resume(server_thread_handle);    // Start it
266
267     cyg_thread_create(MAIN_THREAD_PRIORITY,     // Priority
268                       tcp_client_1,             // entry
269                       0,                        // entry parameter
270                       "TCP loopback client1",   // Name
271                       &stack_client1[0],        // Stack
272                       STACK_SIZE,               // Size
273                       &client1_thread_handle,   // Handle
274                       &client1_thread_data      // Thread data structure
275             );
276
277     cyg_thread_create(MAIN_THREAD_PRIORITY,     // Priority
278                       tcp_client_2,             // entry
279                       0,                        // entry parameter
280                       "TCP llopback client2",   // Name
281                       &stack_client2[0],        // Stack
282                       STACK_SIZE,               // Size
283                       &client2_thread_handle,   // Handle
284                       &client2_thread_data      // Thread data structure
285             );
286
287     cyg_scheduler_start();
288 }
289