71c8d43467bd2682f32c7b19eb8889e8aa0afcac
[oweals/gnunet.git] / src / include / gnunet_network_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file include/gnunet_network_lib.h
23  * @brief basic low-level networking interface
24  * @author Nils Durner
25  */
26
27 #ifndef GNUNET_NETWORK_LIB_H
28 #define GNUNET_NETWORK_LIB_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38
39
40 /**
41  * @brief handle to a socket
42  */
43 struct GNUNET_NETWORK_Handle;
44
45 /**
46  * @brief collection of IO descriptors
47  */
48 struct GNUNET_NETWORK_FDSet;
49
50
51 #include "gnunet_disk_lib.h"
52 #include "gnunet_time_lib.h"
53
54
55 /**
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).
59  *
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
64  */
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);
69
70
71 /**
72  * Bind to a connected socket
73  *
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
78  */
79 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
80                     const struct sockaddr *address, socklen_t address_len);
81
82 /**
83  * Close a socket.
84  *
85  * @param desc socket to close
86  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
87  */
88 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
89
90 /**
91  * Connect a socket
92  *
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
97  */
98 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
99                                    const struct sockaddr *address, 
100                                    socklen_t address_len);
101
102
103 /**
104  * Get socket options
105  *
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
112  */
113 int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname,
114        void *optval, socklen_t *optlen);
115
116
117 /**
118  * Listen on a socket
119  *
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
123  */
124 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, int backlog);
125
126 /**
127  * How much data is available to be read on this descriptor?
128  * @param desc socket
129  */
130 unsigned int
131 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle * desc);
132
133 /**
134  * Read data from a connected socket (always non-blocking).
135  * @param desc socket
136  * @param buffer buffer
137  * @param length length of buffer
138  * @param src_addr either the source to recv from, or all zeroes
139  *        to be filled in by recvfrom
140  * @param addrlen length of the addr
141  */
142 ssize_t
143 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
144                                 void *buffer, size_t length,
145                                 struct sockaddr *src_addr, socklen_t *addrlen);
146
147 /**
148  * Read data from a connected socket (always non-blocking).
149  *
150  * @param desc socket
151  * @param buffer buffer
152  * @param length length of buffer
153  * @return number of bytes read
154  */
155 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer,
156                                     size_t length);
157
158 /**
159  * Check if sockets meet certain conditions
160  * @param rfds set of sockets to be checked for readability
161  * @param wfds set of sockets to be checked for writability
162  * @param efds set of sockets to be checked for exceptions
163  * @param timeout relative value when to return
164  * @return number of selected sockets, GNUNET_SYSERR on error
165  */
166 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
167     struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds,
168     struct GNUNET_TIME_Relative timeout);
169
170
171
172 /**
173  * Send data (always non-blocking).
174  *
175  * @param desc socket
176  * @param buffer data to send
177  * @param length size of the buffer
178  * @return number of bytes sent, GNUNET_SYSERR on error
179  */
180 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
181                         const void *buffer, size_t length);
182
183 /**
184  * Send data to a particular destination (always non-blocking).
185  * This function only works for UDP sockets.
186  *
187  * @param desc socket
188  * @param message data to send
189  * @param length size of the data
190  * @param dest_addr destination address
191  * @param dest_len length of address
192  * @return number of bytes sent, GNUNET_SYSERR on error
193  */
194 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
195                           const void *message, size_t length, 
196                           const struct sockaddr *dest_addr,
197                           socklen_t dest_len);
198
199 /**
200  * Set socket option
201  * @param fd socket
202  * @param level protocol level of the option
203  * @param option_name option identifier
204  * @param option_value value to set
205  * @param option_len size of option_value
206  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
207  */
208 int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name,
209        const void *option_value, socklen_t option_len);
210
211 /**
212  * Shut down socket operations
213  * @param desc socket
214  * @param how type of shutdown
215  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
216  */
217 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
218
219
220 /**
221  * Create a new socket.   Configure it for non-blocking IO and
222  * mark it as non-inheritable to child processes (set the
223  * close-on-exec flag).
224  *
225  * @param domain domain of the socket
226  * @param type socket type
227  * @param protocol network protocol
228  * @return new socket, NULL on error
229  */
230 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
231
232 /**
233  * Reset FD set (clears all file descriptors).
234  *
235  * @param fds fd set to clear
236  */
237 void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds);
238
239 /**
240  * Add a socket to the FD set
241  * @param fds fd set
242  * @param desc socket to add
243  */
244 void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
245                               const struct GNUNET_NETWORK_Handle *desc);
246
247
248 /**
249  * Check whether a socket is part of the fd set
250  * @param fds fd set
251  * @param desc socket
252  */
253 int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
254                                const struct GNUNET_NETWORK_Handle *desc);
255
256 /**
257  * Add one fd set to another
258  * @param dst the fd set to add to
259  * @param src the fd set to add from
260  */
261 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
262                                const struct GNUNET_NETWORK_FDSet *src);
263
264 /**
265  * Copy one fd set to another
266  * @param to destination
267  * @param from source
268  */
269 void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
270                                const struct GNUNET_NETWORK_FDSet *from);
271
272 /*
273  * Return file descriptor for this network handle
274  */
275 int
276 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
277 /**
278  * Copy a native fd set
279  * @param to destination
280  * @param from native source set
281  * @param nfds the biggest socket number in from + 1
282  */
283 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, 
284                                        const fd_set *from,
285                                        int nfds);
286
287 /**
288  * Add a file handle to the fd set
289  * @param fds fd set
290  * @param h the file handle to add
291  */
292 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
293                                       const struct GNUNET_DISK_FileHandle *h);
294
295 /**
296  * Check if a file handle is part of an fd set
297  * @param fds fd set
298  * @param h file handle
299  * @return GNUNET_YES if the file handle is part of the set
300  */
301 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
302                                        const struct GNUNET_DISK_FileHandle *h);
303
304 /**
305  * Checks if two fd sets overlap
306  * @param fds1 first fd set
307  * @param fds2 second fd set
308  * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
309  */
310 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, 
311                                   const struct GNUNET_NETWORK_FDSet *fds2);
312
313 /**
314  * Creates an fd set
315  * @return a new fd set
316  */
317 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
318
319 /**
320  * Releases the associated memory of an fd set
321  * @param fds fd set
322  */
323 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
324
325
326 #if 0                           /* keep Emacsens' auto-indent happy */
327 {
328 #endif
329 #ifdef __cplusplus
330 }
331 #endif
332
333 #endif /* GNUNET_NETWORK_LIB_H */