Home | History | Annotate | Download | only in qemu
      1 /* Copyright (C) 2007-2008 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 /* headers to use the BSD sockets */
     13 #ifndef QEMU_SOCKET_H
     14 #define QEMU_SOCKET_H
     15 
     16 #include <stddef.h>
     17 #include <stdint.h>
     18 #include <errno.h>
     19 
     20 /* we're going to hide the implementation details of sockets behind
     21  * a simple wrapper interface declared here.
     22  *
     23  * all socket operations set the global 'errno' variable on error.
     24  * this is unlike Winsock which instead modifies another internal
     25  * variable accessed through WSAGetLastError() and WSASetLastError()
     26  */
     27 
     28 /* the wrapper will convert any Winsock error message into an errno
     29  * code for you. There are however a few standard Unix error codes
     30  * that are not defined by the MS C library headers, so we add them
     31  * here. We use the official Winsock error codes, which are documented
     32  * even though we don't want to include the Winsock headers
     33  */
     34 #ifdef _WIN32
     35 #  ifndef EINTR
     36 #    define EINTR        10004
     37 #  endif
     38 #  ifndef EAGAIN
     39 #    define EAGAIN       10035
     40 #  endif
     41 #  ifndef EWOULDBLOCK
     42 #    define EWOULDBLOCK  EAGAIN
     43 #  endif
     44 #  ifndef EINPROGRESS
     45 #    define EINPROGRESS  10036
     46 #  endif
     47 #  ifndef EALREADY
     48 #    define EALREADY     10037
     49 #  endif
     50 #  ifndef EDESTADDRREQ
     51 #    define EDESTADDRREQ 10039
     52 #  endif
     53 #  ifndef EMSGSIZE
     54 #    define EMSGSIZE     10040
     55 #  endif
     56 #  ifndef EPROTOTYPE
     57 #    define EPROTOTYPE   10041
     58 #  endif
     59 #  ifndef ENOPROTOOPT
     60 #    define ENOPROTOOPT  10042
     61 #  endif
     62 #  ifndef EAFNOSUPPORT
     63 #    define EAFNOSUPPORT 10047
     64 #  endif
     65 #  ifndef EADDRINUSE
     66 #    define EADDRINUSE   10048
     67 #  endif
     68 #  ifndef EADDRNOTAVAIL
     69 #    define EADDRNOTAVAIL 10049
     70 #  endif
     71 #  ifndef ENETDOWN
     72 #    define ENETDOWN     10050
     73 #  endif
     74 #  ifndef ENETUNREACH
     75 #    define ENETUNREACH  10051
     76 #  endif
     77 #  ifndef ENETRESET
     78 #    define ENETRESET    10052
     79 #  endif
     80 #  ifndef ECONNABORTED
     81 #    define ECONNABORTED 10053
     82 #  endif
     83 #  ifndef ECONNRESET
     84 #    define ECONNRESET   10054
     85 #  endif
     86 #  ifndef ENOBUFS
     87 #    define ENOBUFS      10055
     88 #  endif
     89 #  ifndef EISCONN
     90 #    define EISCONN      10056
     91 #  endif
     92 #  ifndef ENOTCONN
     93 #    define ENOTCONN     10057
     94 #  endif
     95 #  ifndef ESHUTDOWN
     96 #    define ESHUTDOWN     10058
     97 #  endif
     98 #  ifndef ETOOMANYREFS
     99 #    define ETOOMANYREFS  10059
    100 #  endif
    101 #  ifndef ETIMEDOUT
    102 #    define ETIMEDOUT     10060
    103 #  endif
    104 #  ifndef ECONNREFUSED
    105 #    define ECONNREFUSED  10061
    106 #  endif
    107 #  ifndef ELOOP
    108 #    define ELOOP         10062
    109 #  endif
    110 #  ifndef EHOSTDOWN
    111 #    define EHOSTDOWN     10064
    112 #  endif
    113 #  ifndef EHOSTUNREACH
    114 #    define EHOSTUNREACH  10065
    115 #  endif
    116 #endif /* _WIN32 */
    117 
    118 /* Define 'errno_str' as a handy macro to return the string
    119  * corresponding to a given errno code. On Unix, this is
    120  * equivalent to strerror(errno), but on Windows, this will
    121  * take care of Winsock-originated errors as well.
    122  */
    123 #ifdef _WIN32
    124   extern const char*  _errno_str(void);
    125 #  define  errno_str   _errno_str()
    126 #else
    127 #  define  errno_str   strerror(errno)
    128 #endif
    129 
    130 /* always enable IPv6 sockets for now.
    131  * the QEMU internal router is not capable of
    132  * supporting them, but we plan to replace it
    133  * with something better in the future.
    134  */
    135 #define  HAVE_IN6_SOCKETS   1
    136 
    137 /* Unix sockets are not available on Win32 */
    138 #ifndef _WIN32
    139 #  define  HAVE_UNIX_SOCKETS  1
    140 #endif
    141 
    142 /* initialize the socket sub-system. this must be called before
    143  * using any of the declarations below.
    144  */
    145 int  socket_init( void );
    146 
    147 /* return the name of the current host */
    148 char*  host_name( void );
    149 
    150 /* supported socket types */
    151 typedef enum {
    152     SOCKET_DGRAM = 0,
    153     SOCKET_STREAM
    154 } SocketType;
    155 
    156 /* supported socket families */
    157 typedef enum {
    158     SOCKET_UNSPEC,
    159     SOCKET_INET,
    160     SOCKET_IN6,
    161     SOCKET_UNIX
    162 } SocketFamily;
    163 
    164 /* Generic socket address structure. Note that for Unix
    165  * sockets, the path is stored in a heap-allocated block,
    166  * unless the 'owner' field is cleared. If this is the case,
    167  */
    168 typedef struct {
    169     SocketFamily  family;
    170     union {
    171         struct {
    172             uint16_t   port;
    173             uint32_t   address;
    174         } inet;
    175         struct {
    176             uint16_t   port;
    177             uint8_t    address[16];
    178         } in6;
    179         struct {
    180             int          owner;
    181             const char*  path;
    182         } _unix;
    183     } u;
    184 } SockAddress;
    185 
    186 #define  SOCK_ADDRESS_INET_ANY       0x00000000
    187 #define  SOCK_ADDRESS_INET_LOOPBACK  0x7f000001
    188 
    189 /* initialize a new IPv4 socket address, the IP address and port are
    190  * in host endianess.
    191  */
    192 void  sock_address_init_inet( SockAddress*  a, uint32_t  ip, uint16_t  port );
    193 
    194 /* Initialize an IPv6 socket address, the address is in network order
    195  * and the port in host endianess.
    196  */
    197 #if HAVE_IN6_SOCKETS
    198 void  sock_address_init_in6 ( SockAddress*  a, const uint8_t*  ip6[16], uint16_t  port );
    199 #endif
    200 
    201 /* Intialize a Unix socket address, this will copy the 'path' string into the
    202  * heap. You need to call sock_address_done() to release the copy
    203  */
    204 #if HAVE_UNIX_SOCKETS
    205 void  sock_address_init_unix( SockAddress*  a, const char*  path );
    206 #endif
    207 
    208 /* Finalize a socket address, only needed for now for Unix addresses */
    209 void  sock_address_done( SockAddress*  a );
    210 
    211 int   sock_address_equal( const SockAddress*  a, const SockAddress*  b );
    212 
    213 /* THIS SHOULD DISAPPEAR SOON - TRANSITIONAL HELPER */
    214 int   sock_address_to_bsd( const SockAddress*  a, void*  sa, size_t* salen );
    215 int   sock_address_from_bsd( SockAddress*  a, const void*  sa, size_t  salen );
    216 int   sock_address_to_inet( SockAddress*  a, int  *paddr_ip, int  *paddr_port );
    217 
    218 /* return a static string describing the address */
    219 const char*  sock_address_to_string( const SockAddress*  a );
    220 
    221 static __inline__
    222 SocketFamily  sock_address_get_family( const SockAddress*  a )
    223 {
    224     return a->family;
    225 }
    226 
    227 /* return the port number of a given socket address, or -1 if it's a Unix one */
    228 int   sock_address_get_port( const SockAddress*  a );
    229 
    230 /* set the port number of a given socket address, don't do anything for Unix ones */
    231 void  sock_address_set_port( SockAddress*  a, uint16_t  port );
    232 
    233 /* return the path of a given Unix socket, returns NULL for non-Unix ones */
    234 const char*  sock_address_get_path( const SockAddress*  a );
    235 
    236 /* return the inet address, or -1 if it's not SOCKET_INET */
    237 int   sock_address_get_ip( const SockAddress*  a );
    238 
    239 /* bufprint a socket address into a human-readable string */
    240 char* bufprint_sock_address( char*  p, char*  end, const SockAddress*  a );
    241 
    242 /* resolve a hostname or decimal IPv4/IPv6 address into a socket address.
    243  * returns 0 on success, or -1 on failure. Note that the values or errno
    244  * set by this function are the following:
    245  *
    246  *   EINVAL    : invalid argument
    247  *   EHOSTDOWN : could not reach DNS server
    248  *   ENOENT    : no host with this name, or host doesn't have any IP address
    249  *   ENOMEM    : not enough memory to perform request
    250  */
    251 int   sock_address_init_resolve( SockAddress*  a,
    252                                  const char*   hostname,
    253                                  uint16_t      port,
    254                                  int           preferIn6 );
    255 
    256 int  sock_address_get_numeric_info( SockAddress*  a,
    257                                     char*         host,
    258                                     size_t        hostlen,
    259                                     char*         serv,
    260                                     size_t        servlen );
    261 
    262 /* Support for listing all socket addresses of a given host */
    263 enum {
    264     SOCKET_LIST_PASSIVE    = (1 << 0),
    265     SOCKET_LIST_FORCE_INET = (1 << 1),
    266     SOCKET_LIST_FORCE_IN6  = (1 << 2)
    267 };
    268 
    269 /* resolve a host and service/port name into a list of SockAddress objects.
    270  * returns a NULL-terminated array of SockAddress pointers on success,
    271  * or NULL in case of failure, with the value of errno set to one of the
    272  * following:
    273  *
    274  *    EINVAL    : invalid argument
    275  *    EHOSTDOWN : could not reach DNS server
    276  *    ENOENT    : no host with this name, or host doesn't have IP address
    277  *    ENOMEM    : not enough memory to perform request
    278  *
    279  * other system-level errors can also be set depending on the host sockets
    280  * implementation.
    281  *
    282  * This function loops on EINTR so the caller shouldn't have to check for it.
    283  */
    284 SockAddress**  sock_address_list_create( const char*  hostname,
    285                                          const char*  port,
    286                                          unsigned     flags );
    287 
    288 void sock_address_list_free( SockAddress**  list );
    289 
    290 /* create a new socket, return the socket number of -1 on failure */
    291 int  socket_create( SocketFamily  family, SocketType  type );
    292 
    293 /* create a new socket intended for IPv4 communication. returns the socket number,
    294  * or -1 on failure.
    295  */
    296 int   socket_create_inet( SocketType  type );
    297 
    298 /* create a new socket intended for IPv6 communication. returns the socket number,
    299  * or -1 on failure.
    300  */
    301 #if HAVE_IN6_SOCKETS
    302 int   socket_create_in6 ( SocketType  type );
    303 #endif
    304 
    305 /* create a unix/local domain socket. returns the socket number,
    306  * or -1 on failure.
    307  */
    308 #if HAVE_UNIX_SOCKETS
    309 int   socket_create_unix( SocketType  type );
    310 #endif
    311 
    312 /* return the type of a given socket */
    313 SocketType  socket_get_type(int  fd);
    314 
    315 /* set SO_REUSEADDR on Unix, SO_EXCLUSIVEADDR on Windows */
    316 int  socket_set_xreuseaddr(int  fd);
    317 
    318 /* set socket in non-blocking mode */
    319 int  socket_set_nonblock(int fd);
    320 
    321 /* set socket in blocking mode */
    322 int  socket_set_blocking(int fd);
    323 
    324 /* disable the TCP Nagle algorithm for lower latency */
    325 int  socket_set_nodelay(int fd);
    326 
    327 /* send OOB data inline for this socket */
    328 int  socket_set_oobinline(int  fd);
    329 
    330 /* force listening to IPv6 interfaces only */
    331 int  socket_set_ipv6only(int  fd);
    332 
    333 /* retrieve last socket error code */
    334 int  socket_get_error(int  fd);
    335 
    336 /* close an opened socket. Note that this is unlike the Unix 'close' because:
    337  * - it will properly shutdown the socket in the background
    338  * - it does not modify errno
    339  */
    340 void  socket_close( int  fd );
    341 
    342 /* the following functions are equivalent to the BSD sockets ones
    343  */
    344 int   socket_recv    ( int  fd, void*  buf, int  buflen );
    345 int   socket_recvfrom( int  fd, void*  buf, int  buflen, SockAddress*  from );
    346 
    347 int   socket_send  ( int  fd, const void*  buf, int  buflen );
    348 int   socket_send_oob( int  fd, const void*  buf, int  buflen );
    349 int   socket_sendto( int  fd, const void*  buf, int  buflen, const SockAddress*  to );
    350 
    351 int   socket_connect( int  fd, const SockAddress*  address );
    352 int   socket_bind( int  fd, const SockAddress*  address );
    353 int   socket_get_address( int  fd, SockAddress*  address );
    354 int   socket_get_peer_address( int  fd, SockAddress*  address );
    355 int   socket_listen( int  fd, int  backlog );
    356 int   socket_accept( int  fd, SockAddress*  address );
    357 
    358 /* returns the number of bytes that can be read from a socket */
    359 int   socket_can_read( int  fd );
    360 
    361 /* this call creates a pair of non-blocking sockets connected
    362  * to each other. this is equivalent to calling the Unix function:
    363  * socketpair(AF_LOCAL,SOCK_STREAM,0,&fds)
    364  *
    365  * on Windows, this will use a pair of TCP loopback sockets instead
    366  * returns 0 on success, -1 on error.
    367  */
    368 int  socket_pair(int  *fd1, int *fd2);
    369 
    370 /* create a server socket listening on the host's loopback interface */
    371 int  socket_loopback_server( int  port, SocketType  type );
    372 
    373 /* connect to a port on the host's loopback interface */
    374 int  socket_loopback_client( int  port, SocketType  type );
    375 
    376 /* create a server socket listening to a Unix domain path */
    377 #if HAVE_UNIX_SOCKETS
    378 int  socket_unix_server( const char*  name, SocketType  type );
    379 #endif
    380 
    381 /* create a Unix sockets and connects it to a Unix server */
    382 #if HAVE_UNIX_SOCKETS
    383 int  socket_unix_client( const char*  name, SocketType  type );
    384 #endif
    385 
    386 /* create an IPv4 client socket and connect it to a given host */
    387 int  socket_network_client( const char*  host, int  port, SocketType  type );
    388 
    389 /* create an IPv4 socket and binds it to a given port of the host's interface */
    390 int  socket_anyaddr_server( int  port, SocketType  type );
    391 
    392 /* accept a connection from the host's any interface, return the new socket
    393  * descriptor or -1 */
    394 int  socket_accept_any( int  server_fd );
    395 
    396 
    397 int  socket_mcast_inet_add_membership( int  s, uint32_t  ip );
    398 int  socket_mcast_inet_drop_membership( int  s, uint32_t  ip );
    399 int  socket_mcast_inet_set_loop( int  s, int  enabled );
    400 int  socket_mcast_inet_set_ttl( int  s, int  ttl );
    401 
    402 #endif /* QEMU_SOCKET_H */
    403