]>
Commit | Line | Data |
---|---|---|
559607ea DB |
1 | /* |
2 | * QEMU I/O channels sockets driver | |
3 | * | |
4 | * Copyright (c) 2015 Red Hat, Inc. | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | * | |
19 | */ | |
20 | ||
2a6a4076 MA |
21 | #ifndef QIO_CHANNEL_SOCKET_H |
22 | #define QIO_CHANNEL_SOCKET_H | |
559607ea DB |
23 | |
24 | #include "io/channel.h" | |
25 | #include "io/task.h" | |
26 | #include "qemu/sockets.h" | |
db1015e9 | 27 | #include "qom/object.h" |
559607ea DB |
28 | |
29 | #define TYPE_QIO_CHANNEL_SOCKET "qio-channel-socket" | |
db1015e9 | 30 | typedef struct QIOChannelSocket QIOChannelSocket; |
8110fa1d EH |
31 | DECLARE_INSTANCE_CHECKER(QIOChannelSocket, QIO_CHANNEL_SOCKET, |
32 | TYPE_QIO_CHANNEL_SOCKET) | |
559607ea | 33 | |
559607ea DB |
34 | |
35 | /** | |
36 | * QIOChannelSocket: | |
37 | * | |
38 | * The QIOChannelSocket class provides a channel implementation | |
39 | * that can transport data over a UNIX socket or TCP socket. | |
40 | * Beyond the core channel API, it also provides functionality | |
41 | * for accepting client connections, tuning some socket | |
42 | * parameters and getting socket address strings. | |
43 | */ | |
44 | ||
45 | struct QIOChannelSocket { | |
46 | QIOChannel parent; | |
47 | int fd; | |
48 | struct sockaddr_storage localAddr; | |
49 | socklen_t localAddrLen; | |
50 | struct sockaddr_storage remoteAddr; | |
51 | socklen_t remoteAddrLen; | |
52 | }; | |
53 | ||
54 | ||
55 | /** | |
56 | * qio_channel_socket_new: | |
57 | * | |
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. | |
62 | * | |
63 | * Returns: the socket channel object | |
64 | */ | |
65 | QIOChannelSocket * | |
66 | qio_channel_socket_new(void); | |
67 | ||
68 | /** | |
69 | * qio_channel_socket_new_fd: | |
70 | * @fd: the socket file descriptor | |
821791b5 | 71 | * @errp: pointer to a NULL-initialized error object |
559607ea DB |
72 | * |
73 | * Create a channel for performing I/O on the socket | |
74 | * connection represented by the file descriptor @fd. | |
75 | * | |
76 | * Returns: the socket channel object, or NULL on error | |
77 | */ | |
78 | QIOChannelSocket * | |
79 | qio_channel_socket_new_fd(int fd, | |
80 | Error **errp); | |
81 | ||
82 | ||
83 | /** | |
84 | * qio_channel_socket_connect_sync: | |
85 | * @ioc: the socket channel object | |
86 | * @addr: the address to connect to | |
821791b5 | 87 | * @errp: pointer to a NULL-initialized error object |
559607ea DB |
88 | * |
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 | |
92 | * an error occurs. | |
93 | */ | |
94 | int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, | |
bd269ebc | 95 | SocketAddress *addr, |
559607ea DB |
96 | Error **errp); |
97 | ||
98 | /** | |
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 | |
8005fdd8 PX |
105 | * @context: the context to run the async task. If %NULL, the default |
106 | * context will be used. | |
559607ea DB |
107 | * |
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 | |
fe81e932 DB |
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. | |
559607ea DB |
114 | */ |
115 | void qio_channel_socket_connect_async(QIOChannelSocket *ioc, | |
bd269ebc | 116 | SocketAddress *addr, |
559607ea DB |
117 | QIOTaskFunc callback, |
118 | gpointer opaque, | |
8005fdd8 PX |
119 | GDestroyNotify destroy, |
120 | GMainContext *context); | |
559607ea DB |
121 | |
122 | ||
123 | /** | |
124 | * qio_channel_socket_listen_sync: | |
125 | * @ioc: the socket channel object | |
126 | * @addr: the address to listen to | |
4e2d8bf6 | 127 | * @num: the expected ammount of connections |
821791b5 | 128 | * @errp: pointer to a NULL-initialized error object |
559607ea DB |
129 | * |
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 | |
133 | * an error occurs. | |
134 | */ | |
135 | int qio_channel_socket_listen_sync(QIOChannelSocket *ioc, | |
bd269ebc | 136 | SocketAddress *addr, |
4e2d8bf6 | 137 | int num, |
559607ea DB |
138 | Error **errp); |
139 | ||
140 | /** | |
141 | * qio_channel_socket_listen_async: | |
142 | * @ioc: the socket channel object | |
143 | * @addr: the address to listen to | |
7959e29e | 144 | * @num: the expected ammount of connections |
559607ea DB |
145 | * @callback: the function to invoke on completion |
146 | * @opaque: user data to pass to @callback | |
147 | * @destroy: the function to free @opaque | |
8005fdd8 PX |
148 | * @context: the context to run the async task. If %NULL, the default |
149 | * context will be used. | |
559607ea DB |
150 | * |
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 | |
fe81e932 DB |
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. | |
559607ea DB |
157 | */ |
158 | void qio_channel_socket_listen_async(QIOChannelSocket *ioc, | |
bd269ebc | 159 | SocketAddress *addr, |
7959e29e | 160 | int num, |
559607ea DB |
161 | QIOTaskFunc callback, |
162 | gpointer opaque, | |
8005fdd8 PX |
163 | GDestroyNotify destroy, |
164 | GMainContext *context); | |
559607ea DB |
165 | |
166 | ||
167 | /** | |
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 | |
821791b5 | 172 | * @errp: pointer to a NULL-initialized error object |
559607ea DB |
173 | * |
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. | |
179 | */ | |
180 | int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc, | |
bd269ebc MA |
181 | SocketAddress *localAddr, |
182 | SocketAddress *remoteAddr, | |
559607ea DB |
183 | Error **errp); |
184 | ||
185 | /** | |
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 | |
8005fdd8 PX |
193 | * @context: the context to run the async task. If %NULL, the default |
194 | * context will be used. | |
559607ea DB |
195 | * |
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. | |
fe81e932 DB |
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. | |
559607ea DB |
204 | */ |
205 | void qio_channel_socket_dgram_async(QIOChannelSocket *ioc, | |
bd269ebc MA |
206 | SocketAddress *localAddr, |
207 | SocketAddress *remoteAddr, | |
559607ea DB |
208 | QIOTaskFunc callback, |
209 | gpointer opaque, | |
8005fdd8 PX |
210 | GDestroyNotify destroy, |
211 | GMainContext *context); | |
559607ea DB |
212 | |
213 | ||
214 | /** | |
215 | * qio_channel_socket_get_local_address: | |
216 | * @ioc: the socket channel object | |
821791b5 | 217 | * @errp: pointer to a NULL-initialized error object |
559607ea DB |
218 | * |
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 | |
bd269ebc | 222 | * release with a call qapi_free_SocketAddress() when no |
559607ea DB |
223 | * longer required. |
224 | * | |
225 | * Returns: 0 on success, -1 on error | |
226 | */ | |
bd269ebc | 227 | SocketAddress * |
559607ea DB |
228 | qio_channel_socket_get_local_address(QIOChannelSocket *ioc, |
229 | Error **errp); | |
230 | ||
231 | /** | |
232 | * qio_channel_socket_get_remote_address: | |
233 | * @ioc: the socket channel object | |
821791b5 | 234 | * @errp: pointer to a NULL-initialized error object |
559607ea DB |
235 | * |
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 | |
bd269ebc | 239 | * release with a call qapi_free_SocketAddress() when no |
559607ea DB |
240 | * longer required. |
241 | * | |
242 | * Returns: the socket address struct, or NULL on error | |
243 | */ | |
bd269ebc | 244 | SocketAddress * |
559607ea DB |
245 | qio_channel_socket_get_remote_address(QIOChannelSocket *ioc, |
246 | Error **errp); | |
247 | ||
248 | ||
249 | /** | |
250 | * qio_channel_socket_accept: | |
251 | * @ioc: the socket channel object | |
821791b5 | 252 | * @errp: pointer to a NULL-initialized error object |
559607ea DB |
253 | * |
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. | |
257 | * | |
258 | * Returns: the new client channel, or NULL on error | |
259 | */ | |
260 | QIOChannelSocket * | |
261 | qio_channel_socket_accept(QIOChannelSocket *ioc, | |
262 | Error **errp); | |
263 | ||
264 | ||
2a6a4076 | 265 | #endif /* QIO_CHANNEL_SOCKET_H */ |