RoutingTable.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_ROUTINGTABLE_HPP
  19. #define ZT_ROUTINGTABLE_HPP
  20. #include <vector>
  21. #include "../node/Constants.hpp"
  22. #include "../node/InetAddress.hpp"
  23. namespace ZeroTier {
  24. class RoutingTable
  25. {
  26. public:
  27. struct Entry
  28. {
  29. /**
  30. * Destination IP and netmask bits (CIDR format)
  31. */
  32. InetAddress destination;
  33. /**
  34. * Gateway or null address if direct link-level route, netmask/port part of InetAddress not used
  35. */
  36. InetAddress gateway;
  37. /**
  38. * System device index or ID (not included in comparison operators, may not be set on all platforms)
  39. */
  40. int deviceIndex;
  41. /**
  42. * Metric or hop count -- higher = lower routing priority
  43. */
  44. int metric;
  45. /**
  46. * Interface scoped route? (always false if not meaningful on this OS)
  47. */
  48. bool ifscope;
  49. /**
  50. * System device name (may be empty if it doesn't exist or isn't important on this OS)
  51. */
  52. char device[128];
  53. /**
  54. * @return True if at least one required field is present (object is not null)
  55. */
  56. inline operator bool() const { return ((destination)||(gateway)); }
  57. };
  58. /**
  59. * Get routing table
  60. *
  61. * @param includeLinkLocal Include link-local IPs?
  62. * @param includeLoopback Include loopback routes?
  63. */
  64. static std::vector<RoutingTable::Entry> get(bool includeLinkLocal,bool includeLoopback);
  65. /**
  66. * Add or replace a routing table entry
  67. *
  68. * @param destination Route destination
  69. * @param gateway Gateway or null if local
  70. * @param device Device name (if applicable)
  71. * @param metric Route metric (if applicable)
  72. * @param ifscope Interface bound route? If so, device must be set. (only applicable on some OSes)
  73. */
  74. static RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric,bool ifscope);
  75. };
  76. } // namespace ZeroTier
  77. #endif