| 1 |
|
| 2 |
#include "protoPktRIP.h" |
| 3 |
#include <string.h> // for memset(), etc |
| 4 |
|
| 5 |
ProtoPktRIP::ProtoPktRIP(void* bufferPtr, |
| 6 |
unsigned int numBytes, |
| 7 |
unsigned int pktLength, |
| 8 |
bool freeOnDestruct) |
| 9 |
: ProtoPkt(bufferPtr, numBytes, freeOnDestruct) |
| 10 |
{ |
| 11 |
if (0 != pktLength) |
| 12 |
InitFromBuffer(pktLength); |
| 13 |
else |
| 14 |
InitIntoBuffer(); |
| 15 |
} |
| 16 |
|
| 17 |
ProtoPktRIP::~ProtoPktRIP() |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
bool ProtoPktRIP::InitIntoBuffer(void* bufferPtr, |
| 22 |
unsigned int numBytes, |
| 23 |
bool freeOnDestruct) |
| 24 |
{ |
| 25 |
if (NULL != bufferPtr) |
| 26 |
{ |
| 27 |
if (numBytes < (4*OFFSET_PAYLOAD)) |
| 28 |
return false; |
| 29 |
else |
| 30 |
AttachBuffer(bufferPtr, numBytes, freeOnDestruct); |
| 31 |
} |
| 32 |
else if (GetBufferLength() < (4*OFFSET_PAYLOAD)) |
| 33 |
{ |
| 34 |
return false; |
| 35 |
} |
| 36 |
SetCommand(INVALID); |
| 37 |
SetVersion(2); |
| 38 |
SetLength(4*OFFSET_PAYLOAD); |
| 39 |
return true; |
| 40 |
} // end ProtoPktRIP::InitIntoBuffer() |
| 41 |
|
| 42 |
bool ProtoPktRIP::AddRouteEntry(const ProtoAddress& destAddr, |
| 43 |
UINT32 maskLen, |
| 44 |
const ProtoAddress& nextHop, |
| 45 |
UINT32 metric, |
| 46 |
UINT16 routeTag) |
| 47 |
{ |
| 48 |
// We only support IPv4 for now |
| 49 |
if (ProtoAddress::IPv4 != destAddr.GetType() || |
| 50 |
ProtoAddress::IPv4 != nextHop.GetType()) |
| 51 |
{ |
| 52 |
PLOG(PL_ERROR, "ProtoPktRIP::AddRouteEntry() error: invalid address type\n"); |
| 53 |
return false; |
| 54 |
} |
| 55 |
if (maskLen > 32) |
| 56 |
{ |
| 57 |
PLOG(PL_ERROR, "ProtoPktRIP::AddRouteEntry() error: invalid mask length\n"); |
| 58 |
return false; |
| 59 |
} |
| 60 |
if (metric > 16) |
| 61 |
{ |
| 62 |
PLOG(PL_ERROR, "ProtoPktRIP::AddRouteEntry() error: invalid metric value\n"); |
| 63 |
return false; |
| 64 |
} |
| 65 |
// Is there space for another entry? |
| 66 |
unsigned space = GetBufferLength() - GetLength(); |
| 67 |
if (space < 20) // RIP route entries are 20 bytes |
| 68 |
{ |
| 69 |
PLOG(PL_WARN, "ProtoPktRIP::AddRouteEntry() warning: insufficient buffer space\n"); |
| 70 |
return false; |
| 71 |
} |
| 72 |
ProtoPktRIP::RouteEntry entry((char*)AccessBuffer() + GetLength(), 20); |
| 73 |
entry.SetAddressFamily(IPv4); |
| 74 |
entry.SetRouteTag(routeTag); |
| 75 |
entry.SetAddress(destAddr); |
| 76 |
entry.SetMaskLength(maskLen); |
| 77 |
entry.SetNextHop(nextHop); |
| 78 |
entry.SetMetric(metric); |
| 79 |
SetLength(GetLength() + 20); |
| 80 |
return true; |
| 81 |
} // end ProtoPktRIP::AddRouteEntry() |
| 82 |
|
| 83 |
bool ProtoPktRIP::InitFromBuffer(unsigned int pktLength, |
| 84 |
void* bufferPtr, |
| 85 |
unsigned int numBytes, |
| 86 |
bool freeOnDestruct) |
| 87 |
{ |
| 88 |
if (NULL != bufferPtr) |
| 89 |
{ |
| 90 |
if (numBytes < pktLength) // UDP header size |
| 91 |
return false; |
| 92 |
else |
| 93 |
AttachBuffer(bufferPtr, numBytes, freeOnDestruct); |
| 94 |
} |
| 95 |
if (GetBufferLength() < pktLength) |
| 96 |
{ |
| 97 |
PLOG(PL_ERROR, "ProtoPktRIP::InitFromBuffer() error: insufficient buffer size\n"); |
| 98 |
return false; |
| 99 |
} |
| 100 |
if (pktLength < (4*OFFSET_PAYLOAD)) |
| 101 |
{ |
| 102 |
PLOG(PL_ERROR, "ProtoPktRIP::InitFromBuffer() error: truncated packet? (pktLen:%u offset:%u\n", pktLength, 4*OFFSET_PAYLOAD); |
| 103 |
return false; |
| 104 |
} |
| 105 |
SetLength(pktLength); |
| 106 |
return true; |
| 107 |
} // end ProtoPktRIP::InitFromBuffer() |
| 108 |
|
| 109 |
unsigned int ProtoPktRIP::GetNumEntry() const |
| 110 |
{ |
| 111 |
unsigned int pktLen = GetLength(); |
| 112 |
if (pktLen < (4*OFFSET_PAYLOAD)) |
| 113 |
return 0; |
| 114 |
else |
| 115 |
pktLen -= 4*OFFSET_PAYLOAD; |
| 116 |
return pktLen / 20; // RIP is 20 bytes per entry |
| 117 |
} // end ProtoPktRIP::GetNumEntry() |
| 118 |
|
| 119 |
bool ProtoPktRIP::AccessRouteEntry(unsigned int index, RouteEntry& entry) |
| 120 |
{ |
| 121 |
if (index > (GetNumEntry() - 1)) |
| 122 |
{ |
| 123 |
PLOG(PL_ERROR, "ProtoPktRIP::AccessRouteEntry() error: invalid route entry index\n"); |
| 124 |
return false; |
| 125 |
} |
| 126 |
// Compute UINT32* pointer (20 byte entry is 5 UINT32's) |
| 127 |
void* entryBuffer = AccessBuffer32(OFFSET_PAYLOAD + 5*index); |
| 128 |
return entry.InitFromBuffer(20, entryBuffer, 20); |
| 129 |
} // end ProtoPktRIP::AccessRouteEntry() |
| 130 |
|
| 131 |
ProtoPktRIP::RouteEntry::RouteEntry(void* bufferPtr, |
| 132 |
unsigned int numBytes, |
| 133 |
bool initFromBuffer, |
| 134 |
bool freeOnDestruct) |
| 135 |
: ProtoPkt(bufferPtr, numBytes, freeOnDestruct) |
| 136 |
{ |
| 137 |
if (initFromBuffer) |
| 138 |
InitFromBuffer(numBytes); |
| 139 |
else |
| 140 |
InitIntoBuffer(); |
| 141 |
} |
| 142 |
|
| 143 |
ProtoPktRIP::RouteEntry::~RouteEntry() |
| 144 |
{ |
| 145 |
} |
| 146 |
|
| 147 |
bool ProtoPktRIP::RouteEntry::InitIntoBuffer(void* bufferPtr, |
| 148 |
unsigned int numBytes, |
| 149 |
bool freeOnDestruct) |
| 150 |
{ |
| 151 |
if (NULL != bufferPtr) |
| 152 |
{ |
| 153 |
if (numBytes < 20) |
| 154 |
return false; |
| 155 |
else |
| 156 |
AttachBuffer(bufferPtr, numBytes, freeOnDestruct); |
| 157 |
} |
| 158 |
else if (GetBufferLength() < 20) |
| 159 |
{ |
| 160 |
return false; |
| 161 |
} |
| 162 |
memset(AccessBuffer(), 0, 20); |
| 163 |
SetLength(20); |
| 164 |
return true; |
| 165 |
} // end ProtoPktRIP::RouteEntry::InitIntoBuffer() |
| 166 |
|
| 167 |
bool ProtoPktRIP::RouteEntry::SetAddress(const ProtoAddress& addr) |
| 168 |
{ |
| 169 |
if (ProtoAddress::IPv4 != addr.GetType()) |
| 170 |
{ |
| 171 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::SetAddress() error: invalid address type\n"); |
| 172 |
return false; |
| 173 |
} |
| 174 |
memcpy(AccessBuffer(OFFSET_ADDR), addr.GetRawHostAddress(), 4); |
| 175 |
return true; |
| 176 |
} // end ProtoPktRIP::RouteEntry::SetAddress() |
| 177 |
|
| 178 |
bool ProtoPktRIP::RouteEntry::SetMask(const ProtoAddress& addr) |
| 179 |
{ |
| 180 |
if (ProtoAddress::IPv4 != addr.GetType()) |
| 181 |
{ |
| 182 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::SetMask() error: invalid mask address\n"); |
| 183 |
return false; |
| 184 |
} |
| 185 |
memcpy(AccessBuffer32(OFFSET_MASK), addr.GetRawHostAddress(), 4); |
| 186 |
return true; |
| 187 |
} // end ProtoPktRIP::RouteEntry::SetMaskLength() |
| 188 |
|
| 189 |
bool ProtoPktRIP::RouteEntry::SetMaskLength(UINT8 maskLen) |
| 190 |
{ |
| 191 |
if (maskLen > 32) |
| 192 |
{ |
| 193 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::SetMaskLength() error: invalid mask length\n"); |
| 194 |
return false; |
| 195 |
} |
| 196 |
ProtoAddress maskAddr; |
| 197 |
maskAddr.GeneratePrefixMask(ProtoAddress::IPv4, maskLen); |
| 198 |
return SetMask(maskAddr); |
| 199 |
} |
| 200 |
|
| 201 |
bool ProtoPktRIP::RouteEntry::SetNextHop(const ProtoAddress& nextHop) |
| 202 |
{ |
| 203 |
if (ProtoAddress::IPv4 != nextHop.GetType()) |
| 204 |
{ |
| 205 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::SetAddress() error: invalid address type\n"); |
| 206 |
return false; |
| 207 |
} |
| 208 |
memcpy(AccessBuffer32(OFFSET_NHOP), nextHop.GetRawHostAddress(), 4); |
| 209 |
return true; |
| 210 |
} // end ProtoPktRIP::RouteEntry::SetNextHop() |
| 211 |
|
| 212 |
bool ProtoPktRIP::RouteEntry::InitFromBuffer(unsigned int pktLength, |
| 213 |
void* bufferPtr, |
| 214 |
unsigned int numBytes, |
| 215 |
bool freeOnDestruct) |
| 216 |
{ |
| 217 |
if (NULL != bufferPtr) |
| 218 |
{ |
| 219 |
if (numBytes < pktLength) // UDP header size |
| 220 |
return false; |
| 221 |
else |
| 222 |
AttachBuffer(bufferPtr, numBytes, freeOnDestruct); |
| 223 |
} |
| 224 |
if (GetBufferLength() < pktLength) |
| 225 |
{ |
| 226 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::InitFromBuffer() error: insufficient buffer size\n"); |
| 227 |
return false; |
| 228 |
} |
| 229 |
if (pktLength < 20) |
| 230 |
{ |
| 231 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::InitFromBuffer() error: truncated packet?\n"); |
| 232 |
return false; |
| 233 |
} |
| 234 |
SetLength(20); |
| 235 |
return true; |
| 236 |
} // end ProtoPktRIP::RouteEntry::InitFromBuffer() |
| 237 |
|
| 238 |
bool ProtoPktRIP::RouteEntry::GetAddress(ProtoAddress& addr) const |
| 239 |
{ |
| 240 |
if (IPv4 != GetAddressFamily()) |
| 241 |
{ |
| 242 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::GetAddress() error: invalid address family: %d\n", GetAddressFamily()); |
| 243 |
return false; |
| 244 |
} |
| 245 |
addr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer32(OFFSET_ADDR), 4); |
| 246 |
return true; |
| 247 |
} // end ProtoPktRIP::RouteEntry::GetAddress() |
| 248 |
|
| 249 |
bool ProtoPktRIP::RouteEntry::GetMask(ProtoAddress& addr) const |
| 250 |
{ |
| 251 |
if (IPv4 != GetAddressFamily()) |
| 252 |
{ |
| 253 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::GetMask() error: invalid address family\n"); |
| 254 |
return 0; |
| 255 |
} |
| 256 |
addr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer32(OFFSET_MASK), 4); |
| 257 |
return true; |
| 258 |
} // end ProtoPktRIP::RouteEntry::GetMask() |
| 259 |
|
| 260 |
UINT8 ProtoPktRIP::RouteEntry::GetMaskLength() const |
| 261 |
{ |
| 262 |
ProtoAddress maskAddr; |
| 263 |
if (!GetMask(maskAddr)) |
| 264 |
{ |
| 265 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::GetMaskLength() error: invalid address family\n"); |
| 266 |
return 0; |
| 267 |
} |
| 268 |
return maskAddr.GetPrefixLength(); |
| 269 |
} // end ProtoPktRIP::RouteEntry::GetMaskLength() |
| 270 |
|
| 271 |
bool ProtoPktRIP::RouteEntry::GetNextHop(ProtoAddress& addr) const |
| 272 |
{ |
| 273 |
if (IPv4 != GetAddressFamily()) |
| 274 |
{ |
| 275 |
PLOG(PL_ERROR, "ProtoPktRIP::RouteEntry::GetNextHop() error: invalid address family\n"); |
| 276 |
return 0; |
| 277 |
} |
| 278 |
addr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer32(OFFSET_NHOP), 4); |
| 279 |
return true; |
| 280 |
} // end ProtoPktRIP::RouteEntry::GetNextHop() |
| 281 |
|
| 282 |
|