aboutsummaryrefslogtreecommitdiff
path: root/src/util/service.c
blob: cf970323582c51df996e3cf816d36152117c9509 (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
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
/*
     This file is part of GNUnet.
     (C) 2009 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 2, 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 util/service.c
 * @brief functions related to starting services
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_configuration_lib.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_directories.h"
#include "gnunet_disk_lib.h"
#include "gnunet_getopt_lib.h"
#include "gnunet_os_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_server_lib.h"
#include "gnunet_service_lib.h"

/* ******************* access control ******************** */

/**
 * @brief IPV4 network in CIDR notation.
 */
struct IPv4NetworkSet
{
  struct in_addr network;
  struct in_addr netmask;
};

/**
 * @brief network in CIDR notation for IPV6.
 */
struct IPv6NetworkSet
{
  struct in6_addr network;
  struct in6_addr netmask;
};


/**
 * Parse a network specification. The argument specifies
 * a list of networks. The format is
 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
 * with a semicolon). The network must be given in dotted-decimal
 * notation. The netmask can be given in CIDR notation (/16) or
 * in dotted-decimal (/255.255.0.0).
 * <p>
 * @param routeList a string specifying the forbidden networks
 * @return the converted list, NULL if the synatx is flawed
 */
static struct IPv4NetworkSet *
parse_ipv4_specification (const char *routeList)
{
  unsigned int count;
  unsigned int i;
  unsigned int j;
  unsigned int len;
  int cnt;
  unsigned int pos;
  unsigned int temps[8];
  int slash;
  struct IPv4NetworkSet *result;

  if (routeList == NULL)
    return NULL;
  len = strlen (routeList);
  if (len == 0)
    return NULL;
  count = 0;
  for (i = 0; i < len; i++)
    if (routeList[i] == ';')
      count++;
  result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
  /* add termination */
  memset (result, 0, sizeof (struct IPv4NetworkSet) * (count + 1));
  i = 0;
  pos = 0;
  while (i < count)
    {
      cnt = sscanf (&routeList[pos],
                    "%u.%u.%u.%u/%u.%u.%u.%u;",
                    &temps[0],
                    &temps[1],
                    &temps[2],
                    &temps[3], &temps[4], &temps[5], &temps[6], &temps[7]);
      if (cnt == 8)
        {
          for (j = 0; j < 8; j++)
            if (temps[j] > 0xFF)
              {
                GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                            _("Invalid format for IP: `%s'\n"),
                            &routeList[pos]);
                GNUNET_free (result);
                return NULL;
              }
          result[i].network.s_addr
            =
            htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
                   temps[3]);
          result[i].netmask.s_addr =
            htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
                   temps[7]);
          while (routeList[pos] != ';')
            pos++;
          pos++;
          i++;
          continue;
        }
      /* try second notation */
      cnt = sscanf (&routeList[pos],
                    "%u.%u.%u.%u/%u;",
                    &temps[0], &temps[1], &temps[2], &temps[3], &slash);
      if (cnt == 5)
        {
          for (j = 0; j < 4; j++)
            if (temps[j] > 0xFF)
              {
                GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                            _("Invalid format for IP: `%s'\n"),
                            &routeList[pos]);
                GNUNET_free (result);
                return NULL;
              }
          result[i].network.s_addr
            =
            htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
                   temps[3]);
          if ((slash <= 32) && (slash >= 0))
            {
              result[i].netmask.s_addr = 0;
              while (slash > 0)
                {
                  result[i].netmask.s_addr
                    = (result[i].netmask.s_addr >> 1) + 0x80000000;
                  slash--;
                }
              result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
              while (routeList[pos] != ';')
                pos++;
              pos++;
              i++;
              continue;
            }
          else
            {
              GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                          _
                          ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
                          slash);
              GNUNET_free (result);
              return NULL;      /* error */
            }
        }
      /* try third notation */
      slash = 32;
      cnt = sscanf (&routeList[pos],
                    "%u.%u.%u.%u;",
                    &temps[0], &temps[1], &temps[2], &temps[3]);
      if (cnt == 4)
        {
          for (j = 0; j < 4; j++)
            if (temps[j] > 0xFF)
              {
                GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                            _("Invalid format for IP: `%s'\n"),
                            &routeList[pos]);
                GNUNET_free (result);
                return NULL;
              }
          result[i].network.s_addr
            =
            htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
                   temps[3]);
          result[i].netmask.s_addr = 0;
          while (slash > 0)
            {
              result[i].netmask.s_addr
                = (result[i].netmask.s_addr >> 1) + 0x80000000;
              slash--;
            }
          result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
          while (routeList[pos] != ';')
            pos++;
          pos++;
          i++;
          continue;
        }
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _("Invalid format for IP: `%s'\n"), &routeList[pos]);
      GNUNET_free (result);
      return NULL;              /* error */
    }
  if (pos < strlen (routeList))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _("Invalid format for IP: `%s'\n"), &routeList[pos]);
      GNUNET_free (result);
      return NULL;              /* oops */
    }
  return result;                /* ok */
}


