Skip to content

Commit 3e64351

Browse files
committed
lua: add add_uint8, add_uint16 and add_uint32 functions
Give an example. We should now have enough functions to add pretty much anything easily to a DHCP message from lua.
1 parent f52e54a commit 3e64351

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ local dhcp_opts = {
8484
['DNSSERVER'] = 6,
8585
['HOSTNAME'] = 12,
8686
['DNSDOMAIN'] = 15,
87+
['MTU'] = 26,
8788
['PARAMETERREQUESTLIST'] = 55,
8889
['FQDN'] = 81,
8990
}
@@ -178,6 +179,9 @@ function lookup_addr(hostname, htype, chaddr)
178179
end
179180

180181
-- dhcpsd will call this function to add options to a DHCP reply.
182+
-- The dhcp table adds the following functions:
183+
-- add_ip, add_string, add_uint32, add_uint16 and add_uint8
184+
-- set_bootp_file, set_bootp_sname
181185
-- Return non zero to stop other plugins applying options.
182186
function add_dhcp_options(hostname, htype, chaddr)
183187
if has_parameter_request(dhcp_opts['SUBNETMASK']) then
@@ -190,6 +194,9 @@ function add_dhcp_options(hostname, htype, chaddr)
190194
dhcp.add_ip(dhcp_opts['DNSSERVER'], '10.73.1.1, 10.73.1.2')
191195
end
192196

197+
-- If the subnet needs a specific MTU for PPPoE, etc
198+
-- dhcp.add_uint16(dhcp_opts['MTU'], 1480);
199+
193200
hostname = trim_domain(hostname)
194201
if dhcp.get_option(dhcp_opts['HOSTNAME']) == 'netbsd' then
195202
dhcp.set_bootp_file('/boot-netbsd.img')

src/dhcp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ uint32_t dhcp_atof(const char *);
267267
memcpy(*(p), (v), (l)); \
268268
*(p) = *(p) + (l); \
269269
} while (0 /* CONSTCOND */)
270+
#define DHCP_PUT_U8(p, e, o, v) \
271+
do { \
272+
DHCP_PUT_CHECK((p), (e), sizeof(uint8_t)); \
273+
DHCP_PUT_BIN((p), (e), (o), &(v), sizeof(uint8_t)); \
274+
} while (0 /* CONSTCOND */)
275+
#define DHCP_PUT_U16(p, e, o, v) \
276+
do { \
277+
DHCP_PUT_CHECK((p), (e), sizeof(uint16_t)); \
278+
DHCP_PUT_BIN((p), (e), (o), &(v), sizeof(uint16_t)); \
279+
} while (0 /* CONSTCOND */)
270280
#define DHCP_PUT_U32(p, e, o, v) \
271281
do { \
272282
DHCP_PUT_CHECK((p), (e), sizeof(uint32_t)); \

src/plugins/lua.c

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ lua_add_dhcp_ip(lua_State *L)
457457
char *str;
458458
int err;
459459

460-
if (optn < 1 || optn > 255) {
460+
if (optn < 1 || optn > 254) {
461461
logerrx("%s: option out of range: %lld", lua_name, optn);
462462
return 0;
463463
}
@@ -477,13 +477,68 @@ lua_add_dhcp_ip(lua_State *L)
477477
}
478478

479479
static int
480-
lua_add_dhcp_u32(lua_State *L)
480+
lua_add_dhcp_uint8(lua_State *L)
481481
{
482482
struct lua_ctx *l = &lua_ctx;
483483
long long optn = luaL_checkinteger(L, 1);
484484
long long data = luaL_checkinteger(L, 2);
485+
uint8_t u8;
485486

486-
if (optn < 1 || optn > 255) {
487+
if (optn < 1 || optn > 254) {
488+
logerrx("%s: option out of range: %lld", lua_name, optn);
489+
return 0;
490+
}
491+
if (data < 0 || data > UINT8_MAX) {
492+
logerrx("%s: data out of range: %lld", lua_name, data);
493+
return 0;
494+
}
495+
496+
if (l->l_p == NULL || l->l_e == NULL) {
497+
logerrx("%s: cannot add options", lua_name);
498+
return 0;
499+
}
500+
501+
u8 = (uint8_t)data;
502+
DHCP_PUT_U8(&l->l_p, l->l_e, (uint8_t)optn, u8);
503+
return 0;
504+
}
505+
506+
static int
507+
lua_add_dhcp_uint16(lua_State *L)
508+
{
509+
struct lua_ctx *l = &lua_ctx;
510+
long long optn = luaL_checkinteger(L, 1);
511+
long long data = luaL_checkinteger(L, 2);
512+
uint16_t u16;
513+
514+
if (optn < 1 || optn > 254) {
515+
logerrx("%s: option out of range: %lld", lua_name, optn);
516+
return 0;
517+
}
518+
if (data < 0 || data > UINT16_MAX) {
519+
logerrx("%s: data out of range: %lld", lua_name, data);
520+
return 0;
521+
}
522+
523+
if (l->l_p == NULL || l->l_e == NULL) {
524+
logerrx("%s: cannot add options", lua_name);
525+
return 0;
526+
}
527+
528+
u16 = htons(data);
529+
DHCP_PUT_U16(&l->l_p, l->l_e, (uint8_t)optn, u16);
530+
return 0;
531+
}
532+
533+
static int
534+
lua_add_dhcp_uint32(lua_State *L)
535+
{
536+
struct lua_ctx *l = &lua_ctx;
537+
long long optn = luaL_checkinteger(L, 1);
538+
long long data = luaL_checkinteger(L, 2);
539+
uint32_t u32;
540+
541+
if (optn < 1 || optn > 254) {
487542
logerrx("%s: option out of range: %lld", lua_name, optn);
488543
return 0;
489544
}
@@ -497,7 +552,8 @@ lua_add_dhcp_u32(lua_State *L)
497552
return 0;
498553
}
499554

500-
DHCP_PUT_U32(&l->l_p, l->l_e, (uint8_t)optn, data);
555+
u32 = htonl(data);
556+
DHCP_PUT_U32(&l->l_p, l->l_e, (uint8_t)optn, u32);
501557
return 0;
502558
}
503559

@@ -508,7 +564,7 @@ lua_add_dhcp_string(lua_State *L)
508564
long long optn = luaL_checkinteger(L, 1);
509565
const char *data = luaL_checkstring(L, 2);
510566

511-
if (optn < 1 || optn > 255) {
567+
if (optn < 1 || optn > 254) {
512568
logerrx("%s: option out of range: %lld", lua_name, optn);
513569
return 0;
514570
}
@@ -719,7 +775,9 @@ lua_init(struct plugin *p)
719775
{ "set_bootp_file", lua_set_bootp_file },
720776
{ "get_option", lua_get_dhcp_option },
721777
{ "add_ip", lua_add_dhcp_ip },
722-
{ "add_u32", lua_add_dhcp_u32 },
778+
{ "add_uint8", lua_add_dhcp_uint8 },
779+
{ "add_uint16", lua_add_dhcp_uint16 },
780+
{ "add_uint32", lua_add_dhcp_uint32 },
723781
{ "add_string", lua_add_dhcp_string },
724782
{ NULL, NULL },
725783
};

0 commit comments

Comments
 (0)