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
70 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
71 * @warning Not implemented on Windows
74 GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Handle
78 * Bind to a connected socket
80 * @param address address to be bound
81 * @param address_len length of address
82 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
84 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
85 const struct sockaddr *address, socklen_t address_len);
90 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
92 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
97 * @param address peer address
98 * @param length of address
99 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
101 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
102 const struct sockaddr *address, socklen_t address_len);
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);
119 * @param backlog length of the listen queue
120 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
122 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, int backlog);
125 * Read data from a connected socket
127 * @param buffer buffer
128 * @param length length of buffer
129 * @param flags type of message reception
131 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer,
132 size_t length, int flags);
135 * Check if sockets meet certain conditions
136 * @param rfds set of sockets to be checked for readability
137 * @param wfds set of sockets to be checked for writability
138 * @param efds set of sockets to be checked for exceptions
139 * @param timeout relative value when to return
140 * @return number of selected sockets, GNUNET_SYSERR on error
142 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
143 struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds,
144 struct GNUNET_TIME_Relative timeout);
147 * Set if a socket should use blocking or non-blocking IO.
149 * @param doBlock blocking mode
150 * @return GNUNET_OK on success, GNUNET_SYSERR on error
152 int GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock);
157 * @param buffer data to send
158 * @param length size of the buffer
159 * @param flags type of message transmission
160 * @return number of bytes sent, GNUNET_SYSERR on error
162 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
163 const void *buffer, size_t length, int flags);
168 * @param message data to send
169 * @param length size of the data
170 * @param flags type of message transmission
171 * @param dest_addr destination address
172 * @param dest_len length of address
173 * @return number of bytes sent, GNUNET_SYSERR on error
175 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
176 const void *message, size_t length, int flags,
177 const struct sockaddr *dest_addr,
183 * @param level protocol level of the option
184 * @param option_name option identifier
185 * @param option_value value to set
186 * @param option_len size of option_value
187 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
189 int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name,
190 const void *option_value, socklen_t option_len);
193 * Shut down socket operations
195 * @param how type of shutdown
196 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
198 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
201 * Create a new socket
202 * @param domain domain of the socket
203 * @param type socket type
204 * @param protocol network protocol
205 * @return new socket, NULL on error
207 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_socket (int domain, int type, int protocol);
213 void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds);
216 * Add a socket to the FD set
218 * @param desc socket to add
220 void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
221 const struct GNUNET_NETWORK_Handle *desc);
224 * Check whether a socket is part of the fd set
228 int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
229 const struct GNUNET_NETWORK_Handle *desc);
232 * Add one fd set to another
233 * @param dst the fd set to add to
234 * @param src the fd set to add from
236 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
237 const struct GNUNET_NETWORK_FDSet *src);
240 * Copy one fd set to another
241 * @param to destination
244 void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
245 const struct GNUNET_NETWORK_FDSet *from);
248 * Copy a native fd set
249 * @param to destination
250 * @param from native source set
251 * @param the biggest socket number in from + 1
253 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, const fd_set *from,
257 * Add a file handle to the fd set
259 * @param h the file handle to add
261 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
262 const struct GNUNET_DISK_FileHandle *h);
265 * Check if a file handle is part of an fd set
267 * @param h file handle
268 * @return GNUNET_YES if the file handle is part of the set
270 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
271 const struct GNUNET_DISK_FileHandle *h);
274 * Checks if two fd sets overlap
275 * @param fds1 first fd set
276 * @param fds2 second fd set
277 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
279 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, const struct GNUNET_NETWORK_FDSet *fds2);
283 * @return a new fd set
285 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
288 * Releases the associated memory of an fd set
291 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
294 #if 0 /* keep Emacsens' auto-indent happy */
301 #endif /* GNUNET_NETWORK_LIB_H */