/**
 * Parse a network specification. The argument specifies
 * a list of networks. The format is
 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
 * with a semicolon). The network must be given in colon-hex
 * notation.  The netmask must be given in CIDR notation (/16) or
 * can be omitted to specify a single host.
 * <p>
 * @param routeList a string specifying the forbidden networks
 * @return the converted list, NULL if the synatx is flawed
 */
static struct IPv6NetworkSet *
parse_ipv6_specification (const char *routeListX)
{
  unsigned int count;
  unsigned int i;
  unsigned int len;
  unsigned int pos;
  int start;
  int slash;
  int ret;
  char *routeList;
  struct IPv6NetworkSet *result;
  unsigned int bits;
  unsigned int off;
  int save;

  if (routeListX == NULL)
    return NULL;
  len = strlen (routeListX);
  if (len == 0)
    return NULL;
  routeList = GNUNET_strdup (routeListX);
  count = 0;
  for (i = 0; i < len; i++)
    if (routeList[i] == ';')
      count++;
  if (routeList[len - 1] != ';')
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _
                  ("Invalid network notation (does not end with ';': `%s')\n"),
                  routeList);
      GNUNET_free (routeList);
      return NULL;
    }

  result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
  memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
  i = 0;
  pos = 0;
  while (i < count)
    {
      start = pos;
      while (routeList[pos] != ';')
        pos++;
      slash = pos;
      while ((slash >= start) && (routeList[slash] != '/'))
        slash--;
      if (slash < start)
        {
          memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
          slash = pos;
        }
      else
        {
          routeList[pos] = '\0';
          ret = inet_pton (AF_INET6,
                           &routeList[slash + 1], &result[i].netmask);
          if (ret <= 0)
            {
              save = errno;
              if ((1 != SSCANF (&routeList[slash + 1],
                                "%u", &bits)) || (bits >= 128))
                {
                  if (ret == 0)
                    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                                _("Wrong format `%s' for netmask\n"),
                                &routeList[slash + 1]);
                  else
                    {
                      errno = save;
                      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                                           "inet_pton");
                    }
                  GNUNET_free (result);
                  GNUNET_free (routeList);
                  return NULL;
                }
              off = 0;
              while (bits > 8)
                {
                  result[i].netmask.s6_addr[off++] = 0xFF;
                  bits -= 8;
                }
              while (bits > 0)
                {
                  result[i].netmask.s6_addr[off]
                    = (result[i].netmask.s6_addr[off] >> 1) + 0x80;
                  bits--;
                }
            }
        }
      routeList[slash] = '\0';
      ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
      if (ret <= 0)
        {
          if (ret == 0)
            GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                        _("Wrong format `%s' for network\n"),
                        &routeList[slash + 1]);
          else
            GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
          GNUNET_free (result);
          GNUNET_free (routeList);
          return NULL;
        }
      pos++;
      i++;
    }
  GNUNET_free (routeList);
  return result;
}


/**
 * Check if the given IP address is in the list of IP addresses.
 *
 * @param list a list of networks
 * @param ip the IP to check (in network byte order)
 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
 */
static int
check_ipv4_listed (const struct IPv4NetworkSet *list,
                   const struct in_addr *add)
{
  int i;

  i = 0;
  if (list == NULL)
    return GNUNET_NO;

  while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
    {
      if ((add->s_addr & list[i].netmask.s_addr) ==
          (list[i].network.s_addr & list[i].netmask.s_addr))
        return GNUNET_YES;
      i++;
    }
  return GNUNET_NO;
}

/**
 * Check if the given IP address is in the list of IP addresses.
 *
 * @param list a list of networks
 * @param ip the IP to check (in network byte order)
 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
 */
static int
check_ipv6_listed (const struct IPv6NetworkSet *list,
                   const struct in6_addr *ip)
{
  unsigned int i;
  unsigned int j;
  struct in6_addr zero;

  if (list == NULL)
    return GNUNET_NO;

  memset (&zero, 0, sizeof (struct in6_addr));
  i = 0;
NEXT:
  while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0)
    {
      for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
        if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
            (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
          {
            i++;
            goto NEXT;
          }
      return GNUNET_YES;
    }
  return GNUNET_NO;
}


