source: network-demos/netdemo/test.c @ c87143a

4.11ERIC-NORUMnetdemos-4-5-branchnetwork-demos-4-10-branchnetwork-demos-4-6-branchnetwork-demos-4-7-branchnetwork-demos-4-8-branchnetwork-demos-4-9-branchrtems-4-5-branch Demos-30May1998
Last change on this file since c87143a was c87143a, checked in by Joel Sherrill <joel.sherrill@…>, on 07/30/98 at 14:42:29

base from Eric Norum -- Demos.30May1998.tar.gz

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*
2 * Test KA9Q networking
3 *
4 * This program may be distributed and used for any purpose.
5 * I ask only that you:
6 *      1. Leave this author information intact.
7 *      2. Document any changes you make.
8 *
9 * W. Eric Norum
10 * Saskatchewan Accelerator Laboratory
11 * University of Saskatchewan
12 * Saskatoon, Saskatchewan, CANADA
13 * eric@skatter.usask.ca
14 */
15#include <stdio.h>
16#include <unistd.h>
17#include <rtems.h>
18#include <rtems/error.h>
19#include <socket.h>
20#include <sockaddr.h>
21#include <netuser.h>
22#include <rtems_ka9q.h>
23
24#define NSERVER         2
25#define BASE_PORT       24742
26
27#define DATA_SINK_HOST  "128.233.14.1"
28
29/*
30 * Display the contents of several KA9Q tables
31 */
32static void
33show_ka9q_tables (void)
34{
35        printf ("\n****************** MALLOC Statistics ***************\n");
36        malloc_dump ();
37        printf ("\n****************** MBUF Statistics ***************\n");
38        mbufstat ();
39        mbufsizes ();
40        printf ("\n****************** Routing Table ***************\n");
41        rtems_ka9q_execute_command ("route");
42        printf ("\n****************** ARP Table ***************\n");
43        rtems_ka9q_execute_command ("arp");
44        printf ("\n****************** Driver Statistics ***************\n");
45        rtems_ka9q_execute_command ("ifconfig rtems");
46        printf ("\n****************** Ip Statistics ***************\n");
47        rtems_ka9q_execute_command ("ip status");
48        printf ("\n****************** ICMP Statistics ***************\n");
49        rtems_ka9q_execute_command ("icmp status");
50        printf ("\n****************** UDP Statistics ***************\n");
51        rtems_ka9q_execute_command ("udp status");
52        printf ("\n****************** TCP Statistics ***************\n");
53        rtems_ka9q_execute_command ("tcp status");
54}
55
56/*
57 * Stress the transmit queue -- send a large number of UDP packets
58 */
59static void
60transmitUdp (void)
61{
62        int s;
63        int i;
64        static struct sockaddr_in myAddr, farAddr;
65        static char cbuf[800];
66        static char bigbuf[20000];
67
68        printf ("Create socket.\n");
69        s = socket (AF_INET, SOCK_DGRAM, 0);
70        if (s < 0)
71                rtems_panic ("Can't create socket: %s", strerror (errno));
72        myAddr.sin_family = AF_INET;
73        myAddr.sin_port = 1234;
74        myAddr.sin_addr.s_addr = INADDR_ANY;
75        printf ("Bind socket.\n");
76        if (bind (s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0)
77                rtems_panic ("Can't bind socket: %s", strerror (errno));
78        farAddr.sin_family = AF_INET;
79        farAddr.sin_port = 9;   /* The `discard' port */
80        farAddr.sin_addr.s_addr = 0xFFFFFFFF;
81        for (i = 0 ; i < 5 ; i++) {
82                if (sendto (s, cbuf, sizeof cbuf, 0, (struct sockaddr *)&farAddr, sizeof farAddr) < 0)
83                        rtems_panic ("Can't broadcast: %s", strerror (errno));
84        }
85        farAddr.sin_addr.s_addr = aton (DATA_SINK_HOST);
86        for (i = 0 ; i < 500 ; i++) {
87                if (sendto (s, cbuf, sizeof cbuf, 0, (struct sockaddr *)&farAddr, sizeof farAddr) < 0)
88                        rtems_panic ("Can't send: %s", strerror (errno));
89                if (sendto (s, cbuf, sizeof cbuf, 0, (struct sockaddr *)&farAddr, sizeof farAddr) < 0)
90                        rtems_panic ("Can't send: %s", strerror (errno));
91        }
92        for (i = 0 ; i < 2 ; i++) {
93                if (sendto (s, bigbuf, sizeof bigbuf, 0, (struct sockaddr *)&farAddr, sizeof farAddr) < 0)
94                        rtems_panic ("Can't send: %s", strerror (errno));
95                if (sendto (s, bigbuf, sizeof bigbuf, 0, (struct sockaddr *)&farAddr, sizeof farAddr) < 0)
96                        rtems_panic ("Can't send: %s", strerror (errno));
97        }
98        close (s);
99}
100       
101/*
102 * Stress the transmit queue -- send a large number of TCP packets
103 */
104static void
105transmitTcp (void)
106{
107        int s;
108        int i;
109        static struct sockaddr_in myAddr, farAddr;
110        static char cbuf[800];
111        static char bigbuf[20000];
112
113        printf ("Create socket.\n");
114        s = socket (AF_INET, SOCK_STREAM, 0);
115        if (s < 0)
116                rtems_panic ("Can't create socket: %s", strerror (errno));
117        myAddr.sin_family = AF_INET;
118        myAddr.sin_port = 1234;
119        myAddr.sin_addr.s_addr = INADDR_ANY;
120        printf ("Bind socket.\n");
121        if (bind (s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0)
122                rtems_panic ("Can't bind socket: %s", strerror (errno));
123        farAddr.sin_family = AF_INET;
124        farAddr.sin_port = 9;   /* The `discard' port */
125        farAddr.sin_addr.s_addr = aton (DATA_SINK_HOST);
126        if (connect (s, (struct sockaddr *)&farAddr, sizeof farAddr) < 0) {
127                printf ("Can't connect socket: %s\n", strerror (errno));
128                close (s);
129                return;
130        }
131        for (i = 0 ; i < 500 ; i++) {
132                if (write (s, cbuf, sizeof cbuf) < 0)
133                        rtems_panic ("Can't send: %s", strerror (errno));
134        }
135        for (i = 0 ; i < 2 ; i++) {
136                if (write (s, bigbuf, sizeof bigbuf) < 0)
137                        rtems_panic ("Can't send: %s", strerror (errno));
138        }
139        close (s);
140}
141
142/*
143 * Echo characters back to a telnet session
144 *
145 * With this running on the test machine you can go to
146 * another machine on your network and run:
147 *      telnet test_machine the_port_number_with_which_this_function_was_started
148 * Everything you type should be echoed back.
149 */
150static void
151echoTask (rtems_task_argument fd)
152{
153        char cbuf[512];
154        int n;
155        rtems_status_code sc;
156
157        for (;;) {
158#if 0
159                n = read (fd, cbuf, sizeof cbuf);
160#else
161                n = read (fd, cbuf, 1);
162#endif
163                if (n == 0) {
164                        printf ("EOF\n");
165                        break;
166                }
167                else if (n < 0) {
168                        rtems_panic ("Error receiving message: %s", strerror (errno));
169                }
170                printf ("Received: %d\n", n);
171                if (send (fd, cbuf, n, 0) < 0)
172                        rtems_panic ("Error sending message: %s", strerror (errno));
173                if (cbuf[0] == '\007')
174                        show_ka9q_tables ();
175                if (cbuf[0] == 'Q')
176                        break;
177        }
178        if (close (fd) < 0)
179                rtems_panic ("Can't close connection: %s", strerror (errno));
180        sc = rtems_task_delete (RTEMS_SELF);
181        rtems_panic ("Task deletion failed: %s", rtems_status_text (sc));
182}
183
184static void
185echoServer (unsigned short port)
186{
187        int s, s1;
188        struct sockaddr_in myAddr, farAddr;
189        int addrlen;
190        rtems_id tid;
191        rtems_task_priority my_priority;
192        rtems_status_code sc;
193        char c = 'a';
194
195        printf ("Create socket.\n");
196        s = socket (AF_INET, SOCK_STREAM, 0);
197        if (s < 0)
198                rtems_panic ("Can't create socket: %s", strerror (errno));
199        myAddr.sin_family = AF_INET;
200        myAddr.sin_port = port;
201        myAddr.sin_addr.s_addr = INADDR_ANY;
202        memset (myAddr.sin_zero, '\0', sizeof myAddr.sin_zero);
203        printf ("Bind socket.\n");
204        if (bind (s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0)
205                rtems_panic ("Can't bind socket: %s", strerror (errno));
206        printf ("Listen.\n");
207        if (listen (s, 2) < 0)
208                rtems_panic ("Can't listen on socket: %s", strerror (errno));
209        for (;;) {
210                printf ("Accept.\n");
211                addrlen = sizeof farAddr;
212                s1 = accept (s, (struct sockaddr *)&farAddr, &addrlen);
213                if (s1 < 0)
214                        rtems_panic ("Can't accept connection: %s", strerror (errno));
215                else
216                        printf ("ACCEPTED:%lX\n", farAddr.sin_addr.s_addr);
217
218                /*
219                 * Start an echo task
220                 */
221                rtems_task_set_priority (RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &my_priority);
222                sc = rtems_task_create (rtems_build_name ('E', 'C', 'H', c),
223                        my_priority,
224                        8*1024,
225                        RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
226                        RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
227                        &tid);
228                if (sc != RTEMS_SUCCESSFUL)
229                        rtems_panic ("Can't create echo task; %s\n", rtems_status_text (sc));
230                if (c == 'z')
231                        c = 'a';
232                else
233                        c++;
234                sc = rtems_task_start (tid, echoTask, s1);
235                if (sc != RTEMS_SUCCESSFUL)
236                        rtems_panic ("Can't start echo task; %s\n", rtems_status_text (sc));
237        }
238}
239
240/*
241 * Run an echo server
242 */
243static void
244runEchoServer (rtems_task_argument arg)
245{
246        echoServer (arg);
247        rtems_task_delete (RTEMS_SELF);
248}
249
250/*
251 * Test some socket stuff
252 */
253void
254doSocket (void)
255{
256        int i;
257        rtems_status_code sc;
258        rtems_task_priority my_priority;
259
260        /*
261         * Spawn other servers
262         */
263        rtems_task_set_priority (RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &my_priority);
264        for (i = 0 ; i < NSERVER ; i++) {
265                rtems_id tid;
266                sc = rtems_task_create (rtems_build_name ('S', 'R', 'V', 'A' + i),
267                        my_priority - 1,
268                        8*1024,
269                        RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
270                        RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
271                        &tid);
272                if (sc != RTEMS_SUCCESSFUL) {
273                        printf ("Can't create server; %s\n", rtems_status_text (sc));
274                        return;
275                }
276                sc = rtems_task_start (tid, runEchoServer, BASE_PORT + i);
277                if (sc != RTEMS_SUCCESSFUL) {
278                        printf ("Can't start server; %s\n", rtems_status_text (sc));
279                        return;
280                }
281        }
282
283        /*
284         * Wait for characters from console terminal
285         */
286        for (;;) {
287                switch (getchar ()) {
288                case '\004':
289                        return;
290
291                case 't':
292                        /*
293                         * Test the transmit queues
294                         */
295                        transmitTcp ();
296                        break;
297
298                case 'u':
299                        /*
300                         * Test the transmit queues
301                         */
302                        transmitUdp ();
303                        break;
304
305                case 's':
306                        /*
307                         * Show what's been accomplished
308                         */
309                        show_ka9q_tables ();
310                        break;
311                }
312        }
313}
Note: See TracBrowser for help on using the repository browser.