master
h 137 lines 4.39 KB
Raw
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 /**
19 * qemu_socketpair:
20 * @domain: specifies a communication domain, such as PF_UNIX
21 * @type: specifies the socket type.
22 * @protocol: specifies a particular protocol to be used with the socket
23 * @sv: an array to store the pair of socket created
24 *
25 * Creates an unnamed pair of connected sockets in the specified domain,
26 * of the specified type, and using the optionally specified protocol.
27 * And automatically set the close-on-exec flags on the returned sockets
28 *
29 * Return 0 on success.
30 */
31 int qemu_socketpair(int domain, int type, int protocol, int sv[2]);
32
33 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
34 /*
35 * A variant of send(2) which handles partial send.
36 *
37 * Return the number of bytes transferred over the socket.
38 * Set errno if fewer than `count' bytes are sent.
39 *
40 * This function don't work with non-blocking socket's.
41 * Any of the possibilities with non-blocking socket's is bad:
42 * - return a short write (then name is wrong)
43 * - busy wait adding (errno == EAGAIN) to the loop
44 */
45 ssize_t qemu_send_full(int s, const void *buf, size_t count)
46 G_GNUC_WARN_UNUSED_RESULT;
47 int socket_set_cork(int fd, int v);
48 int socket_set_nodelay(int fd);
49 int socket_set_fast_reuse(int fd);
50
51 #ifdef WIN32
52 /* Windows has different names for the same constants with the same values */
53 #define SHUT_RD 0
54 #define SHUT_WR 1
55 #define SHUT_RDWR 2
56 #endif
57
58 int inet_ai_family_from_address(InetSocketAddress *addr,
59 Error **errp);
60 int inet_parse(InetSocketAddress *addr, const char *str, Error **errp);
61 int inet_connect_saddr(InetSocketAddress *saddr, Error **errp);
62
63 NetworkAddressFamily inet_netfamily(int family);
64
65 int unix_listen(const char *path, Error **errp);
66 int unix_connect(const char *path, Error **errp);
67
68 char *socket_uri(SocketAddress *addr);
69 SocketAddress *socket_parse(const char *str, Error **errp);
70 int socket_connect(SocketAddress *addr, Error **errp);
71 int socket_listen(SocketAddress *addr, int num, Error **errp);
72 void socket_listen_cleanup(int fd, Error **errp);
73 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
74
75 /* Old, ipv4 only bits. Don't use for new code. */
76 int convert_host_port(struct sockaddr_in *saddr, const char *host,
77 const char *port, Error **errp);
78 int parse_host_port(struct sockaddr_in *saddr, const char *str,
79 Error **errp);
80 int socket_init(void);
81
82 /**
83 * socket_sockaddr_to_address:
84 * @sa: socket address struct
85 * @salen: size of @sa struct
86 * @errp: pointer to uninitialized error object
87 *
88 * Get the string representation of the socket
89 * address. A pointer to the allocated address information
90 * struct will be returned, which the caller is required to
91 * release with a call qapi_free_SocketAddress() when no
92 * longer required.
93 *
94 * Returns: the socket address struct, or NULL on error
95 */
96 SocketAddress *
97 socket_sockaddr_to_address(struct sockaddr_storage *sa,
98 socklen_t salen,
99 Error **errp);
100
101 /**
102 * socket_local_address:
103 * @fd: the socket file handle
104 * @errp: pointer to uninitialized error object
105 *
106 * Get the string representation of the local socket
107 * address. A pointer to the allocated address information
108 * struct will be returned, which the caller is required to
109 * release with a call qapi_free_SocketAddress() when no
110 * longer required.
111 *
112 * Returns: the socket address struct, or NULL on error
113 */
114 SocketAddress *socket_local_address(int fd, Error **errp);
115
116 /**
117 * socket_address_flatten:
118 * @addr: the socket address to flatten
119 *
120 * Convert SocketAddressLegacy to SocketAddress. Caller is responsible
121 * for freeing with qapi_free_SocketAddress().
122 *
123 * Returns: the argument converted to SocketAddress.
124 */
125 SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);
126
127 /**
128 * socket_address_parse_named_fd:
129 *
130 * Modify @addr, replacing a named fd by its corresponding number.
131 * Needed for callers that plan to pass @addr to a context where the
132 * current monitor is not available.
133 *
134 * Return 0 on success.
135 */
136 int socket_address_parse_named_fd(SocketAddress *addr, Error **errp);
137 #endif /* QEMU_SOCKETS_H */