• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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
59 #  ifndef EADDRINUSE
60 #    define EADDRINUSE   10048
61 #  endif
62 #  ifndef EADDRNOTAVAIL
63 #    define EADDRNOTAVAIL 10049
64 #  endif
65 #  ifndef ENETDOWN
66 #    define ENETDOWN     10050
67 #  endif
68 #  ifndef ENETUNREACH
69 #    define ENETUNREACH  10051
70 #  endif
71 #  ifndef ENETRESET
72 #    define ENETRESET    10052
73 #  endif
74 #  ifndef ECONNABORTED
75 #    define ECONNABORTED 10053
76 #  endif
77 #  ifndef ECONNRESET
78 #    define ECONNRESET   10054
79 #  endif
80 #  ifndef ENOBUFS
81 #    define ENOBUFS      10055
82 #  endif
83 #  ifndef EISCONN
84 #    define EISCONN      10056
85 #  endif
86 #  ifndef ENOTCONN
87 #    define ENOTCONN     10057
88 #  endif
89 #  ifndef ESHUTDOWN
90 #    define ESHUTDOWN     10058
91 #  endif
92 #  ifndef ETOOMANYREFS
93 #    define ETOOMANYREFS  10059
94 #  endif
95 #  ifndef ETIMEDOUT
96 #    define ETIMEDOUT     10060
97 #  endif
98 #  ifndef ECONNREFUSED
99 #    define ECONNREFUSED  10061
100 #  endif
101 #  ifndef ELOOP
102 #    define ELOOP         10062
103 #  endif
104 #  ifndef EHOSTDOWN
105 #    define EHOSTDOWN     10064
106 #  endif
107 #  ifndef EHOSTUNREACH
108 #    define EHOSTUNREACH  10065
109 #  endif
110 #endif /* _WIN32 */
111 
112 /* Define 'errno_str' as a handy macro to return the string
113  * corresponding to a given errno code. On Unix, this is
114  * equivalent to strerror(errno), but on Windows, this will
115  * take care of Winsock-originated errors as well.
116  */
117 #ifdef _WIN32
118   extern const char*  _errno_str(void);
119 #  define  errno_str   _errno_str()
120 #else
121 #  define  errno_str   strerror(errno)
122 #endif
123 
124 /* always enable IPv6 sockets for now.
125  * the QEMU internal router is not capable of
126  * supporting them, but we plan to replace it
127  * with something better in the future.
128  */
129 #define  HAVE_IN6_SOCKETS   1
130 
131 /* Unix sockets are not available on Win32 */
132 #ifndef _WIN32
133 #  define  HAVE_UNIX_SOCKETS  1
134 #endif
135 
136 /* initialize the socket sub-system. this must be called before
137  * using any of the declarations below.
138  */
139 int  socket_init( void );
140 
141 /* return the name of the current host */
142 char*  host_name( void );
143 
144 /* supported socket types */
145 typedef enum {
146     SOCKET_DGRAM = 0,
147     SOCKET_STREAM
148 } SocketType;
149 
150 /* supported socket families */
151 typedef enum {
152     SOCKET_UNSPEC,
153     SOCKET_INET,
154     SOCKET_IN6,
155     SOCKET_UNIX
156 } SocketFamily;
157 
158 /* Generic socket address structure. Note that for Unix
159  * sockets, the path is stored in a heap-allocated block,
160  * unless the 'owner' field is cleared. If this is the case,
161  */
162 typedef struct {
163     SocketFamily  family;
164     union {
165         struct {
166             uint16_t   port;
167             uint32_t   address;
168         } inet;
169         struct {
170             uint16_t   port;
171             uint8_t    address[16];
172         } in6;
173         struct {
174             int          owner;
175             const char*  path;
176         } _unix;
177     } u;
178 } SockAddress;
179 
180 #define  SOCK_ADDRESS_INET_ANY       0x00000000
181 #define  SOCK_ADDRESS_INET_LOOPBACK  0x7f000001
182 
183 /* initialize a new IPv4 socket address, the IP address and port are
184  * in host endianess.
185  */
186 void  sock_address_init_inet( SockAddress*  a, uint32_t  ip, uint16_t  port );
187 
188 /* Initialize an IPv6 socket address, the address is in network order
189  * and the port in host endianess.
190  */
191 #if HAVE_IN6_SOCKETS
192 void  sock_address_init_in6 ( SockAddress*  a, const uint8_t*  ip6[16], uint16_t  port );
193 #endif
194 
195 /* Intialize a Unix socket address, this will copy the 'path' string into the
196  * heap. You need to call sock_address_done() to release the copy
197  */
198 #if HAVE_UNIX_SOCKETS
199 void  sock_address_init_unix( SockAddress*  a, const char*  path );
200 #endif
201 
202 /* Finalize a socket address, only needed for now for Unix addresses */
203 void  sock_address_done( SockAddress*  a );
204 
205 int   sock_address_equal( const SockAddress*  a, const SockAddress*  b );
206 
207 /* THIS SHOULD DISAPPEAR SOON - TRANSITIONAL HELPER */
208 int   sock_address_to_bsd( const SockAddress*  a, void*  sa, size_t* salen );
209 int   sock_address_from_bsd( SockAddress*  a, const void*  sa, size_t  salen );
210 int   sock_address_to_inet( SockAddress*  a, int  *paddr_ip, int  *paddr_port );
211 
212 /* return a static string describing the address */
213 const char*  sock_address_to_string( const SockAddress*  a );
214 
215 /* return the port number of a given socket address, or -1 if it's a Unix one */
216 int   sock_address_get_port( const SockAddress*  a );
217 
218 /* set the port number of a given socket address, don't do anything for Unix ones */
219 void  sock_address_set_port( SockAddress*  a, uint16_t  port );
220 
221 /* return the path of a given Unix socket, returns NULL for non-Unix ones */
222 const char*  sock_address_get_path( const SockAddress*  a );
223 
224 /* return the inet address, or -1 if it's not SOCKET_INET */
225 int   sock_address_get_ip( const SockAddress*  a );
226 
227 /* bufprint a socket address into a human-readable string */
228 char* bufprint_sock_address( char*  p, char*  end, const SockAddress*  a );
229 
230 /* resolve a hostname or decimal IPv4/IPv6 address into a socket address.
231  * returns 0 on success, or -1 on failure. Note that the values or errno
232  * set by this function are the following:
233  *
234  *   EINVAL    : invalid argument
235  *   EHOSTDOWN : could not reach DNS server
236  *   ENOENT    : no host with this name, or host doesn't have any IP address
237  *   ENOMEM    : not enough memory to perform request
238  */
239 int   sock_address_init_resolve( SockAddress*  a,
240                                  const char*   hostname,
241                                  uint16_t      port,
242                                  int           preferIn6 );
243 
244 /* create a new socket, return the socket number of -1 on failure */
245 int  socket_create( SocketFamily  family, SocketType  type );
246 
247 /* create a new socket intended for IPv4 communication. returns the socket number,
248  * or -1 on failure.
249  */
250 int   socket_create_inet( SocketType  type );
251 
252 /* create a new socket intended for IPv6 communication. returns the socket number,
253  * or -1 on failure.
254  */
255 #if HAVE_IN6_SOCKETS
256 int   socket_create_in6 ( SocketType  type );
257 #endif
258 
259 /* create a unix/local domain socket. returns the socket number,
260  * or -1 on failure.
261  */
262 #if HAVE_UNIX_SOCKETS
263 int   socket_create_unix( SocketType  type );
264 #endif
265 
266 /* return the type of a given socket */
267 SocketType  socket_get_type(int  fd);
268 
269 /* set SO_REUSEADDR on Unix, SO_EXCLUSIVEADDR on Windows */
270 int  socket_set_xreuseaddr(int  fd);
271 
272 /* set socket in non-blocking mode */
273 int  socket_set_nonblock(int fd);
274 
275 /* set socket in blocking mode */
276 int  socket_set_blocking(int fd);
277 
278 /* disable the TCP Nagle algorithm for lower latency */
279 int  socket_set_nodelay(int fd);
280 
281 /* send OOB data inline for this socket */
282 int  socket_set_oobinline(int  fd);
283 
284 /* close an opened socket. Note that this is unlike the Unix 'close' because:
285  * - it will properly shutdown the socket in the background
286  * - it does not modify errno
287  */
288 void  socket_close( int  fd );
289 
290 /* the following functions are equivalent to the BSD sockets ones
291  */
292 int   socket_recv    ( int  fd, void*  buf, int  buflen );
293 int   socket_recvfrom( int  fd, void*  buf, int  buflen, SockAddress*  from );
294 
295 int   socket_send  ( int  fd, const void*  buf, int  buflen );
296 int   socket_send_oob( int  fd, const void*  buf, int  buflen );
297 int   socket_sendto( int  fd, const void*  buf, int  buflen, const SockAddress*  to );
298 
299 int   socket_connect( int  fd, const SockAddress*  address );
300 int   socket_bind( int  fd, const SockAddress*  address );
301 int   socket_get_address( int  fd, SockAddress*  address );
302 int   socket_listen( int  fd, int  backlog );
303 int   socket_accept( int  fd, SockAddress*  address );
304 
305 /* returns the number of bytes that can be read from a socket */
306 int   socket_can_read( int  fd );
307 
308 /* this call creates a pair of non-blocking sockets connected
309  * to each other. this is equivalent to calling the Unix function:
310  * socketpair(AF_LOCAL,SOCK_STREAM,0,&fds)
311  *
312  * on Windows, this will use a pair of TCP loopback sockets instead
313  * returns 0 on success, -1 on error.
314  */
315 int  socket_pair(int  *fd1, int *fd2);
316 
317 /* create a server socket listening on the host's loopback interface */
318 int  socket_loopback_server( int  port, SocketType  type );
319 
320 /* connect to a port on the host's loopback interface */
321 int  socket_loopback_client( int  port, SocketType  type );
322 
323 /* create a server socket listening to a Unix domain path */
324 #if HAVE_UNIX_SOCKETS
325 int  socket_unix_server( const char*  name, SocketType  type );
326 #endif
327 
328 /* create a Unix sockets and connects it to a Unix server */
329 #if HAVE_UNIX_SOCKETS
330 int  socket_unix_client( const char*  name, SocketType  type );
331 #endif
332 
333 /* create an IPv4 client socket and connect it to a given host */
334 int  socket_network_client( const char*  host, int  port, SocketType  type );
335 
336 /* create an IPv4 socket and binds it to a given port of the host's interface */
337 int  socket_anyaddr_server( int  port, SocketType  type );
338 
339 /* accept a connection from the host's any interface, return the new socket
340  * descriptor or -1 */
341 int  socket_accept_any( int  server_fd );
342 
343 
344 int  socket_mcast_inet_add_membership( int  s, uint32_t  ip );
345 int  socket_mcast_inet_drop_membership( int  s, uint32_t  ip );
346 int  socket_mcast_inet_set_loop( int  s, int  enabled );
347 int  socket_mcast_inet_set_ttl( int  s, int  ttl );
348 
349 #endif /* QEMU_SOCKET_H */
350