2005-04-25 17:31:50 +00:00
|
|
|
/*
|
2013-08-11 14:28:45 +00:00
|
|
|
* Copyright (C) 2012 by Darren Reed.
|
2005-04-25 17:31:50 +00:00
|
|
|
*
|
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
|
|
|
*
|
2013-08-11 14:28:45 +00:00
|
|
|
* $Id$
|
2005-04-25 17:31:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ipf.h"
|
|
|
|
|
|
|
|
|
2013-08-11 14:28:45 +00:00
|
|
|
int genmask(family, msk, mskp)
|
|
|
|
int family;
|
|
|
|
char *msk;
|
|
|
|
i6addr_t *mskp;
|
2005-04-25 17:31:50 +00:00
|
|
|
{
|
|
|
|
char *endptr = 0L;
|
2013-08-11 14:28:45 +00:00
|
|
|
u_32_t addr;
|
2005-04-25 17:31:50 +00:00
|
|
|
int bits;
|
|
|
|
|
|
|
|
if (strchr(msk, '.') || strchr(msk, 'x') || strchr(msk, ':')) {
|
|
|
|
/* possibly of the form xxx.xxx.xxx.xxx
|
|
|
|
* or 0xYYYYYYYY */
|
2013-08-11 14:28:45 +00:00
|
|
|
switch (family)
|
|
|
|
{
|
|
|
|
#ifdef USE_INET6
|
|
|
|
case AF_INET6 :
|
|
|
|
if (inet_pton(AF_INET6, msk, &mskp->in4) != 1)
|
2005-04-25 17:31:50 +00:00
|
|
|
return -1;
|
2013-08-11 14:28:45 +00:00
|
|
|
break;
|
2005-04-25 17:31:50 +00:00
|
|
|
#endif
|
2013-08-11 14:28:45 +00:00
|
|
|
case AF_INET :
|
|
|
|
if (inet_aton(msk, &mskp->in4) == 0)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default :
|
2005-04-25 17:31:50 +00:00
|
|
|
return -1;
|
2013-08-11 14:28:45 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2005-04-25 17:31:50 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* set x most significant bits
|
|
|
|
*/
|
|
|
|
bits = (int)strtol(msk, &endptr, 0);
|
2013-08-11 14:28:45 +00:00
|
|
|
|
|
|
|
switch (family)
|
|
|
|
{
|
|
|
|
case AF_INET6 :
|
|
|
|
if ((*endptr != '\0') || (bits < 0) || (bits > 128))
|
|
|
|
return -1;
|
|
|
|
fill6bits(bits, mskp->i6);
|
|
|
|
break;
|
|
|
|
case AF_INET :
|
|
|
|
if (*endptr != '\0' || bits > 32 || bits < 0)
|
|
|
|
return -1;
|
|
|
|
if (bits == 0)
|
|
|
|
addr = 0;
|
|
|
|
else
|
|
|
|
addr = htonl(0xffffffff << (32 - bits));
|
|
|
|
mskp->in4.s_addr = addr;
|
|
|
|
break;
|
|
|
|
default :
|
2005-04-25 17:31:50 +00:00
|
|
|
return -1;
|
2013-08-11 14:28:45 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2005-04-25 17:31:50 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|