1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
Copyright (C) 1999-2004 IC & S dbmail@ic-s.nl
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "dbmail.h"
struct cidrfilter * cidr_new(const char *str)
{
struct cidrfilter *self;
char *addr, *port, *mask;
char *haddr, *hport;
unsigned i;
assert(str != NULL);
self = (struct cidrfilter *)malloc(sizeof(struct cidrfilter));
self->sock_str = strdup(str);
self->socket = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
self->mask = 32;
addr = strdup(str);
haddr = addr;
while (*addr != ':')
addr++;
addr++;
port = strdup(addr);
hport = port;
while (*port != ':')
port++;
port++;
/* chop port */
for (i=0; i<strlen(addr); i++) {
if (addr[i] == ':') {
addr[i]='\0';
break;
}
}
mask = index(addr,'/');
if (mask && mask[1] != '\0') {
mask++;
self->mask = atoi(mask);
/* chop mask */
for(i=0; i<strlen(addr); i++) {
if (addr[i] == '/') {
addr[i]='\0';
break;
}
}
}
self->socket->sin_family = AF_INET;
self->socket->sin_port = strtol(port,NULL,10);
if (! inet_aton(addr,&self->socket->sin_addr)) {
free(haddr);
free(hport);
cidr_free(self);
return NULL;
}
free(haddr);
free(hport);
return self;
}
int cidr_repr(struct cidrfilter *self)
{
return printf("struct cidrfilter {\n"
"\tsock_str: %s;\n"
"\tsocket->sin_addr: %s;\n"
"\tsocket->sin_port: %d;\n"
"\tmask: %d;\n"
"};\n",
self->sock_str,
inet_ntoa(self->socket->sin_addr),
self->socket->sin_port,
self->mask
);
}
int cidr_match(struct cidrfilter *base, struct cidrfilter *test)
{
char *fullmask = "255.255.255.255";
struct in_addr match_addr, base_addr, test_addr;
inet_aton(fullmask, &base_addr);
inet_aton(fullmask, &test_addr);
unsigned result = 0;
if (base->mask)
base_addr.s_addr = ~((unsigned long)base_addr.s_addr >> (32-base->mask));
if (test->mask)
test_addr.s_addr = ~((unsigned long)test_addr.s_addr >> (32-test->mask));
base_addr.s_addr = (base->socket->sin_addr.s_addr | base_addr.s_addr);
test_addr.s_addr = (test->socket->sin_addr.s_addr | test_addr.s_addr);
match_addr.s_addr = (base_addr.s_addr & test_addr.s_addr);
/* only match ports if specified */
if (test->socket->sin_port > 0 && (base->socket->sin_port != test->socket->sin_port))
return 0;
if (test_addr.s_addr == match_addr.s_addr)
result = base->mask ? base->mask : 32;
return result;
}
void cidr_free(struct cidrfilter *self)
{
if (! self)
return;
if (self->socket)
free(self->socket);
if (self->sock_str)
free(self->sock_str);
if (self)
free(self);
}
|