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