Browse Source

Make host_*() AF-agnostic

Merge host_v{4,6}() into much simpler host_ip() using just getaddrinfo().
host_dns() uses the same procedure.
OK naddy
OPENBSD_6_4
kn 6 years ago
parent
commit
183780a91b
1 changed files with 20 additions and 68 deletions
  1. +20
    -68
      src/usr.sbin/ntpd/config.c

+ 20
- 68
src/usr.sbin/ntpd/config.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: config.c,v 1.28 2015/10/12 06:50:08 reyk Exp $ */
/* $OpenBSD: config.c,v 1.29 2018/09/07 20:31:39 kn Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -29,8 +29,7 @@
#include "ntpd.h"
struct ntp_addr *host_v4(const char *);
struct ntp_addr *host_v6(const char *);
struct ntp_addr *host_ip(const char *);
static u_int32_t maxid = 0;
static u_int32_t constraint_maxid = 0;
@ -38,70 +37,36 @@ static u_int32_t constraint_maxid = 0;
void
host(const char *s, struct ntp_addr **hn)
{
struct ntp_addr *h = NULL;
struct ntp_addr *h;
if (!strcmp(s, "*"))
if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
if (!strcmp(s, "*")) {
if ((h = calloc(1, sizeof(*h))) == NULL)
fatal(NULL);
/* IPv4 address? */
if (h == NULL)
h = host_v4(s);
/* IPv6 address? */
if (h == NULL)
h = host_v6(s);
if (h == NULL)
return;
} else {
if ((h = host_ip(s)) == NULL)
return;
}
*hn = h;
}
struct ntp_addr *
host_v4(const char *s)
{
struct in_addr ina;
struct sockaddr_in *sa_in;
struct ntp_addr *h;
memset(&ina, 0, sizeof(struct in_addr));
if (inet_pton(AF_INET, s, &ina) != 1)
return (NULL);
if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
fatal(NULL);
sa_in = (struct sockaddr_in *)&h->ss;
sa_in->sin_len = sizeof(struct sockaddr_in);
sa_in->sin_family = AF_INET;
sa_in->sin_addr.s_addr = ina.s_addr;
return (h);
}
struct ntp_addr *
host_v6(const char *s)
host_ip(const char *s)
{
struct addrinfo hints, *res;
struct sockaddr_in6 *sa_in6;
struct ntp_addr *h = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
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(struct ntp_addr))) == NULL)
fatal(NULL);
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);
}
@ -124,12 +89,10 @@ host_dns(const char *s, struct ntp_addr **hn)
{
struct addrinfo hints, *res0, *res;
int error, cnt = 0;
struct sockaddr_in *sa_in;
struct sockaddr_in6 *sa_in6;
struct ntp_addr *h, *hh = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
/* ntpd MUST NOT use AI_ADDRCONFIG here */
error = getaddrinfo(s, NULL, &hints, &res0);
@ -145,20 +108,9 @@ host_dns(const char *s, struct ntp_addr **hn)
if (res->ai_family != AF_INET &&
res->ai_family != AF_INET6)
continue;
if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
if ((h = calloc(1, sizeof(*h))) == NULL)
fatal(NULL);
h->ss.ss_family = res->ai_family;
if (res->ai_family == AF_INET) {
sa_in = (struct sockaddr_in *)&h->ss;
sa_in->sin_len = sizeof(struct sockaddr_in);
sa_in->sin_addr.s_addr = ((struct sockaddr_in *)
res->ai_addr)->sin_addr.s_addr;
} else {
sa_in6 = (struct sockaddr_in6 *)&h->ss;
sa_in6->sin6_len = sizeof(struct sockaddr_in6);
memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)
res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
}
memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
h->next = hh;
hh = h;


Loading…
Cancel
Save