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