Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hostname parameter to Ethernet.begin #257

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/Dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
{
_dhcpLeaseTime=0;
_dhcpT1=0;
_dhcpT2=0;
return beginWithDHCP(mac, NULL, timeout, responseTimeout);
}

int DhcpClass::beginWithDHCP(uint8_t *mac, const char* hostName, unsigned long timeout, unsigned long responseTimeout)
{
_dhcpLeaseTime = 0;
_dhcpT1 = 0;
_dhcpT2 = 0;
_timeout = timeout;
_responseTimeout = responseTimeout;
_hostName = hostName;

// zero out _dhcpMacAddr
memset(_dhcpMacAddr, 0, 6);
Expand Down Expand Up @@ -118,7 +124,7 @@ void DhcpClass::presend_DHCP()

void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
{
uint8_t buffer[32];
uint8_t buffer[18 + 63]; // Reserve 63 chars for the hostname.
memset(buffer, 0, 32);
IPAddress dest_addr(255, 255, 255, 255); // Broadcast address

Expand Down Expand Up @@ -188,15 +194,25 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)

// OPT - host name
buffer[16] = hostName;
buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address
strcpy((char*)&(buffer[18]), HOST_NAME);

printByte((char*)&(buffer[24]), _dhcpMacAddr[3]);
printByte((char*)&(buffer[26]), _dhcpMacAddr[4]);
printByte((char*)&(buffer[28]), _dhcpMacAddr[5]);
// if a hostname was given use it, use the default otherwise.
if (_hostName) {
buffer[17] = strlen(_hostName); // length of hostname
strncpy((char*)&(buffer[18]), _hostName, strlen(_hostName));

//put data in W5100 transmit buffer
_dhcpUdpSocket.write(buffer, 30);
//put data in W5100 transmit buffer
_dhcpUdpSocket.write(buffer, 18 + strlen(_hostName));
} else {
buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address
strcpy((char*)&(buffer[18]), HOST_NAME);

printByte((char*)&(buffer[24]), _dhcpMacAddr[3]);
printByte((char*)&(buffer[26]), _dhcpMacAddr[4]);
printByte((char*)&(buffer[28]), _dhcpMacAddr[5]);

//put data in W5100 transmit buffer
_dhcpUdpSocket.write(buffer, 30);
}

if (messageType == DHCP_REQUEST) {
buffer[0] = dhcpRequestedIPaddr;
Expand Down
10 changes: 9 additions & 1 deletion src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ IPAddress EthernetClass::_dnsServerAddress;
DhcpClass* EthernetClass::_dhcp = NULL;

int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
{
return begin(mac,
NULL,
timeout,
responseTimeout);
}

int EthernetClass::begin(uint8_t *mac, const char* hostName, unsigned long timeout, unsigned long responseTimeout)
{
static DhcpClass s_dhcp;
_dhcp = &s_dhcp;
Expand All @@ -39,7 +47,7 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp
SPI.endTransaction();

// Now try to get our config info from a DHCP server
int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout);
int ret = _dhcp->beginWithDHCP(mac, hostName, timeout, responseTimeout);
if (ret == 1) {
// We've successfully found a DHCP server and got our configuration
// info, so set things accordingly
Expand Down
6 changes: 4 additions & 2 deletions src/Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
// does not always seem to work in practice (maybe WIZnet bugs?)
//#define ETHERNET_LARGE_BUFFERS


#include <Arduino.h>

#include "Client.h"
#include "Server.h"
#include "Udp.h"
Expand Down Expand Up @@ -80,6 +80,7 @@ class EthernetClass {
// gain the rest of the configuration through DHCP.
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
static int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
static int begin(uint8_t *mac, const char* hostName, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
static int maintain();
static EthernetLinkStatus linkStatus();
static EthernetHardwareStatus hardwareStatus();
Expand Down Expand Up @@ -303,7 +304,7 @@ class DhcpClass {
void presend_DHCP();
void send_DHCP_MESSAGE(uint8_t, uint16_t);
void printByte(char *, uint8_t);

const char* _hostName;
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
public:
IPAddress getLocalIp();
Expand All @@ -313,6 +314,7 @@ class DhcpClass {
IPAddress getDnsServerIp();

int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
int beginWithDHCP(uint8_t *, const char* hostName, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
int checkLease();
};

Expand Down