aboutsummaryrefslogtreecommitdiff
path: root/src/arm/gnunet-service-arm_interceptor.c
blob: 24dd4ad25c4657f964885d0c39a319dccf127d22 (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
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
/*
     This file is part of GNUnet.
     (C) 2009, 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/
/**
 * @file arm/gnunet-service-arm_interceptor.c
 * @brief listen to incoming connections from clients to services,
 * start services for which incoming an incoming connection occur,
 * and relay communication between the client and the service for
 * that first incoming connection.
 *
 * @author Safey Abdel Halim
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunet_service_lib.h"
#include "gnunet_configuration_lib.h"
#include "gnunet_constants.h"
#include "gnunet_client_lib.h"
#include "gnunet_container_lib.h"
#include "gnunet-service-arm.h"


#define DEBUG_SERVICE_MANAGER GNUNET_EXTRA_LOGGING

#define BUFFER_SIZE (64 * 1024)

/**
 * Problem forwarding from client to service.
 */
#define REASON_CLIENT_TO_SERVICE 1

/**
 * Problem forwarding from service to client.
 */
#define REASON_SERVICE_TO_CLIENT 2

/**
 * Problem in both directions.
 */
#define REASON_ERROR 3

struct ForwardedConnection;

/**
 *
 */
struct ServiceListeningInfo
{
  /**
   * This is a linked list.
   */
  struct ServiceListeningInfo *next;

  /**
   * This is a linked list.
   */
  struct ServiceListeningInfo *prev;

  /**
   * Name of the service being forwarded.
   */
  char *serviceName;

  /**
   *
   */
  struct sockaddr *service_addr;

  /**
   *
   */
  socklen_t service_addr_len;

  /**
   * Our listening socket.
   */
  struct GNUNET_NETWORK_Handle *listeningSocket;

  /**
   *
   */
  struct ForwardedConnection *fc;

  /**
   * Task doing the accepting.
   */
  GNUNET_SCHEDULER_TaskIdentifier acceptTask;
};

/**
 * Information of the connection: client-arm-service
 */
struct ForwardedConnection
{
  /**
   *
   */
  struct GNUNET_NETWORK_Handle *armClientSocket;

  /**
   *
   */
  struct GNUNET_NETWORK_Handle *armServiceSocket;

  /**
   *
   */
  struct ServiceListeningInfo *listen_info;

  /**
   *
   */
  char service_to_client_buffer[BUFFER_SIZE];

  /**
   *
   */
  char client_to_service_buffer[BUFFER_SIZE];

  /**
   *
   */
  char client_addr[32];

  /**
   *
   */
  const char *client_to_service_bufferPos;

  /**
   *
   */
  const char *service_to_client_bufferPos;

  /**
   * Timeout for forwarding.
   */
  struct GNUNET_TIME_Absolute timeout;

  /**
   * Current back-off value.
   */
  struct GNUNET_TIME_Relative back_off;

  /**
   * Task that tries to initiate forwarding.
   */
  GNUNET_SCHEDULER_TaskIdentifier start_task;

  /**
   *
   */
  GNUNET_SCHEDULER_TaskIdentifier client_to_service_task;

  /**
   *
   */
  GNUNET_SCHEDULER_TaskIdentifier service_to_client_task;

  /**
   *
   */
  ssize_t client_to_service_bufferDataLength;

  /**
   *
   */
  ssize_t service_to_client_bufferDataLength;

  /**
   *
   */
  socklen_t client_addr_len;

  /**
   * Have we ever successfully written data to the service?
   */
  int first_write_done;

  /**
   * Reference count (the structure is freed when it reaches zero)
   */
  int reference_count;
};

/**
 * Array with the names of the services started by default.
 */
static char **defaultServicesList;

/**
 * Size of the defaultServicesList array.
 */
static unsigned int numDefaultServices;

/**
 *
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 *
 */
static struct ServiceListeningInfo *serviceListeningInfoList_head;

/**
 *
 */
static struct ServiceListeningInfo *serviceListeningInfoList_tail;


/**
 * Put the default services represented by a space separated string into an array of strings
 *
 * @param services space separated string of default services
 */
static void
addDefaultServicesToList (const char *services)
{
  unsigned int i;
  const char *token;
  char *s;

  if (strlen (services) == 0)
    return;
  s = GNUNET_strdup (services);
  token = strtok (s, " ");
  while (NULL != token)
  {
    numDefaultServices++;
    token = strtok (NULL, " ");
  }
  GNUNET_free (s);

  defaultServicesList = GNUNET_malloc (numDefaultServices * sizeof (char *));
  i = 0;
  s = GNUNET_strdup (services);
  token = strtok (s, " ");
  while (NULL != token)
  {
    defaultServicesList[i++] = GNUNET_strdup (token);
    token = strtok (NULL, " ");
  }
  GNUNET_free (s);
  GNUNET_assert (i == numDefaultServices);
}

/**
 * Checks whether the serviceName is in the list of default services
 *
 * @param serviceName string to check its existance in the list
 * @return GNUNET_YES if the service is started by default
 */
static int
isInDefaultList (const char *serviceName)
{
  unsigned int i;

  for (i = 0; i < numDefaultServices; i++)
    if (strcmp (serviceName, defaultServicesList[i]) == 0)
      return GNUNET_YES;
  return GNUNET_NO;
}


/**
 * Close forwarded connection (partial or full).
 *
 * @param fc connection to close
 * @param reason which direction to close
 */
static void
closeClientAndServiceSockets (struct ForwardedConnection *fc, int reason)
{
  if (0 != (REASON_SERVICE_TO_CLIENT & reason))
  {
#if DEBUG_SERVICE_MANAGER
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Stopping forwarding from service to client\n");
#endif
    if (fc->armClientSocket != NULL)
      GNUNET_NETWORK_socket_shutdown (fc->armClientSocket, SHUT_WR);
    if (fc->armServiceSocket != NULL)
      GNUNET_NETWORK_socket_shutdown (fc->armServiceSocket, SHUT_RD);
  }
  if (0 != (REASON_CLIENT_TO_SERVICE & reason))
  {
#if DEBUG_SERVICE_MANAGER
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Stopping forwarding from client to service\n");
#endif
    if (fc->armClientSocket != NULL)
      GNUNET_NETWORK_socket_shutdown (fc->armClientSocket, SHUT_RD);
    if (fc->armServiceSocket != NULL)
      GNUNET_NETWORK_socket_shutdown (fc->armServiceSocket, SHUT_WR);
  }
#if DEBUG_SERVICE_MANAGER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Closing forwarding connection (done with both directions)\n");
#endif
  fc->reference_count -= 1;
  if (fc->reference_count <= 0)
  {
    if ((NULL != fc->armClientSocket) &&
        (GNUNET_SYSERR == GNUNET_NETWORK_socket_close (fc->armClientSocket)))
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "close");
      fc->armClientSocket = NULL;
    }
    if ((NULL != fc->armServiceSocket) &&
        (GNUNET_SYSERR == GNUNET_NETWORK_socket_close (fc->armServiceSocket)))
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "close");
      fc->armServiceSocket = NULL;
    }
    if (fc->listen_info != NULL)
    {
      if (fc->listen_info->serviceName != NULL)
      {
        GNUNET_free (fc->listen_info->serviceName);
        fc->listen_info->serviceName = NULL;
      }
      if (fc->listen_info->service_addr != NULL)
      {
        GNUNET_free (fc->listen_info->service_addr);
        fc->listen_info->service_addr = NULL;
      }
      GNUNET_free (fc->listen_info);
      fc->listen_info = NULL;
    }
    GNUNET_free (fc);
  }
}