/* ****************** service struct ****************** */


/**
 * Context for "service_task".
 */
struct GNUNET_SERVICE_Context
{
  /**
   * Our configuration.
   */
  struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Handle for the server.
   */
  struct GNUNET_SERVER_Handle *server;

  /**
   * Scheduler for the server.
   */
  struct GNUNET_SCHEDULER_Handle *sched;

  /**
   * Address to bind to.
   */
  struct sockaddr *addr;

  /**
   * Name of our service.
   */
  const char *serviceName;

  /**
   * Main service-specific task to run.
   */
  GNUNET_SERVICE_Main task;

  /**
   * Closure for task.
   */
  void *task_cls;

  /**
   * IPv4 addresses that are not allowed to connect.
   */
  struct IPv4NetworkSet *v4_denied;

  /**
   * IPv6 addresses that are not allowed to connect.
   */
  struct IPv6NetworkSet *v6_denied;

  /**
   * IPv4 addresses that are allowed to connect (if not
   * set, all are allowed).
   */
  struct IPv4NetworkSet *v4_allowed;

  /**
   * IPv6 addresses that are allowed to connect (if not
   * set, all are allowed).
   */
  struct IPv6NetworkSet *v6_allowed;

  /**
   * My (default) message handlers.  Adjusted copy
   * of "defhandlers".
   */
  struct GNUNET_SERVER_MessageHandler *my_handlers;

  /**
   * Idle timeout for server.
   */
  struct GNUNET_TIME_Relative timeout;

  /**
   * Maximum buffer size for the server.
   */
  size_t maxbuf;

  /**
   * Overall success/failure of the service start.
   */
  int ret;

  /**
   * If we are daemonizing, this FD is set to the
   * pipe to the parent.  Send '.' if we started
   * ok, '!' if not.  -1 if we are not daemonizing.
   */
  int ready_confirm_fd;

  /**
   * Do we close connections if we receive messages
   * for which we have no handler?
   */
  int require_found;

  /**
   * Can clients ask us to initiate a shutdown?
   */
  int allow_shutdown;

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

};


/* ****************** message handlers ****************** */

static size_t
write_test (void *cls, size_t size, void *buf)
{
  struct GNUNET_SERVER_Client *client = cls;
  struct GNUNET_MessageHeader *msg;

  if (size < sizeof (struct GNUNET_MessageHeader))
    {
      GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
      return 0;                 /* client disconnected */
    }
  msg = (struct GNUNET_MessageHeader *) buf;
  msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
  msg->size = htons (sizeof (struct GNUNET_MessageHeader));
  GNUNET_SERVER_receive_done (client, GNUNET_OK);
  return sizeof (struct GNUNET_MessageHeader);
}

/**
 * Handler for TEST message.
 *
 * @param cls closure (refers to service)
 * @param client identification of the client
 * @param message the actual message
 */
static void
handle_test (void *cls,
             struct GNUNET_SERVER_Client *client,
             const struct GNUNET_MessageHeader *message)
{
  /* simply bounce message back to acknowledge */
  if (NULL == GNUNET_SERVER_notify_transmit_ready (client,
                                                   sizeof (struct
                                                           GNUNET_MessageHeader),
                                                   GNUNET_TIME_UNIT_FOREVER_REL,
                                                   &write_test, client))
    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
}


/**
 * Handler for SHUTDOWN message.
 *
 * @param cls closure (refers to service)
 * @param client identification of the client
 * @param message the actual message
 */
static void
handle_shutdown (void *cls,
                 struct GNUNET_SERVER_Client *client,
                 const struct GNUNET_MessageHeader *message)
{
  struct GNUNET_SERVICE_Context *service = cls;
  if (!service->allow_shutdown)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _
                  ("Received shutdown request, but configured to ignore!\n"));
      GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
      return;
    }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              _("Initiating shutdown as requested by client.\n"));
  GNUNET_assert (service->sched != NULL);
  GNUNET_SCHEDULER_shutdown (service->sched);
  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}


/**
 * Default handlers for all services.  Will be copied and the
 * "callback_cls" fields will be replaced with the specific service
 * struct.
 */
static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
  {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
   sizeof (struct GNUNET_MessageHeader)},
  {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_SHUTDOWN,
   sizeof (struct GNUNET_MessageHeader)},
  {NULL, NULL, 0, 0}
};



/* ****************** service core routines ************** */


/**
 * Check if access to the service is allowed from the given address.
 */
