xref: /freebsd/tests/sys/netinet/libalias/util.c (revision 7b8696bf128754712a24ba98ce2d88eed2ee68dc)
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 #define REQUIRE(x)	do {				\
42 	if (!(x)) {					\
43 		fprintf(stderr, "Failed in %s %s:%d.\n",\
44 		    __FUNCTION__, __FILE__, __LINE__);	\
45 		exit(-1);				\
46 	}						\
47 } while(0)
48 
49 int
50 randcmp(const void *a, const void *b)
51 {
52 	int res, r = rand();
53 
54 	(void)a;
55 	(void)b;
56 	res = (r/4 < RAND_MAX/9) ? 1
57 	    : (r/5 < RAND_MAX/9) ? 0
58 	    : -1;
59 	return (res);
60 }
61 
62 void
63 hexdump(void *p, size_t len)
64 {
65 	size_t i;
66 	unsigned char *c = p;
67 
68 	for (i = 0; i < len; i++) {
69 		printf(" %02x", c[i]);
70 		switch (i & 0xf) {
71 		case 0xf: printf("\n"); break;
72 		case 0x7: printf(" "); break;
73 		default:  break;
74 		}
75 	}
76 	if ((i & 0xf) != 0x0)
77 		printf("\n");
78 }
79 
80 struct ip *
81 ip_packet(struct in_addr src, struct in_addr dst, u_char protocol, size_t len)
82 {
83 	struct ip * p;
84 
85 	REQUIRE(len >= 64 && len <= IP_MAXPACKET);
86 
87 	p = calloc(1, len);
88 	REQUIRE(p != NULL);
89 
90 	p->ip_v = IPVERSION;
91 	p->ip_hl = sizeof(*p)/4;
92 	p->ip_len = htons(len);
93 	p->ip_ttl = IPDEFTTL;
94 	p->ip_src = src;
95 	p->ip_dst = dst;
96 	p->ip_p = protocol;
97 	REQUIRE(p->ip_hl == 5);
98 
99 	return (p);
100 }
101 
102 struct udphdr *
103 set_udp(struct ip *p, u_short sport, u_short dport) {
104 	uint32_t *up = (void *)p;
105 	struct udphdr *u = (void *)&(up[p->ip_hl]);
106 	int payload = ntohs(p->ip_len) - 4*p->ip_hl;
107 
108 	REQUIRE(payload >= (int)sizeof(*u));
109 	p->ip_p = IPPROTO_UDP;
110 	u->uh_sport = htons(sport);
111 	u->uh_dport = htons(dport);
112 	u->uh_ulen = htons(payload);
113 	return (u);
114 }
115