summaryrefslogtreecommitdiff
path: root/src/util/client.c
blob: 4e5eca32a72b0c0c881344045ffe7d811632ce49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
/*
     This file is part of GNUnet.
     Copyright (C) 2001-2016, 2019 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file util/client.c
 * @brief code for access to services
 * @author Christian Grothoff
 *
 * Generic TCP code for reliable, record-oriented TCP
 * connections between clients and service providers.
 */
#include "platform.h"
#include "gnunet_protocols.h"
#include "gnunet_util_lib.h"
#include "gnunet_resolver_service.h"
#include "gnunet_socks.h"


#define LOG(kind, ...) GNUNET_log_from (kind, "util-client", __VA_ARGS__)

/**
 * Timeout we use on TCP connect before trying another
 * result from the DNS resolver.  Actual value used
 * is this value divided by the number of address families.
 * Default is 5s.
 */
#define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply ( \
    GNUNET_TIME_UNIT_SECONDS, 5)


/**
 * Internal state for a client connected to a GNUnet service.
 */
struct ClientState;


/**
 * During connect, we try multiple possible IP addresses
 * to find out which one might work.
 */
struct AddressProbe
{
  /**
   * This is a linked list.
   */
  struct AddressProbe *next;

  /**
   * This is a doubly-linked list.
   */
  struct AddressProbe *prev;

  /**
   * The address; do not free (allocated at the end of this struct).
   */
  const struct sockaddr *addr;

  /**
   * Underlying OS's socket.
   */
  struct GNUNET_NETWORK_Handle *sock;

  /**
   * Connection for which we are probing.
   */
  struct ClientState *cstate;

  /**
   * Length of addr.
   */
  socklen_t addrlen;

  /**
   * Task waiting for the connection to finish connecting.
   */
  struct GNUNET_SCHEDULER_Task *task;
};


/**
 * Internal state for a client connected to a GNUnet service.
 */
struct ClientState
{
  /**
   * The connection handle, NULL if not live
   */
  struct GNUNET_NETWORK_Handle *sock;

  /**
   * Handle to a pending DNS lookup request, NULL if DNS is finished.
   */
  struct GNUNET_RESOLVER_RequestHandle *dns_active;

  /**
   * Our configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Linked list of sockets we are currently trying out
   * (during connect).
   */
  struct AddressProbe *ap_head;

  /**
   * Linked list of sockets we are currently trying out
   * (during connect).
   */
  struct AddressProbe *ap_tail;

  /**
   * Name of the service we interact with.
   */
  char *service_name;

  /**
   * Hostname, if any.
   */
  char *hostname;

  /**
   * Next message to transmit to the service. NULL for none.
   */
  const struct GNUNET_MessageHeader *msg;

  /**
   * Task for trying to connect to the service.
   */
  struct GNUNET_SCHEDULER_Task *retry_task;

  /**
   * Task for sending messages to the service.
   */
  struct GNUNET_SCHEDULER_Task *send_task;

  /**
   * Task for sending messages to the service.
   */
  struct GNUNET_SCHEDULER_Task *recv_task;

  /**
   * Tokenizer for inbound messages.
   */
  struct GNUNET_MessageStreamTokenizer *mst;

  /**
   * Message queue under our control.
   */
  struct GNUNET_MQ_Handle *mq;

  /**
   * Timeout for receiving a response (absolute time).
   */
  struct GNUNET_TIME_Absolute receive_timeout;

  /**
   * Current value for our incremental back-off (for
   * connect re-tries).
   */
  struct GNUNET_TIME_Relative back_off;

  /**
   * TCP port (0 for disabled).
   */
  unsigned long long port;

  /**
   * Offset in the message where we are for transmission.
   */
  size_t msg_off;

  /**
   * How often have we tried to connect?
   */
  unsigned int attempts;

  /**
   * Are we supposed to die?  #GNUNET_SYSERR if destruction must be
   * deferred, #GNUNET_NO by default, #GNUNET_YES if destruction was
   * deferred.
   */
  int in_destroy;
};


/**
 * Try to connect to the service.
 *
 * @param cls the `struct ClientState` to try to connect to the service
 */
static void
start_connect (void *cls);


/**
 * We've failed for good to establish a connection (timeout or
 * no more addresses to try).
 *
 * @param cstate the connection we tried to establish
 */
