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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [common/] [v2_0/] [tests/] [ga_server_test.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
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), 0) == EAI_NONE) {
125
                        diag_printf("connection from %s(%s)\n", host_addr_buf, host_port_buf);
126
                        diag_sprintf(buf, "Hello %s(%s)\n", host_addr_buf, host_port_buf);
127
                    } else {
128
                        _inet_ntop(&client_addr, addr_buf, sizeof(addr_buf));
129
                        diag_printf("connection from %s(%d)\n", addr_buf, _inet_port(&client_addr));
130
                        diag_sprintf(buf, "Hello %s(%d)\n", addr_buf, _inet_port(&client_addr));
131
                    }
132
                    write(client, buf, strlen(buf));
133
                    tv.tv_sec = 5;
134
                    tv.tv_usec = 0;
135
                    FD_ZERO(&in_fds);
136
                    FD_SET(client, &in_fds);
137
                    num = select(client+1, &in_fds, 0, 0, &tv);
138
                    if (num > 0) {
139
                        len = read(client, buf, sizeof(buf)-1);
140
                        buf[len-1] = '\0';  // Trim \n
141
                        diag_printf("buf = '%s'\n", buf);
142
                    } else if (num == 0) {
143
                        diag_printf("No reply - timed out\n");
144
                    } else {
145
                        perror("select");
146
                    }
147
                    close(client);
148
                }
149
            }
150
        } else {
151
            diag_printf("select returned: %d\n", num);
152
            pexit("bad select");
153
        }
154
    }
155
}
156
 
157
void
158
net_test(cyg_addrword_t param)
159
{
160
    diag_printf("Start SERVER test\n");
161
    init_all_network_interfaces();
162
#ifdef CYGHWR_NET_DRIVER_ETH0
163
    if (eth0_up) {
164
        server_test(&eth0_bootp_data);
165
    }
166
#endif
167
    cyg_test_exit();
168
}
169
 
170
void
171
cyg_start(void)
172
{
173
    // Create a main thread, so we can run the scheduler and have time 'pass'
174
    cyg_thread_create(10,                // Priority - just a number
175
                      net_test,          // entry
176
                      0,                 // entry parameter
177
                      "Network test",    // Name
178
                      &stack[0],         // Stack
179
                      STACK_SIZE,        // Size
180
                      &thread_handle,    // Handle
181
                      &thread_data       // Thread data structure
182
            );
183
    cyg_thread_resume(thread_handle);  // Start it
184
    cyg_scheduler_start();
185
}

powered by: WebSVN 2.1.0

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