xref: /openbmc/qemu/include/io/channel-socket.h (revision 795c40b8)
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 
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 
28 #define TYPE_QIO_CHANNEL_SOCKET "qio-channel-socket"
29 #define QIO_CHANNEL_SOCKET(obj)                                     \
30     OBJECT_CHECK(QIOChannelSocket, (obj), TYPE_QIO_CHANNEL_SOCKET)
31 
32 typedef struct QIOChannelSocket QIOChannelSocket;
33 
34 /**
35  * QIOChannelSocket:
36  *
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.
42  */
43 
44 struct QIOChannelSocket {
45     QIOChannel parent;
46     int fd;
47     struct sockaddr_storage localAddr;
48     socklen_t localAddrLen;
49     struct sockaddr_storage remoteAddr;
50     socklen_t remoteAddrLen;
51 };
52 
53 
54 /**
55  * qio_channel_socket_new:
56  *
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.
61  *
62  * Returns: the socket channel object
63  */
64 QIOChannelSocket *
65 qio_channel_socket_new(void);
66 
67 /**
68  * qio_channel_socket_new_fd:
69  * @fd: the socket file descriptor
70  * @errp: pointer to a NULL-initialized error object
71  *
72  * Create a channel for performing I/O on the socket
73  * connection represented by the file descriptor @fd.
74  *
75  * Returns: the socket channel object, or NULL on error
76  */
77 QIOChannelSocket *
78 qio_channel_socket_new_fd(int fd,
79                           Error **errp);
80 
81 
82 /**
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
87  *
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
91  * an error occurs.
92  */
93 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
94                                     SocketAddress *addr,
95                                     Error **errp);
96 
97 /**
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
104  *
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. The @addr
109  * parameter will be copied, so may be freed as soon
110  * as this function returns without waiting for completion.
111  */
112 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
113                                       SocketAddress *addr,
114                                       QIOTaskFunc callback,
115                                       gpointer opaque,
116                                       GDestroyNotify destroy);
117 
118 
119 /**
120  * qio_channel_socket_listen_sync:
121  * @ioc: the socket channel object
122  * @addr: the address to listen to
123  * @errp: pointer to a NULL-initialized error object
124  *
125  * Attempt to listen to the address @addr. This method
126  * will run in the foreground so the caller will not regain
127  * execution control until the connection is established or
128  * an error occurs.
129  */
130 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
131                                    SocketAddress *addr,
132                                    Error **errp);
133 
134 /**
135  * qio_channel_socket_listen_async:
136  * @ioc: the socket channel object
137  * @addr: the address to listen to
138  * @callback: the function to invoke on completion
139  * @opaque: user data to pass to @callback
140  * @destroy: the function to free @opaque
141  *
142  * Attempt to listen to the address @addr. This method
143  * will run in the background so the caller will regain
144  * execution control immediately. The function @callback
145  * will be invoked on completion or failure. The @addr
146  * parameter will be copied, so may be freed as soon
147  * as this function returns without waiting for completion.
148  */
149 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
150                                      SocketAddress *addr,
151                                      QIOTaskFunc callback,
152                                      gpointer opaque,
153                                      GDestroyNotify destroy);
154 
155 
156 /**
157  * qio_channel_socket_dgram_sync:
158  * @ioc: the socket channel object
159  * @localAddr: the address to local bind address
160  * @remoteAddr: the address to remote peer address
161  * @errp: pointer to a NULL-initialized error object
162  *
163  * Attempt to initialize a datagram socket bound to
164  * @localAddr and communicating with peer @remoteAddr.
165  * This method will run in the foreground so the caller
166  * will not regain execution control until the socket
167  * is established or an error occurs.
168  */
169 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
170                                   SocketAddress *localAddr,
171                                   SocketAddress *remoteAddr,
172                                   Error **errp);
173 
174 /**
175  * qio_channel_socket_dgram_async:
176  * @ioc: the socket channel object
177  * @localAddr: the address to local bind address
178  * @remoteAddr: the address to remote peer address
179  * @callback: the function to invoke on completion
180  * @opaque: user data to pass to @callback
181  * @destroy: the function to free @opaque
182  *
183  * Attempt to initialize a datagram socket bound to
184  * @localAddr and communicating with peer @remoteAddr.
185  * This method will run in the background so the caller
186  * will regain execution control immediately. The function
187  * @callback will be invoked on completion or failure.
188  * The @localAddr and @remoteAddr parameters will be copied,
189  * so may be freed as soon as this function returns without
190  * waiting for completion.
191  */
192 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
193                                     SocketAddress *localAddr,
194                                     SocketAddress *remoteAddr,
195                                     QIOTaskFunc callback,
196                                     gpointer opaque,
197                                     GDestroyNotify destroy);
198 
199 
200 /**
201  * qio_channel_socket_get_local_address:
202  * @ioc: the socket channel object
203  * @errp: pointer to a NULL-initialized error object
204  *
205  * Get the string representation of the local socket
206  * address. A pointer to the allocated address information
207  * struct will be returned, which the caller is required to
208  * release with a call qapi_free_SocketAddress() when no
209  * longer required.
210  *
211  * Returns: 0 on success, -1 on error
212  */
213 SocketAddress *
214 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
215                                      Error **errp);
216 
217 /**
218  * qio_channel_socket_get_remote_address:
219  * @ioc: the socket channel object
220  * @errp: pointer to a NULL-initialized error object
221  *
222  * Get the string representation of the local socket
223  * address. A pointer to the allocated address information
224  * struct will be returned, which the caller is required to
225  * release with a call qapi_free_SocketAddress() when no
226  * longer required.
227  *
228  * Returns: the socket address struct, or NULL on error
229  */
230 SocketAddress *
231 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
232                                       Error **errp);
233 
234 
235 /**
236  * qio_channel_socket_accept:
237  * @ioc: the socket channel object
238  * @errp: pointer to a NULL-initialized error object
239  *
240  * If the socket represents a server, then this accepts
241  * a new client connection. The returned channel will
242  * represent the connected client socket.
243  *
244  * Returns: the new client channel, or NULL on error
245  */
246 QIOChannelSocket *
247 qio_channel_socket_accept(QIOChannelSocket *ioc,
248                           Error **errp);
249 
250 
251 #endif /* QIO_CHANNEL_SOCKET_H */
252