static void
connect_fail_continuation (struct ClientState *cstate)
{
  GNUNET_break (NULL == cstate->ap_head);
  GNUNET_break (NULL == cstate->ap_tail);
  GNUNET_break (NULL == cstate->dns_active);
  GNUNET_break (NULL == cstate->sock);
  GNUNET_assert (NULL == cstate->send_task);
  GNUNET_assert (NULL == cstate->recv_task);
  // GNUNET_assert (NULL == cstate->proxy_handshake);

  cstate->back_off = GNUNET_TIME_STD_BACKOFF (cstate->back_off);
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Failed to establish connection to `%s', no further addresses to try, will try again in %s.\n",
       cstate->service_name,
       GNUNET_STRINGS_relative_time_to_string (cstate->back_off,
                                               GNUNET_YES));
  cstate->retry_task
    = GNUNET_SCHEDULER_add_delayed (cstate->back_off,
                                    &start_connect,
                                    cstate);
}


/**
 * We are ready to send a message to the service.
 *
 * @param cls the `struct ClientState` with the `msg` to transmit
 */
static void
transmit_ready (void *cls)
{
  struct ClientState *cstate = cls;
  ssize_t ret;
  size_t len;
  const char *pos;
  int notify_in_flight;

  cstate->send_task = NULL;
  if (GNUNET_YES == cstate->in_destroy)
    return;
  pos = (const char *) cstate->msg;
  len = ntohs (cstate->msg->size);
  GNUNET_assert (cstate->msg_off < len);
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "message of type %u trying to send with socket %p (MQ: %p\n",
       ntohs (cstate->msg->type),
       cstate->sock,
       cstate->mq);

RETRY:
  ret = GNUNET_NETWORK_socket_send (cstate->sock,
                                    &pos[cstate->msg_off],
                                    len - cstate->msg_off);
  if ( (-1 == ret) &&
       ( (EAGAIN == errno) ||
         (EINTR == errno) ) )
  {
    /* ignore */
    ret = 0;
  }
  if (-1 == ret)
  {
    LOG (GNUNET_ERROR_TYPE_WARNING,
         "Error during sending message of type %u: %s\n",
         ntohs (cstate->msg->type),
         strerror (errno));
    if (EINTR == errno)
    {
      LOG (GNUNET_ERROR_TYPE_DEBUG,
           "Retrying message of type %u\n",
           ntohs (cstate->msg->type));
      goto RETRY;
    }
    GNUNET_MQ_inject_error (cstate->mq,
                            GNUNET_MQ_ERROR_WRITE);
    return;
  }
  notify_in_flight = (0 == cstate->msg_off);
  cstate->msg_off += ret;
  if (cstate->msg_off < len)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "rescheduling message of type %u\n",
         ntohs (cstate->msg->type));
    cstate->send_task
      = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                        cstate->sock,
                                        &transmit_ready,
                                        cstate);
    if (notify_in_flight)
      GNUNET_MQ_impl_send_in_flight (cstate->mq);
    return;
  }
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "sending message of type %u successful\n",
       ntohs (cstate->msg->type));
  cstate->msg = NULL;
  GNUNET_MQ_impl_send_continue (cstate->mq);
}


/**
 * We have received a full message, pass to the MQ dispatcher.
 * Called by the tokenizer via #receive_ready().
 *
 * @param cls the `struct ClientState`
 * @param msg message we received.
 * @return #GNUNET_OK on success,
 *     #GNUNET_NO to stop further processing due to disconnect (no error)
 *     #GNUNET_SYSERR to stop further processing due to error
 */
static int
recv_message (void *cls,
              const struct GNUNET_MessageHeader *msg)
{
  struct ClientState *cstate = cls;

  if (GNUNET_YES == cstate->in_destroy)
    return GNUNET_NO;
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Received message of type %u and size %u from %s\n",
       ntohs (msg->type),
       ntohs (msg->size),
       cstate->service_name);
  GNUNET_MQ_inject_message (cstate->mq,
                            msg);
  if (GNUNET_YES == cstate->in_destroy)
    return GNUNET_NO;
  return GNUNET_OK;
}


/**
 * Cancel all remaining connect attempts
 *
 * @param cstate handle of the client state to process
 */
static void
cancel_aps (struct ClientState *cstate)
{
  struct AddressProbe *pos;

  while (NULL != (pos = cstate->ap_head))
  {
    GNUNET_break (GNUNET_OK ==
                  GNUNET_NETWORK_socket_close (pos->sock));
    GNUNET_SCHEDULER_cancel (pos->task);
    GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
                                 cstate->ap_tail,
                                 pos);
    GNUNET_free (pos);
  }
}


/**
 * Implement the destruction of a message queue.  Implementations must
 * not free @a mq, but should take care of @a impl_state.
 *
 * @param mq the message queue to destroy
 * @param impl_state our `struct ClientState`
 */
static void
connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
                                void *impl_state)
{
  struct ClientState *cstate = impl_state;

  (void) mq;
  if (NULL != cstate->dns_active)
  {
    GNUNET_RESOLVER_request_cancel (cstate->dns_active);
    cstate->dns_active = NULL;
  }
  if (NULL != cstate->send_task)
  {
    GNUNET_SCHEDULER_cancel (cstate->send_task);
    cstate->send_task = NULL;
  }
  if (NULL != cstate->retry_task)
  {
    GNUNET_SCHEDULER_cancel (cstate->retry_task);
    cstate->retry_task = NULL;
  }
  if (GNUNET_SYSERR == cstate->in_destroy)
  {
    /* defer destruction */
    cstate->in_destroy = GNUNET_YES;
    cstate->mq = NULL;
    return;
  }
  if (NULL != cstate->recv_task)
  {
    GNUNET_SCHEDULER_cancel (cstate->recv_task);
    cstate->recv_task = NULL;
  }
  if (NULL != cstate->sock)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "destroying socket: %p\n",
         cstate->sock);
    GNUNET_NETWORK_socket_close (cstate->sock);
  }
  cancel_aps (cstate);
  GNUNET_free (cstate->service_name);
  GNUNET_free (cstate->hostname);
  GNUNET_MST_destroy (cstate->mst);
  GNUNET_free (cstate);
}


/**
 * This function is called once we have data ready to read.
 *
 * @param cls `struct ClientState` with connection to read from
 */
static void
receive_ready (void *cls)
{
  struct ClientState *cstate = cls;
  int ret;

  cstate->recv_task = NULL;
  cstate->in_destroy = GNUNET_SYSERR;
  ret = GNUNET_MST_read (cstate->mst,
                         cstate->sock,
                         GNUNET_NO,
                         GNUNET_NO);
  if (GNUNET_SYSERR == ret)
  {
    if (NULL != cstate->mq)
      GNUNET_MQ_inject_error (cstate->mq,
                              GNUNET_MQ_ERROR_READ);
    if (GNUNET_YES == cstate->in_destroy)
      connection_client_destroy_impl (cstate->mq,
                                      cstate);
    return;
  }
  if (GNUNET_YES == cstate->in_destroy)
  {
    connection_client_destroy_impl (cstate->mq,
                                    cstate);
    return;
  }
  cstate->in_destroy = GNUNET_NO;
  cstate->recv_task
    = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                     cstate->sock,
                                     &receive_ready,
                                     cstate);
}


/**
 * We've succeeded in establishing a connection.
 *
 * @param cstate the connection we tried to establish
 */
static void
connect_success_continuation (struct ClientState *cstate)
{
  GNUNET_assert (NULL == cstate->recv_task);
  cstate->recv_task
    = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                     cstate->sock,
                                     &receive_ready,
                                     cstate);
  if (NULL != cstate->msg)
  {
    GNUNET_assert (NULL == cstate->send_task);
    cstate->send_task
      = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                        cstate->sock,
                                        &transmit_ready,
                                        cstate);
  }
}


/**
 * Try connecting to the server using UNIX domain sockets.
 *
 * @param service_name name of service to connect to
 * @param cfg configuration to use
 * @return NULL on error, socket connected to UNIX otherwise
 */
static struct GNUNET_NETWORK_Handle *
try_unixpath (const char *service_name,
              const struct GNUNET_CONFIGURATION_Handle *cfg)
{
#if AF_UNIX
  struct GNUNET_NETWORK_Handle *sock;
  char *unixpath;
  struct sockaddr_un s_un;

  unixpath = NULL;
  if ((GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_filename (cfg,
                                                service_name,
                                                "UNIXPATH",
                                                &unixpath)) &&
      (0 < strlen (unixpath)))
  {
    /* We have a non-NULL unixpath, need to validate it */
    if (strlen (unixpath) >= sizeof(s_un.sun_path))
    {
      LOG (GNUNET_ERROR_TYPE_WARNING,
           _ ("UNIXPATH `%s' too long, maximum length is %llu\n"),
           unixpath,
           (unsigned long long) sizeof(s_un.sun_path));
      unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
      LOG (GNUNET_ERROR_TYPE_INFO,
           _ ("Using `%s' instead\n"),
           unixpath);
      if (NULL == unixpath)
        return NULL;
    }
    memset (&s_un,
            0,
            sizeof(s_un));
    s_un.sun_family = AF_UNIX;
    GNUNET_strlcpy (s_un.sun_path,
                    unixpath,
                    sizeof(s_un.sun_path));
