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
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
|
/*
This file is part of GNUnet.
(C) 2001, 2002, 2003, 2004, 2005, 2006, 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 hostlist/hostlist-client.c
* @brief hostlist support. Downloads HELLOs via HTTP.
* @author Christian Grothoff
* @author Matthias Wachs
*/
#include "platform.h"
#include "hostlist-client.h"
#include "gnunet_core_service.h"
#include "gnunet_hello_lib.h"
#include "gnunet_statistics_service.h"
#include "gnunet_transport_service.h"
#include "gnunet-daemon-hostlist.h"
#include <curl/curl.h>
#include "gnunet_common.h"
#include "gnunet_bio_lib.h"
/**
* Number of connections that we must have to NOT download
* hostlists anymore.
*/
#define MIN_CONNECTIONS 4
/**
* Interval between two advertised hostlist tests
*/
#define TESTING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
/**
* A single hostlist obtained by hostlist advertisements
*/
struct Hostlist
{
/**
* previous entry, used to manage entries in a double linked list
*/
struct Hostlist *prev;
/**
* next entry, used to manage entries in a double linked list
*/
struct Hostlist *next;
/**
* URI where hostlist can be obtained
*/
const char *hostlist_uri;
/**
* Value describing the quality of the hostlist, the bigger the better but (should) never < 0
* used for deciding which hostlist is replaced if MAX_NUMBER_HOSTLISTS in data structure is reached
* intial value = HOSTLIST_INITIAL
* increased every successful download by HOSTLIST_SUCCESSFULL_DOWNLOAD
* increased every successful download by number of obtained HELLO messages
* decreased every failed download by HOSTLIST_SUCCESSFULL_DOWNLOAD
*/
uint64_t quality;
/**
* Time the hostlist advertisement was recieved and the entry was created
*/
struct GNUNET_TIME_Absolute time_creation;
/**
* Last time the hostlist was obtained
*/
struct GNUNET_TIME_Absolute time_last_usage;
/**
* Number of HELLO messages obtained during last download
*/
uint32_t hello_count;
/**
* Number of times the hostlist was successfully obtained
*/
uint32_t times_used;
};
/**
* Our configuration.
*/
static const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Statistics handle.
*/
static struct GNUNET_STATISTICS_Handle *stats;
/**
* Transport handle.
*/
static struct GNUNET_TRANSPORT_Handle *transport;
/**
* Proxy that we are using (can be NULL).
*/
static char *proxy;
/**
* Number of bytes valid in 'download_buffer'.
*/
static size_t download_pos;
/**
* Current URL that we are using.
*/
static char *current_url;
/**
* Current CURL handle.
*/
static CURL *curl;
/**
* Current multi-CURL handle.
*/
static CURLM *multi;
/**
* How many bytes did we download from the current hostlist URL?
*/
static uint32_t stat_bytes_downloaded;
/**
* Amount of time we wait between hostlist downloads.
*/
static struct GNUNET_TIME_Relative hostlist_delay;
/**
* ID of the task, checking if hostlist download should take plate
*/
static GNUNET_SCHEDULER_TaskIdentifier ti_check_download;
/**
* ID of the task downloading the hostlist
*/
static GNUNET_SCHEDULER_TaskIdentifier ti_download;
/**
* ID of the task saving the hostlsit in a regular intervall
*/
static GNUNET_SCHEDULER_TaskIdentifier ti_saving_task;
/**
* ID of the task called to initiate a download
*/
static GNUNET_SCHEDULER_TaskIdentifier ti_download_dispatcher_task;
/**
* ID of the task controlling the locking between two hostlist tests
*/
static GNUNET_SCHEDULER_TaskIdentifier ti_testing_intervall_task;
/**
* At what time MUST the current hostlist request be done?
*/
static struct GNUNET_TIME_Absolute end_time;
/**
* Head of the linked list used to store hostlists
*/
static struct Hostlist *linked_list_head;
/**
* Tail of the linked list used to store hostlists
*/
static struct Hostlist *linked_list_tail;
/**
* Current hostlist used for downloading
*/
static struct Hostlist *current_hostlist;
/**
* Size of the linke list used to store hostlists
*/
static unsigned int linked_list_size;
/**
* Head of the linked list used to store hostlists
*/
static struct Hostlist *hostlist_to_test;
/**
* Handle for our statistics GET operation.
*/
static struct GNUNET_STATISTICS_GetHandle *sget;
/**
* Set to GNUNET_YES if the current URL had some problems.
*/
static int stat_bogus_url;
/**
* Value controlling if a hostlist is tested at the moment
*/
static int stat_testing_hostlist;
/**
* Value controlling if a hostlist testing is allowed at the moment
*/
static int stat_testing_allowed;
/**
* Value controlling if a hostlist download is running at the moment
*/
static int stat_download_in_progress;
/**
* Value saying if a preconfigured bootstrap server is used
*/
static unsigned int stat_use_bootstrap;
/**
* Set if we are allowed to learn new hostlists and use them
*/
static int stat_learning;
/**
* Value saying if hostlist download was successful
*/
static unsigned int stat_download_successful;
/**
* Value saying how many valid HELLO messages were obtained during download
*/
static unsigned int stat_hellos_obtained;
/**
* Number of active connections (according to core service).
*/
static unsigned int stat_connection_count;
/**
* Process downloaded bits by calling callback on each HELLO.
*
* @param ptr buffer with downloaded data
* @param size size of a record
* @param nmemb number of records downloaded
* @param ctx unused
* @return number of bytes that were processed (always size*nmemb)
*/
static size_t
callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
{
static char download_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
const char *cbuf = ptr;
const struct GNUNET_MessageHeader *msg;
size_t total;
size_t cpy;
size_t left;
uint16_t msize;
total = size * nmemb;
stat_bytes_downloaded += total;
if ((total == 0) || (stat_bogus_url))
{
return total; /* ok, no data or bogus data */
}
GNUNET_STATISTICS_update (stats,
gettext_noop
("# bytes downloaded from hostlist servers"),
(int64_t) total, GNUNET_NO);
left = total;
while ((left > 0) || (download_pos > 0))
{
cpy = GNUNET_MIN (left, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - download_pos);
memcpy (&download_buffer[download_pos], cbuf, cpy);
cbuf += cpy;
download_pos += cpy;
left -= cpy;
if (download_pos < sizeof (struct GNUNET_MessageHeader))
{
GNUNET_assert (0 == left);
break;
}
msg = (const struct GNUNET_MessageHeader *) download_buffer;
msize = ntohs (msg->size);
if (msize < sizeof (struct GNUNET_MessageHeader))
{
GNUNET_STATISTICS_update (stats,
gettext_noop
("# invalid HELLOs downloaded from hostlist servers"),
1, GNUNET_NO);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Invalid `%s' message received from hostlist at `%s'\n"),
"HELLO", current_url);
stat_hellos_obtained++;
stat_bogus_url = 1;
return total;
}
if (download_pos < msize)
{
GNUNET_assert (left == 0);
break;
}
if (GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) msg) == msize)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Received valid `%s' message from hostlist server.\n",
"HELLO");
GNUNET_STATISTICS_update (stats,
gettext_noop
("# valid HELLOs downloaded from hostlist servers"),
1, GNUNET_NO);
stat_hellos_obtained++;
GNUNET_TRANSPORT_offer_hello (transport, msg, NULL, NULL);
}
else
{
GNUNET_STATISTICS_update (stats,
gettext_noop
("# invalid HELLOs downloaded from hostlist servers"),
1, GNUNET_NO);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Invalid `%s' message received from hostlist at `%s'\n"),
"HELLO", current_url);
stat_bogus_url = GNUNET_YES;
stat_hellos_obtained++;
return total;
}
memmove (download_buffer, &download_buffer[msize], download_pos - msize);
download_pos -= msize;
}
return total;
}
/**
* Obtain a hostlist URL that we should use.
*
* @return NULL if there is no URL available
*/
static char *
get_bootstrap_server ()
{
char *servers;
char *ret;
size_t urls;
size_t pos;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST", "SERVERS",
&servers))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
"hostlist", "SERVERS");
return NULL;
}
urls = 0;
if (strlen (servers) > 0)
{
urls++;
pos = strlen (servers) - 1;
while (pos > 0)
{
if (servers[pos] == ' ')
urls++;
pos--;
}
}
if (urls == 0)
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
"hostlist", "SERVERS");
GNUNET_free (servers);
return NULL;
}
urls = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, urls) + 1;
pos = strlen (servers) - 1;
while (pos > 0)
{
if (servers[pos] == ' ')
{
urls--;
servers[pos] = '\0';
}
if (urls == 0)
{
pos++;
break;
}
pos--;
}
ret = GNUNET_strdup (&servers[pos]);
GNUNET_free (servers);
return ret;
}
/**
* Method deciding if a preconfigured or advertisied hostlist is used on a 50:50 ratio
* @return uri to use, NULL if there is no URL available
*/
static char *
download_get_url ()
{
uint32_t index;
unsigned int counter;
struct Hostlist *pos;
if (GNUNET_NO == stat_learning)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Using preconfigured bootstrap server\n");
current_hostlist = NULL;
return get_bootstrap_server ();
}
if ((GNUNET_YES == stat_testing_hostlist) && (NULL != hostlist_to_test))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Testing new advertised hostlist if it is obtainable\n");
current_hostlist = hostlist_to_test;
return GNUNET_strdup (hostlist_to_test->hostlist_uri);
}
if ((GNUNET_YES == stat_use_bootstrap) || (linked_list_size == 0))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Using preconfigured bootstrap server\n");
current_hostlist = NULL;
return get_bootstrap_server ();
}
index =
GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, linked_list_size);
counter = 0;
pos = linked_list_head;
while (counter < index)
{
pos = pos->next;
counter++;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using learned hostlist `%s'\n",
pos->hostlist_uri);
current_hostlist = pos;
return GNUNET_strdup (pos->hostlist_uri);
}
#define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt (c, a, b); if (CURLE_OK != ret) GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror (ret)); } while (0)
/**
* Method to save hostlist to a file during hostlist client shutdown
* @param shutdown set if called because of shutdown, entries in linked list will be destroyed
*/
static void
save_hostlist_file (int shutdown);
/**
* add val2 to val1 with overflow check
* @param val1 value 1
* @param val2 value 2
* @return result
*/
static uint64_t
checked_add (uint64_t val1, uint64_t val2)
{
static uint64_t temp;
static uint64_t maxv;
maxv = 0;
maxv--;
temp = val1 + val2;
if (temp < val1)
return maxv;
return temp;
}
/**
* Subtract val2 from val1 with underflow check
* @param val1 value 1
* @param val2 value 2
* @return result
*/
static uint64_t
checked_sub (uint64_t val1, uint64_t val2)
{
if (val1 <= val2)
return 0;
return (val1 - val2);
}
/**
* Method to check if a URI is in hostlist linked list
* @param uri uri to check
* @return GNUNET_YES if existing in linked list, GNUNET_NO if not
*/
static int
linked_list_contains (const char *uri)
{
struct Hostlist *pos;
pos = linked_list_head;
while (pos != NULL)
{
if (0 == strcmp (pos->hostlist_uri, uri))
return GNUNET_YES;
pos = pos->next;
}
return GNUNET_NO;
}
/**
* Method returning the hostlist element with the lowest quality in the datastore
* @return hostlist with lowest quality
*/
static struct Hostlist *
linked_list_get_lowest_quality ()
{
struct Hostlist *pos;
struct Hostlist *lowest;
if (linked_list_size == 0)
return NULL;
lowest = linked_list_head;
pos = linked_list_head->next;
while (pos != NULL)
{
if (pos->quality < lowest->quality)
lowest = pos;
pos = pos->next;
}
return lowest;
}
/**
* Method to insert a hostlist into the datastore. If datastore
* contains maximum number of elements, the elements with lowest
* quality is dismissed
*/
static void
insert_hostlist ()
{
struct Hostlist *lowest_quality;
if (MAX_NUMBER_HOSTLISTS <= linked_list_size)
{
/* No free entries available, replace existing entry */
lowest_quality = linked_list_get_lowest_quality ();
GNUNET_assert (lowest_quality != NULL);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Removing hostlist with URI `%s' which has the worst quality of all (%llu)\n",
lowest_quality->hostlist_uri,
(unsigned long long) lowest_quality->quality);
GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail,
lowest_quality);
linked_list_size--;
GNUNET_free (lowest_quality);
}
GNUNET_CONTAINER_DLL_insert (linked_list_head, linked_list_tail,
hostlist_to_test);
linked_list_size++;
GNUNET_STATISTICS_set (stats, gettext_noop ("# advertised hostlist URIs"),
linked_list_size, GNUNET_NO);
stat_testing_hostlist = GNUNET_NO;
}
/**
* Method updating hostlist statistics
*/
static void
update_hostlist ()
{
char *stat;
if (((stat_use_bootstrap == GNUNET_NO) && (NULL != current_hostlist)) ||
((stat_testing_hostlist == GNUNET_YES) && (NULL != current_hostlist)))
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Updating hostlist statics for URI `%s'\n",
current_hostlist->hostlist_uri);
current_hostlist->hello_count = stat_hellos_obtained;
current_hostlist->time_last_usage = GNUNET_TIME_absolute_get ();
current_hostlist->quality =
checked_add (current_hostlist->quality,
(stat_hellos_obtained * HOSTLIST_SUCCESSFUL_HELLO));
if (GNUNET_YES == stat_download_successful)
{
current_hostlist->times_used++;
current_hostlist->quality =
checked_add (current_hostlist->quality, HOSTLIST_SUCCESSFUL_DOWNLOAD);
GNUNET_asprintf (&stat, gettext_noop ("# advertised URI `%s' downloaded"),
current_hostlist->hostlist_uri);
GNUNET_STATISTICS_update (stats, stat, 1, GNUNET_YES);
GNUNET_free (stat);
}
else
current_hostlist->quality =
checked_sub (current_hostlist->quality, HOSTLIST_FAILED_DOWNLOAD);
}
current_hostlist = NULL;
/* Alternating the usage of preconfigured and learned hostlists */
if (stat_testing_hostlist == GNUNET_YES)
return;
if (GNUNET_YES == stat_learning)
{
if (stat_use_bootstrap == GNUNET_YES)
stat_use_bootstrap = GNUNET_NO;
else
stat_use_bootstrap = GNUNET_YES;
}
else
stat_use_bootstrap = GNUNET_YES;
}
/**
* Clean up the state from the task that downloaded the
* hostlist and schedule the next task.
*/
static void
clean_up ()
{
CURLMcode mret;
if ((stat_testing_hostlist == GNUNET_YES) &&
(GNUNET_NO == stat_download_successful) && (NULL != hostlist_to_test))
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_
("Advertised hostlist with URI `%s' could not be downloaded. Advertised URI gets dismissed.\n"),
hostlist_to_test->hostlist_uri);
}
if (stat_testing_hostlist == GNUNET_YES)
{
stat_testing_hostlist = GNUNET_NO;
}
if (NULL != hostlist_to_test)
{
GNUNET_free (hostlist_to_test);
hostlist_to_test = NULL;
}
if (multi != NULL)
{
mret = curl_multi_remove_handle (multi, curl);
if (mret != CURLM_OK)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
"curl_multi_remove_handle", __FILE__, __LINE__,
curl_multi_strerror (mret));
}
mret = curl_multi_cleanup (multi);
if (mret != CURLM_OK)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
"curl_multi_cleanup", __FILE__, __LINE__,
curl_multi_strerror (mret));
multi = NULL;
}
if (curl != NULL)
{
curl_easy_cleanup (curl);
curl = NULL;
}
GNUNET_free_non_null (current_url);
current_url = NULL;
stat_bytes_downloaded = 0;
stat_download_in_progress = GNUNET_NO;
}
/**
* Task that is run when we are ready to receive more data from the hostlist
* server.
*
* @param cls closure, unused
* @param tc task context, unused
*/
static void
task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
/**
* Ask CURL for the select set and then schedule the
* receiving task with the scheduler.
*/
static void
download_prepare ()
{
CURLMcode mret;
fd_set rs;
fd_set ws;
fd_set es;
int max;
struct GNUNET_NETWORK_FDSet *grs;
struct GNUNET_NETWORK_FDSet *gws;
long timeout;
struct GNUNET_TIME_Relative rtime;
max = -1;
FD_ZERO (&rs);
FD_ZERO (&ws);
FD_ZERO (&es);
mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
if (mret != CURLM_OK)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
"curl_multi_fdset", __FILE__, __LINE__,
curl_multi_strerror (mret));
clean_up ();
return;
}
mret = curl_multi_timeout (multi, &timeout);
if (mret != CURLM_OK)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
"curl_multi_timeout", __FILE__, __LINE__,
curl_multi_strerror (mret));
clean_up ();
return;
}
rtime =
GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (end_time),
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_MILLISECONDS, timeout));
grs = GNUNET_NETWORK_fdset_create ();
gws = GNUNET_NETWORK_fdset_create ();
GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Scheduling task for hostlist download using cURL\n");
ti_download =
GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
rtime, grs, gws,
&task_download, multi);
GNUNET_NETWORK_fdset_destroy (gws);
GNUNET_NETWORK_fdset_destroy (grs);
}
/**
* Task that is run when we are ready to receive more data from the hostlist
* server.
*
* @param cls closure, unused
* @param tc task context, unused
*/
static void
task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
int running;
struct CURLMsg *msg;
CURLMcode mret;
ti_download = GNUNET_SCHEDULER_NO_TASK;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Shutdown requested while trying to download hostlist from `%s'\n",
current_url);
update_hostlist ();
clean_up ();
return;
}
if (GNUNET_TIME_absolute_get_remaining (end_time).rel_value == 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
_("Timeout trying to download hostlist from `%s'\n"),
current_url);
update_hostlist ();
clean_up ();
return;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Ready for processing hostlist client request\n");
do
{
running = 0;
if (stat_bytes_downloaded > MAX_BYTES_PER_HOSTLISTS)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Download limit of %u bytes exceeded, stopping download\n"),
MAX_BYTES_PER_HOSTLISTS);
clean_up ();
return;
}
mret = curl_multi_perform (multi, &running);
if (running == 0)
{
do
{
msg = curl_multi_info_read (multi, &running);
GNUNET_break (msg != NULL);
if (msg == NULL)
break;
switch (msg->msg)
{
case CURLMSG_DONE:
if ((msg->data.result != CURLE_OK) &&
(msg->data.result != CURLE_GOT_NOTHING))
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Download of hostlist from `%s' failed: `%s'\n"),
current_url,
curl_easy_strerror (msg->data.result));
else
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Download of hostlist `%s' completed.\n"),
current_url);
stat_download_successful = GNUNET_YES;
update_hostlist ();
if (GNUNET_YES == stat_testing_hostlist)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_
("Adding successfully tested hostlist `%s' datastore.\n"),
current_url);
insert_hostlist ();
hostlist_to_test = NULL;
stat_testing_hostlist = GNUNET_NO;
}
}
clean_up ();
return;
default:
break;
}
}
while ((running > 0));
}
}
while (mret == CURLM_CALL_MULTI_PERFORM);
if (mret != CURLM_OK)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("%s failed at %s:%d: `%s'\n"),
"curl_multi_perform", __FILE__, __LINE__,
curl_multi_strerror (mret));
clean_up ();
}
download_prepare ();
}
/**
* Main function that will download a hostlist and process its
* data.
*/
static void
download_hostlist ()
{
CURLcode ret;
CURLMcode mret;
current_url = download_get_url ();
if (current_url == NULL)
return;
curl = curl_easy_init ();
multi = NULL;
if (curl == NULL)
{
GNUNET_break (0);
clean_up ();
return;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
_("Bootstrapping using hostlist at `%s'.\n"), current_url);
stat_download_in_progress = GNUNET_YES;
stat_download_successful = GNUNET_NO;
stat_hellos_obtained = 0;
stat_bytes_downloaded = 0;
GNUNET_STATISTICS_update (stats,
gettext_noop ("# hostlist downloads initiated"), 1,
GNUNET_NO);
if (proxy != NULL)
CURL_EASY_SETOPT (curl, CURLOPT_PROXY, proxy);
download_pos = 0;
stat_bogus_url = 0;
CURL_EASY_SETOPT (curl, CURLOPT_WRITEFUNCTION, &callback_download);
if (ret != CURLE_OK)
{
clean_up ();
return;
}
CURL_EASY_SETOPT (curl, CURLOPT_WRITEDATA, NULL);
if (ret != CURLE_OK)
{
clean_up ();
return;
}
CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
/* no need to abort if the above failed */
CURL_EASY_SETOPT (curl, CURLOPT_URL, current_url);
if (ret != CURLE_OK)
{
clean_up ();
return;
}
CURL_EASY_SETOPT (curl, CURLOPT_FAILONERROR, 1);
#if 0
CURL_EASY_SETOPT (curl, CURLOPT_VERBOSE, 1);
#endif
CURL_EASY_SETOPT (curl, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
if (0 == strncmp (current_url, "http", 4))
CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
CURL_EASY_SETOPT (curl, CURLOPT_CONNECTTIMEOUT, 60L);
CURL_EASY_SETOPT (curl, CURLOPT_TIMEOUT, 60L);
#if 0
/* this should no longer be needed; we're now single-threaded! */
CURL_EASY_SETOPT (curl, CURLOPT_NOSIGNAL, 1);
#endif
multi = curl_multi_init ();
if (multi == NULL)
{
GNUNET_break (0);
/* clean_up (); */
return;
}
mret = curl_multi_add_handle (multi, curl);
if (mret != CURLM_OK)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
"curl_multi_add_handle", __FILE__, __LINE__,
curl_multi_strerror (mret));
mret = curl_multi_cleanup (multi);
if (mret != CURLM_OK)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
"curl_multi_cleanup", __FILE__, __LINE__,
curl_multi_strerror (mret));
multi = NULL;
clean_up ();
return;
}
end_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
download_prepare ();
}
static void
task_download_dispatcher (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
ti_download_dispatcher_task = GNUNET_SCHEDULER_NO_TASK;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
return;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download is initiated...\n");
if (GNUNET_NO == stat_download_in_progress)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download can start immediately...\n");
download_hostlist ();
}
else
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Download in progess, have to wait...\n");
ti_download_dispatcher_task =
GNUNET_SCHEDULER_add_delayed (WAITING_INTERVALL,
&task_download_dispatcher, NULL);
}
}
/**
* Task that checks if we should try to download a hostlist.
* If so, we initiate the download, otherwise we schedule
* this task again for a later time.
*/
static void
task_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
static int once;
struct GNUNET_TIME_Relative delay;
ti_check_download = GNUNET_SCHEDULER_NO_TASK;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
return;
if (stats == NULL)
{
curl_global_cleanup ();
return; /* in shutdown */
}
if ( (stat_connection_count < MIN_CONNECTIONS) &&
(GNUNET_SCHEDULER_NO_TASK == ti_download_dispatcher_task) )
ti_download_dispatcher_task =
GNUNET_SCHEDULER_add_now (&task_download_dispatcher, NULL);
delay = hostlist_delay;
if (hostlist_delay.rel_value == 0)
hostlist_delay = GNUNET_TIME_UNIT_SECONDS;
else
hostlist_delay = GNUNET_TIME_relative_multiply (hostlist_delay, 2);
if (hostlist_delay.rel_value >
GNUNET_TIME_UNIT_HOURS.rel_value * (1 + stat_connection_count))
hostlist_delay =
GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS,
(1 + stat_connection_count));
GNUNET_STATISTICS_set (stats,
gettext_noop
("# milliseconds between hostlist downloads"),
hostlist_delay.rel_value, GNUNET_YES);
if (0 == once)
{
delay = GNUNET_TIME_UNIT_ZERO;
once = 1;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Have %u/%u connections. Will consider downloading hostlist in %s\n"),
stat_connection_count, MIN_CONNECTIONS,
GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
ti_check_download = GNUNET_SCHEDULER_add_delayed (delay, &task_check, NULL);
}
/**
* This tasks sets hostlist testing to allowed after intervall between to testings is reached
*
* @param cls closure
* @param tc TaskContext
*/
static void
task_testing_intervall_reset (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
ti_testing_intervall_task = GNUNET_SCHEDULER_NO_TASK;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
return;
stat_testing_allowed = GNUNET_OK;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Testing new hostlist advertisements is allowed again\n");
}
/**
* Task that writes hostlist entries to a file on a regular base
*
* @param cls closure
* @param tc TaskContext
*/
static void
task_hostlist_saving (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
ti_saving_task = GNUNET_SCHEDULER_NO_TASK;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
return;
save_hostlist_file (GNUNET_NO);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Hostlists will be saved to file again in %s\n",
GNUNET_STRINGS_relative_time_to_string(SAVING_INTERVALL, GNUNET_YES));
ti_saving_task =
GNUNET_SCHEDULER_add_delayed (SAVING_INTERVALL, &task_hostlist_saving,
NULL);
}
/**
* Method called whenever a given peer connects.
*
* @param cls closure
* @param peer peer identity this notification is about
* @param atsi performance data
* @param atsi_count number of records in 'atsi'
*/
static void
handler_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_ATS_Information *atsi,
unsigned int atsi_count)
{
GNUNET_assert (stat_connection_count < UINT_MAX);
stat_connection_count++;
GNUNET_STATISTICS_update (stats, gettext_noop ("# active connections"), 1,
GNUNET_NO);
}
/**
* Method called whenever a given peer disconnects.
*
* @param cls closure
* @param peer peer identity this notification is about
*/
static void
handler_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
{
GNUNET_assert (stat_connection_count > 0);
stat_connection_count--;
GNUNET_STATISTICS_update (stats, gettext_noop ("# active connections"), -1,
GNUNET_NO);
}
/**
* Method called whenever an advertisement message arrives.
*
* @param cls closure (always NULL)
* @param peer the peer sending the message
* @param message the actual message
* @param atsi performance data
* @param atsi_count number of records in 'atsi'
* @return GNUNET_OK to keep the connection open,
* GNUNET_SYSERR to close it (signal serious error)
*/
static int
handler_advertisement (void *cls, const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_MessageHeader *message,
const struct GNUNET_ATS_Information *atsi,
unsigned int atsi_count)
{
size_t size;
size_t uri_size;
const struct GNUNET_MessageHeader *incoming;
const char *uri;
struct Hostlist *hostlist;
GNUNET_assert (ntohs (message->type) ==
GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
size = ntohs (message->size);
if (size <= sizeof (struct GNUNET_MessageHeader))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
incoming = (const struct GNUNET_MessageHeader *) message;
uri = (const char *) &incoming[1];
uri_size = size - sizeof (struct GNUNET_MessageHeader);
if (uri[uri_size - 1] != '\0')
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Hostlist client recieved advertisement from `%s' containing URI `%s'\n",
GNUNET_i2s (peer), uri);
if (GNUNET_NO != linked_list_contains (uri))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "URI `%s' is already known\n", uri);
return GNUNET_OK;
}
if (GNUNET_NO == stat_testing_allowed)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Currently not accepting new advertisements: interval between to advertisements is not reached\n");
return GNUNET_SYSERR;
}
if (GNUNET_YES == stat_testing_hostlist)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Currently not accepting new advertisements: we are already testing a hostlist\n");
return GNUNET_SYSERR;
}
hostlist = GNUNET_malloc (sizeof (struct Hostlist) + uri_size);
hostlist->hostlist_uri = (const char *) &hostlist[1];
memcpy (&hostlist[1], uri, uri_size);
hostlist->time_creation = GNUNET_TIME_absolute_get ();
hostlist->quality = HOSTLIST_INITIAL;
hostlist_to_test = hostlist;
stat_testing_hostlist = GNUNET_YES;
stat_testing_allowed = GNUNET_NO;
ti_testing_intervall_task =
GNUNET_SCHEDULER_add_delayed (TESTING_INTERVAL,
&task_testing_intervall_reset, NULL);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Testing new hostlist advertisements is locked for the next %u ms\n",
TESTING_INTERVAL.rel_value);
ti_download_dispatcher_task =
GNUNET_SCHEDULER_add_now (&task_download_dispatcher, NULL);
return GNUNET_OK;
}
/**
* Continuation called by the statistics code once
* we go the stat. Initiates hostlist download scheduling.
*
* @param cls closure
* @param success GNUNET_OK if statistics were
* successfully obtained, GNUNET_SYSERR if not.
*/
static void
primary_task (void *cls, int success)
{
sget = NULL;
GNUNET_assert (stats != NULL);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Statistics request done, scheduling hostlist download\n");
ti_check_download = GNUNET_SCHEDULER_add_now (&task_check, NULL);
}
static int
process_stat (void *cls, const char *subsystem, const char *name,
uint64_t value, int is_persistent)
{
hostlist_delay.rel_value = value;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Initial time between hostlist downloads is %s\n",
GNUNET_STRINGS_relative_time_to_string (hostlist_delay, GNUNET_YES));
return GNUNET_OK;
}
/**
* Method to load persistent hostlist file during hostlist client startup
*/
static void
load_hostlist_file ()
{
char *filename;
char *uri;
char *emsg;
struct Hostlist *hostlist;
uri = NULL;
uint32_t times_used;
uint32_t hellos_returned;
uint64_t quality;
uint64_t last_used;
uint64_t created;
uint32_t counter;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_filename (cfg, "HOSTLIST", "HOSTLISTFILE",
&filename))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
"hostlist", "HOSTLISTFILE");
return;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Loading saved hostlist entries from file `%s' \n"), filename);
if (GNUNET_NO == GNUNET_DISK_file_test (filename))
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Hostlist file `%s' does not exist\n"), filename);
GNUNET_free (filename);
return;
}
struct GNUNET_BIO_ReadHandle *rh = GNUNET_BIO_read_open (filename);
if (NULL == rh)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_
("Could not open file `%s' for reading to load hostlists: %s\n"),
filename, STRERROR (errno));
GNUNET_free (filename);
return;
}
counter = 0;
while ((GNUNET_OK == GNUNET_BIO_read_string (rh, "url", &uri, MAX_URL_LEN)) &&
(NULL != uri) && (GNUNET_OK == GNUNET_BIO_read_int32 (rh, ×_used))
&& (GNUNET_OK == GNUNET_BIO_read_int64 (rh, &quality)) &&
(GNUNET_OK == GNUNET_BIO_read_int64 (rh, &last_used)) &&
(GNUNET_OK == GNUNET_BIO_read_int64 (rh, &created)) &&
(GNUNET_OK == GNUNET_BIO_read_int32 (rh, &hellos_returned)))
{
hostlist = GNUNET_malloc (sizeof (struct Hostlist) + strlen (uri) + 1);
hostlist->hello_count = hellos_returned;
hostlist->hostlist_uri = (const char *) &hostlist[1];
memcpy (&hostlist[1], uri, strlen (uri) + 1);
hostlist->quality = quality;
hostlist->time_creation.abs_value = created;
hostlist->time_last_usage.abs_value = last_used;
GNUNET_CONTAINER_DLL_insert (linked_list_head, linked_list_tail, hostlist);
linked_list_size++;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Added hostlist entry eith URI `%s' \n",
hostlist->hostlist_uri);
GNUNET_free (uri);
uri = NULL;
counter++;
if (counter >= MAX_NUMBER_HOSTLISTS)
break;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("%u hostlist URIs loaded from file\n"),
counter);
GNUNET_STATISTICS_set (stats, gettext_noop ("# hostlist URIs read from file"),
counter, GNUNET_YES);
GNUNET_STATISTICS_set (stats, gettext_noop ("# advertised hostlist URIs"),
linked_list_size, GNUNET_NO);
GNUNET_free_non_null (uri);
emsg = NULL;
(void) GNUNET_BIO_read_close (rh, &emsg);
if (emsg != NULL)
GNUNET_free (emsg);
GNUNET_free (filename);
}
/**
* Method to save persistent hostlist file during hostlist client shutdown
* @param shutdown set if called because of shutdown, entries in linked list will be destroyed
*/
static void
save_hostlist_file (int shutdown)
{
char *filename;
struct Hostlist *pos;
struct GNUNET_BIO_WriteHandle *wh;
int ok;
uint32_t counter;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_filename (cfg, "HOSTLIST", "HOSTLISTFILE",
&filename))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
"hostlist", "HOSTLISTFILE");
return;
}
if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
{
GNUNET_free (filename);
return;
}
wh = GNUNET_BIO_write_open (filename);
if (NULL == wh)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
_
("Could not open file `%s' for writing to save hostlists: %s\n"),
filename, STRERROR (errno));
GNUNET_free (filename);
return;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Writing %u hostlist URIs to `%s'\n"),
linked_list_size, filename);
/* add code to write hostlists to file using bio */
ok = GNUNET_YES;
counter = 0;
while (NULL != (pos = linked_list_head))
{
if (GNUNET_YES == shutdown)
{
GNUNET_CONTAINER_DLL_remove (linked_list_head, linked_list_tail, pos);
linked_list_size--;
}
if (GNUNET_YES == ok)
{
if ((GNUNET_OK != GNUNET_BIO_write_string (wh, pos->hostlist_uri)) ||
(GNUNET_OK != GNUNET_BIO_write_int32 (wh, pos->times_used)) ||
(GNUNET_OK != GNUNET_BIO_write_int64 (wh, pos->quality)) ||
(GNUNET_OK !=
GNUNET_BIO_write_int64 (wh, pos->time_last_usage.abs_value)) ||
(GNUNET_OK !=
GNUNET_BIO_write_int64 (wh, pos->time_creation.abs_value)) ||
(GNUNET_OK != GNUNET_BIO_write_int32 (wh, pos->hello_count)))
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
_("Error writing hostlist URIs to file `%s'\n"), filename);
ok = GNUNET_NO;
}
}
if (GNUNET_YES == shutdown)
GNUNET_free (pos);
counter++;
if (counter >= MAX_NUMBER_HOSTLISTS)
break;
}
GNUNET_STATISTICS_set (stats,
gettext_noop ("# hostlist URIs written to file"),
counter, GNUNET_YES);
if (GNUNET_OK != GNUNET_BIO_write_close (wh))
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
_("Error writing hostlist URIs to file `%s'\n"), filename);
GNUNET_free (filename);
}
/**
* Start downloading hostlists from hostlist servers as necessary.
*/
int
GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
struct GNUNET_STATISTICS_Handle *st,
GNUNET_CORE_ConnectEventHandler *ch,
GNUNET_CORE_DisconnectEventHandler *dh,
GNUNET_CORE_MessageCallback *msgh, int learn)
{
char *filename;
int result;
GNUNET_assert (NULL != st);
if (0 != curl_global_init (CURL_GLOBAL_WIN32))
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
transport = GNUNET_TRANSPORT_connect (c, NULL, NULL, NULL, NULL, NULL);
if (NULL == transport)
{
curl_global_cleanup ();
return GNUNET_SYSERR;
}
cfg = c;
stats = st;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST", "HTTP-PROXY",
&proxy))
proxy = NULL;
stat_learning = learn;
*ch = &handler_connect;
*dh = &handler_disconnect;
linked_list_head = NULL;
linked_list_tail = NULL;
stat_use_bootstrap = GNUNET_YES;
stat_testing_hostlist = GNUNET_NO;
stat_testing_allowed = GNUNET_YES;
if (GNUNET_YES == stat_learning)
{
*msgh = &handler_advertisement;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Learning is enabled on this peer\n"));
load_hostlist_file ();
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Hostlists will be saved to file again in %s\n",
GNUNET_STRINGS_relative_time_to_string (SAVING_INTERVALL, GNUNET_YES));
ti_saving_task =
GNUNET_SCHEDULER_add_delayed (SAVING_INTERVALL, &task_hostlist_saving,
NULL);
}
else
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Learning is not enabled on this peer\n"));
*msgh = NULL;
if (GNUNET_OK ==
GNUNET_CONFIGURATION_get_value_filename (cfg, "HOSTLIST",
"HOSTLISTFILE", &filename))
{
if (GNUNET_YES == GNUNET_DISK_file_test (filename))
{
result = remove (filename);
if (result == 0)
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_
("Since learning is not enabled on this peer, hostlist file `%s' was removed\n"),
filename);
else
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Hostlist file `%s' could not be removed\n"), filename);
}
}
GNUNET_free (filename);
}
sget = GNUNET_STATISTICS_get (stats, "hostlist",
gettext_noop
("# milliseconds between hostlist downloads"),
GNUNET_TIME_UNIT_MINUTES, &primary_task, &process_stat,
NULL);
return GNUNET_OK;
}
/**
* Stop downloading hostlists from hostlist servers as necessary.
*/
void
GNUNET_HOSTLIST_client_stop ()
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist client shutdown\n");
if (NULL != sget)
{
GNUNET_STATISTICS_get_cancel (sget);
sget = NULL;
}
stats = NULL;
if (GNUNET_YES == stat_learning)
save_hostlist_file (GNUNET_YES);
if (ti_saving_task != GNUNET_SCHEDULER_NO_TASK)
{
GNUNET_SCHEDULER_cancel (ti_saving_task);
ti_saving_task = GNUNET_SCHEDULER_NO_TASK;
}
if (ti_download_dispatcher_task != GNUNET_SCHEDULER_NO_TASK)
{
GNUNET_SCHEDULER_cancel (ti_download_dispatcher_task);
ti_download_dispatcher_task = GNUNET_SCHEDULER_NO_TASK;
}
if (ti_testing_intervall_task != GNUNET_SCHEDULER_NO_TASK)
{
GNUNET_SCHEDULER_cancel (ti_testing_intervall_task);
ti_testing_intervall_task = GNUNET_SCHEDULER_NO_TASK;
}
if (ti_download != GNUNET_SCHEDULER_NO_TASK)
{
GNUNET_SCHEDULER_cancel (ti_download);
ti_download = GNUNET_SCHEDULER_NO_TASK;
}
if (ti_check_download != GNUNET_SCHEDULER_NO_TASK)
{
GNUNET_SCHEDULER_cancel (ti_check_download);
ti_check_download = GNUNET_SCHEDULER_NO_TASK;
curl_global_cleanup ();
}
if (transport != NULL)
{
GNUNET_TRANSPORT_disconnect (transport);
transport = NULL;
}
GNUNET_free_non_null (proxy);
proxy = NULL;
cfg = NULL;
}
/* end of hostlist-client.c */
|