On Sat, Oct 20, 2018 at 05:30:59PM +0200, Klemens Nanni wrote:
> On Sat, Oct 20, 2018 at 11:57:13AM +0200, Denis Fondras wrote:
> > Sync changes to host_*() from ntpd to relayd.
> This looks good, however I'm not a relayd user.
> How did you test it? With both IPv4 and IPv6?
>
I tested both on my machine, that's not extensive test though (using listen and
forward in both IPv4 & IPv6 and checking if backend is reachable).
Also regress is IPv4 only.
> Some nits inline to squash inconsistencies with other `host_ip()' users
> in base.
>
Thank you, new diff below.
Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/relayd/parse.y,v
retrieving revision 1.228
diff -u -p -r1.228 parse.y
--- parse.y 7 Sep 2018 07:35:31 -0000 1.228
+++ parse.y 20 Oct 2018 16:07:19 -0000
@@ -123,8 +123,7 @@ static enum direction dir = RELAY_DIR_A
static char *rulefile = NULL;
static union hashkey *hashkey = NULL;
-struct address *host_v4(const char *);
-struct address *host_v6(const char *);
+struct address *host_ip(const char *);
int host_dns(const char *, struct addresslist *,
int, struct portrange *, const char *, int);
int host_if(const char *, struct addresslist *,
@@ -2929,49 +2928,22 @@ symget(const char *nam)
}
struct address *
-host_v4(const char *s)
+host_ip(const char *s)
{
- struct in_addr ina;
- struct sockaddr_in *sain;
- struct address *h;
-
- bzero(&ina, sizeof(ina));
- if (inet_pton(AF_INET, s, &ina) != 1)
- return (NULL);
-
- if ((h = calloc(1, sizeof(*h))) == NULL)
- fatal(__func__);
- sain = (struct sockaddr_in *)&h->ss;
- sain->sin_len = sizeof(struct sockaddr_in);
- sain->sin_family = AF_INET;
- sain->sin_addr.s_addr = ina.s_addr;
-
- return (h);
-}
-
-struct address *
-host_v6(const char *s)
-{
- struct addrinfo hints, *res;
- struct sockaddr_in6 *sa_in6;
- struct address *h = NULL;
+ struct addrinfo hints, *res;
+ struct address *h = NULL;
- bzero(&hints, sizeof(hints));
- hints.ai_family = AF_INET6;
- hints.ai_socktype = SOCK_DGRAM; /* dummy */
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM; /*dummy*/
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo(s, "0", &hints, &res) == 0) {
- if ((h = calloc(1, sizeof(*h))) == NULL)
- fatal(__func__);
- sa_in6 = (struct sockaddr_in6 *)&h->ss;
- sa_in6->sin6_len = sizeof(struct sockaddr_in6);
- sa_in6->sin6_family = AF_INET6;
- memcpy(&sa_in6->sin6_addr,
- &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
- sizeof(sa_in6->sin6_addr));
- sa_in6->sin6_scope_id =
- ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
-
+ if (res->ai_family == AF_INET ||
+ res->ai_family == AF_INET6) {
+ if ((h = calloc(1, sizeof(*h))) == NULL)
+ fatal(NULL);
+ memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
+ }
freeaddrinfo(res);
}
@@ -2984,15 +2956,13 @@ host_dns(const char *s, struct addressli
{
struct addrinfo hints, *res0, *res;
int error, cnt = 0;
- struct sockaddr_in *sain;
- struct sockaddr_in6 *sin6;
struct address *h;
if ((cnt = host_if(s, al, max, port, ifname, ipproto)) != 0)
return (cnt);
bzero(&hints, sizeof(hints));
- hints.ai_family = PF_UNSPEC;
+ hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
hints.ai_flags = AI_ADDRCONFIG;
error = getaddrinfo(s, NULL, &hints, &res0);
@@ -3024,19 +2994,8 @@ host_dns(const char *s, struct addressli
}
if (ipproto != -1)
h->ipproto = ipproto;
- h->ss.ss_family = res->ai_family;
- if (res->ai_family == AF_INET) {
- sain = (struct sockaddr_in *)&h->ss;
- sain->sin_len = sizeof(struct sockaddr_in);
- sain->sin_addr.s_addr = ((struct sockaddr_in *)
- res->ai_addr)->sin_addr.s_addr;
- } else {
- sin6 = (struct sockaddr_in6 *)&h->ss;
- sin6->sin6_len = sizeof(struct sockaddr_in6);
- memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
- res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
- }
+ memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
TAILQ_INSERT_HEAD(al, h, entry);
cnt++;
@@ -3123,34 +3082,27 @@ int
host(const char *s, struct addresslist *al, int max,
struct portrange *port, const char *ifname, int ipproto)
{
- struct address *h;
+ struct address *h;
- h = host_v4(s);
+ if ((h = host_ip(s)) == NULL)
+ return (host_dns(s, al, max, port, ifname, ipproto));
- /* IPv6 address? */
- if (h == NULL)
- h = host_v6(s);
-
- if (h != NULL) {
- if (port != NULL)
- bcopy(port, &h->port, sizeof(h->port));
- if (ifname != NULL) {
- if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
- sizeof(h->ifname)) {
- log_warnx("%s: interface name truncated",
- __func__);
- free(h);
- return (-1);
- }
+ if (port != NULL)
+ bcopy(port, &h->port, sizeof(h->port));
+ if (ifname != NULL) {
+ if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
+ sizeof(h->ifname)) {
+ log_warnx("%s: interface name truncated",
+ __func__);
+ free(h);
+ return (-1);
}
- if (ipproto != -1)
- h->ipproto = ipproto;
-
- TAILQ_INSERT_HEAD(al, h, entry);
- return (1);
}
+ if (ipproto != -1)
+ h->ipproto = ipproto;
- return (host_dns(s, al, max, port, ifname, ipproto));
+ TAILQ_INSERT_HEAD(al, h, entry);
+ return (1);
}
void