host: Add function to create two hosts from a range definition

This commit is contained in:
Tobias Brunner
2014-10-30 12:32:45 +01:00
parent bbe3070aa2
commit 82be444eb9
3 changed files with 124 additions and 0 deletions
+28
View File
@@ -525,6 +525,34 @@ host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
return &this->public;
}
/*
* Described in header.
*/
bool host_create_from_range(char *string, host_t **from, host_t **to)
{
char *pos;
pos = strchr(string, '-');
if (!pos)
{
return FALSE;
}
*to = host_create_from_string(pos + 1, 0);
if (!*to)
{
return FALSE;
}
pos = strndup(string, pos - string);
*from = host_create_from_string_and_family(pos, (*to)->get_family(*to), 0);
free(pos);
if (!*from)
{
(*to)->destroy(*to);
return FALSE;
}
return TRUE;
}
/*
* Described in header.
*/
+13
View File
@@ -180,6 +180,19 @@ host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
*/
host_t *host_create_from_sockaddr(sockaddr_t *sockaddr);
/**
* Parse a range definition (1.2.3.0-1.2.3.5), return the two hosts.
*
* The two hosts are not ordered, from is simply the first, to is the second,
* from is not necessarily smaller.
*
* @param string string to parse
* @param from returns the first address (out)
* @param to returns the second address (out)
* @return TRUE if parsed successfully, FALSE otherwise
*/
bool host_create_from_range(char *string, host_t **from, host_t **to);
/**
* Create a host from a CIDR subnet definition (1.2.3.0/24), return bits.
*