static int
check_access (void *cls, const struct sockaddr *addr, socklen_t addrlen)
{
  struct GNUNET_SERVICE_Context *sctx = cls;
  const struct sockaddr_in *i4;
  const struct sockaddr_in6 *i6;
  int ret;
  char buf[INET6_ADDRSTRLEN];
  uint16_t port;

  switch (addr->sa_family)
    {
    case AF_INET:
      GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
      i4 = (const struct sockaddr_in *) addr;
      port = ntohs (i4->sin_port);
      ret = ((sctx->v4_allowed == NULL) ||
             (check_ipv4_listed (sctx->v4_allowed,
                                 &i4->sin_addr)))
        && ((sctx->v4_denied == NULL) ||
            (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
      if (ret != GNUNET_OK)
        inet_ntop (AF_INET, &i4->sin_addr, buf, sizeof (buf));
      break;
    case AF_INET6:
      GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
      i6 = (const struct sockaddr_in6 *) addr;
      port = ntohs (i6->sin6_port);
      ret = ((sctx->v6_allowed == NULL) ||
             (check_ipv6_listed (sctx->v6_allowed,
                                 &i6->sin6_addr)))
        && ((sctx->v6_denied == NULL) ||
            (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
      if (ret != GNUNET_OK)
        inet_ntop (AF_INET6, &i6->sin6_addr, buf, sizeof (buf));
      break;
    default:
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Unknown address family %d\n"), addr->sa_family);
      return GNUNET_SYSERR;
    }
  if (ret != GNUNET_OK)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Access from `%s:%u' denied to service `%s'\n"),
                  buf, port, sctx->serviceName);
    }
  return ret;
}


/**
 * Get the name of the file where we will
 * write the PID of the service.
 */
static char *
get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
{

  char *pif;

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
                                               sctx->serviceName,
                                               "PIDFILE", &pif))
    return NULL;
  return pif;
}


/**
 * Parse an IPv4 access control list.
 */
static int
process_acl4 (struct IPv4NetworkSet **ret,
              struct GNUNET_SERVICE_Context *sctx, const char *option)
{
  char *opt;

  if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
    return GNUNET_OK;
  GNUNET_break (GNUNET_OK ==
                GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
                                                       sctx->serviceName,
                                                       option, &opt));
  if (NULL == (*ret = parse_ipv4_specification (opt)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _
                  ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
                  opt, sctx->serviceName, option);
      GNUNET_free (opt);
      return GNUNET_SYSERR;
    }
  GNUNET_free (opt);
  return GNUNET_OK;
}


/**
 * Parse an IPv4 access control list.
 */
static int
process_acl6 (struct IPv6NetworkSet **ret,
              struct GNUNET_SERVICE_Context *sctx, const char *option)
{
  char *opt;
  if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
    return GNUNET_OK;
  GNUNET_break (GNUNET_OK ==
                GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
                                                       sctx->serviceName,
                                                       option, &opt));
  if (NULL == (*ret = parse_ipv6_specification (opt)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _
                  ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
                  opt, sctx->serviceName, option);
      GNUNET_free (opt);
      return GNUNET_SYSERR;
    }
  GNUNET_free (opt);
  return GNUNET_OK;
}


/**
 * Setup addr, addrlen, maxbuf, idle_timeout
 * based on configuration!
 *
 * Configuration must specify a "PORT".  It may
 * specify:
 * - TIMEOUT (after how many ms does an inactive service timeout);
 * - MAXBUF (maximum incoming message size supported)
 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
 * - ALLOW_SHUTDOWN (allow clients to shutdown this service)
 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
 * - ACCEPT_FROM  (only allow connections from specified IPv4 subnets)
 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
 * - REJECT_FROM  (disallow allow connections from specified IPv4 subnets)
 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
 *
 * @return GNUNET_OK if configuration succeeded
 */
