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 /* return a static string describing the address */ 214 const char* sock_address_to_string( const SockAddress* a ); 215 216 static __inline__ 217 SocketFamily sock_address_get_family( const SockAddress* a ) 218 { 219 return a->family; 220 } 221 222 /* return the port number of a given socket address, or -1 if it's a Unix one */ 223 int sock_address_get_port( const SockAddress* a ); 224 225 /* set the port number of a given socket address, don't do anything for Unix ones */ 226 void sock_address_set_port( SockAddress* a, uint16_t port ); 227 228 /* return the path of a given Unix socket, returns NULL for non-Unix ones */ 229 const char* sock_address_get_path( const SockAddress* a ); 230 231 /* return the inet address, or -1 if it's not SOCKET_INET */ 232 int sock_address_get_ip( const SockAddress* a ); 233 234 /* bufprint a socket address into a human-readable string */ 235 char* bufprint_sock_address( char* p, char* end, const SockAddress* a ); 236 237 /* resolve a hostname or decimal IPv4/IPv6 address into a socket address. 238 * returns 0 on success, or -1 on failure. Note that the values or errno 239 * set by this function are the following: 240 * 241 * EINVAL : invalid argument 242 * EHOSTDOWN : could not reach DNS server 243 * ENOENT : no host with this name, or host doesn't have any IP address 244 * ENOMEM : not enough memory to perform request 245 */ 246 int sock_address_init_resolve( SockAddress* a, 247 const char* hostname, 248 uint16_t port, 249 int preferIn6 ); 250 251 int sock_address_get_numeric_info( SockAddress* a, 252 char* host, 253 size_t hostlen, 254 char* serv, 255 size_t servlen ); 256 257 /* Support for listing all socket addresses of a given host */ 258 enum { 259 SOCKET_LIST_PASSIVE = (1 << 0), 260 SOCKET_LIST_FORCE_INET = (1 << 1), 261 SOCKET_LIST_FORCE_IN6 = (1 << 2), 262 SOCKET_LIST_DGRAM = (1 << 3), 263 }; 264 265 /* resolve a host and service/port name into a list of SockAddress objects. 266 * returns a NULL-terminated array of SockAddress pointers on success, 267 * or NULL in case of failure, with the value of errno set to one of the 268 * following: 269 * 270 * EINVAL : invalid argument 271 * EHOSTDOWN : could not reach DNS server 272 * ENOENT : no host with this name, or host doesn't have IP address 273 * ENOMEM : not enough memory to perform request 274 * 275 * other system-level errors can also be set depending on the host sockets 276 * implementation. 277 * 278 * This function loops on EINTR so the caller shouldn't have to check for it. 279 */ 280 SockAddress** sock_address_list_create( const char* hostname, 281 const char* port, 282 unsigned flags ); 283 284 /* resolve a string containing host and port name into a list of SockAddress 285 * objects. Parameter host_and_port should be in format [host:]port, where 286 * 'host' addresses the machine and must be resolvable into an IP address, and 287 * 'port' is a decimal numeric value for the port. 'host' is optional, and if 288 * ommited, localhost will be used. 289 * returns a NULL-terminated array of SockAddress pointers on success, 290 * or NULL in case of failure, with the value of errno set to one of the 291 * following: 292 * 293 * EINVAL : invalid argument 294 * EHOSTDOWN : could not reach DNS server 295 * ENOENT : no host with this name, or host doesn't have IP address 296 * ENOMEM : not enough memory to perform request 297 * 298 * other system-level errors can also be set depending on the host sockets 299 * implementation. 300 * 301 * This function loops on EINTR so the caller shouldn't have to check for it. 302 */ 303 SockAddress** sock_address_list_create2(const char* host_and_port, 304 unsigned flags ); 305 306 void sock_address_list_free( SockAddress** list ); 307 308 /* create a new socket, return the socket number of -1 on failure */ 309 int socket_create( SocketFamily family, SocketType type ); 310 311 /* create a new socket intended for IPv4 communication. returns the socket number, 312 * or -1 on failure. 313 */ 314 int socket_create_inet( SocketType type ); 315 316 /* create a new socket intended for IPv6 communication. returns the socket number, 317 * or -1 on failure. 318 */ 319 #if HAVE_IN6_SOCKETS 320 int socket_create_in6 ( SocketType type ); 321 #endif 322 323 /* create a unix/local domain socket. returns the socket number, 324 * or -1 on failure. 325 */ 326 #if HAVE_UNIX_SOCKETS 327 int socket_create_unix( SocketType type ); 328 #endif 329 330 /* return the type of a given socket */ 331 SocketType socket_get_type(int fd); 332 333 /* set SO_REUSEADDR on Unix, SO_EXCLUSIVEADDR on Windows */ 334 int socket_set_xreuseaddr(int fd); 335 336 /* set socket in non-blocking mode */ 337 int socket_set_nonblock(int fd); 338 339 /* set socket in blocking mode */ 340 int socket_set_blocking(int fd); 341 342 /* disable the TCP Nagle algorithm for lower latency */ 343 int socket_set_nodelay(int fd); 344 345 /* send OOB data inline for this socket */ 346 int socket_set_oobinline(int fd); 347 348 /* force listening to IPv6 interfaces only */ 349 int socket_set_ipv6only(int fd); 350 351 /* retrieve last socket error code */ 352 int socket_get_error(int fd); 353 354 /* close an opened socket. Note that this is unlike the Unix 'close' because: 355 * - it will properly shutdown the socket in the background 356 * - it does not modify errno 357 */ 358 void socket_close( int fd ); 359 360 /* the following functions are equivalent to the BSD sockets ones 361 */ 362 int socket_recv ( int fd, void* buf, int buflen ); 363 int socket_recvfrom( int fd, void* buf, int buflen, SockAddress* from ); 364 365 int socket_send ( int fd, const void* buf, int buflen ); 366 int socket_send_oob( int fd, const void* buf, int buflen ); 367 int socket_sendto( int fd, const void* buf, int buflen, const SockAddress* to ); 368 369 int socket_connect( int fd, const SockAddress* address ); 370 int socket_bind( int fd, const SockAddress* address ); 371 int socket_get_address( int fd, SockAddress* address ); 372 int socket_get_peer_address( int fd, SockAddress* address ); 373 int socket_listen( int fd, int backlog ); 374 int socket_accept( int fd, SockAddress* address ); 375 376 /* returns the number of bytes that can be read from a socket */ 377 int socket_can_read( int fd ); 378 379 /* this call creates a pair of non-blocking sockets connected 380 * to each other. this is equivalent to calling the Unix function: 381 * socketpair(AF_LOCAL,SOCK_STREAM,0,&fds) 382 * 383 * on Windows, this will use a pair of TCP loopback sockets instead 384 * returns 0 on success, -1 on error. 385 */ 386 int socket_pair(int *fd1, int *fd2); 387 388 /* create a server socket listening on the host's loopback interface */ 389 int socket_loopback_server( int port, SocketType type ); 390 391 /* connect to a port on the host's loopback interface */ 392 int socket_loopback_client( int port, SocketType type ); 393 394 /* create a server socket listening to a Unix domain path */ 395 #if HAVE_UNIX_SOCKETS 396 int socket_unix_server( const char* name, SocketType type ); 397 #endif 398 399 /* create a Unix sockets and connects it to a Unix server */ 400 #if HAVE_UNIX_SOCKETS 401 int socket_unix_client( const char* name, SocketType type ); 402 #endif 403 404 /* create an IPv4 client socket and connect it to a given host */ 405 int socket_network_client( const char* host, int port, SocketType type ); 406 407 /* create an IPv4 socket and binds it to a given port of the host's interface */ 408 int socket_anyaddr_server( int port, SocketType type ); 409 410 /* accept a connection from the host's any interface, return the new socket 411 * descriptor or -1 */ 412 int socket_accept_any( int server_fd ); 413 414 415 int socket_mcast_inet_add_membership( int s, uint32_t ip ); 416 int socket_mcast_inet_drop_membership( int s, uint32_t ip ); 417 int socket_mcast_inet_set_loop( int s, int enabled ); 418 int socket_mcast_inet_set_ttl( int s, int ttl ); 419 420 #endif /* QEMU_SOCKET_H */ 421