Exponential backoff for find finger trail task
[oweals/gnunet.git] / src / include / gnunet_network_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009-2013 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 3, 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 #ifndef GNUNET_NETWORK_LIB_H
27 #define GNUNET_NETWORK_LIB_H
28
29 #ifdef __cplusplus
30 extern "C"
31 {
32 #if 0                           /* keep Emacsens' auto-indent happy */
33 }
34 #endif
35 #endif
36
37
38 /**
39  * @brief handle to a socket
40  */
41 struct GNUNET_NETWORK_Handle;
42
43
44 /**
45  * @brief collection of IO descriptors
46  */
47 struct GNUNET_NETWORK_FDSet
48 {
49
50   /**
51    * Maximum number of any socket socket descriptor in the set (plus one)
52    */
53   int nsds;
54
55   /**
56    * Bitset with the descriptors.
57    */
58   fd_set sds;
59
60 #ifdef WINDOWS
61   /**
62    * Linked list of handles
63    */
64   struct GNUNET_CONTAINER_SList *handles;
65 #endif
66
67 };
68
69 #include "gnunet_disk_lib.h"
70 #include "gnunet_time_lib.h"
71
72 /**
73  * Test if the given protocol family is supported by this system.
74  *
75  * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
76  * @return #GNUNET_OK if the PF is supported
77  */
78 int
79 GNUNET_NETWORK_test_pf (int pf);
80
81
82 /**
83  * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
84  * shorten it to an acceptable length while keeping it unique
85  * and making sure it remains a valid filename (if possible).
86  *
87  * @param unixpath long path, will be freed (or same pointer returned
88  *        with moved 0-termination).
89  * @return shortened unixpath, NULL on error
90  */
91 char *
92 GNUNET_NETWORK_shorten_unixpath (char *unixpath);
93
94
95 /**
96  * Accept a new connection on a socket.  Configure it for non-blocking
97  * IO and mark it as non-inheritable to child processes (set the
98  * close-on-exec flag).
99  *
100  * @param desc bound socket
101  * @param address address of the connecting peer, may be NULL
102  * @param address_len length of address
103  * @return client socket
104  */
105 struct GNUNET_NETWORK_Handle *
106 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
107                               struct sockaddr *address,
108                               socklen_t *address_len);
109
110
111 /**
112  * Box a native socket (and check that it is a socket).
113  *
114  * @param fd socket to box
115  * @return NULL on error (including not supported on target platform)
116  */
117 struct GNUNET_NETWORK_Handle *
118 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd);
119
120
121 /**
122  * Set if a socket should use blocking or non-blocking IO.
123  *
124  * @param fd socket
125  * @param doBlock blocking mode
126  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
127  */
128 int
129 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
130                                     int doBlock);
131
132
133 /**
134  * Bind a socket to a particular address.
135  *
136  * @param desc socket to bind
137  * @param address address to be bound
138  * @param address_len length of @a address
139  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
140  */
141 int
142 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
143                             const struct sockaddr *address,
144                             socklen_t address_len);
145
146 /**
147  * Close a socket.
148  *
149  * @param desc socket to close
150  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
151  */
152 int
153 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
154
155
156 /**
157  * Only free memory of a socket, keep the file descriptor untouched.
158  *
159  * @param desc socket
160  */
161 void
162 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc);
163
164
165 /**
166  * Connect a socket to some remote address.
167  *
168  * @param desc socket to connect
169  * @param address peer address
170  * @param address_len of @a address
171  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
172  */
173 int
174 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
175                                const struct sockaddr *address,
176                                socklen_t address_len);
177
178
179 /**
180  * Get socket options
181  *
182  * @param desc socket to inspect
183  * @param level protocol level of the option
184  * @param optname identifier of the option
185  * @param optval options
186  * @param optlen length of optval
187  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
188  */
189 int
190 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
191                                   int level,
192                                   int optname,
193                                   void *optval,
194                                   socklen_t *optlen);
195
196
197 /**
198  * Listen on a socket
199  *
200  * @param desc socket to start listening on
201  * @param backlog length of the listen queue
202  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
203  */
204 int
205 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
206                               int backlog);
207
208
209 /**
210  * How much data is available to be read on this descriptor?
211  *
212  * @param desc socket
213  * @returns #GNUNET_NO if no data is available, or on error!
214  */
215 ssize_t
216 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc);
217
218
219 /**
220  * Read data from a socket (always non-blocking).
221  *
222  * @param desc socket
223  * @param buffer buffer
224  * @param length length of buffer
225  * @param src_addr either the source to recv from, or all zeroes
226  *        to be filled in by recvfrom
227  * @param addrlen length of the addr
228  */
229 ssize_t
230 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
231                                 void *buffer,
232                                 size_t length,
233                                 struct sockaddr *src_addr,
234                                 socklen_t *addrlen);
235
236
237 /**
238  * Read data from a connected socket (always non-blocking).
239  *
240  * @param desc socket
241  * @param buffer buffer
242  * @param length length of buffer
243  * @return number of bytes read
244  */
245 ssize_t
246 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
247                             void *buffer,
248                             size_t length);
249
250
251 /**
252  * Check if sockets meet certain conditions.
253  *
254  * @param rfds set of sockets to be checked for readability
255  * @param wfds set of sockets to be checked for writability
256  * @param efds set of sockets to be checked for exceptions
257  * @param timeout relative value when to return
258  * @return number of selected sockets, #GNUNET_SYSERR on error
259  */
260 int
261 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
262                               struct GNUNET_NETWORK_FDSet *wfds,
263                               struct GNUNET_NETWORK_FDSet *efds,
264                               struct GNUNET_TIME_Relative timeout);
265
266
267 /**
268  * Send data (always non-blocking).
269  *
270  * @param desc socket
271  * @param buffer data to send
272  * @param length size of the buffer
273  * @return number of bytes sent, #GNUNET_SYSERR on error
274  */
275 ssize_t
276 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
277                             const void *buffer,
278                             size_t length);
279
280
281 /**
282  * Send data to a particular destination (always non-blocking).
283  * This function only works for UDP sockets.
284  *
285  * @param desc socket
286  * @param message data to send
287  * @param length size of the data in @a message
288  * @param dest_addr destination address
289  * @param dest_len length of @a dest_addr
290  * @return number of bytes sent, #GNUNET_SYSERR on error
291  */
292 ssize_t
293 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
294                               const void *message,
295                               size_t length,
296                               const struct sockaddr *dest_addr,
297                               socklen_t dest_len);
298
299
300 /**
301  * Set socket option
302  *
303  * @param fd socket
304  * @param level protocol level of the option
305  * @param option_name option identifier
306  * @param option_value value to set
307  * @param option_len size of @a option_value
308  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
309  */
310 int
311 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
312                                   int level,
313                                   int option_name,
314                                   const void *option_value,
315                                   socklen_t option_len);
316
317
318 /**
319  * Shut down socket operations
320  *
321  * @param desc socket
322  * @param how type of shutdown
323  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
324  */
325 int
326 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
327                                 int how);
328
329
330 /**
331  * Disable the "CORK" feature for communication with the given socket,
332  * forcing the OS to immediately flush the buffer on transmission
333  * instead of potentially buffering multiple messages.  Essentially
334  * reduces the OS send buffers to zero.
335  *
336  * @param desc socket
337  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
338  */
339 int
340 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
341
342
343 /**
344  * Create a new socket.   Configure it for non-blocking IO and
345  * mark it as non-inheritable to child processes (set the
346  * close-on-exec flag).
347  *
348  * @param domain domain of the socket
349  * @param type socket type
350  * @param protocol network protocol
351  * @return new socket, NULL on error
352  */
353 struct GNUNET_NETWORK_Handle *
354 GNUNET_NETWORK_socket_create (int domain,
355                               int type,
356                               int protocol);
357
358
359 /**
360  * Reset FD set (clears all file descriptors).
361  *
362  * @param fds fd set to clear
363  */
364 void
365 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
366
367
368 /**
369  * Add a socket to the FD set
370  *
371  * @param fds fd set
372  * @param desc socket to add
373  */
374 void
375 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
376                           const struct GNUNET_NETWORK_Handle *desc);
377
378
379 #if WINDOWS
380 /**
381  * Add a W32 file handle to the fd set
382  *
383  * @param fds fd set
384  * @param h the file handle to add
385  */
386 void
387 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet *fds,
388                                                    HANDLE h);
389 #endif
390
391
392 /**
393  * Check whether a socket is part of the fd set
394  *
395  * @param fds fd set
396  * @param desc socket
397  * @return #GNUNET_YES if the socket is in the set
398  */
399 int
400 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
401                             const struct GNUNET_NETWORK_Handle *desc);
402
403
404 /**
405  * Add one fd set to another (computes the union).
406  *
407  * @param dst the fd set to add to
408  * @param src the fd set to add from
409  */
410 void
411 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
412                           const struct GNUNET_NETWORK_FDSet *src);
413
414
415 /**
416  * Copy one fd set to another
417  *
418  * @param to destination
419  * @param from source
420  */
421 void
422 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
423                            const struct GNUNET_NETWORK_FDSet *from);
424
425
426 /**
427  * Return file descriptor for this network handle
428  *
429  * @param desc wrapper to process
430  * @return POSIX file descriptor
431  */
432 int
433 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
434
435
436 /**
437  * Return the sockaddr for this network handle
438  *
439  * @param desc wrapper to process
440  * @return POSIX file descriptor
441  */
442 struct sockaddr*
443 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc);
444
445
446 /**
447  * Return sockaddr length for this network handle
448  *
449  * @param desc wrapper to process
450  * @return socklen_t for sockaddr
451  */
452 socklen_t
453 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc);
454
455
456 /**
457  * Copy a native fd set into the GNUnet representation.
458  *
459  * @param to destination
460  * @param from native source set
461  * @param nfds the biggest socket number in from + 1
462  */
463 void
464 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
465                                   const fd_set *from,
466                                   int nfds);
467
468
469 /**
470  * Set a native fd in a set
471  *
472  * @param to destination
473  * @param nfd native FD to set
474  */
475 void
476 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
477                                  int nfd);
478
479
480 /**
481  * Test native fd in a set
482  *
483  * @param to set to test, NULL for empty set
484  * @param nfd native FD to test, -1 for none
485  * @return #GNUNET_YES if to contains nfd
486  */
487 int
488 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
489                                   int nfd);
490
491
492 /**
493  * Add a file handle to the fd set
494  *
495  * @param fds fd set
496  * @param h the file handle to add
497  */
498 void
499 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
500                                  const struct GNUNET_DISK_FileHandle *h);
501
502
503 /**
504  * Check if a file handle is part of an fd set
505  *
506  * @param fds fd set
507  * @param h file handle
508  * @return #GNUNET_YES if the file handle is part of the set
509  */
510 int
511 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
512                                    const struct GNUNET_DISK_FileHandle *h);
513
514
515 /**
516  * Checks if two fd sets overlap
517  *
518  * @param fds1 first fd set
519  * @param fds2 second fd set
520  * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
521  */
522 int
523 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
524                               const struct GNUNET_NETWORK_FDSet *fds2);
525
526
527 /**
528  * Creates an fd set
529  *
530  * @return a new fd set
531  */
532 struct GNUNET_NETWORK_FDSet *
533 GNUNET_NETWORK_fdset_create (void);
534
535
536 /**
537  * Releases the associated memory of an fd set
538  *
539  * @param fds fd set
540  */
541 void
542 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
543
544
545 #if 0                           /* keep Emacsens' auto-indent happy */
546 {
547 #endif
548 #ifdef __cplusplus
549 }
550 #endif
551
552 #endif /* GNUNET_NETWORK_LIB_H */