INET Framework for OMNeT++/OMNEST
inet::serializer::IPv6Serializer Class Reference

Converts between IPv6Datagram and binary (network byte order) IPv6 header. More...

#include <IPv6Serializer.h>

Inheritance diagram for inet::serializer::IPv6Serializer:
inet::serializer::SerializerBase

Public Member Functions

 IPv6Serializer (const char *name=nullptr)
 
- Public Member Functions inherited from inet::serializer::SerializerBase
 SerializerBase (const char *name=nullptr)
 
void serializePacket (const cPacket *pkt, Buffer &b, Context &context)
 
cPacket * deserializePacket (const Buffer &b, Context &context)
 

Protected Member Functions

virtual void serialize (const cPacket *pkt, Buffer &b, Context &context) override
 Serializes a cPacket for transmission on the wire. More...
 
virtual cPacket * deserialize (const Buffer &b, Context &context) override
 Puts a packet sniffed from the wire into an EtherFrame. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from inet::serializer::SerializerBase
static SerializerBaselookupSerializer (const cPacket *pkt, Context &context, ProtocolGroup group, int id)
 
static void lookupAndSerialize (const cPacket *pkt, Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
 
static SerializerBaselookupDeserializer (Context &context, ProtocolGroup group, int id)
 
static cPacket * lookupAndDeserialize (const Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
 

Detailed Description

Converts between IPv6Datagram and binary (network byte order) IPv6 header.

Constructor & Destructor Documentation

inet::serializer::IPv6Serializer::IPv6Serializer ( const char *  name = nullptr)
inline
37 : SerializerBase(name) {}
SerializerBase(const char *name=nullptr)
Definition: SerializerBase.h:84

Member Function Documentation

cPacket * inet::serializer::IPv6Serializer::deserialize ( const Buffer b,
Context context 
)
overrideprotectedvirtual

Puts a packet sniffed from the wire into an EtherFrame.

Implements inet::serializer::SerializerBase.

150 {
151  ASSERT(b.getPos() == 0);
152  IPv6Datagram *dest = new IPv6Datagram("parsed-ipv6");
153  const struct ip6_hdr *ip6h = (struct ip6_hdr *) b.accessNBytes(IPv6_HEADER_BYTES);
154  uint32_t flowinfo = ntohl(ip6h->ip6_flow);
155  dest->setFlowLabel(flowinfo & 0xFFFFF);
156  flowinfo >>= 20;
157  dest->setTrafficClass(flowinfo & 0xFF);
158 
159  unsigned int packetLength = ntohs(ip6h->ip6_plen);
160 
161  dest->setTransportProtocol(ip6h->ip6_nxt);
162  dest->setHopLimit(ntohs(ip6h->ip6_hlim));
163 
164  IPv6Address temp;
165  temp.set(ntohl(ip6h->ip6_src.__u6_addr.__u6_addr32[0]),
166  ntohl(ip6h->ip6_src.__u6_addr.__u6_addr32[1]),
167  ntohl(ip6h->ip6_src.__u6_addr.__u6_addr32[2]),
168  ntohl(ip6h->ip6_src.__u6_addr.__u6_addr32[3]));
169  dest->setSrcAddress(temp);
170 
171  temp.set(ntohl(ip6h->ip6_dst.__u6_addr.__u6_addr32[0]),
172  ntohl(ip6h->ip6_dst.__u6_addr.__u6_addr32[1]),
173  ntohl(ip6h->ip6_dst.__u6_addr.__u6_addr32[2]),
174  ntohl(ip6h->ip6_dst.__u6_addr.__u6_addr32[3]));
175  dest->setDestAddress(temp);
176 
177  c.l3AddressesPtr = &ip6h->ip6_src.__u6_addr.__u6_addr32[0];
178  c.l3AddressesLength = 32;
179 
180  if (packetLength + IPv6_HEADER_BYTES > b._getBufSize()) {
181  EV_ERROR << "Can not handle IPv6 packet of total length " << packetLength + IPv6_HEADER_BYTES << "(captured only " << b._getBufSize() << " bytes).\n";
182  b.setError();
183  }
184  dest->setByteLength(b.getPos()); // set header size
185 
186  cPacket *encapPacket = SerializerBase::lookupAndDeserialize(b, c, IP_PROT, dest->getTransportProtocol(), packetLength);
187  if (encapPacket) {
188  dest->encapsulate(encapPacket);
189  dest->setName(encapPacket->getName());
190  }
191  return dest;
192 }
u16_t ntohs(u16_t n)
Definition: SerializerBase.h:38
u32_t ntohl(u32_t n)
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
const int IPv6_HEADER_BYTES
Definition: IPv6Datagram_m.h:44
static cPacket * lookupAndDeserialize(const Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
Definition: SerializerBase.cc:99
value< double, units::m > b
Definition: Units.h:1054
void inet::serializer::IPv6Serializer::serialize ( const cPacket *  pkt,
Buffer b,
Context context 
)
overrideprotectedvirtual

Serializes a cPacket for transmission on the wire.

Returns the length of data written into buffer.

Implements inet::serializer::SerializerBase.

58 {
59  const IPv6Datagram *dgram = check_and_cast<const IPv6Datagram *>(pkt);
60  unsigned int i;
61  uint32_t flowinfo;
62 
63  EV << "Serialize IPv6 packet\n";
64 
65  unsigned int nextHdrCodePos = b.getPos() + 6;
66  struct ip6_hdr *ip6h = (struct ip6_hdr *)b.accessNBytes(sizeof(struct ip6_hdr));
67 
68  flowinfo = 0x06;
69  flowinfo <<= 8;
70  flowinfo |= dgram->getTrafficClass();
71  flowinfo <<= 20;
72  flowinfo |= dgram->getFlowLabel();
73  ip6h->ip6_flow = htonl(flowinfo);
74  ip6h->ip6_hlim = htons(dgram->getHopLimit());
75 
76  ip6h->ip6_nxt = dgram->getTransportProtocol();
77 
78  for (i = 0; i < 4; i++) {
79  ip6h->ip6_src.__u6_addr.__u6_addr32[i] = htonl(dgram->getSrcAddress().words()[i]);
80  }
81  for (i = 0; i < 4; i++) {
82  ip6h->ip6_dst.__u6_addr.__u6_addr32[i] = htonl(dgram->getDestAddress().words()[i]);
83  }
84  c.l3AddressesPtr = &ip6h->ip6_src.__u6_addr.__u6_addr32[0];
85  c.l3AddressesLength = 32;
86 
87  //FIXME serialize extension headers
88  for (i = 0; i < dgram->getExtensionHeaderArraySize(); i++) {
89  const IPv6ExtensionHeader *extHdr = dgram->getExtensionHeader(i);
90  b.writeByteTo(nextHdrCodePos, extHdr->getExtensionType());
91  nextHdrCodePos = b.getPos();
92  b.writeByte(dgram->getTransportProtocol());
93  ASSERT((extHdr->getByteLength() & 7) == 0);
94  b.writeByte((extHdr->getByteLength() - 8) / 8);
95  switch (extHdr->getExtensionType()) {
96  case IP_PROT_IPv6EXT_HOP: {
97  const IPv6HopByHopOptionsHeader *hdr = check_and_cast<const IPv6HopByHopOptionsHeader *>(extHdr);
98  b.fillNBytes(hdr->getByteLength() - 2, '\0'); //TODO
99  break;
100  }
101  case IP_PROT_IPv6EXT_DEST: {
102  const IPv6DestinationOptionsHeader *hdr = check_and_cast<const IPv6DestinationOptionsHeader *>(extHdr);
103  b.fillNBytes(hdr->getByteLength() - 2, '\0'); //TODO
104  break;
105  }
107  const IPv6RoutingHeader *hdr = check_and_cast<const IPv6RoutingHeader *>(extHdr);
108  b.writeByte(hdr->getRoutingType());
109  b.writeByte(hdr->getSegmentsLeft());
110  for (unsigned int j = 0; j < hdr->getAddressArraySize(); j++) {
111  b.writeIPv6Address(hdr->getAddress(j));
112  }
113  b.fillNBytes(4, '\0');
114  break;
115  }
117  const IPv6FragmentHeader *hdr = check_and_cast<const IPv6FragmentHeader *>(extHdr);
118  ASSERT((hdr->getFragmentOffset() & 7) == 0);
119  b.writeUint16(hdr->getFragmentOffset() | (hdr->getMoreFragments() ? 1 : 0));
120  b.writeUint32(hdr->getIdentification());
121  break;
122  }
123  case IP_PROT_IPv6EXT_AUTH: {
124  const IPv6AuthenticationHeader *hdr = check_and_cast<const IPv6AuthenticationHeader *>(extHdr);
125  b.fillNBytes(hdr->getByteLength() - 2, '\0'); //TODO
126  break;
127  }
128  case IP_PROT_IPv6EXT_ESP: {
129  const IPv6EncapsulatingSecurityPayloadHeader *hdr = check_and_cast<const IPv6EncapsulatingSecurityPayloadHeader *>(extHdr);
130  b.fillNBytes(hdr->getByteLength() - 2, '\0'); //TODO
131  break;
132  }
133  default: {
134  throw cRuntimeError("Unknown IPv6 extension header %d (%s)%s", extHdr->getExtensionType(), extHdr->getClassName(), extHdr->getFullName());
135  break;
136  }
137  }
138  ASSERT(nextHdrCodePos + extHdr->getByteLength() == b.getPos());
139  }
140 
141  const cPacket *encapPacket = dgram->getEncapsulatedPacket();
142  unsigned int encapStart = b.getPos();
143  SerializerBase::lookupAndSerialize(encapPacket, b, c, IP_PROT, dgram->getTransportProtocol());
144  unsigned int encapEnd = b.getPos();
145 
146  ip6h->ip6_plen = htons(encapEnd - encapStart);
147 }
Definition: SerializerBase.h:38
Definition: IPProtocolId_m.h:94
Definition: IPProtocolId_m.h:95
u16_t htons(u16_t n)
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
Definition: IPProtocolId_m.h:98
u32_t htonl(u32_t n)
static void lookupAndSerialize(const cPacket *pkt, Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
Definition: SerializerBase.cc:88
Definition: IPProtocolId_m.h:97
Definition: IPProtocolId_m.h:99
value< double, units::m > b
Definition: Units.h:1054
Definition: IPProtocolId_m.h:96

The documentation for this class was generated from the following files: