WIZNet W5500 with additional enhancements

Fork of WIZnetInterface by WIZnet

Files at this revision

API Documentation at this revision

Comitter:
Helmut Tschemernjak
Date:
Wed Oct 11 11:18:41 2017 +0200
Parent:
35:fe3028eda085
Child:
37:018436dccfcd
Commit message:
More DHCP cleanup, added precise DHCP packet construction
DHCP hostname support (added to the DHCP request) which
allows dynamic DNS updates and routers will show the client name.

Changed in this revision

EthernetInterface.cpp Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.h Show annotated file Show diff for this revision Revisions of this file
Socket/DHCPClient.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/DHCPClient.h Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.cpp	Tue Oct 10 20:56:13 2017 +0200
+++ b/EthernetInterface.cpp	Wed Oct 11 11:18:41 2017 +0200
@@ -26,6 +26,7 @@
     ip_set = false;
     domainName = NULL;
     leaseStart = 0;
+    _hostname = NULL;
 }
 
 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
@@ -34,6 +35,7 @@
     ip_set = false;
     domainName = NULL;
     leaseStart = 0;
+    _hostname = NULL;
 }
 #endif
 
@@ -85,8 +87,10 @@
 }
 
 // Connect Bring the interface up, start DHCP if needed.
-int EthernetInterface::connect()
+int EthernetInterface::connect(const char *hostname)
 {
+    if (hostname)
+        _hostname = hostname;
     if (dhcp) {
         int r = IPrenew();
         if (r < 0) {
@@ -164,7 +168,7 @@
 int EthernetInterface::IPrenew(int timeout_ms)
 {
     DHCPClient dhcp;
-    int err = dhcp.setup(timeout_ms);
+    int err = dhcp.setup(_hostname, timeout_ms);
     if (err == (-1)) {
         return -1;
     }
--- a/EthernetInterface.h	Tue Oct 10 20:56:13 2017 +0200
+++ b/EthernetInterface.h	Wed Oct 11 11:18:41 2017 +0200
@@ -61,7 +61,7 @@
   * Bring the interface up, start DHCP if needed.
   * \return 0 on success, a negative number on failure
   */
-  int connect();
+  int connect(const char *hostname = NULL);
   
   /** Disconnect
   * Bring the interface down
@@ -81,7 +81,6 @@
   int getLeaseTime() { return leaseTime; };
   int getLeaseStart() { return leaseStart; };
   char *getDomainName(void) { return domainName; };
-    
   int IPrenew(int timeout_ms = 15*1000);
     
 private:
@@ -93,6 +92,7 @@
     bool ip_set;
     uint32_t leaseStart;
     char *domainName;
+    const char *_hostname;
 };
 
 #include "TCPSocketConnection.h"
--- a/Socket/DHCPClient.cpp	Tue Oct 10 20:56:13 2017 +0200
+++ b/Socket/DHCPClient.cpp	Wed Oct 11 11:18:41 2017 +0200
@@ -17,39 +17,64 @@
 int DHCPClient::discover()
 {
     m_pos = 0;
+    const uint8_t zero[] = { 0, 0, 0, 0 };
     const uint8_t header[] = {0x01,0x01,0x06,0x00};
     add_buf((uint8_t*)header, sizeof(header));
     uint32_t x = time(NULL) + rand();
     xid[0] = x>>24; xid[1] = x>>16; xid[2] = x>>8; xid[3] = x;
-    add_buf(xid, 4);
-    fill_buf(20, 0x00);
-    add_buf(chaddr, 6);
-    fill_buf(10+192, 0x00);
+    add_buf(xid, 4); 			// transaction id
+    add_buf((uint8_t*)zero, 2); // seconds elapsed
+    add_buf((uint8_t*)zero, 2); // bootp flags
+    add_buf((uint8_t*)zero, 4); // requester ip address
+    add_buf((uint8_t*)zero, 4); // client ip address
+    add_buf((uint8_t*)zero, 4); // next server ip address
+    add_buf((uint8_t*)zero, 4); // relay agent ip address
+    add_buf(chaddr, 6);			// MAC address
+    fill_buf(10, 0x00);			// padding
+    fill_buf(192, 0x00);
     const uint8_t options[] = {0x63,0x82,0x53,0x63, // DHCP_MAGIC_COOKIE
         OPT_DHCP_MESSAGE, 1, DHCPDISCOVER,   // // DHCP message, len, discover
-        OPT_PARAMETER_REQ, 5, OPT_SUBNET_MASK, OPT_ROUTER, OPT_TIME_SERVER, OPT_DOMAIN_NAME, OPT_DNS,
-        OPT_END};
+        OPT_PARAMETER_REQ, 5, OPT_SUBNET_MASK, OPT_ROUTER, OPT_TIME_SERVER, OPT_DOMAIN_NAME, OPT_DNS };
     add_buf((uint8_t*)options, sizeof(options));
+    if (_hostname) {
+        int hlen = strlen(_hostname);
+        add_buf(OPT_HOSTNAME);
+        add_buf(hlen);
+        add_buf((uint8_t *)_hostname, hlen);
+    }
+    add_option(OPT_END);
+    
     return m_pos;
 }
 
 int DHCPClient::request()
 {
     m_pos = 0;
+    const uint8_t zero[] = { 0, 0, 0, 0 };
     const uint8_t header[] = {0x01,0x01,0x06,0x00};
     add_buf((uint8_t*)header, sizeof(header));
-    add_buf(xid, 4);
-    fill_buf(12, 0x00);
-    add_buf(siaddr, 4);
-    fill_buf(4, 0x00); // giaddr
-    add_buf(chaddr, 6);
-    fill_buf(10+192, 0x00);
+    add_buf(xid, 4);	// transaction id
+    add_buf((uint8_t*)zero, 2); // seconds elapsed
+    add_buf((uint8_t*)zero, 2); // bootp flags
+    add_buf((uint8_t*)zero, 4); // requester ip address
+    add_buf((uint8_t*)zero, 4); // client ip address
+    add_buf(siaddr, 4);			// next server ip address
+    add_buf((uint8_t*)zero, 4);	// relay agent ip address (giaddr)
+    add_buf(chaddr, 6);			// MAC address
+    fill_buf(10, 0x00);			// padding
+    fill_buf(192, 0x00);
     const uint8_t options[] = {0x63,0x82,0x53,0x63, // DHCP_MAGIC_COOKIE
         OPT_DHCP_MESSAGE,1, DHCPREQUEST,    // DHCP message, len, request
         OPT_PARAMETER_REQ, 5, OPT_SUBNET_MASK, OPT_ROUTER, OPT_TIME_SERVER, OPT_DOMAIN_NAME, OPT_DNS };
     add_buf((uint8_t*)options, sizeof(options));
     add_option(OPT_IP_ADDR_REQ, yiaddr, 4);
     add_option(OPT_SERVER_IDENT, siaddr, 4);
+    if (_hostname) {
+        int hlen = strlen(_hostname);
+        add_buf(OPT_HOSTNAME);
+        add_buf(hlen);
+        add_buf((uint8_t *)_hostname, hlen);
+    }
     add_option(OPT_END);
     return m_pos;
 }
@@ -180,12 +205,13 @@
     }
 }
 
-int DHCPClient::setup(int timeout_ms)
+int DHCPClient::setup(const char *hostnane, int timeout_ms)
 {
     eth = WIZnet_Chip::getInstance();
     if (eth == NULL) {
         return -1;
-    }    
+    }
+    _hostname = hostnane;
     eth->reg_rd_mac(SHAR, chaddr);
     int interval_ms = 5*1000; // 5000msec
     if (timeout_ms < interval_ms) {
--- a/Socket/DHCPClient.h	Tue Oct 10 20:56:13 2017 +0200
+++ b/Socket/DHCPClient.h	Wed Oct 11 11:18:41 2017 +0200
@@ -82,7 +82,7 @@
 public:
     DHCPClient();
     ~DHCPClient();
-    int setup(int timeout_ms = 15*1000);
+    int setup(const char *hostnane, int timeout_ms = 15*1000);
     uint8_t chaddr[6]; // MAC
     uint8_t yiaddr[4]; // IP
     uint8_t siaddr[4]; // DHCP server
@@ -92,7 +92,6 @@
     uint8_t timesrv[4];
     uint8_t leaseTime[4];
     char *domainName;
-   
 private:
     int discover();
     int request();
@@ -111,6 +110,7 @@
     int m_retry;
     uint8_t m_buf[DHCP_MAX_PACKET_SIZE];
     int m_pos;
+    const char *_hostname;
     WIZnet_Chip* eth;
 };
 #endif //DHCPCLIENT_H