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"
56 * Accept a new connection on a socket. Configure it for non-blocking
57 * IO and mark it as non-inheritable to child processes (set the
58 * close-on-exec flag).
60 * @param desc bound socket
61 * @param address address of the connecting peer, may be NULL
62 * @param address_len length of address
63 * @return client socket
65 struct GNUNET_NETWORK_Handle *
66 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
67 struct sockaddr *address,
68 socklen_t *address_len);
72 * Bind to a connected socket
74 * @param desc socket to bind
75 * @param address address to be bound
76 * @param address_len length of address
77 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
79 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
80 const struct sockaddr *address, socklen_t address_len);
85 * @param desc socket to close
86 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
88 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
93 * @param desc socket to connect
94 * @param address peer address
95 * @param address_len of address
96 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
98 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
99 const struct sockaddr *address,
100 socklen_t address_len);
106 * @param desc socket to inspect
107 * @param level protocol level of the option
108 * @param optname identifier of the option
109 * @param optval options
110 * @param optlen length of optval
111 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
113 int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname,
114 void *optval, socklen_t *optlen);
120 * @param desc socket to start listening on
121 * @param backlog length of the listen queue
122 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
124 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, int backlog);
127 * How much data is available to be read on this descriptor?
131 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle * desc);
134 * Read data from a connected socket (always non-blocking).
136 * @param buffer buffer
137 * @param length length of buffer
138 * @param src_addr either the source to recv from, or all zeroes
139 * to be filled in by recvfrom
140 * @param addrlen length of the addr
143 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
144 void *buffer, size_t length,
145 struct sockaddr *src_addr, socklen_t *addrlen);
148 * Read data from a connected socket (always non-blocking).
151 * @param buffer buffer
152 * @param length length of buffer
153 * @return number of bytes read
155 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer,
159 * Check if sockets meet certain conditions
160 * @param rfds set of sockets to be checked for readability
161 * @param wfds set of sockets to be checked for writability
162 * @param efds set of sockets to be checked for exceptions
163 * @param timeout relative value when to return
164 * @return number of selected sockets, GNUNET_SYSERR on error
166 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
167 struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds,
168 struct GNUNET_TIME_Relative timeout);
173 * Send data (always non-blocking).
176 * @param buffer data to send
177 * @param length size of the buffer
178 * @return number of bytes sent, GNUNET_SYSERR on error
180 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
181 const void *buffer, size_t length);
184 * Send data to a particular destination (always non-blocking).
185 * This function only works for UDP sockets.
188 * @param message data to send
189 * @param length size of the data
190 * @param dest_addr destination address
191 * @param dest_len length of address
192 * @return number of bytes sent, GNUNET_SYSERR on error
194 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
195 const void *message, size_t length,
196 const struct sockaddr *dest_addr,
202 * @param level protocol level of the option
203 * @param option_name option identifier
204 * @param option_value value to set
205 * @param option_len size of option_value
206 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
208 int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name,
209 const void *option_value, socklen_t option_len);
212 * Shut down socket operations
214 * @param how type of shutdown
215 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
217 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
221 * Create a new socket. Configure it for non-blocking IO and
222 * mark it as non-inheritable to child processes (set the
223 * close-on-exec flag).
225 * @param domain domain of the socket
226 * @param type socket type
227 * @param protocol network protocol
228 * @return new socket, NULL on error
230 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
233 * Reset FD set (clears all file descriptors).
235 * @param fds fd set to clear
237 void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds);
240 * Add a socket to the FD set
242 * @param desc socket to add
244 void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
245 const struct GNUNET_NETWORK_Handle *desc);
249 * Check whether a socket is part of the fd set
253 int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
254 const struct GNUNET_NETWORK_Handle *desc);
257 * Add one fd set to another
258 * @param dst the fd set to add to
259 * @param src the fd set to add from
261 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
262 const struct GNUNET_NETWORK_FDSet *src);
265 * Copy one fd set to another
266 * @param to destination
269 void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
270 const struct GNUNET_NETWORK_FDSet *from);
273 * Return file descriptor for this network handle
276 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
278 * Copy a native fd set
279 * @param to destination
280 * @param from native source set
281 * @param nfds the biggest socket number in from + 1
283 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
288 * Add a file handle to the fd set
290 * @param h the file handle to add
292 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
293 const struct GNUNET_DISK_FileHandle *h);
296 * Check if a file handle is part of an fd set
298 * @param h file handle
299 * @return GNUNET_YES if the file handle is part of the set
301 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
302 const struct GNUNET_DISK_FileHandle *h);
305 * Checks if two fd sets overlap
306 * @param fds1 first fd set
307 * @param fds2 second fd set
308 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
310 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
311 const struct GNUNET_NETWORK_FDSet *fds2);
315 * @return a new fd set
317 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
320 * Releases the associated memory of an fd set
323 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
326 #if 0 /* keep Emacsens' auto-indent happy */
333 #endif /* GNUNET_NETWORK_LIB_H */