From c6be14b7f4452a05d0a0860af70c8e49bc93a56f Mon Sep 17 00:00:00 2001 From: Thomas Schwanck Date: Mon, 18 Mar 2024 12:43:17 +0100 Subject: [PATCH 1/3] Add a begin() overload which takes a hostname as parameter to use with DHCP. --- src/Dhcp.cpp | 35 ++++++++++++++++++++++++----------- src/Ethernet.cpp | 10 +++++++++- src/Ethernet.h | 6 +++++- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/Dhcp.cpp b/src/Dhcp.cpp index 2bfd584b..79dc1e45 100644 --- a/src/Dhcp.cpp +++ b/src/Dhcp.cpp @@ -8,11 +8,17 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) { - _dhcpLeaseTime=0; - _dhcpT1=0; - _dhcpT2=0; + return beginWithDHCP(mac, NULL, timeout, responseTimeout); +} + +int DhcpClass::beginWithDHCP(uint8_t *mac, const char* hostName, unsigned long timeout, unsigned long responseTimeout) +{ + _dhcpLeaseTime = 0; + _dhcpT1 = 0; + _dhcpT2 = 0; _timeout = timeout; _responseTimeout = responseTimeout; + _hostName = hostName; // zero out _dhcpMacAddr memset(_dhcpMacAddr, 0, 6); @@ -118,7 +124,7 @@ void DhcpClass::presend_DHCP() void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) { - uint8_t buffer[32]; + uint8_t buffer[18 + 63]; // Reserve 63 chars for the hostname. memset(buffer, 0, 32); IPAddress dest_addr(255, 255, 255, 255); // Broadcast address @@ -188,15 +194,22 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) // OPT - host name buffer[16] = hostName; - buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address - strcpy((char*)&(buffer[18]), HOST_NAME); - printByte((char*)&(buffer[24]), _dhcpMacAddr[3]); - printByte((char*)&(buffer[26]), _dhcpMacAddr[4]); - printByte((char*)&(buffer[28]), _dhcpMacAddr[5]); + // if a hostname was given use it, use the default otherwise. + if (_hostName) { + buffer[17] = strlen(_hostName); // length of hostname + strncpy((char*)&(buffer[18]), _hostName, strlen(_hostName)); - //put data in W5100 transmit buffer - _dhcpUdpSocket.write(buffer, 30); + //put data in W5100 transmit buffer + _dhcpUdpSocket.write(buffer, 18 + strlen(_hostName)); + } else { + buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address + strcpy((char*)&(buffer[18]), HOST_NAME); + + printByte((char*)&(buffer[24]), _dhcpMacAddr[3]); + printByte((char*)&(buffer[26]), _dhcpMacAddr[4]); + printByte((char*)&(buffer[28]), _dhcpMacAddr[5]); + } if (messageType == DHCP_REQUEST) { buffer[0] = dhcpRequestedIPaddr; diff --git a/src/Ethernet.cpp b/src/Ethernet.cpp index 8d9ce7fd..a7c2ad3c 100644 --- a/src/Ethernet.cpp +++ b/src/Ethernet.cpp @@ -27,6 +27,14 @@ IPAddress EthernetClass::_dnsServerAddress; DhcpClass* EthernetClass::_dhcp = NULL; int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) +{ + return begin(mac, + NULL, + timeout, + responseTimeout); +} + +int EthernetClass::begin(uint8_t *mac, const char* hostName, unsigned long timeout, unsigned long responseTimeout) { static DhcpClass s_dhcp; _dhcp = &s_dhcp; @@ -39,7 +47,7 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp SPI.endTransaction(); // Now try to get our config info from a DHCP server - int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout); + int ret = _dhcp->beginWithDHCP(mac, hostName, timeout, responseTimeout); if (ret == 1) { // We've successfully found a DHCP server and got our configuration // info, so set things accordingly diff --git a/src/Ethernet.h b/src/Ethernet.h index 0045de88..91c95c3e 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -47,8 +47,10 @@ // does not always seem to work in practice (maybe WIZnet bugs?) //#define ETHERNET_LARGE_BUFFERS +#include #include + #include "Client.h" #include "Server.h" #include "Udp.h" @@ -80,6 +82,7 @@ class EthernetClass { // gain the rest of the configuration through DHCP. // Returns 0 if the DHCP configuration failed, and 1 if it succeeded static int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); + static int begin(uint8_t *mac, const char* hostName, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); static int maintain(); static EthernetLinkStatus linkStatus(); static EthernetHardwareStatus hardwareStatus(); @@ -303,7 +306,7 @@ class DhcpClass { void presend_DHCP(); void send_DHCP_MESSAGE(uint8_t, uint16_t); void printByte(char *, uint8_t); - + const char* _hostName; uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId); public: IPAddress getLocalIp(); @@ -313,6 +316,7 @@ class DhcpClass { IPAddress getDnsServerIp(); int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); + int beginWithDHCP(uint8_t *, const char* hostName, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); int checkLease(); }; From 1249b2c7131cb5e224ecf38524ef78a80e8417a9 Mon Sep 17 00:00:00 2001 From: Thomas Schwanck Date: Mon, 15 Apr 2024 13:31:03 +0200 Subject: [PATCH 2/3] Remove stray include. --- src/Ethernet.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Ethernet.h b/src/Ethernet.h index 91c95c3e..929ebeb4 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -47,8 +47,6 @@ // does not always seem to work in practice (maybe WIZnet bugs?) //#define ETHERNET_LARGE_BUFFERS -#include - #include #include "Client.h" From a672738869cc0cd19d4506deb191af61d747dbe8 Mon Sep 17 00:00:00 2001 From: Thomas Schwanck Date: Tue, 16 Apr 2024 09:47:03 +0200 Subject: [PATCH 3/3] Write hostname if the constant is used, too. --- src/Dhcp.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Dhcp.cpp b/src/Dhcp.cpp index 79dc1e45..2c7b4ae5 100644 --- a/src/Dhcp.cpp +++ b/src/Dhcp.cpp @@ -209,6 +209,9 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) printByte((char*)&(buffer[24]), _dhcpMacAddr[3]); printByte((char*)&(buffer[26]), _dhcpMacAddr[4]); printByte((char*)&(buffer[28]), _dhcpMacAddr[5]); + + //put data in W5100 transmit buffer + _dhcpUdpSocket.write(buffer, 30); } if (messageType == DHCP_REQUEST) {