This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.

Fork of WIZnet_Library by WIZnet

Committer:
jbkim
Date:
Thu May 08 03:57:58 2014 +0000
Revision:
0:b72d22e10709
1st commit

Who changed what in which revision?

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