fix
[oweals/gnunet.git] / src / util / connection.c
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 util/connection.c
23  * @brief  TCP connection management
24  * @author Christian Grothoff
25  *
26  * This code is rather complex.  Only modify it if you
27  * 1) Have a NEW testcase showing that the new code
28  *    is needed and correct
29  * 2) All EXISTING testcases pass with the new code
30  * These rules should apply in general, but for this
31  * module they are VERY, VERY important.
32  */
33
34 #include "platform.h"
35 #include "gnunet_common.h"
36 #include "gnunet_connection_lib.h"
37 #include "gnunet_container_lib.h"
38 #include "gnunet_resolver_service.h"
39 #include "gnunet_scheduler_lib.h"
40 #include "gnunet_server_lib.h"
41
42 #define DEBUG_CONNECTION GNUNET_NO
43
44
45 /**
46  * Possible functions to call after connect failed or succeeded.
47  */
48 enum ConnectContinuations
49 {
50     /**
51      * Call nothing.
52      */
53   COCO_NONE = 0,
54
55     /**
56      * Call "receive_again".
57      */
58   COCO_RECEIVE_AGAIN = 1,
59
60     /**
61      * Call "transmit_ready".
62      */
63   COCO_TRANSMIT_READY = 2,
64
65     /**
66      * Call "destroy_continuation".
67      */
68   COCO_DESTROY_CONTINUATION = 4
69 };
70
71
72 /**
73  * Transmission handle.  There can only be one for each connection.
74  */
75 struct GNUNET_CONNECTION_TransmitHandle
76 {
77
78   /**
79    * Function to call if the send buffer has notify_size
80    * bytes available.
81    */
82   GNUNET_CONNECTION_TransmitReadyNotify notify_ready;
83
84   /**
85    * Closure for notify_ready.
86    */
87   void *notify_ready_cls;
88
89   /**
90    * Our socket handle.
91    */
92   struct GNUNET_CONNECTION_Handle *sh;
93
94   /**
95    * Timeout for receiving (in absolute time).
96    */
97   struct GNUNET_TIME_Absolute transmit_timeout;
98
99   /**
100    * Task called on timeout.
101    */
102   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
103
104   /**
105    * At what number of bytes available in the
106    * write buffer should the notify method be called?
107    */
108   size_t notify_size;
109
110 };
111
112
113 /**
114  * During connect, we try multiple possible IP addresses
115  * to find out which one might work. 
116  */
117 struct AddressProbe
118 {
119
120   /**
121    * This is a linked list.
122    */
123   struct AddressProbe *next;
124
125   /**
126    * This is a doubly-linked list.
127    */
128   struct AddressProbe *prev;
129
130   /**
131    * The address; do not free (allocated at the end of this struct).
132    */
133   const struct sockaddr *addr;
134
135   /**
136    * Underlying OS's socket.
137    */
138   struct GNUNET_NETWORK_Handle *sock;
139
140   /**
141    * Connection for which we are probing.
142    */
143   struct GNUNET_CONNECTION_Handle *h;
144
145   /**
146    * Lenth of addr.
147    */
148   socklen_t addrlen;
149
150   /**
151    * Task waiting for the socket to finish connecting.
152    */
153   GNUNET_SCHEDULER_TaskIdentifier task;
154 };
155
156
157 /**
158  * @brief handle for a network socket
159  */
160 struct GNUNET_CONNECTION_Handle
161 {
162
163   /**
164    * Scheduler that was used for the connect task.
165    */
166   struct GNUNET_SCHEDULER_Handle *sched;
167
168   /**
169    * Configuration to use.
170    */
171   const struct GNUNET_CONFIGURATION_Handle *cfg;
172
173   /**
174    * Linked list of sockets we are currently trying out 
175    * (during connect).
176    */
177   struct AddressProbe *ap_head;
178
179   /**
180    * Linked list of sockets we are currently trying out 
181    * (during connect).
182    */
183   struct AddressProbe *ap_tail;
184
185   /**
186    * Network address of the other end-point, may be NULL.
187    */
188   struct sockaddr *addr;
189
190   /**
191    * Pointer to the hostname if socket was
192    * created using DNS lookup, otherwise NULL.
193    */
194   char *hostname;
195
196   /**
197    * Underlying OS's socket, set to NULL after fatal errors.
198    */
199   struct GNUNET_NETWORK_Handle *sock;
200
201   /**
202    * Function to call on data received, NULL if no receive is pending.
203    */
204   GNUNET_CONNECTION_Receiver receiver;
205
206   /**
207    * Closure for receiver.
208    */
209   void *receiver_cls;
210
211   /**
212    * Pointer to our write buffer.
213    */
214   char *write_buffer;
215
216   /**
217    * Current size of our write buffer.
218    */
219   size_t write_buffer_size;
220
221   /**
222    * Current write-offset in write buffer (where
223    * would we write next).
224    */
225   size_t write_buffer_off;
226
227   /**
228    * Current read-offset in write buffer (how many
229    * bytes have already been sent).
230    */
231   size_t write_buffer_pos;
232
233   /**
234    * Length of addr.
235    */
236   socklen_t addrlen;
237
238   /**
239    * Read task that we may need to wait for.
240    */
241   GNUNET_SCHEDULER_TaskIdentifier read_task;
242
243   /**
244    * Write task that we may need to wait for.
245    */
246   GNUNET_SCHEDULER_TaskIdentifier write_task;
247
248   /**
249    * Destroy task (if already scheduled).
250    */
251   GNUNET_SCHEDULER_TaskIdentifier destroy_task;
252
253   /**
254    * Handle to a pending DNS lookup request.
255    */
256   struct GNUNET_RESOLVER_RequestHandle *dns_active;
257
258   /**
259    * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
260    */
261   struct GNUNET_CONNECTION_TransmitHandle nth;
262
263   /**
264    * Timeout for receiving (in absolute time).
265    */
266   struct GNUNET_TIME_Absolute receive_timeout;
267
268   /**
269    * Functions to call after connect failed or succeeded.
270    */
271   enum ConnectContinuations ccs;
272
273   /**
274    * Maximum number of bytes to read (for receiving).
275    */
276   size_t max;
277
278   /**
279    * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
280    */
281   int ignore_shutdown;
282
283   /**
284    * Port to connect to.
285    */
286   uint16_t port;
287
288   /**
289    * When shutdown, do not ever actually close the socket, but
290    * free resources.  Only should ever be set if using program
291    * termination as a signal (because only then will the leaked
292    * socket be freed!)
293    */
294   int16_t persist;
295
296 };
297
298 /**
299  * Set the persist option on this connection handle.  Indicates
300  * that the underlying socket or fd should never really be closed.
301  * Used for indicating process death.
302  *
303  * @param sock the connection to set persistent
304  */
305 void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock)
306 {
307   sock->persist = GNUNET_YES;
308 }
309
310 /**
311  * Create a socket handle by boxing an existing OS socket.  The OS
312  * socket should henceforth be no longer used directly.
313  * GNUNET_socket_destroy will close it.
314  *
315  * @param sched scheduler to use
316  * @param osSocket existing socket to box
317  * @return the boxed socket handle
318  */
319 struct GNUNET_CONNECTION_Handle *
320 GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
321                                         *sched,
322                                         struct GNUNET_NETWORK_Handle
323                                         *osSocket)
324 {
325   struct GNUNET_CONNECTION_Handle *ret;
326   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
327   ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
328   ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
329   ret->sock = osSocket;
330   ret->sched = sched;
331   return ret;
332 }
333
334
335 /**
336  * Create a socket handle by accepting on a listen socket.  This
337  * function may block if the listen socket has no connection ready.
338  *
339  * @param sched scheduler to use
340  * @param access function to use to check if access is allowed
341  * @param access_cls closure for access
342  * @param lsock listen socket
343  * @return the socket handle, NULL on error
344  */
345 struct GNUNET_CONNECTION_Handle *
346 GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
347                                       *sched,
348                                       GNUNET_CONNECTION_AccessCheck access,
349                                       void *access_cls,
350                                       struct GNUNET_NETWORK_Handle *lsock)
351 {
352   struct GNUNET_CONNECTION_Handle *ret;
353   char addr[128];
354   socklen_t addrlen;
355   struct GNUNET_NETWORK_Handle *sock;
356   int aret;
357   struct sockaddr_in *v4;
358   struct sockaddr_in6 *v6;
359   struct sockaddr *sa;
360   void *uaddr;
361
362   addrlen = sizeof (addr);
363   sock =
364     GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
365   if (NULL == sock)
366     {
367       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
368       return NULL;
369     }
370   if ( (addrlen > sizeof (addr)) ||
371        (addrlen < sizeof (sa_family_t)) )
372     {
373       GNUNET_break (0);
374       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
375       return NULL;
376     }
377
378   sa = (struct sockaddr *) addr;
379   v6 = (struct sockaddr_in6 *) addr;
380   if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
381     {
382       /* convert to V4 address */
383       v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
384       memset (v4, 0, sizeof (struct sockaddr_in));
385       v4->sin_family = AF_INET;
386       memcpy (&v4->sin_addr,
387               &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
388                                          sizeof (struct in_addr)],
389               sizeof (struct in_addr));
390       v4->sin_port = v6->sin6_port;
391       uaddr = v4;
392       addrlen = sizeof (struct sockaddr_in);
393     }
394   else
395     {
396       uaddr = GNUNET_malloc (addrlen);
397       memcpy (uaddr, addr, addrlen);
398     }
399
400   if ((access != NULL) &&
401       (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
402     {
403       if (aret == GNUNET_NO)
404         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
405                     _("Access denied to `%s'\n"),
406                     GNUNET_a2s (uaddr, addrlen));
407       GNUNET_break (GNUNET_OK ==
408                     GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
409       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
410       GNUNET_free (uaddr);
411       return NULL;
412     }
413   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
414   ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
415   ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
416   ret->addr = uaddr;
417   ret->addrlen = addrlen;
418   ret->sock = sock;
419   ret->sched = sched;
420 #if DEBUG_CONNECTION
421   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
422               _("Accepting connection from `%s': %p\n"),
423               GNUNET_a2s (uaddr, addrlen), ret);
424 #endif
425   return ret;
426 }
427
428 /**
429  * Obtain the network address of the other party.
430  *
431  * @param sock the client to get the address for
432  * @param addr where to store the address
433  * @param addrlen where to store the length of the address
434  * @return GNUNET_OK on success
435  */
436 int
437 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
438                                void **addr, size_t * addrlen)
439 {
440   if ((sock->addr == NULL) || (sock->addrlen == 0))
441     return GNUNET_NO;
442   *addr = GNUNET_malloc (sock->addrlen);
443   memcpy (*addr, sock->addr, sock->addrlen);
444   *addrlen = sock->addrlen;
445   return GNUNET_OK;
446 }
447
448
449 /**
450  * This function is called after establishing a connection either has
451  * succeeded or timed out.  Note that it is possible that the attempt
452  * timed out and that we're immediately retrying.  If we are retrying,
453  * we need to wait again (or timeout); if we succeeded, we need to
454  * wait for data (or timeout).
455  *
456  * @param cls our connection handle
457  * @param tc task context describing why we are here
458  */
459 static void
460 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
461
462
463 /**
464  * Scheduler let us know that the connect task is finished (or was
465  * cancelled due to shutdown).  Now really clean up.
466  *
467  * @param cls our "struct GNUNET_CONNECTION_Handle *"
468  * @param tc unused
469  */
470 static void
471 destroy_continuation (void *cls,
472                       const struct GNUNET_SCHEDULER_TaskContext *tc)
473 {
474   struct GNUNET_CONNECTION_Handle *sock = cls;
475   GNUNET_CONNECTION_TransmitReadyNotify notify;
476   struct AddressProbe *pos;
477   
478   sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
479   GNUNET_assert (sock->dns_active == NULL);
480   if (0 != (sock->ccs & COCO_TRANSMIT_READY))
481     {
482 #if DEBUG_CONNECTION
483       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484                   "Destroy waits for CCS-TR to be done (%p)\n", sock);
485 #endif
486       sock->ccs |= COCO_DESTROY_CONTINUATION;
487       return;
488     }
489   if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
490     {
491 #if DEBUG_CONNECTION
492       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493                   "Destroy waits for write_task to be done (%p)\n", sock);
494 #endif
495       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
496       sock->destroy_task 
497         = GNUNET_SCHEDULER_add_after (sock->sched,
498                                       sock->write_task,
499                                       &destroy_continuation, sock);
500       return;
501     }
502   if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
503     {
504       sock->ccs |= COCO_DESTROY_CONTINUATION;
505       return;
506     }
507   if (sock->sock != NULL)
508     {
509 #if DEBUG_CONNECTION
510       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511                   "Shutting down socket (%p)\n", sock);
512 #endif
513       if (sock->persist != GNUNET_YES)
514         GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
515     }
516   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
517     {
518       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
519       sock->destroy_task 
520         = GNUNET_SCHEDULER_add_after (sock->sched,
521                                       sock->read_task,
522                                       &destroy_continuation, sock);
523       return;
524     }
525 #if DEBUG_CONNECTION
526   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
527               "Destroy actually runs (%p)!\n", sock);
528 #endif
529   while (NULL != (pos = sock->ap_head))
530     {
531       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
532       GNUNET_SCHEDULER_cancel (sock->sched, pos->task);
533       GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
534       GNUNET_free (pos);
535     }
536   GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
537   GNUNET_assert (sock->ccs == COCO_NONE);
538   if (NULL != (notify = sock->nth.notify_ready))
539     {
540       sock->nth.notify_ready = NULL;
541       notify (sock->nth.notify_ready_cls, 0, NULL);
542     }
543
544   if (sock->sock != NULL) 
545     {
546       if (sock->persist != GNUNET_YES)
547         GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
548       else
549         GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
550                                      leak the socket in this special case) ... */
551     }
552   GNUNET_free_non_null (sock->addr);
553   GNUNET_free_non_null (sock->hostname);
554   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
555 #if DEBUG_CONNECTION
556   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557               "Freeing memory of connection %p.\n", sock);
558 #endif
559   GNUNET_free (sock->write_buffer);
560   GNUNET_free (sock);
561 }
562
563
564
565 /**
566  * See if we are now connected.  If not, wait longer for
567  * connect to succeed.  If connected, we should be able
568  * to write now as well, unless we timed out.
569  *
570  * @param cls our connection handle
571  * @param tc task context describing why we are here
572  */
573 static void
574 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
575
576
577 /**
578  * We've failed for good to establish a connection.
579  *
580  * @param h the connection we tried to establish
581  */
582 static void
583 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
584 {
585 #if DEBUG_CONNECTION
586   GNUNET_log ((0 != strncmp (h->hostname, 
587                              "localhost:",
588                              10)) 
589               ? GNUNET_ERROR_TYPE_INFO 
590               : GNUNET_ERROR_TYPE_WARNING,
591               _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
592               h->hostname, h->port);
593 #endif
594   /* connect failed / timed out */
595   GNUNET_break (h->ap_head == NULL);
596   GNUNET_break (h->ap_tail == NULL);
597   GNUNET_break (h->dns_active == GNUNET_NO);
598   GNUNET_break (h->sock == NULL);
599
600   /* trigger jobs that used to wait on "connect_task" */
601   if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
602     {
603 #if DEBUG_CONNECTION
604       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605                   "connect_fail_continuation triggers receive_again (%p)\n",
606                   h);
607 #endif
608       h->ccs -= COCO_RECEIVE_AGAIN;
609       h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
610                                                  GNUNET_SCHEDULER_NO_TASK,
611                                                  &receive_again, h);
612     }
613   if (0 != (h->ccs & COCO_TRANSMIT_READY))
614     {
615 #if DEBUG_CONNECTION
616       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617                   "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
618                   h);
619 #endif
620       GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
621       GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
622       h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
623       h->ccs -= COCO_TRANSMIT_READY;
624       GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
625       h->write_task = GNUNET_SCHEDULER_add_after (h->sched,
626                                                   GNUNET_SCHEDULER_NO_TASK,
627                                                   &transmit_ready, h);
628     }
629   if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
630     {
631 #if DEBUG_CONNECTION
632       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633                   "connect_fail_continuation runs destroy_continuation (%p)\n",
634                   h);
635 #endif
636       h->ccs -= COCO_DESTROY_CONTINUATION;
637       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
638       h->destroy_task
639         = GNUNET_SCHEDULER_add_now (h->sched,
640                                     &destroy_continuation,
641                                     h);
642     }
643 }
644
645
646 /**
647  * We've succeeded in establishing a connection.
648  *
649  * @param h the connection we tried to establish
650  */
651 static void
652 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
653 {
654 #if DEBUG_CONNECTION
655   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656               "Connection to `%s' succeeded! (%p)\n",
657               GNUNET_a2s (h->addr, h->addrlen), h);
658 #endif
659   /* trigger jobs that waited for the connection */
660   if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
661     {
662 #if DEBUG_CONNECTION
663       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664                   "connect_success_continuation runs receive_again (%p)\n",
665                   h);
666 #endif
667       h->ccs -= COCO_RECEIVE_AGAIN;
668       h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
669                                                  GNUNET_SCHEDULER_NO_TASK,
670                                                  &receive_again, h);
671     }
672   if (0 != (h->ccs & COCO_TRANSMIT_READY))
673     {
674 #if DEBUG_CONNECTION
675       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676                   "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
677                   h);
678 #endif
679       GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
680       GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
681       h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
682       h->ccs -= COCO_TRANSMIT_READY;
683       GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
684       h->write_task =
685         GNUNET_SCHEDULER_add_write_net (h->sched,
686                                         GNUNET_TIME_absolute_get_remaining
687                                         (h->nth.transmit_timeout), h->sock,
688                                         &transmit_ready, h);
689     }
690   if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
691     {
692 #if DEBUG_CONNECTION
693       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694                   "connect_success_continuation runs destroy_continuation (%p)\n",
695                   h);
696 #endif
697       h->ccs -= COCO_DESTROY_CONTINUATION;
698       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
699       h->destroy_task
700         = GNUNET_SCHEDULER_add_now (h->sched,
701                                     &destroy_continuation,
702                                     h);
703     }
704 }
705
706
707 /**
708  * Scheduler let us know that we're either ready to write on the
709  * socket OR connect timed out.  Do the right thing.
710  *
711  * @param cls the "struct AddressProbe*" with the address that we are probing
712  * @param tc success or failure info about the connect attempt.
713  */
714 static void
715 connect_probe_continuation (void *cls,
716                             const struct GNUNET_SCHEDULER_TaskContext *tc)
717 {
718   struct AddressProbe *ap = cls;
719   struct GNUNET_CONNECTION_Handle *h = ap->h;
720   struct AddressProbe *pos;
721   int error;
722   unsigned int len;
723
724   GNUNET_assert (ap->sock != NULL);
725   GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
726   len = sizeof (error);
727   errno = 0;
728   error = 0;
729   if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
730        (GNUNET_OK !=
731         GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
732                                           &error, &len)) ||
733        (error != 0) )
734     {
735       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
736       GNUNET_free (ap);
737       if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
738         connect_fail_continuation (h);
739       return;
740     }
741   GNUNET_assert (h->sock == NULL);
742   h->sock = ap->sock;
743   GNUNET_assert (h->addr == NULL);
744   h->addr = GNUNET_malloc (ap->addrlen);
745   memcpy (h->addr, ap->addr, ap->addrlen);
746   h->addrlen = ap->addrlen;
747   GNUNET_free (ap);
748   /* cancel all other attempts */
749   while (NULL != (pos = h->ap_head))
750     {
751       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
752       GNUNET_SCHEDULER_cancel (h->sched, pos->task);
753       GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
754       GNUNET_free (pos);
755     }
756   connect_success_continuation (h);
757 }
758
759
760 /**
761  * Try to establish a socket connection given the specified address.
762  * This function is called by the resolver once we have a DNS reply.
763  *
764  * @param cls our "struct GNUNET_CONNECTION_Handle *"
765  * @param addr address to try, NULL for "last call"
766  * @param addrlen length of addr
767  */
768 static void
769 try_connect_using_address (void *cls,
770                            const struct sockaddr *addr, socklen_t addrlen)
771 {
772   struct GNUNET_CONNECTION_Handle *h = cls;
773   struct AddressProbe *ap;
774   struct GNUNET_TIME_Relative delay;
775
776   if (addr == NULL)
777     {
778       h->dns_active = NULL;
779       if (NULL == h->ap_head)
780         connect_fail_continuation (h);
781       return;
782     }
783   if (h->sock != NULL)
784     return;                     /* already connected */
785   GNUNET_assert (h->addr == NULL);
786   /* try to connect */
787 #if DEBUG_CONNECTION
788   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789               "Trying to connect using address `%s:%u/%s:%u'\n",
790               h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
791 #endif
792   ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
793   ap->addr = (const struct sockaddr *) &ap[1];
794   memcpy (&ap[1], addr, addrlen);
795   ap->addrlen = addrlen;
796   ap->h = h;
797
798   switch (ap->addr->sa_family)
799     {
800     case AF_INET:
801       ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
802       break;
803     case AF_INET6:
804       ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
805       break;
806     default:
807       GNUNET_break (0);
808       GNUNET_free (ap);
809       return;                   /* not supported by us */
810     }
811   ap->sock =
812     GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
813   if (ap->sock == NULL)
814     {
815       GNUNET_free (ap);
816       return;                   /* not supported by OS */
817     }
818 #if DEBUG_CONNECTION
819   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
820               _("Trying to connect to `%s' (%p)\n"),
821               GNUNET_a2s (ap->addr, ap->addrlen), h);
822 #endif
823   if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
824                                                    ap->addr,
825                                                    ap->addrlen)) &&
826       (errno != EINPROGRESS))
827     {
828       /* maybe refused / unsupported address, try next */
829       GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
830       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
831       GNUNET_free (ap);
832       return;
833     }
834   GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
835   delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
836   if (h->nth.notify_ready != NULL)
837     delay = GNUNET_TIME_relative_min (delay,
838                                       GNUNET_TIME_absolute_get_remaining
839                                       (h->nth.transmit_timeout));
840   if (h->receiver != NULL)
841     delay = GNUNET_TIME_relative_min (delay,
842                                       GNUNET_TIME_absolute_get_remaining
843                                       (h->receive_timeout));
844   ap->task =
845     GNUNET_SCHEDULER_add_write_net (h->sched, delay, ap->sock,
846                                     &connect_probe_continuation, ap);
847 }
848
849
850 /**
851  * Create a socket handle by (asynchronously) connecting to a host.
852  * This function returns immediately, even if the connection has not
853  * yet been established.  This function only creates TCP connections.
854  *
855  * @param sched scheduler to use
856  * @param cfg configuration to use
857  * @param hostname name of the host to connect to
858  * @param port port to connect to
859  * @return the socket handle
860  */
861 struct GNUNET_CONNECTION_Handle *
862 GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
863                                        const struct
864                                        GNUNET_CONFIGURATION_Handle *cfg,
865                                        const char *hostname, uint16_t port)
866 {
867   struct GNUNET_CONNECTION_Handle *ret;
868
869   GNUNET_assert (0 < strlen (hostname));        /* sanity check */
870   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
871   ret->cfg = cfg;
872   ret->sched = sched;
873   ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
874   ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
875   ret->port = port;
876   ret->hostname = GNUNET_strdup (hostname);
877   ret->dns_active = GNUNET_RESOLVER_ip_get (sched,
878                                             cfg,
879                                             ret->hostname,
880                                             AF_UNSPEC,
881                                             GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
882                                             &try_connect_using_address, ret);
883   return ret;
884 }
885
886
887 /**
888  * Create a socket handle by connecting to a UNIX domain service.
889  * This function returns immediately, even if the connection has not
890  * yet been established.  This function only creates UNIX connections.
891  *
892  * @param sched scheduler to use
893  * @param cfg configuration to use
894  * @param unixpath path to connect to
895  * @return the socket handle, NULL on systems without UNIX support
896  */
897 struct GNUNET_CONNECTION_Handle *
898 GNUNET_CONNECTION_create_from_connect_to_unixpath (struct GNUNET_SCHEDULER_Handle *sched,
899                                                    const struct
900                                                    GNUNET_CONFIGURATION_Handle *cfg,
901                                                    const char *unixpath)
902 {
903 #ifdef AF_UNIX
904   struct GNUNET_CONNECTION_Handle *ret;
905   struct sockaddr_un *un;
906   size_t slen;
907
908   GNUNET_assert (0 < strlen (unixpath));        /* sanity check */
909   un = GNUNET_malloc (sizeof (struct sockaddr_un));
910   un->sun_family = AF_UNIX;
911   slen = strlen (unixpath) + 1;
912   if (slen >= sizeof (un->sun_path))
913     slen = sizeof (un->sun_path) - 1;
914   memcpy (un->sun_path,
915           unixpath,
916           slen);
917   un->sun_path[slen] = '\0';
918   slen += sizeof (sa_family_t);
919 #if LINUX
920   un->sun_path[0] = '\0';
921   slen = sizeof (struct sockaddr_un);
922 #endif
923   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
924   ret->cfg = cfg;
925   ret->sched = sched;
926   ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
927   ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
928   ret->port = 0;
929   ret->hostname = NULL;
930   ret->addr = (struct sockaddr*) un;
931   ret->addrlen = slen;
932   ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
933   if (NULL == ret->sock)
934     {
935       GNUNET_free (ret->addr);
936       GNUNET_free (ret->write_buffer);
937       GNUNET_free (ret);
938       return NULL;
939     }
940   if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
941                                                   ret->addr,
942                                                   ret->addrlen)) 
943     {
944       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
945       GNUNET_free (ret->addr);
946       GNUNET_free (ret->write_buffer);
947       GNUNET_free (ret);
948       return NULL;
949     }
950   connect_success_continuation (ret);
951   return ret;
952 #else
953   return NULL;
954 #endif
955 }
956
957
958 /**
959  * Create a socket handle by (asynchronously) connecting to a host.
960  * This function returns immediately, even if the connection has not
961  * yet been established.  This function only creates TCP connections.
962  *
963  * @param sched scheduler to use
964  * @param af_family address family to use
965  * @param serv_addr server address
966  * @param addrlen length of server address
967  * @return the socket handle
968  */
969 struct GNUNET_CONNECTION_Handle *
970 GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
971                                         *sched, int af_family,
972                                         const struct sockaddr *serv_addr,
973                                         socklen_t addrlen)
974 {
975   struct GNUNET_NETWORK_Handle *s;
976   struct GNUNET_CONNECTION_Handle *ret;
977
978   s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
979   if (s == NULL)
980     {
981       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
982                            GNUNET_ERROR_TYPE_BULK, "socket");
983       return NULL;
984     }
985   if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
986       && (errno != EINPROGRESS))
987     {
988       /* maybe refused / unsupported address, try next */
989       GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
990       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
991       return NULL;
992     }
993   ret = GNUNET_CONNECTION_create_from_existing (sched, s);
994   ret->addr = GNUNET_malloc (addrlen);
995   memcpy (ret->addr, serv_addr, addrlen);
996   ret->addrlen = addrlen;
997 #if DEBUG_CONNECTION
998   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
999               _("Trying to connect to `%s' (%p)\n"),
1000               GNUNET_a2s (serv_addr, addrlen), ret);
1001 #endif
1002   return ret;
1003 }
1004
1005
1006 /**
1007  * Check if socket is valid (no fatal errors have happened so far).
1008  * Note that a socket that is still trying to connect is considered
1009  * valid.
1010  *
1011  * @param sock socket to check
1012  * @return GNUNET_YES if valid, GNUNET_NO otherwise
1013  */
1014 int
1015 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1016 {
1017   if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1018     return GNUNET_YES;          /* still trying to connect */
1019   return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1020 }
1021
1022
1023 /**
1024  * Close the socket and free associated resources. Pending
1025  * transmissions may be completed or dropped depending on the
1026  * arguments.   If a receive call is pending and should 
1027  * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1028  * should be called explicitly first.
1029  *
1030  * @param sock socket to destroy
1031  * @param finish_pending_write should pending writes be completed or aborted?
1032  *        (this applies to transmissions where the data has already been
1033  *        read from the application; all other transmissions should be
1034  *        aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1035  */
1036 void
1037 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1038                            int finish_pending_write)
1039 {
1040   if (GNUNET_NO == finish_pending_write)
1041     {
1042       if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1043         {
1044           GNUNET_SCHEDULER_cancel (sock->sched,
1045                                    sock->write_task);
1046           sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1047           sock->write_buffer_off = 0;
1048         }
1049     }
1050   if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1051     {
1052       GNUNET_RESOLVER_request_cancel (sock->dns_active);
1053       sock->dns_active = NULL;
1054     }
1055   GNUNET_assert (sock->sched != NULL);
1056   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1057   sock->destroy_task 
1058     = GNUNET_SCHEDULER_add_now (sock->sched,
1059                                 &destroy_continuation, sock);
1060 }
1061
1062
1063 /**
1064  * Tell the receiver callback that a timeout was reached.
1065  */
1066 static void
1067 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1068 {
1069   GNUNET_CONNECTION_Receiver receiver;
1070
1071 #if DEBUG_CONNECTION
1072   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073               "Network signals time out to receiver (%p)!\n", sh);
1074 #endif
1075   GNUNET_assert (NULL != (receiver = sh->receiver));
1076   sh->receiver = NULL;
1077   receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1078 }
1079
1080
1081 /**
1082  * Tell the receiver callback that we had an IO error.
1083  */
1084 static void
1085 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1086 {
1087   GNUNET_CONNECTION_Receiver receiver;
1088   GNUNET_assert (NULL != (receiver = sh->receiver));
1089   sh->receiver = NULL;
1090   receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1091 }
1092
1093
1094 /**
1095  * This function is called once we either timeout
1096  * or have data ready to read.
1097  */
1098 static void
1099 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1100 {
1101   struct GNUNET_CONNECTION_Handle *sh = cls;
1102   struct GNUNET_TIME_Absolute now;
1103   char buffer[sh->max];
1104   ssize_t ret;
1105   GNUNET_CONNECTION_Receiver receiver;
1106
1107   sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1108   if ( (GNUNET_YES == sh->ignore_shutdown) &&
1109        (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 
1110     {
1111       /* ignore shutdown request, go again immediately */
1112 #if DEBUG_CONNECTION
1113       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114                   "Ignoring shutdown signal per configuration\n");
1115 #endif
1116       sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1117                                                      GNUNET_TIME_absolute_get_remaining
1118                                                      (sh->receive_timeout),
1119                                                      sh->sock,
1120                                                      &receive_ready, sh);
1121       return;
1122     }
1123   now = GNUNET_TIME_absolute_get ();
1124   if ((now.value > sh->receive_timeout.value) ||
1125       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1126       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1127     {
1128 #if DEBUG_CONNECTION
1129       if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1130         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131                     "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1132                     GNUNET_a2s (sh->addr, sh->addrlen),
1133                     GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1134                     value, sh);
1135 #endif
1136       signal_timeout (sh);
1137       return;
1138     }
1139   if (sh->sock == NULL)
1140     {
1141       /* connect failed for good */
1142 #if DEBUG_CONNECTION
1143       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144                   "Receive encounters error, socket closed... (%p)\n", sh);
1145 #endif
1146       signal_error (sh, ECONNREFUSED);
1147       return;
1148     }
1149   GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1150 RETRY:
1151   ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1152   if (ret == -1)
1153     {
1154       if (errno == EINTR)
1155         goto RETRY;
1156 #if DEBUG_CONNECTION
1157       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158                   "Error receiving: %s\n", STRERROR (errno));
1159 #endif
1160       signal_error (sh, errno);
1161       return;
1162     }
1163 #if DEBUG_CONNECTION
1164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165               "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1166               (unsigned int) ret,
1167               sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1168 #endif
1169   GNUNET_assert (NULL != (receiver = sh->receiver));
1170   sh->receiver = NULL;
1171   receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1172 }
1173
1174
1175 /**
1176  * This function is called after establishing a connection either has
1177  * succeeded or timed out.  Note that it is possible that the attempt
1178  * timed out and that we're immediately retrying.  If we are retrying,
1179  * we need to wait again (or timeout); if we succeeded, we need to
1180  * wait for data (or timeout).
1181  *
1182  * @param cls our connection handle
1183  * @param tc task context describing why we are here
1184  */
1185 static void
1186 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1187 {
1188   struct GNUNET_CONNECTION_Handle *sh = cls;
1189   struct GNUNET_TIME_Absolute now;
1190
1191   sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1192   if (sh->sock == NULL)
1193     {
1194       /* not connected and no longer trying */
1195 #if DEBUG_CONNECTION
1196       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197                   "Receive encounters error, socket closed (%p)...\n", sh);
1198 #endif
1199       signal_error (sh, ECONNREFUSED);
1200       return;
1201     }
1202   now = GNUNET_TIME_absolute_get ();
1203   if ((now.value > sh->receive_timeout.value) ||
1204       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1205     {
1206 #if DEBUG_CONNECTION
1207       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208                   "Receive encounters error: time out (%p)...\n", sh);
1209 #endif
1210       signal_timeout (sh);
1211       return;
1212     }
1213   GNUNET_assert (sh->sock != NULL);
1214   /* connect succeeded, wait for data! */
1215   sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1216                                                  GNUNET_TIME_absolute_get_remaining
1217                                                  (sh->receive_timeout),
1218                                                  sh->sock,
1219                                                  &receive_ready, sh);
1220 }
1221
1222
1223 /**
1224  * Receive data from the given socket.  Note that this function will
1225  * call "receiver" asynchronously using the scheduler.  It will
1226  * "immediately" return.  Note that there MUST only be one active
1227  * receive call per socket at any given point in time (so do not
1228  * call receive again until the receiver callback has been invoked).
1229  *
1230  * @param sock socket handle
1231  * @param max maximum number of bytes to read
1232  * @param timeout maximum amount of time to wait (use -1 for "forever")
1233  * @param receiver function to call with received data
1234  * @param receiver_cls closure for receiver
1235  */
1236 void
1237 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1238                            size_t max,
1239                            struct GNUNET_TIME_Relative timeout,
1240                            GNUNET_CONNECTION_Receiver receiver,
1241                            void *receiver_cls)
1242 {
1243   struct GNUNET_SCHEDULER_TaskContext tc;
1244
1245   GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1246                  (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1247                  (sock->receiver == NULL));
1248   sock->receiver = receiver;
1249   sock->receiver_cls = receiver_cls;
1250   sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1251   sock->max = max;
1252   if (sock->sock != NULL)
1253     {
1254       memset (&tc, 0, sizeof (tc));
1255       tc.sched = sock->sched;
1256       tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1257       receive_again (sock, &tc);
1258       return;
1259     }
1260   if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1261     {
1262       receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1263       return;
1264     }
1265   sock->ccs += COCO_RECEIVE_AGAIN;
1266 }
1267
1268
1269 /**
1270  * Configure this connection to ignore shutdown signals.
1271  *
1272  * @param sock socket handle
1273  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1274  */
1275 void
1276 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1277                                    int do_ignore)
1278 {
1279   sock->ignore_shutdown = do_ignore;
1280 }
1281
1282
1283 /**
1284  * Cancel receive job on the given socket.  Note that the
1285  * receiver callback must not have been called yet in order
1286  * for the cancellation to be valid.
1287  *
1288  * @param sock socket handle
1289  * @return closure of the original receiver callback closure
1290  */
1291 void *
1292 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1293 {
1294   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1295     {
1296       GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched,
1297                                                       sock->read_task));
1298       sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1299     }
1300   else
1301     {
1302       GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1303       sock->ccs -= COCO_RECEIVE_AGAIN;
1304     }
1305   sock->receiver = NULL;
1306   return sock->receiver_cls;
1307 }
1308
1309
1310 /**
1311  * Try to call the transmit notify method (check if we do
1312  * have enough space available first)!
1313  *
1314  * @param sock socket for which we should do this processing
1315  * @return GNUNET_YES if we were able to call notify
1316  */
1317 static int
1318 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1319 {
1320   size_t used;
1321   size_t avail;
1322   size_t size;
1323   GNUNET_CONNECTION_TransmitReadyNotify notify;
1324
1325   GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1326   if (NULL == (notify = sock->nth.notify_ready))
1327     return GNUNET_NO;
1328   used = sock->write_buffer_off - sock->write_buffer_pos;
1329   avail = sock->write_buffer_size - used;
1330   size = sock->nth.notify_size;
1331   if (size > avail)
1332     return GNUNET_NO;
1333   sock->nth.notify_ready = NULL;
1334   if (sock->write_buffer_size - sock->write_buffer_off < size)
1335     {
1336       /* need to compact */
1337       memmove (sock->write_buffer,
1338                &sock->write_buffer[sock->write_buffer_pos], used);
1339       sock->write_buffer_off -= sock->write_buffer_pos;
1340       sock->write_buffer_pos = 0;
1341     }
1342   avail = sock->write_buffer_size - sock->write_buffer_off;
1343   GNUNET_assert (avail >= size);
1344   size = notify (sock->nth.notify_ready_cls,
1345                  avail,
1346                  &sock->write_buffer[sock->write_buffer_off]);
1347   GNUNET_assert (size <= avail);
1348   sock->write_buffer_off += size;
1349   return GNUNET_YES;
1350 }
1351
1352
1353 /**
1354  * Task invoked by the scheduler when a call to transmit
1355  * is timing out (we never got enough buffer space to call
1356  * the callback function before the specified timeout
1357  * expired).
1358  *
1359  * This task notifies the client about the timeout.
1360  */
1361 static void
1362 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1363 {
1364   struct GNUNET_CONNECTION_Handle *sock = cls;
1365   GNUNET_CONNECTION_TransmitReadyNotify notify;
1366
1367 #if DEBUG_CONNECTION
1368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369               "transmit_timeout running (%p)\n", sock);
1370 #endif
1371   sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1372 #if DEBUG_CONNECTION
1373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374               "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1375               sock->hostname,
1376               sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1377 #endif
1378   GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1379   sock->ccs -= COCO_TRANSMIT_READY;     /* remove request */
1380   notify = sock->nth.notify_ready;
1381   sock->nth.notify_ready = NULL;
1382   notify (sock->nth.notify_ready_cls, 0, NULL);
1383 }
1384
1385
1386 /**
1387  * Task invoked by the scheduler when we failed to connect
1388  * at the time of being asked to transmit.
1389  *
1390  * This task notifies the client about the error.
1391  */
1392 static void
1393 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1394 {
1395   struct GNUNET_CONNECTION_Handle *sock = cls;
1396   GNUNET_CONNECTION_TransmitReadyNotify notify;
1397
1398 #if DEBUG_CONNECTION
1399   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400               "Transmission request of size %u fails, connection failed (%p).\n",
1401               sock->nth.notify_size, sock);
1402 #endif
1403   sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1404   notify = sock->nth.notify_ready;
1405   sock->nth.notify_ready = NULL;
1406   notify (sock->nth.notify_ready_cls, 0, NULL);
1407 }
1408
1409
1410 static void
1411 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1412 {
1413   GNUNET_CONNECTION_TransmitReadyNotify notify;
1414
1415   if (NULL != sock->sock)
1416     {
1417       GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1418       GNUNET_break (GNUNET_OK ==
1419                     GNUNET_NETWORK_socket_close (sock->sock));
1420       sock->sock = NULL;
1421     }
1422   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1423     {
1424       GNUNET_SCHEDULER_cancel (sock->sched,
1425                                sock->read_task);
1426       sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1427       signal_timeout (sock);
1428       return;
1429     }
1430   if (sock->nth.notify_ready == NULL)
1431     return;                     /* nobody to tell about it */
1432   notify = sock->nth.notify_ready;
1433   sock->nth.notify_ready = NULL;
1434   notify (sock->nth.notify_ready_cls, 0, NULL);
1435 }
1436
1437
1438 /**
1439  * See if we are now connected.  If not, wait longer for
1440  * connect to succeed.  If connected, we should be able
1441  * to write now as well, unless we timed out.
1442  *
1443  * @param cls our connection handle
1444  * @param tc task context describing why we are here
1445  */
1446 static void
1447 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1448 {
1449   struct GNUNET_CONNECTION_Handle *sock = cls;
1450   GNUNET_CONNECTION_TransmitReadyNotify notify;
1451   ssize_t ret;
1452   size_t have;
1453
1454 #if DEBUG_CONNECTION
1455   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456               "transmit_ready running (%p).\n", sock);
1457 #endif
1458   GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1459   sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1460   GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1461   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 
1462     {
1463       if (sock->ignore_shutdown == GNUNET_YES)
1464         goto SCHEDULE_WRITE;    /* ignore shutdown, go again immediately */
1465 #if DEBUG_CONNECTION
1466       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467                   "Transmit to `%s' fails, shutdown happened (%p).\n",
1468                   GNUNET_a2s (sock->addr, sock->addrlen), sock);
1469 #endif
1470       notify = sock->nth.notify_ready;
1471       sock->nth.notify_ready = NULL;
1472       notify (sock->nth.notify_ready_cls, 0, NULL);
1473       return;
1474     }
1475   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1476     {
1477 #if DEBUG_CONNECTION
1478       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479                   "Transmit to `%s' fails, time out reached (%p).\n",
1480                   GNUNET_a2s (sock->addr, sock->addrlen), sock);
1481 #endif
1482       notify = sock->nth.notify_ready;
1483       sock->nth.notify_ready = NULL;
1484       notify (sock->nth.notify_ready_cls, 0, NULL);
1485       return;
1486     }
1487   GNUNET_assert (NULL != sock->sock);
1488   if (tc->write_ready == NULL)
1489     {
1490       /* special circumstances (in particular,
1491          PREREQ_DONE after connect): not yet ready to write,
1492          but no "fatal" error either.  Hence retry.  */
1493       goto SCHEDULE_WRITE;
1494     }
1495   if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1496     {
1497 #if DEBUG_CONNECTION
1498       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1499                   _
1500                   ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1501                   sock);
1502 #endif
1503       transmit_error (sock);
1504       return;                   /* connect failed for good, we're finished */
1505     }
1506   GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1507   if ( (sock->nth.notify_ready != NULL) &&
1508        (sock->write_buffer_size < sock->nth.notify_size) )
1509     {
1510       sock->write_buffer = GNUNET_realloc(sock->write_buffer, 
1511                                           sock->nth.notify_size);
1512       sock->write_buffer_size = sock->nth.notify_size;
1513     }    
1514   process_notify (sock);
1515   have = sock->write_buffer_off - sock->write_buffer_pos;
1516   if (have == 0)
1517     {
1518       /* no data ready for writing, terminate write loop */
1519       return;
1520     }
1521   GNUNET_assert (have <= sock->write_buffer_size);
1522   GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1523   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1524 RETRY:
1525   ret = GNUNET_NETWORK_socket_send (sock->sock,
1526                                     &sock->write_buffer[sock->
1527                                                         write_buffer_pos],
1528                                     have);
1529   if (ret == -1)
1530     {
1531       if (errno == EINTR)
1532         goto RETRY;
1533 #if 0
1534       int en = errno;
1535       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1536                   _("Failed to send to `%s': %s\n"),
1537                   GNUNET_a2s (sock->addr,
1538                               sock->addrlen),
1539                   STRERROR (en));
1540 #endif
1541 #if DEBUG_CONNECTION
1542       GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1543 #endif
1544       transmit_error (sock);
1545       return;
1546     }
1547 #if DEBUG_CONNECTION
1548   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549               "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1550               (unsigned int) ret,
1551               have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1552 #endif
1553   sock->write_buffer_pos += ret;
1554   if (sock->write_buffer_pos == sock->write_buffer_off)
1555     {
1556       /* transmitted all pending data */
1557       sock->write_buffer_pos = 0;
1558       sock->write_buffer_off = 0;
1559     }
1560   if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1561     return;                     /* all data sent! */
1562   /* not done writing, schedule more */
1563 SCHEDULE_WRITE:
1564 #if DEBUG_CONNECTION
1565   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566               "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1567 #endif
1568   if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1569     sock->write_task =
1570       GNUNET_SCHEDULER_add_write_net (tc->sched,
1571                                       GNUNET_TIME_absolute_get_remaining
1572                                       (sock->nth.transmit_timeout),
1573                                       sock->sock, &transmit_ready, sock);
1574 }
1575
1576
1577 /**
1578  * Ask the socket to call us once the specified number of bytes
1579  * are free in the transmission buffer.  May call the notify
1580  * method immediately if enough space is available.
1581  *
1582  * @param sock socket
1583  * @param size number of bytes to send
1584  * @param timeout after how long should we give up (and call
1585  *        notify with buf NULL and size 0)?
1586  * @param notify function to call
1587  * @param notify_cls closure for notify
1588  * @return non-NULL if the notify callback was queued,
1589  *         NULL if we are already going to notify someone else (busy)
1590  */
1591 struct GNUNET_CONNECTION_TransmitHandle *
1592 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1593                                          *sock, size_t size,
1594                                          struct GNUNET_TIME_Relative timeout,
1595                                          GNUNET_CONNECTION_TransmitReadyNotify
1596                                          notify, void *notify_cls)
1597 {
1598   if (sock->nth.notify_ready != NULL)
1599     return NULL;
1600   GNUNET_assert (notify != NULL);
1601   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1602   GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1603   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1604   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1605   sock->nth.notify_ready = notify;
1606   sock->nth.notify_ready_cls = notify_cls;
1607   sock->nth.sh = sock;
1608   sock->nth.notify_size = size;
1609   sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1610   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1611   if ((sock->sock == NULL) &&
1612       (sock->ap_head == NULL) && (sock->dns_active == NULL))
1613     {
1614       if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1615         GNUNET_SCHEDULER_cancel (sock->sched,
1616                                  sock->write_task);
1617       sock->write_task = GNUNET_SCHEDULER_add_now (sock->sched,
1618                                                    &connect_error, sock);
1619       return &sock->nth;
1620     }
1621   if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1622     return &sock->nth;
1623   if (sock->sock != NULL)
1624     {
1625 #if DEBUG_CONNECTION
1626       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627                   "Scheduling transmit_ready (%p).\n", sock);
1628 #endif
1629       sock->write_task = GNUNET_SCHEDULER_add_write_net (sock->sched,
1630                                                          GNUNET_TIME_absolute_get_remaining
1631                                                          (sock->nth.
1632                                                           transmit_timeout),
1633                                                          sock->sock,
1634                                                          &transmit_ready,
1635                                                          sock);
1636     }
1637   else
1638     {
1639 #if DEBUG_CONNECTION
1640       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1641                   "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1642                   sock);
1643 #endif
1644       sock->ccs |= COCO_TRANSMIT_READY;
1645       sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1646                                                              timeout,
1647                                                              &transmit_timeout,
1648                                                              sock);
1649     }
1650   return &sock->nth;
1651 }
1652
1653
1654 /**
1655  * Cancel the specified transmission-ready
1656  * notification.
1657  */
1658 void
1659 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1660                                                 GNUNET_CONNECTION_TransmitHandle
1661                                                 *h)
1662 {
1663   GNUNET_assert (h->notify_ready != NULL);
1664   if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
1665     {
1666 #if DEBUG_CONNECTION
1667       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668                   "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
1669                   h);
1670 #endif
1671       GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
1672       h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1673       h->sh->ccs -= COCO_TRANSMIT_READY;
1674     }
1675   else
1676     {
1677       if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1678         {
1679           GNUNET_SCHEDULER_cancel (h->sh->sched, h->sh->write_task);
1680           h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1681         }
1682     }
1683   h->notify_ready = NULL;
1684 }
1685
1686 /* end of connection.c */