| 1 |
#ifndef _PROTO_PKT_ARP |
| 2 |
#define _PROTO_PKT_ARP |
| 3 |
|
| 4 |
#include "protoPktETH.h" |
| 5 |
|
| 6 |
/** |
| 7 |
* @class ProtoPktARP |
| 8 |
* |
| 9 |
* @brief Parses ARP packets. |
| 10 |
*/ |
| 11 |
class ProtoPktARP : public ProtoPkt |
| 12 |
{ |
| 13 |
public: |
| 14 |
ProtoPktARP(void* bufferPtr = NULL, |
| 15 |
unsigned int numBytes = 0, |
| 16 |
bool initFromBuffer = false, |
| 17 |
bool freeOnDestruct = false); |
| 18 |
~ProtoPktARP(); |
| 19 |
|
| 20 |
|
| 21 |
enum HardwareType |
| 22 |
{ |
| 23 |
ETHERNET = 1, |
| 24 |
IEEE802 = 6, |
| 25 |
ARCNET = 7, |
| 26 |
FRAME_RELAY = 15, |
| 27 |
ATM = 16, |
| 28 |
HDLC = 17, |
| 29 |
FIBRE_CHAN = 18, |
| 30 |
ATM2 = 19, |
| 31 |
SERIAL = 20 |
| 32 |
}; |
| 33 |
|
| 34 |
enum Opcode |
| 35 |
{ |
| 36 |
ARP_REQ = 1, |
| 37 |
ARP_REP = 2, |
| 38 |
RARP_REQ = 3, |
| 39 |
RARP_REP = 4, |
| 40 |
DRARP_REQ = 5, |
| 41 |
DRARP_REP = 6, |
| 42 |
DRARP_ERR = 7, |
| 43 |
IN_ARP_REQ = 8, |
| 44 |
IN_ARP_REP = 9 |
| 45 |
}; |
| 46 |
|
| 47 |
// Use these to parse the ARP message |
| 48 |
bool InitFromBuffer(void* bufferPtr = NULL, |
| 49 |
unsigned int numBytes = 0, |
| 50 |
bool freeOnDestruct = false); |
| 51 |
|
| 52 |
HardwareType GetHardwareType() const |
| 53 |
{return (HardwareType)GetWord16(OFFSET_HRD);} |
| 54 |
|
| 55 |
ProtoPktETH::Type GetEtherType() const |
| 56 |
{return (ProtoPktETH::Type)GetWord16(OFFSET_PRO);} |
| 57 |
|
| 58 |
UINT8 GetHardwareAddrLen() const |
| 59 |
{return GetUINT8(OFFSET_HLN);} |
| 60 |
|
| 61 |
UINT8 GetProtocolAddrLen() const |
| 62 |
{return GetUINT8(OFFSET_PLN);} |
| 63 |
|
| 64 |
Opcode GetOpcode() const |
| 65 |
{return (Opcode)GetWord16(OFFSET_OP);} |
| 66 |
|
| 67 |
bool GetSenderHardwareAddress(ProtoAddress& addr) const; |
| 68 |
|
| 69 |
bool GetSenderProtocolAddress(ProtoAddress& addr) const; |
| 70 |
|
| 71 |
bool GetTargetHardwareAddress(ProtoAddress& addr) const; |
| 72 |
|
| 73 |
bool GetTargetProtocolAddress(ProtoAddress& addr) const; |
| 74 |
|
| 75 |
|
| 76 |
// Use these to build the ARP message |
| 77 |
// (MUST be called in order of appearance here) |
| 78 |
bool InitIntoBuffer(void* bufferPtr = 0, |
| 79 |
unsigned int numBytes = 0, |
| 80 |
bool freeOnDestruct = false); |
| 81 |
void SetOpcode(Opcode opcode) |
| 82 |
{SetWord16(OFFSET_OP, (UINT16)opcode);} |
| 83 |
bool SetSenderHardwareAddress(const ProtoAddress& addr); |
| 84 |
bool SetSenderProtocolAddress(const ProtoAddress& addr); |
| 85 |
bool SetTargetHardwareAddress(const ProtoAddress& addr); |
| 86 |
bool SetTargetProtocolAddress(const ProtoAddress& addr); |
| 87 |
|
| 88 |
|
| 89 |
private: |
| 90 |
void SetHardwareType(HardwareType hwType) |
| 91 |
{SetWord16(OFFSET_HRD, (UINT16)hwType);} |
| 92 |
void SetEtherType(ProtoPktETH::Type etherType) // protocol address type |
| 93 |
{SetWord16(OFFSET_PRO, (UINT16)etherType);} |
| 94 |
void SetHardwareAddrLen(UINT8 numBytes) |
| 95 |
{SetUINT8(OFFSET_HLN, numBytes);} |
| 96 |
void SetProtocolAddrLen(UINT8 numBytes) |
| 97 |
{SetUINT8(OFFSET_PLN, numBytes);} |
| 98 |
|
| 99 |
enum |
| 100 |
{ |
| 101 |
OFFSET_HRD = 0, // UINT16 offset |
| 102 |
OFFSET_PRO = OFFSET_HRD + 1, // UINT16 offset |
| 103 |
OFFSET_HLN = (OFFSET_PRO + 1)*2, // UINT8 offset |
| 104 |
OFFSET_PLN = OFFSET_HLN + 1, // UIN8 offset |
| 105 |
OFFSET_OP = (OFFSET_PLN + 1)/2, // UINT16 offset |
| 106 |
OFFSET_SNDR_HRD_ADDR = (OFFSET_OP+1)*2 // UINT8 offset |
| 107 |
}; |
| 108 |
|
| 109 |
unsigned int OffsetSenderHardwareAddr() const |
| 110 |
{return OFFSET_SNDR_HRD_ADDR;} |
| 111 |
|
| 112 |
unsigned int OffsetSenderProtocolAddr() const |
| 113 |
{return (OFFSET_SNDR_HRD_ADDR + GetHardwareAddrLen());} |
| 114 |
|
| 115 |
unsigned int OffsetTargetHardwareAddr() const |
| 116 |
{return (OffsetSenderProtocolAddr() + GetProtocolAddrLen());} |
| 117 |
|
| 118 |
unsigned int OffsetTargetProtocolAddr() const |
| 119 |
{return (OffsetTargetHardwareAddr() + GetHardwareAddrLen());} |
| 120 |
|
| 121 |
}; // end class ProtoPktARP |
| 122 |
|
| 123 |
|
| 124 |
// Data structure for both MAC->IP and IP->MAC lookups (dually indexed) |
| 125 |
// (Note multiple IPs per MAC is allowed, but one MAC per IP) |
| 126 |
class ProtoArpTable |
| 127 |
{ |
| 128 |
public: |
| 129 |
ProtoArpTable(); |
| 130 |
~ProtoArpTable(); |
| 131 |
|
| 132 |
bool AddEntry(const ProtoAddress& ipAddr, const ProtoAddress& macAddr); |
| 133 |
void RemoveEntryByIP(const ProtoAddress& ipAddr); |
| 134 |
void RemoveEntryByMAC(const ProtoAddress& macAddr); |
| 135 |
|
| 136 |
bool GetMacAddress(const ProtoAddress& ipAddr, ProtoAddress& macAddr); |
| 137 |
bool GetAddressList(const ProtoAddress& macAddr, ProtoAddressList addrList); |
| 138 |
|
| 139 |
void Destroy() |
| 140 |
{ |
| 141 |
mac_list.Destroy(); |
| 142 |
ip_list.Destroy(); |
| 143 |
} |
| 144 |
|
| 145 |
private: |
| 146 |
// Record of IP address listings for a given MAC address |
| 147 |
class MacItem : public ProtoTree::Item |
| 148 |
{ |
| 149 |
public: |
| 150 |
MacItem(const ProtoAddress& macAddr); |
| 151 |
~MacItem(); |
| 152 |
|
| 153 |
const ProtoAddress& GetMacAddr() |
| 154 |
{return mac_addr;} |
| 155 |
ProtoAddressList& AccessAddressList() |
| 156 |
{return ip_addr_list;} |
| 157 |
|
| 158 |
bool AddAddress(const ProtoAddress& ipAddr) |
| 159 |
{return ip_addr_list.Insert(ipAddr);} |
| 160 |
void RemoveAddress(const ProtoAddress& ipAddr) |
| 161 |
{ip_addr_list.Remove(ipAddr);} |
| 162 |
|
| 163 |
private: |
| 164 |
const char* GetKey() const |
| 165 |
{return mac_addr.GetRawHostAddress();} |
| 166 |
unsigned int GetKeysize() const |
| 167 |
{return (8 * mac_addr.GetLength());} |
| 168 |
|
| 169 |
ProtoAddress mac_addr; |
| 170 |
ProtoAddressList ip_addr_list; |
| 171 |
}; // end class ProtoArpTable::MacItem |
| 172 |
class MacList : public ProtoTreeTemplate<MacItem> |
| 173 |
{ |
| 174 |
public: |
| 175 |
MacItem* FindItem(const ProtoAddress& macAddr) |
| 176 |
{return Find(macAddr.GetRawHostAddress(), 8 * macAddr.GetLength());} |
| 177 |
}; // end class ProtoArpTable::IPList |
| 178 |
|
| 179 |
// MAC address indexed by IP address |
| 180 |
class IPItem : public ProtoTree::Item |
| 181 |
{ |
| 182 |
public: |
| 183 |
IPItem(const ProtoAddress& ipAddr, MacItem* macItem) |
| 184 |
: ip_addr(ipAddr), mac_item(macItem) {} |
| 185 |
~IPItem() {} |
| 186 |
|
| 187 |
const ProtoAddress& GetAddress()const |
| 188 |
{return ip_addr;} |
| 189 |
const ProtoAddress& GetMacAddr() const |
| 190 |
{return mac_item->GetMacAddr();} |
| 191 |
|
| 192 |
MacItem* GetMacItem() |
| 193 |
{return mac_item;} |
| 194 |
|
| 195 |
private: |
| 196 |
const char* GetKey() const |
| 197 |
{return ip_addr.GetRawHostAddress();} |
| 198 |
unsigned int GetKeysize() const |
| 199 |
{return (8 * ip_addr.GetLength());} |
| 200 |
|
| 201 |
ProtoAddress ip_addr; |
| 202 |
MacItem* mac_item; |
| 203 |
}; // end class ProtoArpTable::IPItem |
| 204 |
|
| 205 |
|
| 206 |
// List of MAC address item indexed by IP address |
| 207 |
class IPList : public ProtoTreeTemplate<IPItem> |
| 208 |
{ |
| 209 |
public: |
| 210 |
IPItem* FindItem(const ProtoAddress& ipAddr) |
| 211 |
{return Find(ipAddr.GetRawHostAddress(), 8 * ipAddr.GetLength());} |
| 212 |
}; // end class ProtoArpTable::IPList |
| 213 |
|
| 214 |
void DeleteIPItem(IPItem* ipItem); |
| 215 |
void DeleteMacItem(MacItem* macItem); |
| 216 |
|
| 217 |
MacList mac_list; |
| 218 |
IPList ip_list; |
| 219 |
|
| 220 |
}; // end class ProtoArpTable |
| 221 |
|
| 222 |
#endif // _PROTO_PKT_ARP |