alist_new.c revision 170263
1/*
2 * Copyright (C) 2006 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 *
6 * $Id: alist_new.c,v 1.1.2.2 2006/08/25 22:43:21 darrenr Exp $
7 */
8
9#include "ipf.h"
10
11alist_t *
12alist_new(int v, char *host)
13{
14	int a, b, c, d, bits;
15	char *slash;
16	alist_t *al;
17	u_int mask;
18
19	al = calloc(1, sizeof(*al));
20	if (al == NULL) {
21		fprintf(stderr, "alist_new out of memory\n");
22		return NULL;
23	}
24
25	bits = -1;
26	slash = strchr(host, '/');
27	if (slash != NULL) {
28		*slash = '\0';
29		bits = atoi(slash + 1);
30	}
31
32	a = b = c = d = -1;
33	sscanf(host, "%d.%d.%d.%d", &a, &b, &c, &d);
34
35	if (bits > 0 && bits < 33) {
36		mask = 0xffffffff << (32 - bits);
37	} else if (b == -1) {
38		mask = 0xff000000;
39		b = c = d = 0;
40	} else if (c == -1) {
41		mask = 0xffff0000;
42		c = d = 0;
43	} else if (d == -1) {
44		mask = 0xffffff00;
45		d = 0;
46	} else {
47		mask = 0xffffffff;
48	}
49
50	if (*host == '!') {
51		al->al_not = 1;
52		host++;
53	}
54
55	if (gethost(host, &al->al_addr) == -1) {
56		*slash = '/';
57		fprintf(stderr, "Cannot parse hostname\n");
58		free(al);
59		return NULL;
60	}
61	al->al_mask = htonl(mask);
62	*slash = '/';
63	return al;
64}
65