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