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

Commit

Permalink
Merge pull request #13 from dmorina/develop
Browse files Browse the repository at this point in the history
adds support for reading options 12 and 15 of DHCP -> hostname and dns domain name
  • Loading branch information
ladyada authored Feb 28, 2017
2 parents 3e436cc + 350d353 commit 97bcbb1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,26 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
break;

case dns :

opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
for (int i = 0; i < opt_len-4; i++)
{
_dhcpUdpSocket.read();
}
break;


case domainName:
opt_len = _dhcpUdpSocket.read();
_dhcpDnsdomainName = (char*)malloc(sizeof(char)*opt_len+1);
_dhcpUdpSocket.read(_dhcpDnsdomainName, opt_len);
_dhcpDnsdomainName[opt_len] = '\0';
break;
case hostName:
opt_len = _dhcpUdpSocket.read();
_dhcpHostName = (char*)malloc(sizeof(char)*opt_len+1);
_dhcpUdpSocket.read(_dhcpHostName, opt_len);
_dhcpHostName[opt_len] = '\0';
break;
case dhcpServerIdentifier :

opt_len = _dhcpUdpSocket.read();
Expand Down Expand Up @@ -485,6 +496,16 @@ IPAddress DhcpClass::getDnsServerIp()
return IPAddress(_dhcpDnsServerIp);
}

char* DhcpClass::getDnsDomainName()
{
return _dhcpDnsdomainName;
}

char* DhcpClass::getHostName()
{
return _dhcpHostName;
}

void DhcpClass::printByte(char * buf, uint8_t n ) {
char *str = &buf[1];
buf[0]='0';
Expand Down
4 changes: 4 additions & 0 deletions src/Dhcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class DhcpClass {
uint32_t _dhcpTransactionId;
uint8_t _dhcpMacAddr[6];
uint8_t _dhcpLocalIp[4];
char* _dhcpDnsdomainName;
char* _dhcpHostName;
uint8_t _dhcpSubnetMask[4];
uint8_t _dhcpGatewayIp[4];
uint8_t _dhcpDhcpServerIp[4];
Expand Down Expand Up @@ -170,6 +172,8 @@ class DhcpClass {
IPAddress getGatewayIp();
IPAddress getDhcpServerIp();
IPAddress getDnsServerIp();
char* getDnsDomainName();
char* getHostName();

int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 5000);
int checkLease();
Expand Down
14 changes: 14 additions & 0 deletions src/Ethernet2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ int EthernetClass::begin(void)
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
_dnsServerAddress = _dhcp->getDnsServerIp();
_dnsDomainName = _dhcp->getDnsDomainName();
_hostName = _dhcp->getHostName();
}

return ret;
Expand Down Expand Up @@ -100,6 +102,8 @@ int EthernetClass::begin(uint8_t *mac_address)
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
_dnsServerAddress = _dhcp->getDnsServerIp();
_dnsDomainName = _dhcp->getDnsDomainName();
_hostName = _dhcp->getHostName();
}

return ret;
Expand Down Expand Up @@ -157,6 +161,8 @@ int EthernetClass::maintain(){
w5500.setGatewayIp(_dhcp->getGatewayIp().raw_address());
w5500.setSubnetMask(_dhcp->getSubnetMask().raw_address());
_dnsServerAddress = _dhcp->getDnsServerIp();
_dnsDomainName = _dhcp->getDnsDomainName();
_hostName = _dhcp->getHostName();
break;
default:
//this is actually a error, it will retry though
Expand Down Expand Up @@ -192,4 +198,12 @@ IPAddress EthernetClass::dnsServerIP()
return _dnsServerAddress;
}

char* EthernetClass::dnsDomainName(){
return _dnsDomainName;
}

char* EthernetClass::hostName(){
return _hostName;
}

EthernetClass Ethernet;
4 changes: 4 additions & 0 deletions src/Ethernet2.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
class EthernetClass {
private:
IPAddress _dnsServerAddress;
char* _dnsDomainName;
char* _hostName;
DhcpClass* _dhcp;
public:
uint8_t w5500_cspin;
Expand Down Expand Up @@ -60,6 +62,8 @@ class EthernetClass {
IPAddress subnetMask();
IPAddress gatewayIP();
IPAddress dnsServerIP();
char* dnsDomainName();
char* hostName();

friend class EthernetClient;
friend class EthernetServer;
Expand Down

0 comments on commit 97bcbb1

Please sign in to comment.