[PATCH rtems-libbsd v3 2/3] rtemsbsd: Add interface support routines
Gedare Bloom
gedare at rtems.org
Mon Aug 9 15:50:38 UTC 2021
On Sun, Aug 8, 2021 at 7:22 PM Chris Johns <chrisj at rtems.org> wrote:
>
> - Add the ability to check if an interface is up
> ---
> libbsd.py | 1 +
> rtemsbsd/include/rtems/bsd/iface.h | 62 ++++++++++++
> rtemsbsd/rtems/rtems-bsd-iface.c | 146 +++++++++++++++++++++++++++++
> 3 files changed, 209 insertions(+)
> create mode 100644 rtemsbsd/include/rtems/bsd/iface.h
> create mode 100644 rtemsbsd/rtems/rtems-bsd-iface.c
>
> diff --git a/libbsd.py b/libbsd.py
> index 09a1fbc4..6e09a07c 100644
> --- a/libbsd.py
> +++ b/libbsd.py
> @@ -253,6 +253,7 @@ class rtems(builder.Module):
> 'rtems/rtems-bsd-cxx.cc',
> 'rtems/rtems-bsd-get-ethernet-addr.c',
> 'rtems/rtems-bsd-ifconfig.c',
> + 'rtems/rtems-bsd-iface.c',
> 'rtems/rtems-bsd-ifconfig-lo0.c',
> 'rtems/rtems-bsd-init-dhcp.c',
> 'rtems/rtems-bsd-rc-conf-net.c',
> diff --git a/rtemsbsd/include/rtems/bsd/iface.h b/rtemsbsd/include/rtems/bsd/iface.h
> new file mode 100644
> index 00000000..9e583b8a
> --- /dev/null
> +++ b/rtemsbsd/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/rtemsbsd/rtems/rtems-bsd-iface.c b/rtemsbsd/rtems/rtems-bsd-iface.c
> new file mode 100644
> index 00000000..4cf25a9b
> --- /dev/null
> +++ b/rtemsbsd/rtems/rtems-bsd-iface.c
> @@ -0,0 +1,146 @@
> +/**
> + * @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 <sys/ioctl.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;
> +
> + memset(iface, 0, sizeof(*iface));
Should you validate arguments? (iface != NULL at least?)
> +
> + 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);
> + }
This failure case won't set an errno, should it?
> + return -1;
> + }
> +
> + strlcpy(iface->name, name, sizeof(iface->name));
If name is larger than the iface->name buffer, then the name gets
truncated, does this matter to the caller?
> +
> + return 0;
> +}
> +
> +int
> +rtems_bsd_iface_link_state(const char *name, bool *state)
> +{
> + struct rtems_bsd_iface iface;
> + struct ifmediareq ifmr;
> + int s;
> + int rc;
> +
> + *state = false;
> +
> + rc = rtems_bsd_iface_get(name, &iface);
> + if (rc < 0) {
errno?
> + 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));
Care if the name is truncated?
> +
> + rc = ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr);
> + close(s);
The indentation alignment looks wrong.
> +
> + 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;
> +}
> --
> 2.24.1
>
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
More information about the devel
mailing list