Skip to content

Commit 6ad334a

Browse files
committed
create option for setting and getting the hostname
1 parent d31148d commit 6ad334a

File tree

7 files changed

+192
-13
lines changed

7 files changed

+192
-13
lines changed

docs/api.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,83 @@ void setup() {
473473
void loop () {}
474474
```
475475

476+
### `Ethernet.setHostName()`
477+
478+
#### Description
479+
480+
Set the hostname of the device. This is used in DHCP requests and responses.
481+
482+
483+
#### Syntax
484+
485+
```
486+
Ethernet.setHostName(hostName)
487+
488+
```
489+
490+
#### Parameters
491+
- hostName: the hostname of the device (const char*)
492+
493+
#### Returns
494+
Nothing
495+
496+
#### Example
497+
498+
```
499+
#include <SPI.h>
500+
#include <Ethernet.h>
501+
502+
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
503+
char hostName[] = "NameOfTheDevice";
504+
505+
void setup() {
506+
Ethernet.setHostName(hostName);
507+
Ethernet.begin(mac);
508+
}
509+
510+
void loop () {}
511+
```
512+
513+
### `Ethernet.getHostName()`
514+
515+
#### Description
516+
517+
Get the hostname of the device. This is used in DHCP requests and responses.
518+
519+
520+
#### Syntax
521+
522+
```
523+
Ethernet.getHostName()
524+
525+
```
526+
527+
#### Parameters
528+
none
529+
530+
#### Returns
531+
- hostName: the hostname of the device (const char*)
532+
533+
#### Example
534+
535+
```
536+
#include <SPI.h>
537+
#include <Ethernet.h>
538+
539+
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
540+
541+
void setup() {
542+
Ethernet.begin(mac);
543+
const char* hostName = Ethernet.getHostName();
544+
545+
Serial.begin(9600);
546+
Serial.print("Host name: ");
547+
Serial.println(hostName);
548+
}
549+
550+
void loop () {}
551+
```
552+
476553
### `Ethernet.setGatewayIP()`
477554

478555
#### Description

examples/SetHostName/SetHostName.ino

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Set Host Name
3+
4+
This example shows you how to set the host name with the Ethernet library.
5+
6+
Circuit:
7+
* Ethernet shield attached to pins 10, 11, 12, 13
8+
9+
created 28 May 2023
10+
by Attila Herczog
11+
*/
12+
13+
#include <SPI.h>
14+
#include <Ethernet.h>
15+
16+
// Enter a MAC address for your controller below.
17+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
18+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
19+
20+
// Host name to use
21+
char hostName[] = "ExampleHostName";
22+
23+
void setup()
24+
{
25+
// Open serial communications and wait for port to open:
26+
Serial.begin(9600);
27+
while (!Serial)
28+
{
29+
; // wait for serial port to connect. Needed for native USB port only
30+
}
31+
Serial.println("Host Name Example");
32+
33+
// Set the Host Name
34+
// Call this function before Ethernet.begin() to set your host name.
35+
Ethernet.setHostName(hostName);
36+
37+
// Start the Ethernet connection and the server:
38+
Ethernet.begin(mac);
39+
40+
// Check for Ethernet hardware present
41+
if (Ethernet.hardwareStatus() == EthernetNoHardware)
42+
{
43+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
44+
while (true)
45+
{
46+
delay(1); // do nothing, no point running without Ethernet hardware
47+
}
48+
}
49+
if (Ethernet.linkStatus() == LinkOFF)
50+
{
51+
Serial.println("Ethernet cable is not connected.");
52+
}
53+
54+
Serial.print("My IP is: ");
55+
Serial.println(Ethernet.localIP());
56+
Serial.print("My host name is: ");
57+
Serial.println(Ethernet.getHostName());
58+
Serial.println("You can now check your router's DHCP table to see the assigned host name.");
59+
}
60+
61+
void loop() {}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Ethernet
2-
version=2.0.2
2+
version=2.0.3
33
author=Various (see AUTHORS file for details)
44
maintainer=Arduino <[email protected]>
55
sentence=Enables network connection (local and Internet) using the Arduino Ethernet Board or Shield.

src/Dhcp.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
#include "Dhcp.h"
77
#include "utility/w5100.h"
88

9-
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
9+
int DhcpClass::beginWithDHCP(uint8_t *mac, const char *hostName, unsigned long timeout, unsigned long responseTimeout)
1010
{
1111
_dhcpLeaseTime=0;
1212
_dhcpT1=0;
1313
_dhcpT2=0;
1414
_timeout = timeout;
1515
_responseTimeout = responseTimeout;
16+
_dhcpHostName = hostName;
1617

1718
// zero out _dhcpMacAddr
1819
memset(_dhcpMacAddr, 0, 6);
@@ -186,17 +187,16 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
186187
buffer[9] = 0x01;
187188
memcpy(buffer + 10, _dhcpMacAddr, 6);
188189

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

194-
printByte((char*)&(buffer[24]), _dhcpMacAddr[3]);
195-
printByte((char*)&(buffer[26]), _dhcpMacAddr[4]);
196-
printByte((char*)&(buffer[28]), _dhcpMacAddr[5]);
192+
// OPT - host name
193+
buffer[0] = hostName;
194+
uint8_t hostNameLength = strlen(_dhcpHostName);
195+
buffer[1] = hostNameLength;
196+
strcpy((char*)&(buffer[2]), _dhcpHostName);
197197

198198
//put data in W5100 transmit buffer
199-
_dhcpUdpSocket.write(buffer, 30);
199+
_dhcpUdpSocket.write(buffer, hostNameLength + 2);
200200

201201
if (messageType == DHCP_REQUEST) {
202202
buffer[0] = dhcpRequestedIPaddr;

src/Dhcp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#define MAGIC_COOKIE 0x63825363
4343
#define MAX_DHCP_OPT 16
4444

45-
#define HOST_NAME "WIZnet"
4645
#define DEFAULT_LEASE (900) //default lease time in seconds
4746

4847
#define DHCP_CHECK_NONE (0)

src/Ethernet.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
IPAddress EthernetClass::_dnsServerAddress;
2727
DhcpClass* EthernetClass::_dhcp = NULL;
28+
bool EthernetClass::_manualHostName = false;
29+
char EthernetClass::_hostName[HOST_NAME_MAX_LEN] = "";
2830

2931
int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
3032
{
@@ -38,8 +40,13 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp
3840
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
3941
SPI.endTransaction();
4042

43+
// Generate a default host name based on the MAC address if not already set by user
44+
if(!_manualHostName) {
45+
generateDefaultHostName(mac);
46+
}
47+
4148
// Now try to get our config info from a DHCP server
42-
int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout);
49+
int ret = _dhcp->beginWithDHCP(mac, _hostName, timeout, responseTimeout);
4350
if (ret == 1) {
4451
// We've successfully found a DHCP server and got our configuration
4552
// info, so set things accordingly
@@ -224,6 +231,32 @@ void EthernetClass::setRetransmissionCount(uint8_t num)
224231
SPI.endTransaction();
225232
}
226233

234+
void EthernetClass::generateDefaultHostName(uint8_t *mac) {
235+
// Copy the default host name base
236+
strcpy(_hostName, DEFAULT_HOST_NAME);
237+
238+
// Append the last 3 bytes of the MAC (HEX'd)
239+
char macAddrStr[3];
240+
sprintf(macAddrStr, "%02X", mac[3]);
241+
strcat(_hostName, macAddrStr);
242+
sprintf(macAddrStr, "%02X", mac[4]);
243+
strcat(_hostName, macAddrStr);
244+
sprintf(macAddrStr, "%02X", mac[5]);
245+
strcat(_hostName, macAddrStr);
246+
}
247+
248+
void EthernetClass::setHostName(const char *dhcpHost) {
249+
// Copy the host name and ensure it is null terminated
250+
strncpy(_hostName, dhcpHost, HOST_NAME_MAX_LEN);
251+
_hostName[HOST_NAME_MAX_LEN - 1] = '\0';
252+
253+
// Indicate that a host name has been set manually
254+
_manualHostName = true;
255+
}
256+
257+
char* EthernetClass::getHostName() {
258+
return _hostName;
259+
}
227260

228261

229262

src/Ethernet.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
#include "Server.h"
5454
#include "Udp.h"
5555

56+
#define DEFAULT_HOST_NAME "WIZnet"
57+
#define HOST_NAME_MAX_LEN 20 // Max 30 or change the DHCP local buffer size
58+
5659
enum EthernetLinkStatus {
5760
Unknown,
5861
LinkON,
@@ -75,6 +78,8 @@ class EthernetClass {
7578
private:
7679
static IPAddress _dnsServerAddress;
7780
static DhcpClass* _dhcp;
81+
static char _hostName[HOST_NAME_MAX_LEN];
82+
static bool _manualHostName;
7883
public:
7984
// Initialise the Ethernet shield to use the provided MAC address and
8085
// gain the rest of the configuration through DHCP.
@@ -104,6 +109,8 @@ class EthernetClass {
104109
void setDnsServerIP(const IPAddress dns_server) { _dnsServerAddress = dns_server; }
105110
void setRetransmissionTimeout(uint16_t milliseconds);
106111
void setRetransmissionCount(uint8_t num);
112+
void setHostName(const char *hostName);
113+
char* getHostName();
107114

108115
friend class EthernetClient;
109116
friend class EthernetServer;
@@ -142,6 +149,7 @@ class EthernetClass {
142149
static bool socketSendUDP(uint8_t s);
143150
// Initialize the "random" source port number
144151
static void socketPortRand(uint16_t n);
152+
static void generateDefaultHostName(uint8_t *mac);
145153
};
146154

147155
extern EthernetClass Ethernet;
@@ -275,6 +283,7 @@ class DhcpClass {
275283
uint32_t _dhcpInitialTransactionId;
276284
uint32_t _dhcpTransactionId;
277285
uint8_t _dhcpMacAddr[6];
286+
const char* _dhcpHostName;
278287
#ifdef __arm__
279288
uint8_t _dhcpLocalIp[4] __attribute__((aligned(4)));
280289
uint8_t _dhcpSubnetMask[4] __attribute__((aligned(4)));
@@ -312,7 +321,7 @@ class DhcpClass {
312321
IPAddress getDhcpServerIp();
313322
IPAddress getDnsServerIp();
314323

315-
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
324+
int beginWithDHCP(uint8_t *, const char *hostName, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
316325
int checkLease();
317326
};
318327

0 commit comments

Comments
 (0)