Helmut Tschemernjak / WIZnetInterface

Fork of WIZnetInterface by WIZnet

Revision:
36:0ba2e8d5274a
Parent:
35:fe3028eda085
diff -r fe3028eda085 -r 0ba2e8d5274a Socket/DHCPClient.cpp
--- 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) {