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;
72 #include "gnunet_disk_lib.h"
73 #include "gnunet_time_lib.h"
77 * Accept a new connection on a socket. Configure it for non-blocking
78 * IO and mark it as non-inheritable to child processes (set the
79 * close-on-exec flag).
81 * @param desc bound socket
82 * @param address address of the connecting peer, may be NULL
83 * @param address_len length of address
84 * @return client socket
86 struct GNUNET_NETWORK_Handle *
87 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
88 struct sockaddr *address,
89 socklen_t * address_len);
93 * Box a native socket (and check that it is a socket).
95 * @param fd socket to box
96 * @return NULL on error (including not supported on target platform)
98 struct GNUNET_NETWORK_Handle *
99 GNUNET_NETWORK_socket_box_native (int fd);
103 * Bind to a connected socket
105 * @param desc socket to bind
106 * @param address address to be bound
107 * @param address_len length of address
108 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
111 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
112 const struct sockaddr *address,
113 socklen_t address_len);
118 * @param desc socket to close
119 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
122 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
128 * @param desc socket to connect
129 * @param address peer address
130 * @param address_len of address
131 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
134 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
135 const struct sockaddr *address,
136 socklen_t address_len);
142 * @param desc socket to inspect
143 * @param level protocol level of the option
144 * @param optname identifier of the option
145 * @param optval options
146 * @param optlen length of optval
147 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
150 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
151 int level, int optname, void *optval,
158 * @param desc socket to start listening on
159 * @param backlog length of the listen queue
160 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
163 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
168 * How much data is available to be read on this descriptor?
172 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
177 * Read data from a connected socket (always non-blocking).
179 * @param buffer buffer
180 * @param length length of buffer
181 * @param src_addr either the source to recv from, or all zeroes
182 * to be filled in by recvfrom
183 * @param addrlen length of the addr
186 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
187 void *buffer, size_t length,
188 struct sockaddr *src_addr, socklen_t * addrlen);
192 * Read data from a connected socket (always non-blocking).
195 * @param buffer buffer
196 * @param length length of buffer
197 * @return number of bytes read
200 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
201 void *buffer, size_t length);
205 * Check if sockets meet certain conditions
206 * @param rfds set of sockets to be checked for readability
207 * @param wfds set of sockets to be checked for writability
208 * @param efds set of sockets to be checked for exceptions
209 * @param timeout relative value when to return
210 * @return number of selected sockets, GNUNET_SYSERR on error
213 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
214 struct GNUNET_NETWORK_FDSet *wfds,
215 struct GNUNET_NETWORK_FDSet *efds,
216 struct GNUNET_TIME_Relative timeout);
220 * Send data (always non-blocking).
223 * @param buffer data to send
224 * @param length size of the buffer
225 * @return number of bytes sent, GNUNET_SYSERR on error
228 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
229 const void *buffer, size_t length);
233 * Send data to a particular destination (always non-blocking).
234 * This function only works for UDP sockets.
237 * @param message data to send
238 * @param length size of the data
239 * @param dest_addr destination address
240 * @param dest_len length of address
241 * @return number of bytes sent, GNUNET_SYSERR on error
244 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
245 const void *message, size_t length,
246 const struct sockaddr *dest_addr,
254 * @param level protocol level of the option
255 * @param option_name option identifier
256 * @param option_value value to set
257 * @param option_len size of option_value
258 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
261 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
262 int option_name, const void *option_value,
263 socklen_t option_len);
267 * Shut down socket operations
270 * @param how type of shutdown
271 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
274 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
278 * Disable the "CORK" feature for communication with the given socket,
279 * forcing the OS to immediately flush the buffer on transmission
280 * instead of potentially buffering multiple messages. Essentially
281 * reduces the OS send buffers to zero.
284 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
287 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
291 * Create a new socket. Configure it for non-blocking IO and
292 * mark it as non-inheritable to child processes (set the
293 * close-on-exec flag).
295 * @param domain domain of the socket
296 * @param type socket type
297 * @param protocol network protocol
298 * @return new socket, NULL on error
300 struct GNUNET_NETWORK_Handle *
301 GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
305 * Reset FD set (clears all file descriptors).
307 * @param fds fd set to clear
310 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
314 * Add a socket to the FD set
316 * @param desc socket to add
319 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
320 const struct GNUNET_NETWORK_Handle *desc);
324 /* TODO: maybe #ifdef WINDOWS? -ndurner */
326 * Add a W32 file handle to the fd set
328 * @param h the file handle to add
331 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet
337 * Check whether a socket is part of the fd set
340 * @return GNUNET_YES if the socket is in the set
343 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
344 const struct GNUNET_NETWORK_Handle *desc);
348 * Add one fd set to another
349 * @param dst the fd set to add to
350 * @param src the fd set to add from
353 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
354 const struct GNUNET_NETWORK_FDSet *src);
358 * Copy one fd set to another
359 * @param to destination
363 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
364 const struct GNUNET_NETWORK_FDSet *from);
368 * Return file descriptor for this network handle
370 * @param desc wrapper to process
371 * @return POSIX file descriptor
374 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
378 * Copy a native fd set
379 * @param to destination
380 * @param from native source set
381 * @param nfds the biggest socket number in from + 1
384 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
385 const fd_set * from, int nfds);
389 * Set a native fd in a set
391 * @param to destination
392 * @param nfd native FD to set
395 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd);
399 * Test native fd in a set
401 * @param to set to test, NULL for empty set
402 * @param nfd native FD to test, -1 for none
403 * @return GNUNET_YES if to contains nfd
406 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
411 * Add a file handle to the fd set
413 * @param h the file handle to add
416 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
417 const struct GNUNET_DISK_FileHandle *h);
421 * Check if a file handle is part of an fd set
423 * @param h file handle
424 * @return GNUNET_YES if the file handle is part of the set
427 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
428 const struct GNUNET_DISK_FileHandle *h);
432 * Checks if two fd sets overlap
433 * @param fds1 first fd set
434 * @param fds2 second fd set
435 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
438 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
439 const struct GNUNET_NETWORK_FDSet *fds2);
444 * @return a new fd set
446 struct GNUNET_NETWORK_FDSet *
447 GNUNET_NETWORK_fdset_create (void);
451 * Releases the associated memory of an fd set
455 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
458 #if 0 /* keep Emacsens' auto-indent happy */
465 #endif /* GNUNET_NETWORK_LIB_H */