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

📄 ncbi_socket.c

📁 ncbi源码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbi_socket.c,v $ * PRODUCTION Revision 1000.4  2004/06/01 18:45:25  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.145 * PRODUCTION * =========================================================================== *//*  $Id: ncbi_socket.c,v 1000.4 2004/06/01 18:45:25 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Author:  Denis Vakatov * * File Description: *   Plain portable TCP/IP socket API for:  UNIX, MS-Win, MacOS *     [UNIX ]   -DNCBI_OS_UNIX     -lresolv -lsocket -lnsl *     [MSWIN]   -DNCBI_OS_MSWIN    wsock32.lib *     [MacOS]   -DNCBI_OS_MAC      NCSASOCK -- BSD-style socket emulation lib * *//* NCBI core headers */#include "ncbi_ansi_ext.h"#include "ncbi_priv.h"/* The next header implicitly includes <connect/ncbi_socket.h> */#include <connect/ncbi_connutil.h>/* OS must be specified in the command-line ("-D....") or in the conf. header */#if !defined(NCBI_OS_UNIX) && !defined(NCBI_OS_MSWIN) && !defined(NCBI_OS_MAC)#  error "Unknown OS, must be one of NCBI_OS_UNIX, NCBI_OS_MSWIN, NCBI_OS_MAC!"#endif /*supported platforms*//* Uncomment these(or specify "-DHAVE_GETADDRINFO -DHAVE_GETNAMEINFO") only if: * 0) you are compiling this outside of the NCBI C or C++ Toolkits *    (USE_NCBICONF is not #define'd), and * 1) your platform has "getaddrinfo()" and "getnameinfo()", and * 2) you are going to use this API code in multi-thread application, and * 3) "gethostbyname()" gets called somewhere else in your code *//* #define HAVE_GETADDRINFO 1 *//* #define HAVE_GETNAMEINFO 1 *//* Uncomment this (or specify "-DHAVE_GETHOSTBY***_R=") only if: * 0) you are compiling this outside of the NCBI C or C++ Toolkits *    (USE_NCBICONF is not #define'd), and * 1) your platform has "gethostbyname_r()" but not "getnameinfo()", and * 2) you are going to use this API code in multi-thread application, and * 3) "gethostbyname()" gets called somewhere else in your code *//*   Solaris: *//* #define HAVE_GETHOSTBYNAME_R 5 *//* #define HAVE_GETHOSTBYADDR_R 7 *//*   Linux, IRIX: *//* #define HAVE_GETHOSTBYNAME_R 6 *//* #define HAVE_GETHOSTBYADDR_R 8 *//* Uncomment this (or specify "-DHAVE_SIN_LEN") only if: * 0) you are compiling this outside of the NCBI C or C++ Toolkits *    (USE_NCBICONF is not #define'd), and * 1) on your platform, struct sockaddr_in contains a field called "sin_len" *//* #define HAVE_SIN_LEN 1 *//* Platform-specific system headers */#if defined(NCBI_OS_UNIX)#  include <sys/time.h>#  include <unistd.h>#  ifdef NCBI_COMPILER_MW_MSL#    include <ncbi_mslextras.h>#  else#    include <netdb.h>#  endif#  include <fcntl.h>#  include <sys/socket.h>#  include <netinet/in.h>#  if !defined(NCBI_OS_BEOS) && !defined(NCBI_COMPILER_MW_MSL)#    include <arpa/inet.h>#  endif /*NCBI_OS_BEOS*/#  include <signal.h>#  include <sys/un.h>#elif defined(NCBI_OS_MSWIN)#  ifndef COMP_METRO#    include <winsock2.h>#  else#    define SD_RECEIVE      0x00#    define SD_SEND         0x01#    define SD_BOTH         0x02#  endif#elif defined(NCBI_OS_MAC)#  include <unistd.h>#  include <sock_ext.h>#  include <netdb.h>#  include <s_types.h>#  include <s_socket.h>#  include <neti_in.h>#  include <a_inet.h>#  include <neterrno.h> /* missing error numbers on Mac */#else#  error "Unsupported platform, must be one of NCBI_OS_UNIX, NCBI_OS_MSWIN, NCBI_OS_MAC !!!"#endif /* platform-specific headers (for UNIX, MSWIN, MAC) *//* Portable standard C headers */#include <errno.h>#include <stdlib.h>/****************************************************************************** *  TYPEDEFS & MACROS *//* Minimal size of the data buffer chunk in the socket internal buffer(s) */#define SOCK_BUF_CHUNK_SIZE 4096/* Macro #def for the platform-dependent constants, error codes and functions */#if defined(NCBI_OS_MSWIN)typedef SOCKET TSOCK_Handle;#  define SOCK_INVALID        INVALID_SOCKET#  define SOCK_ERRNO          WSAGetLastError()#  define SOCK_EINTR          WSAEINTR#  define SOCK_EWOULDBLOCK    WSAEWOULDBLOCK#  define SOCK_EADDRINUSE     WSAEADDRINUSE#  define SOCK_ECONNRESET     WSAECONNRESET#  define SOCK_EPIPE          WSAESHUTDOWN#  define SOCK_EAGAIN         WSAEINPROGRESS#  define SOCK_EINPROGRESS    WSAEINPROGRESS#  define SOCK_EALREADY       WSAEALREADY#  define SOCK_ENOTCONN       WSAENOTCONN#  define SOCK_ECONNABORTED   WSAECONNABORTED#  define SOCK_ECONNREFUSED   WSAECONNREFUSED#  define SOCK_ENETRESET      WSAENETRESET#  define SOCK_NFDS(s)        0#  define SOCK_CLOSE(s)       closesocket(s)#  define SOCK_SHUTDOWN(s,h)  shutdown(s,h)#  define SOCK_SHUTDOWN_RD    SD_RECEIVE#  define SOCK_SHUTDOWN_WR    SD_SEND#  define SOCK_SHUTDOWN_RDWR  SD_BOTH#  define SOCK_STRERROR(err)  s_StrError(err)/* NCBI_OS_MSWIN */#elif defined(NCBI_OS_UNIX)typedef int TSOCK_Handle;#  define SOCK_INVALID        (-1)#  define SOCK_ERRNO          errno#  define SOCK_EINTR          EINTR#  define SOCK_EWOULDBLOCK    EWOULDBLOCK#  define SOCK_EADDRINUSE     EADDRINUSE#  define SOCK_ECONNRESET     ECONNRESET#  define SOCK_EPIPE          EPIPE#  define SOCK_EAGAIN         EAGAIN#  define SOCK_EINPROGRESS    EINPROGRESS#  define SOCK_EALREADY       EALREADY#  define SOCK_ENOTCONN       ENOTCONN#  define SOCK_ECONNABORTED   ECONNABORTED#  define SOCK_ECONNREFUSED   ECONNREFUSED#  define SOCK_ENETRESET      ENETRESET#  define SOCK_NFDS(s)        (s + 1)#  ifdef NCBI_OS_BEOS#    define SOCK_CLOSE(s)     closesocket(s)#  else#    define SOCK_CLOSE(s)     close(s)	#  endif /*NCBI_OS_BEOS*/#  define SOCK_SHUTDOWN(s,h)  shutdown(s,h)#  ifndef SHUT_RD#    define SHUT_RD           0#  endif /*SHUT_RD*/#  define SOCK_SHUTDOWN_RD    SHUT_RD#  ifndef SHUT_WR#    define SHUT_WR           1#  endif /*SHUT_WR*/#  define SOCK_SHUTDOWN_WR    SHUT_WR#  ifndef SHUT_RDWR#    define SHUT_RDWR         2#  endif /*SHUT_RDWR*/#  define SOCK_SHUTDOWN_RDWR  SHUT_RDWR#  ifndef INADDR_NONE#    define INADDR_NONE       (unsigned int)(-1)#  endif /*INADDR_NONE*/#  define SOCK_STRERROR(err)  s_StrError(err)/* NCBI_OS_UNIX */#elif defined(NCBI_OS_MAC)#  if TARGET_API_MAC_CARBON#    define O_NONBLOCK kO_NONBLOCK#  endif /*TARGET_API_MAC_CARBON*/typedef int TSOCK_Handle;#  define SOCK_INVALID        (-1)#  ifndef SOCK_ERRNO#    define SOCK_ERRNO        errno#  endif /*SOCK_ERRNO*/#  define SOCK_EINTR          EINTR#  define SOCK_EWOULDBLOCK    EWOULDBLOCK#  define SOCK_EADDRINUSE     EADDRINUSE#  define SOCK_ECONNRESET     ECONNRESET#  define SOCK_EPIPE          EPIPE#  define SOCK_EAGAIN         EAGAIN#  define SOCK_EINPROGRESS    EINPROGRESS#  define SOCK_EALREADY       EALREADY#  define SOCK_ENOTCONN       ENOTCONN#  define SOCK_ECONNABORTED   ECONNABORTED#  define SOCK_ECONNREFUSED   ECONNREFUSED#  define SOCK_ENETRESET      ENETRESET#  define SOCK_NFDS(s)        (s + 1)#  define SOCK_CLOSE(s)       close(s)#  define SOCK_SHUTDOWN(s,h)  shutdown(s,h)#  define SOCK_SHUTDOWN_RD    0#  define SOCK_SHUTDOWN_WR    1#  define SOCK_SHUTDOWN_RDWR  2#  define SOCK_STRERROR(err)  s_StrError(err)#  ifdef NETDB_INTERNAL#    undef NETDB_INTERNAL#  endif /*NETDB_INTERNAL*/#endif /*NCBI_OS_MSWIN, NCBI_OS_UNIX, NCBI_OS_MAC*/#ifdef HAVE_SOCKLEN_Ttypedef socklen_t SOCK_socklen_t;#elsetypedef int       SOCK_socklen_t;#endif /*HAVE_SOCKLEN_T*//* Type of connecting socket (except listening) */typedef enum {    eSOCK_Datagram = 0,    eSOCK_ClientSide,    eSOCK_ServerSide,    eSOCK_ServerSideKeep} ESockType;#if 0/*defined(__GNUC__)*/typedef ESwitch    EBSwitch;typedef EIO_Status EBIO_Status;typedef ESockType  EBSockType;#elsetypedef unsigned   EBSwitch;typedef unsigned   EBIO_Status;typedef unsigned   EBSockType;#endif#define SET_LISTENING(s) ((s)->r_on_w  =   (unsigned) eDefault + 1)#define IS_LISTENING(s)  ((s)->r_on_w  ==  (unsigned) eDefault + 1)/* Listening socket */struct LSOCK_tag {    TSOCK_Handle    sock;       /* OS-specific socket handle                 */    unsigned int    id;         /* the internal ID (see also "s_ID_Counter") */    unsigned int    n_accept;   /* total number of accepted clients          */    unsigned short  port;       /* port on which the socket is listening     */    /* type, status, EOF, log, read-on-write etc bit-field indicators */    EBSwitch             log:2; /* how to log events and data for this socket*/    EBSockType          type:2; /* MBZ (NB: eSOCK_Datagram)                  */    EBSwitch          r_on_w:2; /* 3 [=(int)eDefault + 1]                    */    EBSwitch        i_on_sig:2; /* eDefault                                  */    EBIO_Status     r_status:3; /* MBZ (NB: eIO_Success)                     */    unsigned/*bool*/     eof:1; /* 0                                         */    EBIO_Status     w_status:3; /* MBZ (NB: eIO_Success)                     */    unsigned/*bool*/ pending:1; /* 0                                         */};/* Socket [it must be in one-2-one correspondence with LSOCK above] */struct SOCK_tag {    TSOCK_Handle    sock;       /* OS-specific socket handle                 */    unsigned int    id;         /* the internal ID (see also "s_ID_Counter") */    /* connection point */    unsigned int    host;       /* peer host (in the network byte order)     */    unsigned short  port;       /* peer port (in the network byte order)     */    /* type, status, EOF, log, read-on-write etc bit-field indicators */    EBSwitch             log:2; /* how to log events and data for this socket*/    EBSockType          type:2; /* socket type: client- or server-side, dgram*/    EBSwitch          r_on_w:2; /* enable/disable automatic read-on-write    */    EBSwitch        i_on_sig:2; /* enable/disable I/O restart on signals     */    EBIO_Status     r_status:3; /* read  status:  eIO_Closed if was shut down*/    unsigned/*bool*/     eof:1; /* Stream sockets: 'End of file' seen on read                                   Datagram socks: 'End of message' written  */    EBIO_Status     w_status:3; /* write status:  eIO_Closed if was shut down*/    unsigned/*bool*/ pending:1; /* != 0 if connection is still pending       */    /* timeouts */    const struct timeval* r_timeout;/* NULL if infinite, or points to "r_tv" */    struct timeval  r_tv;       /* finite read timeout value                 */    STimeout        r_to;       /* finite read timeout value (aux., temp.)   */    const struct timeval* w_timeout;/* NULL if infinite, or points to "w_tv" */    struct timeval  w_tv;       /* finite write timeout value                */    STimeout        w_to;       /* finite write timeout value (aux., temp.)  */    const struct timeval* c_timeout;/* NULL if infinite, or points to "c_tv" */    struct timeval  c_tv;       /* finite close timeout value                */    STimeout        c_to;       /* finite close timeout value (aux., temp.)  */    /* aux I/O data */    BUF             r_buf;      /* read  buffer                              */    BUF             w_buf;      /* write buffer                              */    size_t          w_len;      /* SOCK: how much data is pending for output */    /* statistics */    size_t          n_read;     /* DSOCK: total #; SOCK: last connect/ only  */    size_t          n_written;  /* DSOCK: total #; SOCK: last /session only  */    size_t          n_in;       /* DSOCK: msg #; SOCK: total # of bytes read */    size_t          n_out;      /* DSOCK: msg #; SOCK: total # of bytes sent */#ifdef NCBI_OS_UNIX    /* filename for UNIX socket */    char            file[1];    /* must go last                              */#endif /*NCBI_OS_UNIX*/};

⌨️ 快捷键说明

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