Debian Codegraph @

protolib/src/common/protoPktTCP.cpp

raw
1 #include "protoPktTCP.h"
2
3 ProtoPktTCP::ProtoPktTCP(void* bufferPtr,
4 unsigned int numBytes,
5 bool initFromBuffer,
6 bool freeOnDestruct)
7 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
8 {
9 if (NULL != bufferPtr)
10 {
11 if (initFromBuffer)
12 InitFromBuffer();
13 else
14 InitIntoBuffer();
15 }
16 }
17
18 ProtoPktTCP::~ProtoPktTCP()
19 {
20 }
21
22 bool ProtoPktTCP::InitFromPacket(ProtoPktIP& ipPkt)
23 {
24 switch (ipPkt.GetVersion())
25 {
26 case 4:
27 {
28 // (TBD) support IPv4 extended headers
29 ProtoPktIPv4 ip4Pkt(ipPkt);
30 if (ProtoPktIP::TCP == ip4Pkt.GetProtocol())
31 {
32 return InitFromBuffer(ip4Pkt.AccessPayload(), ip4Pkt.GetPayloadLength(), false);
33 }
34 else
35 {
36 return false; // not a TCP packet
37 }
38 break;
39 }
40 case 6:
41 {
42 ProtoPktIPv6 ip6Pkt(ipPkt);
43 if (ip6Pkt.HasExtendedHeader())
44 {
45 unsigned int extHeaderLength = 0;
46 ProtoPktIPv6::Extension::Iterator extIterator(ip6Pkt);
47 ProtoPktIPv6::Extension ext;
48 while (extIterator.GetNextExtension(ext))
49 {
50 extHeaderLength += ext.GetLength();
51 if (ProtoPktIP::TCP == ext.GetNextHeader())
52 {
53 void* tcpBuffer = (char*)ip6Pkt.AccessPayload() + extHeaderLength;
54 unsigned int tcpLength = ip6Pkt.GetPayloadLength() - extHeaderLength;
55 return InitFromBuffer(tcpBuffer, tcpLength, false);
56 }
57 }
58 return false; // not a TCP packet
59 }
60 else if (ProtoPktIP::TCP == ip6Pkt.GetNextHeader())
61 {
62 return InitFromBuffer(ip6Pkt.AccessPayload(), ip6Pkt.GetPayloadLength(), false);
63 }
64 else
65 {
66 return false; // not a TCP packet
67 }
68 break;
69 }
70 default:
71 PLOG(PL_ERROR, "ProtoPktTCP::InitFromPacket() error: bad IP packet version: %d\n", ipPkt.GetVersion());
72 return false;
73 }
74 return true;
75 } // end ProtoPktTCP::InitFromPacket()
76
77 bool ProtoPktTCP::InitFromBuffer(void* bufferPtr,
78 unsigned int numBytes,
79 bool freeOnDestruct)
80 {
81 if (NULL != bufferPtr)
82 AttachBuffer(bufferPtr, numBytes, freeOnDestruct);
83 UINT16 totalLen = GetPayloadLength() + (OffsetPayload() << 2);
84 if (totalLen > GetBufferLength())
85 {
86 ProtoPkt::SetLength(0);
87 if (NULL != bufferPtr) DetachBuffer();
88 return false;
89 }
90 else
91 {
92 // (TBD) We could validate the checksum, too?
93 ProtoPkt::SetLength(totalLen);
94 return true;
95 }
96 } // end bool ProtoPktTCP::InitFromBuffer()
97
98 bool ProtoPktTCP::InitIntoBuffer(void* bufferPtr,
99 unsigned int numBytes,
100 bool freeOnDestruct)
101 {
102 if (NULL != bufferPtr)
103 {
104 if (numBytes < 20) // TCP header size
105 return false;
106 else
107 AttachBuffer(bufferPtr, numBytes, freeOnDestruct);
108 }
109 if (GetBufferLength() < 20) return false;
110 SetDataOffset(5);
111 ClearFlags();
112 SetChecksum(0);
113 return true;
114 } // end ProtoPktTCP::InitIntoBuffer()
115
116 UINT16 ProtoPktTCP::ComputeChecksum(ProtoPktIP& ipPkt) const
117 {
118 UINT32 sum = 0;
119 // 1) Calculate pseudo-header part
120 switch(ipPkt.GetVersion())
121 {
122 case 4:
123 {
124 ProtoPktIPv4 ipv4Pkt(ipPkt);
125 // a) src/dst addr pseudo header portion
126 const UINT16* ptr = (const UINT16*)ipv4Pkt.GetSrcAddrPtr();
127 int addrEndex = ProtoPktIPv4::ADDR_LEN; // note src + dst = 2 addresses
128 for (int i = 0; i < addrEndex; i++)
129 sum += GetUINT16(ptr++);
130 // b) protocol & "total length" pseudo header portions
131 sum += (UINT16)ipv4Pkt.GetProtocol();
132 sum += (UINT16)GetLength(); // TCP length
133 break;
134 }
135 case 6:
136 {
137 ProtoPktIPv6 ipv6Pkt(ipPkt);
138 // a) src/dst addr pseudo header portion
139 const UINT16* ptr = (const UINT16*)ipv6Pkt.GetSrcAddrPtr();
140 int addrEndex = ProtoPktIPv6::ADDR_LEN; // note src + dst = 2 addresses
141 for (int i = 0; i < addrEndex; i++)
142 sum += GetUINT16(ptr++);
143 sum += (UINT16)GetLength(); // TCP length
144 sum += (UINT16)ipv6Pkt.GetNextHeader();
145 break;
146 }
147 default:
148 return 0;
149 }
150 // 2) TCP header part, sans "checksum" field
151 unsigned int i;
152 for (i = 0; i < OFFSET_CHECKSUM; i++)
153 sum += GetWord16(i);
154 // 3) TCP payload part (note adjustment for odd number of payload bytes)
155 unsigned int dataEndex = GetLength();
156 if (0 != (dataEndex & 0x01))
157 sum += ((UINT16)GetUINT8(dataEndex-1) << 8);
158 dataEndex >>= 1; // convert from bytes to UINT16 index
159 for (i = (OFFSET_CHECKSUM+1); i < dataEndex; i++)
160 sum += GetWord16(i);
161
162 // 4) Carry as needed
163 while (0 != (sum >> 16))
164 sum = (sum & 0x0000ffff) + (sum >> 16);
165
166 sum = ~sum;
167
168 // 5) ZERO check/correct as needed
169 if (0 == sum) sum = 0x0000ffff;
170 return (UINT16)sum;
171 } // end ProtoPktTCP::CalculateChecksum()