This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsa.c
391 lines (341 loc) · 7.62 KB
/
sa.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* @file sa.c Socket address Testcode
*
* Copyright (C) 2010 Creytiv.com
*/
#include <string.h>
#include <re.h>
#include "test.h"
#define DEBUG_MODULE "test_sa"
#define DEBUG_LEVEL 5
#include <re_dbg.h>
int test_sa_cmp(void)
{
const struct {
const char *host1;
uint16_t port1;
const char *host2;
uint16_t port2;
bool eq;
} testv[] = {
{
"unix:/test.sock", 0,
"unix:/test.sock", 0,
true
},
{
"1.2.3.4", 12345,
"1.2.3.4", 12345,
true
},
{
"1.2.3.4", 12345,
"1.2.3.5", 12345,
false
},
{
"1.2.3.4", 12345,
"1.2.3.4", 12344,
false
},
#ifdef HAVE_INET6
{
"0:0:0:0:0:0:0:0", 123,
"::", 123,
true
},
{
"0:0:0:0:0:0:0:1", 123,
"::1", 123,
true
},
{
"0:0:0:0:0:0:1:1", 123,
"::1", 123,
false
},
{
"2001:0:53aa:64c:0:fbff:ab2e:1eac", 2001,
"2001:0000:53aa:064c:0000:fbff:ab2e:1eac", 2001,
true
},
{
"3001:0:53aa:64c:0:fbff:ab2e:1eac", 2001,
"2001:0000:53aa:064c:0000:fbff:ab2e:1eac", 2001,
false
},
{
"192.168.1.1", 123,
"2001:0000:53aa:064c:0000:fbff:ab2e:1eac", 123,
false
},
{ /* IPv6-mapped IPv4-address */
"::ffff:208.68.208.201", 3478,
"208.68.208.201", 3478,
true
},
{
"fe80::215:58ff:fe2d:90ab", 3333,
"fe80:0000:0000:0000:0215:58ff:fe2d:90ab", 3333,
true
},
{
"fe80::215:58ff:fe2d:90ab", 3333,
"fe80:0000:0000:0000:1215:58ff:fe2d:90ab", 3333,
false
},
#endif
};
size_t i;
int err = 0;
for (i=0; i<RE_ARRAY_SIZE(testv); i++) {
struct sa sa1, sa2;
bool eq;
err = sa_set_str(&sa1, testv[i].host1, testv[i].port1);
if (err)
break;
err = sa_set_str(&sa2, testv[i].host2, testv[i].port2);
if (err)
break;
eq = sa_cmp(&sa1, &sa2, SA_ALL);
if (!testv[i].eq == !eq)
continue;
DEBUG_WARNING("sa cmp %u: (%J) (%J) expected (%d),"
" got (%d)\n", i, &sa1, &sa2,
testv[i].eq, eq);
return EINVAL;
}
return err;
}
int test_sa_decode(void)
{
const struct {
int err;
int af;
const char *str;
const char *addr;
uint16_t port;
} testv[] = {
{0, AF_INET, "1.2.3.4:1234", "1.2.3.4", 1234},
{0, AF_INET, "1.2.3.4:0", "1.2.3.4", 0},
{EINVAL, AF_INET, "1.2.3.4", "", 0},
{EINVAL, AF_INET, "1.2.3.4.:1234", "", 0},
#ifdef HAVE_INET6
{0, AF_INET6, "[::1]:1", "::1", 1},
{0, AF_INET6, "[fe80::215:58ff:fe2d:90ab]:3333",
"fe80::215:58ff:fe2d:90ab", 3333},
{EINVAL, AF_INET6, "[::1]", "", 0},
#endif
};
uint32_t i;
int err = 0;
for (i=0; i<RE_ARRAY_SIZE(testv); i++) {
struct sa sa, sa2;
char buf[64];
int e;
e = sa_decode(&sa, testv[i].str, strlen(testv[i].str));
if (testv[i].err != e) {
DEBUG_WARNING("sa_decode: test %u:"
" expected (%m) got (%m) [%s]\n", i,
testv[i].err, e, testv[i].str);
err = EINVAL;
break;
}
if (e)
continue;
if (testv[i].af != sa_af(&sa)) {
DEBUG_WARNING("sa_decode: af mismatch %d != %d\n",
testv[i].af, sa_af(&sa));
err = EINVAL;
break;
}
err = sa_set_str(&sa2, testv[i].addr, testv[i].port);
if (err)
break;
if (!sa_cmp(&sa, &sa2, SA_ALL)) {
DEBUG_WARNING("sa_decode: sa_cmp() failed\n");
err = EINVAL;
break;
}
(void)re_snprintf(buf, sizeof(buf), "%J", &sa);
if (0 != strcmp(testv[i].str, buf)) {
DEBUG_WARNING("%u: strcmp: %s != %s\n",
testv[i].str, buf);
err = EINVAL;
break;
}
}
return err;
}
/* Test classification of loopback and link-local IP address */
int test_sa_class(void)
{
const struct {
bool lo;
bool ll;
bool any;
const char *addr;
} testv[] = {
{false, false, true, "0.0.0.0"},
{false, false, false, "1.2.3.4"},
{true, false, false, "127.0.0.0"},
{true, false, false, "127.0.0.1"},
{true, false, false, "127.3.0.3"},
{false, true, false, "169.254.1.2"},
#ifdef HAVE_INET6
{false, false, true, "::"},
{true, false, false, "::1"},
{false, true, false, "fe80::215:58ff:fe2d:90ab"},
{false, false, false, "2610:a0:c779:b::d1ad:35b4"}
#endif
};
uint32_t i;
int err = 0;
for (i=0; i<RE_ARRAY_SIZE(testv); i++) {
struct sa sa;
int lo, ll, any;
err = sa_set_str(&sa, testv[i].addr, 0);
if (err)
goto out;
lo = sa_is_loopback(&sa);
if ((int)testv[i].lo != lo) {
DEBUG_WARNING("%u: %s: loopback mismatch %d!=%d\n",
i, testv[i].addr, testv[i].lo, lo);
err = EINVAL;
goto out;
}
ll = sa_is_linklocal(&sa);
if ((int)testv[i].ll != ll) {
DEBUG_WARNING("%u: %s: linklocal mismatch %d!=%d\n",
i, testv[i].addr, testv[i].ll, ll);
err = EINVAL;
goto out;
}
if (ll && sa_af(&sa)==AF_INET6) {
sa_set_scopeid(&sa, 2);
TEST_EQUALS(2, sa_scopeid(&sa));
}
any = sa_is_any(&sa);
if ((int)testv[i].any != any) {
DEBUG_WARNING("%u: %s: any mismatch %d!=%d\n",
i, testv[i].addr, testv[i].any, any);
err = EINVAL;
goto out;
}
}
#if 0
{
struct sa sax;
TEST_ASSERT(sizeof(sax.u) <= sizeof(sax.u.padding));
}
#endif
out:
return err;
}
int test_sa_ntop(void)
{
const struct {
int af;
const char *addr;
} testv[] = {
{AF_INET, "0.0.0.0"},
{AF_INET, "1.2.3.4"},
{AF_INET, "255.254.253.128"},
#ifdef HAVE_INET6
{AF_INET6, "::1"},
{AF_INET6, "fe80::215:58ff:fe2d:90ab"},
{AF_INET6, "2610:a0:c779:b::d1ad:35b4"}
#endif
};
uint32_t i;
int err = 0;
for (i=0; i<RE_ARRAY_SIZE(testv); i++) {
struct sa sa0, sa;
char buf[64];
err = sa_set_str(&sa0, testv[i].addr, 0);
if (err)
break;
if (testv[i].af != sa_af(&sa0)) {
DEBUG_WARNING("ntop: af mismatch %d != %d\n",
testv[i].af, sa_af(&sa0));
err = EINVAL;
break;
}
err = sa_ntop(&sa0, buf, 2);
TEST_NOT_EQUALS(0, err);
err = sa_ntop(&sa0, buf, sizeof(buf));
if (err)
break;
if (0 != strcmp(buf, testv[i].addr)) {
DEBUG_WARNING("ntop: addr mismatch (%s) != (%s)\n",
testv[i].addr, buf);
err = EINVAL;
break;
}
err = sa_set_sa(&sa, &sa0.u.sa);
if (err) {
DEBUG_WARNING("sa_set_sa: %m\n", err);
break;
}
if (testv[i].af != sa_af(&sa)) {
err = EINVAL;
DEBUG_WARNING("af mismatch (test=%d sa=%d)\n",
testv[i].af, sa_af(&sa));
break;
}
}
out:
return err;
}
int test_sa_pton(void)
{
struct sa sa;
int err = 0;
const struct {
const char *addr;
int err;
} testv[] = {
{"github.com", EINVAL },
{"6002", EINVAL },
{"ga01::3a28", EINVAL },
{"fa01::2a29", 0 },
{"127.0.0.1", 0 },
{"192.168.110.2", 0 },
{"unix:/test.sock", 0 },
{"fe80::xxxx:d8d9:ddc3:25dd:%eth0", EADDRNOTAVAIL},
};
for (size_t i=0; i<RE_ARRAY_SIZE(testv); i++) {
int e = sa_pton(testv[i].addr, &sa);
TEST_EQUALS(testv[i].err, e);
}
out:
return err;
}
int test_sa_pton_linklocal(void)
{
const char test_ipv6ll_scope[] = "fe80::3a28:d8d9:ddc3:25dd%";
char buf[256];
struct sa sa_default_ip, sa;
int err;
#ifndef WIN32
char ifname[64];
#endif
if (0 != net_if_getlinklocal(NULL, AF_INET6, &sa_default_ip))
return ESKIPPED;
/* Use IPv4 since not all test systems have a default IPv6 route */
net_default_source_addr_get(AF_INET, &sa_default_ip);
#ifdef WIN32
re_snprintf(buf, sizeof(buf), "%s%d",
test_ipv6ll_scope, sa_scopeid(&sa_default_ip));
#else
net_if_getname(ifname, sizeof(ifname), AF_INET, &sa_default_ip);
re_snprintf(buf, sizeof(buf), "%s%s",
test_ipv6ll_scope, ifname);
#endif
err = sa_pton(buf, &sa);
TEST_ERR(err);
ASSERT_EQ(AF_INET6, sa_af(&sa));
ASSERT_TRUE(sa_is_linklocal(&sa));
out:
return err;
}