verb
[oweals/gnunet.git] / src / include / gnunet_network_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_network_lib.h
23  * @brief basic low-level networking interface
24  * @author Nils Durner
25  */
26
27 #ifndef GNUNET_NETWORK_LIB_H
28 #define GNUNET_NETWORK_LIB_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38
39
40 /**
41  * @brief handle to a socket
42  */
43 struct GNUNET_NETWORK_Handle;
44
45 /**
46  * @brief collection of IO descriptors
47  */
48 struct GNUNET_NETWORK_FDSet;
49
50
51 #include "gnunet_disk_lib.h"
52 #include "gnunet_time_lib.h"
53
54
55 /**
56  * Accept a new connection on a socket.  Configure it for non-blocking
57  * IO and mark it as non-inheritable to child processes (set the
58  * close-on-exec flag).
59  *
60  * @param desc bound socket
61  * @param address address of the connecting peer, may be NULL
62  * @param address_len length of address
63  * @return client socket
64  */
65 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_accept (const struct
66                                                             GNUNET_NETWORK_Handle
67                                                             *desc,
68                                                             struct sockaddr
69                                                             *address,
70                                                             socklen_t *
71                                                             address_len);
72
73
74 /**
75  * Bind to a connected socket
76  *
77  * @param desc socket to bind
78  * @param address address to be bound
79  * @param address_len length of address
80  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
81  */
82 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
83                                 const struct sockaddr *address,
84                                 socklen_t address_len);
85
86 /**
87  * Close a socket.
88  *
89  * @param desc socket to close
90  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
91  */
92 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
93
94 /**
95  * Connect a socket
96  *
97  * @param desc socket to connect
98  * @param address peer address
99  * @param address_len of address
100  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
101  */
102 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
103                                    const struct sockaddr *address,
104                                    socklen_t address_len);
105
106
107 /**
108  * Get socket options
109  *
110  * @param desc socket to inspect
111  * @param level protocol level of the option
112  * @param optname identifier of the option
113  * @param optval options
114  * @param optlen length of optval
115  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
116  */
117 int GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle
118                                       *desc, int level, int optname,
119                                       void *optval, socklen_t * optlen);
120
121
122 /**
123  * Listen on a socket
124  *
125  * @param desc socket to start listening on
126  * @param backlog length of the listen queue
127  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
128  */
129 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
130                                   int backlog);
131
132 /**
133  * How much data is available to be read on this descriptor?
134  * @param desc socket
135  */
136 ssize_t
137 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
138                                        *desc);
139
140 /**
141  * Read data from a connected socket (always non-blocking).
142  * @param desc socket
143  * @param buffer buffer
144  * @param length length of buffer
145  * @param src_addr either the source to recv from, or all zeroes
146  *        to be filled in by recvfrom
147  * @param addrlen length of the addr
148  */
149 ssize_t
150 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
151                                 void *buffer, size_t length,
152                                 struct sockaddr *src_addr,
153                                 socklen_t * addrlen);
154
155 /**
156  * Read data from a connected socket (always non-blocking).
157  *
158  * @param desc socket
159  * @param buffer buffer
160  * @param length length of buffer
161  * @return number of bytes read
162  */
163 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
164                                     void *buffer, size_t length);
165
166 /**
167  * Check if sockets meet certain conditions
168  * @param rfds set of sockets to be checked for readability
169  * @param wfds set of sockets to be checked for writability
170  * @param efds set of sockets to be checked for exceptions
171  * @param timeout relative value when to return
172  * @return number of selected sockets, GNUNET_SYSERR on error
173  */
174 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
175                                   struct GNUNET_NETWORK_FDSet *wfds,
176                                   struct GNUNET_NETWORK_FDSet *efds,
177                                   struct GNUNET_TIME_Relative timeout);
178
179
180
181 /**
182  * Send data (always non-blocking).
183  *
184  * @param desc socket
185  * @param buffer data to send
186  * @param length size of the buffer
187  * @return number of bytes sent, GNUNET_SYSERR on error
188  */
189 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
190                                     const void *buffer, size_t length);
191
192 /**
193  * Send data to a particular destination (always non-blocking).
194  * This function only works for UDP sockets.
195  *
196  * @param desc socket
197  * @param message data to send
198  * @param length size of the data
199  * @param dest_addr destination address
200  * @param dest_len length of address
201  * @return number of bytes sent, GNUNET_SYSERR on error
202  */
203 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle
204                                       *desc, const void *message,
205                                       size_t length,
206                                       const struct sockaddr *dest_addr,
207                                       socklen_t dest_len);
208
209 /**
210  * Set socket option
211  * @param fd socket
212  * @param level protocol level of the option
213  * @param option_name option identifier
214  * @param option_value value to set
215  * @param option_len size of option_value
216  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
217  */
218 int GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
219                                       int level, int option_name,
220                                       const void *option_value,
221                                       socklen_t option_len);
222
223 /**
224  * Shut down socket operations
225  * @param desc socket
226  * @param how type of shutdown
227  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
228  */
229 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
230                                     int how);
231
232
233 /**
234  * Create a new socket.   Configure it for non-blocking IO and
235  * mark it as non-inheritable to child processes (set the
236  * close-on-exec flag).
237  *
238  * @param domain domain of the socket
239  * @param type socket type
240  * @param protocol network protocol
241  * @return new socket, NULL on error
242  */
243 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_create (int domain,
244                                                             int type,
245                                                             int protocol);
246
247 /**
248  * Reset FD set (clears all file descriptors).
249  *
250  * @param fds fd set to clear
251  */
252 void GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
253
254 /**
255  * Add a socket to the FD set
256  * @param fds fd set
257  * @param desc socket to add
258  */
259 void GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
260                                const struct GNUNET_NETWORK_Handle *desc);
261
262
263 /**
264  * Check whether a socket is part of the fd set
265  * @param fds fd set
266  * @param desc socket
267  */
268 int GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
269                                 const struct GNUNET_NETWORK_Handle *desc);
270
271 /**
272  * Add one fd set to another
273  * @param dst the fd set to add to
274  * @param src the fd set to add from
275  */
276 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
277                                const struct GNUNET_NETWORK_FDSet *src);
278
279 /**
280  * Copy one fd set to another
281  * @param to destination
282  * @param from source
283  */
284 void GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
285                                 const struct GNUNET_NETWORK_FDSet *from);
286
287 /*
288  * Return file descriptor for this network handle
289  */
290 int GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
291 /**
292  * Copy a native fd set
293  * @param to destination
294  * @param from native source set
295  * @param nfds the biggest socket number in from + 1
296  */
297 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
298                                        const fd_set * from, int nfds);
299
300 /**
301  * Add a file handle to the fd set
302  * @param fds fd set
303  * @param h the file handle to add
304  */
305 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
306                                       const struct GNUNET_DISK_FileHandle *h);
307
308 /**
309  * Check if a file handle is part of an fd set
310  * @param fds fd set
311  * @param h file handle
312  * @return GNUNET_YES if the file handle is part of the set
313  */
314 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
315                                        const struct GNUNET_DISK_FileHandle
316                                        *h);
317
318 /**
319  * Checks if two fd sets overlap
320  * @param fds1 first fd set
321  * @param fds2 second fd set
322  * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
323  */
324 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
325                                   const struct GNUNET_NETWORK_FDSet *fds2);
326
327 /**
328  * Creates an fd set
329  * @return a new fd set
330  */
331 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
332
333 /**
334  * Releases the associated memory of an fd set
335  * @param fds fd set
336  */
337 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
338
339
340 #if 0                           /* keep Emacsens' auto-indent happy */
341 {
342 #endif
343 #ifdef __cplusplus
344 }
345 #endif
346
347 #endif /* GNUNET_NETWORK_LIB_H */