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