Added a CIDR notation based host constructor

This commit is contained in:
Martin Willi 2010-10-29 09:54:15 +02:00
parent 84f89634ef
commit 65697c2734
2 changed files with 44 additions and 0 deletions

View File

@ -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.
*/

View File

@ -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.
*