2014-07-13 19:57:30 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
2014-07-13 19:57:30 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
2014-07-15 19:32:06 +00:00
|
|
|
#ifndef ZT_ROUTINGTABLE_HPP
|
|
|
|
#define ZT_ROUTINGTABLE_HPP
|
2014-07-13 19:57:30 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "InetAddress.hpp"
|
|
|
|
#include "NonCopyable.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2014-07-15 19:32:06 +00:00
|
|
|
* Base class for OS routing table interfaces
|
2014-07-13 19:57:30 +00:00
|
|
|
*/
|
2014-07-15 19:32:06 +00:00
|
|
|
class RoutingTable : NonCopyable
|
2014-07-13 19:57:30 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-07-15 19:32:06 +00:00
|
|
|
class Entry
|
|
|
|
{
|
|
|
|
public:
|
2014-07-18 00:17:59 +00:00
|
|
|
Entry() throw() { device[0] = (char)0; }
|
2014-07-13 19:57:30 +00:00
|
|
|
|
2014-07-18 00:17:59 +00:00
|
|
|
/**
|
|
|
|
* Destination IP and netmask bits (CIDR format)
|
|
|
|
*/
|
2014-07-15 19:32:06 +00:00
|
|
|
InetAddress destination;
|
2014-07-18 00:17:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gateway or null address if direct link-level route, netmask/port part of InetAddress not used
|
|
|
|
*/
|
|
|
|
InetAddress gateway;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* System device index or ID (not included in comparison operators, may not be set on all platforms)
|
|
|
|
*/
|
|
|
|
int deviceIndex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Metric or hop count -- higher = lower routing priority
|
|
|
|
*/
|
|
|
|
int metric;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* System device name
|
|
|
|
*/
|
2014-07-15 19:32:06 +00:00
|
|
|
char device[128];
|
2014-07-13 19:57:30 +00:00
|
|
|
|
2014-07-18 00:17:59 +00:00
|
|
|
/**
|
|
|
|
* @return Human-readable representation of this route
|
|
|
|
*/
|
2014-07-15 20:48:42 +00:00
|
|
|
std::string toString() const;
|
|
|
|
|
2014-07-18 00:17:59 +00:00
|
|
|
/**
|
|
|
|
* @return True if at least one required field is present (object is not null)
|
|
|
|
*/
|
|
|
|
inline operator bool() const { return ((destination)||(gateway)||(device[0])); }
|
|
|
|
|
2014-07-15 20:48:42 +00:00
|
|
|
bool operator==(const Entry &re) const;
|
2014-07-15 19:32:06 +00:00
|
|
|
inline bool operator!=(const Entry &re) const { return (!(*this == re)); }
|
2014-07-15 20:48:42 +00:00
|
|
|
bool operator<(const Entry &re) const;
|
2014-07-15 19:32:06 +00:00
|
|
|
inline bool operator>(const Entry &re) const { return (re < *this); }
|
|
|
|
inline bool operator<=(const Entry &re) const { return (!(re < *this)); }
|
|
|
|
inline bool operator>=(const Entry &re) const { return (!(*this < re)); }
|
|
|
|
};
|
2014-07-13 19:57:30 +00:00
|
|
|
|
2014-07-15 20:48:42 +00:00
|
|
|
RoutingTable();
|
|
|
|
virtual ~RoutingTable();
|
2014-07-13 19:57:30 +00:00
|
|
|
|
2014-07-15 19:32:06 +00:00
|
|
|
/**
|
2014-07-17 20:08:37 +00:00
|
|
|
* Get routing table
|
|
|
|
*
|
|
|
|
* @param includeLinkLocal If true, include link-local address routes (default: false)
|
|
|
|
* @param includeLoopback Include loopback (default: false)
|
2014-07-15 20:48:42 +00:00
|
|
|
* @return Sorted routing table entries
|
2014-07-15 19:32:06 +00:00
|
|
|
*/
|
2014-07-18 00:17:59 +00:00
|
|
|
virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const = 0;
|
2014-07-13 19:57:30 +00:00
|
|
|
|
2014-07-15 19:32:06 +00:00
|
|
|
/**
|
|
|
|
* Add or update a routing table entry
|
|
|
|
*
|
2014-07-18 00:17:59 +00:00
|
|
|
* If there is no change, the existing entry is returned. Use a value of -1
|
|
|
|
* for metric to delete a route.
|
|
|
|
*
|
|
|
|
* @param destination Destination IP/netmask
|
|
|
|
* @param gateway Gateway IP (netmask/port part unused) or NULL/zero for device-level route
|
|
|
|
* @param device Device name (can be null for gateway routes)
|
|
|
|
* @param metric Route metric or hop count (higher = lower priority) or negative to delete
|
|
|
|
* @return Entry or null entry on failure (or delete)
|
2014-07-15 19:32:06 +00:00
|
|
|
*/
|
2014-07-18 00:17:59 +00:00
|
|
|
virtual RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) = 0;
|
2014-07-13 19:57:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|