xref: /qemu/include/qemu/sockets.h (revision fb72e779)
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 /*
37  * A variant of send(2) which handles partial send.
38  *
39  * Return the number of bytes transferred over the socket.
40  * Set errno if fewer than `count' bytes are sent.
41  *
42  * This function don't work with non-blocking socket's.
43  * Any of the possibilities with non-blocking socket's is bad:
44  *   - return a short write (then name is wrong)
45  *   - busy wait adding (errno == EAGAIN) to the loop
46  */
47 ssize_t qemu_send_full(int s, const void *buf, size_t count)
48     G_GNUC_WARN_UNUSED_RESULT;
49 int socket_set_cork(int fd, int v);
50 int socket_set_nodelay(int fd);
51 void qemu_socket_set_block(int fd);
52 int qemu_socket_try_set_nonblock(int fd);
53 void qemu_socket_set_nonblock(int fd);
54 int socket_set_fast_reuse(int fd);
55 
56 #ifdef WIN32
57 /* Windows has different names for the same constants with the same values */
58 #define SHUT_RD   0
59 #define SHUT_WR   1
60 #define SHUT_RDWR 2
61 #endif
62 
63 int inet_ai_family_from_address(InetSocketAddress *addr,
64                                 Error **errp);
65 int inet_parse(InetSocketAddress *addr, const char *str, Error **errp);
66 int inet_connect(const char *str, Error **errp);
67 int inet_connect_saddr(InetSocketAddress *saddr, Error **errp);
68 
69 NetworkAddressFamily inet_netfamily(int family);
70 
71 int unix_listen(const char *path, Error **errp);
72 int unix_connect(const char *path, Error **errp);
73 
74 char *socket_uri(SocketAddress *addr);
75 SocketAddress *socket_parse(const char *str, Error **errp);
76 int socket_connect(SocketAddress *addr, Error **errp);
77 int socket_listen(SocketAddress *addr, int num, Error **errp);
78 void socket_listen_cleanup(int fd, Error **errp);
79 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
80 
81 /* Old, ipv4 only bits.  Don't use for new code. */
82 int convert_host_port(struct sockaddr_in *saddr, const char *host,
83                       const char *port, Error **errp);
84 int parse_host_port(struct sockaddr_in *saddr, const char *str,
85                     Error **errp);
86 int socket_init(void);
87 
88 /**
89  * socket_sockaddr_to_address:
90  * @sa: socket address struct
91  * @salen: size of @sa struct
92  * @errp: pointer to uninitialized error object
93  *
94  * Get the string representation of the socket
95  * address. A pointer to the allocated address information
96  * struct will be returned, which the caller is required to
97  * release with a call qapi_free_SocketAddress() when no
98  * longer required.
99  *
100  * Returns: the socket address struct, or NULL on error
101  */
102 SocketAddress *
103 socket_sockaddr_to_address(struct sockaddr_storage *sa,
104                            socklen_t salen,
105                            Error **errp);
106 
107 /**
108  * socket_local_address:
109  * @fd: the socket file handle
110  * @errp: pointer to uninitialized error object
111  *
112  * Get the string representation of the local socket
113  * address. A pointer to the allocated address information
114  * struct will be returned, which the caller is required to
115  * release with a call qapi_free_SocketAddress() when no
116  * longer required.
117  *
118  * Returns: the socket address struct, or NULL on error
119  */
120 SocketAddress *socket_local_address(int fd, Error **errp);
121 
122 /**
123  * socket_remote_address:
124  * @fd: the socket file handle
125  * @errp: pointer to uninitialized error object
126  *
127  * Get the string representation of the remote socket
128  * address. A pointer to the allocated address information
129  * struct will be returned, which the caller is required to
130  * release with a call qapi_free_SocketAddress() when no
131  * longer required.
132  *
133  * Returns: the socket address struct, or NULL on error
134  */
135 SocketAddress *socket_remote_address(int fd, Error **errp);
136 
137 /**
138  * socket_address_flatten:
139  * @addr: the socket address to flatten
140  *
141  * Convert SocketAddressLegacy to SocketAddress.  Caller is responsible
142  * for freeing with qapi_free_SocketAddress().
143  *
144  * Returns: the argument converted to SocketAddress.
145  */
146 SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);
147 
148 /**
149  * socket_address_parse_named_fd:
150  *
151  * Modify @addr, replacing a named fd by its corresponding number.
152  * Needed for callers that plan to pass @addr to a context where the
153  * current monitor is not available.
154  *
155  * Return 0 on success.
156  */
157 int socket_address_parse_named_fd(SocketAddress *addr, Error **errp);
158 #endif /* QEMU_SOCKETS_H */
159