7 #include "NetClientDns.hpp"
8 #include "LibEndian.hpp"
9 #include "RtosInclude.hpp"
10 #include "LibStreamOutArray.hpp"
12 NetUdpSocket NetClientDns::sSocket;
15 bool NetClientDns::resolve(
const char* name, NetDefs::IP_ADDR* ipAddr) {
19 NetUdpSocket::TX_PACKET* txPkt = sSocket.allocTxPacket(512);
28 NetDefs::DNS_PKT* dnsPkt = (NetDefs::DNS_PKT*) txPkt->data;
31 dnsPkt->hdr.flags.value = 0;
32 dnsPkt->hdr.flags.bits.qr =
true;
33 dnsPkt->hdr.questionCount = 1;
34 dnsPkt->hdr.answerRecordCount = 0;
35 dnsPkt->hdr.authorityRecordCount = 0;
36 dnsPkt->hdr.additionalRecordCount = 0;
37 swapHeader(&dnsPkt->hdr);
40 LibStreamOutArray stream(dnsPkt->data, 512 -
sizeof(dnsPkt->hdr));
41 for (
int i = 0; loop;) {
44 char c = name[next++];
52 else if (next >= 64) {
57 uint8_t len = (next - i) - 1;
58 stream.write(&len, 1);
59 stream.write((uint8_t*) &name[i], len);
64 0x00, 0x00, 0x01, 0x00, 0x01 };
65 stream.write(trail,
sizeof(trail));
68 txPkt->hdr.ip = Net::getConfig().dnsIpAddr;
69 sSocket.sendPacket(*txPkt,
sizeof(dnsPkt->hdr) + stream.getPosition());
77 NetDefs::DNS_PKT* dnsPkt = (NetDefs::DNS_PKT*) rxPkt->data;
78 swapHeader(&dnsPkt->hdr);
79 if (dnsPkt->hdr.id ==
id && dnsPkt->hdr.answerRecordCount > 0 && dnsPkt->hdr.flags.bits.qr ) {
83 for (
int count = 0; count < dnsPkt->hdr.questionCount; count++) {
84 if (dnsPkt->data[i] < 64) {
85 while (dnsPkt->data[i++] != 0) {
89 else if (dnsPkt->data[i] >= 192) {
99 for (
int count = 0; !exit && count < dnsPkt->hdr.answerRecordCount; count++) {
100 if (dnsPkt->data[i] < 64) {
101 while (dnsPkt->data[i++] != 0) {
104 else if (dnsPkt->data[i] >= 192) {
112 if ((dnsPkt->data[i] == 0) && (dnsPkt->data[i + 1] == 1) &&
113 (dnsPkt->data[i + 2] == 0) && (dnsPkt->data[i + 3] == 1) &&
114 (dnsPkt->data[i + 8] == 0) && (dnsPkt->data[i + 9] == 4)) {
115 memcpy(ipAddr, &dnsPkt->data[i + 10], 4);
122 i += 10 + (uint16_t) dnsPkt->data[i + 8] + (uint16_t) dnsPkt->data[i + 9];
126 sSocket.discardRxPacket(rxPkt);
136 void NetClientDns::swapHeader(NetDefs::DNS_HEADER* header) {
137 header->id = LibEndian::hwToBe(header->id);
138 header->questionCount = LibEndian::hwToBe(header->questionCount);
139 header->answerRecordCount = LibEndian::hwToBe(header->answerRecordCount);
140 header->authorityRecordCount = LibEndian::hwToBe(header->authorityRecordCount);
141 header->additionalRecordCount = LibEndian::hwToBe(header->additionalRecordCount);