fix bad free
[oweals/gnunet.git] / src / include / gnunet_network_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @author Nils Durner
21  *
22  * @file
23  * Basic low-level networking interface
24  *
25  * @defgroup network  Network library
26  * Basic low-level networking interface
27  * @{
28  */
29 #ifndef GNUNET_NETWORK_LIB_H
30 #define GNUNET_NETWORK_LIB_H
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40
41 /**
42  * @brief handle to a socket
43  */
44 struct GNUNET_NETWORK_Handle;
45
46
47 /**
48  * @brief collection of IO descriptors
49  */
50 struct GNUNET_NETWORK_FDSet
51 {
52
53   /**
54    * Maximum number of any socket descriptor in the set (plus one)
55    */
56   int nsds;
57
58   /**
59    * Bitset with the descriptors.
60    */
61   fd_set sds;
62
63 #ifdef WINDOWS
64   /**
65    * Array of file handles (from pipes) that are also in
66    * the FDSet.  Needed as those cannot go into @e sds
67    * on W32.
68    */
69   const struct GNUNET_DISK_FileHandle **handles;
70
71   /**
72    * Size of the @e handles array
73    */
74   unsigned int handles_size;
75
76   /**
77    * Number of @e handles slots in use. Always
78    * smaller than @e handles_size.
79    */
80   unsigned int handles_pos;
81
82 #endif
83
84 };
85
86 #include "gnunet_disk_lib.h"
87 #include "gnunet_time_lib.h"
88
89 /**
90  * Test if the given protocol family is supported by this system.
91  *
92  * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
93  * @return #GNUNET_OK if the PF is supported
94  */
95 int
96 GNUNET_NETWORK_test_pf (int pf);
97
98
99 /**
100  * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
101  * shorten it to an acceptable length while keeping it unique
102  * and making sure it remains a valid filename (if possible).
103  *
104  * @param unixpath long path, will be freed (or same pointer returned
105  *        with moved 0-termination).
106  * @return shortened unixpath, NULL on error
107  */
108 char *
109 GNUNET_NETWORK_shorten_unixpath (char *unixpath);
110
111
112 #ifndef WINDOWS
113 /**
114  * If services crash, they can leave a unix domain socket file on the
115  * disk. This needs to be manually removed, because otherwise both
116  * bind() and connect() for the respective address will fail.  In this
117  * function, we test if such a left-over file exists, and if so,
118  * remove it (unless there is a listening service at the address).
119  *
120  * @param un unix domain socket address to check
121  */
122 void
123 GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un);
124 #endif
125
126
127 /**
128  * Accept a new connection on a socket.  Configure it for non-blocking
129  * IO and mark it as non-inheritable to child processes (set the
130  * close-on-exec flag).
131  *
132  * @param desc bound socket
133  * @param address address of the connecting peer, may be NULL
134  * @param address_len length of address
135  * @return client socket
136  */
137 struct GNUNET_NETWORK_Handle *
138 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
139                               struct sockaddr *address,
140                               socklen_t *address_len);
141
142
143 /**
144  * Box a native socket (and check that it is a socket).
145  *
146  * @param fd socket to box
147  * @return NULL on error (including not supported on target platform)
148  */
149 struct GNUNET_NETWORK_Handle *
150 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd);
151
152
153 /**
154  * Set if a socket should use blocking or non-blocking IO.
155  *
156  * @param fd socket
157  * @param doBlock blocking mode
158  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
159  */
160 int
161 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
162                                     int doBlock);
163
164
165 /**
166  * Bind a socket to a particular address.
167  *
168  * @param desc socket to bind
169  * @param address address to be bound
170  * @param address_len length of @a address
171  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
172  */
173 int
174 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
175                             const struct sockaddr *address,
176                             socklen_t address_len);
177
178 /**
179  * Close a socket.
180  *
181  * @param desc socket to close
182  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
183  */
184 int
185 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
186
187
188 /**
189  * Only free memory of a socket, keep the file descriptor untouched.
190  *
191  * @param desc socket
192  */
193 void
194 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc);
195
196
197 /**
198  * Connect a socket to some remote address.
199  *
200  * @param desc socket to connect
201  * @param address peer address
202  * @param address_len of @a address
203  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
204  */
205 int
206 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
207                                const struct sockaddr *address,
208                                socklen_t address_len);
209
210
211 /**
212  * Get socket options
213  *
214  * @param desc socket to inspect
215  * @param level protocol level of the option
216  * @param optname identifier of the option
217  * @param optval options
218  * @param optlen length of optval
219  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
220  */
221 int
222 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
223                                   int level,
224                                   int optname,
225                                   void *optval,
226                                   socklen_t *optlen);
227
228
229 /**
230  * Listen on a socket
231  *
232  * @param desc socket to start listening on
233  * @param backlog length of the listen queue
234  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
235  */
236 int
237 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
238                               int backlog);
239
240
241 /**
242  * How much data is available to be read on this descriptor?
243  *
244  * @param desc socket
245  * @returns #GNUNET_SYSERR if no data is available, or on error!
246  */
247 ssize_t
248 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc);
249
250
251 /**
252  * Read data from a socket (always non-blocking).
253  *
254  * @param desc socket
255  * @param buffer buffer
256  * @param length length of buffer
257  * @param src_addr either the source to recv from, or all zeroes
258  *        to be filled in by recvfrom
259  * @param addrlen length of the addr
260  */
261 ssize_t
262 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
263                                 void *buffer,
264                                 size_t length,
265                                 struct sockaddr *src_addr,
266                                 socklen_t *addrlen);
267
268
269 /**
270  * Read data from a connected socket (always non-blocking).
271  *
272  * @param desc socket
273  * @param buffer buffer
274  * @param length length of buffer
275  * @return number of bytes read
276  */
277 ssize_t
278 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
279                             void *buffer,
280                             size_t length);
281
282
283 /**
284  * Check if sockets meet certain conditions.
285  *
286  * @param rfds set of sockets to be checked for readability
287  * @param wfds set of sockets to be checked for writability
288  * @param efds set of sockets to be checked for exceptions
289  * @param timeout relative value when to return
290  * @return number of selected sockets, #GNUNET_SYSERR on error
291  */
292 int
293 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
294                               struct GNUNET_NETWORK_FDSet *wfds,
295                               struct GNUNET_NETWORK_FDSet *efds,
296                               struct GNUNET_TIME_Relative timeout);
297
298
299 /**
300  * Send data (always non-blocking).
301  *
302  * @param desc socket
303  * @param buffer data to send
304  * @param length size of the buffer
305  * @return number of bytes sent, #GNUNET_SYSERR on error
306  */
307 ssize_t
308 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
309                             const void *buffer,
310                             size_t length);
311
312
313 /**
314  * Send data to a particular destination (always non-blocking).
315  * This function only works for UDP sockets.
316  *
317  * @param desc socket
318  * @param message data to send
319  * @param length size of the data in @a message
320  * @param dest_addr destination address
321  * @param dest_len length of @a dest_addr
322  * @return number of bytes sent, #GNUNET_SYSERR on error
323  */
324 ssize_t
325 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
326                               const void *message,
327                               size_t length,
328                               const struct sockaddr *dest_addr,
329                               socklen_t dest_len);
330
331
332 /**
333  * Set socket option
334  *
335  * @param fd socket
336  * @param level protocol level of the option
337  * @param option_name option identifier
338  * @param option_value value to set
339  * @param option_len size of @a option_value
340  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
341  */
342 int
343 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
344                                   int level,
345                                   int option_name,
346                                   const void *option_value,
347                                   socklen_t option_len);
348
349
350 /**
351  * Shut down socket operations
352  *
353  * @param desc socket
354  * @param how type of shutdown
355  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
356  */
357 int
358 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
359                                 int how);
360
361
362 /**
363  * Disable the "CORK" feature for communication with the given socket,
364  * forcing the OS to immediately flush the buffer on transmission
365  * instead of potentially buffering multiple messages.  Essentially
366  * reduces the OS send buffers to zero.
367  *
368  * @param desc socket
369  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
370  */
371 int
372 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc);
373
374
375 /**
376  * Create a new socket.   Configure it for non-blocking IO and
377  * mark it as non-inheritable to child processes (set the
378  * close-on-exec flag).
379  *
380  * @param domain domain of the socket
381  * @param type socket type
382  * @param protocol network protocol
383  * @return new socket, NULL on error
384  */
385 struct GNUNET_NETWORK_Handle *
386 GNUNET_NETWORK_socket_create (int domain,
387                               int type,
388                               int protocol);
389
390
391 /**
392  * Reset FD set (clears all file descriptors).
393  *
394  * @param fds fd set to clear
395  */
396 void
397 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds);
398
399
400 /**
401  * Add a socket to the FD set
402  *
403  * @param fds fd set
404  * @param desc socket to add
405  */
406 void
407 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
408                           const struct GNUNET_NETWORK_Handle *desc);
409
410
411 #if WINDOWS
412 /**
413  * Add a W32 file handle to the fd set
414  *
415  * @param fds fd set
416  * @param h the file handle to add
417  */
418 void
419 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet *fds,
420                                                    HANDLE h);
421 #endif
422
423
424 /**
425  * Check whether a socket is part of the fd set
426  *
427  * @param fds fd set
428  * @param desc socket
429  * @return #GNUNET_YES if the socket is in the set
430  */
431 int
432 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
433                             const struct GNUNET_NETWORK_Handle *desc);
434
435
436 /**
437  * Add one fd set to another (computes the union).
438  *
439  * @param dst the fd set to add to
440  * @param src the fd set to add from
441  */
442 void
443 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
444                           const struct GNUNET_NETWORK_FDSet *src);
445
446
447 /**
448  * Copy one fd set to another
449  *
450  * @param to destination
451  * @param from source
452  */
453 void
454 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
455                            const struct GNUNET_NETWORK_FDSet *from);
456
457
458 /**
459  * Return file descriptor for this network handle
460  *
461  * @param desc wrapper to process
462  * @return POSIX file descriptor
463  */
464 int
465 GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc);
466
467
468 /**
469  * Return the sockaddr for this network handle
470  *
471  * @param desc wrapper to process
472  * @return POSIX file descriptor
473  */
474 struct sockaddr*
475 GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc);
476
477
478 /**
479  * Return sockaddr length for this network handle
480  *
481  * @param desc wrapper to process
482  * @return socklen_t for sockaddr
483  */
484 socklen_t
485 GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc);
486
487
488 /**
489  * Copy a native fd set into the GNUnet representation.
490  *
491  * @param to destination
492  * @param from native source set
493  * @param nfds the biggest socket number in from + 1
494  */
495 void
496 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
497                                   const fd_set *from,
498                                   int nfds);
499
500
501 /**
502  * Set a native fd in a set
503  *
504  * @param to destination
505  * @param nfd native FD to set
506  */
507 void
508 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
509                                  int nfd);
510
511
512 /**
513  * Test native fd in a set
514  *
515  * @param to set to test, NULL for empty set
516  * @param nfd native FD to test, -1 for none
517  * @return #GNUNET_YES if to contains nfd
518  */
519 int
520 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
521                                   int nfd);
522
523
524 /**
525  * Add a file handle to the fd set
526  *
527  * @param fds fd set
528  * @param h the file handle to add
529  */
530 void
531 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
532                                  const struct GNUNET_DISK_FileHandle *h);
533
534
535 /**
536  * Add a file handle to the fd set
537  * On W32: ensure that the handle is first in the array.
538  *
539  * @param fds fd set
540  * @param h the file handle to add
541  */
542 void
543 GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
544                                        const struct GNUNET_DISK_FileHandle *h);
545
546
547 /**
548  * Check if a file handle is part of an fd set
549  *
550  * @param fds fd set
551  * @param h file handle
552  * @return #GNUNET_YES if the file handle is part of the set
553  */
554 int
555 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
556                                    const struct GNUNET_DISK_FileHandle *h);
557
558
559 /**
560  * Checks if two fd sets overlap
561  *
562  * @param fds1 first fd set
563  * @param fds2 second fd set
564  * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
565  */
566 int
567 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
568                               const struct GNUNET_NETWORK_FDSet *fds2);
569
570
571 /**
572  * Creates an fd set
573  *
574  * @return a new fd set
575  */
576 struct GNUNET_NETWORK_FDSet *
577 GNUNET_NETWORK_fdset_create (void);
578
579
580 /**
581  * Releases the associated memory of an fd set
582  *
583  * @param fds fd set
584  */
585 void
586 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
587
588
589 /**
590  * Test if the given @a port is available.
591  *
592  * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
593  * @param port port number to test
594  * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
595  */
596 int
597 GNUNET_NETWORK_test_port_free (int ipproto,
598                                uint16_t port);
599
600
601 #if 0                           /* keep Emacsens' auto-indent happy */
602 {
603 #endif
604 #ifdef __cplusplus
605 }
606 #endif
607
608 #endif /* GNUNET_NETWORK_LIB_H */
609
610 /** @} */  /* end of group */