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