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