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@132:db2174b36a6d, 2019-03-20 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Mar 20 10:55:30 2019 +0000
- Revision:
- 132:db2174b36a6d
- Parent:
- 128:79052cb4a41c
- Child:
- 133:a37eb35a03f1
Updated clock library
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 | 13:9cd54f7db57a | 13 | #include "slaac.h" |
andrewboyson | 13:9cd54f7db57a | 14 | #include "dnshdr.h" |
andrewboyson | 13:9cd54f7db57a | 15 | #include "dnsname.h" |
andrewboyson | 128:79052cb4a41c | 16 | #include "dnslabel.h" |
andrewboyson | 13:9cd54f7db57a | 17 | |
andrewboyson | 37:793b39683406 | 18 | bool DnsQueryTrace = false; |
andrewboyson | 13:9cd54f7db57a | 19 | |
andrewboyson | 93:580fc113d9e9 | 20 | #define TIME_OUT_SENT_MS 3000 |
andrewboyson | 13:9cd54f7db57a | 21 | |
andrewboyson | 13:9cd54f7db57a | 22 | #define MDNS_UNICAST false |
andrewboyson | 13:9cd54f7db57a | 23 | |
andrewboyson | 35:93c39d260a83 | 24 | char DnsQueryName[DNS_MAX_LABEL_LENGTH+1]; |
andrewboyson | 13:9cd54f7db57a | 25 | uint32_t DnsQueryIp4 = 0; |
andrewboyson | 13:9cd54f7db57a | 26 | char DnsQueryIp6[16]; |
andrewboyson | 13:9cd54f7db57a | 27 | |
andrewboyson | 30:e34173b7585c | 28 | char DnsQueryRecordType = DNS_RECORD_NONE; |
andrewboyson | 30:e34173b7585c | 29 | int DnsQueryProtocol = DNS_PROTOCOL_NONE; |
andrewboyson | 30:e34173b7585c | 30 | bool DnsQueryIsBusy = false; |
andrewboyson | 13:9cd54f7db57a | 31 | |
andrewboyson | 93:580fc113d9e9 | 32 | static uint32_t startedMs = 0; |
andrewboyson | 13:9cd54f7db57a | 33 | static void reap() |
andrewboyson | 13:9cd54f7db57a | 34 | { |
andrewboyson | 13:9cd54f7db57a | 35 | if (!DnsQueryIsBusy) return; |
andrewboyson | 13:9cd54f7db57a | 36 | |
andrewboyson | 132:db2174b36a6d | 37 | if (MsTimerInterval(startedMs, TIME_OUT_SENT_MS)) |
andrewboyson | 13:9cd54f7db57a | 38 | { |
andrewboyson | 13:9cd54f7db57a | 39 | LogTimeF("DNS reaped ongoing request for "); |
andrewboyson | 13:9cd54f7db57a | 40 | if (DnsQueryName[0]) LogF("name %s", DnsQueryName); |
andrewboyson | 13:9cd54f7db57a | 41 | if (DnsQueryIp4) |
andrewboyson | 13:9cd54f7db57a | 42 | { |
andrewboyson | 47:73af5c0b0dc2 | 43 | Log("ip4 "); Ip4AddressLog(DnsQueryIp4); |
andrewboyson | 13:9cd54f7db57a | 44 | } |
andrewboyson | 13:9cd54f7db57a | 45 | if (DnsQueryIp6[0]) |
andrewboyson | 13:9cd54f7db57a | 46 | { |
andrewboyson | 47:73af5c0b0dc2 | 47 | Log("ip6 "); Ip6AddressLog(DnsQueryIp6); |
andrewboyson | 13:9cd54f7db57a | 48 | } |
andrewboyson | 13:9cd54f7db57a | 49 | LogF("\r\n"); |
andrewboyson | 13:9cd54f7db57a | 50 | |
andrewboyson | 13:9cd54f7db57a | 51 | DnsQueryName[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 52 | DnsQueryIp4 = 0; |
andrewboyson | 13:9cd54f7db57a | 53 | DnsQueryIp6[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 54 | DnsQueryIsBusy = false; |
andrewboyson | 93:580fc113d9e9 | 55 | startedMs = MsTimerCount; |
andrewboyson | 13:9cd54f7db57a | 56 | DnsQueryProtocol = DNS_PROTOCOL_NONE; |
andrewboyson | 30:e34173b7585c | 57 | DnsQueryRecordType = DNS_RECORD_NONE; |
andrewboyson | 13:9cd54f7db57a | 58 | } |
andrewboyson | 13:9cd54f7db57a | 59 | } |
andrewboyson | 43:bc028d5a6424 | 60 | void DnsQueryMain() |
andrewboyson | 13:9cd54f7db57a | 61 | { |
andrewboyson | 93:580fc113d9e9 | 62 | reap(); |
andrewboyson | 13:9cd54f7db57a | 63 | } |
andrewboyson | 13:9cd54f7db57a | 64 | void DnsQueryIp4FromName(char * name, int protocol) |
andrewboyson | 13:9cd54f7db57a | 65 | { |
andrewboyson | 128:79052cb4a41c | 66 | DnsLabelMakeFullNameFromName(protocol, name, sizeof(DnsQueryName), DnsQueryName); |
andrewboyson | 13:9cd54f7db57a | 67 | DnsQueryIp4 = 0; |
andrewboyson | 13:9cd54f7db57a | 68 | DnsQueryIp6[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 69 | DnsQueryIsBusy = true; |
andrewboyson | 93:580fc113d9e9 | 70 | startedMs = MsTimerCount; |
andrewboyson | 13:9cd54f7db57a | 71 | DnsQueryProtocol = protocol; |
andrewboyson | 30:e34173b7585c | 72 | DnsQueryRecordType = DNS_RECORD_A; |
andrewboyson | 13:9cd54f7db57a | 73 | } |
andrewboyson | 13:9cd54f7db57a | 74 | void DnsQueryIp6FromName(char * name, int protocol) |
andrewboyson | 13:9cd54f7db57a | 75 | { |
andrewboyson | 128:79052cb4a41c | 76 | DnsLabelMakeFullNameFromName(protocol, name, sizeof(DnsQueryName), DnsQueryName); |
andrewboyson | 13:9cd54f7db57a | 77 | DnsQueryIp4 = 0; |
andrewboyson | 13:9cd54f7db57a | 78 | DnsQueryIp6[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 79 | DnsQueryIsBusy = true; |
andrewboyson | 93:580fc113d9e9 | 80 | startedMs = MsTimerCount; |
andrewboyson | 13:9cd54f7db57a | 81 | DnsQueryProtocol = protocol; |
andrewboyson | 30:e34173b7585c | 82 | DnsQueryRecordType = DNS_RECORD_AAAA; |
andrewboyson | 13:9cd54f7db57a | 83 | } |
andrewboyson | 13:9cd54f7db57a | 84 | void DnsQueryNameFromIp4(uint32_t ip, int protocol) |
andrewboyson | 13:9cd54f7db57a | 85 | { |
andrewboyson | 13:9cd54f7db57a | 86 | DnsQueryName[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 87 | DnsQueryIp4 = ip; |
andrewboyson | 13:9cd54f7db57a | 88 | DnsQueryIp6[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 89 | DnsQueryIsBusy = true; |
andrewboyson | 93:580fc113d9e9 | 90 | startedMs = MsTimerCount; |
andrewboyson | 13:9cd54f7db57a | 91 | DnsQueryProtocol = protocol; |
andrewboyson | 30:e34173b7585c | 92 | DnsQueryRecordType = DNS_RECORD_PTR; |
andrewboyson | 13:9cd54f7db57a | 93 | } |
andrewboyson | 13:9cd54f7db57a | 94 | void DnsQueryNameFromIp6(char* ip, int protocol) |
andrewboyson | 13:9cd54f7db57a | 95 | { |
andrewboyson | 13:9cd54f7db57a | 96 | DnsQueryName[0] = 0; |
andrewboyson | 13:9cd54f7db57a | 97 | DnsQueryIp4 = 0; |
andrewboyson | 49:1a6336f2b3f9 | 98 | Ip6AddressCopy(DnsQueryIp6, ip); |
andrewboyson | 13:9cd54f7db57a | 99 | DnsQueryIsBusy = true; |
andrewboyson | 93:580fc113d9e9 | 100 | startedMs = MsTimerCount; |
andrewboyson | 13:9cd54f7db57a | 101 | DnsQueryProtocol = protocol; |
andrewboyson | 30:e34173b7585c | 102 | DnsQueryRecordType = DNS_RECORD_PTR; |
andrewboyson | 30:e34173b7585c | 103 | } |
andrewboyson | 30:e34173b7585c | 104 | static void logQuery() |
andrewboyson | 30:e34173b7585c | 105 | { |
andrewboyson | 43:bc028d5a6424 | 106 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 107 | LogTimeF("DnsQuery sent "); |
andrewboyson | 47:73af5c0b0dc2 | 108 | DnsProtocolLog(DnsQueryProtocol); |
andrewboyson | 47:73af5c0b0dc2 | 109 | Log(" request for "); |
andrewboyson | 47:73af5c0b0dc2 | 110 | DnsRecordTypeLog(DnsQueryRecordType); |
andrewboyson | 47:73af5c0b0dc2 | 111 | Log(" "); |
andrewboyson | 30:e34173b7585c | 112 | if (DnsQueryIp4) //Reverse |
andrewboyson | 30:e34173b7585c | 113 | { |
andrewboyson | 47:73af5c0b0dc2 | 114 | Ip4AddressLog(DnsQueryIp4); |
andrewboyson | 30:e34173b7585c | 115 | } |
andrewboyson | 30:e34173b7585c | 116 | else if (DnsQueryIp6[0]) |
andrewboyson | 30:e34173b7585c | 117 | { |
andrewboyson | 47:73af5c0b0dc2 | 118 | Ip6AddressLog(DnsQueryIp6); |
andrewboyson | 30:e34173b7585c | 119 | } |
andrewboyson | 30:e34173b7585c | 120 | else //Forward |
andrewboyson | 30:e34173b7585c | 121 | { |
andrewboyson | 47:73af5c0b0dc2 | 122 | Log(DnsQueryName); |
andrewboyson | 30:e34173b7585c | 123 | } |
andrewboyson | 47:73af5c0b0dc2 | 124 | Log("\r\n"); |
andrewboyson | 13:9cd54f7db57a | 125 | } |
andrewboyson | 59:e0e556c8bd46 | 126 | int DnsQueryPoll(void* pPacket, int* pSize) |
andrewboyson | 13:9cd54f7db57a | 127 | { |
andrewboyson | 59:e0e556c8bd46 | 128 | DnsHdrSetup(pPacket, *pSize); |
andrewboyson | 59:e0e556c8bd46 | 129 | |
andrewboyson | 13:9cd54f7db57a | 130 | if (!DnsQueryIsBusy) return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 131 | if (DnsQueryProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 132 | |
andrewboyson | 57:e0fb648acf48 | 133 | NetTraceHostCheckIp6(DnsQueryIp6); |
andrewboyson | 57:e0fb648acf48 | 134 | |
andrewboyson | 57:e0fb648acf48 | 135 | if (DnsQueryTrace || NetTraceHostGetMatched()) logQuery(); |
andrewboyson | 30:e34173b7585c | 136 | |
andrewboyson | 13:9cd54f7db57a | 137 | static uint16_t id = 0; |
andrewboyson | 13:9cd54f7db57a | 138 | DnsHdrId = ++id; |
andrewboyson | 13:9cd54f7db57a | 139 | DnsHdrIsReply = false; |
andrewboyson | 13:9cd54f7db57a | 140 | DnsHdrIsRecursiveQuery = false; |
andrewboyson | 13:9cd54f7db57a | 141 | |
andrewboyson | 13:9cd54f7db57a | 142 | DnsHdrQdcount = 1; |
andrewboyson | 13:9cd54f7db57a | 143 | DnsHdrAncount = 0; |
andrewboyson | 13:9cd54f7db57a | 144 | DnsHdrNscount = 0; |
andrewboyson | 13:9cd54f7db57a | 145 | DnsHdrArcount = 0; |
andrewboyson | 13:9cd54f7db57a | 146 | |
andrewboyson | 13:9cd54f7db57a | 147 | DnsHdrWrite(); |
andrewboyson | 13:9cd54f7db57a | 148 | char* p = DnsHdrData; |
andrewboyson | 13:9cd54f7db57a | 149 | |
andrewboyson | 30:e34173b7585c | 150 | if (DnsQueryIp4 ) DnsNameEncodeIp4(DnsQueryIp4, &p); |
andrewboyson | 30:e34173b7585c | 151 | else if (DnsQueryIp6[0]) DnsNameEncodeIp6(DnsQueryIp6, &p); |
andrewboyson | 37:793b39683406 | 152 | else DnsNameEncodePtr(DnsQueryName, &p); |
andrewboyson | 30:e34173b7585c | 153 | |
andrewboyson | 30:e34173b7585c | 154 | *p++ = 0; |
andrewboyson | 30:e34173b7585c | 155 | *p++ = DnsQueryRecordType; |
andrewboyson | 13:9cd54f7db57a | 156 | *p++ = DnsQueryProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS |
andrewboyson | 13:9cd54f7db57a | 157 | *p++ = 1; //QCLASS_IN = 1 - internet |
andrewboyson | 13:9cd54f7db57a | 158 | |
andrewboyson | 13:9cd54f7db57a | 159 | *pSize = p - DnsHdrPacket; |
andrewboyson | 13:9cd54f7db57a | 160 | |
andrewboyson | 13:9cd54f7db57a | 161 | DnsQueryIsBusy = false; |
andrewboyson | 38:cc8945857a0d | 162 | |
andrewboyson | 57:e0fb648acf48 | 163 | if (DnsQueryTrace || NetTraceHostGetMatched()) DnsHdrLog(DnsQueryProtocol); |
andrewboyson | 13:9cd54f7db57a | 164 | |
andrewboyson | 37:793b39683406 | 165 | int dest = DO_NOTHING; |
andrewboyson | 37:793b39683406 | 166 | |
andrewboyson | 13:9cd54f7db57a | 167 | switch (DnsQueryProtocol) |
andrewboyson | 13:9cd54f7db57a | 168 | { |
andrewboyson | 37:793b39683406 | 169 | case DNS_PROTOCOL_UDNS: dest = UNICAST_DNS; break; //IPv6 ==> NdpDnsServer; IPv4 ==> DhcpDnsServer |
andrewboyson | 37:793b39683406 | 170 | case DNS_PROTOCOL_MDNS: dest = MULTICAST_MDNS; break; |
andrewboyson | 37:793b39683406 | 171 | case DNS_PROTOCOL_LLMNR: dest = MULTICAST_LLMNR; break; |
andrewboyson | 13:9cd54f7db57a | 172 | default: |
andrewboyson | 13:9cd54f7db57a | 173 | LogTimeF("DNS unknown query protocol %d\r\n", DnsQueryProtocol); |
andrewboyson | 13:9cd54f7db57a | 174 | return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 175 | } |
andrewboyson | 37:793b39683406 | 176 | |
andrewboyson | 57:e0fb648acf48 | 177 | return ActionMakeFromDestAndTrace(dest, DnsQueryTrace || NetTraceHostGetMatched()); |
andrewboyson | 13:9cd54f7db57a | 178 | } |