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