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 length 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, socklen_t address_len);
113 * @param desc socket to inspect
114 * @param level protocol level of the option
115 * @param optname identifier of the option
116 * @param optval options
117 * @param optlen length of optval
118 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
120 int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname,
121 void *optval, socklen_t *optlen);
127 * @param desc socket to start listening on
128 * @param backlog length of the listen queue
129 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
131 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, int backlog);
134 * Read data from a connected socket
137 * @param buffer buffer
138 * @param length length of buffer
139 * @param flags type of message reception
140 * @return number of bytes read
142 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer,
143 size_t length, int flags);
146 * Check if sockets meet certain conditions
147 * @param rfds set of sockets to be checked for readability
148 * @param wfds set of sockets to be checked for writability
149 * @param efds set of sockets to be checked for exceptions
150 * @param timeout relative value when to return
151 * @return number of selected sockets, GNUNET_SYSERR on error
153 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
154 struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds,
155 struct GNUNET_TIME_Relative timeout);
158 * Set if a socket should use blocking or non-blocking IO.
160 * @param doBlock blocking mode
161 * @return GNUNET_OK on success, GNUNET_SYSERR on error
163 int GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
169 * @param buffer data to send
170 * @param length size of the buffer
171 * @param flags type of message transmission
172 * @return number of bytes sent, GNUNET_SYSERR on error
174 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
175 const void *buffer, size_t length, int flags);
180 * @param message data to send
181 * @param length size of the data
182 * @param flags type of message transmission
183 * @param dest_addr destination address
184 * @param dest_len length of address
185 * @return number of bytes sent, GNUNET_SYSERR on error
187 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
188 const void *message, size_t length, int flags,
189 const struct sockaddr *dest_addr,
195 * @param level protocol level of the option
196 * @param option_name option identifier
197 * @param option_value value to set
198 * @param option_len size of option_value
199 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
201 int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name,
202 const void *option_value, socklen_t option_len);
205 * Shut down socket operations
207 * @param how type of shutdown
208 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
210 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
213 * Create a new socket
214 * @param domain domain of the socket
215 * @param type socket type
216 * @param protocol network protocol
217 * @return new socket, NULL on error
219 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_socket (int domain, int type, int protocol);
222 * Reset FD set (clears all file descriptors).
224 * @param fds fd set to clear
226 void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds);
229 * Add a socket to the FD set
231 * @param desc socket to add
233 void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
234 const struct GNUNET_NETWORK_Handle *desc);
238 * Check whether a socket is part of the fd set
242 int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
243 const struct GNUNET_NETWORK_Handle *desc);
246 * Add one fd set to another
247 * @param dst the fd set to add to
248 * @param src the fd set to add from
250 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
251 const struct GNUNET_NETWORK_FDSet *src);
254 * Copy one fd set to another
255 * @param to destination
258 void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
259 const struct GNUNET_NETWORK_FDSet *from);
262 * Copy a native fd set
263 * @param to destination
264 * @param from native source set
265 * @param the biggest socket number in from + 1
267 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
272 * Add a file handle to the fd set
274 * @param h the file handle to add
276 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
277 const struct GNUNET_DISK_FileHandle *h);
280 * Check if a file handle is part of an fd set
282 * @param h file handle
283 * @return GNUNET_YES if the file handle is part of the set
285 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
286 const struct GNUNET_DISK_FileHandle *h);
289 * Checks if two fd sets overlap
290 * @param fds1 first fd set
291 * @param fds2 second fd set
292 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
294 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
295 const struct GNUNET_NETWORK_FDSet *fds2);
299 * @return a new fd set
301 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
304 * Releases the associated memory of an fd set
307 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
310 #if 0 /* keep Emacsens' auto-indent happy */
317 #endif /* GNUNET_NETWORK_LIB_H */