From 65697c27345bdae19e5ca5d43ee15509e18b19ec Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Fri, 29 Oct 2010 09:54:15 +0200 Subject: [PATCH] Added a CIDR notation based host constructor --- src/libstrongswan/utils/host.c | 35 ++++++++++++++++++++++++++++++++++ src/libstrongswan/utils/host.h | 9 +++++++++ 2 files changed, 44 insertions(+) diff --git a/src/libstrongswan/utils/host.c b/src/libstrongswan/utils/host.c index 112d07e5cb..50e43d9926 100644 --- a/src/libstrongswan/utils/host.c +++ b/src/libstrongswan/utils/host.c @@ -561,6 +561,41 @@ host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port) return &this->public; } +/* + * 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. */ diff --git a/src/libstrongswan/utils/host.h b/src/libstrongswan/utils/host.h index f5796154cf..0a1be6e477 100644 --- a/src/libstrongswan/utils/host.h +++ b/src/libstrongswan/utils/host.h @@ -189,6 +189,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. *