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
udp/dns/dnsreply.cpp
- Committer:
- andrewboyson
- Date:
- 2017-05-01
- Revision:
- 13:9cd54f7db57a
- Child:
- 14:e75a59c1123d
File content as of revision 13:9cd54f7db57a:
#include "mbed.h" #include "log.h" #include "net.h" #include "dns.h" #include "dnshdr.h" #include "dnsname.h" #include "ar.h" #define DEBUG true char DnsReplyRecordName[256]; uint32_t DnsReplyRecordNameAsIp4 = 0; char DnsReplyRecordNameAsIp6[16]; char DnsReplyName[64]; uint32_t DnsReplyIp4 = 0; char DnsReplyIp6[16]; static char *p; static int recordNameOffset; static int recordType; static int recordDataLength; static char* pRecordData; static int scanQuery() { int recordNameLength = DnsNameLength(p); if (!recordNameLength) { LogTimeF("DnsReply scanRecord name length of zero\r\n"); return -1; //failure } recordNameOffset = DnsNameIndexFromPointer(p); p += recordNameLength; p++ ; //skip the first byte of the type recordType = *p++; p += 2; //skip the class return 0; //success } static int scanAnswer() { int recordNameLength = DnsNameLength(p); if (!recordNameLength) { LogTimeF("DnsReply scanRecord name length of zero\r\n"); return -1; //failure } recordNameOffset = DnsNameIndexFromPointer(p); p += recordNameLength; p++ ; //skip the first byte of the type recordType = *p++; p += 6; //skip the class, TTL recordDataLength = 0; recordDataLength |= *p++ << 8; recordDataLength |= *p++; pRecordData = p; //record the start of the data p += recordDataLength; //Move to the start of the next record return 0; //success } static void readAnswer() { DnsReplyRecordName[0] = 0; DnsReplyRecordNameAsIp4 = 0; DnsReplyRecordNameAsIp6[0] = 0; DnsReplyName[0] = 0; DnsReplyIp4 = 0; DnsReplyIp6[0] = 0; switch (recordType) { case DNS_RECORD_A: case DNS_RECORD_AAAA: case DNS_RECORD_PTR: break; default: LogTimeF("DnsReply readAnswer unrecognised record type %d\r\n", recordType); return; } DnsNameDecode (recordNameOffset, sizeof(DnsReplyRecordName), DnsReplyRecordName); DnsNameDecodeIp4(recordNameOffset, &DnsReplyRecordNameAsIp4); DnsNameDecodeIp6(recordNameOffset, DnsReplyRecordNameAsIp6); if (DEBUG) { LogF(" record name as string %s\r\n", DnsReplyRecordName); char text[100]; if (DnsReplyRecordNameAsIp4) { NetIp4AddressToString(DnsReplyRecordNameAsIp4, sizeof(text), text); LogF(" record name as ip4 %s\r\n", text); } if (DnsReplyRecordNameAsIp6[0]) { NetIp6AddressToString(DnsReplyRecordNameAsIp6, sizeof(text), text); LogF(" record name as ip6 %s\r\n", text); } DnsRecordTypeToString(recordType, sizeof(text), text); LogF(" record type %s\r\n", text); LogF(" record data length %d bytes\r\n", recordDataLength); } switch (recordType) { case DNS_RECORD_A: if (recordDataLength != 4) { LogTimeF("DnsReply A type length of %d\r\n", recordDataLength); return; } memcpy(&DnsReplyIp4, pRecordData, 4); break; case DNS_RECORD_AAAA: if (recordDataLength != 16) { LogTimeF("DnsReply AAAA type length of %d\r\n", recordDataLength); return; } memcpy(DnsReplyIp6, pRecordData, 16); break; case DNS_RECORD_PTR: if (recordDataLength > 63) { LogTimeF("DnsReply PTR type length of %d\r\n", recordDataLength); return; } DnsNameDecode(DnsNameIndexFromPointer(pRecordData), sizeof(DnsReplyName), DnsReplyName); break; } if (DEBUG) { char text[100]; switch (recordType) { case DNS_RECORD_A: NetIp4AddressToString(DnsReplyIp4, sizeof(text), text); LogF(" ip4 %s\r\n", text); break; case DNS_RECORD_AAAA: NetIp6AddressToString(DnsReplyIp6, sizeof(text), text); LogF(" ip6 %s\r\n", text); break; case DNS_RECORD_PTR: LogF(" ptr %s\r\n", DnsReplyName); break; } } } static void sendToAr(int dnsProtocol) { if (DnsReplyIp4 && DnsReplyRecordName[0]) ArAddName4(DnsReplyIp4, DnsReplyRecordName, dnsProtocol); if (DnsReplyIp6[0] && DnsReplyRecordName[0]) ArAddName6(DnsReplyIp6, DnsReplyRecordName, dnsProtocol); if (DnsReplyRecordNameAsIp4 && DnsReplyName[0] ) ArAddName4(DnsReplyRecordNameAsIp4, DnsReplyName, dnsProtocol); if (DnsReplyRecordNameAsIp6[0] && DnsReplyName[0] ) ArAddName6(DnsReplyRecordNameAsIp6, DnsReplyName, dnsProtocol); } int DnsReplyHandle(int dnsProtocol, int *pSize) { if (!DnsHdrAncount) return DO_NOTHING; if (DEBUG) DnsHdrLog("received reply", dnsProtocol); p = DnsHdrData; for (int q = 0; q < DnsHdrQdcount; q++) { if (scanQuery()) return DO_NOTHING; } for (int a = 0; a < DnsHdrAncount; a++) { if (scanAnswer()) return DO_NOTHING; readAnswer(); sendToAr(dnsProtocol); } return DO_NOTHING; }