1 | /* Init |
---|
2 | * |
---|
3 | * This routine is the initialization task for this test program. |
---|
4 | * |
---|
5 | * Don't forget to change the IP addresses |
---|
6 | * |
---|
7 | * $Id$ |
---|
8 | */ |
---|
9 | |
---|
10 | #define TEST_INIT |
---|
11 | |
---|
12 | #define CONFIGURE_EXECUTIVE_RAM_SIZE (512*1024) |
---|
13 | #define CONFIGURE_MAXIMUM_SEMAPHORES 20 |
---|
14 | #define CONFIGURE_MAXIMUM_TASKS 20 |
---|
15 | #define CONFIGURE_MAXIMUM_TIMERS 10 |
---|
16 | #define CONFIGURE_MAXIMUM_PERIODS 1 |
---|
17 | |
---|
18 | #include "system.h" |
---|
19 | |
---|
20 | #include <rtems/error.h> |
---|
21 | #include <ka9q/rtems_ka9q.h> |
---|
22 | #include <ka9q/netuser.h> |
---|
23 | #include <ka9q/socket.h> |
---|
24 | #include <netinet/in.h> |
---|
25 | #include <time.h> |
---|
26 | |
---|
27 | #define ARGUMENT 0 |
---|
28 | |
---|
29 | /* define and include for socket test */ |
---|
30 | /*#define PINKFLOYD */ |
---|
31 | |
---|
32 | /*#define SERVER*/ |
---|
33 | #define CLIENT |
---|
34 | |
---|
35 | /*#define UDP*/ /* if enabled, testing UDP socket else TCP socket */ |
---|
36 | |
---|
37 | #include "buffer.h" |
---|
38 | |
---|
39 | |
---|
40 | #define DEFAULT_IP_SERVER "194.2.81.61" |
---|
41 | #define DEFAULT_LISTEN_IP_SERVER "194.2.81.126" |
---|
42 | #define DEFAULT_IP_CLIENT "194.2.81.126" |
---|
43 | #define DEFAULT_LISTEN_IP_CLIENT "194.2.81.61" |
---|
44 | |
---|
45 | rtems_task Init( |
---|
46 | rtems_task_argument argument |
---|
47 | ) |
---|
48 | { |
---|
49 | unsigned int oldPri; |
---|
50 | rtems_name task_name; |
---|
51 | rtems_id tid; |
---|
52 | rtems_status_code status; |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | printf("\n\n*** SAMPLE SINGLE PROCESSOR APPLICATION ***\n\r" ); |
---|
57 | printf("Creating and starting an application task\n\r" ); |
---|
58 | fflush(stdout); |
---|
59 | |
---|
60 | rtems_task_set_priority (RTEMS_SELF, 30, &oldPri); |
---|
61 | |
---|
62 | rtems_ka9q_start (10); |
---|
63 | |
---|
64 | #ifdef PINKFLOYD |
---|
65 | if (rtems_ka9q_execute_command("attach rtems " |
---|
66 | "broadcast n " |
---|
67 | "ip 194.2.81.127 " |
---|
68 | "irno 5 " |
---|
69 | "bpar 0xd0000 " |
---|
70 | "port 0x240 " |
---|
71 | "ether 00:E0:29:12:3f:85 ")) |
---|
72 | #else |
---|
73 | if (rtems_ka9q_execute_command("attach rtems " |
---|
74 | "broadcast y " |
---|
75 | "ip 194.2.81.61 " |
---|
76 | "irno 3 " |
---|
77 | "bpar 0xcc000 " |
---|
78 | "port 0x260 " |
---|
79 | "ether 00:E0:29:12:40:77 ")) |
---|
80 | #endif |
---|
81 | rtems_panic ("Can't attach Ethernet driver."); |
---|
82 | |
---|
83 | if (rtems_ka9q_execute_command("ifconfig rtems " |
---|
84 | "broadcast 255.255.255.255")) |
---|
85 | rtems_panic ("Can't configure Ethernet driver."); |
---|
86 | |
---|
87 | if (rtems_ka9q_execute_command ("arp add 255.255.255.255 " |
---|
88 | "ether FF:FF:FF:FF:FF:FF")) |
---|
89 | rtems_panic ("Can't add broadcast entry to ARP table."); |
---|
90 | |
---|
91 | if (rtems_ka9q_execute_command ("ifconfig rtems " |
---|
92 | "netmask 255.255.255.0")) |
---|
93 | rtems_panic ("Can't set netmask."); |
---|
94 | |
---|
95 | if (rtems_ka9q_execute_command ("route add default rtems")) |
---|
96 | rtems_panic ("Can't add default route."); |
---|
97 | |
---|
98 | rtems_ka9q_execute_command ("route"); |
---|
99 | |
---|
100 | if (rtems_ka9q_execute_command ("arp gratuitous rtems")) |
---|
101 | rtems_panic ("Can't send gratuitous ARP.\n"); |
---|
102 | |
---|
103 | printf("End of INIT\n\r"); |
---|
104 | fflush(stdout); |
---|
105 | |
---|
106 | #ifdef SERVER |
---|
107 | printf("Creation of SERVER SocketTask\n\r"); |
---|
108 | fflush(stdout); |
---|
109 | |
---|
110 | task_name = rtems_build_name( 'T', 'A', '1', ' ' ); |
---|
111 | if ((status = rtems_task_create( task_name, 20, RTEMS_MINIMUM_STACK_SIZE, |
---|
112 | RTEMS_INTERRUPT_LEVEL(0), |
---|
113 | RTEMS_DEFAULT_ATTRIBUTES, &tid )) |
---|
114 | != RTEMS_SUCCESSFUL){ |
---|
115 | printf("status = %d\n",status); |
---|
116 | rtems_panic ("Can't create task.\n"); |
---|
117 | } |
---|
118 | |
---|
119 | #ifdef UDP |
---|
120 | status = rtems_task_start(tid, testUdpServerSocket, 0); |
---|
121 | #else |
---|
122 | status = rtems_task_start(tid, testTcpServerSocket, 0); |
---|
123 | #endif |
---|
124 | |
---|
125 | #endif /* SERVER */ |
---|
126 | |
---|
127 | #ifdef CLIENT |
---|
128 | printf("Creation of CLIENT SocketTask\n\r"); |
---|
129 | fflush(stdout); |
---|
130 | |
---|
131 | task_name = rtems_build_name( 'T', 'A', '2', ' ' ); |
---|
132 | if ((status = rtems_task_create( task_name, 20, RTEMS_MINIMUM_STACK_SIZE, |
---|
133 | RTEMS_INTERRUPT_LEVEL(0), |
---|
134 | RTEMS_DEFAULT_ATTRIBUTES, &tid )) |
---|
135 | != RTEMS_SUCCESSFUL){ |
---|
136 | printf("status = %d\n",status); |
---|
137 | rtems_panic ("Can't create task.\n"); |
---|
138 | } |
---|
139 | |
---|
140 | #ifdef UDP |
---|
141 | status = rtems_task_start(tid, testUdpClientSocket, 0); |
---|
142 | #else |
---|
143 | status = rtems_task_start(tid, testTcpClientSocket, 0); |
---|
144 | #endif |
---|
145 | |
---|
146 | #endif /* CLIENT */ |
---|
147 | |
---|
148 | status = rtems_task_delete( RTEMS_SELF ); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | rtems_task testUdpServerSocket( |
---|
154 | rtems_task_argument argument |
---|
155 | ){ |
---|
156 | |
---|
157 | int sd; |
---|
158 | struct sockaddr_in server, from; |
---|
159 | int fromlen; |
---|
160 | char *tabChar; |
---|
161 | int rc; |
---|
162 | unsigned long msgId; |
---|
163 | char *server_name= "localhost"; |
---|
164 | unsigned short port; |
---|
165 | |
---|
166 | /****************************************/ |
---|
167 | /* Now, we are going to test the socket */ |
---|
168 | /****************************************/ |
---|
169 | |
---|
170 | printf("Task testServerSocket : created\n\r"); |
---|
171 | fflush(stdout); |
---|
172 | |
---|
173 | port = DEFAULT_PORT_SERVER; |
---|
174 | server_name = DEFAULT_IP_SERVER; |
---|
175 | |
---|
176 | bzero((void *)(&server), sizeof(server)); |
---|
177 | server.sin_family = AF_INET; |
---|
178 | server.sin_addr.s_addr = aton (server_name); |
---|
179 | |
---|
180 | server.sin_port = htons(port); |
---|
181 | |
---|
182 | sd = socket(AF_INET,SOCK_DGRAM, 0); |
---|
183 | if (sd == -1) { |
---|
184 | printf(" socket failed errno = %d\n", errno); |
---|
185 | return ; |
---|
186 | } |
---|
187 | |
---|
188 | rc = bind(sd, (struct sockaddr *)&server, sizeof(server)) ; |
---|
189 | if (rc == -1) { |
---|
190 | printf(" bind failed errno = %d\n", errno); |
---|
191 | return ; |
---|
192 | } |
---|
193 | |
---|
194 | tabChar = AllocBuffer(); |
---|
195 | |
---|
196 | if(!tabChar) { |
---|
197 | printf("malloc failed ...\n"); |
---|
198 | return ; |
---|
199 | } |
---|
200 | |
---|
201 | bzero((void *)(&from), sizeof(from)); |
---|
202 | from.sin_family = AF_INET; |
---|
203 | from.sin_addr.s_addr = aton (DEFAULT_LISTEN_IP_SERVER); |
---|
204 | fromlen = sizeof(from); |
---|
205 | |
---|
206 | msgId = 0; |
---|
207 | |
---|
208 | for(;;){ |
---|
209 | rc=recvfrom(sd, tabChar, SIZE_MAX_RCV_BUFFER, 0, (struct sockaddr *)&from, &fromlen); |
---|
210 | |
---|
211 | if (rc < 0) { |
---|
212 | printf("\n Recvfrom error %d\n\n",errno); |
---|
213 | fflush(stdout); |
---|
214 | } |
---|
215 | else{ |
---|
216 | msgId = CheckBuffer(tabChar); |
---|
217 | } |
---|
218 | if (msgId == 0) |
---|
219 | break ; |
---|
220 | } |
---|
221 | |
---|
222 | FreeBuffer(tabChar); |
---|
223 | |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | rtems_task testUdpClientSocket( |
---|
228 | rtems_task_argument argument |
---|
229 | ){ |
---|
230 | |
---|
231 | int sd; |
---|
232 | struct sockaddr_in server; |
---|
233 | char *tabChar; |
---|
234 | int i; |
---|
235 | char *server_name= "localhost"; |
---|
236 | unsigned short port; |
---|
237 | unsigned long lenChar; |
---|
238 | struct timespec timeToWait, timeRem; |
---|
239 | |
---|
240 | /****************************************/ |
---|
241 | /* Now, we are going to test the socket */ |
---|
242 | /****************************************/ |
---|
243 | |
---|
244 | printf("Task testClientSocket : created\n\r"); |
---|
245 | fflush(stdout); |
---|
246 | |
---|
247 | port = DEFAULT_PORT_CLIENT; |
---|
248 | server_name = DEFAULT_IP_CLIENT; |
---|
249 | |
---|
250 | bzero((void *)(&server), sizeof(server)); |
---|
251 | server.sin_family = AF_INET; |
---|
252 | server.sin_addr.s_addr = aton (server_name); |
---|
253 | |
---|
254 | server.sin_port = htons(port); |
---|
255 | |
---|
256 | sd = socket(AF_INET,SOCK_DGRAM, 0); |
---|
257 | if (sd == -1) { |
---|
258 | printf(" socket failed errno = %d\n", errno); |
---|
259 | return ; |
---|
260 | } |
---|
261 | |
---|
262 | while(1) { |
---|
263 | tabChar = BuildBuffer(); |
---|
264 | lenChar = *((unsigned long *)(tabChar)); |
---|
265 | i = sendto(sd, tabChar, lenChar, 0, (struct sockaddr *)&server, sizeof(server)); |
---|
266 | |
---|
267 | if (i == -1) { |
---|
268 | fprintf(stderr,"sendto() failed (at least local errors): %d\n", errno); |
---|
269 | return ; |
---|
270 | } |
---|
271 | |
---|
272 | timeToWait.tv_sec = 1; |
---|
273 | timeToWait.tv_nsec = 0; |
---|
274 | nanosleep(&timeToWait, &timeRem); |
---|
275 | FreeBuffer(tabChar); |
---|
276 | } |
---|
277 | |
---|
278 | } |
---|
279 | |
---|
280 | rtems_task testTcpServerSocket( |
---|
281 | rtems_task_argument argument |
---|
282 | ){ |
---|
283 | int sd, psd; |
---|
284 | struct sockaddr_in server; |
---|
285 | struct sockaddr_in from; |
---|
286 | int fromlen; |
---|
287 | char *tabChar; |
---|
288 | int rc; |
---|
289 | int i; |
---|
290 | char *server_name= "localhost"; |
---|
291 | unsigned short port; |
---|
292 | |
---|
293 | |
---|
294 | /****************************************/ |
---|
295 | /* Now, we are going to test the socket */ |
---|
296 | /****************************************/ |
---|
297 | |
---|
298 | printf("Task testServerSocket : created\n\r"); |
---|
299 | fflush(stdout); |
---|
300 | |
---|
301 | port = DEFAULT_PORT_SERVER; |
---|
302 | server_name = DEFAULT_IP_SERVER; |
---|
303 | |
---|
304 | bzero((void *)(&server), sizeof(server)); |
---|
305 | server.sin_family = AF_INET; |
---|
306 | server.sin_addr.s_addr = aton (server_name); |
---|
307 | |
---|
308 | server.sin_port = htons(port); |
---|
309 | |
---|
310 | sd = socket (AF_INET,SOCK_STREAM,0); |
---|
311 | printf("socket() returns <%d>\n", sd); |
---|
312 | fflush(stdout); |
---|
313 | |
---|
314 | if (sd<0) { |
---|
315 | printf("socket() call failure: opening stream socket\n"); |
---|
316 | fflush(stdout); |
---|
317 | exit(-1); |
---|
318 | } |
---|
319 | |
---|
320 | rc = bind( sd, (struct sockaddr *)&server, sizeof(server) ) ; |
---|
321 | if (rc == -1) { |
---|
322 | printf("bind failed errno = %d\n", errno); |
---|
323 | fflush(stdout); |
---|
324 | close_s(sd); |
---|
325 | return; |
---|
326 | } |
---|
327 | printf("bind() returns <%d>\n", rc); |
---|
328 | fflush(stdout); |
---|
329 | |
---|
330 | printf("Server Port is: %d\n", ntohs(server.sin_port)); |
---|
331 | fflush(stdout); |
---|
332 | rc = listen(sd,5); |
---|
333 | printf("listen() returns <%d>\n", rc); |
---|
334 | fflush(stdout); |
---|
335 | fromlen = sizeof(from); |
---|
336 | |
---|
337 | tabChar = AllocBuffer(); |
---|
338 | if(!tabChar) { |
---|
339 | printf("malloc failed ...\n"); |
---|
340 | fflush(stdout); |
---|
341 | return; |
---|
342 | } |
---|
343 | |
---|
344 | psd = accept(sd,(struct sockaddr *)&from, &fromlen); |
---|
345 | printf("accept() returns <%d>\n", psd); |
---|
346 | fflush(stdout); |
---|
347 | |
---|
348 | for(;;){ |
---|
349 | rc = recv(psd, tabChar, SIZE_MAX_RCV_BUFFER, 0 ); |
---|
350 | |
---|
351 | if (rc == -1) { |
---|
352 | printf("recv() failed errno = %d\n", errno); |
---|
353 | fflush(stdout); |
---|
354 | close_s(sd); |
---|
355 | return; |
---|
356 | } |
---|
357 | |
---|
358 | if (rc == 0) { |
---|
359 | continue; |
---|
360 | } |
---|
361 | |
---|
362 | if (CheckBuffer(tabChar) == 0){ |
---|
363 | printf("Received %d bytes, data [%s] from client\n",rc,tabChar); |
---|
364 | printf("Packet received:<%d> bytes\n", rc); |
---|
365 | printf("Received:<"); |
---|
366 | fflush(stdout); |
---|
367 | tabChar += 8; |
---|
368 | for (i=0; i<rc ; i++) printf("%c",tabChar[i]); |
---|
369 | printf(">\n"); |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | FreeBuffer(tabChar); |
---|
374 | |
---|
375 | } |
---|
376 | |
---|
377 | |
---|
378 | rtems_task testTcpClientSocket( |
---|
379 | rtems_task_argument argument |
---|
380 | ){ |
---|
381 | int sd; |
---|
382 | struct sockaddr_in server; |
---|
383 | /*struct hostent *hp;*/ |
---|
384 | struct sockaddr_in from; |
---|
385 | int fromlen; |
---|
386 | int i,retval; |
---|
387 | char *tabChar; |
---|
388 | char *server_name= "localhost"; |
---|
389 | unsigned short port; |
---|
390 | unsigned long lenChar; |
---|
391 | struct timespec timeToWait, timeRem; |
---|
392 | |
---|
393 | /****************************************/ |
---|
394 | /* Now, we are going to test the socket */ |
---|
395 | /****************************************/ |
---|
396 | |
---|
397 | printf("Task testServerSocket : created\n\r"); |
---|
398 | fflush(stdout); |
---|
399 | |
---|
400 | port = DEFAULT_PORT_CLIENT; |
---|
401 | server_name = DEFAULT_IP_CLIENT; |
---|
402 | |
---|
403 | bzero((void *)(&server), sizeof(server)); |
---|
404 | |
---|
405 | sd = socket (AF_INET,SOCK_STREAM,0); |
---|
406 | printf("socket() result:<%d>\n", sd); |
---|
407 | fflush(stdout); |
---|
408 | |
---|
409 | if (sd<0) { |
---|
410 | printf("opening stream socket\n"); |
---|
411 | fflush(stdout); |
---|
412 | return; |
---|
413 | } |
---|
414 | |
---|
415 | server.sin_addr.s_addr = aton (server_name); |
---|
416 | |
---|
417 | server.sin_family = AF_INET; |
---|
418 | server.sin_port = htons(port); |
---|
419 | |
---|
420 | /* Connect to TCP/SERVER */ |
---|
421 | if ( (retval = connect(sd, (struct sockaddr *)&server, sizeof(server))) < 0 ) { |
---|
422 | close_s(sd); |
---|
423 | perror("connecting stream socket"); |
---|
424 | fflush(stdout); |
---|
425 | exit(0); |
---|
426 | } |
---|
427 | printf("connect() result:<%d>\n", retval); |
---|
428 | fflush(stdout); |
---|
429 | fromlen = sizeof(from); |
---|
430 | if ( (retval = getpeername(sd,(struct sockaddr *)&from,&fromlen)) <0){ |
---|
431 | perror("could't get peername\n"); |
---|
432 | fflush(stdout); |
---|
433 | exit(1); |
---|
434 | } |
---|
435 | printf("getpeername() result:<%d>\n", retval); |
---|
436 | printf("Connected to TCP/Server:"); |
---|
437 | printf("%s:%d\n", inet_ntoa((int32)(from.sin_addr.s_addr)), ntohs(from.sin_port)); |
---|
438 | fflush(stdout); |
---|
439 | |
---|
440 | printf("Now, let's send packets..."); |
---|
441 | fflush(stdout); |
---|
442 | |
---|
443 | while(1) { |
---|
444 | |
---|
445 | tabChar = BuildBuffer(); |
---|
446 | lenChar = *((unsigned long *)(tabChar)); |
---|
447 | |
---|
448 | i = send(sd, tabChar, lenChar, 0); |
---|
449 | if (i == -1) |
---|
450 | { |
---|
451 | fprintf(stderr,"send() failed (at least locally detected errors)\n"); |
---|
452 | fflush(stdout); |
---|
453 | return; |
---|
454 | } |
---|
455 | |
---|
456 | timeToWait.tv_sec = 1; |
---|
457 | timeToWait.tv_nsec = 0; |
---|
458 | nanosleep(&timeToWait, &timeRem); |
---|
459 | FreeBuffer(tabChar); |
---|
460 | |
---|
461 | } |
---|
462 | |
---|
463 | close_s(sd); |
---|
464 | } |
---|
465 | |
---|