Extend specifying DNS servers by domain-name to --rev-server

Also Dbus SetDomainServers method.

Revert getaddrinfo hints.ai_socktype to SOCK_DGRAM to eliminate
duplicating every address three times for DGRAM, STREAM and RAW
in the results.
This commit is contained in:
Simon Kelley
2022-11-06 20:56:43 +00:00
parent 5b868c213b
commit 1db9943c68
4 changed files with 56 additions and 31 deletions

View File

@@ -538,7 +538,7 @@ of a server during startup. If name resolution fails, starting dnsmasq fails, to
If the system's configuration is such that the system resolver sends DNS queries If the system's configuration is such that the system resolver sends DNS queries
through the dnsmasq instance which is starting up then this will time-out and fail. through the dnsmasq instance which is starting up then this will time-out and fail.
.TP .TP
.B --rev-server=<ip-address>[/<prefix-len>][,<ipaddr>][#<port>][@<interface>][@<source-ip>[#<port>]] .B --rev-server=<ip-address>[/<prefix-len>][,<server>][#<port>][@<interface>][@<source-ip>[#<port>]]
This is functionally the same as This is functionally the same as
.B --server, .B --server,
but provides some syntactic sugar to make specifying address-to-name queries easier. For example but provides some syntactic sugar to make specifying address-to-name queries easier. For example

View File

@@ -380,7 +380,7 @@ static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings)
} }
/* parse the IP address */ /* parse the IP address */
if ((addr_err = parse_server(str_addr, &sdetails, 0)) || if ((addr_err = parse_server(str_addr, &sdetails)) ||
(addr_err = parse_server_addr(&sdetails))) (addr_err = parse_server_addr(&sdetails)))
{ {
error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,

View File

@@ -1476,7 +1476,7 @@ void read_servers_file(void);
void set_option_bool(unsigned int opt); void set_option_bool(unsigned int opt);
void reset_option_bool(unsigned int opt); void reset_option_bool(unsigned int opt);
struct hostsfile *expand_filelist(struct hostsfile *list); struct hostsfile *expand_filelist(struct hostsfile *list);
char *parse_server(char *arg, struct server_details *sdetails, const int can_resolve); char *parse_server(char *arg, struct server_details *sdetails);
char *parse_server_addr(struct server_details *sdetails); char *parse_server_addr(struct server_details *sdetails);
int option_read_dynfile(char *file, int flags); int option_read_dynfile(char *file, int flags);

View File

@@ -855,7 +855,7 @@ static char *parse_mysockaddr(char *arg, union mysockaddr *addr)
return NULL; return NULL;
} }
char *parse_server(char *arg, struct server_details *sdetails, const int can_resolve) char *parse_server(char *arg, struct server_details *sdetails)
{ {
sdetails->serv_port = NAMESERVER_PORT; sdetails->serv_port = NAMESERVER_PORT;
char *portno; char *portno;
@@ -901,11 +901,10 @@ char *parse_server(char *arg, struct server_details *sdetails, const int can_res
sdetails->addr_type = AF_INET; sdetails->addr_type = AF_INET;
else if (inet_pton(AF_INET6, arg, &sdetails->addr->in6.sin6_addr) > 0) else if (inet_pton(AF_INET6, arg, &sdetails->addr->in6.sin6_addr) > 0)
sdetails->addr_type = AF_INET6; sdetails->addr_type = AF_INET6;
/* if the argument is neither an IPv4 not an IPv6 address, it might be a else
hostname and we should try to resolve it to a suitable address.
However, we don't try this in domain_rev4/6 (can_resolve = 0) */
else if (can_resolve)
{ {
/* if the argument is neither an IPv4 not an IPv6 address, it might be a
hostname and we should try to resolve it to a suitable address. */
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
/* The AI_ADDRCONFIG flag ensures that then IPv4 addresses are returned in /* The AI_ADDRCONFIG flag ensures that then IPv4 addresses are returned in
the result only if the local system has at least one IPv4 address the result only if the local system has at least one IPv4 address
@@ -927,8 +926,10 @@ char *parse_server(char *arg, struct server_details *sdetails, const int can_res
that can be used with node <arg> and service "domain". */ that can be used with node <arg> and service "domain". */
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
/* Get addresses suitable for sending datagrams or TCP connections. */ /* Get addresses suitable for sending datagrams. We assume that we can use the
hints.ai_socktype = 0; same addresses for TCP connections. Settting this to zero gets each address
threes times, for SOCK_STREAM, SOCK_RAW and SOCK_DGRAM, which is not useful. */
hints.ai_socktype = SOCK_DGRAM;
/* Get address associated with this hostname */ /* Get address associated with this hostname */
ecode = getaddrinfo(arg, NULL, &hints, &sdetails->hostinfo); ecode = getaddrinfo(arg, NULL, &hints, &sdetails->hostinfo);
@@ -952,8 +953,6 @@ char *parse_server(char *arg, struct server_details *sdetails, const int can_res
return _((char*)gai_strerror(ecode)); return _((char*)gai_strerror(ecode));
} }
} }
else
return _("bad address");
sdetails->valid = 1; sdetails->valid = 1;
return NULL; return NULL;
@@ -1104,8 +1103,7 @@ static char *domain_rev4(int from_file, char *server, struct in_addr *addr4, int
if (!server) if (!server)
flags = SERV_LITERAL_ADDRESS; flags = SERV_LITERAL_ADDRESS;
else if ((string = parse_server(server, &sdetails, 0)) || else if ((string = parse_server(server, &sdetails)))
(string = parse_server_addr(&sdetails)))
return string; return string;
if (from_file) if (from_file)
@@ -1144,9 +1142,23 @@ static char *domain_rev4(int from_file, char *server, struct in_addr *addr4, int
sprintf(string, "in-addr.arpa"); sprintf(string, "in-addr.arpa");
if (flags & SERV_LITERAL_ADDRESS)
{
if (!add_update_server(flags, &serv_addr, &source_addr, interface, domain, NULL)) if (!add_update_server(flags, &serv_addr, &source_addr, interface, domain, NULL))
return _("error"); return _("error");
} }
else
{
while (parse_server_next(&sdetails))
{
if ((string = parse_server_addr(&sdetails)))
return string;
if (!add_update_server(flags, &serv_addr, &source_addr, interface, domain, NULL))
return _("error");
}
}
}
return NULL; return NULL;
} }
@@ -1169,8 +1181,7 @@ static char *domain_rev6(int from_file, char *server, struct in6_addr *addr6, in
if (!server) if (!server)
flags = SERV_LITERAL_ADDRESS; flags = SERV_LITERAL_ADDRESS;
else if ((string = parse_server(server, &sdetails, 0)) || else if ((string = parse_server(server, &sdetails)))
(string = parse_server_addr(&sdetails)))
return string; return string;
if (from_file) if (from_file)
@@ -1211,9 +1222,23 @@ static char *domain_rev6(int from_file, char *server, struct in6_addr *addr6, in
sprintf(string, "ip6.arpa"); sprintf(string, "ip6.arpa");
if (flags & SERV_LITERAL_ADDRESS)
{
if (!add_update_server(flags, &serv_addr, &source_addr, interface, domain, NULL)) if (!add_update_server(flags, &serv_addr, &source_addr, interface, domain, NULL))
return _("error"); return _("error");
} }
else
{
while (parse_server_next(&sdetails))
{
if ((string = parse_server_addr(&sdetails)))
return string;
if (!add_update_server(flags, &serv_addr, &source_addr, interface, domain, NULL))
return _("error");
}
}
}
return NULL; return NULL;
} }
@@ -2970,7 +2995,7 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
} }
else else
{ {
if ((err = parse_server(arg, &sdetails, 1))) if ((err = parse_server(arg, &sdetails)))
ret_err(err); ret_err(err);
} }