#if HAVE_SOCKADDR_UN_SUN_LEN
    s_un.sun_len = (u_char) sizeof(struct sockaddr_un);
#endif
    sock = GNUNET_NETWORK_socket_create (AF_UNIX,
                                         SOCK_STREAM,
                                         0);
    if ((NULL != sock) &&
        ((GNUNET_OK ==
          GNUNET_NETWORK_socket_connect (sock,
                                         (struct sockaddr *) &s_un,
                                         sizeof(s_un))) ||
         (EINPROGRESS == errno)))
    {
      LOG (GNUNET_ERROR_TYPE_DEBUG,
           "Successfully connected to unixpath `%s'!\n",
           unixpath);
      GNUNET_free (unixpath);
      return sock;
    }
    if (NULL != sock)
      GNUNET_NETWORK_socket_close (sock);
  }
  GNUNET_free (unixpath);
#endif
  return NULL;
}


/**
 * Scheduler let us know that we're either ready to write on the
 * socket OR connect timed out.  Do the right thing.
 *
 * @param cls the `struct AddressProbe *` with the address that we are probing
 */
static void
connect_probe_continuation (void *cls)
{
  struct AddressProbe *ap = cls;
  struct ClientState *cstate = ap->cstate;
  const struct GNUNET_SCHEDULER_TaskContext *tc;
  int error;
  socklen_t len;

  ap->task = NULL;
  GNUNET_assert (NULL != ap->sock);
  GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
                               cstate->ap_tail,
                               ap);
  len = sizeof(error);
  error = 0;
  tc = GNUNET_SCHEDULER_get_task_context ();
  if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
      (GNUNET_OK !=
       GNUNET_NETWORK_socket_getsockopt (ap->sock,
                                         SOL_SOCKET,
                                         SO_ERROR,
                                         &error,
                                         &len)) ||
      (0 != error))
  {
    GNUNET_break (GNUNET_OK ==
                  GNUNET_NETWORK_socket_close (ap->sock));
    GNUNET_free (ap);
    if ((NULL == cstate->ap_head) &&
        //	 (NULL == cstate->proxy_handshake) &&
        (NULL == cstate->dns_active))
      connect_fail_continuation (cstate);
    return;
  }
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Connection to `%s' succeeded!\n",
       cstate->service_name);
  /* trigger jobs that waited for the connection */
  GNUNET_assert (NULL == cstate->sock);
  cstate->sock = ap->sock;
  GNUNET_free (ap);
  cancel_aps (cstate);
  connect_success_continuation (cstate);
}


/**
 * Try to establish a connection given the specified address.
 * This function is called by the resolver once we have a DNS reply.
 *
 * @param cls our `struct ClientState *`
 * @param addr address to try, NULL for "last call"
 * @param addrlen length of @a addr
 */
static void
try_connect_using_address (void *cls,
                           const struct sockaddr *addr,
                           socklen_t addrlen)
{
  struct ClientState *cstate = cls;
  struct AddressProbe *ap;

  if (NULL == addr)
  {
    cstate->dns_active = NULL;
    if ((NULL == cstate->ap_head) &&
        //  (NULL == cstate->proxy_handshake) &&
        (NULL == cstate->sock))
      connect_fail_continuation (cstate);
    return;
  }
  if (NULL != cstate->sock)
    return;                     /* already connected */
  /* try to connect */
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Trying to connect using address `%s:%u'\n",
       GNUNET_a2s (addr,
                   addrlen),
       (unsigned int) cstate->port);
  ap = GNUNET_malloc (sizeof(struct AddressProbe) + addrlen);
  ap->addr = (const struct sockaddr *) &ap[1];
  GNUNET_memcpy (&ap[1],
                 addr,
                 addrlen);
  ap->addrlen = addrlen;
  ap->cstate = cstate;

  switch (ap->addr->sa_family)
  {
  case AF_INET:
    ((struct sockaddr_in *) ap->addr)->sin_port = htons (cstate->port);
    break;

  case AF_INET6:
    ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (cstate->port);
    break;

  default:
    GNUNET_break (0);
    GNUNET_free (ap);
    return;                     /* not supported by us */
  }
  ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family,
                                           SOCK_STREAM,
                                           0);
  if (NULL == ap->sock)
  {
    GNUNET_free (ap);
    return;                     /* not supported by OS */
  }
  if ((GNUNET_OK !=
       GNUNET_NETWORK_socket_connect (ap->sock,
                                      ap->addr,
                                      ap->addrlen)) &&
      (EINPROGRESS != errno))
  {
    /* maybe refused / unsupported address, try next */
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
                         "connect");
    GNUNET_break (GNUNET_OK ==
                  GNUNET_NETWORK_socket_close (ap->sock));
    GNUNET_free (ap);
    return;
  }
  GNUNET_CONTAINER_DLL_insert (cstate->ap_head,
                               cstate->ap_tail,
                               ap);
  ap->task = GNUNET_SCHEDULER_add_write_net (CONNECT_RETRY_TIMEOUT,
                                             ap->sock,
                                             &connect_probe_continuation,
                                             ap);
}


