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