xref: /qemu/include/io/channel-socket.h (revision c8198bd5f991f5674960bf6a7754614f69f71620)
1559607eaSDaniel P. Berrange /*
2559607eaSDaniel P. Berrange  * QEMU I/O channels sockets driver
3559607eaSDaniel P. Berrange  *
4559607eaSDaniel P. Berrange  * Copyright (c) 2015 Red Hat, Inc.
5559607eaSDaniel P. Berrange  *
6559607eaSDaniel P. Berrange  * This library is free software; you can redistribute it and/or
7559607eaSDaniel P. Berrange  * modify it under the terms of the GNU Lesser General Public
8559607eaSDaniel P. Berrange  * License as published by the Free Software Foundation; either
9*c8198bd5SChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10559607eaSDaniel P. Berrange  *
11559607eaSDaniel P. Berrange  * This library is distributed in the hope that it will be useful,
12559607eaSDaniel P. Berrange  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13559607eaSDaniel P. Berrange  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14559607eaSDaniel P. Berrange  * Lesser General Public License for more details.
15559607eaSDaniel P. Berrange  *
16559607eaSDaniel P. Berrange  * You should have received a copy of the GNU Lesser General Public
17559607eaSDaniel P. Berrange  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18559607eaSDaniel P. Berrange  *
19559607eaSDaniel P. Berrange  */
20559607eaSDaniel P. Berrange 
212a6a4076SMarkus Armbruster #ifndef QIO_CHANNEL_SOCKET_H
222a6a4076SMarkus Armbruster #define QIO_CHANNEL_SOCKET_H
23559607eaSDaniel P. Berrange 
24559607eaSDaniel P. Berrange #include "io/channel.h"
25559607eaSDaniel P. Berrange #include "io/task.h"
26559607eaSDaniel P. Berrange #include "qemu/sockets.h"
27db1015e9SEduardo Habkost #include "qom/object.h"
28559607eaSDaniel P. Berrange 
29559607eaSDaniel P. Berrange #define TYPE_QIO_CHANNEL_SOCKET "qio-channel-socket"
308063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelSocket, QIO_CHANNEL_SOCKET)
31559607eaSDaniel P. Berrange 
32559607eaSDaniel P. Berrange 
33559607eaSDaniel P. Berrange /**
34559607eaSDaniel P. Berrange  * QIOChannelSocket:
35559607eaSDaniel P. Berrange  *
36559607eaSDaniel P. Berrange  * The QIOChannelSocket class provides a channel implementation
37559607eaSDaniel P. Berrange  * that can transport data over a UNIX socket or TCP socket.
38559607eaSDaniel P. Berrange  * Beyond the core channel API, it also provides functionality
39559607eaSDaniel P. Berrange  * for accepting client connections, tuning some socket
40559607eaSDaniel P. Berrange  * parameters and getting socket address strings.
41559607eaSDaniel P. Berrange  */
42559607eaSDaniel P. Berrange 
43559607eaSDaniel P. Berrange struct QIOChannelSocket {
44559607eaSDaniel P. Berrange     QIOChannel parent;
45559607eaSDaniel P. Berrange     int fd;
46559607eaSDaniel P. Berrange     struct sockaddr_storage localAddr;
47559607eaSDaniel P. Berrange     socklen_t localAddrLen;
48559607eaSDaniel P. Berrange     struct sockaddr_storage remoteAddr;
49559607eaSDaniel P. Berrange     socklen_t remoteAddrLen;
50559607eaSDaniel P. Berrange };
51559607eaSDaniel P. Berrange 
52559607eaSDaniel P. Berrange 
53559607eaSDaniel P. Berrange /**
54559607eaSDaniel P. Berrange  * qio_channel_socket_new:
55559607eaSDaniel P. Berrange  *
56559607eaSDaniel P. Berrange  * Create a channel for performing I/O on a socket
57559607eaSDaniel P. Berrange  * connection, that is initially closed. After
58559607eaSDaniel P. Berrange  * creating the socket, it must be setup as a client
59559607eaSDaniel P. Berrange  * connection or server.
60559607eaSDaniel P. Berrange  *
61559607eaSDaniel P. Berrange  * Returns: the socket channel object
62559607eaSDaniel P. Berrange  */
63559607eaSDaniel P. Berrange QIOChannelSocket *
64559607eaSDaniel P. Berrange qio_channel_socket_new(void);
65559607eaSDaniel P. Berrange 
66559607eaSDaniel P. Berrange /**
67559607eaSDaniel P. Berrange  * qio_channel_socket_new_fd:
68559607eaSDaniel P. Berrange  * @fd: the socket file descriptor
69821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
70559607eaSDaniel P. Berrange  *
71559607eaSDaniel P. Berrange  * Create a channel for performing I/O on the socket
72559607eaSDaniel P. Berrange  * connection represented by the file descriptor @fd.
73559607eaSDaniel P. Berrange  *
74559607eaSDaniel P. Berrange  * Returns: the socket channel object, or NULL on error
75559607eaSDaniel P. Berrange  */
76559607eaSDaniel P. Berrange QIOChannelSocket *
77559607eaSDaniel P. Berrange qio_channel_socket_new_fd(int fd,
78559607eaSDaniel P. Berrange                           Error **errp);
79559607eaSDaniel P. Berrange 
80559607eaSDaniel P. Berrange 
81559607eaSDaniel P. Berrange /**
82559607eaSDaniel P. Berrange  * qio_channel_socket_connect_sync:
83559607eaSDaniel P. Berrange  * @ioc: the socket channel object
84559607eaSDaniel P. Berrange  * @addr: the address to connect to
85821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
86559607eaSDaniel P. Berrange  *
87559607eaSDaniel P. Berrange  * Attempt to connect to the address @addr. This method
88559607eaSDaniel P. Berrange  * will run in the foreground so the caller will not regain
89559607eaSDaniel P. Berrange  * execution control until the connection is established or
90559607eaSDaniel P. Berrange  * an error occurs.
91559607eaSDaniel P. Berrange  */
92559607eaSDaniel P. Berrange int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
93bd269ebcSMarkus Armbruster                                     SocketAddress *addr,
94559607eaSDaniel P. Berrange                                     Error **errp);
95559607eaSDaniel P. Berrange 
96559607eaSDaniel P. Berrange /**
97559607eaSDaniel P. Berrange  * qio_channel_socket_connect_async:
98559607eaSDaniel P. Berrange  * @ioc: the socket channel object
99559607eaSDaniel P. Berrange  * @addr: the address to connect to
100559607eaSDaniel P. Berrange  * @callback: the function to invoke on completion
101559607eaSDaniel P. Berrange  * @opaque: user data to pass to @callback
102559607eaSDaniel P. Berrange  * @destroy: the function to free @opaque
1038005fdd8SPeter Xu  * @context: the context to run the async task. If %NULL, the default
1048005fdd8SPeter Xu  *           context will be used.
105559607eaSDaniel P. Berrange  *
106559607eaSDaniel P. Berrange  * Attempt to connect to the address @addr. This method
107559607eaSDaniel P. Berrange  * will run in the background so the caller will regain
108559607eaSDaniel P. Berrange  * execution control immediately. The function @callback
109fe81e932SDaniel P. Berrange  * will be invoked on completion or failure. The @addr
110fe81e932SDaniel P. Berrange  * parameter will be copied, so may be freed as soon
111fe81e932SDaniel P. Berrange  * as this function returns without waiting for completion.
112559607eaSDaniel P. Berrange  */
113559607eaSDaniel P. Berrange void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
114bd269ebcSMarkus Armbruster                                       SocketAddress *addr,
115559607eaSDaniel P. Berrange                                       QIOTaskFunc callback,
116559607eaSDaniel P. Berrange                                       gpointer opaque,
1178005fdd8SPeter Xu                                       GDestroyNotify destroy,
1188005fdd8SPeter Xu                                       GMainContext *context);
119559607eaSDaniel P. Berrange 
120559607eaSDaniel P. Berrange 
121559607eaSDaniel P. Berrange /**
122559607eaSDaniel P. Berrange  * qio_channel_socket_listen_sync:
123559607eaSDaniel P. Berrange  * @ioc: the socket channel object
124559607eaSDaniel P. Berrange  * @addr: the address to listen to
1254e2d8bf6SJuan Quintela  * @num: the expected ammount of connections
126821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
127559607eaSDaniel P. Berrange  *
128559607eaSDaniel P. Berrange  * Attempt to listen to the address @addr. This method
129559607eaSDaniel P. Berrange  * will run in the foreground so the caller will not regain
130559607eaSDaniel P. Berrange  * execution control until the connection is established or
131559607eaSDaniel P. Berrange  * an error occurs.
132559607eaSDaniel P. Berrange  */
133559607eaSDaniel P. Berrange int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
134bd269ebcSMarkus Armbruster                                    SocketAddress *addr,
1354e2d8bf6SJuan Quintela                                    int num,
136559607eaSDaniel P. Berrange                                    Error **errp);
137559607eaSDaniel P. Berrange 
138559607eaSDaniel P. Berrange /**
139559607eaSDaniel P. Berrange  * qio_channel_socket_listen_async:
140559607eaSDaniel P. Berrange  * @ioc: the socket channel object
141559607eaSDaniel P. Berrange  * @addr: the address to listen to
1427959e29eSJuan Quintela  * @num: the expected ammount of connections
143559607eaSDaniel P. Berrange  * @callback: the function to invoke on completion
144559607eaSDaniel P. Berrange  * @opaque: user data to pass to @callback
145559607eaSDaniel P. Berrange  * @destroy: the function to free @opaque
1468005fdd8SPeter Xu  * @context: the context to run the async task. If %NULL, the default
1478005fdd8SPeter Xu  *           context will be used.
148559607eaSDaniel P. Berrange  *
149559607eaSDaniel P. Berrange  * Attempt to listen to the address @addr. This method
150559607eaSDaniel P. Berrange  * will run in the background so the caller will regain
151559607eaSDaniel P. Berrange  * execution control immediately. The function @callback
152fe81e932SDaniel P. Berrange  * will be invoked on completion or failure. The @addr
153fe81e932SDaniel P. Berrange  * parameter will be copied, so may be freed as soon
154fe81e932SDaniel P. Berrange  * as this function returns without waiting for completion.
155559607eaSDaniel P. Berrange  */
156559607eaSDaniel P. Berrange void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
157bd269ebcSMarkus Armbruster                                      SocketAddress *addr,
1587959e29eSJuan Quintela                                      int num,
159559607eaSDaniel P. Berrange                                      QIOTaskFunc callback,
160559607eaSDaniel P. Berrange                                      gpointer opaque,
1618005fdd8SPeter Xu                                      GDestroyNotify destroy,
1628005fdd8SPeter Xu                                      GMainContext *context);
163559607eaSDaniel P. Berrange 
164559607eaSDaniel P. Berrange 
165559607eaSDaniel P. Berrange /**
166559607eaSDaniel P. Berrange  * qio_channel_socket_dgram_sync:
167559607eaSDaniel P. Berrange  * @ioc: the socket channel object
168559607eaSDaniel P. Berrange  * @localAddr: the address to local bind address
169559607eaSDaniel P. Berrange  * @remoteAddr: the address to remote peer address
170821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
171559607eaSDaniel P. Berrange  *
172559607eaSDaniel P. Berrange  * Attempt to initialize a datagram socket bound to
173559607eaSDaniel P. Berrange  * @localAddr and communicating with peer @remoteAddr.
174559607eaSDaniel P. Berrange  * This method will run in the foreground so the caller
175559607eaSDaniel P. Berrange  * will not regain execution control until the socket
176559607eaSDaniel P. Berrange  * is established or an error occurs.
177559607eaSDaniel P. Berrange  */
178559607eaSDaniel P. Berrange int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
179bd269ebcSMarkus Armbruster                                   SocketAddress *localAddr,
180bd269ebcSMarkus Armbruster                                   SocketAddress *remoteAddr,
181559607eaSDaniel P. Berrange                                   Error **errp);
182559607eaSDaniel P. Berrange 
183559607eaSDaniel P. Berrange /**
184559607eaSDaniel P. Berrange  * qio_channel_socket_dgram_async:
185559607eaSDaniel P. Berrange  * @ioc: the socket channel object
186559607eaSDaniel P. Berrange  * @localAddr: the address to local bind address
187559607eaSDaniel P. Berrange  * @remoteAddr: the address to remote peer address
188559607eaSDaniel P. Berrange  * @callback: the function to invoke on completion
189559607eaSDaniel P. Berrange  * @opaque: user data to pass to @callback
190559607eaSDaniel P. Berrange  * @destroy: the function to free @opaque
1918005fdd8SPeter Xu  * @context: the context to run the async task. If %NULL, the default
1928005fdd8SPeter Xu  *           context will be used.
193559607eaSDaniel P. Berrange  *
194559607eaSDaniel P. Berrange  * Attempt to initialize a datagram socket bound to
195559607eaSDaniel P. Berrange  * @localAddr and communicating with peer @remoteAddr.
196559607eaSDaniel P. Berrange  * This method will run in the background so the caller
197559607eaSDaniel P. Berrange  * will regain execution control immediately. The function
198559607eaSDaniel P. Berrange  * @callback will be invoked on completion or failure.
199fe81e932SDaniel P. Berrange  * The @localAddr and @remoteAddr parameters will be copied,
200fe81e932SDaniel P. Berrange  * so may be freed as soon as this function returns without
201fe81e932SDaniel P. Berrange  * waiting for completion.
202559607eaSDaniel P. Berrange  */
203559607eaSDaniel P. Berrange void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
204bd269ebcSMarkus Armbruster                                     SocketAddress *localAddr,
205bd269ebcSMarkus Armbruster                                     SocketAddress *remoteAddr,
206559607eaSDaniel P. Berrange                                     QIOTaskFunc callback,
207559607eaSDaniel P. Berrange                                     gpointer opaque,
2088005fdd8SPeter Xu                                     GDestroyNotify destroy,
2098005fdd8SPeter Xu                                     GMainContext *context);
210559607eaSDaniel P. Berrange 
211559607eaSDaniel P. Berrange 
212559607eaSDaniel P. Berrange /**
213559607eaSDaniel P. Berrange  * qio_channel_socket_get_local_address:
214559607eaSDaniel P. Berrange  * @ioc: the socket channel object
215821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
216559607eaSDaniel P. Berrange  *
217559607eaSDaniel P. Berrange  * Get the string representation of the local socket
218559607eaSDaniel P. Berrange  * address. A pointer to the allocated address information
219559607eaSDaniel P. Berrange  * struct will be returned, which the caller is required to
220bd269ebcSMarkus Armbruster  * release with a call qapi_free_SocketAddress() when no
221559607eaSDaniel P. Berrange  * longer required.
222559607eaSDaniel P. Berrange  *
223559607eaSDaniel P. Berrange  * Returns: 0 on success, -1 on error
224559607eaSDaniel P. Berrange  */
225bd269ebcSMarkus Armbruster SocketAddress *
226559607eaSDaniel P. Berrange qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
227559607eaSDaniel P. Berrange                                      Error **errp);
228559607eaSDaniel P. Berrange 
229559607eaSDaniel P. Berrange /**
230559607eaSDaniel P. Berrange  * qio_channel_socket_get_remote_address:
231559607eaSDaniel P. Berrange  * @ioc: the socket channel object
232821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
233559607eaSDaniel P. Berrange  *
234559607eaSDaniel P. Berrange  * Get the string representation of the local socket
235559607eaSDaniel P. Berrange  * address. A pointer to the allocated address information
236559607eaSDaniel P. Berrange  * struct will be returned, which the caller is required to
237bd269ebcSMarkus Armbruster  * release with a call qapi_free_SocketAddress() when no
238559607eaSDaniel P. Berrange  * longer required.
239559607eaSDaniel P. Berrange  *
240559607eaSDaniel P. Berrange  * Returns: the socket address struct, or NULL on error
241559607eaSDaniel P. Berrange  */
242bd269ebcSMarkus Armbruster SocketAddress *
243559607eaSDaniel P. Berrange qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
244559607eaSDaniel P. Berrange                                       Error **errp);
245559607eaSDaniel P. Berrange 
246559607eaSDaniel P. Berrange 
247559607eaSDaniel P. Berrange /**
248559607eaSDaniel P. Berrange  * qio_channel_socket_accept:
249559607eaSDaniel P. Berrange  * @ioc: the socket channel object
250821791b5SDaniel P. Berrange  * @errp: pointer to a NULL-initialized error object
251559607eaSDaniel P. Berrange  *
252559607eaSDaniel P. Berrange  * If the socket represents a server, then this accepts
253559607eaSDaniel P. Berrange  * a new client connection. The returned channel will
254559607eaSDaniel P. Berrange  * represent the connected client socket.
255559607eaSDaniel P. Berrange  *
256559607eaSDaniel P. Berrange  * Returns: the new client channel, or NULL on error
257559607eaSDaniel P. Berrange  */
258559607eaSDaniel P. Berrange QIOChannelSocket *
259559607eaSDaniel P. Berrange qio_channel_socket_accept(QIOChannelSocket *ioc,
260559607eaSDaniel P. Berrange                           Error **errp);
261559607eaSDaniel P. Berrange 
262559607eaSDaniel P. Berrange 
2632a6a4076SMarkus Armbruster #endif /* QIO_CHANNEL_SOCKET_H */
264