xref: /qemu/include/qemu/sockets.h (revision cc75a50c)
1 /* headers to use the BSD sockets */
2 #ifndef QEMU_SOCKET_H
3 #define QEMU_SOCKET_H
4 
5 #ifdef _WIN32
6 #include <windows.h>
7 #include <winsock2.h>
8 #include <ws2tcpip.h>
9 
10 #define socket_error() WSAGetLastError()
11 
12 int inet_aton(const char *cp, struct in_addr *ia);
13 
14 #else
15 
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <netinet/tcp.h>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <sys/un.h>
23 
24 #define socket_error() errno
25 #define closesocket(s) close(s)
26 
27 #endif /* !_WIN32 */
28 
29 #include "qemu/option.h"
30 #include "qapi/error.h"
31 #include "qapi-types.h"
32 
33 extern QemuOptsList socket_optslist;
34 
35 /* misc helpers */
36 int qemu_socket(int domain, int type, int protocol);
37 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
38 int socket_set_cork(int fd, int v);
39 int socket_set_nodelay(int fd);
40 void qemu_set_block(int fd);
41 void qemu_set_nonblock(int fd);
42 int socket_set_fast_reuse(int fd);
43 
44 #ifdef WIN32
45 /* Windows has different names for the same constants with the same values */
46 #define SHUT_RD   0
47 #define SHUT_WR   1
48 #define SHUT_RDWR 2
49 #endif
50 
51 /* callback function for nonblocking connect
52  * valid fd on success, negative error code on failure
53  */
54 typedef void NonBlockingConnectHandler(int fd, Error *err, void *opaque);
55 
56 InetSocketAddress *inet_parse(const char *str, Error **errp);
57 int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp);
58 int inet_listen(const char *str, char *ostr, int olen,
59                 int socktype, int port_offset, Error **errp);
60 int inet_connect_opts(QemuOpts *opts, Error **errp,
61                       NonBlockingConnectHandler *callback, void *opaque);
62 int inet_connect(const char *str, Error **errp);
63 int inet_nonblocking_connect(const char *str,
64                              NonBlockingConnectHandler *callback,
65                              void *opaque, Error **errp);
66 
67 int inet_dgram_opts(QemuOpts *opts, Error **errp);
68 NetworkAddressFamily inet_netfamily(int family);
69 
70 int unix_listen_opts(QemuOpts *opts, Error **errp);
71 int unix_listen(const char *path, char *ostr, int olen, Error **errp);
72 int unix_connect_opts(QemuOpts *opts, Error **errp,
73                       NonBlockingConnectHandler *callback, void *opaque);
74 int unix_connect(const char *path, Error **errp);
75 int unix_nonblocking_connect(const char *str,
76                              NonBlockingConnectHandler *callback,
77                              void *opaque, Error **errp);
78 
79 SocketAddress *socket_parse(const char *str, Error **errp);
80 int socket_connect(SocketAddress *addr, Error **errp,
81                    NonBlockingConnectHandler *callback, void *opaque);
82 int socket_listen(SocketAddress *addr, Error **errp);
83 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
84 
85 /* Old, ipv4 only bits.  Don't use for new code. */
86 int parse_host_port(struct sockaddr_in *saddr, const char *str);
87 int socket_init(void);
88 
89 /**
90  * socket_sockaddr_to_address:
91  * @sa: socket address struct
92  * @salen: size of @sa struct
93  * @errp: pointer to uninitialized error object
94  *
95  * Get the string representation of the socket
96  * address. A pointer to the allocated address information
97  * struct will be returned, which the caller is required to
98  * release with a call qapi_free_SocketAddress when no
99  * longer required.
100  *
101  * Returns: the socket address struct, or NULL on error
102  */
103 SocketAddress *
104 socket_sockaddr_to_address(struct sockaddr_storage *sa,
105                            socklen_t salen,
106                            Error **errp);
107 
108 /**
109  * socket_local_address:
110  * @fd: the socket file handle
111  * @errp: pointer to uninitialized error object
112  *
113  * Get the string representation of the local socket
114  * address. A pointer to the allocated address information
115  * struct will be returned, which the caller is required to
116  * release with a call qapi_free_SocketAddress when no
117  * longer required.
118  *
119  * Returns: the socket address struct, or NULL on error
120  */
121 SocketAddress *socket_local_address(int fd, Error **errp);
122 
123 /**
124  * socket_remote_address:
125  * @fd: the socket file handle
126  * @errp: pointer to uninitialized error object
127  *
128  * Get the string representation of the remote socket
129  * address. A pointer to the allocated address information
130  * struct will be returned, which the caller is required to
131  * release with a call qapi_free_SocketAddress when no
132  * longer required.
133  *
134  * Returns: the socket address struct, or NULL on error
135  */
136 SocketAddress *socket_remote_address(int fd, Error **errp);
137 
138 
139 void qapi_copy_SocketAddress(SocketAddress **p_dest,
140                              SocketAddress *src);
141 
142 #endif /* QEMU_SOCKET_H */
143