/**
 * Test whether the configuration has proper values for connection
 * (UNIXPATH || (PORT && HOSTNAME)).
 *
 * @param service_name name of service to connect to
 * @param cfg configuration to use
 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not
 */
static int
test_service_configuration (const char *service_name,
                            const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  int ret = GNUNET_SYSERR;
  char *hostname = NULL;
  unsigned long long port;

#if AF_UNIX
  char *unixpath = NULL;

  if ((GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_filename (cfg,
                                                service_name,
                                                "UNIXPATH",
                                                &unixpath)) &&
      (0 < strlen (unixpath)))
    ret = GNUNET_OK;
  else if ((GNUNET_OK ==
            GNUNET_CONFIGURATION_have_value (cfg,
                                             service_name,
                                             "UNIXPATH")))
  {
    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
                               service_name,
                               "UNIXPATH",
                               _ ("not a valid filename"));
    GNUNET_free (unixpath);
    return GNUNET_SYSERR;   /* UNIXPATH specified but invalid! */
  }
  GNUNET_free (unixpath);
#endif

  if ((GNUNET_YES ==
       GNUNET_CONFIGURATION_have_value (cfg,
                                        service_name,
                                        "PORT")) &&
      (GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_number (cfg,
                                              service_name,
                                              "PORT",
                                              &port)) &&
      (port <= 65535) &&
      (0 != port) &&
      (GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_string (cfg,
                                              service_name,
                                              "HOSTNAME",
                                              &hostname)) &&
      (0 != strlen (hostname)))
    ret = GNUNET_OK;
  GNUNET_free (hostname);
  return ret;
}