/**
 * Read data from the client and then forward it to the service.
 *
 * @param cls callback data,   struct ForwardedConnection for the communication between client and service
 * @param tc context
 */
static void
receiveFromClient (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);


/**
 * Receive service messages sent by the service and forward it to client
 *
 * @param cls callback data, struct ForwardedConnection for the communication between client and service
 * @param tc scheduler context
 */
static void
receiveFromService (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);


/**
 *
 */
static void
start_forwarding (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);



/**
 * Forward messages sent from service to client
 *
 * @param cls callback data, struct ForwardedConnection for the communication between client and service
 * @param tc context
 */
static void
forwardToClient (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ForwardedConnection *fc = cls;
  ssize_t numberOfBytesSent;

  fc->service_to_client_task = GNUNET_SCHEDULER_NO_TASK;
  if (GNUNET_YES !=
      GNUNET_NETWORK_fdset_isset (tc->write_ready, fc->armClientSocket))
  {
    fc->service_to_client_task =
        GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                        fc->armClientSocket, &forwardToClient,
                                        fc);
    return;
  }
  /* Forwarding service response to client */
  numberOfBytesSent =
      GNUNET_NETWORK_socket_send (fc->armClientSocket,
                                  fc->service_to_client_bufferPos,
                                  fc->service_to_client_bufferDataLength);
  if (numberOfBytesSent <= 0)
  {
    if ((errno != EPIPE) && (errno != ECONNRESET))
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Failed to forward %u bytes of data to client: %s\n",
                  fc->service_to_client_bufferDataLength, STRERROR (errno));
    closeClientAndServiceSockets (fc, REASON_SERVICE_TO_CLIENT);
    return;
  }
