Serializes a cPacket for transmission on the wire.
Returns the length of data written into buffer.
138 ASSERT(
b.getPos() == 0);
140 if (
typeid(*pkt) !=
typeid(IPv4Datagram)) {
141 if (
c.throwOnSerializerNotFound)
142 throw cRuntimeError(
"IPv4Serializer: class '%s' not accepted", pkt->getClassName());
143 EV_ERROR <<
"IPv4Serializer: class '" << pkt->getClassName() <<
"' not accepted.\n";
144 b.fillNBytes(pkt->getByteLength(),
'?');
150 EV_ERROR <<
"IPv4Serializer: not enough space for IPv4 header.\n";
153 const IPv4Datagram *dgram = check_and_cast<
const IPv4Datagram *>(pkt);
154 unsigned int headerLength = dgram->getHeaderLength();
155 ASSERT((headerLength & 3) == 0);
156 ip->ip_hl = headerLength >> 2;
157 ip->ip_v = dgram->getVersion();
158 ip->ip_tos = dgram->getTypeOfService();
159 ip->ip_id =
htons(dgram->getIdentification());
160 ASSERT((dgram->getFragmentOffset() & 7) == 0);
161 uint16_t ip_off = dgram->getFragmentOffset() / 8;
162 if (dgram->getMoreFragments())
164 if (dgram->getDontFragment())
166 ip->ip_off =
htons(ip_off);
167 ip->ip_ttl = dgram->getTimeToLive();
168 ip->ip_p = dgram->getTransportProtocol();
169 ip->ip_src.s_addr =
htonl(dgram->getSrcAddress().getInt());
170 ip->ip_dst.s_addr =
htonl(dgram->getDestAddress().getInt());
171 ip->ip_len =
htons(dgram->getTotalLengthField());
173 c.l3AddressesPtr = &ip->ip_src.s_addr;
174 c.l3AddressesLength =
sizeof(ip->ip_src.s_addr) +
sizeof(ip->ip_dst.s_addr);
179 b.accessNBytes(sb.getPos());
184 const cPacket *encapPacket = dgram->getEncapsulatedPacket();
185 unsigned int payloadLength = dgram->getByteLength() -
b.getPos();
188 unsigned int totalLength = encapPacket->getByteLength();
189 int fragmentOffset = dgram->getFragmentOffset();
190 if ((dgram->getMoreFragments() || fragmentOffset != 0) && (payloadLength < totalLength)) {
191 char *buf =
new char[totalLength];
192 Buffer tmpBuffer(buf, totalLength);
194 tmpBuffer.seek(fragmentOffset);
195 b.writeNBytes(tmpBuffer, payloadLength);
202 b.fillNBytes(payloadLength,
'?');
#define IP_MF
Definition: ip.h:73
const int IP_HEADER_BYTES
Definition: IPv4Datagram_m.h:41
Definition: SerializerBase.h:38
static uint16_t checksum(const void *addr, unsigned int count)
Definition: TCPIPchecksum.h:44
#define IP_DF
Definition: ip.h:72
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
void serializeOptions(const IPv4Datagram *dgram, Buffer &b, Context &c)
Definition: IPv4Serializer.cc:374
static void lookupAndSerialize(const cPacket *pkt, Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
Definition: SerializerBase.cc:88
value< double, units::m > b
Definition: Units.h:1054