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
77
78
79
80
81
82
83
86
87
88
89
90
91
100
101
102
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
133
134
141
142
146
147
148
149
156
157
162
163
164
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
189
195
196
197
199
207
208
209
210
211
212
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
262
263
264
266
267
268
269
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
314
315
316
317
318
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
360
361
371
372
373
374
375
376
377
378
379
380
381
389
390
394
395
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
454
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
482
483
484
485
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
540
541
542
543
544
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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
610
611
615
616
617
618
619
620
621
622
623
624
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
662
663
664
668
669
670
671
674
677
681
682
683
684
685
686
687
688
691
692
693
697
698
699
700
707
708
709
710
711
712
713
714
715
718
719
729
730
733
734
735
738
739
740
741
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
799
800
804
805
806
807
808
809
810
811
812
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
836
837
838
839
840
843
844
850
851
852
853
854
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
876
885
889
890
891
892
893
899
900
901
902
903
904
905
910
918
920
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
960
961
962
970
974
975
976
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1016
1017
1018
1019
1020
1023
1024
1025
1026
1027
1028
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
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1094
1095
1096
1097
1101
1102
1103
1111
1114
1117
1118
1119
1120
1123
1124
1132
1133
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1152
1153
1154
1164
1165
1166
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1202
1203
1204
1205
1206
1210
1214
1215
1216
1217
1218
1225
1226
1228
1229
1233
1234
1235
1238
1244
1245
1248
1249
1250
1251
1252
1253
1254
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1274
1275
1276
1279
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1329
1330
1331
1332
1333
1334
1335
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
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1423
1424
1431
1432
1433
1434
1445
1446
1447
1461
1462
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
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1556
1560
1561
1565
1566
1570
1571
1572
1573
1578
1579
1580
1589
1590
1593
1594
1595
1596
1597
1602
1605
1606
1607
1608
1609
1610
1611
1618
1619
1625
1626
1627
1628
1629
1630
1631
/* ... */
/* ... */
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_DNS
#include "lwip/def.h"
#include "lwip/udp.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/dns.h"
#include "lwip/prot/dns.h"
#include <string.h>
7 includes
#ifndef DNS_RAND_TXID
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_XID) != 0)
#define DNS_RAND_TXID LWIP_RAND
#else
static u16_t dns_txid;
#define DNS_RAND_TXID() (++dns_txid)
/* ... */#endif/* ... */
#endif
#ifndef DNS_PORT_ALLOWED
#define DNS_PORT_ALLOWED(port) ((port) >= 1024)
#endif
#ifndef DNS_MAX_TTL
#define DNS_MAX_TTL 604800
#elif DNS_MAX_TTL > 0x7FFFFFFF
#error DNS_MAX_TTL must be a positive 32-bit value
#endif
#if DNS_TABLE_SIZE > 255
#error DNS_TABLE_SIZE must fit into an u8_t
#endif
#if DNS_MAX_SERVERS > 255
#error DNS_MAX_SERVERS must fit into an u8_t
#endif
/* ... */
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
#ifndef DNS_MAX_REQUESTS
#define DNS_MAX_REQUESTS DNS_TABLE_SIZE
#else
#if DNS_MAX_REQUESTS > 255
#error DNS_MAX_REQUESTS must fit into an u8_t
#endif/* ... */
#endif/* ... */
#else
/* ... */
#define DNS_MAX_REQUESTS DNS_TABLE_SIZE
/* ... */#endif
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
#ifndef DNS_MAX_SOURCE_PORTS
#define DNS_MAX_SOURCE_PORTS DNS_MAX_REQUESTS
#else
#if DNS_MAX_SOURCE_PORTS > 255
#error DNS_MAX_SOURCE_PORTS must fit into an u8_t
#endif/* ... */
#endif/* ... */
#else
#ifdef DNS_MAX_SOURCE_PORTS
#undef DNS_MAX_SOURCE_PORTS
#endif
#define DNS_MAX_SOURCE_PORTS 1
/* ... */#endif
#if LWIP_IPV4 && LWIP_IPV6
#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) (((t) == LWIP_DNS_ADDRTYPE_IPV6_IPV4) || ((t) == LWIP_DNS_ADDRTYPE_IPV6))
#define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) (IP_IS_V6_VAL(ip) ? LWIP_DNS_ADDRTYPE_IS_IPV6(t) : (!LWIP_DNS_ADDRTYPE_IS_IPV6(t)))
#define LWIP_DNS_ADDRTYPE_ARG(x) , x
#define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) x
#define LWIP_DNS_SET_ADDRTYPE(x, y) do { x = y; } while(0)
/* ... */#else
#if LWIP_IPV6
#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 1
#else
#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 0
#endif
#define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) 1
#define LWIP_DNS_ADDRTYPE_ARG(x)
#define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) 0
#define LWIP_DNS_SET_ADDRTYPE(x, y)
/* ... */#endif
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
#define LWIP_DNS_ISMDNS_ARG(x) , x
#else
#define LWIP_DNS_ISMDNS_ARG(x)
#endif
/* ... */
struct dns_query {
/* ... */
u16_t type;
u16_t cls;
...};
#define SIZEOF_DNS_QUERY 4
/* ... */
struct dns_answer {
/* ... */
u16_t type;
u16_t cls;
u32_t ttl;
u16_t len;
...};
#define SIZEOF_DNS_ANSWER 10
#define SIZEOF_DNS_ANSWER_ASSERT 12
typedef enum {
DNS_STATE_UNUSED = 0,
DNS_STATE_NEW = 1,
DNS_STATE_ASKING = 2,
DNS_STATE_DONE = 3
...} dns_state_enum_t;
struct dns_table_entry {
u32_t ttl;
ip_addr_t ipaddr;
u16_t txid;
u8_t state;
u8_t server_idx;
u8_t tmr;
u8_t retries;
u8_t seqno;
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
u8_t pcb_idx;
#endif
char name[DNS_MAX_NAME_LENGTH];
#if LWIP_IPV4 && LWIP_IPV6
u8_t reqaddrtype;
#endif
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
u8_t is_mdns;
#endif
...};
/* ... */
struct dns_req_entry {
dns_found_callback found;
void *arg;
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
u8_t dns_table_idx;
#endif
#if LWIP_IPV4 && LWIP_IPV6
u8_t reqaddrtype;
#endif
...};
#if DNS_LOCAL_HOSTLIST
#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
/* ... */
static struct local_hostlist_entry *local_hostlist_dynamic;/* ... */
#else
/* ... */
#ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE
#define DNS_LOCAL_HOSTLIST_STORAGE_PRE static
#endif
/* ... */
#ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST
#define DNS_LOCAL_HOSTLIST_STORAGE_POST
#endif
DNS_LOCAL_HOSTLIST_STORAGE_PRE struct local_hostlist_entry local_hostlist_static[]
DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT;
/* ... */
#endif
static void dns_init_local(void);
static err_t dns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype));/* ... */
#endif
static void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
static void dns_check_entries(void);
static void dns_call_found(u8_t idx, ip_addr_t *addr);
/* ... */
static struct udp_pcb *dns_pcbs[DNS_MAX_SOURCE_PORTS];
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
static u8_t dns_last_pcb_idx;
#endif
static u8_t dns_seqno;
static struct dns_table_entry dns_table[DNS_TABLE_SIZE];
static struct dns_req_entry dns_requests[DNS_MAX_REQUESTS];
static ip_addr_t dns_servers[DNS_MAX_SERVERS];
#if LWIP_IPV4
const ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT;
#endif
#if LWIP_IPV6
const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT;
#endif
/* ... */
void
dns_init(void)
{
#ifdef DNS_SERVER_ADDRESS
ip_addr_t dnsserver;
DNS_SERVER_ADDRESS(&dnsserver);
dns_setserver(0, &dnsserver);/* ... */
#endif
LWIP_ASSERT("sanity check SIZEOF_DNS_QUERY",
sizeof(struct dns_query) == SIZEOF_DNS_QUERY);
LWIP_ASSERT("sanity check SIZEOF_DNS_ANSWER",
sizeof(struct dns_answer) <= SIZEOF_DNS_ANSWER_ASSERT);
LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n"));
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0)
if (dns_pcbs[0] == NULL) {
dns_pcbs[0] = udp_new_ip_type(IPADDR_TYPE_ANY);
LWIP_ASSERT("dns_pcbs[0] != NULL", dns_pcbs[0] != NULL);
/* ... */
LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0",
DNS_STATE_UNUSED == 0);
udp_bind(dns_pcbs[0], IP_ANY_TYPE, 0);
udp_recv(dns_pcbs[0], dns_recv, NULL);
}if (dns_pcbs[0] == NULL) { ... }
/* ... */#endif
#if DNS_LOCAL_HOSTLIST
dns_init_local();
#endif
}dns_init (void) { ... }
/* ... */
void
dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
{
if (numdns < DNS_MAX_SERVERS) {
if (dnsserver != NULL) {
dns_servers[numdns] = (*dnsserver);
}if (dnsserver != NULL) { ... } else {
dns_servers[numdns] = *IP_ADDR_ANY;
}else { ... }
}if (numdns < DNS_MAX_SERVERS) { ... }
}dns_setserver (u8_t numdns, const ip_addr_t *dnsserver) { ... }
/* ... */
const ip_addr_t *
dns_getserver(u8_t numdns)
{
if (numdns < DNS_MAX_SERVERS) {
return &dns_servers[numdns];
}if (numdns < DNS_MAX_SERVERS) { ... } else {
return IP_ADDR_ANY;
}else { ... }
}dns_getserver (u8_t numdns) { ... }
/* ... */
void
dns_tmr(void)
{
LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n"));
dns_check_entries();
}dns_tmr (void) { ... }
#if DNS_LOCAL_HOSTLIST
static void
dns_init_local(void)
{
#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT)
size_t i;
struct local_hostlist_entry *entry;
struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT;
size_t namelen;
for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_init); i++) {
struct local_hostlist_entry *init_entry = &local_hostlist_init[i];
LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL);
namelen = strlen(init_entry->name);
LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);
LWIP_ASSERT("mem-error in dns_init_local", entry != NULL);
if (entry != NULL) {
char *entry_name = (char *)entry + sizeof(struct local_hostlist_entry);
MEMCPY(entry_name, init_entry->name, namelen);
entry_name[namelen] = 0;
entry->name = entry_name;
entry->addr = init_entry->addr;
entry->next = local_hostlist_dynamic;
local_hostlist_dynamic = entry;
}if (entry != NULL) { ... }
}for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_init); i++) { ... }
/* ... */#endif
}dns_init_local (void) { ... }
/* ... */
size_t
dns_local_iterate(dns_found_callback iterator_fn, void *iterator_arg)
{
size_t i;
#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
struct local_hostlist_entry *entry = local_hostlist_dynamic;
i = 0;
while (entry != NULL) {
if (iterator_fn != NULL) {
iterator_fn(entry->name, &entry->addr, iterator_arg);
}if (iterator_fn != NULL) { ... }
i++;
entry = entry->next;
}while (entry != NULL) { ... }
/* ... */#else
for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
if (iterator_fn != NULL) {
iterator_fn(local_hostlist_static[i].name, &local_hostlist_static[i].addr, iterator_arg);
}if (iterator_fn != NULL) { ... }
}for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) { ... }
/* ... */#endif
return i;
}dns_local_iterate (dns_found_callback iterator_fn, void *iterator_arg) { ... }
/* ... */
err_t
dns_local_lookup(const char *hostname, ip_addr_t *addr, u8_t dns_addrtype)
{
LWIP_UNUSED_ARG(dns_addrtype);
return dns_lookup_local(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype));
}dns_local_lookup (const char *hostname, ip_addr_t *addr, u8_t dns_addrtype) { ... }
static err_t
dns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
{
#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
struct local_hostlist_entry *entry = local_hostlist_dynamic;
while (entry != NULL) {
if ((lwip_stricmp(entry->name, hostname) == 0) &&
LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, entry->addr)) {
if (addr) {
ip_addr_copy(*addr, entry->addr);
}if (addr) { ... }
return ERR_OK;
}if ((lwip_stricmp(entry->name, hostname) == 0) && LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, entry->addr)) { ... }
entry = entry->next;
}while (entry != NULL) { ... }
/* ... */#else
size_t i;
for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
if ((lwip_stricmp(local_hostlist_static[i].name, hostname) == 0) &&
LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, local_hostlist_static[i].addr)) {
if (addr) {
ip_addr_copy(*addr, local_hostlist_static[i].addr);
}if (addr) { ... }
return ERR_OK;
}if ((lwip_stricmp(local_hostlist_static[i].name, hostname) == 0) && LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, local_hostlist_static[i].addr)) { ... }
}for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) { ... }
/* ... */#endif
return ERR_ARG;
}dns_lookup_local (const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype)) { ... }
#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
/* ... */
int
dns_local_removehost(const char *hostname, const ip_addr_t *addr)
{
int removed = 0;
struct local_hostlist_entry *entry = local_hostlist_dynamic;
struct local_hostlist_entry *last_entry = NULL;
while (entry != NULL) {
if (((hostname == NULL) || !lwip_stricmp(entry->name, hostname)) &&
((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) {
struct local_hostlist_entry *free_entry;
if (last_entry != NULL) {
last_entry->next = entry->next;
}if (last_entry != NULL) { ... } else {
local_hostlist_dynamic = entry->next;
}else { ... }
free_entry = entry;
entry = entry->next;
memp_free(MEMP_LOCALHOSTLIST, free_entry);
removed++;
}if (((hostname == NULL) || !lwip_stricmp(entry->name, hostname)) && ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) { ... } else {
last_entry = entry;
entry = entry->next;
}else { ... }
}while (entry != NULL) { ... }
return removed;
}dns_local_removehost (const char *hostname, const ip_addr_t *addr) { ... }
/* ... */
err_t
dns_local_addhost(const char *hostname, const ip_addr_t *addr)
{
struct local_hostlist_entry *entry;
size_t namelen;
char *entry_name;
LWIP_ASSERT("invalid host name (NULL)", hostname != NULL);
namelen = strlen(hostname);
LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);
if (entry == NULL) {
return ERR_MEM;
}if (entry == NULL) { ... }
entry_name = (char *)entry + sizeof(struct local_hostlist_entry);
MEMCPY(entry_name, hostname, namelen);
entry_name[namelen] = 0;
entry->name = entry_name;
ip_addr_copy(entry->addr, *addr);
entry->next = local_hostlist_dynamic;
local_hostlist_dynamic = entry;
return ERR_OK;
}dns_local_addhost (const char *hostname, const ip_addr_t *addr) { ... }
/* ... */#endif /* ... */
#endif
/* ... */
static err_t
dns_lookup(const char *name, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
{
u8_t i;
#if DNS_LOCAL_HOSTLIST
if (dns_lookup_local(name, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) {
return ERR_OK;
}if (dns_lookup_local(name, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) { ... }
/* ... */#endif
#ifdef DNS_LOOKUP_LOCAL_EXTERN
if (DNS_LOOKUP_LOCAL_EXTERN(name, addr, LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(dns_addrtype)) == ERR_OK) {
return ERR_OK;
}if (DNS_LOOKUP_LOCAL_EXTERN(name, addr, LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(dns_addrtype)) == ERR_OK) { ... }
/* ... */#endif
for (i = 0; i < DNS_TABLE_SIZE; ++i) {
if ((dns_table[i].state == DNS_STATE_DONE) &&
(lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0) &&
LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, dns_table[i].ipaddr)) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name));
ip_addr_debug_print_val(DNS_DEBUG, dns_table[i].ipaddr);
LWIP_DEBUGF(DNS_DEBUG, ("\n"));
if (addr) {
ip_addr_copy(*addr, dns_table[i].ipaddr);
}if (addr) { ... }
return ERR_OK;
}if ((dns_table[i].state == DNS_STATE_DONE) && (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0) && LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, dns_table[i].ipaddr)) { ... }
}for (i = 0; i < DNS_TABLE_SIZE; ++i) { ... }
return ERR_ARG;
}dns_lookup (const char *name, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype)) { ... }
/* ... */
static u16_t
dns_compare_name(const char *query, struct pbuf *p, u16_t start_offset)
{
int n;
u16_t response_offset = start_offset;
do {
n = pbuf_try_get_at(p, response_offset);
if ((n < 0) || (response_offset == 0xFFFF)) {
return 0xFFFF;
}if ((n < 0) || (response_offset == 0xFFFF)) { ... }
response_offset++;
if ((n & 0xc0) == 0xc0) {
return 0xFFFF;
}if ((n & 0xc0) == 0xc0) { ... } else {
while (n > 0) {
int c = pbuf_try_get_at(p, response_offset);
if (c < 0) {
return 0xFFFF;
}if (c < 0) { ... }
if (lwip_tolower((*query)) != lwip_tolower((u8_t)c)) {
return 0xFFFF;
}if (lwip_tolower((*query)) != lwip_tolower((u8_t)c)) { ... }
if (response_offset == 0xFFFF) {
return 0xFFFF;
}if (response_offset == 0xFFFF) { ... }
response_offset++;
++query;
--n;
}while (n > 0) { ... }
++query;
}else { ... }
n = pbuf_try_get_at(p, response_offset);
if (n < 0) {
return 0xFFFF;
}if (n < 0) { ... }
...} while (n != 0);
if (response_offset == 0xFFFF) {
return 0xFFFF;
}if (response_offset == 0xFFFF) { ... }
return (u16_t)(response_offset + 1);
}dns_compare_name (const char *query, struct pbuf *p, u16_t start_offset) { ... }
/* ... */
static u16_t
dns_skip_name(struct pbuf *p, u16_t query_idx)
{
int n;
u16_t offset = query_idx;
do {
n = pbuf_try_get_at(p, offset++);
if ((n < 0) || (offset == 0)) {
return 0xFFFF;
}if ((n < 0) || (offset == 0)) { ... }
if ((n & 0xc0) == 0xc0) {
break;
}if ((n & 0xc0) == 0xc0) { ... } else {
if (offset + n >= p->tot_len) {
return 0xFFFF;
}if (offset + n >= p->tot_len) { ... }
offset = (u16_t)(offset + n);
}else { ... }
n = pbuf_try_get_at(p, offset);
if (n < 0) {
return 0xFFFF;
}if (n < 0) { ... }
...} while (n != 0);
if (offset == 0xFFFF) {
return 0xFFFF;
}if (offset == 0xFFFF) { ... }
return (u16_t)(offset + 1);
}dns_skip_name (struct pbuf *p, u16_t query_idx) { ... }
/* ... */
static err_t
dns_send(u8_t idx)
{
err_t err;
struct dns_hdr hdr;
struct dns_query qry;
struct pbuf *p;
u16_t query_idx, copy_len;
const char *hostname, *hostname_part;
u8_t n;
u8_t pcb_idx;
struct dns_table_entry *entry = &dns_table[idx];
LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n",
(u16_t)(entry->server_idx), entry->name));
LWIP_ASSERT("dns server out of array", entry->server_idx < DNS_MAX_SERVERS);
if (ip_addr_isany_val(dns_servers[entry->server_idx])
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
&& !entry->is_mdns
#endif
) {
dns_call_found(idx, NULL);
entry->state = DNS_STATE_UNUSED;
return ERR_OK;
}if (ip_addr_isany_val(dns_servers[entry->server_idx]) #if LWIP_DNS_SUPPORT_MDNS_QUERIES && !entry->is_mdns #endif) { ... }
p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(SIZEOF_DNS_HDR + strlen(entry->name) + 2 +
SIZEOF_DNS_QUERY), PBUF_RAM);
if (p != NULL) {
const ip_addr_t *dst;
u16_t dst_port;
memset(&hdr, 0, SIZEOF_DNS_HDR);
hdr.id = lwip_htons(entry->txid);
hdr.flags1 = DNS_FLAG1_RD;
hdr.numquestions = PP_HTONS(1);
pbuf_take(p, &hdr, SIZEOF_DNS_HDR);
hostname = entry->name;
--hostname;
query_idx = SIZEOF_DNS_HDR;
do {
++hostname;
hostname_part = hostname;
for (n = 0; *hostname != '.' && *hostname != 0; ++hostname) {
++n;
}for (n = 0; *hostname != '.' && *hostname != 0; ++hostname) { ... }
copy_len = (u16_t)(hostname - hostname_part);
if (query_idx + n + 1 > 0xFFFF) {
goto overflow_return;
}if (query_idx + n + 1 > 0xFFFF) { ... }
pbuf_put_at(p, query_idx, n);
pbuf_take_at(p, hostname_part, copy_len, (u16_t)(query_idx + 1));
query_idx = (u16_t)(query_idx + n + 1);
...} while (*hostname != 0);
pbuf_put_at(p, query_idx, 0);
query_idx++;
if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) {
qry.type = PP_HTONS(DNS_RRTYPE_AAAA);
}if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) { ... } else {
qry.type = PP_HTONS(DNS_RRTYPE_A);
}else { ... }
qry.cls = PP_HTONS(DNS_RRCLASS_IN);
pbuf_take_at(p, &qry, SIZEOF_DNS_QUERY, query_idx);
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
pcb_idx = entry->pcb_idx;
#else
pcb_idx = 0;
#endif
LWIP_DEBUGF(DNS_DEBUG, ("sending DNS request ID %d for name \"%s\" to server %d\r\n",
entry->txid, entry->name, entry->server_idx));
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
if (entry->is_mdns) {
dst_port = DNS_MQUERY_PORT;
#if LWIP_IPV6
if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) {
dst = &dns_mquery_v6group;
}if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) { ... }
/* ... */#endif
#if LWIP_IPV4 && LWIP_IPV6
else
#endif
#if LWIP_IPV4
{
dst = &dns_mquery_v4group;
...}/* ... */
#endif
}if (entry->is_mdns) { ... } else
#endif
{
dst_port = DNS_SERVER_PORT;
dst = &dns_servers[entry->server_idx];
}else { ... }
err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port);
pbuf_free(p);
}if (p != NULL) { ... } else {
err = ERR_MEM;
}else { ... }
return err;
overflow_return:
pbuf_free(p);
return ERR_VAL;
}dns_send (u8_t idx) { ... }
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
static struct udp_pcb *
dns_alloc_random_port(void)
{
err_t err;
struct udp_pcb *pcb;
pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
if (pcb == NULL) {
return NULL;
}if (pcb == NULL) { ... }
do {
u16_t port = (u16_t)DNS_RAND_TXID();
if (DNS_PORT_ALLOWED(port)) {
err = udp_bind(pcb, IP_ANY_TYPE, port);
}if (DNS_PORT_ALLOWED(port)) { ... } else {
err = ERR_USE;
}else { ... }
...} while (err == ERR_USE);
if (err != ERR_OK) {
udp_remove(pcb);
return NULL;
}if (err != ERR_OK) { ... }
udp_recv(pcb, dns_recv, NULL);
return pcb;
}dns_alloc_random_port (void) { ... }
/* ... */
static u8_t
dns_alloc_pcb(void)
{
u8_t i;
u8_t idx;
for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) {
if (dns_pcbs[i] == NULL) {
break;
}if (dns_pcbs[i] == NULL) { ... }
}for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) { ... }
if (i < DNS_MAX_SOURCE_PORTS) {
dns_pcbs[i] = dns_alloc_random_port();
if (dns_pcbs[i] != NULL) {
dns_last_pcb_idx = i;
return i;
}if (dns_pcbs[i] != NULL) { ... }
}if (i < DNS_MAX_SOURCE_PORTS) { ... }
/* ... */
for (i = 0, idx = (u8_t)(dns_last_pcb_idx + 1); i < DNS_MAX_SOURCE_PORTS; i++, idx++) {
if (idx >= DNS_MAX_SOURCE_PORTS) {
idx = 0;
}if (idx >= DNS_MAX_SOURCE_PORTS) { ... }
if (dns_pcbs[idx] != NULL) {
dns_last_pcb_idx = idx;
return idx;
}if (dns_pcbs[idx] != NULL) { ... }
}for (i = 0, idx = (u8_t)(dns_last_pcb_idx + 1); i < DNS_MAX_SOURCE_PORTS; i++, idx++) { ... }
return DNS_MAX_SOURCE_PORTS;
}dns_alloc_pcb (void) { ... }
/* ... */#endif
/* ... */
static void
dns_call_found(u8_t idx, ip_addr_t *addr)
{
#if ((LWIP_DNS_SECURE & (LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT)) != 0)
u8_t i;
#endif
#if LWIP_IPV4 && LWIP_IPV6
if (addr != NULL) {
if (IP_IS_V6_VAL(*addr)) {
LWIP_ASSERT("invalid response", LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
}if (IP_IS_V6_VAL(*addr)) { ... } else {
LWIP_ASSERT("invalid response", !LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
}else { ... }
}if (addr != NULL) { ... }
/* ... */#endif
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
for (i = 0; i < DNS_MAX_REQUESTS; i++) {
if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) {
(*dns_requests[i].found)(dns_table[idx].name, addr, dns_requests[i].arg);
dns_requests[i].found = NULL;
}if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) { ... }
}for (i = 0; i < DNS_MAX_REQUESTS; i++) { ... }
/* ... */#else
if (dns_requests[idx].found) {
(*dns_requests[idx].found)(dns_table[idx].name, addr, dns_requests[idx].arg);
}if (dns_requests[idx].found) { ... }
dns_requests[idx].found = NULL;/* ... */
#endif
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
for (i = 0; i < DNS_MAX_REQUESTS; i++) {
if (i == idx) {
continue;
}if (i == idx) { ... }
if (dns_table[i].state == DNS_STATE_ASKING) {
if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) {
dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
break;
}if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) { ... }
}if (dns_table[i].state == DNS_STATE_ASKING) { ... }
}for (i = 0; i < DNS_MAX_REQUESTS; i++) { ... }
if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) {
udp_remove(dns_pcbs[dns_table[idx].pcb_idx]);
dns_pcbs[dns_table[idx].pcb_idx] = NULL;
dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
}if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) { ... }
/* ... */#endif
}dns_call_found (u8_t idx, ip_addr_t *addr) { ... }
static u16_t
dns_create_txid(void)
{
u16_t txid;
u8_t i;
again:
txid = (u16_t)DNS_RAND_TXID();
for (i = 0; i < DNS_TABLE_SIZE; i++) {
if ((dns_table[i].state == DNS_STATE_ASKING) &&
(dns_table[i].txid == txid)) {
goto again;
}if ((dns_table[i].state == DNS_STATE_ASKING) && (dns_table[i].txid == txid)) { ... }
}for (i = 0; i < DNS_TABLE_SIZE; i++) { ... }
return txid;
}dns_create_txid (void) { ... }
/* ... */
static u8_t
dns_backupserver_available(struct dns_table_entry *pentry)
{
u8_t ret = 0;
if (pentry) {
if ((pentry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany_val(dns_servers[pentry->server_idx + 1])) {
ret = 1;
}if ((pentry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany_val(dns_servers[pentry->server_idx + 1])) { ... }
}if (pentry) { ... }
return ret;
}dns_backupserver_available (struct dns_table_entry *pentry) { ... }
/* ... */
static void
dns_check_entry(u8_t i)
{
err_t err;
struct dns_table_entry *entry = &dns_table[i];
LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE);
switch (entry->state) {
case DNS_STATE_NEW:
entry->txid = dns_create_txid();
entry->state = DNS_STATE_ASKING;
entry->server_idx = 0;
entry->tmr = 1;
entry->retries = 0;
err = dns_send(i);
if (err != ERR_OK) {
LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,
("dns_send returned error: %s\n", lwip_strerr(err)));
}if (err != ERR_OK) { ... }
break;case DNS_STATE_NEW:
case DNS_STATE_ASKING:
if (--entry->tmr == 0) {
if (++entry->retries == DNS_MAX_RETRIES) {
if (dns_backupserver_available(entry)
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
&& !entry->is_mdns
#endif
) {
entry->server_idx++;
entry->tmr = 1;
entry->retries = 0;
}if (dns_backupserver_available(entry) #if LWIP_DNS_SUPPORT_MDNS_QUERIES && !entry->is_mdns #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */) { ... } else {
LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", entry->name));
dns_call_found(i, NULL);
entry->state = DNS_STATE_UNUSED;
break;
}else { ... }
}if (++entry->retries == DNS_MAX_RETRIES) { ... } else {
entry->tmr = entry->retries;
}else { ... }
err = dns_send(i);
if (err != ERR_OK) {
LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,
("dns_send returned error: %s\n", lwip_strerr(err)));
}if (err != ERR_OK) { ... }
}if (--entry->tmr == 0) { ... }
break;case DNS_STATE_ASKING:
case DNS_STATE_DONE:
if ((entry->ttl == 0) || (--entry->ttl == 0)) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", entry->name));
entry->state = DNS_STATE_UNUSED;
}if ((entry->ttl == 0) || (--entry->ttl == 0)) { ... }
break;case DNS_STATE_DONE:
case DNS_STATE_UNUSED:
break;case DNS_STATE_UNUSED:
default:
LWIP_ASSERT("unknown dns_table entry state:", 0);
break;default
}switch (entry->state) { ... }
}dns_check_entry (u8_t i) { ... }
/* ... */
static void
dns_check_entries(void)
{
u8_t i;
for (i = 0; i < DNS_TABLE_SIZE; ++i) {
dns_check_entry(i);
}for (i = 0; i < DNS_TABLE_SIZE; ++i) { ... }
}dns_check_entries (void) { ... }
/* ... */
static void
dns_correct_response(u8_t idx, u32_t ttl)
{
struct dns_table_entry *entry = &dns_table[idx];
entry->state = DNS_STATE_DONE;
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", entry->name));
ip_addr_debug_print_val(DNS_DEBUG, entry->ipaddr);
LWIP_DEBUGF(DNS_DEBUG, ("\n"));
entry->ttl = ttl;
if (entry->ttl > DNS_MAX_TTL) {
entry->ttl = DNS_MAX_TTL;
}if (entry->ttl > DNS_MAX_TTL) { ... }
dns_call_found(idx, &entry->ipaddr);
if (entry->ttl == 0) {
/* ... */
if (entry->state == DNS_STATE_DONE) {
entry->state = DNS_STATE_UNUSED;
}if (entry->state == DNS_STATE_DONE) { ... }
}if (entry->ttl == 0) { ... }
}dns_correct_response (u8_t idx, u32_t ttl) { ... }
/* ... */
static void
dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
u8_t i;
u16_t txid;
u16_t res_idx;
struct dns_hdr hdr;
struct dns_answer ans;
struct dns_query qry;
u16_t nquestions, nanswers;
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(pcb);
LWIP_UNUSED_ARG(port);
if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY)) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n"));
goto ignore_packet;
}if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY)) { ... }
if (pbuf_copy_partial(p, &hdr, SIZEOF_DNS_HDR, 0) == SIZEOF_DNS_HDR) {
txid = lwip_htons(hdr.id);
for (i = 0; i < DNS_TABLE_SIZE; i++) {
struct dns_table_entry *entry = &dns_table[i];
if ((entry->state == DNS_STATE_ASKING) &&
(entry->txid == txid)) {
/* ... */
nquestions = lwip_htons(hdr.numquestions);
nanswers = lwip_htons(hdr.numanswers);
if ((hdr.flags1 & DNS_FLAG1_RESPONSE) == 0) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": not a response\n", entry->name));
goto ignore_packet;
}if ((hdr.flags1 & DNS_FLAG1_RESPONSE) == 0) { ... }
if (nquestions != 1) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
goto ignore_packet;
}if (nquestions != 1) { ... }
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
if (!entry->is_mdns)
#endif
{
/* ... */
if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) {
goto ignore_packet;
}if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) { ... }
...}
/* ... */
res_idx = dns_compare_name(entry->name, p, SIZEOF_DNS_HDR);
if (res_idx == 0xFFFF) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
goto ignore_packet;
}if (res_idx == 0xFFFF) { ... }
if (pbuf_copy_partial(p, &qry, SIZEOF_DNS_QUERY, res_idx) != SIZEOF_DNS_QUERY) {
goto ignore_packet;
}if (pbuf_copy_partial(p, &qry, SIZEOF_DNS_QUERY, res_idx) != SIZEOF_DNS_QUERY) { ... }
if ((qry.cls != PP_HTONS(DNS_RRCLASS_IN)) ||
(LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_AAAA))) ||
(!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_A)))) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
goto ignore_packet;
}if ((qry.cls != PP_HTONS(DNS_RRCLASS_IN)) || (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_AAAA))) || (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_A)))) { ... }
if (res_idx + SIZEOF_DNS_QUERY > 0xFFFF) {
goto ignore_packet;
}if (res_idx + SIZEOF_DNS_QUERY > 0xFFFF) { ... }
res_idx = (u16_t)(res_idx + SIZEOF_DNS_QUERY);
if (hdr.flags2 & DNS_FLAG2_ERR_MASK) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", entry->name));
/* ... */
if (dns_backupserver_available(entry)) {
entry->retries = DNS_MAX_RETRIES-1;
entry->tmr = 1;
dns_check_entry(i);
goto ignore_packet;
}if (dns_backupserver_available(entry)) { ... }
}if (hdr.flags2 & DNS_FLAG2_ERR_MASK) { ... } else {
while ((nanswers > 0) && (res_idx < p->tot_len)) {
res_idx = dns_skip_name(p, res_idx);
if (res_idx == 0xFFFF) {
goto ignore_packet;
}if (res_idx == 0xFFFF) { ... }
if (pbuf_copy_partial(p, &ans, SIZEOF_DNS_ANSWER, res_idx) != SIZEOF_DNS_ANSWER) {
goto ignore_packet;
}if (pbuf_copy_partial(p, &ans, SIZEOF_DNS_ANSWER, res_idx) != SIZEOF_DNS_ANSWER) { ... }
if (res_idx + SIZEOF_DNS_ANSWER > 0xFFFF) {
goto ignore_packet;
}if (res_idx + SIZEOF_DNS_ANSWER > 0xFFFF) { ... }
res_idx = (u16_t)(res_idx + SIZEOF_DNS_ANSWER);
if (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) {
#if LWIP_IPV4
if ((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.len == PP_HTONS(sizeof(ip4_addr_t)))) {
#if LWIP_IPV4 && LWIP_IPV6
if (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
#endif
{
ip4_addr_t ip4addr;
if (pbuf_copy_partial(p, &ip4addr, sizeof(ip4_addr_t), res_idx) != sizeof(ip4_addr_t)) {
goto ignore_packet;
}if (pbuf_copy_partial(p, &ip4addr, sizeof(ip4_addr_t), res_idx) != sizeof(ip4_addr_t)) { ... }
ip_addr_copy_from_ip4(dns_table[i].ipaddr, ip4addr);
pbuf_free(p);
dns_correct_response(i, lwip_ntohl(ans.ttl));
return;
...}
}if ((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.len == PP_HTONS(sizeof(ip4_addr_t)))) { ... }
/* ... */#endif
#if LWIP_IPV6
if ((ans.type == PP_HTONS(DNS_RRTYPE_AAAA)) && (ans.len == PP_HTONS(sizeof(ip6_addr_p_t)))) {
#if LWIP_IPV4 && LWIP_IPV6
if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
#endif
{
ip6_addr_p_t ip6addr;
if (pbuf_copy_partial(p, &ip6addr, sizeof(ip6_addr_p_t), res_idx) != sizeof(ip6_addr_p_t)) {
goto ignore_packet;
}if (pbuf_copy_partial(p, &ip6addr, sizeof(ip6_addr_p_t), res_idx) != sizeof(ip6_addr_p_t)) { ... }
ip_addr_copy_from_ip6_packed(dns_table[i].ipaddr, ip6addr);
pbuf_free(p);
dns_correct_response(i, lwip_ntohl(ans.ttl));
return;
...}
}if ((ans.type == PP_HTONS(DNS_RRTYPE_AAAA)) && (ans.len == PP_HTONS(sizeof(ip6_addr_p_t)))) { ... }
/* ... */#endif
}if (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) { ... }
if ((int)(res_idx + lwip_htons(ans.len)) > 0xFFFF) {
goto ignore_packet;
}if ((int)(res_idx + lwip_htons(ans.len)) > 0xFFFF) { ... }
res_idx = (u16_t)(res_idx + lwip_htons(ans.len));
--nanswers;
}while ((nanswers > 0) && (res_idx < p->tot_len)) { ... }
#if LWIP_IPV4 && LWIP_IPV6
if ((entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) ||
(entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
if (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
}if (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) { ... } else {
dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
}else { ... }
pbuf_free(p);
dns_table[i].state = DNS_STATE_NEW;
dns_check_entry(i);
return;
}if ((entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) { ... }
/* ... */#endif
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", entry->name));
}else { ... }
pbuf_free(p);
dns_call_found(i, NULL);
dns_table[i].state = DNS_STATE_UNUSED;
return;
}if ((entry->state == DNS_STATE_ASKING) && (entry->txid == txid)) { ... }
}for (i = 0; i < DNS_TABLE_SIZE; i++) { ... }
}if (pbuf_copy_partial(p, &hdr, SIZEOF_DNS_HDR, 0) == SIZEOF_DNS_HDR) { ... }
ignore_packet:
pbuf_free(p);
return;
}dns_recv (void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) { ... }
/* ... */
static err_t
dns_enqueue(const char *name, size_t hostnamelen, dns_found_callback found,
void *callback_arg LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype) LWIP_DNS_ISMDNS_ARG(u8_t is_mdns))
{
u8_t i;
u8_t lseq, lseqi;
struct dns_table_entry *entry = NULL;
size_t namelen;
struct dns_req_entry *req;
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
u8_t r;
for (i = 0; i < DNS_TABLE_SIZE; i++) {
if ((dns_table[i].state == DNS_STATE_ASKING) &&
(lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0)) {
#if LWIP_IPV4 && LWIP_IPV6
if (dns_table[i].reqaddrtype != dns_addrtype) {
/* ... */
continue;
}if (dns_table[i].reqaddrtype != dns_addrtype) { ... }
/* ... */#endif
for (r = 0; r < DNS_MAX_REQUESTS; r++) {
if (dns_requests[r].found == 0) {
dns_requests[r].found = found;
dns_requests[r].arg = callback_arg;
dns_requests[r].dns_table_idx = i;
LWIP_DNS_SET_ADDRTYPE(dns_requests[r].reqaddrtype, dns_addrtype);
LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": duplicate request\n", name));
return ERR_INPROGRESS;
}if (dns_requests[r].found == 0) { ... }
}for (r = 0; r < DNS_MAX_REQUESTS; r++) { ... }
}if ((dns_table[i].state == DNS_STATE_ASKING) && (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0)) { ... }
}for (i = 0; i < DNS_TABLE_SIZE; i++) { ... }
/* ... */
#endif
lseq = 0;
lseqi = DNS_TABLE_SIZE;
for (i = 0; i < DNS_TABLE_SIZE; ++i) {
entry = &dns_table[i];
if (entry->state == DNS_STATE_UNUSED) {
break;
}if (entry->state == DNS_STATE_UNUSED) { ... }
if (entry->state == DNS_STATE_DONE) {
u8_t age = (u8_t)(dns_seqno - entry->seqno);
if (age > lseq) {
lseq = age;
lseqi = i;
}if (age > lseq) { ... }
}if (entry->state == DNS_STATE_DONE) { ... }
}for (i = 0; i < DNS_TABLE_SIZE; ++i) { ... }
if (i == DNS_TABLE_SIZE) {
if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name));
return ERR_MEM;
}if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) { ... } else {
i = lseqi;
entry = &dns_table[i];
}else { ... }
}if (i == DNS_TABLE_SIZE) { ... }
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
req = NULL;
for (r = 0; r < DNS_MAX_REQUESTS; r++) {
if (dns_requests[r].found == NULL) {
req = &dns_requests[r];
break;
}if (dns_requests[r].found == NULL) { ... }
}for (r = 0; r < DNS_MAX_REQUESTS; r++) { ... }
if (req == NULL) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS request entries table is full\n", name));
return ERR_MEM;
}if (req == NULL) { ... }
req->dns_table_idx = i;/* ... */
#else
req = &dns_requests[i];/* ... */
#endif
LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i)));
entry->state = DNS_STATE_NEW;
entry->seqno = dns_seqno;
LWIP_DNS_SET_ADDRTYPE(entry->reqaddrtype, dns_addrtype);
LWIP_DNS_SET_ADDRTYPE(req->reqaddrtype, dns_addrtype);
req->found = found;
req->arg = callback_arg;
namelen = LWIP_MIN(hostnamelen, DNS_MAX_NAME_LENGTH - 1);
MEMCPY(entry->name, name, namelen);
entry->name[namelen] = 0;
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
entry->pcb_idx = dns_alloc_pcb();
if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": failed to allocate a pcb\n", name));
entry->state = DNS_STATE_UNUSED;
req->found = NULL;
return ERR_MEM;
}if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) { ... }
LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS pcb %"U16_F"\n", name, (u16_t)(entry->pcb_idx)));/* ... */
#endif
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
entry->is_mdns = is_mdns;
#endif
dns_seqno++;
dns_check_entry(i);
return ERR_INPROGRESS;
}dns_enqueue (const char *name, size_t hostnamelen, dns_found_callback found, void *callback_arg LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype) LWIP_DNS_ISMDNS_ARG(u8_t is_mdns)) { ... }
/* ... */
err_t
dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found,
void *callback_arg)
{
return dns_gethostbyname_addrtype(hostname, addr, found, callback_arg, LWIP_DNS_ADDRTYPE_DEFAULT);
}dns_gethostbyname (const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg) { ... }
/* ... */
err_t
dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_callback found,
void *callback_arg, u8_t dns_addrtype)
{
size_t hostnamelen;
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
u8_t is_mdns;
#endif
/* ... */
if ((addr == NULL) ||
(!hostname) || (!hostname[0])) {
return ERR_ARG;
}if ((addr == NULL) || (!hostname) || (!hostname[0])) { ... }
#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0)
if (dns_pcbs[0] == NULL) {
return ERR_ARG;
}if (dns_pcbs[0] == NULL) { ... }
/* ... */#endif
hostnamelen = strlen(hostname);
if (hostnamelen >= DNS_MAX_NAME_LENGTH) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_gethostbyname: name too long to resolve"));
return ERR_ARG;
}if (hostnamelen >= DNS_MAX_NAME_LENGTH) { ... }
#if LWIP_HAVE_LOOPIF
if (strcmp(hostname, "localhost") == 0) {
ip_addr_set_loopback(LWIP_DNS_ADDRTYPE_IS_IPV6(dns_addrtype), addr);
return ERR_OK;
}if (strcmp(hostname, "localhost") == 0) { ... }
/* ... */#endif
if (ipaddr_aton(hostname, addr)) {
#if LWIP_IPV4 && LWIP_IPV6
if ((IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV4)) ||
(IP_IS_V4(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6)))/* ... */
#endif
{
return ERR_OK;
...}
}if (ipaddr_aton(hostname, addr)) { ... }
if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) {
return ERR_OK;
}if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) { ... }
#if LWIP_IPV4 && LWIP_IPV6
if ((dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
u8_t fallback;
if (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
fallback = LWIP_DNS_ADDRTYPE_IPV6;
}if (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) { ... } else {
fallback = LWIP_DNS_ADDRTYPE_IPV4;
}else { ... }
if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(fallback)) == ERR_OK) {
return ERR_OK;
}if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(fallback)) == ERR_OK) { ... }
}if ((dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) { ... }
/* ... */#else
LWIP_UNUSED_ARG(dns_addrtype);
#endif
#if LWIP_DNS_SUPPORT_MDNS_QUERIES
if (strstr(hostname, ".local") == &hostname[hostnamelen] - 6) {
is_mdns = 1;
}if (strstr(hostname, ".local") == &hostname[hostnamelen] - 6) { ... } else {
is_mdns = 0;
}else { ... }
if (!is_mdns)/* ... */
#endif
{
if (ip_addr_isany_val(dns_servers[0])) {
return ERR_VAL;
}if (ip_addr_isany_val(dns_servers[0])) { ... }
...}
return dns_enqueue(hostname, hostnamelen, found, callback_arg LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)
LWIP_DNS_ISMDNS_ARG(is_mdns));
}dns_gethostbyname_addrtype (const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg, u8_t dns_addrtype) { ... }
/* ... */
#endif