strongswan/src/libstrongswan/utils/host.c

631 lines
12 KiB
C
Raw Normal View History

2005-11-16 16:11:08 +00:00
/*
* Copyright (C) 2006-2009 Tobias Brunner
2007-02-28 14:04:36 +00:00
* Copyright (C) 2006 Daniel Roethlisberger
2006-07-07 08:49:06 +00:00
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
2005-11-16 16:11:08 +00:00
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#define _GNU_SOURCE
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
2005-11-16 16:11:08 +00:00
#include "host.h"
2005-11-16 16:11:08 +00:00
#include <debug.h>
2005-11-24 09:17:51 +00:00
2008-11-05 04:53:45 +00:00
#define IPV4_LEN 4
#define IPV6_LEN 16
2005-11-24 09:17:51 +00:00
typedef struct private_host_t private_host_t;
2005-11-16 16:11:08 +00:00
/**
* Private Data of a host object.
2005-11-16 16:11:08 +00:00
*/
struct private_host_t {
2005-11-16 16:11:08 +00:00
/**
* Public data
*/
host_t public;
2005-11-16 16:11:08 +00:00
/**
* low-lewel structure, which stores the address
2005-11-16 16:11:08 +00:00
*/
union {
/** generic type */
struct sockaddr address;
2007-02-28 14:04:36 +00:00
/** maximum sockaddr size */
struct sockaddr_storage address_max;
/** IPv4 address */
struct sockaddr_in address4;
/** IPv6 address */
struct sockaddr_in6 address6;
};
2005-11-16 16:11:08 +00:00
/**
* length of address structure
*/
socklen_t socklen;
};
2005-11-21 11:45:04 +00:00
/**
* implements host_t.get_sockaddr
*/
static sockaddr_t *get_sockaddr(private_host_t *this)
2005-11-16 16:11:08 +00:00
{
return &(this->address);
}
2005-11-21 11:45:04 +00:00
/**
* implements host_t.get_sockaddr_len
*/
static socklen_t *get_sockaddr_len(private_host_t *this)
2005-11-16 16:11:08 +00:00
{
return &(this->socklen);
}
/**
* Implementation of host_t.is_anyaddr.
*/
static bool is_anyaddr(private_host_t *this)
{
switch (this->address.sa_family)
{
case AF_INET:
{
2008-11-05 04:53:45 +00:00
u_int8_t zeroes[IPV4_LEN];
memset(zeroes, 0, IPV4_LEN);
return memeq(zeroes, &(this->address4.sin_addr.s_addr), IPV4_LEN);
}
case AF_INET6:
{
2008-11-05 04:53:45 +00:00
u_int8_t zeroes[IPV6_LEN];
memset(zeroes, 0, IPV6_LEN);
return memeq(zeroes, &(this->address6.sin6_addr.s6_addr), IPV6_LEN);
}
default:
{
return FALSE;
}
}
}
2005-11-16 16:11:08 +00:00
/**
* Described in header.
*/
int host_printf_hook(char *dst, size_t dstlen, printf_hook_spec_t *spec,
const void *const *args)
{
2006-09-27 14:15:49 +00:00
private_host_t *this = *((private_host_t**)(args[0]));
char buffer[INET6_ADDRSTRLEN + 16];
2006-09-27 14:15:49 +00:00
if (this == NULL)
{
snprintf(buffer, sizeof(buffer), "(null)");
2006-09-27 14:15:49 +00:00
}
else if (is_anyaddr(this))
2006-09-27 14:15:49 +00:00
{
2009-03-19 09:04:20 +00:00
snprintf(buffer, sizeof(buffer), "%%any%s",
this->address.sa_family == AF_INET6 ? "6" : "");
2006-09-27 14:15:49 +00:00
}
else
{
void *address;
u_int16_t port;
2008-05-14 06:34:54 +00:00
int len;
address = &this->address6.sin6_addr;
port = this->address6.sin6_port;
switch (this->address.sa_family)
{
case AF_INET:
address = &this->address4.sin_addr;
port = this->address4.sin_port;
/* fall */
case AF_INET6:
if (inet_ntop(this->address.sa_family, address,
buffer, sizeof(buffer)) == NULL)
{
snprintf(buffer, sizeof(buffer),
"(address conversion failed)");
}
else if (spec->hash)
{
2008-05-14 06:34:54 +00:00
len = strlen(buffer);
snprintf(buffer + len, sizeof(buffer) - len,
"[%d]", ntohs(port));
}
break;
default:
snprintf(buffer, sizeof(buffer), "(family not supported)");
break;
}
2005-11-21 11:45:04 +00:00
}
if (spec->minus)
{
return print_in_hook(dst, dstlen, "%-*s", spec->width, buffer);
}
return print_in_hook(dst, dstlen, "%*s", spec->width, buffer);
2006-09-27 14:15:49 +00:00
}
2005-11-29 15:23:04 +00:00
/**
* Implementation of host_t.get_address.
2005-11-29 15:23:04 +00:00
*/
static chunk_t get_address(private_host_t *this)
2005-11-29 15:23:04 +00:00
{
chunk_t address = chunk_empty;
switch (this->address.sa_family)
2005-11-29 15:23:04 +00:00
{
case AF_INET:
2005-11-29 15:23:04 +00:00
{
address.ptr = (char*)&(this->address4.sin_addr.s_addr);
2008-11-05 04:53:45 +00:00
address.len = IPV4_LEN;
return address;
}
case AF_INET6:
{
address.ptr = (char*)&(this->address6.sin6_addr.s6_addr);
2008-11-05 04:53:45 +00:00
address.len = IPV6_LEN;
return address;
2005-11-29 15:23:04 +00:00
}
default:
{
/* return empty chunk */
2005-11-29 15:23:04 +00:00
return address;
}
}
}
/**
* implements host_t.get_family
*/
static int get_family(private_host_t *this)
{
return this->address.sa_family;
2005-11-29 15:23:04 +00:00
}
2005-11-21 11:45:04 +00:00
/**
* implements host_t.get_port
*/
static u_int16_t get_port(private_host_t *this)
{
switch (this->address.sa_family)
2005-11-21 11:45:04 +00:00
{
case AF_INET:
2005-11-21 11:45:04 +00:00
{
return ntohs(this->address4.sin_port);
2005-11-21 11:45:04 +00:00
}
case AF_INET6:
{
return ntohs(this->address6.sin6_port);
}
2005-11-21 11:45:04 +00:00
default:
{
return 0;
}
}
}
2006-06-22 06:36:28 +00:00
/**
* implements host_t.set_port
*/
static void set_port(private_host_t *this, u_int16_t port)
{
switch (this->address.sa_family)
2006-06-22 06:36:28 +00:00
{
case AF_INET:
{
this->address4.sin_port = htons(port);
break;
}
case AF_INET6:
{
this->address6.sin6_port = htons(port);
break;
2006-06-22 06:36:28 +00:00
}
default:
{
break;
2006-06-22 06:36:28 +00:00
}
}
}
2005-11-16 16:11:08 +00:00
/**
2005-11-21 11:45:04 +00:00
* Implements host_t.clone.
2005-11-16 16:11:08 +00:00
*/
static private_host_t *clone_(private_host_t *this)
2005-11-16 16:11:08 +00:00
{
private_host_t *new = malloc_thing(private_host_t);
2005-11-16 16:11:08 +00:00
memcpy(new, this, sizeof(private_host_t));
2005-11-28 20:29:47 +00:00
return new;
2005-11-16 16:11:08 +00:00
}
/**
* Impelements host_t.ip_equals
*/
static bool ip_equals(private_host_t *this, private_host_t *other)
{
if (this->address.sa_family != other->address.sa_family)
{
2008-11-05 04:53:45 +00:00
/* 0.0.0.0 and 0::0 are equal */
return (is_anyaddr(this) && is_anyaddr(other));
}
switch (this->address.sa_family)
{
case AF_INET:
{
2008-11-05 04:53:45 +00:00
return memeq(&this->address4.sin_addr, &other->address4.sin_addr,
sizeof(this->address4.sin_addr));
}
case AF_INET6:
{
2008-11-05 04:53:45 +00:00
return memeq(&this->address6.sin6_addr, &other->address6.sin6_addr,
sizeof(this->address6.sin6_addr));
}
default:
break;
}
return FALSE;
}
2005-11-16 16:11:08 +00:00
2006-06-22 06:36:28 +00:00
/**
* Implements host_t.get_differences
*/
static host_diff_t get_differences(host_t *this, host_t *other)
2006-06-22 06:36:28 +00:00
{
host_diff_t ret = HOST_DIFF_NONE;
if (!this->ip_equals(this, other))
2006-06-22 06:36:28 +00:00
{
ret |= HOST_DIFF_ADDR;
}
if (this->get_port(this) != other->get_port(other))
2006-06-22 06:36:28 +00:00
{
ret |= HOST_DIFF_PORT;
}
2006-06-22 06:36:28 +00:00
return ret;
}
/**
2008-11-05 04:53:45 +00:00
* Implements host_t.equals
*/
static bool equals(private_host_t *this, private_host_t *other)
{
if (!ip_equals(this, other))
{
return FALSE;
}
switch (this->address.sa_family)
{
case AF_INET:
{
2008-11-05 04:53:45 +00:00
return (this->address4.sin_port == other->address4.sin_port);
}
case AF_INET6:
{
2008-11-05 04:53:45 +00:00
return (this->address6.sin6_port == other->address6.sin6_port);
}
default:
break;
}
return FALSE;
}
2005-12-02 16:09:04 +00:00
/**
* Implements host_t.destroy
*/
static void destroy(private_host_t *this)
{
free(this);
2005-12-02 16:09:04 +00:00
}
/**
* Creates an empty host_t object
2005-11-16 16:11:08 +00:00
*/
static private_host_t *host_create_empty(void)
2005-11-16 16:11:08 +00:00
{
private_host_t *this = malloc_thing(private_host_t);
2005-11-16 16:11:08 +00:00
this->public.get_sockaddr = (sockaddr_t* (*) (host_t*))get_sockaddr;
this->public.get_sockaddr_len = (socklen_t*(*) (host_t*))get_sockaddr_len;
this->public.clone = (host_t* (*) (host_t*))clone_;
this->public.get_family = (int (*) (host_t*))get_family;
this->public.get_address = (chunk_t (*) (host_t *)) get_address;
2005-11-21 11:45:04 +00:00
this->public.get_port = (u_int16_t (*) (host_t *))get_port;
2006-06-22 06:36:28 +00:00
this->public.set_port = (void (*) (host_t *,u_int16_t))set_port;
this->public.get_differences = get_differences;
this->public.ip_equals = (bool (*) (host_t *,host_t *)) ip_equals;
this->public.equals = (bool (*) (host_t *,host_t *)) equals;
this->public.is_anyaddr = (bool (*) (host_t *)) is_anyaddr;
2005-11-28 20:29:47 +00:00
this->public.destroy = (void (*) (host_t*))destroy;
return this;
}
/*
* Create a %any host with port
*/
static host_t *host_create_any_port(int family, u_int16_t port)
{
host_t *this;
this = host_create_any(family);
this->set_port(this, port);
return this;
}
/*
* Described in header.
*/
host_t *host_create_from_string(char *string, u_int16_t port)
{
private_host_t *this;
2008-11-05 04:53:45 +00:00
if (streq(string, "%any"))
{
return host_create_any_port(AF_INET, port);
2008-11-05 04:53:45 +00:00
}
if (streq(string, "%any6"))
{
return host_create_any_port(AF_INET6, port);
}
this = host_create_empty();
if (strchr(string, '.'))
{
this->address.sa_family = AF_INET;
}
else
{
this->address.sa_family = AF_INET6;
}
switch (this->address.sa_family)
{
case AF_INET:
{
if (inet_pton(AF_INET, string, &this->address4.sin_addr) <=0)
{
break;
}
this->address4.sin_port = htons(port);
this->socklen = sizeof(struct sockaddr_in);
return &this->public;
}
case AF_INET6:
{
if (inet_pton(AF_INET6, string, &this->address6.sin6_addr) <=0)
{
break;
}
this->address6.sin6_port = htons(port);
this->socklen = sizeof(struct sockaddr_in6);
return &this->public;
}
default:
{
break;
}
}
free(this);
return NULL;
2005-11-16 16:11:08 +00:00
}
2005-11-29 15:23:04 +00:00
/*
* Described in header.
*/
host_t *host_create_from_sockaddr(sockaddr_t *sockaddr)
{
private_host_t *this = host_create_empty();
switch (sockaddr->sa_family)
{
case AF_INET:
{
memcpy(&this->address4, sockaddr, sizeof(struct sockaddr_in));
this->socklen = sizeof(struct sockaddr_in);
return &this->public;
}
case AF_INET6:
{
memcpy(&this->address6, sockaddr, sizeof(struct sockaddr_in6));
this->socklen = sizeof(struct sockaddr_in6);
return &this->public;
}
default:
break;
}
free(this);
return NULL;
}
/*
* Described in header.
*/
host_t *host_create_from_dns(char *string, int af, u_int16_t port)
{
private_host_t *this;
struct addrinfo hints, *result;
int error;
if (streq(string, "%any"))
{
return host_create_any_port(af ? af : AF_INET, port);
}
2009-02-05 22:13:48 +00:00
if (streq(string, "%any6"))
{
return host_create_any_port(af ? af : AF_INET6, port);
2009-02-05 22:13:48 +00:00
}
if (af == AF_INET && strchr(string, ':'))
{ /* do not try to convert v6 addresses for v4 family */
return NULL;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = af;
error = getaddrinfo(string, NULL, &hints, &result);
if (error != 0)
{
DBG1(DBG_LIB, "resolving '%s' failed: %s", string, gai_strerror(error));
return NULL;
}
/* result is a linked list, but we use only the first address */
this = (private_host_t*)host_create_from_sockaddr(result->ai_addr);
freeaddrinfo(result);
if (this)
{
switch (this->address.sa_family)
{
case AF_INET:
this->address4.sin_port = htons(port);
break;
case AF_INET6:
this->address6.sin6_port = htons(port);
break;
}
return &this->public;
}
return NULL;
}
2005-11-29 15:23:04 +00:00
/*
* Described in header.
*/
host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
{
private_host_t *this;
switch (family)
2005-11-29 15:23:04 +00:00
{
case AF_INET:
if (address.len < IPV4_LEN)
2005-11-29 15:23:04 +00:00
{
return NULL;
2005-11-29 15:23:04 +00:00
}
address.len = IPV4_LEN;
break;
case AF_INET6:
if (address.len < IPV6_LEN)
{
return NULL;
}
address.len = IPV6_LEN;
break;
case AF_UNSPEC:
switch (address.len)
{
case IPV4_LEN:
family = AF_INET;
break;
case IPV6_LEN:
family = AF_INET6;
break;
default:
return NULL;
}
break;
default:
return NULL;
}
this = host_create_empty();
this->address.sa_family = family;
switch (family)
{
case AF_INET:
memcpy(&this->address4.sin_addr.s_addr, address.ptr, address.len);
this->address4.sin_port = htons(port);
this->socklen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
memcpy(&this->address6.sin6_addr.s6_addr, address.ptr, address.len);
this->address6.sin6_port = htons(port);
this->socklen = sizeof(struct sockaddr_in6);
break;
2005-11-29 15:23:04 +00:00
}
return &this->public;
2005-11-29 15:23:04 +00:00
}
/*
* Described in header.
*/
host_t *host_create_from_subnet(char *string, int *bits)
{
char *pos, buf[64];
host_t *net;
pos = strchr(string, '/');
if (pos)
{
if (pos - string >= sizeof(buf))
{
return NULL;
}
strncpy(buf, string, pos - string);
buf[pos - string] = '\0';
*bits = atoi(pos + 1);
return host_create_from_string(buf, 0);
}
net = host_create_from_string(buf, 0);
if (net)
{
if (net->get_family(net) == AF_INET)
{
*bits = 32;
}
else
{
*bits = 128;
}
}
return net;
}
2007-02-28 14:04:36 +00:00
/*
* Described in header.
*/
host_t *host_create_any(int family)
{
private_host_t *this = host_create_empty();
2007-02-28 14:04:36 +00:00
memset(&this->address_max, 0, sizeof(struct sockaddr_storage));
this->address.sa_family = family;
2007-02-28 14:04:36 +00:00
switch (family)
{
case AF_INET:
{
this->socklen = sizeof(struct sockaddr_in);
return &(this->public);
}
case AF_INET6:
{
this->socklen = sizeof(struct sockaddr_in6);
return &this->public;
}
default:
break;
}
free(this);
2007-02-28 14:04:36 +00:00
return NULL;
}