For mbed OS-5 version for WIZnet Ethernet Interface, this is Library using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.

Dependents:   ledMapperTest

Warning

  • If you want to use existing codes, you need to change the class used as EthernetInterface to WIZnetInterface.

This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500_enabled.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500P_enabled2.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500ECO_enabled2.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/components/components/fetch.phpmediaoshw5500_ethernet_shieldw5500_main_picture2.png.200x200_q85.jpg

This library is an Ethernet Interface library port-based on [EthernetInterface](https://developer.mbed.org/users/mbed_official/code/EthernetInterface/docs/tip/).

For more detail, visit http://embeddist.blogspot.kr/2015/06/wiznetinterface-for-armmbed.html

Committer:
justinkim
Date:
Mon Sep 04 00:23:04 2017 +0000
Revision:
0:d4c8fe4d9b29
mbed OS 5 version migration...

Who changed what in which revision?

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