| 1 |
/** |
| 2 |
* @file protoRouteTable.cpp |
| 3 |
* |
| 4 |
* @brief Class based on the ProtoTree Patricia tree to store routing table information. Uses the ProtoAddress class to store network routing addresses |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "protoRouteTable.h" |
| 8 |
#include "protoDebug.h" |
| 9 |
|
| 10 |
ProtoRouteTable::ProtoRouteTable() |
| 11 |
{ |
| 12 |
} |
| 13 |
|
| 14 |
ProtoRouteTable::~ProtoRouteTable() |
| 15 |
{ |
| 16 |
Destroy(); |
| 17 |
} |
| 18 |
|
| 19 |
void ProtoRouteTable::Destroy() |
| 20 |
{ |
| 21 |
// First, get rid of entries contained in tree. |
| 22 |
Entry* next; |
| 23 |
while (NULL != (next = static_cast<Entry*>(tree.GetRoot()))) |
| 24 |
{ |
| 25 |
tree.Remove(*next); |
| 26 |
delete next; |
| 27 |
} |
| 28 |
// Second, get rid of default_entry |
| 29 |
if (default_entry.IsValid()) default_entry.Clear(); |
| 30 |
} // end ProtoRouteTable::Destroy() |
| 31 |
|
| 32 |
|
| 33 |
bool ProtoRouteTable::GetRoute(const ProtoAddress& dst, |
| 34 |
unsigned int prefixSize, |
| 35 |
ProtoAddress& gw, |
| 36 |
unsigned int& ifIndex, |
| 37 |
int& metric) |
| 38 |
{ |
| 39 |
if (0 == prefixSize) |
| 40 |
{ |
| 41 |
gw = default_entry.gateway; |
| 42 |
ifIndex = default_entry.iface_index; |
| 43 |
metric = default_entry.metric; |
| 44 |
return true; |
| 45 |
} |
| 46 |
Entry* entry = GetEntry(dst, prefixSize); |
| 47 |
if (entry) |
| 48 |
{ |
| 49 |
gw = entry->gateway; |
| 50 |
ifIndex = entry->iface_index; |
| 51 |
metric = entry->metric; |
| 52 |
return true; |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
return false; |
| 57 |
} |
| 58 |
} // end ProtoRouteTable::GetRoute() |
| 59 |
|
| 60 |
bool ProtoRouteTable::SetRoute(const ProtoAddress& dst, |
| 61 |
unsigned int prefixSize, |
| 62 |
const ProtoAddress& gw, |
| 63 |
unsigned int ifIndex, |
| 64 |
int metric) |
| 65 |
{ |
| 66 |
if (0 == prefixSize) |
| 67 |
{ |
| 68 |
|
| 69 |
default_entry.destination = dst; |
| 70 |
default_entry.gateway = gw; |
| 71 |
default_entry.iface_index = ifIndex; |
| 72 |
default_entry.metric = metric; |
| 73 |
return true; |
| 74 |
} |
| 75 |
Entry* entry = GetEntry(dst, prefixSize); |
| 76 |
// (TBD) allow for multiple routes to same dst/mask ??? |
| 77 |
if (NULL == entry) |
| 78 |
entry = CreateEntry(dst, prefixSize); |
| 79 |
if (entry) |
| 80 |
{ |
| 81 |
entry->gateway = gw; |
| 82 |
entry->iface_index = ifIndex; |
| 83 |
entry->metric = metric; |
| 84 |
return true; |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
PLOG(PL_ERROR, "ProtoRouteTable::SetRoute() error creating entry\n"); |
| 89 |
return false; |
| 90 |
} |
| 91 |
} // end ProtoRouteTable::SetRoute() |
| 92 |
|
| 93 |
bool ProtoRouteTable::FindRoute(const ProtoAddress& dst, |
| 94 |
unsigned int prefixSize, |
| 95 |
ProtoAddress& gw, |
| 96 |
unsigned int& ifIndex, |
| 97 |
int& metric) |
| 98 |
{ |
| 99 |
Entry* entry = FindRouteEntry(dst, prefixSize); |
| 100 |
if (entry) |
| 101 |
{ |
| 102 |
gw = entry->gateway; |
| 103 |
ifIndex = entry->iface_index; |
| 104 |
metric = entry->metric; |
| 105 |
return true; |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
return false; |
| 110 |
} |
| 111 |
} // end ProtoRouteTable::FindRoute() |
| 112 |
|
| 113 |
bool ProtoRouteTable::DeleteRoute(const ProtoAddress& dst, |
| 114 |
unsigned int maskLen, |
| 115 |
const ProtoAddress* gw, |
| 116 |
unsigned int index) |
| 117 |
{ |
| 118 |
Entry* entry = GetEntry(dst, maskLen); |
| 119 |
if (NULL != entry) |
| 120 |
{ |
| 121 |
if (NULL != gw) |
| 122 |
{ |
| 123 |
if(gw->IsValid() || entry->gateway.IsValid()) |
| 124 |
{ |
| 125 |
//make sure at least one of this is valid |
| 126 |
if (!entry->gateway.HostIsEqual(*gw)) |
| 127 |
{ |
| 128 |
PLOG(PL_ERROR, "ProtoRouteTable::DeleteRoute() non-matching gateway addr Provided GW=%s\n",gw->GetHostString()); |
| 129 |
PLOG(PL_ERROR, " Entry GW=%s\n",entry->gateway.GetHostString()); |
| 130 |
return false; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
if ((0 != index) && (index != entry->iface_index)) |
| 135 |
{ |
| 136 |
PLOG(PL_ERROR, "ProtoRouteTable::DeleteRoute() non-matching interface index\n"); |
| 137 |
return false; |
| 138 |
} |
| 139 |
DeleteEntry(entry); |
| 140 |
return true; |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
return false; |
| 145 |
} |
| 146 |
} // end ProtoRouteTable::DeleteRoute() |
| 147 |
|
| 148 |
ProtoRouteTable::Entry* ProtoRouteTable::CreateEntry(const ProtoAddress& dstAddr, |
| 149 |
unsigned int prefixSize) |
| 150 |
{ |
| 151 |
if (!dstAddr.IsValid()) |
| 152 |
{ |
| 153 |
PLOG(PL_ERROR, "ProtoRouteTable::CreateEntry() invalid destination addr\n"); |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
Entry* entry = new Entry(dstAddr, prefixSize); |
| 157 |
if (NULL == entry) |
| 158 |
{ |
| 159 |
PLOG(PL_ERROR, "ProtoRouteTable::CreateEntry() memory allocation error: %s\n", |
| 160 |
GetErrorString()); |
| 161 |
delete entry; |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
// Bind the item and the entry |
| 165 |
if (tree.Insert(*entry)) |
| 166 |
{ |
| 167 |
return entry; |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
PLOG(PL_ERROR, "ProtoRouteTable::CreateEntry() equivalent entry already exists?\n"); |
| 172 |
delete entry; |
| 173 |
return NULL; |
| 174 |
} |
| 175 |
} // end ProtoRouteTable::CreateEntry() |
| 176 |
|
| 177 |
ProtoRouteTable::Entry* ProtoRouteTable::GetEntry(const ProtoAddress& dstAddr, |
| 178 |
unsigned int prefixSize) const |
| 179 |
{ |
| 180 |
if (0 == prefixSize) |
| 181 |
{ |
| 182 |
if (default_entry.IsValid()) |
| 183 |
return (Entry*)&default_entry; |
| 184 |
else |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
return (static_cast<Entry*>(tree.Find(dstAddr.GetRawHostAddress(), prefixSize))); |
| 188 |
} // end ProtoRouteTable::GetEntry() |
| 189 |
|
| 190 |
ProtoRouteTable::Entry* ProtoRouteTable::FindRouteEntry(const ProtoAddress& dstAddr, |
| 191 |
unsigned int prefixSize) const |
| 192 |
{ |
| 193 |
if (0 == prefixSize) return GetDefaultEntry(); |
| 194 |
Entry* entry = static_cast<Entry*>(tree.FindPrefix(dstAddr.GetRawHostAddress(), prefixSize)); |
| 195 |
return (NULL != entry) ? entry : GetDefaultEntry(); |
| 196 |
} // end ProtoRouteTable::FindRouteEntry() |
| 197 |
|
| 198 |
void ProtoRouteTable::DeleteEntry(ProtoRouteTable::Entry* entry) |
| 199 |
{ |
| 200 |
if (NULL == entry) return; |
| 201 |
if (&default_entry == entry) |
| 202 |
{ |
| 203 |
default_entry.Clear(); |
| 204 |
return; |
| 205 |
} |
| 206 |
Entry* entryFound = static_cast<Entry*>(tree.Find(entry->GetDestination().GetRawHostAddress(), entry->GetPrefixSize())); |
| 207 |
if (entryFound == entry) |
| 208 |
{ |
| 209 |
tree.Remove(*entry); |
| 210 |
delete entry; |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
PLOG(PL_ERROR, "ProtoRouteTable::DeleteEntry() invalid entry\n"); |
| 215 |
} |
| 216 |
} // end ProtoRouteTable::DeleteEntry() |
| 217 |
|
| 218 |
ProtoRouteTable::Entry::Entry() |
| 219 |
: prefix_size(0), iface_index(0), metric(-1) |
| 220 |
{ |
| 221 |
destination.Invalidate(); |
| 222 |
gateway.Invalidate(); |
| 223 |
} |
| 224 |
|
| 225 |
ProtoRouteTable::Entry::~Entry() |
| 226 |
{ |
| 227 |
} |
| 228 |
|
| 229 |
ProtoRouteTable::Entry::Entry(const ProtoAddress& dstAddr, unsigned int prefixSize) |
| 230 |
: iface_index(0), metric(-1) |
| 231 |
{ |
| 232 |
destination = dstAddr; |
| 233 |
prefix_size = prefixSize; |
| 234 |
gateway.Invalidate(); |
| 235 |
} |
| 236 |
|
| 237 |
void ProtoRouteTable::Entry::Init(const ProtoAddress& dstAddr, unsigned int prefixSize) |
| 238 |
{ |
| 239 |
destination = dstAddr; |
| 240 |
prefix_size = prefixSize; |
| 241 |
gateway.Invalidate(); |
| 242 |
iface_index = 0; |
| 243 |
metric = -1; |
| 244 |
} // end ProtoRouteTable::Entry::Init() |
| 245 |
|
| 246 |
ProtoRouteTable::Iterator::Iterator(ProtoRouteTable& theTable) |
| 247 |
: table(theTable), iterator(theTable.tree), default_pending(true) |
| 248 |
{ |
| 249 |
|
| 250 |
} |
| 251 |
ProtoRouteTable::Entry* ProtoRouteTable::Iterator::GetNextEntry() |
| 252 |
{ |
| 253 |
if (default_pending) |
| 254 |
{ |
| 255 |
default_pending = false; |
| 256 |
Entry* entry = (Entry*)table.GetDefaultEntry(); |
| 257 |
if (entry) return entry; |
| 258 |
} |
| 259 |
return (static_cast<Entry*>(iterator.GetNextItem())); |
| 260 |
} // end ProtoRouteTable::Iterator::GetNextEntry() |