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:
Sat Dec 12 20:10:02 2020 +0000
Revision:
171:f708d6776752
Parent:
133:a37eb35a03f1
Child:
172:9bc3c7b2cca1
Modified NR to accept both IPV6 and IPV4 addresses instead of having two modules with diffrent address lengths. Encapsulated 32but address into lsb 128 bit address

Who changed what in which revision?

UserRevisionLine numberNew 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 30:e34173b7585c 27 bool DnsQueryIsBusy = false;
andrewboyson 13:9cd54f7db57a 28
andrewboyson 171:f708d6776752 29 static char _RecordType = DNS_RECORD_NONE;
andrewboyson 171:f708d6776752 30 static int _DnsProtocol = DNS_PROTOCOL_NONE;
andrewboyson 171:f708d6776752 31 static int _IpProtocol = 0;
andrewboyson 171:f708d6776752 32 static uint32_t _StartedMs = 0;
andrewboyson 171:f708d6776752 33
andrewboyson 13:9cd54f7db57a 34 static void reap()
andrewboyson 13:9cd54f7db57a 35 {
andrewboyson 13:9cd54f7db57a 36 if (!DnsQueryIsBusy) return;
andrewboyson 13:9cd54f7db57a 37
andrewboyson 171:f708d6776752 38 if (MsTimerRelative(_StartedMs, TIME_OUT_SENT_MS))
andrewboyson 13:9cd54f7db57a 39 {
andrewboyson 13:9cd54f7db57a 40 LogTimeF("DNS reaped ongoing request for ");
andrewboyson 13:9cd54f7db57a 41 if (DnsQueryName[0]) LogF("name %s", DnsQueryName);
andrewboyson 13:9cd54f7db57a 42 if (DnsQueryIp4)
andrewboyson 13:9cd54f7db57a 43 {
andrewboyson 47:73af5c0b0dc2 44 Log("ip4 "); Ip4AddressLog(DnsQueryIp4);
andrewboyson 13:9cd54f7db57a 45 }
andrewboyson 13:9cd54f7db57a 46 if (DnsQueryIp6[0])
andrewboyson 13:9cd54f7db57a 47 {
andrewboyson 47:73af5c0b0dc2 48 Log("ip6 "); Ip6AddressLog(DnsQueryIp6);
andrewboyson 13:9cd54f7db57a 49 }
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 171:f708d6776752 68 DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName);
andrewboyson 171:f708d6776752 69 DnsQueryIp4 = 0;
andrewboyson 171:f708d6776752 70 DnsQueryIp6[0] = 0;
andrewboyson 171:f708d6776752 71 DnsQueryIsBusy = true;
andrewboyson 171:f708d6776752 72 _StartedMs = MsTimerCount;
andrewboyson 171:f708d6776752 73 _DnsProtocol = dnsProtocol;
andrewboyson 171:f708d6776752 74 _IpProtocol = ipProtocol;
andrewboyson 171:f708d6776752 75 _RecordType = DNS_RECORD_A;
andrewboyson 13:9cd54f7db57a 76 }
andrewboyson 171:f708d6776752 77 void DnsQueryIp6FromName(char * name, int dnsProtocol, int ipProtocol)
andrewboyson 13:9cd54f7db57a 78 {
andrewboyson 171:f708d6776752 79 DnsLabelMakeFullNameFromName(dnsProtocol, name, sizeof(DnsQueryName), DnsQueryName);
andrewboyson 171:f708d6776752 80 DnsQueryIp4 = 0;
andrewboyson 171:f708d6776752 81 DnsQueryIp6[0] = 0;
andrewboyson 171:f708d6776752 82 DnsQueryIsBusy = true;
andrewboyson 171:f708d6776752 83 _StartedMs = MsTimerCount;
andrewboyson 171:f708d6776752 84 _DnsProtocol = dnsProtocol;
andrewboyson 171:f708d6776752 85 _IpProtocol = ipProtocol;
andrewboyson 171:f708d6776752 86 _RecordType = DNS_RECORD_AAAA;
andrewboyson 13:9cd54f7db57a 87 }
andrewboyson 171:f708d6776752 88 void DnsQueryNameFromIp4(uint32_t ip, int dnsProtocol, int ipProtocol)
andrewboyson 13:9cd54f7db57a 89 {
andrewboyson 171:f708d6776752 90 DnsQueryName[0] = 0;
andrewboyson 171:f708d6776752 91 DnsQueryIp4 = ip;
andrewboyson 171:f708d6776752 92 DnsQueryIp6[0] = 0;
andrewboyson 171:f708d6776752 93 DnsQueryIsBusy = true;
andrewboyson 171:f708d6776752 94 _StartedMs = MsTimerCount;
andrewboyson 171:f708d6776752 95 _DnsProtocol = dnsProtocol;
andrewboyson 171:f708d6776752 96 _IpProtocol = ipProtocol;
andrewboyson 171:f708d6776752 97 _RecordType = DNS_RECORD_PTR;
andrewboyson 13:9cd54f7db57a 98 }
andrewboyson 171:f708d6776752 99 void DnsQueryNameFromIp6(char* ip, int dnsProtocol, int ipProtocol)
andrewboyson 13:9cd54f7db57a 100 {
andrewboyson 171:f708d6776752 101 DnsQueryName[0] = 0;
andrewboyson 171:f708d6776752 102 DnsQueryIp4 = 0;
andrewboyson 49:1a6336f2b3f9 103 Ip6AddressCopy(DnsQueryIp6, ip);
andrewboyson 171:f708d6776752 104 DnsQueryIsBusy = true;
andrewboyson 171:f708d6776752 105 _StartedMs = MsTimerCount;
andrewboyson 171:f708d6776752 106 _DnsProtocol = dnsProtocol;
andrewboyson 171:f708d6776752 107 _IpProtocol = ipProtocol;
andrewboyson 171:f708d6776752 108 _RecordType = DNS_RECORD_PTR;
andrewboyson 30:e34173b7585c 109 }
andrewboyson 30:e34173b7585c 110 static void logQuery()
andrewboyson 30:e34173b7585c 111 {
andrewboyson 43:bc028d5a6424 112 if (NetTraceNewLine) Log("\r\n");
andrewboyson 47:73af5c0b0dc2 113 LogTimeF("DnsQuery sent ");
andrewboyson 171:f708d6776752 114 DnsProtocolLog(_DnsProtocol);
andrewboyson 47:73af5c0b0dc2 115 Log(" request for ");
andrewboyson 171:f708d6776752 116 DnsRecordTypeLog(_RecordType);
andrewboyson 47:73af5c0b0dc2 117 Log(" ");
andrewboyson 30:e34173b7585c 118 if (DnsQueryIp4) //Reverse
andrewboyson 30:e34173b7585c 119 {
andrewboyson 47:73af5c0b0dc2 120 Ip4AddressLog(DnsQueryIp4);
andrewboyson 30:e34173b7585c 121 }
andrewboyson 30:e34173b7585c 122 else if (DnsQueryIp6[0])
andrewboyson 30:e34173b7585c 123 {
andrewboyson 47:73af5c0b0dc2 124 Ip6AddressLog(DnsQueryIp6);
andrewboyson 30:e34173b7585c 125 }
andrewboyson 30:e34173b7585c 126 else //Forward
andrewboyson 30:e34173b7585c 127 {
andrewboyson 47:73af5c0b0dc2 128 Log(DnsQueryName);
andrewboyson 30:e34173b7585c 129 }
andrewboyson 47:73af5c0b0dc2 130 Log("\r\n");
andrewboyson 13:9cd54f7db57a 131 }
andrewboyson 171:f708d6776752 132 int DnsQueryPoll(int ipType, void* pPacket, int* pSize)
andrewboyson 13:9cd54f7db57a 133 {
andrewboyson 59:e0e556c8bd46 134 DnsHdrSetup(pPacket, *pSize);
andrewboyson 59:e0e556c8bd46 135
andrewboyson 171:f708d6776752 136 if (!DnsQueryIsBusy) return DO_NOTHING;
andrewboyson 171:f708d6776752 137 if (_DnsProtocol == DNS_PROTOCOL_UDNS && DhcpLocalIp == 0) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 138
andrewboyson 57:e0fb648acf48 139 NetTraceHostCheckIp6(DnsQueryIp6);
andrewboyson 57:e0fb648acf48 140
andrewboyson 57:e0fb648acf48 141 if (DnsQueryTrace || NetTraceHostGetMatched()) logQuery();
andrewboyson 30:e34173b7585c 142
andrewboyson 13:9cd54f7db57a 143 static uint16_t id = 0;
andrewboyson 13:9cd54f7db57a 144 DnsHdrId = ++id;
andrewboyson 13:9cd54f7db57a 145 DnsHdrIsReply = false;
andrewboyson 171:f708d6776752 146 DnsHdrIsAuthoritative = false; //Added 12/12/2020
andrewboyson 13:9cd54f7db57a 147 DnsHdrIsRecursiveQuery = false;
andrewboyson 13:9cd54f7db57a 148
andrewboyson 13:9cd54f7db57a 149 DnsHdrQdcount = 1;
andrewboyson 13:9cd54f7db57a 150 DnsHdrAncount = 0;
andrewboyson 13:9cd54f7db57a 151 DnsHdrNscount = 0;
andrewboyson 13:9cd54f7db57a 152 DnsHdrArcount = 0;
andrewboyson 13:9cd54f7db57a 153
andrewboyson 13:9cd54f7db57a 154 DnsHdrWrite();
andrewboyson 13:9cd54f7db57a 155 char* p = DnsHdrData;
andrewboyson 13:9cd54f7db57a 156
andrewboyson 30:e34173b7585c 157 if (DnsQueryIp4 ) DnsNameEncodeIp4(DnsQueryIp4, &p);
andrewboyson 30:e34173b7585c 158 else if (DnsQueryIp6[0]) DnsNameEncodeIp6(DnsQueryIp6, &p);
andrewboyson 37:793b39683406 159 else DnsNameEncodePtr(DnsQueryName, &p);
andrewboyson 30:e34173b7585c 160
andrewboyson 30:e34173b7585c 161 *p++ = 0;
andrewboyson 171:f708d6776752 162 *p++ = _RecordType;
andrewboyson 171:f708d6776752 163 *p++ = _DnsProtocol == DNS_PROTOCOL_MDNS && MDNS_UNICAST ? 0x80 : 0; //Set the 15th bit (UNICAST_RESPONSE) to 1 if MDNS
andrewboyson 13:9cd54f7db57a 164 *p++ = 1; //QCLASS_IN = 1 - internet
andrewboyson 13:9cd54f7db57a 165
andrewboyson 13:9cd54f7db57a 166 *pSize = p - DnsHdrPacket;
andrewboyson 13:9cd54f7db57a 167
andrewboyson 13:9cd54f7db57a 168 DnsQueryIsBusy = false;
andrewboyson 38:cc8945857a0d 169
andrewboyson 171:f708d6776752 170 if (DnsQueryTrace || NetTraceHostGetMatched()) DnsHdrLog(_DnsProtocol);
andrewboyson 13:9cd54f7db57a 171
andrewboyson 37:793b39683406 172 int dest = DO_NOTHING;
andrewboyson 37:793b39683406 173
andrewboyson 171:f708d6776752 174 switch (_DnsProtocol)
andrewboyson 13:9cd54f7db57a 175 {
andrewboyson 37:793b39683406 176 case DNS_PROTOCOL_UDNS: dest = UNICAST_DNS; break; //IPv6 ==> NdpDnsServer; IPv4 ==> DhcpDnsServer
andrewboyson 37:793b39683406 177 case DNS_PROTOCOL_MDNS: dest = MULTICAST_MDNS; break;
andrewboyson 37:793b39683406 178 case DNS_PROTOCOL_LLMNR: dest = MULTICAST_LLMNR; break;
andrewboyson 13:9cd54f7db57a 179 default:
andrewboyson 171:f708d6776752 180 LogTimeF("DNS unknown query protocol %d\r\n", _DnsProtocol);
andrewboyson 13:9cd54f7db57a 181 return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 182 }
andrewboyson 37:793b39683406 183
andrewboyson 57:e0fb648acf48 184 return ActionMakeFromDestAndTrace(dest, DnsQueryTrace || NetTraceHostGetMatched());
andrewboyson 13:9cd54f7db57a 185 }