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
105 * Attempt to connect to the address @addr. This method
106 * will run in the background so the caller will regain
107 * execution control immediately. The function @callback
108 * will be invoked on completion or failure.
110 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
112 QIOTaskFunc callback,
114 GDestroyNotify destroy);
118 * qio_channel_socket_listen_sync:
119 * @ioc: the socket channel object
120 * @addr: the address to listen to
121 * @errp: pointer to a NULL-initialized error object
123 * Attempt to listen to the address @addr. This method
124 * will run in the foreground so the caller will not regain
125 * execution control until the connection is established or
128 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
133 * qio_channel_socket_listen_async:
134 * @ioc: the socket channel object
135 * @addr: the address to listen to
136 * @callback: the function to invoke on completion
137 * @opaque: user data to pass to @callback
138 * @destroy: the function to free @opaque
140 * Attempt to listen to the address @addr. This method
141 * will run in the background so the caller will regain
142 * execution control immediately. The function @callback
143 * will be invoked on completion or failure.
145 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
147 QIOTaskFunc callback,
149 GDestroyNotify destroy);
153 * qio_channel_socket_dgram_sync:
154 * @ioc: the socket channel object
155 * @localAddr: the address to local bind address
156 * @remoteAddr: the address to remote peer address
157 * @errp: pointer to a NULL-initialized error object
159 * Attempt to initialize a datagram socket bound to
160 * @localAddr and communicating with peer @remoteAddr.
161 * This method will run in the foreground so the caller
162 * will not regain execution control until the socket
163 * is established or an error occurs.
165 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
166 SocketAddress *localAddr,
167 SocketAddress *remoteAddr,
171 * qio_channel_socket_dgram_async:
172 * @ioc: the socket channel object
173 * @localAddr: the address to local bind address
174 * @remoteAddr: the address to remote peer address
175 * @callback: the function to invoke on completion
176 * @opaque: user data to pass to @callback
177 * @destroy: the function to free @opaque
179 * Attempt to initialize a datagram socket bound to
180 * @localAddr and communicating with peer @remoteAddr.
181 * This method will run in the background so the caller
182 * will regain execution control immediately. The function
183 * @callback will be invoked on completion or failure.
185 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
186 SocketAddress *localAddr,
187 SocketAddress *remoteAddr,
188 QIOTaskFunc callback,
190 GDestroyNotify destroy);
194 * qio_channel_socket_get_local_address:
195 * @ioc: the socket channel object
196 * @errp: pointer to a NULL-initialized error object
198 * Get the string representation of the local socket
199 * address. A pointer to the allocated address information
200 * struct will be returned, which the caller is required to
201 * release with a call qapi_free_SocketAddress when no
204 * Returns: 0 on success, -1 on error
207 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
211 * qio_channel_socket_get_remote_address:
212 * @ioc: the socket channel object
213 * @errp: pointer to a NULL-initialized error object
215 * Get the string representation of the local socket
216 * address. A pointer to the allocated address information
217 * struct will be returned, which the caller is required to
218 * release with a call qapi_free_SocketAddress when no
221 * Returns: the socket address struct, or NULL on error
224 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
229 * qio_channel_socket_accept:
230 * @ioc: the socket channel object
231 * @errp: pointer to a NULL-initialized error object
233 * If the socket represents a server, then this accepts
234 * a new client connection. The returned channel will
235 * represent the connected client socket.
237 * Returns: the new client channel, or NULL on error
240 qio_channel_socket_accept(QIOChannelSocket *ioc,
244 #endif /* QIO_CHANNEL_SOCKET_H__ */