⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sock.c

📁 Wine-20031016
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Unit test suite for winsock functions * * Copyright 2002 Martin Wilck * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <stdarg.h>#include <windef.h>#include <winbase.h>#include <winsock2.h>#include <mswsock.h>#include "wine/test.h"#include <winnt.h>#include <winerror.h>#define MAX_CLIENTS 4      /* Max number of clients */#define NUM_TESTS   2      /* Number of tests performed */#define FIRST_CHAR 'A'     /* First character in transferred pattern */#define BIND_SLEEP 10      /* seconds to wait between attempts to bind() */#define BIND_TRIES 6       /* Number of bind() attempts */#define TEST_TIMEOUT 30    /* seconds to wait before killing child threads                              after server initialization, if something hangs */#define wsa_ok(op, cond, msg) \   do { \        int tmp, err = 0; \        tmp = op; \        if ( !(cond tmp) ) err = WSAGetLastError(); \        ok ( cond tmp, msg, GetCurrentThreadId(), err); \   } while (0);/**************** Structs and typedefs ***************/typedef struct thread_info{    HANDLE thread;    DWORD id;} thread_info;/* Information in the server about open client connections */typedef struct sock_info{    SOCKET                 s;    struct sockaddr_in     addr;    struct sockaddr_in     peer;    char                  *buf;    int                    nread;} sock_info;/* Test parameters for both server & client */typedef struct test_params{    int          sock_type;    int          sock_prot;    const char  *inet_addr;    short        inet_port;    int          chunk_size;    int          n_chunks;    int          n_clients;} test_params;/* server-specific test parameters */typedef struct server_params{    test_params   *general;    DWORD          sock_flags;    int            buflen;} server_params;/* client-specific test parameters */typedef struct client_params{    test_params   *general;    DWORD          sock_flags;    int            buflen;} client_params;/* This type combines all information for setting up a test scenario */typedef struct test_setup{    test_params              general;    LPVOID                   srv;    server_params            srv_params;    LPVOID                   clt;    client_params            clt_params;} test_setup;/* Thread local storage for server */typedef struct server_memory{    SOCKET                  s;    struct sockaddr_in      addr;    sock_info               sock[MAX_CLIENTS];} server_memory;/* Thread local storage for client */typedef struct client_memory{    SOCKET s;    struct sockaddr_in      addr;    char                   *send_buf;    char                   *recv_buf;} client_memory;/**************** Static variables ***************/static DWORD      tls;              /* Thread local storage index */static HANDLE     thread[1+MAX_CLIENTS];static DWORD      thread_id[1+MAX_CLIENTS];static HANDLE     server_ready;static HANDLE     client_ready[MAX_CLIENTS];static int        client_id;/**************** General utility functions ***************/static void set_so_opentype ( BOOL overlapped ){    int optval = !overlapped, newval, len = sizeof (int);    ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,                      (LPVOID) &optval, sizeof (optval) ) == 0,         "setting SO_OPENTYPE failed" );    ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,                      (LPVOID) &newval, &len ) == 0,         "getting SO_OPENTYPE failed" );    ok ( optval == newval, "failed to set SO_OPENTYPE" );}static int set_blocking ( SOCKET s, BOOL blocking ){    u_long val = !blocking;    return ioctlsocket ( s, FIONBIO, &val );}static void fill_buffer ( char *buf, int chunk_size, int n_chunks ){    char c, *p;    for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )        memset ( p, c, chunk_size );}static char* test_buffer ( char *buf, int chunk_size, int n_chunks ){    char c, *p;    int i;    for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )    {        for ( i = 0; i < chunk_size; i++ )            if ( p[i] != c ) return p + i;    }    return NULL;}/* * This routine is called when a client / server does not expect any more data, * but needs to acknowedge the closing of the connection (by reasing 0 bytes). */static void read_zero_bytes ( SOCKET s ){    char buf[256];    int tmp, n = 0;    while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )        n += tmp;    ok ( n <= 0, "garbage data received: %d bytes\n", n );}static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen ){    char* last = buf + buflen, *p;    int n = 1;    for ( p = buf; n > 0 && p < last; p += n )        n = send ( s, p, min ( sendlen, last - p ), 0 );    wsa_ok ( n, 0 <=, "do_synchronous_send (%lx): error %d" );    return p - buf;}static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen ){    char* last = buf + buflen, *p;    int n = 1;    for ( p = buf; n > 0 && p < last; p += n )        n = recv ( s, p, min ( recvlen, last - p ), 0 );    wsa_ok ( n, 0 <=, "do_synchronous_recv (%lx): error %d:" );    return p - buf;}/* *  Call this routine right after thread startup. *  SO_OPENTYPE must by 0, regardless what the server did. */static void check_so_opentype (void){    int tmp = 1, len;    len = sizeof (tmp);    getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );    ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d", tmp );}/**************** Server utility functions ***************//* *  Even if we have closed our server socket cleanly, *  the OS may mark the address "in use" for some time - *  this happens with native Linux apps, too. */static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen ){    int err, wsaerr = 0, n_try = BIND_TRIES;    while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&            ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&            n_try-- >= 0)    {        trace ( "address in use, waiting ...\n" );        Sleep ( 1000 * BIND_SLEEP );    }    ok ( err == 0, "failed to bind: %d\n", wsaerr );}static void server_start ( server_params *par ){    int i;    test_params *gen = par->general;    server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));    TlsSetValue ( tls, mem );    mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,                          NULL, 0, par->sock_flags );    ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed" );    mem->addr.sin_family = AF_INET;    mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );    mem->addr.sin_port = htons ( gen->inet_port );    for (i = 0; i < MAX_CLIENTS; i++)    {        mem->sock[i].s = INVALID_SOCKET;        mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );        mem->sock[i].nread = 0;    }    if ( gen->sock_type == SOCK_STREAM )        do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );}static void server_stop (void){    int i;    server_memory *mem = TlsGetValue ( tls );    for (i = 0; i < MAX_CLIENTS; i++ )    {        LocalFree ( (HANDLE) mem->sock[i].buf );        if ( mem->sock[i].s != INVALID_SOCKET )            closesocket ( mem->sock[i].s );    }    ok ( closesocket ( mem->s ) == 0, "closesocket failed" );    LocalFree ( (HANDLE) mem );    ExitThread ( GetCurrentThreadId () );}/**************** Client utilitiy functions ***************/static void client_start ( client_params *par ){    test_params *gen = par->general;    client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));    TlsSetValue ( tls, mem );    WaitForSingleObject ( server_ready, INFINITE );    mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,                          NULL, 0, par->sock_flags );    mem->addr.sin_family = AF_INET;    mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );    mem->addr.sin_port = htons ( gen->inet_port );    ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed" );    mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );    mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;    fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );    SetEvent ( client_ready[client_id] );    /* Wait for the other clients to come up */    WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );}static void client_stop (void){    client_memory *mem = TlsGetValue ( tls );    wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%lx): %d\n" );    LocalFree ( (HANDLE) mem->send_buf );    LocalFree ( (HANDLE) mem );    ExitThread(0);}/**************** Servers ***************//* * simple_server: A very basic server doing synchronous IO. */static VOID WINAPI simple_server ( server_params *par ){    test_params *gen = par->general;    server_memory *mem;    int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,        id = GetCurrentThreadId();    char *p;    trace ( "simple_server (%x) starting\n", id );    set_so_opentype ( FALSE ); /* non-overlapped */    server_start ( par );    mem = TlsGetValue ( tls );    wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%lx): failed to set blocking mode: %d");    wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%lx): listen failed: %d");    trace ( "simple_server (%x) ready\n", id );    SetEvent ( server_ready ); /* notify clients */    for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )    {        trace ( "simple_server (%x): waiting for client\n", id );        /* accept a single connection */        tmp = sizeof ( mem->sock[0].peer );        mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );        wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%lx): accept failed: %d" );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -