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