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