LinuxNetLink.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_LINUX_NETLINK_HPP
  27. #define ZT_LINUX_NETLINK_HPP
  28. #include <vector>
  29. #include <bits/sockaddr.h>
  30. #include <asm/types.h>
  31. #include <linux/rtnetlink.h>
  32. #include <sys/socket.h>
  33. #include "../node/InetAddress.hpp"
  34. #include "Thread.hpp"
  35. namespace ZeroTier {
  36. struct route_entry {
  37. InetAddress target;
  38. InetAddress via;
  39. const char *iface;
  40. };
  41. typedef std::vector<route_entry> RouteList;
  42. struct nl_req {
  43. struct nlmsghdr nl;
  44. struct rtmsg rt;
  45. char buf[8192];
  46. };
  47. /**
  48. * Interface with Linux's RTNETLINK
  49. */
  50. class LinuxNetLink
  51. {
  52. private:
  53. LinuxNetLink();
  54. ~LinuxNetLink();
  55. public:
  56. static LinuxNetLink& getInstance()
  57. {
  58. static LinuxNetLink instance;
  59. return instance;
  60. }
  61. LinuxNetLink(LinuxNetLink const&) = delete;
  62. void operator=(LinuxNetLink const&) = delete;
  63. void addRoute(const InetAddress &target, const InetAddress &via, const char *ifaceName);
  64. void delRoute(const InetAddress &target, const InetAddress &via, const char *ifaceName);
  65. RouteList getIPV4Routes() const;
  66. RouteList getIPV6Routes() const;
  67. void addInterface(const char *iface);
  68. void addAddress(const InetAddress &addr, const char *iface);
  69. void threadMain() throw();
  70. private:
  71. void _processMessage();
  72. void _routeAdded();
  73. void _routeDeleted();
  74. void _linkAdded();
  75. void _linkDeleted();
  76. void _ipAddressAdded();
  77. void _ipAddressDeleted();
  78. void _requestIPv4Routes();
  79. void _requestIPv6Routes();
  80. Thread _t;
  81. bool _running;
  82. RouteList _routes_ipv4;
  83. RouteList _routes_ipv6;
  84. // socket communication vars;
  85. int _fd;
  86. struct sockaddr_nl _la;
  87. struct sockaddr_nl _pa;
  88. struct msghdr _msg;
  89. struct iovec _iov;
  90. int _rtn;
  91. char _buf[8192];
  92. // RTNETLINK message pointers & lengths
  93. // used for processing messages
  94. struct nlmsghdr *_nlp;
  95. int _nll;
  96. struct rtmsg *_rtp;
  97. int _rtl;
  98. struct rtattr *_rtap;
  99. struct ifinfomsg *_ifip;
  100. int _ifil;
  101. struct ifaddrmsg *_ifap;
  102. int _ifal;
  103. };
  104. }
  105. #endif // ZT_LINUX_NETLINK_HPPS