OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [ga_server_test.c] - Blame information for rev 856

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.