]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/common/v2_0/tests/server_test.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / common / v2_0 / tests / server_test.c
1 //==========================================================================
2 //
3 //      tests/server_test.c
4 //
5 //      Simple TCP 'server' 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):    gthomas
22 // Contributors: gthomas
23 // Date:         2000-01-10
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 static void
56 server_test(struct bootp *bp)
57 {
58     int s, client;
59     socklen_t client_len;
60     struct sockaddr_in client_addr, local;
61     char buf[256];
62     int one = 1;
63     fd_set in_fds;
64     int num, len;
65     struct timeval tv;
66
67     s = socket(AF_INET, SOCK_STREAM, 0);
68     if (s < 0) {
69         pexit("stream socket");
70     }
71     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
72         pexit("setsockopt SO_REUSEADDR");
73     }
74     if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
75         pexit("setsockopt SO_REUSEPORT");
76     }
77     memset(&local, 0, sizeof(local));
78     local.sin_family = AF_INET;
79     local.sin_len = sizeof(local);
80     local.sin_port = htons(7734);
81     local.sin_addr.s_addr = INADDR_ANY;
82     if(bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
83         pexit("bind error");
84     }
85     listen(s, SOMAXCONN);
86     while (true) {
87         client_len = sizeof(client_addr);
88         if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
89             pexit("accept");
90         }
91         client_len = sizeof(client_addr);
92         getpeername(client, (struct sockaddr *)&client_addr, &client_len);
93         diag_printf("connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
94
95         diag_sprintf(buf, "Hello %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
96         
97         write(client, buf, strlen(buf));
98         tv.tv_sec = 5;
99         tv.tv_usec = 0;
100         FD_ZERO(&in_fds);
101         FD_SET(client, &in_fds);
102         num = select(client+1, &in_fds, 0, 0, &tv);
103         if (num > 0) {
104             len = read(client, buf, sizeof(buf)-1);
105             buf[len] = '\0';
106             diag_printf("buf = '%s'\n", buf);
107         } else if (num == 0) {
108             diag_printf("No reply - timed out\n");
109         } else {
110             perror("select");
111         }
112         close(client);
113     }
114     close(s);
115 }
116
117 void
118 net_test(cyg_addrword_t param)
119 {
120     diag_printf("Start SERVER test\n");
121     init_all_network_interfaces();
122 #ifdef CYGHWR_NET_DRIVER_ETH0
123     if (eth0_up) {
124         server_test(&eth0_bootp_data);
125     }
126 #endif
127     cyg_test_exit();
128 }
129
130 void
131 cyg_start(void)
132 {
133     // Create a main thread, so we can run the scheduler and have time 'pass'
134     cyg_thread_create(10,                // Priority - just a number
135                       net_test,          // entry
136                       0,                 // entry parameter
137                       "Network test",    // Name
138                       &stack[0],         // Stack
139                       STACK_SIZE,        // Size
140                       &thread_handle,    // Handle
141                       &thread_data       // Thread data structure
142             );
143     cyg_thread_resume(thread_handle);  // Start it
144     cyg_scheduler_start();
145 }