[rtems-libbsd commit] Add rtems_get_route to fetch a specific route from the routing tables.
Chris Johns
chrisj at rtems.org
Wed Jun 29 23:20:11 UTC 2016
Module: rtems-libbsd
Branch: master
Commit: 45960a350d9a83061ba9f6a4c48ea8ce6eb9a413
Changeset: http://git.rtems.org/rtems-libbsd/commit/?id=45960a350d9a83061ba9f6a4c48ea8ce6eb9a413
Author: Chris Johns <chrisj at rtems.org>
Date: Thu Jun 30 09:17:21 2016 +1000
Add rtems_get_route to fetch a specific route from the routing tables.
---
libbsd.py | 1 +
libbsd_waf.py | 1 +
rtemsbsd/include/rtems/rtems-routes.h | 48 ++++++++++++
rtemsbsd/rtems/rtems-routes.c | 141 ++++++++++++++++++++++++++++++++++
4 files changed, 191 insertions(+)
diff --git a/libbsd.py b/libbsd.py
index ca73bcd..13dad6d 100755
--- a/libbsd.py
+++ b/libbsd.py
@@ -105,6 +105,7 @@ def rtems(mm):
'rtems/rtems-legacy-newproc.c',
'rtems/rtems-legacy-mii.c',
'rtems/rtems-kvm.c',
+ 'rtems/rtems-routes.c',
'rtems/syslog.c',
'ftpd/ftpd.c',
'ftpd/ftpd-init.c',
diff --git a/libbsd_waf.py b/libbsd_waf.py
index 5e18f5e..9ea85be 100644
--- a/libbsd_waf.py
+++ b/libbsd_waf.py
@@ -1098,6 +1098,7 @@ def build(bld):
'rtemsbsd/rtems/rtems-legacy-mii.c',
'rtemsbsd/rtems/rtems-legacy-newproc.c',
'rtemsbsd/rtems/rtems-legacy-rtrequest.c',
+ 'rtemsbsd/rtems/rtems-routes.c',
'rtemsbsd/rtems/syslog.c',
'rtemsbsd/sys/dev/dw_mmc/dw_mmc.c',
'rtemsbsd/sys/dev/ffec/if_ffec_mcf548x.c',
diff --git a/rtemsbsd/include/rtems/rtems-routes.h b/rtemsbsd/include/rtems/rtems-routes.h
new file mode 100644
index 0000000..1628cb5
--- /dev/null
+++ b/rtemsbsd/include/rtems/rtems-routes.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2016 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_ROUTES_h
+#define RTEMS_ROUTES_h
+
+#include <sys/socket.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/*
+ * Useful functions to access the routing tables.
+ */
+
+/*
+ * Get a route. The array size of sockadd's must be RTAX_MAX.
+ */
+int rtems_get_route(const struct sockaddr_in* sin, struct sockaddr** rti_info);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* RTEMS_ROUTES_h */
diff --git a/rtemsbsd/rtems/rtems-routes.c b/rtemsbsd/rtems/rtems-routes.c
new file mode 100644
index 0000000..6663e8d
--- /dev/null
+++ b/rtemsbsd/rtems/rtems-routes.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2016 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.
+ */
+
+/*
+ * Useful functions to access the routing tables. Based on unpv13e from
+ * Stevens.
+ */
+
+#include <stdbool.h>
+#include <errno.h>
+
+#include <net/if.h>
+#include <net/route.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+
+/*
+ * Round up 'a' to next multiple of 'size', which must be a power of 2
+ */
+#define RR_ROUNDUP(a, size) (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
+
+/*
+ * Step to next socket address structure;
+ * if sa_len is 0, assume it is sizeof(u_long).
+ */
+#define NEXT_SA(ap) \
+ ap = (struct sockaddr*) ((caddr_t) ap + \
+ (ap->sa_len ? RR_ROUNDUP(ap->sa_len, sizeof (u_long)) : sizeof(u_long)))
+
+static void
+get_rtaddrs(int addrs, struct sockaddr* sa, struct sockaddr** rti_info)
+{
+ int i;
+
+ for (i = 0; i < RTAX_MAX; i++) {
+ if ((addrs & (1 << i)) != 0) {
+ rti_info[i] = sa;
+ NEXT_SA(sa);
+ } else
+ rti_info[i] = NULL;
+ }
+}
+
+/*
+ * Get a route.
+ */
+int rtems_get_route(const struct sockaddr_in* sin, struct sockaddr** rti_info)
+{
+ int s;
+ char* buf;
+ const size_t buflen = sizeof(struct rt_msghdr) + 512;
+ pid_t pid;
+ struct rt_msghdr* rtm;
+ struct sockaddr_in* r_sin;
+ struct sockaddr* sa;
+ const int seq = (int) 0x28290817;
+ ssize_t r;
+
+ buf = calloc(1, buflen);
+ if (buf == NULL) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ s = socket(AF_ROUTE, SOCK_RAW, AF_UNSPEC);
+ if (s < 0)
+ return -1;
+
+ rtm = (struct rt_msghdr *) buf;
+ rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
+ rtm->rtm_version = RTM_VERSION;
+ rtm->rtm_type = RTM_GET;
+ rtm->rtm_addrs = RTA_DST;
+ rtm->rtm_pid = pid = getpid();
+ rtm->rtm_seq = seq;
+
+ r_sin = (struct sockaddr_in *) (rtm + 1);
+ *r_sin = *sin;
+
+ r = write(s, rtm, rtm->rtm_msglen);
+ if (r < 0) {
+ int en = errno;
+ close(s);
+ free(buf);
+ errno = en;
+ return -1;
+ }
+
+ while (true) {
+ r = read(s, rtm, buflen);
+ if (r < 0) {
+ int en = errno;
+ close(s);
+ free(buf);
+ errno = en;
+ return -1;
+ }
+
+ /*
+ * The kernel sends all routing message to all routing sockets. We need to
+ * filter them for the one for us.
+ */
+ if (rtm->rtm_type == RTM_GET &&
+ rtm->rtm_seq == seq &&
+ rtm->rtm_pid == pid)
+ break;
+ }
+
+ close(s);
+
+ rtm = (struct rt_msghdr*) buf;
+ sa = (struct sockaddr*) (rtm + 1);
+
+ get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
+
+ free(buf);
+
+ return 0;
+}
More information about the vc
mailing list