|
FreeNOS
|
Dynamic Host Configuration Protocol (DHCP) client application. More...
#include <DhcpClient.h>
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 | |
| NetworkClient * | m_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... | |
| ArgumentParser & | parser () |
| Get program arguments parser. More... | |
| const ArgumentParser & | parser () const |
| Get constant program arguments parser. More... | |
| const ArgumentContainer & | arguments () 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... | |
Dynamic Host Configuration Protocol (DHCP) client application.
Definition at line 36 of file DhcpClient.h.
|
private |
DHCP message types.
| Enumerator | |
|---|---|
| Discover | |
| Offer | |
| Request | |
| Decline | |
| Ack | |
| Nak | |
| Release | |
Definition at line 88 of file DhcpClient.h.
|
private |
|
private |
DHCP options.
| Enumerator | |
|---|---|
| SubnetMask | |
| Router | |
| DomainNameServer | |
| RequestedIP | |
| DhcpMessageType | |
| ServerIdentifier | |
| ParameterRequestList | |
| EndMark | |
Definition at line 102 of file DhcpClient.h.
| DhcpClient::DhcpClient | ( | int | argc, |
| char ** | argv | ||
| ) |
Class constructor.
Definition at line 33 of file DhcpClient.cpp.
References m_etherAddress, Application::parser(), ArgumentParser::registerPositional(), MemoryBlock::set(), and ArgumentParser::setDescription().
|
virtual |
|
private |
Receive DHCP Acknowledge message.
| ipAddr | Acknowledged IP address on output |
| ipServer | Server IP address on output |
| ipGateway | Gateway IP address on output |
Definition at line 205 of file DhcpClient.cpp.
References Ack, DEBUG, and receiveBootResponse().
Referenced by exec().
|
private |
Send DHCP Discover message.
| ipAddr | Requested IP address |
| ipServer | Server IP address |
| ipGateway | Gateway IP address |
Definition at line 180 of file DhcpClient.cpp.
References DEBUG, Discover, and sendBootRequest().
Referenced by exec().
|
virtual |
Execute the application event loop.
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().
|
virtual |
Initialize the application.
Reimplemented from Application.
Definition at line 50 of file DhcpClient.cpp.
References Application::arguments(), NetworkClient::bindSocket(), BufferedFile::buffer(), ClientPort, MemoryBlock::copy(), NetworkClient::createSocket(), DEBUG, ERROR, ArgumentContainer::get(), NetworkClient::initialize(), Application::IOError, m_client, m_etherAddress, m_socket, BufferedFile::read(), BufferedFile::Success, Application::Success, NetworkClient::Success, and NetworkClient::UDP.
|
private |
Receive DHCP Offer message.
| ipAddr | Offered IP address on output |
| ipServer | Server IP address on output |
| ipGateway | Gateway IP address on output |
Definition at line 188 of file DhcpClient.cpp.
References DEBUG, Offer, and receiveBootResponse().
Referenced by exec().
|
private |
Receive DHCP boot response.
| messageType | Expected value for DHCP message type option |
| ipAddr | IP address on output |
| ipServer | Server IP address on output |
| ipGateway | Gateway IP address on output |
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().
|
private |
Send DHCP Request message.
| ipAddr | Requested IP address |
| ipServer | Server IP address |
| ipGateway | Gateway IP address |
Definition at line 197 of file DhcpClient.cpp.
References DEBUG, Request, and sendBootRequest().
Referenced by exec().
|
private |
Send DHCP boot request.
| messageType | Value for the DHCP message type option |
| ipAddr | Requested IP address |
| ipServer | Server IP address |
| ipGateway | Gateway IP address |
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().
|
private |
Set IP address on a device.
| device | Name of the network device |
| ipAddr | IPV4 address to set |
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().
|
private |
Receive UDP packet.
| packet | Payload output |
| size | Output for number of bytes received |
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().
|
private |
Send UDP broadcast packet.
| packet | Payload to send |
| size | Number of bytes to send |
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().
|
staticprivate |
|
private |
Network client.
Definition at line 263 of file DhcpClient.h.
Referenced by initialize(), udpReceive(), and ~DhcpClient().
|
private |
Host ethernet address.
Definition at line 269 of file DhcpClient.h.
Referenced by DhcpClient(), initialize(), receiveBootResponse(), and sendBootRequest().
|
private |
UDP socket.
Definition at line 266 of file DhcpClient.h.
Referenced by initialize(), udpReceive(), and udpSend().
|
private |
Transaction ID of the current request.
Definition at line 272 of file DhcpClient.h.
Referenced by exec(), receiveBootResponse(), and sendBootRequest().
|
staticprivate |
Magic number value for the packet header.
Definition at line 50 of file DhcpClient.h.
Referenced by sendBootRequest().
|
staticprivate |
Maximum number of retries to receive an IP address.
Definition at line 47 of file DhcpClient.h.
Referenced by exec().
|
staticprivate |
Timeout in milliseconds to wait for packet receive.
Definition at line 53 of file DhcpClient.h.
Referenced by udpReceive().
|
staticprivate |
1.8.17