Skip to content

Commit 294063e

Browse files
committed
Ethernet.setHostname added (hostname to send with DHCP request)
1 parent 39103da commit 294063e

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/Dhcp.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
188188

189189
// OPT - host name
190190
buffer[16] = hostName;
191+
192+
if (_hostname == nullptr) {
191193
buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address
192194
strcpy((char*)&(buffer[18]), HOST_NAME);
193195

@@ -197,6 +199,12 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
197199

198200
//put data in W5100 transmit buffer
199201
_dhcpUdpSocket.write(buffer, 30);
202+
} else {
203+
uint8_t len = strlen(_hostname);
204+
buffer[17] = len;
205+
_dhcpUdpSocket.write(buffer, 18);
206+
_dhcpUdpSocket.write(_hostname, len);
207+
}
200208

201209
if (messageType == DHCP_REQUEST) {
202210
buffer[0] = dhcpRequestedIPaddr;
@@ -420,6 +428,11 @@ IPAddress DhcpClass::getDnsServerIp()
420428
return IPAddress(_dhcpDnsServerIp);
421429
}
422430

431+
void DhcpClass::setHostname(const char* hostname)
432+
{
433+
_hostname = hostname;
434+
}
435+
423436
void DhcpClass::printByte(char * buf, uint8_t n )
424437
{
425438
char *str = &buf[1];

src/Ethernet.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ DhcpClass* EthernetClass::_dhcp = NULL;
2828

2929
int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
3030
{
31-
static DhcpClass s_dhcp;
32-
_dhcp = &s_dhcp;
31+
if (_dhcp == NULL) {
32+
_dhcp = new DhcpClass();
33+
}
3334

3435
// Initialise the basic info
3536
if (W5100.init() == 0) return 0;
@@ -178,6 +179,14 @@ IPAddress EthernetClass::gatewayIP()
178179
return ret;
179180
}
180181

182+
void EthernetClass::setHostname(const char* hostname)
183+
{
184+
if (_dhcp == NULL) {
185+
_dhcp = new DhcpClass();
186+
}
187+
_dhcp->setHostname(hostname);
188+
}
189+
181190
void EthernetClass::setMACAddress(const uint8_t *mac_address)
182191
{
183192
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);

src/Ethernet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class EthernetClass {
9797
static IPAddress gatewayIP();
9898
static IPAddress dnsServerIP() { return _dnsServerAddress; }
9999

100+
void setHostname(const char* hostname); // only the pointer is stored!
100101
void setMACAddress(const uint8_t *mac_address);
101102
void setLocalIP(const IPAddress local_ip);
102103
void setSubnetMask(const IPAddress subnet);
@@ -275,6 +276,7 @@ class DhcpClass {
275276
uint32_t _dhcpInitialTransactionId;
276277
uint32_t _dhcpTransactionId;
277278
uint8_t _dhcpMacAddr[6];
279+
const char* _hostname = nullptr;
278280
#ifdef __arm__
279281
uint8_t _dhcpLocalIp[4] __attribute__((aligned(4)));
280282
uint8_t _dhcpSubnetMask[4] __attribute__((aligned(4)));
@@ -314,6 +316,7 @@ class DhcpClass {
314316

315317
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
316318
int checkLease();
319+
void setHostname(const char* hostname);
317320
};
318321

319322

0 commit comments

Comments
 (0)