File: test-common.c

package info (click to toggle)
kronosnet 1.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: ansic: 25,419; sh: 5,295; makefile: 664
file content (220 lines) | stat: -rw-r--r-- 4,008 bytes parent folder | download
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
/*
 * Copyright (C) 2018-2025 Red Hat, Inc.  All rights reserved.
 *
 * Author: Fabio M. Di Nitto <fabbione@kronosnet.org>
 *
 * This software licensed under GPL-2.0+
 */

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <ifaddrs.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef KNET_BSD
#include <sys/ioctl.h>
#include <net/if_tap.h>
#endif
#ifdef KNET_SOLARIS
#include <libdlpi.h>
#endif
#include "test-common.h"

void need_root(void)
{
	if (geteuid() != 0) {
		printf("This test requires root privileges\n");
		exit(SKIP);
	}
}

void need_tun(void)
{
	int fd;
#ifdef KNET_LINUX
	const char *tundev = "/dev/net/tun";
#endif
#ifdef KNET_BSD
	const char *tundev = "/dev/tap";
	struct ifreq ifr;
#endif
#ifdef KNET_SOLARIS
	const char *tundev = "/dev/tun";
#endif
#if defined(KNET_BSD) || defined(KNET_SOLARIS)
	int ioctlfd = socket(AF_LOCAL, SOCK_DGRAM, 0);

	if (ioctlfd < 0) {
		printf("Unable to init ioctlfd (errno=%d)\n", errno);
		exit(FAIL);
	}
#endif

	fd = open(tundev, O_RDWR);
	if (fd < 0) {
		printf("Failed to open %s (errno=%d); this test requires TUN support\n", tundev, errno);
#if defined(KNET_BSD) || defined(KNET_SOLARIS)
		close(ioctlfd);
#endif
		exit(SKIP);
	}
#ifdef KNET_BSD
	memset(&ifr, 0, sizeof(struct ifreq));
	ioctl(fd, TAPGIFNAME, &ifr);
#endif
	close(fd);
#ifdef KNET_BSD
	ioctl(ioctlfd, SIOCIFDESTROY, &ifr);
	ioctl(ioctlfd, SIOCGIFFLAGS, &ifr);
	close(ioctlfd);
#endif
}

int test_iface(char *name, size_t size, const char *updownpath)
{
	nozzle_t nozzle;

	nozzle=nozzle_open(name, size, updownpath);
	if (!nozzle) {
		printf("Unable to open nozzle (errno=%d).\n", errno);
		return -1;
	}
	printf("Created interface: %s\n", name);

	if (is_if_in_system(name) > 0) {
		printf("Found interface %s on the system\n", name);
	} else {
		printf("Unable to find interface %s on the system\n", name);
	}

	if (!nozzle_get_handle_by_name(name)) {
		printf("Unable to find interface %s in nozzle db\n", name);
	} else {
		printf("Found interface %s in nozzle db\n", name);
	}

	nozzle_close(nozzle);

	if (is_if_in_system(name) == 0)
		printf("Successfully removed interface %s from the system\n", name);

	return 0;
}

int is_if_in_system(char *name)
{
#ifdef KNET_SOLARIS
	dlpi_handle_t dlpi_handle;

	int err = dlpi_open(name, &dlpi_handle, 0);
	if (err != DLPI_SUCCESS) {
		return 0;
	}
	dlpi_close(dlpi_handle);
	return 1;
#else
	struct ifaddrs *ifap = NULL;
	struct ifaddrs *ifa;
	int found = 0;

	if (getifaddrs(&ifap) < 0) {
		printf("Unable to get interface list.\n");
		return -1;
	}

	ifa = ifap;

	while (ifa) {
		if (!strncmp(name, ifa->ifa_name, IFNAMSIZ)) {
			found = 1;
			break;
		}
		ifa=ifa->ifa_next;
	}

	freeifaddrs(ifap);
	return found;
#endif
}

int get_random_byte(void)
{
	pid_t mypid;
	uint8_t *pid;
	uint8_t randombyte = 0;
	uint8_t i;

	if (sizeof(pid_t) < 4) {
		printf("pid_t is smaller than 4 bytes?\n");
		exit(77);
	}

	mypid = getpid();
	pid = (uint8_t *)&mypid;

	for (i = 0; i < sizeof(pid_t); i++) {
		if (pid[i] == 0) {
			pid[i] = 128;
		}
	}

	randombyte = pid[1];

	return randombyte;
}

void make_local_ips(char *testipv4_1, char *testipv4_2, char *testipv6_1, char *testipv6_2)
{
	pid_t mypid;
	uint8_t *pid;
	uint8_t i;

	memset(testipv4_1, 0, IPBUFSIZE);
	memset(testipv4_2, 0, IPBUFSIZE);
	memset(testipv6_1, 0, IPBUFSIZE);
	memset(testipv6_2, 0, IPBUFSIZE);

	mypid = getpid();
	pid = (uint8_t *)&mypid;

	for (i = 0; i < sizeof(pid_t); i++) {
		if ((pid[i] == 0) || (pid[i] == 255)) {
			pid[i] = 128;
		}
	}

	snprintf(testipv4_1,
		 IPBUFSIZE - 1,
		 "127.%u.%u.%u",
		 pid[1],
		 pid[2],
		 pid[0]);

	snprintf(testipv4_2,
		 IPBUFSIZE - 1,
		 "127.%u.%d.%u",
		 pid[1],
		 pid[2]+1,
		 pid[0]);

	snprintf(testipv6_1,
		 IPBUFSIZE - 1,
		 "fe%02x:%x%x::1",
		 pid[1] & 0x7f,
		 pid[2],
		 pid[0]);

	snprintf(testipv6_2,
		 IPBUFSIZE - 1,
		 "fe%02x:%x%x:1::1",
		 pid[1] & 0x7f,
		 pid[2],
		 pid[0]);
}