From b8fbffeac47e285bd68db80a0d5e716d8997df42 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Wed, 14 Dec 2011 00:55:10 -0500 Subject: [PATCH] Fixing DHCP hostname (peter). http://code.google.com/p/arduino/issues/detail?id=742 --- libraries/Ethernet/Dhcp.cpp | 20 +++++++++++++++----- libraries/Ethernet/Dhcp.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/libraries/Ethernet/Dhcp.cpp b/libraries/Ethernet/Dhcp.cpp index e7a29323b..62bbfebc0 100755 --- a/libraries/Ethernet/Dhcp.cpp +++ b/libraries/Ethernet/Dhcp.cpp @@ -163,15 +163,15 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) // OPT - host name buffer[16] = hostName; - buffer[17] = strlen(HOST_NAME) + 3; // length of hostname + last 3 bytes of mac address + buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address strcpy((char*)&(buffer[18]), HOST_NAME); - buffer[24] = _dhcpMacAddr[3]; - buffer[25] = _dhcpMacAddr[4]; - buffer[26] = _dhcpMacAddr[5]; + 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, 27); + _dhcpUdpSocket.write(buffer, 30); if(messageType == DHCP_REQUEST) { @@ -347,3 +347,13 @@ IPAddress DhcpClass::getDnsServerIp() return IPAddress(_dhcpDnsServerIp); } +void DhcpClass::printByte(char * buf, uint8_t n ) { + char *str = &buf[1]; + buf[0]='0'; + do { + unsigned long m = n; + n /= 16; + char c = m - 16 * n; + *str-- = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); +} diff --git a/libraries/Ethernet/Dhcp.h b/libraries/Ethernet/Dhcp.h index f87719959..149876d79 100755 --- a/libraries/Ethernet/Dhcp.h +++ b/libraries/Ethernet/Dhcp.h @@ -143,6 +143,7 @@ private: void presend_DHCP(); void send_DHCP_MESSAGE(uint8_t, uint16_t); + void printByte(char *, uint8_t); uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId); public: