| 1 |
#ifndef _PROTO_ROUTE_TABLE |
| 2 |
#define _PROTO_ROUTE_TABLE |
| 3 |
|
| 4 |
#include "protoAddress.h" |
| 5 |
#include "protoTree.h" |
| 6 |
|
| 7 |
/** |
| 8 |
* @class ProtoRouteTable |
| 9 |
* |
| 10 |
* @brief Class based on the ProtoTree Patricia tree to |
| 11 |
* store routing table information. Uses the |
| 12 |
* ProtoAddress class to store network routing |
| 13 |
* addresses. |
| 14 |
* |
| 15 |
* It's a pretty dumbed-down routing |
| 16 |
* table at the moment, but may be enhanced in |
| 17 |
* the future. Example use of the ProtoTree. |
| 18 |
* |
| 19 |
* Notes: |
| 20 |
* |
| 21 |
* 1) Only one entry per destination/prefixLen is currently allowed. |
| 22 |
* (Only one route per unique destination is maintained) |
| 23 |
* (We may support multiple routes per dest in the future) |
| 24 |
* |
| 25 |
* 2) (ifIndex == 0) and (metric < 0) are "wildcards" |
| 26 |
*/ |
| 27 |
class ProtoRouteTable |
| 28 |
{ |
| 29 |
public: |
| 30 |
ProtoRouteTable(); |
| 31 |
~ProtoRouteTable(); |
| 32 |
|
| 33 |
void Init() {Destroy();} |
| 34 |
void Destroy(); |
| 35 |
|
| 36 |
bool IsEmpty() {return (tree.IsEmpty() && !default_entry.IsValid());} |
| 37 |
|
| 38 |
// For "GetRoute()" to work there _must_ be |
| 39 |
// an exact match (dst and prefixLen) route entry in the table. |
| 40 |
bool GetRoute(const ProtoAddress& dst, // input |
| 41 |
unsigned int prefixLen, // input |
| 42 |
ProtoAddress& gw, // output |
| 43 |
unsigned int& ifIndex, // output |
| 44 |
int& metric); // output |
| 45 |
|
| 46 |
bool SetRoute(const ProtoAddress& dst, |
| 47 |
unsigned int maskLen, |
| 48 |
const ProtoAddress& gw, |
| 49 |
unsigned int ifIndex = 0, |
| 50 |
int metric = -1); |
| 51 |
|
| 52 |
// Note gateway is optional here |
| 53 |
bool DeleteRoute(const ProtoAddress& dst, |
| 54 |
unsigned int maskLen, |
| 55 |
const ProtoAddress* gw = NULL, |
| 56 |
unsigned int ifIndex = 0); |
| 57 |
|
| 58 |
// Find the "best" route to the given destination |
| 59 |
// (Finds route entry with longest matching prefix (or default)) |
| 60 |
bool FindRoute(const ProtoAddress& dstAddr, |
| 61 |
unsigned int prefixLen, |
| 62 |
ProtoAddress& gwAddr, |
| 63 |
unsigned int& ifIndex, |
| 64 |
int& metric); |
| 65 |
|
| 66 |
class Entry : public ProtoTree::Item |
| 67 |
{ |
| 68 |
friend class ProtoRouteTable; |
| 69 |
|
| 70 |
public: |
| 71 |
bool IsValid() const |
| 72 |
{return destination.IsValid();} |
| 73 |
bool IsDefault() const |
| 74 |
{return (0 == GetKeysize());} |
| 75 |
bool IsGatewayRoute() const |
| 76 |
{return gateway.IsValid();} |
| 77 |
bool IsDirectRoute() const |
| 78 |
{return (!gateway.IsValid() && iface_index > 0);} |
| 79 |
void SetGateway(const ProtoAddress& gwAddr) |
| 80 |
{gateway = gwAddr;} |
| 81 |
void ClearGateway() |
| 82 |
{gateway.Invalidate();} |
| 83 |
void SetInterface(unsigned int ifaceIndex) |
| 84 |
{iface_index = ifaceIndex;} |
| 85 |
void ClearInterface() |
| 86 |
{iface_index = 0;} |
| 87 |
void SetMetric(int value) |
| 88 |
{metric = value;} |
| 89 |
|
| 90 |
const ProtoAddress& GetDestination() const {return destination;} |
| 91 |
unsigned int GetPrefixSize() const {return prefix_size;} // in bits |
| 92 |
const ProtoAddress& GetGateway() const {return gateway;} |
| 93 |
unsigned int GetInterfaceIndex() const {return iface_index;} |
| 94 |
int GetMetric() const {return metric;} |
| 95 |
|
| 96 |
void Clear() |
| 97 |
{ |
| 98 |
destination.Invalidate(); |
| 99 |
prefix_size = 0; |
| 100 |
gateway.Invalidate(); |
| 101 |
iface_index = 0; |
| 102 |
metric = -1; |
| 103 |
} |
| 104 |
|
| 105 |
// Required ProtoTree::Item overrides |
| 106 |
const char* GetKey() const {return destination.GetRawHostAddress();} |
| 107 |
unsigned int GetKeysize() const {return prefix_size;} |
| 108 |
|
| 109 |
private: |
| 110 |
Entry(); |
| 111 |
Entry(const ProtoAddress& dstAddr, unsigned int prefixSize); |
| 112 |
Entry(const Entry& entry) |
| 113 |
{*this = entry;} |
| 114 |
~Entry(); |
| 115 |
|
| 116 |
void Init(const ProtoAddress& dstAddr, unsigned int prefixSize); |
| 117 |
|
| 118 |
ProtoAddress destination; |
| 119 |
unsigned int prefix_size; // in bits |
| 120 |
ProtoAddress gateway; |
| 121 |
unsigned int iface_index; |
| 122 |
int metric; |
| 123 |
}; // end class ProtoRouteTable::Entry |
| 124 |
|
| 125 |
class Iterator |
| 126 |
{ |
| 127 |
public: |
| 128 |
Iterator(ProtoRouteTable& table); |
| 129 |
void Reset() |
| 130 |
{ |
| 131 |
iterator.Reset(); |
| 132 |
default_pending = true; |
| 133 |
} |
| 134 |
Entry* GetNextEntry(); |
| 135 |
|
| 136 |
private: |
| 137 |
const ProtoRouteTable& table; |
| 138 |
ProtoTree::Iterator iterator; |
| 139 |
bool default_pending; |
| 140 |
}; // end class ProtoRouteTable::Iterator |
| 141 |
friend class ProtoRouteTable::Iterator; |
| 142 |
|
| 143 |
ProtoRouteTable::Entry* CreateEntry(const ProtoAddress& dstAddr, |
| 144 |
unsigned int prefixLen); |
| 145 |
|
| 146 |
// Get entry exactly matching dstAddr/prefixLen |
| 147 |
ProtoRouteTable::Entry* GetEntry(const ProtoAddress& dstAddr, |
| 148 |
unsigned int prefixLen) const; |
| 149 |
|
| 150 |
// Find best matching route entry |
| 151 |
ProtoRouteTable::Entry* FindRouteEntry(const ProtoAddress& dstAddr, |
| 152 |
unsigned int prefixLen) const; |
| 153 |
|
| 154 |
ProtoRouteTable::Entry* GetDefaultEntry() const |
| 155 |
{return (default_entry.IsValid() ? (Entry*)&default_entry : NULL);} |
| 156 |
|
| 157 |
void DeleteEntry(ProtoRouteTable::Entry* entry); |
| 158 |
|
| 159 |
// IMPORTANT - cannot use these two for default route entries |
| 160 |
void InsertEntry(ProtoRouteTable::Entry& entry) |
| 161 |
{tree.Insert(entry);} |
| 162 |
void RemoveEntry(ProtoRouteTable::Entry& entry) |
| 163 |
{tree.Remove(entry);} |
| 164 |
|
| 165 |
private: |
| 166 |
ProtoTree tree; |
| 167 |
Entry default_entry; |
| 168 |
}; // end class ProtoRouteTable |
| 169 |
|
| 170 |
#endif // _PROTO_ROUTE_TABLE |