Tidy code for --umbrella option.

This commit is contained in:
Simon Kelley
2022-01-01 23:33:39 +00:00
parent 2748fb81e2
commit 011f8cf1d0
3 changed files with 68 additions and 58 deletions

View File

@@ -766,12 +766,13 @@ will add 1.2.3.0/24 for IPv4 requestors and ::/0 for IPv6 requestors.
.B --add-subnet=1.2.3.4/24,1.2.3.4/24 .B --add-subnet=1.2.3.4/24,1.2.3.4/24
will add 1.2.3.0/24 for both IPv4 and IPv6 requestors. will add 1.2.3.0/24 for both IPv4 and IPv6 requestors.
.TP .TP
.B --umbrella[=deviceid:<deviceid>[,orgid:<orgid>]] .B --umbrella[=[deviceid:<deviceid>][,orgid:<orgid>][,assetid:<id>]]
Embeds the requestor's IP address in DNS queries forwarded upstream. Embeds the requestor's IP address in DNS queries forwarded upstream.
If device id or organization id are specified, the information is If device id or, asset id or organization id are specified, the information is
included in the forwarded queries and may be able to be used in included in the forwarded queries and may be able to be used in
filtering policies and reporting. The order of the deviceid and orgid filtering policies and reporting. The order of the id
attributes is irrelevant, but must be separated by a comma. attributes is irrelevant, but they must be separated by a comma. Deviceid is
a sixteen digit hexadecimal number, org and asset ids are decimal numbers.
.TP .TP
.B \-c, --cache-size=<cachesize> .B \-c, --cache-size=<cachesize>
Set the size of dnsmasq's cache. The default is 150 names. Setting the cache size to zero disables caching. Note: huge cache size impacts performance. Set the size of dnsmasq's cache. The default is 150 names. Setting the cache size to zero disables caching. Note: huge cache size impacts performance.

View File

@@ -460,31 +460,33 @@ static size_t add_umbrella_opt(struct dns_header *header, size_t plen, unsigned
struct umbrella_opt opt = {{"ODNS"}, UMBRELLA_VERSION, 0, {}}; struct umbrella_opt opt = {{"ODNS"}, UMBRELLA_VERSION, 0, {}};
u8 *u = &opt.fields[0]; u8 *u = &opt.fields[0];
int family = source->sa.sa_family;
int size = family == AF_INET ? INADDRSZ : IN6ADDRSZ;
if (daemon->umbrella_org) { if (daemon->umbrella_org)
{
PUTSHORT(UMBRELLA_ORG, u); PUTSHORT(UMBRELLA_ORG, u);
PUTLONG(daemon->umbrella_org, u); PUTLONG(daemon->umbrella_org, u);
} }
int family = source->sa.sa_family;
PUTSHORT(family == AF_INET ? UMBRELLA_IPV4 : UMBRELLA_IPV6, u); PUTSHORT(family == AF_INET ? UMBRELLA_IPV4 : UMBRELLA_IPV6, u);
int size = family == AF_INET ? INADDRSZ : IN6ADDRSZ;
memcpy(u, get_addrp(source, family), size); memcpy(u, get_addrp(source, family), size);
u += size; u += size;
if (option_bool(OPT_UMBRELLA_DEVID)) { if (option_bool(OPT_UMBRELLA_DEVID))
{
PUTSHORT(UMBRELLA_DEVICE, u); PUTSHORT(UMBRELLA_DEVICE, u);
memcpy(u, (char *)&daemon->umbrella_device, UMBRELLA_DEVICESZ); memcpy(u, (char *)&daemon->umbrella_device, UMBRELLA_DEVICESZ);
u += UMBRELLA_DEVICESZ; u += UMBRELLA_DEVICESZ;
} }
if (daemon->umbrella_asset) { if (daemon->umbrella_asset)
{
PUTSHORT(UMBRELLA_ASSET, u); PUTSHORT(UMBRELLA_ASSET, u);
PUTLONG(daemon->umbrella_asset, u); PUTLONG(daemon->umbrella_asset, u);
} }
int len = u - &opt.magic[0]; return add_pseudoheader(header, plen, (unsigned char *)limit, PACKETSZ, EDNS0_OPTION_UMBRELLA, (unsigned char *)&opt, u - (u8 *)&opt, 0, 1);
return add_pseudoheader(header, plen, (unsigned char *)limit, PACKETSZ, EDNS0_OPTION_UMBRELLA, (unsigned char *)&opt, len, 0, 1);
} }
/* Set *check_subnet if we add a client subnet option, which needs to checked /* Set *check_subnet if we add a client subnet option, which needs to checked

View File

@@ -2519,37 +2519,44 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
case LOPT_UMBRELLA: /* --umbrella */ case LOPT_UMBRELLA: /* --umbrella */
set_option_bool(OPT_UMBRELLA); set_option_bool(OPT_UMBRELLA);
while (arg) { while (arg)
{
comma = split(arg); comma = split(arg);
if (strstr(arg, "deviceid:")) { if (strstr(arg, "deviceid:"))
{
char *p;
u8 *u = daemon->umbrella_device;
char word[3];
arg += 9; arg += 9;
if (strlen(arg) != 16) if (strlen(arg) != 16)
ret_err(gen_err); ret_err(gen_err);
char *p;
for (p = arg; *p; p++) { for (p = arg; *p; p++)
if (!isxdigit((int)*p)) if (!isxdigit((int)*p))
ret_err(gen_err); ret_err(gen_err);
}
set_option_bool(OPT_UMBRELLA_DEVID); set_option_bool(OPT_UMBRELLA_DEVID);
u8 *u = daemon->umbrella_device; for (i = 0; i < (int)sizeof(daemon->umbrella_device); i++, arg+=2)
char word[3]; {
u8 i;
for (i = 0; i < sizeof(daemon->umbrella_device); i++, arg+=2) {
memcpy(word, &(arg[0]), 2); memcpy(word, &(arg[0]), 2);
*u++ = strtoul(word, NULL, 16); *u++ = strtoul(word, NULL, 16);
} }
} }
else if (strstr(arg, "orgid:")) { else if (strstr(arg, "orgid:"))
if (!strtoul_check(arg+6, &daemon->umbrella_org)) { {
if (!strtoul_check(arg+6, &daemon->umbrella_org))
ret_err(gen_err); ret_err(gen_err);
} }
} else if (strstr(arg, "assetid:"))
else if (strstr(arg, "assetid:")) { {
if (!strtoul_check(arg+8, &daemon->umbrella_asset)) { if (!strtoul_check(arg+8, &daemon->umbrella_asset))
ret_err(gen_err); ret_err(gen_err);
} }
} else
ret_err(gen_err);
arg = comma; arg = comma;
} }
break; break;