2 * decodenetnum - return a net number (this is crude, but careful)
7 #ifdef HAVE_SYS_SOCKET_H
8 #include <sys/socket.h>
10 #ifdef HAVE_NETINET_IN_H
11 #include <netinet/in.h>
15 #include "ntp_stdlib.h"
16 #include "ntp_assert.h"
19 * decodenetnum convert text IP address and port to sockaddr_u
21 * Returns 0 for failure, 1 for success.
29 struct addrinfo hints, *ai = NULL;
38 NTP_REQUIRE(num != NULL);
39 NTP_REQUIRE(strlen(num) < sizeof(name));
44 * to distinguish IPv6 embedded colons from a port
45 * specification on an IPv4 address, assume all
46 * legal IPv6 addresses have at least two colons.
48 pp = strchr(num, ':');
50 cp = num; /* no colons */
51 else if (NULL != strchr(pp + 1, ':'))
52 cp = num; /* two or more colons */
53 else { /* one colon */
54 strlcpy(name, num, sizeof(name));
63 while (*cp && ']' != *cp)
66 if (']' == cp[0] && ':' == cp[1] && '\0' != cp[2])
71 hints.ai_flags = Z_AI_NUMERICHOST;
72 err = getaddrinfo(cp, "ntp", &hints, &ai);
75 NTP_INSIST(ai->ai_addrlen <= sizeof(*netnum));
77 memcpy(netnum, ai->ai_addr, ai->ai_addrlen);
79 if (NULL == port_str || 1 != sscanf(port_str, "%hu", &port))
81 SET_PORT(netnum, port);