Helmut Tschemernjak / WIZnetInterface

Fork of WIZnetInterface by WIZnet

Revision:
35:fe3028eda085
Parent:
15:24a9f2df2145
Child:
36:0ba2e8d5274a
--- a/Socket/DHCPClient.cpp	Mon Oct 09 19:58:19 2017 +0200
+++ b/Socket/DHCPClient.cpp	Tue Oct 10 20:56:13 2017 +0200
@@ -25,10 +25,10 @@
     fill_buf(20, 0x00);
     add_buf(chaddr, 6);
     fill_buf(10+192, 0x00);
-    const uint8_t options[] = {0x63,0x82,0x53,0x63, // magic cookie
-                               53,1,DHCPDISCOVER,   // DHCP option 53: DHCP Discover
-                               55,4,1,3,15,6,
-                               255}; 
+    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};
     add_buf((uint8_t*)options, sizeof(options));
     return m_pos;
 }
@@ -44,24 +44,30 @@
     fill_buf(4, 0x00); // giaddr
     add_buf(chaddr, 6);
     fill_buf(10+192, 0x00);
-    const uint8_t options[] = {0x63,0x82,0x53,0x63, // magic cookie
-                               53,1,DHCPREQUEST,    // DHCP option 53: DHCP Request
-                               55,4,1,3,15,6,       // DHCP option 55:
-                               };
+    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(50, yiaddr, 4);
-    add_option(54, siaddr, 4);
-    add_option(255);
+    add_option(OPT_IP_ADDR_REQ, yiaddr, 4);
+    add_option(OPT_SERVER_IDENT, siaddr, 4);
+    add_option(OPT_END);
     return m_pos;
 }
 
 int DHCPClient::offer(uint8_t buf[], int size) {
     memcpy(yiaddr, buf+DHCP_OFFSET_YIADDR, 4);   
-    memcpy(siaddr, buf+DHCP_OFFSET_SIADDR, 4);   
+    memcpy(siaddr, buf+DHCP_OFFSET_SIADDR, 4);
+ 
+    memset(dnsaddr, 0, sizeof(dnsaddr));
+    memset(gateway, 0, sizeof(gateway));
+    memset(netmask, 0, sizeof(netmask));
+    memset(timesrv, 0, sizeof(timesrv));
+    memset(leaseTime, 0, sizeof(leaseTime));
+    
     uint8_t *p;
     int msg_type = -1;
     p = buf + DHCP_OFFSET_OPTIONS;
-    while(*p != 255 && p < (buf+size)) {
+    while(*p != OPT_END && p < (buf+size)) {
         uint8_t code = *p++;
         if (code == 0) { // Pad Option
             continue;
@@ -72,21 +78,40 @@
         DBG_HEX(p, len);
 
         switch(code) {
-            case 53:
+            case OPT_DHCP_MESSAGE:
                 msg_type = *p;
                 break;
-            case 1:
-                memcpy(netmask, p, 4); // Subnet mask address
+            case OPT_SUBNET_MASK:
+                memcpy(netmask, p, 4);
                 break;
-            case 3:
-                memcpy(gateway, p, 4); // Gateway IP address
+            case OPT_ROUTER:
+                memcpy(gateway, p, 4);
                 break; 
-            case 6:  // DNS server
+            case OPT_DNS:
                 memcpy(dnsaddr, p, 4);
                 break;
-            case 51: // IP lease time 
+            case OPT_TIME_SERVER:
+                memcpy(timesrv, p, 4);
+                break;
+            case OPT_ADDR_LEASE_TIME:
+                memcpy(leaseTime, p, 4);
                 break;
-            case 54: // DHCP server
+            case OPT_DOMAIN_NAME:
+            	{
+                	int cplen = len;
+                	if (cplen > 63)
+                    	cplen = 63; // max domain name
+                    if (domainName) {
+                        free(domainName);
+                        domainName = NULL;
+                    }
+                    if (cplen) {
+                    	domainName = (char *)calloc(1, cplen+1);
+                    	memcpy(domainName, p, cplen); // zero term is already here via calloc
+                    }
+            	}
+                break;
+            case OPT_SERVER_IDENT:
                 memcpy(siaddr, p, 4);
                 break;
         }
@@ -170,8 +195,8 @@
     m_udp->init();
     m_udp->set_blocking(false);
     eth->reg_wr<uint32_t>(SIPR, 0x00000000); // local ip "0.0.0.0"
-    m_udp->bind(68); // local port
-    m_server.set_address("255.255.255.255", 67); // DHCP broadcast
+    m_udp->bind(DHCP_CLIENT_PORT); // local port
+    m_server.set_address("255.255.255.255", DHCP_SERVER_PORT); // DHCP broadcast
     exit_flag = false;
     int err = 0;
     int seq = 0;
@@ -208,5 +233,10 @@
 }
 
 DHCPClient::DHCPClient() {
+    domainName = NULL;
 }
 
+DHCPClient::~DHCPClient() {
+    if (domainName)
+        free(domainName);
+}