Skip to content

Commit

Permalink
Merge pull request #491 from soldierkam/edge
Browse files Browse the repository at this point in the history
DHCP option 12 support (hostname)
  • Loading branch information
wolfmanjm committed Aug 20, 2014
2 parents 1bd3e4a + aacbe57 commit 8b4e17b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
38 changes: 34 additions & 4 deletions src/libs/Network/uip/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define network_telnet_checksum CHECKSUM("telnet")
#define network_mac_override_checksum CHECKSUM("mac_override")
#define network_ip_address_checksum CHECKSUM("ip_address")
#define network_hostname_checksum CHECKSUM("hostname")
#define network_ip_gateway_checksum CHECKSUM("ip_gateway")
#define network_ip_mask_checksum CHECKSUM("ip_mask")

Expand All @@ -55,11 +56,15 @@ Network::Network()
theNetwork= this;
sftpd= NULL;
instance= this;
hostname = NULL;
}

Network::~Network()
{
delete ethernet;
if (hostname != NULL) {
delete hostname;
}
}

static uint32_t getSerialNumberHash()
Expand Down Expand Up @@ -96,6 +101,24 @@ static bool parse_ip_str(const string &s, uint8_t *a, int len, char sep = '.')
return true;
}

static bool parse_hostname(const string &s)
{
const std::string::size_type str_len = s.size();
if(str_len > 63){
return false;
}
for (unsigned int i = 0; i < str_len; i++) {
const char c = s.at(i);
if(!(c >= 'a' && c <= 'z')
&& !(c >= 'A' && c <= 'Z')
&& !(i != 0 && c >= '0' && c <= '9')
&& !(i != 0 && i != str_len - 1 && c == '-')){
return false;
}
}
return true;
}

void Network::on_module_loaded()
{
if ( !THEKERNEL->config->value( network_checksum, network_enable_checksum )->by_default(false)->as_bool() ) {
Expand Down Expand Up @@ -128,12 +151,20 @@ void Network::on_module_loaded()
ethernet->set_mac(mac_address);

// get IP address, mask and gateway address here....
bool bad = false;
string s = THEKERNEL->config->value( network_checksum, network_ip_address_checksum )->by_default("auto")->as_string();
if (s == "auto") {
use_dhcp = true;

s = THEKERNEL->config->value( network_checksum, network_hostname_checksum )->as_string();
if (!s.empty()) {
if(parse_hostname(s)){
hostname = new char [s.length() + 1];
strcpy(hostname, s.c_str());
}else{
printf("Invalid hostname: %s\n", s.c_str());
}
}
} else {
bool bad = false;
use_dhcp = false;
if (!parse_ip_str(s, ipaddr, 4)) {
printf("Invalid IP address: %s\n", s.c_str());
Expand All @@ -149,7 +180,6 @@ void Network::on_module_loaded()
printf("Invalid IP gateway: %s\n", s.c_str());
bad = true;
}

if (bad) {
printf("Network not started due to errors in config");
return;
Expand Down Expand Up @@ -338,7 +368,7 @@ void Network::init(void)

}else{
#if UIP_CONF_UDP
dhcpc_init(mac_address, sizeof(mac_address));
dhcpc_init(mac_address, sizeof(mac_address), hostname);
dhcpc_request();
printf("Getting IP address....\n");
#endif
Expand Down
1 change: 1 addition & 0 deletions src/libs/Network/uip/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Network : public Module
uint8_t ipaddr[4];
uint8_t ipmask[4];
uint8_t ipgw[4];
char *hostname;
volatile uint32_t tickcnt;

};
Expand Down
23 changes: 21 additions & 2 deletions src/libs/Network/uip/dhcpc/dhcpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct dhcp_msg {
#define DHCP_OPTION_SUBNET_MASK 1
#define DHCP_OPTION_ROUTER 3
#define DHCP_OPTION_DNS_SERVER 6
#define DHCP_OPTION_HOSTNAME 12
#define DHCP_OPTION_REQ_IPADDR 50
#define DHCP_OPTION_LEASE_TIME 51
#define DHCP_OPTION_MSG_TYPE 53
Expand Down Expand Up @@ -127,13 +128,29 @@ add_req_ipaddr(u8_t *optptr)
}
/*---------------------------------------------------------------------------*/
static u8_t *
add_hostname(u8_t *optptr)
{
if (s.hostname == NULL) {
return optptr;
}
const u8_t l = strlen(s.hostname);
*optptr++ = DHCP_OPTION_HOSTNAME;
*optptr++ = l;
memcpy(optptr, s.hostname, l);
return optptr + l;
}
/*---------------------------------------------------------------------------*/
static u8_t *
add_req_options(u8_t *optptr)
{
*optptr++ = DHCP_OPTION_REQ_LIST;
*optptr++ = 3;
*optptr++ = s.hostname == NULL ? 3 : 4;
*optptr++ = DHCP_OPTION_SUBNET_MASK;
*optptr++ = DHCP_OPTION_ROUTER;
*optptr++ = DHCP_OPTION_DNS_SERVER;
if (s.hostname != NULL) {
*optptr++ = DHCP_OPTION_HOSTNAME;
}
return optptr;
}
/*---------------------------------------------------------------------------*/
Expand Down Expand Up @@ -196,6 +213,7 @@ send_request(int rea)
end = add_msg_type(&m->options[4], DHCPREQUEST);
end = add_server_id(end);
end = add_req_ipaddr(end);
end = add_hostname(end);
end = add_end(end);

uip_send(uip_appdata, end - (u8_t *)uip_appdata);
Expand Down Expand Up @@ -350,12 +368,13 @@ PT_THREAD(handle_dhcp(void))
}
/*---------------------------------------------------------------------------*/
void
dhcpc_init(const void *mac_addr, int mac_len)
dhcpc_init(const void *mac_addr, int mac_len, char *hostname)
{
uip_ipaddr_t addr;

s.mac_addr = mac_addr;
s.mac_len = mac_len;
s.hostname = hostname;

s.state = STATE_INITIAL;
uip_ipaddr(addr, 255, 255, 255, 255);
Expand Down
4 changes: 2 additions & 2 deletions src/libs/Network/uip/dhcpc/dhcpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct dhcpc_state {
u16_t ticks;
const void *mac_addr;
int mac_len;

char *hostname;
u8_t serverid[4];

uint32_t lease_time;
Expand All @@ -58,7 +58,7 @@ struct dhcpc_state {
extern "C" {
#endif

void dhcpc_init(const void *mac_addr, int mac_len);
void dhcpc_init(const void *mac_addr, int mac_len, char *hostname);
void dhcpc_request(void);

void dhcpc_appcall(void);
Expand Down

0 comments on commit 8b4e17b

Please sign in to comment.