From e49b008c6169ae7eaf1c765e2a729095b9b59d19 Mon Sep 17 00:00:00 2001 From: Eric Molitor Date: Sun, 17 May 2020 11:09:27 +0100 Subject: Add xsend, xrecv and send_nlrtmsg --- toys/pending/route.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'toys') 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 +#include 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) { -- cgit v1.2.3