xref: /qemu/include/qemu/sockets.h (revision 34f58df5)
1 /* headers to use the BSD sockets */
2 
3 #ifndef QEMU_SOCKETS_H
4 #define QEMU_SOCKETS_H
5 
6 #ifdef _WIN32
7 
8 int inet_aton(const char *cp, struct in_addr *ia);
9 
10 #endif /* !_WIN32 */
11 
12 #include "qapi/qapi-types-sockets.h"
13 
14 /* misc helpers */
15 bool fd_is_socket(int fd);
16 int qemu_socket(int domain, int type, int protocol);
17 
18 #ifndef WIN32
19 /**
20  * qemu_socketpair:
21  * @domain: specifies a communication domain, such as PF_UNIX
22  * @type: specifies the socket type.
23  * @protocol: specifies a particular protocol to be used with the  socket
24  * @sv: an array to store the pair of socket created
25  *
26  * Creates an unnamed pair of connected sockets in the specified domain,
27  * of the specified type, and using the optionally specified protocol.
28  * And automatically set the close-on-exec flags on the returned sockets
29  *
30  * Return 0 on success.
31  */
32 int qemu_socketpair(int domain, int type, int protocol, int sv[2]);
33 #endif
34 
35 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
36 int socket_set_cork(int fd, int v);
37 int socket_set_nodelay(int fd);
38 void qemu_socket_set_block(int fd);
39 int qemu_socket_try_set_nonblock(int fd);
40 void qemu_socket_set_nonblock(int fd);
41 int socket_set_fast_reuse(int fd);
42 
43 #ifdef WIN32
44 /* Windows has different names for the same constants with the same values */
45 #define SHUT_RD   0
46 #define SHUT_WR   1
47 #define SHUT_RDWR 2
48 #endif
49 
50 int inet_ai_family_from_address(InetSocketAddress *addr,
51                                 Error **errp);
52 int inet_parse(InetSocketAddress *addr, const char *str, Error **errp);
53 int inet_connect(const char *str, Error **errp);
54 int inet_connect_saddr(InetSocketAddress *saddr, Error **errp);
55 
56 NetworkAddressFamily inet_netfamily(int family);
57 
58 int unix_listen(const char *path, Error **errp);
59 int unix_connect(const char *path, Error **errp);
60 
61 SocketAddress *socket_parse(const char *str, Error **errp);
62 int socket_connect(SocketAddress *addr, Error **errp);
63 int socket_listen(SocketAddress *addr, int num, Error **errp);
64 void socket_listen_cleanup(int fd, Error **errp);
65 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
66 
67 /* Old, ipv4 only bits.  Don't use for new code. */
68 int parse_host_port(struct sockaddr_in *saddr, const char *str,
69                     Error **errp);
70 int socket_init(void);
71 
72 /**
73  * socket_sockaddr_to_address:
74  * @sa: socket address struct
75  * @salen: size of @sa struct
76  * @errp: pointer to uninitialized error object
77  *
78  * Get the string representation of the socket
79  * address. A pointer to the allocated address information
80  * struct will be returned, which the caller is required to
81  * release with a call qapi_free_SocketAddress() when no
82  * longer required.
83  *
84  * Returns: the socket address struct, or NULL on error
85  */
86 SocketAddress *
87 socket_sockaddr_to_address(struct sockaddr_storage *sa,
88                            socklen_t salen,
89                            Error **errp);
90 
91 /**
92  * socket_local_address:
93  * @fd: the socket file handle
94  * @errp: pointer to uninitialized error object
95  *
96  * Get the string representation of the local socket
97  * address. A pointer to the allocated address information
98  * struct will be returned, which the caller is required to
99  * release with a call qapi_free_SocketAddress() when no
100  * longer required.
101  *
102  * Returns: the socket address struct, or NULL on error
103  */
104 SocketAddress *socket_local_address(int fd, Error **errp);
105 
106 /**
107  * socket_remote_address:
108  * @fd: the socket file handle
109  * @errp: pointer to uninitialized error object
110  *
111  * Get the string representation of the remote socket
112  * address. A pointer to the allocated address information
113  * struct will be returned, which the caller is required to
114  * release with a call qapi_free_SocketAddress() when no
115  * longer required.
116  *
117  * Returns: the socket address struct, or NULL on error
118  */
119 SocketAddress *socket_remote_address(int fd, Error **errp);
120 
121 /**
122  * socket_address_flatten:
123  * @addr: the socket address to flatten
124  *
125  * Convert SocketAddressLegacy to SocketAddress.  Caller is responsible
126  * for freeing with qapi_free_SocketAddress().
127  *
128  * Returns: the argument converted to SocketAddress.
129  */
130 SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);
131 
132 /**
133  * socket_address_parse_named_fd:
134  *
135  * Modify @addr, replacing a named fd by its corresponding number.
136  * Needed for callers that plan to pass @addr to a context where the
137  * current monitor is not available.
138  *
139  * Return 0 on success.
140  */
141 int socket_address_parse_named_fd(SocketAddress *addr, Error **errp);
142 
143 #endif /* QEMU_SOCKETS_H */
144