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