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