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;
51 #include "gnunet_disk_lib.h"
52 #include "gnunet_time_lib.h"
55 * accept a new connection on a socket
57 * @param desc bound socket
58 * @param address address of the connecting peer, may be NULL
59 * @param address_len length of address
60 * @return client socket
62 struct GNUNET_NETWORK_Handle *
63 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
64 struct sockaddr *address,
65 socklen_t *address_len);
68 * Make a non-inheritable to child processes (sets the
69 * close-on-exec flag).
71 * @param h the socket to make non-inheritable
72 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
73 * @warning Not implemented on Windows
76 GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Handle
80 * Bind to a connected socket
82 * @param desc socket to bind
83 * @param address address to be bound
84 * @param address_len length of address
85 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
87 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
88 const struct sockaddr *address, socklen_t address_len);
93 * @param desc socket to close
94 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
96 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
101 * @param desc socket to connect
102 * @param address peer address
103 * @param address_len of address
104 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
106 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
107 const struct sockaddr *address,
108 socklen_t address_len);
114 * @param desc socket to inspect
115 * @param level protocol level of the option
116 * @param optname identifier of the option
117 * @param optval options
118 * @param optlen length of optval
119 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
121 int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname,
122 void *optval, socklen_t *optlen);
128 * @param desc socket to start listening on
129 * @param backlog length of the listen queue
130 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
132 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, int backlog);
135 * Read data from a connected socket
138 * @param buffer buffer
139 * @param length length of buffer
140 * @param flags type of message reception
141 * @return number of bytes read
143 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer,
144 size_t length, int flags);
147 * Check if sockets meet certain conditions
148 * @param rfds set of sockets to be checked for readability
149 * @param wfds set of sockets to be checked for writability
150 * @param efds set of sockets to be checked for exceptions
151 * @param timeout relative value when to return
152 * @return number of selected sockets, GNUNET_SYSERR on error
154 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
155 struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds,
156 struct GNUNET_TIME_Relative timeout);
159 * Set if a socket should use blocking or non-blocking IO.
161 * @param doBlock blocking mode
162 * @return GNUNET_OK on success, GNUNET_SYSERR on error
164 int GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
170 * @param buffer data to send
171 * @param length size of the buffer
172 * @param flags type of message transmission
173 * @return number of bytes sent, GNUNET_SYSERR on error
175 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
176 const void *buffer, size_t length, int flags);
181 * @param message data to send
182 * @param length size of the data
183 * @param flags type of message transmission
184 * @param dest_addr destination address
185 * @param dest_len length of address
186 * @return number of bytes sent, GNUNET_SYSERR on error
188 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
189 const void *message, size_t length, int flags,
190 const struct sockaddr *dest_addr,
196 * @param level protocol level of the option
197 * @param option_name option identifier
198 * @param option_value value to set
199 * @param option_len size of option_value
200 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
202 int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name,
203 const void *option_value, socklen_t option_len);
206 * Shut down socket operations
208 * @param how type of shutdown
209 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
211 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
214 * Create a new socket
215 * @param domain domain of the socket
216 * @param type socket type
217 * @param protocol network protocol
218 * @return new socket, NULL on error
220 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_socket (int domain, int type, int protocol);
223 * Reset FD set (clears all file descriptors).
225 * @param fds fd set to clear
227 void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds);
230 * Add a socket to the FD set
232 * @param desc socket to add
234 void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
235 const struct GNUNET_NETWORK_Handle *desc);
239 * Check whether a socket is part of the fd set
243 int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
244 const struct GNUNET_NETWORK_Handle *desc);
247 * Add one fd set to another
248 * @param dst the fd set to add to
249 * @param src the fd set to add from
251 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
252 const struct GNUNET_NETWORK_FDSet *src);
255 * Copy one fd set to another
256 * @param to destination
259 void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
260 const struct GNUNET_NETWORK_FDSet *from);
263 * Copy a native fd set
264 * @param to destination
265 * @param from native source set
266 * @param nfds the biggest socket number in from + 1
268 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
273 * Add a file handle to the fd set
275 * @param h the file handle to add
277 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
278 const struct GNUNET_DISK_FileHandle *h);
281 * Check if a file handle is part of an fd set
283 * @param h file handle
284 * @return GNUNET_YES if the file handle is part of the set
286 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
287 const struct GNUNET_DISK_FileHandle *h);
290 * Checks if two fd sets overlap
291 * @param fds1 first fd set
292 * @param fds2 second fd set
293 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
295 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
296 const struct GNUNET_NETWORK_FDSet *fds2);
300 * @return a new fd set
302 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
305 * Releases the associated memory of an fd set
308 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
311 #if 0 /* keep Emacsens' auto-indent happy */
318 #endif /* GNUNET_NETWORK_LIB_H */