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
75
80 {
82 BootResponse = 2
83 };
84
89 {
91 Offer = 2,
94 Ack = 5,
95 Nak = 6,
96 Release = 7
97 };
98
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
267
270
273};
274
279#endif /* __BIN_DHCPC_DHCPCLIENT_H */
Result
Result codes.
Definition Application.h:54
Dynamic Host Configuration Protocol (DHCP) client application.
Definition DhcpClient.h:37
Result receiveBootResponse(const DhcpClient::MessageType messageType, IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP boot response.
static const u16 ServerPort
Server UDP port.
Definition DhcpClient.h:41
u32 m_transactionId
Transaction ID of the current request.
Definition DhcpClient.h:272
Result acknowledge(IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP Acknowledge message.
static const u16 ClientPort
Client UDP port.
Definition DhcpClient.h:44
static const u32 MagicValue
Magic number value for the packet header.
Definition DhcpClient.h:50
Result udpSend(const void *packet, const Size size) const
Send UDP broadcast packet.
virtual ~DhcpClient()
Class destructor.
Ethernet::Address m_etherAddress
Host ethernet address.
Definition DhcpClient.h:269
int m_socket
UDP socket.
Definition DhcpClient.h:266
Result udpReceive(void *packet, Size &size) const
Receive UDP packet.
MessageType
DHCP message types.
Definition DhcpClient.h:89
static const Size MaximumRetries
Maximum number of retries to receive an IP address.
Definition DhcpClient.h:47
Result offer(IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP Offer message.
static const Size ReceiveTimeoutMs
Timeout in milliseconds to wait for packet receive.
Definition DhcpClient.h:53
Result request(const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP Request message.
Result setIpAddress(const char *device, const IPV4::Address ipAddr) const
Set IP address on a device.
Result sendBootRequest(const DhcpClient::MessageType messageType, const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP boot request.
Operation
DHCP operations.
Definition DhcpClient.h:80
Result discover(const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP Discover message.
NetworkClient * m_client
Network client.
Definition DhcpClient.h:263
virtual Result exec()
Execute the application event loop.
virtual Result initialize()
Initialize the application.
Options
DHCP options.
Definition DhcpClient.h:103
@ ParameterRequestList
Definition DhcpClient.h:110
u32 Address
IP-address.
Definition IPV4.h:47
Networking Client implementation.
POSIX-compatible application.
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
unsigned short u16
Unsigned 16-bit number.
Definition Types.h:56
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
Protocol packet header.
Definition DhcpClient.h:59
Ethernet network address.
Definition Ethernet.h:53