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@187:122fc1996c86, 2021-01-18 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon Jan 18 18:23:46 2021 +0000
- Revision:
- 187:122fc1996c86
- Parent:
- 176:7eb916c22084
- Child:
- 193:47a953ab571b
Changed Ip4Address to Ip4Addr.; Moved Ip6AddrIsExternal from NdpNeedsToBeRouted.
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 | 173:9bc30cd82a76 | 41 | LogTimeF("DnsQuery reaped ongoing request for record type "); |
andrewboyson | 173:9bc30cd82a76 | 42 | DnsRecordTypeLog(_RecordType); |
andrewboyson | 173:9bc30cd82a76 | 43 | if (DnsQueryName[0]) { LogF(" name '%s'", DnsQueryName); } |
andrewboyson | 187:122fc1996c86 | 44 | if (DnsQueryIp4) { Log(" address "); Ip4AddrLog(DnsQueryIp4); } |
andrewboyson | 187:122fc1996c86 | 45 | if (DnsQueryIp6[0]) { Log(" address "); Ip6AddrLog(DnsQueryIp6); } |
andrewboyson | 173:9bc30cd82a76 | 46 | Log(" using "); |
andrewboyson | 173:9bc30cd82a76 | 47 | DnsProtocolLog(_DnsProtocol); |
andrewboyson | 173:9bc30cd82a76 | 48 | Log(" over "); |
andrewboyson | 173:9bc30cd82a76 | 49 | EthProtocolLog(_IpProtocol); |
andrewboyson | 13:9cd54f7db57a | 50 | LogF("\r\n"); |
andrewboyson | 13:9cd54f7db57a | 51 | |
andrewboyson | 13:9cd54f7db57a | 52 | DnsQueryName[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 53 | DnsQueryIp4 = 0; |
andrewboyson | 13:9cd54f7db57a | 54 | DnsQueryIp6[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 55 | DnsQueryIsBusy = false; |
andrewboyson | 171:f708d6776752 | 56 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 57 | _DnsProtocol = DNS_PROTOCOL_NONE; |
andrewboyson | 171:f708d6776752 | 58 | _IpProtocol = 0; |
andrewboyson | 171:f708d6776752 | 59 | _RecordType = DNS_RECORD_NONE; |
andrewboyson | 13:9cd54f7db57a | 60 | } |
andrewboyson | 13:9cd54f7db57a | 61 | } |
andrewboyson | 43:bc028d5a6424 | 62 | void DnsQueryMain() |
andrewboyson | 13:9cd54f7db57a | 63 | { |
andrewboyson | 93:580fc113d9e9 | 64 | reap(); |
andrewboyson | 13:9cd54f7db57a | 65 | } |
andrewboyson | 171:f708d6776752 | 66 | void DnsQueryIp4FromName(char * name, int dnsProtocol, int ipProtocol) |
andrewboyson | 13:9cd54f7db57a | 67 | { |
andrewboyson | 176:7eb916c22084 | 68 | if (!name[0]) |
andrewboyson | 176:7eb916c22084 | 69 | { |
andrewboyson | 176:7eb916c22084 | 70 | LogTime("DnsQueryIp4FromName called with no name\r\n"); |
andrewboyson | 176:7eb916c22084 | 71 | return; |
andrewboyson | 176:7eb916c22084 | 72 | } |
andrewboyson | 171:f708d6776752 | 73 | DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName); |
andrewboyson | 171:f708d6776752 | 74 | DnsQueryIp4 = 0; |
andrewboyson | 171:f708d6776752 | 75 | DnsQueryIp6[0] = 0; |
andrewboyson | 171:f708d6776752 | 76 | DnsQueryIsBusy = true; |
andrewboyson | 171:f708d6776752 | 77 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 78 | _DnsProtocol = dnsProtocol; |
andrewboyson | 171:f708d6776752 | 79 | _IpProtocol = ipProtocol; |
andrewboyson | 171:f708d6776752 | 80 | _RecordType = DNS_RECORD_A; |
andrewboyson | 13:9cd54f7db57a | 81 | } |
andrewboyson | 171:f708d6776752 | 82 | void DnsQueryIp6FromName(char * name, int dnsProtocol, int ipProtocol) |
andrewboyson | 13:9cd54f7db57a | 83 | { |
andrewboyson | 176:7eb916c22084 | 84 | if (!name[0]) |
andrewboyson | 176:7eb916c22084 | 85 | { |
andrewboyson | 176:7eb916c22084 | 86 | LogTime("DnsQueryIp6FromName called with no name\r\n"); |
andrewboyson | 176:7eb916c22084 | 87 | return; |
andrewboyson | 176:7eb916c22084 | 88 | } |
andrewboyson | 171:f708d6776752 | 89 | DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName); |
andrewboyson | 171:f708d6776752 | 90 | DnsQueryIp4 = 0; |
andrewboyson | 171:f708d6776752 | 91 | DnsQueryIp6[0] = 0; |
andrewboyson | 171:f708d6776752 | 92 | DnsQueryIsBusy = true; |
andrewboyson | 171:f708d6776752 | 93 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 94 | _DnsProtocol = dnsProtocol; |
andrewboyson | 171:f708d6776752 | 95 | _IpProtocol = ipProtocol; |
andrewboyson | 171:f708d6776752 | 96 | _RecordType = DNS_RECORD_AAAA; |
andrewboyson | 13:9cd54f7db57a | 97 | } |
andrewboyson | 171:f708d6776752 | 98 | void DnsQueryNameFromIp4(uint32_t ip, int dnsProtocol, int ipProtocol) |
andrewboyson | 13:9cd54f7db57a | 99 | { |
andrewboyson | 176:7eb916c22084 | 100 | if (!ip) |
andrewboyson | 176:7eb916c22084 | 101 | { |
andrewboyson | 176:7eb916c22084 | 102 | LogTime("DnsQueryNameFromIp4 called with no ip\r\n"); |
andrewboyson | 176:7eb916c22084 | 103 | return; |
andrewboyson | 176:7eb916c22084 | 104 | } |
andrewboyson | 171:f708d6776752 | 105 | DnsQueryName[0] = 0; |
andrewboyson | 171:f708d6776752 | 106 | DnsQueryIp4 = ip; |
andrewboyson | 171:f708d6776752 | 107 | DnsQueryIp6[0] = 0; |
andrewboyson | 171:f708d6776752 | 108 | DnsQueryIsBusy = true; |
andrewboyson | 171:f708d6776752 | 109 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 110 | _DnsProtocol = dnsProtocol; |
andrewboyson | 171:f708d6776752 | 111 | _IpProtocol = ipProtocol; |
andrewboyson | 171:f708d6776752 | 112 | _RecordType = DNS_RECORD_PTR; |
andrewboyson | 13:9cd54f7db57a | 113 | } |
andrewboyson | 171:f708d6776752 | 114 | void DnsQueryNameFromIp6(char* ip, int dnsProtocol, int ipProtocol) |
andrewboyson | 13:9cd54f7db57a | 115 | { |
andrewboyson | 176:7eb916c22084 | 116 | if (!ip[0]) |
andrewboyson | 176:7eb916c22084 | 117 | { |
andrewboyson | 176:7eb916c22084 | 118 | LogTime("DnsQueryNameFromIp6 called with no ip\r\n"); |
andrewboyson | 176:7eb916c22084 | 119 | return; |
andrewboyson | 176:7eb916c22084 | 120 | } |
andrewboyson | 171:f708d6776752 | 121 | DnsQueryName[0] = 0; |
andrewboyson | 171:f708d6776752 | 122 | DnsQueryIp4 = 0; |
andrewboyson | 172:9bc3c7b2cca1 | 123 | Ip6AddrCopy(DnsQueryIp6, ip); |
andrewboyson | 171:f708d6776752 | 124 | DnsQueryIsBusy = true; |
andrewboyson | 171:f708d6776752 | 125 | _StartedMs = MsTimerCount; |
andrewboyson | 171:f708d6776752 | 126 | _DnsProtocol = dnsProtocol; |
andrewboyson | 171:f708d6776752 | 127 | _IpProtocol = ipProtocol; |
andrewboyson | 171:f708d6776752 | 128 | _RecordType = DNS_RECORD_PTR; |
andrewboyson | 30:e34173b7585c | 129 | } |
andrewboyson | 30:e34173b7585c | 130 | static void logQuery() |
andrewboyson | 30:e34173b7585c | 131 | { |
andrewboyson | 43:bc028d5a6424 | 132 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 133 | LogTimeF("DnsQuery sent "); |
andrewboyson | 171:f708d6776752 | 134 | DnsProtocolLog(_DnsProtocol); |
andrewboyson | 47:73af5c0b0dc2 | 135 | Log(" request for "); |
andrewboyson | 171:f708d6776752 | 136 | DnsRecordTypeLog(_RecordType); |
andrewboyson | 47:73af5c0b0dc2 | 137 | Log(" "); |
andrewboyson | 30:e34173b7585c | 138 | if (DnsQueryIp4) //Reverse |
andrewboyson | 30:e34173b7585c | 139 | { |
andrewboyson | 187:122fc1996c86 | 140 | Ip4AddrLog(DnsQueryIp4); |
andrewboyson | 30:e34173b7585c | 141 | } |
andrewboyson | 30:e34173b7585c | 142 | else if (DnsQueryIp6[0]) |
andrewboyson | 30:e34173b7585c | 143 | { |
andrewboyson | 172:9bc3c7b2cca1 | 144 | Ip6AddrLog(DnsQueryIp6); |
andrewboyson | 30:e34173b7585c | 145 | } |
andrewboyson | 30:e34173b7585c | 146 | else //Forward |
andrewboyson | 30:e34173b7585c | 147 | { |
andrewboyson | 47:73af5c0b0dc2 | 148 | Log(DnsQueryName); |
andrewboyson | 30:e34173b7585c | 149 | } |
andrewboyson | 47:73af5c0b0dc2 | 150 | Log("\r\n"); |
andrewboyson | 13:9cd54f7db57a | 151 | } |
andrewboyson | 171:f708d6776752 | 152 | int DnsQueryPoll(int ipType, void* pPacket, int* pSize) |
andrewboyson | 13:9cd54f7db57a | 153 | { |
andrewboyson | 59:e0e556c8bd46 | 154 | DnsHdrSetup(pPacket, *pSize); |
andrewboyson | 59:e0e556c8bd46 | 155 | |
andrewboyson | 171:f708d6776752 | 156 | if (!DnsQueryIsBusy) return DO_NOTHING; |
andrewboyson | 172:9bc3c7b2cca1 | 157 | if (_IpProtocol != EthProtocol) return DO_NOTHING; //Only use a poll from the required protocol |
andrewboyson | 171:f708d6776752 | 158 | if (_DnsProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING; |
andrewboyson | 173:9bc30cd82a76 | 159 | if (_RecordType == DNS_RECORD_NONE) return DO_NOTHING; |
andrewboyson | 173:9bc30cd82a76 | 160 | if (_DnsProtocol == DNS_PROTOCOL_NONE) return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 161 | |
andrewboyson | 57:e0fb648acf48 | 162 | NetTraceHostCheckIp6(DnsQueryIp6); |
andrewboyson | 57:e0fb648acf48 | 163 | |
andrewboyson | 57:e0fb648acf48 | 164 | if (DnsQueryTrace || NetTraceHostGetMatched()) logQuery(); |
andrewboyson | 30:e34173b7585c | 165 | |
andrewboyson | 13:9cd54f7db57a | 166 | static uint16_t id = 0; |
andrewboyson | 13:9cd54f7db57a | 167 | DnsHdrId = ++id; |
andrewboyson | 13:9cd54f7db57a | 168 | DnsHdrIsReply = false; |
andrewboyson | 171:f708d6776752 | 169 | DnsHdrIsAuthoritative = false; //Added 12/12/2020 |
andrewboyson | 13:9cd54f7db57a | 170 | DnsHdrIsRecursiveQuery = false; |
andrewboyson | 13:9cd54f7db57a | 171 | |
andrewboyson | 13:9cd54f7db57a | 172 | DnsHdrQdcount = 1; |
andrewboyson | 13:9cd54f7db57a | 173 | DnsHdrAncount = 0; |
andrewboyson | 13:9cd54f7db57a | 174 | DnsHdrNscount = 0; |
andrewboyson | 13:9cd54f7db57a | 175 | DnsHdrArcount = 0; |
andrewboyson | 13:9cd54f7db57a | 176 | |
andrewboyson | 13:9cd54f7db57a | 177 | DnsHdrWrite(); |
andrewboyson | 13:9cd54f7db57a | 178 | char* p = DnsHdrData; |
andrewboyson | 13:9cd54f7db57a | 179 | |
andrewboyson | 176:7eb916c22084 | 180 | if (DnsQueryIp4 ) DnsNameEncodeIp4(DnsQueryIp4, &p); |
andrewboyson | 176:7eb916c22084 | 181 | else if (DnsQueryIp6[0] ) DnsNameEncodeIp6(DnsQueryIp6, &p); |
andrewboyson | 176:7eb916c22084 | 182 | else if (DnsQueryName[0]) DnsNameEncodePtr(DnsQueryName, &p); |
andrewboyson | 176:7eb916c22084 | 183 | else return DO_NOTHING; |
andrewboyson | 30:e34173b7585c | 184 | |
andrewboyson | 30:e34173b7585c | 185 | *p++ = 0; |
andrewboyson | 171:f708d6776752 | 186 | *p++ = _RecordType; |
andrewboyson | 171:f708d6776752 | 187 | *p++ = _DnsProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS |
andrewboyson | 13:9cd54f7db57a | 188 | *p++ = 1; //QCLASS_IN = 1 - internet |
andrewboyson | 13:9cd54f7db57a | 189 | |
andrewboyson | 13:9cd54f7db57a | 190 | *pSize = p - DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 191 | |
andrewboyson | 13:9cd54f7db57a | 192 | DnsQueryIsBusy = false; |
andrewboyson | 38:cc8945857a0d | 193 | |
andrewboyson | 171:f708d6776752 | 194 | if (DnsQueryTrace || NetTraceHostGetMatched()) DnsHdrLog(_DnsProtocol); |
andrewboyson | 13:9cd54f7db57a | 195 | |
andrewboyson | 37:793b39683406 | 196 | int dest = DO_NOTHING; |
andrewboyson | 37:793b39683406 | 197 | |
andrewboyson | 171:f708d6776752 | 198 | switch (_DnsProtocol) |
andrewboyson | 13:9cd54f7db57a | 199 | { |
andrewboyson | 37:793b39683406 | 200 | case DNS_PROTOCOL_UDNS: dest = UNICAST_DNS; break; //IPv6 ==> NdpDnsServer; IPv4 ==> DhcpDnsServer |
andrewboyson | 37:793b39683406 | 201 | case DNS_PROTOCOL_MDNS: dest = MULTICAST_MDNS; break; |
andrewboyson | 37:793b39683406 | 202 | case DNS_PROTOCOL_LLMNR: dest = MULTICAST_LLMNR; break; |
andrewboyson | 13:9cd54f7db57a | 203 | default: |
andrewboyson | 171:f708d6776752 | 204 | LogTimeF("DNS unknown query protocol %d\r\n", _DnsProtocol); |
andrewboyson | 13:9cd54f7db57a | 205 | return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 206 | } |
andrewboyson | 37:793b39683406 | 207 | |
andrewboyson | 57:e0fb648acf48 | 208 | return ActionMakeFromDestAndTrace(dest, DnsQueryTrace || NetTraceHostGetMatched()); |
andrewboyson | 13:9cd54f7db57a | 209 | } |