[Toybox] [PATCH 1/3] Add xsend, xrecv and send_nlrtmsg

Eric Molitor emolitor at molitor.org
Sun May 17 03:09:27 PDT 2020


---
 toys/pending/route.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/toys/pending/route.c b/toys/pending/route.c
index b4e7ea0d..562e5ecd 100644
--- a/toys/pending/route.c
+++ b/toys/pending/route.c
@@ -49,6 +49,7 @@ config ROUTE
 #define FOR_route
 #include "toys.h"
 #include <net/route.h>
+#include <linux/rtnetlink.h>
 
 GLOBALS(
   char *family;
@@ -74,6 +75,37 @@ static struct _arglist arglist2[] = {
   { NULL, 0 }
 };
 
+void xsend(int sockfd, void *buf, size_t len)
+{
+  if (send(sockfd, buf, len, 0) != len) exit(EXIT_FAILURE);
+}
+
+int xrecv(int sockfd, void *buf, size_t len)
+{
+  int msg_len = recv(sockfd, buf, len, 0);
+  if (msg_len < 0) exit(EXIT_FAILURE);
+
+  return msg_len;
+}
+
+void send_nlrtmsg(int fd, int type, int flags, struct rtmsg *rt)
+{
+  struct {
+    struct nlmsghdr nl;
+    struct rtmsg rt;
+  } req;
+
+  memset(&req, 0, sizeof(req));
+  req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
+  req.nl.nlmsg_type = type;
+  req.nl.nlmsg_flags = flags;
+  req.nl.nlmsg_pid = getpid();
+  req.nl.nlmsg_seq = 1;
+  req.rt = *rt;
+
+  xsend(fd, &req, sizeof(req));
+}
+
 // to get the host name from the given ip.
 static int get_hostname(char *ipstr, struct sockaddr_in *sockin)
 {
-- 
2.25.1



More information about the Toybox mailing list