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:
Mon May 01 18:20:55 2017 +0000
Revision:
13:9cd54f7db57a
Child:
14:e75a59c1123d
Added ability to read DNS queries with encoded IP addresses

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 13:9cd54f7db57a 3 #include "net.h"
andrewboyson 13:9cd54f7db57a 4 #include "dns.h"
andrewboyson 13:9cd54f7db57a 5 #include "dnshdr.h"
andrewboyson 13:9cd54f7db57a 6 #include "dnsname.h"
andrewboyson 13:9cd54f7db57a 7 #include "ar.h"
andrewboyson 13:9cd54f7db57a 8
andrewboyson 13:9cd54f7db57a 9 #define DEBUG true
andrewboyson 13:9cd54f7db57a 10
andrewboyson 13:9cd54f7db57a 11 char DnsReplyRecordName[256];
andrewboyson 13:9cd54f7db57a 12 uint32_t DnsReplyRecordNameAsIp4 = 0;
andrewboyson 13:9cd54f7db57a 13 char DnsReplyRecordNameAsIp6[16];
andrewboyson 13:9cd54f7db57a 14 char DnsReplyName[64];
andrewboyson 13:9cd54f7db57a 15 uint32_t DnsReplyIp4 = 0;
andrewboyson 13:9cd54f7db57a 16 char DnsReplyIp6[16];
andrewboyson 13:9cd54f7db57a 17
andrewboyson 13:9cd54f7db57a 18 static char *p;
andrewboyson 13:9cd54f7db57a 19 static int recordNameOffset;
andrewboyson 13:9cd54f7db57a 20 static int recordType;
andrewboyson 13:9cd54f7db57a 21 static int recordDataLength;
andrewboyson 13:9cd54f7db57a 22 static char* pRecordData;
andrewboyson 13:9cd54f7db57a 23
andrewboyson 13:9cd54f7db57a 24 static int scanQuery()
andrewboyson 13:9cd54f7db57a 25 {
andrewboyson 13:9cd54f7db57a 26 int recordNameLength = DnsNameLength(p);
andrewboyson 13:9cd54f7db57a 27 if (!recordNameLength)
andrewboyson 13:9cd54f7db57a 28 {
andrewboyson 13:9cd54f7db57a 29 LogTimeF("DnsReply scanRecord name length of zero\r\n");
andrewboyson 13:9cd54f7db57a 30 return -1; //failure
andrewboyson 13:9cd54f7db57a 31 }
andrewboyson 13:9cd54f7db57a 32
andrewboyson 13:9cd54f7db57a 33 recordNameOffset = DnsNameIndexFromPointer(p);
andrewboyson 13:9cd54f7db57a 34 p += recordNameLength;
andrewboyson 13:9cd54f7db57a 35
andrewboyson 13:9cd54f7db57a 36 p++ ; //skip the first byte of the type
andrewboyson 13:9cd54f7db57a 37 recordType = *p++;
andrewboyson 13:9cd54f7db57a 38
andrewboyson 13:9cd54f7db57a 39 p += 2; //skip the class
andrewboyson 13:9cd54f7db57a 40
andrewboyson 13:9cd54f7db57a 41 return 0; //success
andrewboyson 13:9cd54f7db57a 42 }
andrewboyson 13:9cd54f7db57a 43
andrewboyson 13:9cd54f7db57a 44 static int scanAnswer()
andrewboyson 13:9cd54f7db57a 45 {
andrewboyson 13:9cd54f7db57a 46 int recordNameLength = DnsNameLength(p);
andrewboyson 13:9cd54f7db57a 47 if (!recordNameLength)
andrewboyson 13:9cd54f7db57a 48 {
andrewboyson 13:9cd54f7db57a 49 LogTimeF("DnsReply scanRecord name length of zero\r\n");
andrewboyson 13:9cd54f7db57a 50 return -1; //failure
andrewboyson 13:9cd54f7db57a 51 }
andrewboyson 13:9cd54f7db57a 52
andrewboyson 13:9cd54f7db57a 53 recordNameOffset = DnsNameIndexFromPointer(p);
andrewboyson 13:9cd54f7db57a 54 p += recordNameLength;
andrewboyson 13:9cd54f7db57a 55
andrewboyson 13:9cd54f7db57a 56 p++ ; //skip the first byte of the type
andrewboyson 13:9cd54f7db57a 57 recordType = *p++;
andrewboyson 13:9cd54f7db57a 58
andrewboyson 13:9cd54f7db57a 59 p += 6; //skip the class, TTL
andrewboyson 13:9cd54f7db57a 60 recordDataLength = 0;
andrewboyson 13:9cd54f7db57a 61 recordDataLength |= *p++ << 8;
andrewboyson 13:9cd54f7db57a 62 recordDataLength |= *p++;
andrewboyson 13:9cd54f7db57a 63
andrewboyson 13:9cd54f7db57a 64 pRecordData = p; //record the start of the data
andrewboyson 13:9cd54f7db57a 65
andrewboyson 13:9cd54f7db57a 66 p += recordDataLength; //Move to the start of the next record
andrewboyson 13:9cd54f7db57a 67
andrewboyson 13:9cd54f7db57a 68 return 0; //success
andrewboyson 13:9cd54f7db57a 69 }
andrewboyson 13:9cd54f7db57a 70 static void readAnswer()
andrewboyson 13:9cd54f7db57a 71 {
andrewboyson 13:9cd54f7db57a 72 DnsReplyRecordName[0] = 0;
andrewboyson 13:9cd54f7db57a 73 DnsReplyRecordNameAsIp4 = 0;
andrewboyson 13:9cd54f7db57a 74 DnsReplyRecordNameAsIp6[0] = 0;
andrewboyson 13:9cd54f7db57a 75 DnsReplyName[0] = 0;
andrewboyson 13:9cd54f7db57a 76 DnsReplyIp4 = 0;
andrewboyson 13:9cd54f7db57a 77 DnsReplyIp6[0] = 0;
andrewboyson 13:9cd54f7db57a 78
andrewboyson 13:9cd54f7db57a 79 switch (recordType)
andrewboyson 13:9cd54f7db57a 80 {
andrewboyson 13:9cd54f7db57a 81 case DNS_RECORD_A:
andrewboyson 13:9cd54f7db57a 82 case DNS_RECORD_AAAA:
andrewboyson 13:9cd54f7db57a 83 case DNS_RECORD_PTR:
andrewboyson 13:9cd54f7db57a 84 break;
andrewboyson 13:9cd54f7db57a 85 default:
andrewboyson 13:9cd54f7db57a 86 LogTimeF("DnsReply readAnswer unrecognised record type %d\r\n", recordType);
andrewboyson 13:9cd54f7db57a 87 return; }
andrewboyson 13:9cd54f7db57a 88
andrewboyson 13:9cd54f7db57a 89 DnsNameDecode (recordNameOffset, sizeof(DnsReplyRecordName), DnsReplyRecordName);
andrewboyson 13:9cd54f7db57a 90 DnsNameDecodeIp4(recordNameOffset, &DnsReplyRecordNameAsIp4);
andrewboyson 13:9cd54f7db57a 91 DnsNameDecodeIp6(recordNameOffset, DnsReplyRecordNameAsIp6);
andrewboyson 13:9cd54f7db57a 92 if (DEBUG)
andrewboyson 13:9cd54f7db57a 93 {
andrewboyson 13:9cd54f7db57a 94 LogF(" record name as string %s\r\n", DnsReplyRecordName);
andrewboyson 13:9cd54f7db57a 95 char text[100];
andrewboyson 13:9cd54f7db57a 96 if (DnsReplyRecordNameAsIp4)
andrewboyson 13:9cd54f7db57a 97 {
andrewboyson 13:9cd54f7db57a 98 NetIp4AddressToString(DnsReplyRecordNameAsIp4, sizeof(text), text);
andrewboyson 13:9cd54f7db57a 99 LogF(" record name as ip4 %s\r\n", text);
andrewboyson 13:9cd54f7db57a 100 }
andrewboyson 13:9cd54f7db57a 101 if (DnsReplyRecordNameAsIp6[0])
andrewboyson 13:9cd54f7db57a 102 {
andrewboyson 13:9cd54f7db57a 103 NetIp6AddressToString(DnsReplyRecordNameAsIp6, sizeof(text), text);
andrewboyson 13:9cd54f7db57a 104 LogF(" record name as ip6 %s\r\n", text);
andrewboyson 13:9cd54f7db57a 105 }
andrewboyson 13:9cd54f7db57a 106 DnsRecordTypeToString(recordType, sizeof(text), text);
andrewboyson 13:9cd54f7db57a 107 LogF(" record type %s\r\n", text);
andrewboyson 13:9cd54f7db57a 108 LogF(" record data length %d bytes\r\n", recordDataLength);
andrewboyson 13:9cd54f7db57a 109 }
andrewboyson 13:9cd54f7db57a 110
andrewboyson 13:9cd54f7db57a 111 switch (recordType)
andrewboyson 13:9cd54f7db57a 112 {
andrewboyson 13:9cd54f7db57a 113 case DNS_RECORD_A:
andrewboyson 13:9cd54f7db57a 114 if (recordDataLength != 4)
andrewboyson 13:9cd54f7db57a 115 {
andrewboyson 13:9cd54f7db57a 116 LogTimeF("DnsReply A type length of %d\r\n", recordDataLength);
andrewboyson 13:9cd54f7db57a 117 return;
andrewboyson 13:9cd54f7db57a 118 }
andrewboyson 13:9cd54f7db57a 119 memcpy(&DnsReplyIp4, pRecordData, 4);
andrewboyson 13:9cd54f7db57a 120 break;
andrewboyson 13:9cd54f7db57a 121 case DNS_RECORD_AAAA:
andrewboyson 13:9cd54f7db57a 122 if (recordDataLength != 16)
andrewboyson 13:9cd54f7db57a 123 {
andrewboyson 13:9cd54f7db57a 124 LogTimeF("DnsReply AAAA type length of %d\r\n", recordDataLength);
andrewboyson 13:9cd54f7db57a 125 return;
andrewboyson 13:9cd54f7db57a 126 }
andrewboyson 13:9cd54f7db57a 127 memcpy(DnsReplyIp6, pRecordData, 16);
andrewboyson 13:9cd54f7db57a 128 break;
andrewboyson 13:9cd54f7db57a 129 case DNS_RECORD_PTR:
andrewboyson 13:9cd54f7db57a 130 if (recordDataLength > 63)
andrewboyson 13:9cd54f7db57a 131 {
andrewboyson 13:9cd54f7db57a 132 LogTimeF("DnsReply PTR type length of %d\r\n", recordDataLength);
andrewboyson 13:9cd54f7db57a 133 return;
andrewboyson 13:9cd54f7db57a 134 }
andrewboyson 13:9cd54f7db57a 135 DnsNameDecode(DnsNameIndexFromPointer(pRecordData), sizeof(DnsReplyName), DnsReplyName);
andrewboyson 13:9cd54f7db57a 136 break;
andrewboyson 13:9cd54f7db57a 137 }
andrewboyson 13:9cd54f7db57a 138 if (DEBUG)
andrewboyson 13:9cd54f7db57a 139 {
andrewboyson 13:9cd54f7db57a 140 char text[100];
andrewboyson 13:9cd54f7db57a 141 switch (recordType)
andrewboyson 13:9cd54f7db57a 142 {
andrewboyson 13:9cd54f7db57a 143 case DNS_RECORD_A:
andrewboyson 13:9cd54f7db57a 144 NetIp4AddressToString(DnsReplyIp4, sizeof(text), text);
andrewboyson 13:9cd54f7db57a 145 LogF(" ip4 %s\r\n", text);
andrewboyson 13:9cd54f7db57a 146 break;
andrewboyson 13:9cd54f7db57a 147 case DNS_RECORD_AAAA:
andrewboyson 13:9cd54f7db57a 148 NetIp6AddressToString(DnsReplyIp6, sizeof(text), text);
andrewboyson 13:9cd54f7db57a 149 LogF(" ip6 %s\r\n", text);
andrewboyson 13:9cd54f7db57a 150 break;
andrewboyson 13:9cd54f7db57a 151 case DNS_RECORD_PTR:
andrewboyson 13:9cd54f7db57a 152 LogF(" ptr %s\r\n", DnsReplyName);
andrewboyson 13:9cd54f7db57a 153 break;
andrewboyson 13:9cd54f7db57a 154 }
andrewboyson 13:9cd54f7db57a 155 }
andrewboyson 13:9cd54f7db57a 156 }
andrewboyson 13:9cd54f7db57a 157
andrewboyson 13:9cd54f7db57a 158 static void sendToAr(int dnsProtocol)
andrewboyson 13:9cd54f7db57a 159 {
andrewboyson 13:9cd54f7db57a 160 if (DnsReplyIp4 && DnsReplyRecordName[0]) ArAddName4(DnsReplyIp4, DnsReplyRecordName, dnsProtocol);
andrewboyson 13:9cd54f7db57a 161 if (DnsReplyIp6[0] && DnsReplyRecordName[0]) ArAddName6(DnsReplyIp6, DnsReplyRecordName, dnsProtocol);
andrewboyson 13:9cd54f7db57a 162 if (DnsReplyRecordNameAsIp4 && DnsReplyName[0] ) ArAddName4(DnsReplyRecordNameAsIp4, DnsReplyName, dnsProtocol);
andrewboyson 13:9cd54f7db57a 163 if (DnsReplyRecordNameAsIp6[0] && DnsReplyName[0] ) ArAddName6(DnsReplyRecordNameAsIp6, DnsReplyName, dnsProtocol);
andrewboyson 13:9cd54f7db57a 164 }
andrewboyson 13:9cd54f7db57a 165 int DnsReplyHandle(int dnsProtocol, int *pSize)
andrewboyson 13:9cd54f7db57a 166 {
andrewboyson 13:9cd54f7db57a 167 if (!DnsHdrAncount) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 168 if (DEBUG) DnsHdrLog("received reply", dnsProtocol);
andrewboyson 13:9cd54f7db57a 169
andrewboyson 13:9cd54f7db57a 170 p = DnsHdrData;
andrewboyson 13:9cd54f7db57a 171
andrewboyson 13:9cd54f7db57a 172 for (int q = 0; q < DnsHdrQdcount; q++)
andrewboyson 13:9cd54f7db57a 173 {
andrewboyson 13:9cd54f7db57a 174 if (scanQuery()) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 175 }
andrewboyson 13:9cd54f7db57a 176 for (int a = 0; a < DnsHdrAncount; a++)
andrewboyson 13:9cd54f7db57a 177 {
andrewboyson 13:9cd54f7db57a 178 if (scanAnswer()) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 179 readAnswer();
andrewboyson 13:9cd54f7db57a 180 sendToAr(dnsProtocol);
andrewboyson 13:9cd54f7db57a 181 }
andrewboyson 13:9cd54f7db57a 182
andrewboyson 13:9cd54f7db57a 183 return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 184 }