diff --git a/src/Dhcp.cpp b/src/Dhcp.cpp index 2bfd584..135f8a9 100644 --- a/src/Dhcp.cpp +++ b/src/Dhcp.cpp @@ -6,6 +6,8 @@ #include "Dhcp.h" #include "utility/w5100.h" +char DhcpClass::_hostName[HOST_NAME_LENGTH] = HOST_NAME; + int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) { _dhcpLeaseTime=0; @@ -188,15 +190,15 @@ 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); + buffer[17] = strlen(_hostName) + 6; // length of hostname + last 3 bytes of mac address + strcpy((char*)&(buffer[18]), _hostName); - printByte((char*)&(buffer[24]), _dhcpMacAddr[3]); - printByte((char*)&(buffer[26]), _dhcpMacAddr[4]); - printByte((char*)&(buffer[28]), _dhcpMacAddr[5]); + printByte((char*)&(buffer[(18 + strlen(_hostName))]), _dhcpMacAddr[3]); + printByte((char*)&(buffer[(20 + strlen(_hostName))]), _dhcpMacAddr[4]); + printByte((char*)&(buffer[(22 + strlen(_hostName))]), _dhcpMacAddr[5]); - //put data in W5100 transmit buffer - _dhcpUdpSocket.write(buffer, 30); + //put data in W5100 transmit buffer + _dhcpUdpSocket.write(buffer, (24 + strlen(_hostName))); if (messageType == DHCP_REQUEST) { buffer[0] = dhcpRequestedIPaddr; @@ -431,3 +433,13 @@ void DhcpClass::printByte(char * buf, uint8_t n ) *str-- = c < 10 ? c + '0' : c + 'A' - 10; } while(n); } + +void DhcpClass::setHostName(const char * newHostName) +{ + strncpy(_hostName, newHostName, HOST_NAME_LENGTH-1); +} + +char * DhcpClass::getHostName() +{ + return _hostName; +} diff --git a/src/Dhcp.h b/src/Dhcp.h index 43ec4f8..536ab9b 100644 --- a/src/Dhcp.h +++ b/src/Dhcp.h @@ -42,6 +42,7 @@ #define MAGIC_COOKIE 0x63825363 #define MAX_DHCP_OPT 16 +#define HOST_NAME_LENGTH 13 #define HOST_NAME "WIZnet" #define DEFAULT_LEASE (900) //default lease time in seconds diff --git a/src/Ethernet.cpp b/src/Ethernet.cpp index 658bd64..a72621c 100644 --- a/src/Ethernet.cpp +++ b/src/Ethernet.cpp @@ -184,6 +184,16 @@ IPAddress EthernetClass::gatewayIP() return ret; } +void EthernetClass::setHostName(const char * newHostName) +{ + _dhcp->setHostName(newHostName); +} + +char * EthernetClass::getHostName() +{ + return _dhcp->getHostName(); +} + void EthernetClass::setMACAddress(const uint8_t *mac_address) { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); @@ -239,4 +249,5 @@ void EthernetClass::setRetransmissionCount(uint8_t num) + EthernetClass Ethernet; diff --git a/src/Ethernet.h b/src/Ethernet.h index 376e6c5..3bea528 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -47,11 +47,15 @@ // does not always seem to work in practice (maybe Wiznet bugs?) //#define ETHERNET_LARGE_BUFFERS +// This is used to check from outside, if there is a function +// to set the hostname in the library. Inside the library it is not used. +#define SET_HOSTNAME_AVAILABLE #include #include "Client.h" #include "Server.h" #include "Udp.h" +#include "Dhcp.h" enum EthernetLinkStatus { Unknown, @@ -108,6 +112,10 @@ class EthernetClass { friend class EthernetClient; friend class EthernetServer; friend class EthernetUDP; + + static void setHostName(const char * newHostName); + static char * getHostName(); + private: // Opens a socket(TCP or UDP or IP_RAW mode) static uint8_t socketBegin(uint8_t protocol, uint16_t port); @@ -297,6 +305,8 @@ class DhcpClass { uint8_t _dhcp_state; EthernetUDP _dhcpUdpSocket; + static char _hostName[HOST_NAME_LENGTH]; + int request_DHCP_lease(); void reset_DHCP_lease(); void presend_DHCP(); @@ -313,9 +323,11 @@ class DhcpClass { int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); int checkLease(); -}; + static void setHostName(const char * newHostName); + static char * getHostName(); +};