| 1 |
/** |
| 2 |
* @file protoPktARP.cpp |
| 3 |
* |
| 4 |
* @brief This class provides access to ARP packets |
| 5 |
*/ |
| 6 |
#include "protoPktARP.h" |
| 7 |
#include "protoDebug.h" |
| 8 |
|
| 9 |
ProtoPktARP::ProtoPktARP(void* bufferPtr, |
| 10 |
unsigned int numBytes, |
| 11 |
bool initFromBuffer, |
| 12 |
bool freeOnDestruct) |
| 13 |
: ProtoPkt(bufferPtr, numBytes, freeOnDestruct) |
| 14 |
{ |
| 15 |
if (initFromBuffer) |
| 16 |
InitFromBuffer(); |
| 17 |
else |
| 18 |
InitIntoBuffer(); |
| 19 |
} |
| 20 |
|
| 21 |
ProtoPktARP::~ProtoPktARP() |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
bool ProtoPktARP::InitFromBuffer(void* bufferPtr, |
| 26 |
unsigned int numBytes, |
| 27 |
bool freeOnDestruct) |
| 28 |
{ |
| 29 |
unsigned int minLength = OFFSET_SNDR_HRD_ADDR; |
| 30 |
if (NULL != bufferPtr) |
| 31 |
{ |
| 32 |
if (numBytes < minLength) // UDP header size |
| 33 |
return false; |
| 34 |
else |
| 35 |
AttachBuffer(bufferPtr, numBytes, freeOnDestruct); |
| 36 |
} |
| 37 |
if (GetBufferLength() < minLength) |
| 38 |
{ |
| 39 |
PLOG(PL_ERROR, "ProtoPktARP::InitFromBuffer() error: insufficient buffer size\n"); |
| 40 |
return false; |
| 41 |
} |
| 42 |
minLength += 2*(GetHardwareAddrLen() + GetProtocolAddrLen()); |
| 43 |
if (GetBufferLength() < minLength) |
| 44 |
{ |
| 45 |
PLOG(PL_ERROR, "ProtoPktARP::InitFromBuffer() error: invalid packet\n"); |
| 46 |
return false; |
| 47 |
} |
| 48 |
SetLength(minLength); |
| 49 |
return true; |
| 50 |
} // end ProtoPktARP::InitFromBuffer() |
| 51 |
|
| 52 |
bool ProtoPktARP::GetSenderHardwareAddress(ProtoAddress& addr) const |
| 53 |
{ |
| 54 |
switch (GetHardwareType()) |
| 55 |
{ |
| 56 |
case ETHERNET: |
| 57 |
case IEEE802: |
| 58 |
addr.SetRawHostAddress(ProtoAddress::ETH, (char*)GetBuffer(OffsetSenderHardwareAddr()), GetHardwareAddrLen()); |
| 59 |
break; |
| 60 |
default: |
| 61 |
PLOG(PL_ERROR, "ProtoPktARP::GetSenderHardwareAddress() error: unsupported hardware type\n"); |
| 62 |
return false; |
| 63 |
} |
| 64 |
return true; |
| 65 |
} // end ProtoPktARP::GetSenderHardwareAddress() |
| 66 |
|
| 67 |
bool ProtoPktARP::GetSenderProtocolAddress(ProtoAddress& addr) const |
| 68 |
{ |
| 69 |
switch (GetEtherType()) |
| 70 |
{ |
| 71 |
case ProtoPktETH::IP: |
| 72 |
case ProtoPktETH::IPv6: |
| 73 |
if (4 == GetProtocolAddrLen()) |
| 74 |
{ |
| 75 |
addr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer(OffsetSenderProtocolAddr()), 4); |
| 76 |
} |
| 77 |
else if (16 == GetProtocolAddrLen()) |
| 78 |
{ |
| 79 |
addr.SetRawHostAddress(ProtoAddress::IPv6, (char*)GetBuffer(OffsetSenderProtocolAddr()), 16); |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
PLOG(PL_ERROR, "ProtoPktARP::GetSenderProtocolAddress() error: invalid IP address length\n"); |
| 84 |
return false; |
| 85 |
} |
| 86 |
break; |
| 87 |
default: |
| 88 |
PLOG(PL_ERROR, "ProtoPktARP::GetSenderProtocolAddress() error: unsupported hardware type\n"); |
| 89 |
return false; |
| 90 |
} |
| 91 |
return true; |
| 92 |
} // end ProtoPktARP::GetSenderProtocolAddress() |
| 93 |
|
| 94 |
bool ProtoPktARP::GetTargetHardwareAddress(ProtoAddress& addr) const |
| 95 |
{ |
| 96 |
switch (GetHardwareType()) |
| 97 |
{ |
| 98 |
case ETHERNET: |
| 99 |
case IEEE802: |
| 100 |
addr.SetRawHostAddress(ProtoAddress::ETH, (char*)GetBuffer(OffsetTargetHardwareAddr()), GetHardwareAddrLen()); |
| 101 |
break; |
| 102 |
default: |
| 103 |
PLOG(PL_ERROR, "ProtoPktARP::GetTargetHardwareAddress() error: unsupported hardware type\n"); |
| 104 |
return false; |
| 105 |
} |
| 106 |
return true; |
| 107 |
} // end ProtoPktARP::GetTargetHardwareAddress() |
| 108 |
|
| 109 |
bool ProtoPktARP::GetTargetProtocolAddress(ProtoAddress& addr) const |
| 110 |
{ |
| 111 |
switch (GetEtherType()) |
| 112 |
{ |
| 113 |
case ProtoPktETH::IP: |
| 114 |
case ProtoPktETH::IPv6: |
| 115 |
if (4 == GetProtocolAddrLen()) |
| 116 |
{ |
| 117 |
addr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer(OffsetTargetProtocolAddr()), 4); |
| 118 |
} |
| 119 |
else if (16 == GetProtocolAddrLen()) |
| 120 |
{ |
| 121 |
addr.SetRawHostAddress(ProtoAddress::IPv6, (char*)GetBuffer(OffsetTargetProtocolAddr()), 16); |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
PLOG(PL_ERROR, "ProtoPktARP::GetTargetProtocolAddress() error: invalid IP address length\n"); |
| 126 |
return false; |
| 127 |
} |
| 128 |
break; |
| 129 |
default: |
| 130 |
PLOG(PL_ERROR, "ProtoPktARP::GetTargetProtocolAddress() error: unsupported hardware type\n"); |
| 131 |
return false; |
| 132 |
} |
| 133 |
return true; |
| 134 |
} // end ProtoPktARP::GetTargetProtocolAddress() |
| 135 |
|
| 136 |
bool ProtoPktARP::InitIntoBuffer(void* bufferPtr, |
| 137 |
unsigned int numBytes, |
| 138 |
bool freeOnDestruct) |
| 139 |
{ |
| 140 |
unsigned int minLength = OFFSET_SNDR_HRD_ADDR; |
| 141 |
if (NULL != bufferPtr) |
| 142 |
{ |
| 143 |
if (numBytes < minLength) |
| 144 |
return false; |
| 145 |
else |
| 146 |
AttachBuffer(bufferPtr, numBytes, freeOnDestruct); |
| 147 |
} |
| 148 |
else if (GetBufferLength() < minLength) |
| 149 |
{ |
| 150 |
return false; |
| 151 |
} |
| 152 |
SetLength(minLength); |
| 153 |
return true; |
| 154 |
} // end ProtoPktARP::InitIntoBuffer() |
| 155 |
|
| 156 |
bool ProtoPktARP::SetSenderHardwareAddress(const ProtoAddress& addr) |
| 157 |
{ |
| 158 |
// TBD - support more types and verify that target/sender hw addr type are equal! |
| 159 |
if (ProtoAddress::ETH != addr.GetType()) |
| 160 |
{ |
| 161 |
PLOG(PL_ERROR, "ProtoPktARP::SetSenderHardwareAddress() error: unsupported address type\n"); |
| 162 |
return false; |
| 163 |
} |
| 164 |
SetHardwareType(ETHERNET); |
| 165 |
SetHardwareAddrLen(addr.GetLength()); |
| 166 |
memcpy(AccessBuffer(OffsetSenderHardwareAddr()), addr.GetRawHostAddress(), addr.GetLength()); |
| 167 |
SetLength(GetLength() + addr.GetLength()); |
| 168 |
return true; |
| 169 |
} // end ProtoPktARP::SetSenderHardwareAddress() |
| 170 |
|
| 171 |
bool ProtoPktARP::SetSenderProtocolAddress(const ProtoAddress& addr) |
| 172 |
{ |
| 173 |
ProtoPktETH::Type etherType; |
| 174 |
switch (addr.GetType()) |
| 175 |
{ |
| 176 |
case ProtoAddress::IPv4: |
| 177 |
etherType = ProtoPktETH::IP; |
| 178 |
break; |
| 179 |
case ProtoAddress::IPv6: |
| 180 |
etherType = ProtoPktETH::IPv6; |
| 181 |
break; |
| 182 |
default: |
| 183 |
PLOG(PL_ERROR, "ProtoPktARP::SetSenderProtocolAddress() error: unsupported address type\n"); |
| 184 |
return false; |
| 185 |
} |
| 186 |
SetEtherType(etherType); |
| 187 |
SetProtocolAddrLen(addr.GetLength()); |
| 188 |
memcpy(AccessBuffer(OffsetSenderProtocolAddr()), addr.GetRawHostAddress(), addr.GetLength()); |
| 189 |
SetLength(GetLength() + addr.GetLength()); |
| 190 |
return true; |
| 191 |
} // end ProtoPktARP::SetSenderProtocolAddress() |
| 192 |
|
| 193 |
bool ProtoPktARP::SetTargetHardwareAddress(const ProtoAddress& addr) |
| 194 |
{ |
| 195 |
// TBD - support more types and verify that target/sender hw addr type are equal! |
| 196 |
if (ProtoAddress::ETH != addr.GetType()) |
| 197 |
{ |
| 198 |
PLOG(PL_ERROR, "ProtoPktARP::SetTargetHardwareAddress() error: unsupported address type\n"); |
| 199 |
return false; |
| 200 |
} |
| 201 |
SetHardwareType(ETHERNET); |
| 202 |
SetHardwareAddrLen(addr.GetLength()); |
| 203 |
memcpy(AccessBuffer(OffsetTargetHardwareAddr()), addr.GetRawHostAddress(), addr.GetLength()); |
| 204 |
SetLength(GetLength() + addr.GetLength()); |
| 205 |
return true; |
| 206 |
} // end ProtoPktARP::SetTargetHardwareAddress() |
| 207 |
|
| 208 |
bool ProtoPktARP::SetTargetProtocolAddress(const ProtoAddress& addr) |
| 209 |
{ |
| 210 |
ProtoPktETH::Type etherType; |
| 211 |
switch (addr.GetType()) |
| 212 |
{ |
| 213 |
case ProtoAddress::IPv4: |
| 214 |
etherType = ProtoPktETH::IP; |
| 215 |
break; |
| 216 |
case ProtoAddress::IPv6: |
| 217 |
etherType = ProtoPktETH::IPv6; |
| 218 |
break; |
| 219 |
default: |
| 220 |
PLOG(PL_ERROR, "ProtoPktARP::SetTargetProtocolAddress() error: unsupported address type\n"); |
| 221 |
return false; |
| 222 |
} |
| 223 |
SetEtherType(etherType); |
| 224 |
SetProtocolAddrLen(addr.GetLength()); |
| 225 |
memcpy(AccessBuffer(OffsetTargetProtocolAddr()), addr.GetRawHostAddress(), addr.GetLength()); |
| 226 |
SetLength(GetLength() + addr.GetLength()); |
| 227 |
return true; |
| 228 |
} // end ProtoPktARP::SetTargetProtocolAddress() |
| 229 |
|
| 230 |
|
| 231 |
///////////////////////////////////////////////// |
| 232 |
// ProtoArpTable implementation |
| 233 |
|
| 234 |
ProtoArpTable::MacItem::MacItem(const ProtoAddress& macAddr) |
| 235 |
: mac_addr(macAddr) |
| 236 |
{ |
| 237 |
} |
| 238 |
|
| 239 |
ProtoArpTable::MacItem::~MacItem() |
| 240 |
{ |
| 241 |
ip_addr_list.Destroy(); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
ProtoArpTable::ProtoArpTable() |
| 246 |
{ |
| 247 |
} |
| 248 |
|
| 249 |
ProtoArpTable::~ProtoArpTable() |
| 250 |
{ |
| 251 |
mac_list.Destroy(); |
| 252 |
ip_list.Destroy(); |
| 253 |
} |
| 254 |
|
| 255 |
bool ProtoArpTable::AddEntry(const ProtoAddress& ipAddr, const ProtoAddress& macAddr) |
| 256 |
{ |
| 257 |
// One MAC addr entry per IP address (do we already know this IP address) |
| 258 |
IPItem* ipItem = ip_list.FindItem(ipAddr); |
| 259 |
if (NULL == ipItem) |
| 260 |
{ |
| 261 |
// It's a new IP address, is it for an existing MAC addr? |
| 262 |
MacItem* macItem = mac_list.FindItem(macAddr); |
| 263 |
if (NULL == macItem) |
| 264 |
{ |
| 265 |
if (NULL == (macItem = new MacItem(macAddr))) |
| 266 |
{ |
| 267 |
PLOG(PL_ERROR, "ProtoArpTable::AddEntry() new MacItem error: %s\n", GetErrorString()); |
| 268 |
return false; |
| 269 |
} |
| 270 |
if (!mac_list.Insert(*macItem)) |
| 271 |
{ |
| 272 |
PLOG(PL_ERROR, "ProtoArpTable::AddEntry() error: unable to insert MacItem\n"); |
| 273 |
delete macItem; |
| 274 |
return false; |
| 275 |
} |
| 276 |
} |
| 277 |
// Create new IPItem |
| 278 |
if (NULL == (ipItem = new IPItem(ipAddr, macItem))) |
| 279 |
{ |
| 280 |
PLOG(PL_ERROR, "ProtoArpTable::AddEntry() new IPItem error: %s\n", GetErrorString()); |
| 281 |
} |
| 282 |
else if (!ip_list.Insert(*ipItem)) // attempt insertion |
| 283 |
{ |
| 284 |
PLOG(PL_ERROR, "ProtoArpTable::AddEntry() unab IPItem error: %s\n", GetErrorString()); |
| 285 |
delete ipItem; |
| 286 |
ipItem = NULL; |
| 287 |
} |
| 288 |
else if (!macItem->AddAddress(ipAddr)) // add IP address to macItem |
| 289 |
{ |
| 290 |
PLOG(PL_ERROR, "ProtoArpTable::AddEntry() error: unable to add IP address\n"); |
| 291 |
ip_list.Remove(*ipItem); |
| 292 |
delete ipItem; |
| 293 |
ipItem = NULL; |
| 294 |
} |
| 295 |
if (NULL == ipItem) |
| 296 |
{ |
| 297 |
// something went wrong |
| 298 |
macItem->RemoveAddress(ipAddr); |
| 299 |
if (macItem->AccessAddressList().IsEmpty()) |
| 300 |
{ |
| 301 |
mac_list.Remove(*macItem); |
| 302 |
delete macItem; |
| 303 |
} |
| 304 |
return false; |
| 305 |
} |
| 306 |
} |
| 307 |
else |
| 308 |
{ |
| 309 |
// It's an existing IPItem, update stuff if different MAC addr |
| 310 |
if (!macAddr.HostIsEqual(ipItem->GetMacAddr())) |
| 311 |
{ |
| 312 |
// Remove the record for this IP address and re-add with new MAC |
| 313 |
DeleteIPItem(ipItem); |
| 314 |
return AddEntry(ipAddr, macAddr); |
| 315 |
} |
| 316 |
} |
| 317 |
return true; |
| 318 |
} // end ProtoArpTable::AddEntry() |
| 319 |
|
| 320 |
void ProtoArpTable::DeleteIPItem(IPItem* ipItem) |
| 321 |
{ |
| 322 |
if (NULL != ipItem) |
| 323 |
{ |
| 324 |
ip_list.Remove(*ipItem); |
| 325 |
MacItem* macItem = ipItem->GetMacItem(); |
| 326 |
macItem->RemoveAddress(ipItem->GetAddress()); |
| 327 |
if (macItem->AccessAddressList().IsEmpty()) |
| 328 |
delete macItem; |
| 329 |
delete ipItem; |
| 330 |
} |
| 331 |
} // end ProtoArpTable:RemoveIPItem() |
| 332 |
|
| 333 |
void ProtoArpTable::DeleteMacItem(MacItem* macItem) |
| 334 |
{ |
| 335 |
if (NULL != macItem) |
| 336 |
{ |
| 337 |
ProtoAddressList::Iterator iterator(macItem->AccessAddressList()); |
| 338 |
ProtoAddress ipAddr; |
| 339 |
while (iterator.GetNextAddress(ipAddr)) |
| 340 |
{ |
| 341 |
IPItem* ipItem = ip_list.FindItem(ipAddr); |
| 342 |
ASSERT(NULL != ipItem); |
| 343 |
ip_list.Remove(*ipItem); |
| 344 |
delete ipItem; |
| 345 |
} |
| 346 |
delete macItem; |
| 347 |
} |
| 348 |
} // end ProtoArpTable::DeleteMacItem() |
| 349 |
|
| 350 |
void ProtoArpTable::RemoveEntryByIP(const ProtoAddress& ipAddr) |
| 351 |
{ |
| 352 |
IPItem* ipItem = ip_list.FindItem(ipAddr); |
| 353 |
if (NULL != ipItem) DeleteIPItem(ipItem); |
| 354 |
} // end ProtoArpTable::RemoveEntryByIP() |
| 355 |
|
| 356 |
|
| 357 |
void ProtoArpTable::RemoveEntryByMAC(const ProtoAddress& macAddr) |
| 358 |
{ |
| 359 |
MacItem* macItem = mac_list.FindItem(macAddr); |
| 360 |
if (NULL != macItem) DeleteMacItem(macItem); |
| 361 |
} // end ProtoArpTable::RemoveEntryByMAC() |
| 362 |
|
| 363 |
bool ProtoArpTable::GetMacAddress(const ProtoAddress& ipAddr, ProtoAddress& macAddr) |
| 364 |
{ |
| 365 |
IPItem* ipItem = ip_list.FindItem(ipAddr); |
| 366 |
if (NULL == ipItem) |
| 367 |
{ |
| 368 |
macAddr.Invalidate(); |
| 369 |
return false; |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
macAddr = ipItem->GetMacAddr(); |
| 374 |
return true; |
| 375 |
} |
| 376 |
} // end ProtoArpTable::GetMacAddress() |
| 377 |
|
| 378 |
bool ProtoArpTable::GetAddressList(const ProtoAddress& macAddr, ProtoAddressList addrList) |
| 379 |
{ |
| 380 |
MacItem* macItem = mac_list.FindItem(macAddr); |
| 381 |
if (NULL == macItem) |
| 382 |
{ |
| 383 |
PLOG(PL_WARN, "ProtoArpTable::GetAddressList() warning: unknown MAC address\n"); |
| 384 |
return false; |
| 385 |
} |
| 386 |
ProtoAddressList::Iterator iterator(macItem->AccessAddressList()); |
| 387 |
ProtoAddress ipAddr; |
| 388 |
while (iterator.GetNextAddress(ipAddr)) |
| 389 |
{ |
| 390 |
if (!addrList.Insert(ipAddr)) |
| 391 |
{ |
| 392 |
PLOG(PL_ERROR, "ProtoArpTable::GetAddressList() error: %s\n", GetErrorString()); |
| 393 |
return false; |
| 394 |
} |
| 395 |
} |
| 396 |
return true; |
| 397 |
} // end ProtoArpTable::GetIPAddresses() |
| 398 |
|
| 399 |
|