]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/common/v2_0/tests/ga_server_test.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / common / v2_0 / tests / ga_server_test.c
1 //==========================================================================
2 //
3 //      tests/server_test.c
4 //
5 //      Simple TCP 'server' test - using getaddrinfo()
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):    gthomas
22 // Contributors: gthomas
23 // Date:         2002-03-05
24 // Purpose:      
25 // Description:  
26 //              
27 //
28 //####DESCRIPTIONEND####
29 //
30 //==========================================================================
31 // Network server test code
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <network.h>
35
36 #ifndef CYGPKG_LIBC_STDIO
37 #define perror(s) diag_printf(#s ": %s\n", strerror(errno))
38 #endif
39
40 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
41 static char stack[STACK_SIZE];
42 static cyg_thread thread_data;
43 static cyg_handle_t thread_handle;
44
45 extern void
46 cyg_test_exit(void);
47
48 void
49 pexit(char *s)
50 {
51     perror(s);
52     cyg_test_exit();
53 }
54
55 #define MAXSOCK 16
56 static void
57 server_test(struct bootp *bp)
58 {
59     int sock_indx, i, s, socks[MAXSOCK], client, client_len;
60     struct sockaddr client_addr;
61     char buf[256], addr_buf[256];
62     char host_addr_buf[256], host_port_buf[32];
63     int one = 1;
64     fd_set in_fds, src_fds;
65     int num, len;
66     struct timeval tv;
67     struct addrinfo *ai, *addrs, hints;
68     int err, last_sock;
69
70     bzero(&hints, sizeof(hints));
71     hints.ai_family = PF_UNSPEC;
72     hints.ai_socktype = SOCK_STREAM;
73     hints.ai_flags = AI_PASSIVE;
74     if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
75         diag_printf("can't getaddrinfo(): %s\n", gai_strerror(err));
76         pexit("getaddrinfo failed");
77     }
78     sock_indx = 0;  last_sock = -1;
79     ai = addrs;
80     while (ai) {
81         _inet_ntop(ai->ai_addr, addr_buf, sizeof(addr_buf));
82         diag_printf("Family: %d, Socket: %d, Addr: %s\n", ai->ai_family, ai->ai_socktype, addr_buf);
83         s = socket(ai->ai_family, ai->ai_socktype, 0);
84         if (s < 0) {
85             pexit("stream socket");
86         }
87         if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
88             pexit("setsockopt SO_REUSEADDR");
89         }
90         if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
91             pexit("setsockopt SO_REUSEPORT");
92         }
93         if(bind(s, ai->ai_addr, ai->ai_addr->sa_len) < 0) {
94             pexit("bind error");
95         }
96         listen(s, SOMAXCONN);
97         socks[sock_indx++] = s;
98         if (sock_indx >= MAXSOCK) {
99             pexit("Too many address types");
100         }
101         ai = ai->ai_next;
102         if (s > last_sock) last_sock = s;
103     }
104     while (true) {
105         // Wait for some activity on one of the ports
106         FD_ZERO(&src_fds);
107         for (s = 0;  s < sock_indx;  s++) {
108             FD_SET(socks[s], &src_fds);
109         }
110         num = select(last_sock+1, &src_fds, 0, 0, 0);
111         if (num > 0) {
112             // There are 'num' sockets ready to connect
113             for (i = 0;  i < sock_indx; i++) {
114                 s = socks[i];
115                 if (FD_ISSET(s, &src_fds)) {
116                     client_len = sizeof(client_addr);
117                     if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
118                         pexit("accept");
119                     }
120                     client_len = sizeof(client_addr);
121                     getpeername(client, &client_addr, &client_len);
122                     if (getnameinfo (&client_addr, client_len, 
123                                      host_addr_buf, sizeof(host_addr_buf),
124                                      host_port_buf, sizeof(host_port_buf), 
125                                      NI_NUMERICHOST) == EAI_NONE) {
126                         diag_printf("connection from %s(%s)\n", host_addr_buf, host_port_buf);
127                         diag_sprintf(buf, "Hello %s(%s)\n", host_addr_buf, host_port_buf);
128                     } else {
129                         _inet_ntop(&client_addr, addr_buf, sizeof(addr_buf));
130                         diag_printf("connection from %s(%d)\n", addr_buf, _inet_port(&client_addr));
131                         diag_sprintf(buf, "Hello %s(%d)\n", addr_buf, _inet_port(&client_addr));
132                     }
133                     write(client, buf, strlen(buf));
134                     tv.tv_sec = 5;
135                     tv.tv_usec = 0;
136                     FD_ZERO(&in_fds);
137                     FD_SET(client, &in_fds);
138                     num = select(client+1, &in_fds, 0, 0, &tv);
139                     if (num > 0) {
140                         len = read(client, buf, sizeof(buf)-1);
141                         buf[len-1] = '\0';  // Trim \n
142                         diag_printf("buf = '%s'\n", buf);
143                     } else if (num == 0) {
144                         diag_printf("No reply - timed out\n");
145                     } else {
146                         perror("select");
147                     }
148                     close(client);
149                 }
150             }
151         } else {
152             diag_printf("select returned: %d\n", num);
153             pexit("bad select");
154         }
155     }
156 }
157
158 void
159 net_test(cyg_addrword_t param)
160 {
161     diag_printf("Start SERVER test\n");
162     init_all_network_interfaces();
163 #ifdef CYGHWR_NET_DRIVER_ETH0
164     if (eth0_up) {
165         server_test(&eth0_bootp_data);
166     }
167 #endif
168     cyg_test_exit();
169 }
170
171 void
172 cyg_start(void)
173 {
174     // Create a main thread, so we can run the scheduler and have time 'pass'
175     cyg_thread_create(10,                // Priority - just a number
176                       net_test,          // entry
177                       0,                 // entry parameter
178                       "Network test",    // Name
179                       &stack[0],         // Stack
180                       STACK_SIZE,        // Size
181                       &thread_handle,    // Handle
182                       &thread_data       // Thread data structure
183             );
184     cyg_thread_resume(thread_handle);  // Start it
185     cyg_scheduler_start();
186 }