/**
 * Try to connect to the service.
 *
 * @param cls the `struct ClientState` to try to connect to the service
 */
static void
start_connect (void *cls)
{
  struct ClientState *cstate = cls;

  cstate->retry_task = NULL;
#if 0
  /* Never use a local source if a proxy is configured */
  if (GNUNET_YES ==
      GNUNET_SOCKS_check_service (cstate->service_name,
                                  cstate->cfg))
  {
    socks_connect (cstate);
    return;
  }
#endif

  if ((0 == (cstate->attempts++ % 2)) ||
      (0 == cstate->port) ||
      (NULL == cstate->hostname))
  {
    /* on even rounds, try UNIX first, or always
       if we do not have a DNS name and TCP port. */
    cstate->sock = try_unixpath (cstate->service_name,
                                 cstate->cfg);
    if (NULL != cstate->sock)
    {
      connect_success_continuation (cstate);
      return;
    }
  }
  if ((NULL == cstate->hostname) ||
      (0 == cstate->port))
  {
    /* All options failed. Boo! */
    connect_fail_continuation (cstate);
    return;
  }
  cstate->dns_active
    = GNUNET_RESOLVER_ip_get (cstate->hostname,
                              AF_UNSPEC,
                              CONNECT_RETRY_TIMEOUT,
                              &try_connect_using_address,
                              cstate);
}


