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