Skip to content

Commit 2ceeb23

Browse files
committed
lua: add add_domain function to encode a string using RFC 1035
1 parent 254e0b1 commit 2ceeb23

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ local dhcp_opts = {
8787
['MTU'] = 26,
8888
['PARAMETERREQUESTLIST'] = 55,
8989
['FQDN'] = 81,
90+
['DOMAINSEARCH'] = 119,
9091
}
9192

9293
-- Checks if the client requested the DHCP option or not.
@@ -181,6 +182,7 @@ end
181182
-- dhcpsd will call this function to add options to a DHCP reply.
182183
-- The dhcp table adds the following functions:
183184
-- add_ip, add_string, add_uint32, add_uint16 and add_uint8
185+
-- add_domain
184186
-- set_bootp_file, set_bootp_sname
185187
-- Return non zero to stop other plugins applying options.
186188
function add_dhcp_options(hostname, htype, chaddr)
@@ -193,6 +195,12 @@ function add_dhcp_options(hostname, htype, chaddr)
193195
if has_parameter_request(dhcp_opts['DNSSERVER']) then
194196
dhcp.add_ip(dhcp_opts['DNSSERVER'], '10.73.1.1, 10.73.1.2')
195197
end
198+
if has_parameter_request(dhcp_opts['DNSDOMAIN']) then
199+
dhcp.add_string(dhcp_opts['DNSDOMAIN'], domain)
200+
end
201+
if has_parameter_request(dhcp_opts['DOMAINSEARCH']) then
202+
dhcp.add_domain(dhcp_opts['DOMAINSEARCH'], domain)
203+
end
196204

197205
-- If the subnet needs a specific MTU for PPPoE, etc
198206
-- dhcp.add_uint16(dhcp_opts['MTU'], 1480);

src/dhcp.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,17 @@ uint32_t dhcp_atof(const char *);
257257
**(p) = (v); \
258258
*(p) = *(p) + 1; \
259259
} while (0 /* CONSTCOND */)
260-
#define DHCP_PUT_BIN(p, e, o, v, l) \
261-
do { \
262-
DHCP_PUT_CHECK((p), (e), (l)); \
263-
**(p) = (o); \
264-
*(p) = *(p) + 1; \
265-
**(p) = (uint8_t)(l); \
266-
*(p) = *(p) + 1; \
267-
memcpy(*(p), (v), (l)); \
268-
*(p) = *(p) + (l); \
260+
#define DHCP_PUT_BIN(p, e, o, v, l) \
261+
do { \
262+
DHCP_PUT_CHECK((p), (e), (l)); \
263+
**(p) = (o); \
264+
*(p) = *(p) + 1; \
265+
**(p) = (uint8_t)(l); \
266+
*(p) = *(p) + 1; \
267+
if ((l) != 0) { \
268+
memcpy(*(p), (v), (l)); \
269+
*(p) = *(p) + (l); \
270+
} \
269271
} while (0 /* CONSTCOND */)
270272
#define DHCP_PUT_U8(p, e, o, v) \
271273
do { \

src/plugins/lua.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,39 @@ lua_add_dhcp_string(lua_State *L)
578578
return 0;
579579
}
580580

581+
static int
582+
lua_add_dhcp_domain(lua_State *L)
583+
{
584+
struct lua_ctx *l = &lua_ctx;
585+
long long optn = luaL_checkinteger(L, 1);
586+
const char *data = luaL_checkstring(L, 2);
587+
uint8_t *val;
588+
size_t len;
589+
590+
if (optn < 1 || optn > 254) {
591+
logerrx("%s: option out of range: %lld", lua_name, optn);
592+
return 0;
593+
}
594+
595+
if (l->l_p == NULL || l->l_e == NULL) {
596+
logerrx("%s: cannot add options", lua_name);
597+
return 0;
598+
}
599+
600+
len = encode_rfc1035(data, NULL);
601+
if (len == 0)
602+
val = NULL;
603+
else {
604+
val = malloc(len);
605+
if (val == NULL)
606+
return -1;
607+
len = encode_rfc1035(data, val);
608+
}
609+
610+
DHCP_PUT_BIN(&l->l_p, l->l_e, (uint8_t)optn, val, len);
611+
return 0;
612+
}
613+
581614
static ssize_t
582615
lua_run_add_dhcp_options(struct plugin *p, struct svc_ctx *sctx,
583616
const void *dhcp, size_t dhcplen)
@@ -779,6 +812,7 @@ lua_init(struct plugin *p)
779812
{ "add_uint16", lua_add_dhcp_uint16 },
780813
{ "add_uint32", lua_add_dhcp_uint32 },
781814
{ "add_string", lua_add_dhcp_string },
815+
{ "add_domain", lua_add_dhcp_domain },
782816
{ NULL, NULL },
783817
};
784818

0 commit comments

Comments
 (0)