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