Sync usage with man page.
[netbsd-mini2440.git] / dist / ipf / lib / alist_new.c
blob37941e8353eda8f34bceac8866c6fcef16a14c6c
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2006 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Id: alist_new.c,v 1.1.2.3 2007/06/06 08:05:33 darrenr Exp
9 */
11 #include "ipf.h"
13 alist_t *
14 alist_new(int v, char *host)
16 int a, b, c, d, bits;
17 char *slash;
18 alist_t *al;
19 u_int mask;
21 al = calloc(1, sizeof(*al));
22 if (al == NULL) {
23 fprintf(stderr, "alist_new out of memory\n");
24 return NULL;
27 bits = -1;
28 slash = strchr(host, '/');
29 if (slash != NULL) {
30 *slash = '\0';
31 bits = atoi(slash + 1);
34 a = b = c = d = -1;
35 sscanf(host, "%d.%d.%d.%d", &a, &b, &c, &d);
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;
52 if (*host == '!') {
53 al->al_not = 1;
54 host++;
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;
64 al->al_mask = htonl(mask);
65 if (slash != NULL)
66 *slash = '/';
67 return al;