Added a CIDR notation based host constructor
authorMartin Willi <martin@revosec.ch>
Fri, 29 Oct 2010 07:54:15 +0000 (09:54 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 5 Jan 2011 15:45:40 +0000 (16:45 +0100)
src/libstrongswan/utils/host.c
src/libstrongswan/utils/host.h

index 112d07e..50e43d9 100644 (file)
@@ -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();
index f579615..0a1be6e 100644 (file)
@@ -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