FreeNOS
Data Structures | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
DhcpClient Class Reference

Dynamic Host Configuration Protocol (DHCP) client application. More...

#include <DhcpClient.h>

Inheritance diagram for DhcpClient:
POSIXApplication Application

Data Structures

struct  Header
 Protocol packet header. More...
 

Public Member Functions

 DhcpClient (int argc, char **argv)
 Class constructor. More...
 
virtual ~DhcpClient ()
 Class destructor. More...
 
virtual Result initialize ()
 Initialize the application. More...
 
virtual Result exec ()
 Execute the application event loop. More...
 
- Public Member Functions inherited from POSIXApplication
 POSIXApplication (int argc, char **argv)
 Class constructor. More...
 
virtual ~POSIXApplication ()
 Class destructor. More...
 
- Public Member Functions inherited from Application
 Application (int argc, char **argv)
 Class constructor. More...
 
virtual ~Application ()
 Class destructor. More...
 
virtual int run ()
 Run the application. More...
 

Private Types

enum  Operation { BootRequest = 1, BootResponse = 2 }
 DHCP operations. More...
 
enum  MessageType {
  Discover = 1, Offer = 2, Request = 3, Decline = 4,
  Ack = 5, Nak = 6, Release = 7
}
 DHCP message types. More...
 
enum  Options {
  SubnetMask = 1, Router = 3, DomainNameServer = 6, RequestedIP = 50,
  DhcpMessageType = 53, ServerIdentifier = 54, ParameterRequestList = 55, EndMark = 255
}
 DHCP options. More...
 

Private Member Functions

Result setIpAddress (const char *device, const IPV4::Address ipAddr) const
 Set IP address on a device. More...
 
