File: ip_endpoint_check.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (177 lines) | stat: -rw-r--r-- 5,458 bytes parent folder | download | duplicates (2)
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
/* ip_endpoint tests, for libreswan
 *
 * Copyright (C) 2000  Henry Spencer.
 * Copyright (C) 2012 Paul Wouters <paul@libreswan.org>
 * Copyright (C) 2018 Andrew Cagney
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
 *
 * This library 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 Library General Public
 * License for more details.
 */

#include <stdio.h>
#include <string.h>

#include "lswcdefs.h"		/* for elemsof() */
#include "constants.h"		/* for streq() */
#include "ip_endpoint.h"
#include "ip_protocol.h"
#include "ipcheck.h"

void ip_endpoint_check(void)
{
	/*
	 * XXX: can't yet do invalid ports.
	 */
	static const struct test {
		int line;
		const struct ip_info *afi;
		const char *in;
		uint16_t hport;
		const char *str;
		uint8_t nport[2];
		bool is_unset;
		bool is_specified;
		bool is_any;
	} tests[] = {
		/* anything else? */
		{ LN, &ipv4_info, "1.2.3.4",	65535, "1.2.3.4:65535", { 255, 255, }, .is_specified = true, },
		{ LN, &ipv4_info, "255.255.255.255",	65535, "255.255.255.255:65535", { 255, 255, }, .is_specified = true, },
		{ LN, &ipv6_info, "1:12:3:14:5:16:7:18", 65535, "[1:12:3:14:5:16:7:18]:65535", { 255, 255, }, .is_specified = true, },
		{ LN, &ipv6_info, "11:22:33:44:55:66:77:88",	65535, "[11:22:33:44:55:66:77:88]:65535", { 255, 255, }, .is_specified = true, },

		/* treat special different ? */
		{ LN, &ipv4_info, "0.0.0.1", 65535, "0.0.0.1:65535", { 255, 255, }, .is_specified = true, },
		{ LN, &ipv6_info, "::1", 65535, "[::1]:65535", { 255, 255, }, .is_specified = true, },

		/* never suppress the port */
		{ LN, &ipv4_info, "0.0.0.0", 0, "0.0.0.0:0", { 0, 0, }, .is_any = true, },
		{ LN, &ipv6_info, "::", 0, "[::]:0", { 0, 0, }, .is_any = true, },
		/* not valid, hence not specified */
		{ LN, &ipv4_info, "0.0.0.0", 1, "0.0.0.0:1", { 0, 1, }, .is_specified = false, },
		{ LN, &ipv6_info, "::", 1, "[::]:1", { 0, 1, }, .is_specified = false, },

		/* longest */
		{ LN, &ipv4_info, "101.102.103.104", 65534, "101.102.103.104:65534", { 255, 254, }, .is_specified = true, },
		{ LN, &ipv6_info, "1001:1002:1003:1004:1005:1006:1007:1008", 65534, "[1001:1002:1003:1004:1005:1006:1007:1008]:65534", { 255, 254, }, .is_specified = true, },

	};

	const char *oops;

	for (size_t ti = 0; ti < elemsof(tests); ti++) {
		const struct test *t = &tests[ti];
		PRINT("%s '%s'%d->%s", pri_afi(t->afi), t->in, t->hport, t->str);

		const struct ip_info *type = t->afi;

		ip_address a;
		oops = ttoaddress_num(shunk1(t->in), type, &a);
		if (oops != NULL) {
			/* Error occurred, but we didn't expect one */
			FAIL("ttosubnet failed: %s", oops);
		}

		ip_endpoint e, *endpoint = &e;
		const struct ip_protocol *protocol = t->hport == 0 ? &ip_protocol_icmp : &ip_protocol_udp;
		if (t->is_specified) {
			e = endpoint_from_address_protocol_port(a, protocol,
								ip_hport(t->hport));
		} else {
			/*
			 * Construct the bogus endpoint by hand - the
			 * endpoint_from_*() code would pexpect().
			 */
			e = (ip_endpoint) {
				.is_set = true,
				.version = a.version,
				.bytes = a.bytes,
				.hport = t->hport,
				.ipproto = protocol->ipproto,
			};
		}

		CHECK_INFO(endpoint);

		/*
		 * str_endpoint() / jam_endpoint()
		 */
		CHECK_STR2(endpoint);

		CHECK_COND(endpoint, is_unset);
		CHECK_COND2(endpoint, is_specified);

		/*
		 * endpoint_*address()
		 */
		ip_address aout = endpoint_address(e);
		address_buf astr;
		if (!streq(str_address(&aout, &astr), t->in)) {
			FAIL("endpoint_address() returned %s, expecting %s",
				astr.buf, t->in);
		}

		/*
		 * endpoint_*port()
		 */

		/* host port */
		uint16_t heport = endpoint_hport(e);
		if (!memeq(&heport, &t->hport, sizeof(heport))) {
			FAIL("endpoint_hport() returned '%d', expected '%d'",
				heport, t->hport);
		}

		/* network port */
		uint16_t neport = nport(endpoint_port(e));
		if (!memeq(&neport, &t->nport, sizeof(neport))) {
			FAIL("endpoint_nport() returned '%04x', expected '%02x%02x'",
				neport, t->nport[0], t->nport[1]);
		}

		/* tweak the port numbers */
		uint16_t hport_plus_one = t->hport+1;
		uint16_t nport_plus_one = ntohs(t->hport+1);
		/* check math? handle carry; */
		uint8_t nport_plus_plus[2];
		memcpy(nport_plus_plus, t->nport, sizeof(nport_plus_plus));
		nport_plus_plus[1]++;
		if (nport_plus_plus[1] < t->nport[1])
			nport_plus_plus[0]++;
		if (!memeq(&nport_plus_one, nport_plus_plus, sizeof(nport_plus_one))) {
			FAIL("can't do basic math");
		}

		/* hport+1 -> nport+1 */
		ip_endpoint hp = set_endpoint_port(e, ip_hport(hport_plus_one));
		uint16_t nportp = nport(endpoint_port(hp));
		if (!memeq(&nportp, &nport_plus_one, sizeof(nportp))) {
			FAIL("endpoint_nport(set_endpoint_hport(+1)) returned '%04x', expected '%04x'",
				nportp, nport_plus_one);
		}

		/*
		 * endpoint_eq()
		 */
		if (!endpoint_eq_endpoint(e, e)) {
			FAIL("endpoint_eq(e, e) failed");
		}
		if (endpoint_eq_endpoint(e, hp)) {
			FAIL("endpoint_eq(e, e+1) succeeded");
		}

		/*
		 * endpoint_address_eq()
		 */
		if (!endpoint_address_eq_address(e, a)) {
			FAIL("endpoint_address_eq(e, a) failed");
		}

	}
}