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