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