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 * Read data from a connected socket (always non-blocking).
130 * @param buffer buffer
131 * @param length length of buffer
132 * @return number of bytes read
134 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer,
138 * Check if sockets meet certain conditions
139 * @param rfds set of sockets to be checked for readability
140 * @param wfds set of sockets to be checked for writability
141 * @param efds set of sockets to be checked for exceptions
142 * @param timeout relative value when to return
143 * @return number of selected sockets, GNUNET_SYSERR on error
145 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
146 struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds,
147 struct GNUNET_TIME_Relative timeout);
152 * Send data (always non-blocking).
155 * @param buffer data to send
156 * @param length size of the buffer
157 * @return number of bytes sent, GNUNET_SYSERR on error
159 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
160 const void *buffer, size_t length);
163 * Send data to a particular destination (always non-blocking).
164 * This function only works for UDP sockets.
167 * @param message data to send
168 * @param length size of the data
169 * @param dest_addr destination address
170 * @param dest_len length of address
171 * @return number of bytes sent, GNUNET_SYSERR on error
173 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
174 const void *message, size_t length,
175 const struct sockaddr *dest_addr,
181 * @param level protocol level of the option
182 * @param option_name option identifier
183 * @param option_value value to set
184 * @param option_len size of option_value
185 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
187 int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name,
188 const void *option_value, socklen_t option_len);
191 * Shut down socket operations
193 * @param how type of shutdown
194 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
196 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
200 * Create a new socket. Configure it for non-blocking IO and
201 * mark it as non-inheritable to child processes (set the
202 * close-on-exec flag).
204 * @param domain domain of the socket
205 * @param type socket type
206 * @param protocol network protocol
207 * @return new socket, NULL on error
209 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
212 * Reset FD set (clears all file descriptors).
214 * @param fds fd set to clear
216 void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds);
219 * Add a socket to the FD set
221 * @param desc socket to add
223 void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
224 const struct GNUNET_NETWORK_Handle *desc);
228 * Check whether a socket is part of the fd set
232 int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
233 const struct GNUNET_NETWORK_Handle *desc);
236 * Add one fd set to another
237 * @param dst the fd set to add to
238 * @param src the fd set to add from
240 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
241 const struct GNUNET_NETWORK_FDSet *src);
244 * Copy one fd set to another
245 * @param to destination
248 void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
249 const struct GNUNET_NETWORK_FDSet *from);
252 * Copy a native fd set
253 * @param to destination
254 * @param from native source set
255 * @param nfds the biggest socket number in from + 1
257 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
262 * Add a file handle to the fd set
264 * @param h the file handle to add
266 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
267 const struct GNUNET_DISK_FileHandle *h);
270 * Check if a file handle is part of an fd set
272 * @param h file handle
273 * @return GNUNET_YES if the file handle is part of the set
275 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
276 const struct GNUNET_DISK_FileHandle *h);
279 * Checks if two fd sets overlap
280 * @param fds1 first fd set
281 * @param fds2 second fd set
282 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
284 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
285 const struct GNUNET_NETWORK_FDSet *fds2);
289 * @return a new fd set
291 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
294 * Releases the associated memory of an fd set
297 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
300 #if 0 /* keep Emacsens' auto-indent happy */
307 #endif /* GNUNET_NETWORK_LIB_H */