Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
cassy73 authored Dec 15, 2016
1 parent 64ef152 commit c606b36
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 252 deletions.
50 changes: 31 additions & 19 deletions Dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
#include <string.h>
#include <stdlib.h>
#include "Dhcp.h"
#include "Arduino.h"
#if defined(ARDUINO)
#include <Arduino.h>
#endif
#if defined(__MBED__)
#include <mbed.h>
#include "mbed/millis.h"
#define delay(x) wait_ms(x)
#endif
#include "utility/logging.h"
#include "utility/uip.h"

Expand All @@ -26,20 +33,25 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long
return request_DHCP_lease();
}

void DhcpClass::reset_DHCP_lease(){
void DhcpClass::reset_DHCP_lease(void){
// zero out _dhcpSubnetMask, _dhcpGatewayIp, _dhcpLocalIp, _dhcpDhcpServerIp, _dhcpDnsServerIp
memset(_dhcpLocalIp, 0, 20);
}

//return:0 on error, 1 if request is sent and response is received
int DhcpClass::request_DHCP_lease(){
int DhcpClass::request_DHCP_lease(void){

uint8_t messageType = 0;



// Pick an initial transaction ID
_dhcpTransactionId = random(1UL, 2000UL);
#if defined(ARDUINO)
_dhcpTransactionId = random(1UL, 2000UL);
#endif
#if defined(__MBED__)
_dhcpTransactionId = (rand() % 2000UL) + 1;
#endif
_dhcpInitialTransactionId = _dhcpTransactionId;

_dhcpUdpSocket.stop();
Expand Down Expand Up @@ -127,7 +139,7 @@ int DhcpClass::request_DHCP_lease(){
return result;
}

void DhcpClass::presend_DHCP()
void DhcpClass::presend_DHCP(void)
{
}

Expand Down Expand Up @@ -266,7 +278,7 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
}
// start reading in the packet
RIP_MSG_FIXED fixedMsg;
_dhcpUdpSocket.read((uint8_t*)&fixedMsg, sizeof(RIP_MSG_FIXED));
_dhcpUdpSocket.read((char*)&fixedMsg, sizeof(RIP_MSG_FIXED));

if(fixedMsg.op == DHCP_BOOTREPLY && _dhcpUdpSocket.remotePort() == DHCP_SERVER_PORT)
{
Expand Down Expand Up @@ -305,12 +317,12 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr

case subnetMask :
opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read(_dhcpSubnetMask, 4);
_dhcpUdpSocket.read((char*)_dhcpSubnetMask, 4);
break;

case routersOnSubnet :
opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read(_dhcpGatewayIp, 4);
_dhcpUdpSocket.read((char*)_dhcpGatewayIp, 4);
for (int i = 0; i < opt_len-4; i++)
{
_dhcpUdpSocket.read();
Expand All @@ -319,7 +331,7 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr

case dns :
opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
_dhcpUdpSocket.read((char*)_dhcpDnsServerIp, 4);
for (int i = 0; i < opt_len-4; i++)
{
_dhcpUdpSocket.read();
Expand All @@ -331,7 +343,7 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
if( *((uint32_t*)_dhcpDhcpServerIp) == 0 ||
IPAddress(_dhcpDhcpServerIp) == _dhcpUdpSocket.remoteIP() )
{
_dhcpUdpSocket.read(_dhcpDhcpServerIp, sizeof(_dhcpDhcpServerIp));
_dhcpUdpSocket.read((char*)_dhcpDhcpServerIp, sizeof(_dhcpDhcpServerIp));
}
else
{
Expand All @@ -345,19 +357,19 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr

case dhcpT1value :
opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read((uint8_t*)&_dhcpT1, sizeof(_dhcpT1));
_dhcpUdpSocket.read((char*)&_dhcpT1, sizeof(_dhcpT1));
_dhcpT1 = ntohl(_dhcpT1);
break;

case dhcpT2value :
opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read((uint8_t*)&_dhcpT2, sizeof(_dhcpT2));
_dhcpUdpSocket.read((char*)&_dhcpT2, sizeof(_dhcpT2));
_dhcpT2 = ntohl(_dhcpT2);
break;

case dhcpIPaddrLeaseTime :
opt_len = _dhcpUdpSocket.read();
_dhcpUdpSocket.read((uint8_t*)&_dhcpLeaseTime, sizeof(_dhcpLeaseTime));
_dhcpUdpSocket.read((char*)&_dhcpLeaseTime, sizeof(_dhcpLeaseTime));
_dhcpLeaseTime = ntohl(_dhcpLeaseTime);
_renewInSec = _dhcpLeaseTime;
break;
Expand Down Expand Up @@ -389,7 +401,7 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
3/DHCP_CHECK_REBIND_FAIL: rebind fail
4/DHCP_CHECK_REBIND_OK: rebind success
*/
int DhcpClass::checkLease(){
int DhcpClass::checkLease(void){
//this uses a signed / unsigned trick to deal with millis overflow
unsigned long now = millis();
signed long snow = (long)now;
Expand Down Expand Up @@ -442,27 +454,27 @@ int DhcpClass::checkLease(){
return rc;
}

IPAddress DhcpClass::getLocalIp()
IPAddress DhcpClass::getLocalIp(void)
{
return IPAddress(_dhcpLocalIp);
}

IPAddress DhcpClass::getSubnetMask()
IPAddress DhcpClass::getSubnetMask(void)
{
return IPAddress(_dhcpSubnetMask);
}

IPAddress DhcpClass::getGatewayIp()
IPAddress DhcpClass::getGatewayIp(void)
{
return IPAddress(_dhcpGatewayIp);
}

IPAddress DhcpClass::getDhcpServerIp()
IPAddress DhcpClass::getDhcpServerIp(void)
{
return IPAddress(_dhcpDhcpServerIp);
}

IPAddress DhcpClass::getDnsServerIp()
IPAddress DhcpClass::getDnsServerIp(void)
{
return IPAddress(_dhcpDnsServerIp);
}
Expand Down
18 changes: 9 additions & 9 deletions Dhcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,22 @@ class DhcpClass {
uint8_t _dhcp_state;
UIPUDP _dhcpUdpSocket;

int request_DHCP_lease();
void reset_DHCP_lease();
void presend_DHCP();
int request_DHCP_lease(void);
void reset_DHCP_lease(void);
void presend_DHCP(void);
void send_DHCP_MESSAGE(uint8_t, uint16_t);
void printByte(char *, uint8_t);

uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
public:
IPAddress getLocalIp();
IPAddress getSubnetMask();
IPAddress getGatewayIp();
IPAddress getDhcpServerIp();
IPAddress getDnsServerIp();
IPAddress getLocalIp(void);
IPAddress getSubnetMask(void);
IPAddress getGatewayIp(void);
IPAddress getDhcpServerIp(void);
IPAddress getDnsServerIp(void);

int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
int checkLease();
int checkLease(void);
};

#endif
31 changes: 19 additions & 12 deletions Dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
// (c) Copyright 2009-2010 MCQN Ltd.
// Released under Apache License, version 2.0

#include "Udp.h"

#include "Dns.h"
#include <string.h>
//#include <stdlib.h>
#include "Arduino.h"
#if defined(ARDUINO)
#include "Arduino.h"
#include "Udp.h"
#endif
#if defined(__MBED__)
#include <mbed.h>
#include "mbed/Udp.h"
#include "mbed/millis.h"
#define delay(x) wait_ms(x)
#endif
#include "utility/logging.h"
#include "utility/uip.h"

Expand Down Expand Up @@ -82,7 +89,7 @@ int DNSClient::inet_aton(const char* aIPAddrString, IPAddress& aResult)
}
else
{
aResult[segment] = (byte)segmentValue;
aResult[segment] = (uint8_t)segmentValue;
segment++;
segmentValue = 0;
}
Expand All @@ -104,7 +111,7 @@ int DNSClient::inet_aton(const char* aIPAddrString, IPAddress& aResult)
}
else
{
aResult[segment] = (byte)segmentValue;
aResult[segment] = (uint8_t)segmentValue;
return 1;
}
}
Expand Down Expand Up @@ -279,7 +286,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress)
{
return TRUNCATED;
}
iUdp.read(header, DNS_HEADER_SIZE);
iUdp.read((char*)header, DNS_HEADER_SIZE);

uint16_t header_flags = htons(*((uint16_t*)&header[2]));
// Check that it's a response to this request
Expand Down Expand Up @@ -315,7 +322,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress)
uint8_t len;
do
{
iUdp.read(&len, sizeof(len));
iUdp.read((char*)&len, sizeof(len));
if (len > 0)
{
// Don't need to actually read the data out for the string, just
Expand Down Expand Up @@ -345,7 +352,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress)
uint8_t len;
do
{
iUdp.read(&len, sizeof(len));
iUdp.read((char*)&len, sizeof(len));
if ((len & LABEL_COMPRESSION_MASK) == 0)
{
// It's just a normal label
Expand Down Expand Up @@ -378,8 +385,8 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress)
// Check the type and class
uint16_t answerType;
uint16_t answerClass;
iUdp.read((uint8_t*)&answerType, sizeof(answerType));
iUdp.read((uint8_t*)&answerClass, sizeof(answerClass));
iUdp.read((char*)&answerType, sizeof(answerType));
iUdp.read((char*)&answerClass, sizeof(answerClass));

// Ignore the Time-To-Live as we don't do any caching
for (int i =0; i < TTL_SIZE; i++)
Expand All @@ -389,7 +396,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress)

// And read out the length of this answer
// Don't need header_flags anymore, so we can reuse it here
iUdp.read((uint8_t*)&header_flags, sizeof(header_flags));
iUdp.read((char*)&header_flags, sizeof(header_flags));

if ( (htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN) )
{
Expand All @@ -400,7 +407,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress)
iUdp.flush();
return -9;//INVALID_RESPONSE;
}
iUdp.read(aAddress.raw_address(), 4);
iUdp.read((char*)aAddress.raw_address(), 4);
return SUCCESS;
}
else
Expand Down
2 changes: 1 addition & 1 deletion Dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef DNSClient_h
#define DNSClient_h

#include <UIPUdp.h>
#include "UIPUdp.h"

class DNSClient
{
Expand Down
Loading

0 comments on commit c606b36

Please sign in to comment.