Skip to content

Commit 433dd3a

Browse files
committed
Ethernet.setHostname added (hostname to send with DHCP request)
1 parent 323c226 commit 433dd3a

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/Dhcp.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
204204

205205
// OPT - host name
206206
buffer[16] = hostName;
207+
if (_hostname == nullptr) {
207208
buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address
208209
strcpy((char*)&(buffer[18]), HOST_NAME);
209210

@@ -213,6 +214,12 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
213214

214215
//put data in W5100 transmit buffer
215216
_dhcpUdpSocket.write(buffer, 30);
217+
} else {
218+
uint8_t len = strlen(_hostname);
219+
buffer[17] = len;
220+
_dhcpUdpSocket.write(buffer, 18);
221+
_dhcpUdpSocket.write(_hostname, len);
222+
}
216223

217224
if(messageType == DHCP_REQUEST)
218225
{
@@ -468,6 +475,10 @@ IPAddress DhcpClass::getDnsServerIp()
468475
return IPAddress(_dhcpDnsServerIp);
469476
}
470477

478+
void DhcpClass::setHostname(const char* hostname) {
479+
_hostname = hostname;
480+
}
481+
471482
void DhcpClass::printByte(char * buf, uint8_t n ) {
472483
char *str = &buf[1];
473484
buf[0]='0';

src/Dhcp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class DhcpClass {
141141
uint32_t _dhcpInitialTransactionId;
142142
uint32_t _dhcpTransactionId;
143143
uint8_t _dhcpMacAddr[6];
144+
const char* _hostname = nullptr;
144145
#ifdef __arm__
145146
uint8_t _dhcpLocalIp[4] __attribute__((aligned(4)));
146147
uint8_t _dhcpSubnetMask[4] __attribute__((aligned(4)));
@@ -181,6 +182,8 @@ class DhcpClass {
181182

182183
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
183184
int checkLease();
185+
186+
void setHostname(const char* hostname);
184187
};
185188

186189
#endif

src/Ethernet.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,21 @@ void UIPEthernetClass::init(uint8_t csPin)
5656
}
5757

5858
#if UIP_UDP
59+
void
60+
UIPEthernetClass::setHostname(const char* hostname)
61+
{
62+
if (_dhcp == NULL) {
63+
_dhcp = new DhcpClass();
64+
}
65+
_dhcp->setHostname(hostname);
66+
}
67+
5968
int
6069
UIPEthernetClass::begin(const uint8_t* mac, unsigned long timeout, unsigned long responseTimeout)
6170
{
62-
static DhcpClass s_dhcp;
63-
_dhcp = &s_dhcp;
71+
if (_dhcp == NULL) {
72+
_dhcp = new DhcpClass();
73+
}
6474

6575
// Initialise the basic info
6676
init(mac);

src/Ethernet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class UIPEthernetClass
104104
void setDnsServerIP(const IPAddress dns_server) { _dnsServerAddress = dns_server; }
105105
void setDNS(IPAddress dns_server) { _dnsServerAddress = dns_server; }
106106

107+
void setHostname(const char* hostname); // only the pointer is stored!
108+
107109
int hostByName(const char* hostname, IPAddress& result);
108110

109111
private:

0 commit comments

Comments
 (0)