A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
Diff: udp/dns/dnsquery.cpp
- Revision:
- 30:e34173b7585c
- Parent:
- 22:914b970356f0
- Child:
- 32:679654f2d023
--- a/udp/dns/dnsquery.cpp Mon Aug 07 12:13:52 2017 +0000 +++ b/udp/dns/dnsquery.cpp Thu Aug 10 17:45:37 2017 +0000 @@ -20,8 +20,9 @@ uint32_t DnsQueryIp4 = 0; char DnsQueryIp6[16]; -int DnsQueryProtocol = DNS_PROTOCOL_NONE; -bool DnsQueryIsBusy = false; +char DnsQueryRecordType = DNS_RECORD_NONE; +int DnsQueryProtocol = DNS_PROTOCOL_NONE; +bool DnsQueryIsBusy = false; static uint32_t started = 0; static uint32_t elapsed = 0; @@ -52,6 +53,7 @@ DnsQueryIsBusy = false; started = 0; DnsQueryProtocol = DNS_PROTOCOL_NONE; + DnsQueryRecordType = DNS_RECORD_NONE; } } void DnsQueryTick() @@ -67,6 +69,7 @@ DnsQueryIsBusy = true; started = elapsed; DnsQueryProtocol = protocol; + DnsQueryRecordType = DNS_RECORD_A; } void DnsQueryIp6FromName(char * name, int protocol) { @@ -76,6 +79,7 @@ DnsQueryIsBusy = true; started = elapsed; DnsQueryProtocol = protocol; + DnsQueryRecordType = DNS_RECORD_AAAA; } void DnsQueryNameFromIp4(uint32_t ip, int protocol) { @@ -85,6 +89,7 @@ DnsQueryIsBusy = true; started = elapsed; DnsQueryProtocol = protocol; + DnsQueryRecordType = DNS_RECORD_PTR; } void DnsQueryNameFromIp6(char* ip, int protocol) { @@ -94,12 +99,38 @@ DnsQueryIsBusy = true; started = elapsed; DnsQueryProtocol = protocol; + DnsQueryRecordType = DNS_RECORD_PTR; +} +static void logQuery() +{ + if (!DEBUG) return; + char text[60]; + DnsProtocolToString(DnsQueryProtocol, sizeof(text), text); + LogTimeF("%s sent request for ", text); + DnsRecordTypeToString(DnsQueryRecordType, sizeof(text), text); + LogF("%s ", text); + if (DnsQueryIp4) //Reverse + { + Ip4AddressToString(DnsQueryIp4, sizeof(text), text); + LogF("%s\r\n", text); + } + else if (DnsQueryIp6[0]) + { + Ip6AddressToString(DnsQueryIp6, sizeof(text), text); + LogF("%s\r\n", text); + } + else //Forward + { + LogF("%s\r\n", DnsQueryName); + } } int DnsQueryPoll(int* pSize) { if (!DnsQueryIsBusy) return DO_NOTHING; if (DnsQueryProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING; + logQuery(); + static uint16_t id = 0; DnsHdrId = ++id; DnsHdrIsReply = false; @@ -113,40 +144,12 @@ DnsHdrWrite(); char* p = DnsHdrData; - if (DnsQueryIp4) //Reverse - { - if (DEBUG) - { - char text[20]; - DnsProtocolToString(DnsQueryProtocol, sizeof(text), text); - LogTimeF("%s sent request for ", text); - Ip4AddressToString(DnsQueryIp4, sizeof(text), text); - LogF("%s\r\n", text); - } - DnsNameEncodeIp4(DnsQueryIp4, &p); - *p++ = 0; - *p++ = DNS_RECORD_PTR; - } - else if (DnsQueryIp6[0]) - { - if (DEBUG) - { - char text[60]; - DnsProtocolToString(DnsQueryProtocol, sizeof(text), text); - LogTimeF("%s sent request for ", text); - Ip6AddressToString(DnsQueryIp6, sizeof(text), text); - LogF("%s\r\n", text); - } - DnsNameEncodeIp6(DnsQueryIp6, &p); - *p++ = 0; - *p++ = DNS_RECORD_PTR; - } - else //Forward - { - DnsNameEncode(DnsQueryName, &p); - *p++ = 0; - *p++ = DNS_RECORD_A; - } + if (DnsQueryIp4 ) DnsNameEncodeIp4(DnsQueryIp4, &p); + else if (DnsQueryIp6[0]) DnsNameEncodeIp6(DnsQueryIp6, &p); + else DnsNameEncode (DnsQueryName, &p); + + *p++ = 0; + *p++ = DnsQueryRecordType; *p++ = DnsQueryProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS *p++ = 1; //QCLASS_IN = 1 - internet