converting to GNUNET_LOG_from*
[oweals/gnunet.git] / src / util / client.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2006, 2008, 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/client.c
23  * @brief code for access to services
24  * @author Christian Grothoff
25  *
26  * Generic TCP code for reliable, record-oriented TCP
27  * connections between clients and service providers.
28  */
29
30 #include "platform.h"
31 #include "gnunet_common.h"
32 #include "gnunet_client_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_scheduler_lib.h"
36
37 #define DEBUG_CLIENT GNUNET_EXTRA_LOGGING
38
39 /**
40  * How often do we re-try tranmsitting requests before giving up?
41  * Note that if we succeeded transmitting a request but failed to read
42  * a response, we do NOT re-try.
43  */
44 #define MAX_ATTEMPTS 50
45
46 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
47
48 /**
49  * Handle for a transmission request.
50  */
51 struct GNUNET_CLIENT_TransmitHandle
52 {
53   /**
54    * Connection state.
55    */
56   struct GNUNET_CLIENT_Connection *sock;
57
58   /**
59    * Function to call to get the data for transmission.
60    */
61   GNUNET_CONNECTION_TransmitReadyNotify notify;
62
63   /**
64    * Closure for notify.
65    */
66   void *notify_cls;
67
68   /**
69    * Handle to the transmission with the underlying
70    * connection.
71    */
72   struct GNUNET_CONNECTION_TransmitHandle *th;
73
74   /**
75    * If we are re-trying and are delaying to do so,
76    * handle to the scheduled task managing the delay.
77    */
78   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
79
80   /**
81    * Timeout for the operation overall.
82    */
83   struct GNUNET_TIME_Absolute timeout;
84
85   /**
86    * Number of bytes requested.
87    */
88   size_t size;
89
90   /**
91    * Are we allowed to re-try to connect without telling
92    * the user (of this API) about the connection troubles?
93    */
94   int auto_retry;
95
96   /**
97    * Number of attempts left for transmitting the request.  We may
98    * fail the first time (say because the service is not yet up), in
99    * which case (if auto_retry is set) we wait a bit and re-try
100    * (timeout permitting).
101    */
102   unsigned int attempts_left;
103
104 };
105
106
107 /**
108  * Context for processing
109  * "GNUNET_CLIENT_transmit_and_get_response" requests.
110  */
111 struct TransmitGetResponseContext
112 {
113   /**
114    * Client handle.
115    */
116   struct GNUNET_CLIENT_Connection *sock;
117
118   /**
119    * Message to transmit; do not free, allocated
120    * right after this struct.
121    */
122   const struct GNUNET_MessageHeader *hdr;
123
124   /**
125    * Timeout to use.
126    */
127   struct GNUNET_TIME_Absolute timeout;
128
129   /**
130    * Function to call when done.
131    */
132   GNUNET_CLIENT_MessageHandler rn;
133
134   /**
135    * Closure for "rn".
136    */
137   void *rn_cls;
138 };
139
140 /**
141  * Struct to refer to a GNUnet TCP connection.
142  * This is more than just a socket because if the server
143  * drops the connection, the client automatically tries
144  * to reconnect (and for that needs connection information).
145  */
146 struct GNUNET_CLIENT_Connection
147 {
148
149   /**
150    * the socket handle, NULL if not live
151    */
152   struct GNUNET_CONNECTION_Handle *sock;
153
154   /**
155    * Our configuration.
156    */
157   const struct GNUNET_CONFIGURATION_Handle *cfg;
158
159   /**
160    * Name of the service we interact with.
161    */
162   char *service_name;
163
164   /**
165    * Context of a transmit_and_get_response operation, NULL
166    * if no such operation is pending.
167    */
168   struct TransmitGetResponseContext *tag;
169
170   /**
171    * Handler for current receiver task.
172    */
173   GNUNET_CLIENT_MessageHandler receiver_handler;
174
175   /**
176    * Closure for receiver_handler.
177    */
178   void *receiver_handler_cls;
179
180   /**
181    * Handle for a pending transmission request, NULL if there is
182    * none pending.
183    */
184   struct GNUNET_CLIENT_TransmitHandle *th;
185
186   /**
187    * Handler for service test completion (NULL unless in service_test)
188    */
189   GNUNET_SCHEDULER_Task test_cb;
190
191   /**
192    * Deadline for calling 'test_cb'.
193    */
194   struct GNUNET_TIME_Absolute test_deadline;
195
196   /**
197    * If we are re-trying and are delaying to do so,
198    * handle to the scheduled task managing the delay.
199    */
200   GNUNET_SCHEDULER_TaskIdentifier receive_task;
201
202   /**
203    * Closure for test_cb (NULL unless in service_test)
204    */
205   void *test_cb_cls;
206
207   /**
208    * Buffer for received message.
209    */
210   char *received_buf;
211
212   /**
213    * Timeout for receiving a response (absolute time).
214    */
215   struct GNUNET_TIME_Absolute receive_timeout;
216
217   /**
218    * Current value for our incremental back-off (for
219    * connect re-tries).
220    */
221   struct GNUNET_TIME_Relative back_off;
222
223   /**
224    * Number of bytes in received_buf that are valid.
225    */
226   size_t received_pos;
227
228   /**
229    * Size of received_buf.
230    */
231   unsigned int received_size;
232
233   /**
234    * Do we have a complete response in received_buf?
235    */
236   int msg_complete;
237
238   /**
239    * Are we currently busy doing receive-processing?
240    * GNUNET_YES if so, GNUNET_NO if not.
241    */
242   int in_receive;
243
244   /**
245    * Are we ignoring shutdown signals?
246    */
247   int ignore_shutdown;
248
249   /**
250    * How often have we tried to connect?
251    */
252   unsigned int attempts;
253
254 };
255
256
257 /**
258  * Try to connect to the service.
259  *
260  * @param service_name name of service to connect to
261  * @param cfg configuration to use
262  * @param attempt counter used to alternate between IP and UNIX domain sockets
263  * @return NULL on error
264  */
265 static struct GNUNET_CONNECTION_Handle *
266 do_connect (const char *service_name,
267             const struct GNUNET_CONFIGURATION_Handle *cfg,
268             unsigned int attempt)
269 {
270   struct GNUNET_CONNECTION_Handle *sock;
271   char *hostname;
272   char *unixpath;
273   unsigned long long port;
274
275   sock = NULL;
276 #if AF_UNIX
277   if (0 == (attempt % 2))
278     {
279       /* on even rounds, try UNIX */
280       unixpath = NULL;
281       if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath)))   /* We have a non-NULL unixpath, does that mean it's valid? */
282         {
283           sock =
284             GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
285           if (sock != NULL)
286             {
287 #if DEBUG_CLIENT
288               LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
289                    unixpath);
290 #endif
291               GNUNET_free (unixpath);
292               return sock;
293             }
294         }
295       GNUNET_free_non_null (unixpath);
296     }
297 #endif
298
299   if ((GNUNET_OK !=
300        GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT",
301                                               &port)) || (port > 65535)
302       || (GNUNET_OK !=
303           GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
304                                                  "HOSTNAME", &hostname)))
305     {
306       LOG (GNUNET_ERROR_TYPE_WARNING,
307            _
308            ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
309            service_name);
310       return NULL;
311     }
312   if (0 == strlen (hostname))
313     {
314       GNUNET_free (hostname);
315       LOG (GNUNET_ERROR_TYPE_WARNING,
316            _("Need a non-empty hostname for service `%s'.\n"), service_name);
317       return NULL;
318     }
319   if (port == 0)
320     {
321 #if AF_UNIX
322       if (0 != (attempt % 2))
323         {
324           /* try UNIX */
325           unixpath = NULL;
326           if ((GNUNET_OK ==
327                GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
328                                                       "UNIXPATH", &unixpath))
329               && (0 < strlen (unixpath)))
330             {
331               sock =
332                 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
333                                                                    unixpath);
334               if (sock != NULL)
335                 {
336                   GNUNET_free (unixpath);
337                   GNUNET_free (hostname);
338                   return sock;
339                 }
340             }
341           GNUNET_free_non_null (unixpath);
342         }
343 #endif
344 #if DEBUG_CLIENT
345       LOG (GNUNET_ERROR_TYPE_DEBUG,
346            "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
347            service_name);
348 #endif
349       GNUNET_free (hostname);
350       return NULL;
351     }
352
353   sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
354   GNUNET_free (hostname);
355   return sock;
356 }
357
358
359 /**
360  * Get a connection with a service.
361  *
362  * @param service_name name of the service
363  * @param cfg configuration to use
364  * @return NULL on error (service unknown to configuration)
365  */
366 struct GNUNET_CLIENT_Connection *
367 GNUNET_CLIENT_connect (const char *service_name,
368                        const struct GNUNET_CONFIGURATION_Handle *cfg)
369 {
370   struct GNUNET_CLIENT_Connection *ret;
371   struct GNUNET_CONNECTION_Handle *sock;
372
373   sock = do_connect (service_name, cfg, 0);
374   ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
375   ret->attempts = 1;
376   ret->sock = sock;
377   ret->service_name = GNUNET_strdup (service_name);
378   ret->cfg = cfg;
379   ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
380   return ret;
381 }
382
383
384 /**
385  * Configure this connection to ignore shutdown signals.
386  *
387  * @param h client handle
388  * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
389  */
390 void
391 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
392                                int do_ignore)
393 {
394   h->ignore_shutdown = do_ignore;
395   if (h->sock != NULL)
396     GNUNET_CONNECTION_ignore_shutdown (h->sock, do_ignore);
397 }
398
399
400 /**
401  * Destroy connection with the service.  This will automatically
402  * cancel any pending "receive" request (however, the handler will
403  * *NOT* be called, not even with a NULL message).  Any pending
404  * transmission request will also be cancelled UNLESS the callback for
405  * the transmission request has already been called, in which case the
406  * transmission 'finish_pending_write' argument determines whether or
407  * not the write is guaranteed to complete before the socket is fully
408  * destroyed (unless, of course, there is an error with the server in
409  * which case the message may still be lost).
410  *
411  * @param finish_pending_write should a transmission already passed to the
412  *          handle be completed?
413  * @param sock handle to the service connection
414  */
415 void
416 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
417                           int finish_pending_write)
418 {
419   if (sock->in_receive == GNUNET_YES)
420     {
421       GNUNET_CONNECTION_receive_cancel (sock->sock);
422       sock->in_receive = GNUNET_NO;
423     }
424   if (sock->th != NULL)
425     {
426       GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
427       sock->th = NULL;
428     }
429   if (NULL != sock->sock)
430     {
431       GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
432       sock->sock = NULL;
433     }
434   if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
435     {
436       GNUNET_SCHEDULER_cancel (sock->receive_task);
437       sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
438     }
439   if (sock->tag != NULL)
440     {
441       GNUNET_free (sock->tag);
442       sock->tag = NULL;
443     }
444   sock->receiver_handler = NULL;
445   GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
446   GNUNET_free (sock->service_name);
447   GNUNET_free (sock);
448 }
449
450
451 /**
452  * Check if message is complete
453  */
454 static void
455 check_complete (struct GNUNET_CLIENT_Connection *conn)
456 {
457   if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
458       (conn->received_pos >=
459        ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
460               size)))
461     conn->msg_complete = GNUNET_YES;
462 }
463
464
465 /**
466  * Callback function for data received from the network.  Note that
467  * both "available" and "errCode" would be 0 if the read simply timed out.
468  *
469  * @param cls closure
470  * @param buf pointer to received data
471  * @param available number of bytes availabe in "buf",
472  *        possibly 0 (on errors)
473  * @param addr address of the sender
474  * @param addrlen size of addr
475  * @param errCode value of errno (on errors receiving)
476  */
477 static void
478 receive_helper (void *cls, const void *buf, size_t available,
479                 const struct sockaddr *addr, socklen_t addrlen, int errCode)
480 {
481   struct GNUNET_CLIENT_Connection *conn = cls;
482   struct GNUNET_TIME_Relative remaining;
483   GNUNET_CLIENT_MessageHandler receive_handler;
484   void *receive_handler_cls;
485
486   GNUNET_assert (conn->msg_complete == GNUNET_NO);
487   conn->in_receive = GNUNET_NO;
488   if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
489     {
490       /* signal timeout! */
491 #if DEBUG_CLIENT
492       LOG (GNUNET_ERROR_TYPE_DEBUG,
493            "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
494            (unsigned int) available,
495            conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode));
496 #endif
497       if (NULL != (receive_handler = conn->receiver_handler))
498         {
499           receive_handler_cls = conn->receiver_handler_cls;
500           conn->receiver_handler = NULL;
501           receive_handler (receive_handler_cls, NULL);
502         }
503       return;
504     }
505
506   /* FIXME: optimize for common fast case where buf contains the
507    * entire message and we need no copying... */
508
509
510   /* slow path: append to array */
511   if (conn->received_size < conn->received_pos + available)
512     GNUNET_array_grow (conn->received_buf, conn->received_size,
513                        conn->received_pos + available);
514   memcpy (&conn->received_buf[conn->received_pos], buf, available);
515   conn->received_pos += available;
516   check_complete (conn);
517   /* check for timeout */
518   remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
519   if (remaining.rel_value == 0)
520     {
521       /* signal timeout! */
522       if (NULL != conn->receiver_handler)
523         conn->receiver_handler (conn->receiver_handler_cls, NULL);
524       return;
525     }
526   /* back to receive -- either for more data or to call callback! */
527   GNUNET_CLIENT_receive (conn, conn->receiver_handler,
528                          conn->receiver_handler_cls, remaining);
529 }
530
531
532 /**
533  * Continuation to call the receive callback.
534  *
535  * @param cls  our handle to the client connection
536  * @param tc scheduler context
537  */
538 static void
539 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
540 {
541   struct GNUNET_CLIENT_Connection *sock = cls;
542   GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
543   const struct GNUNET_MessageHeader *cmsg =
544     (const struct GNUNET_MessageHeader *) sock->received_buf;
545   void *handler_cls = sock->receiver_handler_cls;
546   uint16_t msize = ntohs (cmsg->size);
547   char mbuf[msize];
548   struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
549
550 #if DEBUG_CLIENT
551   LOG (GNUNET_ERROR_TYPE_DEBUG,
552        "Received message of type %u and size %u\n", ntohs (cmsg->type),
553        msize);
554 #endif
555   sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
556   GNUNET_assert (GNUNET_YES == sock->msg_complete);
557   GNUNET_assert (sock->received_pos >= msize);
558   memcpy (msg, cmsg, msize);
559   memmove (sock->received_buf, &sock->received_buf[msize],
560            sock->received_pos - msize);
561   sock->received_pos -= msize;
562   sock->msg_complete = GNUNET_NO;
563   sock->receiver_handler = NULL;
564   check_complete (sock);
565   if (handler != NULL)
566     handler (handler_cls, msg);
567 }
568
569
570 /**
571  * Read from the service.
572  *
573  * @param sock the service
574  * @param handler function to call with the message
575  * @param handler_cls closure for handler
576  * @param timeout how long to wait until timing out
577  */
578 void
579 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
580                        GNUNET_CLIENT_MessageHandler handler,
581                        void *handler_cls, struct GNUNET_TIME_Relative timeout)
582 {
583   if (sock->sock == NULL)
584     {
585       /* already disconnected, fail instantly! */
586       GNUNET_break (0);         /* this should not happen in well-written code! */
587       if (NULL != handler)
588         handler (handler_cls, NULL);
589       return;
590     }
591   sock->receiver_handler = handler;
592   sock->receiver_handler_cls = handler_cls;
593   sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
594   if (GNUNET_YES == sock->msg_complete)
595     {
596       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
597       sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
598     }
599   else
600     {
601       GNUNET_assert (sock->in_receive == GNUNET_NO);
602       sock->in_receive = GNUNET_YES;
603 #if DEBUG_CLIENT
604       LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
605 #endif
606       GNUNET_CONNECTION_receive (sock->sock,
607                                  GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, timeout,
608                                  &receive_helper, sock);
609     }
610 }
611
612
613 /**
614  * Report service unavailable.
615  */
616 static void
617 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
618 {
619   GNUNET_SCHEDULER_add_continuation (task, task_cls,
620                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
621 }
622
623
624 /**
625  * Receive confirmation from test, service is up.
626  *
627  * @param cls closure
628  * @param msg message received, NULL on timeout or fatal error
629  */
630 static void
631 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
632 {
633   struct GNUNET_CLIENT_Connection *conn = cls;
634
635   /* We may want to consider looking at the reply in more
636    * detail in the future, for example, is this the
637    * correct service? FIXME! */
638   if (msg != NULL)
639     {
640 #if DEBUG_CLIENT
641       LOG (GNUNET_ERROR_TYPE_DEBUG,
642            "Received confirmation that service is running.\n");
643 #endif
644       GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls,
645                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
646     }
647   else
648     {
649       service_test_error (conn->test_cb, conn->test_cb_cls);
650     }
651   GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
652 }
653
654
655 /**
656  * Send the 'TEST' message to the service.  If successful, prepare to
657  * receive the reply.
658  *
659  * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
660  * @param size number of bytes available in buf
661  * @param buf where to write the message
662  * @return number of bytes written to buf
663  */
664 static size_t
665 write_test (void *cls, size_t size, void *buf)
666 {
667   struct GNUNET_CLIENT_Connection *conn = cls;
668   struct GNUNET_MessageHeader *msg;
669
670   if (size < sizeof (struct GNUNET_MessageHeader))
671     {
672 #if DEBUG_CLIENT
673       LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
674 #endif
675       service_test_error (conn->test_cb, conn->test_cb_cls);
676       GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
677       return 0;                 /* client disconnected */
678     }
679 #if DEBUG_CLIENT
680   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
681 #endif
682   msg = (struct GNUNET_MessageHeader *) buf;
683   msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
684   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
685   GNUNET_CLIENT_receive (conn, &confirm_handler, conn,
686                          GNUNET_TIME_absolute_get_remaining
687                          (conn->test_deadline));
688   return sizeof (struct GNUNET_MessageHeader);
689 }
690
691
692 /**
693  * Test if the service is running.  If we are given a UNIXPATH or a local address,
694  * we do this NOT by trying to connect to the service, but by trying to BIND to
695  * the same port.  If the BIND fails, we know the service is running.
696  *
697  * @param service name of the service to wait for
698  * @param cfg configuration to use
699  * @param timeout how long to wait at most
700  * @param task task to run if service is running
701  *        (reason will be "PREREQ_DONE" (service running)
702  *         or "TIMEOUT" (service not known to be running))
703  * @param task_cls closure for task
704  */
705 void
706 GNUNET_CLIENT_service_test (const char *service,
707                             const struct GNUNET_CONFIGURATION_Handle *cfg,
708                             struct GNUNET_TIME_Relative timeout,
709                             GNUNET_SCHEDULER_Task task, void *task_cls)
710 {
711   char *hostname;
712   unsigned long long port;
713   struct GNUNET_NETWORK_Handle *sock;
714   struct GNUNET_CLIENT_Connection *conn;
715
716 #if DEBUG_CLIENT
717   LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
718        service);
719 #endif
720 #ifdef AF_UNIX
721   {
722     /* probe UNIX support */
723     struct sockaddr_un s_un;
724     size_t slen;
725     char *unixpath;
726
727     unixpath = NULL;
728     if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath)))  /* We have a non-NULL unixpath, does that mean it's valid? */
729       {
730         if (strlen (unixpath) >= sizeof (s_un.sun_path))
731           {
732             LOG (GNUNET_ERROR_TYPE_WARNING,
733                  _("UNIXPATH `%s' too long, maximum length is %llu\n"),
734                  unixpath, sizeof (s_un.sun_path));
735           }
736         else
737           {
738             sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
739             if (sock != NULL)
740               {
741                 memset (&s_un, 0, sizeof (s_un));
742                 s_un.sun_family = AF_UNIX;
743                 slen = strlen (unixpath) + 1;
744                 if (slen >= sizeof (s_un.sun_path))
745                   slen = sizeof (s_un.sun_path) - 1;
746                 memcpy (s_un.sun_path, unixpath, slen);
747                 s_un.sun_path[slen] = '\0';
748                 slen = sizeof (struct sockaddr_un);
749 #if LINUX
750                 s_un.sun_path[0] = '\0';
751 #endif
752 #if HAVE_SOCKADDR_IN_SIN_LEN
753                 s_un.sun_len = (u_char) slen;
754 #endif
755                 if (GNUNET_OK !=
756                     GNUNET_NETWORK_socket_bind (sock,
757                                                 (const struct sockaddr *)
758                                                 &s_un, slen))
759                   {
760                     /* failed to bind => service must be running */
761                     GNUNET_free (unixpath);
762                     (void) GNUNET_NETWORK_socket_close (sock);
763                     GNUNET_SCHEDULER_add_continuation (task, task_cls,
764                                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
765                     return;
766                   }
767                 (void) GNUNET_NETWORK_socket_close (sock);
768               }
769             /* let's try IP */
770           }
771       }
772     GNUNET_free_non_null (unixpath);
773   }
774 #endif
775
776   hostname = NULL;
777   if ((GNUNET_OK !=
778        GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
779       (port > 65535) ||
780       (GNUNET_OK !=
781        GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
782                                               &hostname)))
783     {
784       /* UNIXPATH failed (if possible) AND IP failed => error */
785       service_test_error (task, task_cls);
786       return;
787     }
788
789   if (0 == strcmp ("localhost", hostname)
790 #if !LINUX
791       && 0
792 #endif
793     )
794     {
795       /* can test using 'bind' */
796       struct sockaddr_in s_in;
797
798       memset (&s_in, 0, sizeof (s_in));
799 #if HAVE_SOCKADDR_IN_SIN_LEN
800       s_in.sin_len = sizeof (struct sockaddr_in);
801 #endif
802       s_in.sin_family = AF_INET;
803       s_in.sin_port = htons (port);
804
805       sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
806       if (sock != NULL)
807         {
808           if (GNUNET_OK !=
809               GNUNET_NETWORK_socket_bind (sock,
810                                           (const struct sockaddr *) &s_in,
811                                           sizeof (s_in)))
812             {
813               /* failed to bind => service must be running */
814               GNUNET_free (hostname);
815               (void) GNUNET_NETWORK_socket_close (sock);
816               GNUNET_SCHEDULER_add_continuation (task, task_cls,
817                                                  GNUNET_SCHEDULER_REASON_PREREQ_DONE);
818               return;
819             }
820           (void) GNUNET_NETWORK_socket_close (sock);
821         }
822     }
823
824   if (0 == strcmp ("ip6-localhost", hostname)
825 #if !LINUX
826       && 0
827 #endif
828     )
829     {
830       /* can test using 'bind' */
831       struct sockaddr_in6 s_in6;
832
833       memset (&s_in6, 0, sizeof (s_in6));
834 #if HAVE_SOCKADDR_IN_SIN_LEN
835       s_in6.sin6_len = sizeof (struct sockaddr_in6);
836 #endif
837       s_in6.sin6_family = AF_INET6;
838       s_in6.sin6_port = htons (port);
839
840       sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
841       if (sock != NULL)
842         {
843           if (GNUNET_OK !=
844               GNUNET_NETWORK_socket_bind (sock,
845                                           (const struct sockaddr *) &s_in6,
846                                           sizeof (s_in6)))
847             {
848               /* failed to bind => service must be running */
849               GNUNET_free (hostname);
850               (void) GNUNET_NETWORK_socket_close (sock);
851               GNUNET_SCHEDULER_add_continuation (task, task_cls,
852                                                  GNUNET_SCHEDULER_REASON_PREREQ_DONE);
853               return;
854             }
855           (void) GNUNET_NETWORK_socket_close (sock);
856         }
857     }
858
859   if (((0 == strcmp ("localhost", hostname)) ||
860        (0 == strcmp ("ip6-localhost", hostname)))
861 #if !LINUX
862       && 0
863 #endif
864     )
865     {
866       /* all binds succeeded => claim service not running right now */
867       GNUNET_free_non_null (hostname);
868       service_test_error (task, task_cls);
869       return;
870     }
871   GNUNET_free_non_null (hostname);
872
873   /* non-localhost, try 'connect' method */
874   conn = GNUNET_CLIENT_connect (service, cfg);
875   if (conn == NULL)
876     {
877       LOG (GNUNET_ERROR_TYPE_INFO,
878            _("Could not connect to service `%s', must not be running.\n"),
879            service);
880       service_test_error (task, task_cls);
881       return;
882     }
883   conn->test_cb = task;
884   conn->test_cb_cls = task_cls;
885   conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
886
887   if (NULL ==
888       GNUNET_CLIENT_notify_transmit_ready (conn,
889                                            sizeof (struct
890                                                    GNUNET_MessageHeader),
891                                            timeout, GNUNET_YES, &write_test,
892                                            conn))
893     {
894       LOG (GNUNET_ERROR_TYPE_WARNING,
895            _("Failure to transmit request to service `%s'\n"), service);
896       service_test_error (task, task_cls);
897       GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
898       return;
899     }
900 }
901
902
903 /**
904  * Connection notifies us about failure or success of
905  * a transmission request.  Either pass it on to our
906  * user or, if possible, retry.
907  *
908  * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
909  * @param size number of bytes available for transmission
910  * @param buf where to write them
911  * @return number of bytes written to buf
912  */
913 static size_t client_notify (void *cls, size_t size, void *buf);
914
915
916 /**
917  * This task is run if we should re-try connection to the
918  * service after a while.
919  *
920  * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
921  * @param tc unused
922  */
923 static void
924 client_delayed_retry (void *cls,
925                       const struct GNUNET_SCHEDULER_TaskContext *tc)
926 {
927   struct GNUNET_CLIENT_TransmitHandle *th = cls;
928   struct GNUNET_TIME_Relative delay;
929
930   th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
931   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
932     {
933 #if DEBUG_CLIENT
934       LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n");
935 #endif
936       th->sock->th = NULL;
937       th->notify (th->notify_cls, 0, NULL);
938       GNUNET_free (th);
939       return;
940     }
941   th->sock->sock =
942     do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++);
943   if (NULL == th->sock->sock)
944     {
945       /* could happen if we're out of sockets */
946       delay =
947         GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
948                                   (th->timeout), th->sock->back_off);
949       th->sock->back_off =
950         GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
951                                   (th->sock->back_off, 2),
952                                   GNUNET_TIME_UNIT_SECONDS);
953 #if DEBUG_CLIENT
954       LOG (GNUNET_ERROR_TYPE_DEBUG,
955            "Transmission failed %u times, trying again in %llums.\n",
956            MAX_ATTEMPTS - th->attempts_left,
957            (unsigned long long) delay.rel_value);
958 #endif
959       th->reconnect_task =
960         GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
961       return;
962     }
963   GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
964                                      th->sock->ignore_shutdown);
965   th->th =
966     GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size,
967                                              GNUNET_TIME_absolute_get_remaining
968                                              (th->timeout), &client_notify,
969                                              th);
970   if (th->th == NULL)
971     {
972       GNUNET_break (0);
973       th->sock->th = NULL;
974       th->notify (th->notify_cls, 0, NULL);
975       GNUNET_free (th);
976       return;
977     }
978 }
979
980
981 /**
982  * Connection notifies us about failure or success of a transmission
983  * request.  Either pass it on to our user or, if possible, retry.
984  *
985  * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
986  * @param size number of bytes available for transmission
987  * @param buf where to write them
988  * @return number of bytes written to buf
989  */
990 static size_t
991 client_notify (void *cls, size_t size, void *buf)
992 {
993   struct GNUNET_CLIENT_TransmitHandle *th = cls;
994   size_t ret;
995   struct GNUNET_TIME_Relative delay;
996
997   th->th = NULL;
998   th->sock->th = NULL;
999   if (buf == NULL)
1000     {
1001       delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1002       delay.rel_value /= 2;
1003       if ((0 !=
1004            (GNUNET_SCHEDULER_REASON_SHUTDOWN &
1005             GNUNET_SCHEDULER_get_reason ())) || (GNUNET_YES != th->auto_retry)
1006           || (0 == --th->attempts_left) || (delay.rel_value < 1))
1007         {
1008 #if DEBUG_CLIENT
1009           LOG (GNUNET_ERROR_TYPE_DEBUG,
1010                "Transmission failed %u times, giving up.\n",
1011                MAX_ATTEMPTS - th->attempts_left);
1012 #endif
1013           GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1014           GNUNET_free (th);
1015           return 0;
1016         }
1017       /* auto-retry */
1018 #if DEBUG_CLIENT
1019       LOG (GNUNET_ERROR_TYPE_DEBUG,
1020            "Failed to connect to `%s', automatically trying again.\n",
1021            th->sock->service_name);
1022 #endif
1023       GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
1024       th->sock->sock = NULL;
1025       delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
1026       th->sock->back_off =
1027         GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1028                                   (th->sock->back_off, 2),
1029                                   GNUNET_TIME_UNIT_SECONDS);
1030 #if DEBUG_CLIENT
1031       LOG (GNUNET_ERROR_TYPE_DEBUG,
1032            "Transmission failed %u times, trying again in %llums.\n",
1033            MAX_ATTEMPTS - th->attempts_left,
1034            (unsigned long long) delay.rel_value);
1035 #endif
1036       th->sock->th = th;
1037       th->reconnect_task =
1038         GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1039       return 0;
1040     }
1041   GNUNET_assert (size >= th->size);
1042   ret = th->notify (th->notify_cls, size, buf);
1043   GNUNET_free (th);
1044   return ret;
1045 }
1046
1047
1048 /**
1049  * Ask the client to call us once the specified number of bytes
1050  * are free in the transmission buffer.  May call the notify
1051  * method immediately if enough space is available.
1052  *
1053  * @param sock connection to the service
1054  * @param size number of bytes to send
1055  * @param timeout after how long should we give up (and call
1056  *        notify with buf NULL and size 0)?
1057  * @param auto_retry if the connection to the service dies, should we
1058  *        automatically re-connect and retry (within the timeout period)
1059  *        or should we immediately fail in this case?  Pass GNUNET_YES
1060  *        if the caller does not care about temporary connection errors,
1061  *        for example because the protocol is stateless
1062  * @param notify function to call
1063  * @param notify_cls closure for notify
1064  * @return NULL if our buffer will never hold size bytes,
1065  *         a handle if the notify callback was queued (can be used to cancel)
1066  */
1067 struct GNUNET_CLIENT_TransmitHandle *
1068 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1069                                      size_t size,
1070                                      struct GNUNET_TIME_Relative timeout,
1071                                      int auto_retry,
1072                                      GNUNET_CONNECTION_TransmitReadyNotify
1073                                      notify, void *notify_cls)
1074 {
1075   struct GNUNET_CLIENT_TransmitHandle *th;
1076
1077   if (NULL != sock->th)
1078     {
1079       /* If this breaks, you most likley called this function twice without waiting
1080        * for completion or canceling the request */
1081       GNUNET_break (0);
1082       return NULL;
1083     }
1084   th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1085   th->sock = sock;
1086   th->size = size;
1087   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1088   th->auto_retry = auto_retry;
1089   th->notify = notify;
1090   th->notify_cls = notify_cls;
1091   th->attempts_left = MAX_ATTEMPTS;
1092   sock->th = th;
1093   if (sock->sock == NULL)
1094     {
1095       th->reconnect_task =
1096         GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry,
1097                                       th);
1098
1099     }
1100   else
1101     {
1102       th->th =
1103         GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout,
1104                                                  &client_notify, th);
1105       if (NULL == th->th)
1106         {
1107           GNUNET_break (0);
1108           GNUNET_free (th);
1109           sock->th = NULL;
1110           return NULL;
1111         }
1112     }
1113   return th;
1114 }
1115
1116
1117 /**
1118  * Cancel a request for notification.
1119  *
1120  * @param th handle from the original request.
1121  */
1122 void
1123 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
1124                                             GNUNET_CLIENT_TransmitHandle *th)
1125 {
1126   if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1127     {
1128       GNUNET_assert (NULL == th->th);
1129       GNUNET_SCHEDULER_cancel (th->reconnect_task);
1130       th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1131     }
1132   else
1133     {
1134       GNUNET_assert (NULL != th->th);
1135       GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1136     }
1137   th->sock->th = NULL;
1138   GNUNET_free (th);
1139 }
1140
1141
1142 /**
1143  * Function called to notify a client about the socket
1144  * begin ready to queue the message.  "buf" will be
1145  * NULL and "size" zero if the socket was closed for
1146  * writing in the meantime.
1147  *
1148  * @param cls closure of type "struct TransmitGetResponseContext*"
1149  * @param size number of bytes available in buf
1150  * @param buf where the callee should write the message
1151  * @return number of bytes written to buf
1152  */
1153 static size_t
1154 transmit_for_response (void *cls, size_t size, void *buf)
1155 {
1156   struct TransmitGetResponseContext *tc = cls;
1157   uint16_t msize;
1158
1159   tc->sock->tag = NULL;
1160   msize = ntohs (tc->hdr->size);
1161   if (NULL == buf)
1162     {
1163 #if DEBUG_CLIENT
1164       LOG (GNUNET_ERROR_TYPE_DEBUG,
1165            _
1166            ("Could not submit request, not expecting to receive a response.\n"));
1167 #endif
1168       if (NULL != tc->rn)
1169         tc->rn (tc->rn_cls, NULL);
1170       GNUNET_free (tc);
1171       return 0;
1172     }
1173   GNUNET_assert (size >= msize);
1174   memcpy (buf, tc->hdr, msize);
1175   GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls,
1176                          GNUNET_TIME_absolute_get_remaining (tc->timeout));
1177   GNUNET_free (tc);
1178   return msize;
1179 }
1180
1181
1182 /**
1183  * Convenience API that combines sending a request
1184  * to the service and waiting for a response.
1185  * If either operation times out, the callback
1186  * will be called with a "NULL" response (in which
1187  * case the connection should probably be destroyed).
1188  *
1189  * @param sock connection to use
1190  * @param hdr message to transmit
1191  * @param timeout when to give up (for both transmission
1192  *         and for waiting for a response)
1193  * @param auto_retry if the connection to the service dies, should we
1194  *        automatically re-connect and retry (within the timeout period)
1195  *        or should we immediately fail in this case?  Pass GNUNET_YES
1196  *        if the caller does not care about temporary connection errors,
1197  *        for example because the protocol is stateless
1198  * @param rn function to call with the response
1199  * @param rn_cls closure for rn
1200  * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1201  *         is already pending
1202  */
1203 int
1204 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1205                                          *sock,
1206                                          const struct GNUNET_MessageHeader
1207                                          *hdr,
1208                                          struct GNUNET_TIME_Relative timeout,
1209                                          int auto_retry,
1210                                          GNUNET_CLIENT_MessageHandler rn,
1211                                          void *rn_cls)
1212 {
1213   struct TransmitGetResponseContext *tc;
1214   uint16_t msize;
1215
1216   if (NULL != sock->th)
1217     return GNUNET_SYSERR;
1218   GNUNET_assert (sock->tag == NULL);
1219   msize = ntohs (hdr->size);
1220   tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1221   tc->sock = sock;
1222   tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1223   memcpy (&tc[1], hdr, msize);
1224   tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1225   tc->rn = rn;
1226   tc->rn_cls = rn_cls;
1227   if (NULL ==
1228       GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry,
1229                                            &transmit_for_response, tc))
1230     {
1231       GNUNET_break (0);
1232       GNUNET_free (tc);
1233       return GNUNET_SYSERR;
1234     }
1235   sock->tag = tc;
1236   return GNUNET_OK;
1237 }
1238
1239
1240
1241 /*  end of client.c */