xref: /freebsd/tests/sys/netinet/libalias/util.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright 2021 Lutz Donnerhacke
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above
13  *    copyright notice, this list of conditions and the following
14  *    disclaimer in the documentation and/or other materials provided
15  *    with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 #include <netinet/in.h>
38 
39 #include "util.h"
40 
41 /* common ip ranges */
42 struct in_addr masq = { htonl(0x01020304) };
43 struct in_addr pub  = { htonl(0x0102dead) };
44 struct in_addr prv1 = { htonl(0x0a00dead) };
45 struct in_addr prv2 = { htonl(0xac10dead) };
46 struct in_addr prv3 = { htonl(0xc0a8dead) };
47 struct in_addr cgn  = { htonl(0x6440dead) };
48 struct in_addr ext  = { htonl(0x12345678) };
49 struct in_addr ANY_ADDR = { 0 };
50 
51 #define REQUIRE(x)	do {				\
52 	if (!(x)) {					\
53 		fprintf(stderr, "Failed in %s %s:%d.\n",\
54 		    __FUNCTION__, __FILE__, __LINE__);	\
55 		exit(-1);				\
56 	}						\
57 } while(0)
58 
59 int
60 randcmp(const void *a, const void *b)
61 {
62 	int res, r = rand();
63 
64 	(void)a;
65 	(void)b;
66 	res = (r/4 < RAND_MAX/9) ? 1
67 	    : (r/5 < RAND_MAX/9) ? 0
68 	    : -1;
69 	return (res);
70 }
71 
72 void
73 hexdump(void *p, size_t len)
74 {
75 	size_t i;
76 	unsigned char *c = p;
77 
78 	for (i = 0; i < len; i++) {
79 		printf(" %02x", c[i]);
80 		switch (i & 0xf) {
81 		case 0xf: printf("\n"); break;
82 		case 0x7: printf(" "); break;
83 		default:  break;
84 		}
85 	}
86 	if ((i & 0xf) != 0x0)
87 		printf("\n");
88 }
89 
90 struct ip *
91 ip_packet(u_char protocol, size_t len)
92 {
93 	struct ip * p;
94 
95 	REQUIRE(len >= 64 && len <= IP_MAXPACKET);
96 
97 	p = calloc(1, len);
98 	REQUIRE(p != NULL);
99 
100 	p->ip_v = IPVERSION;
101 	p->ip_hl = sizeof(*p)/4;
102 	p->ip_len = htons(len);
103 	p->ip_ttl = IPDEFTTL;
104 	p->ip_p = protocol;
105 	REQUIRE(p->ip_hl == 5);
106 
107 	return (p);
108 }
109 
110 struct udphdr *
111 set_udp(struct ip *p, u_short sport, u_short dport) {
112 	int hlen = p->ip_hl << 2;
113 	struct udphdr *u = (struct udphdr *)((uintptr_t)p + hlen);
114 	int payload = ntohs(p->ip_len) - hlen;
115 
116 	REQUIRE(payload >= (int)sizeof(*u));
117 	p->ip_p = IPPROTO_UDP;
118 	u->uh_sport = htons(sport);
119 	u->uh_dport = htons(dport);
120 	u->uh_ulen = htons(payload);
121 	return (u);
122 }
123