forked from CAIDA/cc-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_utils.c
169 lines (147 loc) · 4.48 KB
/
ip_utils.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2012 The Regents of the University of California.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <assert.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "ip_utils.h"
/**
* Recursively compute network addresses to cover range lo-hi
*
* Note: Worst case scenario is when lo=0.0.0.1 and hi=255.255.255.254
* We then have 62 CIDR bloks to cover this interval, and 125
* calls to split_range();
* The maximum possible recursion depth is 32.
*/
static int split_range(uint32_t addr, int masklen,
uint32_t lo, uint32_t hi,
ip_prefix_list_t **pfx_list)
{
uint32_t bc, lower_half, upper_half;
ip_prefix_list_t *new_node = NULL;
if ((masklen < 0) || (masklen > 32) || pfx_list == NULL)
{
return -1;
}
bc = ip_broadcast_addr(addr, masklen);
if ((lo < addr) || (hi > bc))
{
return -1;
}
if ( (lo == addr) && (hi == bc) )
{
/* add this (addr, masklen) to the list */
if((new_node = malloc(sizeof(ip_prefix_list_t))) == NULL)
{
return -1;
}
new_node->prefix.addr = addr;
new_node->prefix.masklen = masklen;
/* this will work even for the first element in the list
as pfx_list will be null */
new_node->next = *pfx_list;
*pfx_list = new_node;
return 0;
}
masklen++;
lower_half = addr;
upper_half = ip_set_bit(addr, masklen, 1);
if (hi < upper_half)
{
return split_range(lower_half, masklen, lo, hi, pfx_list);
}
else if (lo >= upper_half)
{
return split_range(upper_half, masklen, lo, hi, pfx_list);
}
else
{
if(split_range(lower_half, masklen, lo,
ip_broadcast_addr(lower_half, masklen), pfx_list) != 0)
{
return -1;
}
return split_range(upper_half, masklen, upper_half, hi, pfx_list);
}
assert(0);
}
/**
* Set a bit to a given value (0 or 1); MSB is bit 1, LSB is bit 32
*/
uint32_t ip_set_bit(uint32_t addr, int bitno, int val)
{
if (val)
return(addr | (1 << (32 - bitno)));
else
return(addr & ~(1 << (32 - bitno)));
}
/**
* Compute netmask address given a prefix bit length
*/
uint32_t ip_netmask(int masklen)
{
if ( masklen == 0 )
return( ~((uint32_t) -1) );
else
return( ~((1 << (32 - masklen)) - 1) );
}
/**
* Compute broadcast address given address and prefix
*/
uint32_t ip_broadcast_addr(uint32_t addr, int masklen)
{
return(addr | ~ip_netmask(masklen));
}
/**
* Compute network address given address and prefix
*/
uint32_t ip_network_addr(uint32_t addr, int masklen)
{
return(addr & ip_netmask(masklen));
}
int ip_range_to_prefix(ip_prefix_t lower, ip_prefix_t upper,
ip_prefix_list_t **pfx_list)
{
/* get the first address of the lower prefix */
uint32_t lo = ip_network_addr(lower.addr, lower.masklen);
/* get the last address of the upper prefix */
uint32_t hi = ip_broadcast_addr(upper.addr, upper.masklen);
*pfx_list = NULL;
return split_range(0, 0, lo, hi, pfx_list);
}
void ip_prefix_list_free(ip_prefix_list_t *pfx_list)
{
ip_prefix_list_t *temp = pfx_list;
while(temp != NULL)
{
temp = pfx_list->next;
pfx_list->next = NULL;
free(pfx_list);
pfx_list = temp;
}
}