/**
 * Implements the transmission functionality of a message queue.
 *
 * @param mq the message queue
 * @param msg the message to send
 * @param impl_state our `struct ClientState`
 */
static void
connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
                             const struct GNUNET_MessageHeader *msg,
                             void *impl_state)
{
  struct ClientState *cstate = impl_state;

  (void) mq;
  /* only one message at a time allowed */
  GNUNET_assert (NULL == cstate->msg);
  GNUNET_assert (NULL == cstate->send_task);
  cstate->msg = msg;
  cstate->msg_off = 0;
  if (NULL == cstate->sock)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "message of type %u waiting for socket\n",
         ntohs (msg->type));
    return;   /* still waiting for connection */
  }
  cstate->send_task
    = GNUNET_SCHEDULER_add_now (&transmit_ready,
                                cstate);
}


/**
 * Cancel the currently sent message.
 *
 * @param mq message queue
 * @param impl_state our `struct ClientState`
 */
static void
connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
                               void *impl_state)
{
  struct ClientState *cstate = impl_state;

  (void) mq;
  GNUNET_assert (NULL != cstate->msg);
  GNUNET_assert (0 == cstate->msg_off);
  cstate->msg = NULL;
  if (NULL != cstate->send_task)
  {
    GNUNET_SCHEDULER_cancel (cstate->send_task);
    cstate->send_task = NULL;
  }
}


/**
 * Test if the port or UNIXPATH of the given @a service_name
 * is in use and thus (most likely) the respective service is up.
 *
 * @param cfg our configuration
 * @param service_name name of the service to connect to
 * @return #GNUNET_YES if the service is (likely) up,
 *         #GNUNET_NO if the service is (definitively) down,
 *         #GNUNET_SYSERR if the configuration does not give us
 *          the necessary information about the service, or if
 *          we could not check (e.g. socket() failed)
 */
int
GNUNET_CLIENT_test (const struct GNUNET_CONFIGURATION_Handle *cfg,
                    const char *service_name)
{
  char *hostname = NULL;
  unsigned long long port;
  int ret;

#if AF_UNIX
  {
    char *unixpath = NULL;

    if (GNUNET_OK ==
        GNUNET_CONFIGURATION_get_value_filename (cfg,
                                                 service_name,
                                                 "UNIXPATH",
                                                 &unixpath))
    {
      if (0 == strlen (unixpath))
      {
        GNUNET_free (unixpath);
        return GNUNET_SYSERR; /* empty string not OK */
      }
      if (0 == access (unixpath,
                       F_OK))
      {
        GNUNET_free (unixpath);
        return GNUNET_OK; /* file exists, we assume service is running */
      }
      GNUNET_free (unixpath);
    }
    else if (GNUNET_OK ==
             GNUNET_CONFIGURATION_have_value (cfg,
                                              service_name,
                                              "UNIXPATH"))
    {
      /* UNIXPATH specified but not a valid path! */
      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
                                 service_name,
                                 "UNIXPATH",
                                 _ ("not a valid filename"));
      return GNUNET_SYSERR;
    }
  }
