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 Jul 03 14:29:07 2017 +0000
Revision:
22:914b970356f0
Parent:
15:6ca6778168b1
Child:
30:e34173b7585c
Corrected NTP client function

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