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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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