static int
setup_service (struct GNUNET_SERVICE_Context *sctx)
{
  unsigned long long maxbuf;
  unsigned long long idleout;
  char *hostname;
  unsigned long long port;
  int disablev6;
  struct addrinfo hints;
  struct addrinfo *res;
  struct addrinfo *pos;
  int ret;
  int tolerant;

  if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
                                       sctx->serviceName, "TIMEOUT"))
    {
      if (GNUNET_OK !=
          GNUNET_CONFIGURATION_get_value_number (sctx->cfg,
                                                 sctx->serviceName,
                                                 "TIMEOUT", &idleout))
        return GNUNET_SYSERR;

      sctx->timeout.value = idleout;
    }
  else
    sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
                                       sctx->serviceName, "MAXBUF"))
    {
      if (GNUNET_OK !=
          GNUNET_CONFIGURATION_get_value_number (sctx->cfg,
                                                 sctx->serviceName,
                                                 "MAXBUF", &maxbuf))
        return GNUNET_SYSERR;
    }
  else
    maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
  if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
                                       sctx->serviceName, "DISABLEV6"))
    {
      if (GNUNET_SYSERR ==
          (disablev6 = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
                                                             sctx->serviceName,
                                                             "DISABLEV6")))
        return GNUNET_SYSERR;
    }
  else
    disablev6 = GNUNET_NO;
  if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
                                       sctx->serviceName, "ALLOW_SHUTDOWN"))
    {
      if (GNUNET_SYSERR ==
          (sctx->allow_shutdown =
           GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
                                                 "ALLOW_SHUTDOWN")))
        return GNUNET_SYSERR;
    }
  else
    sctx->allow_shutdown = GNUNET_NO;

  if (!disablev6)
    {
      /* probe IPv6 support */
      ret = SOCKET (PF_INET6, SOCK_STREAM, 0);
      if (ret == -1)
        {
          if ((errno == ENOBUFS) ||
              (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
            {
              GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
              return GNUNET_SYSERR;
            }
          ret = SOCKET (PF_INET, SOCK_STREAM, 0);
          if (ret != -1)
            {
              GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                          _
                          ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
                          sctx->serviceName, strerror (errno));
              disablev6 = GNUNET_YES;
            }
        }
      if (ret != -1)
        GNUNET_break (0 == CLOSE (ret));
    }



  if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
                                       sctx->serviceName, "TOLERANT"))
    {
      if (GNUNET_SYSERR ==
          (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
                                                            sctx->serviceName,
                                                            "TOLERANT")))
        return GNUNET_SYSERR;
    }
  else
    tolerant = GNUNET_NO;
  sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;


  if ((GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (sctx->cfg,
                                              sctx->serviceName,
                                              "PORT",
                                              &port)) || (port > 65535))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _
                  ("Require valid port number for service `%s' in configuration!\n"),
                  sctx->serviceName);
      return GNUNET_SYSERR;
    }
  if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
                                       sctx->serviceName, "BINDTO"))
    {
      GNUNET_break (GNUNET_OK ==
                    GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
                                                           sctx->serviceName,
                                                           "BINDTO",
                                                           &hostname));
    }
  else
    hostname = NULL;

  if (hostname != NULL)
    {
      memset (&hints, 0, sizeof (struct addrinfo));
      if (disablev6)
        hints.ai_family = AF_INET;
      if ((0 != (ret = getaddrinfo (hostname,
                                    NULL, &hints, &res))) || (res == NULL))
        {
          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                      _("Failed to resolve `%s': %s\n"),
                      hostname, gai_strerror (ret));
          GNUNET_free (hostname);
          return GNUNET_SYSERR;
        }
      pos = res;
      while ((NULL != pos) &&
             (((disablev6) &&
               (pos->ai_family != AF_INET)) ||
              ((pos->ai_family != AF_INET) && (pos->ai_family != AF_INET6))))
        pos = pos->ai_next;
      if (pos == NULL)
        {
          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                      _("Failed to find IPv4 address for `%s'.\n"), hostname);
          freeaddrinfo (res);
          GNUNET_free (hostname);
          return GNUNET_SYSERR;
        }
      GNUNET_free (hostname);
      if (pos->ai_family == AF_INET)
        {
          GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
          sctx->addrlen = pos->ai_addrlen;
          sctx->addr = GNUNET_malloc (sctx->addrlen);
          memcpy (sctx->addr, res->ai_addr, sctx->addrlen);
          ((struct sockaddr_in *) sctx->addr)->sin_port = htons (port);
          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                      _
                      ("Configured to bind to %s address; %s connections to this service will fail!\n"),
                      "IPv4", "IPv6");
        }
      else
        {
          GNUNET_assert (pos->ai_family == AF_INET6);
          GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
          sctx->addrlen = pos->ai_addrlen;
          sctx->addr = GNUNET_malloc (sctx->addrlen);
          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                      _
                      ("Configured to bind to %s address; %s connections to this service will fail!\n"),
                      "IPv6", "IPv4");
          ((struct sockaddr_in6 *) sctx->addr)->sin6_port = htons (port);
        }
      freeaddrinfo (res);
    }
  else
    {
      /* will bind against everything, just set port */
      if (disablev6)
        {
          /* V4-only */
          sctx->addrlen = sizeof (struct sockaddr_in6);
          sctx->addr = GNUNET_malloc (sctx->addrlen);
          ((struct sockaddr_in *) sctx->addr)->sin_family = AF_INET;
          ((struct sockaddr_in *) sctx->addr)->sin_port = htons (port);
          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                      _
                      ("Configured to bind to %s address; %s connections to this service will fail!\n"),
                      "IPv4", "IPv6");
        }
      else
        {
          /* dual stack */
          sctx->addrlen = sizeof (struct sockaddr_in6);
          sctx->addr = GNUNET_malloc (sctx->addrlen);
          ((struct sockaddr_in6 *) sctx->addr)->sin6_family = AF_INET6;
          ((struct sockaddr_in6 *) sctx->addr)->sin6_port = htons (port);
        }
    }
  sctx->maxbuf = (size_t) maxbuf;
  if (sctx->maxbuf != maxbuf)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _
                  ("Value in configuration for `%s' and service `%s' too large!\n"),
                  "MAXBUF", sctx->serviceName);
      return GNUNET_SYSERR;
    }


  if ((GNUNET_OK !=
       process_acl4 (&sctx->v4_denied,
                     sctx,
                     "REJECT_FROM")) ||
      (GNUNET_OK !=
       process_acl4 (&sctx->v4_allowed,
                     sctx,
                     "ACCEPT_FROM")) ||
      (GNUNET_OK !=
       process_acl6 (&sctx->v6_denied,
                     sctx,
                     "REJECT_FROM6")) ||
      (GNUNET_OK != process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6")))
    return GNUNET_SYSERR;
  return GNUNET_OK;
}


/**
 * Get the name of the user that'll be used
 * to provide the service.
 */
static char *
get_user_name (struct GNUNET_SERVICE_Context *sctx)
{

  char *un;

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
                                               sctx->serviceName,
                                               "USERNAME", &un))
    return NULL;
  return un;
}

