xref: /freebsd/sbin/ipf/libipf/ntomask.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert  *
541edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert  *
741edb306SCy Schubert  * $Id$
841edb306SCy Schubert  */
941edb306SCy Schubert 
1041edb306SCy Schubert #include "ipf.h"
1141edb306SCy Schubert 
12efeb8bffSCy Schubert int
ntomask(int family,int nbits,u_32_t * ap)13efeb8bffSCy Schubert ntomask(int family, int nbits, u_32_t *ap)
1441edb306SCy Schubert {
1541edb306SCy Schubert 	u_32_t mask;
1641edb306SCy Schubert 
1741edb306SCy Schubert 	if (nbits < 0)
18*2582ae57SCy Schubert 		return (-1);
1941edb306SCy Schubert 
2041edb306SCy Schubert 	switch (family)
2141edb306SCy Schubert 	{
2241edb306SCy Schubert 	case AF_INET :
2341edb306SCy Schubert 		if (nbits > 32 || use_inet6 == 1)
24*2582ae57SCy Schubert 			return (-1);
2541edb306SCy Schubert 		if (nbits == 0) {
2641edb306SCy Schubert 			mask = 0;
2741edb306SCy Schubert 		} else {
2841edb306SCy Schubert 			mask = 0xffffffff;
2941edb306SCy Schubert 			mask <<= (32 - nbits);
3041edb306SCy Schubert 		}
3141edb306SCy Schubert 		*ap = htonl(mask);
3241edb306SCy Schubert 		break;
3341edb306SCy Schubert 
3441edb306SCy Schubert 	case 0 :
3541edb306SCy Schubert 	case AF_INET6 :
3641edb306SCy Schubert 		if ((nbits > 128) || (use_inet6 == -1))
37*2582ae57SCy Schubert 			return (-1);
3841edb306SCy Schubert 		fill6bits(nbits, ap);
3941edb306SCy Schubert 		break;
4041edb306SCy Schubert 
4141edb306SCy Schubert 	default :
42*2582ae57SCy Schubert 		return (-1);
4341edb306SCy Schubert 	}
44*2582ae57SCy Schubert 	return (0);
4541edb306SCy Schubert }
46