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).
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 address address to be bound
83 * @param address_len length of address
84 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
86 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
87 const struct sockaddr *address, socklen_t address_len);
92 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
94 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
99 * @param address peer address
100 * @param length of address
101 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
103 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
104 const struct sockaddr *address, socklen_t address_len);
109 * @param level protocol level of the option
110 * @param optname identifier of the option
111 * @param optval options
112 * @param optlen length of optval
113 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
115 int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname,
116 void *optval, socklen_t *optlen);
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
130 * @param buffer buffer
131 * @param length length of buffer
132 * @param flags type of message reception
133 * @return number of bytes read
135 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer,
136 size_t length, int flags);
139 * Check if sockets meet certain conditions
140 * @param rfds set of sockets to be checked for readability
141 * @param wfds set of sockets to be checked for writability
142 * @param efds set of sockets to be checked for exceptions
143 * @param timeout relative value when to return
144 * @return number of selected sockets, GNUNET_SYSERR on error
146 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
147 struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds,
148 struct GNUNET_TIME_Relative timeout);
151 * Set if a socket should use blocking or non-blocking IO.
153 * @param doBlock blocking mode
154 * @return GNUNET_OK on success, GNUNET_SYSERR on error
156 int GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
162 * @param buffer data to send
163 * @param length size of the buffer
164 * @param flags type of message transmission
165 * @return number of bytes sent, GNUNET_SYSERR on error
167 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
168 const void *buffer, size_t length, int flags);
173 * @param message data to send
174 * @param length size of the data
175 * @param flags type of message transmission
176 * @param dest_addr destination address
177 * @param dest_len length of address
178 * @return number of bytes sent, GNUNET_SYSERR on error
180 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
181 const void *message, size_t length, int flags,
182 const struct sockaddr *dest_addr,
188 * @param level protocol level of the option
189 * @param option_name option identifier
190 * @param option_value value to set
191 * @param option_len size of option_value
192 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
194 int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name,
195 const void *option_value, socklen_t option_len);
198 * Shut down socket operations
200 * @param how type of shutdown
201 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
203 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
206 * Create a new socket
207 * @param domain domain of the socket
208 * @param type socket type
209 * @param protocol network protocol
210 * @return new socket, NULL on error
212 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_socket (int domain, int type, int protocol);
215 * Reset FD set (clears all file descriptors).
217 * @param fds fd set to clear
219 void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds);
222 * Add a socket to the FD set
224 * @param desc socket to add
226 void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
227 const struct GNUNET_NETWORK_Handle *desc);
231 * Check whether a socket is part of the fd set
235 int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
236 const struct GNUNET_NETWORK_Handle *desc);
239 * Add one fd set to another
240 * @param dst the fd set to add to
241 * @param src the fd set to add from
243 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
244 const struct GNUNET_NETWORK_FDSet *src);
247 * Copy one fd set to another
248 * @param to destination
251 void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
252 const struct GNUNET_NETWORK_FDSet *from);
255 * Copy a native fd set
256 * @param to destination
257 * @param from native source set
258 * @param the biggest socket number in from + 1
260 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
265 * Add a file handle to the fd set
267 * @param h the file handle to add
269 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
270 const struct GNUNET_DISK_FileHandle *h);
273 * Check if a file handle is part of an fd set
275 * @param h file handle
276 * @return GNUNET_YES if the file handle is part of the set
278 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
279 const struct GNUNET_DISK_FileHandle *h);
282 * Checks if two fd sets overlap
283 * @param fds1 first fd set
284 * @param fds2 second fd set
285 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
287 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
288 const struct GNUNET_NETWORK_FDSet *fds2);
292 * @return a new fd set
294 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
297 * Releases the associated memory of an fd set
300 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
303 #if 0 /* keep Emacsens' auto-indent happy */
310 #endif /* GNUNET_NETWORK_LIB_H */