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
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/param.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/time.h>
#include <time.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <unistd.h>
#include <fstream>
#include "misc.hh"
#include <vector>
#include <sstream>
#include <errno.h>
#include <cstring>
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <algorithm>
#include <poll.h>
#include <iomanip>
#include <netinet/tcp.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "pdnsexception.hh"
#include <sys/types.h>
#include <boost/algorithm/string.hpp>
#include "iputils.hh"
#include "dnsparser.hh"
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#ifdef __FreeBSD__
# include <pthread_np.h>
#endif
#ifdef __NetBSD__
# include <pthread.h>
# include <sched.h>
#endif
size_t writen2(int fd, const void *buf, size_t count)
{
const char *ptr = reinterpret_cast<const char*>(buf);
const char *eptr = ptr + count;
ssize_t res;
while(ptr != eptr) {
res = ::write(fd, ptr, eptr - ptr);
if(res < 0) {
if (errno == EAGAIN)
throw std::runtime_error("used writen2 on non-blocking socket, got EAGAIN");
else
unixDie("failed in writen2");
}
else if (res == 0)
throw std::runtime_error("could not write all bytes, got eof in writen2");
ptr += (size_t) res;
}
return count;
}
size_t readn2(int fd, void* buffer, size_t len)
{
size_t pos=0;
ssize_t res;
for(;;) {
res = read(fd, (char*)buffer + pos, len - pos);
if(res == 0)
throw runtime_error("EOF while reading message");
if(res < 0) {
if (errno == EAGAIN)
throw std::runtime_error("used readn2 on non-blocking socket, got EAGAIN");
else
unixDie("failed in readn2");
}
pos+=(size_t)res;
if(pos == len)
break;
}
return len;
}
size_t readn2WithTimeout(int fd, void* buffer, size_t len, const struct timeval& idleTimeout, const struct timeval& totalTimeout, bool allowIncomplete)
{
size_t pos = 0;
struct timeval start{0,0};
struct timeval remainingTime = totalTimeout;
if (totalTimeout.tv_sec != 0 || totalTimeout.tv_usec != 0) {
gettimeofday(&start, nullptr);
}
do {
ssize_t got = read(fd, (char *)buffer + pos, len - pos);
if (got > 0) {
pos += (size_t) got;
if (allowIncomplete) {
break;
}
}
else if (got == 0) {
throw runtime_error("EOF while reading message");
}
else {
if (errno == EAGAIN) {
struct timeval w = ((totalTimeout.tv_sec == 0 && totalTimeout.tv_usec == 0) || idleTimeout <= remainingTime) ? idleTimeout : remainingTime;
int res = waitForData(fd, w.tv_sec, w.tv_usec);
if (res > 0) {
/* there is data available */
}
else if (res == 0) {
throw runtime_error("Timeout while waiting for data to read");
} else {
throw runtime_error("Error while waiting for data to read");
}
}
else {
unixDie("failed in readn2WithTimeout");
}
}
if (totalTimeout.tv_sec != 0 || totalTimeout.tv_usec != 0) {
struct timeval now;
gettimeofday(&now, nullptr);
struct timeval elapsed = now - start;
if (remainingTime < elapsed) {
throw runtime_error("Timeout while reading data");
}
start = now;
remainingTime = remainingTime - elapsed;
}
}
while (pos < len);
return len;
}
size_t writen2WithTimeout(int fd, const void * buffer, size_t len, const struct timeval& timeout)
{
size_t pos = 0;
do {
ssize_t written = write(fd, reinterpret_cast<const char *>(buffer) + pos, len - pos);
if (written > 0) {
pos += (size_t) written;
}
else if (written == 0)
throw runtime_error("EOF while writing message");
else {
if (errno == EAGAIN) {
int res = waitForRWData(fd, false, timeout.tv_sec, timeout.tv_usec);
if (res > 0) {
/* there is room available */
}
else if (res == 0) {
throw runtime_error("Timeout while waiting to write data");
} else {
throw runtime_error("Error while waiting for room to write data");
}
}
else {
unixDie("failed in write2WithTimeout");
}
}
}
while (pos < len);
return len;
}
string nowTime()
{
time_t now = time(nullptr);
struct tm tm;
localtime_r(&now, &tm);
char buffer[30];
// YYYY-mm-dd HH:MM:SS TZOFF
strftime(buffer, sizeof(buffer), "%F %T %z", &tm);
buffer[sizeof(buffer)-1] = '\0';
return string(buffer);
}
uint16_t getShort(const unsigned char *p)
{
return p[0] * 256 + p[1];
}
uint16_t getShort(const char *p)
{
return getShort((const unsigned char *)p);
}
uint32_t getLong(const unsigned char* p)
{
return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3];
}
uint32_t getLong(const char* p)
{
return getLong(reinterpret_cast<const unsigned char *>(p));
}
static bool ciEqual(const string& a, const string& b)
{
if(a.size()!=b.size())
return false;
string::size_type pos=0, epos=a.size();
for(;pos < epos; ++pos)
if(dns_tolower(a[pos])!=dns_tolower(b[pos]))
return false;
return true;
}
/** does domain end on suffix? Is smart about "wwwds9a.nl" "ds9a.nl" not matching */
static bool endsOn(const string &domain, const string &suffix)
{
if( suffix.empty() || ciEqual(domain, suffix) )
return true;
if(domain.size()<=suffix.size())
return false;
string::size_type dpos=domain.size()-suffix.size()-1, spos=0;
if(domain[dpos++]!='.')
return false;
for(; dpos < domain.size(); ++dpos, ++spos)
if(dns_tolower(domain[dpos]) != dns_tolower(suffix[spos]))
return false;
return true;
}
/** strips a domain suffix from a domain, returns true if it stripped */
bool stripDomainSuffix(string *qname, const string &domain)
{
if(!endsOn(*qname, domain))
return false;
if(toLower(*qname)==toLower(domain))
*qname="@";
else {
if((*qname)[qname->size()-domain.size()-1]!='.')
return false;
qname->resize(qname->size()-domain.size()-1);
}
return true;
}
static void parseService4(const string &descr, ServiceTuple &st)
{
vector<string>parts;
stringtok(parts,descr,":");
if(parts.empty())
throw PDNSException("Unable to parse '"+descr+"' as a service");
st.host=parts[0];
if(parts.size()>1)
st.port=pdns_stou(parts[1]);
}
static void parseService6(const string &descr, ServiceTuple &st)
{
string::size_type pos=descr.find(']');
if(pos == string::npos)
throw PDNSException("Unable to parse '"+descr+"' as an IPv6 service");
st.host=descr.substr(1, pos-1);
if(pos + 2 < descr.length())
st.port=pdns_stou(descr.substr(pos+2));
}
void parseService(const string &descr, ServiceTuple &st)
{
if(descr.empty())
throw PDNSException("Unable to parse '"+descr+"' as a service");
vector<string> parts;
stringtok(parts, descr, ":");
if(descr[0]=='[') {
parseService6(descr, st);
}
else if(descr[0]==':' || parts.size() > 2 || descr.find("::") != string::npos) {
st.host=descr;
}
else {
parseService4(descr, st);
}
}
// returns -1 in case if error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
int waitForData(int fd, int seconds, int useconds)
{
return waitForRWData(fd, true, seconds, useconds);
}
int waitForRWData(int fd, bool waitForRead, int seconds, int useconds, bool* error, bool* disconnected)
{
int ret;
struct pollfd pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.fd = fd;
if(waitForRead)
pfd.events=POLLIN;
else
pfd.events=POLLOUT;
ret = poll(&pfd, 1, seconds * 1000 + useconds/1000);
if (ret > 0) {
if (error && (pfd.revents & POLLERR)) {
*error = true;
}
if (disconnected && (pfd.revents & POLLHUP)) {
*disconnected = true;
}
}
return ret;
}
// returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fdOut) {
set<int> realFDs;
for (const auto& fd : fds) {
if (fd >= 0 && realFDs.count(fd) == 0) {
realFDs.insert(fd);
}
}
std::vector<struct pollfd> pfds(realFDs.size());
memset(pfds.data(), 0, realFDs.size()*sizeof(struct pollfd));
int ctr = 0;
for (const auto& fd : realFDs) {
pfds[ctr].fd = fd;
pfds[ctr].events = POLLIN;
ctr++;
}
int ret;
if(seconds >= 0)
ret = poll(pfds.data(), realFDs.size(), seconds * 1000 + useconds/1000);
else
ret = poll(pfds.data(), realFDs.size(), -1);
if(ret <= 0)
return ret;
set<int> pollinFDs;
for (const auto& pfd : pfds) {
if (pfd.revents & POLLIN) {
pollinFDs.insert(pfd.fd);
}
}
set<int>::const_iterator it(pollinFDs.begin());
advance(it, random() % pollinFDs.size());
*fdOut = *it;
return 1;
}
// returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int*fd)
{
int ret;
struct pollfd pfds[2];
memset(&pfds[0], 0, 2*sizeof(struct pollfd));
pfds[0].fd = fd1;
pfds[1].fd = fd2;
pfds[0].events= pfds[1].events = POLLIN;
int nsocks = 1 + (fd2 >= 0); // fd2 can optionally be -1
if(seconds >= 0)
ret = poll(pfds, nsocks, seconds * 1000 + useconds/1000);
else
ret = poll(pfds, nsocks, -1);
if(!ret || ret < 0)
return ret;
if((pfds[0].revents & POLLIN) && !(pfds[1].revents & POLLIN))
*fd = pfds[0].fd;
else if((pfds[1].revents & POLLIN) && !(pfds[0].revents & POLLIN))
*fd = pfds[1].fd;
else if(ret == 2) {
*fd = pfds[random()%2].fd;
}
else
*fd = -1; // should never happen
return 1;
}
string humanDuration(time_t passed)
{
ostringstream ret;
if(passed<60)
ret<<passed<<" seconds";
else if(passed<3600)
ret<<std::setprecision(2)<<passed/60.0<<" minutes";
else if(passed<86400)
ret<<std::setprecision(3)<<passed/3600.0<<" hours";
else if(passed<(86400*30.41))
ret<<std::setprecision(3)<<passed/86400.0<<" days";
else
ret<<std::setprecision(3)<<passed/(86400*30.41)<<" months";
return ret.str();
}
const string unquotify(const string &item)
{
if(item.size()<2)
return item;
string::size_type bpos=0, epos=item.size();
if(item[0]=='"')
bpos=1;
if(item[epos-1]=='"')
epos-=1;
return item.substr(bpos,epos-bpos);
}
void stripLine(string &line)
{
string::size_type pos=line.find_first_of("\r\n");
if(pos!=string::npos) {
line.resize(pos);
}
}
string urlEncode(const string &text)
{
string ret;
for(char i : text)
if(i==' ')ret.append("%20");
else ret.append(1,i);
return ret;
}
string getHostname()
{
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 255
#endif
char tmp[MAXHOSTNAMELEN];
if(gethostname(tmp, MAXHOSTNAMELEN))
return "UNKNOWN";
return string(tmp);
}
string itoa(int i)
{
ostringstream o;
o<<i;
return o.str();
}
string uitoa(unsigned int i) // MSVC 6 doesn't grok overloading (un)signed
{
ostringstream o;
o<<i;
return o.str();
}
string bitFlip(const string &str)
{
string::size_type pos=0, epos=str.size();
string ret;
ret.reserve(epos);
for(;pos < epos; ++pos)
ret.append(1, ~str[pos]);
return ret;
}
string stringerror(int err)
{
return strerror(err);
}
string stringerror()
{
return strerror(errno);
}
void cleanSlashes(string &str)
{
string::const_iterator i;
string out;
for(i=str.begin();i!=str.end();++i) {
if(*i=='/' && i!=str.begin() && *(i-1)=='/')
continue;
out.append(1,*i);
}
str=out;
}
bool IpToU32(const string &str, uint32_t *ip)
{
if(str.empty()) {
*ip=0;
return true;
}
struct in_addr inp;
if(inet_aton(str.c_str(), &inp)) {
*ip=inp.s_addr;
return true;
}
return false;
}
string U32ToIP(uint32_t val)
{
char tmp[17];
snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
(val >> 24)&0xff,
(val >> 16)&0xff,
(val >> 8)&0xff,
(val )&0xff);
return string(tmp);
}
string makeHexDump(const string& str)
{
char tmp[5];
string ret;
ret.reserve((int)(str.size()*2.2));
for(char n : str) {
snprintf(tmp, sizeof(tmp), "%02x ", (unsigned char)n);
ret+=tmp;
}
return ret;
}
string makeBytesFromHex(const string &in) {
if (in.size() % 2 != 0) {
throw std::range_error("odd number of bytes in hex string");
}
string ret;
ret.reserve(in.size());
unsigned int num;
for (size_t i = 0; i < in.size(); i+=2) {
string numStr = in.substr(i, 2);
num = 0;
sscanf(numStr.c_str(), "%02x", &num);
ret.push_back((uint8_t)num);
}
return ret;
}
void normalizeTV(struct timeval& tv)
{
if(tv.tv_usec > 1000000) {
++tv.tv_sec;
tv.tv_usec-=1000000;
}
else if(tv.tv_usec < 0) {
--tv.tv_sec;
tv.tv_usec+=1000000;
}
}
const struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs)
{
struct timeval ret;
ret.tv_sec=lhs.tv_sec + rhs.tv_sec;
ret.tv_usec=lhs.tv_usec + rhs.tv_usec;
normalizeTV(ret);
return ret;
}
const struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs)
{
struct timeval ret;
ret.tv_sec=lhs.tv_sec - rhs.tv_sec;
ret.tv_usec=lhs.tv_usec - rhs.tv_usec;
normalizeTV(ret);
return ret;
}
pair<string, string> splitField(const string& inp, char sepa)
{
pair<string, string> ret;
string::size_type cpos=inp.find(sepa);
if(cpos==string::npos)
ret.first=inp;
else {
ret.first=inp.substr(0, cpos);
ret.second=inp.substr(cpos+1);
}
return ret;
}
int logFacilityToLOG(unsigned int facility)
{
switch(facility) {
case 0:
return LOG_LOCAL0;
case 1:
return(LOG_LOCAL1);
case 2:
return(LOG_LOCAL2);
case 3:
return(LOG_LOCAL3);
case 4:
return(LOG_LOCAL4);
case 5:
return(LOG_LOCAL5);
case 6:
return(LOG_LOCAL6);
case 7:
return(LOG_LOCAL7);
default:
return -1;
}
}
string stripDot(const string& dom)
{
if(dom.empty())
return dom;
if(dom[dom.size()-1]!='.')
return dom;
return dom.substr(0,dom.size()-1);
}
int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret)
{
if(addr.empty())
return -1;
string ourAddr(addr);
bool portSet = false;
unsigned int port;
if(addr[0]=='[') { // [::]:53 style address
string::size_type pos = addr.find(']');
if(pos == string::npos)
return -1;
ourAddr.assign(addr.c_str() + 1, pos-1);
if (pos + 1 != addr.size()) { // complete after ], no port specified
if (pos + 2 > addr.size() || addr[pos+1]!=':')
return -1;
try {
port = pdns_stou(addr.substr(pos+2));
portSet = true;
}
catch(const std::out_of_range&) {
return -1;
}
}
}
ret->sin6_scope_id=0;
ret->sin6_family=AF_INET6;
if(inet_pton(AF_INET6, ourAddr.c_str(), (void*)&ret->sin6_addr) != 1) {
struct addrinfo* res;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_flags = AI_NUMERICHOST;
// getaddrinfo has anomalous return codes, anything nonzero is an error, positive or negative
if (getaddrinfo(ourAddr.c_str(), nullptr, &hints, &res) != 0) {
return -1;
}
memcpy(ret, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
}
if(portSet) {
if(port > 65535)
return -1;
ret->sin6_port = htons(port);
}
return 0;
}
int makeIPv4sockaddr(const std::string& str, struct sockaddr_in* ret)
{
if(str.empty()) {
return -1;
}
struct in_addr inp;
string::size_type pos = str.find(':');
if(pos == string::npos) { // no port specified, not touching the port
if(inet_aton(str.c_str(), &inp)) {
ret->sin_addr.s_addr=inp.s_addr;
return 0;
}
return -1;
}
if(!*(str.c_str() + pos + 1)) // trailing :
return -1;
char *eptr = const_cast<char*>(str.c_str()) + str.size();
int port = strtol(str.c_str() + pos + 1, &eptr, 10);
if (port < 0 || port > 65535)
return -1;
if(*eptr)
return -1;
ret->sin_port = htons(port);
if(inet_aton(str.substr(0, pos).c_str(), &inp)) {
ret->sin_addr.s_addr=inp.s_addr;
return 0;
}
return -1;
}
int makeUNsockaddr(const std::string& path, struct sockaddr_un* ret)
{
if (path.empty())
return -1;
memset(ret, 0, sizeof(struct sockaddr_un));
ret->sun_family = AF_UNIX;
if (path.length() >= sizeof(ret->sun_path))
return -1;
path.copy(ret->sun_path, sizeof(ret->sun_path), 0);
return 0;
}
//! read a line of text from a FILE* to a std::string, returns false on 'no data'
bool stringfgets(FILE* fp, std::string& line)
{
char buffer[1024];
line.clear();
do {
if(!fgets(buffer, sizeof(buffer), fp))
return !line.empty();
line.append(buffer);
} while(!strchr(buffer, '\n'));
return true;
}
bool readFileIfThere(const char* fname, std::string* line)
{
line->clear();
auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname, "r"), fclose);
if(!fp)
return false;
stringfgets(fp.get(), *line);
fp.reset();
return true;
}
Regex::Regex(const string &expr)
{
if(regcomp(&d_preg, expr.c_str(), REG_ICASE|REG_NOSUB|REG_EXTENDED))
throw PDNSException("Regular expression did not compile");
}
// if you end up here because valgrind told you were are doing something wrong
// with msgh->msg_controllen, please refer to https://github.com/PowerDNS/pdns/pull/3962
// first.
// Note that cmsgbuf should be aligned the same as a struct cmsghdr
void addCMsgSrcAddr(struct msghdr* msgh, cmsgbuf_aligned* cmsgbuf, const ComboAddress* source, int itfIndex)
{
struct cmsghdr *cmsg = nullptr;
if(source->sin4.sin_family == AF_INET6) {
struct in6_pktinfo *pkt;
msgh->msg_control = cmsgbuf;
#if !defined( __APPLE__ )
/* CMSG_SPACE is not a constexpr on macOS */
static_assert(CMSG_SPACE(sizeof(*pkt)) <= sizeof(*cmsgbuf), "Buffer is too small for in6_pktinfo");
#else /* __APPLE__ */
if (CMSG_SPACE(sizeof(*pkt)) > sizeof(*cmsgbuf)) {
throw std::runtime_error("Buffer is too small for in6_pktinfo");
}
#endif /* __APPLE__ */
msgh->msg_controllen = CMSG_SPACE(sizeof(*pkt));
cmsg = CMSG_FIRSTHDR(msgh);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
pkt = (struct in6_pktinfo *) CMSG_DATA(cmsg);
// Include the padding to stop valgrind complaining about passing uninitialized data
memset(pkt, 0, CMSG_SPACE(sizeof(*pkt)));
pkt->ipi6_addr = source->sin6.sin6_addr;
pkt->ipi6_ifindex = itfIndex;
}
else {
#if defined(IP_PKTINFO)
struct in_pktinfo *pkt;
msgh->msg_control = cmsgbuf;
#if !defined( __APPLE__ )
/* CMSG_SPACE is not a constexpr on macOS */
static_assert(CMSG_SPACE(sizeof(*pkt)) <= sizeof(*cmsgbuf), "Buffer is too small for in_pktinfo");
#else /* __APPLE__ */
if (CMSG_SPACE(sizeof(*pkt)) > sizeof(*cmsgbuf)) {
throw std::runtime_error("Buffer is too small for in_pktinfo");
}
#endif /* __APPLE__ */
msgh->msg_controllen = CMSG_SPACE(sizeof(*pkt));
cmsg = CMSG_FIRSTHDR(msgh);
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
pkt = (struct in_pktinfo *) CMSG_DATA(cmsg);
// Include the padding to stop valgrind complaining about passing uninitialized data
memset(pkt, 0, CMSG_SPACE(sizeof(*pkt)));
pkt->ipi_spec_dst = source->sin4.sin_addr;
pkt->ipi_ifindex = itfIndex;
#elif defined(IP_SENDSRCADDR)
struct in_addr *in;
msgh->msg_control = cmsgbuf;
#if !defined( __APPLE__ )
static_assert(CMSG_SPACE(sizeof(*in)) <= sizeof(*cmsgbuf), "Buffer is too small for in_addr");
#else /* __APPLE__ */
if (CMSG_SPACE(sizeof(*in)) > sizeof(*cmsgbuf)) {
throw std::runtime_error("Buffer is too small for in_addr");
}
#endif /* __APPLE__ */
msgh->msg_controllen = CMSG_SPACE(sizeof(*in));
cmsg = CMSG_FIRSTHDR(msgh);
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_SENDSRCADDR;
cmsg->cmsg_len = CMSG_LEN(sizeof(*in));
// Include the padding to stop valgrind complaining about passing uninitialized data
in = (struct in_addr *) CMSG_DATA(cmsg);
memset(in, 0, CMSG_SPACE(sizeof(*in)));
*in = source->sin4.sin_addr;
#endif
}
}
unsigned int getFilenumLimit(bool hardOrSoft)
{
struct rlimit rlim;
if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
unixDie("Requesting number of available file descriptors");
return hardOrSoft ? rlim.rlim_max : rlim.rlim_cur;
}
void setFilenumLimit(unsigned int lim)
{
struct rlimit rlim;
if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
unixDie("Requesting number of available file descriptors");
rlim.rlim_cur=lim;
if(setrlimit(RLIMIT_NOFILE, &rlim) < 0)
unixDie("Setting number of available file descriptors");
}
#define burtlemix(a,b,c) \
{ \
a -= b; a -= c; a ^= (c>>13); \
b -= c; b -= a; b ^= (a<<8); \
c -= a; c -= b; c ^= (b>>13); \
a -= b; a -= c; a ^= (c>>12); \
b -= c; b -= a; b ^= (a<<16); \
c -= a; c -= b; c ^= (b>>5); \
a -= b; a -= c; a ^= (c>>3); \
b -= c; b -= a; b ^= (a<<10); \
c -= a; c -= b; c ^= (b>>15); \
}
uint32_t burtle(const unsigned char* k, uint32_t length, uint32_t initval)
{
uint32_t a,b,c,len;
/* Set up the internal state */
len = length;
a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
c = initval; /* the previous hash value */
/*---------------------------------------- handle most of the key */
while (len >= 12) {
a += (k[0] +((uint32_t)k[1]<<8) +((uint32_t)k[2]<<16) +((uint32_t)k[3]<<24));
b += (k[4] +((uint32_t)k[5]<<8) +((uint32_t)k[6]<<16) +((uint32_t)k[7]<<24));
c += (k[8] +((uint32_t)k[9]<<8) +((uint32_t)k[10]<<16)+((uint32_t)k[11]<<24));
burtlemix(a,b,c);
k += 12; len -= 12;
}
/*------------------------------------- handle the last 11 bytes */
c += length;
switch(len) { /* all the case statements fall through */
case 11: c+=((uint32_t)k[10]<<24);
/* fall-through */
case 10: c+=((uint32_t)k[9]<<16);
/* fall-through */
case 9 : c+=((uint32_t)k[8]<<8);
/* the first byte of c is reserved for the length */
/* fall-through */
case 8 : b+=((uint32_t)k[7]<<24);
/* fall-through */
case 7 : b+=((uint32_t)k[6]<<16);
/* fall-through */
case 6 : b+=((uint32_t)k[5]<<8);
/* fall-through */
case 5 : b+=k[4];
/* fall-through */
case 4 : a+=((uint32_t)k[3]<<24);
/* fall-through */
case 3 : a+=((uint32_t)k[2]<<16);
/* fall-through */
case 2 : a+=((uint32_t)k[1]<<8);
/* fall-through */
case 1 : a+=k[0];
/* case 0: nothing left to add */
}
burtlemix(a,b,c);
/*-------------------------------------------- report the result */
return c;
}
uint32_t burtleCI(const unsigned char* k, uint32_t length, uint32_t initval)
{
uint32_t a,b,c,len;
/* Set up the internal state */
len = length;
a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
c = initval; /* the previous hash value */
/*---------------------------------------- handle most of the key */
while (len >= 12) {
a += (dns_tolower(k[0]) +((uint32_t)dns_tolower(k[1])<<8) +((uint32_t)dns_tolower(k[2])<<16) +((uint32_t)dns_tolower(k[3])<<24));
b += (dns_tolower(k[4]) +((uint32_t)dns_tolower(k[5])<<8) +((uint32_t)dns_tolower(k[6])<<16) +((uint32_t)dns_tolower(k[7])<<24));
c += (dns_tolower(k[8]) +((uint32_t)dns_tolower(k[9])<<8) +((uint32_t)dns_tolower(k[10])<<16)+((uint32_t)dns_tolower(k[11])<<24));
burtlemix(a,b,c);
k += 12; len -= 12;
}
/*------------------------------------- handle the last 11 bytes */
c += length;
switch(len) { /* all the case statements fall through */
case 11: c+=((uint32_t)dns_tolower(k[10])<<24);
/* fall-through */
case 10: c+=((uint32_t)dns_tolower(k[9])<<16);
/* fall-through */
case 9 : c+=((uint32_t)dns_tolower(k[8])<<8);
/* the first byte of c is reserved for the length */
/* fall-through */
case 8 : b+=((uint32_t)dns_tolower(k[7])<<24);
/* fall-through */
case 7 : b+=((uint32_t)dns_tolower(k[6])<<16);
/* fall-through */
case 6 : b+=((uint32_t)dns_tolower(k[5])<<8);
/* fall-through */
case 5 : b+=dns_tolower(k[4]);
/* fall-through */
case 4 : a+=((uint32_t)dns_tolower(k[3])<<24);
/* fall-through */
case 3 : a+=((uint32_t)dns_tolower(k[2])<<16);
/* fall-through */
case 2 : a+=((uint32_t)dns_tolower(k[1])<<8);
/* fall-through */
case 1 : a+=dns_tolower(k[0]);
/* case 0: nothing left to add */
}
burtlemix(a,b,c);
/*-------------------------------------------- report the result */
return c;
}
bool setSocketTimestamps(int fd)
{
#ifdef SO_TIMESTAMP
int on=1;
return setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, (char*)&on, sizeof(on)) == 0;
#else
return true; // we pretend this happened.
#endif
}
bool setTCPNoDelay(int sock)
{
int flag = 1;
return setsockopt(sock, /* socket affected */
IPPROTO_TCP, /* set option at TCP level */
TCP_NODELAY, /* name of option */
(char *) &flag, /* the cast is historical cruft */
sizeof(flag)) == 0; /* length of option value */
}
bool setNonBlocking(int sock)
{
int flags=fcntl(sock,F_GETFL,0);
if(flags<0 || fcntl(sock, F_SETFL,flags|O_NONBLOCK) <0)
return false;
return true;
}
bool setBlocking(int sock)
{
int flags=fcntl(sock,F_GETFL,0);
if(flags<0 || fcntl(sock, F_SETFL,flags&(~O_NONBLOCK)) <0)
return false;
return true;
}
bool setReuseAddr(int sock)
{
int tmp = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&tmp, static_cast<unsigned>(sizeof tmp))<0)
throw PDNSException(string("Setsockopt failed: ")+stringerror());
return true;
}
bool isNonBlocking(int sock)
{
int flags=fcntl(sock,F_GETFL,0);
return flags & O_NONBLOCK;
}
bool setReceiveSocketErrors(int sock, int af)
{
#ifdef __linux__
int tmp = 1, ret;
if (af == AF_INET) {
ret = setsockopt(sock, IPPROTO_IP, IP_RECVERR, &tmp, sizeof(tmp));
} else {
ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVERR, &tmp, sizeof(tmp));
}
if (ret < 0) {
throw PDNSException(string("Setsockopt failed: ") + stringerror());
}
#endif
return true;
}
// Closes a socket.
int closesocket( int socket )
{
int ret=::close(socket);
if(ret < 0 && errno == ECONNRESET) // see ticket 192, odd BSD behaviour
return 0;
if(ret < 0)
throw PDNSException("Error closing socket: "+stringerror());
return ret;
}
bool setCloseOnExec(int sock)
{
int flags=fcntl(sock,F_GETFD,0);
if(flags<0 || fcntl(sock, F_SETFD,flags|FD_CLOEXEC) <0)
return false;
return true;
}
string getMACAddress(const ComboAddress& ca)
{
string ret;
#ifdef __linux__
ifstream ifs("/proc/net/arp");
if(!ifs)
return ret;
string line;
string match=ca.toString()+' ';
while(getline(ifs, line)) {
if(boost::starts_with(line, match)) {
vector<string> parts;
stringtok(parts, line, " \n\t\r");
if(parts.size() < 4)
return ret;
unsigned int tmp[6];
if (sscanf(parts[3].c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5) != 6) {
return ret;
}
for(unsigned int i : tmp)
ret.append(1, (char)i);
return ret;
}
}
#endif
return ret;
}
uint64_t udpErrorStats(const std::string& str)
{
#ifdef __linux__
ifstream ifs("/proc/net/snmp");
if (!ifs) {
return 0;
}
string line;
while (getline(ifs, line)) {
if (boost::starts_with(line, "Udp: ") && isdigit(line.at(5))) {
vector<string> parts;
stringtok(parts, line, " \n\t\r");
if (parts.size() < 7) {
break;
}
if (str == "udp-rcvbuf-errors") {
return std::stoull(parts.at(5));
}
else if (str == "udp-sndbuf-errors") {
return std::stoull(parts.at(6));
}
else if (str == "udp-noport-errors") {
return std::stoull(parts.at(2));
}
else if (str == "udp-in-errors") {
return std::stoull(parts.at(3));
}
else if (parts.size() >= 8 && str == "udp-in-csum-errors") {
return std::stoull(parts.at(7));
}
else {
return 0;
}
}
}
#endif
return 0;
}
uint64_t udp6ErrorStats(const std::string& str)
{
#ifdef __linux__
const std::map<std::string, std::string> keys = {
{ "udp6-in-errors", "Udp6InErrors" },
{ "udp6-recvbuf-errors", "Udp6RcvbufErrors" },
{ "udp6-sndbuf-errors", "Udp6SndbufErrors" },
{ "udp6-noport-errors", "Udp6NoPorts" },
{ "udp6-in-csum-errors", "Udp6InCsumErrors" }
};
auto key = keys.find(str);
if (key == keys.end()) {
return 0;
}
ifstream ifs("/proc/net/snmp6");
if (!ifs) {
return 0;
}
std::string line;
while (getline(ifs, line)) {
if (!boost::starts_with(line, key->second)) {
continue;
}
std::vector<std::string> parts;
stringtok(parts, line, " \n\t\r");
if (parts.size() != 2) {
return 0;
}
return std::stoull(parts.at(1));
}
#endif
return 0;
}
uint64_t tcpErrorStats(const std::string& str)
{
#ifdef __linux__
ifstream ifs("/proc/net/netstat");
if (!ifs) {
return 0;
}
string line;
vector<string> parts;
while (getline(ifs,line)) {
if (line.size() > 9 && boost::starts_with(line, "TcpExt: ") && isdigit(line.at(8))) {
stringtok(parts, line, " \n\t\r");
if (parts.size() < 21) {
break;
}
return std::stoull(parts.at(20));
}
}
#endif
return 0;
}
uint64_t getCPUIOWait(const std::string& str)
{
#ifdef __linux__
ifstream ifs("/proc/stat");
if (!ifs) {
return 0;
}
string line;
vector<string> parts;
while (getline(ifs, line)) {
if (boost::starts_with(line, "cpu ")) {
stringtok(parts, line, " \n\t\r");
if (parts.size() < 6) {
break;
}
return std::stoull(parts[5]);
}
}
#endif
return 0;
}
uint64_t getCPUSteal(const std::string& str)
{
#ifdef __linux__
ifstream ifs("/proc/stat");
if (!ifs) {
return 0;
}
string line;
vector<string> parts;
while (getline(ifs, line)) {
if (boost::starts_with(line, "cpu ")) {
stringtok(parts, line, " \n\t\r");
if (parts.size() < 9) {
break;
}
return std::stoull(parts[8]);
}
}
#endif
return 0;
}
bool getTSIGHashEnum(const DNSName& algoName, TSIGHashEnum& algoEnum)
{
if (algoName == DNSName("hmac-md5.sig-alg.reg.int") || algoName == DNSName("hmac-md5"))
algoEnum = TSIG_MD5;
else if (algoName == DNSName("hmac-sha1"))
algoEnum = TSIG_SHA1;
else if (algoName == DNSName("hmac-sha224"))
algoEnum = TSIG_SHA224;
else if (algoName == DNSName("hmac-sha256"))
algoEnum = TSIG_SHA256;
else if (algoName == DNSName("hmac-sha384"))
algoEnum = TSIG_SHA384;
else if (algoName == DNSName("hmac-sha512"))
algoEnum = TSIG_SHA512;
else if (algoName == DNSName("gss-tsig"))
algoEnum = TSIG_GSS;
else {
return false;
}
return true;
}
DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum)
{
switch(algoEnum) {
case TSIG_MD5: return DNSName("hmac-md5.sig-alg.reg.int.");
case TSIG_SHA1: return DNSName("hmac-sha1.");
case TSIG_SHA224: return DNSName("hmac-sha224.");
case TSIG_SHA256: return DNSName("hmac-sha256.");
case TSIG_SHA384: return DNSName("hmac-sha384.");
case TSIG_SHA512: return DNSName("hmac-sha512.");
case TSIG_GSS: return DNSName("gss-tsig.");
}
throw PDNSException("getTSIGAlgoName does not understand given algorithm, please fix!");
}
uint64_t getOpenFileDescriptors(const std::string&)
{
#ifdef __linux__
DIR* dirhdl=opendir(("/proc/"+std::to_string(getpid())+"/fd/").c_str());
if(!dirhdl)
return 0;
struct dirent *entry;
int ret=0;
while((entry = readdir(dirhdl))) {
uint32_t num;
try {
num = pdns_stou(entry->d_name);
} catch (...) {
continue; // was not a number.
}
if(std::to_string(num) == entry->d_name)
ret++;
}
closedir(dirhdl);
return ret;
#elif defined(__OpenBSD__)
// FreeBSD also has this in libopenbsd, but I don't know if that's available always
return getdtablecount();
#else
return 0;
#endif
}
uint64_t getRealMemoryUsage(const std::string&)
{
#ifdef __linux__
ifstream ifs("/proc/self/statm");
if(!ifs)
return 0;
uint64_t size, resident, shared, text, lib, data;
ifs >> size >> resident >> shared >> text >> lib >> data;
// We used to use "data" here, but it proves unreliable and even is marked "broken"
// in https://www.kernel.org/doc/html/latest/filesystems/proc.html
return resident * getpagesize();
#else
struct rusage ru;
if (getrusage(RUSAGE_SELF, &ru) != 0)
return 0;
return ru.ru_maxrss * 1024;
#endif
}
uint64_t getSpecialMemoryUsage(const std::string&)
{
#ifdef __linux__
ifstream ifs("/proc/self/smaps");
if(!ifs)
return 0;
string line;
uint64_t bytes=0;
string header("Private_Dirty:");
while(getline(ifs, line)) {
if(boost::starts_with(line, header)) {
bytes += std::stoull(line.substr(header.length() + 1))*1024;
}
}
return bytes;
#else
return 0;
#endif
}
uint64_t getCPUTimeUser(const std::string&)
{
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
return (ru.ru_utime.tv_sec*1000ULL + ru.ru_utime.tv_usec/1000);
}
uint64_t getCPUTimeSystem(const std::string&)
{
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
return (ru.ru_stime.tv_sec*1000ULL + ru.ru_stime.tv_usec/1000);
}
double DiffTime(const struct timespec& first, const struct timespec& second)
{
int seconds=second.tv_sec - first.tv_sec;
int nseconds=second.tv_nsec - first.tv_nsec;
if(nseconds < 0) {
seconds-=1;
nseconds+=1000000000;
}
return seconds + nseconds/1000000000.0;
}
double DiffTime(const struct timeval& first, const struct timeval& second)
{
int seconds=second.tv_sec - first.tv_sec;
int useconds=second.tv_usec - first.tv_usec;
if(useconds < 0) {
seconds-=1;
useconds+=1000000;
}
return seconds + useconds/1000000.0;
}
uid_t strToUID(const string &str)
{
uid_t result = 0;
const char * cstr = str.c_str();
struct passwd * pwd = getpwnam(cstr);
if (pwd == nullptr) {
long long val;
try {
val = stoll(str);
}
catch(std::exception& e) {
throw runtime_error((boost::format("Error: Unable to parse user ID %s") % cstr).str() );
}
if (val < std::numeric_limits<uid_t>::min() || val > std::numeric_limits<uid_t>::max()) {
throw runtime_error((boost::format("Error: Unable to parse user ID %s") % cstr).str() );
}
result = static_cast<uid_t>(val);
}
else {
result = pwd->pw_uid;
}
return result;
}
gid_t strToGID(const string &str)
{
gid_t result = 0;
const char * cstr = str.c_str();
struct group * grp = getgrnam(cstr);
if (grp == nullptr) {
long long val;
try {
val = stoll(str);
}
catch(std::exception& e) {
throw runtime_error((boost::format("Error: Unable to parse group ID %s") % cstr).str() );
}
if (val < std::numeric_limits<gid_t>::min() || val > std::numeric_limits<gid_t>::max()) {
throw runtime_error((boost::format("Error: Unable to parse group ID %s") % cstr).str() );
}
result = static_cast<gid_t>(val);
}
else {
result = grp->gr_gid;
}
return result;
}
unsigned int pdns_stou(const std::string& str, size_t * idx, int base)
{
if (str.empty()) return 0; // compatibility
unsigned long result;
try {
result = std::stoul(str, idx, base);
}
catch(std::invalid_argument& e) {
throw std::invalid_argument(string(e.what()) + "; (invalid argument during std::stoul); data was \""+str+"\"");
}
catch(std::out_of_range& e) {
throw std::out_of_range(string(e.what()) + "; (out of range during std::stoul); data was \""+str+"\"");
}
if (result > std::numeric_limits<unsigned int>::max()) {
throw std::out_of_range("stoul returned result out of unsigned int range; data was \""+str+"\"");
}
return static_cast<unsigned int>(result);
}
bool isSettingThreadCPUAffinitySupported()
{
#ifdef HAVE_PTHREAD_SETAFFINITY_NP
return true;
#else
return false;
#endif
}
int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus)
{
#ifdef HAVE_PTHREAD_SETAFFINITY_NP
# ifdef __NetBSD__
cpuset_t *cpuset;
cpuset = cpuset_create();
for (const auto cpuID : cpus) {
cpuset_set(cpuID, cpuset);
}
return pthread_setaffinity_np(tid,
cpuset_size(cpuset),
cpuset);
# else
# ifdef __FreeBSD__
# define cpu_set_t cpuset_t
# endif
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
for (const auto cpuID : cpus) {
CPU_SET(cpuID, &cpuset);
}
return pthread_setaffinity_np(tid,
sizeof(cpuset),
&cpuset);
# endif
#else
return ENOSYS;
#endif /* HAVE_PTHREAD_SETAFFINITY_NP */
}
std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath)
{
std::vector<ComboAddress> results;
ifstream ifs(resolvConfPath);
if (!ifs) {
return results;
}
string line;
while(std::getline(ifs, line)) {
boost::trim_right_if(line, boost::is_any_of(" \r\n\x1a"));
boost::trim_left(line); // leading spaces, let's be nice
string::size_type tpos = line.find_first_of(";#");
if (tpos != string::npos) {
line.resize(tpos);
}
if (boost::starts_with(line, "nameserver ") || boost::starts_with(line, "nameserver\t")) {
vector<string> parts;
stringtok(parts, line, " \t,"); // be REALLY nice
for(vector<string>::const_iterator iter = parts.begin() + 1; iter != parts.end(); ++iter) {
try {
results.emplace_back(*iter, 53);
}
catch(...)
{
}
}
}
}
return results;
}
size_t getPipeBufferSize(int fd)
{
#ifdef F_GETPIPE_SZ
int res = fcntl(fd, F_GETPIPE_SZ);
if (res == -1) {
return 0;
}
return res;
#else
errno = ENOSYS;
return 0;
#endif /* F_GETPIPE_SZ */
}
bool setPipeBufferSize(int fd, size_t size)
{
#ifdef F_SETPIPE_SZ
if (size > static_cast<size_t>(std::numeric_limits<int>::max())) {
errno = EINVAL;
return false;
}
int newSize = static_cast<int>(size);
int res = fcntl(fd, F_SETPIPE_SZ, newSize);
if (res == -1) {
return false;
}
return true;
#else
errno = ENOSYS;
return false;
#endif /* F_SETPIPE_SZ */
}
DNSName reverseNameFromIP(const ComboAddress& ip)
{
if (ip.isIPv4()) {
std::string result("in-addr.arpa.");
auto ptr = reinterpret_cast<const uint8_t*>(&ip.sin4.sin_addr.s_addr);
for (size_t idx = 0; idx < sizeof(ip.sin4.sin_addr.s_addr); idx++) {
result = std::to_string(ptr[idx]) + "." + result;
}
return DNSName(result);
}
else if (ip.isIPv6()) {
std::string result("ip6.arpa.");
auto ptr = reinterpret_cast<const uint8_t*>(&ip.sin6.sin6_addr.s6_addr[0]);
for (size_t idx = 0; idx < sizeof(ip.sin6.sin6_addr.s6_addr); idx++) {
std::stringstream stream;
stream << std::hex << (ptr[idx] & 0x0F);
stream << '.';
stream << std::hex << (((ptr[idx]) >> 4) & 0x0F);
stream << '.';
result = stream.str() + result;
}
return DNSName(result);
}
throw std::runtime_error("Calling reverseNameFromIP() for an address which is neither an IPv4 nor an IPv6");
}
static size_t getMaxHostNameSize()
{
#if defined(HOST_NAME_MAX)
return HOST_NAME_MAX;
#endif
#if defined(_SC_HOST_NAME_MAX)
auto tmp = sysconf(_SC_HOST_NAME_MAX);
if (tmp != -1) {
return tmp;
}
#endif
/* _POSIX_HOST_NAME_MAX */
return 255;
}
std::string getCarbonHostName()
{
std::string hostname;
hostname.resize(getMaxHostNameSize() + 1, 0);
if (gethostname(const_cast<char*>(hostname.c_str()), hostname.size()) != 0) {
throw std::runtime_error(stringerror());
}
boost::replace_all(hostname, ".", "_");
hostname.resize(strlen(hostname.c_str()));
return hostname;
}
std::string makeLuaString(const std::string& in)
{
ostringstream str;
str<<'"';
char item[5];
for (unsigned char n : in) {
if (islower(n) || isupper(n)) {
item[0] = n;
item[1] = 0;
}
else {
snprintf(item, sizeof(item), "\\%03d", n);
}
str << item;
}
str<<'"';
return str.str();
}
size_t parseSVCBValueList(const std::string &in, vector<std::string> &val) {
std::string parsed;
auto ret = parseRFC1035CharString(in, parsed);
parseSVCBValueListFromParsedRFC1035CharString(parsed, val);
return ret;
};
#ifdef HAVE_CRYPTO_MEMCMP
#include <openssl/crypto.h>
#else /* HAVE_CRYPTO_MEMCMP */
#ifdef HAVE_SODIUM_MEMCMP
#include <sodium.h>
#endif /* HAVE_SODIUM_MEMCMP */
#endif /* HAVE_CRYPTO_MEMCMP */
bool constantTimeStringEquals(const std::string& a, const std::string& b)
{
if (a.size() != b.size()) {
return false;
}
const size_t size = a.size();
#ifdef HAVE_CRYPTO_MEMCMP
return CRYPTO_memcmp(a.c_str(), b.c_str(), size) == 0;
#else /* HAVE_CRYPTO_MEMCMP */
#ifdef HAVE_SODIUM_MEMCMP
return sodium_memcmp(a.c_str(), b.c_str(), size) == 0;
#else /* HAVE_SODIUM_MEMCMP */
const volatile unsigned char *_a = (const volatile unsigned char *) a.c_str();
const volatile unsigned char *_b = (const volatile unsigned char *) b.c_str();
unsigned char res = 0;
for (size_t idx = 0; idx < size; idx++) {
res |= _a[idx] ^ _b[idx];
}
return res == 0;
#endif /* !HAVE_SODIUM_MEMCMP */
#endif /* !HAVE_CRYPTO_MEMCMP */
}
|