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
udp/dns/dnsquery.c@172:9bc3c7b2cca1, 2020-12-16 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Dec 16 17:33:22 2020 +0000
- Revision:
- 172:9bc3c7b2cca1
- Parent:
- 171:f708d6776752
- Child:
- 173:9bc30cd82a76
Modified name resolution to work with both IPv4 and IPv6. Before there were two independent modules.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdint.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | #include <stdbool.h> |
andrewboyson | 61:aad055f1b0d1 | 3 | |
andrewboyson | 13:9cd54f7db57a | 4 | #include "log.h" |
andrewboyson | 93:580fc113d9e9 | 5 | #include "mstimer.h" |
andrewboyson | 13:9cd54f7db57a | 6 | #include "net.h" |
andrewboyson | 37:793b39683406 | 7 | #include "action.h" |
andrewboyson | 49:1a6336f2b3f9 | 8 | #include "ip4addr.h" |
andrewboyson | 49:1a6336f2b3f9 | 9 | #include "ip6addr.h" |
andrewboyson | 13:9cd54f7db57a | 10 | #include "dhcp.h" |
andrewboyson | 13:9cd54f7db57a | 11 | #include "dns.h" |
andrewboyson | 13:9cd54f7db57a | 12 | #include "udp.h" |
andrewboyson | 172:9bc3c7b2cca1 | 13 | #include "eth.h" |
andrewboyson | 13:9cd54f7db57a | 14 | #include "slaac.h" |
andrewboyson | 13:9cd54f7db57a | 15 | #include "dnshdr.h" |
andrewboyson | 13:9cd54f7db57a | 16 | #include "dnsname.h" |
andrewboyson | 128:79052cb4a41c | 17 | #include "dnslabel.h" |
andrewboyson | 13:9cd54f7db57a | 18 | |
andrewboyson | 37:793b39683406 | 19 | bool DnsQueryTrace = false; |
andrewboyson | 13:9cd54f7db57a | 20 | |
andrewboyson | 93:580fc113d9e9 | 21 | #define TIME_OUT_SENT_MS 3000 |
andrewboyson | 13:9cd54f7db57a | 22 | |
andrewboyson | 13:9cd54f7db57a | 23 | #define MDNS_UNICAST false |
andrewboyson | 13:9cd54f7db57a | 24 | |
andrewboyson | 35:93c39d260a83 | 25 | char DnsQueryName[DNS_MAX_LABEL_LENGTH+1]; |
andrewboyson | 13:9cd54f7db57a | 26 | uint32_t DnsQueryIp4 = 0; |
andrewboyson | 13:9cd54f7db57a | 27 | char DnsQueryIp6[16]; |
andrewboyson | 30:e34173b7585c | 28 | bool DnsQueryIsBusy = false; |
andrewboyson | 13:9cd54f7db57a | 29 | |
andrewboyson | 171:f708d6776752 | 30 | static char _RecordType = DNS_RECORD_NONE; |
andrewboyson | 171:f708d6776752 | 31 | static int _DnsProtocol = DNS_PROTOCOL_NONE; |
andrewboyson | 171:f708d6776752 | 32 | static int _IpProtocol = 0; |
andrewboyson | 171:f708d6776752 | 33 | static uint32_t _StartedMs = 0; |
andrewboyson | 171:f708d6776752 | 34 | |
andrewboyson | 13:9cd54f7db57a | 35 | static void reap() |
andrewboyson | 13:9cd54f7db57a | 36 | { |
andrewboyson | 13:9cd54f7db57a | 37 | if (!DnsQueryIsBusy) return; |
andrewboyson | 13:9cd54f7db57a | 38 | |
andrewboyson | 171:f708d6776752 | 39 | if (MsTimerRelative(_StartedMs, TIME_OUT_SENT_MS)) |
andrewboyson | 13:9cd54f7db57a | 40 | { |
andrewboyson | 13:9cd54f7db57a | 41 | LogTimeF("DNS reaped ongoing request for "); |
andrewboyson | 13:9cd54f7db57a | 42 | if (DnsQueryName[0]) LogF("name %s", DnsQueryName); |
andrewboyson | 13:9cd54f7db57a | 43 | if (DnsQueryIp4) |
andrewboyson | 13:9cd54f7db57a | 44 | { |
andrewboyson | 47:73af5c0b0dc2 | 45 | Log("ip4 "); Ip4AddressLog(DnsQueryIp4); |
andrewboyson | 13:9cd54f7db57a | 46 | } |
andrewboyson | 13:9cd54f7db57a | 47 | if (DnsQueryIp6[0]) |
andrewboyson | 13:9cd54f7db57a | 48 | { |
andrewboyson | 172:9bc3c7b2cca1 | 49 | Log("ip6 "); Ip6AddrLog(DnsQueryIp6); |
andrewboyson | 13:9cd54f7db57a | 50 | } |
andrewboyson | 13:9cd54f7db57a | 51 | LogF("\r\n"); |
andrewboyson | 13:9cd54f7db57a | 52 | |
andrewboyson | 13:9cd54f7db57a | 53 | DnsQueryName[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 54 | DnsQueryIp4 = 0; |
andrewboyson | 13:9cd54f7db57a | 55 | DnsQueryIp6[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 56 | DnsQueryIsBusy = false; |
andrewboyson | 171:f708d6776752 | 57 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 58 | _DnsProtocol = DNS_PROTOCOL_NONE; |
andrewboyson | 171:f708d6776752 | 59 | _IpProtocol = 0; |
andrewboyson | 171:f708d6776752 | 60 | _RecordType = DNS_RECORD_NONE; |
andrewboyson | 13:9cd54f7db57a | 61 | } |
andrewboyson | 13:9cd54f7db57a | 62 | } |
andrewboyson | 43:bc028d5a6424 | 63 | void DnsQueryMain() |
andrewboyson | 13:9cd54f7db57a | 64 | { |
andrewboyson | 93:580fc113d9e9 | 65 | reap(); |
andrewboyson | 13:9cd54f7db57a | 66 | } |
andrewboyson | 171:f708d6776752 | 67 | void DnsQueryIp4FromName(char * name, int dnsProtocol, int ipProtocol) |
andrewboyson | 13:9cd54f7db57a | 68 | { |
andrewboyson | 171:f708d6776752 | 69 | DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName); |
andrewboyson | 171:f708d6776752 | 70 | DnsQueryIp4 = 0; |
andrewboyson | 171:f708d6776752 | 71 | DnsQueryIp6[0] = 0; |
andrewboyson | 171:f708d6776752 | 72 | DnsQueryIsBusy = true; |
andrewboyson | 171:f708d6776752 | 73 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 74 | _DnsProtocol = dnsProtocol; |
andrewboyson | 171:f708d6776752 | 75 | _IpProtocol = ipProtocol; |
andrewboyson | 171:f708d6776752 | 76 | _RecordType = DNS_RECORD_A; |
andrewboyson | 13:9cd54f7db57a | 77 | } |
andrewboyson | 171:f708d6776752 | 78 | void DnsQueryIp6FromName(char * name, int dnsProtocol, int ipProtocol) |
andrewboyson | 13:9cd54f7db57a | 79 | { |
andrewboyson | 171:f708d6776752 | 80 | DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName); |
andrewboyson | 171:f708d6776752 | 81 | DnsQueryIp4 = 0; |
andrewboyson | 171:f708d6776752 | 82 | DnsQueryIp6[0] = 0; |
andrewboyson | 171:f708d6776752 | 83 | DnsQueryIsBusy = true; |
andrewboyson | 171:f708d6776752 | 84 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 85 | _DnsProtocol = dnsProtocol; |
andrewboyson | 171:f708d6776752 | 86 | _IpProtocol = ipProtocol; |
andrewboyson | 171:f708d6776752 | 87 | _RecordType = DNS_RECORD_AAAA; |
andrewboyson | 13:9cd54f7db57a | 88 | } |
andrewboyson | 171:f708d6776752 | 89 | void DnsQueryNameFromIp4(uint32_t ip, int dnsProtocol, int ipProtocol) |
andrewboyson | 13:9cd54f7db57a | 90 | { |
andrewboyson | 171:f708d6776752 | 91 | DnsQueryName[0] = 0; |
andrewboyson | 171:f708d6776752 | 92 | DnsQueryIp4 = ip; |
andrewboyson | 171:f708d6776752 | 93 | DnsQueryIp6[0] = 0; |
andrewboyson | 171:f708d6776752 | 94 | DnsQueryIsBusy = true; |
andrewboyson | 171:f708d6776752 | 95 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 96 | _DnsProtocol = dnsProtocol; |
andrewboyson | 171:f708d6776752 | 97 | _IpProtocol = ipProtocol; |
andrewboyson | 171:f708d6776752 | 98 | _RecordType = DNS_RECORD_PTR; |
andrewboyson | 13:9cd54f7db57a | 99 | } |
andrewboyson | 171:f708d6776752 | 100 | void DnsQueryNameFromIp6(char* ip, int dnsProtocol, int ipProtocol) |
andrewboyson | 13:9cd54f7db57a | 101 | { |
andrewboyson | 171:f708d6776752 | 102 | DnsQueryName[0] = 0; |
andrewboyson | 171:f708d6776752 | 103 | DnsQueryIp4 = 0; |
andrewboyson | 172:9bc3c7b2cca1 | 104 | Ip6AddrCopy(DnsQueryIp6, ip); |
andrewboyson | 171:f708d6776752 | 105 | DnsQueryIsBusy = true; |
andrewboyson | 171:f708d6776752 | 106 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 107 | _DnsProtocol = dnsProtocol; |
andrewboyson | 171:f708d6776752 | 108 | _IpProtocol = ipProtocol; |
andrewboyson | 171:f708d6776752 | 109 | _RecordType = DNS_RECORD_PTR; |
andrewboyson | 30:e34173b7585c | 110 | } |
andrewboyson | 30:e34173b7585c | 111 | static void logQuery() |
andrewboyson | 30:e34173b7585c | 112 | { |
andrewboyson | 43:bc028d5a6424 | 113 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 114 | LogTimeF("DnsQuery sent "); |
andrewboyson | 171:f708d6776752 | 115 | DnsProtocolLog(_DnsProtocol); |
andrewboyson | 47:73af5c0b0dc2 | 116 | Log(" request for "); |
andrewboyson | 171:f708d6776752 | 117 | DnsRecordTypeLog(_RecordType); |
andrewboyson | 47:73af5c0b0dc2 | 118 | Log(" "); |
andrewboyson | 30:e34173b7585c | 119 | if (DnsQueryIp4) //Reverse |
andrewboyson | 30:e34173b7585c | 120 | { |
andrewboyson | 47:73af5c0b0dc2 | 121 | Ip4AddressLog(DnsQueryIp4); |
andrewboyson | 30:e34173b7585c | 122 | } |
andrewboyson | 30:e34173b7585c | 123 | else if (DnsQueryIp6[0]) |
andrewboyson | 30:e34173b7585c | 124 | { |
andrewboyson | 172:9bc3c7b2cca1 | 125 | Ip6AddrLog(DnsQueryIp6); |
andrewboyson | 30:e34173b7585c | 126 | } |
andrewboyson | 30:e34173b7585c | 127 | else //Forward |
andrewboyson | 30:e34173b7585c | 128 | { |
andrewboyson | 47:73af5c0b0dc2 | 129 | Log(DnsQueryName); |
andrewboyson | 30:e34173b7585c | 130 | } |
andrewboyson | 47:73af5c0b0dc2 | 131 | Log("\r\n"); |
andrewboyson | 13:9cd54f7db57a | 132 | } |
andrewboyson | 171:f708d6776752 | 133 | int DnsQueryPoll(int ipType, void* pPacket, int* pSize) |
andrewboyson | 13:9cd54f7db57a | 134 | { |
andrewboyson | 59:e0e556c8bd46 | 135 | DnsHdrSetup(pPacket, *pSize); |
andrewboyson | 59:e0e556c8bd46 | 136 | |
andrewboyson | 171:f708d6776752 | 137 | if (!DnsQueryIsBusy) return DO_NOTHING; |
andrewboyson | 172:9bc3c7b2cca1 | 138 | if (_IpProtocol != EthProtocol) return DO_NOTHING; //Only use a poll from the required protocol |
andrewboyson | 171:f708d6776752 | 139 | if (_DnsProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 140 | |
andrewboyson | 57:e0fb648acf48 | 141 | NetTraceHostCheckIp6(DnsQueryIp6); |
andrewboyson | 57:e0fb648acf48 | 142 | |
andrewboyson | 57:e0fb648acf48 | 143 | if (DnsQueryTrace || NetTraceHostGetMatched()) logQuery(); |
andrewboyson | 30:e34173b7585c | 144 | |
andrewboyson | 13:9cd54f7db57a | 145 | static uint16_t id = 0; |
andrewboyson | 13:9cd54f7db57a | 146 | DnsHdrId = ++id; |
andrewboyson | 13:9cd54f7db57a | 147 | DnsHdrIsReply = false; |
andrewboyson | 171:f708d6776752 | 148 | DnsHdrIsAuthoritative = false; //Added 12/12/2020 |
andrewboyson | 13:9cd54f7db57a | 149 | DnsHdrIsRecursiveQuery = false; |
andrewboyson | 13:9cd54f7db57a | 150 | |
andrewboyson | 13:9cd54f7db57a | 151 | DnsHdrQdcount = 1; |
andrewboyson | 13:9cd54f7db57a | 152 | DnsHdrAncount = 0; |
andrewboyson | 13:9cd54f7db57a | 153 | DnsHdrNscount = 0; |
andrewboyson | 13:9cd54f7db57a | 154 | DnsHdrArcount = 0; |
andrewboyson | 13:9cd54f7db57a | 155 | |
andrewboyson | 13:9cd54f7db57a | 156 | DnsHdrWrite(); |
andrewboyson | 13:9cd54f7db57a | 157 | char* p = DnsHdrData; |
andrewboyson | 13:9cd54f7db57a | 158 | |
andrewboyson | 30:e34173b7585c | 159 | if (DnsQueryIp4 ) DnsNameEncodeIp4(DnsQueryIp4, &p); |
andrewboyson | 30:e34173b7585c | 160 | else if (DnsQueryIp6[0]) DnsNameEncodeIp6(DnsQueryIp6, &p); |
andrewboyson | 37:793b39683406 | 161 | else DnsNameEncodePtr(DnsQueryName, &p); |
andrewboyson | 30:e34173b7585c | 162 | |
andrewboyson | 30:e34173b7585c | 163 | *p++ = 0; |
andrewboyson | 171:f708d6776752 | 164 | *p++ = _RecordType; |
andrewboyson | 171:f708d6776752 | 165 | *p++ = _DnsProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS |
andrewboyson | 13:9cd54f7db57a | 166 | *p++ = 1; //QCLASS_IN = 1 - internet |
andrewboyson | 13:9cd54f7db57a | 167 | |
andrewboyson | 13:9cd54f7db57a | 168 | *pSize = p - DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 169 | |
andrewboyson | 13:9cd54f7db57a | 170 | DnsQueryIsBusy = false; |
andrewboyson | 38:cc8945857a0d | 171 | |
andrewboyson | 171:f708d6776752 | 172 | if (DnsQueryTrace || NetTraceHostGetMatched()) DnsHdrLog(_DnsProtocol); |
andrewboyson | 13:9cd54f7db57a | 173 | |
andrewboyson | 37:793b39683406 | 174 | int dest = DO_NOTHING; |
andrewboyson | 37:793b39683406 | 175 | |
andrewboyson | 171:f708d6776752 | 176 | switch (_DnsProtocol) |
andrewboyson | 13:9cd54f7db57a | 177 | { |
andrewboyson | 37:793b39683406 | 178 | case DNS_PROTOCOL_UDNS: dest = UNICAST_DNS; break; //IPv6 ==> NdpDnsServer; IPv4 ==> DhcpDnsServer |
andrewboyson | 37:793b39683406 | 179 | case DNS_PROTOCOL_MDNS: dest = MULTICAST_MDNS; break; |
andrewboyson | 37:793b39683406 | 180 | case DNS_PROTOCOL_LLMNR: dest = MULTICAST_LLMNR; break; |
andrewboyson | 13:9cd54f7db57a | 181 | default: |
andrewboyson | 171:f708d6776752 | 182 | LogTimeF("DNS unknown query protocol %d\r\n", _DnsProtocol); |
andrewboyson | 13:9cd54f7db57a | 183 | return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 184 | } |
andrewboyson | 37:793b39683406 | 185 | |
andrewboyson | 57:e0fb648acf48 | 186 | return ActionMakeFromDestAndTrace(dest, DnsQueryTrace || NetTraceHostGetMatched()); |
andrewboyson | 13:9cd54f7db57a | 187 | } |