Serializes a cPacket for transmission on the wire.
Returns the length of data written into buffer.
131 ASSERT(
b.getPos() == 0);
132 const TCPSegment *tcpseg = check_and_cast<
const TCPSegment *>(pkt);
135 int writtenbytes = tcpseg->getByteLength();
139 tcp.th_sport =
htons(tcpseg->getSrcPort());
140 tcp.th_dport =
htons(tcpseg->getDestPort());
141 tcp.th_seq =
htonl(tcpseg->getSequenceNo());
142 tcp.th_ack =
htonl(tcpseg->getAckNo());
147 unsigned char flags = 0;
148 if (tcpseg->getFinBit())
150 if (tcpseg->getSynBit())
152 if (tcpseg->getRstBit())
154 if (tcpseg->getPshBit())
156 if (tcpseg->getAckBit())
158 if (tcpseg->getUrgBit())
161 tcp.th_win =
htons(tcpseg->getWindow());
162 tcp.th_urp =
htons(tcpseg->getUrgentPointer());
163 if (tcpseg->getHeaderLength() % 4 != 0)
164 throw cRuntimeError(
"invalid TCP header length=%u: must be dividable by 4", tcpseg->getHeaderLength());
165 tcp.th_offs = tcpseg->getHeaderLength() / 4;
167 unsigned short numOptions = tcpseg->getHeaderOptionArraySize();
168 unsigned int optionsLength = 0;
169 if (numOptions > 0) {
170 for (
unsigned short i = 0; i < numOptions; i++) {
171 const TCPOption *option = tcpseg->getHeaderOption(i);
173 optionsLength += option->getLength();
178 b.fillNBytes(4 - optionsLength, 0);
182 if (tcpseg->getByteLength() > tcpseg->getHeaderLength()) {
183 unsigned int dataLength = tcpseg->getByteLength() - tcpseg->getHeaderLength();
185 if (tcpseg->getByteArray().getDataArraySize() > 0) {
186 ASSERT(tcpseg->getByteArray().getDataArraySize() == dataLength);
187 tcpseg->getByteArray().copyDataToBuffer(
b.accessNBytes(0),
b.getRemainingSize());
188 b.accessNBytes(dataLength);
191 b.fillNBytes(dataLength,
't');
193 writtenbytes =
b.getPos();
#define TH_ACK
Definition: tcphdr.h:30
void serializeOption(const tcp::TCPOption *option, Buffer &b, Context &c)
Definition: TCPSerializer.cc:54
#define TH_PUSH
Definition: tcphdr.h:29
#define TH_URG
Definition: tcphdr.h:31
static uint16_t checksum(const void *addr, unsigned int count)
Definition: TCPIPchecksum.h:44
#define TH_RST
Definition: tcphdr.h:28
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
#define TH_FIN
Definition: tcphdr.h:26
#define TCP_HEADER_OCTETS
Definition: TCPSegment_m.h:50
#define TH_SYN
Definition: tcphdr.h:27
#define TH_FLAGS
Definition: tcphdr.h:32
Definition: IPProtocolId_m.h:80
value< double, units::m > b
Definition: Units.h:1054