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

Committer:
andrewboyson
Date:
Thu Oct 26 14:50:24 2017 +0000
Revision:
47:73af5c0b0dc2
Parent:
43:bc028d5a6424
Child:
49:1a6336f2b3f9
Replaced a number of temporary buffers with direct writes to the Log or HTTP.

Who changed what in which revision?

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