FreeNOS
DhcpClient.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __BIN_DHCPC_DHCPCLIENT_H
19 #define __BIN_DHCPC_DHCPCLIENT_H
20 
21 #include <NetworkClient.h>
22 #include <POSIXApplication.h>
23 
37 {
38  private:
39 
41  static const u16 ServerPort = 67;
42 
44  static const u16 ClientPort = 68;
45 
47  static const Size MaximumRetries = 25;
48 
50  static const u32 MagicValue = 0x63825363;
51 
53  static const Size ReceiveTimeoutMs = 500;
54 
58  struct Header
59  {
72  u8 padding[192];
74  };
75 
79  enum Operation
80  {
83  };
84 
89  {
90  Discover = 1,
91  Offer = 2,
92  Request = 3,
93  Decline = 4,
94  Ack = 5,
95  Nak = 6,
96  Release = 7
97  };
98 
102  enum Options
103  {
105  Router = 3,
111  EndMark = 255
112  };
113 
114  public:
115 
122  DhcpClient(int argc, char **argv);
123 
127  virtual ~DhcpClient();
128 
134  virtual Result initialize();
135 
141  virtual Result exec();
142 
143  private:
144 
153  Result setIpAddress(const char *device,
154  const IPV4::Address ipAddr) const;
155 
165  Result discover(const IPV4::Address &ipAddr,
166  const IPV4::Address &ipServer,
167  const IPV4::Address &ipGateway) const;
168 
178  Result offer(IPV4::Address &ipAddr,
179  IPV4::Address &ipServer,
180  IPV4::Address &ipGateway) const;
181 
191  Result request(const IPV4::Address &ipAddr,
192  const IPV4::Address &ipServer,
193  const IPV4::Address &ipGateway) const;
194 
205  IPV4::Address &ipServer,
206  IPV4::Address &ipGateway) const;
207 
219  const IPV4::Address & ipAddr,
220  const IPV4::Address & ipServer,
221  const IPV4::Address & ipGateway) const;
222 
234  IPV4::Address &ipAddr,
235  IPV4::Address &ipServer,
236  IPV4::Address &ipGateway) const;
237 
246  Result udpSend(const void *packet,
247  const Size size) const;
248 
257  Result udpReceive(void *packet,
258  Size & size) const;
259 
260  private:
261 
264 
266  int m_socket;
267 
270 
273 };
274 
279 #endif /* __BIN_DHCPC_DHCPCLIENT_H */
DhcpClient::Router
@ Router
Definition: DhcpClient.h:105
DhcpClient::Decline
@ Decline
Definition: DhcpClient.h:93
DhcpClient::ServerIdentifier
@ ServerIdentifier
Definition: DhcpClient.h:109
DhcpClient::DomainNameServer
@ DomainNameServer
Definition: DhcpClient.h:106
DhcpClient::MagicValue
static const u32 MagicValue
Magic number value for the packet header.
Definition: DhcpClient.h:50
DhcpClient::DhcpClient
DhcpClient(int argc, char **argv)
Class constructor.
Definition: DhcpClient.cpp:33
DhcpClient::DhcpMessageType
@ DhcpMessageType
Definition: DhcpClient.h:108
DhcpClient::Header::seconds
u16 seconds
Definition: DhcpClient.h:65
DhcpClient::Header::transactionId
u32 transactionId
Definition: DhcpClient.h:64
DhcpClient::exec
virtual Result exec()
Execute the application event loop.
Definition: DhcpClient.cpp:106
DhcpClient::Discover
@ Discover
Definition: DhcpClient.h:90
DhcpClient::Header::hopCount
u8 hopCount
Definition: DhcpClient.h:63
DhcpClient::m_transactionId
u32 m_transactionId
Transaction ID of the current request.
Definition: DhcpClient.h:272
DhcpClient::Header::yourAddress
u32 yourAddress
Definition: DhcpClient.h:68
DhcpClient::Header::padding
u8 padding[192]
Definition: DhcpClient.h:72
DhcpClient::Header::serverAddress
u32 serverAddress
Definition: DhcpClient.h:69
DhcpClient::Ack
@ Ack
Definition: DhcpClient.h:94
DhcpClient::Header
Protocol packet header.
Definition: DhcpClient.h:58
DhcpClient::Header::flags
u16 flags
Definition: DhcpClient.h:66
DhcpClient::BootResponse
@ BootResponse
Definition: DhcpClient.h:82
DhcpClient::Nak
@ Nak
Definition: DhcpClient.h:95
DhcpClient::sendBootRequest
Result sendBootRequest(const DhcpClient::MessageType messageType, const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP boot request.
Definition: DhcpClient.cpp:213
DhcpClient::m_socket
int m_socket
UDP socket.
Definition: DhcpClient.h:266
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
DhcpClient::Header::hardwareType
u8 hardwareType
Definition: DhcpClient.h:61
DhcpClient::Release
@ Release
Definition: DhcpClient.h:96
DhcpClient::ParameterRequestList
@ ParameterRequestList
Definition: DhcpClient.h:110
DhcpClient::ClientPort
static const u16 ClientPort
Client UDP port.
Definition: DhcpClient.h:44
DhcpClient::ReceiveTimeoutMs
static const Size ReceiveTimeoutMs
Timeout in milliseconds to wait for packet receive.
Definition: DhcpClient.h:53
DhcpClient::Offer
@ Offer
Definition: DhcpClient.h:91
DhcpClient::offer
Result offer(IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP Offer message.
Definition: DhcpClient.cpp:188
DhcpClient::Header::gatewayAddress
u32 gatewayAddress
Definition: DhcpClient.h:70
DhcpClient::initialize
virtual Result initialize()
Initialize the application.
Definition: DhcpClient.cpp:50
POSIXApplication.h
Ethernet::Address
Ethernet network address.
Definition: Ethernet.h:52
DhcpClient::Header::hardwareLength
u8 hardwareLength
Definition: DhcpClient.h:62
DhcpClient::Options
Options
DHCP options.
Definition: DhcpClient.h:102
DhcpClient::EndMark
@ EndMark
Definition: DhcpClient.h:111
u16
unsigned short u16
Unsigned 16-bit number.
Definition: Types.h:56
DhcpClient
Dynamic Host Configuration Protocol (DHCP) client application.
Definition: DhcpClient.h:36
DhcpClient::BootRequest
@ BootRequest
Definition: DhcpClient.h:81
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
DhcpClient::m_client
NetworkClient * m_client
Network client.
Definition: DhcpClient.h:263
Application::Result
Result
Result codes.
Definition: Application.h:53
DhcpClient::m_etherAddress
Ethernet::Address m_etherAddress
Host ethernet address.
Definition: DhcpClient.h:269
DhcpClient::SubnetMask
@ SubnetMask
Definition: DhcpClient.h:104
DhcpClient::Request
@ Request
Definition: DhcpClient.h:92
DhcpClient::receiveBootResponse
Result receiveBootResponse(const DhcpClient::MessageType messageType, IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP boot response.
Definition: DhcpClient.cpp:272
IPV4::Address
u32 Address
IP-address.
Definition: IPV4.h:47
DhcpClient::Operation
Operation
DHCP operations.
Definition: DhcpClient.h:79
NetworkClient
Networking Client implementation.
Definition: NetworkClient.h:44
DhcpClient::Header::clientHardware
u8 clientHardware[16]
Definition: DhcpClient.h:71
DhcpClient::setIpAddress
Result setIpAddress(const char *device, const IPV4::Address ipAddr) const
Set IP address on a device.
Definition: DhcpClient.cpp:159
DhcpClient::Header::clientAddress
u32 clientAddress
Definition: DhcpClient.h:67
DhcpClient::udpReceive
Result udpReceive(void *packet, Size &size) const
Receive UDP packet.
Definition: DhcpClient.cpp:398
DhcpClient::request
Result request(const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP Request message.
Definition: DhcpClient.cpp:197
DhcpClient::Header::magic
u32 magic
Definition: DhcpClient.h:73
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
DhcpClient::RequestedIP
@ RequestedIP
Definition: DhcpClient.h:107
DhcpClient::MessageType
MessageType
DHCP message types.
Definition: DhcpClient.h:88
DhcpClient::MaximumRetries
static const Size MaximumRetries
Maximum number of retries to receive an IP address.
Definition: DhcpClient.h:47
DhcpClient::acknowledge
Result acknowledge(IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP Acknowledge message.
Definition: DhcpClient.cpp:205
DhcpClient::Header::operation
u8 operation
Definition: DhcpClient.h:60
NetworkClient.h
DhcpClient::ServerPort
static const u16 ServerPort
Server UDP port.
Definition: DhcpClient.h:41
DhcpClient::~DhcpClient
virtual ~DhcpClient()
Class destructor.
Definition: DhcpClient.cpp:45
DhcpClient::udpSend
Result udpSend(const void *packet, const Size size) const
Send UDP broadcast packet.
Definition: DhcpClient.cpp:376
DhcpClient::discover
Result discover(const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP Discover message.
Definition: DhcpClient.cpp:180