bc4aeea9b8242b4347a7557e4ece3c8c35a17ddd
[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_NETWORK_LIB_H\r
27 #define GNUNET_NETWORK_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. 5s.\r
44  */\r
45 #define GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)\r
46 \r
47 /**\r
48  * @brief handle for a network connection\r
49  */\r
50 struct GNUNET_NETWORK_ConnectionHandle;\r
51 \r
52 \r
53 /**\r
54  * Function to call for access control checks.\r
55  *\r
56  * @param cls closure\r
57  * @param addr address\r
58  * @param addrlen length of address\r
59  * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR\r
60  *   for unknown address family (will be denied).\r
61  */\r
62 typedef int (*GNUNET_NETWORK_AccessCheck) (void *cls,\r
63                                            const struct sockaddr * addr,\r
64                                            socklen_t addrlen);\r
65 \r
66 \r
67 /**\r
68  * Callback function for data received from the network.  Note that\r
69  * both "available" and "err" would be 0 if the read simply timed out.\r
70  *\r
71  * @param cls closure\r
72  * @param buf pointer to received data\r
73  * @param available number of bytes availabe in "buf",\r
74  *        possibly 0 (on errors)\r
75  * @param addr address of the sender\r
76  * @param addrlen size of addr\r
77  * @param errCode value of errno (on errors receiving)\r
78  */\r
79 typedef void (*GNUNET_NETWORK_Receiver) (void *cls,\r
80                                          const void *buf,\r
81                                          size_t available,\r
82                                          const struct sockaddr * addr,\r
83                                          socklen_t addrlen, int errCode);\r
84 \r
85 \r
86 /**\r
87  * Create a socket handle by boxing an existing OS socket.  The OS\r
88  * socket should henceforth be no longer used directly.\r
89  * GNUNET_socket_destroy will close it.\r
90  *\r
91  * @param sched scheduler to use\r
92  * @param osSocket existing socket to box\r
93  * @param maxbuf maximum write buffer size for the socket (use\r
94  *        0 for sockets that need no write buffers, such as listen sockets)\r
95  * @return the boxed socket handle\r
96  */\r
97 struct GNUNET_NETWORK_ConnectionHandle\r
98   *GNUNET_NETWORK_connection_create_from_existing (struct\r
99                                                    GNUNET_SCHEDULER_Handle\r
100                                                    *sched,\r
101                                                    struct\r
102                                                    GNUNET_NETWORK_Descriptor\r
103                                                    *osSocket, size_t maxbuf);\r
104 \r
105 \r
106 /**\r
107  * Create a socket handle by accepting on a listen socket.  This\r
108  * function may block if the listen socket has no connection ready.\r
109  *\r
110  * @param sched scheduler to use\r
111  * @param access function to use to check if access is allowed\r
112  * @param access_cls closure for access\r
113  * @param lsock listen socket\r
114  * @param maxbuf maximum write buffer size for the socket (use\r
115  *        0 for sockets that need no write buffers, such as listen sockets)\r
116  * @return the socket handle, NULL on error (for example, access refused)\r
117  */\r
118 struct GNUNET_NETWORK_ConnectionHandle\r
119   *GNUNET_NETWORK_connection_create_from_accept (struct\r
120                                                  GNUNET_SCHEDULER_Handle\r
121                                                  *sched,\r
122                                                  GNUNET_NETWORK_AccessCheck\r
123                                                  access, void *access_cls,\r
124                                                  struct\r
125                                                  GNUNET_NETWORK_Descriptor\r
126                                                  *lsock, size_t maxbuf);\r
127 \r
128 \r
129 /**\r
130  * Create a socket handle by (asynchronously) connecting to a host.\r
131  * This function returns immediately, even if the connection has not\r
132  * yet been established.  This function only creates TCP connections.\r
133  *\r
134  * @param sched scheduler to use\r
135  * @param hostname name of the host to connect to\r
136  * @param port port to connect to\r
137  * @param maxbuf maximum write buffer size for the socket (use\r
138  *        0 for sockets that need no write buffers, such as listen sockets)\r
139  * @return the socket handle\r
140  */\r
141 struct GNUNET_NETWORK_ConnectionHandle\r
142   *GNUNET_NETWORK_connection_create_from_connect (struct\r
143                                                   GNUNET_SCHEDULER_Handle\r
144                                                   *sched,\r
145                                                   const char *hostname,\r
146                                                   uint16_t port,\r
147                                                   size_t maxbuf);\r
148 \r
149 \r
150 \r
151 /**\r
152  * Create a socket handle by (asynchronously) connecting to a host.\r
153  * This function returns immediately, even if the connection has not\r
154  * yet been established.  This function only creates TCP connections.\r
155  *\r
156  * @param sched scheduler to use\r
157  * @param af_family address family to use\r
158  * @param serv_addr server address\r
159  * @param addrlen length of server address\r
160  * @param maxbuf maximum write buffer size for the socket (use\r
161  *        0 for sockets that need no write buffers, such as listen sockets)\r
162  * @return the socket handle\r
163  */\r
164 struct GNUNET_NETWORK_ConnectionHandle\r
165   *GNUNET_NETWORK_connection_create_from_sockaddr (struct\r
166                                                    GNUNET_SCHEDULER_Handle\r
167                                                    *sched, int af_family,\r
168                                                    const struct sockaddr\r
169                                                    *serv_addr,\r
170                                                    socklen_t addrlen,\r
171                                                    size_t maxbuf);\r
172 \r
173 /**\r
174  * Check if socket is valid (no fatal errors have happened so far).\r
175  * Note that a socket that is still trying to connect is considered\r
176  * valid.\r
177  *\r
178  * @param sock socket to check\r
179  * @return GNUNET_YES if valid, GNUNET_NO otherwise\r
180  */\r
181 int GNUNET_NETWORK_connection_check (struct GNUNET_NETWORK_ConnectionHandle\r
182                                      *sock);\r
183 \r
184 \r
185 /**\r
186  * Obtain the network address of the other party.\r
187  *\r
188  * @param sock the client to get the address for\r
189  * @param addr where to store the address\r
190  * @param addrlen where to store the length of the address\r
191  * @return GNUNET_OK on success\r
192  */\r
193 int GNUNET_NETWORK_connection_get_address (struct\r
194                                            GNUNET_NETWORK_ConnectionHandle\r
195                                            *sock, void **addr,\r
196                                            size_t * addrlen);\r
197 \r
198 /**\r
199  * Close the socket and free associated resources.  Pending\r
200  * transmissions are simply dropped.  A pending receive call will be\r
201  * called with an error code of "EPIPE".\r
202  *\r
203  * @param sock socket to destroy\r
204  */\r
205 void GNUNET_NETWORK_connection_destroy (struct GNUNET_NETWORK_ConnectionHandle\r
206                                         *sock);\r
207 \r
208 \r
209 /**\r
210  * Receive data from the given socket.  Note that this function will\r
211  * call "receiver" asynchronously using the scheduler.  It will\r
212  * "immediately" return.  Note that there MUST only be one active\r
213  * receive call per socket at any given point in time (so do not\r
214  * call receive again until the receiver callback has been invoked).\r
215  *\r
216  * @param sock socket handle\r
217  * @param max maximum number of bytes to read\r
218  * @param timeout maximum amount of time to wait\r
219  * @param receiver function to call with received data\r
220  * @param receiver_cls closure for receiver\r
221  * @return scheduler task ID used for receiving, GNUNET_SCHEDULER_NO_TASK on error\r
222  */\r
223 GNUNET_SCHEDULER_TaskIdentifier\r
224 GNUNET_NETWORK_connection_receive (struct GNUNET_NETWORK_ConnectionHandle\r
225                                    *sock, size_t max,\r
226                                    struct GNUNET_TIME_Relative timeout,\r
227                                    GNUNET_NETWORK_Receiver receiver,\r
228                                    void *receiver_cls);\r
229 \r
230 \r
231 /**\r
232  * Cancel receive job on the given socket.  Note that the\r
233  * receiver callback must not have been called yet in order\r
234  * for the cancellation to be valid.\r
235  *\r
236  * @param sock socket handle\r
237  * @param task task identifier returned from the receive call\r
238  * @return closure of the original receiver callback\r
239  */\r
240 void *GNUNET_NETWORK_connection_receive_cancel (struct\r
241                                                 GNUNET_NETWORK_ConnectionHandle\r
242                                                 *sock,\r
243                                                 GNUNET_SCHEDULER_TaskIdentifier\r
244                                                 task);\r
245 \r
246 \r
247 /**\r
248  * Function called to notify a client about the socket\r
249  * begin ready to queue more data.  "buf" will be\r
250  * NULL and "size" zero if the socket was closed for\r
251  * writing in the meantime.\r
252  *\r
253  * @param cls closure\r
254  * @param size number of bytes available in buf\r
255  * @param buf where the callee should write the message\r
256  * @return number of bytes written to buf\r
257  */\r
258 typedef size_t (*GNUNET_NETWORK_TransmitReadyNotify) (void *cls,\r
259                                                       size_t size, void *buf);\r
260 \r
261 \r
262 /**\r
263  * Opaque handle that can be used to cancel\r
264  * a transmit-ready notification.\r
265  */\r
266 struct GNUNET_NETWORK_TransmitHandle;\r
267 \r
268 /**\r
269  * Ask the socket to call us once the specified number of bytes\r
270  * are free in the transmission buffer.  May call the notify\r
271  * method immediately if enough space is available.  Note that\r
272  * this function will abort if "size" is greater than\r
273  * "maxbuf" (as specified when the socket handle was created).\r
274  *\r
275  * Note that "notify" will be called either when enough\r
276  * buffer space is available OR when the socket is destroyed.\r
277  * The size parameter given to notify is guaranteed to be\r
278  * larger or equal to size if the buffer is ready, or zero\r
279  * if the socket was destroyed (or at least closed for\r
280  * writing).  Finally, any time before 'notify' is called, a\r
281  * client may call "notify_transmit_ready_cancel" to cancel\r
282  * the transmission request.\r
283  *\r
284  * Only one transmission request can be scheduled at the same\r
285  * time.  Notify will be run with the same scheduler priority\r
286  * as that of the caller.\r
287  *\r
288  * @param sock socket\r
289  * @param size number of bytes to send\r
290  * @param timeout after how long should we give up (and call\r
291  *        notify with buf NULL and size 0)?\r
292  * @param notify function to call when buffer space is available\r
293  * @param notify_cls closure for notify\r
294  * @return non-NULL if the notify callback was queued,\r
295  *         NULL if we are already going to notify someone else (busy)\r
296  */\r
297 struct GNUNET_NETWORK_TransmitHandle\r
298   *GNUNET_NETWORK_connection_notify_transmit_ready (struct\r
299                                                     GNUNET_NETWORK_ConnectionHandle\r
300                                                     *sock, size_t size,\r
301                                                     struct\r
302                                                     GNUNET_TIME_Relative\r
303                                                     timeout,\r
304                                                     GNUNET_NETWORK_TransmitReadyNotify\r
305                                                     notify, void *notify_cls);\r
306 \r
307 \r
308 /**\r
309  * Cancel the specified transmission-ready\r
310  * notification.\r
311  *\r
312  * @param h handle for notification to cancel\r
313  */\r
314 void\r
315 GNUNET_NETWORK_connection_notify_transmit_ready_cancel (struct\r
316                                                         GNUNET_NETWORK_TransmitHandle\r
317                                                         *h);\r
318 \r
319 \r
320 #if 0                           /* keep Emacsens' auto-indent happy */\r
321 {\r
322 #endif\r
323 #ifdef __cplusplus\r
324 }\r
325 #endif\r
326 \r
327 \r
328 /* ifndef GNUNET_NETWORK_LIB_H */\r
329 #endif\r
330 /* end of gnunet_connection_lib.h */\r