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 (SOCKTYPE 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);
325 * Add a W32 file handle to the fd set
327 * @param h the file handle to add
330 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet
336 * Check whether a socket is part of the fd set
339 * @return GNUNET_YES if the socket is in the set
342 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
343 const struct GNUNET_NETWORK_Handle *desc);
347 * Add one fd set to another
348 * @param dst the fd set to add to
349 * @param src the fd set to add from
352 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
353 const struct GNUNET_NETWORK_FDSet *src);
357 * Copy one fd set to another
358 * @param to destination
362 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
363 const struct GNUNET_NETWORK_FDSet *from);
367 * Return file descriptor for this network handle
369 * @param desc wrapper to process
370 * @return POSIX file descriptor
373 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
377 * Return the sockaddr for this network handle
379 * @param desc wrapper to process
380 * @return POSIX file descriptor
383 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc);
387 * Return sockaddr length for this network handle
389 * @param desc wrapper to process
390 * @return socklen_t for sockaddr
393 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc);
397 * Copy a native fd set
398 * @param to destination
399 * @param from native source set
400 * @param nfds the biggest socket number in from + 1
403 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
404 const fd_set * from, int nfds);
408 * Set a native fd in a set
410 * @param to destination
411 * @param nfd native FD to set
414 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd);
418 * Test native fd in a set
420 * @param to set to test, NULL for empty set
421 * @param nfd native FD to test, -1 for none
422 * @return GNUNET_YES if to contains nfd
425 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
430 * Add a file handle to the fd set
432 * @param h the file handle to add
435 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
436 const struct GNUNET_DISK_FileHandle *h);
440 * Check if a file handle is part of an fd set
442 * @param h file handle
443 * @return GNUNET_YES if the file handle is part of the set
446 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
447 const struct GNUNET_DISK_FileHandle *h);
451 * Checks if two fd sets overlap
452 * @param fds1 first fd set
453 * @param fds2 second fd set
454 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
457 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
458 const struct GNUNET_NETWORK_FDSet *fds2);
463 * @return a new fd set
465 struct GNUNET_NETWORK_FDSet *
466 GNUNET_NETWORK_fdset_create (void);
470 * Releases the associated memory of an fd set
474 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
477 #if 0 /* keep Emacsens' auto-indent happy */
484 #endif /* GNUNET_NETWORK_LIB_H */