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 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"
28 #define TYPE_QIO_CHANNEL_SOCKET "qio-channel-socket"
29 #define QIO_CHANNEL_SOCKET(obj) \
30 OBJECT_CHECK(QIOChannelSocket, (obj), TYPE_QIO_CHANNEL_SOCKET)
32 typedef struct QIOChannelSocket QIOChannelSocket
;
37 * The QIOChannelSocket class provides a channel implementation
38 * that can transport data over a UNIX socket or TCP socket.
39 * Beyond the core channel API, it also provides functionality
40 * for accepting client connections, tuning some socket
41 * parameters and getting socket address strings.
44 struct QIOChannelSocket
{
47 struct sockaddr_storage localAddr
;
48 socklen_t localAddrLen
;
49 struct sockaddr_storage remoteAddr
;
50 socklen_t remoteAddrLen
;
55 * qio_channel_socket_new:
57 * Create a channel for performing I/O on a socket
58 * connection, that is initially closed. After
59 * creating the socket, it must be setup as a client
60 * connection or server.
62 * Returns: the socket channel object
65 qio_channel_socket_new(void);
68 * qio_channel_socket_new_fd:
69 * @fd: the socket file descriptor
70 * @errp: pointer to a NULL-initialized error object
72 * Create a channel for performing I/O on the socket
73 * connection represented by the file descriptor @fd.
75 * Returns: the socket channel object, or NULL on error
78 qio_channel_socket_new_fd(int fd
,
83 * qio_channel_socket_connect_sync:
84 * @ioc: the socket channel object
85 * @addr: the address to connect to
86 * @errp: pointer to a NULL-initialized error object
88 * Attempt to connect to the address @addr. This method
89 * will run in the foreground so the caller will not regain
90 * execution control until the connection is established or
93 int qio_channel_socket_connect_sync(QIOChannelSocket
*ioc
,
98 * qio_channel_socket_connect_async:
99 * @ioc: the socket channel object
100 * @addr: the address to connect to
101 * @callback: the function to invoke on completion
102 * @opaque: user data to pass to @callback
103 * @destroy: the function to free @opaque
104 * @context: the context to run the async task. If %NULL, the default
105 * context will be used.
107 * Attempt to connect to the address @addr. This method
108 * will run in the background so the caller will regain
109 * execution control immediately. The function @callback
110 * will be invoked on completion or failure. The @addr
111 * parameter will be copied, so may be freed as soon
112 * as this function returns without waiting for completion.
114 void qio_channel_socket_connect_async(QIOChannelSocket
*ioc
,
116 QIOTaskFunc callback
,
118 GDestroyNotify destroy
,
119 GMainContext
*context
);
123 * qio_channel_socket_listen_sync:
124 * @ioc: the socket channel object
125 * @addr: the address to listen to
126 * @errp: pointer to a NULL-initialized error object
128 * Attempt to listen to the address @addr. This method
129 * will run in the foreground so the caller will not regain
130 * execution control until the connection is established or
133 int qio_channel_socket_listen_sync(QIOChannelSocket
*ioc
,
138 * qio_channel_socket_listen_async:
139 * @ioc: the socket channel object
140 * @addr: the address to listen to
141 * @callback: the function to invoke on completion
142 * @opaque: user data to pass to @callback
143 * @destroy: the function to free @opaque
144 * @context: the context to run the async task. If %NULL, the default
145 * context will be used.
147 * Attempt to listen to the address @addr. This method
148 * will run in the background so the caller will regain
149 * execution control immediately. The function @callback
150 * will be invoked on completion or failure. The @addr
151 * parameter will be copied, so may be freed as soon
152 * as this function returns without waiting for completion.
154 void qio_channel_socket_listen_async(QIOChannelSocket
*ioc
,
156 QIOTaskFunc callback
,
158 GDestroyNotify destroy
,
159 GMainContext
*context
);
163 * qio_channel_socket_dgram_sync:
164 * @ioc: the socket channel object
165 * @localAddr: the address to local bind address
166 * @remoteAddr: the address to remote peer address
167 * @errp: pointer to a NULL-initialized error object
169 * Attempt to initialize a datagram socket bound to
170 * @localAddr and communicating with peer @remoteAddr.
171 * This method will run in the foreground so the caller
172 * will not regain execution control until the socket
173 * is established or an error occurs.
175 int qio_channel_socket_dgram_sync(QIOChannelSocket
*ioc
,
176 SocketAddress
*localAddr
,
177 SocketAddress
*remoteAddr
,
181 * qio_channel_socket_dgram_async:
182 * @ioc: the socket channel object
183 * @localAddr: the address to local bind address
184 * @remoteAddr: the address to remote peer address
185 * @callback: the function to invoke on completion
186 * @opaque: user data to pass to @callback
187 * @destroy: the function to free @opaque
188 * @context: the context to run the async task. If %NULL, the default
189 * context will be used.
191 * Attempt to initialize a datagram socket bound to
192 * @localAddr and communicating with peer @remoteAddr.
193 * This method will run in the background so the caller
194 * will regain execution control immediately. The function
195 * @callback will be invoked on completion or failure.
196 * The @localAddr and @remoteAddr parameters will be copied,
197 * so may be freed as soon as this function returns without
198 * waiting for completion.
200 void qio_channel_socket_dgram_async(QIOChannelSocket
*ioc
,
201 SocketAddress
*localAddr
,
202 SocketAddress
*remoteAddr
,
203 QIOTaskFunc callback
,
205 GDestroyNotify destroy
,
206 GMainContext
*context
);
210 * qio_channel_socket_get_local_address:
211 * @ioc: the socket channel object
212 * @errp: pointer to a NULL-initialized error object
214 * Get the string representation of the local socket
215 * address. A pointer to the allocated address information
216 * struct will be returned, which the caller is required to
217 * release with a call qapi_free_SocketAddress() when no
220 * Returns: 0 on success, -1 on error
223 qio_channel_socket_get_local_address(QIOChannelSocket
*ioc
,
227 * qio_channel_socket_get_remote_address:
228 * @ioc: the socket channel object
229 * @errp: pointer to a NULL-initialized error object
231 * Get the string representation of the local socket
232 * address. A pointer to the allocated address information
233 * struct will be returned, which the caller is required to
234 * release with a call qapi_free_SocketAddress() when no
237 * Returns: the socket address struct, or NULL on error
240 qio_channel_socket_get_remote_address(QIOChannelSocket
*ioc
,
245 * qio_channel_socket_accept:
246 * @ioc: the socket channel object
247 * @errp: pointer to a NULL-initialized error object
249 * If the socket represents a server, then this accepts
250 * a new client connection. The returned channel will
251 * represent the connected client socket.
253 * Returns: the new client channel, or NULL on error
256 qio_channel_socket_accept(QIOChannelSocket
*ioc
,
260 #endif /* QIO_CHANNEL_SOCKET_H */