fixing error message -- 1635
[oweals/gnunet.git] / src / include / gnunet_connection_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_connection_lib.h
23  * @brief basic, low-level TCP networking interface
24  * @author Christian Grothoff
25  */
26 #ifndef GNUNET_CONNECTION_LIB_H
27 #define GNUNET_CONNECTION_LIB_H
28
29 #ifdef __cplusplus
30 extern "C"
31 {
32 #if 0                           /* keep Emacsens' auto-indent happy */
33 }
34 #endif
35 #endif
36
37 #include "gnunet_network_lib.h"
38 #include "gnunet_scheduler_lib.h"
39 #include "gnunet_time_lib.h"
40
41 /**
42  * Timeout we use on TCP connect before trying another
43  * result from the DNS resolver.  Actual value used
44  * is this value divided by the number of address families.
45  * Default is 5s.
46  */
47 #define GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
48
49 /**
50  * @brief handle for a network connection
51  */
52 struct GNUNET_CONNECTION_Handle;
53
54
55 /**
56  * Function to call for access control checks.
57  *
58  * @param cls closure
59  * @param addr address
60  * @param addrlen length of address
61  * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
62  *   for unknown address family (will be denied).
63  */
64 typedef int (*GNUNET_CONNECTION_AccessCheck) (void *cls,
65                                            const struct sockaddr * addr,
66                                            socklen_t addrlen);
67
68
69 /**
70  * Callback function for data received from the network.  Note that
71  * both "available" and "err" would be 0 if the read simply timed out.
72  *
73  * @param cls closure
74  * @param buf pointer to received data
75  * @param available number of bytes availabe in "buf",
76  *        possibly 0 (on errors)
77  * @param addr address of the sender
78  * @param addrlen size of addr
79  * @param errCode value of errno (on errors receiving)
80  */
81 typedef void (*GNUNET_CONNECTION_Receiver) (void *cls,
82                                          const void *buf,
83                                          size_t available,
84                                          const struct sockaddr * addr,
85                                          socklen_t addrlen, int errCode);
86
87 /**
88  * Set the persist option on this connection handle.  Indicates
89  * that the underlying socket or fd should never really be closed.
90  * Used for indicating process death.
91  *
92  * @param sock the connection to set persistent
93  */
94 void
95 GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock);
96
97 /**
98  * Create a socket handle by boxing an existing OS socket.  The OS
99  * socket should henceforth be no longer used directly.
100  * GNUNET_socket_destroy will close it.
101  *
102  * @param osSocket existing socket to box
103  * @return the boxed socket handle
104  */
105 struct GNUNET_CONNECTION_Handle
106   *GNUNET_CONNECTION_create_from_existing (struct
107                                                    GNUNET_NETWORK_Handle
108                                                    *osSocket);
109
110
111 /**
112  * Create a socket handle by accepting on a listen socket.  This
113  * function may block if the listen socket has no connection ready.
114  *
115  * @param access function to use to check if access is allowed
116  * @param access_cls closure for access
117  * @param lsock listen socket
118  * @return the socket handle, NULL on error (for example, access refused)
119  */
120 struct GNUNET_CONNECTION_Handle
121   *GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck
122                                                  access, void *access_cls,
123                                                  struct
124                                                  GNUNET_NETWORK_Handle
125                                                  *lsock);
126
127
128 /**
129  * Create a socket handle by (asynchronously) connecting to a host.
130  * This function returns immediately, even if the connection has not
131  * yet been established.  This function only creates TCP connections.
132  *
133  * @param cfg configuration to use
134  * @param hostname name of the host to connect to
135  * @param port port to connect to
136  * @return the socket handle
137  */
138 struct GNUNET_CONNECTION_Handle
139   *GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
140                                           const char *hostname,
141                                           uint16_t port);
142
143
144 /**
145  * Create a socket handle by connecting to a UNIX domain service.
146  * This function returns immediately, even if the connection has not
147  * yet been established.  This function only creates UNIX connections.
148  *
149  * @param cfg configuration to use
150  * @param unixpath path to connect to)
151  * @return the socket handle, NULL on systems without UNIX support
152  */
153 struct GNUNET_CONNECTION_Handle *
154 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
155                                                    GNUNET_CONFIGURATION_Handle *cfg,
156                                                    const char *unixpath);
157
158
159
160
161 /**
162  * Create a socket handle by (asynchronously) connecting to a host.
163  * This function returns immediately, even if the connection has not
164  * yet been established.  This function only creates TCP connections.
165  *
166  * @param af_family address family to use
167  * @param serv_addr server address
168  * @param addrlen length of server address
169  * @return the socket handle
170  */
171 struct GNUNET_CONNECTION_Handle
172   *GNUNET_CONNECTION_create_from_sockaddr (int af_family,
173                                                    const struct sockaddr
174                                                    *serv_addr,
175                                                    socklen_t addrlen);
176
177 /**
178  * Check if socket is valid (no fatal errors have happened so far).
179  * Note that a socket that is still trying to connect is considered
180  * valid.
181  *
182  * @param sock socket to check
183  * @return GNUNET_YES if valid, GNUNET_NO otherwise
184  */
185 int GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle
186                                      *sock);
187
188
189 /**
190  * Obtain the network address of the other party.
191  *
192  * @param sock the client to get the address for
193  * @param addr where to store the address
194  * @param addrlen where to store the length of the address
195  * @return GNUNET_OK on success
196  */
197 int GNUNET_CONNECTION_get_address (struct
198                                            GNUNET_CONNECTION_Handle
199                                            *sock, void **addr,
200                                            size_t * addrlen);
201
202
203 /**
204  * Close the socket and free associated resources. Pending
205  * transmissions may be completed or dropped depending on the
206  * arguments.   If a receive call is pending and should 
207  * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
208  * should be called explicitly first.
209  *
210  * @param sock socket to destroy
211  * @param finish_pending_write should pending writes be completed or aborted?
212  *        (this applies to transmissions where the data has already been
213  *        read from the application; all other transmissions should be
214  *        aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
215  */
216 void
217 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
218                            int finish_pending_write);
219
220
221 /**
222  * Receive data from the given socket.  Note that this function will
223  * call "receiver" asynchronously using the scheduler.  It will
224  * "immediately" return.  Note that there MUST only be one active
225  * receive call per socket at any given point in time (so do not
226  * call receive again until the receiver callback has been invoked).
227  *
228  * @param sock socket handle
229  * @param max maximum number of bytes to read
230  * @param timeout maximum amount of time to wait
231  * @param receiver function to call with received data
232  * @param receiver_cls closure for receiver
233  */
234 void
235 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle
236                                    *sock, size_t max,
237                                    struct GNUNET_TIME_Relative timeout,
238                                    GNUNET_CONNECTION_Receiver receiver,
239                                    void *receiver_cls);
240
241
242 /**
243  * Cancel receive job on the given socket.  Note that the
244  * receiver callback must not have been called yet in order
245  * for the cancellation to be valid.
246  *
247  * @param sock socket handle
248  * @return closure of the original receiver callback closure
249  */
250 void *GNUNET_CONNECTION_receive_cancel (struct
251                                         GNUNET_CONNECTION_Handle
252                                         *sock);
253
254
255 /**
256  * Function called to notify a client about the socket
257  * begin ready to queue more data.  "buf" will be
258  * NULL and "size" zero if the socket was closed for
259  * writing in the meantime.
260  *
261  * @param cls closure
262  * @param size number of bytes available in buf
263  * @param buf where the callee should write the message
264  * @return number of bytes written to buf
265  */
266 typedef size_t (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
267                                                          size_t size, void *buf);
268
269
270 /**
271  * Opaque handle that can be used to cancel
272  * a transmit-ready notification.
273  */
274 struct GNUNET_CONNECTION_TransmitHandle;
275
276 /**
277  * Ask the socket to call us once the specified number of bytes
278  * are free in the transmission buffer.  May call the notify
279  * method immediately if enough space is available.  Note that
280  * this function will abort if "size" is greater than
281  * GNUNET_SERVER_MAX_MESSAGE_SIZE.
282  *
283  * Note that "notify" will be called either when enough
284  * buffer space is available OR when the socket is destroyed.
285  * The size parameter given to notify is guaranteed to be
286  * larger or equal to size if the buffer is ready, or zero
287  * if the socket was destroyed (or at least closed for
288  * writing).  Finally, any time before 'notify' is called, a
289  * client may call "notify_transmit_ready_cancel" to cancel
290  * the transmission request.
291  *
292  * Only one transmission request can be scheduled at the same
293  * time.  Notify will be run with the same scheduler priority
294  * as that of the caller.
295  *
296  * @param sock socket
297  * @param size number of bytes to send
298  * @param timeout after how long should we give up (and call
299  *        notify with buf NULL and size 0)?
300  * @param notify function to call when buffer space is available
301  * @param notify_cls closure for notify
302  * @return non-NULL if the notify callback was queued,
303  *         NULL if we are already going to notify someone else (busy)
304  */
305 struct GNUNET_CONNECTION_TransmitHandle
306   *GNUNET_CONNECTION_notify_transmit_ready (struct
307                                             GNUNET_CONNECTION_Handle
308                                             *sock, size_t size,
309                                             struct
310                                             GNUNET_TIME_Relative
311                                             timeout,
312                                             GNUNET_CONNECTION_TransmitReadyNotify
313                                             notify, void *notify_cls);
314
315
316 /**
317  * Cancel the specified transmission-ready
318  * notification.
319  *
320  * @param h handle for notification to cancel
321  */
322 void
323 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
324                                                         GNUNET_CONNECTION_TransmitHandle
325                                                         *h);
326
327
328 /**
329  * Configure this connection to ignore shutdown signals.
330  *
331  * @param sock socket handle
332  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
333  */
334 void
335 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
336                                    int do_ignore);
337
338
339 #if 0                           /* keep Emacsens' auto-indent happy */
340 {
341 #endif
342 #ifdef __cplusplus
343 }
344 #endif
345
346
347 /* ifndef GNUNET_CONNECTION_LIB_H */
348 #endif
349 /* end of gnunet_connection_lib.h */