Go to the documentation of this file.
18 #pragma clang optimize off
19 #pragma GCC push_options
20 #pragma GCC optimize ("O0")
59 ethFilePath <<
"/network/" << device <<
"/ethernet/address";
65 ERROR(
"failed to read ethernet device address of " <<
66 device <<
": result = " << (
int) readResult);
79 ERROR(
"failed to initialize network client for device "
80 << device <<
": result = " << (
int) result);
88 ERROR(
"failed to create UDP socket on device " << device <<
89 ": result = " << (
int) result);
98 " on device " << device <<
": result = " << (
int) result);
116 DEBUG(
"device = " <<
arguments().get(
"DEVICE") <<
" attempt = " << (i + 1));
119 ipAddr = ipServer = ipGateway = 0;
121 result =
discover(ipAddr, ipServer, ipGateway);
124 ERROR(
"failed to send discover: result = " << (
int) result);
128 result =
offer(ipAddr, ipServer, ipGateway);
131 ERROR(
"failed to receive offer: result = " << (
int) result);
135 result =
request(ipAddr, ipServer, ipGateway);
138 ERROR(
"failed to send request: result = " << (
int) result);
145 ERROR(
"failed to receive acknowledge: result = " << (
int) result);
166 ipFilePath <<
"/network/" << device <<
"/ipv4/address";
172 ERROR(
"failed to set IPV4 address for device " <<
173 device <<
": result = " << (
int) writeResult);
221 DEBUG(
"messageType = " << (
int) messageType <<
236 u8 *opt = (
u8 *) (hdr + 1);
239 *opt++ = messageType;
257 if (ipServer !=
ZERO)
269 return udpSend(&pkt, opt - &pkt[0]);
278 Size size =
sizeof(pkt);
286 ERROR(
"failed to receive UDP packet: result = " << (
int) result);
317 for (
u8 *option = (
u8 *) (hdr + 1); option < &pkt[size - 2]; )
320 const Size optionLength = *option++;
322 if (option + optionLength > &pkt[size])
343 ERROR(
"invalid message type: " << (
int) (*option) <<
" != " << (
int) messageType);
358 DEBUG(
"ignored unsupported option " << (
int) optionValue);
362 option += optionLength;
368 DEBUG(
"messageType = " << (
int) messageType <<
377 const Size size)
const
379 DEBUG(
"size = " << size);
383 addr.addr = 0xffffffff;
409 ERROR(
"failed to wait for UDP socket " <<
m_socket <<
": result = " << (
int) result);
424 " at port " <<
addr.port);
const char * get(const char *name) const
Get argument by name.
static const u32 MagicValue
Magic number value for the packet header.
DhcpClient(int argc, char **argv)
Class constructor.
Provides a buffered abstract interface to a file.
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
const u32 readBe32(const void *data)
Read 32-bit big endian integer.
virtual Result exec()
Execute the application event loop.
Result initialize()
Perform initialization.
Result createSocket(const SocketType type, int *socket)
Create new socket.
C int errno
The lvalue errno is used by many functions to return error values.
u32 m_transactionId
Transaction ID of the current request.
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Result sendBootRequest(const DhcpClient::MessageType messageType, const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP boot request.
POSIX-compatible application.
static bool compare(const void *p1, const void *p2, const Size count)
Compare memory.
const ArgumentContainer & arguments() const
Get program arguments.
void setDescription(const String &desc)
Set program description.
static const u16 ClientPort
Client UDP port.
Result write(const void *data, const Size size) const
Write the file (unbuffered)
static const Size ReceiveTimeoutMs
Timeout in milliseconds to wait for packet receive.
Defines a socket address and port pair.
const void * buffer() const
Get file buffer.
Result offer(IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP Offer message.
C int recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t addrlen)
Receive a single datagram from a socket.
Result read()
Read the file (buffered)
virtual Result initialize()
Initialize the application.
#define DEBUG(msg)
Output a debug message to standard output.
Ethernet network address.
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
unsigned int Size
Any sane size indicator cannot go negative.
C int sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen)
Send a single datagram to a remote host.
NetworkClient * m_client
Network client.
Ethernet::Address m_etherAddress
Host ethernet address.
Result receiveBootResponse(const DhcpClient::MessageType messageType, IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP boot response.
Networking Client implementation.
Result setIpAddress(const char *device, const IPV4::Address ipAddr) const
Set IP address on a device.
Result udpReceive(void *packet, Size &size) const
Receive UDP packet.
Result bindSocket(const int sock, const IPV4::Address addr=0, const u16 port=0)
Bind socket to address/port.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Result request(const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP Request message.
static const String toString(const Address address)
Convert address to string.
ArgumentParser & parser()
Get program arguments parser.
void writeBe32(void *data, const u32 input)
Write 32-bit big endian integer.
#define ERROR(msg)
Output an error message.
unsigned char u8
Unsigned 8-bit number.
MessageType
DHCP message types.
static const Size MaximumRetries
Maximum number of retries to receive an IP address.
Result acknowledge(IPV4::Address &ipAddr, IPV4::Address &ipServer, IPV4::Address &ipGateway) const
Receive DHCP Acknowledge message.
static const u16 ServerPort
Server UDP port.
Result waitSocket(const NetworkClient::SocketType type, const int sock, const Size msecTimeout)
Wait until the given socket has data to receive.
virtual ~DhcpClient()
Class destructor.
Result udpSend(const void *packet, const Size size) const
Send UDP broadcast packet.
Result discover(const IPV4::Address &ipAddr, const IPV4::Address &ipServer, const IPV4::Address &ipGateway) const
Send DHCP Discover message.