LinuxNetLink.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <linux/if.h>
  34. #include "../node/InetAddress.hpp"
  35. #include "../node/MAC.hpp"
  36. #include "Thread.hpp"
  37. #include "../node/Hashtable.hpp"
  38. namespace ZeroTier {
  39. struct route_entry {
  40. InetAddress target;
  41. InetAddress via;
  42. int if_index;
  43. char iface[IFNAMSIZ];
  44. };
  45. typedef std::vector<route_entry> RouteList;
  46. /**
  47. * Interface with Linux's RTNETLINK
  48. */
  49. class LinuxNetLink
  50. {
  51. private:
  52. LinuxNetLink();
  53. ~LinuxNetLink();
  54. public:
  55. static LinuxNetLink& getInstance()
  56. {
  57. static LinuxNetLink instance;
  58. return instance;
  59. }
  60. LinuxNetLink(LinuxNetLink const&) = delete;
  61. void operator=(LinuxNetLink const&) = delete;
  62. void addRoute(const InetAddress &target, const InetAddress &via, const char *ifaceName);
  63. void delRoute(const InetAddress &target, const InetAddress &via, const char *ifaceName);
  64. RouteList getIPV4Routes() const;
  65. RouteList getIPV6Routes() const;
  66. // void addInterface(const char *iface, unsigned int mtu, const MAC &mac);
  67. // void removeInterface(const char *iface);
  68. void addAddress(const InetAddress &addr, const char *iface);
  69. void removeAddress(const InetAddress &addr, const char *iface);
  70. void threadMain() throw();
  71. private:
  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. Thread _t;
  83. bool _running;
  84. RouteList _routes_ipv4;
  85. RouteList _routes_ipv6;
  86. uint32_t _seq;
  87. struct iface_entry {
  88. int index;
  89. char ifacename[IFNAMSIZ];
  90. char mac[18];
  91. char mac_bin[6];
  92. unsigned int mtu;
  93. };
  94. Hashtable<int, iface_entry> _interfaces;
  95. // socket communication vars;
  96. int _fd;
  97. struct sockaddr_nl _la;
  98. };
  99. }
  100. #endif // ZT_LINUX_NETLINK_HPPS