xref: /openbmc/qemu/include/io/channel-socket.h (revision 6598f0cd)
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  * @context: the context to run the async task. If %NULL, the default
105  *           context will be used.
106  *
107  * Attempt to connect to the address @addr. This method
108  * will run in the background so the caller will regain
109  * execution control immediately. The function @callback
110  * will be invoked on completion or failure. The @addr
111  * parameter will be copied, so may be freed as soon
112  * as this function returns without waiting for completion.
113  */
114 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
115                                       SocketAddress *addr,
116                                       QIOTaskFunc callback,
117                                       gpointer opaque,
118                                       GDestroyNotify destroy,
119                                       GMainContext *context);
120 
121 
122 /**
123  * qio_channel_socket_listen_sync:
124  * @ioc: the socket channel object
125  * @addr: the address to listen to
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  */
133 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
134                                    SocketAddress *addr,
135                                    Error **errp);
136 
137 /**
138  * qio_channel_socket_listen_async:
139  * @ioc: the socket channel object
140  * @addr: the address to listen to
141  * @callback: the function to invoke on completion
142  * @opaque: user data to pass to @callback
143  * @destroy: the function to free @opaque
144  * @context: the context to run the async task. If %NULL, the default
145  *           context will be used.
146  *
147  * Attempt to listen to the address @addr. This method
148  * will run in the background so the caller will regain
149  * execution control immediately. The function @callback
150  * will be invoked on completion or failure. The @addr
151  * parameter will be copied, so may be freed as soon
152  * as this function returns without waiting for completion.
153  */
154 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
155                                      SocketAddress *addr,
156                                      QIOTaskFunc callback,
157                                      gpointer opaque,
158                                      GDestroyNotify destroy,
159                                      GMainContext *context);
160 
161 
162 /**
163  * qio_channel_socket_dgram_sync:
164  * @ioc: the socket channel object
165  * @localAddr: the address to local bind address
166  * @remoteAddr: the address to remote peer address
167  * @errp: pointer to a NULL-initialized error object
168  *
169  * Attempt to initialize a datagram socket bound to
170  * @localAddr and communicating with peer @remoteAddr.
171  * This method will run in the foreground so the caller
172  * will not regain execution control until the socket
173  * is established or an error occurs.
174  */
175 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
176                                   SocketAddress *localAddr,
177                                   SocketAddress *remoteAddr,
178                                   Error **errp);
179 
180 /**
181  * qio_channel_socket_dgram_async:
182  * @ioc: the socket channel object
183  * @localAddr: the address to local bind address
184  * @remoteAddr: the address to remote peer address
185  * @callback: the function to invoke on completion
186  * @opaque: user data to pass to @callback
187  * @destroy: the function to free @opaque
188  * @context: the context to run the async task. If %NULL, the default
189  *           context will be used.
190  *
191  * Attempt to initialize a datagram socket bound to
192  * @localAddr and communicating with peer @remoteAddr.
193  * This method will run in the background so the caller
194  * will regain execution control immediately. The function
195  * @callback will be invoked on completion or failure.
196  * The @localAddr and @remoteAddr parameters will be copied,
197  * so may be freed as soon as this function returns without
198  * waiting for completion.
199  */
200 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
201                                     SocketAddress *localAddr,
202                                     SocketAddress *remoteAddr,
203                                     QIOTaskFunc callback,
204                                     gpointer opaque,
205                                     GDestroyNotify destroy,
206                                     GMainContext *context);
207 
208 
209 /**
210  * qio_channel_socket_get_local_address:
211  * @ioc: the socket channel object
212  * @errp: pointer to a NULL-initialized error object
213  *
214  * Get the string representation of the local socket
215  * address. A pointer to the allocated address information
216  * struct will be returned, which the caller is required to
217  * release with a call qapi_free_SocketAddress() when no
218  * longer required.
219  *
220  * Returns: 0 on success, -1 on error
221  */
222 SocketAddress *
223 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
224                                      Error **errp);
225 
226 /**
227  * qio_channel_socket_get_remote_address:
228  * @ioc: the socket channel object
229  * @errp: pointer to a NULL-initialized error object
230  *
231  * Get the string representation of the local socket
232  * address. A pointer to the allocated address information
233  * struct will be returned, which the caller is required to
234  * release with a call qapi_free_SocketAddress() when no
235  * longer required.
236  *
237  * Returns: the socket address struct, or NULL on error
238  */
239 SocketAddress *
240 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
241                                       Error **errp);
242 
243 
244 /**
245  * qio_channel_socket_accept:
246  * @ioc: the socket channel object
247  * @errp: pointer to a NULL-initialized error object
248  *
249  * If the socket represents a server, then this accepts
250  * a new client connection. The returned channel will
251  * represent the connected client socket.
252  *
253  * Returns: the new client channel, or NULL on error
254  */
255 QIOChannelSocket *
256 qio_channel_socket_accept(QIOChannelSocket *ioc,
257                           Error **errp);
258 
259 
260 #endif /* QIO_CHANNEL_SOCKET_H */
261