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
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
|
/* $Id: evdns.c 6979 2006-08-04 18:31:13Z nickm $ */
/* The original version of this module was written by Adam Langley; for
* a history of modifications, check out the subversion logs.
*
* When editing this module, try to keep it re-mergeable by Adam. Don't
* reformat the whitespace, add Tor dependencies, or so on.
*
* TODO:
* - Support IPv6 and PTR records.
* - Replace all externally visible magic numbers with #defined constants.
* - Write doccumentation for APIs of all external functions.
*/
/* Async DNS Library
* Adam Langley <agl@imperialviolet.org>
* http://www.imperialviolet.org/eventdns.html
* Public Domain code
*
* This software is Public Domain. To view a copy of the public domain dedication,
* visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
* Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
*
* I ask and expect, but do not require, that all derivative works contain an
* attribution similar to:
* Parts developed by Adam Langley <agl@imperialviolet.org>
*
* You may wish to replace the word "Parts" with something else depending on
* the amount of original code.
*
* (Derivative works does not include programs which link against, run or include
* the source verbatim in their source distributions)
*
* Version: 0.1b
*/
#include "../config.h"
#include "../main.h"
#include "evdns.h"
/* According to SUSv2 */
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
#endif
/* for strtok_r */
#define _REENTRANT
#undef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#ifdef __USE_ISOC99B
/* libevent doesn't work without this */
typedef unsigned int uint;
#endif
#define u64 uint64_t
#define u32 uint32_t
#define u16 uint16_t
#define u8 uint8_t
#ifdef WIN32
#define open _open
#define read _read
#define close _close
#endif
#define MAX_ADDRS 32 /* maximum number of addresses from a single packet */
/* which we bother recording */
#define TYPE_A EVDNS_TYPE_A
#define TYPE_CNAME 5
#define TYPE_TXT EVDNS_TYPE_TXT
#define TYPE_MX EVDNS_TYPE_MX
#define TYPE_PTR EVDNS_TYPE_PTR
#define TYPE_AAAA EVDNS_TYPE_AAAA
#define CLASS_INET EVDNS_CLASS_INET
#define EVUTIL_SOCKET_ERROR() (errno)
#define EVUTIL_SET_SOCKET_ERROR(errcode) \
do { errno = (errcode); } while (0)
#define evutil_socket_geterror(sock) (errno)
#define evutil_socket_error_to_string(errcode) (strerror(errcode))
#define evtimer_assign(ev, b, cb, arg) event_assign(ev, b, -1, 0, cb, arg)
#define EVUTIL_UPCAST(ptr, type, field) \
((type *)((char*)ptr) - offsetof(type, field))
struct evdns_request {
u8 *request; /* the dns packet data */
unsigned int request_len;
int reissue_count;
int tx_count; /* the number of times that this packet has been sent */
unsigned int request_type; /* TYPE_PTR or TYPE_A */
void *user_pointer; /* the pointer given to us for this request */
evdns_callback_type user_callback;
struct nameserver *ns; /* the server which we last sent it */
/* elements used by the searching code */
int search_index;
struct search_state *search_state;
char *search_origname; /* needs to be free()ed */
int search_flags;
/* these objects are kept in a circular list */
struct evdns_request *next, *prev;
struct event timeout_event;
u16 trans_id; /* the transaction id */
char request_appended; /* true if the request pointer is data which follows this struct */
char transmit_me; /* needs to be transmitted */
struct evdns_base *base;
};
struct reply {
unsigned int type;
unsigned int have_answer;
union {
struct {
u32 addrcount;
u32 addresses[MAX_ADDRS];
} a;
struct {
u32 addrcount;
struct in6_addr addresses[MAX_ADDRS];
} aaaa;
struct {
char name[HOST_NAME_MAX];
} ptr;
struct {
char name[HOST_NAME_MAX];
u32 priority;
} mx;
/* TXT field may be longer than 508 bytes, but UDP packets are limited to 512 octets */
struct {
char data[508];
} txt;
} data;
};
struct nameserver {
int socket; /* a connected UDP socket */
struct sockaddr_storage address;
socklen_t addrlen;
int failed_times; /* number of times which we have given this server a chance */
int timedout; /* number of times in a row a request has timed out */
struct event event;
/* these objects are kept in a circular list */
struct nameserver *next, *prev;
struct event timeout_event; /* used to keep the timeout for */
/* when we next probe this server. */
/* Valid if state == 0 */
/* Outstanding probe request for this nameserver, if any */
struct evdns_request *probe_request;
char state; /* zero if we think that this server is down */
char choked; /* true if we have an EAGAIN from this server's socket */
char write_waiting; /* true if we are waiting for EV_WRITE events */
struct evdns_base *base;
};
/* Represents a local port where we're listening for DNS requests. Right now, */
/* only UDP is supported. */
struct evdns_server_port {
int socket; /* socket we use to read queries and write replies. */
int refcnt; /* reference count. */
char choked; /* Are we currently blocked from writing? */
char closing; /* Are we trying to close this port, pending writes? */
evdns_request_callback_fn_type user_callback; /* Fn to handle requests */
void *user_data; /* Opaque pointer passed to user_callback */
struct event event; /* Read/write event */
/* circular list of replies that we want to write. */
struct server_request *pending_replies;
struct event_base *event_base;
};
/* Represents part of a reply being built. (That is, a single RR.) */
struct server_reply_item {
struct server_reply_item *next; /* next item in sequence. */
char *name; /* name part of the RR */
u16 type; /* The RR type */
u16 class; /* The RR class (usually CLASS_INET) */
u32 ttl; /* The RR TTL */
char is_name; /* True iff data is a label */
u16 datalen; /* Length of data; -1 if data is a label */
void *data; /* The contents of the RR */
};
/* Represents a request that we've received as a DNS server, and holds */
/* the components of the reply as we're constructing it. */
struct server_request {
/* Pointers to the next and previous entries on the list of replies */
/* that we're waiting to write. Only set if we have tried to respond */
/* and gotten EAGAIN. */
struct server_request *next_pending;
struct server_request *prev_pending;
u16 trans_id; /* Transaction id. */
struct evdns_server_port *port; /* Which port received this request on? */
struct sockaddr_storage addr; /* Where to send the response */
socklen_t addrlen; /* length of addr */
int n_answer; /* how many answer RRs have been set? */
int n_authority; /* how many authority RRs have been set? */
int n_additional; /* how many additional RRs have been set? */
struct server_reply_item *answer; /* linked list of answer RRs */
struct server_reply_item *authority; /* linked list of authority RRs */
struct server_reply_item *additional; /* linked list of additional RRs */
/* Constructed response. Only set once we're ready to send a reply. */
/* Once this is set, the RR fields are cleared, and no more should be set. */
char *response;
size_t response_len;
/* Caller-visible fields: flags, questions. */
struct evdns_server_request base;
};
struct evdns_base {
/* An array of n_req_heads circular lists for inflight requests.
* Each inflight request req is in req_heads[req->trans_id % n_req_heads].
*/
struct evdns_request **req_heads;
/* A circular list of requests that we're waiting to send, but haven't
* sent yet because there are too many requests inflight */
struct evdns_request *req_waiting_head;
/* A circular list of nameservers. */
struct nameserver *server_head;
int n_req_heads;
struct event_base *event_base;
/* The number of good nameservers that we have */
int global_good_nameservers;
/* inflight requests are contained in the req_head list */
/* and are actually going out across the network */
int global_requests_inflight;
/* requests which aren't inflight are in the waiting list */
/* and are counted here */
int global_requests_waiting;
int global_max_requests_inflight;
struct timeval global_timeout; /* 5 seconds */
int global_max_reissues; /* a reissue occurs when we get some errors from the server */
int global_max_retransmits; /* number of times we'll retransmit a request which timed out */
/* number of timeouts in a row before we consider this server to be down */
int global_max_nameserver_timeout;
/* true iff we will use the 0x20 hack to prevent poisoning attacks. */
int global_randomize_case;
/** Port to bind to for outgoing DNS packets. */
struct sockaddr_storage global_outgoing_address;
/** socklen_t for global_outgoing_address. 0 if it isn't set. */
socklen_t global_outgoing_addrlen;
struct search_state *global_search_state;
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
void *lock;
int lock_count;
#endif
};
static struct evdns_base *current_base = NULL;
/* Given a pointer to an evdns_server_request, get the corresponding */
/* server_request. */
#define TO_SERVER_REQUEST(base_ptr) \
((struct server_request*) \
(((char*)(base_ptr) - offsetof(struct server_request, base))))
#define REQ_HEAD(base, id) ((base)->req_heads[id % (base)->n_req_heads])
/* These are the timeout values for nameservers. If we find a nameserver is down */
/* we try to probe it at intervals as given below. Values are in seconds. */
static const struct timeval global_nameserver_timeouts[] = {{10, 0}, {60, 0}, {300, 0}, {900, 0}, {3600, 0}};
static const int global_nameserver_timeouts_length = sizeof(global_nameserver_timeouts)/sizeof(struct timeval);
static struct nameserver *nameserver_pick(struct evdns_base *base);
static void evdns_request_insert(struct evdns_request *req, struct evdns_request **head);
static void evdns_request_remove(struct evdns_request *req, struct evdns_request **head);
static void nameserver_ready_callback(int fd, short events, void *arg);
static int evdns_transmit(struct evdns_base *base);
static int evdns_request_transmit(struct evdns_request *req);
static void nameserver_send_probe(struct nameserver *const ns);
static void search_request_finished(struct evdns_request *const);
static int search_try_next(struct evdns_request *const req);
static struct evdns_request *search_request_new(struct evdns_base *base, int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg);
static void evdns_requests_pump_waiting_queue(struct evdns_base *base);
static u16 transaction_id_pick(struct evdns_base *base);
static struct evdns_request *request_new(struct evdns_base *base, int type, const char *name, int flags, evdns_callback_type callback, void *ptr);
static void request_submit(struct evdns_request *const req);
static int server_request_free(struct server_request *req);
static void server_request_free_answers(struct server_request *req);
static void server_port_free(struct evdns_server_port *port);
static void server_port_ready_callback(int fd, short events, void *arg);
static int evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char *const filename);
static int evdns_base_set_option_impl(struct evdns_base *base,
const char *option, const char *val, int flags);
static void evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests);
static int strtoint(const char *const str);
#define CLOSE_SOCKET(s) close(s)
static int
evutil_parse_sockaddr_port(const char *ip_as_string, struct sockaddr *out, int *outlen)
{
int port;
char buf[128];
const char *cp, *addr_part, *port_part;
int is_ipv6;
/* recognized formats are:
* [ipv6]:port
* ipv6
* [ipv6]
* ipv4:port
* ipv4
*/
cp = strchr(ip_as_string, ':');
if (*ip_as_string == '[') {
int len;
if (!(cp = strchr(ip_as_string, ']'))) {
return -1;
}
len = cp-(ip_as_string + 1);
if (len > (int)sizeof(buf)-1) {
return -1;
}
memcpy(buf, ip_as_string+1, len);
buf[len] = '\0';
addr_part = buf;
if (cp[1] == ':')
port_part = cp+2;
else
port_part = NULL;
is_ipv6 = 1;
} else if (cp && strchr(cp+1, ':')) {
is_ipv6 = 1;
addr_part = ip_as_string;
port_part = NULL;
} else if (cp) {
is_ipv6 = 0;
if (cp - ip_as_string > (int)sizeof(buf)-1) {
return -1;
}
memcpy(buf, ip_as_string, cp-ip_as_string);
buf[cp-ip_as_string] = '\0';
addr_part = buf;
port_part = cp+1;
} else {
addr_part = ip_as_string;
port_part = NULL;
is_ipv6 = 0;
}
if (port_part == NULL) {
port = 0;
} else {
port = atoi(port_part);
if (port <= 0 || port > 65535) {
return -1;
}
}
if (!addr_part)
return -1; /* Should be impossible. */
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
if (1 != inet_pton(AF_INET, addr_part, &sin.sin_addr))
return -1;
if (sizeof(sin) > *outlen)
return -1;
memset(out, 0, *outlen);
memcpy(out, &sin, sizeof(sin));
*outlen = sizeof(sin);
return 0;
}
static int
evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
int include_port)
{
int r;
if (0 != (r = (sa1->sa_family - sa2->sa_family)))
return r;
if (sa1->sa_family == AF_INET) {
const struct sockaddr_in *sin1, *sin2;
sin1 = (const struct sockaddr_in *)sa1;
sin2 = (const struct sockaddr_in *)sa2;
if (sin1->sin_addr.s_addr < sin2->sin_addr.s_addr)
return -1;
else if (sin1->sin_addr.s_addr > sin2->sin_addr.s_addr)
return 1;
else if (include_port &&
(r = ((int)sin1->sin_port - (int)sin2->sin_port)))
return r;
else
return 0;
}
return 1;
}
static void
event_assign(struct event *ev, struct event_base *base, int fd, short events, void (*cb)(int, short, void *), void *arg)
{
event_set(ev, fd, events, cb, arg);
if (base != NULL)
g_assert(event_base_set(base, ev) == 0);
}
static const char *
debug_ntoa(u32 address)
{
static char buf[32];
u32 a = ntohl(address);
snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
(int)(u8)((a>>24)&0xff),
(int)(u8)((a>>16)&0xff),
(int)(u8)((a>>8 )&0xff),
(int)(u8)((a )&0xff));
return buf;
}
static const char *
debug_ntop(const struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
return debug_ntoa(sin->sin_addr.s_addr);
}
#ifdef AF_INET6
if (sa->sa_family == AF_INET6) {
static char buf[128];
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
const char *result;
result = inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf));
return result ? result : "unknown";
}
#endif
return "<unknown>";
}
/* This walks the list of inflight requests to find the */
/* one with a matching transaction id. Returns NULL on */
/* failure */
static struct evdns_request *
request_find_from_trans_id(struct evdns_base *base, u16 trans_id) {
struct evdns_request *req = REQ_HEAD(base, trans_id);
struct evdns_request *const started_at = req;
if (req) {
do {
if (req->trans_id == trans_id) return req;
req = req->next;
} while (req != started_at);
}
return NULL;
}
/* a libevent callback function which is called when a nameserver */
/* has gone down and we want to test if it has came back to life yet */
static void
nameserver_prod_callback(int fd, short events, void *arg) {
struct nameserver *const ns = (struct nameserver *) arg;
(void)fd;
(void)events;
nameserver_send_probe(ns);
}
/* a libevent callback which is called when a nameserver probe (to see if */
/* it has come back to life) times out. We increment the count of failed_times */
/* and wait longer to send the next probe packet. */
static void
nameserver_probe_failed(struct nameserver *const ns) {
const struct timeval * timeout;
(void) evtimer_del(&ns->timeout_event);
if (ns->state == 1) {
/* This can happen if the nameserver acts in a way which makes us mark */
/* it as bad and then starts sending good replies. */
return;
}
timeout =
&global_nameserver_timeouts[MIN(ns->failed_times,
global_nameserver_timeouts_length - 1)];
ns->failed_times++;
if (evtimer_add(&ns->timeout_event, (struct timeval *) timeout) < 0) {
msg_warn(
"Error from libevent when adding timer event for %s",
debug_ntop((struct sockaddr *)&ns->address));
/* ???? Do more? */
}
}
/* called when a nameserver has been deemed to have failed. For example, too */
/* many packets have timed out etc */
static void
nameserver_failed(struct nameserver *const ns, const char *msg) {
struct evdns_request *req, *started_at;
struct evdns_base *base = ns->base;
int i;
/* if this nameserver has already been marked as failed */
/* then don't do anything */
if (!ns->state) return;
base->global_good_nameservers--;
g_assert(base->global_good_nameservers >= 0);
ns->state = 0;
ns->failed_times = 1;
if (evtimer_add(&ns->timeout_event, (struct timeval *) &global_nameserver_timeouts[0]) < 0) {
msg_warn(
"Error from libevent when adding timer event for %s",
debug_ntop((struct sockaddr*)&ns->address));
/* ???? Do more? */
}
/* walk the list of inflight requests to see if any can be reassigned to */
/* a different server. Requests in the waiting queue don't have a */
/* nameserver assigned yet */
/* if we don't have *any* good nameservers then there's no point */
/* trying to reassign requests to one */
if (!base->global_good_nameservers) return;
for (i = 0; i < base->n_req_heads; ++i) {
req = started_at = base->req_heads[i];
if (req) {
do {
if (req->tx_count == 0 && req->ns == ns) {
/* still waiting to go out, can be moved */
/* to another server */
req->ns = nameserver_pick(base);
}
req = req->next;
} while (req != started_at);
}
}
}
static void
nameserver_up(struct nameserver *const ns) {
if (ns->state) return;
evtimer_del(&ns->timeout_event);
if (ns->probe_request) {
evdns_cancel_request(ns->base, ns->probe_request);
ns->probe_request = NULL;
}
ns->state = 1;
ns->failed_times = 0;
ns->timedout = 0;
ns->base->global_good_nameservers++;
}
static void
request_trans_id_set(struct evdns_request *const req, const u16 trans_id) {
req->trans_id = trans_id;
*((u16 *) req->request) = htons(trans_id);
}
/* Called to remove a request from a list and dealloc it. */
/* head is a pointer to the head of the list it should be */
/* removed from or NULL if the request isn't in a list. */
static void
request_finished(struct evdns_request *const req, struct evdns_request **head) {
struct evdns_base *base = req->base;
int was_inflight = (head != &base->req_waiting_head);
if (head)
evdns_request_remove(req, head);
search_request_finished(req);
if (was_inflight) {
evtimer_del(&req->timeout_event);
base->global_requests_inflight--;
} else {
base->global_requests_waiting--;
}
if (!req->request_appended) {
/* need to free the request data on it's own */
g_free(req->request);
} else {
/* the request data is appended onto the header */
/* so everything gets free()ed when we: */
}
g_free(req);
evdns_requests_pump_waiting_queue(base);
}
/* This is called when a server returns a funny error code. */
/* We try the request again with another server. */
/* */
/* return: */
/* 0 ok */
/* 1 failed/reissue is pointless */
static int
request_reissue(struct evdns_request *req) {
const struct nameserver *const last_ns = req->ns;
/* the last nameserver should have been marked as failing */
/* by the caller of this function, therefore pick will try */
/* not to return it */
req->ns = nameserver_pick(req->base);
if (req->ns == last_ns) {
/* ... but pick did return it */
/* not a lot of point in trying again with the */
/* same server */
return 1;
}
req->reissue_count++;
req->tx_count = 0;
req->transmit_me = 1;
return 0;
}
/* this function looks for space on the inflight queue and promotes */
/* requests from the waiting queue if it can. */
static void
evdns_requests_pump_waiting_queue(struct evdns_base *base) {
while (base->global_requests_inflight < base->global_max_requests_inflight &&
base->global_requests_waiting) {
struct evdns_request *req;
/* move a request from the waiting queue to the inflight queue */
g_assert(base->req_waiting_head);
req = base->req_waiting_head;
evdns_request_remove(req, &base->req_waiting_head);
base->global_requests_waiting--;
base->global_requests_inflight++;
req->ns = nameserver_pick(base);
request_trans_id_set(req, transaction_id_pick(base));
evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
evdns_request_transmit(req);
evdns_transmit(base);
}
}
static void
reply_callback(struct evdns_request *const req, u32 ttl, u32 err, struct reply *reply) {
switch (req->request_type) {
case TYPE_A:
if (reply)
req->user_callback(DNS_ERR_NONE, DNS_IPv4_A,
reply->data.a.addrcount, ttl,
reply->data.a.addresses,
req->user_pointer);
else
req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
return;
case TYPE_PTR:
if (reply) {
char *name = reply->data.ptr.name;
req->user_callback(DNS_ERR_NONE, DNS_PTR, 1, ttl,
&name, req->user_pointer);
} else {
req->user_callback(err, 0, 0, 0, NULL,
req->user_pointer);
}
return;
case TYPE_AAAA:
if (reply)
req->user_callback(DNS_ERR_NONE, DNS_IPv6_AAAA,
reply->data.aaaa.addrcount, ttl,
reply->data.aaaa.addresses,
req->user_pointer);
else
req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
return;
case TYPE_TXT:
if (reply) {
char *data = reply->data.txt.data;
req->user_callback(DNS_ERR_NONE, DNS_TXT, 1, ttl,
&data, req->user_pointer);
}
else {
req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
}
return;
case TYPE_MX:
if (reply) {
struct evdns_mx mx;
mx.host = reply->data.mx.name;
mx.priority = reply->data.mx.priority;
req->user_callback(DNS_ERR_NONE, DNS_MX, 1, ttl,
&mx, req->user_pointer);
}
else {
req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
}
return;
}
g_assert(0);
}
/* this processes a parsed reply packet */
static void
reply_handle(struct evdns_request *const req, u16 flags, u32 ttl, struct reply *reply) {
int error;
static const int error_codes[] = {
DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST,
DNS_ERR_NOTIMPL, DNS_ERR_REFUSED
};
if (flags & 0x020f || !reply || !reply->have_answer) {
/* there was an error */
if (flags & 0x0200) {
error = DNS_ERR_TRUNCATED;
} else {
u16 error_code = (flags & 0x000f) - 1;
if (error_code > 4) {
error = DNS_ERR_UNKNOWN;
} else {
error = error_codes[error_code];
}
}
switch(error) {
case DNS_ERR_NOTIMPL:
case DNS_ERR_REFUSED:
/* we regard these errors as marking a bad nameserver */
if (req->reissue_count < req->base->global_max_reissues) {
char msg[64];
snprintf(msg, sizeof(msg), "Bad response %d (%s)",
error, evdns_err_to_string(error));
nameserver_failed(req->ns, msg);
if (!request_reissue(req)) return;
}
break;
case DNS_ERR_SERVERFAILED:
/* rcode 2 (servfailed) sometimes means "we
* are broken" and sometimes (with some binds)
* means "that request was very confusing."
* Treat this as a timeout, not a failure.
*/
break;
default:
/* we got a good reply from the nameserver */
nameserver_up(req->ns);
}
if (req->search_state && req->request_type != TYPE_PTR) {
/* if we have a list of domains to search in,
* try the next one */
if (!search_try_next(req)) {
/* a new request was issued so this
* request is finished and */
/* the user callback will be made when
* that request (or a */
/* child of it) finishes. */
request_finished(req, &REQ_HEAD(req->base, req->trans_id));
return;
}
}
/* all else failed. Pass the failure up */
reply_callback(req, 0, error, NULL);
request_finished(req, &REQ_HEAD(req->base, req->trans_id));
} else {
/* all ok, tell the user */
reply_callback(req, ttl, 0, reply);
if (req == req->ns->probe_request)
req->ns->probe_request = NULL; /* Avoid double-free */
nameserver_up(req->ns);
request_finished(req, &REQ_HEAD(req->base, req->trans_id));
}
}
static int
name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
int name_end = -1;
int j = *idx;
int ptr_count = 0;
#define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while(0)
#define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while(0)
#define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while(0)
char *cp = name_out;
const char *const end = name_out + name_out_len;
/* Normally, names are a series of length prefixed strings terminated */
/* with a length of 0 (the lengths are u8's < 63). */
/* However, the length can start with a pair of 1 bits and that */
/* means that the next 14 bits are a pointer within the current */
/* packet. */
for(;;) {
u8 label_len;
if (j >= length) return -1;
GET8(label_len);
if (!label_len) break;
if (label_len & 0xc0) {
u8 ptr_low;
GET8(ptr_low);
if (name_end < 0) name_end = j;
j = (((int)label_len & 0x3f) << 8) + ptr_low;
/* Make sure that the target offset is in-bounds. */
if (j < 0 || j >= length) return -1;
/* If we've jumped more times than there are characters in the
* message, we must have a loop. */
if (++ptr_count > length) return -1;
continue;
}
if (label_len > 63) return -1;
if (cp != name_out) {
if (cp + 1 >= end) return -1;
*cp++ = '.';
}
if (cp + label_len >= end) return -1;
memcpy(cp, packet + j, label_len);
cp += label_len;
j += label_len;
}
if (cp >= end) return -1;
*cp = '\0';
if (name_end < 0)
*idx = j;
else
*idx = name_end;
return 0;
err:
return -1;
}
/* parses a raw request from a nameserver */
static int
reply_parse(struct evdns_base *base, u8 *packet, int length) {
int j = 0, k = 0; /* index into packet */
u16 _t; /* used by the macros */
u32 _t32; /* used by the macros */
char tmp_name[256], cmp_name[256]; /* used by the macros */
int name_matches = 0;
u16 trans_id, questions, answers, authority, additional, datalength;
u16 flags = 0;
u32 ttl, ttl_r = 0xffffffff;
struct reply reply;
struct evdns_request *req = NULL;
unsigned int i;
GET16(trans_id);
GET16(flags);
GET16(questions);
GET16(answers);
GET16(authority);
GET16(additional);
(void) authority; /* suppress "unused variable" warnings. */
(void) additional; /* suppress "unused variable" warnings. */
req = request_find_from_trans_id(base, trans_id);
if (!req) return -1;
g_assert(req->base == base);
memset(&reply, 0, sizeof(reply));
/* If it's not an answer, it doesn't correspond to any request. */
if (!(flags & 0x8000)) return -1; /* must be an answer */
if (flags & 0x020f) {
/* there was an error */
goto err;
}
/* if (!answers) return; */ /* must have an answer of some form */
/* This macro skips a name in the DNS reply. */
#define SKIP_NAME \
do { tmp_name[0] = '\0'; \
if (name_parse(packet, length, &j, tmp_name, \
sizeof(tmp_name))<0) \
goto err; \
} while(0)
#define TEST_NAME \
do { tmp_name[0] = '\0'; \
cmp_name[0] = '\0'; \
k = j; \
if (name_parse(packet, length, &j, tmp_name, \
sizeof(tmp_name))<0) \
goto err; \
if (name_parse(req->request, req->request_len, &k, \
cmp_name, sizeof(cmp_name))<0) \
goto err; \
if (base->global_randomize_case) { \
if (strcmp(tmp_name, cmp_name) == 0) \
name_matches = 1; \
} else { \
if (g_ascii_strcasecmp(tmp_name, cmp_name) == 0) \
name_matches = 1; \
} \
} while(0)
reply.type = req->request_type;
/* skip over each question in the reply */
for (i = 0; i < questions; ++i) {
/* the question looks like
* <label:name><u16:type><u16:class>
*/
TEST_NAME;
j += 4;
if (j >= length) goto err;
}
if (!name_matches)
goto err;
/* now we have the answer section which looks like
* <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
*/
for (i = 0; i < answers; ++i) {
u16 type, class;
SKIP_NAME;
GET16(type);
GET16(class);
GET32(ttl);
GET16(datalength);
if (type == TYPE_A && class == CLASS_INET) {
int addrcount, addrtocopy;
if (req->request_type != TYPE_A) {
j += datalength; continue;
}
if ((datalength & 3) != 0) /* not an even number of As. */
goto err;
addrcount = datalength >> 2;
addrtocopy = MIN(MAX_ADDRS - reply.data.a.addrcount, (unsigned)addrcount);
ttl_r = MIN(ttl_r, ttl);
/* we only bother with the first four addresses. */
if (j + 4*addrtocopy > length) goto err;
memcpy(&reply.data.a.addresses[reply.data.a.addrcount],
packet + j, 4*addrtocopy);
j += 4*addrtocopy;
reply.data.a.addrcount += addrtocopy;
reply.have_answer = 1;
if (reply.data.a.addrcount == MAX_ADDRS) break;
} else if (type == TYPE_PTR && class == CLASS_INET) {
if (req->request_type != TYPE_PTR) {
j += datalength; continue;
}
if (name_parse(packet, length, &j, reply.data.ptr.name,
sizeof(reply.data.ptr.name))<0)
goto err;
ttl_r = MIN(ttl_r, ttl);
reply.have_answer = 1;
break;
} else if (type == TYPE_AAAA && class == CLASS_INET) {
int addrcount, addrtocopy;
if (req->request_type != TYPE_AAAA) {
j += datalength; continue;
}
if ((datalength & 15) != 0) /* not an even number of AAAAs. */
goto err;
addrcount = datalength >> 4; /* each address is 16 bytes long */
addrtocopy = MIN(MAX_ADDRS - reply.data.aaaa.addrcount, (unsigned)addrcount);
ttl_r = MIN(ttl_r, ttl);
/* we only bother with the first four addresses. */
if (j + 16*addrtocopy > length) goto err;
memcpy(&reply.data.aaaa.addresses[reply.data.aaaa.addrcount],
packet + j, 16*addrtocopy);
reply.data.aaaa.addrcount += addrtocopy;
j += 16*addrtocopy;
reply.have_answer = 1;
if (reply.data.aaaa.addrcount == MAX_ADDRS) break;
} else if (type == TYPE_TXT) {
if (req->request_type != TYPE_TXT) {
j += datalength;
continue;
}
memcpy(&reply.data.txt.data, packet + j + 1, MIN(sizeof(reply.data.txt.data), datalength - 1));
j += datalength;
ttl_r = MIN(ttl_r, ttl);
reply.have_answer = 1;
break;
} else if (type == TYPE_MX) {
if (req->request_type != TYPE_MX) {
j += datalength;
continue;
}
GET16(reply.data.mx.priority);
if (name_parse(packet, length, &j, reply.data.mx.name,
sizeof(reply.data.mx.name))<0)
goto err;
ttl_r = MIN(ttl_r, ttl);
reply.have_answer = 1;
break;
} else {
/* skip over any other type of resource */
j += datalength;
}
}
reply_handle(req, flags, ttl_r, &reply);
return 0;
err:
if (req)
reply_handle(req, flags, 0, NULL);
return -1;
}
/* Parse a raw request (packet,length) sent to a nameserver port (port) from */
/* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
/* callback. */
static int
request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, socklen_t addrlen)
{
int j = 0; /* index into packet */
u16 _t; /* used by the macros */
char tmp_name[256]; /* used by the macros */
int i;
u16 trans_id, flags, questions, answers, authority, additional;
struct server_request *server_req = NULL;
/* Get the header fields */
GET16(trans_id);
GET16(flags);
GET16(questions);
GET16(answers);
GET16(authority);
GET16(additional);
if (flags & 0x8000) return -1; /* Must not be an answer. */
flags &= 0x0110; /* Only RD and CD get preserved. */
server_req = g_malloc(sizeof(struct server_request));
if (server_req == NULL) return -1;
memset(server_req, 0, sizeof(struct server_request));
server_req->trans_id = trans_id;
memcpy(&server_req->addr, addr, addrlen);
server_req->addrlen = addrlen;
server_req->base.flags = flags;
server_req->base.nquestions = 0;
server_req->base.questions = g_malloc(sizeof(struct evdns_server_question *) * questions);
if (server_req->base.questions == NULL)
goto err;
for (i = 0; i < questions; ++i) {
u16 type, class;
struct evdns_server_question *q;
int namelen;
if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0)
goto err;
GET16(type);
GET16(class);
namelen = strlen(tmp_name);
q = g_malloc(sizeof(struct evdns_server_question) + namelen);
if (!q)
goto err;
q->type = type;
q->dns_question_class = class;
memcpy(q->name, tmp_name, namelen+1);
server_req->base.questions[server_req->base.nquestions++] = q;
}
/* Ignore answers, authority, and additional. */
server_req->port = port;
port->refcnt++;
/* Only standard queries are supported. */
if (flags & 0x7800) {
evdns_server_request_respond(&(server_req->base), DNS_ERR_NOTIMPL);
return -1;
}
port->user_callback(&(server_req->base), port->user_data);
return 0;
err:
if (server_req) {
if (server_req->base.questions) {
for (i = 0; i < server_req->base.nquestions; ++i)
g_free(server_req->base.questions[i]);
g_free(server_req->base.questions);
}
g_free(server_req);
}
return -1;
#undef SKIP_NAME
#undef GET32
#undef GET16
#undef GET8
}
static u16
default_transaction_id_fn(void)
{
u16 trans_id;
struct timespec ts;
static int clkid = -1;
if (clkid == -1) {
clkid = CLOCK_REALTIME;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != -1)
clkid = CLOCK_MONOTONIC;
}
clock_gettime(clkid, &ts);
trans_id = ts.tv_nsec & 0xffff;
return trans_id;
}
static uint16_t (*trans_id_function)(void) = default_transaction_id_fn;
static void
default_random_bytes_fn(char *buf, size_t n)
{
unsigned i;
for (i = 0; i < n; i += 2) {
u16 tid = trans_id_function();
buf[i] = (tid >> 8) & 0xff;
if (i+1<n)
buf[i+1] = tid & 0xff;
}
}
static void (*rand_bytes_function)(char *buf, size_t n) =
default_random_bytes_fn;
static u16
trans_id_from_random_bytes_fn(void)
{
u16 tid;
rand_bytes_function((char*) &tid, sizeof(tid));
return tid;
}
void
evdns_set_transaction_id_fn(uint16_t (*fn)(void))
{
if (fn)
trans_id_function = fn;
else
trans_id_function = default_transaction_id_fn;
rand_bytes_function = default_random_bytes_fn;
}
void
evdns_set_random_bytes_fn(void (*fn)(char *, size_t))
{
rand_bytes_function = fn;
trans_id_function = trans_id_from_random_bytes_fn;
}
/* Try to choose a strong transaction id which isn't already in flight */
static u16
transaction_id_pick(struct evdns_base *base) {
for (;;) {
u16 trans_id = trans_id_function();
if (trans_id == 0xffff) continue;
/* now check to see if that id is already inflight */
if (request_find_from_trans_id(base, trans_id) == NULL)
return trans_id;
}
}
/* choose a namesever to use. This function will try to ignore */
/* nameservers which we think are down and load balance across the rest */
/* by updating the server_head global each time. */
static struct nameserver *
nameserver_pick(struct evdns_base *base) {
struct nameserver *started_at = base->server_head, *picked;
if (!base->server_head) return NULL;
/* if we don't have any good nameservers then there's no */
/* point in trying to find one. */
if (!base->global_good_nameservers) {
base->server_head = base->server_head->next;
return base->server_head;
}
/* remember that nameservers are in a circular list */
for (;;) {
if (base->server_head->state) {
/* we think this server is currently good */
picked = base->server_head;
base->server_head = base->server_head->next;
return picked;
}
base->server_head = base->server_head->next;
if (base->server_head == started_at) {
/* all the nameservers seem to be down */
/* so we just return this one and hope for the */
/* best */
g_assert(base->global_good_nameservers == 0);
picked = base->server_head;
base->server_head = base->server_head->next;
return picked;
}
}
}
/* this is called when a namesever socket is ready for reading */
static void
nameserver_read(struct nameserver *ns) {
struct sockaddr_storage ss;
socklen_t addrlen = sizeof(ss);
u8 packet[1500];
for (;;) {
const int r = recvfrom(ns->socket, packet, sizeof(packet), 0,
(struct sockaddr*)&ss, &addrlen);
if (r < 0) {
int err = evutil_socket_geterror(ns->socket);
nameserver_failed(ns,
evutil_socket_error_to_string(err));
return;
}
if (evutil_sockaddr_cmp((struct sockaddr*)&ss,
(struct sockaddr*)&ns->address, 0)) {
msg_warn("Address mismatch on received "
"DNS packet. Apparent source was %s",
debug_ntop((struct sockaddr*)&ss));
return;
}
ns->timedout = 0;
reply_parse(ns->base, packet, r);
}
}
/* Read a packet from a DNS client on a server port s, parse it, and */
/* act accordingly. */
static void
server_port_read(struct evdns_server_port *s) {
u8 packet[1500];
struct sockaddr_storage addr;
socklen_t addrlen;
int r;
for (;;) {
addrlen = sizeof(struct sockaddr_storage);
r = recvfrom(s->socket, packet, sizeof(packet), 0,
(struct sockaddr*) &addr, &addrlen);
if (r < 0) {
int err = evutil_socket_geterror(s->socket);
msg_warn("Error %s (%d) while reading request.",
evutil_socket_error_to_string(err), err);
return;
}
request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen);
}
}
/* Try to write all pending replies on a given DNS server port. */
static void
server_port_flush(struct evdns_server_port *port)
{
while (port->pending_replies) {
struct server_request *req = port->pending_replies;
int r = sendto(port->socket, req->response, req->response_len, 0,
(struct sockaddr*) &req->addr, req->addrlen);
if (r < 0) {
int err = evutil_socket_geterror(port->socket);
msg_warn("Error %s (%d) while writing response to port; dropping", evutil_socket_error_to_string(err), err);
}
if (server_request_free(req)) {
/* we released the last reference to req->port. */
return;
}
}
/* We have no more pending requests; stop listening for 'writeable' events. */
(void) event_del(&port->event);
event_assign(&port->event, port->event_base,
port->socket, EV_READ | EV_PERSIST,
server_port_ready_callback, port);
if (event_add(&port->event, NULL) < 0) {
msg_warn("Error from libevent when adding event for DNS server.");
/* ???? Do more? */
}
}
/* set if we are waiting for the ability to write to this server. */
/* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
/* we stop these events. */
static void
nameserver_write_waiting(struct nameserver *ns, char waiting) {
if (ns->write_waiting == waiting) return;
ns->write_waiting = waiting;
(void) event_del(&ns->event);
event_assign(&ns->event, ns->base->event_base,
ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
nameserver_ready_callback, ns);
if (event_add(&ns->event, NULL) < 0) {
msg_warn("Error from libevent when adding event for %s",
debug_ntop((struct sockaddr *)&ns->address));
/* ???? Do more? */
}
}
/* a callback function. Called by libevent when the kernel says that */
/* a nameserver socket is ready for writing or reading */
static void
nameserver_ready_callback(int fd, short events, void *arg) {
struct nameserver *ns = (struct nameserver *) arg;
(void)fd;
if (events & EV_WRITE) {
ns->choked = 0;
if (!evdns_transmit(ns->base)) {
nameserver_write_waiting(ns, 0);
}
}
if (events & EV_READ) {
nameserver_read(ns);
}
}
/* a callback function. Called by libevent when the kernel says that */
/* a server socket is ready for writing or reading. */
static void
server_port_ready_callback(int fd, short events, void *arg) {
struct evdns_server_port *port = (struct evdns_server_port *) arg;
(void) fd;
if (events & EV_WRITE) {
port->choked = 0;
server_port_flush(port);
}
if (events & EV_READ) {
server_port_read(port);
}
}
/* This is an inefficient representation; only use it via the dnslabel_table_*
* functions, so that is can be safely replaced with something smarter later. */
#define MAX_LABELS 128
/* Structures used to implement name compression */
struct dnslabel_entry { char *v; off_t pos; };
struct dnslabel_table {
int n_labels; /* number of current entries */
/* map from name to position in message */
struct dnslabel_entry labels[MAX_LABELS];
};
/* Initialize dnslabel_table. */
static void
dnslabel_table_init(struct dnslabel_table *table)
{
table->n_labels = 0;
}
/* Free all storage held by table, but not the table itself. */
static void
dnslabel_clear(struct dnslabel_table *table)
{
int i;
for (i = 0; i < table->n_labels; ++i)
g_free(table->labels[i].v);
table->n_labels = 0;
}
/* return the position of the label in the current message, or -1 if the label */
/* hasn't been used yet. */
static int
dnslabel_table_get_pos(const struct dnslabel_table *table, const char *label)
{
int i;
for (i = 0; i < table->n_labels; ++i) {
if (!strcmp(label, table->labels[i].v))
return table->labels[i].pos;
}
return -1;
}
/* remember that we've used the label at position pos */
static int
dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos)
{
char *v;
int p;
if (table->n_labels == MAX_LABELS)
return (-1);
v = g_strdup(label);
if (v == NULL)
return (-1);
p = table->n_labels++;
table->labels[p].v = v;
table->labels[p].pos = pos;
return (0);
}
/* Converts a string to a length-prefixed set of DNS labels, starting */
/* at buf[j]. name and buf must not overlap. name_len should be the length */
/* of name. table is optional, and is used for compression. */
/* */
/* Input: abc.def */
/* Output: <3>abc<3>def<0> */
/* */
/* Returns the first index after the encoded name, or negative on error. */
/* -1 label was > 63 bytes */
/* -2 name too long to fit in buffer. */
/* */
static off_t
dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
const char *name, const int name_len,
struct dnslabel_table *table) {
const char *end = name + name_len;
int ref = 0;
u16 _t;
#define APPEND16(x) do { \
if (j + 2 > (off_t)buf_len) \
goto overflow; \
_t = htons(x); \
memcpy(buf + j, &_t, 2); \
j += 2; \
} while (0)
#define APPEND32(x) do { \
if (j + 4 > (off_t)buf_len) \
goto overflow; \
_t32 = htonl(x); \
memcpy(buf + j, &_t32, 4); \
j += 4; \
} while (0)
if (name_len > 255) return -2;
for (;;) {
const char *const start = name;
if (table && (ref = dnslabel_table_get_pos(table, name)) >= 0) {
APPEND16(ref | 0xc000);
return j;
}
name = strchr(name, '.');
if (!name) {
const unsigned int label_len = end - start;
if (label_len > 63) return -1;
if ((size_t)(j+label_len+1) > buf_len) return -2;
if (table) dnslabel_table_add(table, start, j);
buf[j++] = label_len;
memcpy(buf + j, start, end - start);
j += end - start;
break;
} else {
/* append length of the label. */
const unsigned int label_len = name - start;
if (label_len > 63) return -1;
if ((size_t)(j+label_len+1) > buf_len) return -2;
if (table) dnslabel_table_add(table, start, j);
buf[j++] = label_len;
memcpy(buf + j, start, name - start);
j += name - start;
/* hop over the '.' */
name++;
}
}
/* the labels must be terminated by a 0. */
/* It's possible that the name ended in a . */
/* in which case the zero is already there */
if (!j || buf[j-1]) buf[j++] = 0;
return j;
overflow:
return (-2);
}
/* Finds the length of a dns request for a DNS name of the given */
/* length. The actual request may be smaller than the value returned */
/* here */
static size_t
evdns_request_len(const size_t name_len) {
return 96 + /* length of the DNS standard header */
name_len + 2 +
4; /* space for the resource type */
}
/* build a dns request packet into buf. buf should be at least as long */
/* as evdns_request_len told you it should be. */
/* */
/* Returns the amount of space used. Negative on error. */
static int
evdns_request_data_build(const char *const name, const int name_len,
const u16 trans_id, const u16 type, const u16 class,
u8 *const buf, size_t buf_len) {
off_t j = 0; /* current offset into buf */
u16 _t; /* used by the macros */
APPEND16(trans_id);
APPEND16(0x0100); /* standard query, recusion needed */
APPEND16(1); /* one question */
APPEND16(0); /* no answers */
APPEND16(0); /* no authority */
APPEND16(0); /* no additional */
j = dnsname_to_labels(buf, buf_len, j, name, name_len, NULL);
if (j < 0) {
return (int)j;
}
APPEND16(type);
APPEND16(class);
return (int)j;
overflow:
return (-1);
}
/* exported function */
struct evdns_server_port *
evdns_add_server_port_with_base(struct event_base *base, int socket, int is_tcp, evdns_request_callback_fn_type cb, void *user_data)
{
struct evdns_server_port *port;
if (!(port = g_malloc(sizeof(struct evdns_server_port))))
return NULL;
memset(port, 0, sizeof(struct evdns_server_port));
g_assert(!is_tcp); /* TCP sockets not yet implemented */
port->socket = socket;
port->refcnt = 1;
port->choked = 0;
port->closing = 0;
port->user_callback = cb;
port->user_data = user_data;
port->pending_replies = NULL;
port->event_base = base;
event_assign(&port->event, port->event_base,
port->socket, EV_READ | EV_PERSIST,
server_port_ready_callback, port);
if (event_add(&port->event, NULL) < 0) {
g_free(port);
return NULL;
}
return port;
}
struct evdns_server_port *
evdns_add_server_port(int socket, int is_tcp, evdns_request_callback_fn_type cb, void *user_data)
{
return evdns_add_server_port_with_base(NULL, socket, is_tcp, cb, user_data);
}
/* exported function */
void
evdns_close_server_port(struct evdns_server_port *port)
{
if (--port->refcnt == 0) {
server_port_free(port);
} else {
port->closing = 1;
}
}
/* exported function */
int
evdns_server_request_add_reply(struct evdns_server_request *_req, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data)
{
struct server_request *req = TO_SERVER_REQUEST(_req);
struct server_reply_item **itemp, *item;
int *countp;
int result = -1;
if (req->response) /* have we already answered? */
goto done;
switch (section) {
case EVDNS_ANSWER_SECTION:
itemp = &req->answer;
countp = &req->n_answer;
break;
case EVDNS_AUTHORITY_SECTION:
itemp = &req->authority;
countp = &req->n_authority;
break;
case EVDNS_ADDITIONAL_SECTION:
itemp = &req->additional;
countp = &req->n_additional;
break;
default:
goto done;
}
while (*itemp) {
itemp = &((*itemp)->next);
}
item = g_malloc(sizeof(struct server_reply_item));
if (!item)
goto done;
item->next = NULL;
if (!(item->name = g_strdup(name))) {
g_free(item);
goto done;
}
item->type = type;
item->dns_question_class = class;
item->ttl = ttl;
item->is_name = is_name != 0;
item->datalen = 0;
item->data = NULL;
if (data) {
if (item->is_name) {
if (!(item->data = g_strdup(data))) {
g_free(item->name);
g_free(item);
goto done;
}
item->datalen = (u16)-1;
} else {
if (!(item->data = g_malloc(datalen))) {
g_free(item->name);
g_free(item);
goto done;
}
item->datalen = datalen;
memcpy(item->data, data, datalen);
}
}
*itemp = item;
++(*countp);
result = 0;
done:
return result;
}
/* exported function */
int
evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl)
{
return evdns_server_request_add_reply(
req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
ttl, n*4, 0, addrs);
}
/* exported function */
int
evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl)
{
return evdns_server_request_add_reply(
req, EVDNS_ANSWER_SECTION, name, TYPE_AAAA, CLASS_INET,
ttl, n*16, 0, addrs);
}
/* exported function */
int
evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl)
{
u32 a;
char buf[32];
g_assert(in || inaddr_name);
g_assert(!(in && inaddr_name));
if (in) {
a = ntohl(in->s_addr);
snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
(int)(u8)((a )&0xff),
(int)(u8)((a>>8 )&0xff),
(int)(u8)((a>>16)&0xff),
(int)(u8)((a>>24)&0xff));
inaddr_name = buf;
}
return evdns_server_request_add_reply(
req, EVDNS_ANSWER_SECTION, inaddr_name, TYPE_PTR, CLASS_INET,
ttl, -1, 1, hostname);
}
/* exported function */
int
evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl)
{
return evdns_server_request_add_reply(
req, EVDNS_ANSWER_SECTION, name, TYPE_CNAME, CLASS_INET,
ttl, -1, 1, cname);
}
/* exported function */
void
evdns_server_request_set_flags(struct evdns_server_request *exreq, int flags)
{
struct server_request *req = TO_SERVER_REQUEST(exreq);
req->base.flags &= ~(EVDNS_FLAGS_AA|EVDNS_FLAGS_RD);
req->base.flags |= flags;
}
static int
evdns_server_request_format_response(struct server_request *req, int err)
{
unsigned char buf[1500];
size_t buf_len = sizeof(buf);
off_t j = 0, r;
u16 _t;
u32 _t32;
int i;
u16 flags;
struct dnslabel_table table;
if (err < 0 || err > 15) return -1;
/* Set response bit and error code; copy OPCODE and RD fields from
* question; copy RA and AA if set by caller. */
flags = req->base.flags;
flags |= (0x8000 | err);
dnslabel_table_init(&table);
APPEND16(req->trans_id);
APPEND16(flags);
APPEND16(req->base.nquestions);
APPEND16(req->n_answer);
APPEND16(req->n_authority);
APPEND16(req->n_additional);
/* Add questions. */
for (i=0; i < req->base.nquestions; ++i) {
const char *s = req->base.questions[i]->name;
j = dnsname_to_labels(buf, buf_len, j, s, strlen(s), &table);
if (j < 0) {
dnslabel_clear(&table);
return (int) j;
}
APPEND16(req->base.questions[i]->type);
APPEND16(req->base.questions[i]->dns_question_class);
}
/* Add answer, authority, and additional sections. */
for (i=0; i<3; ++i) {
struct server_reply_item *item;
if (i==0)
item = req->answer;
else if (i==1)
item = req->authority;
else
item = req->additional;
while (item) {
r = dnsname_to_labels(buf, buf_len, j, item->name, strlen(item->name), &table);
if (r < 0)
goto overflow;
j = r;
APPEND16(item->type);
APPEND16(item->dns_question_class);
APPEND32(item->ttl);
if (item->is_name) {
off_t len_idx = j, name_start;
j += 2;
name_start = j;
r = dnsname_to_labels(buf, buf_len, j, item->data, strlen(item->data), &table);
if (r < 0)
goto overflow;
j = r;
_t = htons( (short) (j-name_start) );
memcpy(buf+len_idx, &_t, 2);
} else {
APPEND16(item->datalen);
if (j+item->datalen > (off_t)buf_len)
goto overflow;
memcpy(buf+j, item->data, item->datalen);
j += item->datalen;
}
item = item->next;
}
}
if (j > 512) {
overflow:
j = 512;
buf[2] |= 0x02; /* set the truncated bit. */
}
req->response_len = j;
if (!(req->response = g_malloc(req->response_len))) {
server_request_free_answers(req);
dnslabel_clear(&table);
return (-1);
}
memcpy(req->response, buf, req->response_len);
server_request_free_answers(req);
dnslabel_clear(&table);
return (0);
}
/* exported function */
int
evdns_server_request_respond(struct evdns_server_request *_req, int err)
{
struct server_request *req = TO_SERVER_REQUEST(_req);
struct evdns_server_port *port = req->port;
int r = -1;
if (!req->response) {
if ((r = evdns_server_request_format_response(req, err))<0)
goto done;
}
r = sendto(port->socket, req->response, req->response_len, 0,
(struct sockaddr*) &req->addr, req->addrlen);
if (r<0) {
if (port->pending_replies) {
req->prev_pending = port->pending_replies->prev_pending;
req->next_pending = port->pending_replies;
req->prev_pending->next_pending =
req->next_pending->prev_pending = req;
} else {
req->prev_pending = req->next_pending = req;
port->pending_replies = req;
port->choked = 1;
(void) event_del(&port->event);
event_assign(&port->event, port->event_base, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port);
if (event_add(&port->event, NULL) < 0) {
msg_warn("Error from libevent when adding event for DNS server");
}
}
r = 1;
goto done;
}
if (server_request_free(req)) {
r = 0;
goto done;
}
if (port->pending_replies)
server_port_flush(port);
r = 0;
done:
return r;
}
/* Free all storage held by RRs in req. */
static void
server_request_free_answers(struct server_request *req)
{
struct server_reply_item *victim, *next, **list;
int i;
for (i = 0; i < 3; ++i) {
if (i==0)
list = &req->answer;
else if (i==1)
list = &req->authority;
else
list = &req->additional;
victim = *list;
while (victim) {
next = victim->next;
g_free(victim->name);
if (victim->data)
g_free(victim->data);
g_free(victim);
victim = next;
}
*list = NULL;
}
}
/* Free all storage held by req, and remove links to it. */
/* return true iff we just wound up freeing the server_port. */
static int
server_request_free(struct server_request *req)
{
int i, rc=1, lock=0;
if (req->base.questions) {
for (i = 0; i < req->base.nquestions; ++i)
g_free(req->base.questions[i]);
g_free(req->base.questions);
}
if (req->port) {
lock=1;
if (req->port->pending_replies == req) {
if (req->next_pending)
req->port->pending_replies = req->next_pending;
else
req->port->pending_replies = NULL;
}
rc = --req->port->refcnt;
}
if (req->response) {
g_free(req->response);
}
server_request_free_answers(req);
if (req->next_pending && req->next_pending != req) {
req->next_pending->prev_pending = req->prev_pending;
req->prev_pending->next_pending = req->next_pending;
}
if (rc == 0) {
server_port_free(req->port);
g_free(req);
return (1);
}
g_free(req);
return (0);
}
/* Free all storage held by an evdns_server_port. Only called when */
static void
server_port_free(struct evdns_server_port *port)
{
g_assert(port);
g_assert(!port->refcnt);
g_assert(!port->pending_replies);
if (port->socket > 0) {
CLOSE_SOCKET(port->socket);
port->socket = -1;
}
(void) event_del(&port->event);
g_free(port);
}
/* exported function */
int
evdns_server_request_drop(struct evdns_server_request *_req)
{
struct server_request *req = TO_SERVER_REQUEST(_req);
server_request_free(req);
return 0;
}
/* exported function */
int
evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len)
{
struct server_request *req = TO_SERVER_REQUEST(_req);
if (addr_len < (int)req->addrlen)
return -1;
memcpy(sa, &(req->addr), req->addrlen);
return req->addrlen;
}
#undef APPEND16
#undef APPEND32
/* this is a libevent callback function which is called when a request */
/* has timed out. */
static void
evdns_request_timeout_callback(int fd, short events, void *arg) {
struct evdns_request *const req = (struct evdns_request *) arg;
(void) fd;
(void) events;
req->ns->timedout++;
if (req->ns->timedout > req->base->global_max_nameserver_timeout) {
req->ns->timedout = 0;
nameserver_failed(req->ns, "request timed out.");
}
if (req->tx_count >= req->base->global_max_retransmits) {
/* this request has failed */
reply_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
request_finished(req, &REQ_HEAD(req->base, req->trans_id));
} else {
/* retransmit it */
(void) evtimer_del(&req->timeout_event);
evdns_request_transmit(req);
}
}
/* try to send a request to a given server. */
/* */
/* return: */
/* 0 ok */
/* 1 temporary failure */
/* 2 other failure */
static int
evdns_request_transmit_to(struct evdns_request *req, struct nameserver *server) {
int r;
r = sendto(server->socket, req->request, req->request_len, 0,
(struct sockaddr *)&server->address, server->addrlen);
if (r < 0) {
int err = evutil_socket_geterror(server->socket);
nameserver_failed(req->ns, evutil_socket_error_to_string(err));
return 2;
} else if (r != (int)req->request_len) {
return 1; /* short write */
} else {
return 0;
}
}
/* try to send a request, updating the fields of the request */
/* as needed */
/* */
/* return: */
/* 0 ok */
/* 1 failed */
static int
evdns_request_transmit(struct evdns_request *req) {
int retcode = 0, r;
/* if we fail to send this packet then this flag marks it */
/* for evdns_transmit */
req->transmit_me = 1;
if (req->trans_id == 0xffff) abort();
if (req->ns->choked) {
/* don't bother trying to write to a socket */
/* which we have had EAGAIN from */
return 1;
}
r = evdns_request_transmit_to(req, req->ns);
switch (r) {
case 1:
/* temp failure */
req->ns->choked = 1;
nameserver_write_waiting(req->ns, 1);
return 1;
case 2:
/* failed to transmit the request entirely. */
retcode = 1;
/* fall through: we'll set a timeout, which will time out,
* and make us retransmit the request anyway. */
default:
/* all ok */
if (evtimer_add(&req->timeout_event, &req->base->global_timeout) < 0) {
msg_warn(
"Error from libevent when adding timer for request %lx",
(unsigned long) req);
/* ???? Do more? */
}
req->tx_count++;
req->transmit_me = 0;
return retcode;
}
}
static void
nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
struct nameserver *const ns = (struct nameserver *) arg;
(void) type;
(void) count;
(void) ttl;
(void) addresses;
ns->probe_request = NULL;
if (result == DNS_ERR_CANCEL) {
/* We canceled this request because the nameserver came up
* for some other reason. Do not change our opinion about
* the nameserver. */
} else if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
/* this is a good reply */
nameserver_up(ns);
} else {
nameserver_probe_failed(ns);
}
}
static void
nameserver_send_probe(struct nameserver *const ns) {
struct evdns_request *req;
/* here we need to send a probe to a given nameserver */
/* in the hope that it is up now. */
req = request_new(ns->base, TYPE_A, "google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
if (!req) return;
ns->probe_request = req;
/* we force this into the inflight queue no matter what */
request_trans_id_set(req, transaction_id_pick(ns->base));
req->ns = ns;
request_submit(req);
}
/* returns: */
/* 0 didn't try to transmit anything */
/* 1 tried to transmit something */
static int
evdns_transmit(struct evdns_base *base) {
char did_try_to_transmit = 0;
int i;
for (i = 0; i < base->n_req_heads; ++i) {
if (base->req_heads[i]) {
struct evdns_request *const started_at = base->req_heads[i], *req = started_at;
/* first transmit all the requests which are currently waiting */
do {
if (req->transmit_me) {
did_try_to_transmit = 1;
evdns_request_transmit(req);
}
req = req->next;
} while (req != started_at);
}
}
return did_try_to_transmit;
}
/* exported function */
int
evdns_base_count_nameservers(struct evdns_base *base)
{
const struct nameserver *server;
int n = 0;
server = base->server_head;
if (!server)
goto done;
do {
++n;
server = server->next;
} while (server != base->server_head);
done:
return n;
}
int
evdns_count_nameservers(void)
{
return evdns_base_count_nameservers(current_base);
}
/* exported function */
int
evdns_base_clear_nameservers_and_suspend(struct evdns_base *base)
{
struct nameserver *server, *started_at;
int i;
server = base->server_head;
started_at = base->server_head;
if (!server) {
return 0;
}
while (1) {
struct nameserver *next = server->next;
(void) event_del(&server->event);
if (evtimer_initialized(&server->timeout_event))
(void) evtimer_del(&server->timeout_event);
if (server->socket >= 0)
CLOSE_SOCKET(server->socket);
g_free(server);
if (next == started_at)
break;
server = next;
}
base->server_head = NULL;
base->global_good_nameservers = 0;
for (i = 0; i < base->n_req_heads; ++i) {
struct evdns_request *req, *req_started_at;
req = req_started_at = base->req_heads[i];
while (req) {
struct evdns_request *next = req->next;
req->tx_count = req->reissue_count = 0;
req->ns = NULL;
/* ???? What to do about searches? */
(void) evtimer_del(&req->timeout_event);
req->trans_id = 0;
req->transmit_me = 0;
base->global_requests_waiting++;
evdns_request_insert(req, &base->req_waiting_head);
/* We want to insert these suspended elements at the front of
* the waiting queue, since they were pending before any of
* the waiting entries were added. This is a circular list,
* so we can just shift the start back by one.*/
base->req_waiting_head = base->req_waiting_head->prev;
if (next == req_started_at)
break;
req = next;
}
base->req_heads[i] = NULL;
}
base->global_requests_inflight = 0;
return 0;
}
int
evdns_clear_nameservers_and_suspend(void)
{
return evdns_base_clear_nameservers_and_suspend(current_base);
}
/* exported function */
int
evdns_base_resume(struct evdns_base *base)
{
evdns_requests_pump_waiting_queue(base);
return 0;
}
int
evdns_resume(void)
{
return evdns_base_resume(current_base);
}
static int
_evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *address, int addrlen) {
/* first check to see if we already have this nameserver */
const struct nameserver *server = base->server_head, *const started_at = base->server_head;
struct nameserver *ns;
int err = 0;
if (server) {
do {
if (!evutil_sockaddr_cmp((struct sockaddr*)&server->address, address, 1)) return 3;
server = server->next;
} while (server != started_at);
}
if (addrlen > (int)sizeof(ns->address)) {
return 2;
}
ns = (struct nameserver *) g_malloc(sizeof(struct nameserver));
if (!ns) return -1;
memset(ns, 0, sizeof(struct nameserver));
ns->base = base;
evtimer_assign(&ns->timeout_event, ns->base->event_base, nameserver_prod_callback, ns);
ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
if (ns->socket < 0) { err = 1; goto out1; }
make_socket_nonblocking(ns->socket);
if (base->global_outgoing_addrlen) {
if (bind(ns->socket,
(struct sockaddr*)&base->global_outgoing_address,
base->global_outgoing_addrlen) < 0) {
msg_warn("Couldn't bind to outgoing address");
err = 2;
goto out2;
}
}
memcpy(&ns->address, address, addrlen);
ns->addrlen = addrlen;
ns->state = 1;
event_assign(&ns->event, ns->base->event_base, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
if (event_add(&ns->event, NULL) < 0) {
err = 2;
goto out2;
}
/* insert this nameserver into the list of them */
if (!base->server_head) {
ns->next = ns->prev = ns;
base->server_head = ns;
} else {
ns->next = base->server_head->next;
ns->prev = base->server_head;
base->server_head->next = ns;
if (base->server_head->prev == base->server_head) {
base->server_head->prev = ns;
}
}
base->global_good_nameservers++;
return 0;
out2:
CLOSE_SOCKET(ns->socket);
out1:
g_free(ns);
msg_warn("Unable to add nameserver %s: error %d", debug_ntop(address), err);
return err;
}
/* exported function */
int
evdns_base_nameserver_add(struct evdns_base *base,
unsigned long int address)
{
struct sockaddr_in sin;
int res;
sin.sin_addr.s_addr = address;
sin.sin_port = htons(53);
sin.sin_family = AF_INET;
res = _evdns_nameserver_add_impl(base, (struct sockaddr*)&sin, sizeof(sin));
return res;
}
int
evdns_nameserver_add(unsigned long int address) {
if (!current_base)
current_base = evdns_base_new(NULL, 0);
return evdns_base_nameserver_add(current_base, address);
}
/* exported function */
int
evdns_base_nameserver_ip_add(struct evdns_base *base, const char *ip_as_string) {
struct sockaddr_storage ss;
struct sockaddr *sa;
int len = sizeof(ss);
int res;
if (evutil_parse_sockaddr_port(ip_as_string, (struct sockaddr *)&ss,
&len)) {
msg_warn("Unable to parse nameserver address %s",
ip_as_string);
return 4;
}
sa = (struct sockaddr *) &ss;
if (sa->sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
if (sin->sin_port == 0)
sin->sin_port = htons(53);
}
#ifdef AF_INET6
else if (sa->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
if (sin6->sin6_port == 0)
sin6->sin6_port = htons(53);
}
#endif
else
return -1;
res = _evdns_nameserver_add_impl(base, sa, len);
return res;
}
int
evdns_nameserver_ip_add(const char *ip_as_string) {
if (!current_base)
current_base = evdns_base_new(NULL, 0);
return evdns_base_nameserver_ip_add(current_base, ip_as_string);
}
/* remove from the queue */
static void
evdns_request_remove(struct evdns_request *req, struct evdns_request **head)
{
#if 0
{
struct evdns_request *ptr;
int found = 0;
g_assert(*head != NULL);
ptr = *head;
do {
if (ptr == req) {
found = 1;
break;
}
ptr = ptr->next;
} while (ptr != *head);
g_assert(found);
g_assert(req->next);
}
#endif
if (req->next == req) {
/* only item in the list */
*head = NULL;
} else {
req->next->prev = req->prev;
req->prev->next = req->next;
if (*head == req) *head = req->next;
}
req->next = req->prev = NULL;
}
/* insert into the tail of the queue */
static void
evdns_request_insert(struct evdns_request *req, struct evdns_request **head) {
if (!*head) {
*head = req;
req->next = req->prev = req;
return;
}
req->prev = (*head)->prev;
req->prev->next = req;
req->next = *head;
(*head)->prev = req;
}
static int
string_num_dots(const char *s) {
int count = 0;
while ((s = strchr(s, '.'))) {
s++;
count++;
}
return count;
}
static struct evdns_request *
request_new(struct evdns_base *base, int type, const char *name, int flags,
evdns_callback_type callback, void *user_ptr) {
const char issuing_now =
(base->global_requests_inflight < base->global_max_requests_inflight) ? 1 : 0;
const size_t name_len = strlen(name);
const size_t request_max_len = evdns_request_len(name_len);
const u16 trans_id = issuing_now ? transaction_id_pick(base) : 0xffff;
/* the request data is alloced in a single block with the header */
struct evdns_request *const req =
g_malloc(sizeof(struct evdns_request) + request_max_len);
int rlen;
char namebuf[256];
(void) flags;
if (!req) return NULL;
if (name_len >= sizeof(namebuf)) {
free(req);
return NULL;
}
memset(req, 0, sizeof(struct evdns_request));
req->base = base;
evtimer_assign(&req->timeout_event, req->base->event_base, evdns_request_timeout_callback, req);
if (base->global_randomize_case) {
unsigned i;
char randbits[(sizeof(namebuf)+7)/8];
g_strlcpy(namebuf, name, sizeof(namebuf));
rand_bytes_function(randbits, (name_len+7)/8);
for (i = 0; i < name_len; ++i) {
if (g_ascii_isalpha(namebuf[i])) {
if ((randbits[i >> 3] & (1<<(i & 7))))
namebuf[i] |= 0x20;
else
namebuf[i] &= ~0x20;
}
}
name = namebuf;
}
/* request data lives just after the header */
req->request = ((u8 *) req) + sizeof(struct evdns_request);
/* denotes that the request data shouldn't be free()ed */
req->request_appended = 1;
rlen = evdns_request_data_build(name, name_len, trans_id,
type, CLASS_INET, req->request, request_max_len);
if (rlen < 0)
goto err1;
req->request_len = rlen;
req->trans_id = trans_id;
req->tx_count = 0;
req->request_type = type;
req->user_pointer = user_ptr;
req->user_callback = callback;
req->ns = issuing_now ? nameserver_pick(base) : NULL;
req->next = req->prev = NULL;
return req;
err1:
g_free(req);
return NULL;
}
static void
request_submit(struct evdns_request *const req) {
struct evdns_base *base = req->base;
if (req->ns) {
/* if it has a nameserver assigned then this is going */
/* straight into the inflight queue */
evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
base->global_requests_inflight++;
evdns_request_transmit(req);
} else {
evdns_request_insert(req, &base->req_waiting_head);
base->global_requests_waiting++;
}
}
/* exported function */
void
evdns_cancel_request(struct evdns_base *base, struct evdns_request *req)
{
if (!base)
base = req->base;
reply_callback(req, 0, DNS_ERR_CANCEL, NULL);
if (req->ns) {
/* remove from inflight queue */
request_finished(req, &REQ_HEAD(base, req->trans_id));
} else {
/* remove from global_waiting head */
request_finished(req, &base->req_waiting_head);
}
}
/* exported function */
struct evdns_request *
evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags,
evdns_callback_type callback, void *ptr) {
struct evdns_request *req;
if (flags & DNS_QUERY_NO_SEARCH) {
req =
request_new(base, TYPE_A, name, flags, callback, ptr);
if (req)
request_submit(req);
} else {
req = search_request_new(base, TYPE_A, name, flags, callback, ptr);
}
return req;
}
int evdns_resolve_ipv4(const char *name, int flags,
evdns_callback_type callback, void *ptr)
{
return evdns_base_resolve_ipv4(current_base, name, flags, callback, ptr)
? 0 : -1;
}
/* exported function */
struct evdns_request *
evdns_base_resolve_ipv6(struct evdns_base *base,
const char *name, int flags,
evdns_callback_type callback, void *ptr)
{
struct evdns_request *req;
if (flags & DNS_QUERY_NO_SEARCH) {
req = request_new(base, TYPE_AAAA, name, flags, callback, ptr);
if (req)
request_submit(req);
} else {
req = search_request_new(base,TYPE_AAAA, name, flags, callback, ptr);
}
return req;
}
int evdns_resolve_ipv6(const char *name, int flags,
evdns_callback_type callback, void *ptr) {
return evdns_base_resolve_ipv6(current_base, name, flags, callback, ptr)
? 0 : -1;
}
struct evdns_request *
evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
char buf[32];
struct evdns_request *req;
u32 a;
g_assert(in);
a = ntohl(in->s_addr);
snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
(int)(u8)((a )&0xff),
(int)(u8)((a>>8 )&0xff),
(int)(u8)((a>>16)&0xff),
(int)(u8)((a>>24)&0xff));
req = request_new(base, TYPE_PTR, buf, flags, callback, ptr);
if (req)
request_submit(req);
return (req);
}
int evdns_resolve_reverse(const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
return evdns_base_resolve_reverse(current_base, in, flags, callback, ptr)
? 0 : -1;
}
struct evdns_request *
evdns_base_resolve_reverse_ipv6(struct evdns_base *base, const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
/* 32 nybbles, 32 periods, "ip6.arpa", NUL. */
char buf[73];
char *cp;
struct evdns_request *req;
int i;
g_assert(in);
cp = buf;
for (i=15; i >= 0; --i) {
u8 byte = in->s6_addr[i];
*cp++ = "0123456789abcdef"[byte & 0x0f];
*cp++ = '.';
*cp++ = "0123456789abcdef"[byte >> 4];
*cp++ = '.';
}
g_assert(cp + strlen("ip6.arpa") < buf+sizeof(buf));
memcpy(cp, "ip6.arpa", strlen("ip6.arpa")+1);
req = request_new(base, TYPE_PTR, buf, flags, callback, ptr);
if (req)
request_submit(req);
return (req);
}
int evdns_resolve_reverse_ipv6(const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
return evdns_base_resolve_reverse_ipv6(current_base, in, flags, callback, ptr)
? 0 : -1;
}
int
evdns_resolve_txt(const char *in, int flags, evdns_callback_type callback, void *ptr) {
struct evdns_request *req;
g_assert(in);
req = request_new(current_base, TYPE_TXT, in, flags, callback, ptr);
if (!req) return 1;
request_submit(req);
return 0;
}
int
evdns_resolve_mx(const char *in, int flags, evdns_callback_type callback, void *ptr) {
struct evdns_request *req;
g_assert(in);
req = request_new(current_base, TYPE_MX, in, flags, callback, ptr);
if (!req) return 1;
request_submit(req);
return 0;
}
/*/////////////////////////////////////////////////////////////////// */
/* Search support */
/* */
/* the libc resolver has support for searching a number of domains */
/* to find a name. If nothing else then it takes the single domain */
/* from the gethostname() call. */
/* */
/* It can also be configured via the domain and search options in a */
/* resolv.conf. */
/* */
/* The ndots option controls how many dots it takes for the resolver */
/* to decide that a name is non-local and so try a raw lookup first. */
struct search_domain {
int len;
struct search_domain *next;
/* the text string is appended to this structure */
};
struct search_state {
int refcount;
int ndots;
int num_domains;
struct search_domain *head;
};
static void
search_state_decref(struct search_state *const state) {
if (!state) return;
state->refcount--;
if (!state->refcount) {
struct search_domain *next, *dom;
for (dom = state->head; dom; dom = next) {
next = dom->next;
g_free(dom);
}
g_free(state);
}
}
static struct search_state *
search_state_new(void) {
struct search_state *state = (struct search_state *) g_malloc(sizeof(struct search_state));
if (!state) return NULL;
memset(state, 0, sizeof(struct search_state));
state->refcount = 1;
state->ndots = 1;
return state;
}
static void
search_postfix_clear(struct evdns_base *base) {
search_state_decref(base->global_search_state);
base->global_search_state = search_state_new();
}
/* exported function */
void
evdns_base_search_clear(struct evdns_base *base)
{
search_postfix_clear(base);
}
void
evdns_search_clear(void) {
evdns_base_search_clear(current_base);
}
static void
search_postfix_add(struct evdns_base *base, const char *domain) {
int domain_len;
struct search_domain *sdomain;
while (domain[0] == '.') domain++;
domain_len = strlen(domain);
if (!base->global_search_state) base->global_search_state = search_state_new();
if (!base->global_search_state) return;
base->global_search_state->num_domains++;
sdomain = (struct search_domain *) g_malloc(sizeof(struct search_domain) + domain_len);
if (!sdomain) return;
memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
sdomain->next = base->global_search_state->head;
sdomain->len = domain_len;
base->global_search_state->head = sdomain;
}
/* reverse the order of members in the postfix list. This is needed because, */
/* when parsing resolv.conf we push elements in the wrong order */
static void
search_reverse(struct evdns_base *base) {
struct search_domain *cur, *prev = NULL, *next;
cur = base->global_search_state->head;
while (cur) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
base->global_search_state->head = prev;
}
/* exported function */
void
evdns_base_search_add(struct evdns_base *base, const char *domain) {
search_postfix_add(base, domain);
}
void
evdns_search_add(const char *domain) {
evdns_base_search_add(current_base, domain);
}
/* exported function */
void
evdns_base_search_ndots_set(struct evdns_base *base, const int ndots) {
if (!base->global_search_state) base->global_search_state = search_state_new();
if (base->global_search_state)
base->global_search_state->ndots = ndots;
}
void
evdns_search_ndots_set(const int ndots) {
evdns_base_search_ndots_set(current_base, ndots);
}
static void
search_set_from_hostname(struct evdns_base *base) {
char hostname[HOST_NAME_MAX + 1], *domainname;
search_postfix_clear(base);
if (gethostname(hostname, sizeof(hostname))) return;
domainname = strchr(hostname, '.');
if (!domainname) return;
search_postfix_add(base, domainname);
}
/* warning: returns malloced string */
static char *
search_make_new(const struct search_state *const state, int n, const char *const base_name) {
const int base_len = strlen(base_name);
const char need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
struct search_domain *dom;
for (dom = state->head; dom; dom = dom->next) {
if (!n--) {
/* this is the postfix we want */
/* the actual postfix string is kept at the end of the structure */
const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
const int postfix_len = dom->len;
char *const newname = (char *) g_malloc(base_len + need_to_append_dot + postfix_len + 1);
if (!newname) return NULL;
memcpy(newname, base_name, base_len);
if (need_to_append_dot) newname[base_len] = '.';
memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
newname[base_len + need_to_append_dot + postfix_len] = 0;
return newname;
}
}
/* we ran off the end of the list and still didn't find the requested string */
abort();
return NULL; /* unreachable; stops warnings in some compilers. */
}
static struct evdns_request *
search_request_new(struct evdns_base *base, int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg) {
g_assert(type == TYPE_A || type == TYPE_AAAA);
if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
base->global_search_state &&
base->global_search_state->num_domains) {
/* we have some domains to search */
struct evdns_request *req;
if (string_num_dots(name) >= base->global_search_state->ndots) {
req = request_new(base, type, name, flags, user_callback, user_arg);
if (!req) return NULL;
req->search_index = -1;
} else {
char *const new_name = search_make_new(base->global_search_state, 0, name);
if (!new_name) return NULL;
req = request_new(base, type, new_name, flags, user_callback, user_arg);
g_free(new_name);
if (!req) return NULL;
req->search_index = 0;
}
req->search_origname = g_strdup(name);
req->search_state = base->global_search_state;
req->search_flags = flags;
base->global_search_state->refcount++;
request_submit(req);
return req;
} else {
struct evdns_request *const req = request_new(base, type, name, flags, user_callback, user_arg);
if (!req) return NULL;
request_submit(req);
return req;
}
}
/* if it is part of a search and, if so, try the next name in the list */
/* returns: */
/* 0 another request has been submitted */
/* 1 no more requests needed */
static int
search_try_next(struct evdns_request *const req) {
struct evdns_base *base = req->base;
if (req->search_state) {
/* it is part of a search */
char *new_name;
struct evdns_request *newreq;
req->search_index++;
if (req->search_index >= req->search_state->num_domains) {
/* no more postfixes to try, however we may need to try */
/* this name without a postfix */
if (string_num_dots(req->search_origname) < req->search_state->ndots) {
/* yep, we need to try it raw */
newreq = request_new(base, req->request_type, req->search_origname, req->search_flags, req->user_callback, req->user_pointer);
if (newreq) {
request_submit(newreq);
return 0;
}
}
return 1;
}
new_name = search_make_new(req->search_state, req->search_index, req->search_origname);
if (!new_name) return 1;
newreq = request_new(base, req->request_type, new_name, req->search_flags, req->user_callback, req->user_pointer);
g_free(new_name);
if (!newreq) return 1;
newreq->search_origname = req->search_origname;
req->search_origname = NULL;
newreq->search_state = req->search_state;
newreq->search_flags = req->search_flags;
newreq->search_index = req->search_index;
newreq->search_state->refcount++;
request_submit(newreq);
return 0;
}
return 1;
}
static void
search_request_finished(struct evdns_request *const req) {
if (req->search_state) {
search_state_decref(req->search_state);
req->search_state = NULL;
}
if (req->search_origname) {
g_free(req->search_origname);
req->search_origname = NULL;
}
}
/*/////////////////////////////////////////////////////////////////// */
/* Parsing resolv.conf files */
static void
evdns_resolv_set_defaults(struct evdns_base *base, int flags) {
/* if the file isn't found then we assume a local resolver */
if (flags & DNS_OPTION_SEARCH) search_set_from_hostname(base);
if (flags & DNS_OPTION_NAMESERVERS) evdns_base_nameserver_ip_add(base,"127.0.0.1");
}
/* helper version of atoi which returns -1 on error */
static int
strtoint(const char *const str)
{
char *endptr;
const int r = strtol(str, &endptr, 10);
if (*endptr) return -1;
return r;
}
/* Parse a number of seconds into a timeval; return -1 on error. */
static int
strtotimeval(const char *const str, struct timeval *out)
{
double d;
char *endptr;
d = strtod(str, &endptr);
if (*endptr) return -1;
out->tv_sec = (int) d;
out->tv_usec = (int) ((d - (int) d)*1000000);
return 0;
}
/* helper version of atoi that returns -1 on error and clips to bounds. */
static int
strtoint_clipped(const char *const str, int min, int max)
{
int r = strtoint(str);
if (r == -1)
return r;
else if (r<min)
return min;
else if (r>max)
return max;
else
return r;
}
static int
evdns_base_set_max_requests_inflight(struct evdns_base *base, int maxinflight)
{
int old_n_heads = base->n_req_heads, n_heads;
struct evdns_request **old_heads = base->req_heads, **new_heads, *req;
int i;
if (maxinflight < 1)
maxinflight = 1;
n_heads = (maxinflight+4) / 5;
g_assert(n_heads > 0);
new_heads = g_malloc(n_heads * sizeof(struct evdns_request*));
if (!new_heads)
return (-1);
for (i=0; i < n_heads; ++i)
new_heads[i] = NULL;
if (old_heads) {
for (i = 0; i < old_n_heads; ++i) {
while (old_heads[i]) {
req = old_heads[i];
evdns_request_remove(req, &old_heads[i]);
evdns_request_insert(req, &new_heads[req->trans_id % n_heads]);
}
}
g_free(old_heads);
}
base->req_heads = new_heads;
base->n_req_heads = n_heads;
base->global_max_requests_inflight = maxinflight;
return (0);
}
/* exported function */
int
evdns_base_set_option(struct evdns_base *base,
const char *option, const char *val, int flags)
{
int res;
res = evdns_base_set_option_impl(base, option, val, flags);
return res;
}
static int
evdns_base_set_option_impl(struct evdns_base *base,
const char *option, const char *val, int flags)
{
if (!strncmp(option, "ndots:", 6)) {
const int ndots = strtoint(val);
if (ndots == -1) return -1;
if (!(flags & DNS_OPTION_SEARCH)) return 0;
if (!base->global_search_state) base->global_search_state = search_state_new();
if (!base->global_search_state) return -1;
base->global_search_state->ndots = ndots;
} else if (!strncmp(option, "timeout:", 8)) {
struct timeval tv;
if (strtotimeval(val, &tv) == -1) return -1;
if (!(flags & DNS_OPTION_MISC)) return 0;
memcpy(&base->global_timeout, &tv, sizeof(struct timeval));
} else if (!strncmp(option, "max-timeouts:", 12)) {
const int maxtimeout = strtoint_clipped(val, 1, 255);
if (maxtimeout == -1) return -1;
if (!(flags & DNS_OPTION_MISC)) return 0;
base->global_max_nameserver_timeout = maxtimeout;
} else if (!strncmp(option, "max-inflight:", 13)) {
const int maxinflight = strtoint_clipped(val, 1, 65000);
if (maxinflight == -1) return -1;
if (!(flags & DNS_OPTION_MISC)) return 0;
evdns_base_set_max_requests_inflight(base, maxinflight);
} else if (!strncmp(option, "attempts:", 9)) {
int retries = strtoint(val);
if (retries == -1) return -1;
if (retries > 255) retries = 255;
if (!(flags & DNS_OPTION_MISC)) return 0;
base->global_max_retransmits = retries;
} else if (!strncmp(option, "randomize-case:", 15)) {
int randcase = strtoint(val);
if (!(flags & DNS_OPTION_MISC)) return 0;
base->global_randomize_case = randcase;
} else if (!strncmp(option, "bind-to:", 8)) {
/* XXX This only applies to successive nameservers, not
* to already-configured ones. We might want to fix that. */
int len = sizeof(base->global_outgoing_address);
if (!(flags & DNS_OPTION_NAMESERVERS)) return 0;
if (evutil_parse_sockaddr_port(val,
(struct sockaddr*)&base->global_outgoing_address, &len))
return -1;
base->global_outgoing_addrlen = len;
}
return 0;
}
int
evdns_set_option(const char *option, const char *val, int flags)
{
return evdns_base_set_option(current_base, option, val, flags);
}
static void
resolv_conf_parse_line(struct evdns_base *base, char *const start, int flags) {
char *strtok_state;
static const char *const delims = " \t";
#define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
char *const first_token = strtok_r(start, delims, &strtok_state);
if (!first_token) return;
if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) {
const char *const nameserver = NEXT_TOKEN;
evdns_base_nameserver_ip_add(base, nameserver);
} else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
const char *const domain = NEXT_TOKEN;
if (domain) {
search_postfix_clear(base);
search_postfix_add(base, domain);
}
} else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
const char *domain;
search_postfix_clear(base);
while ((domain = NEXT_TOKEN)) {
search_postfix_add(base, domain);
}
search_reverse(base);
} else if (!strcmp(first_token, "options")) {
const char *option;
while ((option = NEXT_TOKEN)) {
const char *val = strchr(option, ':');
evdns_base_set_option(base, option, val ? val+1 : "", flags);
}
}
#undef NEXT_TOKEN
}
/* exported function */
/* returns: */
/* 0 no errors */
/* 1 failed to open file */
/* 2 failed to stat file */
/* 3 file too large */
/* 4 out of memory */
/* 5 short read from file */
int
evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename) {
int res;
res = evdns_base_resolv_conf_parse_impl(base, flags, filename);
return res;
}
static int
evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char *const filename) {
struct stat st;
int fd, n, r;
u8 *resolv;
char *start;
int err = 0;
fd = open(filename, O_RDONLY);
if (fd < 0) {
evdns_resolv_set_defaults(base, flags);
return 1;
}
if (fstat(fd, &st)) { err = 2; goto out1; }
if (!st.st_size) {
evdns_resolv_set_defaults(base, flags);
err = (flags & DNS_OPTION_NAMESERVERS) ? 6 : 0;
goto out1;
}
if (st.st_size > 65535) { err = 3; goto out1; } /* no resolv.conf should be any bigger */
resolv = (u8 *) g_malloc((size_t)st.st_size + 1);
if (!resolv) { err = 4; goto out1; }
n = 0;
while ((r = read(fd, resolv+n, (size_t)st.st_size-n)) > 0) {
n += r;
if (n == st.st_size)
break;
g_assert(n < st.st_size);
}
if (r < 0) { err = 5; goto out2; }
resolv[n] = 0; /* we malloced an extra byte; this should be fine. */
start = (char *) resolv;
for (;;) {
char *const newline = strchr(start, '\n');
if (!newline) {
resolv_conf_parse_line(base, start, flags);
break;
} else {
*newline = 0;
resolv_conf_parse_line(base, start, flags);
start = newline + 1;
}
}
if (!base->server_head && (flags & DNS_OPTION_NAMESERVERS)) {
/* no nameservers were configured. */
evdns_base_nameserver_ip_add(base, "127.0.0.1");
err = 6;
}
if (flags & DNS_OPTION_SEARCH && (!base->global_search_state || base->global_search_state->num_domains == 0)) {
search_set_from_hostname(base);
}
out2:
g_free(resolv);
out1:
close(fd);
return err;
}
int
evdns_resolv_conf_parse(int flags, const char *const filename) {
return evdns_base_resolv_conf_parse(current_base, flags, filename);
}
struct evdns_base *
evdns_base_new(struct event_base *event_base, int initialize_nameservers)
{
struct evdns_base *base;
base = g_malloc(sizeof(struct evdns_base));
if (base == NULL)
return (NULL);
memset(base, 0, sizeof(struct evdns_base));
base->req_waiting_head = NULL;
/* Set max requests inflight and allocate req_heads. */
base->req_heads = NULL;
evdns_base_set_max_requests_inflight(base, 64);
base->server_head = NULL;
base->event_base = event_base;
base->global_good_nameservers = base->global_requests_inflight =
base->global_requests_waiting = 0;
base->global_timeout.tv_sec = 5;
base->global_timeout.tv_usec = 0;
base->global_max_reissues = 1;
base->global_max_retransmits = 3;
base->global_max_nameserver_timeout = 3;
base->global_search_state = NULL;
base->global_randomize_case = 1;
if (initialize_nameservers) {
int r;
r = evdns_base_resolv_conf_parse(base, DNS_OPTIONS_ALL, "/etc/resolv.conf");
if (r == -1) {
evdns_base_free_and_unlock(base, 0);
return NULL;
}
}
return base;
}
int
evdns_init(void)
{
struct evdns_base *base = evdns_base_new(NULL, 1);
if (base) {
current_base = base;
return 0;
} else {
return -1;
}
}
const char *
evdns_err_to_string(int err)
{
switch (err) {
case DNS_ERR_NONE: return "no error";
case DNS_ERR_FORMAT: return "misformatted query";
case DNS_ERR_SERVERFAILED: return "server failed";
case DNS_ERR_NOTEXIST: return "name does not exist";
case DNS_ERR_NOTIMPL: return "query not implemented";
case DNS_ERR_REFUSED: return "refused";
case DNS_ERR_TRUNCATED: return "reply truncated or ill-formed";
case DNS_ERR_UNKNOWN: return "unknown";
case DNS_ERR_TIMEOUT: return "request timed out";
case DNS_ERR_SHUTDOWN: return "dns subsystem shut down";
case DNS_ERR_CANCEL: return "dns request canceled";
default: return "[Unknown error code]";
}
}
static void
evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests)
{
struct nameserver *server, *server_next;
struct search_domain *dom, *dom_next;
int i;
/* Requires that we hold the lock. */
/* TODO(nickm) we might need to refcount here. */
for (i = 0; i < base->n_req_heads; ++i) {
while (base->req_heads[i]) {
if (fail_requests)
reply_callback(base->req_heads[i], 0, DNS_ERR_SHUTDOWN, NULL);
request_finished(base->req_heads[i], &REQ_HEAD(base, base->req_heads[i]->trans_id));
}
}
while (base->req_waiting_head) {
if (fail_requests)
reply_callback(base->req_waiting_head, 0, DNS_ERR_SHUTDOWN, NULL);
request_finished(base->req_waiting_head, &base->req_waiting_head);
}
base->global_requests_inflight = base->global_requests_waiting = 0;
for (server = base->server_head; server; server = server_next) {
server_next = server->next;
if (server->socket >= 0)
CLOSE_SOCKET(server->socket);
(void) event_del(&server->event);
if (server->state == 0)
(void) event_del(&server->timeout_event);
g_free(server);
if (server_next == base->server_head)
break;
}
base->server_head = NULL;
base->global_good_nameservers = 0;
if (base->global_search_state) {
for (dom = base->global_search_state->head; dom; dom = dom_next) {
dom_next = dom->next;
g_free(dom);
}
g_free(base->global_search_state);
base->global_search_state = NULL;
}
g_free(base);
}
void
evdns_base_free(struct evdns_base *base, int fail_requests)
{
evdns_base_free_and_unlock(base, fail_requests);
}
void
evdns_shutdown(int fail_requests)
{
if (current_base) {
struct evdns_base *b = current_base;
current_base = NULL;
evdns_base_free(b, fail_requests);
}
}
|