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
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 socket descriptor in the set (plus one)
59 * Bitset with the descriptors.
65 * Linked list of handles
67 struct GNUNET_CONTAINER_SList *handles;
72 #include "gnunet_disk_lib.h"
73 #include "gnunet_time_lib.h"
76 * Test if the given protocol family is supported by this system.
78 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
79 * @return #GNUNET_OK if the PF is supported
82 GNUNET_NETWORK_test_pf (int pf);
86 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
87 * shorten it to an acceptable length while keeping it unique
88 * and making sure it remains a valid filename (if possible).
90 * @param unixpath long path, will be freed (or same pointer returned
91 * with moved 0-termination).
92 * @return shortened unixpath, NULL on error
95 GNUNET_NETWORK_shorten_unixpath (char *unixpath);
99 * Accept a new connection on a socket. Configure it for non-blocking
100 * IO and mark it as non-inheritable to child processes (set the
101 * close-on-exec flag).
103 * @param desc bound socket
104 * @param address address of the connecting peer, may be NULL
105 * @param address_len length of address
106 * @return client socket
108 struct GNUNET_NETWORK_Handle *
109 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
110 struct sockaddr *address,
111 socklen_t *address_len);
115 * Box a native socket (and check that it is a socket).
117 * @param fd socket to box
118 * @return NULL on error (including not supported on target platform)
120 struct GNUNET_NETWORK_Handle *
121 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd);
125 * Set if a socket should use blocking or non-blocking IO.
128 * @param doBlock blocking mode
129 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
132 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
137 * Fail to bind if an address is already in use.
139 #define GNUNET_BIND_EXCLUSIVE 0x01
143 * Bind a socket to a particular address.
145 * @param desc socket to bind
146 * @param address address to be bound
147 * @param address_len length of address
148 * @param flags flags affecting bind behaviour
149 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
152 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
153 const struct sockaddr *address,
154 socklen_t address_len,
160 * @param desc socket to close
161 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
164 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
168 * Only free memory of a socket, keep the file descriptor untouched.
173 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc);
177 * Connect a socket to some remote address.
179 * @param desc socket to connect
180 * @param address peer address
181 * @param address_len of address
182 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
185 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
186 const struct sockaddr *address,
187 socklen_t address_len);
193 * @param desc socket to inspect
194 * @param level protocol level of the option
195 * @param optname identifier of the option
196 * @param optval options
197 * @param optlen length of optval
198 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
201 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
202 int level, int optname, void *optval,
209 * @param desc socket to start listening on
210 * @param backlog length of the listen queue
211 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
214 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
219 * How much data is available to be read on this descriptor?
222 * @returns #GNUNET_NO if no data is available, or on error!
225 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
230 * Read data from a socket (always non-blocking).
233 * @param buffer buffer
234 * @param length length of buffer
235 * @param src_addr either the source to recv from, or all zeroes
236 * to be filled in by recvfrom
237 * @param addrlen length of the addr
240 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
241 void *buffer, size_t length,
242 struct sockaddr *src_addr, socklen_t *addrlen);
246 * Read data from a connected socket (always non-blocking).
249 * @param buffer buffer
250 * @param length length of buffer
251 * @return number of bytes read
254 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
255 void *buffer, size_t length);
259 * Check if sockets meet certain conditions
260 * @param rfds set of sockets to be checked for readability
261 * @param wfds set of sockets to be checked for writability
262 * @param efds set of sockets to be checked for exceptions
263 * @param timeout relative value when to return
264 * @return number of selected sockets, #GNUNET_SYSERR on error
267 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
268 struct GNUNET_NETWORK_FDSet *wfds,
269 struct GNUNET_NETWORK_FDSet *efds,
270 struct GNUNET_TIME_Relative timeout);
274 * Send data (always non-blocking).
277 * @param buffer data to send
278 * @param length size of the buffer
279 * @return number of bytes sent, #GNUNET_SYSERR on error
282 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
283 const void *buffer, size_t length);
287 * Send data to a particular destination (always non-blocking).
288 * This function only works for UDP sockets.
291 * @param message data to send
292 * @param length size of the data
293 * @param dest_addr destination address
294 * @param dest_len length of address
295 * @return number of bytes sent, #GNUNET_SYSERR on error
298 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
299 const void *message, size_t length,
300 const struct sockaddr *dest_addr,
308 * @param level protocol level of the option
309 * @param option_name option identifier
310 * @param option_value value to set
311 * @param option_len size of option_value
312 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
315 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
316 int option_name, const void *option_value,
317 socklen_t option_len);
321 * Shut down socket operations
324 * @param how type of shutdown
325 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
328 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
332 * Disable the "CORK" feature for communication with the given socket,
333 * forcing the OS to immediately flush the buffer on transmission
334 * instead of potentially buffering multiple messages. Essentially
335 * reduces the OS send buffers to zero.
338 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
341 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
345 * Create a new socket. Configure it for non-blocking IO and
346 * mark it as non-inheritable to child processes (set the
347 * close-on-exec flag).
349 * @param domain domain of the socket
350 * @param type socket type
351 * @param protocol network protocol
352 * @return new socket, NULL on error
354 struct GNUNET_NETWORK_Handle *
355 GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
359 * Reset FD set (clears all file descriptors).
361 * @param fds fd set to clear
364 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
368 * Add a socket to the FD set
371 * @param desc socket to add
374 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
375 const struct GNUNET_NETWORK_Handle *desc);
380 * Add a W32 file handle to the fd set
383 * @param h the file handle to add
386 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet
392 * Check whether a socket is part of the fd set
396 * @return #GNUNET_YES if the socket is in the set
399 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
400 const struct GNUNET_NETWORK_Handle *desc);
404 * Add one fd set to another (computes the union).
406 * @param dst the fd set to add to
407 * @param src the fd set to add from
410 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
411 const struct GNUNET_NETWORK_FDSet *src);
415 * Copy one fd set to another
417 * @param to destination
421 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
422 const struct GNUNET_NETWORK_FDSet *from);
426 * Return file descriptor for this network handle
428 * @param desc wrapper to process
429 * @return POSIX file descriptor
432 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
436 * Return the sockaddr for this network handle
438 * @param desc wrapper to process
439 * @return POSIX file descriptor
442 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc);
446 * Return sockaddr length for this network handle
448 * @param desc wrapper to process
449 * @return socklen_t for sockaddr
452 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc);
456 * Copy a native fd set into the GNUnet representation.
458 * @param to destination
459 * @param from native source set
460 * @param nfds the biggest socket number in from + 1
463 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
464 const fd_set *from, int nfds);
468 * Set a native fd in a set
470 * @param to destination
471 * @param nfd native FD to set
474 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd);
478 * Test native fd in a set
480 * @param to set to test, NULL for empty set
481 * @param nfd native FD to test, -1 for none
482 * @return GNUNET_YES if to contains nfd
485 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
490 * Add a file handle to the fd set
493 * @param h the file handle to add
496 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
497 const struct GNUNET_DISK_FileHandle *h);
501 * Check if a file handle is part of an fd set
503 * @param h file handle
504 * @return #GNUNET_YES if the file handle is part of the set
507 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
508 const struct GNUNET_DISK_FileHandle *h);
512 * Checks if two fd sets overlap
514 * @param fds1 first fd set
515 * @param fds2 second fd set
516 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
519 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
520 const struct GNUNET_NETWORK_FDSet *fds2);
526 * @return a new fd set
528 struct GNUNET_NETWORK_FDSet *
529 GNUNET_NETWORK_fdset_create (void);
533 * Releases the associated memory of an fd set
538 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
541 #if 0 /* keep Emacsens' auto-indent happy */
548 #endif /* GNUNET_NETWORK_LIB_H */