2 This file is part of GNUnet.
3 (C) 2009-2013 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 3, 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;
70 #include "gnunet_disk_lib.h"
71 #include "gnunet_time_lib.h"
74 * Test if the given protocol family is supported by this system.
76 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
77 * @return #GNUNET_OK if the PF is supported
80 GNUNET_NETWORK_test_pf (int pf);
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).
88 * @param unixpath long path, will be freed (or same pointer returned
89 * with moved 0-termination).
90 * @return shortened unixpath, NULL on error
93 GNUNET_NETWORK_shorten_unixpath (char *unixpath);
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).
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
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);
113 * Box a native socket (and check that it is a socket).
115 * @param fd socket to box
116 * @return NULL on error (including not supported on target platform)
118 struct GNUNET_NETWORK_Handle *
119 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd);
123 * Set if a socket should use blocking or non-blocking IO.
126 * @param doBlock blocking mode
127 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
130 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
135 * Fail to bind if an address is already in use.
137 #define GNUNET_BIND_EXCLUSIVE 0x01
141 * Bind a socket to a particular address.
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
150 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
151 const struct sockaddr *address,
152 socklen_t address_len,
158 * @param desc socket to close
159 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
162 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
166 * Only free memory of a socket, keep the file descriptor untouched.
171 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc);
175 * Connect a socket to some remote address.
177 * @param desc socket to connect
178 * @param address peer address
179 * @param address_len of address
180 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
183 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
184 const struct sockaddr *address,
185 socklen_t address_len);
191 * @param desc socket to inspect
192 * @param level protocol level of the option
193 * @param optname identifier of the option
194 * @param optval options
195 * @param optlen length of optval
196 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
199 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
200 int level, int optname, void *optval,
207 * @param desc socket to start listening on
208 * @param backlog length of the listen queue
209 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
212 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
217 * How much data is available to be read on this descriptor?
220 * @returns #GNUNET_NO if no data is available, or on error!
223 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
228 * Read data from a socket (always non-blocking).
231 * @param buffer buffer
232 * @param length length of buffer
233 * @param src_addr either the source to recv from, or all zeroes
234 * to be filled in by recvfrom
235 * @param addrlen length of the addr
238 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
239 void *buffer, size_t length,
240 struct sockaddr *src_addr, socklen_t *addrlen);
244 * Read data from a connected socket (always non-blocking).
247 * @param buffer buffer
248 * @param length length of buffer
249 * @return number of bytes read
252 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
253 void *buffer, size_t length);
257 * Check if sockets meet certain conditions
258 * @param rfds set of sockets to be checked for readability
259 * @param wfds set of sockets to be checked for writability
260 * @param efds set of sockets to be checked for exceptions
261 * @param timeout relative value when to return
262 * @return number of selected sockets, #GNUNET_SYSERR on error
265 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
266 struct GNUNET_NETWORK_FDSet *wfds,
267 struct GNUNET_NETWORK_FDSet *efds,
268 struct GNUNET_TIME_Relative timeout);
272 * Send data (always non-blocking).
275 * @param buffer data to send
276 * @param length size of the buffer
277 * @return number of bytes sent, #GNUNET_SYSERR on error
280 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
281 const void *buffer, size_t length);
285 * Send data to a particular destination (always non-blocking).
286 * This function only works for UDP sockets.
289 * @param message data to send
290 * @param length size of the data
291 * @param dest_addr destination address
292 * @param dest_len length of address
293 * @return number of bytes sent, #GNUNET_SYSERR on error
296 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
297 const void *message, size_t length,
298 const struct sockaddr *dest_addr,
306 * @param level protocol level of the option
307 * @param option_name option identifier
308 * @param option_value value to set
309 * @param option_len size of option_value
310 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
313 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
314 int option_name, const void *option_value,
315 socklen_t option_len);
319 * Shut down socket operations
322 * @param how type of shutdown
323 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
326 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
330 * Disable the "CORK" feature for communication with the given socket,
331 * forcing the OS to immediately flush the buffer on transmission
332 * instead of potentially buffering multiple messages. Essentially
333 * reduces the OS send buffers to zero.
336 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
339 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
343 * Create a new socket. Configure it for non-blocking IO and
344 * mark it as non-inheritable to child processes (set the
345 * close-on-exec flag).
347 * @param domain domain of the socket
348 * @param type socket type
349 * @param protocol network protocol
350 * @return new socket, NULL on error
352 struct GNUNET_NETWORK_Handle *
353 GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
357 * Reset FD set (clears all file descriptors).
359 * @param fds fd set to clear
362 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
366 * Add a socket to the FD set
369 * @param desc socket to add
372 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
373 const struct GNUNET_NETWORK_Handle *desc);
378 * Add a W32 file handle to the fd set
381 * @param h the file handle to add
384 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet
390 * Check whether a socket is part of the fd set
394 * @return #GNUNET_YES if the socket is in the set
397 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
398 const struct GNUNET_NETWORK_Handle *desc);
402 * Add one fd set to another (computes the union).
404 * @param dst the fd set to add to
405 * @param src the fd set to add from
408 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
409 const struct GNUNET_NETWORK_FDSet *src);
413 * Copy one fd set to another
415 * @param to destination
419 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
420 const struct GNUNET_NETWORK_FDSet *from);
424 * Return file descriptor for this network handle
426 * @param desc wrapper to process
427 * @return POSIX file descriptor
430 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
434 * Return the sockaddr for this network handle
436 * @param desc wrapper to process
437 * @return POSIX file descriptor
440 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc);
444 * Return sockaddr length for this network handle
446 * @param desc wrapper to process
447 * @return socklen_t for sockaddr
450 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc);
454 * Copy a native fd set into the GNUnet representation.
456 * @param to destination
457 * @param from native source set
458 * @param nfds the biggest socket number in from + 1
461 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
462 const fd_set *from, int nfds);
466 * Set a native fd in a set
468 * @param to destination
469 * @param nfd native FD to set
472 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd);
476 * Test native fd in a set
478 * @param to set to test, NULL for empty set
479 * @param nfd native FD to test, -1 for none
480 * @return GNUNET_YES if to contains nfd
483 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
488 * Add a file handle to the fd set
491 * @param h the file handle to add
494 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
495 const struct GNUNET_DISK_FileHandle *h);
499 * Check if a file handle is part of an fd set
501 * @param h file handle
502 * @return #GNUNET_YES if the file handle is part of the set
505 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
506 const struct GNUNET_DISK_FileHandle *h);
510 * Checks if two fd sets overlap
512 * @param fds1 first fd set
513 * @param fds2 second fd set
514 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
517 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
518 const struct GNUNET_NETWORK_FDSet *fds2);
524 * @return a new fd set
526 struct GNUNET_NETWORK_FDSet *
527 GNUNET_NETWORK_fdset_create (void);
531 * Releases the associated memory of an fd set
536 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
539 #if 0 /* keep Emacsens' auto-indent happy */
546 #endif /* GNUNET_NETWORK_LIB_H */