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