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