| 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.1 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 | |
| 21 | #ifndef QIO_CHANNEL_SOCKET_H |
| 22 | #define QIO_CHANNEL_SOCKET_H |
| 23 | |
| 24 | #include "io/channel.h" |
| 25 | #include "io/task.h" |
| 26 | #include "qemu/sockets.h" |
| 27 | #include "qom/object.h" |
| 28 | |
| 29 | #define TYPE_QIO_CHANNEL_SOCKET "qio-channel-socket" |
| 30 | OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelSocket, QIO_CHANNEL_SOCKET) |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * QIOChannelSocket: |
| 35 | * |
| 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. |
| 41 | */ |
| 42 | |
| 43 | struct QIOChannelSocket { |
| 44 | QIOChannel parent; |
| 45 | int fd; |
| 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; |
| 52 | bool blocking; |
| 53 | bool zero_copy_fallback; |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * qio_channel_socket_new: |
| 59 | * |
| 60 | * Create a channel for performing I/O on a socket |
| 61 | * connection, that is initially closed. After |
| 62 | * creating the socket, it must be setup as a client |
| 63 | * connection or server. |
| 64 | * |
| 65 | * Returns: the socket channel object |
| 66 | */ |
| 67 | QIOChannelSocket * |
| 68 | qio_channel_socket_new(void); |
| 69 | |
| 70 | /** |
| 71 | * qio_channel_socket_new_fd: |
| 72 | * @fd: the socket file descriptor |
| 73 | * @errp: pointer to a NULL-initialized error object |
| 74 | * |
| 75 | * Create a channel for performing I/O on the socket |
| 76 | * connection represented by the file descriptor @fd. |
| 77 | * |
| 78 | * Returns: the socket channel object, or NULL on error |
| 79 | */ |
| 80 | QIOChannelSocket * |
| 81 | qio_channel_socket_new_fd(int fd, |
| 82 | Error **errp); |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * qio_channel_socket_connect_sync: |
| 87 | * @ioc: the socket channel object |
| 88 | * @addr: the address to connect to |
| 89 | * @errp: pointer to a NULL-initialized error object |
| 90 | * |
| 91 | * Attempt to connect to the address @addr. This method |
| 92 | * will run in the foreground so the caller will not regain |
| 93 | * execution control until the connection is established or |
| 94 | * an error occurs. |
| 95 | */ |
| 96 | int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, |
| 97 | SocketAddress *addr, |
| 98 | Error **errp); |
| 99 | |
| 100 | /** |
| 101 | * qio_channel_socket_connect_async: |
| 102 | * @ioc: the socket channel object |
| 103 | * @addr: the address to connect to |
| 104 | * @callback: the function to invoke on completion |
| 105 | * @opaque: user data to pass to @callback |
| 106 | * @destroy: the function to free @opaque |
| 107 | * @context: the context to run the async task. If %NULL, the default |
| 108 | * context will be used. |
| 109 | * |
| 110 | * Attempt to connect to the address @addr. This method |
| 111 | * will run in the background so the caller will regain |
| 112 | * execution control immediately. The function @callback |
| 113 | * will be invoked on completion or failure. The @addr |
| 114 | * parameter will be copied, so may be freed as soon |
| 115 | * as this function returns without waiting for completion. |
| 116 | */ |
| 117 | void qio_channel_socket_connect_async(QIOChannelSocket *ioc, |
| 118 | SocketAddress *addr, |
| 119 | QIOTaskFunc callback, |
| 120 | gpointer opaque, |
| 121 | GDestroyNotify destroy, |
| 122 | GMainContext *context); |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * qio_channel_socket_listen_sync: |
| 127 | * @ioc: the socket channel object |
| 128 | * @addr: the address to listen to |
| 129 | * @num: the expected amount of connections |
| 130 | * @errp: pointer to a NULL-initialized error object |
| 131 | * |
| 132 | * Attempt to listen to the address @addr. This method |
| 133 | * will run in the foreground so the caller will not regain |
| 134 | * execution control until the connection is established or |
| 135 | * an error occurs. |
| 136 | */ |
| 137 | int qio_channel_socket_listen_sync(QIOChannelSocket *ioc, |
| 138 | SocketAddress *addr, |
| 139 | int num, |
| 140 | Error **errp); |
| 141 | |
| 142 | /** |
| 143 | * qio_channel_socket_listen_async: |
| 144 | * @ioc: the socket channel object |
| 145 | * @addr: the address to listen to |
| 146 | * @num: the expected amount of connections |
| 147 | * @callback: the function to invoke on completion |
| 148 | * @opaque: user data to pass to @callback |
| 149 | * @destroy: the function to free @opaque |
| 150 | * @context: the context to run the async task. If %NULL, the default |
| 151 | * context will be used. |
| 152 | * |
| 153 | * Attempt to listen to the address @addr. This method |
| 154 | * will run in the background so the caller will regain |
| 155 | * execution control immediately. The function @callback |
| 156 | * will be invoked on completion or failure. The @addr |
| 157 | * parameter will be copied, so may be freed as soon |
| 158 | * as this function returns without waiting for completion. |
| 159 | */ |
| 160 | void qio_channel_socket_listen_async(QIOChannelSocket *ioc, |
| 161 | SocketAddress *addr, |
| 162 | int num, |
| 163 | QIOTaskFunc callback, |
| 164 | gpointer opaque, |
| 165 | GDestroyNotify destroy, |
| 166 | GMainContext *context); |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * qio_channel_socket_dgram_sync: |
| 171 | * @ioc: the socket channel object |
| 172 | * @localAddr: the address to local bind address |
| 173 | * @remoteAddr: the address to remote peer address |
| 174 | * @errp: pointer to a NULL-initialized error object |
| 175 | * |
| 176 | * Attempt to initialize a datagram socket bound to |
| 177 | * @localAddr and communicating with peer @remoteAddr. |
| 178 | * This method will run in the foreground so the caller |
| 179 | * will not regain execution control until the socket |
| 180 | * is established or an error occurs. |
| 181 | */ |
| 182 | int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc, |
| 183 | SocketAddress *localAddr, |
| 184 | SocketAddress *remoteAddr, |
| 185 | Error **errp); |
| 186 | |
| 187 | /** |
| 188 | * qio_channel_socket_dgram_async: |
| 189 | * @ioc: the socket channel object |
| 190 | * @localAddr: the address to local bind address |
| 191 | * @remoteAddr: the address to remote peer address |
| 192 | * @callback: the function to invoke on completion |
| 193 | * @opaque: user data to pass to @callback |
| 194 | * @destroy: the function to free @opaque |
| 195 | * @context: the context to run the async task. If %NULL, the default |
| 196 | * context will be used. |
| 197 | * |
| 198 | * Attempt to initialize a datagram socket bound to |
| 199 | * @localAddr and communicating with peer @remoteAddr. |
| 200 | * This method will run in the background so the caller |
| 201 | * will regain execution control immediately. The function |
| 202 | * @callback will be invoked on completion or failure. |
| 203 | * The @localAddr and @remoteAddr parameters will be copied, |
| 204 | * so may be freed as soon as this function returns without |
| 205 | * waiting for completion. |
| 206 | */ |
| 207 | void qio_channel_socket_dgram_async(QIOChannelSocket *ioc, |
| 208 | SocketAddress *localAddr, |
| 209 | SocketAddress *remoteAddr, |
| 210 | QIOTaskFunc callback, |
| 211 | gpointer opaque, |
| 212 | GDestroyNotify destroy, |
| 213 | GMainContext *context); |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * qio_channel_socket_get_local_address: |
| 218 | * @ioc: the socket channel object |
| 219 | * @errp: pointer to a NULL-initialized error object |
| 220 | * |
| 221 | * Get the string representation of the local socket |
| 222 | * address. A pointer to the allocated address information |
| 223 | * struct will be returned, which the caller is required to |
| 224 | * release with a call qapi_free_SocketAddress() when no |
| 225 | * longer required. |
| 226 | * |
| 227 | * Returns: the socket address struct, or NULL on error |
| 228 | */ |
| 229 | SocketAddress * |
| 230 | qio_channel_socket_get_local_address(QIOChannelSocket *ioc, |
| 231 | Error **errp); |
| 232 | |
| 233 | /** |
| 234 | * qio_channel_socket_get_remote_address: |
| 235 | * @ioc: the socket channel object |
| 236 | * @errp: pointer to a NULL-initialized error object |
| 237 | * |
| 238 | * Get the string representation of the local socket |
| 239 | * address. A pointer to the allocated address information |
| 240 | * struct will be returned, which the caller is required to |
| 241 | * release with a call qapi_free_SocketAddress() when no |
| 242 | * longer required. |
| 243 | * |
| 244 | * Returns: the socket address struct, or NULL on error |
| 245 | */ |
| 246 | SocketAddress * |
| 247 | qio_channel_socket_get_remote_address(QIOChannelSocket *ioc, |
| 248 | Error **errp); |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * qio_channel_socket_accept: |
| 253 | * @ioc: the socket channel object |
| 254 | * @errp: pointer to a NULL-initialized error object |
| 255 | * |
| 256 | * If the socket represents a server, then this accepts |
| 257 | * a new client connection. The returned channel will |
| 258 | * represent the connected client socket. |
| 259 | * |
| 260 | * Returns: the new client channel, or NULL on error |
| 261 | */ |
| 262 | QIOChannelSocket * |
| 263 | qio_channel_socket_accept(QIOChannelSocket *ioc, |
| 264 | Error **errp); |
| 265 | |
| 266 | /** |
| 267 | * qio_channel_socket_set_send_buffer: |
| 268 | * @ioc: the socket channel object |
| 269 | * @size: buffer size |
| 270 | * @errp: pointer to a NULL-initialized error object |
| 271 | * |
| 272 | * Set the underlying socket send buffer size. |
| 273 | * |
| 274 | * Retruns: 0 on success, or -1 on error. |
| 275 | */ |
| 276 | int qio_channel_socket_set_send_buffer(QIOChannelSocket *ioc, |
| 277 | size_t size, |
| 278 | Error **errp); |
| 279 | |
| 280 | #endif /* QIO_CHANNEL_SOCKET_H */ |