LinuxNetLink cleanup

This commit is contained in:
Adam Ierymenko 2020-11-13 21:06:34 -05:00
parent 57516cfbe6
commit 3730917dda
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
2 changed files with 49 additions and 79 deletions

View file

@ -45,10 +45,6 @@ struct nl_adr_req {
LinuxNetLink::LinuxNetLink() LinuxNetLink::LinuxNetLink()
: _t() : _t()
, _running(false) , _running(false)
, _routes_ipv4()
, _rv4_m()
, _routes_ipv6()
, _rv6_m()
, _seq(0) , _seq(0)
, _interfaces() , _interfaces()
, _if_m() , _if_m()
@ -147,9 +143,9 @@ int LinuxNetLink::_doRecv(int fd)
} }
if (nlp->nlmsg_type == NLMSG_OVERRUN) { if (nlp->nlmsg_type == NLMSG_OVERRUN) {
//#ifdef ZT_NETLINK_TRACE #ifdef ZT_NETLINK_TRACE
fprintf(stderr, "NLMSG_OVERRUN: Data lost\n"); fprintf(stderr, "NLMSG_OVERRUN: Data lost\n");
//#endif #endif
p = buf; p = buf;
nll = 0; nll = 0;
break; break;
@ -175,11 +171,10 @@ int LinuxNetLink::_doRecv(int fd)
void LinuxNetLink::threadMain() throw() void LinuxNetLink::threadMain() throw()
{ {
int rtn = 0; int rtn = 0;
while(_running) { while(_running) {
rtn = _doRecv(_fd); rtn = _doRecv(_fd);
if (rtn <= 0) { if (rtn <= 0) {
Thread::sleep(100); Thread::sleep(250);
continue; continue;
} }
} }
@ -217,6 +212,7 @@ void LinuxNetLink::_processMessage(struct nlmsghdr *nlp, int nll)
void LinuxNetLink::_ipAddressAdded(struct nlmsghdr *nlp) void LinuxNetLink::_ipAddressAdded(struct nlmsghdr *nlp)
{ {
#ifdef ZT_NETLINK_TRACE
struct ifaddrmsg *ifap = (struct ifaddrmsg *)NLMSG_DATA(nlp); struct ifaddrmsg *ifap = (struct ifaddrmsg *)NLMSG_DATA(nlp);
struct rtattr *rtap = (struct rtattr *)IFA_RTA(ifap); struct rtattr *rtap = (struct rtattr *)IFA_RTA(ifap);
int ifal = IFA_PAYLOAD(nlp); 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); fprintf(stderr,"Added IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast);
#endif #endif
} }
void LinuxNetLink::_ipAddressDeleted(struct nlmsghdr *nlp) void LinuxNetLink::_ipAddressDeleted(struct nlmsghdr *nlp)
{ {
#ifdef ZT_NETLINK_TRACE
struct ifaddrmsg *ifap = (struct ifaddrmsg *)NLMSG_DATA(nlp); struct ifaddrmsg *ifap = (struct ifaddrmsg *)NLMSG_DATA(nlp);
struct rtattr *rtap = (struct rtattr *)IFA_RTA(ifap); struct rtattr *rtap = (struct rtattr *)IFA_RTA(ifap);
int ifal = IFA_PAYLOAD(nlp); 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); fprintf(stderr, "Removed IP Address %s local: %s label: %s broadcast: %s\n", addr, local, label, bcast);
#endif #endif
} }
void LinuxNetLink::_routeAdded(struct nlmsghdr *nlp) void LinuxNetLink::_routeAdded(struct nlmsghdr *nlp)
{ {
#ifdef ZT_NETLINK_TRACE
char dsts[40] = {0}; char dsts[40] = {0};
char gws[40] = {0}; char gws[40] = {0};
char srcs[40] = {0}; char srcs[40] = {0};
@ -315,13 +311,13 @@ void LinuxNetLink::_routeAdded(struct nlmsghdr *nlp)
} }
sprintf(ms, "%d", rtp->rtm_dst_len); 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); fprintf(stderr, "Route Added: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs);
#endif #endif
} }
void LinuxNetLink::_routeDeleted(struct nlmsghdr *nlp) void LinuxNetLink::_routeDeleted(struct nlmsghdr *nlp)
{ {
#ifdef ZT_NETLINK_TRACE
char dsts[40] = {0}; char dsts[40] = {0};
char gws[40] = {0}; char gws[40] = {0};
char srcs[40] = {0}; char srcs[40] = {0};
@ -352,7 +348,6 @@ void LinuxNetLink::_routeDeleted(struct nlmsghdr *nlp)
} }
sprintf(ms, "%d", rtp->rtm_dst_len); 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); fprintf(stderr, "Route Deleted: dst %s/%s gw %s src %s if %s\n", dsts, ms, gws, srcs, ifs);
#endif #endif
} }
@ -1045,16 +1040,6 @@ void LinuxNetLink::removeAddress(const InetAddress &addr, const char *iface)
close(fd); close(fd);
} }
RouteList LinuxNetLink::getIPV4Routes() const
{
return _routes_ipv4;
}
RouteList LinuxNetLink::getIPV6Routes() const
{
return _routes_ipv6;
}
int LinuxNetLink::_indexForInterface(const char *iface) int LinuxNetLink::_indexForInterface(const char *iface)
{ {
Mutex::Lock l(_if_m); Mutex::Lock l(_if_m);

View file

@ -35,84 +35,69 @@
namespace ZeroTier { namespace ZeroTier {
struct route_entry {
InetAddress target;
InetAddress via;
int if_index;
char iface[IFNAMSIZ];
};
typedef std::vector<route_entry> RouteList;
/** /**
* Interface with Linux's RTNETLINK * Interface with Linux's RTNETLINK
*/ */
class LinuxNetLink class LinuxNetLink
{ {
private: private:
LinuxNetLink(); LinuxNetLink();
~LinuxNetLink(); ~LinuxNetLink();
public: public:
static LinuxNetLink& getInstance() static LinuxNetLink& getInstance()
{ {
static LinuxNetLink instance; static LinuxNetLink instance;
return instance; return instance;
} }
LinuxNetLink(LinuxNetLink const&) = delete; LinuxNetLink(LinuxNetLink const&) = delete;
void operator=(LinuxNetLink const&) = delete; void operator=(LinuxNetLink const&) = delete;
void addRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName); 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 delRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName);
RouteList getIPV4Routes() const;
RouteList getIPV6Routes() const;
void addAddress(const InetAddress &addr, const char *iface); void addAddress(const InetAddress &addr, const char *iface);
void removeAddress(const InetAddress &addr, const char *iface); void removeAddress(const InetAddress &addr, const char *iface);
void threadMain() throw(); void threadMain() throw();
private: private:
int _doRecv(int fd); int _doRecv(int fd);
void _processMessage(struct nlmsghdr *nlp, int nll); void _processMessage(struct nlmsghdr *nlp, int nll);
void _routeAdded(struct nlmsghdr *nlp); void _routeAdded(struct nlmsghdr *nlp);
void _routeDeleted(struct nlmsghdr *nlp); void _routeDeleted(struct nlmsghdr *nlp);
void _linkAdded(struct nlmsghdr *nlp); void _linkAdded(struct nlmsghdr *nlp);
void _linkDeleted(struct nlmsghdr *nlp); void _linkDeleted(struct nlmsghdr *nlp);
void _ipAddressAdded(struct nlmsghdr *nlp); void _ipAddressAdded(struct nlmsghdr *nlp);
void _ipAddressDeleted(struct nlmsghdr *nlp); void _ipAddressDeleted(struct nlmsghdr *nlp);
void _requestInterfaceList(); void _requestInterfaceList();
void _requestIPv4Routes(); void _requestIPv4Routes();
void _requestIPv6Routes(); 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; Thread _t;
bool _running; bool _running;
RouteList _routes_ipv4; uint32_t _seq;
Mutex _rv4_m;
RouteList _routes_ipv6;
Mutex _rv6_m;
uint32_t _seq; struct iface_entry {
int index;
char ifacename[IFNAMSIZ];
char mac[18];
char mac_bin[6];
unsigned int mtu;
};
Hashtable<int, iface_entry> _interfaces;
Mutex _if_m;
struct iface_entry { // socket communication vars;
int index; int _fd;
char ifacename[IFNAMSIZ]; struct sockaddr_nl _la;
char mac[18];
char mac_bin[6];
unsigned int mtu;
};
Hashtable<int, iface_entry> _interfaces;
Mutex _if_m;
// socket communication vars;
int _fd;
struct sockaddr_nl _la;
}; };
} }