[Dnsmasq-discuss] [PATCH 1/1] dhcp protocol: adding long options support (rfc3396)
Peter Kaestle
peter.kaestle at nokia.com
Wed May 15 13:00:43 UTC 2024
When the value length of an option exceeds 255 bytes, split it into a
second option with same option id, following rfc3396.
Signed-off-by: Peter Kaestle <peter.kaestle at nokia.com>
---
src/dhcp-protocol.h | 5 ++++-
src/rfc2131.c | 22 +++++++++++++++++-----
2 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/src/dhcp-protocol.h b/src/dhcp-protocol.h
index 3dde354..faf1b2b 100644
--- a/src/dhcp-protocol.h
+++ b/src/dhcp-protocol.h
@@ -92,11 +92,14 @@
#define DHCP_CHADDR_MAX 16
+#define MAX_OPTION_BUFF_SIZE 3000
+#define MAX_OPTION_SIZE 255
+
struct dhcp_packet {
u8 op, htype, hlen, hops;
u32 xid;
u16 secs, flags;
struct in_addr ciaddr, yiaddr, siaddr, giaddr;
u8 chaddr[DHCP_CHADDR_MAX], sname[64], file[128];
- u8 options[312];
+ u8 options[MAX_OPTION_BUFF_SIZE];
};
diff --git a/src/rfc2131.c b/src/rfc2131.c
index 68834ea..b428c40 100644
--- a/src/rfc2131.c
+++ b/src/rfc2131.c
@@ -1979,13 +1979,25 @@ static void option_put_string(struct dhcp_packet *mess, unsigned char *end, int
const char *string, int null_term)
{
unsigned char *p;
- size_t len = strlen(string);
-
- if (null_term && len != 255)
+ /* using signed int here to detect if length is shorter than MAX_OPTION_SIZE */
+ int len = strlen(string);
+
+ /*
+ * if string is not terminated, add one byte for termination
+ * - don't do that in case data fits exactly in 255 bytes of one option
+ */
+ if (null_term && len % MAX_OPTION_SIZE)
len++;
- if ((p = free_space(mess, end, opt, len)))
- memcpy(p, string, len);
+ /* if option data length is bigger than 255, split it like specified in rfc3396 */
+ for (; len > 0; len -= MAX_OPTION_SIZE, string += MAX_OPTION_SIZE) {
+ size_t l = len >= MAX_OPTION_SIZE ? MAX_OPTION_SIZE : len;
+
+ if ((p = free_space(mess, end, opt, l)))
+ memcpy(p, string, l);
+ else
+ break;
+ }
}
/* return length, note this only does the data part */
--
2.45.0
More information about the Dnsmasq-discuss
mailing list