1405efa2a9a9b8877295e7b59cb1f16444ac2aa9
[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  * @brief handle to a socket
41  */
42 struct GNUNET_NETWORK_Handle;
43
44
45 /**
46  * @brief collection of IO descriptors
47  */
48 struct GNUNET_NETWORK_FDSet
49 {
50
51   /**
52    * Maximum number of any socket socket descriptor in the set (plus one)
53    */
54   int nsds;
55
56   /**
57    * Bitset with the descriptors.
58    */
59   fd_set sds;
60
61 #ifdef WINDOWS
62   /**
63    * Linked list of handles
64    */
65   struct GNUNET_CONTAINER_SList *handles;
66 #endif
67
68 };
69
70
71
72 #include "gnunet_disk_lib.h"
73 #include "gnunet_time_lib.h"
74
75
76 /**
77  * Accept a new connection on a socket.  Configure it for non-blocking
78  * IO and mark it as non-inheritable to child processes (set the
79  * close-on-exec flag).
80  *
81  * @param desc bound socket
82  * @param address address of the connecting peer, may be NULL
83  * @param address_len length of address
84  * @return client socket
85  */
86 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_accept (const struct
87                                                             GNUNET_NETWORK_Handle
88                                                             *desc,
89                                                             struct sockaddr
90                                                             *address,
91                                                             socklen_t *
92                                                             address_len);
93
94
95 /**
96  * Box a native socket (and check that it is a socket).
97  *
98  * @param fd socket to box
99  * @return NULL on error (including not supported on target platform)
100  */
101 struct GNUNET_NETWORK_Handle *
102 GNUNET_NETWORK_socket_box_native (int fd);
103
104
105 /**
106  * Bind to a connected socket
107  *
108  * @param desc socket to bind
109  * @param address address to be bound
110  * @param address_len length of address
111  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
112  */
113 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
114                                 const struct sockaddr *address,
115                                 socklen_t address_len);
116
117 /**
118  * Close a socket.
119  *
120  * @param desc socket to close
121  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
122  */
123 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
124
125
126 /**
127  * Connect a socket
128  *
129  * @param desc socket to connect
130  * @param address peer address
131  * @param address_len of address
132  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
133  */
134 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
135                                    const struct sockaddr *address,
136                                    socklen_t address_len);
137
138
139 /**
140  * Get socket options
141  *
142  * @param desc socket to inspect
143  * @param level protocol level of the option
144  * @param optname identifier of the option
145  * @param optval options
146  * @param optlen length of optval
147  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
148  */
149 int GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle
150                                       *desc, int level, int optname,
151                                       void *optval, socklen_t * optlen);
152
153
154 /**
155  * Listen on a socket
156  *
157  * @param desc socket to start listening on
158  * @param backlog length of the listen queue
159  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
160  */
161 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
162                                   int backlog);
163
164
165 /**
166  * How much data is available to be read on this descriptor?
167  * @param desc socket
168  */
169 ssize_t
170 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
171                                        *desc);
172
173
174 /**
175  * Read data from a connected socket (always non-blocking).
176  * @param desc socket
177  * @param buffer buffer
178  * @param length length of buffer
179  * @param src_addr either the source to recv from, or all zeroes
180  *        to be filled in by recvfrom
181  * @param addrlen length of the addr
182  */
183 ssize_t
184 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
185                                 void *buffer, size_t length,
186                                 struct sockaddr *src_addr,
187                                 socklen_t * addrlen);
188
189
190 /**
191  * Read data from a connected socket (always non-blocking).
192  *
193  * @param desc socket
194  * @param buffer buffer
195  * @param length length of buffer
196  * @return number of bytes read
197  */
198 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
199                                     void *buffer, size_t length);
200
201
202 /**
203  * Check if sockets meet certain conditions
204  * @param rfds set of sockets to be checked for readability
205  * @param wfds set of sockets to be checked for writability
206  * @param efds set of sockets to be checked for exceptions
207  * @param timeout relative value when to return
208  * @return number of selected sockets, GNUNET_SYSERR on error
209  */
210 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
211                                   struct GNUNET_NETWORK_FDSet *wfds,
212                                   struct GNUNET_NETWORK_FDSet *efds,
213                                   struct GNUNET_TIME_Relative timeout);
214
215
216 /**
217  * Send data (always non-blocking).
218  *
219  * @param desc socket
220  * @param buffer data to send
221  * @param length size of the buffer
222  * @return number of bytes sent, GNUNET_SYSERR on error
223  */
224 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
225                                     const void *buffer, size_t length);
226
227
228 /**
229  * Send data to a particular destination (always non-blocking).
230  * This function only works for UDP sockets.
231  *
232  * @param desc socket
233  * @param message data to send
234  * @param length size of the data
235  * @param dest_addr destination address
236  * @param dest_len length of address
237  * @return number of bytes sent, GNUNET_SYSERR on error
238  */
239 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle
240                                       *desc, const void *message,
241                                       size_t length,
242                                       const struct sockaddr *dest_addr,
243                                       socklen_t dest_len);
244
245
246 /**
247  * Set socket option
248  *
249  * @param fd socket
250  * @param level protocol level of the option
251  * @param option_name option identifier
252  * @param option_value value to set
253  * @param option_len size of option_value
254  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
255  */
256 int GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
257                                       int level, int option_name,
258                                       const void *option_value,
259                                       socklen_t option_len);
260
261
262 /**
263  * Shut down socket operations
264  *
265  * @param desc socket
266  * @param how type of shutdown
267  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
268  */
269 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
270                                     int how);
271
272
273 /**
274  * Disable the "CORK" feature for communication with the given socket,
275  * forcing the OS to immediately flush the buffer on transmission
276  * instead of potentially buffering multiple messages.  Essentially
277  * reduces the OS send buffers to zero.
278  *
279  * @param desc socket
280  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
281  */
282 int GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
283
284
285 /**
286  * Create a new socket.   Configure it for non-blocking IO and
287  * mark it as non-inheritable to child processes (set the
288  * close-on-exec flag).
289  *
290  * @param domain domain of the socket
291  * @param type socket type
292  * @param protocol network protocol
293  * @return new socket, NULL on error
294  */
295 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_create (int domain,
296                                                             int type,
297                                                             int protocol);
298
299
300 /**
301  * Reset FD set (clears all file descriptors).
302  *
303  * @param fds fd set to clear
304  */
305 void GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
306
307
308 /**
309  * Add a socket to the FD set
310  * @param fds fd set
311  * @param desc socket to add
312  */
313 void GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
314                                const struct GNUNET_NETWORK_Handle *desc);
315
316
317 #ifdef __MINGW32__
318 /* TODO: maybe #ifdef WINDOWS? -ndurner */
319 /**
320  * Add a W32 file handle to the fd set
321  * @param fds fd set
322  * @param h the file handle to add
323  */
324 void
325 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet *fds,
326                                                    HANDLE h);
327 #endif
328
329
330 /**
331  * Check whether a socket is part of the fd set
332  * @param fds fd set
333  * @param desc socket
334  * @return GNUNET_YES if the socket is in the set
335  */
336 int GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
337                                 const struct GNUNET_NETWORK_Handle *desc);
338
339
340 /**
341  * Add one fd set to another
342  * @param dst the fd set to add to
343  * @param src the fd set to add from
344  */
345 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
346                                const struct GNUNET_NETWORK_FDSet *src);
347
348
349 /**
350  * Copy one fd set to another
351  * @param to destination
352  * @param from source
353  */
354 void GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
355                                 const struct GNUNET_NETWORK_FDSet *from);
356
357
358 /**
359  * Return file descriptor for this network handle
360  *
361  * @param desc wrapper to process
362  * @return POSIX file descriptor
363  */
364 int GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc);
365
366
367 /**
368  * Copy a native fd set
369  * @param to destination
370  * @param from native source set
371  * @param nfds the biggest socket number in from + 1
372  */
373 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
374                                        const fd_set * from, int nfds);
375
376
377 /**
378  * Set a native fd in a set
379  *
380  * @param to destination
381  * @param nfd native FD to set
382  */
383 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
384                                       int nfd);
385
386
387 /**
388  * Test native fd in a set
389  *
390  * @param to set to test, NULL for empty set
391  * @param nfd native FD to test, -1 for none
392  * @return GNUNET_YES if to contains nfd
393  */
394 int 
395 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
396                                   int nfd);
397
398
399 /**
400  * Add a file handle to the fd set
401  * @param fds fd set
402  * @param h the file handle to add
403  */
404 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
405                                       const struct GNUNET_DISK_FileHandle *h);
406
407
408 /**
409  * Check if a file handle is part of an fd set
410  * @param fds fd set
411  * @param h file handle
412  * @return GNUNET_YES if the file handle is part of the set
413  */
414 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
415                                        const struct GNUNET_DISK_FileHandle
416                                        *h);
417
418
419 /**
420  * Checks if two fd sets overlap
421  * @param fds1 first fd set
422  * @param fds2 second fd set
423  * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
424  */
425 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
426                                   const struct GNUNET_NETWORK_FDSet *fds2);
427
428
429 /**
430  * Creates an fd set
431  * @return a new fd set
432  */
433 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
434
435
436 /**
437  * Releases the associated memory of an fd set
438  * @param fds fd set
439  */
440 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
441
442
443 #if 0                           /* keep Emacsens' auto-indent happy */
444 {
445 #endif
446 #ifdef __cplusplus
447 }
448 #endif
449
450 #endif /* GNUNET_NETWORK_LIB_H */