2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_network_lib.h
23 * @brief basic low-level networking interface
27 #ifndef GNUNET_NETWORK_LIB_H
28 #define GNUNET_NETWORK_LIB_H
33 #if 0 /* keep Emacsens' auto-indent happy */
40 * @brief handle to a socket
42 struct GNUNET_NETWORK_Handle;
46 * @brief collection of IO descriptors
48 struct GNUNET_NETWORK_FDSet
52 * Maximum number of any socket socket descriptor in the set (plus one)
57 * Bitset with the descriptors.
63 * Linked list of handles
65 struct GNUNET_CONTAINER_SList *handles;
71 #include "gnunet_disk_lib.h"
72 #include "gnunet_time_lib.h"
75 * Test if the given protocol family is supported by this system.
77 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
78 * @return GNUNET_OK if the PF is supported
81 GNUNET_NETWORK_test_pf (int pf);
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).
89 * @param unixpath long path, will be freed (or same pointer returned
90 * with moved 0-termination).
91 * @return shortened unixpath, NULL on error
94 GNUNET_NETWORK_shorten_unixpath (char *unixpath);
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).
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
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);
114 * Box a native socket (and check that it is a socket).
116 * @param fd socket to box
117 * @return NULL on error (including not supported on target platform)
119 struct GNUNET_NETWORK_Handle *
120 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd);
124 * Set if a socket should use blocking or non-blocking IO.
127 * @param doBlock blocking mode
128 * @return GNUNET_OK on success, GNUNET_SYSERR on error
131 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
136 * Fail to bind if an address is already in use.
138 #define GNUNET_BIND_EXCLUSIVE 0x01
142 * Bind to a connected socket
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
151 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
152 const struct sockaddr *address,
153 socklen_t address_len,
159 * @param desc socket to close
160 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
163 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
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
175 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
176 const struct sockaddr *address,
177 socklen_t address_len);
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
191 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
192 int level, int optname, void *optval,
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
204 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
209 * How much data is available to be read on this descriptor?
213 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
218 * Read data from a connected socket (always non-blocking).
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
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);
233 * Read data from a connected socket (always non-blocking).
236 * @param buffer buffer
237 * @param length length of buffer
238 * @return number of bytes read
241 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
242 void *buffer, size_t length);
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
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);
261 * Send data (always non-blocking).
264 * @param buffer data to send
265 * @param length size of the buffer
266 * @return number of bytes sent, GNUNET_SYSERR on error
269 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
270 const void *buffer, size_t length);
274 * Send data to a particular destination (always non-blocking).
275 * This function only works for UDP sockets.
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
285 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
286 const void *message, size_t length,
287 const struct sockaddr *dest_addr,
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
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);
308 * Shut down socket operations
311 * @param how type of shutdown
312 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
315 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
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.
325 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
328 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
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).
336 * @param domain domain of the socket
337 * @param type socket type
338 * @param protocol network protocol
339 * @return new socket, NULL on error
341 struct GNUNET_NETWORK_Handle *
342 GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
346 * Reset FD set (clears all file descriptors).
348 * @param fds fd set to clear
351 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
355 * Add a socket to the FD set
357 * @param desc socket to add
360 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
361 const struct GNUNET_NETWORK_Handle *desc);
366 * Add a W32 file handle to the fd set
368 * @param h the file handle to add
371 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet
377 * Check whether a socket is part of the fd set
380 * @return GNUNET_YES if the socket is in the set
383 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
384 const struct GNUNET_NETWORK_Handle *desc);
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
393 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
394 const struct GNUNET_NETWORK_FDSet *src);
398 * Copy one fd set to another
399 * @param to destination
403 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
404 const struct GNUNET_NETWORK_FDSet *from);
408 * Return file descriptor for this network handle
410 * @param desc wrapper to process
411 * @return POSIX file descriptor
414 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
418 * Return the sockaddr for this network handle
420 * @param desc wrapper to process
421 * @return POSIX file descriptor
424 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc);
428 * Return sockaddr length for this network handle
430 * @param desc wrapper to process
431 * @return socklen_t for sockaddr
434 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc);
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
444 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
445 const fd_set * from, int nfds);
449 * Set a native fd in a set
451 * @param to destination
452 * @param nfd native FD to set
455 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd);
459 * Test native fd in a set
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
466 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
471 * Add a file handle to the fd set
473 * @param h the file handle to add
476 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
477 const struct GNUNET_DISK_FileHandle *h);
481 * Check if a file handle is part of an fd set
483 * @param h file handle
484 * @return GNUNET_YES if the file handle is part of the set
487 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
488 const struct GNUNET_DISK_FileHandle *h);
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
498 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
499 const struct GNUNET_NETWORK_FDSet *fds2);
504 * @return a new fd set
506 struct GNUNET_NETWORK_FDSet *
507 GNUNET_NETWORK_fdset_create (void);
511 * Releases the associated memory of an fd set
515 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
518 #if 0 /* keep Emacsens' auto-indent happy */
525 #endif /* GNUNET_NETWORK_LIB_H */