[rtems-libbsd commit] rtemsbsd: Add interface support routines

Chris Johns chrisj at rtems.org
Thu Sep 2 02:43:00 UTC 2021


Module:    rtems-libbsd
Branch:    6-freebsd-12
Commit:    45ff774c3c25ee52c5c7305f0790b2a6ed75ec9e
Changeset: http://git.rtems.org/rtems-libbsd/commit/?id=45ff774c3c25ee52c5c7305f0790b2a6ed75ec9e

Author:    Chris Johns <chrisj at rtems.org>
Date:      Sun Aug  8 16:32:38 2021 +1000

rtemsbsd: Add interface support routines

- 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   | 152 +++++++++++++++++++++++++++++++++++++
 3 files changed, 215 insertions(+)

diff --git a/libbsd.py b/libbsd.py
index 09a1fbc..6e09a07 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 0000000..9e583b8
--- /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 0000000..f13eeeb
--- /dev/null
+++ b/rtemsbsd/rtems/rtems-bsd-iface.c
@@ -0,0 +1,152 @@
+/**
+ * @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;
+
+	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;
+	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);
+	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;
+}



More information about the vc mailing list