/**
 * Write PID file.
 */
static int
write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
{
  FILE *pidfd;
  char *pif;
  char *user;
  char *rdir;
  int len;

  if (NULL == (pif = get_pid_file_name (sctx)))
    return GNUNET_OK;           /* no file desired */
  user = get_user_name (sctx);
  rdir = GNUNET_strdup (pif);
  len = strlen (rdir);
  while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
    len--;
  rdir[len] = '\0';
  if (0 != ACCESS (rdir, F_OK))
    {
      /* we get to create a directory -- and claim it
         as ours! */
      GNUNET_DISK_directory_create (rdir);
      if ((user != NULL) && (0 < strlen (user)))
        GNUNET_DISK_file_change_owner (rdir, user);
    }
  if (0 != ACCESS (rdir, W_OK | X_OK))
    {
      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
      GNUNET_free (rdir);
      GNUNET_free_non_null (user);
      GNUNET_free (pif);
      return GNUNET_SYSERR;
    }
  GNUNET_free (rdir);
  pidfd = FOPEN (pif, "w");
  if (pidfd == NULL)
    {
      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
      GNUNET_free (pif);
      GNUNET_free_non_null (user);
      return GNUNET_SYSERR;
    }
  if (0 > FPRINTF (pidfd, "%u", pid))
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
  GNUNET_break (0 == fclose (pidfd));
  if ((user != NULL) && (0 < strlen (user)))
    GNUNET_DISK_file_change_owner (pif, user);
  GNUNET_free_non_null (user);
  GNUNET_free (pif);
  return GNUNET_OK;
}


/**
 * Initial task for the service.
 */
static void
service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_SERVICE_Context *sctx = cls;
  unsigned int i;

  sctx->sched = tc->sched;
  sctx->server = GNUNET_SERVER_create (tc->sched,
                                       &check_access,
                                       sctx,
                                       sctx->addr,
                                       sctx->addrlen,
                                       sctx->maxbuf,
                                       sctx->timeout, sctx->require_found);
  if (sctx->server == NULL)
    {
      sctx->ret = GNUNET_SYSERR;
      return;
    }
  sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
  memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
  i = 0;
  while ((sctx->my_handlers[i].callback != NULL))
    sctx->my_handlers[i++].callback_cls = sctx;
  GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
  if (sctx->ready_confirm_fd != -1)
    {
      GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
      GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
      sctx->ready_confirm_fd = -1;
      write_pid_file (sctx, getpid ());
    }

  sctx->task (sctx->task_cls, tc->sched, sctx->server, sctx->cfg);
}


/**
 * Detach from terminal.
 */
