2 * QEMU I/O channels sockets driver
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #ifndef QIO_CHANNEL_SOCKET_H
22 #define QIO_CHANNEL_SOCKET_H
24 #include "io/channel.h"
26 #include "qemu/sockets.h"
27 #include "qom/object.h"
29 #define TYPE_QIO_CHANNEL_SOCKET "qio-channel-socket"
30 OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelSocket
, QIO_CHANNEL_SOCKET
)
36 * The QIOChannelSocket class provides a channel implementation
37 * that can transport data over a UNIX socket or TCP socket.
38 * Beyond the core channel API, it also provides functionality
39 * for accepting client connections, tuning some socket
40 * parameters and getting socket address strings.
43 struct QIOChannelSocket
{
46 struct sockaddr_storage localAddr
;
47 socklen_t localAddrLen
;
48 struct sockaddr_storage remoteAddr
;
49 socklen_t remoteAddrLen
;
50 ssize_t zero_copy_queued
;
51 ssize_t zero_copy_sent
;
56 * qio_channel_socket_new:
58 * Create a channel for performing I/O on a socket
59 * connection, that is initially closed. After
60 * creating the socket, it must be setup as a client
61 * connection or server.
63 * Returns: the socket channel object
66 qio_channel_socket_new(void);
69 * qio_channel_socket_new_fd:
70 * @fd: the socket file descriptor
71 * @errp: pointer to a NULL-initialized error object
73 * Create a channel for performing I/O on the socket
74 * connection represented by the file descriptor @fd.
76 * Returns: the socket channel object, or NULL on error
79 qio_channel_socket_new_fd(int fd
,
84 * qio_channel_socket_connect_sync:
85 * @ioc: the socket channel object
86 * @addr: the address to connect to
87 * @errp: pointer to a NULL-initialized error object
89 * Attempt to connect to the address @addr. This method
90 * will run in the foreground so the caller will not regain
91 * execution control until the connection is established or
94 int qio_channel_socket_connect_sync(QIOChannelSocket
*ioc
,
99 * qio_channel_socket_connect_async:
100 * @ioc: the socket channel object
101 * @addr: the address to connect to
102 * @callback: the function to invoke on completion
103 * @opaque: user data to pass to @callback
104 * @destroy: the function to free @opaque
105 * @context: the context to run the async task. If %NULL, the default
106 * context will be used.
108 * Attempt to connect to the address @addr. This method
109 * will run in the background so the caller will regain
110 * execution control immediately. The function @callback
111 * will be invoked on completion or failure. The @addr
112 * parameter will be copied, so may be freed as soon
113 * as this function returns without waiting for completion.
115 void qio_channel_socket_connect_async(QIOChannelSocket
*ioc
,
117 QIOTaskFunc callback
,
119 GDestroyNotify destroy
,
120 GMainContext
*context
);
124 * qio_channel_socket_listen_sync:
125 * @ioc: the socket channel object
126 * @addr: the address to listen to
127 * @num: the expected amount of connections
128 * @errp: pointer to a NULL-initialized error object
130 * Attempt to listen to the address @addr. This method
131 * will run in the foreground so the caller will not regain
132 * execution control until the connection is established or
135 int qio_channel_socket_listen_sync(QIOChannelSocket
*ioc
,
141 * qio_channel_socket_listen_async:
142 * @ioc: the socket channel object
143 * @addr: the address to listen to
144 * @num: the expected amount of connections
145 * @callback: the function to invoke on completion
146 * @opaque: user data to pass to @callback
147 * @destroy: the function to free @opaque
148 * @context: the context to run the async task. If %NULL, the default
149 * context will be used.
151 * Attempt to listen to the address @addr. This method
152 * will run in the background so the caller will regain
153 * execution control immediately. The function @callback
154 * will be invoked on completion or failure. The @addr
155 * parameter will be copied, so may be freed as soon
156 * as this function returns without waiting for completion.
158 void qio_channel_socket_listen_async(QIOChannelSocket
*ioc
,
161 QIOTaskFunc callback
,
163 GDestroyNotify destroy
,
164 GMainContext
*context
);
168 * qio_channel_socket_dgram_sync:
169 * @ioc: the socket channel object
170 * @localAddr: the address to local bind address
171 * @remoteAddr: the address to remote peer address
172 * @errp: pointer to a NULL-initialized error object
174 * Attempt to initialize a datagram socket bound to
175 * @localAddr and communicating with peer @remoteAddr.
176 * This method will run in the foreground so the caller
177 * will not regain execution control until the socket
178 * is established or an error occurs.
180 int qio_channel_socket_dgram_sync(QIOChannelSocket
*ioc
,
181 SocketAddress
*localAddr
,
182 SocketAddress
*remoteAddr
,
186 * qio_channel_socket_dgram_async:
187 * @ioc: the socket channel object
188 * @localAddr: the address to local bind address
189 * @remoteAddr: the address to remote peer address
190 * @callback: the function to invoke on completion
191 * @opaque: user data to pass to @callback
192 * @destroy: the function to free @opaque
193 * @context: the context to run the async task. If %NULL, the default
194 * context will be used.
196 * Attempt to initialize a datagram socket bound to
197 * @localAddr and communicating with peer @remoteAddr.
198 * This method will run in the background so the caller
199 * will regain execution control immediately. The function
200 * @callback will be invoked on completion or failure.
201 * The @localAddr and @remoteAddr parameters will be copied,
202 * so may be freed as soon as this function returns without
203 * waiting for completion.
205 void qio_channel_socket_dgram_async(QIOChannelSocket
*ioc
,
206 SocketAddress
*localAddr
,
207 SocketAddress
*remoteAddr
,
208 QIOTaskFunc callback
,
210 GDestroyNotify destroy
,
211 GMainContext
*context
);
215 * qio_channel_socket_get_local_address:
216 * @ioc: the socket channel object
217 * @errp: pointer to a NULL-initialized error object
219 * Get the string representation of the local socket
220 * address. A pointer to the allocated address information
221 * struct will be returned, which the caller is required to
222 * release with a call qapi_free_SocketAddress() when no
225 * Returns: 0 on success, -1 on error
228 qio_channel_socket_get_local_address(QIOChannelSocket
*ioc
,
232 * qio_channel_socket_get_remote_address:
233 * @ioc: the socket channel object
234 * @errp: pointer to a NULL-initialized error object
236 * Get the string representation of the local socket
237 * address. A pointer to the allocated address information
238 * struct will be returned, which the caller is required to
239 * release with a call qapi_free_SocketAddress() when no
242 * Returns: the socket address struct, or NULL on error
245 qio_channel_socket_get_remote_address(QIOChannelSocket
*ioc
,
250 * qio_channel_socket_accept:
251 * @ioc: the socket channel object
252 * @errp: pointer to a NULL-initialized error object
254 * If the socket represents a server, then this accepts
255 * a new client connection. The returned channel will
256 * represent the connected client socket.
258 * Returns: the new client channel, or NULL on error
261 qio_channel_socket_accept(QIOChannelSocket
*ioc
,
265 #endif /* QIO_CHANNEL_SOCKET_H */