Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
udp/dns/dnsquery.c@61:aad055f1b0d1, 2018-01-11 (annotated)
- Committer:
 - andrewboyson
 - Date:
 - Thu Jan 11 17:38:21 2018 +0000
 - Revision:
 - 61:aad055f1b0d1
 - Parent:
 - udp/dns/dnsquery.cpp@59:e0e556c8bd46
 - Child:
 - 65:37acccf2752f
 
Removed dependence on Mbed OS
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 | 43:bc028d5a6424 | 5 | #include "clock.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 | 13:9cd54f7db57a | 16 | |
| andrewboyson | 37:793b39683406 | 17 | bool DnsQueryTrace = false; | 
| andrewboyson | 13:9cd54f7db57a | 18 | |
| andrewboyson | 13:9cd54f7db57a | 19 | #define TIME_OUT_SENT 3 | 
| andrewboyson | 13:9cd54f7db57a | 20 | |
| andrewboyson | 13:9cd54f7db57a | 21 | #define MDNS_UNICAST false | 
| andrewboyson | 13:9cd54f7db57a | 22 | |
| andrewboyson | 35:93c39d260a83 | 23 | char DnsQueryName[DNS_MAX_LABEL_LENGTH+1]; | 
| andrewboyson | 13:9cd54f7db57a | 24 | uint32_t DnsQueryIp4 = 0; | 
| andrewboyson | 13:9cd54f7db57a | 25 | char DnsQueryIp6[16]; | 
| andrewboyson | 13:9cd54f7db57a | 26 | |
| andrewboyson | 30:e34173b7585c | 27 | char DnsQueryRecordType = DNS_RECORD_NONE; | 
| andrewboyson | 30:e34173b7585c | 28 | int DnsQueryProtocol = DNS_PROTOCOL_NONE; | 
| andrewboyson | 30:e34173b7585c | 29 | bool DnsQueryIsBusy = false; | 
| andrewboyson | 13:9cd54f7db57a | 30 | |
| andrewboyson | 13:9cd54f7db57a | 31 | static uint32_t started = 0; | 
| andrewboyson | 13:9cd54f7db57a | 32 | static uint32_t elapsed = 0; | 
| andrewboyson | 13:9cd54f7db57a | 33 | static void reap() | 
| andrewboyson | 13:9cd54f7db57a | 34 | { | 
| andrewboyson | 13:9cd54f7db57a | 35 | if (!DnsQueryIsBusy) return; | 
| andrewboyson | 13:9cd54f7db57a | 36 | |
| andrewboyson | 13:9cd54f7db57a | 37 | if (elapsed - started >= TIME_OUT_SENT) | 
| 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 | 13:9cd54f7db57a | 55 | started = 0; | 
| 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 | 43:bc028d5a6424 | 62 | if (ClockTicked) | 
| andrewboyson | 43:bc028d5a6424 | 63 | { | 
| andrewboyson | 43:bc028d5a6424 | 64 | elapsed++; | 
| andrewboyson | 43:bc028d5a6424 | 65 | reap(); | 
| andrewboyson | 43:bc028d5a6424 | 66 | } | 
| andrewboyson | 13:9cd54f7db57a | 67 | } | 
| andrewboyson | 13:9cd54f7db57a | 68 | void DnsQueryIp4FromName(char * name, int protocol) | 
| andrewboyson | 13:9cd54f7db57a | 69 | { | 
| andrewboyson | 32:679654f2d023 | 70 | DnsMakeFullNameFromName(protocol, name, sizeof(DnsQueryName), DnsQueryName); | 
| andrewboyson | 13:9cd54f7db57a | 71 | DnsQueryIp4 = 0; | 
| andrewboyson | 13:9cd54f7db57a | 72 | DnsQueryIp6[0] = 0; | 
| andrewboyson | 13:9cd54f7db57a | 73 | DnsQueryIsBusy = true; | 
| andrewboyson | 13:9cd54f7db57a | 74 | started = elapsed; | 
| andrewboyson | 13:9cd54f7db57a | 75 | DnsQueryProtocol = protocol; | 
| andrewboyson | 30:e34173b7585c | 76 | DnsQueryRecordType = DNS_RECORD_A; | 
| andrewboyson | 13:9cd54f7db57a | 77 | } | 
| andrewboyson | 13:9cd54f7db57a | 78 | void DnsQueryIp6FromName(char * name, int protocol) | 
| andrewboyson | 13:9cd54f7db57a | 79 | { | 
| andrewboyson | 32:679654f2d023 | 80 | DnsMakeFullNameFromName(protocol, name, sizeof(DnsQueryName), DnsQueryName); | 
| andrewboyson | 13:9cd54f7db57a | 81 | DnsQueryIp4 = 0; | 
| andrewboyson | 13:9cd54f7db57a | 82 | DnsQueryIp6[0] = 0; | 
| andrewboyson | 13:9cd54f7db57a | 83 | DnsQueryIsBusy = true; | 
| andrewboyson | 13:9cd54f7db57a | 84 | started = elapsed; | 
| andrewboyson | 13:9cd54f7db57a | 85 | DnsQueryProtocol = protocol; | 
| andrewboyson | 30:e34173b7585c | 86 | DnsQueryRecordType = DNS_RECORD_AAAA; | 
| andrewboyson | 13:9cd54f7db57a | 87 | } | 
| andrewboyson | 13:9cd54f7db57a | 88 | void DnsQueryNameFromIp4(uint32_t ip, int protocol) | 
| andrewboyson | 13:9cd54f7db57a | 89 | { | 
| andrewboyson | 13:9cd54f7db57a | 90 | DnsQueryName[0] = 0; | 
| andrewboyson | 13:9cd54f7db57a | 91 | DnsQueryIp4 = ip; | 
| andrewboyson | 13:9cd54f7db57a | 92 | DnsQueryIp6[0] = 0; | 
| andrewboyson | 13:9cd54f7db57a | 93 | DnsQueryIsBusy = true; | 
| andrewboyson | 13:9cd54f7db57a | 94 | started = elapsed; | 
| andrewboyson | 13:9cd54f7db57a | 95 | DnsQueryProtocol = protocol; | 
| andrewboyson | 30:e34173b7585c | 96 | DnsQueryRecordType = DNS_RECORD_PTR; | 
| andrewboyson | 13:9cd54f7db57a | 97 | } | 
| andrewboyson | 13:9cd54f7db57a | 98 | void DnsQueryNameFromIp6(char* ip, int protocol) | 
| andrewboyson | 13:9cd54f7db57a | 99 | { | 
| andrewboyson | 13:9cd54f7db57a | 100 | DnsQueryName[0] = 0; | 
| andrewboyson | 13:9cd54f7db57a | 101 | DnsQueryIp4 = 0; | 
| andrewboyson | 49:1a6336f2b3f9 | 102 | Ip6AddressCopy(DnsQueryIp6, ip); | 
| andrewboyson | 13:9cd54f7db57a | 103 | DnsQueryIsBusy = true; | 
| andrewboyson | 13:9cd54f7db57a | 104 | started = elapsed; | 
| andrewboyson | 13:9cd54f7db57a | 105 | DnsQueryProtocol = protocol; | 
| andrewboyson | 30:e34173b7585c | 106 | DnsQueryRecordType = DNS_RECORD_PTR; | 
| andrewboyson | 30:e34173b7585c | 107 | } | 
| andrewboyson | 30:e34173b7585c | 108 | static void logQuery() | 
| andrewboyson | 30:e34173b7585c | 109 | { | 
| andrewboyson | 43:bc028d5a6424 | 110 | if (NetTraceNewLine) Log("\r\n"); | 
| andrewboyson | 47:73af5c0b0dc2 | 111 | LogTimeF("DnsQuery sent "); | 
| andrewboyson | 47:73af5c0b0dc2 | 112 | DnsProtocolLog(DnsQueryProtocol); | 
| andrewboyson | 47:73af5c0b0dc2 | 113 | Log(" request for "); | 
| andrewboyson | 47:73af5c0b0dc2 | 114 | DnsRecordTypeLog(DnsQueryRecordType); | 
| andrewboyson | 47:73af5c0b0dc2 | 115 | Log(" "); | 
| andrewboyson | 30:e34173b7585c | 116 | if (DnsQueryIp4) //Reverse | 
| andrewboyson | 30:e34173b7585c | 117 | { | 
| andrewboyson | 47:73af5c0b0dc2 | 118 | Ip4AddressLog(DnsQueryIp4); | 
| andrewboyson | 30:e34173b7585c | 119 | } | 
| andrewboyson | 30:e34173b7585c | 120 | else if (DnsQueryIp6[0]) | 
| andrewboyson | 30:e34173b7585c | 121 | { | 
| andrewboyson | 47:73af5c0b0dc2 | 122 | Ip6AddressLog(DnsQueryIp6); | 
| andrewboyson | 30:e34173b7585c | 123 | } | 
| andrewboyson | 30:e34173b7585c | 124 | else //Forward | 
| andrewboyson | 30:e34173b7585c | 125 | { | 
| andrewboyson | 47:73af5c0b0dc2 | 126 | Log(DnsQueryName); | 
| andrewboyson | 30:e34173b7585c | 127 | } | 
| andrewboyson | 47:73af5c0b0dc2 | 128 | Log("\r\n"); | 
| andrewboyson | 13:9cd54f7db57a | 129 | } | 
| andrewboyson | 59:e0e556c8bd46 | 130 | int DnsQueryPoll(void* pPacket, int* pSize) | 
| andrewboyson | 13:9cd54f7db57a | 131 | { | 
| andrewboyson | 59:e0e556c8bd46 | 132 | DnsHdrSetup(pPacket, *pSize); | 
| andrewboyson | 59:e0e556c8bd46 | 133 | |
| andrewboyson | 13:9cd54f7db57a | 134 | if (!DnsQueryIsBusy) return DO_NOTHING; | 
| andrewboyson | 13:9cd54f7db57a | 135 | if (DnsQueryProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING; | 
| andrewboyson | 13:9cd54f7db57a | 136 | |
| andrewboyson | 57:e0fb648acf48 | 137 | NetTraceHostCheckIp6(DnsQueryIp6); | 
| andrewboyson | 57:e0fb648acf48 | 138 | |
| andrewboyson | 57:e0fb648acf48 | 139 | if (DnsQueryTrace || NetTraceHostGetMatched()) logQuery(); | 
| andrewboyson | 30:e34173b7585c | 140 | |
| andrewboyson | 13:9cd54f7db57a | 141 | static uint16_t id = 0; | 
| andrewboyson | 13:9cd54f7db57a | 142 | DnsHdrId = ++id; | 
| andrewboyson | 13:9cd54f7db57a | 143 | DnsHdrIsReply = false; | 
| andrewboyson | 13:9cd54f7db57a | 144 | DnsHdrIsRecursiveQuery = false; | 
| andrewboyson | 13:9cd54f7db57a | 145 | |
| andrewboyson | 13:9cd54f7db57a | 146 | DnsHdrQdcount = 1; | 
| andrewboyson | 13:9cd54f7db57a | 147 | DnsHdrAncount = 0; | 
| andrewboyson | 13:9cd54f7db57a | 148 | DnsHdrNscount = 0; | 
| andrewboyson | 13:9cd54f7db57a | 149 | DnsHdrArcount = 0; | 
| andrewboyson | 13:9cd54f7db57a | 150 | |
| andrewboyson | 13:9cd54f7db57a | 151 | DnsHdrWrite(); | 
| andrewboyson | 13:9cd54f7db57a | 152 | char* p = DnsHdrData; | 
| andrewboyson | 13:9cd54f7db57a | 153 | |
| andrewboyson | 30:e34173b7585c | 154 | if (DnsQueryIp4 ) DnsNameEncodeIp4(DnsQueryIp4, &p); | 
| andrewboyson | 30:e34173b7585c | 155 | else if (DnsQueryIp6[0]) DnsNameEncodeIp6(DnsQueryIp6, &p); | 
| andrewboyson | 37:793b39683406 | 156 | else DnsNameEncodePtr(DnsQueryName, &p); | 
| andrewboyson | 30:e34173b7585c | 157 | |
| andrewboyson | 30:e34173b7585c | 158 | *p++ = 0; | 
| andrewboyson | 30:e34173b7585c | 159 | *p++ = DnsQueryRecordType; | 
| andrewboyson | 13:9cd54f7db57a | 160 | *p++ = DnsQueryProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS | 
| andrewboyson | 13:9cd54f7db57a | 161 | *p++ = 1; //QCLASS_IN = 1 - internet | 
| andrewboyson | 13:9cd54f7db57a | 162 | |
| andrewboyson | 13:9cd54f7db57a | 163 | *pSize = p - DnsHdrPacket; | 
| andrewboyson | 13:9cd54f7db57a | 164 | |
| andrewboyson | 13:9cd54f7db57a | 165 | DnsQueryIsBusy = false; | 
| andrewboyson | 38:cc8945857a0d | 166 | |
| andrewboyson | 57:e0fb648acf48 | 167 | if (DnsQueryTrace || NetTraceHostGetMatched()) DnsHdrLog(DnsQueryProtocol); | 
| andrewboyson | 13:9cd54f7db57a | 168 | |
| andrewboyson | 37:793b39683406 | 169 | int dest = DO_NOTHING; | 
| andrewboyson | 37:793b39683406 | 170 | |
| andrewboyson | 13:9cd54f7db57a | 171 | switch (DnsQueryProtocol) | 
| andrewboyson | 13:9cd54f7db57a | 172 | { | 
| andrewboyson | 37:793b39683406 | 173 | case DNS_PROTOCOL_UDNS: dest = UNICAST_DNS; break; //IPv6 ==> NdpDnsServer; IPv4 ==> DhcpDnsServer | 
| andrewboyson | 37:793b39683406 | 174 | case DNS_PROTOCOL_MDNS: dest = MULTICAST_MDNS; break; | 
| andrewboyson | 37:793b39683406 | 175 | case DNS_PROTOCOL_LLMNR: dest = MULTICAST_LLMNR; break; | 
| andrewboyson | 13:9cd54f7db57a | 176 | default: | 
| andrewboyson | 13:9cd54f7db57a | 177 | LogTimeF("DNS unknown query protocol %d\r\n", DnsQueryProtocol); | 
| andrewboyson | 13:9cd54f7db57a | 178 | return DO_NOTHING; | 
| andrewboyson | 13:9cd54f7db57a | 179 | } | 
| andrewboyson | 37:793b39683406 | 180 | |
| andrewboyson | 57:e0fb648acf48 | 181 | return ActionMakeFromDestAndTrace(dest, DnsQueryTrace || NetTraceHostGetMatched()); | 
| andrewboyson | 13:9cd54f7db57a | 182 | } |