static int
detach_terminal (struct GNUNET_SERVICE_Context *sctx)
{
  pid_t pid;
  int nullfd;
  int filedes[2];

#ifndef MINGW
  if (0 != PIPE (filedes))
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
      return GNUNET_SYSERR;
    }
  pid = fork ();
  if (pid < 0)
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
      return GNUNET_SYSERR;
    }
  if (pid != 0)
    {
      /* Parent */
      char c;

      GNUNET_break (0 == CLOSE (filedes[1]));
      c = 'X';
      if (1 != READ (filedes[0], &c, sizeof (char)))
        GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
      fflush (stdout);
      switch (c)
        {
        case '.':
          exit (0);
        case 'I':
          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                      _("Service process failed to initialize\n"));
          break;
        case 'S':
          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                      _
                      ("Service process could not initialize server function\n"));
          break;
        case 'X':
          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                      _("Service process failed to report status\n"));
          break;
        }
      exit (1);                 /* child reported error */
    }
  GNUNET_break (0 == CLOSE (0));
  GNUNET_break (0 == CLOSE (1));
  GNUNET_break (0 == CLOSE (filedes[0]));
  nullfd = GNUNET_DISK_file_open ("/dev/null", O_RDWR | O_APPEND);
  if (nullfd < 0)
    return GNUNET_SYSERR;
  /* set stdin/stdout to /dev/null */
  if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
      return GNUNET_SYSERR;
    }
  /* Detach from controlling terminal */
  pid = setsid ();
  if (pid == -1)
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsid");
  sctx->ready_confirm_fd = filedes[1];
#else
  /* FIXME: we probably need to do something else
     elsewhere in order to fork the process itself... */
  FreeConsole ();
#endif
  return GNUNET_OK;
}


/**
 * Set user ID.
 */
static int
set_user_id (struct GNUNET_SERVICE_Context *sctx)
{
  char *user;

  if (NULL == (user = get_user_name (sctx)))
    return GNUNET_OK;           /* keep */
#ifndef MINGW
  struct passwd *pws;

  errno = 0;
  pws = getpwnam (user);
  if (pws == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _("Cannot obtain information about user `%s': %s\n"),
                  user, errno == 0 ? _("No such user") : STRERROR (errno));
      GNUNET_free (user);
      return GNUNET_SYSERR;
    }
  if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
#if HAVE_INITGROUPS
      (0 != initgroups (user, pws->pw_gid)) ||
#endif
      (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
    {
      if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
          (0 != setreuid (pws->pw_uid, pws->pw_uid)))
        {
          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                      _("Cannot change user/group to `%s': %s\n"), user,
                      STRERROR (errno));
          GNUNET_free (user);
          return GNUNET_SYSERR;
        }
    }
#endif
  GNUNET_free (user);
  return GNUNET_OK;
}


/**
 * Delete the PID file that was created by our parent.
 */
static void
pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
{
  char *pif = get_pid_file_name (sctx);
  if (pif == NULL)
    return;                     /* no PID file */
  if (0 != UNLINK (pif))
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
  GNUNET_free (pif);
}

/**
 * Run a standard GNUnet service startup sequence (initialize loggers
 * and configuration, parse options).
 *
 * @param argc number of command line arguments
 * @param argv command line arguments
 * @param serviceName our service name
 * @param task main task of the service
 * @param task_cls closure for task
 * @param term termination task of the service
 * @param term_cls closure for term
 * @return GNUNET_SYSERR on error, GNUNET_OK
 *         if we shutdown nicely
 */
