diff --git a/src/applications/ping6/ping6.cc b/src/applications/ping6/ping6.cc index c17fe7934..156910382 100644 --- a/src/applications/ping6/ping6.cc +++ b/src/applications/ping6/ping6.cc @@ -239,7 +239,10 @@ void Ping6::HandleRead (Ptr socket) packet->RemoveHeader (hdr); - switch (*packet->PeekData ()) + uint8_t type; + packet->CopyData (&type, sizeof(type)); + + switch (type) { case Icmpv6Header::ICMPV6_ECHO_REPLY: packet->RemoveHeader (reply); diff --git a/src/applications/radvd/radvd.cc b/src/applications/radvd/radvd.cc index e568c123b..1dcf7e191 100644 --- a/src/applications/radvd/radvd.cc +++ b/src/applications/radvd/radvd.cc @@ -247,7 +247,10 @@ void Radvd::HandleRead (Ptr socket) Time t; packet->RemoveHeader (hdr); - switch (*packet->PeekData ()) + uint8_t type; + packet->CopyData (&type, sizeof(type)); + + switch (type) { case Icmpv6Header::ICMPV6_ND_ROUTER_SOLICITATION: packet->RemoveHeader (rsHdr); diff --git a/src/common/packet.cc b/src/common/packet.cc index bfe715b4e..d4c985b86 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -1134,8 +1134,13 @@ PacketTest::DoRun (void) NS_TEST_EXPECT_MSG_EQ (packet->GetSize (), 11, "trivial"); - std::string msg = std::string (reinterpret_cast(packet->PeekData ()), + uint8_t *buf = new uint8_t[packet->GetSize ()]; + packet->CopyData (buf, packet->GetSize ()); + + std::string msg = std::string (reinterpret_cast(buf), packet->GetSize ()); + delete [] buf; + NS_TEST_EXPECT_MSG_EQ (msg, "hello world", "trivial"); @@ -1294,8 +1299,10 @@ PacketTest::DoRun (void) CHECK (tmp, 1, E (20, 2, 1002)); tmp->RemoveAtStart (1); CHECK (tmp, 1, E (20, 1, 1001)); +#if 0 tmp->PeekData (); CHECK (tmp, 1, E (20, 1, 1001)); +#endif } return GetErrorStatus (); diff --git a/src/internet-stack/icmpv6-header.cc b/src/internet-stack/icmpv6-header.cc index b624585f5..46bd06ef1 100644 --- a/src/internet-stack/icmpv6-header.cc +++ b/src/internet-stack/icmpv6-header.cc @@ -983,7 +983,6 @@ uint32_t Icmpv6DestinationUnreachable::GetSerializedSize () const void Icmpv6DestinationUnreachable::Serialize (Buffer::Iterator start) const { - const uint8_t *packet = m_packet->PeekData (); uint16_t checksum = 0; Buffer::Iterator i = start; @@ -992,7 +991,11 @@ void Icmpv6DestinationUnreachable::Serialize (Buffer::Iterator start) const i.WriteHtonU16 (0); i.WriteHtonU32 (0); - i.Write (packet, m_packet->GetSize ()); + uint32_t size = m_packet->GetSize (); + uint8_t *buf = new uint8_t[size]; + m_packet->CopyData (buf, size); + i.Write (buf, size); + delete[] buf; i = start; checksum = i.CalculateIpChecksum (i.GetSize (), GetChecksum ()); @@ -1078,7 +1081,6 @@ uint32_t Icmpv6TooBig::GetSerializedSize () const void Icmpv6TooBig::Serialize (Buffer::Iterator start) const { - const uint8_t *packet = m_packet->PeekData (); uint16_t checksum = 0; Buffer::Iterator i = start; @@ -1087,7 +1089,11 @@ void Icmpv6TooBig::Serialize (Buffer::Iterator start) const i.WriteHtonU16 (0); i.WriteHtonU32 (GetMtu ()); - i.Write (packet, m_packet->GetSize ()); + uint32_t size = m_packet->GetSize (); + uint8_t *buf = new uint8_t[size]; + m_packet->CopyData (buf, size); + i.Write (buf, size); + delete[] buf; i = start; checksum = i.CalculateIpChecksum (i.GetSize (), GetChecksum ()); @@ -1162,7 +1168,6 @@ uint32_t Icmpv6TimeExceeded::GetSerializedSize () const void Icmpv6TimeExceeded::Serialize (Buffer::Iterator start) const { - const uint8_t *packet = m_packet->PeekData (); uint16_t checksum = 0; Buffer::Iterator i = start; @@ -1171,7 +1176,11 @@ void Icmpv6TimeExceeded::Serialize (Buffer::Iterator start) const i.WriteHtonU16 (0); i.WriteHtonU32 (0); - i.Write (packet, m_packet->GetSize ()); + uint32_t size = m_packet->GetSize (); + uint8_t *buf = new uint8_t[size]; + m_packet->CopyData (buf, size); + i.Write (buf, size); + delete[] buf; i = start; checksum = i.CalculateIpChecksum (i.GetSize (), GetChecksum ()); @@ -1257,7 +1266,6 @@ uint32_t Icmpv6ParameterError::GetSerializedSize () const void Icmpv6ParameterError::Serialize (Buffer::Iterator start) const { - const uint8_t *packet = m_packet->PeekData (); uint16_t checksum = 0; Buffer::Iterator i = start; @@ -1266,7 +1274,11 @@ void Icmpv6ParameterError::Serialize (Buffer::Iterator start) const i.WriteHtonU16 (0); i.WriteHtonU32 (GetPtr ()); - i.Write (packet, m_packet->GetSize ()); + uint32_t size = m_packet->GetSize (); + uint8_t *buf = new uint8_t[size]; + m_packet->CopyData (buf, size); + i.Write (buf, size); + delete[] buf; i = start; checksum = i.CalculateIpChecksum (i.GetSize (), GetChecksum ()); @@ -1749,7 +1761,11 @@ void Icmpv6OptionRedirected::Serialize (Buffer::Iterator start) const i.WriteU16 (0); i.WriteU32 (0); - i.Write (m_packet->PeekData (), m_packet->GetSize ()); + uint32_t size = m_packet->GetSize (); + uint8_t *buf = new uint8_t[size]; + m_packet->CopyData (buf, size); + i.Write (buf, size); + delete[] buf; } uint32_t Icmpv6OptionRedirected::Deserialize (Buffer::Iterator start) diff --git a/src/internet-stack/icmpv6-l4-protocol.cc b/src/internet-stack/icmpv6-l4-protocol.cc index 5b94daf02..260d15158 100644 --- a/src/internet-stack/icmpv6-l4-protocol.cc +++ b/src/internet-stack/icmpv6-l4-protocol.cc @@ -180,7 +180,11 @@ enum Ipv6L4Protocol::RxStatus_e Icmpv6L4Protocol::Receive (Ptr packet, I Ptr p = packet->Copy (); Ptr ipv6 = m_node->GetObject (); - switch (*p->PeekData ()) /* very ugly! try to find something better in the future */ + /* very ugly! try to find something better in the future */ + uint8_t type; + p->CopyData (&type, sizeof(type)); + + switch (type) { case Icmpv6Header::ICMPV6_ND_ROUTER_SOLICITATION: if (ipv6->IsForwarding (ipv6->GetInterfaceForDevice (interface->GetDevice ()))) @@ -217,7 +221,7 @@ enum Ipv6L4Protocol::RxStatus_e Icmpv6L4Protocol::Receive (Ptr packet, I case Icmpv6Header::ICMPV6_ERROR_PARAMETER_ERROR: break; default: - NS_LOG_LOGIC ("Unknown ICMPv6 message type=" << (uint8_t)*p->PeekData ()); + NS_LOG_LOGIC ("Unknown ICMPv6 message type=" << type); break; } @@ -372,7 +376,10 @@ void Icmpv6L4Protocol::HandleRS (Ptr packet, Ipv6Address const &src, Ipv { /* XXX search all options following the RS header */ /* test if the next option is SourceLinkLayerAddress */ - if (*packet->PeekData () != Icmpv6Header::ICMPV6_OPT_LINK_LAYER_SOURCE) + uint8_t type; + packet->CopyData (&type, sizeof(type)); + + if (type != Icmpv6Header::ICMPV6_OPT_LINK_LAYER_SOURCE) { return; } @@ -440,7 +447,10 @@ void Icmpv6L4Protocol::HandleNS (Ptr packet, Ipv6Address const &src, Ipv if (src != Ipv6Address::GetAny ()) { - if (*packet->PeekData () != Icmpv6Header::ICMPV6_OPT_LINK_LAYER_SOURCE) + uint8_t type; + packet->CopyData (&type, sizeof(type)); + + if (type != Icmpv6Header::ICMPV6_OPT_LINK_LAYER_SOURCE) { return; } @@ -570,7 +580,10 @@ void Icmpv6L4Protocol::HandleNA (Ptr packet, Ipv6Address const &src, Ipv /* XXX search all options following the NA header */ /* Get LLA */ - if (*packet->PeekData () != Icmpv6Header::ICMPV6_OPT_LINK_LAYER_TARGET) + uint8_t type; + packet->CopyData (&type, sizeof(type)); + + if (type != Icmpv6Header::ICMPV6_OPT_LINK_LAYER_TARGET) { return; } @@ -667,7 +680,9 @@ void Icmpv6L4Protocol::HandleRedirection (Ptr packet, Ipv6Address const p->RemoveHeader (redirectionHeader); /* little ugly try to find a better way */ - if (*p->PeekData () == Icmpv6Header::ICMPV6_OPT_LINK_LAYER_TARGET) + uint8_t type; + p->CopyData (&type, sizeof(type)); + if (type == Icmpv6Header::ICMPV6_OPT_LINK_LAYER_TARGET) { hasLla = true; p->RemoveHeader (llOptionHeader); diff --git a/src/internet-stack/ipv6-extension.cc b/src/internet-stack/ipv6-extension.cc index f6f83bc2e..4f03f2b93 100644 --- a/src/internet-stack/ipv6-extension.cc +++ b/src/internet-stack/ipv6-extension.cc @@ -97,7 +97,10 @@ uint8_t Ipv6Extension::ProcessOptions (Ptr& packet, uint8_t offset, uint Ptr ipv6Option; uint8_t processedSize = 0; - const uint8_t *data = p->PeekData (); + uint32_t size = p->GetSize(); + uint8_t *data = new uint8_t[size]; + p->CopyData (data, size); + uint8_t optionType = 0; uint8_t optionLength = 0; @@ -161,6 +164,8 @@ uint8_t Ipv6Extension::ProcessOptions (Ptr& packet, uint8_t offset, uint p->RemoveAtStart (optionLength); } + delete [] data; + return processedSize; } @@ -378,9 +383,12 @@ void Ipv6ExtensionFragment::GetFragments (Ptr packet, uint32_t maxFragme uint8_t nextHeader = ipv6Header.GetNextHeader (); uint8_t ipv6HeaderSize = ipv6Header.GetSerializedSize (); + uint8_t type; + p->CopyData (&type, sizeof(type)); + bool moreHeader = true; if (!(nextHeader == Ipv6Header::IPV6_EXT_HOP_BY_HOP || nextHeader == Ipv6Header::IPV6_EXT_ROUTING - || (nextHeader == Ipv6Header::IPV6_EXT_DESTINATION && *p->PeekData () == Ipv6Header::IPV6_EXT_ROUTING))) + || (nextHeader == Ipv6Header::IPV6_EXT_DESTINATION && type == Ipv6Header::IPV6_EXT_ROUTING))) { moreHeader = false; ipv6Header.SetNextHeader (Ipv6Header::IPV6_EXT_FRAGMENTATION); @@ -403,8 +411,11 @@ void Ipv6ExtensionFragment::GetFragments (Ptr packet, uint32_t maxFragme nextHeader = hopbyhopHeader->GetNextHeader (); extensionHeaderLength = hopbyhopHeader->GetLength (); + uint8_t type; + p->CopyData (&type, sizeof(type)); + if (!(nextHeader == Ipv6Header::IPV6_EXT_HOP_BY_HOP || nextHeader == Ipv6Header::IPV6_EXT_ROUTING - || (nextHeader == Ipv6Header::IPV6_EXT_DESTINATION && *p->PeekData () == Ipv6Header::IPV6_EXT_ROUTING))) + || (nextHeader == Ipv6Header::IPV6_EXT_DESTINATION && type == Ipv6Header::IPV6_EXT_ROUTING))) { moreHeader = false; hopbyhopHeader->SetNextHeader (Ipv6Header::IPV6_EXT_FRAGMENTATION); @@ -415,7 +426,9 @@ void Ipv6ExtensionFragment::GetFragments (Ptr packet, uint32_t maxFragme } else if (nextHeader == Ipv6Header::IPV6_EXT_ROUTING) { - uint8_t numberAddress = (*(p->PeekData () + 1)) / 2; + uint8_t buf[2]; + p->CopyData (buf, sizeof(buf)); + uint8_t numberAddress = buf[1] / 2; Ipv6ExtensionLooseRoutingHeader *routingHeader = new Ipv6ExtensionLooseRoutingHeader (); routingHeader->SetNumberAddress (numberAddress); p->RemoveHeader (*routingHeader); @@ -423,8 +436,10 @@ void Ipv6ExtensionFragment::GetFragments (Ptr packet, uint32_t maxFragme nextHeader = routingHeader->GetNextHeader (); extensionHeaderLength = routingHeader->GetLength (); + uint8_t type; + p->CopyData (&type, sizeof(type)); if (!(nextHeader == Ipv6Header::IPV6_EXT_HOP_BY_HOP || nextHeader == Ipv6Header::IPV6_EXT_ROUTING - || (nextHeader == Ipv6Header::IPV6_EXT_DESTINATION && *p->PeekData () == Ipv6Header::IPV6_EXT_ROUTING))) + || (nextHeader == Ipv6Header::IPV6_EXT_DESTINATION && type == Ipv6Header::IPV6_EXT_ROUTING))) { moreHeader = false; routingHeader->SetNextHeader (Ipv6Header::IPV6_EXT_FRAGMENTATION); @@ -441,8 +456,10 @@ void Ipv6ExtensionFragment::GetFragments (Ptr packet, uint32_t maxFragme nextHeader = destinationHeader->GetNextHeader (); extensionHeaderLength = destinationHeader->GetLength (); + uint8_t type; + p->CopyData (&type, sizeof(type)); if (!(nextHeader == Ipv6Header::IPV6_EXT_HOP_BY_HOP || nextHeader == Ipv6Header::IPV6_EXT_ROUTING - || (nextHeader == Ipv6Header::IPV6_EXT_DESTINATION && *p->PeekData () == Ipv6Header::IPV6_EXT_ROUTING))) + || (nextHeader == Ipv6Header::IPV6_EXT_DESTINATION && type == Ipv6Header::IPV6_EXT_ROUTING))) { moreHeader = false; destinationHeader->SetNextHeader (Ipv6Header::IPV6_EXT_FRAGMENTATION); @@ -637,12 +654,13 @@ uint8_t Ipv6ExtensionRouting::Process (Ptr& packet, uint8_t offset, Ipv6 Ptr p = packet->Copy (); p->RemoveAtStart (offset); - const uint8_t *buff = packet->PeekData (); + uint8_t buf[4]; + packet->CopyData(buf, sizeof(buf)); - uint8_t routingNextHeader = *buff; - uint8_t routingLength = *(buff + 1); - uint8_t routingTypeRouting = *(buff + 2); - uint8_t routingSegmentsLeft = *(buff + 3); + uint8_t routingNextHeader = buf[0]; + uint8_t routingLength = buf[1]; + uint8_t routingTypeRouting = buf[2]; + uint8_t routingSegmentsLeft = buf[3]; if (nextHeader) { @@ -786,7 +804,9 @@ uint8_t Ipv6ExtensionLooseRouting::Process (Ptr& packet, uint8_t offset, ipv6header.Deserialize (it); // Get the number of routers' address field - uint8_t numberAddress = (*(p->PeekData () + 1)) / 2; + uint8_t buf[2]; + p->CopyData (buf, sizeof(buf)); + uint8_t numberAddress = buf[1] / 2; Ipv6ExtensionLooseRoutingHeader routingHeader; routingHeader.SetNumberAddress (numberAddress); p->RemoveHeader (routingHeader); diff --git a/src/internet-stack/ipv6-l3-protocol.cc b/src/internet-stack/ipv6-l3-protocol.cc index 3392b590d..0286b1489 100644 --- a/src/internet-stack/ipv6-l3-protocol.cc +++ b/src/internet-stack/ipv6-l3-protocol.cc @@ -954,10 +954,10 @@ void Ipv6L3Protocol::LocalDeliver (Ptr packet, Ipv6Header const& i /* process hop-by-hop extension first if exists */ if (nextHeader == Ipv6Header::IPV6_EXT_HOP_BY_HOP) { - const uint8_t *buff = p->PeekData (); - - nextHeader = *buff; - nextHeaderPosition = *(buff + 1); + uint8_t buf[2]; + p->CopyData(buf, sizeof(buf)); + nextHeader = buf[0]; + nextHeaderPosition = buf[1]; } /* process all the extensions found and the layer 4 protocol */ diff --git a/src/internet-stack/ipv6-raw-socket-impl.cc b/src/internet-stack/ipv6-raw-socket-impl.cc index c2c0b54c2..e07fdc75d 100644 --- a/src/internet-stack/ipv6-raw-socket-impl.cc +++ b/src/internet-stack/ipv6-raw-socket-impl.cc @@ -224,7 +224,9 @@ int Ipv6RawSocketImpl::SendTo (Ptr p, uint32_t flags, const Address& toA /* calculate checksum here for ICMPv6 echo request (sent by ping6) * as we cannot determine source IPv6 address at application level */ - if (*p->PeekData () == Icmpv6Header::ICMPV6_ECHO_REQUEST) + uint8_t type; + p->CopyData (&type, sizeof(type)); + if (type == Icmpv6Header::ICMPV6_ECHO_REQUEST) { Icmpv6Echo hdr (1); p->RemoveHeader (hdr); diff --git a/src/internet-stack/nsc-tcp-l4-protocol.cc b/src/internet-stack/nsc-tcp-l4-protocol.cc index 1cbd647a3..6b5ca448d 100644 --- a/src/internet-stack/nsc-tcp-l4-protocol.cc +++ b/src/internet-stack/nsc-tcp-l4-protocol.cc @@ -325,10 +325,14 @@ NscTcpL4Protocol::Receive (Ptr packet, packet->AddHeader(ipHeader); packetSize = packet->GetSize(); - const uint8_t *data = const_cast(packet->PeekData()); + uint8_t *buf = new uint8_t[packetSize]; + packet->CopyData (buf, packetSize); + const uint8_t *data = const_cast(buf); // deliver complete packet to the NSC network stack m_nscStack->if_receive_packet(0, data, packetSize); + delete[] buf; + wakeup (); return Ipv4L4Protocol::RX_OK; } diff --git a/src/internet-stack/nsc-tcp-socket-impl.cc b/src/internet-stack/nsc-tcp-socket-impl.cc index f0af3f9e0..619e4cd8b 100644 --- a/src/internet-stack/nsc-tcp-socket-impl.cc +++ b/src/internet-stack/nsc-tcp-socket-impl.cc @@ -622,7 +622,12 @@ bool NscTcpSocketImpl::SendPendingData (void) NS_ASSERT (size > 0); m_errno = ERROR_NOTERROR; - ret = m_nscTcpSocket->send_data((const char *)p->PeekData (), size); + + uint8_t *buf = new uint8_t[size]; + p->CopyData (buf, size); + ret = m_nscTcpSocket->send_data((const char *)buf, size); + delete[] buf; + if (ret <= 0) { break; diff --git a/src/test/ns3tcp/ns3tcp-interop-test-suite.cc b/src/test/ns3tcp/ns3tcp-interop-test-suite.cc index 3849d55cc..fce7eb5c3 100644 --- a/src/test/ns3tcp/ns3tcp-interop-test-suite.cc +++ b/src/test/ns3tcp/ns3tcp-interop-test-suite.cc @@ -153,10 +153,16 @@ Ns3TcpInteroperabilityTestCase::Ipv4L3Tx (std::string context, Ptr // Time tNow = Simulator::Now (); int64_t tMicroSeconds = tNow.GetMicroSeconds (); + + uint32_t size = p->GetSize (); + uint8_t *buf = new uint8_t[size]; + p->CopyData (buf, size); + m_pcapFile.Write (uint32_t (tMicroSeconds / 1000000), uint32_t (tMicroSeconds % 1000000), - p->PeekData(), - p->GetSize ()); + buf, + size); + delete [] buf; } else { @@ -168,10 +174,13 @@ Ns3TcpInteroperabilityTestCase::Ipv4L3Tx (std::string context, Ptr uint32_t tsSec, tsUsec, inclLen, origLen, readLen; m_pcapFile.Read (expected, sizeof(expected), tsSec, tsUsec, inclLen, origLen, readLen); - uint8_t const *actual = p->PeekData(); + uint8_t *actual = new uint8_t[readLen]; + p->CopyData (actual, readLen); uint32_t result = memcmp(actual, expected, readLen); + delete [] actual; + // // Avoid streams of errors -- only report the first. // diff --git a/src/test/ns3tcp/ns3tcp-loss-test-suite.cc b/src/test/ns3tcp/ns3tcp-loss-test-suite.cc index d2aea7c9d..4aca58cc0 100644 --- a/src/test/ns3tcp/ns3tcp-loss-test-suite.cc +++ b/src/test/ns3tcp/ns3tcp-loss-test-suite.cc @@ -127,10 +127,16 @@ Ns3TcpLossTestCase1::Ipv4L3Tx (std::string context, Ptr packet, Pt // Time tNow = Simulator::Now (); int64_t tMicroSeconds = tNow.GetMicroSeconds (); + + uint32_t size = p->GetSize (); + uint8_t *buf = new uint8_t[size]; + p->CopyData (buf, size); + m_pcapFile.Write (uint32_t (tMicroSeconds / 1000000), uint32_t (tMicroSeconds % 1000000), - p->PeekData(), - p->GetSize ()); + buf, + size); + delete [] buf; } else { @@ -142,10 +148,13 @@ Ns3TcpLossTestCase1::Ipv4L3Tx (std::string context, Ptr packet, Pt uint32_t tsSec, tsUsec, inclLen, origLen, readLen; m_pcapFile.Read (expected, sizeof(expected), tsSec, tsUsec, inclLen, origLen, readLen); - uint8_t const *actual = p->PeekData(); + uint8_t *actual = new uint8_t[readLen]; + p->CopyData (actual, readLen); uint32_t result = memcmp(actual, expected, readLen); + delete [] actual; + // // Avoid streams of errors -- only report the first. // @@ -303,10 +312,14 @@ Ns3TcpLossTestCase2::Ipv4L3Tx (std::string context, Ptr packet, Pt // Time tNow = Simulator::Now (); int64_t tMicroSeconds = tNow.GetMicroSeconds (); + uint32_t size = p->GetSize (); + uint8_t *buf = new uint8_t[size]; + p->CopyData (buf, size); + m_pcapFile.Write (uint32_t (tMicroSeconds / 1000000), uint32_t (tMicroSeconds % 1000000), - p->PeekData(), - p->GetSize ()); + buf, + size); } else { @@ -318,10 +331,13 @@ Ns3TcpLossTestCase2::Ipv4L3Tx (std::string context, Ptr packet, Pt uint32_t tsSec, tsUsec, inclLen, origLen, readLen; m_pcapFile.Read (expected, sizeof(expected), tsSec, tsUsec, inclLen, origLen, readLen); - uint8_t const *actual = p->PeekData(); + uint8_t *actual = new uint8_t[readLen]; + p->CopyData (actual, readLen); uint32_t result = memcmp(actual, expected, readLen); + delete [] actual; + // // Avoid streams of errors -- only report the first. //