summaryrefslogtreecommitdiffstats
path: root/stack/libbsd/net_adapter.c
blob: 3760c8966a9baeda51baecb47d2ac1d8cdd8acac (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
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @brief This file contains the adapter necessary for network services tests to
 *        run on lwIP.
 */

/*
 * COPYRIGHT (c) 2023. On-Line Applications Research Corporation (OAR).
 * All rights reserved.
 *
 * 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 AUTHOR 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 AUTHOR 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 <net_adapter.h>
#include <stddef.h>
#include <assert.h>
#include <sysexits.h>
#include <machine/rtems-bsd-commands.h>
#include <rtems/rtems/status.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <rtems/dhcpcd.h>
#include <rtems/bsd/bsd.h>
#include <rtems/bsd/iface.h>

#define RTEMS_BSD_CONFIG_NET_PF_UNIX
#define RTEMS_BSD_CONFIG_NET_IP_MROUTE

#ifdef RTEMS_BSD_MODULE_NETINET6
#define RTEMS_BSD_CONFIG_NET_IP6_MROUTE
#endif

#define RTEMS_BSD_CONFIG_NET_IF_BRIDGE
#define RTEMS_BSD_CONFIG_NET_IF_LAGG
#define RTEMS_BSD_CONFIG_NET_IF_VLAN
#define RTEMS_BSD_CONFIG_BSP_CONFIG
#define RTEMS_BSD_CONFIG_INIT
#include <machine/rtems-bsd-config.h>

static void
default_network_ifconfig_hwif0(char *ifname)
{
  int exit_code;
  char *ifcfg[] = {
    "ifconfig",
    ifname,
    "up",
    NULL
  };

  exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(ifcfg), ifcfg);
  assert(exit_code == EX_OK);
}

static void
default_network_dhcpcd(void)
{
  static const char default_cfg[] = "clientid libbsd test client\n";
  rtems_status_code sc;
  int fd;
  int rv;
  ssize_t n;

  fd = open("/etc/dhcpcd.conf", O_CREAT | O_WRONLY,
      S_IRWXU | S_IRWXG | S_IRWXO);
  assert(fd >= 0);

  n = write(fd, default_cfg, sizeof(default_cfg) - 1);
  assert(n == (ssize_t) sizeof(default_cfg) - 1);

  rv = close(fd);
  assert(rv == 0);

  sc = rtems_dhcpcd_start(NULL);
  assert(sc == RTEMS_SUCCESSFUL);
}

static void
default_network_set_self_prio(rtems_task_priority prio)
{
  rtems_status_code sc;

  sc = rtems_task_set_priority(RTEMS_SELF, prio, &prio);
  assert(sc == RTEMS_SUCCESSFUL);
}

int net_start()
{
  char *ifname;
  char ifnamebuf[IF_NAMESIZE];
  rtems_status_code sc;

  /*
   * Default the syslog priority to 'debug' to aid developers.
   */
  rtems_bsd_setlogpriority("debug");

  /* Let other tasks run to complete background work */
  default_network_set_self_prio(RTEMS_MAXIMUM_PRIORITY - 1U);

  rtems_bsd_initialize();

  ifname = if_indextoname(1, &ifnamebuf[0]);

  /* Let the callout timer allocate its resources */
  sc = rtems_task_wake_after(2);
  assert(sc == RTEMS_SUCCESSFUL);
  
  rtems_bsd_ifconfig_lo0();
  default_network_ifconfig_hwif0(ifname);
  default_network_dhcpcd();
  
  // needs to wait for DHCP to finish
  return 0;
}