int
GNUNET_SERVICE_run (int argc,
                    char *const *argv,
                    const char *serviceName,
                    GNUNET_SERVICE_Main task,
                    void *task_cls, GNUNET_SERVICE_Term term, void *term_cls)
{
  char *cfg_fn;
  char *loglev;
  char *logfile;
  int do_daemonize;
  struct GNUNET_SERVICE_Context sctx;
  struct GNUNET_GETOPT_CommandLineOption service_options[] = {
    GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
    {'d', "daemonize", NULL,
     gettext_noop ("do daemonize (detach from terminal)"), 0,
     GNUNET_GETOPT_set_one, &do_daemonize},
    GNUNET_GETOPT_OPTION_HELP (serviceName),
    GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
    GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
    GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
    GNUNET_GETOPT_OPTION_END
  };
  do_daemonize = 0;
  logfile = NULL;
  loglev = GNUNET_strdup ("WARNING");
  cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_DAEMON_CONFIG_FILE);
  memset (&sctx, 0, sizeof (sctx));
  sctx.ready_confirm_fd = -1;
  sctx.ret = GNUNET_OK;
  sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  sctx.maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
  sctx.task = task;
  sctx.serviceName = serviceName;
  sctx.cfg = GNUNET_CONFIGURATION_create ();
  /* setup subsystems */
  if ((GNUNET_SYSERR ==
       GNUNET_GETOPT_run (serviceName,
                          sctx.cfg,
                          service_options,
                          argc,
                          argv)) ||
      (GNUNET_OK !=
       GNUNET_log_setup (serviceName, loglev, logfile)) ||
      (GNUNET_OK !=
       GNUNET_CONFIGURATION_load (sctx.cfg, cfg_fn)) ||
      (GNUNET_OK !=
       setup_service (&sctx)) ||
      ((do_daemonize == 1) &&
       (GNUNET_OK != detach_terminal (&sctx))) ||
      (GNUNET_OK != set_user_id (&sctx)))
    {
      if (sctx.ready_confirm_fd != -1)
        {
          if (1 != WRITE (sctx.ready_confirm_fd, "I", 1))
            GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
          GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
        }
      GNUNET_CONFIGURATION_destroy (sctx.cfg);
      GNUNET_free_non_null (sctx.addr);
      GNUNET_free_non_null (logfile);
      GNUNET_free (loglev);
      GNUNET_free (cfg_fn);
      GNUNET_free_non_null (sctx.v4_denied);
      GNUNET_free_non_null (sctx.v6_denied);
      GNUNET_free_non_null (sctx.v4_allowed);
      GNUNET_free_non_null (sctx.v6_allowed);
      return GNUNET_SYSERR;
    }

  /* actually run service */
  GNUNET_SCHEDULER_run (&service_task, &sctx);
  if (sctx.ready_confirm_fd != -1)
    {
      if (1 != WRITE (sctx.ready_confirm_fd, "S", 1))
        GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
      GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
    }

  /* shutdown */
  if (term != NULL)
    term (term_cls, sctx.cfg);
  if ((do_daemonize == 1) && (sctx.server != NULL))
    pid_file_delete (&sctx);
  if (sctx.server != NULL)
    GNUNET_SERVER_destroy (sctx.server);
  GNUNET_free_non_null (sctx.my_handlers);
  GNUNET_CONFIGURATION_destroy (sctx.cfg);
  GNUNET_free_non_null (sctx.addr);
  GNUNET_free_non_null (logfile);
  GNUNET_free (loglev);
  GNUNET_free (cfg_fn);
  GNUNET_free_non_null (sctx.v4_denied);
  GNUNET_free_non_null (sctx.v6_denied);
  GNUNET_free_non_null (sctx.v4_allowed);
  GNUNET_free_non_null (sctx.v6_allowed);
  return sctx.ret;
}


/**
 * Run a service startup sequence within an existing
 * initialized system.
 *
 * @param serviceName our service name
 * @param sched scheduler to use
 * @param cfg configuration to use
 * @return NULL on error, service handle
 */
struct GNUNET_SERVICE_Context *
GNUNET_SERVICE_start (const char *serviceName,
                      struct GNUNET_SCHEDULER_Handle *sched,
                      struct GNUNET_CONFIGURATION_Handle *cfg)
{
  int i;
  struct GNUNET_SERVICE_Context *sctx;

  sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
  sctx->ready_confirm_fd = -1;  /* no daemonizing */
  sctx->ret = GNUNET_OK;
  sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  sctx->maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
  sctx->serviceName = serviceName;
  sctx->cfg = cfg;
  sctx->sched = sched;

  /* setup subsystems */
  if ((GNUNET_OK != setup_service (sctx)) ||
      (NULL == (sctx->server = GNUNET_SERVER_create (sched,
                                                     &check_access,
                                                     sctx,
                                                     sctx->addr,
                                                     sctx->addrlen,
                                                     sctx->maxbuf,
                                                     sctx->timeout,
                                                     sctx->require_found))))
    {
      GNUNET_SERVICE_stop (sctx);
      return NULL;
    }
  sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
  memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
  i = 0;
  while ((sctx->my_handlers[i].callback != NULL))
    sctx->my_handlers[i++].callback_cls = sctx;
  GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);


  return sctx;
}

/**
 * Obtain the server used by a service.  Note that the server must NOT
 * be destroyed by the caller.
 *
 * @param ctx the service context returned from the start function
 * @return handle to the server for this service, NULL if there is none
 */
struct GNUNET_SERVER_Handle *
GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
{
  return ctx->server;
}


/**
 * Stop a service that was started with "GNUNET_SERVICE_start".
 *
 * @param ctx the service context returned from the start function
 */
void
GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
{
  if (NULL != sctx->server)
    GNUNET_SERVER_destroy (sctx->server);
  GNUNET_free_non_null (sctx->my_handlers);
  GNUNET_free_non_null (sctx->addr);
  GNUNET_free_non_null (sctx->v4_denied);
  GNUNET_free_non_null (sctx->v6_denied);
  GNUNET_free_non_null (sctx->v4_allowed);
  GNUNET_free_non_null (sctx->v6_allowed);
  GNUNET_free (sctx);
}


/* end of service.c */