bad commit 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);
560 }
561
562
563
564 /**
565  * See if we are now connected.  If not, wait longer for
566  * connect to succeed.  If connected, we should be able
567  * to write now as well, unless we timed out.
568  *
569  * @param cls our connection handle
570  * @param tc task context describing why we are here
571  */
572 static void
573 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
574
575
576 /**
577  * We've failed for good to establish a connection.
578  *
579  * @param h the connection we tried to establish
580  */
581 static void
582 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
583 {
584 #if DEBUG_CONNECTION
585   GNUNET_log ((0 != strncmp (h->hostname, 
586                              "localhost:",
587                              10)) 
588               ? GNUNET_ERROR_TYPE_INFO 
589               : GNUNET_ERROR_TYPE_WARNING,
590               _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
591               h->hostname, h->port);
592 #endif
593   /* connect failed / timed out */
594   GNUNET_break (h->ap_head == NULL);
595   GNUNET_break (h->ap_tail == NULL);
596   GNUNET_break (h->dns_active == GNUNET_NO);
597   GNUNET_break (h->sock == NULL);
598
599   /* trigger jobs that used to wait on "connect_task" */
600   if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
601     {
602 #if DEBUG_CONNECTION
603       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604                   "connect_fail_continuation triggers receive_again (%p)\n",
605                   h);
606 #endif
607       h->ccs -= COCO_RECEIVE_AGAIN;
608       h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
609                                                  GNUNET_SCHEDULER_NO_TASK,
610                                                  &receive_again, h);
611     }
612   if (0 != (h->ccs & COCO_TRANSMIT_READY))
613     {
614 #if DEBUG_CONNECTION
615       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616                   "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
617                   h);
618 #endif
619       GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
620       GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
621       h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
622       h->ccs -= COCO_TRANSMIT_READY;
623       GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
624       h->write_task = GNUNET_SCHEDULER_add_after (h->sched,
625                                                   GNUNET_SCHEDULER_NO_TASK,
626                                                   &transmit_ready, h);
627     }
628   if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
629     {
630 #if DEBUG_CONNECTION
631       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632                   "connect_fail_continuation runs destroy_continuation (%p)\n",
633                   h);
634 #endif
635       h->ccs -= COCO_DESTROY_CONTINUATION;
636       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
637       h->destroy_task
638         = GNUNET_SCHEDULER_add_now (h->sched,
639                                     &destroy_continuation,
640                                     h);
641     }
642 }
643
644
645 /**
646  * We've succeeded in establishing a connection.
647  *
648  * @param h the connection we tried to establish
649  */
650 static void
651 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
652 {
653 #if DEBUG_CONNECTION
654   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655               "Connection to `%s' succeeded! (%p)\n",
656               GNUNET_a2s (h->addr, h->addrlen), h);
657 #endif
658   /* trigger jobs that waited for the connection */
659   if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
660     {
661 #if DEBUG_CONNECTION
662       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663                   "connect_success_continuation runs receive_again (%p)\n",
664                   h);
665 #endif
666       h->ccs -= COCO_RECEIVE_AGAIN;
667       h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
668                                                  GNUNET_SCHEDULER_NO_TASK,
669                                                  &receive_again, h);
670     }
671   if (0 != (h->ccs & COCO_TRANSMIT_READY))
672     {
673 #if DEBUG_CONNECTION
674       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675                   "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
676                   h);
677 #endif
678       GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
679       GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
680       h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
681       h->ccs -= COCO_TRANSMIT_READY;
682       GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
683       h->write_task =
684         GNUNET_SCHEDULER_add_write_net (h->sched,
685                                         GNUNET_TIME_absolute_get_remaining
686                                         (h->nth.transmit_timeout), h->sock,
687                                         &transmit_ready, h);
688     }
689   if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
690     {
691 #if DEBUG_CONNECTION
692       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693                   "connect_success_continuation runs destroy_continuation (%p)\n",
694                   h);
695 #endif
696       h->ccs -= COCO_DESTROY_CONTINUATION;
697       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
698       h->destroy_task
699         = GNUNET_SCHEDULER_add_now (h->sched,
700                                     &destroy_continuation,
701                                     h);
702     }
703 }
704
705
706 /**
707  * Scheduler let us know that we're either ready to write on the
708  * socket OR connect timed out.  Do the right thing.
709  *
710  * @param cls the "struct AddressProbe*" with the address that we are probing
711  * @param tc success or failure info about the connect attempt.
712  */
713 static void
714 connect_probe_continuation (void *cls,
715                             const struct GNUNET_SCHEDULER_TaskContext *tc)
716 {
717   struct AddressProbe *ap = cls;
718   struct GNUNET_CONNECTION_Handle *h = ap->h;
719   struct AddressProbe *pos;
720   int error;
721   unsigned int len;
722
723   GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
724   len = sizeof (error);
725   errno = 0;
726   error = 0;
727   if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
728        (GNUNET_OK !=
729         GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
730                                           &error, &len)) ||
731        (error != 0) )
732     {
733       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
734       GNUNET_free (ap);
735       if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
736         connect_fail_continuation (h);
737       return;
738     }
739   GNUNET_assert (h->sock == NULL);
740   GNUNET_assert (ap->sock != NULL);
741   h->sock = ap->sock;
742   GNUNET_assert (h->addr == NULL);
743   h->addr = GNUNET_malloc (ap->addrlen);
744   memcpy (h->addr, ap->addr, ap->addrlen);
745   h->addrlen = ap->addrlen;
746   GNUNET_free (ap);
747   /* cancel all other attempts */
748   while (NULL != (pos = h->ap_head))
749     {
750       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
751       GNUNET_SCHEDULER_cancel (h->sched, pos->task);
752       GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
753       GNUNET_free (pos);
754     }
755   connect_success_continuation (h);
756 }
757
758
759 /**
760  * Try to establish a socket connection given the specified address.
761  * This function is called by the resolver once we have a DNS reply.
762  *
763  * @param cls our "struct GNUNET_CONNECTION_Handle *"
764  * @param addr address to try, NULL for "last call"
765  * @param addrlen length of addr
766  */
767 static void
768 try_connect_using_address (void *cls,
769                            const struct sockaddr *addr, socklen_t addrlen)
770 {
771   struct GNUNET_CONNECTION_Handle *h = cls;
772   struct AddressProbe *ap;
773   struct GNUNET_TIME_Relative delay;
774
775   if (addr == NULL)
776     {
777       h->dns_active = NULL;
778       if (NULL == h->ap_head)
779         connect_fail_continuation (h);
780       return;
781     }
782   if (h->sock != NULL)
783     return;                     /* already connected */
784   GNUNET_assert (h->addr == NULL);
785   /* try to connect */
786 #if DEBUG_CONNECTION
787   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788               "Trying to connect using address `%s:%u/%s:%u'\n",
789               h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
790 #endif
791   ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
792   ap->addr = (const struct sockaddr *) &ap[1];
793   memcpy (&ap[1], addr, addrlen);
794   ap->addrlen = addrlen;
795   ap->h = h;
796
797   switch (ap->addr->sa_family)
798     {
799     case AF_INET:
800       ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
801       break;
802     case AF_INET6:
803       ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
804       break;
805     default:
806       GNUNET_break (0);
807       GNUNET_free (ap);
808       return;                   /* not supported by us */
809     }
810   ap->sock =
811     GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
812   if (ap->sock == NULL)
813     {
814       GNUNET_free (ap);
815       return;                   /* not supported by OS */
816     }
817 #if DEBUG_CONNECTION
818   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
819               _("Trying to connect to `%s' (%p)\n"),
820               GNUNET_a2s (ap->addr, ap->addrlen), h);
821 #endif
822   if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
823                                                    ap->addr,
824                                                    ap->addrlen)) &&
825       (errno != EINPROGRESS))
826     {
827       /* maybe refused / unsupported address, try next */
828       GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
829       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
830       GNUNET_free (ap);
831       return;
832     }
833   GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
834   delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
835   if (h->nth.notify_ready != NULL)
836     delay = GNUNET_TIME_relative_min (delay,
837                                       GNUNET_TIME_absolute_get_remaining
838                                       (h->nth.transmit_timeout));
839   if (h->receiver != NULL)
840     delay = GNUNET_TIME_relative_min (delay,
841                                       GNUNET_TIME_absolute_get_remaining
842                                       (h->receive_timeout));
843   ap->task =
844     GNUNET_SCHEDULER_add_write_net (h->sched, delay, ap->sock,
845                                     &connect_probe_continuation, ap);
846 }
847
848
849 /**
850  * Create a socket handle by (asynchronously) connecting to a host.
851  * This function returns immediately, even if the connection has not
852  * yet been established.  This function only creates TCP connections.
853  *
854  * @param sched scheduler to use
855  * @param cfg configuration to use
856  * @param hostname name of the host to connect to
857  * @param port port to connect to
858  * @return the socket handle
859  */
860 struct GNUNET_CONNECTION_Handle *
861 GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
862                                        const struct
863                                        GNUNET_CONFIGURATION_Handle *cfg,
864                                        const char *hostname, uint16_t port)
865 {
866   struct GNUNET_CONNECTION_Handle *ret;
867
868   GNUNET_assert (0 < strlen (hostname));        /* sanity check */
869   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
870   ret->cfg = cfg;
871   ret->sched = sched;
872   ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
873   ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
874   ret->port = port;
875   ret->hostname = GNUNET_strdup (hostname);
876   ret->dns_active = GNUNET_RESOLVER_ip_get (sched,
877                                             cfg,
878                                             ret->hostname,
879                                             AF_UNSPEC,
880                                             GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
881                                             &try_connect_using_address, ret);
882   return ret;
883 }
884
885
886 /**
887  * Create a socket handle by connecting to a UNIX domain service.
888  * This function returns immediately, even if the connection has not
889  * yet been established.  This function only creates UNIX connections.
890  *
891  * @param sched scheduler to use
892  * @param cfg configuration to use
893  * @param unixpath path to connect to
894  * @return the socket handle, NULL on systems without UNIX support
895  */
896 struct GNUNET_CONNECTION_Handle *
897 GNUNET_CONNECTION_create_from_connect_to_unixpath (struct GNUNET_SCHEDULER_Handle *sched,
898                                                    const struct
899                                                    GNUNET_CONFIGURATION_Handle *cfg,
900                                                    const char *unixpath)
901 {
902 #ifdef AF_UNIX
903   struct GNUNET_CONNECTION_Handle *ret;
904   struct sockaddr_un *un;
905   size_t slen;
906
907   GNUNET_assert (0 < strlen (unixpath));        /* sanity check */
908   un = GNUNET_malloc (sizeof (struct sockaddr_un));
909   un->sun_family = AF_UNIX;
910   slen = strlen (unixpath) + 1;
911   if (slen >= sizeof (un->sun_path))
912     slen = sizeof (un->sun_path) - 1;
913   memcpy (un->sun_path,
914           unixpath,
915           slen);
916   un->sun_path[slen] = '\0';
917   slen += sizeof (sa_family_t);
918 #if LINUX
919   un->sun_path[0] = '\0';
920   slen = sizeof (struct sockaddr_un);
921 #endif
922   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
923   ret->cfg = cfg;
924   ret->sched = sched;
925   ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
926   ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
927   ret->port = 0;
928   ret->hostname = NULL;
929   ret->addr = (struct sockaddr*) un;
930   ret->addrlen = slen;
931   ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
932   if (NULL == ret->sock)
933     {
934       GNUNET_free (ret->addr);
935       GNUNET_free (ret->write_buffer);
936       GNUNET_free (ret);
937       return NULL;
938     }
939   if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
940                                                   ret->addr,
941                                                   ret->addrlen)) 
942     {
943       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
944       GNUNET_free (ret->addr);
945       GNUNET_free (ret->write_buffer);
946       GNUNET_free (ret);
947       return NULL;
948     }
949   connect_success_continuation (ret);
950   return ret;
951 #else
952   return NULL;
953 #endif
954 }
955
956
957 /**
958  * Create a socket handle by (asynchronously) connecting to a host.
959  * This function returns immediately, even if the connection has not
960  * yet been established.  This function only creates TCP connections.
961  *
962  * @param sched scheduler to use
963  * @param af_family address family to use
964  * @param serv_addr server address
965  * @param addrlen length of server address
966  * @return the socket handle
967  */
968 struct GNUNET_CONNECTION_Handle *
969 GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
970                                         *sched, int af_family,
971                                         const struct sockaddr *serv_addr,
972                                         socklen_t addrlen)
973 {
974   struct GNUNET_NETWORK_Handle *s;
975   struct GNUNET_CONNECTION_Handle *ret;
976
977   s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
978   if (s == NULL)
979     {
980       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
981                            GNUNET_ERROR_TYPE_BULK, "socket");
982       return NULL;
983     }
984   if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
985       && (errno != EINPROGRESS))
986     {
987       /* maybe refused / unsupported address, try next */
988       GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
989       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
990       return NULL;
991     }
992   ret = GNUNET_CONNECTION_create_from_existing (sched, s);
993   ret->addr = GNUNET_malloc (addrlen);
994   memcpy (ret->addr, serv_addr, addrlen);
995   ret->addrlen = addrlen;
996 #if DEBUG_CONNECTION
997   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
998               _("Trying to connect to `%s' (%p)\n"),
999               GNUNET_a2s (serv_addr, addrlen), ret);
1000 #endif
1001   return ret;
1002 }
1003
1004
1005 /**
1006  * Check if socket is valid (no fatal errors have happened so far).
1007  * Note that a socket that is still trying to connect is considered
1008  * valid.
1009  *
1010  * @param sock socket to check
1011  * @return GNUNET_YES if valid, GNUNET_NO otherwise
1012  */
1013 int
1014 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1015 {
1016   if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1017     return GNUNET_YES;          /* still trying to connect */
1018   return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1019 }
1020
1021
1022 /**
1023  * Close the socket and free associated resources. Pending
1024  * transmissions may be completed or dropped depending on the
1025  * arguments.   If a receive call is pending and should 
1026  * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1027  * should be called explicitly first.
1028  *
1029  * @param sock socket to destroy
1030  * @param finish_pending_write should pending writes be completed or aborted?
1031  *        (this applies to transmissions where the data has already been
1032  *        read from the application; all other transmissions should be
1033  *        aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1034  */
1035 void
1036 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1037                            int finish_pending_write)
1038 {
1039   if (GNUNET_NO == finish_pending_write)
1040     {
1041       if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1042         {
1043           GNUNET_SCHEDULER_cancel (sock->sched,
1044                                    sock->write_task);
1045           sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1046           sock->write_buffer_off = 0;
1047         }
1048     }
1049   if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1050     {
1051       GNUNET_RESOLVER_request_cancel (sock->dns_active);
1052       sock->dns_active = NULL;
1053     }
1054   GNUNET_assert (sock->sched != NULL);
1055   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1056   sock->destroy_task 
1057     = GNUNET_SCHEDULER_add_now (sock->sched,
1058                                 &destroy_continuation, sock);
1059 }
1060
1061
1062 /**
1063  * Tell the receiver callback that a timeout was reached.
1064  */
1065 static void
1066 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1067 {
1068   GNUNET_CONNECTION_Receiver receiver;
1069
1070 #if DEBUG_CONNECTION
1071   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072               "Network signals time out to receiver (%p)!\n", sh);
1073 #endif
1074   GNUNET_assert (NULL != (receiver = sh->receiver));
1075   sh->receiver = NULL;
1076   receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1077 }
1078
1079
1080 /**
1081  * Tell the receiver callback that we had an IO error.
1082  */
1083 static void
1084 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1085 {
1086   GNUNET_CONNECTION_Receiver receiver;
1087   GNUNET_assert (NULL != (receiver = sh->receiver));
1088   sh->receiver = NULL;
1089   receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1090 }
1091
1092
1093 /**
1094  * This function is called once we either timeout
1095  * or have data ready to read.
1096  */
1097 static void
1098 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1099 {
1100   struct GNUNET_CONNECTION_Handle *sh = cls;
1101   struct GNUNET_TIME_Absolute now;
1102   char buffer[sh->max];
1103   ssize_t ret;
1104   GNUNET_CONNECTION_Receiver receiver;
1105
1106   sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1107   if ( (GNUNET_YES == sh->ignore_shutdown) &&
1108        (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 
1109     {
1110       /* ignore shutdown request, go again immediately */
1111 #if DEBUG_CONNECTION
1112       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113                   "Ignoring shutdown signal per configuration\n");
1114 #endif
1115       sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1116                                                      GNUNET_TIME_absolute_get_remaining
1117                                                      (sh->receive_timeout),
1118                                                      sh->sock,
1119                                                      &receive_ready, sh);
1120       return;
1121     }
1122   now = GNUNET_TIME_absolute_get ();
1123   if ((now.value > sh->receive_timeout.value) ||
1124       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1125       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1126     {
1127 #if DEBUG_CONNECTION
1128       if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1129         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130                     "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1131                     GNUNET_a2s (sh->addr, sh->addrlen),
1132                     GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1133                     value, sh);
1134 #endif
1135       signal_timeout (sh);
1136       return;
1137     }
1138   if (sh->sock == NULL)
1139     {
1140       /* connect failed for good */
1141 #if DEBUG_CONNECTION
1142       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143                   "Receive encounters error, socket closed... (%p)\n", sh);
1144 #endif
1145       signal_error (sh, ECONNREFUSED);
1146       return;
1147     }
1148   GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1149 RETRY:
1150   ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1151   if (ret == -1)
1152     {
1153       if (errno == EINTR)
1154         goto RETRY;
1155 #if DEBUG_CONNECTION
1156       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157                   "Error receiving: %s\n", STRERROR (errno));
1158 #endif
1159       signal_error (sh, errno);
1160       return;
1161     }
1162 #if DEBUG_CONNECTION
1163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164               "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1165               (unsigned int) ret,
1166               sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1167 #endif
1168   GNUNET_assert (NULL != (receiver = sh->receiver));
1169   sh->receiver = NULL;
1170   receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1171 }
1172
1173
1174 /**
1175  * This function is called after establishing a connection either has
1176  * succeeded or timed out.  Note that it is possible that the attempt
1177  * timed out and that we're immediately retrying.  If we are retrying,
1178  * we need to wait again (or timeout); if we succeeded, we need to
1179  * wait for data (or timeout).
1180  *
1181  * @param cls our connection handle
1182  * @param tc task context describing why we are here
1183  */
1184 static void
1185 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1186 {
1187   struct GNUNET_CONNECTION_Handle *sh = cls;
1188   struct GNUNET_TIME_Absolute now;
1189
1190   sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1191   if (sh->sock == NULL)
1192     {
1193       /* not connected and no longer trying */
1194 #if DEBUG_CONNECTION
1195       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196                   "Receive encounters error, socket closed (%p)...\n", sh);
1197 #endif
1198       signal_error (sh, ECONNREFUSED);
1199       return;
1200     }
1201   now = GNUNET_TIME_absolute_get ();
1202   if ((now.value > sh->receive_timeout.value) ||
1203       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1204     {
1205 #if DEBUG_CONNECTION
1206       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207                   "Receive encounters error: time out (%p)...\n", sh);
1208 #endif
1209       signal_timeout (sh);
1210       return;
1211     }
1212   GNUNET_assert (sh->sock != NULL);
1213   /* connect succeeded, wait for data! */
1214   sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1215                                                  GNUNET_TIME_absolute_get_remaining
1216                                                  (sh->receive_timeout),
1217                                                  sh->sock,
1218                                                  &receive_ready, sh);
1219 }
1220
1221
1222 /**
1223  * Receive data from the given socket.  Note that this function will
1224  * call "receiver" asynchronously using the scheduler.  It will
1225  * "immediately" return.  Note that there MUST only be one active
1226  * receive call per socket at any given point in time (so do not
1227  * call receive again until the receiver callback has been invoked).
1228  *
1229  * @param sock socket handle
1230  * @param max maximum number of bytes to read
1231  * @param timeout maximum amount of time to wait (use -1 for "forever")
1232  * @param receiver function to call with received data
1233  * @param receiver_cls closure for receiver
1234  */
1235 void
1236 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1237                            size_t max,
1238                            struct GNUNET_TIME_Relative timeout,
1239                            GNUNET_CONNECTION_Receiver receiver,
1240                            void *receiver_cls)
1241 {
1242   struct GNUNET_SCHEDULER_TaskContext tc;
1243
1244   GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1245                  (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1246                  (sock->receiver == NULL));
1247   sock->receiver = receiver;
1248   sock->receiver_cls = receiver_cls;
1249   sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1250   sock->max = max;
1251   if (sock->sock != NULL)
1252     {
1253       memset (&tc, 0, sizeof (tc));
1254       tc.sched = sock->sched;
1255       tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1256       receive_again (sock, &tc);
1257       return;
1258     }
1259   if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1260     {
1261       receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1262       return;
1263     }
1264   sock->ccs += COCO_RECEIVE_AGAIN;
1265 }
1266
1267
1268 /**
1269  * Configure this connection to ignore shutdown signals.
1270  *
1271  * @param sock socket handle
1272  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1273  */
1274 void
1275 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1276                                    int do_ignore)
1277 {
1278   sock->ignore_shutdown = do_ignore;
1279 }
1280
1281
1282 /**
1283  * Cancel receive job on the given socket.  Note that the
1284  * receiver callback must not have been called yet in order
1285  * for the cancellation to be valid.
1286  *
1287  * @param sock socket handle
1288  * @return closure of the original receiver callback closure
1289  */
1290 void *
1291 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1292 {
1293   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1294     {
1295       GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched,
1296                                                       sock->read_task));
1297       sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1298     }
1299   else
1300     {
1301       GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1302       sock->ccs -= COCO_RECEIVE_AGAIN;
1303     }
1304   sock->receiver = NULL;
1305   return sock->receiver_cls;
1306 }
1307
1308
1309 /**
1310  * Try to call the transmit notify method (check if we do
1311  * have enough space available first)!
1312  *
1313  * @param sock socket for which we should do this processing
1314  * @return GNUNET_YES if we were able to call notify
1315  */
1316 static int
1317 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1318 {
1319   size_t used;
1320   size_t avail;
1321   size_t size;
1322   GNUNET_CONNECTION_TransmitReadyNotify notify;
1323
1324   GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1325   if (NULL == (notify = sock->nth.notify_ready))
1326     return GNUNET_NO;
1327   used = sock->write_buffer_off - sock->write_buffer_pos;
1328   avail = sock->write_buffer_size - used;
1329   size = sock->nth.notify_size;
1330   if (sock->nth.notify_size > avail)
1331     return GNUNET_NO;
1332   sock->nth.notify_ready = NULL;
1333   if (sock->write_buffer_size - sock->write_buffer_off < size)
1334     {
1335       /* need to compact */
1336       memmove (sock->write_buffer,
1337                &sock->write_buffer[sock->write_buffer_pos], used);
1338       sock->write_buffer_off -= sock->write_buffer_pos;
1339       sock->write_buffer_pos = 0;
1340     }
1341   GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
1342   size = notify (sock->nth.notify_ready_cls,
1343                  sock->write_buffer_size - sock->write_buffer_off,
1344                  &sock->write_buffer[sock->write_buffer_off]);
1345   sock->write_buffer_off += size;
1346   return GNUNET_YES;
1347 }
1348
1349
1350 /**
1351  * Task invoked by the scheduler when a call to transmit
1352  * is timing out (we never got enough buffer space to call
1353  * the callback function before the specified timeout
1354  * expired).
1355  *
1356  * This task notifies the client about the timeout.
1357  */
1358 static void
1359 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1360 {
1361   struct GNUNET_CONNECTION_Handle *sock = cls;
1362   GNUNET_CONNECTION_TransmitReadyNotify notify;
1363
1364 #if DEBUG_CONNECTION
1365   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366               "transmit_timeout running (%p)\n", sock);
1367 #endif
1368   sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1369 #if DEBUG_CONNECTION
1370   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371               "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1372               sock->hostname,
1373               sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1374 #endif
1375   GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1376   sock->ccs -= COCO_TRANSMIT_READY;     /* remove request */
1377   notify = sock->nth.notify_ready;
1378   sock->nth.notify_ready = NULL;
1379   notify (sock->nth.notify_ready_cls, 0, NULL);
1380 }
1381
1382
1383 /**
1384  * Task invoked by the scheduler when we failed to connect
1385  * at the time of being asked to transmit.
1386  *
1387  * This task notifies the client about the error.
1388  */
1389 static void
1390 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1391 {
1392   struct GNUNET_CONNECTION_Handle *sock = cls;
1393   GNUNET_CONNECTION_TransmitReadyNotify notify;
1394
1395 #if DEBUG_CONNECTION
1396   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397               "Transmission request of size %u fails, connection failed (%p).\n",
1398               sock->nth.notify_size, sock);
1399 #endif
1400   sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1401   notify = sock->nth.notify_ready;
1402   sock->nth.notify_ready = NULL;
1403   notify (sock->nth.notify_ready_cls, 0, NULL);
1404 }
1405
1406
1407 static void
1408 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1409 {
1410   GNUNET_CONNECTION_TransmitReadyNotify notify;
1411
1412   if (NULL != sock->sock)
1413     {
1414       GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1415       GNUNET_break (GNUNET_OK ==
1416                     GNUNET_NETWORK_socket_close (sock->sock));
1417       sock->sock = NULL;
1418     }
1419   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1420     {
1421       GNUNET_SCHEDULER_cancel (sock->sched,
1422                                sock->read_task);
1423       sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1424       signal_timeout (sock);
1425       return;
1426     }
1427   if (sock->nth.notify_ready == NULL)
1428     return;                     /* nobody to tell about it */
1429   notify = sock->nth.notify_ready;
1430   sock->nth.notify_ready = NULL;
1431   notify (sock->nth.notify_ready_cls, 0, NULL);
1432 }
1433
1434
1435 /**
1436  * See if we are now connected.  If not, wait longer for
1437  * connect to succeed.  If connected, we should be able
1438  * to write now as well, unless we timed out.
1439  *
1440  * @param cls our connection handle
1441  * @param tc task context describing why we are here
1442  */
1443 static void
1444 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1445 {
1446   struct GNUNET_CONNECTION_Handle *sock = cls;
1447   GNUNET_CONNECTION_TransmitReadyNotify notify;
1448   ssize_t ret;
1449   size_t have;
1450
1451 #if DEBUG_CONNECTION
1452   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1453               "transmit_ready running (%p).\n", sock);
1454 #endif
1455   GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1456   sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1457   GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1458   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 
1459     {
1460       if (sock->ignore_shutdown == GNUNET_YES)
1461         goto SCHEDULE_WRITE;    /* ignore shutdown, go again immediately */
1462 #if DEBUG_CONNECTION
1463       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464                   "Transmit to `%s' fails, shutdown happened (%p).\n",
1465                   GNUNET_a2s (sock->addr, sock->addrlen), sock);
1466 #endif
1467       notify = sock->nth.notify_ready;
1468       sock->nth.notify_ready = NULL;
1469       notify (sock->nth.notify_ready_cls, 0, NULL);
1470       return;
1471     }
1472   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1473     {
1474 #if DEBUG_CONNECTION
1475       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476                   "Transmit to `%s' fails, time out reached (%p).\n",
1477                   GNUNET_a2s (sock->addr, sock->addrlen), sock);
1478 #endif
1479       notify = sock->nth.notify_ready;
1480       sock->nth.notify_ready = NULL;
1481       notify (sock->nth.notify_ready_cls, 0, NULL);
1482       return;
1483     }
1484   GNUNET_assert (NULL != sock->sock);
1485   if (tc->write_ready == NULL)
1486     {
1487       /* special circumstances (in particular,
1488          PREREQ_DONE after connect): not yet ready to write,
1489          but no "fatal" error either.  Hence retry.  */
1490       goto SCHEDULE_WRITE;
1491     }
1492   if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1493     {
1494 #if DEBUG_CONNECTION
1495       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1496                   _
1497                   ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1498                   sock);
1499 #endif
1500       transmit_error (sock);
1501       return;                   /* connect failed for good, we're finished */
1502     }
1503   GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1504   process_notify (sock);
1505   have = sock->write_buffer_off - sock->write_buffer_pos;
1506   if (have == 0)
1507     {
1508       /* no data ready for writing, terminate write loop */
1509       return;
1510     }
1511   GNUNET_assert (have <= sock->write_buffer_size);
1512   GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1513   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1514 RETRY:
1515   ret = GNUNET_NETWORK_socket_send (sock->sock,
1516                                     &sock->write_buffer[sock->
1517                                                         write_buffer_pos],
1518                                     have);
1519   if (ret == -1)
1520     {
1521       if (errno == EINTR)
1522         goto RETRY;
1523 #if 0
1524       int en = errno;
1525       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1526                   _("Failed to send to `%s': %s\n"),
1527                   GNUNET_a2s (sock->addr,
1528                               sock->addrlen),
1529                   STRERROR (en));
1530 #endif
1531 #if DEBUG_CONNECTION
1532       GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1533 #endif
1534       transmit_error (sock);
1535       return;
1536     }
1537 #if DEBUG_CONNECTION
1538   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539               "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1540               (unsigned int) ret,
1541               have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1542 #endif
1543   sock->write_buffer_pos += ret;
1544   if (sock->write_buffer_pos == sock->write_buffer_off)
1545     {
1546       /* transmitted all pending data */
1547       sock->write_buffer_pos = 0;
1548       sock->write_buffer_off = 0;
1549     }
1550   if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1551     return;                     /* all data sent! */
1552   /* not done writing, schedule more */
1553 SCHEDULE_WRITE:
1554 #if DEBUG_CONNECTION
1555   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556               "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1557 #endif
1558   if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1559     sock->write_task =
1560       GNUNET_SCHEDULER_add_write_net (tc->sched,
1561                                       GNUNET_TIME_absolute_get_remaining
1562                                       (sock->nth.transmit_timeout),
1563                                       sock->sock, &transmit_ready, sock);
1564 }
1565
1566
1567 /**
1568  * Ask the socket to call us once the specified number of bytes
1569  * are free in the transmission buffer.  May call the notify
1570  * method immediately if enough space is available.
1571  *
1572  * @param sock socket
1573  * @param size number of bytes to send
1574  * @param timeout after how long should we give up (and call
1575  *        notify with buf NULL and size 0)?
1576  * @param notify function to call
1577  * @param notify_cls closure for notify
1578  * @return non-NULL if the notify callback was queued,
1579  *         NULL if we are already going to notify someone else (busy)
1580  */
1581 struct GNUNET_CONNECTION_TransmitHandle *
1582 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1583                                          *sock, size_t size,
1584                                          struct GNUNET_TIME_Relative timeout,
1585                                          GNUNET_CONNECTION_TransmitReadyNotify
1586                                          notify, void *notify_cls)
1587 {
1588   size_t temp_size;
1589   if (sock->nth.notify_ready != NULL)
1590     return NULL;
1591   GNUNET_assert (notify != NULL);
1592   if ((sock->write_buffer_size < size) && (size < GNUNET_SERVER_MAX_MESSAGE_SIZE))
1593     {
1594       temp_size = sock->write_buffer_size + size;
1595       if (temp_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1596         temp_size = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1597
1598       sock->write_buffer = GNUNET_realloc(sock->write_buffer, temp_size);
1599       sock->write_buffer_size = temp_size;
1600     }
1601   GNUNET_assert (sock->write_buffer_size >= 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 */