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()
: _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);

View File

@ -35,84 +35,69 @@
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
*/
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<int, iface_entry> _interfaces;
Mutex _if_m;
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;
// socket communication vars;
int _fd;
struct sockaddr_nl _la;
// socket communication vars;
int _fd;
struct sockaddr_nl _la;
};
}