From 515183caae68715e895359d08dd634658126e4ae Mon Sep 17 00:00:00 2001 From: Craig Dowell Date: Sun, 11 May 2008 11:18:21 -0700 Subject: [PATCH] fix bug 161 --- examples/tcp-large-transfer.cc | 2 +- src/common/pcap-writer.cc | 13 ++- src/common/pcap-writer.h | 2 + .../point-to-point-net-device.cc | 16 ++-- src/devices/point-to-point/ppp-header.cc | 82 +++++++++++++++++++ src/devices/point-to-point/ppp-header.h | 72 ++++++++++++++++ src/devices/point-to-point/wscript | 1 + src/helper/point-to-point-helper.cc | 2 +- 8 files changed, 177 insertions(+), 13 deletions(-) create mode 100644 src/devices/point-to-point/ppp-header.cc create mode 100644 src/devices/point-to-point/ppp-header.h diff --git a/examples/tcp-large-transfer.cc b/examples/tcp-large-transfer.cc index cd304fc57..641de0537 100644 --- a/examples/tcp-large-transfer.cc +++ b/examples/tcp-large-transfer.cc @@ -190,7 +190,7 @@ int main (int argc, char *argv[]) ascii.open ("tcp-large-transfer.tr"); PointToPointHelper::EnableAsciiAll (ascii); - InternetStackHelper::EnablePcapAll ("tcp-large-transfer"); + PointToPointHelper::EnablePcapAll ("tcp-large-transfer"); Simulator::StopAt (Seconds(1000)); Simulator::Run (); diff --git a/src/common/pcap-writer.cc b/src/common/pcap-writer.cc index a4a39bc3e..121802215 100644 --- a/src/common/pcap-writer.cc +++ b/src/common/pcap-writer.cc @@ -34,6 +34,7 @@ namespace ns3 { enum { PCAP_ETHERNET = 1, + PCAP_PPP = 9, PCAP_RAW_IP = 101, PCAP_80211 = 105, }; @@ -42,6 +43,7 @@ PcapWriter::PcapWriter () { m_writer = 0; } + PcapWriter::~PcapWriter () { delete m_writer; @@ -72,6 +74,12 @@ PcapWriter::WriteWifiHeader (void) WriteHeader (PCAP_80211); } +void +PcapWriter::WritePppHeader (void) +{ + WriteHeader (PCAP_PPP); +} + void PcapWriter::WriteHeader (uint32_t network) { @@ -84,9 +92,6 @@ PcapWriter::WriteHeader (uint32_t network) Write32 (network); } - - - void PcapWriter::WritePacket (Ptr packet) { @@ -108,6 +113,7 @@ PcapWriter::WriteData (uint8_t const*buffer, uint32_t size) { m_writer->write ((char const *)buffer, size); } + void PcapWriter::Write32 (uint32_t data) { @@ -118,6 +124,7 @@ PcapWriter::Write32 (uint32_t data) buffer[3] = (data >> 24) & 0xff; WriteData (buffer, 4); } + void PcapWriter::Write16 (uint16_t data) { diff --git a/src/common/pcap-writer.h b/src/common/pcap-writer.h index 87ba8c2c2..2a34a5b63 100644 --- a/src/common/pcap-writer.h +++ b/src/common/pcap-writer.h @@ -60,6 +60,8 @@ public: void WriteWifiHeader (void); + void WritePppHeader (void); + /** * \param packet packet to write to output file */ diff --git a/src/devices/point-to-point/point-to-point-net-device.cc b/src/devices/point-to-point/point-to-point-net-device.cc index 6e3337a2e..fb7d51110 100644 --- a/src/devices/point-to-point/point-to-point-net-device.cc +++ b/src/devices/point-to-point/point-to-point-net-device.cc @@ -29,6 +29,7 @@ #include "ns3/pointer.h" #include "point-to-point-net-device.h" #include "point-to-point-channel.h" +#include "ppp-header.h" NS_LOG_COMPONENT_DEFINE ("PointToPointNetDevice"); @@ -96,20 +97,19 @@ void PointToPointNetDevice::AddHeader(Ptr p, uint16_t protocolNumber) { NS_LOG_FUNCTION_NOARGS (); - LlcSnapHeader llc; - llc.SetType (protocolNumber); - p->AddHeader (llc); + NS_ASSERT_MSG (protocolNumber == 0x800, + "PointToPointNetDevice::AddHeader(): protocolNumber must be 0x800"); + PppHeader ppp; + p->AddHeader (ppp); } bool PointToPointNetDevice::ProcessHeader(Ptr p, uint16_t& param) { NS_LOG_FUNCTION_NOARGS (); - LlcSnapHeader llc; - p->RemoveHeader (llc); - - param = llc.GetType (); - + PppHeader ppp; + p->RemoveHeader (ppp); + param = 0x800; return true; } diff --git a/src/devices/point-to-point/ppp-header.cc b/src/devices/point-to-point/ppp-header.cc new file mode 100644 index 000000000..909e9f1b9 --- /dev/null +++ b/src/devices/point-to-point/ppp-header.cc @@ -0,0 +1,82 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2008 University of Washington + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include "ns3/assert.h" +#include "ns3/log.h" +#include "ns3/header.h" +#include "ppp-header.h" + +NS_LOG_COMPONENT_DEFINE ("PppHeader"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (PppHeader); + +PppHeader::PppHeader () +{ +} + +PppHeader::~PppHeader () +{ +} + + TypeId +PppHeader::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::PppHeader") + .SetParent
() + .AddConstructor () + ; + return tid; +} + + TypeId +PppHeader::GetInstanceTypeId (void) const +{ + return GetTypeId (); +} + +void +PppHeader::Print (std::ostream &os) const +{ + os << "Point-to-Point Protocol: IP (0x0021)" << std::endl; +} + + uint32_t +PppHeader::GetSerializedSize (void) const +{ + return 2; +} + + void +PppHeader::Serialize (Buffer::Iterator start) const +{ + start.WriteHtonU16 (0x0021); +} + + uint32_t +PppHeader::Deserialize (Buffer::Iterator start) +{ + uint16_t __attribute__((unused))data = start.ReadNtohU16 (); + NS_ASSERT_MSG (data == 0x0021, "MyHeader::Deserialize(): " + "expected protocol 0x0021"); + return 2; +} + +} // namespace ns3 diff --git a/src/devices/point-to-point/ppp-header.h b/src/devices/point-to-point/ppp-header.h new file mode 100644 index 000000000..74502e04e --- /dev/null +++ b/src/devices/point-to-point/ppp-header.h @@ -0,0 +1,72 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2008 University of Washington + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PPP_HEADER_H +#define PPP_HEADER_H + +#include "ns3/header.h" + +namespace ns3 { + +/** + * \brief Packet header for PPP + * + * This class can be used to add a header to PPP packet. Currently we do not + * implement any of the state machine in RFC 1661, we just encapsulate the + * inbound packet as an IP version 4 type and send it on. The goal here is + * not really to implement the point-to-point protocol, but to encapsulate our + * packets in a known protocol so packet sniffers can parse them. + * + * if PPP is transmitted over a serial link, it will typically be framed in + * some way derivative of IBM SDLC (HDLC) with all that that entails. + * Thankfully, we don't have to deal with all of that -- we can use our own + * protocol for getting bits across the serial link which we call an ns3 + * Packet. What we do have to worry about is being able to capture PPP frames + * which are understandable by Wireshark. All this means is that we need to + * teach the PcapWriter about the appropriate data link type (DLT_PPP = 9), + * and we need to add a PPP header to each packet. Since we are not using + * framed PPP, this just means prepending the sixteen bit PPP protocol number + * (0x0021) to the packet. The ns-3 way to do this is via a class that + * inherits from class Header. + */ +class PppHeader : public Header +{ +public: + + /** + * \brief Construct an IP version 4 PPP header. + */ + PppHeader (); + + /** + * \brief Destroy an IP version 4 PPP header. + */ + virtual ~PppHeader (); + + static TypeId GetTypeId (void); + virtual TypeId GetInstanceTypeId (void) const; + virtual void Print (std::ostream &os) const; + virtual void Serialize (Buffer::Iterator start) const; + virtual uint32_t Deserialize (Buffer::Iterator start); + virtual uint32_t GetSerializedSize (void) const; +}; + +}; // namespace ns3 + + +#endif /* PPP_HEADER_H */ diff --git a/src/devices/point-to-point/wscript b/src/devices/point-to-point/wscript index d7ad3005b..e9861027d 100644 --- a/src/devices/point-to-point/wscript +++ b/src/devices/point-to-point/wscript @@ -7,6 +7,7 @@ def build(bld): 'point-to-point-net-device.cc', 'point-to-point-channel.cc', 'point-to-point-test.cc', + 'ppp-header.cc', ] headers = bld.create_obj('ns3header') headers.module = 'point-to-point' diff --git a/src/helper/point-to-point-helper.cc b/src/helper/point-to-point-helper.cc index 900ba13ad..7f1fbcd03 100644 --- a/src/helper/point-to-point-helper.cc +++ b/src/helper/point-to-point-helper.cc @@ -70,7 +70,7 @@ PointToPointHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t oss << filename << "-" << nodeid << "-" << deviceid << ".pcap"; Ptr pcap = Create (); pcap->Open (oss.str ()); - pcap->WriteEthernetHeader (); + pcap->WritePppHeader (); oss.str (""); oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/Rx"; Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PointToPointHelper::RxEvent, pcap));