2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * Basic low-level networking interface
25 * @defgroup network Network library
26 * Basic low-level networking interface
29 #ifndef GNUNET_NETWORK_LIB_H
30 #define GNUNET_NETWORK_LIB_H
35 #if 0 /* keep Emacsens' auto-indent happy */
42 * @brief handle to a socket
44 struct GNUNET_NETWORK_Handle;
48 * @brief collection of IO descriptors
50 struct GNUNET_NETWORK_FDSet
54 * Maximum number of any socket descriptor in the set (plus one)
59 * Bitset with the descriptors.
65 * Array of file handles (from pipes) that are also in
66 * the FDSet. Needed as those cannot go into @e sds
69 const struct GNUNET_DISK_FileHandle **handles;
72 * Size of the @e handles array
74 unsigned int handles_size;
77 * Number of @e handles slots in use. Always
78 * smaller than @e handles_size.
80 unsigned int handles_pos;
86 #include "gnunet_disk_lib.h"
87 #include "gnunet_time_lib.h"
90 * Test if the given protocol family is supported by this system.
92 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
93 * @return #GNUNET_OK if the PF is supported
96 GNUNET_NETWORK_test_pf (int pf);
100 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
101 * shorten it to an acceptable length while keeping it unique
102 * and making sure it remains a valid filename (if possible).
104 * @param unixpath long path, will be freed (or same pointer returned
105 * with moved 0-termination).
106 * @return shortened unixpath, NULL on error
109 GNUNET_NETWORK_shorten_unixpath (char *unixpath);
114 * If services crash, they can leave a unix domain socket file on the
115 * disk. This needs to be manually removed, because otherwise both
116 * bind() and connect() for the respective address will fail. In this
117 * function, we test if such a left-over file exists, and if so,
118 * remove it (unless there is a listening service at the address).
120 * @param un unix domain socket address to check
123 GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un);
128 * Accept a new connection on a socket. Configure it for non-blocking
129 * IO and mark it as non-inheritable to child processes (set the
130 * close-on-exec flag).
132 * @param desc bound socket
133 * @param address address of the connecting peer, may be NULL
134 * @param address_len length of address
135 * @return client socket
137 struct GNUNET_NETWORK_Handle *
138 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
139 struct sockaddr *address,
140 socklen_t *address_len);
144 * Box a native socket (and check that it is a socket).
146 * @param fd socket to box
147 * @return NULL on error (including not supported on target platform)
149 struct GNUNET_NETWORK_Handle *
150 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd);
154 * Set if a socket should use blocking or non-blocking IO.
157 * @param doBlock blocking mode
158 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
161 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
166 * Bind a socket to a particular address.
168 * @param desc socket to bind
169 * @param address address to be bound
170 * @param address_len length of @a address
171 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
174 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
175 const struct sockaddr *address,
176 socklen_t address_len);
181 * @param desc socket to close
182 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
185 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
189 * Only free memory of a socket, keep the file descriptor untouched.
194 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc);
198 * Connect a socket to some remote address.
200 * @param desc socket to connect
201 * @param address peer address
202 * @param address_len of @a address
203 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
206 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
207 const struct sockaddr *address,
208 socklen_t address_len);
214 * @param desc socket to inspect
215 * @param level protocol level of the option
216 * @param optname identifier of the option
217 * @param optval options
218 * @param optlen length of optval
219 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
222 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
232 * @param desc socket to start listening on
233 * @param backlog length of the listen queue
234 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
237 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
242 * How much data is available to be read on this descriptor?
245 * @returns #GNUNET_SYSERR if no data is available, or on error!
248 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc);
252 * Read data from a socket (always non-blocking).
255 * @param buffer buffer
256 * @param length length of buffer
257 * @param src_addr either the source to recv from, or all zeroes
258 * to be filled in by recvfrom
259 * @param addrlen length of the addr
262 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
265 struct sockaddr *src_addr,
270 * Read data from a connected socket (always non-blocking).
273 * @param buffer buffer
274 * @param length length of buffer
275 * @return number of bytes read
278 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
284 * Check if sockets meet certain conditions.
286 * @param rfds set of sockets to be checked for readability
287 * @param wfds set of sockets to be checked for writability
288 * @param efds set of sockets to be checked for exceptions
289 * @param timeout relative value when to return
290 * @return number of selected sockets, #GNUNET_SYSERR on error
293 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
294 struct GNUNET_NETWORK_FDSet *wfds,
295 struct GNUNET_NETWORK_FDSet *efds,
296 struct GNUNET_TIME_Relative timeout);
300 * Send data (always non-blocking).
303 * @param buffer data to send
304 * @param length size of the buffer
305 * @return number of bytes sent, #GNUNET_SYSERR on error
308 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
314 * Send data to a particular destination (always non-blocking).
315 * This function only works for UDP sockets.
318 * @param message data to send
319 * @param length size of the data in @a message
320 * @param dest_addr destination address
321 * @param dest_len length of @a dest_addr
322 * @return number of bytes sent, #GNUNET_SYSERR on error
325 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
328 const struct sockaddr *dest_addr,
336 * @param level protocol level of the option
337 * @param option_name option identifier
338 * @param option_value value to set
339 * @param option_len size of @a option_value
340 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
343 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
346 const void *option_value,
347 socklen_t option_len);
351 * Shut down socket operations
354 * @param how type of shutdown
355 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
358 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
363 * Disable the "CORK" feature for communication with the given socket,
364 * forcing the OS to immediately flush the buffer on transmission
365 * instead of potentially buffering multiple messages. Essentially
366 * reduces the OS send buffers to zero.
369 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
372 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
376 * Create a new socket. Configure it for non-blocking IO and
377 * mark it as non-inheritable to child processes (set the
378 * close-on-exec flag).
380 * @param domain domain of the socket
381 * @param type socket type
382 * @param protocol network protocol
383 * @return new socket, NULL on error
385 struct GNUNET_NETWORK_Handle *
386 GNUNET_NETWORK_socket_create (int domain,
392 * Reset FD set (clears all file descriptors).
394 * @param fds fd set to clear
397 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
401 * Add a socket to the FD set
404 * @param desc socket to add
407 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
408 const struct GNUNET_NETWORK_Handle *desc);
413 * Add a W32 file handle to the fd set
416 * @param h the file handle to add
419 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet *fds,
425 * Check whether a socket is part of the fd set
429 * @return #GNUNET_YES if the socket is in the set
432 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
433 const struct GNUNET_NETWORK_Handle *desc);
437 * Add one fd set to another (computes the union).
439 * @param dst the fd set to add to
440 * @param src the fd set to add from
443 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
444 const struct GNUNET_NETWORK_FDSet *src);
448 * Copy one fd set to another
450 * @param to destination
454 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
455 const struct GNUNET_NETWORK_FDSet *from);
459 * Return file descriptor for this network handle
461 * @param desc wrapper to process
462 * @return POSIX file descriptor
465 GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc);
469 * Return the sockaddr for this network handle
471 * @param desc wrapper to process
472 * @return POSIX file descriptor
475 GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc);
479 * Return sockaddr length for this network handle
481 * @param desc wrapper to process
482 * @return socklen_t for sockaddr
485 GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc);
489 * Copy a native fd set into the GNUnet representation.
491 * @param to destination
492 * @param from native source set
493 * @param nfds the biggest socket number in from + 1
496 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
502 * Set a native fd in a set
504 * @param to destination
505 * @param nfd native FD to set
508 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
513 * Test native fd in a set
515 * @param to set to test, NULL for empty set
516 * @param nfd native FD to test, -1 for none
517 * @return #GNUNET_YES if to contains nfd
520 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
525 * Add a file handle to the fd set
528 * @param h the file handle to add
531 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
532 const struct GNUNET_DISK_FileHandle *h);
536 * Add a file handle to the fd set
537 * On W32: ensure that the handle is first in the array.
540 * @param h the file handle to add
543 GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
544 const struct GNUNET_DISK_FileHandle *h);
548 * Check if a file handle is part of an fd set
551 * @param h file handle
552 * @return #GNUNET_YES if the file handle is part of the set
555 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
556 const struct GNUNET_DISK_FileHandle *h);
560 * Checks if two fd sets overlap
562 * @param fds1 first fd set
563 * @param fds2 second fd set
564 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
567 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
568 const struct GNUNET_NETWORK_FDSet *fds2);
574 * @return a new fd set
576 struct GNUNET_NETWORK_FDSet *
577 GNUNET_NETWORK_fdset_create (void);
581 * Releases the associated memory of an fd set
586 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
590 * Test if the given @a port is available.
592 * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
593 * @param port port number to test
594 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
597 GNUNET_NETWORK_test_port_free (int ipproto,
601 #if 0 /* keep Emacsens' auto-indent happy */
608 #endif /* GNUNET_NETWORK_LIB_H */
610 /** @} */ /* end of group */