WIZnetInterface using namespace

Dependents:   DualNetworkInterface-Basic

Fork of WIZnetInterface by WIZnet

Committer:
SteveKim
Date:
Tue Jul 14 10:16:16 2015 +0000
Revision:
20:3e61863c1f67
Parent:
0:6f28332c466f
Dual Network Interface for mbed;  - WIZwiki-W7500;  - ESP8266

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Soohwan Kim 0:6f28332c466f 1 // DNSClient.cpp 2013/8/27
Soohwan Kim 0:6f28332c466f 2 #include "mbed.h"
Soohwan Kim 0:6f28332c466f 3 #include "mbed_debug.h"
SteveKim 20:3e61863c1f67 4 #include "DNSClient.hpp"
SteveKim 20:3e61863c1f67 5 #include "UDPSocket.hpp"
SteveKim 20:3e61863c1f67 6 #include "dnsname.hpp"
SteveKim 20:3e61863c1f67 7 #include "eth_arch.hpp"
Soohwan Kim 0:6f28332c466f 8
Soohwan Kim 0:6f28332c466f 9 #define DBG_DNS 0
Soohwan Kim 0:6f28332c466f 10
Soohwan Kim 0:6f28332c466f 11 #if DBG_DNS
Soohwan Kim 0:6f28332c466f 12 #define DBG2(...) do{debug("[DNS]%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
Soohwan Kim 0:6f28332c466f 13 #else
Soohwan Kim 0:6f28332c466f 14 #define DBG2(...) while(0);
Soohwan Kim 0:6f28332c466f 15 #endif
Soohwan Kim 0:6f28332c466f 16
SteveKim 20:3e61863c1f67 17 using namespace wiznet_space;
SteveKim 20:3e61863c1f67 18
Soohwan Kim 0:6f28332c466f 19 DNSClient::DNSClient(const char* hostname) : m_state(MYNETDNS_START), m_udp(NULL) {
Soohwan Kim 0:6f28332c466f 20 m_hostname = hostname;
Soohwan Kim 0:6f28332c466f 21 }
Soohwan Kim 0:6f28332c466f 22
Soohwan Kim 0:6f28332c466f 23 DNSClient::DNSClient(Endpoint* pHost) : m_state(MYNETDNS_START), m_udp(NULL) {
Soohwan Kim 0:6f28332c466f 24 }
Soohwan Kim 0:6f28332c466f 25
Soohwan Kim 0:6f28332c466f 26 DNSClient::~DNSClient() {
Soohwan Kim 0:6f28332c466f 27 if (m_udp) {
Soohwan Kim 0:6f28332c466f 28 delete m_udp;
Soohwan Kim 0:6f28332c466f 29 }
Soohwan Kim 0:6f28332c466f 30 }
Soohwan Kim 0:6f28332c466f 31
Soohwan Kim 0:6f28332c466f 32 void DNSClient::callback()
Soohwan Kim 0:6f28332c466f 33 {
Soohwan Kim 0:6f28332c466f 34 uint8_t buf[512];
Soohwan Kim 0:6f28332c466f 35 Endpoint host;
Soohwan Kim 0:6f28332c466f 36 int len = m_udp->receiveFrom(host, (char*)buf, sizeof(buf));
Soohwan Kim 0:6f28332c466f 37 if (len < 0) {
Soohwan Kim 0:6f28332c466f 38 return;
Soohwan Kim 0:6f28332c466f 39 }
Soohwan Kim 0:6f28332c466f 40 if (memcmp(buf+0, m_id, 2) != 0) { //verify
Soohwan Kim 0:6f28332c466f 41 return;
Soohwan Kim 0:6f28332c466f 42 }
Soohwan Kim 0:6f28332c466f 43 int rcode = response(buf, len);
Soohwan Kim 0:6f28332c466f 44 if (rcode == 0) {
Soohwan Kim 0:6f28332c466f 45 m_state = MYNETDNS_OK;
Soohwan Kim 0:6f28332c466f 46 } else {
Soohwan Kim 0:6f28332c466f 47 m_state = MYNETDNS_NOTFOUND;
Soohwan Kim 0:6f28332c466f 48 }
Soohwan Kim 0:6f28332c466f 49 }
Soohwan Kim 0:6f28332c466f 50
Soohwan Kim 0:6f28332c466f 51 int DNSClient::response(uint8_t buf[], int size) {
Soohwan Kim 0:6f28332c466f 52 int rcode = buf[3] & 0x0f;
Soohwan Kim 0:6f28332c466f 53 if (rcode != 0) {
Soohwan Kim 0:6f28332c466f 54 return rcode;
Soohwan Kim 0:6f28332c466f 55 }
Soohwan Kim 0:6f28332c466f 56 int qdcount = buf[4]<<8|buf[5];
Soohwan Kim 0:6f28332c466f 57 int ancount = buf[6]<<8|buf[7];
Soohwan Kim 0:6f28332c466f 58 int pos = 12;
Soohwan Kim 0:6f28332c466f 59 while(qdcount-- > 0) {
Soohwan Kim 0:6f28332c466f 60 dnsname qname(buf);
Soohwan Kim 0:6f28332c466f 61 pos = qname.decode(pos); // qname
Soohwan Kim 0:6f28332c466f 62 pos += 4; // qtype qclass
Soohwan Kim 0:6f28332c466f 63 }
Soohwan Kim 0:6f28332c466f 64 while(ancount-- > 0) {
Soohwan Kim 0:6f28332c466f 65 dnsname name(buf);
Soohwan Kim 0:6f28332c466f 66 pos = name.decode(pos); // name
Soohwan Kim 0:6f28332c466f 67 int type = buf[pos]<<8|buf[pos+1];
Soohwan Kim 0:6f28332c466f 68 pos += 8; // type class TTL
Soohwan Kim 0:6f28332c466f 69 int rdlength = buf[pos]<<8|buf[pos+1]; pos += 2;
Soohwan Kim 0:6f28332c466f 70 int rdata_pos = pos;
Soohwan Kim 0:6f28332c466f 71 pos += rdlength;
Soohwan Kim 0:6f28332c466f 72 if (type == 1) { // A record
Soohwan Kim 0:6f28332c466f 73 ip = (buf[rdata_pos]<<24) | (buf[rdata_pos+1]<<16) | (buf[rdata_pos+2]<<8) | buf[rdata_pos+3];
Soohwan Kim 0:6f28332c466f 74 }
Soohwan Kim 0:6f28332c466f 75 #if DBG_DNS
Soohwan Kim 0:6f28332c466f 76 printf("%s", name.str.c_str());
Soohwan Kim 0:6f28332c466f 77 if (type == 1) {
Soohwan Kim 0:6f28332c466f 78 printf(" A %d.%d.%d.%d\n",
Soohwan Kim 0:6f28332c466f 79 buf[rdata_pos],buf[rdata_pos+1],buf[rdata_pos+2],buf[rdata_pos+3]);
Soohwan Kim 0:6f28332c466f 80 } else if (type == 5) {
Soohwan Kim 0:6f28332c466f 81 dnsname rdname(buf);
Soohwan Kim 0:6f28332c466f 82 rdname.decode(rdata_pos);
Soohwan Kim 0:6f28332c466f 83 printf(" CNAME %s\n", rdname.str.c_str());
Soohwan Kim 0:6f28332c466f 84 } else {
Soohwan Kim 0:6f28332c466f 85 printf(" TYPE:%d", type);
Soohwan Kim 0:6f28332c466f 86 printfBytes(" RDATA:", &buf[rdata_pos], rdlength);
Soohwan Kim 0:6f28332c466f 87 }
Soohwan Kim 0:6f28332c466f 88 #endif
Soohwan Kim 0:6f28332c466f 89 }
Soohwan Kim 0:6f28332c466f 90 return rcode;
Soohwan Kim 0:6f28332c466f 91 }
Soohwan Kim 0:6f28332c466f 92
Soohwan Kim 0:6f28332c466f 93 int DNSClient::query(uint8_t buf[], int size, const char* hostname) {
Soohwan Kim 0:6f28332c466f 94 const uint8_t header[] = {
Soohwan Kim 0:6f28332c466f 95 0x00,0x00,0x01,0x00, // id=0x0000 QR=0 rd=1 opcode=0 rcode=0
Soohwan Kim 0:6f28332c466f 96 0x00,0x01,0x00,0x00, // qdcount=1 ancount=0
Soohwan Kim 0:6f28332c466f 97 0x00,0x00,0x00,0x00};// nscount=0 arcount=0
Soohwan Kim 0:6f28332c466f 98 const uint8_t tail[] = {0x00,0x01,0x00,0x01}; // qtype=A qclass=IN
Soohwan Kim 0:6f28332c466f 99 memcpy(buf, header, sizeof(header));
Soohwan Kim 0:6f28332c466f 100 int t = rand();
Soohwan Kim 0:6f28332c466f 101 m_id[0] = t>>8;
Soohwan Kim 0:6f28332c466f 102 m_id[1] = t;
Soohwan Kim 0:6f28332c466f 103 memcpy(buf, m_id, 2);
Soohwan Kim 0:6f28332c466f 104 dnsname qname(buf);
Soohwan Kim 0:6f28332c466f 105 int pos = qname.encode(sizeof(header), (char*)hostname);
Soohwan Kim 0:6f28332c466f 106 memcpy(buf+pos, tail, sizeof(tail));
Soohwan Kim 0:6f28332c466f 107 pos += sizeof(tail);
Soohwan Kim 0:6f28332c466f 108 return pos;
Soohwan Kim 0:6f28332c466f 109 }
Soohwan Kim 0:6f28332c466f 110
Soohwan Kim 0:6f28332c466f 111 void DNSClient::resolve(const char* hostname) {
Soohwan Kim 0:6f28332c466f 112 if (m_udp == NULL) {
Soohwan Kim 0:6f28332c466f 113 m_udp = new UDPSocket;
Soohwan Kim 0:6f28332c466f 114 }
Soohwan Kim 0:6f28332c466f 115 m_udp->init();
Soohwan Kim 0:6f28332c466f 116 m_udp->set_blocking(false);
Soohwan Kim 0:6f28332c466f 117 Endpoint server;
Soohwan Kim 0:6f28332c466f 118 server.set_address("8.8.8.8", 53); // DNS
Soohwan Kim 0:6f28332c466f 119 m_udp->bind(rand()&0x7fff);
Soohwan Kim 0:6f28332c466f 120 uint8_t buf[256];
Soohwan Kim 0:6f28332c466f 121 int size = query(buf, sizeof(buf), hostname);
Soohwan Kim 0:6f28332c466f 122 #if DBG_DNS
Soohwan Kim 0:6f28332c466f 123 printf("hostname:[%s]\n", hostname);
Soohwan Kim 0:6f28332c466f 124 printHex(buf, size);
Soohwan Kim 0:6f28332c466f 125 #endif
Soohwan Kim 0:6f28332c466f 126 m_udp->sendTo(server, (char*)buf, size);
Soohwan Kim 0:6f28332c466f 127 m_interval.reset();
Soohwan Kim 0:6f28332c466f 128 m_interval.start();
Soohwan Kim 0:6f28332c466f 129 }
Soohwan Kim 0:6f28332c466f 130
Soohwan Kim 0:6f28332c466f 131 void DNSClient::poll() {
Soohwan Kim 0:6f28332c466f 132 #if DBG_DNS
Soohwan Kim 0:6f28332c466f 133 printf("%p m_state: %d, m_udp: %p\n", this, m_state, m_udp);
Soohwan Kim 0:6f28332c466f 134 wait_ms(400);
Soohwan Kim 0:6f28332c466f 135 #endif
Soohwan Kim 0:6f28332c466f 136 switch(m_state) {
Soohwan Kim 0:6f28332c466f 137 case MYNETDNS_START:
Soohwan Kim 0:6f28332c466f 138 m_retry = 0;
Soohwan Kim 0:6f28332c466f 139 resolve(m_hostname);
Soohwan Kim 0:6f28332c466f 140 m_state = MYNETDNS_PROCESSING;
Soohwan Kim 0:6f28332c466f 141 break;
Soohwan Kim 0:6f28332c466f 142 case MYNETDNS_PROCESSING:
Soohwan Kim 0:6f28332c466f 143 break;
Soohwan Kim 0:6f28332c466f 144 case MYNETDNS_NOTFOUND:
Soohwan Kim 0:6f28332c466f 145 break;
Soohwan Kim 0:6f28332c466f 146 case MYNETDNS_ERROR:
Soohwan Kim 0:6f28332c466f 147 break;
Soohwan Kim 0:6f28332c466f 148 case MYNETDNS_OK:
Soohwan Kim 0:6f28332c466f 149 DBG2("m_retry=%d, m_interval=%d\n", m_retry, m_interval.read_ms());
Soohwan Kim 0:6f28332c466f 150 break;
Soohwan Kim 0:6f28332c466f 151 }
Soohwan Kim 0:6f28332c466f 152 if (m_interval.read_ms() > 1000) {
Soohwan Kim 0:6f28332c466f 153 m_interval.stop();
Soohwan Kim 0:6f28332c466f 154 DBG2("timeout m_retry=%d\n", m_retry);
Soohwan Kim 0:6f28332c466f 155 if (++m_retry >= 2) {
Soohwan Kim 0:6f28332c466f 156 m_state = MYNETDNS_ERROR;
Soohwan Kim 0:6f28332c466f 157 } else {
Soohwan Kim 0:6f28332c466f 158 resolve(m_hostname);
Soohwan Kim 0:6f28332c466f 159 m_state = MYNETDNS_PROCESSING;
Soohwan Kim 0:6f28332c466f 160 }
Soohwan Kim 0:6f28332c466f 161 }
Soohwan Kim 0:6f28332c466f 162 }
Soohwan Kim 0:6f28332c466f 163
Soohwan Kim 0:6f28332c466f 164 bool DNSClient::lookup(const char* hostname) {
Soohwan Kim 0:6f28332c466f 165 m_hostname = hostname;
Soohwan Kim 0:6f28332c466f 166 m_state = MYNETDNS_START;
Soohwan Kim 0:6f28332c466f 167 while(1) {
Soohwan Kim 0:6f28332c466f 168 poll();
Soohwan Kim 0:6f28332c466f 169 callback();
Soohwan Kim 0:6f28332c466f 170 if (m_state != MYNETDNS_PROCESSING) {
Soohwan Kim 0:6f28332c466f 171 break;
Soohwan Kim 0:6f28332c466f 172 }
Soohwan Kim 0:6f28332c466f 173 }
Soohwan Kim 0:6f28332c466f 174 return m_state == MYNETDNS_OK;
Soohwan Kim 0:6f28332c466f 175 }
Soohwan Kim 0:6f28332c466f 176