-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Custom dhcp options #8571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom dhcp options #8571
Changes from 2 commits
de32824
01ef879
647d0cd
287c78a
54d56d9
f9778c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ | |
|
||
#include "user_interface.h" | ||
#include "mem.h" | ||
#include "Arduino.h" | ||
|
||
typedef struct dhcps_state | ||
{ | ||
|
@@ -536,6 +537,7 @@ void DhcpServer::send_offer(struct dhcps_msg* m) | |
|
||
end = add_msg_type(&m->options[4], DHCPOFFER); | ||
end = add_offer_options(end); | ||
end = insert_custom_offer_options(end, &m->options[0]); | ||
end = add_end(end); | ||
|
||
p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); | ||
|
@@ -658,6 +660,7 @@ void DhcpServer::send_ack(struct dhcps_msg* m) | |
|
||
end = add_msg_type(&m->options[4], DHCPACK); | ||
end = add_offer_options(end); | ||
end = insert_custom_offer_options(end, &m->options[0]); | ||
end = add_end(end); | ||
|
||
p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); | ||
|
@@ -1591,3 +1594,51 @@ uint32 DhcpServer::dhcps_client_update(u8* bssid, struct ipv4_addr* ip) | |
|
||
return pdhcps_pool->ip.addr; | ||
} | ||
|
||
uint16 DhcpServer::add_dhcps_custom_options(uint8 offerCode, char *offerContent) | ||
{ | ||
Serial.print("OfferCode: "); | ||
Serial.println(String(offerCode)+offerContent); | ||
|
||
int sizeOfCustomOptions = strlen(dhcpCustomOffers); | ||
|
||
if (sizeOfCustomOptions + strlen(offerContent) + 1 < 100){ | ||
|
||
dhcpCustomOffers[sizeOfCustomOptions] = offerCode; | ||
dhcpCustomOffers[sizeOfCustomOptions +1] = strlen(offerContent); | ||
for(int i = 0; i<(strlen(offerContent)); i++){ | ||
dhcpCustomOffers[sizeOfCustomOptions + 2 + i] = offerContent[i]; | ||
} | ||
|
||
} else{ | ||
return 0; | ||
} | ||
return strlen(dhcpCustomOffers); | ||
} | ||
|
||
void DhcpServer::remove_dhcps_custom_options() | ||
{ | ||
for(uint16 i = 0; i < 100; i++){ | ||
dhcpCustomOffers[i] = '\0'; | ||
} | ||
|
||
} | ||
|
||
uint8_t* DhcpServer::insert_custom_offer_options(uint8_t* optptr, uint8_t* optionsStart) | ||
{ | ||
Serial.println("Adding Options"); | ||
Serial.println(dhcpCustomOffers); | ||
int sizeOfCustomOptions = strlen(dhcpCustomOffers); | ||
Serial.println(sizeOfCustomOptions); | ||
uint16 i = 0; | ||
while (i < sizeOfCustomOptions){ | ||
if((uint16(dhcpCustomOffers[i+1]) +1) < (uint16(312) - uint16(optptr - optionsStart))){ | ||
|
||
Serial.println("DHCP: Made it into IF:"); | ||
Serial.println((uint16(312) - uint16(optptr - optionsStart))); | ||
for(int y = 0; y < uint16(dhcpCustomOffers[i+1]) + 2; y++){ | ||
*optptr++ = dhcpCustomOffers[i+y]; | ||
|
||
Serial.println(dhcpCustomOffers[i+y]); | ||
} | ||
} | ||
else{ | ||
return optptr; | ||
} | ||
i += uint16(dhcpCustomOffers[i+1]) +2; | ||
} | ||
return optptr; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using
int8
orint16
generates more code thanint
on esp8266.if 16 is important but not critical, we have
int_fast16_t
.https://en.cppreference.com/w/c/types/integer
const char*
is always preferred thanchar*
A comment may mention that the first bytes indicates the option length.