[rtems-net-legacy PATCH] bsd: Add iface calls to help user manage the iface state
Gedare Bloom
gedare at rtems.org
Fri Apr 14 14:14:15 UTC 2023
two nits below
On Wed, Apr 12, 2023 at 8:17 PM <chrisj at rtems.org> wrote:
>
> From: Chris Johns <chrisj at rtems.org>
>
> ---
> include/rtems/bsd/iface.h | 62 +++++++++++++++
> netsources.py | 26 +++----
> rtems/rtems-bsd-iface.c | 154 ++++++++++++++++++++++++++++++++++++++
> testsuites/wscript | 8 ++
> wscript | 7 +-
> 5 files changed, 236 insertions(+), 21 deletions(-)
> create mode 100755 include/rtems/bsd/iface.h
> create mode 100755 rtems/rtems-bsd-iface.c
>
> diff --git a/include/rtems/bsd/iface.h b/include/rtems/bsd/iface.h
> new file mode 100755
> index 0000000..9e583b8
> --- /dev/null
> +++ b/include/rtems/bsd/iface.h
> @@ -0,0 +1,62 @@
> +/**
> + * @file
> + *
> + * @ingroup rtems_bsd_rtems
> + *
> + * @brief This file provide a high level interface to simple interface
> + * support calls.
> + */
> +
> +/*
> + * Copyright (c) 2021. Chris Johns <chrisj at rtems.org>. 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.
> + */
> +
> +#ifndef _RTEMS_BSD_IFACE_H_
> +#define _RTEMS_BSD_IFACE_H_
> +
> +#include <stdbool.h>
> +
> +#include <sys/socket.h>
> +
> +#include <net/if.h>
> +#include <net/if_media.h>
> +#include <netinet/in.h>
> +
> +struct rtems_bsd_iface {
> + char name[IFNAMSIZ];
> + int unit;
> + struct in_addr address;
> + size_t hw_len;
> + uint8_t hw_address[16];
> + struct ifreq ifr;
> + int linkstate;
> +};
> +
> +/*
> + * These calls return 0 is successful and -1 and errno set on error.
> + */
> +int rtems_bsd_iface_get(const char *name, struct rtems_bsd_iface *iface);
> +int rtems_bsd_iface_link_state(const char *name, bool *state);
> +
> +#endif
> diff --git a/netsources.py b/netsources.py
> index be9334d..9f1b7ff 100644
> --- a/netsources.py
> +++ b/netsources.py
> @@ -31,6 +31,7 @@ class source:
> # rtems
> 'rtems/mkrootfs.c',
> 'rtems/rtems_bootp.c',
> + 'rtems/rtems-bsd-iface.c',
> 'rtems/rtems_bsdnet_malloc_starvation.c',
> 'rtems/rtems_dhcp.c',
> 'rtems/rtems_dhcp_failsafe.c',
> @@ -223,22 +224,12 @@ class source:
> ]
>
> nfsclient = [
> - 'pppd/auth.c',
> - 'pppd/ccp.c',
> - 'pppd/chap.c',
> - 'pppd/chap_ms.c',
> - 'pppd/chat.c',
> - 'pppd/demand.c',
> - 'pppd/fsm.c',
> - 'pppd/ipcp.c',
> - 'pppd/lcp.c',
> - 'pppd/magic.c',
> - 'pppd/options.c',
> - 'pppd/rtemsmain.c',
> - 'pppd/rtemspppd.c',
> - 'pppd/sys-rtems.c',
> - 'pppd/upap.c',
> - 'pppd/utils.c',
> + 'nfsclient/proto/mount_prot_xdr.c',
> + 'nfsclient/proto/nfs_prot_xdr.c',
> + 'nfsclient/src/nfs.c',
> + 'nfsclient/src/rpcio.c',
> + 'nfsclient/src/sock_mbuf.c',
> + 'nfsclient/src/xdr_mbuf.c',
> ]
>
> pppd = [
> @@ -316,6 +307,9 @@ header = {
> 'rtems/rtems_mii_ioctl.h', 'rtems/rtems_netdb.h',
> 'rtems/rtems_netinet_in.h', 'rtems/rtems_syscall.h'
> ],
> + 'rtems/bsd': [
> + 'include/rtems/bsd/iface.h',
> + ],
> 'rtems/bsdnet': ['rtems/bsdnet/_types.h', 'rtems/bsdnet/servers.h'],
> 'sys': [
> 'sys/callout.h', 'sys/conf.h', 'sys/domain.h', 'sys/kernel.h',
> diff --git a/rtems/rtems-bsd-iface.c b/rtems/rtems-bsd-iface.c
> new file mode 100755
> index 0000000..0f6432f
> --- /dev/null
> +++ b/rtems/rtems-bsd-iface.c
> @@ -0,0 +1,154 @@
> +/**
> + * @file
> + *
> + * @ingroup rtems_bsd_rtems
> + *
> + * @brief Interface support routines
> + */
> +
> +/*
> + * Copyright (c) 2021. Chris Johns <chrisj at rtems.org> 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 <errno.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <sys/ioctl.h>
> +#include <unistd.h>
> +
> +#include <ifaddrs.h>
> +#include <net/if_dl.h>
> +
> +#include <rtems/bsd/iface.h>
> +
> +#if !defined(RTEMS_BSD_IFACE_TRACE)
> +#define RTEMS_BSD_IFACE_TRACE 0
> +#endif
> +
> +int
> +rtems_bsd_iface_get(const char *name, struct rtems_bsd_iface *iface)
> +{
> + struct ifaddrs *ifap, *ifa;
> + bool found = false;
> +
> + if (iface == NULL) {
> + errno = EINVAL;
> + return -1;
> + }
> +
> + memset(iface, 0, sizeof(*iface));
> +
> + if (getifaddrs(&ifap) != 0) {
> + if (RTEMS_BSD_IFACE_TRACE) {
> + printf("bsd: iface: getifaddrs failed: %s\n", strerror(errno));
> + }
> + return -1;
> + }
> +
> + for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
> + if ((ifa->ifa_flags & IFF_LOOPBACK) ||
> + (ifa->ifa_flags & IFF_POINTOPOINT))
> + continue;
> +
> + if (strcmp(name, ifa->ifa_name) != 0)
> + continue;
> +
> + found = true;
> +
> + if (ifa->ifa_addr->sa_family == AF_LINK) {
> + struct sockaddr_dl *sa;
> + sa = (struct sockaddr_dl *)ifa->ifa_addr;
> + iface->unit = sa->sdl_index;
> + iface->hw_len = sa->sdl_alen;
> + memcpy(iface->hw_address, LLADDR(sa), sa->sdl_alen);
> + } else if (ifa->ifa_addr->sa_family == AF_INET) {
> + struct sockaddr_in sa;
> + memcpy(&sa, ifa->ifa_addr, sizeof(sa));
> + if (sa.sin_addr.s_addr == htonl(INADDR_LOOPBACK))
> + continue;
> + strlcpy(iface->ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
> + memcpy(&iface->ifr.ifr_addr, ifa->ifa_addr,
> + ifa->ifa_addr->sa_len);
> + iface->address = sa.sin_addr;
> + }
> +
> + }
> +
> + freeifaddrs(ifap);
> +
> + if (!found) {
> + if (RTEMS_BSD_IFACE_TRACE) {
> + printf("bsd: iface: not-found: %s\n", name);
> + }
> + errno = ENOENT;
> + return -1;
> + }
> +
> + strlcpy(iface->name, name, sizeof(iface->name));
> +
> + return 0;
> +}
> +
> +int
> +rtems_bsd_iface_link_state(const char *name, bool *state)
> +{
> + struct rtems_bsd_iface iface;
> + struct ifmediareq ifmr;
nit, you can use: ifmr = {0};
to initialize structs to 0 instead of memset(..., 0, ...). In fact it
is a bit safer to do this way.
> + int s;
> + int rc;
> +
> + *state = false;
> +
> + rc = rtems_bsd_iface_get(name, &iface);
> + if (rc < 0) {
> + return -1;
> + }
> +
> + s = socket(iface.ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
> + if (s < 0) {
> + if (RTEMS_BSD_IFACE_TRACE) {
> + printf("bsd: iface: link state: socket open failed: %s\n", strerror(errno));
> + }
> + return -1;
> + }
> +
> + memset(&ifmr, 0, sizeof(ifmr));
> + strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
> +
> + rc = ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr);
whitespace here is off-by-1
> + close(s);
> +
> + if (rc < 0) {
> + if (RTEMS_BSD_IFACE_TRACE) {
> + printf("bsd: iface: link state: socket ioctl failed: %s\n", strerror(errno));
> + }
> + return -1;
> + }
> +
> + if ((ifmr.ifm_status & IFM_AVALID) != 0) {
> + *state = (ifmr.ifm_status & IFM_ACTIVE) != 0;
> + }
> +
> + return 0;
> +}
> diff --git a/testsuites/wscript b/testsuites/wscript
> index 3ce5153..6bfc1eb 100644
> --- a/testsuites/wscript
> +++ b/testsuites/wscript
> @@ -42,6 +42,14 @@ def init(ctx):
> pass
>
>
> +def options(opt):
> + copts = opt.option_groups['configure options']
> + copts.add_option('--net-test-config',
> + default='config.inc',
> + dest='net_config',
> + help='Network test configuration (default: %default)')
> +
> +
> def configure(conf):
> recurse(conf)
>
> diff --git a/wscript b/wscript
> index c365f14..d41f93e 100644
> --- a/wscript
> +++ b/wscript
> @@ -51,11 +51,7 @@ def init(ctx):
> def options(opt):
> rtems.options(opt)
> netlegacy.options(opt)
> - copts = opt.option_groups['configure options']
> - copts.add_option('--net-test-config',
> - default='config.inc',
> - dest='net_config',
> - help='Network test configuration (default: %default)')
> + opt.recurse('testsuites')
>
>
> def bsp_configure(conf, arch_bsp):
> @@ -64,6 +60,7 @@ def bsp_configure(conf, arch_bsp):
> conf.msg('Configure variant: ', ab)
> conf.setenv(ab, env)
> netlegacy.bsp_configure(conf, arch_bsp)
> + conf.recurse('testsuites')
> conf.setenv(ab)
>
>
> --
> 2.37.1
>
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
More information about the devel
mailing list