Skip to content

Commit 0197eb7

Browse files
committed
Add Ethernet.setHostname method and make DHCP hostname configurable.
- Added Ethernet.setHostname(const char* hostname) method. - Increased DHCP buffer size to 64 bytes to accommodate variable hostname length. - Updated DhcpClass to use the custom hostname.
1 parent 33a27e6 commit 0197eb7

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

src/Dhcp.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,15 @@ void DhcpClass::presend_DHCP()
116116
{
117117
}
118118

119+
void DhcpClass::setCustomHostname(const char* name)
120+
{
121+
_dhcpHostname = name;
122+
}
123+
119124
void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
120125
{
121-
uint8_t buffer[32];
122-
memset(buffer, 0, 32);
126+
uint8_t buffer[64];
127+
memset(buffer, 0, 64);
123128
IPAddress dest_addr(255, 255, 255, 255); // Broadcast address
124129

125130
if (_dhcpUdpSocket.beginPacket(dest_addr, DHCP_SERVER_PORT) == -1) {
@@ -153,14 +158,14 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
153158
//put data in W5100 transmit buffer
154159
_dhcpUdpSocket.write(buffer, 28);
155160

156-
memset(buffer, 0, 32); // clear local buffer
161+
memset(buffer, 0, 64); // clear local buffer
157162

158163
memcpy(buffer, _dhcpMacAddr, 6); // chaddr
159164

160165
//put data in W5100 transmit buffer
161166
_dhcpUdpSocket.write(buffer, 16);
162167

163-
memset(buffer, 0, 32); // clear local buffer
168+
memset(buffer, 0, 64); // clear local buffer
164169

165170
// leave zeroed out for sname && file
166171
// put in W5100 transmit buffer x 6 (192 bytes)
@@ -187,16 +192,18 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
187192
memcpy(buffer + 10, _dhcpMacAddr, 6);
188193

189194
// OPT - host name
195+
const char* hostname = _dhcpHostname ? _dhcpHostname : HOST_NAME;
196+
size_t hostnameLen = strlen(hostname);
190197
buffer[16] = hostName;
191-
buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address
192-
strcpy((char*)&(buffer[18]), HOST_NAME);
198+
buffer[17] = hostnameLen + 6; // length of hostname + last 3 bytes of mac address
199+
strcpy((char*)&(buffer[18]), hostname);
193200

194-
printByte((char*)&(buffer[24]), _dhcpMacAddr[3]);
195-
printByte((char*)&(buffer[26]), _dhcpMacAddr[4]);
196-
printByte((char*)&(buffer[28]), _dhcpMacAddr[5]);
201+
printByte((char*)&(buffer[18 + hostnameLen]), _dhcpMacAddr[3]);
202+
printByte((char*)&(buffer[18 + hostnameLen + 2]), _dhcpMacAddr[4]);
203+
printByte((char*)&(buffer[18 + hostnameLen + 4]), _dhcpMacAddr[5]);
197204

198205
//put data in W5100 transmit buffer
199-
_dhcpUdpSocket.write(buffer, 30);
206+
_dhcpUdpSocket.write(buffer, 18 + hostnameLen + 6);
200207

201208
if (messageType == DHCP_REQUEST) {
202209
buffer[0] = dhcpRequestedIPaddr;

src/Ethernet.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525

2626
IPAddress EthernetClass::_dnsServerAddress;
2727
DhcpClass* EthernetClass::_dhcp = NULL;
28+
char EthernetClass::_hostname[32] = {0};
2829

2930
int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
3031
{
3132
static DhcpClass s_dhcp;
3233
_dhcp = &s_dhcp;
34+
if (_hostname[0] != 0) {
35+
_dhcp->setCustomHostname(_hostname);
36+
}
3337

3438
// Initialise the basic info
3539
if (W5100.init() == 0) return 0;
@@ -178,6 +182,16 @@ IPAddress EthernetClass::gatewayIP()
178182
return ret;
179183
}
180184

185+
void EthernetClass::setHostname(const char* hostname)
186+
{
187+
if (strlen(hostname) < 32) {
188+
strcpy(_hostname, hostname);
189+
if (_dhcp) {
190+
_dhcp->setCustomHostname(_hostname);
191+
}
192+
}
193+
}
194+
181195
void EthernetClass::setMACAddress(const uint8_t *mac_address)
182196
{
183197
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);

src/Ethernet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class EthernetClass {
7575
private:
7676
static IPAddress _dnsServerAddress;
7777
static DhcpClass* _dhcp;
78+
static char _hostname[32];
7879
public:
7980
// Initialise the Ethernet shield to use the provided MAC address and
8081
// gain the rest of the configuration through DHCP.
@@ -97,6 +98,7 @@ class EthernetClass {
9798
static IPAddress gatewayIP();
9899
static IPAddress dnsServerIP() { return _dnsServerAddress; }
99100

101+
void setHostname(const char* hostname);
100102
void setMACAddress(const uint8_t *mac_address);
101103
void setLocalIP(const IPAddress local_ip);
102104
void setSubnetMask(const IPAddress subnet);
@@ -297,6 +299,7 @@ class DhcpClass {
297299
unsigned long _lastCheckLeaseMillis;
298300
uint8_t _dhcp_state;
299301
EthernetUDP _dhcpUdpSocket;
302+
const char *_dhcpHostname = NULL;
300303

301304
int request_DHCP_lease();
302305
void reset_DHCP_lease();
@@ -311,6 +314,7 @@ class DhcpClass {
311314
IPAddress getGatewayIp();
312315
IPAddress getDhcpServerIp();
313316
IPAddress getDnsServerIp();
317+
void setCustomHostname(const char* name);
314318

315319
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
316320
int checkLease();

0 commit comments

Comments
 (0)