summaryrefslogtreecommitdiffstats
path: root/testsuite/mcast01/test_main.c
blob: ad6c45b7c621b3b461f67068abc442725c38f3b6 (plain) (blame)
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
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @brief Example code to test multicasts.
 *
 * ping 224.0.0.1
 *
 * Linux:
 *
 * echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
 * echo 1 > /proc/sys/net/ipv4/ip_forward
 * ip route add 224.0.0.0/4 dev eth0
 * echo "Hello, world!" > /dev/udp/239.1.2.3/1234
 */

/*
 * Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/sysctl.h>

#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

#include <arpa/inet.h>
#include <netinet/in.h>

#include <machine/rtems-bsd-commands.h>

#include <rtems/console.h>
#include <rtems/shell.h>
#include <rtems/dhcpcd.h>

#define TEST_NAME "LIBBSD MCAST 1"
#define TEST_STATE_USER_INPUT 1

#define MCAST_PORT 1234

#define MAX_PACKET_SIZE UINT16_MAX

static const char mcast_addr[] = "239.1.2.3";

static const char hello[] = "Hello, world!";

typedef struct {
	in_addr_t addr;
	char name[];
} iface_binding;

static void
set_mcast_route(char *iface_name)
{
	static char net[] = "224.0.0.0/4";
	int exit_code;
	char *route[] = {
		"route",
		"add",
		"-net",
		net,
		"-iface",
		iface_name,
		NULL
	};

	exit_code = rtems_bsd_command_route(RTEMS_BSD_ARGC(route), route);
	assert(exit_code == EXIT_SUCCESS);
}

static void
mcast_rx_task(rtems_task_argument arg)
{
	iface_binding *ifb;
	struct ip_mreq mreq;
	int sd;
	struct sockaddr_in addr;
	int rv;
	char addr_buf[INET_ADDRSTRLEN];
	const char *ip;
	ssize_t n;

	ifb = (iface_binding *)arg;
	set_mcast_route(ifb->name);

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = ifb->addr;

	ip = inet_ntop(AF_INET, &addr.sin_addr, addr_buf, sizeof(addr_buf));
	printf("mcast: join %s using interface IP %s\n", mcast_addr, ip);

	sd = socket(PF_INET,  SOCK_DGRAM, IPPROTO_UDP);
	assert(sd >= 0);

	addr.sin_port = htons(MCAST_PORT);
	addr.sin_addr.s_addr = INADDR_ANY;

	rv = bind(sd, (const struct sockaddr *)&addr, sizeof(addr));
	assert(rv == 0);

	memset(&mreq, 0, sizeof(mreq));
	mreq.imr_multiaddr.s_addr = inet_addr(mcast_addr);
	mreq.imr_interface.s_addr = ifb->addr;

	rv = setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
	assert(rv == 0);

	addr.sin_family = AF_INET;
	addr.sin_port = htons(MCAST_PORT);
	addr.sin_addr.s_addr = inet_addr(mcast_addr);
	n = sendto(sd, hello, sizeof(hello), 0,
	    (const struct sockaddr *)&addr, sizeof(addr));

	if (n != (ssize_t)sizeof(hello)) {
		perror("send");
	}

	while (true) {
		socklen_t addr_len;
		char buf[MAX_PACKET_SIZE];

		addr_len = sizeof(addr);
		n = recvfrom(sd, buf, sizeof(buf), 0,
		    (struct sockaddr *)&addr, &addr_len);
		if (n >= 0 && addr.sin_addr.s_addr != ifb->addr) {
			size_t m;
			ip = inet_ntop(AF_INET, &addr.sin_addr, addr_buf,
			    sizeof(addr_buf));
			printf("mcast: received %zi bytes from %s\n", n, ip);

			addr.sin_port = htons(MCAST_PORT);
			addr.sin_addr.s_addr = inet_addr(mcast_addr);
			m = (size_t)n;
			n = sendto(sd, buf, m, 0,
			    (const struct sockaddr *)&addr,
			    sizeof(addr));
			assert(n == (ssize_t)m);
		} else if (n < 0 ) {
			perror("recvfrom");
		}
	}
}

static void
start_mcast_rx(const char *iface_ip, const char *iface_name)
{
	rtems_status_code sc;
	rtems_id id;
	iface_binding *ifb;
	size_t len;

	len = strlen(iface_name) + 1;
	ifb = malloc(sizeof(*ifb) + len);
	assert(ifb != NULL);
	ifb->addr = inet_addr(iface_ip);
	memcpy(ifb->name, iface_name, len);

	sc = rtems_task_create(rtems_build_name('M', 'C', 'R', 'X'),
	    RTEMS_MINIMUM_PRIORITY, RTEMS_MINIMUM_STACK_SIZE + MAX_PACKET_SIZE,
	    RTEMS_DEFAULT_MODES, RTEMS_FLOATING_POINT, &id);
	assert(sc == RTEMS_SUCCESSFUL);

	sc = rtems_task_start(id, mcast_rx_task, (rtems_task_argument)ifb);
	assert(sc == RTEMS_SUCCESSFUL);
}

static const char *
get_value(char *const *env, const char *key)
{
	size_t len;

	len = strlen(key);

	while (true) {
		const char *s;

		s = *env;

		if (s == NULL) {
			return "";
		}

		if (strncmp(key, s, len) == 0 && s[len] == '=') {
			return &s[len + 1];
		}

		++env;
	}
}

static void
dhcpcd_hook_handler(rtems_dhcpcd_hook *hook, char *const *env)
{

	(void)hook;

	if (strcmp(get_value(env, "reason"), "BOUND") == 0) {
		start_mcast_rx(get_value(env, "new_ip_address"),
		    get_value(env, "interface"));
	}
}

static rtems_dhcpcd_hook dhcpcd_hook = {
	.name = "mcast",
	.handler = dhcpcd_hook_handler
};

static void
test_main(void)
{
	rtems_status_code sc;
	int enable;
	int rv;

	rtems_dhcpcd_add_hook(&dhcpcd_hook);

	enable = 1;
	rv = sysctlbyname("net.inet.icmp.bmcastecho", NULL, NULL, &enable,
	    sizeof(enable));
	assert(rv == 0);

	sc = rtems_shell_init("SHLL", 16 * 1024, 1, CONSOLE_DEVICE_NAME,
	    false, true, NULL);
	assert(sc == RTEMS_SUCCESSFUL);

	exit(0);
}

#define DEFAULT_NETWORK_DHCPCD_ENABLE

#include <rtems/bsd/test/default-network-init.h>

#define CONFIGURE_SHELL_COMMANDS_INIT

#include <bsp/irq-info.h>

#include <rtems/netcmds-config.h>

#define CONFIGURE_SHELL_USER_COMMANDS \
  &bsp_interrupt_shell_command, \
  &rtems_shell_ARP_Command, \
  &rtems_shell_HOSTNAME_Command, \
  &rtems_shell_PING_Command, \
  &rtems_shell_ROUTE_Command, \
  &rtems_shell_NETSTAT_Command, \
  &rtems_shell_SYSCTL_Command, \
  &rtems_shell_IFCONFIG_Command, \
  &rtems_shell_IFMCSTAT_Command, \
  &rtems_shell_VMSTAT_Command

#define CONFIGURE_SHELL_COMMAND_CPUINFO
#define CONFIGURE_SHELL_COMMAND_CPUUSE
#define CONFIGURE_SHELL_COMMAND_PERIODUSE
#define CONFIGURE_SHELL_COMMAND_STACKUSE
#define CONFIGURE_SHELL_COMMAND_MALLOC_INFO

#include <rtems/shellconfig.h>