Skip to content
This repository was archived by the owner on Jan 7, 2019. It is now read-only.

Commit 97bcbb1

Browse files
authored
Merge pull request #13 from dmorina/develop
adds support for reading options 12 and 15 of DHCP -> hostname and dns domain name
2 parents 3e436cc + 350d353 commit 97bcbb1

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/Dhcp.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,26 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
331331
break;
332332

333333
case dns :
334-
335334
opt_len = _dhcpUdpSocket.read();
336335
_dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
337336
for (int i = 0; i < opt_len-4; i++)
338337
{
339338
_dhcpUdpSocket.read();
340339
}
341340
break;
342-
341+
342+
case domainName:
343+
opt_len = _dhcpUdpSocket.read();
344+
_dhcpDnsdomainName = (char*)malloc(sizeof(char)*opt_len+1);
345+
_dhcpUdpSocket.read(_dhcpDnsdomainName, opt_len);
346+
_dhcpDnsdomainName[opt_len] = '\0';
347+
break;
348+
case hostName:
349+
opt_len = _dhcpUdpSocket.read();
350+
_dhcpHostName = (char*)malloc(sizeof(char)*opt_len+1);
351+
_dhcpUdpSocket.read(_dhcpHostName, opt_len);
352+
_dhcpHostName[opt_len] = '\0';
353+
break;
343354
case dhcpServerIdentifier :
344355

345356
opt_len = _dhcpUdpSocket.read();
@@ -485,6 +496,16 @@ IPAddress DhcpClass::getDnsServerIp()
485496
return IPAddress(_dhcpDnsServerIp);
486497
}
487498

499+
char* DhcpClass::getDnsDomainName()
500+
{
501+
return _dhcpDnsdomainName;
502+
}
503+
504+
char* DhcpClass::getHostName()
505+
{
506+
return _dhcpHostName;
507+
}
508+
488509
void DhcpClass::printByte(char * buf, uint8_t n ) {
489510
char *str = &buf[1];
490511
buf[0]='0';

src/Dhcp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ class DhcpClass {
143143
uint32_t _dhcpTransactionId;
144144
uint8_t _dhcpMacAddr[6];
145145
uint8_t _dhcpLocalIp[4];
146+
char* _dhcpDnsdomainName;
147+
char* _dhcpHostName;
146148
uint8_t _dhcpSubnetMask[4];
147149
uint8_t _dhcpGatewayIp[4];
148150
uint8_t _dhcpDhcpServerIp[4];
@@ -170,6 +172,8 @@ class DhcpClass {
170172
IPAddress getGatewayIp();
171173
IPAddress getDhcpServerIp();
172174
IPAddress getDnsServerIp();
175+
char* getDnsDomainName();
176+
char* getHostName();
173177

174178
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 5000);
175179
int checkLease();

src/Ethernet2.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ int EthernetClass::begin(void)
4141
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
4242
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
4343
_dnsServerAddress = _dhcp->getDnsServerIp();
44+
_dnsDomainName = _dhcp->getDnsDomainName();
45+
_hostName = _dhcp->getHostName();
4446
}
4547

4648
return ret;
@@ -100,6 +102,8 @@ int EthernetClass::begin(uint8_t *mac_address)
100102
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
101103
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
102104
_dnsServerAddress = _dhcp->getDnsServerIp();
105+
_dnsDomainName = _dhcp->getDnsDomainName();
106+
_hostName = _dhcp->getHostName();
103107
}
104108

105109
return ret;
@@ -157,6 +161,8 @@ int EthernetClass::maintain(){
157161
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
158162
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
159163
_dnsServerAddress = _dhcp->getDnsServerIp();
164+
_dnsDomainName = _dhcp->getDnsDomainName();
165+
_hostName = _dhcp->getHostName();
160166
break;
161167
default:
162168
//this is actually a error, it will retry though
@@ -192,4 +198,12 @@ IPAddress EthernetClass::dnsServerIP()
192198
return _dnsServerAddress;
193199
}
194200

201+
char* EthernetClass::dnsDomainName(){
202+
return _dnsDomainName;
203+
}
204+
205+
char* EthernetClass::hostName(){
206+
return _hostName;
207+
}
208+
195209
EthernetClass Ethernet;

src/Ethernet2.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
class EthernetClass {
2323
private:
2424
IPAddress _dnsServerAddress;
25+
char* _dnsDomainName;
26+
char* _hostName;
2527
DhcpClass* _dhcp;
2628
public:
2729
uint8_t w5500_cspin;
@@ -60,6 +62,8 @@ class EthernetClass {
6062
IPAddress subnetMask();
6163
IPAddress gatewayIP();
6264
IPAddress dnsServerIP();
65+
char* dnsDomainName();
66+
char* hostName();
6367

6468
friend class EthernetClient;
6569
friend class EthernetServer;

0 commit comments

Comments
 (0)