A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

Revision:
181:169881ecd52f
Parent:
180:0a30cff6b36a
Child:
183:ee809769bf89
--- a/udp/dhcp/dhcp.c	Thu Dec 31 12:32:51 2020 +0000
+++ b/udp/dhcp/dhcp.c	Mon Jan 04 16:07:45 2021 +0000
@@ -28,7 +28,7 @@
 #define DHCPRELEASE  7
 #define DHCPINFORM   8
 
-#define ID 75648
+#define ID 0x12345
 #define COOKIE 0x63825363
 
 #define MAX_REPEAT_DELAY_TIME_MS 60000
@@ -212,7 +212,6 @@
     {
         Log(" server=" ); Ip4AddressLog(srvIp);
         Log(" request="); Ip4AddressLog(reqIp);
-        Log(" local="  ); Ip4AddressLog(DhcpLocalIp);
         Log("\r\n");
     }
 
@@ -226,9 +225,11 @@
     DhcpHdrSetXid   (pPacket, ID                 ); //Randomly chosed transaction id used to associate messages to responses
     DhcpHdrSetSecs  (pPacket, 0                  ); //Seconds since started to boot
     DhcpHdrSetFlags (pPacket, flags              ); //Broadcast (1) Unicast (0)
-    DhcpHdrSetCiaddr(pPacket, DhcpLocalIp        ); //'Client' address set by client or 0 if don't know address
+    //DhcpHdrSetCiaddr(pPacket, reqIp              ); //'Client' address set by client or 0 if don't know address
+    DhcpHdrSetCiaddr(pPacket, 0                  ); //'Client' address set by client or 0 if don't know address
     DhcpHdrSetYiaddr(pPacket, 0                  ); //'Your' address returned by server
-    DhcpHdrSetSiaddr(pPacket, srvIp              ); //'Server' address to use if required
+    //DhcpHdrSetSiaddr(pPacket, srvIp              ); //'Server' address to use if required
+    DhcpHdrSetSiaddr(pPacket, 0                  ); //'Server' address to use if required
     DhcpHdrSetGiaddr(pPacket, 0                  ); //'Gateway' address
     memcpy(DhcpHdrPtrChaddr(pPacket), MacLocal, 6); //'Client hardware' address. 6 bytes for ethernet
     memset(DhcpHdrPtrLegacy(pPacket), 0, 192     ); //BootP legacy fill with zeros
@@ -240,8 +241,8 @@
     *p++ = 1;
     *p++ = code;
 
-    writeIp(50, reqIp, &p);     //Requested IP
-    writeIp(54, srvIp, &p);     //Server ip
+    if (reqIp) writeIp(50, reqIp, &p);     //Requested IP
+    if (srvIp) writeIp(54, srvIp, &p);     //Server ip
     
     writeText(12, NET_NAME, &p);
 
@@ -272,18 +273,18 @@
     switch (dhcpMessageType)
     {
         case DHCPOFFER:
-            if (DhcpTrace) { LogTime("DHCP <- offer ip "); Ip4AddressLog(yiaddr); Log("\r\n"); }
-            DhcpLocalIp = yiaddr; //Save the offered ip - should really be in another variable as we have not had the ACK yet from our request but hey ho.
-            *pSizeTx = sendRequest(pPacketTx, DHCPREQUEST, siaddr, yiaddr);
+            if (DhcpTrace) { LogTime("DHCP <- offer ip "); Ip4AddressLog(yiaddr); Log(" server ip "); Ip4AddressLog(DhcpServerIp); Log("\r\n"); }
+            //*pSizeTx = sendRequest(pPacketTx, DHCPREQUEST, siaddr, yiaddr);
+            *pSizeTx = sendRequest(pPacketTx, DHCPREQUEST, DhcpServerIp, yiaddr);
             return BROADCAST;
         case DHCPACK:
-            if (DhcpTrace) { LogTime("DHCP <- ack ip ");   Ip4AddressLog(yiaddr); Log("\r\n"); }
+            if (DhcpTrace) { LogTime("DHCP <- ack ip ");   Ip4AddressLog(yiaddr); Log(" server ip "); Ip4AddressLog(DhcpServerIp); Log("\r\n"); }
             DhcpLocalIp = yiaddr; //Save our IP
             elapsedLifeMsTimer = MsTimerCount;  //Start the life timer
             delayMs = MIN_REPEAT_DELAY_TIME_MS; //Set the delay time back to minimum
             break;
         case DHCPNAK:
-            if (DhcpTrace) { LogTime("DHCP <- nack ip ");  Ip4AddressLog(yiaddr); Log("\r\n"); }
+            if (DhcpTrace) { LogTime("DHCP <- nack ip ");  Ip4AddressLog(yiaddr); Log(" server ip "); Ip4AddressLog(DhcpServerIp); Log("\r\n"); }
             clearAll();
             break;
         default:
@@ -322,5 +323,5 @@
         *pSize = sendRequest(pPacket, DHCPDISCOVER, 0, 0); //If outside T2 then start from scratch to do a full DHCP
         dest = BROADCAST;
     }
-    return ActionMakeFromDestAndTrace(dest, DhcpTrace);
+    return ActionMakeFromDestAndTrace(dest, DhcpTrace && NetTraceStack);
 }
\ No newline at end of file