feat: set request options such as the hostname

This commit is contained in:
s3lph 2024-12-28 15:36:45 +01:00
parent 859febf4a8
commit 55fe63f12c
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
5 changed files with 45 additions and 2 deletions

View file

@ -11,6 +11,8 @@ SoftDhcp::SoftDhcp() {
this->leaseHandler = nullptr;
this->offerHandler = nullptr;
this->errorHandler = nullptr;
this->reqoptptr = this->reqopt;
this->reqopt[0] = DhcpOption::END;
}
void SoftDhcp::initializePacket() {
@ -41,6 +43,31 @@ bool SoftDhcp::addOption(uint8_t type, uint8_t value) {
return this->addOption(type, 1, &value);
}
bool SoftDhcp::appendRequestOptions() {
uint8_t *ro = this->reqopt;
while (ro < this->reqoptptr) {
uint8_t rotype = *(ro++);
uint8_t rolen = *(ro++);
if (!this->addOption(rotype, rolen, ro)) {
return false;
}
ro += rolen;
}
return true;
}
bool SoftDhcp::addRequestOption(uint8_t type, uint8_t len, uint8_t *value) {
if (this->reqoptptr + len + 3 >= this->reqopt + 308) { // 3 = type+len+end
return false;
}
*(this->reqoptptr++) = type;
*(this->reqoptptr++) = len;
memcpy(this->reqoptptr, value, len);
this->reqoptptr += len;
*this->reqoptptr = DhcpOption::END;
return true;
}
uint8_t *SoftDhcp::getOption(uint8_t type) {
uint8_t *optptr = this->packet.options;
while (optptr < this->packet.options + 305 && *optptr != DhcpOption::END && *optptr != DhcpOption::PAD) {
@ -54,6 +81,14 @@ uint8_t *SoftDhcp::getOption(uint8_t type) {
return nullptr;
}
void SoftDhcp::hostname(const char *name) {
this->addRequestOption(DhcpOption::HOSTNAME, strlen(name), (uint8_t *) name);
}
void SoftDhcp::hostname(String& name) {
this->addRequestOption(DhcpOption::HOSTNAME, name.length(), (uint8_t *) name.c_str());
}
ssize_t SoftDhcp::getOption(uint8_t type, void *buf, size_t len, ssize_t item) {
uint8_t *optptr = this->packet.options;
while (optptr < this->packet.options + 305 && *optptr != DhcpOption::END && *optptr != DhcpOption::PAD) {
@ -103,6 +138,7 @@ void SoftDhcp::request() {
WiFi.macAddress(macbuf+1);
this->addOption(DhcpOption::CLIENT_ID, 7, macbuf);
this->addOption(DhcpOption::PARAMETER_LIST, this->prl_count, this->prl);
this->appendRequestOptions();
this->udp.beginPacket(this->serverIP, PORT_BOOTPS);
this->udp.write((uint8_t *) &this->packet, sizeof(DhcpPacket));
this->udp.endPacket();
@ -125,6 +161,7 @@ void SoftDhcp::request() {
this->addOption(DhcpOption::PARAMETER_LIST, this->prl_count, this->prl);
this->addOption(DhcpOption::SERVER_ID, 4, this->sid.octets);
this->addOption(DhcpOption::ADDRESS_REQUEST, 4, this->yiaddr.octets);
this->appendRequestOptions();
this->udp.beginPacket(this->serverIP, PORT_BOOTPS);
this->udp.write((uint8_t *) &this->packet, sizeof(DhcpPacket));
this->udp.endPacket();

View file

@ -41,6 +41,8 @@ class SoftDhcp {
uint8_t *optptr;
uint8_t prl[64];
uint8_t prl_count;
uint8_t reqopt[308];
uint8_t *reqoptptr;
IPAddress clientIP;
IPAddress serverIP;
IPAddress routerIP;
@ -69,6 +71,7 @@ class SoftDhcp {
void clearOptions();
bool addOption(uint8_t type, uint8_t len, uint8_t *value);
bool addOption(uint8_t type, uint8_t value);
bool appendRequestOptions();
uint8_t *getOption(uint8_t type);
int32_t getTimeoutOption(uint8_t option, int32_t def);
@ -95,6 +98,8 @@ class SoftDhcp {
ssize_t getOption(uint8_t type, void *buf, size_t len, ssize_t item);
void requestOption(uint8_t type);
bool addRequestOption(uint8_t type, uint8_t len, uint8_t *value);
void hostname(const char *name);
void hostname(String& name);
};

View file

@ -17,6 +17,7 @@ enum DhcpOption: uint8_t {
SUBNET_MASK = 1,
ROUTER = 3,
DOMAIN_SERVER = 6,
HOSTNAME = 12,
ADDRESS_REQUEST = 50,
ADDRESS_TIME = 51,
MSG_TYPE = 53,

View file

@ -6,7 +6,7 @@
"type": "git",
"url": "https://git.kabelsalat.ch/s3lph/Arduino-Library-SoftDhcp.git"
},
"version": "0.0.1",
"version": "0.0.2",
"frameworks": "arduino",
"platforms": "*"
}

View file

@ -1,5 +1,5 @@
name=SoftDhcp
version=0.0.1
version=0.0.2
author=s3lph
maintainer=s3lph <s3lph@kabelsalat.ch>
sentence=Software DHCP client implementation with support for additional options.