various 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 /**
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 *
66 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
67                               struct sockaddr *address,
68                               socklen_t *address_len);
69
70
71 /**
72  * Bind to a connected socket
73  *
74  * @param desc socket to bind
75  * @param address address to be bound
76  * @param address_len length of address
77  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
78  */
79 int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
80                     const struct sockaddr *address, socklen_t address_len);
81
82 /**
83  * Close a socket.
84  *
85  * @param desc socket to close
86  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
87  */
88 int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc);
89
90 /**
91  * Connect a socket
92  *
93  * @param desc socket to connect
94  * @param address peer address
95  * @param address_len of address
96  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
97  */
98 int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
99                                    const struct sockaddr *address, 
100                                    socklen_t address_len);
101
102
103 /**
104  * Get socket options
105  *
106  * @param desc socket to inspect
107  * @param level protocol level of the option
108  * @param optname identifier of the option
109  * @param optval options
110  * @param optlen length of optval
111  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
112  */
113 int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname,
114        void *optval, socklen_t *optlen);
115
116
117 /**
118  * Listen on a socket
119  *
120  * @param desc socket to start listening on
121  * @param backlog length of the listen queue
122  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
123  */
124 int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, int backlog);
125
126 /**
127  * Read data from a connected socket (always non-blocking).
128  *
129  * @param desc socket
130  * @param buffer buffer
131  * @param length length of buffer
132  * @return number of bytes read
133  */
134 ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, void *buffer,
135                                     size_t length);
136
137 /**
138  * Check if sockets meet certain conditions
139  * @param rfds set of sockets to be checked for readability
140  * @param wfds set of sockets to be checked for writability
141  * @param efds set of sockets to be checked for exceptions
142  * @param timeout relative value when to return
143  * @return number of selected sockets, GNUNET_SYSERR on error
144  */
145 int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
146     struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds,
147     struct GNUNET_TIME_Relative timeout);
148
149
150
151 /**
152  * Send data (always non-blocking).
153  *
154  * @param desc socket
155  * @param buffer data to send
156  * @param length size of the buffer
157  * @return number of bytes sent, GNUNET_SYSERR on error
158  */
159 ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
160                         const void *buffer, size_t length);
161
162 /**
163  * Send data to a particular destination (always non-blocking).
164  * This function only works for UDP sockets.
165  *
166  * @param desc socket
167  * @param message data to send
168  * @param length size of the data
169  * @param dest_addr destination address
170  * @param dest_len length of address
171  * @return number of bytes sent, GNUNET_SYSERR on error
172  */
173 ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
174                           const void *message, size_t length, 
175                           const struct sockaddr *dest_addr,
176                           socklen_t dest_len);
177
178 /**
179  * Set socket option
180  * @param fd socket
181  * @param level protocol level of the option
182  * @param option_name option identifier
183  * @param option_value value to set
184  * @param option_len size of option_value
185  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
186  */
187 int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name,
188        const void *option_value, socklen_t option_len);
189
190 /**
191  * Shut down socket operations
192  * @param desc socket
193  * @param how type of shutdown
194  * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
195  */
196 int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how);
197
198
199 /**
200  * Create a new socket.   Configure it for non-blocking IO and
201  * mark it as non-inheritable to child processes (set the
202  * close-on-exec flag).
203  *
204  * @param domain domain of the socket
205  * @param type socket type
206  * @param protocol network protocol
207  * @return new socket, NULL on error
208  */
209 struct GNUNET_NETWORK_Handle *GNUNET_NETWORK_socket_create (int domain, int type, int protocol);
210
211 /**
212  * Reset FD set (clears all file descriptors).
213  *
214  * @param fds fd set to clear
215  */
216 void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds);
217
218 /**
219  * Add a socket to the FD set
220  * @param fds fd set
221  * @param desc socket to add
222  */
223 void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
224                               const struct GNUNET_NETWORK_Handle *desc);
225
226
227 /**
228  * Check whether a socket is part of the fd set
229  * @param fds fd set
230  * @param desc socket
231  */
232 int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
233                                const struct GNUNET_NETWORK_Handle *desc);
234
235 /**
236  * Add one fd set to another
237  * @param dst the fd set to add to
238  * @param src the fd set to add from
239  */
240 void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
241                                const struct GNUNET_NETWORK_FDSet *src);
242
243 /**
244  * Copy one fd set to another
245  * @param to destination
246  * @param from source
247  */
248 void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
249                                const struct GNUNET_NETWORK_FDSet *from);
250
251 /**
252  * Copy a native fd set
253  * @param to destination
254  * @param from native source set
255  * @param nfds the biggest socket number in from + 1
256  */
257 void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, 
258                                        const fd_set *from,
259                                        int nfds);
260
261 /**
262  * Add a file handle to the fd set
263  * @param fds fd set
264  * @param h the file handle to add
265  */
266 void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
267                                       const struct GNUNET_DISK_FileHandle *h);
268
269 /**
270  * Check if a file handle is part of an fd set
271  * @param fds fd set
272  * @param h file handle
273  * @return GNUNET_YES if the file handle is part of the set
274  */
275 int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
276                                        const struct GNUNET_DISK_FileHandle *h);
277
278 /**
279  * Checks if two fd sets overlap
280  * @param fds1 first fd set
281  * @param fds2 second fd set
282  * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
283  */
284 int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, 
285                                   const struct GNUNET_NETWORK_FDSet *fds2);
286
287 /**
288  * Creates an fd set
289  * @return a new fd set
290  */
291 struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (void);
292
293 /**
294  * Releases the associated memory of an fd set
295  * @param fds fd set
296  */
297 void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds);
298
299
300 #if 0                           /* keep Emacsens' auto-indent happy */
301 {
302 #endif
303 #ifdef __cplusplus
304 }
305 #endif
306
307 #endif /* GNUNET_NETWORK_LIB_H */