#if DEBUG_SERVICE_MANAGER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Forwarded %d bytes to client\n",
              numberOfBytesSent);
#endif
  if (numberOfBytesSent < fc->service_to_client_bufferDataLength)
  {
    fc->service_to_client_bufferPos += numberOfBytesSent;
    fc->service_to_client_bufferDataLength -= numberOfBytesSent;
    fc->service_to_client_task =
        GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                        fc->armClientSocket, &forwardToClient,
                                        fc);
    return;
  }
  fc->service_to_client_task =
      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                     fc->armServiceSocket, &receiveFromService,
                                     fc);
}


/**
 * Receive service messages sent by the service and forward it to client
 *
 * @param cls callback data, struct ForwardedConnection for the communication between client and service
 * @param tc scheduler context
 */
static void
receiveFromService (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ForwardedConnection *fc = cls;
  struct GNUNET_TIME_Relative rem;

  fc->service_to_client_task = GNUNET_SCHEDULER_NO_TASK;
  if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
      (fc->first_write_done != GNUNET_YES))
  {
    closeClientAndServiceSockets (fc, REASON_ERROR);
    return;
  }
  if (GNUNET_YES !=
      GNUNET_NETWORK_fdset_isset (tc->read_ready, fc->armServiceSocket))
  {
    fc->service_to_client_task =
        GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                       fc->armServiceSocket,
                                       &receiveFromService, fc);
    return;
  }
  fc->service_to_client_bufferPos = fc->service_to_client_buffer;
  fc->service_to_client_bufferDataLength =
      GNUNET_NETWORK_socket_recv (fc->armServiceSocket,
                                  fc->service_to_client_buffer, BUFFER_SIZE);
  if (fc->service_to_client_bufferDataLength <= 0)
  {
#if DEBUG_SERVICE_MANAGER
    if (fc->service_to_client_bufferDataLength == 0)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Service `%s' stopped sending data.\n",
                  fc->listen_info->serviceName);
    }
#endif
    if (fc->first_write_done != GNUNET_YES)
    {
      fc->service_to_client_bufferDataLength = 0;
      GNUNET_break (GNUNET_OK ==
                    GNUNET_NETWORK_socket_close (fc->armServiceSocket));
      fc->armServiceSocket = NULL;
      if ((fc->client_to_service_bufferDataLength > 0) &&
          (fc->client_to_service_task != GNUNET_SCHEDULER_NO_TASK))
      {
        GNUNET_SCHEDULER_cancel (fc->client_to_service_task);
        fc->client_to_service_task = GNUNET_SCHEDULER_NO_TASK;
      }
      fc->back_off = GNUNET_TIME_relative_multiply (fc->back_off, 2);
#if DEBUG_SERVICE_MANAGER
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Failed to connected to service `%s' at `%s', will try again in %llu ms\n",
                  fc->listen_info->serviceName,
                  GNUNET_a2s (fc->listen_info->service_addr,
                              fc->listen_info->service_addr_len),
                  (unsigned long long) GNUNET_TIME_relative_min (fc->back_off,
                                                                 rem).rel_value);
#endif
      rem = GNUNET_TIME_absolute_get_remaining (fc->timeout);
      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == fc->start_task);
      fc->start_task =
          GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_min
                                        (fc->back_off, rem), &start_forwarding,
                                        fc);
    }
    else
    {
#if DEBUG_SERVICE_MANAGER
      if (fc->service_to_client_bufferDataLength != 0)
        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                    "Error receiving from service: %s\n", STRERROR (errno));
#endif
      closeClientAndServiceSockets (fc, REASON_SERVICE_TO_CLIENT);
    }
    return;
  }
  fc->first_write_done = GNUNET_YES;
#if DEBUG_SERVICE_MANAGER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %d bytes for client\n",
              fc->service_to_client_bufferDataLength);
