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/dnsreply.cpp@48:952dddb74b8b, 2017-10-31 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Oct 31 21:25:09 2017 +0000
- Revision:
- 48:952dddb74b8b
- Parent:
- 43:bc028d5a6424
- Child:
- 50:492f2d2954e4
Split address resolution into AR4 and AR6. Corrected issue clearing a AR6 record.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 37:793b39683406 | 1 | #include "mbed.h" |
| andrewboyson | 37:793b39683406 | 2 | #include "log.h" |
| andrewboyson | 37:793b39683406 | 3 | #include "net.h" |
| andrewboyson | 37:793b39683406 | 4 | #include "action.h" |
| andrewboyson | 37:793b39683406 | 5 | #include "ip4.h" |
| andrewboyson | 37:793b39683406 | 6 | #include "ip6.h" |
| andrewboyson | 37:793b39683406 | 7 | #include "nr.h" |
| andrewboyson | 37:793b39683406 | 8 | #include "dns.h" |
| andrewboyson | 37:793b39683406 | 9 | #include "dnshdr.h" |
| andrewboyson | 37:793b39683406 | 10 | #include "dnsname.h" |
| andrewboyson | 37:793b39683406 | 11 | #include "dhcp.h" |
| andrewboyson | 13:9cd54f7db57a | 12 | |
| andrewboyson | 37:793b39683406 | 13 | bool DnsReplyTrace = false; |
| andrewboyson | 13:9cd54f7db57a | 14 | |
| andrewboyson | 13:9cd54f7db57a | 15 | char DnsReplyRecordName[256]; |
| andrewboyson | 13:9cd54f7db57a | 16 | uint32_t DnsReplyRecordNameAsIp4 = 0; |
| andrewboyson | 13:9cd54f7db57a | 17 | char DnsReplyRecordNameAsIp6[16]; |
| andrewboyson | 13:9cd54f7db57a | 18 | char DnsReplyName[64]; |
| andrewboyson | 13:9cd54f7db57a | 19 | uint32_t DnsReplyIp4 = 0; |
| andrewboyson | 13:9cd54f7db57a | 20 | char DnsReplyIp6[16]; |
| andrewboyson | 13:9cd54f7db57a | 21 | |
| andrewboyson | 13:9cd54f7db57a | 22 | static char *p; |
| andrewboyson | 13:9cd54f7db57a | 23 | static int recordNameOffset; |
| andrewboyson | 13:9cd54f7db57a | 24 | static int recordType; |
| andrewboyson | 13:9cd54f7db57a | 25 | static int recordDataLength; |
| andrewboyson | 13:9cd54f7db57a | 26 | static char* pRecordData; |
| andrewboyson | 13:9cd54f7db57a | 27 | |
| andrewboyson | 13:9cd54f7db57a | 28 | static int scanQuery() |
| andrewboyson | 13:9cd54f7db57a | 29 | { |
| andrewboyson | 13:9cd54f7db57a | 30 | int recordNameLength = DnsNameLength(p); |
| andrewboyson | 43:bc028d5a6424 | 31 | if (!recordNameLength) return -1; //failure |
| 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 | 43:bc028d5a6424 | 47 | if (!recordNameLength) return -1; //failure |
| andrewboyson | 13:9cd54f7db57a | 48 | |
| andrewboyson | 13:9cd54f7db57a | 49 | recordNameOffset = DnsNameIndexFromPointer(p); |
| andrewboyson | 13:9cd54f7db57a | 50 | p += recordNameLength; |
| andrewboyson | 13:9cd54f7db57a | 51 | |
| andrewboyson | 13:9cd54f7db57a | 52 | p++ ; //skip the first byte of the type |
| andrewboyson | 13:9cd54f7db57a | 53 | recordType = *p++; |
| andrewboyson | 13:9cd54f7db57a | 54 | |
| andrewboyson | 13:9cd54f7db57a | 55 | p += 6; //skip the class, TTL |
| andrewboyson | 13:9cd54f7db57a | 56 | recordDataLength = 0; |
| andrewboyson | 13:9cd54f7db57a | 57 | recordDataLength |= *p++ << 8; |
| andrewboyson | 13:9cd54f7db57a | 58 | recordDataLength |= *p++; |
| andrewboyson | 13:9cd54f7db57a | 59 | |
| andrewboyson | 13:9cd54f7db57a | 60 | pRecordData = p; //record the start of the data |
| andrewboyson | 13:9cd54f7db57a | 61 | |
| andrewboyson | 13:9cd54f7db57a | 62 | p += recordDataLength; //Move to the start of the next record |
| andrewboyson | 13:9cd54f7db57a | 63 | |
| andrewboyson | 13:9cd54f7db57a | 64 | return 0; //success |
| andrewboyson | 13:9cd54f7db57a | 65 | } |
| andrewboyson | 13:9cd54f7db57a | 66 | static void readAnswer() |
| andrewboyson | 13:9cd54f7db57a | 67 | { |
| andrewboyson | 13:9cd54f7db57a | 68 | DnsReplyRecordName[0] = 0; |
| andrewboyson | 13:9cd54f7db57a | 69 | DnsReplyRecordNameAsIp4 = 0; |
| andrewboyson | 13:9cd54f7db57a | 70 | DnsReplyRecordNameAsIp6[0] = 0; |
| andrewboyson | 13:9cd54f7db57a | 71 | DnsReplyName[0] = 0; |
| andrewboyson | 13:9cd54f7db57a | 72 | DnsReplyIp4 = 0; |
| andrewboyson | 13:9cd54f7db57a | 73 | DnsReplyIp6[0] = 0; |
| andrewboyson | 13:9cd54f7db57a | 74 | |
| andrewboyson | 13:9cd54f7db57a | 75 | switch (recordType) |
| andrewboyson | 13:9cd54f7db57a | 76 | { |
| andrewboyson | 13:9cd54f7db57a | 77 | case DNS_RECORD_A: |
| andrewboyson | 13:9cd54f7db57a | 78 | case DNS_RECORD_AAAA: |
| andrewboyson | 13:9cd54f7db57a | 79 | case DNS_RECORD_PTR: |
| andrewboyson | 15:6ca6778168b1 | 80 | case DNS_RECORD_SRV: |
| andrewboyson | 15:6ca6778168b1 | 81 | case DNS_RECORD_TXT: |
| andrewboyson | 13:9cd54f7db57a | 82 | break; |
| andrewboyson | 43:bc028d5a6424 | 83 | default: |
| andrewboyson | 15:6ca6778168b1 | 84 | return; |
| andrewboyson | 15:6ca6778168b1 | 85 | } |
| andrewboyson | 13:9cd54f7db57a | 86 | |
| andrewboyson | 37:793b39683406 | 87 | DnsNameDecodePtr(recordNameOffset, sizeof(DnsReplyRecordName), DnsReplyRecordName); |
| andrewboyson | 13:9cd54f7db57a | 88 | DnsNameDecodeIp4(recordNameOffset, &DnsReplyRecordNameAsIp4); |
| andrewboyson | 13:9cd54f7db57a | 89 | DnsNameDecodeIp6(recordNameOffset, DnsReplyRecordNameAsIp6); |
| andrewboyson | 15:6ca6778168b1 | 90 | |
| andrewboyson | 13:9cd54f7db57a | 91 | switch (recordType) |
| andrewboyson | 13:9cd54f7db57a | 92 | { |
| andrewboyson | 13:9cd54f7db57a | 93 | case DNS_RECORD_A: |
| andrewboyson | 43:bc028d5a6424 | 94 | if (recordDataLength != 4) return; |
| andrewboyson | 13:9cd54f7db57a | 95 | memcpy(&DnsReplyIp4, pRecordData, 4); |
| andrewboyson | 13:9cd54f7db57a | 96 | break; |
| andrewboyson | 13:9cd54f7db57a | 97 | case DNS_RECORD_AAAA: |
| andrewboyson | 43:bc028d5a6424 | 98 | if (recordDataLength != 16) return; |
| andrewboyson | 13:9cd54f7db57a | 99 | memcpy(DnsReplyIp6, pRecordData, 16); |
| andrewboyson | 13:9cd54f7db57a | 100 | break; |
| andrewboyson | 13:9cd54f7db57a | 101 | case DNS_RECORD_PTR: |
| andrewboyson | 43:bc028d5a6424 | 102 | if (recordDataLength > DNS_MAX_LABEL_LENGTH) return; |
| andrewboyson | 37:793b39683406 | 103 | DnsNameDecodePtr(DnsNameIndexFromPointer(pRecordData), sizeof(DnsReplyName), DnsReplyName); |
| andrewboyson | 13:9cd54f7db57a | 104 | break; |
| andrewboyson | 13:9cd54f7db57a | 105 | } |
| andrewboyson | 13:9cd54f7db57a | 106 | } |
| andrewboyson | 13:9cd54f7db57a | 107 | |
| andrewboyson | 32:679654f2d023 | 108 | static void sendToDnsCache(int dnsProtocol) |
| andrewboyson | 30:e34173b7585c | 109 | { |
| andrewboyson | 32:679654f2d023 | 110 | char strippedName[100]; |
| andrewboyson | 32:679654f2d023 | 111 | if (DnsReplyRecordName[0]) DnsStripNameFromFullName(dnsProtocol, DnsReplyRecordName, sizeof(strippedName), strippedName); |
| andrewboyson | 32:679654f2d023 | 112 | if (DnsReplyName[0] ) DnsStripNameFromFullName(dnsProtocol, DnsReplyName , sizeof(strippedName), strippedName); |
| andrewboyson | 32:679654f2d023 | 113 | |
| andrewboyson | 35:93c39d260a83 | 114 | if (DnsReplyIp4 && DnsReplyRecordName[0]) NrAddIp4Record(DnsReplyIp4, strippedName, dnsProtocol); |
| andrewboyson | 35:93c39d260a83 | 115 | if (DnsReplyIp6[0] && DnsReplyRecordName[0]) NrAddIp6Record(DnsReplyIp6, strippedName, dnsProtocol); |
| andrewboyson | 35:93c39d260a83 | 116 | if (DnsReplyRecordNameAsIp4 && DnsReplyName[0] ) NrAddIp4Record(DnsReplyRecordNameAsIp4, strippedName, dnsProtocol); |
| andrewboyson | 35:93c39d260a83 | 117 | if (DnsReplyRecordNameAsIp6[0] && DnsReplyName[0] ) NrAddIp6Record(DnsReplyRecordNameAsIp6, strippedName, dnsProtocol); |
| andrewboyson | 30:e34173b7585c | 118 | } |
| andrewboyson | 37:793b39683406 | 119 | int DnsReplyHandle(void (*traceback)(void), int dnsProtocol, int *pSize) |
| andrewboyson | 13:9cd54f7db57a | 120 | { |
| andrewboyson | 13:9cd54f7db57a | 121 | if (!DnsHdrAncount) return DO_NOTHING; |
| andrewboyson | 37:793b39683406 | 122 | if (DnsReplyTrace) |
| andrewboyson | 37:793b39683406 | 123 | { |
| andrewboyson | 43:bc028d5a6424 | 124 | if (NetTraceNewLine) Log("\r\n"); |
| andrewboyson | 43:bc028d5a6424 | 125 | LogTimeF("DnsReply received\r\n"); |
| andrewboyson | 43:bc028d5a6424 | 126 | if (NetTraceStack) traceback(); |
| andrewboyson | 37:793b39683406 | 127 | DnsHdrLog(dnsProtocol); |
| andrewboyson | 37:793b39683406 | 128 | } |
| andrewboyson | 13:9cd54f7db57a | 129 | |
| andrewboyson | 13:9cd54f7db57a | 130 | p = DnsHdrData; |
| andrewboyson | 13:9cd54f7db57a | 131 | |
| andrewboyson | 13:9cd54f7db57a | 132 | for (int q = 0; q < DnsHdrQdcount; q++) |
| andrewboyson | 13:9cd54f7db57a | 133 | { |
| andrewboyson | 13:9cd54f7db57a | 134 | if (scanQuery()) return DO_NOTHING; |
| andrewboyson | 13:9cd54f7db57a | 135 | } |
| andrewboyson | 13:9cd54f7db57a | 136 | for (int a = 0; a < DnsHdrAncount; a++) |
| andrewboyson | 13:9cd54f7db57a | 137 | { |
| andrewboyson | 13:9cd54f7db57a | 138 | if (scanAnswer()) return DO_NOTHING; |
| andrewboyson | 13:9cd54f7db57a | 139 | readAnswer(); |
| andrewboyson | 32:679654f2d023 | 140 | sendToDnsCache(dnsProtocol); |
| andrewboyson | 13:9cd54f7db57a | 141 | } |
| andrewboyson | 13:9cd54f7db57a | 142 | |
| andrewboyson | 13:9cd54f7db57a | 143 | return DO_NOTHING; |
| andrewboyson | 13:9cd54f7db57a | 144 | } |