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