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