Allow user to set custom DHCP name

This commit is contained in:
Sergey Lyubka committed 2025-03-22 16:30:02 +00:00
1 parent 042ea53478
commit 064fc6ff7b
4 files changed
+54 -36

No files matched your search

+24 -18
View File
@@ -4202,7 +4202,7 @@ struct dhcp {
uint32_t ciaddr, yiaddr, siaddr, giaddr; uint32_t ciaddr, yiaddr, siaddr, giaddr;
uint8_t hwaddr[208]; uint8_t hwaddr[208];
uint32_t magic; uint32_t magic;
uint8_t options[32]; uint8_t options[30 + sizeof(((struct mg_tcpip_if *) 0)->dhcp_name)];
}; };
#pragma pack(pop) #pragma pack(pop)
@@ -4361,22 +4361,21 @@ static const uint8_t broadcast[] = {255, 255, 255, 255, 255, 255};
// RFC-2131 #4.3.6, #4.4.1; RFC-2132 #9.8 // RFC-2131 #4.3.6, #4.4.1; RFC-2132 #9.8
static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req, static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req,
uint32_t ip_srv) { uint32_t ip_srv) {
uint8_t opts[] = { uint8_t extra = (ifp->enable_req_dns ? 1 : 0) + (ifp->enable_req_sntp ? 1 : 0);
53, 1, 3, // Type: DHCP request size_t len = strlen(ifp->dhcp_name);
12, 3, 'm', 'i', 'p', // Host name: "mip" size_t olen = 21 + len + extra + 2 + 1; // Total length of options
54, 4, 0, 0, 0, 0, // DHCP server ID uint8_t *opts = alloca(olen), *p = opts; // Allocate options
50, 4, 0, 0, 0, 0, // Requested IP *p++ = 53, *p++ = 1, *p++ = 3; // Type: DHCP request
55, 2, 1, 3, 255, 255, // GW, mask [DNS] [SNTP] *p++ = 54, *p++ = 4, memcpy(p, &ip_srv, 4), p += 4; // DHCP server ID
255 // End of options *p++ = 50, *p++ = 4, memcpy(p, &ip_req, 4), p += 4; // Requested IP
}; *p++ = 12, *p++ = (uint8_t) (len & 255); // DHCP host
uint8_t addopts = 0; memcpy(p, ifp->dhcp_name, len), p += len; // name
memcpy(opts + 10, &ip_srv, sizeof(ip_srv)); *p++ = 55, *p++ = 2 + extra, *p++ = 1, *p++ = 3; // GW, MASK
memcpy(opts + 16, &ip_req, sizeof(ip_req)); if (ifp->enable_req_dns) *p++ = 6; // DNS
if (ifp->enable_req_dns) opts[24 + addopts++] = 6; // DNS if (ifp->enable_req_sntp) *p++ = 42; // SNTP
if (ifp->enable_req_sntp) opts[24 + addopts++] = 42; // SNTP *p++ = 255; // End of options
opts[21] += addopts; assert((size_t) (p - opts) < olen);
tx_dhcp(ifp, (uint8_t *) broadcast, 0, 0xffffffff, opts, tx_dhcp(ifp, (uint8_t *) broadcast, 0, 0xffffffff, opts, olen, 0);
sizeof(opts) + addopts - 2, false);
MG_DEBUG(("DHCP req sent")); MG_DEBUG(("DHCP req sent"));
} }
@@ -4732,7 +4731,8 @@ static void handle_tls_recv(struct mg_connection *c) {
mg_error(c, "oom"); mg_error(c, "oom");
} else { } else {
// Decrypt data directly into c->recv // Decrypt data directly into c->recv
long n = mg_tls_recv(c, io->buf != NULL ? &io->buf[io->len] : io->buf, io->size - io->len); long n = mg_tls_recv(c, io->buf != NULL ? &io->buf[io->len] : io->buf,
io->size - io->len);
if (n == MG_IO_ERR) { if (n == MG_IO_ERR) {
mg_error(c, "TLS recv error"); mg_error(c, "TLS recv error");
} else if (n > 0) { } else if (n > 0) {
@@ -5113,6 +5113,12 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) {
MG_INFO(("MAC not set. Generated random: %M", mg_print_mac, ifp->mac)); MG_INFO(("MAC not set. Generated random: %M", mg_print_mac, ifp->mac));
} }
// Uf DHCP name is not set, use "mip"
if (ifp->dhcp_name[0] == '\0') {
memcpy(ifp->dhcp_name, "mip", 4);
}
ifp->dhcp_name[sizeof(ifp->dhcp_name) - 1] = '\0'; // Just in case
if (ifp->driver->init && !ifp->driver->init(ifp)) { if (ifp->driver->init && !ifp->driver->init(ifp)) {
MG_ERROR(("driver init failed")); MG_ERROR(("driver init failed"));
} else { } else {
+5
View File
@@ -2751,6 +2751,10 @@ bool mg_ota_flash_end(struct mg_flash *flash);
#endif #endif
struct mg_wifi_scan_bss_data { struct mg_wifi_scan_bss_data {
struct mg_str SSID; struct mg_str SSID;
char *BSSID; char *BSSID;
@@ -2823,6 +2827,7 @@ struct mg_tcpip_if {
mg_tcpip_event_handler_t fn; // User-specified event handler function mg_tcpip_event_handler_t fn; // User-specified event handler function
struct mg_mgr *mgr; // Mongoose event manager struct mg_mgr *mgr; // Mongoose event manager
struct mg_queue recv_queue; // Receive queue struct mg_queue recv_queue; // Receive queue
char dhcp_name[12]; // Name reported to DHCP, "mip" if unset
uint16_t mtu; // Interface MTU uint16_t mtu; // Interface MTU
#define MG_TCPIP_MTU_DEFAULT 1500 #define MG_TCPIP_MTU_DEFAULT 1500
+24 -18
View File
@@ -121,7 +121,7 @@ struct dhcp {
uint32_t ciaddr, yiaddr, siaddr, giaddr; uint32_t ciaddr, yiaddr, siaddr, giaddr;
uint8_t hwaddr[208]; uint8_t hwaddr[208];
uint32_t magic; uint32_t magic;
uint8_t options[32]; uint8_t options[30 + sizeof(((struct mg_tcpip_if *) 0)->dhcp_name)];
}; };
#pragma pack(pop) #pragma pack(pop)
@@ -280,22 +280,21 @@ static const uint8_t broadcast[] = {255, 255, 255, 255, 255, 255};
// RFC-2131 #4.3.6, #4.4.1; RFC-2132 #9.8 // RFC-2131 #4.3.6, #4.4.1; RFC-2132 #9.8
static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req, static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req,
uint32_t ip_srv) { uint32_t ip_srv) {
uint8_t opts[] = { uint8_t extra = (ifp->enable_req_dns ? 1 : 0) + (ifp->enable_req_sntp ? 1 : 0);
53, 1, 3, // Type: DHCP request size_t len = strlen(ifp->dhcp_name);
12, 3, 'm', 'i', 'p', // Host name: "mip" size_t olen = 21 + len + extra + 2 + 1; // Total length of options
54, 4, 0, 0, 0, 0, // DHCP server ID uint8_t *opts = alloca(olen), *p = opts; // Allocate options
50, 4, 0, 0, 0, 0, // Requested IP *p++ = 53, *p++ = 1, *p++ = 3; // Type: DHCP request
55, 2, 1, 3, 255, 255, // GW, mask [DNS] [SNTP] *p++ = 54, *p++ = 4, memcpy(p, &ip_srv, 4), p += 4; // DHCP server ID
255 // End of options *p++ = 50, *p++ = 4, memcpy(p, &ip_req, 4), p += 4; // Requested IP
}; *p++ = 12, *p++ = (uint8_t) (len & 255); // DHCP host
uint8_t addopts = 0; memcpy(p, ifp->dhcp_name, len), p += len; // name
memcpy(opts + 10, &ip_srv, sizeof(ip_srv)); *p++ = 55, *p++ = 2 + extra, *p++ = 1, *p++ = 3; // GW, MASK
memcpy(opts + 16, &ip_req, sizeof(ip_req)); if (ifp->enable_req_dns) *p++ = 6; // DNS
if (ifp->enable_req_dns) opts[24 + addopts++] = 6; // DNS if (ifp->enable_req_sntp) *p++ = 42; // SNTP
if (ifp->enable_req_sntp) opts[24 + addopts++] = 42; // SNTP *p++ = 255; // End of options
opts[21] += addopts; // assert((size_t) (p - opts) < olen);
tx_dhcp(ifp, (uint8_t *) broadcast, 0, 0xffffffff, opts, tx_dhcp(ifp, (uint8_t *) broadcast, 0, 0xffffffff, opts, olen, 0);
sizeof(opts) + addopts - 2, false);
MG_DEBUG(("DHCP req sent")); MG_DEBUG(("DHCP req sent"));
} }
@@ -651,7 +650,8 @@ static void handle_tls_recv(struct mg_connection *c) {
mg_error(c, "oom"); mg_error(c, "oom");
} else { } else {
// Decrypt data directly into c->recv // Decrypt data directly into c->recv
long n = mg_tls_recv(c, io->buf != NULL ? &io->buf[io->len] : io->buf, io->size - io->len); long n = mg_tls_recv(c, io->buf != NULL ? &io->buf[io->len] : io->buf,
io->size - io->len);
if (n == MG_IO_ERR) { if (n == MG_IO_ERR) {
mg_error(c, "TLS recv error"); mg_error(c, "TLS recv error");
} else if (n > 0) { } else if (n > 0) {
@@ -1032,6 +1032,12 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) {
MG_INFO(("MAC not set. Generated random: %M", mg_print_mac, ifp->mac)); MG_INFO(("MAC not set. Generated random: %M", mg_print_mac, ifp->mac));
} }
// Uf DHCP name is not set, use "mip"
if (ifp->dhcp_name[0] == '\0') {
memcpy(ifp->dhcp_name, "mip", 4);
}
ifp->dhcp_name[sizeof(ifp->dhcp_name) - 1] = '\0'; // Just in case
if (ifp->driver->init && !ifp->driver->init(ifp)) { if (ifp->driver->init && !ifp->driver->init(ifp)) {
MG_ERROR(("driver init failed")); MG_ERROR(("driver init failed"));
} else { } else {
+1
View File
@@ -47,6 +47,7 @@ struct mg_tcpip_if {
mg_tcpip_event_handler_t fn; // User-specified event handler function mg_tcpip_event_handler_t fn; // User-specified event handler function
struct mg_mgr *mgr; // Mongoose event manager struct mg_mgr *mgr; // Mongoose event manager
struct mg_queue recv_queue; // Receive queue struct mg_queue recv_queue; // Receive queue
char dhcp_name[12]; // Name reported to DHCP, "mip" if unset
uint16_t mtu; // Interface MTU uint16_t mtu; // Interface MTU
#define MG_TCPIP_MTU_DEFAULT 1500 #define MG_TCPIP_MTU_DEFAULT 1500