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