iterate topoogy header related stuff
[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 sched scheduler to use
103  * @param osSocket existing socket to box
104  * @return the boxed socket handle
105  */
106 struct GNUNET_CONNECTION_Handle
107   *GNUNET_CONNECTION_create_from_existing (struct
108                                                    GNUNET_SCHEDULER_Handle
109                                                    *sched,
110                                                    struct
111                                                    GNUNET_NETWORK_Handle
112                                                    *osSocket);
113
114
115 /**
116  * Create a socket handle by accepting on a listen socket.  This
117  * function may block if the listen socket has no connection ready.
118  *
119  * @param sched scheduler to use
120  * @param access function to use to check if access is allowed
121  * @param access_cls closure for access
122  * @param lsock listen socket
123  * @return the socket handle, NULL on error (for example, access refused)
124  */
125 struct GNUNET_CONNECTION_Handle
126   *GNUNET_CONNECTION_create_from_accept (struct
127                                                  GNUNET_SCHEDULER_Handle
128                                                  *sched,
129                                                  GNUNET_CONNECTION_AccessCheck
130                                                  access, void *access_cls,
131                                                  struct
132                                                  GNUNET_NETWORK_Handle
133                                                  *lsock);
134
135
136 /**
137  * Create a socket handle by (asynchronously) connecting to a host.
138  * This function returns immediately, even if the connection has not
139  * yet been established.  This function only creates TCP connections.
140  *
141  * @param sched scheduler to use
142  * @param cfg configuration to use
143  * @param hostname name of the host to connect to
144  * @param port port to connect to
145  * @return the socket handle
146  */
147 struct GNUNET_CONNECTION_Handle
148   *GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
149                                           const struct GNUNET_CONFIGURATION_Handle *cfg,
150                                           const char *hostname,
151                                           uint16_t port);
152
153
154 /**
155  * Create a socket handle by connecting to a UNIX domain service.
156  * This function returns immediately, even if the connection has not
157  * yet been established.  This function only creates UNIX connections.
158  *
159  * @param sched scheduler to use
160  * @param cfg configuration to use
161  * @param unixpath path to connect to)
162  * @return the socket handle, NULL on systems without UNIX support
163  */
164 struct GNUNET_CONNECTION_Handle *
165 GNUNET_CONNECTION_create_from_connect_to_unixpath (struct GNUNET_SCHEDULER_Handle *sched,
166                                                    const struct
167                                                    GNUNET_CONFIGURATION_Handle *cfg,
168                                                    const char *unixpath);
169
170
171
172
173 /**
174  * Create a socket handle by (asynchronously) connecting to a host.
175  * This function returns immediately, even if the connection has not
176  * yet been established.  This function only creates TCP connections.
177  *
178  * @param sched scheduler to use
179  * @param af_family address family to use
180  * @param serv_addr server address
181  * @param addrlen length of server address
182  * @return the socket handle
183  */
184 struct GNUNET_CONNECTION_Handle
185   *GNUNET_CONNECTION_create_from_sockaddr (struct
186                                                    GNUNET_SCHEDULER_Handle
187                                                    *sched, int af_family,
188                                                    const struct sockaddr
189                                                    *serv_addr,
190                                                    socklen_t addrlen);
191
192 /**
193  * Check if socket is valid (no fatal errors have happened so far).
194  * Note that a socket that is still trying to connect is considered
195  * valid.
196  *
197  * @param sock socket to check
198  * @return GNUNET_YES if valid, GNUNET_NO otherwise
199  */
200 int GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle
201                                      *sock);
202
203
204 /**
205  * Obtain the network address of the other party.
206  *
207  * @param sock the client to get the address for
208  * @param addr where to store the address
209  * @param addrlen where to store the length of the address
210  * @return GNUNET_OK on success
211  */
212 int GNUNET_CONNECTION_get_address (struct
213                                            GNUNET_CONNECTION_Handle
214                                            *sock, void **addr,
215                                            size_t * addrlen);
216
217
218 /**
219  * Close the socket and free associated resources. Pending
220  * transmissions may be completed or dropped depending on the
221  * arguments.   If a receive call is pending and should 
222  * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
223  * should be called explicitly first.
224  *
225  * @param sock socket to destroy
226  * @param finish_pending_write should pending writes be completed or aborted?
227  *        (this applies to transmissions where the data has already been
228  *        read from the application; all other transmissions should be
229  *        aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
230  */
231 void
232 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
233                            int finish_pending_write);
234
235
236 /**
237  * Receive data from the given socket.  Note that this function will
238  * call "receiver" asynchronously using the scheduler.  It will
239  * "immediately" return.  Note that there MUST only be one active
240  * receive call per socket at any given point in time (so do not
241  * call receive again until the receiver callback has been invoked).
242  *
243  * @param sock socket handle
244  * @param max maximum number of bytes to read
245  * @param timeout maximum amount of time to wait
246  * @param receiver function to call with received data
247  * @param receiver_cls closure for receiver
248  */
249 void
250 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle
251                                    *sock, size_t max,
252                                    struct GNUNET_TIME_Relative timeout,
253                                    GNUNET_CONNECTION_Receiver receiver,
254                                    void *receiver_cls);
255
256
257 /**
258  * Cancel receive job on the given socket.  Note that the
259  * receiver callback must not have been called yet in order
260  * for the cancellation to be valid.
261  *
262  * @param sock socket handle
263  * @return closure of the original receiver callback closure
264  */
265 void *GNUNET_CONNECTION_receive_cancel (struct
266                                         GNUNET_CONNECTION_Handle
267                                         *sock);
268
269
270 /**
271  * Function called to notify a client about the socket
272  * begin ready to queue more data.  "buf" will be
273  * NULL and "size" zero if the socket was closed for
274  * writing in the meantime.
275  *
276  * @param cls closure
277  * @param size number of bytes available in buf
278  * @param buf where the callee should write the message
279  * @return number of bytes written to buf
280  */
281 typedef size_t (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
282                                                          size_t size, void *buf);
283
284
285 /**
286  * Opaque handle that can be used to cancel
287  * a transmit-ready notification.
288  */
289 struct GNUNET_CONNECTION_TransmitHandle;
290
291 /**
292  * Ask the socket to call us once the specified number of bytes
293  * are free in the transmission buffer.  May call the notify
294  * method immediately if enough space is available.  Note that
295  * this function will abort if "size" is greater than
296  * GNUNET_SERVER_MAX_MESSAGE_SIZE.
297  *
298  * Note that "notify" will be called either when enough
299  * buffer space is available OR when the socket is destroyed.
300  * The size parameter given to notify is guaranteed to be
301  * larger or equal to size if the buffer is ready, or zero
302  * if the socket was destroyed (or at least closed for
303  * writing).  Finally, any time before 'notify' is called, a
304  * client may call "notify_transmit_ready_cancel" to cancel
305  * the transmission request.
306  *
307  * Only one transmission request can be scheduled at the same
308  * time.  Notify will be run with the same scheduler priority
309  * as that of the caller.
310  *
311  * @param sock socket
312  * @param size number of bytes to send
313  * @param timeout after how long should we give up (and call
314  *        notify with buf NULL and size 0)?
315  * @param notify function to call when buffer space is available
316  * @param notify_cls closure for notify
317  * @return non-NULL if the notify callback was queued,
318  *         NULL if we are already going to notify someone else (busy)
319  */
320 struct GNUNET_CONNECTION_TransmitHandle
321   *GNUNET_CONNECTION_notify_transmit_ready (struct
322                                             GNUNET_CONNECTION_Handle
323                                             *sock, size_t size,
324                                             struct
325                                             GNUNET_TIME_Relative
326                                             timeout,
327                                             GNUNET_CONNECTION_TransmitReadyNotify
328                                             notify, void *notify_cls);
329
330
331 /**
332  * Cancel the specified transmission-ready
333  * notification.
334  *
335  * @param h handle for notification to cancel
336  */
337 void
338 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
339                                                         GNUNET_CONNECTION_TransmitHandle
340                                                         *h);
341
342
343 /**
344  * Configure this connection to ignore shutdown signals.
345  *
346  * @param sock socket handle
347  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
348  */
349 void
350 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
351                                    int do_ignore);
352
353
354 #if 0                           /* keep Emacsens' auto-indent happy */
355 {
356 #endif
357 #ifdef __cplusplus
358 }
359 #endif
360
361
362 /* ifndef GNUNET_CONNECTION_LIB_H */
363 #endif
364 /* end of gnunet_connection_lib.h */