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 */
41 * @brief handle to a socket
43 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 *GNUNET_NETWORK_socket_accept (const struct
96 * Box a native socket (and check that it is a socket).
98 * @param fd socket to box
99 * @return NULL on error (including not supported on target platform)
101 struct GNUNET_NETWORK_Handle *
102 GNUNET_NETWORK_socket_box_native (int fd);
106 * Bind to a connected socket
108 * @param desc socket to bind
109 * @param address address to be bound
110 * @param address_len length of address
111 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
113 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
114 const struct sockaddr *address,
115 socklen_t address_len);
120 * @param desc socket to close
121 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
123 int 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
133 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
134 const struct sockaddr *address,
135 socklen_t address_len);
141 * @param desc socket to inspect
142 * @param level protocol level of the option
143 * @param optname identifier of the option
144 * @param optval options
145 * @param optlen length of optval
146 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
148 int GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle
149 *desc, int level, int optname,
150 void *optval, socklen_t * optlen);
156 * @param desc socket to start listening on
157 * @param backlog length of the listen queue
158 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
160 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
164 * How much data is available to be read on this descriptor?
168 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
172 * Read data from a connected socket (always non-blocking).
174 * @param buffer buffer
175 * @param length length of buffer
176 * @param src_addr either the source to recv from, or all zeroes
177 * to be filled in by recvfrom
178 * @param addrlen length of the addr
181 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
182 void *buffer, size_t length,
183 struct sockaddr *src_addr,
184 socklen_t * addrlen);
187 * Read data from a connected socket (always non-blocking).
190 * @param buffer buffer
191 * @param length length of buffer
192 * @return number of bytes read
194 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
195 void *buffer, size_t length);
198 * Check if sockets meet certain conditions
199 * @param rfds set of sockets to be checked for readability
200 * @param wfds set of sockets to be checked for writability
201 * @param efds set of sockets to be checked for exceptions
202 * @param timeout relative value when to return
203 * @return number of selected sockets, GNUNET_SYSERR on error
205 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
206 struct GNUNET_NETWORK_FDSet *wfds,
207 struct GNUNET_NETWORK_FDSet *efds,
208 struct GNUNET_TIME_Relative timeout);
213 * Send data (always non-blocking).
216 * @param buffer data to send
217 * @param length size of the buffer
218 * @return number of bytes sent, GNUNET_SYSERR on error
220 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
221 const void *buffer, size_t length);
225 * Send data to a particular destination (always non-blocking).
226 * This function only works for UDP sockets.
229 * @param message data to send
230 * @param length size of the data
231 * @param dest_addr destination address
232 * @param dest_len length of address
233 * @return number of bytes sent, GNUNET_SYSERR on error
235 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle
236 *desc, const void *message,
238 const struct sockaddr *dest_addr,
244 * @param level protocol level of the option
245 * @param option_name option identifier
246 * @param option_value value to set
247 * @param option_len size of option_value
248 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
250 int GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
251 int level, int option_name,
252 const void *option_value,
253 socklen_t option_len);
256 * Shut down socket operations
258 * @param how type of shutdown
259 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
261 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
265 * Disable the "CORK" feature for communication with the given socket,
266 * forcing the OS to immediately flush the buffer on transmission
267 * instead of potentially buffering multiple messages. Essentially
268 * reduces the OS send buffers to zero.
271 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
273 int GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
277 * Create a new socket. Configure it for non-blocking IO and
278 * mark it as non-inheritable to child processes (set the
279 * close-on-exec flag).
281 * @param domain domain of the socket
282 * @param type socket type
283 * @param protocol network protocol
284 * @return new socket, NULL on error
286 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_create (int domain,
291 * Reset FD set (clears all file descriptors).
293 * @param fds fd set to clear
295 void GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
298 * Add a socket to the FD set
300 * @param desc socket to add
302 void GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
303 const struct GNUNET_NETWORK_Handle *desc);
307 /* TODO: maybe #ifdef WINDOWS? -ndurner */
309 * Add a W32 file handle to the fd set
311 * @param h the file handle to add
314 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet *fds,
320 * Check whether a socket is part of the fd set
323 * @return GNUNET_YES if the socket is in the set
325 int GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
326 const struct GNUNET_NETWORK_Handle *desc);
329 * Add one fd set to another
330 * @param dst the fd set to add to
331 * @param src the fd set to add from
333 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
334 const struct GNUNET_NETWORK_FDSet *src);
337 * Copy one fd set to another
338 * @param to destination
341 void GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
342 const struct GNUNET_NETWORK_FDSet *from);
346 * Return file descriptor for this network handle
348 * @param desc wrapper to process
349 * @return POSIX file descriptor
351 int GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
354 * Copy a native fd set
355 * @param to destination
356 * @param from native source set
357 * @param nfds the biggest socket number in from + 1
359 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
360 const fd_set * from, int nfds);
364 * Set a native fd in a set
366 * @param to destination
367 * @param nfd native FD to set
369 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
374 * Test native fd in a set
376 * @param to set to test, NULL for empty set
377 * @param nfd native FD to test, -1 for none
378 * @return GNUNET_YES if to contains nfd
381 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
386 * Add a file handle to the fd set
388 * @param h the file handle to add
390 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
391 const struct GNUNET_DISK_FileHandle *h);
394 * Check if a file handle is part of an fd set
396 * @param h file handle
397 * @return GNUNET_YES if the file handle is part of the set
399 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
400 const struct GNUNET_DISK_FileHandle
404 * Checks if two fd sets overlap
405 * @param fds1 first fd set
406 * @param fds2 second fd set
407 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
409 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
410 const struct GNUNET_NETWORK_FDSet *fds2);
414 * @return a new fd set
416 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
419 * Releases the associated memory of an fd set
422 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
425 #if 0 /* keep Emacsens' auto-indent happy */
432 #endif /* GNUNET_NETWORK_LIB_H */