Skip to content

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions cores/esp8266/LwipDhcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

#include "user_interface.h"
#include "mem.h"
#include "Arduino.h"

typedef struct dhcps_state
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using int8 or int16 generates more code than int 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 than char*

A comment may mention that the first bytes indicates the option length.

{
Serial.print("OfferCode: ");
Serial.println(String(offerCode)+offerContent);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, debug code must be optional with #if DHCPS_DEBUG barriers and be using os_printf(),
like in other places in this file.

int sizeOfCustomOptions = strlen(dhcpCustomOffers);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is misleading: sizeof generally means a constant which is the size of a type.
This name may be changed to lengthOfCustomOptions.

if (sizeOfCustomOptions + strlen(offerContent) + 1 < 100){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constants are generally forbidden in any code.
In this case 100 must be replaced by sizeof(dhcpCustomOffers).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strlen(offerContent) seems to be invalid, since this is not a regular C-string.
Should it be offerContent[0] instead ?
(= length of the DHCP option)

dhcpCustomOffers[sizeOfCustomOptions] = offerCode;
dhcpCustomOffers[sizeOfCustomOptions +1] = strlen(offerContent);
for(int i = 0; i<(strlen(offerContent)); i++){
dhcpCustomOffers[sizeOfCustomOptions + 2 + i] = offerContent[i];
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may simply want to use strcpy_P(&dhcpCustomOffers[sizeOfCustomOptions + 1], offerContent); ?
(using _P so user input can be in flash with PROGMEM / PSTR)

} else{
return 0;
}
return strlen(dhcpCustomOffers);
}

void DhcpServer::remove_dhcps_custom_options()
{
for(uint16 i = 0; i < 100; i++){
dhcpCustomOffers[i] = '\0';
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memset(dhcpCustomOffers, 0, sizeof(dhcpCustomOffers)) which is libc-optimized ?

}

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))){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casting to size_t is more efficient (casting to 16 or 8 bits produces larger code).

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];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memcpy(optptr, &dhcpCustomOffers[i+1], dhcpCustomOffers[i+1] + 1) ?

Serial.println(dhcpCustomOffers[i+y]);
}
}
else{
return optptr;
}
i += uint16(dhcpCustomOffers[i+1]) +2;
}
return optptr;
}
5 changes: 5 additions & 0 deletions cores/esp8266/LwipDhcpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class DhcpServer
bool reset_dhcps_lease_time(void);
uint32 get_dhcps_lease_time(void);
bool add_dhcps_lease(uint8* macaddr);
uint16 add_dhcps_custom_options(uint8 offerCode, char *offerContent);
void remove_dhcps_custom_options(void);
char dhcpCustomOffers[100];


void dhcps_set_dns(int num, const ipv4_addr_t* dns);

Expand All @@ -76,6 +80,7 @@ class DhcpServer
void node_remove_from_list(list_node** phead, list_node* pdelete);
uint8_t* add_msg_type(uint8_t* optptr, uint8_t type);
uint8_t* add_offer_options(uint8_t* optptr);
uint8_t* insert_custom_offer_options(uint8_t* optptr, uint8_t* optionsStart);
uint8_t* add_end(uint8_t* optptr);
void create_msg(struct dhcps_msg* m);
void send_offer(struct dhcps_msg* m);
Expand Down