#endif
  fc->service_to_client_task =
      GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                      fc->armClientSocket, &forwardToClient,
                                      fc);
}


/**
 * Forward client message to service
 *
 * @param cls callback data, struct ForwardedConnection for the communication between client and service
 * @param tc scheduler context
 */
static void
forwardToService (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ForwardedConnection *fc = cls;
  ssize_t numberOfBytesSent;
  struct GNUNET_TIME_Relative rem;

  fc->client_to_service_task = GNUNET_SCHEDULER_NO_TASK;
  if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
      (fc->first_write_done != GNUNET_YES))
  {
    closeClientAndServiceSockets (fc, REASON_ERROR);
    return;
  }
  if (GNUNET_YES !=
      GNUNET_NETWORK_fdset_isset (tc->write_ready, fc->armServiceSocket))
  {
    fc->client_to_service_task =
        GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                        fc->armServiceSocket, &forwardToService,
                                        fc);
    return;
  }
  numberOfBytesSent =
      GNUNET_NETWORK_socket_send (fc->armServiceSocket,
                                  fc->client_to_service_bufferPos,
                                  fc->client_to_service_bufferDataLength);
  if (numberOfBytesSent <= 0)
  {
    if (GNUNET_YES != fc->first_write_done)
    {
      GNUNET_break (GNUNET_OK ==
                    GNUNET_NETWORK_socket_close (fc->armServiceSocket));
      fc->armServiceSocket = NULL;
      if ((fc->service_to_client_bufferDataLength == 0) &&
          (fc->service_to_client_task != GNUNET_SCHEDULER_NO_TASK))
      {
        GNUNET_SCHEDULER_cancel (fc->service_to_client_task);
        fc->service_to_client_task = GNUNET_SCHEDULER_NO_TASK;
      }
      fc->back_off = GNUNET_TIME_relative_multiply (fc->back_off, 2);
#if DEBUG_SERVICE_MANAGER
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Failed to connect to service `%s' at `%s', will try again in %llu ms\n",
                  fc->listen_info->serviceName,
                  GNUNET_a2s (fc->listen_info->service_addr,
                              fc->listen_info->service_addr_len),
                  (unsigned long long) GNUNET_TIME_relative_min (fc->back_off,
                                                                 rem).rel_value);
#endif
      rem = GNUNET_TIME_absolute_get_remaining (fc->timeout);
      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == fc->start_task);
      fc->start_task =
          GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_min
                                        (fc->back_off, rem), &start_forwarding,
                                        fc);
    }
    else
    {
      if ((errno != EPIPE) && (errno != ECONNRESET))
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    "Failed to forward data to service: %s\n",
                    STRERROR (errno));
      closeClientAndServiceSockets (fc, REASON_CLIENT_TO_SERVICE);
    }
    return;
  }
#if DEBUG_SERVICE_MANAGER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Forwarded %d bytes to service\n",
              numberOfBytesSent);
#endif
  fc->first_write_done = GNUNET_YES;
  if (numberOfBytesSent < fc->client_to_service_bufferDataLength)
  {
    fc->client_to_service_bufferPos += numberOfBytesSent;
    fc->client_to_service_bufferDataLength -= numberOfBytesSent;
    fc->client_to_service_task =
        GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                        fc->armServiceSocket, &forwardToService,
                                        fc);
    return;
  }
  fc->client_to_service_task =
      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                     fc->armClientSocket, &receiveFromClient,
                                     fc);
}


/**
 * Read data from the client and then forward it to the service.
 *
 * @param cls callback data,   struct ForwardedConnection for the communication between client and service
 * @param tc context
 */
