Repository navigation
Expand file tree
/
Copy pathnetwork.c
More file actions
144 lines (125 loc) · 3.7 KB
/
Copy pathnetwork.c
File metadata and controls
144 lines (125 loc) · 3.7 KB
1
2
3
4
5
F313
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
143
144
/*
* Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <sys/param.h>
#include <syslog.h>
#include "config.h"
#include <sys/queue.h>
#include <sys/tree.h>
#include <pcap.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <dnet.h>
#undef timeout_pending
#undef timeout_initialized
#include <event.h>
#include "network.h"
/*
* compare 2 v4 ranges
*/
enum net_order
network_compare(struct network *a, struct network *b)
{
struct addr addr_a, addr_b;
struct addr addr_aend, addr_bend;
/* Set up the addresses; still IPv4 dependent */
addr_a = a->net;
addr_a.addr_bits = IP_ADDR_BITS;
addr_b = b->net;
addr_b.addr_bits = IP_ADDR_BITS;
addr_bcast(&a->net, &addr_aend);
addr_aend.addr_bits = IP_ADDR_BITS;
addr_bcast(&b->net, &addr_bend);
addr_bend.addr_bits = IP_ADDR_BITS;
if (addr_cmp(&addr_aend, &addr_b) < 0)
return (NET_PRECEEDS);
if (addr_cmp(&addr_a, &addr_bend) > 0)
return (NET_FOLLOWS);
if (addr_cmp(&addr_a, &addr_b) <= 0 &&
addr_cmp(&addr_aend, &addr_bend) >= 0){
if (addr_cmp(&a->net, &b->net) == 0)
return (NET_EQUALS);
return (NET_CONTAINS);
}
return (NET_CONTAINED);
}
/* Unittests */
static void
network_test_compare(void)
{
struct addr one, two;
struct network net_one, net_two;
addr_pton("1.0.0.0/24", &one);
addr_pton("2.0.0.0/24", &two);
net_one.net = one;
net_two.net = two;
if (network_compare(&net_one, &net_two) != NET_PRECEEDS)
{
syslog(LOG_ERR, "network_compare");
exit(EXIT_FAILURE);
}
if (network_compare(&net_two, &net_one) != NET_FOLLOWS)
{
syslog(LOG_ERR, "network_compare");
exit(EXIT_FAILURE);
}
if (network_compare(&net_two, &net_two) != NET_EQUALS)
{
syslog(LOG_ERR, "network_compare");
exit(EXIT_FAILURE);
}
addr_pton("2.1.0.0/24", &one);
addr_pton("2.0.0.0/8", &two);
net_one.net = one;
net_two.net = two;
if (network_compare(&net_one, &net_two) != NET_CONTAINED)
{
syslog(LOG_ERR, "network_compare: !contained");
exit(EXIT_FAILURE);
}
if (network_compare(&net_two, &net_one) != NET_CONTAINS)
{
syslog(LOG_ERR, "network_compare: !contains");
exit(EXIT_FAILURE);
}
fprintf(stderr, "\t%s: OK\n", __func__);
}
void
network_test(void)
{
network_test_compare();
}