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