static void
receiveFromClient (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ForwardedConnection *fc = cls;

  fc->client_to_service_task = GNUNET_SCHEDULER_NO_TASK;
  if (GNUNET_YES !=
      GNUNET_NETWORK_fdset_isset (tc->read_ready, fc->armClientSocket))
  {
    fc->client_to_service_task =
        GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                       fc->armClientSocket, &receiveFromClient,
                                       fc);
    return;
  }
  fc->client_to_service_bufferPos = fc->client_to_service_buffer;
  fc->client_to_service_bufferDataLength =
      GNUNET_NETWORK_socket_recv (fc->armClientSocket,
                                  fc->client_to_service_buffer, BUFFER_SIZE);
  if (fc->client_to_service_bufferDataLength <= 0)
  {
    if (fc->client_to_service_bufferDataLength == 0)
    {
#if DEBUG_SERVICE_MANAGER
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Client closed connection with service `%s'\n",
                  fc->listen_info->serviceName);
#endif
    }
    else
    {
#if DEBUG_SERVICE_MANAGER
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error receiving from client: %s\n",
                  STRERROR (errno));
#endif
    }
    closeClientAndServiceSockets (fc, REASON_CLIENT_TO_SERVICE);
    return;
  }
#if DEBUG_SERVICE_MANAGER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %d bytes for service\n",
              fc->client_to_service_bufferDataLength);
#endif
  if (fc->armServiceSocket != NULL)
    fc->client_to_service_task =
        GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                        fc->armServiceSocket, &forwardToService,
                                        fc);
  else
    /* We have not added any task with fc as a closure, so we're
     * dropping our reference to fc
     */
    fc->reference_count -= 1;
}


static void
fc_acceptConnection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ServiceListeningInfo *sli = cls;
  struct ForwardedConnection *fc = sli->fc;

  if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
  {
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_NETWORK_socket_close (sli->listeningSocket));
    closeClientAndServiceSockets (fc, REASON_ERROR);
    GNUNET_free (sli);
    return;
  }
#if DEBUG_SERVICE_MANAGER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Connected to service, now starting forwarding\n");
#endif
  fc->armServiceSocket = sli->listeningSocket;
  GNUNET_free (fc->listen_info->service_addr);
  fc->listen_info->service_addr = sli->service_addr;
  fc->listen_info->service_addr_len = sli->service_addr_len;
  /* Drop fc reference count prematurely, it'll be incremented
   * once or twice in the following conditional branches.
   * This is, apparently, the place where reference count increases
   * past 1.
   */
  fc->reference_count -= 1;
  if (fc->client_to_service_task == GNUNET_SCHEDULER_NO_TASK)
  {
    if (fc->client_to_service_bufferDataLength == 0)
      fc->client_to_service_task =
          GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                         fc->armClientSocket,
                                         &receiveFromClient, fc);
    else
      fc->client_to_service_task =
          GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                          fc->armServiceSocket,
                                          &forwardToService, fc);
    fc->reference_count += 1;
  }
  if (fc->service_to_client_task == GNUNET_SCHEDULER_NO_TASK)
  {
    if (fc->service_to_client_bufferDataLength == 0)
      fc->service_to_client_task =
          GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                         fc->armServiceSocket,
                                         &receiveFromService, fc);
    else
      fc->service_to_client_task =
          GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                          fc->armClientSocket, &forwardToClient,
                                          fc);
    fc->reference_count += 1;
  }
  GNUNET_free (sli);
}


static struct ServiceListeningInfo *
service_try_to_connect (const struct sockaddr *addr, int pf, socklen_t addrlen,
                        struct ForwardedConnection *fc)
{
  struct GNUNET_NETWORK_Handle *sock;
  struct ServiceListeningInfo *serviceListeningInfo;

  sock = GNUNET_NETWORK_socket_create (pf, SOCK_STREAM, 0);
  if (sock == NULL)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
    return NULL;
  }
  if ((GNUNET_SYSERR == GNUNET_NETWORK_socket_connect (sock, addr, addrlen)) &&
      (errno != EINPROGRESS))
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
    return NULL;
  }
  serviceListeningInfo = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
  serviceListeningInfo->serviceName = NULL;
  serviceListeningInfo->service_addr = GNUNET_malloc (addrlen);
  memcpy (serviceListeningInfo->service_addr, addr, addrlen);
  serviceListeningInfo->service_addr_len = addrlen;
  serviceListeningInfo->listeningSocket = sock;
  serviceListeningInfo->fc = fc;
  serviceListeningInfo->acceptTask =
      GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                      serviceListeningInfo->listeningSocket,
                                      &fc_acceptConnection,
                                      serviceListeningInfo);
  return serviceListeningInfo;
}


/**
 *
 */
static void
start_forwarding (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ForwardedConnection *fc = cls;
  struct ServiceListeningInfo *sc;
  struct sockaddr_in target_ipv4;
  struct sockaddr_in6 target_ipv6;
  const struct sockaddr_in *v4;
  const struct sockaddr_in6 *v6;
  char listen_address[INET6_ADDRSTRLEN];

  fc->start_task = GNUNET_SCHEDULER_NO_TASK;
  if ((NULL != tc) && (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _("Unable to forward to service `%s': shutdown\n"),
                fc->listen_info->serviceName);
    closeClientAndServiceSockets (fc, REASON_ERROR);
    return;
  }
  if (0 == GNUNET_TIME_absolute_get_remaining (fc->timeout).rel_value)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _
                ("Unable to forward to service `%s': timeout before connect\n"),
                fc->listen_info->serviceName);
    closeClientAndServiceSockets (fc, REASON_ERROR);
    return;
  }
  switch (fc->listen_info->service_addr->sa_family)
  {
  case AF_UNSPEC:
    GNUNET_break (0);
    closeClientAndServiceSockets (fc, REASON_ERROR);
    return;
  case AF_INET:
    v4 = (const struct sockaddr_in *) fc->listen_info->service_addr;
    inet_ntop (fc->listen_info->service_addr->sa_family,
               (const void *) &v4->sin_addr, listen_address, INET_ADDRSTRLEN);
    if (0 == strncmp (listen_address, "0.0.0.0", 7))
    {
      /* connect to [::1] and 127.0.0.1 instead of [::] and 0.0.0.0 */
      memset (&target_ipv4, 0, sizeof (target_ipv4));
      GNUNET_assert (1 ==
                     inet_pton (AF_INET, "127.0.0.1", &target_ipv4.sin_addr));
      target_ipv4.sin_family = AF_INET;
      target_ipv4.sin_port = v4->sin_port;
      v4 = &target_ipv4;
    }
    sc = service_try_to_connect ((const struct sockaddr *) v4, PF_INET,
                                 sizeof (struct sockaddr_in), fc);
    break;
  case AF_INET6:
    v6 = (struct sockaddr_in6 *) fc->listen_info->service_addr;
    inet_ntop (fc->listen_info->service_addr->sa_family,
               (const void *) &v6->sin6_addr, listen_address, INET6_ADDRSTRLEN);
    if ((strncmp (listen_address, "[::]:", 5) == 0) ||
        (strncmp (listen_address, "::", 2) == 0))
    {
      memset (&target_ipv6, 0, sizeof (target_ipv6));
      target_ipv6.sin6_addr = in6addr_loopback;
      target_ipv6.sin6_family = AF_INET6;
      target_ipv6.sin6_port = v6->sin6_port;
      v6 = &target_ipv6;
    }
    sc = service_try_to_connect ((const struct sockaddr *) v6, PF_INET6,
                                 sizeof (struct sockaddr_in6), fc);
    break;
  case AF_UNIX:
    sc = service_try_to_connect (fc->listen_info->service_addr, PF_UNIX,
                                 fc->listen_info->service_addr_len, fc);
    break;
  default:
    GNUNET_break (0);
    closeClientAndServiceSockets (fc, REASON_ERROR);
    return;
  }
  if (NULL == sc)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Unable to start service `%s': %s\n"),
                fc->listen_info->serviceName, STRERROR (errno));
    closeClientAndServiceSockets (fc, REASON_ERROR);
    return;
  }
}


/**
 *
 */
int
stop_listening (const char *serviceName)
{
  struct ServiceListeningInfo *pos;
  struct ServiceListeningInfo *next;
  int ret;

  ret = GNUNET_NO;
  next = serviceListeningInfoList_head;
  while (NULL != (pos = next))
  {
    next = pos->next;
    if ((serviceName != NULL) && (strcmp (pos->serviceName, serviceName) != 0))
      continue;
    if (pos->acceptTask != GNUNET_SCHEDULER_NO_TASK)
      GNUNET_SCHEDULER_cancel (pos->acceptTask);
    GNUNET_break (GNUNET_OK ==
                  GNUNET_NETWORK_socket_close (pos->listeningSocket));
    GNUNET_CONTAINER_DLL_remove (serviceListeningInfoList_head,
                                 serviceListeningInfoList_tail, pos);
    GNUNET_free (pos->serviceName);
    GNUNET_free (pos->service_addr);
    GNUNET_free (pos);
    ret = GNUNET_OK;
  }
  return ret;
}

/**
 * First connection has come to the listening socket associated with the service,
 * create the service in order to relay the incoming connection to it
 *
 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
 * @param tc context
 */
static void
acceptConnection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);


static void
accept_and_forward (struct ServiceListeningInfo *serviceListeningInfo)
{
  struct ForwardedConnection *fc;

  fc = GNUNET_malloc (sizeof (struct ForwardedConnection));
  fc->reference_count = 1;
  fc->listen_info = serviceListeningInfo;
  fc->service_to_client_bufferPos = fc->service_to_client_buffer;
  fc->client_to_service_bufferPos = fc->client_to_service_buffer;
  fc->client_addr_len = sizeof (fc->client_addr);
  fc->armClientSocket =
      GNUNET_NETWORK_socket_accept (serviceListeningInfo->listeningSocket,
                                    (struct sockaddr *) fc->client_addr,
                                    &fc->client_addr_len);
  if (NULL == fc->armClientSocket)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Unable to accept connection for service `%s': %s\n"),
                serviceListeningInfo->serviceName, STRERROR (errno));
    GNUNET_free (fc);
    GNUNET_CONTAINER_DLL_insert (serviceListeningInfoList_head,
                                 serviceListeningInfoList_tail,
                                 serviceListeningInfo);
    serviceListeningInfo->acceptTask =
        GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                       serviceListeningInfo->listeningSocket,
                                       &acceptConnection, serviceListeningInfo);
    return;
  }
  GNUNET_break (GNUNET_OK ==
                GNUNET_NETWORK_socket_close
                (serviceListeningInfo->listeningSocket));
  start_service (NULL, serviceListeningInfo->serviceName, NULL);
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Service `%s' started\n"),
              fc->listen_info->serviceName);
  fc->timeout =
      GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_SERVICE_TIMEOUT);
  fc->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
  fc->client_to_service_task =
      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                     fc->armClientSocket, &receiveFromClient,
                                     fc);
  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == fc->start_task);
  /* We're creating another chain of tasks for this fc that
   * will have its own reference to it.
   */
  fc->reference_count += 1;
  fc->start_task = GNUNET_SCHEDULER_add_now (&start_forwarding, fc);
}


/**
 * First connection has come to the listening socket associated with the service,
 * create the service in order to relay the incoming connection to it
 *
 * @param cls callback data, struct ServiceListeningInfo describing a listen socket
 * @param tc context
 */
static void
acceptConnection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct ServiceListeningInfo *sli = cls;
  struct ServiceListeningInfo *pos;
  struct ServiceListeningInfo *next;
  int *lsocks;
  unsigned int ls;
  int use_lsocks;

  sli->acceptTask = GNUNET_SCHEDULER_NO_TASK;
  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
    return;
  GNUNET_CONTAINER_DLL_remove (serviceListeningInfoList_head,
                               serviceListeningInfoList_tail, sli);
#ifndef MINGW
  use_lsocks = GNUNET_NO;
  if (GNUNET_YES ==
      GNUNET_CONFIGURATION_have_value (cfg, sli->serviceName,
                                       "DISABLE_SOCKET_FORWARDING"))
    use_lsocks =
        GNUNET_CONFIGURATION_get_value_yesno (cfg, sli->serviceName,
                                              "DISABLE_SOCKET_FORWARDING");
#else
  use_lsocks = GNUNET_YES;
#endif
  if (GNUNET_NO != use_lsocks)
  {
    accept_and_forward (sli);
    return;
  }
  lsocks = NULL;
  ls = 0;
  next = serviceListeningInfoList_head;
  while (NULL != (pos = next))
  {
    next = pos->next;
    if (0 == strcmp (pos->serviceName, sli->serviceName))
    {
      GNUNET_array_append (lsocks, ls,
                           GNUNET_NETWORK_get_fd (pos->listeningSocket));
      GNUNET_free (pos->listeningSocket);       /* deliberately no closing! */
      GNUNET_free (pos->service_addr);
      GNUNET_free (pos->serviceName);
      GNUNET_SCHEDULER_cancel (pos->acceptTask);
      GNUNET_CONTAINER_DLL_remove (serviceListeningInfoList_head,
                                   serviceListeningInfoList_tail, pos);
      GNUNET_free (pos);
    }
  }
  GNUNET_array_append (lsocks, ls,
                       GNUNET_NETWORK_get_fd (sli->listeningSocket));
  GNUNET_free (sli->listeningSocket);   /* deliberately no closing! */
  GNUNET_free (sli->service_addr);
  GNUNET_array_append (lsocks, ls, -1);
  start_service (NULL, sli->serviceName, lsocks);
  ls = 0;
  while (lsocks[ls] != -1)
    GNUNET_break (0 == close (lsocks[ls++]));
  GNUNET_array_grow (lsocks, ls, 0);
  GNUNET_free (sli->serviceName);
  GNUNET_free (sli);
}


/**
 * Creating a listening socket for each of the service's addresses and
 * wait for the first incoming connection to it
 *
 * @param sa address associated with the service
 * @param addr_len length of sa
 * @param serviceName the name of the service in question
 */
static void
createListeningSocket (struct sockaddr *sa, socklen_t addr_len,
                       const char *serviceName)
{
  const static int on = 1;
  struct GNUNET_NETWORK_Handle *sock;
  struct ServiceListeningInfo *serviceListeningInfo;

  switch (sa->sa_family)
  {
  case AF_INET:
    sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
    break;
  case AF_INET6:
    sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
    break;
  case AF_UNIX:
    if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0)   /* Do not bind to blank UNIX path! */
      return;
    sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
    break;
  default:
    GNUNET_break (0);
    sock = NULL;
    errno = EAFNOSUPPORT;
    break;
  }
  if (NULL == sock)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Unable to create socket for service `%s': %s\n"),
                serviceName, STRERROR (errno));
    GNUNET_free (sa);
    return;
  }
  if (GNUNET_NETWORK_socket_setsockopt
      (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                         "setsockopt");
#ifdef IPV6_V6ONLY
  if ((sa->sa_family == AF_INET6) &&
      (GNUNET_NETWORK_socket_setsockopt
       (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                         "setsockopt");
#endif

  if (GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) sa, addr_len)
      != GNUNET_OK)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _
                ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
                serviceName, GNUNET_a2s (sa, addr_len), STRERROR (errno));
    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
    GNUNET_free (sa);
    return;
  }
  if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
    GNUNET_free (sa);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              _("ARM now monitors connections to service `%s' at `%s'\n"),
              serviceName, GNUNET_a2s (sa, addr_len));
  serviceListeningInfo = GNUNET_malloc (sizeof (struct ServiceListeningInfo));
  serviceListeningInfo->serviceName = GNUNET_strdup (serviceName);
  serviceListeningInfo->service_addr = sa;
  serviceListeningInfo->service_addr_len = addr_len;
  serviceListeningInfo->listeningSocket = sock;
  serviceListeningInfo->acceptTask =
      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
                                     &acceptConnection, serviceListeningInfo);
  GNUNET_CONTAINER_DLL_insert (serviceListeningInfoList_head,
                               serviceListeningInfoList_tail,
                               serviceListeningInfo);
}


