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 *GNUNET_NETWORK_socket_accept (const struct
75 * Box a native socket (and check that it is a socket).
77 * @param fd socket to box
78 * @return NULL on error (including not supported on target platform)
80 struct GNUNET_NETWORK_Handle *
81 GNUNET_NETWORK_socket_box_native (int fd);
85 * Bind to a connected socket
87 * @param desc socket to bind
88 * @param address address to be bound
89 * @param address_len length of address
90 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
92 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
93 const struct sockaddr *address,
94 socklen_t address_len);
99 * @param desc socket to close
100 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
102 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
107 * @param desc socket to connect
108 * @param address peer address
109 * @param address_len of address
110 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
112 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
113 const struct sockaddr *address,
114 socklen_t address_len);
120 * @param desc socket to inspect
121 * @param level protocol level of the option
122 * @param optname identifier of the option
123 * @param optval options
124 * @param optlen length of optval
125 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
127 int GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle
128 *desc, int level, int optname,
129 void *optval, socklen_t * optlen);
135 * @param desc socket to start listening on
136 * @param backlog length of the listen queue
137 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
139 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
143 * How much data is available to be read on this descriptor?
147 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
151 * Read data from a connected socket (always non-blocking).
153 * @param buffer buffer
154 * @param length length of buffer
155 * @param src_addr either the source to recv from, or all zeroes
156 * to be filled in by recvfrom
157 * @param addrlen length of the addr
160 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
161 void *buffer, size_t length,
162 struct sockaddr *src_addr,
163 socklen_t * addrlen);
166 * Read data from a connected socket (always non-blocking).
169 * @param buffer buffer
170 * @param length length of buffer
171 * @return number of bytes read
173 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
174 void *buffer, size_t length);
177 * Check if sockets meet certain conditions
178 * @param rfds set of sockets to be checked for readability
179 * @param wfds set of sockets to be checked for writability
180 * @param efds set of sockets to be checked for exceptions
181 * @param timeout relative value when to return
182 * @return number of selected sockets, GNUNET_SYSERR on error
184 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
185 struct GNUNET_NETWORK_FDSet *wfds,
186 struct GNUNET_NETWORK_FDSet *efds,
187 struct GNUNET_TIME_Relative timeout);
192 * Send data (always non-blocking).
195 * @param buffer data to send
196 * @param length size of the buffer
197 * @return number of bytes sent, GNUNET_SYSERR on error
199 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
200 const void *buffer, size_t length);
203 * Send data to a particular destination (always non-blocking).
204 * This function only works for UDP sockets.
207 * @param message data to send
208 * @param length size of the data
209 * @param dest_addr destination address
210 * @param dest_len length of address
211 * @return number of bytes sent, GNUNET_SYSERR on error
213 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle
214 *desc, const void *message,
216 const struct sockaddr *dest_addr,
222 * @param level protocol level of the option
223 * @param option_name option identifier
224 * @param option_value value to set
225 * @param option_len size of option_value
226 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
228 int GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
229 int level, int option_name,
230 const void *option_value,
231 socklen_t option_len);
234 * Shut down socket operations
236 * @param how type of shutdown
237 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
239 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
244 * Create a new socket. Configure it for non-blocking IO and
245 * mark it as non-inheritable to child processes (set the
246 * close-on-exec flag).
248 * @param domain domain of the socket
249 * @param type socket type
250 * @param protocol network protocol
251 * @return new socket, NULL on error
253 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_create (int domain,
258 * Reset FD set (clears all file descriptors).
260 * @param fds fd set to clear
262 void GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
265 * Add a socket to the FD set
267 * @param desc socket to add
269 void GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
270 const struct GNUNET_NETWORK_Handle *desc);
274 * Check whether a socket is part of the fd set
277 * @return GNUNET_YES if the socket is in the set
279 int GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
280 const struct GNUNET_NETWORK_Handle *desc);
283 * Add one fd set to another
284 * @param dst the fd set to add to
285 * @param src the fd set to add from
287 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
288 const struct GNUNET_NETWORK_FDSet *src);
291 * Copy one fd set to another
292 * @param to destination
295 void GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
296 const struct GNUNET_NETWORK_FDSet *from);
299 * Return file descriptor for this network handle
301 * @param desc wrapper to process
302 * @return POSIX file descriptor
304 int GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
307 * Copy a native fd set
308 * @param to destination
309 * @param from native source set
310 * @param nfds the biggest socket number in from + 1
312 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
313 const fd_set * from, int nfds);
317 * Set a native fd in a set
319 * @param to destination
320 * @param nfd native FD to set
322 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
326 * Add a file handle to the fd set
328 * @param h the file handle to add
330 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
331 const struct GNUNET_DISK_FileHandle *h);
334 * Check if a file handle is part of an fd set
336 * @param h file handle
337 * @return GNUNET_YES if the file handle is part of the set
339 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
340 const struct GNUNET_DISK_FileHandle
344 * Checks if two fd sets overlap
345 * @param fds1 first fd set
346 * @param fds2 second fd set
347 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
349 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
350 const struct GNUNET_NETWORK_FDSet *fds2);
354 * @return a new fd set
356 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
359 * Releases the associated memory of an fd set
362 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
365 #if 0 /* keep Emacsens' auto-indent happy */
372 #endif /* GNUNET_NETWORK_LIB_H */