Result discover (const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
 Send DHCP Discover message. More...
 
Result offer (IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
 Receive DHCP Offer message. More...
 
Result request (const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
 Send DHCP Request message. More...
 
Result acknowledge (IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
 Receive DHCP Acknowledge message. More...
 
Result sendBootRequest (const DhcpClient::MessageType messageType, const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
 Send DHCP boot request. More...
 
Result receiveBootResponse (const DhcpClient::MessageType messageType, IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
 Receive DHCP boot response. More...
 
Result udpSend (const void *packet, const Size size) const
 Send UDP broadcast packet. More...
 
Result udpReceive (void *packet, Size &size) const
 Receive UDP packet. More...
 

Private Attributes

NetworkClientm_client
 Network client. More...
 
int m_socket
 UDP socket. More...
 
Ethernet::Address m_etherAddress
 Host ethernet address. More...
 
u32 m_transactionId
 Transaction ID of the current request. More...
 

Static Private Attributes

static const u16 ServerPort = 67
 Server UDP port. More...
 
static const u16 ClientPort = 68
 Client UDP port. More...
 
static const Size MaximumRetries = 25
 Maximum number of retries to receive an IP address. More...
 
static const u32 MagicValue = 0x63825363
 Magic number value for the packet header. More...
 
static const Size ReceiveTimeoutMs = 500
 Timeout in milliseconds to wait for packet receive. More...
 

Additional Inherited Members

- Public Types inherited from Application
enum  Result {
  Success, NotFound, IOError, InvalidArgument,
  ShowUsage, TimedOut, OutOfMemory
}
 Result codes. More...
 
- Protected Member Functions inherited from POSIXApplication
virtual Result output (const char *string) const
 Print text to output. More...
 
int runProgram (const char *path, const char **argv)
 Runs an external program. More...
 
- Protected Member Functions inherited from Application
virtual Result output (String &string) const
 Print string to output. More...
 
ArgumentParserparser ()
 Get program arguments parser. More...
 
const ArgumentParserparser () const
 Get constant program arguments parser. More...
 
const ArgumentContainerarguments () const
 Get program arguments. More...
 
void setVersion (const String &version)
 Set program version. More...
 
- Protected Attributes inherited from Application
int m_argc
 Input argument count. More...
 
char ** m_argv
 Input argument values. More...
 

Detailed Description

Dynamic Host Configuration Protocol (DHCP) client application.

See also
https://tools.ietf.org/html/rfc1541
https://tools.ietf.org/html/rfc2131
https://tools.ietf.org/html/rfc1533

Definition at line 36 of file DhcpClient.h.

Member Enumeration Documentation

◆ MessageType

DHCP message types.

Enumerator
Discover 
Offer 
Request 
Decline 
Ack 
Nak 
Release 

Definition at line 88 of file DhcpClient.h.

◆ Operation

enum DhcpClient::Operation
private

DHCP operations.

Enumerator
BootRequest 
BootResponse 

Definition at line 79 of file DhcpClient.h.

◆ Options

enum DhcpClient::Options
private

DHCP options.

Enumerator
SubnetMask 
Router 
DomainNameServer 
RequestedIP 
DhcpMessageType 
ServerIdentifier 
ParameterRequestList 
EndMark 

Definition at line 102 of file DhcpClient.h.

Constructor & Destructor Documentation

◆ DhcpClient()

DhcpClient::DhcpClient ( int  argc,
char **  argv 
)

Class constructor.

Parameters
argcArgument count
argvArgument values

Definition at line 33 of file DhcpClient.cpp.

References m_etherAddress, Application::parser(), ArgumentParser::registerPositional(), MemoryBlock::set(), and ArgumentParser::setDescription().

◆ ~DhcpClient()

DhcpClient::~DhcpClient ( )
virtual

Class destructor.

Definition at line 45 of file DhcpClient.cpp.

References m_client.

Member Function Documentation

◆ acknowledge()

DhcpClient::Result DhcpClient::acknowledge ( IPV4::Address ipAddr,
IPV4::Address ipServer,
IPV4::Address ipGateway 
) const
private

Receive DHCP Acknowledge message.

Parameters
ipAddrAcknowledged IP address on output
ipServerServer IP address on output
ipGatewayGateway IP address on output
Returns
Result code

Definition at line 205 of file DhcpClient.cpp.

References Ack, DEBUG, and receiveBootResponse().

Referenced by exec().

◆ discover()

DhcpClient::Result DhcpClient::discover ( const IPV4::Address ipAddr,
const IPV4::Address ipServer,
const IPV4::Address ipGateway 
) const
private

Send DHCP Discover message.

Parameters
ipAddrRequested IP address
ipServerServer IP address
ipGatewayGateway IP address
Returns
Result code

Definition at line 180 of file DhcpClient.cpp.

References DEBUG, Discover, and sendBootRequest().

Referenced by exec().

◆ exec()

DhcpClient::Result DhcpClient::exec ( )
virtual

Execute the application event loop.

Returns
Result code

Implements Application.

Definition at line 106 of file DhcpClient.cpp.

References acknowledge(), Application::arguments(), DEBUG, discover(), ERROR, m_transactionId, MaximumRetries, Application::NotFound, offer(), request(), setIpAddress(), Application::Success, and IPV4::toString().

◆ initialize()

DhcpClient::Result DhcpClient::initialize ( )
virtual

◆ offer()

DhcpClient::Result DhcpClient::offer ( IPV4::Address ipAddr,
IPV4::Address ipServer,
IPV4::Address ipGateway 
) const
private

Receive DHCP Offer message.

Parameters
ipAddrOffered IP address on output
ipServerServer IP address on output
ipGatewayGateway IP address on output
Returns
Result code

Definition at line 188 of file DhcpClient.cpp.

References DEBUG, Offer, and receiveBootResponse().

Referenced by exec().

◆ receiveBootResponse()

DhcpClient::Result DhcpClient::receiveBootResponse ( const DhcpClient::MessageType  messageType,
IPV4::Address ipAddr,
IPV4::Address ipServer,
IPV4::Address ipGateway 
) const
private

Receive DHCP boot response.

Parameters
messageTypeExpected value for DHCP message type option
ipAddrIP address on output
ipServerServer IP address on output
ipGatewayGateway IP address on output
Returns
Result code

Definition at line 272 of file DhcpClient.cpp.

References BootResponse, DhcpClient::Header::clientHardware, MemoryBlock::compare(), DEBUG, DhcpMessageType, DomainNameServer, EndMark, ERROR, Application::InvalidArgument, Application::IOError, m_etherAddress, m_transactionId, DhcpClient::Header::operation, ParameterRequestList, readBe32(), RequestedIP, Router, ServerIdentifier, SubnetMask, Application::Success, IPV4::toString(), DhcpClient::Header::transactionId, udpReceive(), and DhcpClient::Header::yourAddress.

Referenced by acknowledge(), and offer().

◆ request()

DhcpClient::Result DhcpClient::request ( const IPV4::Address ipAddr,
const IPV4::Address ipServer,
const IPV4::Address ipGateway 
) const
private

Send DHCP Request message.

Parameters
ipAddrRequested IP address
ipServerServer IP address
ipGatewayGateway IP address
Returns
Result code

Definition at line 197 of file DhcpClient.cpp.

References DEBUG, Request, and sendBootRequest().

Referenced by exec().

◆ sendBootRequest()

DhcpClient::Result DhcpClient::sendBootRequest ( const DhcpClient::MessageType  messageType,
const IPV4::Address ipAddr,
const IPV4::Address ipServer,
const IPV4::Address ipGateway 
) const
private

Send DHCP boot request.

Parameters
messageTypeValue for the DHCP message type option
ipAddrRequested IP address
ipServerServer IP address
ipGatewayGateway IP address
Returns
Result code

Definition at line 213 of file DhcpClient.cpp.

References BootRequest, DhcpClient::Header::clientHardware, MemoryBlock::copy(), DEBUG, DhcpMessageType, DomainNameServer, EndMark, DhcpClient::Header::hardwareLength, DhcpClient::Header::hardwareType, m_etherAddress, m_transactionId, DhcpClient::Header::magic, MagicValue, DhcpClient::Header::operation, ParameterRequestList, RequestedIP, Router, ServerIdentifier, MemoryBlock::set(), IPV4::toString(), DhcpClient::Header::transactionId, udpSend(), writeBe32(), and ZERO.

Referenced by discover(), and request().

◆ setIpAddress()

DhcpClient::Result DhcpClient::setIpAddress ( const char *  device,
const IPV4::Address  ipAddr 
) const
private

Set IP address on a device.

Parameters
deviceName of the network device
ipAddrIPV4 address to set
Returns
Result code

Definition at line 159 of file DhcpClient.cpp.

References DEBUG, ERROR, Application::IOError, BufferedFile::Success, Application::Success, IPV4::toString(), and BufferedFile::write().

Referenced by exec().

◆ udpReceive()

DhcpClient::Result DhcpClient::udpReceive ( void *  packet,
Size size 
) const
private

Receive UDP packet.

Parameters
packetPayload output
sizeOutput for number of bytes received
Returns
Result code

Definition at line 398 of file DhcpClient.cpp.

References sockaddr::addr, DEBUG, errno, ERROR, Application::IOError, m_client, m_socket, ReceiveTimeoutMs, recvfrom(), strerror(), Application::Success, NetworkClient::Success, IPV4::toString(), NetworkClient::UDP, and NetworkClient::waitSocket().

Referenced by receiveBootResponse().

◆ udpSend()

DhcpClient::Result DhcpClient::udpSend ( const void *  packet,
const Size  size 
) const
private

Send UDP broadcast packet.

Parameters
packetPayload to send
sizeNumber of bytes to send
Returns
Result code

Definition at line 376 of file DhcpClient.cpp.

References sockaddr::addr, DEBUG, errno, ERROR, Application::IOError, m_socket, sendto(), ServerPort, strerror(), and Application::Success.

Referenced by sendBootRequest().

Field Documentation

◆ ClientPort

const u16 DhcpClient::ClientPort = 68
staticprivate

Client UDP port.

Definition at line 44 of file DhcpClient.h.

Referenced by initialize().

◆ m_client

NetworkClient* DhcpClient::m_client
private

Network client.

Definition at line 263 of file DhcpClient.h.

Referenced by initialize(), udpReceive(), and ~DhcpClient().

◆ m_etherAddress

Ethernet::Address DhcpClient::m_etherAddress
private

Host ethernet address.

Definition at line 269 of file DhcpClient.h.

Referenced by DhcpClient(), initialize(), receiveBootResponse(), and sendBootRequest().

◆ m_socket

int DhcpClient::m_socket
private

UDP socket.

Definition at line 266 of file DhcpClient.h.

Referenced by initialize(), udpReceive(), and udpSend().

◆ m_transactionId

u32 DhcpClient::m_transactionId
private

Transaction ID of the current request.

Definition at line 272 of file DhcpClient.h.

Referenced by exec(), receiveBootResponse(), and sendBootRequest().

◆ MagicValue

const u32 DhcpClient::MagicValue = 0x63825363
staticprivate

Magic number value for the packet header.

Definition at line 50 of file DhcpClient.h.

Referenced by sendBootRequest().

◆ MaximumRetries

const Size DhcpClient::MaximumRetries = 25
staticprivate

Maximum number of retries to receive an IP address.

Definition at line 47 of file DhcpClient.h.

Referenced by exec().

◆ ReceiveTimeoutMs

const Size DhcpClient::ReceiveTimeoutMs = 500
staticprivate

Timeout in milliseconds to wait for packet receive.

Definition at line 53 of file DhcpClient.h.

Referenced by udpReceive().

◆ ServerPort

const u16 DhcpClient::ServerPort = 67
staticprivate

Server UDP port.

Definition at line 41 of file DhcpClient.h.

Referenced by udpSend().


The documentation for this class was generated from the following files: