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