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