xref: /netbsd-src/external/bsd/ipf/dist/lib/alist_new.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: alist_new.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4c9d5dc6cSdarrenr  * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  *
8*13885a66Sdarrenr  * Id: alist_new.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include "ipf.h"
12bc4097aaSchristos #include <ctype.h>
13bc4097aaSchristos 
14bc4097aaSchristos alist_t *
alist_new(int family,char * host)15bc4097aaSchristos alist_new(int family, char *host)
16bc4097aaSchristos {
17bc4097aaSchristos 	int a, b, c, d, bits;
18bc4097aaSchristos 	char *slash;
19bc4097aaSchristos 	alist_t *al;
20bc4097aaSchristos 	u_int mask;
21bc4097aaSchristos 
22bc4097aaSchristos 	if (family == AF_UNSPEC) {
23bc4097aaSchristos 		if (strchr(host, ':') != NULL)
24bc4097aaSchristos 			family = AF_INET6;
25bc4097aaSchristos 		else
26bc4097aaSchristos 			family = AF_INET;
27bc4097aaSchristos 	}
28bc4097aaSchristos 	if (family != AF_INET && family != AF_INET6)
29bc4097aaSchristos 		return NULL;
30bc4097aaSchristos 
31bc4097aaSchristos 	al = calloc(1, sizeof(*al));
32bc4097aaSchristos 	if (al == NULL) {
33bc4097aaSchristos 		fprintf(stderr, "alist_new out of memory\n");
34bc4097aaSchristos 		return NULL;
35bc4097aaSchristos 	}
36bc4097aaSchristos 
37bc4097aaSchristos 	while (ISSPACE(*host))
38bc4097aaSchristos 		host++;
39bc4097aaSchristos 
40bc4097aaSchristos 	if (*host == '!') {
41bc4097aaSchristos 		al->al_not = 1;
42bc4097aaSchristos 		host++;
43bc4097aaSchristos 		while (ISSPACE(*host))
44bc4097aaSchristos 			host++;
45bc4097aaSchristos 	}
46bc4097aaSchristos 
47bc4097aaSchristos 	bits = -1;
48bc4097aaSchristos 	slash = strchr(host, '/');
49bc4097aaSchristos 	if (slash != NULL) {
50bc4097aaSchristos 		*slash = '\0';
51bc4097aaSchristos 		bits = atoi(slash + 1);
52bc4097aaSchristos 	}
53bc4097aaSchristos 
54bc4097aaSchristos 	if (family == AF_INET) {
55bc4097aaSchristos 		if (bits > 32)
56bc4097aaSchristos 			goto bad;
57bc4097aaSchristos 
58bc4097aaSchristos 		a = b = c = d = -1;
59bc4097aaSchristos 		sscanf(host, "%d.%d.%d.%d", &a, &b, &c, &d);
60bc4097aaSchristos 
61bc4097aaSchristos 		if (bits > 0 && bits < 33) {
62bc4097aaSchristos 			mask = 0xffffffff << (32 - bits);
63bc4097aaSchristos 		} else if (b == -1) {
64bc4097aaSchristos 			mask = 0xff000000;
65bc4097aaSchristos 			b = c = d = 0;
66bc4097aaSchristos 		} else if (c == -1) {
67bc4097aaSchristos 			mask = 0xffff0000;
68bc4097aaSchristos 			c = d = 0;
69bc4097aaSchristos 		} else if (d == -1) {
70bc4097aaSchristos 			mask = 0xffffff00;
71bc4097aaSchristos 			d = 0;
72bc4097aaSchristos 		} else {
73bc4097aaSchristos 			mask = 0xffffffff;
74bc4097aaSchristos 		}
75bc4097aaSchristos 		al->al_mask = htonl(mask);
76bc4097aaSchristos 	} else {
77bc4097aaSchristos 		if (bits > 128)
78bc4097aaSchristos 			goto bad;
79bc4097aaSchristos 		fill6bits(bits, al->al_i6mask.i6);
80bc4097aaSchristos 	}
81bc4097aaSchristos 
82bc4097aaSchristos 	if (gethost(family, host, &al->al_i6addr) == -1) {
83bc4097aaSchristos 		if (slash != NULL)
84bc4097aaSchristos 			*slash = '/';
85bc4097aaSchristos 		fprintf(stderr, "Cannot parse hostname\n");
86bc4097aaSchristos 		goto bad;
87bc4097aaSchristos 	}
88bc4097aaSchristos 	al->al_family = family;
89bc4097aaSchristos 	if (slash != NULL)
90bc4097aaSchristos 		*slash = '/';
91bc4097aaSchristos 	return al;
92bc4097aaSchristos bad:
93bc4097aaSchristos 	free(al);
94bc4097aaSchristos 	return NULL;
95bc4097aaSchristos }
96