From 3730917ddaa1303c7f62ef2240908da816f54242 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 13 Nov 2020 21:06:34 -0500 Subject: [PATCH] LinuxNetLink cleanup --- osdep/LinuxNetLink.cpp | 29 +++---------- osdep/LinuxNetLink.hpp | 99 ++++++++++++++++++------------------------ 2 files changed, 49 insertions(+), 79 deletions(-) diff --git a/osdep/LinuxNetLink.cpp b/osdep/LinuxNetLink.cpp index 8eff09229..e101b407d 100644 --- a/osdep/LinuxNetLink.cpp +++ b/osdep/LinuxNetLink.cpp @@ -45,10 +45,6 @@ struct nl_adr_req { LinuxNetLink::LinuxNetLink() : _t() , _running(false) - , _routes_ipv4() - , _rv4_m() - , _routes_ipv6() - , _rv6_m() , _seq(0) , _interfaces() , _if_m() @@ -147,9 +143,9 @@ int LinuxNetLink::_doRecv(int fd) } if (nlp->nlmsg_type == NLMSG_OVERRUN) { -//#ifdef ZT_NETLINK_TRACE +#ifdef ZT_NETLINK_TRACE fprintf(stderr, "NLMSG_OVERRUN: Data lost\n"); -//#endif +#endif p = buf; nll = 0; break; @@ -175,11 +171,10 @@ int LinuxNetLink::_doRecv(int fd) void LinuxNetLink::threadMain() throw() { int rtn = 0; - while(_running) { rtn = _doRecv(_fd); if (rtn <= 0) { - Thread::sleep(100); + Thread::sleep(250); continue; } } @@ -217,6 +212,7 @@ void LinuxNetLink::_processMessage(struct nlmsghdr *nlp, int nll) void LinuxNetLink::_ipAddressAdded(struct nlmsghdr *nlp) { +#ifdef ZT_NETLINK_TRACE struct ifaddrmsg *ifap = (struct ifaddrmsg *)NLMSG_DATA(nlp); struct rtattr *rtap = (struct rtattr *)IFA_RTA(ifap); int ifal = IFA_PAYLOAD(nlp); @@ -244,13 +240,13 @@ void LinuxNetLink::_ipAddressAdded(struct nlmsghdr *nlp) } } -#ifdef ZT_NETLINK_TRACE fprintf(stderr,"Added IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast); #endif } void LinuxNetLink::_ipAddressDeleted(struct nlmsghdr *nlp) { +#ifdef ZT_NETLINK_TRACE struct ifaddrmsg *ifap = (struct ifaddrmsg *)NLMSG_DATA(nlp); struct rtattr *rtap = (struct rtattr *)IFA_RTA(ifap); int ifal = IFA_PAYLOAD(nlp); @@ -278,13 +274,13 @@ void LinuxNetLink::_ipAddressDeleted(struct nlmsghdr *nlp) } } -#ifdef ZT_NETLINK_TRACE fprintf(stderr, "Removed IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast); #endif } void LinuxNetLink::_routeAdded(struct nlmsghdr *nlp) { +#ifdef ZT_NETLINK_TRACE char dsts[40] = {0}; char gws[40] = {0}; char srcs[40] = {0}; @@ -315,13 +311,13 @@ void LinuxNetLink::_routeAdded(struct nlmsghdr *nlp) } sprintf(ms, "%d", rtp->rtm_dst_len); -#ifdef ZT_NETLINK_TRACE fprintf(stderr, "Route Added: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs); #endif } void LinuxNetLink::_routeDeleted(struct nlmsghdr *nlp) { +#ifdef ZT_NETLINK_TRACE char dsts[40] = {0}; char gws[40] = {0}; char srcs[40] = {0}; @@ -352,7 +348,6 @@ void LinuxNetLink::_routeDeleted(struct nlmsghdr *nlp) } sprintf(ms, "%d", rtp->rtm_dst_len); -#ifdef ZT_NETLINK_TRACE fprintf(stderr, "Route Deleted: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs); #endif } @@ -1045,16 +1040,6 @@ void LinuxNetLink::removeAddress(const InetAddress &addr, const char *iface) close(fd); } -RouteList LinuxNetLink::getIPV4Routes() const -{ - return _routes_ipv4; -} - -RouteList LinuxNetLink::getIPV6Routes() const -{ - return _routes_ipv6; -} - int LinuxNetLink::_indexForInterface(const char *iface) { Mutex::Lock l(_if_m); diff --git a/osdep/LinuxNetLink.hpp b/osdep/LinuxNetLink.hpp index 73c017736..c6c4be00a 100644 --- a/osdep/LinuxNetLink.hpp +++ b/osdep/LinuxNetLink.hpp @@ -35,84 +35,69 @@ namespace ZeroTier { -struct route_entry { - InetAddress target; - InetAddress via; - int if_index; - char iface[IFNAMSIZ]; -}; -typedef std::vector RouteList; - /** * Interface with Linux's RTNETLINK */ class LinuxNetLink { private: - LinuxNetLink(); - ~LinuxNetLink(); + LinuxNetLink(); + ~LinuxNetLink(); public: - static LinuxNetLink& getInstance() - { - static LinuxNetLink instance; - return instance; - } + static LinuxNetLink& getInstance() + { + static LinuxNetLink instance; + return instance; + } - LinuxNetLink(LinuxNetLink const&) = delete; - void operator=(LinuxNetLink const&) = delete; + LinuxNetLink(LinuxNetLink const&) = delete; + void operator=(LinuxNetLink const&) = delete; - void addRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName); - void delRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName); - RouteList getIPV4Routes() const; - RouteList getIPV6Routes() const; + void addRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName); + void delRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName); - void addAddress(const InetAddress &addr, const char *iface); - void removeAddress(const InetAddress &addr, const char *iface); + void addAddress(const InetAddress &addr, const char *iface); + void removeAddress(const InetAddress &addr, const char *iface); - void threadMain() throw(); + void threadMain() throw(); private: - int _doRecv(int fd); + int _doRecv(int fd); - void _processMessage(struct nlmsghdr *nlp, int nll); - void _routeAdded(struct nlmsghdr *nlp); - void _routeDeleted(struct nlmsghdr *nlp); - void _linkAdded(struct nlmsghdr *nlp); - void _linkDeleted(struct nlmsghdr *nlp); - void _ipAddressAdded(struct nlmsghdr *nlp); - void _ipAddressDeleted(struct nlmsghdr *nlp); + void _processMessage(struct nlmsghdr *nlp, int nll); + void _routeAdded(struct nlmsghdr *nlp); + void _routeDeleted(struct nlmsghdr *nlp); + void _linkAdded(struct nlmsghdr *nlp); + void _linkDeleted(struct nlmsghdr *nlp); + void _ipAddressAdded(struct nlmsghdr *nlp); + void _ipAddressDeleted(struct nlmsghdr *nlp); - void _requestInterfaceList(); - void _requestIPv4Routes(); - void _requestIPv6Routes(); + void _requestInterfaceList(); + void _requestIPv4Routes(); + void _requestIPv6Routes(); - int _indexForInterface(const char *iface); + int _indexForInterface(const char *iface); - void _setSocketTimeout(int fd, int seconds = 1); + void _setSocketTimeout(int fd, int seconds = 1); - Thread _t; - bool _running; + Thread _t; + bool _running; - RouteList _routes_ipv4; - Mutex _rv4_m; - RouteList _routes_ipv6; - Mutex _rv6_m; + uint32_t _seq; - uint32_t _seq; + struct iface_entry { + int index; + char ifacename[IFNAMSIZ]; + char mac[18]; + char mac_bin[6]; + unsigned int mtu; + }; + Hashtable _interfaces; + Mutex _if_m; - struct iface_entry { - int index; - char ifacename[IFNAMSIZ]; - char mac[18]; - char mac_bin[6]; - unsigned int mtu; - }; - Hashtable _interfaces; - Mutex _if_m; - - // socket communication vars; - int _fd; - struct sockaddr_nl _la; + // socket communication vars; + int _fd; + struct sockaddr_nl _la; }; }