-
Notifications
You must be signed in to change notification settings - Fork 267
Updated with Set Host Name fuctionality -Tested with Arduino 1.6.13 #50
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
DHCP-based IP printer | ||
|
||
This sketch uses the DHCP extensions to the Ethernet library | ||
to get an IP address via DHCP and print the address obtained. | ||
using an Arduino Wiznet Ethernet shield. | ||
|
||
Circuit: | ||
* Ethernet shield attached to pins 10, 11, 12, 13 | ||
|
||
created 12 April 2011 | ||
modified 9 Apr 2012 | ||
by Tom Igoe | ||
|
||
Modified by Sudheer Thimmaiya | ||
Updated on 15 Sept 2017 | ||
Set DHCP Host Name: | ||
char SetHostName[] = "HOSTNAMEUINO"; | ||
Ethernet.hostName(SetHostName); | ||
Print Host Name: | ||
Serial.println(Ethernet.getHostName()); | ||
*/ | ||
|
||
#include <SPI.h> | ||
#include <Ethernet.h> | ||
|
||
// Enter a MAC address for your controller below. | ||
// Newer Ethernet shields have a MAC address printed on a sticker on the shield | ||
byte mac[] = { | ||
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 | ||
}; | ||
|
||
// Host Name MAX 12 Characters | ||
char SetHostName[] = "HOSTNAMEUINO"; | ||
|
||
// Initialize the Ethernet client library | ||
// with the IP address and port of the server | ||
// that you want to connect to (port 80 is default for HTTP): | ||
EthernetClient client; | ||
|
||
void setup() { | ||
// Open serial communications and wait for port to open: | ||
Serial.begin(9600); | ||
// this check is only needed on the Leonardo: | ||
while (!Serial) { | ||
; // wait for serial port to connect. Needed for Leonardo only | ||
} | ||
|
||
// Set DHCP Host Name - New Feature (Updated) | ||
Ethernet.hostName(SetHostName); | ||
// start the Ethernet connection: | ||
if (Ethernet.begin(mac) == 0) { | ||
Serial.println(); | ||
Serial.println("Failed to configure Ethernet using DHCP"); | ||
// no point in carrying on, so do nothing forevermore: | ||
for (;;) | ||
; | ||
} | ||
// print your local IP address: | ||
Serial.print("My IP address: "); | ||
for (byte thisByte = 0; thisByte < 4; thisByte++) { | ||
// print the value of each byte of the IP address: | ||
Serial.print(Ethernet.localIP()[thisByte], DEC); | ||
Serial.print("."); | ||
} | ||
Serial.println(); | ||
Serial.print("Host Name: "); | ||
Serial.print(Ethernet.getHostName()); | ||
} | ||
|
||
void loop() { | ||
|
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,12 @@ | |
#include "Arduino.h" | ||
#include "utility/util.h" | ||
|
||
char HOST_NAME[12] = "WIZNet"; | ||
|
||
void DhcpClass::setHostName(char *dhcpHost) { | ||
strcpy(HOST_NAME, dhcpHost); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing sanity check. |
||
} | ||
// | ||
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) | ||
{ | ||
_dhcpLeaseTime=0; | ||
|
@@ -203,15 +209,24 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) | |
memcpy(buffer + 10, _dhcpMacAddr, 6); | ||
|
||
// OPT - host name | ||
if (strcmp ("WIZNet",HOST_NAME) == 0) // If Default 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]); | ||
|
||
//put data in W5100 transmit buffer | ||
} | ||
else | ||
{ | ||
buffer[16] = hostName; | ||
buffer[17] = strlen(HOST_NAME); // length of hostname + last 3 bytes of mac address | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrong comment. |
||
strcpy((char*)&(buffer[18]), HOST_NAME); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible buffer overflow. |
||
} | ||
|
||
//put data in W5100 transmit buffer | ||
_dhcpUdpSocket.write(buffer, 30); | ||
|
||
if(messageType == DHCP_REQUEST) | ||
|
@@ -459,6 +474,11 @@ IPAddress DhcpClass::getDnsServerIp() | |
return IPAddress(_dhcpDnsServerIp); | ||
} | ||
|
||
//Get Host Name. | ||
char * DhcpClass::getHostName() { | ||
return HOST_NAME; | ||
} | ||
|
||
void DhcpClass::printByte(char * buf, uint8_t n ) { | ||
char *str = &buf[1]; | ||
buf[0]='0'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ | |
#define MAGIC_COOKIE 0x63825363 | ||
#define MAX_DHCP_OPT 16 | ||
|
||
#define HOST_NAME "WIZnet" | ||
//#define HOST_NAME "WIZnet" // Commented Modififed by Sudheer Thimmaiya | ||
#define DEFAULT_LEASE (900) //default lease time in seconds | ||
|
||
#define DHCP_CHECK_NONE (0) | ||
|
@@ -172,6 +172,9 @@ class DhcpClass { | |
|
||
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); | ||
int checkLease(); | ||
// | ||
void setHostName(char *); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing documentation: min max value; type of input: char array vs string. |
||
char * getHostName(); | ||
}; | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,12 @@ uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { | |
uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { | ||
0, 0, 0, 0 }; | ||
|
||
//DHCP Set Host Name. | ||
void EthernetClass::hostName(char *dhcp_HostName){ | ||
_dhcp->setHostName(dhcp_HostName); | ||
} | ||
|
||
|
||
int EthernetClass::begin(uint8_t *mac_address, unsigned long timeout, unsigned long responseTimeout) | ||
{ | ||
static DhcpClass s_dhcp; | ||
|
@@ -133,4 +139,10 @@ IPAddress EthernetClass::dnsServerIP() | |
return _dnsServerAddress; | ||
} | ||
|
||
char *EthernetClass::getHostName() | ||
{ | ||
char *_getDhcpHostName = _dhcp ->getHostName(); | ||
return _getDhcpHostName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be simplified with previous line. |
||
} | ||
// | ||
EthernetClass Ethernet; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,9 @@ class EthernetClass { | |
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet); | ||
int maintain(); | ||
|
||
void hostName(char *dhcp_HostName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misleading naming. Should begin with |
||
char* getHostName(); | ||
|
||
IPAddress localIP(); | ||
IPAddress subnetMask(); | ||
IPAddress gatewayIP(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UPPERCASE is for constants.