#endif

  if ( (GNUNET_OK !=
        GNUNET_CONFIGURATION_get_value_number (cfg,
                                               service_name,
                                               "PORT",
                                               &port)) ||
       (port > 65535) ||
       (0 == port) )
  {
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_string (cfg,
                                             service_name,
                                             "HOSTNAME",
                                             &hostname))
  {
    /* We always assume remotes are up */
    ret = GNUNET_YES;
  }
  else
  {
    /* We look for evidence the service is up */
    ret = GNUNET_NO;
  }
  if ( (NULL == hostname) ||
       (0 == strcasecmp (hostname,
                         "localhost")) ||
       (0 == strcasecmp (hostname,
                         "ip6-localnet")) )
  {
    /* service runs on loopback */
    struct sockaddr_in v4;
    struct sockaddr_in6 v6;
    int sock;

    memset (&v4, 0, sizeof (v4));
    memset (&v6, 0, sizeof (v6));
    v4.sin_family = AF_INET;
    v4.sin_port = htons ((uint16_t) port);
#if HAVE_SOCKADDR_IN_SUN_LEN
    v4.sin_len = (u_char) sizeof(struct sockaddr_in);
#endif
    inet_pton (AF_INET,
               "127.0.0.1",
               &v4.sin_addr);
    ret = GNUNET_NO;
    sock = socket (AF_INET,
                   SOCK_STREAM,
                   0);
    if (-1 != sock)
    {
      if (0 != bind (sock,
                     (struct sockaddr *) &v4,
                     sizeof (v4)))
      {
        /* bind failed, so someone is listening! */
        ret = GNUNET_YES;
      }
      (void) close (sock);
    }
    else
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                           "socket");
      if (GNUNET_NO == ret)
        ret = GNUNET_SYSERR;
    }
    v6.sin6_family = AF_INET6;
    v6.sin6_port = htons ((uint16_t) port);
#if HAVE_SOCKADDR_IN_SUN_LEN
    v6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
#endif
    inet_pton (AF_INET6,
               "::1",
               &v6.sin6_addr);
    sock = socket (AF_INET6,
                   SOCK_STREAM,
                   0);
    if (-1 != sock)
    {
      if (0 != bind (sock,
                     (struct sockaddr *) &v6,
                     sizeof (v6)))
      {
        /* bind failed, so someone is listening! */
        ret = GNUNET_YES;
      }
      (void) close (sock);
    }
    else
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                           "socket");
      /* not changing 'ret' intentionally here, as
         v4 succeeding and v6 failing just means we
         should use v4 */
    }
  }
  else
  {
    /* service running remotely */
    ret = GNUNET_OK;
  }
  GNUNET_free (hostname);
  return ret;
}


/**
 * Create a message queue to connect to a GNUnet service.
 * If handlers are specified, receive messages from the connection.
 *
 * @param cfg our configuration
 * @param service_name name of the service to connect to
 * @param handlers handlers for receiving messages, can be NULL
 * @param error_handler error handler
 * @param error_handler_cls closure for the @a error_handler
 * @return the message queue, NULL on error
 */
struct GNUNET_MQ_Handle *
GNUNET_CLIENT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
                       const char *service_name,
                       const struct GNUNET_MQ_MessageHandler *handlers,
                       GNUNET_MQ_ErrorHandler error_handler,
                       void *error_handler_cls)
{
  struct ClientState *cstate;

  if (GNUNET_OK !=
      test_service_configuration (service_name,
                                  cfg))
    return NULL;
  cstate = GNUNET_new (struct ClientState);
  cstate->service_name = GNUNET_strdup (service_name);
  cstate->cfg = cfg;
  cstate->retry_task = GNUNET_SCHEDULER_add_now (&start_connect,
                                                 cstate);
  cstate->mst = GNUNET_MST_create (&recv_message,
                                   cstate);
  if (GNUNET_YES ==
      GNUNET_CONFIGURATION_have_value (cfg,
                                       service_name,
                                       "PORT"))
  {
    if (! ((GNUNET_OK !=
            GNUNET_CONFIGURATION_get_value_number (cfg,
                                                   service_name,
                                                   "PORT",
                                                   &cstate->port)) ||
           (cstate->port > 65535) ||
           (GNUNET_OK !=
            GNUNET_CONFIGURATION_get_value_string (cfg,
                                                   service_name,
                                                   "HOSTNAME",
                                                   &cstate->hostname))) &&
        (0 == strlen (cstate->hostname)))
    {
      GNUNET_free (cstate->hostname);
      cstate->hostname = NULL;
      LOG (GNUNET_ERROR_TYPE_WARNING,
           _ ("Need a non-empty hostname for service `%s'.\n"),
           service_name);
    }
  }
  cstate->mq = GNUNET_MQ_queue_for_callbacks (&connection_client_send_impl,
                                              &connection_client_destroy_impl,
                                              &connection_client_cancel_impl,
                                              cstate,
                                              handlers,
                                              error_handler,
                                              error_handler_cls);
  return cstate->mq;
}


/* end of client.c */