xref: /qemu/include/qemu/sockets.h (revision 814bb12a)
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-types.h"
13 
14 /* misc helpers */
15 int qemu_socket(int domain, int type, int protocol);
16 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
17 int socket_set_cork(int fd, int v);
18 int socket_set_nodelay(int fd);
19 void qemu_set_block(int fd);
20 void qemu_set_nonblock(int fd);
21 int socket_set_fast_reuse(int fd);
22 
23 #ifdef WIN32
24 /* Windows has different names for the same constants with the same values */
25 #define SHUT_RD   0
26 #define SHUT_WR   1
27 #define SHUT_RDWR 2
28 #endif
29 
30 /* callback function for nonblocking connect
31  * valid fd on success, negative error code on failure
32  */
33 typedef void NonBlockingConnectHandler(int fd, Error *err, void *opaque);
34 
35 InetSocketAddress *inet_parse(const char *str, Error **errp);
36 int inet_connect(const char *str, Error **errp);
37 
38 NetworkAddressFamily inet_netfamily(int family);
39 
40 int unix_listen(const char *path, char *ostr, int olen, Error **errp);
41 int unix_connect(const char *path, Error **errp);
42 
43 SocketAddress *socket_parse(const char *str, Error **errp);
44 int socket_connect(SocketAddress *addr, Error **errp,
45                    NonBlockingConnectHandler *callback, void *opaque);
46 int socket_listen(SocketAddress *addr, Error **errp);
47 void socket_listen_cleanup(int fd, Error **errp);
48 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
49 
50 /* Old, ipv4 only bits.  Don't use for new code. */
51 int parse_host_port(struct sockaddr_in *saddr, const char *str);
52 int socket_init(void);
53 
54 /**
55  * socket_sockaddr_to_address:
56  * @sa: socket address struct
57  * @salen: size of @sa struct
58  * @errp: pointer to uninitialized error object
59  *
60  * Get the string representation of the socket
61  * address. A pointer to the allocated address information
62  * struct will be returned, which the caller is required to
63  * release with a call qapi_free_SocketAddress when no
64  * longer required.
65  *
66  * Returns: the socket address struct, or NULL on error
67  */
68 SocketAddress *
69 socket_sockaddr_to_address(struct sockaddr_storage *sa,
70                            socklen_t salen,
71                            Error **errp);
72 
73 /**
74  * socket_local_address:
75  * @fd: the socket file handle
76  * @errp: pointer to uninitialized error object
77  *
78  * Get the string representation of the local 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 *socket_local_address(int fd, Error **errp);
87 
88 /**
89  * socket_remote_address:
90  * @fd: the socket file handle
91  * @errp: pointer to uninitialized error object
92  *
93  * Get the string representation of the remote socket
94  * address. A pointer to the allocated address information
95  * struct will be returned, which the caller is required to
96  * release with a call qapi_free_SocketAddress when no
97  * longer required.
98  *
99  * Returns: the socket address struct, or NULL on error
100  */
101 SocketAddress *socket_remote_address(int fd, Error **errp);
102 
103 /**
104  * socket_address_to_string:
105  * @addr: the socket address struct
106  * @errp: pointer to uninitialized error object
107  *
108  * Get the string representation of the socket
109  * address. A pointer to the char array containing
110  * string format will be returned, the caller is
111  * required to release the returned value when no
112  * longer required with g_free.
113  *
114  * Returns: the socket address in string format, or NULL on error
115  */
116 char *socket_address_to_string(struct SocketAddress *addr, Error **errp);
117 
118 #endif /* QEMU_SOCKETS_H */
119