mirror of
https://github.com/strongswan/strongswan.git
synced 2025-10-09 00:00:53 -04:00
host: add a netmask constructor taking the number of network bits
This commit is contained in:
parent
4dc83e9fac
commit
2d8a01d1c6
@ -558,6 +558,55 @@ host_t *host_create_from_subnet(char *string, int *bits)
|
|||||||
return net;
|
return net;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* See header.
|
||||||
|
*/
|
||||||
|
host_t *host_create_netmask(int family, int netbits)
|
||||||
|
{
|
||||||
|
private_host_t *this;
|
||||||
|
int bits, bytes, len = 0;
|
||||||
|
char *target;
|
||||||
|
|
||||||
|
switch (family)
|
||||||
|
{
|
||||||
|
case AF_INET:
|
||||||
|
if (netbits < 0 || netbits > 32)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
this = host_create_empty();
|
||||||
|
this->socklen = sizeof(struct sockaddr_in);
|
||||||
|
target = (char*)&this->address4.sin_addr;
|
||||||
|
len = 4;
|
||||||
|
break;
|
||||||
|
case AF_INET6:
|
||||||
|
if (netbits < 0 || netbits > 128)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
this = host_create_empty();
|
||||||
|
this->socklen = sizeof(struct sockaddr_in6);
|
||||||
|
target = (char*)&this->address6.sin6_addr;
|
||||||
|
len = 16;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&this->address_max, 0, sizeof(struct sockaddr_storage));
|
||||||
|
this->address.sa_family = family;
|
||||||
|
update_sa_len(this);
|
||||||
|
|
||||||
|
bytes = (netbits + 7) / 8;
|
||||||
|
bits = (bytes * 8) - netbits;
|
||||||
|
|
||||||
|
memset(target, 0xff, bytes);
|
||||||
|
memset(target + bytes, 0x00, len - bytes);
|
||||||
|
target[bytes - 1] = bits ? (u_int8_t)(0xff << bits) : 0xff;
|
||||||
|
|
||||||
|
return &this->public;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Described in header.
|
* Described in header.
|
||||||
*/
|
*/
|
||||||
|
@ -191,6 +191,14 @@ host_t *host_create_from_sockaddr(sockaddr_t *sockaddr);
|
|||||||
*/
|
*/
|
||||||
host_t *host_create_from_subnet(char *string, int *bits);
|
host_t *host_create_from_subnet(char *string, int *bits);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a netmask host having the first netbits bits set.
|
||||||
|
*
|
||||||
|
* @param netbits number of leading bits set in the host
|
||||||
|
* @return netmask host
|
||||||
|
*/
|
||||||
|
host_t *host_create_netmask(int family, int netbits);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a host without an address, a "any" host.
|
* Create a host without an address, a "any" host.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user