more docu
[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 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 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, int optname, void *optval,
192                                   socklen_t * optlen);
193
194
195 /**
196  * Listen on a socket
197  *
198  * @param desc socket to start listening on
199  * @param backlog length of the listen queue
200  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
201  */
202 int
203 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
204                               int backlog);
205
206
207 /**
208  * How much data is available to be read on this descriptor?
209  *
210  * @param desc socket
211  * @returns #GNUNET_NO if no data is available, or on error!
212  */
213 ssize_t
214 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
215                                        *desc);
216
217
218 /**
219  * Read data from a socket (always non-blocking).
220  *
221  * @param desc socket
222  * @param buffer buffer
223  * @param length length of buffer
224  * @param src_addr either the source to recv from, or all zeroes
225  *        to be filled in by recvfrom
226  * @param addrlen length of the addr
227  */
228 ssize_t
229 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
230                                 void *buffer, size_t length,
231                                 struct sockaddr *src_addr, socklen_t *addrlen);
232
233
234 /**
235  * Read data from a connected socket (always non-blocking).
236  *
237  * @param desc socket
238  * @param buffer buffer
239  * @param length length of buffer
240  * @return number of bytes read
241  */
242 ssize_t
243 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
244                             void *buffer, size_t length);
245
246
247 /**
248  * Check if sockets meet certain conditions
249  * @param rfds set of sockets to be checked for readability
250  * @param wfds set of sockets to be checked for writability
251  * @param efds set of sockets to be checked for exceptions
252  * @param timeout relative value when to return
253  * @return number of selected sockets, #GNUNET_SYSERR on error
254  */
255 int
256 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
257                               struct GNUNET_NETWORK_FDSet *wfds,
258                               struct GNUNET_NETWORK_FDSet *efds,
259                               struct GNUNET_TIME_Relative timeout);
260
261
262 /**
263  * Send data (always non-blocking).
264  *
265  * @param desc socket
266  * @param buffer data to send
267  * @param length size of the buffer
268  * @return number of bytes sent, #GNUNET_SYSERR on error
269  */
270 ssize_t
271 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
272                             const void *buffer, size_t length);
273
274
275 /**
276  * Send data to a particular destination (always non-blocking).
277  * This function only works for UDP sockets.
278  *
279  * @param desc socket
280  * @param message data to send
281  * @param length size of the data
282  * @param dest_addr destination address
283  * @param dest_len length of address
284  * @return number of bytes sent, #GNUNET_SYSERR on error
285  */
286 ssize_t
287 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
288                               const void *message, size_t length,
289                               const struct sockaddr *dest_addr,
290                               socklen_t dest_len);
291
292
293 /**
294  * Set socket option
295  *
296  * @param fd socket
297  * @param level protocol level of the option
298  * @param option_name option identifier
299  * @param option_value value to set
300  * @param option_len size of option_value
301  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
302  */
303 int
304 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
305                                   int option_name, const void *option_value,
306                                   socklen_t option_len);
307
308
309 /**
310  * Shut down socket operations
311  *
312  * @param desc socket
313  * @param how type of shutdown
314  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
315  */
316 int
317 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
318
319
320 /**
321  * Disable the "CORK" feature for communication with the given socket,
322  * forcing the OS to immediately flush the buffer on transmission
323  * instead of potentially buffering multiple messages.  Essentially
324  * reduces the OS send buffers to zero.
325  *
326  * @param desc socket
327  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
328  */
329 int
330 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
331
332
333 /**
334  * Create a new socket.   Configure it for non-blocking IO and
335  * mark it as non-inheritable to child processes (set the
336  * close-on-exec flag).
337  *
338  * @param domain domain of the socket
339  * @param type socket type
340  * @param protocol network protocol
341  * @return new socket, NULL on error
342  */
343 struct GNUNET_NETWORK_Handle *
344 GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
345
346
347 /**
348  * Reset FD set (clears all file descriptors).
349  *
350  * @param fds fd set to clear
351  */
352 void
353 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
354
355
356 /**
357  * Add a socket to the FD set
358  *
359  * @param fds fd set
360  * @param desc socket to add
361  */
362 void
363 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
364                           const struct GNUNET_NETWORK_Handle *desc);
365
366
367 #if WINDOWS
368 /**
369  * Add a W32 file handle to the fd set
370  *
371  * @param fds fd set
372  * @param h the file handle to add
373  */
374 void
375 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet
376                                                    *fds, HANDLE h);
377 #endif
378
379
380 /**
381  * Check whether a socket is part of the fd set
382  *
383  * @param fds fd set
384  * @param desc socket
385  * @return #GNUNET_YES if the socket is in the set
386  */
387 int
388 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
389                             const struct GNUNET_NETWORK_Handle *desc);
390
391
392 /**
393  * Add one fd set to another (computes the union).
394  *
395  * @param dst the fd set to add to
396  * @param src the fd set to add from
397  */
398 void
399 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
400                           const struct GNUNET_NETWORK_FDSet *src);
401
402
403 /**
404  * Copy one fd set to another
405  *
406  * @param to destination
407  * @param from source
408  */
409 void
410 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
411                            const struct GNUNET_NETWORK_FDSet *from);
412
413
414 /**
415  * Return file descriptor for this network handle
416  *
417  * @param desc wrapper to process
418  * @return POSIX file descriptor
419  */
420 int
421 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
422
423
424 /**
425  * Return the sockaddr for this network handle
426  *
427  * @param desc wrapper to process
428  * @return POSIX file descriptor
429  */
430 struct sockaddr*
431 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc);
432
433
434 /**
435  * Return sockaddr length for this network handle
436  *
437  * @param desc wrapper to process
438  * @return socklen_t for sockaddr
439  */
440 socklen_t
441 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc);
442
443
444 /**
445  * Copy a native fd set into the GNUnet representation.
446  *
447  * @param to destination
448  * @param from native source set
449  * @param nfds the biggest socket number in from + 1
450  */
451 void
452 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
453                                   const fd_set *from, int nfds);
454
455
456 /**
457  * Set a native fd in a set
458  *
459  * @param to destination
460  * @param nfd native FD to set
461  */
462 void
463 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd);
464
465
466 /**
467  * Test native fd in a set
468  *
469  * @param to set to test, NULL for empty set
470  * @param nfd native FD to test, -1 for none
471  * @return GNUNET_YES if to contains nfd
472  */
473 int
474 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
475                                   int nfd);
476
477
478 /**
479  * Add a file handle to the fd set
480  *
481  * @param fds fd set
482  * @param h the file handle to add
483  */
484 void
485 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
486                                  const struct GNUNET_DISK_FileHandle *h);
487
488
489 /**
490  * Check if a file handle is part of an fd set
491  * @param fds fd set
492  * @param h file handle
493  * @return #GNUNET_YES if the file handle is part of the set
494  */
495 int
496 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
497                                    const struct GNUNET_DISK_FileHandle *h);
498
499
500 /**
501  * Checks if two fd sets overlap
502  *
503  * @param fds1 first fd set
504  * @param fds2 second fd set
505  * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
506  */
507 int
508 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
509                               const struct GNUNET_NETWORK_FDSet *fds2);
510
511
512 /**
513  * Creates an fd set
514  *
515  * @return a new fd set
516  */
517 struct GNUNET_NETWORK_FDSet *
518 GNUNET_NETWORK_fdset_create (void);
519
520
521 /**
522  * Releases the associated memory of an fd set
523  *
524  * @param fds fd set
525  */
526 void
527 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
528
529
530 #if 0                           /* keep Emacsens' auto-indent happy */
531 {
532 #endif
533 #ifdef __cplusplus
534 }
535 #endif
536
537 #endif /* GNUNET_NETWORK_LIB_H */