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