diff options
author | Martin Willi <martin@revosec.ch> | 2010-10-29 09:54:15 +0200 |
---|---|---|
committer | Martin Willi <martin@revosec.ch> | 2011-01-05 16:45:40 +0100 |
commit | 65697c27345bdae19e5ca5d43ee15509e18b19ec (patch) | |
tree | ce692f36d1c1e32de6348d09c1c6445985c8b4ea /src/libstrongswan/utils | |
parent | 84f89634efdfd8d3eebcba3d0cfac3704ff8ec06 (diff) | |
download | strongswan-65697c27345bdae19e5ca5d43ee15509e18b19ec.tar.bz2 strongswan-65697c27345bdae19e5ca5d43ee15509e18b19ec.tar.xz |
Added a CIDR notation based host constructor
Diffstat (limited to 'src/libstrongswan/utils')
-rw-r--r-- | src/libstrongswan/utils/host.c | 35 | ||||
-rw-r--r-- | src/libstrongswan/utils/host.h | 9 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/libstrongswan/utils/host.c b/src/libstrongswan/utils/host.c index 112d07e5c..50e43d992 100644 --- a/src/libstrongswan/utils/host.c +++ b/src/libstrongswan/utils/host.c @@ -564,6 +564,41 @@ host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port) /* * 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; +} + +/* + * Described in header. + */ host_t *host_create_any(int family) { private_host_t *this = host_create_empty(); diff --git a/src/libstrongswan/utils/host.h b/src/libstrongswan/utils/host.h index f5796154c..0a1be6e47 100644 --- a/src/libstrongswan/utils/host.h +++ b/src/libstrongswan/utils/host.h @@ -190,6 +190,15 @@ host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port); host_t *host_create_from_sockaddr(sockaddr_t *sockaddr); /** + * Create a host from a CIDR subnet definition (1.2.3.0/24), return bits. + * + * @param string string to parse + * @param bits gets the number of network bits in CIDR notation + * @return network start address, NULL on error + */ +host_t *host_create_from_subnet(char *string, int *bits); + +/** * Create a host without an address, a "any" host. * * @param family family of the any host |