1559607eaSDaniel P. Berrange /* 2559607eaSDaniel P. Berrange * QEMU I/O channels sockets driver 3559607eaSDaniel P. Berrange * 4559607eaSDaniel P. Berrange * Copyright (c) 2015 Red Hat, Inc. 5559607eaSDaniel P. Berrange * 6559607eaSDaniel P. Berrange * This library is free software; you can redistribute it and/or 7559607eaSDaniel P. Berrange * modify it under the terms of the GNU Lesser General Public 8559607eaSDaniel P. Berrange * License as published by the Free Software Foundation; either 9559607eaSDaniel P. Berrange * version 2 of the License, or (at your option) any later version. 10559607eaSDaniel P. Berrange * 11559607eaSDaniel P. Berrange * This library is distributed in the hope that it will be useful, 12559607eaSDaniel P. Berrange * but WITHOUT ANY WARRANTY; without even the implied warranty of 13559607eaSDaniel P. Berrange * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14559607eaSDaniel P. Berrange * Lesser General Public License for more details. 15559607eaSDaniel P. Berrange * 16559607eaSDaniel P. Berrange * You should have received a copy of the GNU Lesser General Public 17559607eaSDaniel P. Berrange * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18559607eaSDaniel P. Berrange * 19559607eaSDaniel P. Berrange */ 20559607eaSDaniel P. Berrange 212a6a4076SMarkus Armbruster #ifndef QIO_CHANNEL_SOCKET_H 222a6a4076SMarkus Armbruster #define QIO_CHANNEL_SOCKET_H 23559607eaSDaniel P. Berrange 24559607eaSDaniel P. Berrange #include "io/channel.h" 25559607eaSDaniel P. Berrange #include "io/task.h" 26559607eaSDaniel P. Berrange #include "qemu/sockets.h" 27559607eaSDaniel P. Berrange 28559607eaSDaniel P. Berrange #define TYPE_QIO_CHANNEL_SOCKET "qio-channel-socket" 29559607eaSDaniel P. Berrange #define QIO_CHANNEL_SOCKET(obj) \ 30559607eaSDaniel P. Berrange OBJECT_CHECK(QIOChannelSocket, (obj), TYPE_QIO_CHANNEL_SOCKET) 31559607eaSDaniel P. Berrange 32559607eaSDaniel P. Berrange typedef struct QIOChannelSocket QIOChannelSocket; 33559607eaSDaniel P. Berrange 34559607eaSDaniel P. Berrange /** 35559607eaSDaniel P. Berrange * QIOChannelSocket: 36559607eaSDaniel P. Berrange * 37559607eaSDaniel P. Berrange * The QIOChannelSocket class provides a channel implementation 38559607eaSDaniel P. Berrange * that can transport data over a UNIX socket or TCP socket. 39559607eaSDaniel P. Berrange * Beyond the core channel API, it also provides functionality 40559607eaSDaniel P. Berrange * for accepting client connections, tuning some socket 41559607eaSDaniel P. Berrange * parameters and getting socket address strings. 42559607eaSDaniel P. Berrange */ 43559607eaSDaniel P. Berrange 44559607eaSDaniel P. Berrange struct QIOChannelSocket { 45559607eaSDaniel P. Berrange QIOChannel parent; 46559607eaSDaniel P. Berrange int fd; 47559607eaSDaniel P. Berrange struct sockaddr_storage localAddr; 48559607eaSDaniel P. Berrange socklen_t localAddrLen; 49559607eaSDaniel P. Berrange struct sockaddr_storage remoteAddr; 50559607eaSDaniel P. Berrange socklen_t remoteAddrLen; 51559607eaSDaniel P. Berrange }; 52559607eaSDaniel P. Berrange 53559607eaSDaniel P. Berrange 54559607eaSDaniel P. Berrange /** 55559607eaSDaniel P. Berrange * qio_channel_socket_new: 56559607eaSDaniel P. Berrange * 57559607eaSDaniel P. Berrange * Create a channel for performing I/O on a socket 58559607eaSDaniel P. Berrange * connection, that is initially closed. After 59559607eaSDaniel P. Berrange * creating the socket, it must be setup as a client 60559607eaSDaniel P. Berrange * connection or server. 61559607eaSDaniel P. Berrange * 62559607eaSDaniel P. Berrange * Returns: the socket channel object 63559607eaSDaniel P. Berrange */ 64559607eaSDaniel P. Berrange QIOChannelSocket * 65559607eaSDaniel P. Berrange qio_channel_socket_new(void); 66559607eaSDaniel P. Berrange 67559607eaSDaniel P. Berrange /** 68559607eaSDaniel P. Berrange * qio_channel_socket_new_fd: 69559607eaSDaniel P. Berrange * @fd: the socket file descriptor 70821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 71559607eaSDaniel P. Berrange * 72559607eaSDaniel P. Berrange * Create a channel for performing I/O on the socket 73559607eaSDaniel P. Berrange * connection represented by the file descriptor @fd. 74559607eaSDaniel P. Berrange * 75559607eaSDaniel P. Berrange * Returns: the socket channel object, or NULL on error 76559607eaSDaniel P. Berrange */ 77559607eaSDaniel P. Berrange QIOChannelSocket * 78559607eaSDaniel P. Berrange qio_channel_socket_new_fd(int fd, 79559607eaSDaniel P. Berrange Error **errp); 80559607eaSDaniel P. Berrange 81559607eaSDaniel P. Berrange 82559607eaSDaniel P. Berrange /** 83559607eaSDaniel P. Berrange * qio_channel_socket_connect_sync: 84559607eaSDaniel P. Berrange * @ioc: the socket channel object 85559607eaSDaniel P. Berrange * @addr: the address to connect to 86821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 87559607eaSDaniel P. Berrange * 88559607eaSDaniel P. Berrange * Attempt to connect to the address @addr. This method 89559607eaSDaniel P. Berrange * will run in the foreground so the caller will not regain 90559607eaSDaniel P. Berrange * execution control until the connection is established or 91559607eaSDaniel P. Berrange * an error occurs. 92559607eaSDaniel P. Berrange */ 93559607eaSDaniel P. Berrange int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, 94bd269ebcSMarkus Armbruster SocketAddress *addr, 95559607eaSDaniel P. Berrange Error **errp); 96559607eaSDaniel P. Berrange 97559607eaSDaniel P. Berrange /** 98559607eaSDaniel P. Berrange * qio_channel_socket_connect_async: 99559607eaSDaniel P. Berrange * @ioc: the socket channel object 100559607eaSDaniel P. Berrange * @addr: the address to connect to 101559607eaSDaniel P. Berrange * @callback: the function to invoke on completion 102559607eaSDaniel P. Berrange * @opaque: user data to pass to @callback 103559607eaSDaniel P. Berrange * @destroy: the function to free @opaque 1048005fdd8SPeter Xu * @context: the context to run the async task. If %NULL, the default 1058005fdd8SPeter Xu * context will be used. 106559607eaSDaniel P. Berrange * 107559607eaSDaniel P. Berrange * Attempt to connect to the address @addr. This method 108559607eaSDaniel P. Berrange * will run in the background so the caller will regain 109559607eaSDaniel P. Berrange * execution control immediately. The function @callback 110fe81e932SDaniel P. Berrange * will be invoked on completion or failure. The @addr 111fe81e932SDaniel P. Berrange * parameter will be copied, so may be freed as soon 112fe81e932SDaniel P. Berrange * as this function returns without waiting for completion. 113559607eaSDaniel P. Berrange */ 114559607eaSDaniel P. Berrange void qio_channel_socket_connect_async(QIOChannelSocket *ioc, 115bd269ebcSMarkus Armbruster SocketAddress *addr, 116559607eaSDaniel P. Berrange QIOTaskFunc callback, 117559607eaSDaniel P. Berrange gpointer opaque, 1188005fdd8SPeter Xu GDestroyNotify destroy, 1198005fdd8SPeter Xu GMainContext *context); 120559607eaSDaniel P. Berrange 121559607eaSDaniel P. Berrange 122559607eaSDaniel P. Berrange /** 123559607eaSDaniel P. Berrange * qio_channel_socket_listen_sync: 124559607eaSDaniel P. Berrange * @ioc: the socket channel object 125559607eaSDaniel P. Berrange * @addr: the address to listen to 1264e2d8bf6SJuan Quintela * @num: the expected ammount of connections 127821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 128559607eaSDaniel P. Berrange * 129559607eaSDaniel P. Berrange * Attempt to listen to the address @addr. This method 130559607eaSDaniel P. Berrange * will run in the foreground so the caller will not regain 131559607eaSDaniel P. Berrange * execution control until the connection is established or 132559607eaSDaniel P. Berrange * an error occurs. 133559607eaSDaniel P. Berrange */ 134559607eaSDaniel P. Berrange int qio_channel_socket_listen_sync(QIOChannelSocket *ioc, 135bd269ebcSMarkus Armbruster SocketAddress *addr, 1364e2d8bf6SJuan Quintela int num, 137559607eaSDaniel P. Berrange Error **errp); 138559607eaSDaniel P. Berrange 139559607eaSDaniel P. Berrange /** 140559607eaSDaniel P. Berrange * qio_channel_socket_listen_async: 141559607eaSDaniel P. Berrange * @ioc: the socket channel object 142559607eaSDaniel P. Berrange * @addr: the address to listen to 143*7959e29eSJuan Quintela * @num: the expected ammount of connections 144559607eaSDaniel P. Berrange * @callback: the function to invoke on completion 145559607eaSDaniel P. Berrange * @opaque: user data to pass to @callback 146559607eaSDaniel P. Berrange * @destroy: the function to free @opaque 1478005fdd8SPeter Xu * @context: the context to run the async task. If %NULL, the default 1488005fdd8SPeter Xu * context will be used. 149559607eaSDaniel P. Berrange * 150559607eaSDaniel P. Berrange * Attempt to listen to the address @addr. This method 151559607eaSDaniel P. Berrange * will run in the background so the caller will regain 152559607eaSDaniel P. Berrange * execution control immediately. The function @callback 153fe81e932SDaniel P. Berrange * will be invoked on completion or failure. The @addr 154fe81e932SDaniel P. Berrange * parameter will be copied, so may be freed as soon 155fe81e932SDaniel P. Berrange * as this function returns without waiting for completion. 156559607eaSDaniel P. Berrange */ 157559607eaSDaniel P. Berrange void qio_channel_socket_listen_async(QIOChannelSocket *ioc, 158bd269ebcSMarkus Armbruster SocketAddress *addr, 159*7959e29eSJuan Quintela int num, 160559607eaSDaniel P. Berrange QIOTaskFunc callback, 161559607eaSDaniel P. Berrange gpointer opaque, 1628005fdd8SPeter Xu GDestroyNotify destroy, 1638005fdd8SPeter Xu GMainContext *context); 164559607eaSDaniel P. Berrange 165559607eaSDaniel P. Berrange 166559607eaSDaniel P. Berrange /** 167559607eaSDaniel P. Berrange * qio_channel_socket_dgram_sync: 168559607eaSDaniel P. Berrange * @ioc: the socket channel object 169559607eaSDaniel P. Berrange * @localAddr: the address to local bind address 170559607eaSDaniel P. Berrange * @remoteAddr: the address to remote peer address 171821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 172559607eaSDaniel P. Berrange * 173559607eaSDaniel P. Berrange * Attempt to initialize a datagram socket bound to 174559607eaSDaniel P. Berrange * @localAddr and communicating with peer @remoteAddr. 175559607eaSDaniel P. Berrange * This method will run in the foreground so the caller 176559607eaSDaniel P. Berrange * will not regain execution control until the socket 177559607eaSDaniel P. Berrange * is established or an error occurs. 178559607eaSDaniel P. Berrange */ 179559607eaSDaniel P. Berrange int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc, 180bd269ebcSMarkus Armbruster SocketAddress *localAddr, 181bd269ebcSMarkus Armbruster SocketAddress *remoteAddr, 182559607eaSDaniel P. Berrange Error **errp); 183559607eaSDaniel P. Berrange 184559607eaSDaniel P. Berrange /** 185559607eaSDaniel P. Berrange * qio_channel_socket_dgram_async: 186559607eaSDaniel P. Berrange * @ioc: the socket channel object 187559607eaSDaniel P. Berrange * @localAddr: the address to local bind address 188559607eaSDaniel P. Berrange * @remoteAddr: the address to remote peer address 189559607eaSDaniel P. Berrange * @callback: the function to invoke on completion 190559607eaSDaniel P. Berrange * @opaque: user data to pass to @callback 191559607eaSDaniel P. Berrange * @destroy: the function to free @opaque 1928005fdd8SPeter Xu * @context: the context to run the async task. If %NULL, the default 1938005fdd8SPeter Xu * context will be used. 194559607eaSDaniel P. Berrange * 195559607eaSDaniel P. Berrange * Attempt to initialize a datagram socket bound to 196559607eaSDaniel P. Berrange * @localAddr and communicating with peer @remoteAddr. 197559607eaSDaniel P. Berrange * This method will run in the background so the caller 198559607eaSDaniel P. Berrange * will regain execution control immediately. The function 199559607eaSDaniel P. Berrange * @callback will be invoked on completion or failure. 200fe81e932SDaniel P. Berrange * The @localAddr and @remoteAddr parameters will be copied, 201fe81e932SDaniel P. Berrange * so may be freed as soon as this function returns without 202fe81e932SDaniel P. Berrange * waiting for completion. 203559607eaSDaniel P. Berrange */ 204559607eaSDaniel P. Berrange void qio_channel_socket_dgram_async(QIOChannelSocket *ioc, 205bd269ebcSMarkus Armbruster SocketAddress *localAddr, 206bd269ebcSMarkus Armbruster SocketAddress *remoteAddr, 207559607eaSDaniel P. Berrange QIOTaskFunc callback, 208559607eaSDaniel P. Berrange gpointer opaque, 2098005fdd8SPeter Xu GDestroyNotify destroy, 2108005fdd8SPeter Xu GMainContext *context); 211559607eaSDaniel P. Berrange 212559607eaSDaniel P. Berrange 213559607eaSDaniel P. Berrange /** 214559607eaSDaniel P. Berrange * qio_channel_socket_get_local_address: 215559607eaSDaniel P. Berrange * @ioc: the socket channel object 216821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 217559607eaSDaniel P. Berrange * 218559607eaSDaniel P. Berrange * Get the string representation of the local socket 219559607eaSDaniel P. Berrange * address. A pointer to the allocated address information 220559607eaSDaniel P. Berrange * struct will be returned, which the caller is required to 221bd269ebcSMarkus Armbruster * release with a call qapi_free_SocketAddress() when no 222559607eaSDaniel P. Berrange * longer required. 223559607eaSDaniel P. Berrange * 224559607eaSDaniel P. Berrange * Returns: 0 on success, -1 on error 225559607eaSDaniel P. Berrange */ 226bd269ebcSMarkus Armbruster SocketAddress * 227559607eaSDaniel P. Berrange qio_channel_socket_get_local_address(QIOChannelSocket *ioc, 228559607eaSDaniel P. Berrange Error **errp); 229559607eaSDaniel P. Berrange 230559607eaSDaniel P. Berrange /** 231559607eaSDaniel P. Berrange * qio_channel_socket_get_remote_address: 232559607eaSDaniel P. Berrange * @ioc: the socket channel object 233821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 234559607eaSDaniel P. Berrange * 235559607eaSDaniel P. Berrange * Get the string representation of the local socket 236559607eaSDaniel P. Berrange * address. A pointer to the allocated address information 237559607eaSDaniel P. Berrange * struct will be returned, which the caller is required to 238bd269ebcSMarkus Armbruster * release with a call qapi_free_SocketAddress() when no 239559607eaSDaniel P. Berrange * longer required. 240559607eaSDaniel P. Berrange * 241559607eaSDaniel P. Berrange * Returns: the socket address struct, or NULL on error 242559607eaSDaniel P. Berrange */ 243bd269ebcSMarkus Armbruster SocketAddress * 244559607eaSDaniel P. Berrange qio_channel_socket_get_remote_address(QIOChannelSocket *ioc, 245559607eaSDaniel P. Berrange Error **errp); 246559607eaSDaniel P. Berrange 247559607eaSDaniel P. Berrange 248559607eaSDaniel P. Berrange /** 249559607eaSDaniel P. Berrange * qio_channel_socket_accept: 250559607eaSDaniel P. Berrange * @ioc: the socket channel object 251821791b5SDaniel P. Berrange * @errp: pointer to a NULL-initialized error object 252559607eaSDaniel P. Berrange * 253559607eaSDaniel P. Berrange * If the socket represents a server, then this accepts 254559607eaSDaniel P. Berrange * a new client connection. The returned channel will 255559607eaSDaniel P. Berrange * represent the connected client socket. 256559607eaSDaniel P. Berrange * 257559607eaSDaniel P. Berrange * Returns: the new client channel, or NULL on error 258559607eaSDaniel P. Berrange */ 259559607eaSDaniel P. Berrange QIOChannelSocket * 260559607eaSDaniel P. Berrange qio_channel_socket_accept(QIOChannelSocket *ioc, 261559607eaSDaniel P. Berrange Error **errp); 262559607eaSDaniel P. Berrange 263559607eaSDaniel P. Berrange 2642a6a4076SMarkus Armbruster #endif /* QIO_CHANNEL_SOCKET_H */ 265