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/] [ipv6_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/ipv6_server_test.c
4
//
5
//      Simple TCP 'server' test - modified for IPv6
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, client_len;
59
    struct sockaddr_in6 client_addr, local;
60
    char buf[256], addr_buf[256];
61
    int one = 1;
62
    fd_set in_fds;
63
    int num, len;
64
    struct timeval tv;
65
 
66
    s = socket(AF_INET6, SOCK_STREAM, 0);
67
    if (s < 0) {
68
        pexit("stream socket");
69
    }
70
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
71
        pexit("setsockopt SO_REUSEADDR");
72
    }
73
    if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
74
        pexit("setsockopt SO_REUSEPORT");
75
    }
76
    memset(&local, 0, sizeof(local));
77
    local.sin6_family = AF_INET6;
78
    local.sin6_len = sizeof(local);
79
    local.sin6_port = htons(7734);
80
    local.sin6_addr = in6addr_any;
81
    if(bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
82
        pexit("bind error");
83
    }
84
    listen(s, SOMAXCONN);
85
    while (true) {
86
        client_len = sizeof(client_addr);
87
        if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
88
            pexit("accept");
89
        }
90
        client_len = sizeof(client_addr);
91
        getpeername(client, (struct sockaddr *)&client_addr, &client_len);
92
        inet_ntop(AF_INET6, (char *)&client_addr.sin6_addr, addr_buf, sizeof(addr_buf));
93
        diag_printf("connection from %s:%d\n", addr_buf, ntohs(client_addr.sin6_port));
94
        diag_sprintf(buf, "Hello %s:%d\n", addr_buf, ntohs(client_addr.sin6_port));
95
        write(client, buf, strlen(buf));
96
        tv.tv_sec = 5;
97
        tv.tv_usec = 0;
98
        FD_ZERO(&in_fds);
99
        FD_SET(client, &in_fds);
100
        num = select(client+1, &in_fds, 0, 0, &tv);
101
        if (num > 0) {
102
            len = read(client, buf, sizeof(buf)-1);
103
            buf[len] = '\0';
104
            diag_printf("buf = '%s'\n", buf);
105
        } else if (num == 0) {
106
            diag_printf("No reply - timed out\n");
107
        } else {
108
            perror("select");
109
        }
110
        sprintf(buf, "Thanks, bye now!\n");
111
        write(client, buf, strlen(buf));
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
}

powered by: WebSVN 2.1.0

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