LinuxNetLink.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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 <sys/socket.h>
  30. #include <asm/types.h>
  31. #include <linux/rtnetlink.h>
  32. #include <sys/socket.h>
  33. #include <linux/if.h>
  34. #include "../node/InetAddress.hpp"
  35. #include "../node/MAC.hpp"
  36. #include "Thread.hpp"
  37. #include "../node/Hashtable.hpp"
  38. #include "../node/Mutex.hpp"
  39. namespace ZeroTier {
  40. struct route_entry {
  41. InetAddress target;
  42. InetAddress via;
  43. int if_index;
  44. char iface[IFNAMSIZ];
  45. };
  46. typedef std::vector<route_entry> RouteList;
  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 InetAddress &src, const char *ifaceName);
  64. void delRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName);
  65. RouteList getIPV4Routes() const;
  66. RouteList getIPV6Routes() const;
  67. void addAddress(const InetAddress &addr, const char *iface);
  68. void removeAddress(const InetAddress &addr, const char *iface);
  69. void threadMain() throw();
  70. private:
  71. int _doRecv(int fd);
  72. void _processMessage(struct nlmsghdr *nlp, int nll);
  73. void _routeAdded(struct nlmsghdr *nlp);
  74. void _routeDeleted(struct nlmsghdr *nlp);
  75. void _linkAdded(struct nlmsghdr *nlp);
  76. void _linkDeleted(struct nlmsghdr *nlp);
  77. void _ipAddressAdded(struct nlmsghdr *nlp);
  78. void _ipAddressDeleted(struct nlmsghdr *nlp);
  79. void _requestInterfaceList();
  80. void _requestIPv4Routes();
  81. void _requestIPv6Routes();
  82. int _indexForInterface(const char *iface);
  83. void _setSocketTimeout(int fd, int seconds = 1);
  84. Thread _t;
  85. bool _running;
  86. RouteList _routes_ipv4;
  87. Mutex _rv4_m;
  88. RouteList _routes_ipv6;
  89. Mutex _rv6_m;
  90. uint32_t _seq;
  91. struct iface_entry {
  92. int index;
  93. char ifacename[IFNAMSIZ];
  94. char mac[18];
  95. char mac_bin[6];
  96. unsigned int mtu;
  97. };
  98. Hashtable<int, iface_entry> _interfaces;
  99. Mutex _if_m;
  100. // socket communication vars;
  101. int _fd;
  102. struct sockaddr_nl _la;
  103. };
  104. }
  105. #endif // ZT_LINUX_NETLINK_HPPS