/**
 * Callback function, checks whether the current tokens are representing a service,
 * gets its addresses and create listening socket for it.
 *
 * @param cls callback data, not used
 * @param section configuration section
 * @param option configuration option
 * @param value the option's value
 */
static void
checkPortNumberCB (void *cls, const char *section, const char *option,
                   const char *value)
{
  struct sockaddr **addrs;
  socklen_t *addr_lens;
  int ret;
  unsigned int i;

  if ((strcasecmp (section, "arm") == 0) ||
      (strcasecmp (option, "AUTOSTART") != 0) ||
      (strcasecmp (value, "YES") != 0) ||
      (isInDefaultList (section) == GNUNET_YES))
    return;
  if (0 >=
      (ret =
       GNUNET_SERVICE_get_server_addresses (section, cfg, &addrs, &addr_lens)))
    return;
  /* this will free (or capture) addrs[i] */
  for (i = 0; i < ret; i++)
    createListeningSocket (addrs[i], addr_lens[i], section);
  GNUNET_free (addrs);
  GNUNET_free (addr_lens);
}


/**
 * Entry point to the Service Manager
 *
 * @param configurationHandle configuration to use to get services
 */
void
prepareServices (const struct GNUNET_CONFIGURATION_Handle *configurationHandle)
{
  char *defaultServicesString;

  cfg = configurationHandle;
  /* Split the default services into a list */
  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_string (cfg, "arm", "DEFAULTSERVICES",
                                             &defaultServicesString))
  {
    addDefaultServicesToList (defaultServicesString);
    GNUNET_free (defaultServicesString);
  }
  /* Spot the services from the configuration and create a listening
   * socket for each */
  GNUNET_CONFIGURATION_iterate (cfg, &checkPortNumberCB, NULL);
}

/* end of gnunet-service-arm_interceptor.c */