| 1 |
#ifndef _PROTO_ROUTE_MGR |
| 2 |
#define _PROTO_ROUTE_MGR |
| 3 |
|
| 4 |
#include "protoRouteTable.h" |
| 5 |
#include "protoSocket.h" |
| 6 |
|
| 7 |
/** |
| 8 |
* @class ProtoRouteMgr |
| 9 |
* |
| 10 |
* @brief Base class for providing a consistent |
| 11 |
* interface to manage operating system (or |
| 12 |
* other) routing engines. |
| 13 |
* |
| 14 |
* Note: Since ProtoRouteTree can handle only one route per destination |
| 15 |
* GetAllRoutes() may miss some routes. (Our bsdRouteMgr, linuxRouteMgr |
| 16 |
* and win32RouteMgr code has been tuned to deal with this for the |
| 17 |
* moment). We'll likely extend ProtoRouteTree soon. |
| 18 |
*/ |
| 19 |
class ProtoRouteMgr |
| 20 |
{ |
| 21 |
public: |
| 22 |
enum Type |
| 23 |
{ |
| 24 |
SYSTEM, |
| 25 |
ZEBRA |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
static ProtoRouteMgr* Create(Type type = SYSTEM); |
| 30 |
|
| 31 |
virtual ~ProtoRouteMgr(); |
| 32 |
|
| 33 |
virtual bool Open(const void* userData = NULL) = 0; |
| 34 |
virtual bool IsOpen() const = 0; |
| 35 |
virtual void Close() = 0; |
| 36 |
|
| 37 |
virtual bool GetAllRoutes(ProtoAddress::Type addrType, |
| 38 |
ProtoRouteTable& routeTable) = 0; |
| 39 |
bool DeleteAllRoutes(); |
| 40 |
bool DeleteAllRoutes(ProtoAddress::Type addrType, unsigned int maxIterations = 8); |
| 41 |
bool SetRoutes(ProtoRouteTable& routeTable); |
| 42 |
/** |
| 43 |
* @brief Entries in the route table will be updated to reflect the new route table*. |
| 44 |
* Routes which existe in the old route table but not the new one will be removed. |
| 45 |
* Routes which exist in the new route table but not the old will be added. |
| 46 |
* Any routes with differing parameters will be updated. |
| 47 |
* @param oldRouteTable The older set of routes which are to be diff'ed against. |
| 48 |
* @param newRouteTable The new/current routes which are to be added. |
| 49 |
* @param settedRouteTable If non-null pointer is supplied the list will be populated with routes which were added/updated |
| 50 |
* @param deletedRouteTable If non-null pointer is supplied the list will be populated with routes which were deleted |
| 51 |
* @return true upon success. |
| 52 |
*/ |
| 53 |
bool UpdateRoutes(ProtoRouteTable& oldRouteTable, ProtoRouteTable& newRouteTable, ProtoRouteTable* settedRouteTable = NULL, ProtoRouteTable* deletedRouteTable = NULL); |
| 54 |
/** |
| 55 |
* @brief Entries in the route table will be updated to reflect the new route table*. |
| 56 |
* Routes which existe in the old route table but not the new one will be removed. |
| 57 |
* Routes which exist in the new route table but not the old will be added. |
| 58 |
* Any routes with differing parameters will be updated. |
| 59 |
* @param oldRouteTable The older set of routes which are to be diff'ed against. |
| 60 |
* @param newRouteTable The new/current routes which are to be added. |
| 61 |
* @param settedRouteTable The list will be populated with routes which should be added |
| 62 |
* @param deletedRouteTable The list will be populated with routes which should be deleted |
| 63 |
* @return true upon success. |
| 64 |
*/ |
| 65 |
bool GetDiff(ProtoRouteTable& oldRouteTable, ProtoRouteTable& newRouteTable, ProtoRouteTable& settedRouteTable, ProtoRouteTable& deletedRouteTable); |
| 66 |
bool DeleteRoutes(ProtoRouteTable& routeTable); |
| 67 |
/** |
| 68 |
* |
| 69 |
* @brief will save IPv4 and IPv6 route tables |
| 70 |
* @return true upon success of saving EITHER IPv4 or IPv6 route tables |
| 71 |
*/ |
| 72 |
bool SaveAllRoutes(); |
| 73 |
/** |
| 74 |
* @brief function will read the current route table and save the routing state in a proto route table |
| 75 |
* @param addrType the type of route table to save |
| 76 |
* @return true upon success |
| 77 |
*/ |
| 78 |
bool SaveAllRoutes(ProtoAddress::Type addrType); |
| 79 |
/** |
| 80 |
* @brief will attempt to restore both IPv4 and IPv6 route tables |
| 81 |
* @return true upon success of restoring EITHER IPv4 or IPv6 route tables. |
| 82 |
*/ |
| 83 |
bool RestoreSavedRoutes(); |
| 84 |
/** |
| 85 |
* @brief if the routes have been saved then the route table will be updated with the saved routes |
| 86 |
* @param addrType the type of route table to save |
| 87 |
* @return will return true upon success. Will return false if no route state was previously saved. |
| 88 |
*/ |
| 89 |
bool RestoreSavedRoutes(ProtoAddress::Type addrType); |
| 90 |
|
| 91 |
void ClearSavedRoutes(); |
| 92 |
|
| 93 |
virtual bool GetRoute(const ProtoAddress& dst, |
| 94 |
unsigned int prefixLen, |
| 95 |
ProtoAddress& gw, |
| 96 |
unsigned int& ifIndex, |
| 97 |
int& metric) = 0; |
| 98 |
|
| 99 |
virtual bool SetRoute(const ProtoAddress& dst, |
| 100 |
unsigned int prefixLen, |
| 101 |
const ProtoAddress& gw, |
| 102 |
unsigned int ifIndex, |
| 103 |
int metric) = 0; |
| 104 |
|
| 105 |
virtual bool DeleteRoute(const ProtoAddress& dst, |
| 106 |
unsigned int prefixLen, |
| 107 |
const ProtoAddress& gw, |
| 108 |
unsigned int ifIndex) = 0; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
bool SetRoute(ProtoRouteTable::Entry& entry) |
| 113 |
{ |
| 114 |
return SetRoute(entry.GetDestination(), |
| 115 |
entry.GetPrefixSize(), |
| 116 |
entry.GetGateway(), |
| 117 |
entry.GetInterfaceIndex(), |
| 118 |
entry.GetMetric()); |
| 119 |
} |
| 120 |
|
| 121 |
bool DeleteRoute(ProtoRouteTable::Entry& entry) |
| 122 |
{ |
| 123 |
return DeleteRoute(entry.GetDestination(), |
| 124 |
entry.GetPrefixSize(), |
| 125 |
entry.GetGateway(), |
| 126 |
entry.GetInterfaceIndex()); |
| 127 |
} |
| 128 |
|
| 129 |
// Here are some "shortcut" set route methods |
| 130 |
bool SetHostRoute(const ProtoAddress& dst, |
| 131 |
const ProtoAddress& gw, |
| 132 |
unsigned int ifIndex, |
| 133 |
int metric) |
| 134 |
{ |
| 135 |
return SetRoute(dst, (dst.GetLength() << 3), gw, ifIndex, metric); |
| 136 |
} |
| 137 |
|
| 138 |
bool SetDirectHostRoute(const ProtoAddress& dst, |
| 139 |
unsigned int ifIndex, |
| 140 |
int metric) |
| 141 |
{ |
| 142 |
ProtoAddress gw; |
| 143 |
gw.Invalidate(); |
| 144 |
return SetRoute(dst, (dst.GetLength() << 3), gw, ifIndex, metric); |
| 145 |
} |
| 146 |
|
| 147 |
bool SetNetRoute(const ProtoAddress& dst, |
| 148 |
unsigned int prefixLen, |
| 149 |
const ProtoAddress& gw, |
| 150 |
unsigned int ifIndex, |
| 151 |
int metric) |
| 152 |
{ |
| 153 |
return SetRoute(dst, prefixLen, gw, ifIndex, metric); |
| 154 |
} |
| 155 |
|
| 156 |
bool SetDirectNetRoute(const ProtoAddress& dst, |
| 157 |
unsigned int prefixLen, |
| 158 |
unsigned int ifIndex, |
| 159 |
int metric) |
| 160 |
{ |
| 161 |
ProtoAddress gw; |
| 162 |
gw.Invalidate(); |
| 163 |
return SetRoute(dst, prefixLen, gw, ifIndex, metric); |
| 164 |
} |
| 165 |
|
| 166 |
// Turn IP forward on/off |
| 167 |
virtual bool SetForwarding(bool state) = 0; |
| 168 |
|
| 169 |
// Network interface helper functions |
| 170 |
bool GetInterfaceAddress(unsigned int ifIndex, |
| 171 |
ProtoAddress::Type addrType, |
| 172 |
ProtoAddress& theAddress) |
| 173 |
{ |
| 174 |
ProtoAddressList addrList; |
| 175 |
GetInterfaceAddressList(ifIndex, addrType, addrList); |
| 176 |
return addrList.GetFirstAddress(theAddress); |
| 177 |
} |
| 178 |
virtual unsigned int GetInterfaceIndex(const char* interfaceName) = 0; |
| 179 |
virtual bool GetInterfaceName(unsigned int interfaceIndex, |
| 180 |
char* buffer, |
| 181 |
unsigned int buflen) = 0; |
| 182 |
|
| 183 |
// Retrieve all addresses for a given interface |
| 184 |
// (the retrieved addresses are added to the "addrList" provided) |
| 185 |
virtual bool GetInterfaceAddressList(unsigned int ifIndex, |
| 186 |
ProtoAddress::Type addrType, |
| 187 |
ProtoAddressList& addrList) = 0; |
| 188 |
|
| 189 |
protected: |
| 190 |
ProtoRouteMgr(); |
| 191 |
|
| 192 |
private: |
| 193 |
ProtoRouteTable* savedRoutesIPv4; |
| 194 |
ProtoRouteTable* savedRoutesIPv6; |
| 195 |
}; // end class ProtoRouteMgr |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
#endif // _PROTO_ROUTE_MGR |