source: rtems-libbsd/testsuite/include/rtems/bsd/test/default-network-init.h @ 8bd38d6

55-freebsd-126-freebsd-12
Last change on this file since 8bd38d6 was 8bd38d6, checked in by Sebastian Huber <sebastian.huber@…>, on 05/02/18 at 06:58:48

dhcpcd: Add rtems_dhcpcd_start()

Use it throughout to start the DHCP client (dhcpcd).

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[d617293]1/*
[7831313]2 * Copyright (c) 2013-2014 embedded brains GmbH.  All rights reserved.
[d617293]3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
[917a78b]32#include <sys/stat.h>
[27aedffc]33#include <sys/socket.h>
[917a78b]34
[defe035]35#include <net/if.h>
36
[d617293]37#include <assert.h>
[917a78b]38#include <fcntl.h>
[defe035]39#include <ifaddrs.h>
[d617293]40#include <stdio.h>
41#include <stdlib.h>
[5cd6a53]42#include <sysexits.h>
[d617293]43
44#include <machine/rtems-bsd-commands.h>
45
46#include <rtems.h>
[32fd702]47#include <rtems/printer.h>
[d617293]48#include <rtems/stackchk.h>
49#include <rtems/bsd/bsd.h>
[338f300]50#include <rtems/bsd/modules.h>
[8bd38d6]51#include <rtems/dhcpcd.h>
[d617293]52
[917a78b]53#if defined(DEFAULT_NETWORK_DHCPCD_ENABLE) && \
54    !defined(DEFAULT_NETWORK_NO_STATIC_IFCONFIG)
55#define DEFAULT_NETWORK_NO_STATIC_IFCONFIG
56#endif
57
[defe035]58#ifndef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
[ee9991f]59#include <rtems/bsd/test/network-config.h>
[defe035]60#endif
61
[31ab470]62#ifdef DEFAULT_NETWORK_SHELL
63#include <rtems/console.h>
64#include <rtems/shell.h>
65#endif
66
[d617293]67static void
68default_network_set_self_prio(rtems_task_priority prio)
69{
70        rtems_status_code sc;
71
72        sc = rtems_task_set_priority(RTEMS_SELF, prio, &prio);
73        assert(sc == RTEMS_SUCCESSFUL);
74}
75
[675acb2]76#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
[d617293]77static void
[defe035]78default_network_ifconfig_hwif0(char *ifname)
[d617293]79{
80        int exit_code;
[defe035]81        char *ifcfg[] = {
[d617293]82                "ifconfig",
[defe035]83                ifname,
84#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
85                "up",
86#else
[d617293]87                "inet",
88                NET_CFG_SELF_IP,
89                "netmask",
90                NET_CFG_NETMASK,
[defe035]91#endif
[d617293]92                NULL
93        };
94
[defe035]95        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(ifcfg), ifcfg);
[d617293]96        assert(exit_code == EX_OK);
97}
98
99static void
[defe035]100default_network_route_hwif0(char *ifname)
[d617293]101{
[defe035]102#ifndef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
[d617293]103        int exit_code;
104        char *dflt_route[] = {
105                "route",
106                "add",
107                "-host",
108                NET_CFG_GATEWAY_IP,
109                "-iface",
[defe035]110                ifname,
[d617293]111                NULL
112        };
113        char *dflt_route2[] = {
114                "route",
115                "add",
116                "default",
117                NET_CFG_GATEWAY_IP,
118                NULL
119        };
120
[7831313]121        exit_code = rtems_bsd_command_route(RTEMS_BSD_ARGC(dflt_route), dflt_route);
[d617293]122        assert(exit_code == EXIT_SUCCESS);
123
[7831313]124        exit_code = rtems_bsd_command_route(RTEMS_BSD_ARGC(dflt_route2), dflt_route2);
[d617293]125        assert(exit_code == EXIT_SUCCESS);
[defe035]126#endif
[d617293]127}
[04a64b1]128#endif
[d617293]129
[917a78b]130static void
[8bd38d6]131default_network_dhcpcd(void)
[917a78b]132{
[8bd38d6]133#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
[b1404f2]134        static const char default_cfg[] = "clientid libbsd test client\n";
[8bd38d6]135        rtems_status_code sc;
[917a78b]136        int fd;
137        int rv;
138        ssize_t n;
139
140        fd = open("/etc/dhcpcd.conf", O_CREAT | O_WRONLY,
141            S_IRWXU | S_IRWXG | S_IRWXO);
142        assert(fd >= 0);
143
[b1404f2]144        n = write(fd, default_cfg, sizeof(default_cfg));
145        assert(n == (ssize_t) sizeof(default_cfg));
146
147#ifdef DEFAULT_NETWORK_DHCPCD_NO_DHCP_DISCOVERY
148        static const char nodhcp_cfg[] = "nodhcp\nnodhcp6\n";
149
150        n = write(fd, nodhcp_cfg, sizeof(nodhcp_cfg));
151        assert(n == (ssize_t) sizeof(nodhcp_cfg));
152#endif
[917a78b]153
154        rv = close(fd);
155        assert(rv == 0);
156
[8bd38d6]157        sc = rtems_dhcpcd_start(NULL);
[917a78b]158        assert(sc == RTEMS_SUCCESSFUL);
159#endif
160}
161
[d617293]162static void
163default_network_on_exit(int exit_code, void *arg)
164{
[92e9493]165        rtems_printer printer;
166
[27aedffc]167        (void)arg;
[92e9493]168
169        rtems_print_printer_printf(&printer);
170        rtems_stack_checker_report_usage_with_plugin(&printer);
[d617293]171
172        if (exit_code == 0) {
173                puts("*** END OF TEST " TEST_NAME " ***");
174        }
175}
176
177static void
178Init(rtems_task_argument arg)
179{
180        rtems_status_code sc;
[675acb2]181#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
[defe035]182#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
183        char ifnamebuf[IF_NAMESIZE];
184#endif
185        char *ifname;
[04a64b1]186#endif
[d617293]187
[b8c7bf9]188        /*
189         * Default the syslog priority to 'debug' to aid developers.
190         */
191        rtems_bsd_setlogpriority("debug");
192
[27aedffc]193        (void)arg;
[d617293]194        puts("*** " TEST_NAME " TEST ***");
195
196        on_exit(default_network_on_exit, NULL);
197
[f8367a1]198#ifdef DEFAULT_EARLY_INITIALIZATION
199        early_initialization();
200#endif
201
[d617293]202        /* Let other tasks run to complete background work */
[27aedffc]203        default_network_set_self_prio(RTEMS_MAXIMUM_PRIORITY - 1U);
[d617293]204
[31ab470]205#ifdef DEFAULT_NETWORK_SHELL
206        sc = rtems_shell_init(
207                "SHLL",
208                32 * 1024,
209                1,
210                CONSOLE_DEVICE_NAME,
211                false,
212                false,
213                NULL
214        );
215        assert(sc == RTEMS_SUCCESSFUL);
216#endif
217
[e51bc97]218        rtems_bsd_initialize();
[d617293]219
[675acb2]220#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
[defe035]221#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
222        ifname = if_indextoname(1, &ifnamebuf[0]);
223        assert(ifname != NULL);
224#else
225        ifname = NET_CFG_INTERFACE_0;
[04a64b1]226#endif
[defe035]227#endif
228
[d617293]229        /* Let the callout timer allocate its resources */
230        sc = rtems_task_wake_after(2);
231        assert(sc == RTEMS_SUCCESSFUL);
232
[23cd284]233        rtems_bsd_ifconfig_lo0();
[675acb2]234#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
[defe035]235        default_network_ifconfig_hwif0(ifname);
236        default_network_route_hwif0(ifname);
[04a64b1]237#endif
[917a78b]238        default_network_dhcpcd();
[d617293]239
240        test_main();
241
242        assert(0);
243}
244
[f01b1f72]245/*
246 * Configure LibBSD.
247 */
[e49f301]248
249#if defined(LIBBSP_I386_PC386_BSP_H)
250#define RTEMS_BSD_CONFIG_DOMAIN_PAGE_MBUFS_SIZE (64 * 1024 * 1024)
251#elif defined(LIBBSP_POWERPC_QORIQ_BSP_H)
252#define RTEMS_BSD_CONFIG_DOMAIN_PAGE_MBUFS_SIZE (32 * 1024 * 1024)
253#endif
254
[f01b1f72]255#define RTEMS_BSD_CONFIG_NET_PF_UNIX
[46f0b8b]256#define RTEMS_BSD_CONFIG_NET_IP_MROUTE
257#define RTEMS_BSD_CONFIG_NET_IP6_MROUTE
[3360232]258#define RTEMS_BSD_CONFIG_NET_IF_BRIDGE
[f01b1f72]259#define RTEMS_BSD_CONFIG_NET_IF_LAGG
260#define RTEMS_BSD_CONFIG_NET_IF_VLAN
261#define RTEMS_BSD_CONFIG_BSP_CONFIG
262#define RTEMS_BSD_CONFIG_INIT
[164c5f5]263
[f01b1f72]264#include <machine/rtems-bsd-config.h>
[6cfc98d]265
[d617293]266#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
267#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
[defe035]268#define CONFIGURE_APPLICATION_NEEDS_STUB_DRIVER
269#define CONFIGURE_APPLICATION_NEEDS_ZERO_DRIVER
[fc9e83b]270#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
[d617293]271
272#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32
273
274#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1
275
276#define CONFIGURE_UNLIMITED_ALLOCATION_SIZE 32
277#define CONFIGURE_UNLIMITED_OBJECTS
278#define CONFIGURE_UNIFIED_WORK_AREAS
279
280#define CONFIGURE_STACK_CHECKER_ENABLED
281
[293fefa]282#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE (64 * 1024)
283#define CONFIGURE_BDBUF_MAX_READ_AHEAD_BLOCKS 4
284#define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (1 * 1024 * 1024)
285
[d617293]286#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
287
288#define CONFIGURE_INIT_TASK_STACK_SIZE (32 * 1024)
289#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
[4b8bc5c]290#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
[d617293]291
292#define CONFIGURE_INIT
293
294#include <rtems/confdefs.h>
[31ab470]295
296#ifdef DEFAULT_NETWORK_SHELL
297
298#define CONFIGURE_SHELL_COMMANDS_INIT
299
300#include <bsp/irq-info.h>
301
302#include <rtems/netcmds-config.h>
303
[338f300]304#ifdef RTEMS_BSD_MODULE_USER_SPACE_WLANSTATS
305  #define SHELL_WLANSTATS_COMMAND &rtems_shell_WLANSTATS_Command,
306#else
307  #define SHELL_WLANSTATS_COMMAND
308#endif
309
310#ifdef RTEMS_BSD_MODULE_USR_SBIN_WPA_SUPPLICANT
311  #define SHELL_WPA_SUPPLICANT_COMMAND &rtems_shell_WPA_SUPPLICANT_Command,
312#else
313  #define SHELL_WPA_SUPPLICANT_COMMAND
314#endif
315
[31ab470]316#define CONFIGURE_SHELL_USER_COMMANDS \
[338f300]317  SHELL_WLANSTATS_COMMAND \
318  SHELL_WPA_SUPPLICANT_COMMAND \
[31ab470]319  &bsp_interrupt_shell_command, \
[64c663c]320  &rtems_shell_ARP_Command, \
[50e82a6]321  &rtems_shell_HOSTNAME_Command, \
[31ab470]322  &rtems_shell_PING_Command, \
323  &rtems_shell_ROUTE_Command, \
324  &rtems_shell_NETSTAT_Command, \
[a1a2e10]325  &rtems_shell_IFCONFIG_Command, \
[83eb07c]326  &rtems_shell_TCPDUMP_Command, \
[c4e89a9]327  &rtems_shell_SYSCTL_Command, \
[338f300]328  &rtems_shell_VMSTAT_Command
[31ab470]329
[5be4f06]330#define CONFIGURE_SHELL_COMMAND_CPUINFO
[31ab470]331#define CONFIGURE_SHELL_COMMAND_CPUUSE
332#define CONFIGURE_SHELL_COMMAND_PERIODUSE
333#define CONFIGURE_SHELL_COMMAND_STACKUSE
[3cafb9a]334#define CONFIGURE_SHELL_COMMAND_PROFREPORT
[31ab470]335
336#define CONFIGURE_SHELL_COMMAND_CP
337#define CONFIGURE_SHELL_COMMAND_PWD
338#define CONFIGURE_SHELL_COMMAND_LS
339#define CONFIGURE_SHELL_COMMAND_LN
340#define CONFIGURE_SHELL_COMMAND_LSOF
341#define CONFIGURE_SHELL_COMMAND_CHDIR
342#define CONFIGURE_SHELL_COMMAND_CD
343#define CONFIGURE_SHELL_COMMAND_MKDIR
344#define CONFIGURE_SHELL_COMMAND_RMDIR
345#define CONFIGURE_SHELL_COMMAND_CAT
346#define CONFIGURE_SHELL_COMMAND_MV
347#define CONFIGURE_SHELL_COMMAND_RM
348#define CONFIGURE_SHELL_COMMAND_MALLOC_INFO
[c6713d1]349#define CONFIGURE_SHELL_COMMAND_SHUTDOWN
[31ab470]350
351#include <rtems/shellconfig.h>
352
353#endif /* DEFAULT_NETWORK_SHELL */
Note: See TracBrowser for help on using the repository browser.