- latest changes for HEP support
This commit is contained in:
parent
8c0f9510e6
commit
2f69f58088
|
|
@ -9,141 +9,143 @@ static const uint32_t HEPID3 = 0x48455033;
|
|||
|
||||
bool Packet::parseV3(const ByteBuffer& packet)
|
||||
{
|
||||
if (packet.size() < 30)
|
||||
return false;
|
||||
if (packet.size() < 30)
|
||||
return false;
|
||||
|
||||
BufferReader r(packet);
|
||||
char signature[4];
|
||||
r.readBuffer(signature, 4);
|
||||
BufferReader r(packet);
|
||||
char signature[4];
|
||||
r.readBuffer(signature, 4);
|
||||
|
||||
if (signature[0] != 'H' || signature[1] != 'E' || signature[2] != 'P' || signature[3] != '3')
|
||||
return false;
|
||||
if (signature[0] != 'H' || signature[1] != 'E' || signature[2] != 'P' || signature[3] != '3')
|
||||
return false;
|
||||
|
||||
// Total length
|
||||
int l = r.readUShort();
|
||||
l -= 6;
|
||||
// Total length
|
||||
int l = r.readUShort();
|
||||
l -= 6;
|
||||
|
||||
InternetAddress sourceAddr4, destAddr4, sourceAddr6, destAddr6;
|
||||
uint16_t sourcePort = 0, destPort = 0;
|
||||
while (r.count() < packet.size())
|
||||
{
|
||||
mVendorId = (VendorId)r.readUShort();
|
||||
ChunkType chunkType = (ChunkType)r.readUShort();
|
||||
int chunkLength = r.readUShort();
|
||||
|
||||
switch (chunkType)
|
||||
InternetAddress sourceAddr4, destAddr4, sourceAddr6, destAddr6;
|
||||
uint16_t sourcePort = 0, destPort = 0;
|
||||
while (r.count() < packet.size())
|
||||
{
|
||||
case ChunkType::IPProtocolFamily:
|
||||
mIpProtocolFamily = r.readUChar();
|
||||
break;
|
||||
mVendorId = (VendorId)r.readUShort();
|
||||
ChunkType chunkType = (ChunkType)r.readUShort();
|
||||
int chunkLength = r.readUShort();
|
||||
|
||||
case ChunkType::IPProtocolID:
|
||||
mIpProtocolId = r.readUChar();
|
||||
break;
|
||||
switch (chunkType)
|
||||
{
|
||||
case ChunkType::IPProtocolFamily:
|
||||
mIpProtocolFamily = r.readUChar();
|
||||
break;
|
||||
|
||||
case ChunkType::IP4SourceAddress:
|
||||
sourceAddr4 = r.readIp(AF_INET);
|
||||
break;
|
||||
case ChunkType::IPProtocolID:
|
||||
mIpProtocolId = r.readUChar();
|
||||
break;
|
||||
|
||||
case ChunkType::IP4DestinationAddress:
|
||||
destAddr4 = r.readIp(AF_INET);
|
||||
break;
|
||||
case ChunkType::IP4SourceAddress:
|
||||
sourceAddr4 = r.readIp(AF_INET);
|
||||
break;
|
||||
|
||||
case ChunkType::IP6SourceAddress:
|
||||
sourceAddr6 = r.readIp(AF_INET);
|
||||
break;
|
||||
case ChunkType::IP4DestinationAddress:
|
||||
destAddr4 = r.readIp(AF_INET);
|
||||
break;
|
||||
|
||||
case ChunkType::IP6DestinationAddress:
|
||||
destAddr6 = r.readIp(AF_INET6);
|
||||
break;
|
||||
case ChunkType::IP6SourceAddress:
|
||||
sourceAddr6 = r.readIp(AF_INET);
|
||||
break;
|
||||
|
||||
case ChunkType::SourcePort:
|
||||
sourcePort = r.readUShort();
|
||||
break;
|
||||
case ChunkType::IP6DestinationAddress:
|
||||
destAddr6 = r.readIp(AF_INET6);
|
||||
break;
|
||||
|
||||
case ChunkType::DestinationPort:
|
||||
destPort = r.readUShort();
|
||||
break;
|
||||
case ChunkType::SourcePort:
|
||||
sourcePort = r.readUShort();
|
||||
break;
|
||||
|
||||
case ChunkType::Timestamp:
|
||||
mTimestamp.tv_sec = r.readUInt();
|
||||
break;
|
||||
case ChunkType::DestinationPort:
|
||||
destPort = r.readUShort();
|
||||
break;
|
||||
|
||||
case ChunkType::TimestampMicro:
|
||||
mTimestamp.tv_usec = r.readUInt() * 1000;
|
||||
break;
|
||||
case ChunkType::Timestamp:
|
||||
mTimestamp.tv_sec = r.readUInt();
|
||||
break;
|
||||
|
||||
case ChunkType::ProtocolType:
|
||||
mProtocolType = (ProtocolId)r.readUChar();
|
||||
break;
|
||||
case ChunkType::TimestampMicro:
|
||||
mTimestamp.tv_usec = r.readUInt() * 1000;
|
||||
break;
|
||||
|
||||
case ChunkType::CaptureAgentID:
|
||||
mCaptureAgentId = r.readUInt();
|
||||
break;
|
||||
case ChunkType::ProtocolType:
|
||||
mProtocolType = (ProtocolId)r.readUChar();
|
||||
break;
|
||||
|
||||
case ChunkType::KeepAliveTimer:
|
||||
mKeepAliveTimer = r.readUShort();
|
||||
break;
|
||||
case ChunkType::CaptureAgentID:
|
||||
mCaptureAgentId = r.readUInt();
|
||||
break;
|
||||
|
||||
case ChunkType::AuthenticationKey:
|
||||
mAuthenticateKey.resize(chunkLength - 6);
|
||||
r.readBuffer(mAuthenticateKey.mutableData(), mAuthenticateKey.size());
|
||||
break;
|
||||
case ChunkType::KeepAliveTimer:
|
||||
mKeepAliveTimer = r.readUShort();
|
||||
break;
|
||||
|
||||
case ChunkType::PacketPayload:
|
||||
r.readBuffer(mBody, chunkLength - 6);
|
||||
break;
|
||||
case ChunkType::AuthenticationKey:
|
||||
mAuthenticateKey.resize(chunkLength - 6);
|
||||
r.readBuffer(mAuthenticateKey.mutableData(), mAuthenticateKey.size());
|
||||
break;
|
||||
|
||||
default:
|
||||
r.readBuffer(nullptr, chunkLength - 6);
|
||||
case ChunkType::PacketPayload:
|
||||
mBodyOffset = r.count();
|
||||
r.readBuffer(mBody, chunkLength - 6);
|
||||
break;
|
||||
|
||||
default:
|
||||
r.readBuffer(nullptr, chunkLength - 6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!sourceAddr4.isEmpty())
|
||||
mSourceAddress = sourceAddr4;
|
||||
else
|
||||
if (!sourceAddr6.isEmpty())
|
||||
mSourceAddress = sourceAddr6;
|
||||
if (!sourceAddr4.isEmpty())
|
||||
mSourceAddress = sourceAddr4;
|
||||
else
|
||||
if (!sourceAddr6.isEmpty())
|
||||
mSourceAddress = sourceAddr6;
|
||||
|
||||
if (!mSourceAddress.isEmpty())
|
||||
mSourceAddress.setPort(sourcePort);
|
||||
if (!mSourceAddress.isEmpty())
|
||||
mSourceAddress.setPort(sourcePort);
|
||||
|
||||
if (!destAddr4.isEmpty())
|
||||
mDestinationAddress = destAddr4;
|
||||
else
|
||||
if (!destAddr6.isEmpty())
|
||||
mDestinationAddress = destAddr6;
|
||||
if (!destAddr4.isEmpty())
|
||||
mDestinationAddress = destAddr4;
|
||||
else
|
||||
if (!destAddr6.isEmpty())
|
||||
mDestinationAddress = destAddr6;
|
||||
|
||||
if (!mDestinationAddress.isEmpty())
|
||||
mDestinationAddress.setPort(destPort);
|
||||
if (!mDestinationAddress.isEmpty())
|
||||
mDestinationAddress.setPort(destPort);
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Packet::parseV2(const ByteBuffer &packet)
|
||||
{
|
||||
if (packet.size() < 31)
|
||||
return false;
|
||||
if (packet.size() < 31)
|
||||
return false;
|
||||
|
||||
if (packet[0] != 0x02)
|
||||
return false;
|
||||
if (packet[0] != 0x02)
|
||||
return false;
|
||||
|
||||
BufferReader r(packet);
|
||||
r.readBuffer(nullptr, 4);
|
||||
BufferReader r(packet);
|
||||
r.readBuffer(nullptr, 4);
|
||||
|
||||
uint16_t sourcePort = r.readUShort();
|
||||
uint16_t dstPort = r.readUShort();
|
||||
mSourceAddress = r.readIp(AF_INET);
|
||||
mSourceAddress.setPort(sourcePort);
|
||||
mDestinationAddress = r.readIp(AF_INET);
|
||||
mDestinationAddress.setPort(dstPort);
|
||||
mTimestamp.tv_sec = r.readUInt();
|
||||
mTimestamp.tv_usec = r.readUInt() * 1000;
|
||||
mCaptureAgentId = r.readUShort();
|
||||
r.readBuffer(nullptr, 2);
|
||||
mBody.clear();
|
||||
r.readBuffer(mBody, 65536 - 28);
|
||||
return true;
|
||||
uint16_t sourcePort = r.readUShort();
|
||||
uint16_t dstPort = r.readUShort();
|
||||
mSourceAddress = r.readIp(AF_INET);
|
||||
mSourceAddress.setPort(sourcePort);
|
||||
mDestinationAddress = r.readIp(AF_INET);
|
||||
mDestinationAddress.setPort(dstPort);
|
||||
mTimestamp.tv_sec = r.readUInt();
|
||||
mTimestamp.tv_usec = r.readUInt() * 1000;
|
||||
mCaptureAgentId = r.readUShort();
|
||||
r.readBuffer(nullptr, 2);
|
||||
mBody.clear();
|
||||
mBodyOffset = r.count();
|
||||
r.readBuffer(mBody, 65536 - 28);
|
||||
return true;
|
||||
}
|
||||
|
||||
#define WRITE_CHUNK_UCHAR(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(1); w.writeUChar((uint8_t)V);}
|
||||
|
|
@ -155,66 +157,66 @@ bool Packet::parseV2(const ByteBuffer &packet)
|
|||
|
||||
ByteBuffer Packet::buildV3()
|
||||
{
|
||||
ByteBuffer r; r.resize(mBody.size() + 512);
|
||||
BufferWriter w(r);
|
||||
ByteBuffer r; r.resize(mBody.size() + 512);
|
||||
BufferWriter w(r);
|
||||
|
||||
// Signature
|
||||
w.writeBuffer("HEP3", 4);
|
||||
// Signature
|
||||
w.writeBuffer("HEP3", 4);
|
||||
|
||||
// Reserve place for total length
|
||||
w.writeUShort(0);
|
||||
// Reserve place for total length
|
||||
w.writeUShort(0);
|
||||
|
||||
WRITE_CHUNK_UCHAR(ChunkType::IPProtocolFamily, mIpProtocolFamily);
|
||||
WRITE_CHUNK_UCHAR(ChunkType::IPProtocolID, mIpProtocolId);
|
||||
WRITE_CHUNK_UCHAR(ChunkType::IPProtocolFamily, mIpProtocolFamily);
|
||||
WRITE_CHUNK_UCHAR(ChunkType::IPProtocolID, mIpProtocolId);
|
||||
|
||||
// Source address
|
||||
if (!mSourceAddress.isEmpty())
|
||||
{
|
||||
if (mSourceAddress.isV4())
|
||||
WRITE_CHUNK_IP4(ChunkType::IP4SourceAddress, mSourceAddress)
|
||||
else
|
||||
if (mSourceAddress.isV6())
|
||||
WRITE_CHUNK_IP6(ChunkType::IP6SourceAddress, mSourceAddress);
|
||||
// Source address
|
||||
if (!mSourceAddress.isEmpty())
|
||||
{
|
||||
if (mSourceAddress.isV4())
|
||||
WRITE_CHUNK_IP4(ChunkType::IP4SourceAddress, mSourceAddress)
|
||||
else
|
||||
if (mSourceAddress.isV6())
|
||||
WRITE_CHUNK_IP6(ChunkType::IP6SourceAddress, mSourceAddress);
|
||||
|
||||
WRITE_CHUNK_USHORT(ChunkType::SourcePort, mSourceAddress.port());
|
||||
}
|
||||
WRITE_CHUNK_USHORT(ChunkType::SourcePort, mSourceAddress.port());
|
||||
}
|
||||
|
||||
// Destination address
|
||||
if (!mDestinationAddress.isEmpty())
|
||||
{
|
||||
if (mDestinationAddress.isV4())
|
||||
WRITE_CHUNK_IP4(ChunkType::IP4DestinationAddress, mDestinationAddress)
|
||||
else
|
||||
if (mDestinationAddress.isV6())
|
||||
WRITE_CHUNK_IP6(ChunkType::IP6DestinationAddress, mDestinationAddress);
|
||||
// Destination address
|
||||
if (!mDestinationAddress.isEmpty())
|
||||
{
|
||||
if (mDestinationAddress.isV4())
|
||||
WRITE_CHUNK_IP4(ChunkType::IP4DestinationAddress, mDestinationAddress)
|
||||
else
|
||||
if (mDestinationAddress.isV6())
|
||||
WRITE_CHUNK_IP6(ChunkType::IP6DestinationAddress, mDestinationAddress);
|
||||
|
||||
WRITE_CHUNK_USHORT(ChunkType::DestinationPort, mDestinationAddress.port());
|
||||
}
|
||||
WRITE_CHUNK_USHORT(ChunkType::DestinationPort, mDestinationAddress.port());
|
||||
}
|
||||
|
||||
// Timestamp
|
||||
WRITE_CHUNK_UINT(ChunkType::Timestamp, mTimestamp.tv_sec);
|
||||
// Timestamp
|
||||
WRITE_CHUNK_UINT(ChunkType::Timestamp, mTimestamp.tv_sec);
|
||||
|
||||
// TimestampMicro
|
||||
WRITE_CHUNK_UINT(ChunkType::TimestampMicro, mTimestamp.tv_usec / 1000);
|
||||
// TimestampMicro
|
||||
WRITE_CHUNK_UINT(ChunkType::TimestampMicro, mTimestamp.tv_usec / 1000);
|
||||
|
||||
// Protocol type
|
||||
WRITE_CHUNK_UINT(ChunkType::ProtocolType, mProtocolType);
|
||||
// Protocol type
|
||||
WRITE_CHUNK_UINT(ChunkType::ProtocolType, mProtocolType);
|
||||
|
||||
// Capture agent ID
|
||||
WRITE_CHUNK_UINT(ChunkType::CaptureAgentID, mCaptureAgentId);
|
||||
// Capture agent ID
|
||||
WRITE_CHUNK_UINT(ChunkType::CaptureAgentID, mCaptureAgentId);
|
||||
|
||||
// Keep alive timer value
|
||||
WRITE_CHUNK_USHORT(ChunkType::KeepAliveTimer, mKeepAliveTimer);
|
||||
// Keep alive timer value
|
||||
WRITE_CHUNK_USHORT(ChunkType::KeepAliveTimer, mKeepAliveTimer);
|
||||
|
||||
// Authentication key
|
||||
WRITE_CHUNK_BUFFER(ChunkType::AuthenticationKey, mAuthenticateKey);
|
||||
// Authentication key
|
||||
WRITE_CHUNK_BUFFER(ChunkType::AuthenticationKey, mAuthenticateKey);
|
||||
|
||||
// Payload
|
||||
WRITE_CHUNK_BUFFER(ChunkType::PacketPayload, mBody);
|
||||
// Payload
|
||||
WRITE_CHUNK_BUFFER(ChunkType::PacketPayload, mBody);
|
||||
|
||||
r.resize(w.offset());
|
||||
r.resize(w.offset());
|
||||
|
||||
w.rewind(); w.skip(4); w.writeUShort((uint16_t)r.size());
|
||||
w.rewind(); w.skip(4); w.writeUShort((uint16_t)r.size());
|
||||
|
||||
return r;
|
||||
return r;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,6 +77,7 @@ namespace HEP
|
|||
ByteBuffer mAuthenticateKey;
|
||||
ByteBuffer mBody;
|
||||
VendorId mVendorId;
|
||||
uint32_t mBodyOffset = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,23 +13,23 @@
|
|||
|
||||
NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForEthernet(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
|
||||
{
|
||||
PacketData result(packet);
|
||||
PacketData result(packet);
|
||||
|
||||
const EthernetHeader* ethernet = reinterpret_cast<const EthernetHeader*>(packet.mData);
|
||||
const EthernetHeader* ethernet = reinterpret_cast<const EthernetHeader*>(packet.mData);
|
||||
|
||||
// Skip ethernet header
|
||||
packet.mData += sizeof(EthernetHeader);
|
||||
packet.mLength -= sizeof(EthernetHeader);
|
||||
// Skip ethernet header
|
||||
packet.mData += sizeof(EthernetHeader);
|
||||
packet.mLength -= sizeof(EthernetHeader);
|
||||
|
||||
// See if there is Vlan header
|
||||
uint16_t proto = 0;
|
||||
if (ethernet->mEtherType == 129)
|
||||
{
|
||||
const VlanHeader* vlan = reinterpret_cast<const VlanHeader*>(packet.mData);
|
||||
packet.mData += sizeof(VlanHeader);
|
||||
packet.mLength -= sizeof(VlanHeader);
|
||||
proto = ntohs(vlan->mData);
|
||||
}
|
||||
// See if there is Vlan header
|
||||
uint16_t proto = 0;
|
||||
if (ethernet->mEtherType == 129)
|
||||
{
|
||||
const VlanHeader* vlan = reinterpret_cast<const VlanHeader*>(packet.mData);
|
||||
packet.mData += sizeof(VlanHeader);
|
||||
packet.mLength -= sizeof(VlanHeader);
|
||||
proto = ntohs(vlan->mData);
|
||||
}
|
||||
|
||||
// Skip MPLS headers
|
||||
switch (proto)
|
||||
|
|
@ -38,7 +38,7 @@ NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForEthernet(NetworkFrame::Pa
|
|||
case ETHERTYPE_MPLS_MC:
|
||||
// Parse MPLS here until marker "bottom of mpls stack"
|
||||
for(bool bottomOfStack = false; !bottomOfStack;
|
||||
bottomOfStack = ((ntohl(*(uint32_t*)(packet.mData - 4)) & MPLS_STACK_MASK) >> MPLS_STACK_SHIFT) != 0)
|
||||
bottomOfStack = ((ntohl(*(uint32_t*)(packet.mData - 4)) & MPLS_STACK_MASK) >> MPLS_STACK_SHIFT) != 0)
|
||||
{
|
||||
packet.mData += 4;
|
||||
packet.mLength -=4;
|
||||
|
|
@ -54,93 +54,93 @@ NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForEthernet(NetworkFrame::Pa
|
|||
break;
|
||||
}
|
||||
|
||||
const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(packet.mData);
|
||||
const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(packet.mData);
|
||||
|
||||
if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
|
||||
return PacketData();
|
||||
if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
|
||||
return PacketData();
|
||||
|
||||
|
||||
switch (ip4->version())
|
||||
{
|
||||
case 4:
|
||||
return GetUdpPayloadForIp4(packet, source, destination);
|
||||
switch (ip4->version())
|
||||
{
|
||||
case 4:
|
||||
return GetUdpPayloadForIp4(packet, source, destination);
|
||||
|
||||
case 6:
|
||||
return GetUdpPayloadForIp6(packet, source, destination);
|
||||
case 6:
|
||||
return GetUdpPayloadForIp6(packet, source, destination);
|
||||
|
||||
default:
|
||||
return PacketData();
|
||||
}
|
||||
default:
|
||||
return PacketData();
|
||||
}
|
||||
}
|
||||
|
||||
NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForSLL(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
|
||||
{
|
||||
PacketData result(packet);
|
||||
PacketData result(packet);
|
||||
|
||||
if (packet.mLength < 16)
|
||||
return PacketData();
|
||||
if (packet.mLength < 16)
|
||||
return PacketData();
|
||||
|
||||
const LinuxSllHeader* sll = reinterpret_cast<const LinuxSllHeader*>(packet.mData);
|
||||
const LinuxSllHeader* sll = reinterpret_cast<const LinuxSllHeader*>(packet.mData);
|
||||
|
||||
packet.mData += sizeof(LinuxSllHeader);
|
||||
packet.mLength -= sizeof(LinuxSllHeader);
|
||||
packet.mData += sizeof(LinuxSllHeader);
|
||||
packet.mLength -= sizeof(LinuxSllHeader);
|
||||
|
||||
switch (ntohs(sll->mProtocolType))
|
||||
{
|
||||
case 0x0800:
|
||||
return GetUdpPayloadForIp4(packet, source, destination);
|
||||
switch (ntohs(sll->mProtocolType))
|
||||
{
|
||||
case 0x0800:
|
||||
return GetUdpPayloadForIp4(packet, source, destination);
|
||||
|
||||
case 0x86DD:
|
||||
return GetUdpPayloadForIp6(packet, source, destination);
|
||||
case 0x86DD:
|
||||
return GetUdpPayloadForIp6(packet, source, destination);
|
||||
|
||||
default:
|
||||
return PacketData();
|
||||
}
|
||||
default:
|
||||
return PacketData();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForIp4(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
|
||||
{
|
||||
PacketData result(packet);
|
||||
const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(packet.mData);
|
||||
if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
|
||||
return PacketData(nullptr, 0);
|
||||
PacketData result(packet);
|
||||
const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(packet.mData);
|
||||
if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
|
||||
return PacketData(nullptr, 0);
|
||||
|
||||
result.mData += ip4->headerLength();
|
||||
result.mLength -= ip4->headerLength();
|
||||
result.mData += ip4->headerLength();
|
||||
result.mLength -= ip4->headerLength();
|
||||
|
||||
const UdpHeader* udp = reinterpret_cast<const UdpHeader*>(result.mData);
|
||||
result.mData += sizeof(UdpHeader);
|
||||
result.mLength -= sizeof(UdpHeader);
|
||||
const UdpHeader* udp = reinterpret_cast<const UdpHeader*>(result.mData);
|
||||
result.mData += sizeof(UdpHeader);
|
||||
result.mLength -= sizeof(UdpHeader);
|
||||
|
||||
// Check if UDP payload length is smaller than full packet length. It can be VLAN trailer data - we need to skip it
|
||||
size_t length = ntohs(udp->mDatagramLength);
|
||||
if (length - sizeof(UdpHeader) < (size_t)result.mLength)
|
||||
result.mLength = length - sizeof(UdpHeader);
|
||||
// Check if UDP payload length is smaller than full packet length. It can be VLAN trailer data - we need to skip it
|
||||
size_t length = ntohs(udp->mDatagramLength);
|
||||
if (length - sizeof(UdpHeader) < (size_t)result.mLength)
|
||||
result.mLength = length - sizeof(UdpHeader);
|
||||
|
||||
source.setIp(ip4->mSource);
|
||||
source.setPort(ntohs(udp->mSourcePort));
|
||||
source.setIp(ip4->mSource);
|
||||
source.setPort(ntohs(udp->mSourcePort));
|
||||
|
||||
destination.setIp(ip4->mDestination);
|
||||
destination.setPort(ntohs(udp->mDestinationPort));
|
||||
destination.setIp(ip4->mDestination);
|
||||
destination.setPort(ntohs(udp->mDestinationPort));
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
struct Ip6Header
|
||||
{
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
uint8_t traffic_class_hi:4,
|
||||
version:4;
|
||||
version:4;
|
||||
uint8_t flow_label_hi:4,
|
||||
traffic_class_lo:4;
|
||||
traffic_class_lo:4;
|
||||
uint16_t flow_label_lo;
|
||||
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
uint8_t version:4,
|
||||
traffic_class_hi:4;
|
||||
traffic_class_hi:4;
|
||||
uint8_t traffic_class_lo:4,
|
||||
flow_label_hi:4;
|
||||
flow_label_hi:4;
|
||||
uint16_t flow_label_lo;
|
||||
#else
|
||||
# error "Please fix endianness defines"
|
||||
|
|
@ -156,31 +156,31 @@ struct Ip6Header
|
|||
|
||||
NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForIp6(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
|
||||
{
|
||||
PacketData result(packet);
|
||||
const Ip6Header* ip6 = reinterpret_cast<const Ip6Header*>(packet.mData);
|
||||
/*if (ip6->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
|
||||
PacketData result(packet);
|
||||
const Ip6Header* ip6 = reinterpret_cast<const Ip6Header*>(packet.mData);
|
||||
/*if (ip6->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
|
||||
return PacketData(nullptr, 0);
|
||||
*/
|
||||
result.mData += sizeof(Ip6Header);
|
||||
result.mLength -= sizeof(Ip6Header);
|
||||
//std::cout << sizeof(Ip6Header) << std::endl;
|
||||
result.mData += sizeof(Ip6Header);
|
||||
result.mLength -= sizeof(Ip6Header);
|
||||
//std::cout << sizeof(Ip6Header) << std::endl;
|
||||
|
||||
const UdpHeader* udp = reinterpret_cast<const UdpHeader*>(result.mData);
|
||||
result.mData += sizeof(UdpHeader);
|
||||
result.mLength -= sizeof(UdpHeader);
|
||||
const UdpHeader* udp = reinterpret_cast<const UdpHeader*>(result.mData);
|
||||
result.mData += sizeof(UdpHeader);
|
||||
result.mLength -= sizeof(UdpHeader);
|
||||
|
||||
/*
|
||||
/*
|
||||
if (result.mLength != ntohs(udp->mDatagramLength))
|
||||
return PacketData(nullptr, 0);
|
||||
*/
|
||||
|
||||
source.setIp(ip6->src_ip);
|
||||
source.setPort(ntohs(udp->mSourcePort));
|
||||
//std::cout << source.toStdString() << " - ";
|
||||
source.setIp(ip6->src_ip);
|
||||
source.setPort(ntohs(udp->mSourcePort));
|
||||
//std::cout << source.toStdString() << " - ";
|
||||
|
||||
destination.setIp(ip6->dst_ip);
|
||||
destination.setPort(ntohs(udp->mDestinationPort));
|
||||
//std::cout << destination.toStdString() << std::endl;
|
||||
destination.setIp(ip6->dst_ip);
|
||||
destination.setPort(ntohs(udp->mDestinationPort));
|
||||
//std::cout << destination.toStdString() << std::endl;
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,104 +7,104 @@
|
|||
class NetworkFrame
|
||||
{
|
||||
public:
|
||||
struct PacketData
|
||||
{
|
||||
const uint8_t* mData;
|
||||
int mLength;
|
||||
struct PacketData
|
||||
{
|
||||
const uint8_t* mData;
|
||||
int mLength;
|
||||
|
||||
PacketData(const uint8_t* data, int length)
|
||||
:mData(data), mLength(length)
|
||||
{}
|
||||
PacketData(const uint8_t* data, int length)
|
||||
:mData(data), mLength(length)
|
||||
{}
|
||||
|
||||
PacketData()
|
||||
:mData(nullptr), mLength(0)
|
||||
{}
|
||||
};
|
||||
PacketData()
|
||||
:mData(nullptr), mLength(0)
|
||||
{}
|
||||
};
|
||||
|
||||
static PacketData GetUdpPayloadForEthernet(PacketData& packet, InternetAddress& source, InternetAddress& destination);
|
||||
static PacketData GetUdpPayloadForIp4(PacketData& packet, InternetAddress& source, InternetAddress& destination);
|
||||
static PacketData GetUdpPayloadForIp6(PacketData& packet, InternetAddress& source, InternetAddress& destination);
|
||||
static PacketData GetUdpPayloadForSLL(PacketData& packet, InternetAddress& source, InternetAddress& destination);
|
||||
static PacketData GetUdpPayloadForEthernet(PacketData& packet, InternetAddress& source, InternetAddress& destination);
|
||||
static PacketData GetUdpPayloadForIp4(PacketData& packet, InternetAddress& source, InternetAddress& destination);
|
||||
static PacketData GetUdpPayloadForIp6(PacketData& packet, InternetAddress& source, InternetAddress& destination);
|
||||
static PacketData GetUdpPayloadForSLL(PacketData& packet, InternetAddress& source, InternetAddress& destination);
|
||||
|
||||
struct EthernetHeader
|
||||
{
|
||||
/* Ethernet addresses are 6 bytes */
|
||||
static const int AddressLength = 6;
|
||||
uint8_t mEtherDHost[AddressLength]; /* Destination host address */
|
||||
uint8_t mEtherSHost[AddressLength]; /* Source host address */
|
||||
uint16_t mEtherType; /* IP? ARP? RARP? etc */
|
||||
};
|
||||
struct EthernetHeader
|
||||
{
|
||||
/* Ethernet addresses are 6 bytes */
|
||||
static const int AddressLength = 6;
|
||||
uint8_t mEtherDHost[AddressLength]; /* Destination host address */
|
||||
uint8_t mEtherSHost[AddressLength]; /* Source host address */
|
||||
uint16_t mEtherType; /* IP? ARP? RARP? etc */
|
||||
};
|
||||
|
||||
#if defined(TARGET_WIN)
|
||||
struct /*__attribute__((packed))*/ LinuxSllHeader
|
||||
#else
|
||||
struct __attribute__((packed)) LinuxSllHeader
|
||||
struct /*__attribute__((packed))*/ LinuxSllHeader
|
||||
#else
|
||||
struct __attribute__((packed)) LinuxSllHeader
|
||||
#endif
|
||||
{
|
||||
uint16_t mPacketType;
|
||||
uint16_t mARPHRD;
|
||||
uint16_t mAddressLength;
|
||||
uint64_t mAddress;
|
||||
uint16_t mProtocolType;
|
||||
};
|
||||
{
|
||||
uint16_t mPacketType;
|
||||
uint16_t mARPHRD;
|
||||
uint16_t mAddressLength;
|
||||
uint64_t mAddress;
|
||||
uint16_t mProtocolType;
|
||||
};
|
||||
|
||||
struct VlanHeader
|
||||
{
|
||||
uint16_t mMagicId;
|
||||
uint16_t mData;
|
||||
};
|
||||
struct VlanHeader
|
||||
{
|
||||
uint16_t mMagicId;
|
||||
uint16_t mData;
|
||||
};
|
||||
|
||||
struct Ip4Header
|
||||
{
|
||||
uint8_t mVhl; /* version << 4 | header length >> 2 */
|
||||
uint8_t mTos; /* type of service */
|
||||
uint16_t mLen; /* total length */
|
||||
uint16_t mId; /* identification */
|
||||
uint16_t mOffset; /* fragment offset field */
|
||||
struct Ip4Header
|
||||
{
|
||||
uint8_t mVhl; /* version << 4 | header length >> 2 */
|
||||
uint8_t mTos; /* type of service */
|
||||
uint16_t mLen; /* total length */
|
||||
uint16_t mId; /* identification */
|
||||
uint16_t mOffset; /* fragment offset field */
|
||||
#define IP_RF 0x8000 /* reserved fragment flag */
|
||||
#define IP_DF 0x4000 /* dont fragment flag */
|
||||
#define IP_MF 0x2000 /* more fragments flag */
|
||||
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
|
||||
uint8_t mTtl; /* time to live */
|
||||
uint8_t mProtocol; /* protocol */
|
||||
uint16_t mChecksum; /* checksum */
|
||||
in_addr mSource,
|
||||
mDestination; /* source and dest address */
|
||||
uint8_t mTtl; /* time to live */
|
||||
uint8_t mProtocol; /* protocol */
|
||||
uint16_t mChecksum; /* checksum */
|
||||
in_addr mSource,
|
||||
mDestination; /* source and dest address */
|
||||
|
||||
int headerLength() const
|
||||
int headerLength() const
|
||||
{
|
||||
return (mVhl & 0x0f) * 4;
|
||||
}
|
||||
|
||||
int version() const
|
||||
{
|
||||
return mVhl >> 4;
|
||||
}
|
||||
|
||||
const in_addr& source4() const { return mSource; }
|
||||
const in_addr& dest4() const { return mDestination; }
|
||||
const in6_addr& source6() const { return (const in6_addr&)mSource; }
|
||||
const in6_addr& dest6() const { return (const in6_addr&)mDestination; }
|
||||
};
|
||||
|
||||
struct UdpHeader
|
||||
{
|
||||
return (mVhl & 0x0f) * 4;
|
||||
}
|
||||
uint16_t mSourcePort; /* source port */
|
||||
uint16_t mDestinationPort;
|
||||
uint16_t mDatagramLength; /* datagram length */
|
||||
uint16_t mDatagramChecksum; /* datagram checksum */
|
||||
};
|
||||
|
||||
int version() const
|
||||
|
||||
struct TcpHeader
|
||||
{
|
||||
return mVhl >> 4;
|
||||
}
|
||||
|
||||
const in_addr& source4() const { return mSource; }
|
||||
const in_addr& dest4() const { return mDestination; }
|
||||
const in6_addr& source6() const { return (const in6_addr&)mSource; }
|
||||
const in6_addr& dest6() const { return (const in6_addr&)mDestination; }
|
||||
};
|
||||
|
||||
struct UdpHeader
|
||||
{
|
||||
uint16_t mSourcePort; /* source port */
|
||||
uint16_t mDestinationPort;
|
||||
uint16_t mDatagramLength; /* datagram length */
|
||||
uint16_t mDatagramChecksum; /* datagram checksum */
|
||||
};
|
||||
|
||||
|
||||
struct TcpHeader
|
||||
{
|
||||
uint16_t mSourcePort; /* source port */
|
||||
uint16_t mDestinationPort; /* destination port */
|
||||
uint32_t mSeqNo; /* sequence number */
|
||||
uint32_t mAckNo; /* acknowledgement number */
|
||||
uint32_t mDataOffset; /* data offset, rsvd */
|
||||
uint16_t mSourcePort; /* source port */
|
||||
uint16_t mDestinationPort; /* destination port */
|
||||
uint32_t mSeqNo; /* sequence number */
|
||||
uint32_t mAckNo; /* acknowledgement number */
|
||||
uint32_t mDataOffset; /* data offset, rsvd */
|
||||
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
|
||||
uint8_t mFlags;
|
||||
uint8_t mFlags;
|
||||
#define TH_FIN 0x01
|
||||
#define TH_SYN 0x02
|
||||
#define TH_RST 0x04
|
||||
|
|
@ -114,10 +114,10 @@ public:
|
|||
#define TH_ECE 0x40
|
||||
#define TH_CWR 0x80
|
||||
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
|
||||
uint16_t mWindow; /* window */
|
||||
uint16_t mChecksum; /* checksum */
|
||||
uint16_t mUrgentPointer; /* urgent pointer */
|
||||
};
|
||||
uint16_t mWindow; /* window */
|
||||
uint16_t mChecksum; /* checksum */
|
||||
uint16_t mUrgentPointer; /* urgent pointer */
|
||||
};
|
||||
|
||||
};
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Reference in New Issue