//==========================================================================
|
//==========================================================================
|
//
|
//
|
// tests/server_test.c
|
// tests/server_test.c
|
//
|
//
|
// Simple TCP 'server' test
|
// Simple TCP 'server' test
|
//
|
//
|
//==========================================================================
|
//==========================================================================
|
//####BSDCOPYRIGHTBEGIN####
|
//####BSDCOPYRIGHTBEGIN####
|
//
|
//
|
// -------------------------------------------
|
// -------------------------------------------
|
//
|
//
|
// Portions of this software may have been derived from OpenBSD or other sources,
|
// Portions of this software may have been derived from OpenBSD or other sources,
|
// and are covered by the appropriate copyright disclaimers included herein.
|
// and are covered by the appropriate copyright disclaimers included herein.
|
//
|
//
|
// -------------------------------------------
|
// -------------------------------------------
|
//
|
//
|
//####BSDCOPYRIGHTEND####
|
//####BSDCOPYRIGHTEND####
|
//==========================================================================
|
//==========================================================================
|
//#####DESCRIPTIONBEGIN####
|
//#####DESCRIPTIONBEGIN####
|
//
|
//
|
// Author(s): gthomas
|
// Author(s): gthomas
|
// Contributors: gthomas
|
// Contributors: gthomas
|
// Date: 2000-01-10
|
// Date: 2000-01-10
|
// Purpose:
|
// Purpose:
|
// Description:
|
// Description:
|
//
|
//
|
//
|
//
|
//####DESCRIPTIONEND####
|
//####DESCRIPTIONEND####
|
//
|
//
|
//==========================================================================
|
//==========================================================================
|
// Network server test code
|
// Network server test code
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <network.h>
|
#include <network.h>
|
|
|
#ifndef CYGPKG_LIBC_STDIO
|
#ifndef CYGPKG_LIBC_STDIO
|
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
|
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
|
#endif
|
#endif
|
|
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
|
static char stack[STACK_SIZE];
|
static char stack[STACK_SIZE];
|
static cyg_thread thread_data;
|
static cyg_thread thread_data;
|
static cyg_handle_t thread_handle;
|
static cyg_handle_t thread_handle;
|
|
|
extern void
|
extern void
|
cyg_test_exit(void);
|
cyg_test_exit(void);
|
|
|
void
|
void
|
pexit(char *s)
|
pexit(char *s)
|
{
|
{
|
perror(s);
|
perror(s);
|
cyg_test_exit();
|
cyg_test_exit();
|
}
|
}
|
|
|
static void
|
static void
|
server_test(struct bootp *bp)
|
server_test(struct bootp *bp)
|
{
|
{
|
int s, client, client_len;
|
int s, client, client_len;
|
struct sockaddr_in client_addr, local;
|
struct sockaddr_in client_addr, local;
|
char buf[256];
|
char buf[256];
|
int one = 1;
|
int one = 1;
|
fd_set in_fds;
|
fd_set in_fds;
|
int num, len;
|
int num, len;
|
struct timeval tv;
|
struct timeval tv;
|
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
if (s < 0) {
|
if (s < 0) {
|
pexit("stream socket");
|
pexit("stream socket");
|
}
|
}
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
|
pexit("setsockopt SO_REUSEADDR");
|
pexit("setsockopt SO_REUSEADDR");
|
}
|
}
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
|
pexit("setsockopt SO_REUSEPORT");
|
pexit("setsockopt SO_REUSEPORT");
|
}
|
}
|
memset(&local, 0, sizeof(local));
|
memset(&local, 0, sizeof(local));
|
local.sin_family = AF_INET;
|
local.sin_family = AF_INET;
|
local.sin_len = sizeof(local);
|
local.sin_len = sizeof(local);
|
local.sin_port = htons(7734);
|
local.sin_port = htons(7734);
|
local.sin_addr.s_addr = INADDR_ANY;
|
local.sin_addr.s_addr = INADDR_ANY;
|
if(bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
|
if(bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
|
pexit("bind error");
|
pexit("bind error");
|
}
|
}
|
listen(s, SOMAXCONN);
|
listen(s, SOMAXCONN);
|
while (true) {
|
while (true) {
|
client_len = sizeof(client_addr);
|
client_len = sizeof(client_addr);
|
if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
|
if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
|
pexit("accept");
|
pexit("accept");
|
}
|
}
|
client_len = sizeof(client_addr);
|
client_len = sizeof(client_addr);
|
getpeername(client, (struct sockaddr *)&client_addr, &client_len);
|
getpeername(client, (struct sockaddr *)&client_addr, &client_len);
|
diag_printf("connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
diag_printf("connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
|
|
#ifdef CYGPKG_LIBC_STDIO
|
#ifdef CYGPKG_LIBC_STDIO
|
sprintf(buf, "Hello %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
sprintf(buf, "Hello %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
#else
|
#else
|
strcpy(buf, "Hello ");
|
strcpy(buf, "Hello ");
|
strcat(buf, inet_ntoa(client_addr.sin_addr));
|
strcat(buf, inet_ntoa(client_addr.sin_addr));
|
strcat(buf,":");
|
strcat(buf,":");
|
strcat(buf, atoi(ntohs(client_addr.sin_port)));
|
strcat(buf, atoi(ntohs(client_addr.sin_port)));
|
strcat(buf,"\n");
|
strcat(buf,"\n");
|
#endif
|
#endif
|
|
|
write(client, buf, strlen(buf));
|
write(client, buf, strlen(buf));
|
tv.tv_sec = 5;
|
tv.tv_sec = 5;
|
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
FD_ZERO(&in_fds);
|
FD_ZERO(&in_fds);
|
FD_SET(client, &in_fds);
|
FD_SET(client, &in_fds);
|
num = select(client+1, &in_fds, 0, 0, &tv);
|
num = select(client+1, &in_fds, 0, 0, &tv);
|
if (num > 0) {
|
if (num > 0) {
|
len = read(client, buf, sizeof(buf)-1);
|
len = read(client, buf, sizeof(buf)-1);
|
buf[len] = '\0';
|
buf[len] = '\0';
|
diag_printf("buf = '%s'\n", buf);
|
diag_printf("buf = '%s'\n", buf);
|
} else if (num == 0) {
|
} else if (num == 0) {
|
diag_printf("No reply - timed out\n");
|
diag_printf("No reply - timed out\n");
|
} else {
|
} else {
|
perror("select");
|
perror("select");
|
}
|
}
|
close(client);
|
close(client);
|
}
|
}
|
close(s);
|
close(s);
|
}
|
}
|
|
|
void
|
void
|
net_test(cyg_addrword_t param)
|
net_test(cyg_addrword_t param)
|
{
|
{
|
diag_printf("Start SERVER test\n");
|
diag_printf("Start SERVER test\n");
|
init_all_network_interfaces();
|
init_all_network_interfaces();
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
if (eth0_up) {
|
if (eth0_up) {
|
server_test(ð0_bootp_data);
|
server_test(ð0_bootp_data);
|
}
|
}
|
#endif
|
#endif
|
cyg_test_exit();
|
cyg_test_exit();
|
}
|
}
|
|
|
void
|
void
|
cyg_start(void)
|
cyg_start(void)
|
{
|
{
|
// Create a main thread, so we can run the scheduler and have time 'pass'
|
// Create a main thread, so we can run the scheduler and have time 'pass'
|
cyg_thread_create(10, // Priority - just a number
|
cyg_thread_create(10, // Priority - just a number
|
net_test, // entry
|
net_test, // entry
|
0, // entry parameter
|
0, // entry parameter
|
"Network test", // Name
|
"Network test", // Name
|
&stack[0], // Stack
|
&stack[0], // Stack
|
STACK_SIZE, // Size
|
STACK_SIZE, // Size
|
&thread_handle, // Handle
|
&thread_handle, // Handle
|
&thread_data // Thread data structure
|
&thread_data // Thread data structure
|
);
|
);
|
cyg_thread_resume(thread_handle); // Start it
|
cyg_thread_resume(thread_handle); // Start it
|
cyg_scheduler_start();
|
cyg_scheduler_start();
|
}
|
}
|
|
|