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