dnl ** IPv6 support
dnl **
-have_ipv6=no
AC_MSG_CHECKING([for IPv6])
AC_CACHE_VAL(i_cv_type_in6_addr,
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
[[struct in6_addr i;]])],
[i_cv_type_in6_addr=yes],
[i_cv_type_in6_addr=no])])
-if test $i_cv_type_in6_addr = yes; then
- AC_DEFINE(HAVE_IPV6,, [Build with IPv6 support])
- have_ipv6=yes
-fi
AC_MSG_RESULT($i_cv_type_in6_addr)
+if test $i_cv_type_in6_addr = no; then
+ AC_ERROR([Can't build without IPv6 support.])
+fi
dnl **
dnl ** storage classes
struct ip_addr ip;
ip = auth_request->remote_ip;
-#ifdef HAVE_IPV6
if (IPADDR_IS_V6(&ip)) {
memset(ip.u.ip6.s6_addr + PENALTY_IPV6_MASK_BITS/CHAR_BIT, 0,
sizeof(ip.u.ip6.s6_addr) -
PENALTY_IPV6_MASK_BITS/CHAR_BIT);
}
-#endif
return net_ip2addr(&ip);
}
max_bits = 32;
last_bits = 8;
} else {
-#ifndef HAVE_IPV6
- i_error("IPv6 not supported");
- return -1;
-#else
ip1_arr = (void *)&ip1.u.ip6;
ip2_arr = (void *)&ip2.u.ip6;
max_bits = 128;
last_bits = 16;
-#endif
}
/* make sure initial bits match */
.url_parsed = {
.host_name = "127.0.0.1",
.have_host_ip = TRUE }
-#ifdef HAVE_IPV6
},{
.url = "http://[::1]",
.url_parsed = {
.host_name = "[::1]",
.have_host_ip = TRUE,
.port = 8080, .have_port = TRUE }
-#endif
},{
.url = "http://user@api.dovecot.org",
.flags = HTTP_URL_ALLOW_USERINFO_PART,
.url = "http://[]/index.html"
},{
.url = "http://[v08.234:232:234:234:2221]/index.html"
-#ifdef HAVE_IPV6
},{
.url = "http://[1::34a:34:234::6]/index.html"
-#endif
},{
.url = "http://example%a.com/index.html"
},{
static const char *parse_create_url_tests[] = {
"http://www.example.com/",
"http://10.0.0.1/",
-#ifdef HAVE_IPV6
"http://[::1]/",
-#endif
"http://www.example.com:993/",
"http://www.example.com/index.html",
"http://www.example.com/settings/index.html",
return FALSE; /* 127/8 */
#undef IP4
}
-#ifdef HAVE_IPV6
else if (ip->family == AF_INET6) {
addr = ip->u.ip6.s6_addr[0];
if (addr == 0xfc || addr == 0xfd)
return FALSE; /* fc00::/7 */
}
-#endif
return TRUE;
}
.host_name = "127.0.0.1",
.userid = "user",
.have_host_ip = TRUE }
-#ifdef HAVE_IPV6
},{
.url = "imap://user@[::1]",
.url_parsed = {
.host_name = "[::1]",
.userid = "user",
.have_host_ip = TRUE }
-#endif
},{
.url = "imap://user@4example.com:423",
.url_parsed = {
.url = "imap://[]/INBOX"
},{
.url = "imap://[v08.234:232:234:234:2221]/INBOX"
-#ifdef HAVE_IPV6
},{
.url = "imap://[1::34a:34:234::6]/INBOX"
-#endif
},{
.url = "imap://example%a.com/INBOX"
},{
static const char *parse_create_url_tests[] = {
"imap://host.example.com/",
"imap://10.0.0.1/",
-#ifdef HAVE_IPV6
"imap://[::1]/",
-#endif
"imap://user@host.example.com/",
"imap://user@host.example.com:993/",
"imap://su%3auser@host.example.com/",
union sockaddr_union {
struct sockaddr sa;
struct sockaddr_in sin;
-#ifdef HAVE_IPV6
struct sockaddr_in6 sin6;
-#endif
};
union sockaddr_union_unix {
struct sockaddr_un un;
};
-#ifdef HAVE_IPV6
-# define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
+#define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
sizeof(so.sin6) : sizeof(so.sin))
-#else
-# define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
-#endif
#if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && !defined(HAVE_GETPEERUCRED) && defined(MSG_WAITALL) && defined(LOCAL_CREDS)
# define NEEDS_LOCAL_CREDS 1
if (ip1->family != ip2->family)
return ip1->family - ip2->family;
-#ifdef HAVE_IPV6
if (ip1->family == AF_INET6)
return memcmp(&ip1->u.ip6, &ip2->u.ip6, sizeof(ip1->u.ip6));
-#endif
return memcmp(&ip1->u.ip4, &ip2->u.ip4, sizeof(ip1->u.ip4));
}
const unsigned char *p;
unsigned int len, g, h = 0;
-#ifdef HAVE_IPV6
if (ip->family == AF_INET6) {
p = ip->u.ip6.s6_addr;
len = sizeof(ip->u.ip6);
} else
-#endif
{
return ip->u.ip4.s_addr;
}
sin_set_ip(union sockaddr_union *so, const struct ip_addr *ip)
{
if (ip == NULL) {
-#ifdef HAVE_IPV6
so->sin6.sin6_family = AF_INET6;
so->sin6.sin6_addr = in6addr_any;
-#else
- so->sin.sin_family = AF_INET;
- so->sin.sin_addr.s_addr = INADDR_ANY;
-#endif
return;
}
so->sin.sin_family = ip->family;
-#ifdef HAVE_IPV6
if (ip->family == AF_INET6)
memcpy(&so->sin6.sin6_addr, &ip->u.ip6, sizeof(ip->u.ip6));
else
-#endif
memcpy(&so->sin.sin_addr, &ip->u.ip4, sizeof(ip->u.ip4));
}
ip->family = so->sin.sin_family;
-#ifdef HAVE_IPV6
if (ip->family == AF_INET6)
memcpy(&ip->u.ip6, &so->sin6.sin6_addr, sizeof(ip->u.ip6));
else
-#endif
if (ip->family == AF_INET)
memcpy(&ip->u.ip4, &so->sin.sin_addr, sizeof(ip->u.ip4));
else
static inline void sin_set_port(union sockaddr_union *so, in_port_t port)
{
-#ifdef HAVE_IPV6
if (so->sin.sin_family == AF_INET6)
so->sin6.sin6_port = htons(port);
else
-#endif
so->sin.sin_port = htons(port);
}
static inline in_port_t sin_get_port(union sockaddr_union *so)
{
-#ifdef HAVE_IPV6
if (so->sin.sin_family == AF_INET6)
return ntohs(so->sin6.sin6_port);
-#endif
if (so->sin.sin_family == AF_INET)
return ntohs(so->sin.sin_port);
void net_get_ip_any6(struct ip_addr *ip)
{
-#ifdef HAVE_IPV6
ip->family = AF_INET6;
ip->u.ip6 = in6addr_any;
-#else
- memset(ip, 0, sizeof(struct ip_addr));
-#endif
}
int net_listen(const struct ip_addr *my_ip, in_port_t *port, int backlog)
/* create the socket */
fd = socket(so.sin.sin_family, SOCK_STREAM, 0);
-#ifdef HAVE_IPV6
if (fd == -1 && my_ip == NULL &&
(errno == EINVAL || errno == EAFNOSUPPORT)) {
/* IPv6 is not supported by OS */
fd = socket(AF_INET, SOCK_STREAM, 0);
}
-#endif
if (fd == -1) {
i_error("socket() failed: %m");
return -1;
unsigned int *ips_count)
{
/* @UNSAFE */
-#ifdef HAVE_IPV6
union sockaddr_union *so;
struct addrinfo hints, *ai, *origai;
struct ip_addr ip;
int host_error;
-#else
- struct hostent *hp;
-#endif
int count;
*ips = NULL;
*ips_count = 0;
-#ifdef HAVE_IPV6
/* support [ipv6] style addresses here so they work globally */
if (addr[0] == '[' && net_addr2ip(addr, &ip) == 0) {
*ips_count = 1;
sin_get_ip(so, &(*ips)[count]);
}
freeaddrinfo(origai);
-#else
- hp = gethostbyname(addr);
- if (hp == NULL)
- return h_errno;
-
- /* get number of IPs */
- count = 0;
- while (hp->h_addr_list[count] != NULL)
- count++;
-
- *ips_count = count;
- *ips = t_malloc(sizeof(struct ip_addr) * count);
-
- while (count > 0) {
- count--;
-
- (*ips)[count].family = AF_INET;
- memcpy(&(*ips)[count].u.ip4, hp->h_addr_list[count],
- sizeof((*ips)[count].u.ip4));
- }
-#endif
return 0;
}
const char *net_ip2addr(const struct ip_addr *ip)
{
-#ifdef HAVE_IPV6
char addr[MAX_IP_LEN+1];
addr[MAX_IP_LEN] = '\0';
return "";
return t_strdup(addr);
-#else
- unsigned long ip4;
-
- if (ip->family != AF_INET)
- return "";
-
- ip4 = ntohl(ip->u.ip4.s_addr);
- return t_strdup_printf("%lu.%lu.%lu.%lu",
- (ip4 & 0xff000000UL) >> 24,
- (ip4 & 0x00ff0000) >> 16,
- (ip4 & 0x0000ff00) >> 8,
- (ip4 & 0x000000ff));
-#endif
}
int net_addr2ip(const char *addr, struct ip_addr *ip)
if (strchr(addr, ':') != NULL) {
/* IPv6 */
-#ifdef HAVE_IPV6
T_BEGIN {
if (addr[0] == '[') {
/* allow [ipv6 addr] */
} T_END;
if (ret == 0)
return -1;
-#else
- ip->u.ip4.s_addr = 0;
-#endif
ip->family = AF_INET6;
} else {
/* IPv4 */
int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
struct ip_addr *dest)
{
-#ifdef HAVE_IPV6
static uint8_t v4_prefix[] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
dest->family = AF_INET;
memcpy(&dest->u.ip6, &src->u.ip6.s6_addr[3*4], 4);
return 0;
-#else
- return -1;
-#endif
}
int net_geterror(int fd)
const char *net_gethosterror(int error)
{
-#ifdef HAVE_IPV6
i_assert(error != 0);
return gai_strerror(error);
-#else
- switch (error) {
- case HOST_NOT_FOUND:
- return "Host not found";
- case NO_ADDRESS:
- return "No IP address found for name";
- case NO_RECOVERY:
- return "A non-recoverable name server error occurred";
- case TRY_AGAIN:
- return "A temporary error on an authoritative name server";
- default:
- return t_strdup_printf("Unknown error %d", error);
- }
-#endif
}
int net_hosterror_notfound(int error)
{
-#ifdef HAVE_IPV6
#ifdef EAI_NODATA /* NODATA is depricated */
return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
#else
return error != 1 && (error == EAI_NONAME);
#endif
-#else
- return error == HOST_NOT_FOUND || error == NO_ADDRESS;
-#endif
}
const char *net_getservbyport(in_port_t port)
ip1 = &ip->u.ip4.s_addr;
ip2 = &net_ip->u.ip4.s_addr;
} else {
-#ifdef HAVE_IPV6
ip1 = (const void *)&ip->u.ip6;
ip2 = (const void *)&net_ip->u.ip6;
-#else
- /* shouldn't get here */
- return FALSE;
-#endif
}
/* check first the full 32bit ints */
struct ip_addr {
unsigned short family;
union {
-#ifdef HAVE_IPV6
struct in6_addr ip6;
-#endif
struct in_addr ip4;
} u;
};
};
/* maxmimum string length of IP address */
-#ifdef HAVE_IPV6
-# define MAX_IP_LEN INET6_ADDRSTRLEN
-#else
-# define MAX_IP_LEN 20
-#endif
-
-#ifndef HAVE_IPV6
-# undef EAI_NONAME
-# define EAI_NONAME NO_ADDRESS
-# undef EAI_FAIL
-# define EAI_FAIL NO_RECOVERY
-#endif
+#define MAX_IP_LEN INET6_ADDRSTRLEN
#define IPADDR_IS_V4(ip) ((ip)->family == AF_INET)
#define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6)
{ "1.2.3.255", "1.2.3.0", 24, TRUE },
{ "1.2.255.255", "1.2.254.0", 23, TRUE },
{ "255.255.255.255", "128.0.0.0", 1, TRUE },
- { "255.255.255.255", "127.0.0.0", 1, FALSE }
-#ifdef HAVE_IPV6
- ,
+ { "255.255.255.255", "127.0.0.0", 1, FALSE },
{ "1234:5678::abcf", "1234:5678::abce", 127, TRUE },
{ "1234:5678::abcd", "1234:5678::abce", 127, FALSE },
{ "123e::ffff", "123e::0", 15, TRUE },
{ "::ffff:1.2.3.4", "1.2.3.4", 32, TRUE },
{ "::ffff:1.2.3.4", "1.2.3.3", 32, FALSE },
{ "::ffff:1.2.3.4", "::ffff:1.2.3.4", 0, FALSE }
-#endif
};
struct ip_addr ip, net_ip;
unsigned int i;
net_ip.family = 0;
test_assert(!net_is_in_network(&ip, &net_ip, 0));
test_assert(!net_is_in_network(&net_ip, &ip, 0));
-#ifdef HAVE_IPV6
test_assert(net_addr2ip("::1", &ip) == 0);
net_ip = ip;
net_ip.family = 0;
test_assert(!net_is_in_network(&ip, &net_ip, 0));
test_assert(!net_is_in_network(&net_ip, &ip, 0));
-#endif
test_end();
}
test_assert(net_addr2ip("127.0.0.1", &ip) == 0 &&
ip.family == AF_INET &&
ntohl(ip.u.ip4.s_addr) == (0x7f000001));
-#ifdef HAVE_IPV6
test_assert(net_addr2ip("::5", &ip) == 0 &&
ip.family == AF_INET6 &&
ip.u.ip6.s6_addr[15] == 5);
ip.family = 123;
test_assert(net_addr2ip("abc", &ip) < 0 &&
ip.family == 123);
-#endif
test_end();
}
return 0;
}
-#ifdef HAVE_IPV6
static int
uri_parse_ip_literal(struct uri_parser *parser, string_t *literal,
struct in6_addr *ip6_r) ATTR_NULL(2,3)
*ip6_r = ip6;
return 1;
}
-#endif
static int
uri_parse_host(struct uri_parser *parser,
/* IP-literal / */
if (parser->cur < parser->end && *parser->cur == '[') {
-#ifdef HAVE_IPV6
if ((ret=uri_parse_ip_literal(parser, literal, &ip6)) <= 0)
return -1;
auth->have_host_ip = TRUE;
}
return 1;
-#else
- parser->error = "IPv6 host address is not supported";
- return -1;
-#endif
}
/* IPv4address /
buffer_append_c(buf, client->remote_port & 0xff);
buffer_append_c(buf, (client->remote_port >> 8) & 0xff);
-#ifdef HAVE_IPV6
if (IPADDR_IS_V6(&client->ip))
buffer_append(buf, &client->ip.u.ip6, sizeof(client->ip.u.ip6));
else
-#endif
buffer_append(buf, &client->ip.u.ip4, sizeof(client->ip.u.ip4));
base64_encode(buf->data, buf->used, base64_buf);
client->session_id = p_strdup(client->pool, str_c(base64_buf));
#ifdef IOLOOP_NOTIFY_KQUEUE
" notify=kqueue"
#endif
-#ifdef HAVE_IPV6
- " ipv6"
-#endif
#ifdef HAVE_GNUTLS
" gnutls"
#endif