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;
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 to a connected socket
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);
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
174 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
175 const struct sockaddr *address,
176 socklen_t address_len);
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
190 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
191 int level, int optname, void *optval,
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
203 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
208 * How much data is available to be read on this descriptor?
212 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
217 * Read data from a connected socket (always non-blocking).
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
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);
232 * Read data from a connected socket (always non-blocking).
235 * @param buffer buffer
236 * @param length length of buffer
237 * @return number of bytes read
240 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
241 void *buffer, size_t length);
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
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);
260 * Send data (always non-blocking).
263 * @param buffer data to send
264 * @param length size of the buffer
265 * @return number of bytes sent, GNUNET_SYSERR on error
268 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
269 const void *buffer, size_t length);
273 * Send data to a particular destination (always non-blocking).
274 * This function only works for UDP sockets.
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
284 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
285 const void *message, size_t length,
286 const struct sockaddr *dest_addr,
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
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);
307 * Shut down socket operations
310 * @param how type of shutdown
311 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
314 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
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.
324 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
327 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
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).
335 * @param domain domain of the socket
336 * @param type socket type
337 * @param protocol network protocol
338 * @return new socket, NULL on error
340 struct GNUNET_NETWORK_Handle *
341 GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
345 * Reset FD set (clears all file descriptors).
347 * @param fds fd set to clear
350 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
354 * Add a socket to the FD set
356 * @param desc socket to add
359 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
360 const struct GNUNET_NETWORK_Handle *desc);
365 * Add a W32 file handle to the fd set
367 * @param h the file handle to add
370 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet
376 * Check whether a socket is part of the fd set
379 * @return GNUNET_YES if the socket is in the set
382 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
383 const struct GNUNET_NETWORK_Handle *desc);
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
392 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
393 const struct GNUNET_NETWORK_FDSet *src);
397 * Copy one fd set to another
398 * @param to destination
402 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
403 const struct GNUNET_NETWORK_FDSet *from);
407 * Return file descriptor for this network handle
409 * @param desc wrapper to process
410 * @return POSIX file descriptor
413 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
417 * Return the sockaddr for this network handle
419 * @param desc wrapper to process
420 * @return POSIX file descriptor
423 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc);
427 * Return sockaddr length for this network handle
429 * @param desc wrapper to process
430 * @return socklen_t for sockaddr
433 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc);
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
443 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
444 const fd_set * from, int nfds);
448 * Set a native fd in a set
450 * @param to destination
451 * @param nfd native FD to set
454 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd);
458 * Test native fd in a set
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
465 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
470 * Add a file handle to the fd set
472 * @param h the file handle to add
475 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
476 const struct GNUNET_DISK_FileHandle *h);
480 * Check if a file handle is part of an fd set
482 * @param h file handle
483 * @return GNUNET_YES if the file handle is part of the set
486 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
487 const struct GNUNET_DISK_FileHandle *h);
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
497 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
498 const struct GNUNET_NETWORK_FDSet *fds2);
503 * @return a new fd set
505 struct GNUNET_NETWORK_FDSet *
506 GNUNET_NETWORK_fdset_create (void);
510 * Releases the associated memory of an fd set
514 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
517 #if 0 /* keep Emacsens' auto-indent happy */
524 #endif /* GNUNET_NETWORK_LIB_H */