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
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
|
/*
This file is part of GNUnet
Copyright (C) 2018 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/**
* @file src/namestore/gnunet-zoneimport.c
* @brief import a DNS zone for publication in GNS, incremental
* @author Christian Grothoff
*/
#include "platform.h"
#include <gnunet_util_lib.h>
#include <gnunet_dnsstub_lib.h>
#include <gnunet_dnsparser_lib.h>
#include <gnunet_gnsrecord_lib.h>
#include <gnunet_namestore_service.h>
#include <gnunet_statistics_service.h>
#include <gnunet_identity_service.h>
/**
* Maximum number of queries pending at the same time.
*/
#define THRESH 100
/**
* TIME_THRESH is in usecs. How quickly do we submit fresh queries.
* Used as an additional throttle.
*/
#define TIME_THRESH 10
/**
* How often do we retry a query before giving up for good?
*/
#define MAX_RETRIES 5
/**
* How many DNS requests do we at most issue in rapid series?
*/
#define MAX_SERIES 10
/**
* How long do we wait at least between series of requests?
*/
#define SERIES_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, 10)
/**
* How many requests do we request from NAMESTORE in one batch
* during our initial iteration?
*/
#define NS_BATCH_SIZE 1024
/**
* Some zones may include authoritative records for other
* zones, such as foo.com.uk or bar.com.fr. As for GNS
* each dot represents a zone cut, we then need to create a
* zone on-the-fly to capture those records properly.
*/
struct Zone
{
/**
* Kept in a DLL.
*/
struct Zone *next;
/**
* Kept in a DLL.
*/
struct Zone *prev;
/**
* Domain of the zone (i.e. "fr" or "com.fr")
*/
char *domain;
/**
* Private key of the zone.
*/
struct GNUNET_CRYPTO_EcdsaPrivateKey key;
};
/**
* Record for the request to be stored by GNS.
*/
struct Record
{
/**
* Kept in a DLL.
*/
struct Record *next;
/**
* Kept in a DLL.
*/
struct Record *prev;
/**
* GNS record.
*/
struct GNUNET_GNSRECORD_Data grd;
};
/**
* Request we should make. We keep this struct in memory per request,
* thus optimizing it is crucial for the overall memory consumption of
* the zone importer.
*/
struct Request
{
/**
* Requests are kept in a heap while waiting to be resolved.
*/
struct GNUNET_CONTAINER_HeapNode *hn;
/**
* Active requests are kept in a DLL.
*/
struct Request *next;
/**
* Active requests are kept in a DLL.
*/
struct Request *prev;
/**
* Head of records that should be published in GNS for
* this hostname.
*/
struct Record *rec_head;
/**
* Tail of records that should be published in GNS for
* this hostname.
*/
struct Record *rec_tail;
/**
* Socket used to make the request, NULL if not active.
*/
struct GNUNET_DNSSTUB_RequestSocket *rs;
/**
* Hostname we are resolving, allocated at the end of
* this struct (optimizing memory consumption by reducing
* total number of allocations).
*/
char *hostname;
/**
* Namestore operation pending for this record.
*/
struct GNUNET_NAMESTORE_QueueEntry *qe;
/**
* Zone responsible for this request.
*/
const struct Zone *zone;
/**
* At what time does the (earliest) of the returned records
* for this name expire? At this point, we need to re-fetch
* the record.
*/
struct GNUNET_TIME_Absolute expires;
/**
* While we are fetching the record, the value is set to the
* starting time of the DNS operation. While doing a
* NAMESTORE store, again set to the start time of the
* NAMESTORE operation.
*/
struct GNUNET_TIME_Absolute op_start_time;
/**
* How often did we issue this query? (And failed, reset
* to zero once we were successful.)
*/
unsigned int issue_num;
/**
* random 16-bit DNS query identifier.
*/
uint16_t id;
};
/**
* Handle to the identity service.
*/
static struct GNUNET_IDENTITY_Handle *id;
/**
* Namestore handle.
*/
static struct GNUNET_NAMESTORE_Handle *ns;
/**
* Handle to the statistics service.
*/
static struct GNUNET_STATISTICS_Handle *stats;
/**
* Context for DNS resolution.
*/
static struct GNUNET_DNSSTUB_Context *ctx;
/**
* The number of DNS queries that are outstanding
*/
static unsigned int pending;
/**
* The number of NAMESTORE record store operations that are outstanding
*/
static unsigned int pending_rs;
/**
* Number of lookups we performed overall.
*/
static unsigned int lookups;
/**
* Number of records we had cached.
*/
static unsigned int cached;
/**
* How many hostnames did we reject (malformed).
*/
static unsigned int rejects;
/**
* Number of lookups that failed.
*/
static unsigned int failures;
/**
* Number of records we found.
*/
static unsigned int records;
/**
* Number of record sets given to namestore.
*/
static unsigned int record_sets;
/**
* Heap of all requests to perform, sorted by
* the time we should next do the request (i.e. by expires).
*/
static struct GNUNET_CONTAINER_Heap *req_heap;
/**
* Active requests are kept in a DLL.
*/
static struct Request *req_head;
/**
* Active requests are kept in a DLL.
*/
static struct Request *req_tail;
/**
* Main task.
*/
static struct GNUNET_SCHEDULER_Task *t;
/**
* Hash map of requests for which we may still get a response from
* the namestore. Set to NULL once the initial namestore iteration
* is done.
*/
static struct GNUNET_CONTAINER_MultiHashMap *ns_pending;
/**
* Current zone iteration handle.
*/
static struct GNUNET_NAMESTORE_ZoneIterator *zone_it;
/**
* Head of list of zones we are managing.
*/
static struct Zone *zone_head;
/**
* Tail of list of zones we are managing.
*/
static struct Zone *zone_tail;
/**
* After how many more results must #ns_lookup_result_cb() ask
* the namestore for more?
*/
static uint64_t ns_iterator_trigger_next;
/**
* Number of DNS requests counted in latency total.
*/
static uint64_t total_dns_latency_cnt;
/**
* Sum of DNS latencies observed.
*/
static struct GNUNET_TIME_Relative total_dns_latency;
/**
* Number of NAMESTORE requests counted in latency total.
*/
static uint64_t total_ns_latency_cnt;
/**
* Sum of NAMESTORE latencies observed.
*/
static struct GNUNET_TIME_Relative total_ns_latency;
/**
* Callback for #for_all_records
*
* @param cls closure
* @param rec a DNS record
*/
typedef void
(*RecordProcessor) (void *cls,
const struct GNUNET_DNSPARSER_Record *rec);
/**
* Call @a rp for each record in @a p, regardless of
* what response section it is in.
*
* @param p packet from DNS
* @param rp function to call
* @param rp_cls closure for @a rp
*/
static void
for_all_records (const struct GNUNET_DNSPARSER_Packet *p,
RecordProcessor rp,
void *rp_cls)
{
for (unsigned int i=0;i<p->num_answers;i++)
{
struct GNUNET_DNSPARSER_Record *rs = &p->answers[i];
rp (rp_cls,
rs);
}
for (unsigned int i=0;i<p->num_authority_records;i++)
{
struct GNUNET_DNSPARSER_Record *rs = &p->authority_records[i];
rp (rp_cls,
rs);
}
for (unsigned int i=0;i<p->num_additional_records;i++)
{
struct GNUNET_DNSPARSER_Record *rs = &p->additional_records[i];
rp (rp_cls,
rs);
}
}
/**
* Return just the label of the hostname in @a req.
*
* @param req request to process hostname of
* @return statically allocated pointer to the label,
* overwritten upon the next request!
*/
static const char *
get_label (struct Request *req)
{
static char label[64];
const char *dot;
dot = strchr (req->hostname,
(unsigned char) '.');
if (NULL == dot)
{
GNUNET_break (0);
return NULL;
}
if (((size_t) (dot - req->hostname)) >= sizeof (label))
{
GNUNET_break (0);
return NULL;
}
memcpy (label,
req->hostname,
dot - req->hostname);
label[dot - req->hostname] = '\0';
return label;
}
/**
* Build DNS query for @a hostname.
*
* @param hostname host to build query for
* @param raw_size[out] number of bytes in the query
* @return NULL on error, otherwise pointer to statically (!)
* allocated query buffer
*/
static void *
build_dns_query (struct Request *req,
size_t *raw_size)
{
static char raw[512];
char *rawp;
struct GNUNET_DNSPARSER_Packet p;
struct GNUNET_DNSPARSER_Query q;
q.name = (char *) req->hostname;
q.type = GNUNET_DNSPARSER_TYPE_NS;
q.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
memset (&p,
0,
sizeof (p));
p.num_queries = 1;
p.queries = &q;
p.id = req->id;
if (GNUNET_OK !=
GNUNET_DNSPARSER_pack (&p,
UINT16_MAX,
&rawp,
raw_size))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to pack query for hostname `%s'\n",
req->hostname);
rejects++;
return NULL;
}
if (*raw_size > sizeof (raw))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to pack query for hostname `%s'\n",
req->hostname);
rejects++;
GNUNET_break (0);
GNUNET_free (rawp);
return NULL;
}
memcpy (raw,
rawp,
*raw_size);
GNUNET_free (rawp);
return raw;
}
/**
* Free records associated with @a req.
*
* @param req request to free records of
*/
static void
free_records (struct Request *req)
{
struct Record *rec;
/* Free records */
while (NULL != (rec = req->rec_head))
{
GNUNET_CONTAINER_DLL_remove (req->rec_head,
req->rec_tail,
rec);
GNUNET_free (rec);
}
}
/**
* Free @a req and data structures reachable from it.
*
* @param req request to free
*/
static void
free_request (struct Request *req)
{
free_records (req);
GNUNET_free (req);
}
/**
* Process as many requests as possible from the queue.
*
* @param cls NULL
*/
static void
process_queue (void *cls);
/**
* Insert @a req into DLL sorted by next fetch time.
*
* @param req request to insert into #req_heap
*/
static void
insert_sorted (struct Request *req)
{
req->hn = GNUNET_CONTAINER_heap_insert (req_heap,
req,
req->expires.abs_value_us);
if (req == GNUNET_CONTAINER_heap_peek (req_heap))
{
if (NULL != t)
GNUNET_SCHEDULER_cancel (t);
t = GNUNET_SCHEDULER_add_at (req->expires,
&process_queue,
NULL);
}
}
/**
* Add record to the GNS record set for @a req.
*
* @param req the request to expand GNS record set for
* @param type type to use
* @param expiration_time when should @a rec expire
* @param data raw data to store
* @param data_len number of bytes in @a data
*/
static void
add_record (struct Request *req,
uint32_t type,
struct GNUNET_TIME_Absolute expiration_time,
const void *data,
size_t data_len)
{
struct Record *rec;
rec = GNUNET_malloc (sizeof (struct Record) + data_len);
rec->grd.data = &rec[1];
rec->grd.expiration_time = expiration_time.abs_value_us;
rec->grd.data_size = data_len;
rec->grd.record_type = type;
rec->grd.flags = GNUNET_GNSRECORD_RF_NONE;
GNUNET_memcpy (&rec[1],
data,
data_len);
GNUNET_CONTAINER_DLL_insert (req->rec_head,
req->rec_tail,
rec);
}
/**
* Closure for #check_for_glue.
*/
struct GlueClosure
{
/**
* Overall request we are processing.
*/
struct Request *req;
/**
* NS name we are looking for glue for.
*/
const char *ns;
/**
* Set to #GNUNET_YES if glue was found.
*/
int found;
};
/**
* Try to find glue records for a given NS record.
*
* @param cls a `struct GlueClosure *`
* @param rec record that may contain glue information
*/
static void
check_for_glue (void *cls,
const struct GNUNET_DNSPARSER_Record *rec)
{
struct GlueClosure *gc = cls;
char dst[65536];
size_t dst_len;
size_t off;
char ip[INET6_ADDRSTRLEN+1];
socklen_t ip_size = (socklen_t) sizeof (ip);
if (0 != strcasecmp (rec->name,
gc->ns))
return;
dst_len = sizeof (dst);
off = 0;
switch (rec->type)
{
case GNUNET_DNSPARSER_TYPE_A:
if (sizeof (struct in_addr) != rec->data.raw.data_len)
{
GNUNET_break (0);
return;
}
if (NULL ==
inet_ntop (AF_INET,
rec->data.raw.data,
ip,
ip_size))
{
GNUNET_break (0);
return;
}
if ( (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
gc->req->hostname)) &&
(GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
ip)) )
{
add_record (gc->req,
GNUNET_GNSRECORD_TYPE_GNS2DNS,
rec->expiration_time,
dst,
off);
gc->found = GNUNET_YES;
}
break;
case GNUNET_DNSPARSER_TYPE_AAAA:
if (sizeof (struct in6_addr) != rec->data.raw.data_len)
{
GNUNET_break (0);
return;
}
if (NULL ==
inet_ntop (AF_INET6,
rec->data.raw.data,
ip,
ip_size))
{
GNUNET_break (0);
return;
}
if ( (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
gc->req->hostname)) &&
(GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
ip)) )
{
add_record (gc->req,
GNUNET_GNSRECORD_TYPE_GNS2DNS,
rec->expiration_time,
dst,
off);
gc->found = GNUNET_YES;
}
break;
case GNUNET_DNSPARSER_TYPE_CNAME:
if ( (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
gc->req->hostname)) &&
(GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
rec->data.hostname)) )
{
add_record (gc->req,
GNUNET_GNSRECORD_TYPE_GNS2DNS,
rec->expiration_time,
dst,
off);
gc->found = GNUNET_YES;
}
break;
default:
/* useless, do nothing */
break;
}
}
/**
* Closure for #process_record().
*/
struct ProcessRecordContext
{
/**
* Answer we got back and are currently parsing, or NULL
* if not active.
*/
struct GNUNET_DNSPARSER_Packet *p;
/**
* Request we are processing.
*/
struct Request *req;
};
/**
* We received @a rec for @a req. Remember the answer.
*
* @param cls a `struct ProcessRecordContext`
* @param rec response
*/
static void
process_record (void *cls,
const struct GNUNET_DNSPARSER_Record *rec)
{
struct ProcessRecordContext *prc = cls;
struct Request *req = prc->req;
char dst[65536];
size_t dst_len;
size_t off;
dst_len = sizeof (dst);
off = 0;
records++;
if (0 != strcasecmp (rec->name,
req->hostname))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"DNS returned record from zone `%s' of type %u while resolving `%s'\n",
rec->name,
(unsigned int) rec->type,
req->hostname);
return; /* does not match hostname, might be glue, but
not useful for this pass! */
}
if (0 ==
GNUNET_TIME_absolute_get_remaining (rec->expiration_time).rel_value_us)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"DNS returned expired record for `%s'\n",
req->hostname);
return; /* record expired */
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"DNS returned record that expires at %s for `%s'\n",
GNUNET_STRINGS_absolute_time_to_string (rec->expiration_time),
req->hostname);
switch (rec->type)
{
case GNUNET_DNSPARSER_TYPE_NS:
{
struct GlueClosure gc;
/* check for glue */
gc.req = req;
gc.ns = rec->data.hostname;
gc.found = GNUNET_NO;
for_all_records (prc->p,
&check_for_glue,
&gc);
if ( (GNUNET_NO == gc.found) &&
(GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
req->hostname)) &&
(GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
rec->data.hostname)) )
{
/* FIXME: actually check if this is out-of-bailiwick,
and if not request explicit resolution... */
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converted OOB (`%s') NS record for `%s'\n",
rec->data.hostname,
rec->name);
add_record (req,
GNUNET_GNSRECORD_TYPE_GNS2DNS,
rec->expiration_time,
dst,
off);
}
else
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converted NS record for `%s' using glue\n",
rec->name);
}
break;
}
case GNUNET_DNSPARSER_TYPE_CNAME:
if (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
rec->data.hostname))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converting CNAME (`%s') record for `%s'\n",
rec->data.hostname,
rec->name);
add_record (req,
rec->type,
rec->expiration_time,
dst,
off);
}
break;
case GNUNET_DNSPARSER_TYPE_DNAME:
/* No support for DNAME in GNS yet! FIXME: support later! */
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"FIXME: not supported: %s DNAME %s\n",
rec->name,
rec->data.hostname);
break;
case GNUNET_DNSPARSER_TYPE_MX:
if (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_mx (dst,
dst_len,
&off,
rec->data.mx))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converting MX (`%s') record for `%s'\n",
rec->data.mx->mxhost,
rec->name);
add_record (req,
rec->type,
rec->expiration_time,
dst,
off);
}
break;
case GNUNET_DNSPARSER_TYPE_SOA:
if (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_soa (dst,
dst_len,
&off,
rec->data.soa))
{
/* NOTE: GNS does not really use SOAs */
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converting SOA record for `%s'\n",
rec->name);
add_record (req,
rec->type,
rec->expiration_time,
dst,
off);
}
break;
case GNUNET_DNSPARSER_TYPE_SRV:
if (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_srv (dst,
dst_len,
&off,
rec->data.srv))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converting SRV record for `%s'\n",
rec->name);
add_record (req,
rec->type,
rec->expiration_time,
dst,
off);
}
break;
case GNUNET_DNSPARSER_TYPE_PTR:
if (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
&off,
rec->data.hostname))
{
/* !?: what does a PTR record do in a regular TLD??? */
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converting PTR record for `%s' (weird)\n",
rec->name);
add_record (req,
rec->type,
rec->expiration_time,
dst,
off);
}
break;
case GNUNET_DNSPARSER_TYPE_CERT:
if (GNUNET_OK ==
GNUNET_DNSPARSER_builder_add_cert (dst,
dst_len,
&off,
rec->data.cert))
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converting CERT record for `%s'\n",
rec->name);
add_record (req,
rec->type,
rec->expiration_time,
dst,
off);
}
break;
/* Rest is 'raw' encoded and just needs to be copied IF
the hostname matches the requested name; otherwise we
simply cannot use it. */
case GNUNET_DNSPARSER_TYPE_A:
case GNUNET_DNSPARSER_TYPE_AAAA:
case GNUNET_DNSPARSER_TYPE_TXT:
default:
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Converting record of type %u for `%s'\n",
(unsigned int) rec->type,
rec->name);
add_record (req,
rec->type,
rec->expiration_time,
rec->data.raw.data,
rec->data.raw.data_len);
break;
}
}
/**
* Continuation called to notify client about result of the
* operation.
*
* @param cls closure with our `struct Request`
* @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
* #GNUNET_NO if content was already there or not found
* #GNUNET_YES (or other positive value) on success
* @param emsg NULL on success, otherwise an error message
*/
static void
store_completed_cb (void *cls,
int32_t success,
const char *emsg)
{
static struct GNUNET_TIME_Absolute last;
static unsigned int pdot;
struct Request *req = cls;
req->qe = NULL;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Stored record set in database (%d)\n",
success);
pending_rs--;
{
struct GNUNET_TIME_Relative ns_latency;
ns_latency = GNUNET_TIME_absolute_get_duration (req->op_start_time);
total_ns_latency = GNUNET_TIME_relative_add (total_ns_latency,
ns_latency);
total_ns_latency_cnt++;
if (0 == (total_ns_latency_cnt % 1000))
{
GNUNET_STATISTICS_update (stats,
"# average NAMESTORE PUT latency (μs)",
total_ns_latency.rel_value_us / total_ns_latency_cnt,
GNUNET_NO);
GNUNET_STATISTICS_update (stats,
"# NAMESTORE PUTs",
total_ns_latency_cnt,
GNUNET_NO);
}
}
if (NULL == t)
t = GNUNET_SCHEDULER_add_now (&process_queue,
NULL);
if (GNUNET_SYSERR == success)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to store zone data for `%s': %s\n",
req->hostname,
emsg);
}
else
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Stored records under `%s'\n",
req->hostname);
if (0 == pdot)
last = GNUNET_TIME_absolute_get ();
pdot++;
if (0 == pdot % 1000)
{
struct GNUNET_TIME_Relative delta;
delta = GNUNET_TIME_absolute_get_duration (last);
last = GNUNET_TIME_absolute_get ();
fprintf (stderr,
"Processed 1000 records in %s\n",
GNUNET_STRINGS_relative_time_to_string (delta,
GNUNET_YES));
}
}
free_records (req);
}
/**
* Function called with the result of a DNS resolution.
*
* @param cls closure with the `struct Request`
* @param dns dns response, never NULL
* @param dns_len number of bytes in @a dns
*/
static void
process_result (void *cls,
const struct GNUNET_TUN_DnsHeader *dns,
size_t dns_len)
{
struct Request *req = cls;
struct Record *rec;
struct GNUNET_DNSPARSER_Packet *p;
unsigned int rd_count;
GNUNET_assert (NULL == req->hn);
if (NULL == dns)
{
/* stub gave up */
GNUNET_CONTAINER_DLL_remove (req_head,
req_tail,
req);
pending--;
if (NULL == t)
t = GNUNET_SCHEDULER_add_now (&process_queue,
NULL);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Stub gave up on DNS reply for `%s'\n",
req->hostname);
GNUNET_STATISTICS_update (stats,
"# DNS lookups timed out",
1,
GNUNET_NO);
if (req->issue_num > MAX_RETRIES)
{
failures++;
free_request (req);
GNUNET_STATISTICS_update (stats,
"# requests given up on",
1,
GNUNET_NO);
return;
}
req->rs = NULL;
insert_sorted (req);
return;
}
if (req->id != dns->id)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"DNS ID did not match request, ignoring reply\n");
GNUNET_STATISTICS_update (stats,
"# DNS ID missmatches",
1,
GNUNET_NO);
return;
}
GNUNET_CONTAINER_DLL_remove (req_head,
req_tail,
req);
GNUNET_DNSSTUB_resolve_cancel (req->rs);
req->rs = NULL;
pending--;
p = GNUNET_DNSPARSER_parse ((const char *) dns,
dns_len);
if (NULL == p)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to parse DNS reply for `%s'\n",
req->hostname);
GNUNET_STATISTICS_update (stats,
"# DNS parser errors",
1,
GNUNET_NO);
if (NULL == t)
t = GNUNET_SCHEDULER_add_now (&process_queue,
NULL);
if (req->issue_num > MAX_RETRIES)
{
failures++;
free_request (req);
GNUNET_STATISTICS_update (stats,
"# requests given up on",
1,
GNUNET_NO);
return;
}
insert_sorted (req);
return;
}
/* import new records */
req->issue_num = 0; /* success, reset counter! */
{
struct ProcessRecordContext prc = {
.req = req,
.p = p
};
for_all_records (p,
&process_record,
&prc);
}
GNUNET_DNSPARSER_free_packet (p);
/* count records found, determine minimum expiration time */
req->expires = GNUNET_TIME_UNIT_FOREVER_ABS;
{
struct GNUNET_TIME_Relative dns_latency;
dns_latency = GNUNET_TIME_absolute_get_duration (req->op_start_time);
total_dns_latency = GNUNET_TIME_relative_add (total_dns_latency,
dns_latency);
total_dns_latency_cnt++;
if (0 == (total_dns_latency_cnt % 1000))
{
GNUNET_STATISTICS_update (stats,
"# average DNS latency (μs)",
total_dns_latency.rel_value_us / total_dns_latency_cnt,
GNUNET_NO);
GNUNET_STATISTICS_update (stats,
"# DNS replies",
total_dns_latency_cnt,
GNUNET_NO);
}
}
rd_count = 0;
for (rec = req->rec_head; NULL != rec; rec = rec->next)
{
struct GNUNET_TIME_Absolute at;
at.abs_value_us = rec->grd.expiration_time;
req->expires = GNUNET_TIME_absolute_min (req->expires,
at);
rd_count++;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Obtained %u records for `%s'\n",
rd_count,
req->hostname);
/* Instead of going for SOA, simplified for now to look each
day in case we got an empty response */
if (0 == rd_count)
req->expires
= GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_DAYS);
else
record_sets++;
/* convert records to namestore import format */
{
struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
unsigned int off = 0;
/* convert linked list into array */
for (rec = req->rec_head; NULL != rec; rec =rec->next)
rd[off++] = rec->grd;
pending_rs++;
req->op_start_time = GNUNET_TIME_absolute_get ();
req->qe = GNUNET_NAMESTORE_records_store (ns,
&req->zone->key,
get_label (req),
rd_count,
rd,
&store_completed_cb,
req);
GNUNET_assert (NULL != req->qe);
}
insert_sorted (req);
}
/**
* Process as many requests as possible from the queue.
*
* @param cls NULL
*/
static void
process_queue (void *cls)
{
struct Request *req;
unsigned int series;
void *raw;
size_t raw_size;
(void) cls;
series = 0;
t = NULL;
while (pending + pending_rs < THRESH)
{
req = GNUNET_CONTAINER_heap_peek (req_heap);
if (NULL == req)
break;
if (NULL != req->qe)
return; /* namestore op still pending */
if (NULL != req->rs)
{
GNUNET_break (0);
return; /* already submitted */
}
if (GNUNET_TIME_absolute_get_remaining (req->expires).rel_value_us > 0)
break;
GNUNET_assert (req ==
GNUNET_CONTAINER_heap_remove_root (req_heap));
req->hn = NULL;
GNUNET_CONTAINER_DLL_insert (req_head,
req_tail,
req);
GNUNET_assert (NULL == req->rs);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Requesting resolution for `%s'\n",
req->hostname);
raw = build_dns_query (req,
&raw_size);
if (NULL == raw)
{
GNUNET_break (0);
free_request (req);
continue;
}
req->op_start_time = GNUNET_TIME_absolute_get ();
req->rs = GNUNET_DNSSTUB_resolve (ctx,
raw,
raw_size,
&process_result,
req);
GNUNET_assert (NULL != req->rs);
req->issue_num++;
lookups++;
pending++;
series++;
if (series > MAX_SERIES)
break;
}
if (pending + pending_rs >= THRESH)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Stopped processing queue (%u+%u/%u)]\n",
pending,
pending_rs,
THRESH);
return; /* wait for replies */
}
req = GNUNET_CONTAINER_heap_peek (req_heap);
if (NULL == req)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Stopped processing queue: empty queue\n");
return;
}
if (GNUNET_TIME_absolute_get_remaining (req->expires).rel_value_us > 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Waiting until %s for next record (`%s') to expire\n",
GNUNET_STRINGS_absolute_time_to_string (req->expires),
req->hostname);
if (NULL != t)
GNUNET_SCHEDULER_cancel (t);
t = GNUNET_SCHEDULER_add_at (req->expires,
&process_queue,
NULL);
return;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Throttling\n");
if (NULL != t)
GNUNET_SCHEDULER_cancel (t);
t = GNUNET_SCHEDULER_add_delayed (SERIES_DELAY,
&process_queue,
NULL);
}
/**
* Iterator called during #do_shutdown() to free requests in
* the #ns_pending map.
*
* @param cls NULL
* @param key unused
* @param value the `struct Request` to free
* @return #GNUNET_OK
*/
static int
free_request_it (void *cls,
const struct GNUNET_HashCode *key,
void *value)
{
struct Request *req = value;
(void) cls;
(void) key;
free_request (req);
return GNUNET_OK;
}
/**
* Clean up and terminate the process.
*
* @param cls NULL
*/
static void
do_shutdown (void *cls)
{
struct Request *req;
struct Zone *zone;
(void) cls;
if (NULL != id)
{
GNUNET_IDENTITY_disconnect (id);
id = NULL;
}
if (NULL != t)
{
GNUNET_SCHEDULER_cancel (t);
t = NULL;
}
while (NULL != (req = req_head))
{
GNUNET_CONTAINER_DLL_remove (req_head,
req_tail,
req);
if (NULL != req->qe)
GNUNET_NAMESTORE_cancel (req->qe);
free_request (req);
}
while (NULL != (req = GNUNET_CONTAINER_heap_remove_root (req_heap)))
{
req->hn = NULL;
if (NULL != req->qe)
GNUNET_NAMESTORE_cancel (req->qe);
free_request (req);
}
if (NULL != zone_it)
{
GNUNET_NAMESTORE_zone_iteration_stop (zone_it);
zone_it = NULL;
}
if (NULL != ns)
{
GNUNET_NAMESTORE_disconnect (ns);
ns = NULL;
}
if (NULL != ctx)
{
GNUNET_DNSSTUB_stop (ctx);
ctx = NULL;
}
if (NULL != req_heap)
{
GNUNET_CONTAINER_heap_destroy (req_heap);
req_heap = NULL;
}
if (NULL != ns_pending)
{
GNUNET_CONTAINER_multihashmap_iterate (ns_pending,
&free_request_it,
NULL);
GNUNET_CONTAINER_multihashmap_destroy (ns_pending);
ns_pending = NULL;
}
while (NULL != (zone = zone_head))
{
GNUNET_CONTAINER_DLL_remove (zone_head,
zone_tail,
zone);
GNUNET_free (zone->domain);
GNUNET_free (zone);
}
if (NULL != stats)
{
GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
stats = NULL;
}
}
/**
* Function called if #GNUNET_NAMESTORE_records_lookup() failed.
* Just logs an error.
*
* @param cls a `struct Zone`
*/
static void
ns_lookup_error_cb (void *cls)
{
struct Zone *zone = cls;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Failed to load data from namestore for zone `%s'\n",
zone->domain);
}
/**
* Process a record that was stored in the namestore.
*
* @param cls a `struct Zone *`
* @param key private key of the zone
* @param label label of the records
* @param rd_count number of entries in @a rd array, 0 if label was deleted
* @param rd array of records with data to store
*/
static void
ns_lookup_result_cb (void *cls,
const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
const char *label,
unsigned int rd_count,
const struct GNUNET_GNSRECORD_Data *rd)
{
struct Zone *zone = cls;
struct Request *req;
struct GNUNET_HashCode hc;
char *fqdn;
ns_iterator_trigger_next--;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Obtained NAMESTORE reply, %llu left in round\n",
(unsigned long long) ns_iterator_trigger_next);
if (0 == ns_iterator_trigger_next)
{
ns_iterator_trigger_next = NS_BATCH_SIZE;
GNUNET_STATISTICS_update (stats,
"# NAMESTORE records requested",
ns_iterator_trigger_next,
GNUNET_NO);
GNUNET_NAMESTORE_zone_iterator_next (zone_it,
ns_iterator_trigger_next);
}
GNUNET_asprintf (&fqdn,
"%s.%s",
label,
zone->domain);
GNUNET_CRYPTO_hash (fqdn,
strlen (fqdn) + 1,
&hc);
GNUNET_free (fqdn);
req = GNUNET_CONTAINER_multihashmap_get (ns_pending,
&hc);
if (NULL == req)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Ignoring record `%s' in zone `%s': not on my list!\n",
label,
zone->domain);
return;
}
GNUNET_assert (GNUNET_OK ==
GNUNET_CONTAINER_multihashmap_remove (ns_pending,
&hc,
req));
GNUNET_break (0 == memcmp (key,
&req->zone->key,
sizeof (*key)));
GNUNET_break (0 == strcasecmp (label,
get_label (req)));
for (unsigned int i=0;i<rd_count;i++)
{
struct GNUNET_TIME_Absolute at;
at.abs_value_us = rd->expiration_time;
add_record (req,
rd->record_type,
at,
rd->data,
rd->data_size);
}
if (0 == rd_count)
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Empty record set in namestore for `%s'\n",
req->hostname);
}
else
{
unsigned int pos = 0;
cached++;
req->expires = GNUNET_TIME_UNIT_FOREVER_ABS;
for (struct Record *rec = req->rec_head;
NULL != rec;
rec = rec->next)
{
struct GNUNET_TIME_Absolute at;
at.abs_value_us = rec->grd.expiration_time;
req->expires = GNUNET_TIME_absolute_min (req->expires,
at);
pos++;
}
if (0 == pos)
req->expires = GNUNET_TIME_UNIT_ZERO_ABS;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Hot-start with %u existing records for `%s'\n",
pos,
req->hostname);
}
free_records (req);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Adding `%s' to worklist to start at %s\n",
req->hostname,
GNUNET_STRINGS_absolute_time_to_string (req->expires));
insert_sorted (req);
}
/**
* Add @a hostname to the list of requests to be made.
*
* @param hostname name to resolve
*/
static void
queue (const char *hostname)
{
struct Request *req;
const char *dot;
struct Zone *zone;
size_t hlen;
struct GNUNET_HashCode hc;
if (GNUNET_OK !=
GNUNET_DNSPARSER_check_name (hostname))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Refusing invalid hostname `%s'\n",
hostname);
rejects++;
return;
}
dot = strchr (hostname,
(unsigned char) '.');
if (NULL == dot)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Refusing invalid hostname `%s' (lacks '.')\n",
hostname);
rejects++;
return;
}
for (zone = zone_head; NULL != zone; zone = zone->next)
if (0 == strcmp (zone->domain,
dot + 1))
break;
if (NULL == zone)
{
rejects++;
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Domain name `%s' not in ego list!\n",
dot + 1);
return;
}
hlen = strlen (hostname) + 1;
req = GNUNET_malloc (sizeof (struct Request) + hlen);
req->zone = zone;
req->hostname = (char *) &req[1];
memcpy (req->hostname,
hostname,
hlen);
req->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
UINT16_MAX);
GNUNET_CRYPTO_hash (req->hostname,
hlen,
&hc);
if (GNUNET_OK !=
GNUNET_CONTAINER_multihashmap_put (ns_pending,
&hc,
req,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Duplicate hostname `%s' ignored\n",
hostname);
GNUNET_free (req);
return;
}
}
/**
* We have completed the initial iteration over the namestore's database.
* This function is called on each of the remaining records in
* #move_to_queue to #queue() them, as we will simply not find existing
* records for them any longer.
*
* @param cls NULL
* @param key unused
* @param value a `struct Request`
* @return #GNUNET_OK (continue to iterate)
*/
static int
move_to_queue (void *cls,
const struct GNUNET_HashCode *key,
void *value)
{
struct Request *req = value;
(void) cls;
(void) key;
insert_sorted (req);
return GNUNET_OK;
}
/**
* Iterate over all of the zones we care about and see which records
* we may need to re-fetch when.
*
* @param cls NULL
*/
static void
iterate_zones (void *cls)
{
static struct Zone *last;
(void) cls;
if (NULL != zone_it)
{
zone_it = NULL;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Finished iteration over zone `%s'!\n",
last->domain);
/* subtract left-overs from previous iteration */
GNUNET_STATISTICS_update (stats,
"# NAMESTORE records requested",
(long long) (- ns_iterator_trigger_next),
GNUNET_NO);
ns_iterator_trigger_next = 0;
}
GNUNET_assert (NULL != zone_tail);
if (zone_tail == last)
{
/* Done iterating over relevant zones in NAMESTORE, move
rest of hash map to work queue as well. */
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Finished all NAMESTORE iterations!\n");
GNUNET_STATISTICS_set (stats,
"# NAMESTORE lookups without reply",
GNUNET_CONTAINER_multihashmap_size (ns_pending),
GNUNET_NO);
GNUNET_CONTAINER_multihashmap_iterate (ns_pending,
&move_to_queue,
NULL);
GNUNET_CONTAINER_multihashmap_destroy (ns_pending);
ns_pending = NULL;
return;
}
if (NULL == last)
last = zone_head;
else
last = last->next;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Starting iteration over zone `%s'!\n",
last->domain);
/* subtract left-overs from previous iteration */
GNUNET_STATISTICS_update (stats,
"# NAMESTORE records requested",
1,
GNUNET_NO);
ns_iterator_trigger_next = 1;
GNUNET_STATISTICS_update (stats,
"# zones iterated",
1,
GNUNET_NO);
zone_it = GNUNET_NAMESTORE_zone_iteration_start (ns,
&last->key,
&ns_lookup_error_cb,
NULL,
&ns_lookup_result_cb,
last,
&iterate_zones,
NULL);
}
/**
* Begin processing hostnames from stdin.
*
* @param cls NULL
*/
static void
process_stdin (void *cls)
{
static struct GNUNET_TIME_Absolute last;
static unsigned int pdot;
char hn[256];
(void) cls;
t = NULL;
if (NULL != id)
{
GNUNET_IDENTITY_disconnect (id);
id = NULL;
}
while (NULL !=
fgets (hn,
sizeof (hn),
stdin))
{
if (strlen(hn) > 0)
hn[strlen(hn)-1] = '\0'; /* eat newline */
if (0 == pdot)
last = GNUNET_TIME_absolute_get ();
pdot++;
if (0 == pdot % 1000)
{
struct GNUNET_TIME_Relative delta;
delta = GNUNET_TIME_absolute_get_duration (last);
last = GNUNET_TIME_absolute_get ();
fprintf (stderr,
"Imported 1000 records in %s\n",
GNUNET_STRINGS_relative_time_to_string (delta,
GNUNET_YES));
GNUNET_STATISTICS_set (stats,
"# domain names provided",
pdot,
GNUNET_NO);
}
queue (hn);
}
fprintf (stderr, "\n");
GNUNET_STATISTICS_set (stats,
"# domain names provided",
pdot,
GNUNET_NO);
iterate_zones (NULL);
}
/**
* Method called to inform about the egos of this peer.
*
* When used with #GNUNET_IDENTITY_connect, this function is
* initially called for all egos and then again whenever a
* ego's name changes or if it is deleted. At the end of
* the initial pass over all egos, the function is once called
* with 'NULL' for @a ego. That does NOT mean that the callback won't
* be invoked in the future or that there was an error.
*
* When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
* this function is only called ONCE, and 'NULL' being passed in
* @a ego does indicate an error (i.e. name is taken or no default
* value is known). If @a ego is non-NULL and if '*ctx'
* is set in those callbacks, the value WILL be passed to a subsequent
* call to the identity callback of #GNUNET_IDENTITY_connect (if
* that one was not NULL).
*
* When an identity is renamed, this function is called with the
* (known) @a ego but the NEW @a name.
*
* When an identity is deleted, this function is called with the
* (known) ego and "NULL" for the @a name. In this case,
* the @a ego is henceforth invalid (and the @a ctx should also be
* cleaned up).
*
* @param cls closure
* @param ego ego handle
* @param ctx context for application to store data for this ego
* (during the lifetime of this process, initially NULL)
* @param name name assigned by the user for this ego,
* NULL if the user just deleted the ego and it
* must thus no longer be used
*/
static void
identity_cb (void *cls,
struct GNUNET_IDENTITY_Ego *ego,
void **ctx,
const char *name)
{
(void) cls;
(void) ctx;
if (NULL == ego)
{
if (NULL != zone_head)
{
t = GNUNET_SCHEDULER_add_now (&process_stdin,
NULL);
}
else
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"No zone found\n");
GNUNET_SCHEDULER_shutdown ();
return;
}
}
if (NULL != name)
{
struct Zone *zone;
zone = GNUNET_new (struct Zone);
zone->key = *GNUNET_IDENTITY_ego_get_private_key (ego);
zone->domain = GNUNET_strdup (name);
GNUNET_CONTAINER_DLL_insert (zone_head,
zone_tail,
zone);
}
}
/**
* Process requests from the queue, then if the queue is
* not empty, try again.
*
* @param cls NULL
* @param args remaining command-line arguments
* @param cfgfile name of the configuration file used (for saving, can be NULL!)
* @param cfg configuration
*/
static void
run (void *cls,
char *const *args,
const char *cfgfile,
const struct GNUNET_CONFIGURATION_Handle *cfg)
{
(void) cls;
(void) args;
(void) cfgfile;
stats = GNUNET_STATISTICS_create ("zoneimport",
cfg);
req_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
ns_pending = GNUNET_CONTAINER_multihashmap_create (1024,
GNUNET_NO);
ctx = GNUNET_DNSSTUB_start (256);
if (NULL == ctx)
{
fprintf (stderr,
"Failed to initialize GNUnet DNS STUB\n");
return;
}
if (NULL == args[0])
{
fprintf (stderr,
"You must provide a list of DNS resolvers on the command line\n");
return;
}
for (unsigned int i=0;NULL != args[i];i++)
{
if (GNUNET_OK !=
GNUNET_DNSSTUB_add_dns_ip (ctx,
args[i]))
{
fprintf (stderr,
"Failed to use `%s' for DNS resolver\n",
args[i]);
return;
}
}
GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
NULL);
ns = GNUNET_NAMESTORE_connect (cfg);
if (NULL == ns)
{
GNUNET_SCHEDULER_shutdown ();
return;
}
id = GNUNET_IDENTITY_connect (cfg,
&identity_cb,
NULL);
}
/**
* Call with IP address of resolver to query.
*
* @param argc should be 2
* @param argv[1] should contain IP address
* @return 0 on success
*/
int
main (int argc,
char *const*argv)
{
struct GNUNET_GETOPT_CommandLineOption options[] = {
GNUNET_GETOPT_OPTION_END
};
if (GNUNET_OK !=
GNUNET_STRINGS_get_utf8_args (argc, argv,
&argc, &argv))
return 2;
GNUNET_PROGRAM_run (argc,
argv,
"gnunet-zoneimport",
"import DNS zone into namestore",
options,
&run,
NULL);
GNUNET_free ((void*) argv);
fprintf (stderr,
"Rejected %u names, had %u cached, did %u lookups, stored %u record sets\n"
"Found %u records, %u lookups failed, %u/%u pending on shutdown\n",
rejects,
cached,
lookups,
record_sets,
records,
failures,
pending,
pending_rs);
return 0;
}
/* end of gnunet-zoneimport.c */
|