forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmassip-addr.c
284 lines (235 loc) · 6.65 KB
/
massip-addr.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "massip-addr.h"
#include <string.h>
/**
* Holds the output string, so that we can append to it without
* overflowing buffers. The _append_xxx() functions below append
* to this string.
*/
typedef struct stream_t {
char *buf;
size_t offset;
size_t length;
} stream_t;
/**
* Append a character to the output string. All the other _append_xxx()
* functions call this one, so this is the only one where a
* buffer-overflow can occur.
*/
static void
_append_char(stream_t *out, char c)
{
if (out->offset < out->length)
out->buf[out->offset++] = c;
/* keep the string nul terminated as we build it */
if (out->offset < out->length)
out->buf[out->offset] = '\0';
}
static void
_append_ipv6(stream_t *out, const unsigned char *ipv6)
{
static const char hex[] = "0123456789abcdef";
size_t i;
int is_ellision = 0;
/* An IPv6 address is printed as a series of 2-byte hex words
* separated by colons :, for a total of 16-bytes */
for (i = 0; i < 16; i += 2) {
unsigned n = ipv6[i] << 8 | ipv6[i + 1];
/* Handle the ellision case. A series of words with a value
* of 0 can be removed completely, replaced by an extra colon */
if (n == 0 && !is_ellision) {
is_ellision = 1;
while (i < 13 && ipv6[i + 2] == 0 && ipv6[i + 3] == 0)
i += 2;
_append_char(out, ':');
/* test for all-zero address, in which case the output
* will be "::". */
while (i == 14 && ipv6[i] == 0 && ipv6[i + 1] == 0){
i=16;
_append_char(out, ':');
}
continue;
}
/* Print the colon between numbers. Fence-post alert: only colons
* between numbers are printed, not at the beginning or end of the
* string */
if (i)
_append_char(out, ':');
/* Print the digits. Leading zeroes are not printed */
if (n >> 12)
_append_char(out, hex[(n >> 12) & 0xF]);
if (n >> 8)
_append_char(out, hex[(n >> 8) & 0xF]);
if (n >> 4)
_append_char(out, hex[(n >> 4) & 0xF]);
_append_char(out, hex[(n >> 0) & 0xF]);
}
}
struct ipaddress_formatted ipv6address_fmt(ipv6address a)
{
struct ipaddress_formatted out;
unsigned char tmp[16];
size_t i;
stream_t s;
/*
* Convert address into a sequence of bytes. Our code
* here represents an IPv6 address as two 64-bit numbers, but
* the formatting code above that we copied from a different
* project represents it as an array of bytes.
*/
for (i=0; i<16; i++) {
uint64_t x;
if (i<8)
x = a.hi;
else
x = a.lo;
x >>= (7 - (i%8)) * 8;
tmp[i] = (unsigned char)(x & 0xFF);
}
/* Call the formatting function */
s.buf = out.string;
s.offset = 0;
s.length = sizeof(out.string);
_append_ipv6(&s, tmp);
return out;
}
/**
* Append a decimal integer.
*/
static void
_append_decimal(stream_t *out, unsigned long long n)
{
char tmp[64];
size_t tmp_offset = 0;
/* Create temporary string */
while (n >= 10) {
unsigned digit = n % 10;
n /= 10;
tmp[tmp_offset++] = (char)('0' + digit);
}
/* the final digit, may be zero */
tmp[tmp_offset++] = (char)('0' + n);
/* Copy the result backwards */
while (tmp_offset)
_append_char(out, tmp[--tmp_offset]);
}
static void
_append_hex2(stream_t *out, unsigned long long n)
{
static const char hex[17] = "0123456789abcdef";
_append_char(out, hex[(n>>4)&0xF]);
_append_char(out, hex[(n>>0)&0xF]);
}
struct ipaddress_formatted ipv4address_fmt(ipv4address ip)
{
struct ipaddress_formatted out;
stream_t s;
/* Call the formatting function */
s.buf = out.string;
s.offset = 0;
s.length = sizeof(out.string);
_append_decimal(&s, (ip >> 24) & 0xFF);
_append_char(&s, '.');
_append_decimal(&s, (ip >> 16) & 0xFF);
_append_char(&s, '.');
_append_decimal(&s, (ip >> 8) & 0xFF);
_append_char(&s, '.');
_append_decimal(&s, (ip >> 0) & 0xFF);
return out;
}
struct ipaddress_formatted macaddress_fmt(macaddress_t mac)
{
struct ipaddress_formatted out;
stream_t s;
/* Call the formatting function */
s.buf = out.string;
s.offset = 0;
s.length = sizeof(out.string);
_append_hex2(&s, mac.addr[0]);
_append_char(&s, '-');
_append_hex2(&s, mac.addr[1]);
_append_char(&s, '-');
_append_hex2(&s, mac.addr[2]);
_append_char(&s, '-');
_append_hex2(&s, mac.addr[3]);
_append_char(&s, '-');
_append_hex2(&s, mac.addr[4]);
_append_char(&s, '-');
_append_hex2(&s, mac.addr[5]);
return out;
}
struct ipaddress_formatted ipaddress_fmt(ipaddress a)
{
struct ipaddress_formatted out;
stream_t s;
ipv4address ip = a.ipv4;
if (a.version == 6) {
return ipv6address_fmt(a.ipv6);
}
/* Call the formatting function */
s.buf = out.string;
s.offset = 0;
s.length = sizeof(out.string);
_append_decimal(&s, (ip >> 24) & 0xFF);
_append_char(&s, '.');
_append_decimal(&s, (ip >> 16) & 0xFF);
_append_char(&s, '.');
_append_decimal(&s, (ip >> 8) & 0xFF);
_append_char(&s, '.');
_append_decimal(&s, (ip >> 0) & 0xFF);
return out;
}
static unsigned _count_long(uint64_t number)
{
unsigned i;
unsigned count = 0;
for (i=0; i<64; i++) {
if ((number >> i) & 1)
count = i + 1;
}
return count;
}
unsigned massint128_bitcount(massint128_t number)
{
if (number.hi)
return _count_long(number.hi) + 64;
else
return _count_long(number.lo);
}
int ipv6address_selftest(void)
{
int x = 0;
ipaddress ip;
struct ipaddress_formatted fmt;
ip.version = 4;
ip.ipv4 = 0x01FF00A3;
fmt = ipaddress_fmt(ip);
if (strcmp(fmt.string, "1.255.0.163") != 0)
x++;
return x;
}
int ipv6address_is_equal_prefixed(ipv6address_t lhs, ipv6address_t rhs, unsigned prefix)
{
ipv6address mask;
/* If the prefix is bad, then the answer is 'no'. */
if (prefix > 128) {
return 0;
}
/* Create the mask from the prefix */
if (prefix > 64)
mask.hi = ~0ULL;
else if (prefix == 0)
mask.hi = 0;
else
mask.hi = ~0ULL << (64 - prefix);
if (prefix > 64)
mask.lo = ~0ULL << (128 - prefix);
else
mask.lo = 0;
/* Mask off any non-zero bits from both addresses */
lhs.hi &= mask.hi;
lhs.lo &= mask.lo;
rhs.hi &= mask.hi;
rhs.lo &= mask.lo;
/* Now do a normal compare */
return ipv6address_is_equal(lhs, rhs);
}