From 073e685b0d8518336ca29d23d3d8e3e054f6672b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 26 Jul 2007 14:03:41 +0200 Subject: [PATCH 01/48] new address types --- src/node/address.cc | 113 +++++++++++++++++++++++++++++ src/node/address.h | 43 +++++++++++ src/node/eui48-address.cc | 83 +++++++++++++++++++++ src/node/eui48-address.h | 25 +++++++ src/node/ipv4-address.cc | 41 ++++++++++- src/node/ipv4-address.h | 13 ++++ src/node/ipv4-transport-address.cc | 67 +++++++++++++++++ src/node/ipv4-transport-address.h | 33 +++++++++ src/node/wscript | 6 ++ 9 files changed, 422 insertions(+), 2 deletions(-) create mode 100644 src/node/address.cc create mode 100644 src/node/address.h create mode 100644 src/node/eui48-address.cc create mode 100644 src/node/eui48-address.h create mode 100644 src/node/ipv4-transport-address.cc create mode 100644 src/node/ipv4-transport-address.h diff --git a/src/node/address.cc b/src/node/address.cc new file mode 100644 index 000000000..a09129019 --- /dev/null +++ b/src/node/address.cc @@ -0,0 +1,113 @@ +#include "ns3/assert.h" +#include "address.h" +#include +#include + +namespace ns3 { + +Address::Address (uint8_t type, const uint8_t *buffer, uint8_t len) + : m_type (type), + m_len (len) +{ + NS_ASSERT (m_len <= MAX_SIZE); + memset (m_data, 0, m_len); + memcpy (m_data, buffer, m_len); +} +Address::Address (const Address & address) + : m_type (address.m_type), + m_len (address.m_len) +{ + NS_ASSERT (m_len <= MAX_SIZE); + memset (m_data, 0, m_len); + memcpy (m_data, address.m_data, m_len); +} +Address & +Address::operator = (const Address &address) +{ + NS_ASSERT (m_len <= MAX_SIZE); + m_type = address.m_type; + m_len = address.m_len; + NS_ASSERT (m_len <= MAX_SIZE); + memset (m_data, 0, m_len); + memcpy (m_data, address.m_data, m_len); + return *this; +} + +uint8_t +Address::GetLength (void) const +{ + NS_ASSERT (m_len <= MAX_SIZE); + return m_len; +} +void +Address::CopyTo (uint8_t *buffer) const +{ + NS_ASSERT (m_len <= MAX_SIZE); + memcpy (buffer, m_data, m_len); +} +uint8_t +Address::GetType (void) const +{ + NS_ASSERT (m_len <= MAX_SIZE); + return m_type; +} + +uint8_t +Address::Register (void) +{ + static uint8_t type = 1; + type++; + return type; +} + +bool operator == (const Address &a, const Address &b) +{ + NS_ASSERT (a.GetType () == b.GetType ()); + NS_ASSERT (a.GetLength() == b.GetLength()); + return memcmp (a.m_data, b.m_data, a.m_len) == 0; +} +bool operator != (const Address &a, const Address &b) +{ + return !(a == b); +} +bool operator < (const Address &a, const Address &b) +{ + NS_ASSERT (a.GetType () == b.GetType ()); + NS_ASSERT (a.GetLength() == b.GetLength()); + for (uint8_t i = 0; i < a.GetLength(); i++) + { + if (a.m_data[i] < b.m_data[i]) + { + return true; + } + else if (a.m_data[i] > b.m_data[i]) + { + return false; + } + } + return false; +} + +std::ostream& operator<< (std::ostream& os, const Address & address) +{ + if (address.m_len == 0) + { + os << "NULL-ADDRESS"; + return os; + } + os.setf (std::ios::hex, std::ios::basefield); + std::cout.fill('0'); + for (uint8_t i=0; i < (address.m_len-1); i++) + { + os << std::setw(2) << (uint32_t)address.m_data[i] << ":"; + } + // Final byte not suffixed by ":" + os << std::setw(2) << (uint32_t)address.m_data[address.m_len-1]; + os.setf (std::ios::dec, std::ios::basefield); + std::cout.fill(' '); + return os; +} + + + +} // namespace ns3 diff --git a/src/node/address.h b/src/node/address.h new file mode 100644 index 000000000..39551550d --- /dev/null +++ b/src/node/address.h @@ -0,0 +1,43 @@ +#ifndef ADDRESS_H +#define ADDRESS_H + +#include +#include + +namespace ns3 { + +class Address +{ +public: + Address (uint8_t type, const uint8_t *buffer, uint8_t len); + Address (const Address & address); + Address &operator = (const Address &address); + + uint8_t GetLength (void) const; + void CopyTo (uint8_t *buffer) const; + uint8_t GetType (void) const; + + static uint8_t Register (void); +private: + friend bool operator == (const Address &a, const Address &b); + friend bool operator < (const Address &a, const Address &b); + friend std::ostream& operator<< (std::ostream& os, const Address & address); + + Address (); + enum { + MAX_SIZE = 14 + }; + uint8_t m_type; + uint8_t m_len; + uint8_t m_data[MAX_SIZE]; +}; + +bool operator == (const Address &a, const Address &b); +bool operator != (const Address &a, const Address &b); +bool operator < (const Address &a, const Address &b); +std::ostream& operator<< (std::ostream& os, const Address & address); + +} // namespace ns3 + + +#endif /* ADDRESS_H */ diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc new file mode 100644 index 000000000..6f9e86879 --- /dev/null +++ b/src/node/eui48-address.cc @@ -0,0 +1,83 @@ +#include "eui48-address.h" +#include "address.h" +#include "ns3/assert.h" + +namespace ns3 { + +#define ASCII_a (0x41) +#define ASCII_z (0x5a) +#define ASCII_A (0x61) +#define ASCII_Z (0x7a) +#define ASCII_COLON (0x3a) +#define ASCII_ZERO (0x30) + +static char +AsciiToLowCase (char c) +{ + if (c >= ASCII_a && c <= ASCII_z) { + return c; + } else if (c >= ASCII_A && c <= ASCII_Z) { + return c + (ASCII_a - ASCII_A); + } else { + return c; + } +} + + +Eui48Address::Eui48Address () +{ + memset (m_address, 0, 6); +} +Eui48Address::Eui48Address (const char *str) +{ + int i = 0; + while (*str != 0 && i < 6) + { + uint8_t byte = 0; + while (*str != ASCII_COLON && *str != 0) + { + byte <<= 4; + char low = AsciiToLowCase (*str); + if (low >= ASCII_a) + { + byte |= low - ASCII_a + 10; + } + else + { + byte |= low - ASCII_ZERO; + } + str++; + } + m_address[i] = byte; + i++; + if (*str == 0) + { + break; + } + str++; + } + NS_ASSERT (i == 6); +} +Address +Eui48Address::ConvertTo (void) const +{ + return Address (GetType (), m_address, 6); +} +Eui48Address +Eui48Address::ConvertFrom (const Address &address) +{ + NS_ASSERT (address.GetType () == GetType ()); + NS_ASSERT (address.GetLength () == 6); + Eui48Address retval; + address.CopyTo (retval.m_address); + return retval; +} +uint8_t +Eui48Address::GetType (void) +{ + static uint8_t type = Address::Register (); + return type; +} + + +} // namespace ns3 diff --git a/src/node/eui48-address.h b/src/node/eui48-address.h new file mode 100644 index 000000000..5f57971de --- /dev/null +++ b/src/node/eui48-address.h @@ -0,0 +1,25 @@ +#ifndef EUI48_ADDRESS_H +#define EUI48_ADDRESS_H + +#include + +namespace ns3 { + +class Address; + +class Eui48Address +{ +public: + Eui48Address (); + Eui48Address (const char *str); + Address ConvertTo (void) const; + static Eui48Address ConvertFrom (const Address &address); +private: + static uint8_t GetType (void); + uint8_t m_address[6]; + +}; + +} // namespace ns3 + +#endif /* EUI48_ADDRESS_H */ diff --git a/src/node/ipv4-address.cc b/src/node/ipv4-address.cc index f8ff9f7a6..70b0ff486 100644 --- a/src/node/ipv4-address.cc +++ b/src/node/ipv4-address.cc @@ -19,11 +19,11 @@ * Author: Mathieu Lacage */ #include "ns3/debug.h" +#include "ipv4-address.h" +#include "ns3/assert.h" NS_DEBUG_COMPONENT_DEFINE("Ipv4Address"); -#include "ipv4-address.h" - namespace ns3 { @@ -170,6 +170,20 @@ Ipv4Address::Serialize (uint8_t buf[4]) const buf[2] = (m_address >> 8) & 0xff; buf[3] = (m_address >> 0) & 0xff; } +Ipv4Address +Ipv4Address::Deserialize (const uint8_t buf[4]) +{ + Ipv4Address ipv4; + ipv4.m_address = 0; + ipv4.m_address |= buf[0]; + ipv4.m_address <<= 8; + ipv4.m_address |= buf[1]; + ipv4.m_address <<= 8; + ipv4.m_address |= buf[2]; + ipv4.m_address <<= 8; + ipv4.m_address |= buf[3]; + return ipv4; +} void Ipv4Address::Print (std::ostream &os) const @@ -180,7 +194,30 @@ Ipv4Address::Print (std::ostream &os) const << ((m_address >> 0) & 0xff); } +Address +Ipv4Address::ConvertTo (void) const +{ + uint8_t buf[4]; + Serialize (buf); + return Address (GetType (), buf, 4); +} +Ipv4Address +Ipv4Address::ConvertFrom (const Address &address) +{ + NS_ASSERT (address.GetType () == GetType ()); + NS_ASSERT (address.GetLength () == 4); + uint8_t buf[4]; + address.CopyTo (buf); + return Deserialize (buf); +} + +uint8_t +Ipv4Address::GetType (void) +{ + static uint8_t type = Address::Register (); + return type; +} Ipv4Address Ipv4Address::GetZero (void) diff --git a/src/node/ipv4-address.h b/src/node/ipv4-address.h index dc834052a..2012d2f78 100644 --- a/src/node/ipv4-address.h +++ b/src/node/ipv4-address.h @@ -24,6 +24,7 @@ #include #include +#include "address.h" namespace ns3 { @@ -68,10 +69,18 @@ public: void SetHostOrder (uint32_t ip); /** * Serialize this address to a 4-byte buffer + * * \param buf output buffer to which this address gets overwritten with this * Ipv4Address */ void Serialize (uint8_t buf[4]) const; + /** + * \param buf buffer to read address from + * \returns an Ipv4Address + * + * The input address is expected to be in network byte order format. + */ + static Ipv4Address Deserialize (const uint8_t buf[4]); /** * \brief Print this address to the given output stream * @@ -83,11 +92,15 @@ public: bool IsBroadcast (void); bool IsMulticast (void); + Address ConvertTo (void) const; + static Ipv4Address ConvertFrom (const Address &address); + static Ipv4Address GetZero (void); static Ipv4Address GetAny (void); static Ipv4Address GetBroadcast (void); static Ipv4Address GetLoopback (void); private: + static uint8_t GetType (void); uint32_t m_address; }; diff --git a/src/node/ipv4-transport-address.cc b/src/node/ipv4-transport-address.cc new file mode 100644 index 000000000..b088a1017 --- /dev/null +++ b/src/node/ipv4-transport-address.cc @@ -0,0 +1,67 @@ +#include "ipv4-transport-address.h" +#include "ns3/assert.h" + +namespace ns3 { + +Ipv4TransportAddress::Ipv4TransportAddress (Ipv4Address ipv4, uint16_t port) + : m_ipv4 (ipv4), + m_port (port) +{} +Ipv4TransportAddress::Ipv4TransportAddress (Ipv4Address ipv4) + : m_ipv4 (ipv4), + m_port (0) +{} +Ipv4TransportAddress::Ipv4TransportAddress (uint16_t port) + : m_ipv4 (Ipv4Address::GetAny ()), + m_port (port) +{} +uint16_t +Ipv4TransportAddress::GetPort (void) const +{ + return m_port; +} +Ipv4Address +Ipv4TransportAddress::GetIpv4 (void) const +{ + return m_ipv4; +} + +void +Ipv4TransportAddress::SetPort (uint16_t port) +{ + m_port = port; +} +void +Ipv4TransportAddress::SetIpv4 (Ipv4Address address) +{ + m_ipv4 = address; +} + +Address +Ipv4TransportAddress::ConvertTo (void) const +{ + uint8_t buf[6]; + m_ipv4.Serialize (buf); + buf[4] = m_port & 0xff; + buf[5] = (m_port >> 8) & 0xff; + return Address (GetType (), buf, 6); +} +Ipv4TransportAddress +Ipv4TransportAddress::ConvertFrom (const Address &address) +{ + NS_ASSERT (address.GetType () == GetType ()); + NS_ASSERT (address.GetLength () == 6); + uint8_t buf[6]; + address.CopyTo (buf); + Ipv4Address ipv4 = Ipv4Address::Deserialize (buf); + uint16_t port = buf[0] | (buf[1] << 8); + return Ipv4TransportAddress (ipv4, port); +} +uint8_t +Ipv4TransportAddress::GetType (void) +{ + static uint8_t type = Address::Register (); + return type; +} + +} // namespace ns3 diff --git a/src/node/ipv4-transport-address.h b/src/node/ipv4-transport-address.h new file mode 100644 index 000000000..e73ca8d3d --- /dev/null +++ b/src/node/ipv4-transport-address.h @@ -0,0 +1,33 @@ +#ifndef IPV4_TRANSPORT_ADDRESS_H +#define IPV4_TRANSPORT_ADDRESS_H + +#include "address.h" +#include "ipv4-address.h" +#include + +namespace ns3 { + +class Ipv4TransportAddress +{ +public: + Ipv4TransportAddress (Ipv4Address ipv4, uint16_t port); + Ipv4TransportAddress (Ipv4Address ipv4); + Ipv4TransportAddress (uint16_t post); + uint16_t GetPort (void) const; + Ipv4Address GetIpv4 (void) const; + + void SetPort (uint16_t post); + void SetIpv4 (Ipv4Address address); + + Address ConvertTo (void) const; + static Ipv4TransportAddress ConvertFrom (const Address &address); +private: + static uint8_t GetType (void); + Ipv4Address m_ipv4; + uint16_t m_port; +}; + +} // namespace ns3 + + +#endif /* IPV4_TRANSPORT_ADDRESS_H */ diff --git a/src/node/wscript b/src/node/wscript index 0a5c64aa9..e11cd8eac 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -6,6 +6,9 @@ def build(bld): node.target = node.name node.uselib_local = ['ns3-core', 'ns3-common', 'ns3-simulator'] node.source = [ + 'address.cc', + 'eui48-address.cc', + 'ipv4-transport-address.cc', 'node.cc', 'ipv4-address.cc', 'net-device.cc', @@ -26,6 +29,9 @@ def build(bld): headers = bld.create_obj('ns3header') headers.source = [ + 'address.h', + 'eui48-address.h', + 'ipv4-transport-address.h', 'node.h', 'ipv4-address.h', 'net-device.h', From 8a5f927d8dfbb69edd666800b8c297a0f7bce5dd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 26 Jul 2007 14:47:05 +0200 Subject: [PATCH 02/48] improve type checking --- src/node/address.cc | 28 ++++++++++++++++++++++------ src/node/address.h | 13 ++++++++----- src/node/eui48-address.cc | 3 +-- src/node/ipv4-address.cc | 3 +-- src/node/ipv4-transport-address.cc | 3 +-- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/node/address.cc b/src/node/address.cc index a09129019..408a152e6 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -5,6 +5,13 @@ namespace ns3 { +Address::Address () + : m_type (0), + m_len (0) +{ + memset (m_data, 0, m_len); +} + Address::Address (uint8_t type, const uint8_t *buffer, uint8_t len) : m_type (type), m_len (len) @@ -45,11 +52,16 @@ Address::CopyTo (uint8_t *buffer) const NS_ASSERT (m_len <= MAX_SIZE); memcpy (buffer, m_data, m_len); } -uint8_t -Address::GetType (void) const +void +Address::CopyFrom (uint8_t *buffer, uint8_t len) { - NS_ASSERT (m_len <= MAX_SIZE); - return m_type; + NS_ASSERT (len == m_len); + memcpy (m_data, buffer, len); +} +bool +Address::CheckCompatible (uint8_t type, uint8_t len) const +{ + return m_len == len && (m_type == type || m_type == 0); } uint8_t @@ -62,7 +74,9 @@ Address::Register (void) bool operator == (const Address &a, const Address &b) { - NS_ASSERT (a.GetType () == b.GetType ()); + NS_ASSERT (a.m_type == b.m_type || + a.m_type == 0 || + b.m_type == 0); NS_ASSERT (a.GetLength() == b.GetLength()); return memcmp (a.m_data, b.m_data, a.m_len) == 0; } @@ -72,7 +86,9 @@ bool operator != (const Address &a, const Address &b) } bool operator < (const Address &a, const Address &b) { - NS_ASSERT (a.GetType () == b.GetType ()); + NS_ASSERT (a.m_type == b.m_type || + a.m_type == 0 || + b.m_type == 0); NS_ASSERT (a.GetLength() == b.GetLength()); for (uint8_t i = 0; i < a.GetLength(); i++) { diff --git a/src/node/address.h b/src/node/address.h index 39551550d..417bdce0a 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -9,13 +9,20 @@ namespace ns3 { class Address { public: + enum { + MAX_SIZE = 14 + }; + + Address (); Address (uint8_t type, const uint8_t *buffer, uint8_t len); Address (const Address & address); Address &operator = (const Address &address); uint8_t GetLength (void) const; void CopyTo (uint8_t *buffer) const; - uint8_t GetType (void) const; + void CopyFrom (uint8_t *buffer, uint8_t len); + bool CheckCompatible (uint8_t type, uint8_t len) const; + static uint8_t Register (void); private: @@ -23,10 +30,6 @@ private: friend bool operator < (const Address &a, const Address &b); friend std::ostream& operator<< (std::ostream& os, const Address & address); - Address (); - enum { - MAX_SIZE = 14 - }; uint8_t m_type; uint8_t m_len; uint8_t m_data[MAX_SIZE]; diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index 6f9e86879..622ec3142 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -66,8 +66,7 @@ Eui48Address::ConvertTo (void) const Eui48Address Eui48Address::ConvertFrom (const Address &address) { - NS_ASSERT (address.GetType () == GetType ()); - NS_ASSERT (address.GetLength () == 6); + NS_ASSERT (address.CheckCompatible (GetType (), 6)); Eui48Address retval; address.CopyTo (retval.m_address); return retval; diff --git a/src/node/ipv4-address.cc b/src/node/ipv4-address.cc index 70b0ff486..5c923f06f 100644 --- a/src/node/ipv4-address.cc +++ b/src/node/ipv4-address.cc @@ -205,8 +205,7 @@ Ipv4Address::ConvertTo (void) const Ipv4Address Ipv4Address::ConvertFrom (const Address &address) { - NS_ASSERT (address.GetType () == GetType ()); - NS_ASSERT (address.GetLength () == 4); + NS_ASSERT (address.CheckCompatible (GetType (), 4)); uint8_t buf[4]; address.CopyTo (buf); return Deserialize (buf); diff --git a/src/node/ipv4-transport-address.cc b/src/node/ipv4-transport-address.cc index b088a1017..9330737c8 100644 --- a/src/node/ipv4-transport-address.cc +++ b/src/node/ipv4-transport-address.cc @@ -49,8 +49,7 @@ Ipv4TransportAddress::ConvertTo (void) const Ipv4TransportAddress Ipv4TransportAddress::ConvertFrom (const Address &address) { - NS_ASSERT (address.GetType () == GetType ()); - NS_ASSERT (address.GetLength () == 6); + NS_ASSERT (address.CheckCompatible (GetType (), 6)); uint8_t buf[6]; address.CopyTo (buf); Ipv4Address ipv4 = Ipv4Address::Deserialize (buf); From db6a90d956737d0dc4096d271084a0ffdf0c5876 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 26 Jul 2007 17:36:53 +0200 Subject: [PATCH 03/48] replace MacAddress by Address --- examples/simple-p2p.cc | 1 - src/devices/p2p/p2p-net-device.cc | 11 +- src/devices/p2p/p2p-net-device.h | 6 +- src/internet-node/arp-cache.cc | 4 +- src/internet-node/arp-cache.h | 8 +- src/internet-node/arp-header.cc | 12 +- src/internet-node/arp-header.h | 18 +- src/internet-node/arp-ipv4-interface.cc | 3 +- src/internet-node/arp-l3-protocol.cc | 10 +- src/internet-node/arp-l3-protocol.h | 6 +- src/internet-node/arp-private.cc | 2 +- src/internet-node/arp-private.h | 4 +- src/internet-node/header-utils.cc | 12 +- src/internet-node/header-utils.h | 6 +- src/node/eui48-address.cc | 14 ++ src/node/eui48-address.h | 1 + src/node/mac-address.cc | 210 ------------------------ src/node/mac-address.h | 128 --------------- src/node/net-device.cc | 10 +- src/node/net-device.h | 22 +-- src/node/wscript | 2 - 21 files changed, 83 insertions(+), 407 deletions(-) delete mode 100644 src/node/mac-address.cc delete mode 100644 src/node/mac-address.h diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index 9ad235b5e..ac98440c8 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -56,7 +56,6 @@ #include "ns3/internet-node.h" #include "ns3/p2p-channel.h" #include "ns3/p2p-net-device.h" -#include "ns3/mac-address.h" #include "ns3/ipv4-address.h" #include "ns3/ipv4.h" #include "ns3/socket.h" diff --git a/src/devices/p2p/p2p-net-device.cc b/src/devices/p2p/p2p-net-device.cc index 13d352889..3e0596d48 100644 --- a/src/devices/p2p/p2p-net-device.cc +++ b/src/devices/p2p/p2p-net-device.cc @@ -26,6 +26,7 @@ #include "ns3/queue.h" #include "ns3/simulator.h" #include "ns3/composite-trace-resolver.h" +#include "ns3/eui48-address.h" #include "p2p-net-device.h" #include "p2p-channel.h" @@ -38,10 +39,10 @@ DataRateDefaultValue PointToPointNetDevice::g_defaultRate( "The default data rate for point to point links", DataRate ("10Mb/s")); - PointToPointNetDevice::PointToPointNetDevice (Ptr node, - const DataRate& rate) +PointToPointNetDevice::PointToPointNetDevice (Ptr node, + const DataRate& rate) : - NetDevice(node, MacAddress (6)), + NetDevice(node, Eui48Address::Allocate ().ConvertTo ()), m_txMachineState (READY), m_bps (rate), m_tInterframeGap (Seconds(0)), @@ -54,7 +55,7 @@ DataRateDefaultValue PointToPointNetDevice::g_defaultRate( // BUGBUG FIXME // // You _must_ support broadcast to get any sort of packet from the ARP layer. - EnableBroadcast (MacAddress ("ff:ff:ff:ff:ff:ff")); + EnableBroadcast (Eui48Address ("ff:ff:ff:ff:ff:ff").ConvertTo ()); EnableMulticast(); EnablePointToPoint(); } @@ -82,7 +83,7 @@ void PointToPointNetDevice::SetInterframeGap(const Time& t) m_tInterframeGap = t; } -bool PointToPointNetDevice::SendTo (Packet& p, const MacAddress& dest) +bool PointToPointNetDevice::SendTo (Packet& p, const Address& dest) { NS_DEBUG ("PointToPointNetDevice::SendTo (" << &p << ", " << &dest << ")"); NS_DEBUG ("PointToPointNetDevice::SendTo (): UID is " << p.GetUid () << ")"); diff --git a/src/devices/p2p/p2p-net-device.h b/src/devices/p2p/p2p-net-device.h index a4bc3be00..474a6383c 100644 --- a/src/devices/p2p/p2p-net-device.h +++ b/src/devices/p2p/p2p-net-device.h @@ -22,7 +22,7 @@ #define POINT_TO_POINT_NET_DEVICE_H #include -#include "ns3/mac-address.h" +#include "ns3/address.h" #include "ns3/node.h" #include "ns3/net-device.h" #include "ns3/callback.h" @@ -201,10 +201,10 @@ private: * * @see NetDevice * @param p a reference to the packet to send - * @param dest a reference to the MacAddress of the destination device + * @param dest a reference to the Address of the destination device * @returns true if success, false on failure */ - virtual bool SendTo (Packet& p, const MacAddress& dest); + virtual bool SendTo (Packet& p, const Address& dest); /** * Start Sending a Packet Down the Wire. * diff --git a/src/internet-node/arp-cache.cc b/src/internet-node/arp-cache.cc index e9f48561d..4b166c99b 100644 --- a/src/internet-node/arp-cache.cc +++ b/src/internet-node/arp-cache.cc @@ -146,7 +146,7 @@ ArpCache::Entry::MarkDead (void) UpdateSeen (); } Packet -ArpCache::Entry::MarkAlive (MacAddress macAddress) +ArpCache::Entry::MarkAlive (Address macAddress) { NS_ASSERT (m_state == WAIT_REPLY); //NS_ASSERT (m_waiting != 0); @@ -180,7 +180,7 @@ ArpCache::Entry::MarkWaitReply (Packet waiting) UpdateSeen (); } -MacAddress +Address ArpCache::Entry::GetMacAddress (void) { NS_ASSERT (m_state == ALIVE); diff --git a/src/internet-node/arp-cache.h b/src/internet-node/arp-cache.h index 436d44eba..2cd88c85f 100644 --- a/src/internet-node/arp-cache.h +++ b/src/internet-node/arp-cache.h @@ -26,7 +26,7 @@ #include "ns3/nstime.h" #include "ns3/net-device.h" #include "ns3/ipv4-address.h" -#include "ns3/mac-address.h" +#include "ns3/address.h" #include "ns3/ptr.h" #include "sgi-hashmap.h" @@ -101,7 +101,7 @@ public: * \param macAddress * \return */ - Packet MarkAlive (MacAddress macAddress); + Packet MarkAlive (Address macAddress); /** * \param waiting */ @@ -127,7 +127,7 @@ public: /** * \return The MacAddress of this entry */ - MacAddress GetMacAddress (void); + Address GetMacAddress (void); /** * \return True if this entry has timedout; false otherwise. */ @@ -143,7 +143,7 @@ public: ArpCache *m_arp; ArpCacheEntryState_e m_state; Time m_lastSeen; - MacAddress m_macAddress; + Address m_macAddress; Packet m_waiting; }; diff --git a/src/internet-node/arp-header.cc b/src/internet-node/arp-header.cc index e340404f4..dc8cd3f7a 100644 --- a/src/internet-node/arp-header.cc +++ b/src/internet-node/arp-header.cc @@ -29,9 +29,9 @@ ArpHeader::~ArpHeader () {} void -ArpHeader::SetRequest (MacAddress sourceHardwareAddress, +ArpHeader::SetRequest (Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, - MacAddress destinationHardwareAddress, + Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress) { m_type = ARP_TYPE_REQUEST; @@ -41,9 +41,9 @@ ArpHeader::SetRequest (MacAddress sourceHardwareAddress, m_ipv4Dest = destinationProtocolAddress; } void -ArpHeader::SetReply (MacAddress sourceHardwareAddress, +ArpHeader::SetReply (Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, - MacAddress destinationHardwareAddress, + Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress) { m_type = ARP_TYPE_REPLY; @@ -62,12 +62,12 @@ ArpHeader::IsReply (void) const { return (m_type == ARP_TYPE_REPLY)?true:false; } -MacAddress +Address ArpHeader::GetSourceHardwareAddress (void) { return m_macSource; } -MacAddress +Address ArpHeader::GetDestinationHardwareAddress (void) { return m_macDest; diff --git a/src/internet-node/arp-header.h b/src/internet-node/arp-header.h index 9f85579f5..5affcf84f 100644 --- a/src/internet-node/arp-header.h +++ b/src/internet-node/arp-header.h @@ -23,7 +23,7 @@ #define ARP_HEADER_H #include "ns3/header.h" -#include "ns3/mac-address.h" +#include "ns3/address.h" #include "ns3/ipv4-address.h" namespace ns3 { @@ -34,18 +34,18 @@ class ArpHeader : public Header { public: virtual ~ArpHeader (); - void SetRequest (MacAddress sourceHardwareAddress, + void SetRequest (Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, - MacAddress destinationHardwareAddress, + Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress); - void SetReply (MacAddress sourceHardwareAddress, + void SetReply (Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, - MacAddress destinationHardwareAddress, + Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress); bool IsRequest (void) const; bool IsReply (void) const; - MacAddress GetSourceHardwareAddress (void); - MacAddress GetDestinationHardwareAddress (void); + Address GetSourceHardwareAddress (void); + Address GetDestinationHardwareAddress (void); Ipv4Address GetSourceIpv4Address (void); Ipv4Address GetDestinationIpv4Address (void); @@ -74,8 +74,8 @@ private: ARP_TYPE_REPLY = 2 }; uint16_t m_type; - MacAddress m_macSource; - MacAddress m_macDest; + Address m_macSource; + Address m_macDest; Ipv4Address m_ipv4Source; Ipv4Address m_ipv4Dest; }; diff --git a/src/internet-node/arp-ipv4-interface.cc b/src/internet-node/arp-ipv4-interface.cc index 42d6af238..9319929c2 100644 --- a/src/internet-node/arp-ipv4-interface.cc +++ b/src/internet-node/arp-ipv4-interface.cc @@ -24,6 +24,7 @@ #include "ns3/composite-trace-resolver.h" #include "ns3/node.h" #include "ns3/net-device.h" +#include "ns3/address.h" #include "arp-ipv4-interface.h" #include "arp-private.h" @@ -59,7 +60,7 @@ ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest) if (GetDevice ()->NeedsArp ()) { Ptr arp = m_node->QueryInterface (ArpPrivate::iid); - MacAddress hardwareDestination; + Address hardwareDestination; bool found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination); if (found) { diff --git a/src/internet-node/arp-l3-protocol.cc b/src/internet-node/arp-l3-protocol.cc index f04a6aa25..583b4e574 100644 --- a/src/internet-node/arp-l3-protocol.cc +++ b/src/internet-node/arp-l3-protocol.cc @@ -97,7 +97,7 @@ ArpL3Protocol::Receive(Packet& packet, Ptr device) } else if (arp.IsReply () && arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress ()) && - arp.GetDestinationHardwareAddress ().IsEqual (device->GetAddress ())) + arp.GetDestinationHardwareAddress () == device->GetAddress ()) { Ipv4Address from = arp.GetSourceIpv4Address (); ArpCache::Entry *entry = cache->Lookup (from); @@ -108,7 +108,7 @@ ArpL3Protocol::Receive(Packet& packet, Ptr device) NS_DEBUG ("node="<GetId ()<<", got reply from " << arp.GetSourceIpv4Address () << " for waiting entry -- flush"); - MacAddress from_mac = arp.GetSourceHardwareAddress (); + Address from_mac = arp.GetSourceHardwareAddress (); Packet waiting = entry->MarkAlive (from_mac); cache->GetInterface ()->Send (waiting, arp.GetSourceIpv4Address ()); } @@ -131,8 +131,8 @@ ArpL3Protocol::Receive(Packet& packet, Ptr device) } bool ArpL3Protocol::Lookup (Packet &packet, Ipv4Address destination, - Ptr device, - MacAddress *hardwareDestination) + Ptr device, + Address *hardwareDestination) { ArpCache *cache = FindCache (device); ArpCache::Entry *entry = cache->Lookup (destination); @@ -213,7 +213,7 @@ ArpL3Protocol::SendArpRequest (ArpCache const *cache, Ipv4Address to) } void -ArpL3Protocol::SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac) +ArpL3Protocol::SendArpReply (ArpCache const *cache, Ipv4Address toIp, Address toMac) { ArpHeader arp; arp.SetReply (cache->GetDevice ()->GetAddress (), diff --git a/src/internet-node/arp-l3-protocol.h b/src/internet-node/arp-l3-protocol.h index d27d2dba2..082bbebb5 100644 --- a/src/internet-node/arp-l3-protocol.h +++ b/src/internet-node/arp-l3-protocol.h @@ -23,7 +23,7 @@ #include #include "ns3/ipv4-address.h" -#include "ns3/mac-address.h" +#include "ns3/address.h" #include "ns3/ptr.h" #include "l3-protocol.h" @@ -64,14 +64,14 @@ public: */ bool Lookup (Packet &p, Ipv4Address destination, Ptr device, - MacAddress *hardwareDestination); + Address *hardwareDestination); protected: virtual void DoDispose (void); private: typedef std::list CacheList; ArpCache *FindCache (Ptr device); void SendArpRequest (ArpCache const *cache, Ipv4Address to); - void SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac); + void SendArpReply (ArpCache const *cache, Ipv4Address toIp, Address toMac); CacheList m_cacheList; Ptr m_node; }; diff --git a/src/internet-node/arp-private.cc b/src/internet-node/arp-private.cc index 10a86b3e3..4f60dac99 100644 --- a/src/internet-node/arp-private.cc +++ b/src/internet-node/arp-private.cc @@ -40,7 +40,7 @@ ArpPrivate::~ArpPrivate () bool ArpPrivate::Lookup (Packet &p, Ipv4Address destination, Ptr device, - MacAddress *hardwareDestination) + Address *hardwareDestination) { return m_arp->Lookup (p, destination, device, hardwareDestination); } diff --git a/src/internet-node/arp-private.h b/src/internet-node/arp-private.h index 6e238ade0..af0b8b7a3 100644 --- a/src/internet-node/arp-private.h +++ b/src/internet-node/arp-private.h @@ -27,7 +27,7 @@ namespace ns3 { class NetDevice; -class MacAddress; +class Address; class Packet; class ArpL3Protocol; @@ -39,7 +39,7 @@ public: virtual ~ArpPrivate (); bool Lookup (Packet &p, Ipv4Address destination, Ptr device, - MacAddress *hardwareDestination); + Address *hardwareDestination); protected: virtual void DoDispose (void); private: diff --git a/src/internet-node/header-utils.cc b/src/internet-node/header-utils.cc index b3da88cfa..64bb29285 100644 --- a/src/internet-node/header-utils.cc +++ b/src/internet-node/header-utils.cc @@ -26,10 +26,10 @@ void WriteTo (Buffer::Iterator &i, Ipv4Address ad) { i.WriteHtonU32 (ad.GetHostOrder ()); } -void WriteTo (Buffer::Iterator &i, MacAddress ad) +void WriteTo (Buffer::Iterator &i, Address ad) { - uint8_t mac[MacAddress::MAX_LEN]; - ad.Peek (mac); + uint8_t mac[Address::MAX_SIZE]; + ad.CopyTo (mac); i.Write (mac, ad.GetLength ()); } @@ -37,11 +37,11 @@ void ReadFrom (Buffer::Iterator &i, Ipv4Address &ad) { ad.SetHostOrder (i.ReadNtohU32 ()); } -void ReadFrom (Buffer::Iterator &i, MacAddress &ad, uint32_t len) +void ReadFrom (Buffer::Iterator &i, Address &ad, uint32_t len) { - uint8_t mac[MacAddress::MAX_LEN]; + uint8_t mac[Address::MAX_SIZE]; i.Read (mac, len); - ad.Set (mac, len); + ad.CopyFrom (mac, len); } diff --git a/src/internet-node/header-utils.h b/src/internet-node/header-utils.h index e65e7533c..88d3dfe10 100644 --- a/src/internet-node/header-utils.h +++ b/src/internet-node/header-utils.h @@ -23,15 +23,15 @@ #include "ns3/buffer.h" #include "ns3/ipv4-address.h" -#include "ns3/mac-address.h" +#include "ns3/address.h" namespace ns3 { void WriteTo (Buffer::Iterator &i, Ipv4Address ad); -void WriteTo (Buffer::Iterator &i, MacAddress ad); +void WriteTo (Buffer::Iterator &i, Address ad); void ReadFrom (Buffer::Iterator &i, Ipv4Address &ad); -void ReadFrom (Buffer::Iterator &i, MacAddress &ad, uint32_t len); +void ReadFrom (Buffer::Iterator &i, Address &ad, uint32_t len); }; diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index 622ec3142..a3ed8fd4e 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -71,6 +71,20 @@ Eui48Address::ConvertFrom (const Address &address) address.CopyTo (retval.m_address); return retval; } +Eui48Address +Eui48Address::Allocate (void) +{ + static uint64_t id = 0; + id++; + Eui48Address address; + address.m_address[0] = (id >> 48) & 0xff; + address.m_address[1] = (id >> 32) & 0xff; + address.m_address[2] = (id >> 24) & 0xff; + address.m_address[3] = (id >> 16) & 0xff; + address.m_address[4] = (id >> 8) & 0xff; + address.m_address[5] = (id >> 0) & 0xff; + return address; +} uint8_t Eui48Address::GetType (void) { diff --git a/src/node/eui48-address.h b/src/node/eui48-address.h index 5f57971de..b051b4c31 100644 --- a/src/node/eui48-address.h +++ b/src/node/eui48-address.h @@ -14,6 +14,7 @@ public: Eui48Address (const char *str); Address ConvertTo (void) const; static Eui48Address ConvertFrom (const Address &address); + static Eui48Address Allocate (void); private: static uint8_t GetType (void); uint8_t m_address[6]; diff --git a/src/node/mac-address.cc b/src/node/mac-address.cc deleted file mode 100644 index 9dcf9bdd6..000000000 --- a/src/node/mac-address.cc +++ /dev/null @@ -1,210 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2005 INRIA - * All rights reserved. - * - * 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 - * - * Author: Mathieu Lacage - */ - -#include -#include -#include "ns3/assert.h" -#include "mac-address.h" - -#define ASCII_a (0x41) -#define ASCII_z (0x5a) -#define ASCII_A (0x61) -#define ASCII_Z (0x7a) -#define ASCII_COLON (0x3a) -#define ASCII_ZERO (0x30) - -namespace ns3 { - -// Static variables -uint8_t MacAddress::g_nextAddress[MacAddress::MAX_LEN]; - -static char -AsciiToLowCase (char c) -{ - if (c >= ASCII_a && c <= ASCII_z) { - return c; - } else if (c >= ASCII_A && c <= ASCII_Z) { - return c + (ASCII_a - ASCII_A); - } else { - return c; - } -} - - -MacAddress::MacAddress () : m_len(0) -{ - for (int i=0; i < MacAddress::MAX_LEN; i++) - { - m_address[i] = 0; - } -} - -MacAddress::MacAddress(uint8_t len) : m_len(len) -{ - NS_ASSERT (len <= MacAddress::MAX_LEN); - AdvanceAddress(); - memcpy(m_address, g_nextAddress, len); -} - -MacAddress::MacAddress (uint8_t const *address, uint8_t len) -{ - NS_ASSERT (len <= MacAddress::MAX_LEN); - for (int i=0; i < len; i++) - { - m_address[i] = address[i]; - } - for (int i=len; i < MacAddress::MAX_LEN; i++) - { - m_address[i] = 0; - } - m_len = len; -} - -MacAddress::MacAddress (char const *str) -{ - int i = 0; - while (*str != 0 && i < MacAddress::MAX_LEN) { - uint8_t byte = 0; - while (*str != ASCII_COLON && *str != 0) { - byte <<= 4; - char low = AsciiToLowCase (*str); - if (low >= ASCII_a) { - byte |= low - ASCII_a + 10; - } else { - byte |= low - ASCII_ZERO; - } - str++; - } - m_address[i] = byte; - i++; - if (*str == 0) { - break; - } - str++; - } - m_len = i; -} - -MacAddress::~MacAddress () -{} - -bool -MacAddress::IsEqual (MacAddress other) const -{ - if (memcmp(other.m_address, m_address, m_len)) - { - return false; - } - else - { - return true; - } -} - -void -MacAddress::Print (std::ostream &os) const -{ - int i; - if (m_len == 0) - { - os << "NULL-ADDRESS"; - return; - } - os.setf (std::ios::hex, std::ios::basefield); - std::cout.fill('0'); - for (i=0; i< (m_len-1); i++) - { - os << std::setw(2) << (uint32_t)m_address[i] << ":"; - } - // Final byte not suffixed by ":" - os << std::setw(2) << (uint32_t)m_address[i]; - os.setf (std::ios::dec, std::ios::basefield); - std::cout.fill(' '); -} - -uint8_t -MacAddress::GetLength () const -{ - return m_len; -} - -void -MacAddress::Peek (uint8_t ad[MacAddress::MAX_LEN]) const -{ - memcpy (ad, m_address, MacAddress::MAX_LEN); -} -void -MacAddress::Set (uint8_t const ad[MacAddress::MAX_LEN], uint8_t len) -{ - memcpy (m_address, ad, MacAddress::MAX_LEN); - m_len = len; -} - -// Static methods -void MacAddress::AdvanceAddress() - { - // Advance to next address, little end first - for(size_t i = 0; i < MAX_LEN; ++i) - { - if (++g_nextAddress[i] != 0) break; - } - } - -// Non-member operators -bool operator == (MacAddress const&a, MacAddress const&b) -{ - return a.IsEqual (b); -} - -bool operator != (MacAddress const&a, MacAddress const&b) -{ - return !a.IsEqual (b); -} - -bool operator < (MacAddress const&a, MacAddress const&b) -{ - uint8_t a_p[MacAddress::MAX_LEN]; - uint8_t b_p[MacAddress::MAX_LEN]; - a.Peek (a_p); - b.Peek (b_p); - NS_ASSERT (a.GetLength() == b.GetLength()); - for (uint8_t i = 0; i < a.GetLength(); i++) - { - if (a_p[i] < b_p[i]) - { - return true; - } - else if (a_p[i] > b_p[i]) - { - return false; - } - } - return false; -} - -std::ostream& operator<< (std::ostream& os, MacAddress const& address) -{ - address.Print (os); - return os; -} - - -}; // namespace ns3 diff --git a/src/node/mac-address.h b/src/node/mac-address.h deleted file mode 100644 index 76c5a6ae0..000000000 --- a/src/node/mac-address.h +++ /dev/null @@ -1,128 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2005 INRIA - * All rights reserved. - * - * 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 - * - * Author: Mathieu Lacage - */ - -#ifndef MAC_ADDRESS_H -#define MAC_ADDRESS_H - -#include -#include - -namespace ns3 { - -/** - * \brief base class for Network Device addresses - * - * This class is a base class for different types of Network - * Device addresses. It generically stores an address of - * MAX_ADDR_LEN bytes, and provides methods to compare, print, and set - * the address. - */ -class MacAddress { -public: - enum { - MAX_LEN = 32 - }; - /** - * \brief Construct a null MacAddress - * - * This MacAddress has length of zero, and is internally all zeros - */ - MacAddress (void); - /** - * \brief Construct a MacAddress using the next available - * address. - * \see MacAddres::Next - * \param len length, in bytes, of the desired address - */ - MacAddress(uint8_t len); - /** - * \brief Construct a MacAddress from a byte-array - * - * low byte should be first. - * \param address a byte array indicating the address - * \param len length, in bytes, of the address points to - */ - MacAddress (uint8_t const *address, uint8_t len); - /** - * \brief Construct a MacAddress from a C-string - * - * The string should look like this: - * hh:xx:xx:xx:xx:ll - * where hh is the high byte and ll is - * the low byte. - * \param address the C-string representation of the address - */ - MacAddress (char const *address); - ~MacAddress (); - - /** - * \brief Comparison operation between MacAddresses - * \param other The address against which to compare this one - * \return True if they are equal, false otherwise. - */ - bool IsEqual (MacAddress other) const; - /** - * \brief Print this MacAddress to a stream - * - * The format is colon seperated groups of two hexadecimal digits - * \param os The output stream desired - */ - void Print (std::ostream &os) const; - - /** - * \return The length in bytes of this MacAddress - */ - uint8_t GetLength() const; - /** - * \brief Copy routine to peek the contents of the MacAddress - * - * \param ad Output parameter which holds a copy of this MacAddress - */ - void Peek (uint8_t ad[MAX_LEN]) const; - /** - * \brief Sets this MacAddress to a specific value - * \param ad byte buffer to set the MacAddress to - * \param len the length of the buffer - */ - void Set (uint8_t const ad[MAX_LEN], uint8_t len); - - // Static methods/members - /** - * - * Advance the global to the next available mac address. - */ - static void AdvanceAddress(); - static uint8_t g_nextAddress[MAX_LEN]; - -private: - uint8_t m_address[MAX_LEN]; - uint8_t m_len; -}; - -bool operator == (MacAddress const&a, MacAddress const&b); -bool operator != (MacAddress const&a, MacAddress const&b); -bool operator < (MacAddress const&a, MacAddress const&b); - -std::ostream& operator<< (std::ostream& os, MacAddress const& address); - -}; // namespace ns3 - -#endif /* MAC_ADDRESS_H */ diff --git a/src/node/net-device.cc b/src/node/net-device.cc index 5774abeac..1a9163aad 100644 --- a/src/node/net-device.cc +++ b/src/node/net-device.cc @@ -32,7 +32,7 @@ namespace ns3 { const InterfaceId NetDevice::iid = MakeInterfaceId ("NetDevice", Object::iid); -NetDevice::NetDevice(Ptr node, const MacAddress& addr) : +NetDevice::NetDevice(Ptr node, const Address& addr) : m_node (node), m_name(""), m_ifIndex (0), @@ -50,7 +50,7 @@ NetDevice::NetDevice(Ptr node, const MacAddress& addr) : NetDevice::~NetDevice () {} -MacAddress +Address NetDevice::GetAddress (void) const { return m_address; @@ -110,7 +110,7 @@ NetDevice::IsBroadcast (void) const { return m_isBroadcast; } -MacAddress const & +Address const & NetDevice::GetBroadcast (void) const { NS_ASSERT (m_isBroadcast); @@ -118,7 +118,7 @@ NetDevice::GetBroadcast (void) const } void -NetDevice::EnableBroadcast (MacAddress broadcast) +NetDevice::EnableBroadcast (Address broadcast) { m_isBroadcast = true; m_broadcast = broadcast; @@ -168,7 +168,7 @@ NetDevice::DisablePointToPoint (void) // Receive packet from above bool -NetDevice::Send(Packet& p, const MacAddress& dest, uint16_t protocolNumber) +NetDevice::Send(Packet& p, const Address& dest, uint16_t protocolNumber) { if (m_isUp) { diff --git a/src/node/net-device.h b/src/node/net-device.h index 522b1ed41..9dace11e5 100644 --- a/src/node/net-device.h +++ b/src/node/net-device.h @@ -27,7 +27,7 @@ #include "ns3/packet.h" #include "ns3/object.h" #include "ns3/ptr.h" -#include "mac-address.h" +#include "address.h" namespace ns3 { @@ -78,9 +78,9 @@ public: Ptr GetChannel (void) const; /** - * \return the current MacAddress of this interface. + * \return the current Address of this interface. */ - MacAddress GetAddress (void) const; + Address GetAddress (void) const; /** * \param mtu MTU value, in bytes, to set for the device * \return whether the MTU value was within legal bounds @@ -136,7 +136,7 @@ public: * Calling this method is invalid if IsBroadcast returns * not true. */ - MacAddress const &GetBroadcast (void) const; + Address const &GetBroadcast (void) const; /** * \return value of m_isMulticast flag */ @@ -153,11 +153,11 @@ public: * is received. * * Called from higher layer to send packet into Network Device - * to the specified destination MacAddress + * to the specified destination Address * * \return whether the Send operation succeeded */ - bool Send(Packet& p, const MacAddress& dest, uint16_t protocolNumber); + bool Send(Packet& p, const Address& dest, uint16_t protocolNumber); /** * \returns the node base class which contains this network * interface. @@ -187,12 +187,12 @@ public: * \param node base class node pointer of device's node * \param addr MAC address of this device. */ - NetDevice(Ptr node, const MacAddress& addr); + NetDevice(Ptr node, const Address& addr); /** * Enable broadcast support. This method should be * called by subclasses from their constructor */ - void EnableBroadcast (MacAddress broadcast); + void EnableBroadcast (Address broadcast); /** * Set m_isBroadcast flag to false */ @@ -255,7 +255,7 @@ public: * method. When the link is Up, this method is invoked to ask * subclasses to forward packets. Subclasses MUST override this method. */ - virtual bool SendTo (Packet& p, const MacAddress& dest) = 0; + virtual bool SendTo (Packet& p, const Address& dest) = 0; /** * \returns true if this NetDevice needs the higher-layers * to perform ARP over it, false otherwise. @@ -282,8 +282,8 @@ public: Ptr m_node; std::string m_name; uint16_t m_ifIndex; - MacAddress m_address; - MacAddress m_broadcast; + Address m_address; + Address m_broadcast; uint16_t m_mtu; bool m_isUp; bool m_isBroadcast; diff --git a/src/node/wscript b/src/node/wscript index e11cd8eac..738402fd3 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -12,7 +12,6 @@ def build(bld): 'node.cc', 'ipv4-address.cc', 'net-device.cc', - 'mac-address.cc', 'llc-snap-header.cc', 'ipv4-route.cc', 'queue.cc', @@ -35,7 +34,6 @@ def build(bld): 'node.h', 'ipv4-address.h', 'net-device.h', - 'mac-address.h', 'ipv4-route.h', 'queue.h', 'drop-tail-queue.h', From c716994cabb144cbcc6627f58b11a75c57bf77c5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 08:23:20 +0200 Subject: [PATCH 04/48] convert Socket to use Address class --- examples/simple-p2p.cc | 7 +-- samples/main-simple.cc | 9 ++- src/applications/onoff-application.cc | 20 +++--- src/applications/onoff-application.h | 20 +++--- src/internet-node/udp-socket.cc | 83 +++++++++++++++--------- src/internet-node/udp-socket.h | 27 ++++---- src/node/socket.cc | 28 ++++----- src/node/socket.h | 91 +++++++++------------------ 8 files changed, 133 insertions(+), 152 deletions(-) diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index ac98440c8..c5b0e61ce 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -57,6 +57,7 @@ #include "ns3/p2p-channel.h" #include "ns3/p2p-net-device.h" #include "ns3/ipv4-address.h" +#include "ns3/ipv4-transport-address.h" #include "ns3/ipv4.h" #include "ns3/socket.h" #include "ns3/ipv4-route.h" @@ -142,8 +143,7 @@ int main (int argc, char *argv[]) // 210 bytes at a rate of 448 Kb/s Ptr ooff = Create ( n0, - Ipv4Address("10.1.3.2"), - 80, + Ipv4TransportAddress(Ipv4Address ("10.1.3.2"), 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); @@ -154,8 +154,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, - Ipv4Address("10.1.2.1"), - 80, + Ipv4TransportAddress(Ipv4Address ("10.1.2.1"), 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); diff --git a/samples/main-simple.cc b/samples/main-simple.cc index f0f4ce4dc..80128d7d5 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -4,6 +4,7 @@ #include "ns3/simulator.h" #include "ns3/socket-factory.h" #include "ns3/socket.h" +#include "ns3/ipv4-transport-address.h" #include "ns3/nstime.h" using namespace ns3; @@ -24,7 +25,7 @@ GenerateTraffic (Ptr socket, uint32_t size) } static void -SocketPrinter (Ptr socket, uint32_t size, const Ipv4Address &from, uint16_t fromPort) +SocketPrinter (Ptr socket, uint32_t size, const Address &from) { std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << size << std::endl; } @@ -44,10 +45,12 @@ RunSimulation (void) Ptr socketFactory = a->QueryInterface (iid); Ptr sink = socketFactory->CreateSocket (); - sink->Bind (80); + Ipv4TransportAddress local = Ipv4TransportAddress (Ipv4Address::GetAny (), 80); + sink->Bind (local.ConvertTo ()); Ptr source = socketFactory->CreateSocket (); - source->Connect (Ipv4Address::GetLoopback (), 80); + Ipv4TransportAddress remote = Ipv4TransportAddress (Ipv4Address::GetLoopback (), 80); + source->Connect (remote.ConvertTo ()); GenerateTraffic (source, 500); PrintTraffic (sink); diff --git a/src/applications/onoff-application.cc b/src/applications/onoff-application.cc index f9c4146c8..5fa2c8266 100644 --- a/src/applications/onoff-application.cc +++ b/src/applications/onoff-application.cc @@ -22,7 +22,7 @@ // George F. Riley, Georgia Tech, Spring 2007 // Adapted from ApplicationOnOff in GTNetS. -#include "ns3/ipv4-address.h" +#include "ns3/address.h" #include "ns3/node.h" #include "ns3/nstime.h" #include "ns3/data-rate.h" @@ -47,22 +47,20 @@ static NumericDefaultValue g_defaultSize ("OnOffApplicationPacketSize" // Constructors OnOffApplication::OnOffApplication(Ptr n, - const Ipv4Address rip, - uint16_t rport, + const Address &remote, std::string iid, const RandomVariable& ontime, const RandomVariable& offtime) : Application(n), m_cbrRate (g_defaultRate.GetValue ()) { - Construct (n, rip, rport, iid, + Construct (n, remote, iid, ontime, offtime, g_defaultSize.GetValue ()); } OnOffApplication::OnOffApplication(Ptr n, - const Ipv4Address rip, - uint16_t rport, + const Address &remote, std::string iid, const RandomVariable& ontime, const RandomVariable& offtime, @@ -71,22 +69,20 @@ OnOffApplication::OnOffApplication(Ptr n, : Application(n), m_cbrRate (rate) { - Construct (n, rip, rport, iid, + Construct (n, remote, iid, ontime, offtime, size); } void OnOffApplication::Construct (Ptr n, - const Ipv4Address rip, - uint16_t rport, + const Address &remote, std::string iid, const RandomVariable& onTime, const RandomVariable& offTime, uint32_t size) { m_socket = 0; - m_peerIp = rip; - m_peerPort = rport; + m_peer = remote; m_connected = false; m_onTime = onTime.Copy (); m_offTime = offTime.Copy (); @@ -144,7 +140,7 @@ void OnOffApplication::StartApplication() // Called at time specified by Star Ptr socketFactory = GetNode ()->QueryInterface (iid); m_socket = socketFactory->CreateSocket (); m_socket->Bind (); - m_socket->Connect (m_peerIp, m_peerPort); + m_socket->Connect (m_peer); } // Insure no pending event StopApplication(); diff --git a/src/applications/onoff-application.h b/src/applications/onoff-application.h index e3aafcb11..c42d9d14f 100644 --- a/src/applications/onoff-application.h +++ b/src/applications/onoff-application.h @@ -31,7 +31,7 @@ namespace ns3 { -class Ipv4Address; +class Address; class RandomVariable; class Socket; class DataRate; @@ -52,23 +52,20 @@ class OnOffApplication : public Application public: /** * \param n node associated to this application - * \param rip remote ip address - * \param rport remove port number + * \param remote remote ip address * \param iid * \param ontime on time random variable * \param offtime off time random variable */ OnOffApplication(Ptr n, - const Ipv4Address rip, - uint16_t rport, + const Address &remote, std::string iid, const RandomVariable& ontime, const RandomVariable& offtime); /** * \param n node associated to this application - * \param rip remote ip address - * \param rport remove port number + * \param remote remote ip address * \param iid * \param ontime on time random variable * \param offtime off time random variable @@ -76,8 +73,7 @@ public: * \param size size of packets when sending data. */ OnOffApplication(Ptr n, - const Ipv4Address rip, - uint16_t rport, + const Address &remote, std::string iid, const RandomVariable& ontime, const RandomVariable& offtime, @@ -112,8 +108,7 @@ private: virtual void StopApplication (void); // Called at time specified by Stop void Construct (Ptr n, - const Ipv4Address rip, - uint16_t rport, + const Address &remote, std::string iid, const RandomVariable& ontime, const RandomVariable& offtime, @@ -126,8 +121,7 @@ private: void SendPacket(); Ptr m_socket; // Associated socket - Ipv4Address m_peerIp; // Peer IP address - uint16_t m_peerPort; // Peer port + Address m_peer; // Peer address bool m_connected; // True if connected RandomVariable* m_onTime; // rng for On Time RandomVariable* m_offTime; // rng for Off Time diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 0ab911c9e..97b6a3564 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -19,6 +19,7 @@ * Author: Mathieu Lacage */ #include "ns3/node.h" +#include "ns3/ipv4-transport-address.h" #include "udp-socket.h" #include "udp-l4-protocol.h" #include "ipv4-end-point.h" @@ -88,21 +89,28 @@ UdpSocket::Bind (void) return FinishBind (); } int -UdpSocket::Bind (Ipv4Address address) +UdpSocket::Bind (const Address &address) { - m_endPoint = m_udp->Allocate (address); - return FinishBind (); -} -int -UdpSocket::Bind (uint16_t port) -{ - m_endPoint = m_udp->Allocate (port); - return FinishBind (); -} -int -UdpSocket::Bind (Ipv4Address address, uint16_t port) -{ - m_endPoint = m_udp->Allocate (address, port); + Ipv4TransportAddress transport = Ipv4TransportAddress::ConvertFrom (address); + Ipv4Address ipv4 = transport.GetIpv4 (); + uint16_t port = transport.GetPort (); + if (ipv4 == Ipv4Address::GetAny () && port == 0) + { + m_endPoint = m_udp->Allocate (); + } + else if (ipv4 == Ipv4Address::GetAny () && port != 0) + { + m_endPoint = m_udp->Allocate (port); + } + else if (ipv4 != Ipv4Address::GetAny () && port == 0) + { + m_endPoint = m_udp->Allocate (ipv4); + } + else if (ipv4 != Ipv4Address::GetAny () && port != 0) + { + m_endPoint = m_udp->Allocate (ipv4, port); + } + return FinishBind (); } @@ -134,14 +142,14 @@ UdpSocket::DoClose(ns3::Callback > closeCompleted) } } void -UdpSocket::DoConnect(const Ipv4Address & address, - uint16_t portNumber, +UdpSocket::DoConnect(const Address & address, ns3::Callback > connectionSucceeded, ns3::Callback > connectionFailed, ns3::Callback > halfClose) { - m_defaultAddress = address; - m_defaultPort = portNumber; + Ipv4TransportAddress transport = Ipv4TransportAddress::ConvertFrom (address); + m_defaultAddress = transport.GetIpv4 (); + m_defaultPort = transport.GetPort (); if (!connectionSucceeded.IsNull ()) { connectionSucceeded (this); @@ -149,8 +157,8 @@ UdpSocket::DoConnect(const Ipv4Address & address, m_connected = true; } int -UdpSocket::DoAccept(ns3::Callback, const Ipv4Address&, uint16_t> connectionRequest, - ns3::Callback, const Ipv4Address&, uint16_t> newConnectionCreated, +UdpSocket::DoAccept(ns3::Callback, const Address&> connectionRequest, + ns3::Callback, const Address&> newConnectionCreated, ns3::Callback > closeRequested) { // calling accept on a udp socket is a programming error. @@ -179,7 +187,16 @@ UdpSocket::DoSend (const uint8_t* buffer, return DoSendPacketTo (p, m_defaultAddress, m_defaultPort, dataSent); } int -UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, +UdpSocket::DoSendPacketTo (const Packet &p, const Address &address, + ns3::Callback, uint32_t> dataSent) +{ + Ipv4TransportAddress transport = Ipv4TransportAddress::ConvertFrom (address); + Ipv4Address ipv4 = transport.GetIpv4 (); + uint16_t port = transport.GetPort (); + return DoSendPacketTo (p, ipv4, port, dataSent); +} +int +UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address ipv4, uint16_t port, ns3::Callback, uint32_t> dataSent) { if (m_endPoint == 0) @@ -196,8 +213,8 @@ UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, m_errno = ERROR_SHUTDOWN; return -1; } - m_udp->Send (p, m_endPoint->GetLocalAddress (), daddr, - m_endPoint->GetLocalPort (), dport); + m_udp->Send (p, m_endPoint->GetLocalAddress (), ipv4, + m_endPoint->GetLocalPort (), port); if (!dataSent.IsNull ()) { dataSent (this, p.GetSize ()); @@ -205,8 +222,7 @@ UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, return 0; } int -UdpSocket::DoSendTo(const Ipv4Address &address, - uint16_t port, +UdpSocket::DoSendTo(const Address &address, const uint8_t *buffer, uint32_t size, ns3::Callback, uint32_t> dataSent) @@ -225,34 +241,39 @@ UdpSocket::DoSendTo(const Ipv4Address &address, { p = Packet (buffer, size); } - return DoSendPacketTo (p, address, port, dataSent); + Ipv4TransportAddress transport = Ipv4TransportAddress::ConvertFrom (address); + Ipv4Address ipv4 = transport.GetIpv4 (); + uint16_t port = transport.GetPort (); + return DoSendPacketTo (p, ipv4, port, dataSent); } void -UdpSocket::DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t> callback) +UdpSocket::DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Address&> callback) { m_rxCallback = callback; } void -UdpSocket::DoRecvDummy(ns3::Callback, uint32_t,const Ipv4Address&, uint16_t> callback) +UdpSocket::DoRecvDummy(ns3::Callback, uint32_t,const Address&> callback) { m_dummyRxCallback = callback; } void -UdpSocket::ForwardUp (const Packet &packet, Ipv4Address saddr, uint16_t sport) +UdpSocket::ForwardUp (const Packet &packet, Ipv4Address ipv4, uint16_t port) { if (m_shutdownRecv) { return; } + + Address address = Ipv4TransportAddress (ipv4, port).ConvertTo (); Packet p = packet; if (!m_dummyRxCallback.IsNull ()) { - m_dummyRxCallback (this, p.GetSize (), saddr, sport); + m_dummyRxCallback (this, p.GetSize (), address); } if (!m_rxCallback.IsNull ()) { - m_rxCallback (this, p.PeekData (), p.GetSize (), saddr, sport); + m_rxCallback (this, p.PeekData (), p.GetSize (), address); } } diff --git a/src/internet-node/udp-socket.h b/src/internet-node/udp-socket.h index c2923c80f..82d3cd6a3 100644 --- a/src/internet-node/udp-socket.h +++ b/src/internet-node/udp-socket.h @@ -25,6 +25,7 @@ #include "ns3/callback.h" #include "ns3/socket.h" #include "ns3/ptr.h" +#include "ns3/ipv4-address.h" namespace ns3 { @@ -45,39 +46,37 @@ public: virtual enum SocketErrno GetErrno (void) const; virtual Ptr GetNode (void) const; virtual int Bind (void); - virtual int Bind (Ipv4Address address); - virtual int Bind (uint16_t port); - virtual int Bind (Ipv4Address address, uint16_t port); + virtual int Bind (const Address &address); virtual int ShutdownSend (void); virtual int ShutdownRecv (void); private: virtual void DoClose(ns3::Callback > closeCompleted); - virtual void DoConnect(const Ipv4Address & address, - uint16_t portNumber, + virtual void DoConnect(const Address & address, ns3::Callback > connectionSucceeded, ns3::Callback > connectionFailed, ns3::Callback > halfClose); - virtual int DoAccept(ns3::Callback, const Ipv4Address&, uint16_t> connectionRequest, - ns3::Callback, const Ipv4Address&, uint16_t> newConnectionCreated, + virtual int DoAccept(ns3::Callback, const Address&> connectionRequest, + ns3::Callback, const Address&> newConnectionCreated, ns3::Callback > closeRequested); virtual int DoSend (const uint8_t* buffer, uint32_t size, ns3::Callback, uint32_t> dataSent); - virtual int DoSendTo(const Ipv4Address &address, - uint16_t port, + virtual int DoSendTo(const Address &address, const uint8_t *buffer, uint32_t size, ns3::Callback, uint32_t> dataSent); - virtual void DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t>); - virtual void DoRecvDummy(ns3::Callback, uint32_t,const Ipv4Address&, uint16_t>); + virtual void DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Address&>); + virtual void DoRecvDummy(ns3::Callback, uint32_t,const Address&>); private: friend class Udp; // invoked by Udp class int FinishBind (void); - void ForwardUp (const Packet &p, Ipv4Address saddr, uint16_t sport); + void ForwardUp (const Packet &p, Ipv4Address ipv4, uint16_t port); void Destroy (void); + int DoSendPacketTo (const Packet &p, const Address &daddr, + ns3::Callback, uint32_t> dataSent); int DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, ns3::Callback, uint32_t> dataSent); @@ -86,8 +85,8 @@ private: Ptr m_udp; Ipv4Address m_defaultAddress; uint16_t m_defaultPort; - Callback,uint32_t,const Ipv4Address &,uint16_t> m_dummyRxCallback; - Callback,uint8_t const*,uint32_t,const Ipv4Address &,uint16_t> m_rxCallback; + Callback,uint32_t,const Address &> m_dummyRxCallback; + Callback,uint8_t const*,uint32_t,const Address &> m_rxCallback; enum SocketErrno m_errno; bool m_shutdownSend; bool m_shutdownRecv; diff --git a/src/node/socket.cc b/src/node/socket.cc index 75cd3a023..b8995cfea 100644 --- a/src/node/socket.cc +++ b/src/node/socket.cc @@ -12,17 +12,16 @@ Socket::Close(Callback > closeCompleted) } void -Socket::Connect(const Ipv4Address & address, - uint16_t portNumber, +Socket::Connect(const Address & address, Callback > connectionSucceeded, Callback > connectionFailed, Callback > halfClose) { - DoConnect (address, portNumber, connectionSucceeded, connectionFailed, halfClose); + DoConnect (address, connectionSucceeded, connectionFailed, halfClose); } int -Socket::Accept(Callback, const Ipv4Address&, uint16_t> connectionRequest, - Callback, const Ipv4Address&, uint16_t> newConnectionCreated, +Socket::Accept(Callback, const Address&> connectionRequest, + Callback, const Address&> newConnectionCreated, Callback > closeRequested) { return DoAccept (connectionRequest, newConnectionCreated, closeRequested); @@ -35,28 +34,27 @@ Socket::Send (const uint8_t* buffer, return DoSend (buffer, size, dataSent); } int -Socket::SendTo(const Ipv4Address &address, - uint16_t port, +Socket::SendTo(const Address &address, const uint8_t *buffer, uint32_t size, Callback, uint32_t> dataSent) { - return DoSendTo (address, port, buffer, size, dataSent); + return DoSendTo (address, buffer, size, dataSent); } void -Socket::Recv(Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t> callback) +Socket::Recv(Callback, const uint8_t*, uint32_t,const Address&> callback) { DoRecv (callback); } void -Socket::RecvDummy(Callback, uint32_t,const Ipv4Address&, uint16_t> callback) +Socket::RecvDummy(Callback, uint32_t,const Address&> callback) { DoRecvDummy (callback); } bool -Socket::RefuseAllConnections (Ptr socket, const Ipv4Address& address, uint16_t port) +Socket::RefuseAllConnections (Ptr socket, const Address& address) { return false; } @@ -67,14 +65,14 @@ void Socket::DummyCallbackVoidSocketUi32 (Ptr socket, uint32_t) {} void -Socket::DummyCallbackVoidSocketUi32Ipv4AddressUi16 (Ptr socket, uint32_t, const Ipv4Address &, uint16_t) +Socket::DummyCallbackVoidSocketUi32Address (Ptr socket, uint32_t, const Address &) {} void -Socket::DummyCallbackVoidSocketBufferUi32Ipv4AddressUi16 (Ptr socket, const uint8_t *, uint32_t, - const Ipv4Address &, uint16_t) +Socket::DummyCallbackVoidSocketBufferUi32Address (Ptr socket, const uint8_t *, uint32_t, + const Address &) {} void -Socket::DummyCallbackVoidSocketIpv4AddressUi16 (Ptr socket, const Ipv4Address &, uint16_t) +Socket::DummyCallbackVoidSocketAddress (Ptr socket, const Address &) {} diff --git a/src/node/socket.h b/src/node/socket.h index b0ec81b05..3cba7fd59 100644 --- a/src/node/socket.h +++ b/src/node/socket.h @@ -23,8 +23,8 @@ #include "ns3/callback.h" #include "ns3/ptr.h" -#include "ipv4-address.h" #include "ns3/object.h" +#include "address.h" #include namespace ns3 { @@ -35,8 +35,7 @@ class Node; * \brief Define a Socket API based on the BSD Socket API. * * Contrary to the original BSD socket API, this API is asynchronous: - * it does not contain blocking calls. This API also does not use - * the dreaded BSD sockaddr_t type. Other than that, it tries to stick + * it does not contain blocking calls. Other than that, it tries to stick * to the BSD API to make it easier those who know the BSD API to use * this API. */ @@ -69,42 +68,20 @@ public: virtual Ptr GetNode (void) const = 0; /** - * Allocate a free port number and - * bind this socket to this port number on all - * interfaces of this system. - * + * \param address the address to try to allocate * \returns 0 on success, -1 on failure. + * + * Allocate a local endpoint for this socket. */ - virtual int Bind (void) = 0; + virtual int Bind (const Address &address) = 0; /** - * Allocate a free port number and - * bind this socket to this port number on the - * specified interface. + * Allocate a local endpoint for this socket. * - * \param address address of interface to bind to. * \returns 0 on success, -1 on failure. */ - virtual int Bind (Ipv4Address address) = 0; + virtual int Bind () = 0; - /** - * Bind this socket to this port number - * on all interfaces of this system. - * - * \param port port to bind to on all interfaces - * \returns 0 on success, -1 on failure. - */ - virtual int Bind (uint16_t port) = 0; - - /** - * Bind this socket to this port number - * on the interface specified by address. - * - * \param address address of interface to bind to. - * \param port port to bind to on specified interface - * \returns 0 on success, -1 on failure. - */ - virtual int Bind (Ipv4Address address, uint16_t port) = 0; /** * \brief Close a socket. @@ -134,8 +111,7 @@ public: /** * \brief Initiate a connection to a remote host - * \param address IP Address of remote. - * \param portNumber Port number of remote + * \param address Address of remote. * \param connectionSucceeded this callback is invoked when the connection request * initiated by the user is successfully completed. The callback is passed * back a pointer to the same socket object. @@ -145,8 +121,7 @@ public: * \param halfClose XXX When exactly is this callback invoked ? If it invoked when the * other side closes the connection ? Or when I call Close ? */ - void Connect(const Ipv4Address & address, - uint16_t portNumber, + void Connect(const Address &address, Callback > connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket), Callback > connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket), Callback > halfClose = MakeCallback(&Socket::DummyCallbackVoidSocket)); @@ -170,10 +145,10 @@ public: * \param closeRequested Callback for connection close request from peer. * XXX: when is this callback invoked ? */ - int Accept(Callback, const Ipv4Address&, uint16_t> connectionRequest = + int Accept(Callback, const Address &> connectionRequest = MakeCallback(&Socket::RefuseAllConnections), - Callback, const Ipv4Address&, uint16_t> newConnectionCreated = - MakeCallback (&Socket::DummyCallbackVoidSocketIpv4AddressUi16), + Callback, const Address&> newConnectionCreated = + MakeCallback (&Socket::DummyCallbackVoidSocketAddress), Callback > closeRequested = MakeCallback (&Socket::DummyCallbackVoidSocket)); /** @@ -191,15 +166,13 @@ public: /** * \brief Send data to a specified peer. * \param address IP Address of remote host - * \param port port number * \param buffer Data to send (nil if dummy data). * \param size Number of bytes to send. * \param dataSent Data sent callback. * \returns -1 in case of error or the number of bytes copied in the * internal buffer and accepted for transmission. */ - int SendTo(const Ipv4Address &address, - uint16_t port, + int SendTo(const Address &address, const uint8_t *buffer, uint32_t size, Callback, uint32_t> dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32)); @@ -213,8 +186,8 @@ public: * allocation to hold the dummy memory into a buffer which can be passed * to the user. Instead, consider using the RecvDummy method. */ - void Recv(Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t> receivedData = - MakeCallback (&Socket::DummyCallbackVoidSocketBufferUi32Ipv4AddressUi16)); + void Recv(Callback, const uint8_t*, uint32_t,const Address&> receivedData = + MakeCallback (&Socket::DummyCallbackVoidSocketBufferUi32Address)); /** * \brief Receive data @@ -223,38 +196,36 @@ public: * This method is included because it is vastly more efficient than the * Recv method when you use dummy payload. */ - void RecvDummy(Callback, uint32_t,const Ipv4Address&, uint16_t> receivedData = - MakeCallback (&Socket::DummyCallbackVoidSocketUi32Ipv4AddressUi16)); + void RecvDummy(Callback, uint32_t,const Address&> receivedData = + MakeCallback (&Socket::DummyCallbackVoidSocketUi32Address)); private: virtual void DoClose(Callback > closeCompleted) = 0; - virtual void DoConnect(const Ipv4Address & address, - uint16_t portNumber, + virtual void DoConnect(const Address & address, Callback > connectionSucceeded, Callback > connectionFailed, Callback > halfClose) = 0; - virtual int DoAccept(Callback, const Ipv4Address&, uint16_t> connectionRequest, - Callback, const Ipv4Address&, uint16_t> newConnectionCreated, + virtual int DoAccept(Callback, const Address&> connectionRequest, + Callback, const Address&> newConnectionCreated, Callback > closeRequested) = 0; virtual int DoSend (const uint8_t* buffer, - uint32_t size, - Callback, uint32_t> dataSent) = 0; - virtual int DoSendTo(const Ipv4Address &address, - uint16_t port, + uint32_t size, + Callback, uint32_t> dataSent) = 0; + virtual int DoSendTo(const Address &address, const uint8_t *buffer, uint32_t size, Callback, uint32_t> dataSent) = 0; - virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t> receive) = 0; - virtual void DoRecvDummy(Callback, uint32_t,const Ipv4Address&, uint16_t>) = 0; + virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Address&> receive) = 0; + virtual void DoRecvDummy(Callback, uint32_t,const Address&>) = 0; - static bool RefuseAllConnections (Ptr socket, const Ipv4Address& address, uint16_t port); + static bool RefuseAllConnections (Ptr socket, const Address& address); static void DummyCallbackVoidSocket (Ptr socket); static void DummyCallbackVoidSocketUi32 (Ptr socket, uint32_t); - static void DummyCallbackVoidSocketUi32Ipv4AddressUi16 (Ptr socket, uint32_t, const Ipv4Address &, uint16_t); - static void DummyCallbackVoidSocketBufferUi32Ipv4AddressUi16 (Ptr socket, const uint8_t *, uint32_t, - const Ipv4Address &, uint16_t); - static void DummyCallbackVoidSocketIpv4AddressUi16 (Ptr socket, const Ipv4Address &, uint16_t); + static void DummyCallbackVoidSocketUi32Address (Ptr socket, uint32_t, const Address &); + static void DummyCallbackVoidSocketBufferUi32Address (Ptr socket, const uint8_t *, uint32_t, + const Address &); + static void DummyCallbackVoidSocketAddress (Ptr socket, const Address &); }; } //namespace ns3 From 863de62880e2e4ca7f7ab652015708c569a6947b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 08:32:34 +0200 Subject: [PATCH 05/48] ipv4-transport-address -> inet-address --- examples/simple-p2p.cc | 2 +- samples/main-simple.cc | 2 +- src/internet-node/udp-socket.cc | 2 +- src/node/{ipv4-transport-address.cc => inet-address.cc} | 2 +- src/node/{ipv4-transport-address.h => inet-address.h} | 0 src/node/wscript | 4 ++-- 6 files changed, 6 insertions(+), 6 deletions(-) rename src/node/{ipv4-transport-address.cc => inet-address.cc} (97%) rename src/node/{ipv4-transport-address.h => inet-address.h} (100%) diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index c5b0e61ce..9aaff28f3 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -57,7 +57,7 @@ #include "ns3/p2p-channel.h" #include "ns3/p2p-net-device.h" #include "ns3/ipv4-address.h" -#include "ns3/ipv4-transport-address.h" +#include "ns3/inet-address.h" #include "ns3/ipv4.h" #include "ns3/socket.h" #include "ns3/ipv4-route.h" diff --git a/samples/main-simple.cc b/samples/main-simple.cc index 80128d7d5..bdfd9e6f8 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -4,7 +4,7 @@ #include "ns3/simulator.h" #include "ns3/socket-factory.h" #include "ns3/socket.h" -#include "ns3/ipv4-transport-address.h" +#include "ns3/inet-address.h" #include "ns3/nstime.h" using namespace ns3; diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 97b6a3564..d92cde129 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -19,7 +19,7 @@ * Author: Mathieu Lacage */ #include "ns3/node.h" -#include "ns3/ipv4-transport-address.h" +#include "ns3/inet-address.h" #include "udp-socket.h" #include "udp-l4-protocol.h" #include "ipv4-end-point.h" diff --git a/src/node/ipv4-transport-address.cc b/src/node/inet-address.cc similarity index 97% rename from src/node/ipv4-transport-address.cc rename to src/node/inet-address.cc index 9330737c8..2f7fff7c6 100644 --- a/src/node/ipv4-transport-address.cc +++ b/src/node/inet-address.cc @@ -1,4 +1,4 @@ -#include "ipv4-transport-address.h" +#include "inet-address.h" #include "ns3/assert.h" namespace ns3 { diff --git a/src/node/ipv4-transport-address.h b/src/node/inet-address.h similarity index 100% rename from src/node/ipv4-transport-address.h rename to src/node/inet-address.h diff --git a/src/node/wscript b/src/node/wscript index 738402fd3..d02aea513 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -8,7 +8,7 @@ def build(bld): node.source = [ 'address.cc', 'eui48-address.cc', - 'ipv4-transport-address.cc', + 'inet-address.cc', 'node.cc', 'ipv4-address.cc', 'net-device.cc', @@ -30,7 +30,7 @@ def build(bld): headers.source = [ 'address.h', 'eui48-address.h', - 'ipv4-transport-address.h', + 'inet-address.h', 'node.h', 'ipv4-address.h', 'net-device.h', From 695119b3a505b04d78e6c6ce29b3cb67db8720c2 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 08:44:26 +0200 Subject: [PATCH 06/48] Ipv4TransportAddress -> InetAddress --- examples/simple-p2p.cc | 4 ++-- samples/main-simple.cc | 4 ++-- src/internet-node/udp-socket.cc | 10 +++++----- src/node/inet-address.cc | 24 ++++++++++++------------ src/node/inet-address.h | 10 +++++----- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index 9aaff28f3..ec41936a8 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -143,7 +143,7 @@ int main (int argc, char *argv[]) // 210 bytes at a rate of 448 Kb/s Ptr ooff = Create ( n0, - Ipv4TransportAddress(Ipv4Address ("10.1.3.2"), 80).ConvertTo (), + InetAddress(Ipv4Address ("10.1.3.2"), 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); @@ -154,7 +154,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, - Ipv4TransportAddress(Ipv4Address ("10.1.2.1"), 80).ConvertTo (), + InetAddress(Ipv4Address ("10.1.2.1"), 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); diff --git a/samples/main-simple.cc b/samples/main-simple.cc index bdfd9e6f8..4c709b4c5 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -45,11 +45,11 @@ RunSimulation (void) Ptr socketFactory = a->QueryInterface (iid); Ptr sink = socketFactory->CreateSocket (); - Ipv4TransportAddress local = Ipv4TransportAddress (Ipv4Address::GetAny (), 80); + InetAddress local = InetAddress (Ipv4Address::GetAny (), 80); sink->Bind (local.ConvertTo ()); Ptr source = socketFactory->CreateSocket (); - Ipv4TransportAddress remote = Ipv4TransportAddress (Ipv4Address::GetLoopback (), 80); + InetAddress remote = InetAddress (Ipv4Address::GetLoopback (), 80); source->Connect (remote.ConvertTo ()); GenerateTraffic (source, 500); diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index d92cde129..9ae438f6b 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -91,7 +91,7 @@ UdpSocket::Bind (void) int UdpSocket::Bind (const Address &address) { - Ipv4TransportAddress transport = Ipv4TransportAddress::ConvertFrom (address); + InetAddress transport = InetAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); if (ipv4 == Ipv4Address::GetAny () && port == 0) @@ -147,7 +147,7 @@ UdpSocket::DoConnect(const Address & address, ns3::Callback > connectionFailed, ns3::Callback > halfClose) { - Ipv4TransportAddress transport = Ipv4TransportAddress::ConvertFrom (address); + InetAddress transport = InetAddress::ConvertFrom (address); m_defaultAddress = transport.GetIpv4 (); m_defaultPort = transport.GetPort (); if (!connectionSucceeded.IsNull ()) @@ -190,7 +190,7 @@ int UdpSocket::DoSendPacketTo (const Packet &p, const Address &address, ns3::Callback, uint32_t> dataSent) { - Ipv4TransportAddress transport = Ipv4TransportAddress::ConvertFrom (address); + InetAddress transport = InetAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); return DoSendPacketTo (p, ipv4, port, dataSent); @@ -241,7 +241,7 @@ UdpSocket::DoSendTo(const Address &address, { p = Packet (buffer, size); } - Ipv4TransportAddress transport = Ipv4TransportAddress::ConvertFrom (address); + InetAddress transport = InetAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); return DoSendPacketTo (p, ipv4, port, dataSent); @@ -265,7 +265,7 @@ UdpSocket::ForwardUp (const Packet &packet, Ipv4Address ipv4, uint16_t port) return; } - Address address = Ipv4TransportAddress (ipv4, port).ConvertTo (); + Address address = InetAddress (ipv4, port).ConvertTo (); Packet p = packet; if (!m_dummyRxCallback.IsNull ()) { diff --git a/src/node/inet-address.cc b/src/node/inet-address.cc index 2f7fff7c6..2c48cc922 100644 --- a/src/node/inet-address.cc +++ b/src/node/inet-address.cc @@ -3,42 +3,42 @@ namespace ns3 { -Ipv4TransportAddress::Ipv4TransportAddress (Ipv4Address ipv4, uint16_t port) +InetAddress::InetAddress (Ipv4Address ipv4, uint16_t port) : m_ipv4 (ipv4), m_port (port) {} -Ipv4TransportAddress::Ipv4TransportAddress (Ipv4Address ipv4) +InetAddress::InetAddress (Ipv4Address ipv4) : m_ipv4 (ipv4), m_port (0) {} -Ipv4TransportAddress::Ipv4TransportAddress (uint16_t port) +InetAddress::InetAddress (uint16_t port) : m_ipv4 (Ipv4Address::GetAny ()), m_port (port) {} uint16_t -Ipv4TransportAddress::GetPort (void) const +InetAddress::GetPort (void) const { return m_port; } Ipv4Address -Ipv4TransportAddress::GetIpv4 (void) const +InetAddress::GetIpv4 (void) const { return m_ipv4; } void -Ipv4TransportAddress::SetPort (uint16_t port) +InetAddress::SetPort (uint16_t port) { m_port = port; } void -Ipv4TransportAddress::SetIpv4 (Ipv4Address address) +InetAddress::SetIpv4 (Ipv4Address address) { m_ipv4 = address; } Address -Ipv4TransportAddress::ConvertTo (void) const +InetAddress::ConvertTo (void) const { uint8_t buf[6]; m_ipv4.Serialize (buf); @@ -46,18 +46,18 @@ Ipv4TransportAddress::ConvertTo (void) const buf[5] = (m_port >> 8) & 0xff; return Address (GetType (), buf, 6); } -Ipv4TransportAddress -Ipv4TransportAddress::ConvertFrom (const Address &address) +InetAddress +InetAddress::ConvertFrom (const Address &address) { NS_ASSERT (address.CheckCompatible (GetType (), 6)); uint8_t buf[6]; address.CopyTo (buf); Ipv4Address ipv4 = Ipv4Address::Deserialize (buf); uint16_t port = buf[0] | (buf[1] << 8); - return Ipv4TransportAddress (ipv4, port); + return InetAddress (ipv4, port); } uint8_t -Ipv4TransportAddress::GetType (void) +InetAddress::GetType (void) { static uint8_t type = Address::Register (); return type; diff --git a/src/node/inet-address.h b/src/node/inet-address.h index e73ca8d3d..51c0766fb 100644 --- a/src/node/inet-address.h +++ b/src/node/inet-address.h @@ -7,12 +7,12 @@ namespace ns3 { -class Ipv4TransportAddress +class InetAddress { public: - Ipv4TransportAddress (Ipv4Address ipv4, uint16_t port); - Ipv4TransportAddress (Ipv4Address ipv4); - Ipv4TransportAddress (uint16_t post); + InetAddress (Ipv4Address ipv4, uint16_t port); + InetAddress (Ipv4Address ipv4); + InetAddress (uint16_t post); uint16_t GetPort (void) const; Ipv4Address GetIpv4 (void) const; @@ -20,7 +20,7 @@ public: void SetIpv4 (Ipv4Address address); Address ConvertTo (void) const; - static Ipv4TransportAddress ConvertFrom (const Address &address); + static InetAddress ConvertFrom (const Address &address); private: static uint8_t GetType (void); Ipv4Address m_ipv4; From 8474486b7404ee1ab223345506b962277397bec3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 08:47:58 +0200 Subject: [PATCH 07/48] add extra conveniance constructors and use them --- examples/simple-p2p.cc | 4 ++-- src/node/inet-address.cc | 8 ++++++++ src/node/inet-address.h | 4 +++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index ec41936a8..5d1bb545e 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -143,7 +143,7 @@ int main (int argc, char *argv[]) // 210 bytes at a rate of 448 Kb/s Ptr ooff = Create ( n0, - InetAddress(Ipv4Address ("10.1.3.2"), 80).ConvertTo (), + InetAddress("10.1.3.2", 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); @@ -154,7 +154,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, - InetAddress(Ipv4Address ("10.1.2.1"), 80).ConvertTo (), + InetAddress("10.1.2.1", 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); diff --git a/src/node/inet-address.cc b/src/node/inet-address.cc index 2c48cc922..bf82cfc83 100644 --- a/src/node/inet-address.cc +++ b/src/node/inet-address.cc @@ -11,6 +11,14 @@ InetAddress::InetAddress (Ipv4Address ipv4) : m_ipv4 (ipv4), m_port (0) {} +InetAddress::InetAddress (const char *ipv4, uint16_t port) + : m_ipv4 (Ipv4Address (ipv4)), + m_port (port) +{} +InetAddress::InetAddress (const char * ipv4) + : m_ipv4 (Ipv4Address (ipv4)), + m_port (0) +{} InetAddress::InetAddress (uint16_t port) : m_ipv4 (Ipv4Address::GetAny ()), m_port (port) diff --git a/src/node/inet-address.h b/src/node/inet-address.h index 51c0766fb..6ba6da5de 100644 --- a/src/node/inet-address.h +++ b/src/node/inet-address.h @@ -12,7 +12,9 @@ class InetAddress public: InetAddress (Ipv4Address ipv4, uint16_t port); InetAddress (Ipv4Address ipv4); - InetAddress (uint16_t post); + InetAddress (uint16_t port); + InetAddress (const char *ipv4, uint16_t port); + InetAddress (const char *ipv4); uint16_t GetPort (void) const; Ipv4Address GetIpv4 (void) const; From 3cec6af4a86d98fa9c3cc0aed421a947219536f2 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 09:44:24 +0200 Subject: [PATCH 08/48] add doxygen --- src/node/address.cc | 2 +- src/node/address.h | 98 ++++++++++++++++++++++++++++++++++++++-- src/node/eui48-address.h | 24 ++++++++++ src/node/inet-address.h | 54 +++++++++++++++++++++- 4 files changed, 173 insertions(+), 5 deletions(-) diff --git a/src/node/address.cc b/src/node/address.cc index 408a152e6..9bb67f4fb 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -47,7 +47,7 @@ Address::GetLength (void) const return m_len; } void -Address::CopyTo (uint8_t *buffer) const +Address::CopyTo (uint8_t buffer[MAX_SIZE]) const { NS_ASSERT (m_len <= MAX_SIZE); memcpy (buffer, m_data, m_len); diff --git a/src/node/address.h b/src/node/address.h index 417bdce0a..e6bbec026 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -6,24 +6,116 @@ namespace ns3 { +/** + * \brief a polymophic address class + * + * This class is very similar in design and spirit to the BSD sockaddr + * structure: they are both used to hold multiple types of addresses + * together with the type of the address. + * + * A new address class defined by a user needs to: + * - allocate a type id with Address::Register + * - provide a method to convert his new address to an Address + * instance. This method is typically a member method named ConvertTo: + * Address MyAddress::ConvertTo (void) const; + * - provide a method to convert an Address instance back to + * an instance of his new address type. This method is typically + * a static member method of his address class named ConvertFrom: + * static MyAddress MyAddress::ConvertFrom (const Address &address); + * - the ConvertFrom method is expected to check that the type of the + * input Address instance is compatible with its own type. + * + * Typical code to create a new class type looks like: + * \code + * // this class represents addresses which are 2 bytes long. + * class MyAddress + * { + * public: + * Address ConvertTo (void) const; + * static MyAddress ConvertFrom (void); + * private: + * static uint8_t GetType (void); + * }; + * + * Address MyAddress::ConvertTo (void) const + * { + * return Address (GetType (), m_buffer, 2); + * } + * MyAddress MyAddress::ConvertFrom (const Address &address) + * { + * MyAddress ad; + * NS_ASSERT (address.CheckCompatible (GetType (), 2)); + * address.CopyTo (ad.m_buffer, 2); + * return ad; + * } + * uint8_t MyAddress::GetType (void) + * { + * static uint8_t type = Address::Register (); + * return type; + * } + * \endcode + */ class Address { public: enum { + /** + * The maximum size of a byte buffer which + * can be stored in an Address instance. + */ MAX_SIZE = 14 }; + /** + * Create an invalid address + */ Address (); + /** + * \param type the type of the Address to create + * \param buffer a pointer to a buffer of bytes which hold + * a serialized representation of the address in network + * byte order. + * \param len the length of the buffer. + * + * Create an address from a type and a buffer. This constructor + * is typically invoked from the conversion functions of various + * address types when they have to convert themselves to an + * Address instance. + */ Address (uint8_t type, const uint8_t *buffer, uint8_t len); Address (const Address & address); Address &operator = (const Address &address); + /** + * \returns the length of the underlying address. + */ uint8_t GetLength (void) const; - void CopyTo (uint8_t *buffer) const; + /** + * \param buffer buffer to copy the address bytes to. + */ + void CopyTo (uint8_t buffer[MAX_SIZE]) const; + /** + * \param buffer pointer to a buffer of bytes which contain + * a serialized representation of the address in network + * byte order. + * \param len length of buffer + * + * Copy the input buffer to the internal buffer of this address + * instance. + */ void CopyFrom (uint8_t *buffer, uint8_t len); + /** + * \param type a type id as returned by Address::Register + * \param len the length associated to this type id. + * + * \returns true if the type of the address stored internally + * is compatible with the requested type, false otherwise. + */ bool CheckCompatible (uint8_t type, uint8_t len) const; - - + /** + * Allocate a new type id for a new type of address. + * \returns a new type id. + */ static uint8_t Register (void); private: friend bool operator == (const Address &a, const Address &b); diff --git a/src/node/eui48-address.h b/src/node/eui48-address.h index b051b4c31..d113503db 100644 --- a/src/node/eui48-address.h +++ b/src/node/eui48-address.h @@ -7,13 +7,37 @@ namespace ns3 { class Address; +/** + * \brief an EUI-48 address + * + * This class can contain 48 bit IEEE addresses. + */ class Eui48Address { public: Eui48Address (); + /** + * \param str a string representing the new Eui48Address + * + * The format of the string is "xx:xx:xx:xx:xx:xx" + */ Eui48Address (const char *str); + /** + * \returns a new Address instance + * + * Convert an instance of this class to a polymorphic Address instance. + */ Address ConvertTo (void) const; + /** + * \param address a polymorphic address + * + * Convert a polymorphic address to an Eui48Address instance. + * The conversion performs a type check. + */ static Eui48Address ConvertFrom (const Address &address); + /** + * Allocate a new Eui48Address. + */ static Eui48Address Allocate (void); private: static uint8_t GetType (void); diff --git a/src/node/inet-address.h b/src/node/inet-address.h index 6ba6da5de..577974325 100644 --- a/src/node/inet-address.h +++ b/src/node/inet-address.h @@ -7,21 +7,73 @@ namespace ns3 { + +/** + * \brief an Inet address class + * + * This class is similar to inet_sockaddr in the BSD socket + * API. i.e., this class holds an Ipv4Address and a port number + * to form an ipv4 transport endpoint. + */ class InetAddress { public: + /** + * \param ipv4 the ipv4 address + * \param port the port number + */ InetAddress (Ipv4Address ipv4, uint16_t port); + /** + * \param ipv4 the ipv4 address + * + * The port number is set to zero by default. + */ InetAddress (Ipv4Address ipv4); + /** + * \param port the port number + * + * The ipv4 address is set to the "Any" address by default. + */ InetAddress (uint16_t port); + /** + * \param ipv4 string which represents an ipv4 address + * \param port the port number + */ InetAddress (const char *ipv4, uint16_t port); + /** + * \param ipv4 string which represents an ipv4 address + * + * The port number is set to zero. + */ InetAddress (const char *ipv4); + /** + * \returns the port number + */ uint16_t GetPort (void) const; + /** + * \returns the ipv4 address + */ Ipv4Address GetIpv4 (void) const; - void SetPort (uint16_t post); + /** + * \param port the new port number. + */ + void SetPort (uint16_t port); + /** + * \param address the new ipv4 address + */ void SetIpv4 (Ipv4Address address); + /** + * \returns an Address instance which represents this + * InetAddress instance. + */ Address ConvertTo (void) const; + /** + * \param address the Address instance to convert from. + * \returns an InetAddress which corresponds to the input + * Address + */ static InetAddress ConvertFrom (const Address &address); private: static uint8_t GetType (void); From c80acdf88f8f77fc3bb72ce2326b925ac9799a37 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 10:35:40 +0200 Subject: [PATCH 09/48] remove dead files after merge --- src/devices/p2p/p2p-net-device.cc | 222 ---------------------- src/devices/p2p/p2p-net-device.h | 296 ------------------------------ 2 files changed, 518 deletions(-) delete mode 100644 src/devices/p2p/p2p-net-device.cc delete mode 100644 src/devices/p2p/p2p-net-device.h diff --git a/src/devices/p2p/p2p-net-device.cc b/src/devices/p2p/p2p-net-device.cc deleted file mode 100644 index 3e0596d48..000000000 --- a/src/devices/p2p/p2p-net-device.cc +++ /dev/null @@ -1,222 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2005,2006 INRIA - * All rights reserved. - * - * 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 - * - * Author: Craig Dowell - * Revised: George Riley - */ - -#include -#include -#include "ns3/debug.h" -#include "ns3/queue.h" -#include "ns3/simulator.h" -#include "ns3/composite-trace-resolver.h" -#include "ns3/eui48-address.h" -#include "p2p-net-device.h" -#include "p2p-channel.h" - -NS_DEBUG_COMPONENT_DEFINE ("PointToPointNetDevice"); - -namespace ns3 { - -DataRateDefaultValue PointToPointNetDevice::g_defaultRate( - "PointToPointLinkDataRate", - "The default data rate for point to point links", - DataRate ("10Mb/s")); - -PointToPointNetDevice::PointToPointNetDevice (Ptr node, - const DataRate& rate) -: - NetDevice(node, Eui48Address::Allocate ().ConvertTo ()), - m_txMachineState (READY), - m_bps (rate), - m_tInterframeGap (Seconds(0)), - m_channel (0), - m_queue (0), - m_rxTrace () -{ - NS_DEBUG ("PointToPointNetDevice::PointToPointNetDevice (" << node << ")"); - - // BUGBUG FIXME - // - // You _must_ support broadcast to get any sort of packet from the ARP layer. - EnableBroadcast (Eui48Address ("ff:ff:ff:ff:ff:ff").ConvertTo ()); - EnableMulticast(); - EnablePointToPoint(); -} - -PointToPointNetDevice::~PointToPointNetDevice() -{ - NS_DEBUG ("PointToPointNetDevice::~PointToPointNetDevice ()"); - m_queue = 0; -} - -void PointToPointNetDevice::DoDispose() -{ - m_channel = 0; - NetDevice::DoDispose (); -} - - -void PointToPointNetDevice::SetDataRate(const DataRate& bps) -{ - m_bps = bps; -} - -void PointToPointNetDevice::SetInterframeGap(const Time& t) -{ - m_tInterframeGap = t; -} - -bool PointToPointNetDevice::SendTo (Packet& p, const Address& dest) -{ - NS_DEBUG ("PointToPointNetDevice::SendTo (" << &p << ", " << &dest << ")"); - NS_DEBUG ("PointToPointNetDevice::SendTo (): UID is " << p.GetUid () << ")"); - - // GFR Comment. Why is this an assertion? Can't a link legitimately - // "go down" during the simulation? Shouldn't we just wait for it - // to come back up? - NS_ASSERT (IsLinkUp ()); - -// -// This class simulates a point to point device. In the case of a serial -// link, this means that we're simulating something like a UART. -// -// -// If there's a transmission in progress, we enque the packet for later -// trnsmission; otherwise we send it now. - if (m_txMachineState == READY) - { - return TransmitStart (p); - } - else - { - return m_queue->Enqueue(p); - } -} - - bool -PointToPointNetDevice::TransmitStart (Packet &p) -{ - NS_DEBUG ("PointToPointNetDevice::TransmitStart (" << &p << ")"); - NS_DEBUG ( - "PointToPointNetDevice::TransmitStart (): UID is " << p.GetUid () << ")"); -// -// This function is called to start the process of transmitting a packet. -// We need to tell the channel that we've started wiggling the wire and -// schedule an event that will be executed when the transmission is complete. -// - NS_ASSERT_MSG(m_txMachineState == READY, "Must be READY to transmit"); - m_txMachineState = BUSY; - Time txTime = Seconds (m_bps.CalculateTxTime(p.GetSize())); - Time txCompleteTime = txTime + m_tInterframeGap; - - NS_DEBUG ("PointToPointNetDevice::TransmitStart (): " << - "Schedule TransmitCompleteEvent in " << - txCompleteTime.GetSeconds () << "sec"); - // Schedule the tx complete event - Simulator::Schedule (txCompleteTime, - &PointToPointNetDevice::TransmitComplete, - this); - return m_channel->TransmitStart(p, this, txTime); -} - -void PointToPointNetDevice::TransmitComplete (void) -{ - NS_DEBUG ("PointToPointNetDevice::TransmitCompleteEvent ()"); -// -// This function is called to finish the process of transmitting a packet. -// We need to tell the channel that we've stopped wiggling the wire and -// get the next packet from the queue. If the queue is empty, we are -// done, otherwise transmit the next packet. -// - NS_ASSERT_MSG(m_txMachineState == BUSY, "Must be BUSY if transmitting"); - m_txMachineState = READY; - Packet p; - if (!m_queue->Dequeue(p)) return; // Nothing to do at this point - TransmitStart(p); -} - -TraceResolver* PointToPointNetDevice::DoCreateTraceResolver ( - TraceContext const &context) -{ - CompositeTraceResolver *resolver = new CompositeTraceResolver (context); - resolver->Add ("queue", - MakeCallback (&Queue::CreateTraceResolver, PeekPointer (m_queue)), - PointToPointNetDevice::QUEUE); - resolver->Add ("rx", - m_rxTrace, - PointToPointNetDevice::RX); - return resolver; -} - -bool PointToPointNetDevice::Attach (Ptr ch) -{ - NS_DEBUG ("PointToPointNetDevice::Attach (" << &ch << ")"); - - m_channel = ch; - - m_channel->Attach(this); - m_bps = m_channel->GetDataRate (); - // GFR Comment. Below is definitely wrong. Interframe gap - // is unrelated to channel delay. - //m_tInterframeGap = m_channel->GetDelay (); - - /* - * For now, this device is up whenever a channel is attached to it. - * In fact, it should become up only when the second device - * is attached to the channel. So, there should be a way for - * a PointToPointChannel to notify both of its attached devices - * that the channel is 'complete', hence that the devices are - * up, hence that they can call NotifyLinkUp. - */ - NotifyLinkUp (); - return true; -} - -void PointToPointNetDevice::AddQueue (Ptr q) -{ - NS_DEBUG ("PointToPointNetDevice::AddQueue (" << q << ")"); - - m_queue = q; -} - -void PointToPointNetDevice::Receive (Packet& p) -{ - NS_DEBUG ("PointToPointNetDevice::Receive (" << &p << ")"); - - m_rxTrace (p); - ForwardUp (p); -} - -Ptr PointToPointNetDevice::GetQueue(void) const -{ - return m_queue; -} - -Ptr PointToPointNetDevice::DoGetChannel(void) const -{ - return m_channel; -} - -bool PointToPointNetDevice::DoNeedsArp (void) const -{ - return false; -} - -} // namespace ns3 diff --git a/src/devices/p2p/p2p-net-device.h b/src/devices/p2p/p2p-net-device.h deleted file mode 100644 index 474a6383c..000000000 --- a/src/devices/p2p/p2p-net-device.h +++ /dev/null @@ -1,296 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 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 - * - * Author: Craig Dowell - */ - -#ifndef POINT_TO_POINT_NET_DEVICE_H -#define POINT_TO_POINT_NET_DEVICE_H - -#include -#include "ns3/address.h" -#include "ns3/node.h" -#include "ns3/net-device.h" -#include "ns3/callback.h" -#include "ns3/packet.h" -#include "ns3/callback-trace-source.h" -#include "ns3/nstime.h" -#include "ns3/data-rate.h" -#include "ns3/default-value.h" -#include "ns3/ptr.h" - -namespace ns3 { - -class Queue; -class PointToPointChannel; - -/** - * \class PointToPointNetDevice - * \brief A Device for a Point to Point Network Link. - * - * Ns-3 takes a four-layer view of a protocol stack. This is the same model - * that TCP uses. In this view, layers 5-7 of the OSI reference model are - * grouped together into an application layer; layer four (transport / TCP) is - * broken out; layer three (network / IP) is broken out; and layers 1-2 are - * grouped together. We call this grouping of layers one and two a NetDevice - * and represent it as a class in the system. - * - * The NetDevice class is specialized according to the needs of the specific - * kind of network link. In this case, the link is a PointToPoint link. The - * PointToPoint link is a family of classes that includes this class, the - * PointToPointNetDevice, a PointToPointChannel class that represents the - * actual medium across which bits are sent, a PointToPointIpv4Interface class - * that provides the hook to tie a general purpose node to this specific - * link, and finally, a PointToPointTopology object that is responsible for - * putting all of the pieces together. - * - * This is the PointToPointNetDevice class that represents, essentially, the - * PC card that is used to connect to the PointToPoint network. - */ -class PointToPointNetDevice : public NetDevice { -public: - /** - * Enumeration of the types of traces supported in the class. - * - */ - enum TraceType { - QUEUE, /**< Trace queue events on the attached queue */ - RX, /**< Trace packet reception events (from the channel) */ - }; - /** - * Construct a PointToPointNetDevice - * - * This is the constructor for the PointToPointNetDevice. It takes as a - * parameter the Node to which this device is connected. Ownership of the - * Node pointer is not implied and the node must not be deleded. - * - * @see PointToPointTopology::AddPointToPointLink () - * @param node the Node to which this device is connected. - */ - PointToPointNetDevice (Ptr node, - const DataRate& = g_defaultRate.GetValue()); - /** - * Destroy a PointToPointNetDevice - * - * This is the destructor for the PointToPointNetDevice. - */ - virtual ~PointToPointNetDevice(); - /** - * Set the Data Rate used for transmission of packets. The data rate is - * set in the Attach () method from the corresponding field in the channel - * to which the device is attached. It can be overridden using this method. - * - * @see Attach () - * @param bps the data rate at which this object operates - */ - void SetDataRate(const DataRate& bps); - /** - * Set the inteframe gap used to separate packets. The interframe gap - * defines the minimum space required between packets sent by this device. - * It is usually set in the Attach () method based on the speed of light - * delay of the channel to which the device is attached. It can be - * overridden using this method if desired. - * - * @see Attach () - * @param t the interframe gap time - */ - void SetInterframeGap(const Time& t); - /** - * Attach the device to a channel. - * - * The PointToPointTopology object creates a PointToPointChannel and two - * PointtoPointNetDevices. In order to introduce these components to each - * other, the topology object calls Attach () on each PointToPointNetDevice. - * Inside this method, the Net Device calls out to the PointToPointChannel - * to introduce itself. - * - * @see PointToPointTopology::AddPointToPointLink () - * @see SetDataRate () - * @see SetInterframeGap () - * @param ch a pointer to the channel to which this object is being attached. - */ - bool Attach(Ptr ch); - /** - * Attach a queue to the PointToPointNetDevice. - * - * The PointToPointNetDevice "owns" a queue. This queue is created by the - * PointToPointTopology object and implements a queueing method such as - * DropTail or RED. The PointToPointNetDevice assumes ownership of this - * queue and must delete it when the device is destroyed. - * - * @see PointToPointTopology::AddPointToPointLink () - * @see Queue - * @see DropTailQueue - * @param queue a pointer to the queue for which object is assuming - * ownership. - */ - void AddQueue(Ptr queue); - /** - * Receive a packet from a connected PointToPointChannel. - * - * The PointToPointNetDevice receives packets from its connected channel - * and forwards them up the protocol stack. This is the public method - * used by the channel to indicate that the last bit of a packet has - * arrived at the device. - * - * @see PointToPointChannel - * @param p a reference to the received packet - */ - void Receive (Packet& p); -protected: - virtual void DoDispose (void); - /** - * Get a copy of the attached Queue. - * - * This method is provided for any derived class that may need to get - * direct access to the underlying queue. - * - * @see PointToPointTopology - * @returns a pointer to the queue. - */ - Ptr GetQueue(void) const; - /** - * Get a copy of the attached Channel - * - * This method is provided for any derived class that may need to get - * direct access to the connected channel - * - * @see PointToPointChannel - * @returns a pointer to the channel - */ - virtual Ptr DoGetChannel(void) const; - /** - * Set a new default data rate - * @param Data rate to set for new default - */ - static void SetDefaultRate(const DataRate&); - - /** - * Get the current default rate. - * @returns a const reference to current default - */ - - static const DataRate& GetDefaultRate(); - -private: - // unimplemented methods to make it impossible - // to copy these objects. - PointToPointNetDevice (const PointToPointNetDevice& nd); - PointToPointNetDevice& operator = (const PointToPointNetDevice&o); - - /** - * Send a Packet Down the Wire. - * - * The SendTo method is defined as the standard way that the level three - * protocol uses to tell a NetDevice to send a packet. SendTo is declared - * as abstract in the NetDevice class and we declare it here. - * - * @see NetDevice - * @param p a reference to the packet to send - * @param dest a reference to the Address of the destination device - * @returns true if success, false on failure - */ - virtual bool SendTo (Packet& p, const Address& dest); - /** - * Start Sending a Packet Down the Wire. - * - * The TransmitStart method is the method that is used internally in the - * PointToPointNetDevice to begin the process of sending a packet out on - * the channel. The corresponding method is called on the channel to let - * it know that the physical device this class represents has virually - * started sending signals. An event is scheduled for the time at which - * the bits have been completely transmitted. - * - * @see PointToPointChannel::TransmitStart () - * @see TransmitCompleteEvent () - * @param p a reference to the packet to send - * @returns true if success, false on failure - */ - bool TransmitStart (Packet &p); - /** - * Stop Sending a Packet Down the Wire and Begin the Interframe Gap. - * - * The TransmitComplete method is used internally to finish the process - * of sending a packet out on the channel. - * - */ - void TransmitComplete(void); - /** - * Create a Trace Resolver for events in the net device. - * - * @see class TraceResolver - */ - virtual TraceResolver* DoCreateTraceResolver (TraceContext const &context); - virtual bool DoNeedsArp (void) const; - /** - * Enumeration of the states of the transmit machine of the net device. - */ - enum TxMachineState - { - READY, /**< The transmitter is ready to begin transmission of a packet */ - BUSY /**< The transmitter is busy transmitting a packet */ - }; - /** - * The state of the Net Device transmit state machine. - * @see TxMachineState - */ - TxMachineState m_txMachineState; - /** - * The data rate that the Net Device uses to simulate packet transmission - * timing. - * @see class DataRate - */ - DataRate m_bps; - /** - * The interframe gap that the Net Device uses to throttle packet - * transmission - * @see class Time - */ - Time m_tInterframeGap; - /** - * The PointToPointChannel to which this PointToPointNetDevice has been - * attached. - * @see class PointToPointChannel - */ - Ptr m_channel; - /** - * The Queue which this PointToPointNetDevice uses as a packet source. - * Management of this Queue has been delegated to the PointToPointNetDevice - * and it has the responsibility for deletion. - * @see class Queue - * @see class DropTailQueue - */ - Ptr m_queue; - /** - * The trace source for the packet reception events that the device can - * fire. - * - * @see class CallBackTraceSource - * @see class TraceResolver - */ - CallbackTraceSource m_rxTrace; - /** - * Default data rate. Used for all newly created p2p net devices - */ - static DataRateDefaultValue g_defaultRate; - -}; - -}; // namespace ns3 - -#endif // POINT_TO_POINT_NET_DEVICE_H - From 034469197cfb5727b7bd169c301b693e21d3fa5c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 11:03:13 +0200 Subject: [PATCH 10/48] fix address deserialization --- src/node/address.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/node/address.cc b/src/node/address.cc index 9bb67f4fb..d73f5790e 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -55,8 +55,9 @@ Address::CopyTo (uint8_t buffer[MAX_SIZE]) const void Address::CopyFrom (uint8_t *buffer, uint8_t len) { - NS_ASSERT (len == m_len); + NS_ASSERT (len <= MAX_SIZE); memcpy (m_data, buffer, len); + m_len = len; } bool Address::CheckCompatible (uint8_t type, uint8_t len) const From 41074331ed92b3f651b0adfeee852f43bebfd99c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 11:07:56 +0200 Subject: [PATCH 11/48] fix optimized build --- src/node/eui48-address.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index a5944d146..d83137e02 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -2,6 +2,7 @@ #include "address.h" #include "ns3/assert.h" #include +#include namespace ns3 { From df7f115415d5a2c2f4a434a2333fc6c57ea835ca Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 13:29:36 +0200 Subject: [PATCH 12/48] InetAddress -> InetSocketAddress --- examples/csma-cd-one-subnet.cc | 6 ++-- examples/simple-p2p.cc | 6 ++-- examples/simple-point-to-point.cc | 6 ++-- samples/main-simple.cc | 6 ++-- src/internet-node/udp-socket.cc | 12 ++++---- ...inet-address.cc => inet-socket-address.cc} | 30 +++++++++---------- .../{inet-address.h => inet-socket-address.h} | 18 +++++------ src/node/wscript | 4 +-- 8 files changed, 44 insertions(+), 44 deletions(-) rename src/node/{inet-address.cc => inet-socket-address.cc} (54%) rename src/node/{inet-address.h => inet-socket-address.h} (78%) diff --git a/examples/csma-cd-one-subnet.cc b/examples/csma-cd-one-subnet.cc index f93337823..ceaf88810 100644 --- a/examples/csma-cd-one-subnet.cc +++ b/examples/csma-cd-one-subnet.cc @@ -52,7 +52,7 @@ #include "ns3/csma-cd-ipv4-topology.h" #include "ns3/eui48-address.h" #include "ns3/ipv4-address.h" -#include "ns3/inet-address.h" +#include "ns3/inet-socket-address.h" #include "ns3/ipv4.h" #include "ns3/socket.h" #include "ns3/ipv4-route.h" @@ -132,7 +132,7 @@ int main (int argc, char *argv[]) // from n0 to n1 Ptr ooff = Create ( n0, - InetAddress (Ipv4Address("10.1.1.2"), 80).ConvertTo (), + InetSocketAddress (Ipv4Address("10.1.1.2"), 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); @@ -143,7 +143,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n0, starting at time 1.1 seconds ooff = Create ( n3, - InetAddress (Ipv4Address("10.1.1.1"), 80).ConvertTo (), + InetSocketAddress (Ipv4Address("10.1.1.1"), 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index 5d1bb545e..d06ef329f 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -57,7 +57,7 @@ #include "ns3/p2p-channel.h" #include "ns3/p2p-net-device.h" #include "ns3/ipv4-address.h" -#include "ns3/inet-address.h" +#include "ns3/inet-socket-address.h" #include "ns3/ipv4.h" #include "ns3/socket.h" #include "ns3/ipv4-route.h" @@ -143,7 +143,7 @@ int main (int argc, char *argv[]) // 210 bytes at a rate of 448 Kb/s Ptr ooff = Create ( n0, - InetAddress("10.1.3.2", 80).ConvertTo (), + InetSocketAddress("10.1.3.2", 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); @@ -154,7 +154,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, - InetAddress("10.1.2.1", 80).ConvertTo (), + InetSocketAddress("10.1.2.1", 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); diff --git a/examples/simple-point-to-point.cc b/examples/simple-point-to-point.cc index 2398cc22b..806c4d120 100644 --- a/examples/simple-point-to-point.cc +++ b/examples/simple-point-to-point.cc @@ -58,7 +58,7 @@ #include "ns3/point-to-point-channel.h" #include "ns3/point-to-point-net-device.h" #include "ns3/ipv4-address.h" -#include "ns3/inet-address.h" +#include "ns3/inet-socket-address.h" #include "ns3/ipv4.h" #include "ns3/socket.h" #include "ns3/ipv4-route.h" @@ -144,7 +144,7 @@ int main (int argc, char *argv[]) // 210 bytes at a rate of 448 Kb/s Ptr ooff = Create ( n0, - InetAddress (Ipv4Address("10.1.3.2"), 80).ConvertTo (), + InetSocketAddress (Ipv4Address("10.1.3.2"), 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); @@ -155,7 +155,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, - InetAddress (Ipv4Address("10.1.2.1"), 80).ConvertTo (), + InetSocketAddress (Ipv4Address("10.1.2.1"), 80).ConvertTo (), "Udp", ConstantVariable(1), ConstantVariable(0)); diff --git a/samples/main-simple.cc b/samples/main-simple.cc index 4c709b4c5..e3567e0b1 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -4,7 +4,7 @@ #include "ns3/simulator.h" #include "ns3/socket-factory.h" #include "ns3/socket.h" -#include "ns3/inet-address.h" +#include "ns3/inet-socket-address.h" #include "ns3/nstime.h" using namespace ns3; @@ -45,11 +45,11 @@ RunSimulation (void) Ptr socketFactory = a->QueryInterface (iid); Ptr sink = socketFactory->CreateSocket (); - InetAddress local = InetAddress (Ipv4Address::GetAny (), 80); + InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80); sink->Bind (local.ConvertTo ()); Ptr source = socketFactory->CreateSocket (); - InetAddress remote = InetAddress (Ipv4Address::GetLoopback (), 80); + InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetLoopback (), 80); source->Connect (remote.ConvertTo ()); GenerateTraffic (source, 500); diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 9ae438f6b..020076cb5 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -19,7 +19,7 @@ * Author: Mathieu Lacage */ #include "ns3/node.h" -#include "ns3/inet-address.h" +#include "ns3/inet-socket-address.h" #include "udp-socket.h" #include "udp-l4-protocol.h" #include "ipv4-end-point.h" @@ -91,7 +91,7 @@ UdpSocket::Bind (void) int UdpSocket::Bind (const Address &address) { - InetAddress transport = InetAddress::ConvertFrom (address); + InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); if (ipv4 == Ipv4Address::GetAny () && port == 0) @@ -147,7 +147,7 @@ UdpSocket::DoConnect(const Address & address, ns3::Callback > connectionFailed, ns3::Callback > halfClose) { - InetAddress transport = InetAddress::ConvertFrom (address); + InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); m_defaultAddress = transport.GetIpv4 (); m_defaultPort = transport.GetPort (); if (!connectionSucceeded.IsNull ()) @@ -190,7 +190,7 @@ int UdpSocket::DoSendPacketTo (const Packet &p, const Address &address, ns3::Callback, uint32_t> dataSent) { - InetAddress transport = InetAddress::ConvertFrom (address); + InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); return DoSendPacketTo (p, ipv4, port, dataSent); @@ -241,7 +241,7 @@ UdpSocket::DoSendTo(const Address &address, { p = Packet (buffer, size); } - InetAddress transport = InetAddress::ConvertFrom (address); + InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); return DoSendPacketTo (p, ipv4, port, dataSent); @@ -265,7 +265,7 @@ UdpSocket::ForwardUp (const Packet &packet, Ipv4Address ipv4, uint16_t port) return; } - Address address = InetAddress (ipv4, port).ConvertTo (); + Address address = InetSocketAddress (ipv4, port).ConvertTo (); Packet p = packet; if (!m_dummyRxCallback.IsNull ()) { diff --git a/src/node/inet-address.cc b/src/node/inet-socket-address.cc similarity index 54% rename from src/node/inet-address.cc rename to src/node/inet-socket-address.cc index bf82cfc83..b8d0e10cf 100644 --- a/src/node/inet-address.cc +++ b/src/node/inet-socket-address.cc @@ -1,52 +1,52 @@ -#include "inet-address.h" +#include "inet-socket-address.h" #include "ns3/assert.h" namespace ns3 { -InetAddress::InetAddress (Ipv4Address ipv4, uint16_t port) +InetSocketAddress::InetSocketAddress (Ipv4Address ipv4, uint16_t port) : m_ipv4 (ipv4), m_port (port) {} -InetAddress::InetAddress (Ipv4Address ipv4) +InetSocketAddress::InetSocketAddress (Ipv4Address ipv4) : m_ipv4 (ipv4), m_port (0) {} -InetAddress::InetAddress (const char *ipv4, uint16_t port) +InetSocketAddress::InetSocketAddress (const char *ipv4, uint16_t port) : m_ipv4 (Ipv4Address (ipv4)), m_port (port) {} -InetAddress::InetAddress (const char * ipv4) +InetSocketAddress::InetSocketAddress (const char * ipv4) : m_ipv4 (Ipv4Address (ipv4)), m_port (0) {} -InetAddress::InetAddress (uint16_t port) +InetSocketAddress::InetSocketAddress (uint16_t port) : m_ipv4 (Ipv4Address::GetAny ()), m_port (port) {} uint16_t -InetAddress::GetPort (void) const +InetSocketAddress::GetPort (void) const { return m_port; } Ipv4Address -InetAddress::GetIpv4 (void) const +InetSocketAddress::GetIpv4 (void) const { return m_ipv4; } void -InetAddress::SetPort (uint16_t port) +InetSocketAddress::SetPort (uint16_t port) { m_port = port; } void -InetAddress::SetIpv4 (Ipv4Address address) +InetSocketAddress::SetIpv4 (Ipv4Address address) { m_ipv4 = address; } Address -InetAddress::ConvertTo (void) const +InetSocketAddress::ConvertTo (void) const { uint8_t buf[6]; m_ipv4.Serialize (buf); @@ -54,18 +54,18 @@ InetAddress::ConvertTo (void) const buf[5] = (m_port >> 8) & 0xff; return Address (GetType (), buf, 6); } -InetAddress -InetAddress::ConvertFrom (const Address &address) +InetSocketAddress +InetSocketAddress::ConvertFrom (const Address &address) { NS_ASSERT (address.CheckCompatible (GetType (), 6)); uint8_t buf[6]; address.CopyTo (buf); Ipv4Address ipv4 = Ipv4Address::Deserialize (buf); uint16_t port = buf[0] | (buf[1] << 8); - return InetAddress (ipv4, port); + return InetSocketAddress (ipv4, port); } uint8_t -InetAddress::GetType (void) +InetSocketAddress::GetType (void) { static uint8_t type = Address::Register (); return type; diff --git a/src/node/inet-address.h b/src/node/inet-socket-address.h similarity index 78% rename from src/node/inet-address.h rename to src/node/inet-socket-address.h index 577974325..35fe52863 100644 --- a/src/node/inet-address.h +++ b/src/node/inet-socket-address.h @@ -15,37 +15,37 @@ namespace ns3 { * API. i.e., this class holds an Ipv4Address and a port number * to form an ipv4 transport endpoint. */ -class InetAddress +class InetSocketAddress { public: /** * \param ipv4 the ipv4 address * \param port the port number */ - InetAddress (Ipv4Address ipv4, uint16_t port); + InetSocketAddress (Ipv4Address ipv4, uint16_t port); /** * \param ipv4 the ipv4 address * * The port number is set to zero by default. */ - InetAddress (Ipv4Address ipv4); + InetSocketAddress (Ipv4Address ipv4); /** * \param port the port number * * The ipv4 address is set to the "Any" address by default. */ - InetAddress (uint16_t port); + InetSocketAddress (uint16_t port); /** * \param ipv4 string which represents an ipv4 address * \param port the port number */ - InetAddress (const char *ipv4, uint16_t port); + InetSocketAddress (const char *ipv4, uint16_t port); /** * \param ipv4 string which represents an ipv4 address * * The port number is set to zero. */ - InetAddress (const char *ipv4); + InetSocketAddress (const char *ipv4); /** * \returns the port number */ @@ -66,15 +66,15 @@ public: /** * \returns an Address instance which represents this - * InetAddress instance. + * InetSocketAddress instance. */ Address ConvertTo (void) const; /** * \param address the Address instance to convert from. - * \returns an InetAddress which corresponds to the input + * \returns an InetSocketAddress which corresponds to the input * Address */ - static InetAddress ConvertFrom (const Address &address); + static InetSocketAddress ConvertFrom (const Address &address); private: static uint8_t GetType (void); Ipv4Address m_ipv4; diff --git a/src/node/wscript b/src/node/wscript index 47f5b09a2..9cc3d3daf 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -8,7 +8,7 @@ def build(bld): node.source = [ 'address.cc', 'eui48-address.cc', - 'inet-address.cc', + 'inet-socket-address.cc', 'node.cc', 'ipv4-address.cc', 'net-device.cc', @@ -32,7 +32,7 @@ def build(bld): headers.source = [ 'address.h', 'eui48-address.h', - 'inet-address.h', + 'inet-socket-address.h', 'node.h', 'ipv4-address.h', 'net-device.h', From f2084098e2270e67821ecdc2a4567fc700a52541 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 13:55:28 +0200 Subject: [PATCH 13/48] add InetSocketAddress::IsMatchingType and use it --- src/internet-node/udp-socket.cc | 4 ++++ src/node/inet-socket-address.cc | 6 ++++++ src/node/inet-socket-address.h | 2 ++ src/node/socket.h | 1 + 4 files changed, 13 insertions(+) diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 020076cb5..bc83c942c 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -91,6 +91,10 @@ UdpSocket::Bind (void) int UdpSocket::Bind (const Address &address) { + if (!InetSocketAddress::IsMatchingType (address)) + { + return ERROR_INVAL; + } InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); diff --git a/src/node/inet-socket-address.cc b/src/node/inet-socket-address.cc index b8d0e10cf..232e9215f 100644 --- a/src/node/inet-socket-address.cc +++ b/src/node/inet-socket-address.cc @@ -45,6 +45,12 @@ InetSocketAddress::SetIpv4 (Ipv4Address address) m_ipv4 = address; } +bool +InetSocketAddress::IsMatchingType (const Address &address) +{ + return address.CheckCompatible (GetType (), 6); +} + Address InetSocketAddress::ConvertTo (void) const { diff --git a/src/node/inet-socket-address.h b/src/node/inet-socket-address.h index 35fe52863..bb91d0ff5 100644 --- a/src/node/inet-socket-address.h +++ b/src/node/inet-socket-address.h @@ -64,6 +64,8 @@ public: */ void SetIpv4 (Ipv4Address address); + static bool IsMatchingType (const Address &address); + /** * \returns an Address instance which represents this * InetSocketAddress instance. diff --git a/src/node/socket.h b/src/node/socket.h index 3cba7fd59..547ed4320 100644 --- a/src/node/socket.h +++ b/src/node/socket.h @@ -52,6 +52,7 @@ public: ERROR_AGAIN, ERROR_SHUTDOWN, ERROR_OPNOTSUPP, + ERROR_INVAL, SOCKET_ERRNO_LAST }; From 22cf3afdfa07fc0495249f6aee1f555f04561fc5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 13:58:39 +0200 Subject: [PATCH 14/48] add Ipv4Address::IsMatchingType and Eui48Address::IsMatchingType --- src/node/eui48-address.cc | 5 +++++ src/node/eui48-address.h | 4 ++++ src/node/ipv4-address.cc | 5 +++++ src/node/ipv4-address.h | 1 + 4 files changed, 15 insertions(+) diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index d83137e02..8196d7ba3 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -71,6 +71,11 @@ Eui48Address::CopyTo (uint8_t buffer[6]) const memcpy (buffer, m_address, 6); } +bool +Eui48Address::IsMatchingType (const Address &address) +{ + return address.CheckCompatible (GetType (), 6); +} Address Eui48Address::ConvertTo (void) const { diff --git a/src/node/eui48-address.h b/src/node/eui48-address.h index ad045f078..1d6c37da1 100644 --- a/src/node/eui48-address.h +++ b/src/node/eui48-address.h @@ -42,6 +42,10 @@ public: * Convert an instance of this class to a polymorphic Address instance. */ Address ConvertTo (void) const; + /** + * \returns true if the address matches, false otherwise. + */ + static bool IsMatchingType (const Address &address); /** * \param address a polymorphic address * diff --git a/src/node/ipv4-address.cc b/src/node/ipv4-address.cc index 67028125d..aae53c65f 100644 --- a/src/node/ipv4-address.cc +++ b/src/node/ipv4-address.cc @@ -206,6 +206,11 @@ Ipv4Address::Print (std::ostream &os) const << ((m_address >> 0) & 0xff); } +bool +Ipv4Address::IsMatchingType (const Address &address) +{ + return address.CheckCompatible (GetType (), 4); +} Address Ipv4Address::ConvertTo (void) const { diff --git a/src/node/ipv4-address.h b/src/node/ipv4-address.h index 9911d24c4..8e8976a18 100644 --- a/src/node/ipv4-address.h +++ b/src/node/ipv4-address.h @@ -104,6 +104,7 @@ public: */ Ipv4Address CombineMask (Ipv4Mask const &mask) const; + static bool IsMatchingType (const Address &address); Address ConvertTo (void) const; static Ipv4Address ConvertFrom (const Address &address); From 80bf1491e122cb3d0bda6ede5dde6186526387d7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 13:59:13 +0200 Subject: [PATCH 15/48] add doxygen --- src/node/inet-socket-address.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/node/inet-socket-address.h b/src/node/inet-socket-address.h index bb91d0ff5..6734f1496 100644 --- a/src/node/inet-socket-address.h +++ b/src/node/inet-socket-address.h @@ -64,6 +64,9 @@ public: */ void SetIpv4 (Ipv4Address address); + /** + * \returns true if the address matches, false otherwise. + */ static bool IsMatchingType (const Address &address); /** From 2641f7fe88c7ad5d3b89851ba36a839685b0e389 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 14:07:39 +0200 Subject: [PATCH 16/48] InetSocketAddress: replace explicit conversion to implicit conversion --- examples/csma-cd-one-subnet.cc | 4 ++-- examples/simple-point-to-point.cc | 4 ++-- samples/main-simple.cc | 4 ++-- src/internet-node/udp-socket.cc | 10 +++++----- src/node/inet-socket-address.cc | 10 ++++++++++ src/node/inet-socket-address.h | 10 +++++++--- 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/examples/csma-cd-one-subnet.cc b/examples/csma-cd-one-subnet.cc index ceaf88810..83b5b2259 100644 --- a/examples/csma-cd-one-subnet.cc +++ b/examples/csma-cd-one-subnet.cc @@ -132,7 +132,7 @@ int main (int argc, char *argv[]) // from n0 to n1 Ptr ooff = Create ( n0, - InetSocketAddress (Ipv4Address("10.1.1.2"), 80).ConvertTo (), + InetSocketAddress ("10.1.1.2", 80), "Udp", ConstantVariable(1), ConstantVariable(0)); @@ -143,7 +143,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n0, starting at time 1.1 seconds ooff = Create ( n3, - InetSocketAddress (Ipv4Address("10.1.1.1"), 80).ConvertTo (), + InetSocketAddress ("10.1.1.1", 80), "Udp", ConstantVariable(1), ConstantVariable(0)); diff --git a/examples/simple-point-to-point.cc b/examples/simple-point-to-point.cc index 806c4d120..fa826ab5c 100644 --- a/examples/simple-point-to-point.cc +++ b/examples/simple-point-to-point.cc @@ -144,7 +144,7 @@ int main (int argc, char *argv[]) // 210 bytes at a rate of 448 Kb/s Ptr ooff = Create ( n0, - InetSocketAddress (Ipv4Address("10.1.3.2"), 80).ConvertTo (), + InetSocketAddress ("10.1.3.2", 80), "Udp", ConstantVariable(1), ConstantVariable(0)); @@ -155,7 +155,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, - InetSocketAddress (Ipv4Address("10.1.2.1"), 80).ConvertTo (), + InetSocketAddress ("10.1.2.1", 80), "Udp", ConstantVariable(1), ConstantVariable(0)); diff --git a/samples/main-simple.cc b/samples/main-simple.cc index e3567e0b1..3be286c46 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -46,11 +46,11 @@ RunSimulation (void) Ptr sink = socketFactory->CreateSocket (); InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80); - sink->Bind (local.ConvertTo ()); + sink->Bind (local); Ptr source = socketFactory->CreateSocket (); InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetLoopback (), 80); - source->Connect (remote.ConvertTo ()); + source->Connect (remote); GenerateTraffic (source, 500); PrintTraffic (sink); diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index bc83c942c..438e59a53 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -95,7 +95,7 @@ UdpSocket::Bind (const Address &address) { return ERROR_INVAL; } - InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); + InetSocketAddress transport = address; Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); if (ipv4 == Ipv4Address::GetAny () && port == 0) @@ -151,7 +151,7 @@ UdpSocket::DoConnect(const Address & address, ns3::Callback > connectionFailed, ns3::Callback > halfClose) { - InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); + InetSocketAddress transport = address; m_defaultAddress = transport.GetIpv4 (); m_defaultPort = transport.GetPort (); if (!connectionSucceeded.IsNull ()) @@ -194,7 +194,7 @@ int UdpSocket::DoSendPacketTo (const Packet &p, const Address &address, ns3::Callback, uint32_t> dataSent) { - InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); + InetSocketAddress transport = address; Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); return DoSendPacketTo (p, ipv4, port, dataSent); @@ -245,7 +245,7 @@ UdpSocket::DoSendTo(const Address &address, { p = Packet (buffer, size); } - InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); + InetSocketAddress transport = address; Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); return DoSendPacketTo (p, ipv4, port, dataSent); @@ -269,7 +269,7 @@ UdpSocket::ForwardUp (const Packet &packet, Ipv4Address ipv4, uint16_t port) return; } - Address address = InetSocketAddress (ipv4, port).ConvertTo (); + Address address = InetSocketAddress (ipv4, port); Packet p = packet; if (!m_dummyRxCallback.IsNull ()) { diff --git a/src/node/inet-socket-address.cc b/src/node/inet-socket-address.cc index 232e9215f..43c32dcd1 100644 --- a/src/node/inet-socket-address.cc +++ b/src/node/inet-socket-address.cc @@ -51,6 +51,16 @@ InetSocketAddress::IsMatchingType (const Address &address) return address.CheckCompatible (GetType (), 6); } +InetSocketAddress::operator Address () const +{ + return ConvertTo (); +} + +InetSocketAddress::InetSocketAddress (const Address &address) +{ + *this = ConvertFrom (address); +} + Address InetSocketAddress::ConvertTo (void) const { diff --git a/src/node/inet-socket-address.h b/src/node/inet-socket-address.h index 6734f1496..c09a6c761 100644 --- a/src/node/inet-socket-address.h +++ b/src/node/inet-socket-address.h @@ -73,14 +73,18 @@ public: * \returns an Address instance which represents this * InetSocketAddress instance. */ - Address ConvertTo (void) const; + operator Address () const; + /** * \param address the Address instance to convert from. - * \returns an InetSocketAddress which corresponds to the input + * + * Construct an InetSocketAddress which corresponds to the input * Address */ - static InetSocketAddress ConvertFrom (const Address &address); + InetSocketAddress (const Address &address); private: + Address ConvertTo (void) const; + static InetSocketAddress ConvertFrom (const Address &address); static uint8_t GetType (void); Ipv4Address m_ipv4; uint16_t m_port; From 2e723a700d126db76e9109b269236af6ce1a60f5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 14:17:12 +0200 Subject: [PATCH 17/48] add implicit conversion to Eui48Address --- src/devices/csma-cd/csma-cd-net-device.cc | 14 ++++++------- .../point-to-point-net-device.cc | 4 ++-- src/node/eui48-address.cc | 8 ++++++++ src/node/eui48-address.h | 20 +++++++++++++------ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/devices/csma-cd/csma-cd-net-device.cc b/src/devices/csma-cd/csma-cd-net-device.cc index ea4855f05..c07b8a438 100644 --- a/src/devices/csma-cd/csma-cd-net-device.cc +++ b/src/devices/csma-cd/csma-cd-net-device.cc @@ -37,7 +37,7 @@ namespace ns3 { CsmaCdNetDevice::CsmaCdNetDevice (Ptr node, Eui48Address addr, CsmaCdEncapsulationMode encapMode) - : NetDevice(node, addr.ConvertTo ()), + : NetDevice(node, addr), m_bps (DataRate (0xffffffff)) { NS_DEBUG ("CsmaCdNetDevice::CsmaCdNetDevice (" << node << ")"); @@ -49,7 +49,7 @@ CsmaCdNetDevice::CsmaCdNetDevice (Ptr node, Eui48Address addr, CsmaCdNetDevice::CsmaCdNetDevice (Ptr node, Eui48Address addr, CsmaCdEncapsulationMode encapMode, bool sendEnable, bool receiveEnable) - : NetDevice(node, addr.ConvertTo ()), + : NetDevice(node, addr), m_bps (DataRate (0xffffffff)) { NS_DEBUG ("CsmaCdNetDevice::CsmaCdNetDevice (" << node << ")"); @@ -96,7 +96,7 @@ CsmaCdNetDevice::Init(bool sendEnable, bool receiveEnable) m_channel = 0; m_queue = 0; - EnableBroadcast (Eui48Address ("ff:ff:ff:ff:ff:ff").ConvertTo ()); + EnableBroadcast (Eui48Address ("ff:ff:ff:ff:ff:ff")); EnableMulticast(); EnablePointToPoint(); @@ -160,7 +160,7 @@ CsmaCdNetDevice::AddHeader (Packet& p, Eui48Address dest, } EthernetHeader header (false); EthernetTrailer trailer; - Eui48Address source = Eui48Address::ConvertFrom (GetAddress ()); + Eui48Address source = GetAddress (); header.SetSource(source); header.SetDestination(dest); @@ -201,8 +201,8 @@ CsmaCdNetDevice::ProcessHeader (Packet& p, uint16_t & param) trailer.CheckFcs(p); p.RemoveHeader(header); - Eui48Address broadcast = Eui48Address::ConvertFrom (GetBroadcast ()); - Eui48Address destination = Eui48Address::ConvertFrom (GetAddress ()); + Eui48Address broadcast = GetBroadcast (); + Eui48Address destination = GetAddress (); if ((header.GetDestination() != broadcast) && (header.GetDestination() != destination)) { @@ -252,7 +252,7 @@ CsmaCdNetDevice::SendTo (Packet& p, const Address& dest, uint16_t protocolNumber if (!IsSendEnabled()) return false; - Eui48Address address = Eui48Address::ConvertFrom (dest); + Eui48Address address = dest; AddHeader(p, address, protocolNumber); // Place the packet to be sent on the send queue 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 4b4157b26..1fe15f424 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 @@ -43,7 +43,7 @@ DataRateDefaultValue PointToPointNetDevice::g_defaultRate( PointToPointNetDevice::PointToPointNetDevice (Ptr node, const DataRate& rate) : - NetDevice(node, Eui48Address::Allocate ().ConvertTo ()), + NetDevice(node, Eui48Address::Allocate ()), m_txMachineState (READY), m_bps (rate), m_tInterframeGap (Seconds(0)), @@ -56,7 +56,7 @@ PointToPointNetDevice::PointToPointNetDevice (Ptr node, // BUGBUG FIXME // // You _must_ support broadcast to get any sort of packet from the ARP layer. - EnableBroadcast (Eui48Address ("ff:ff:ff:ff:ff:ff").ConvertTo ()); + EnableBroadcast (Eui48Address ("ff:ff:ff:ff:ff:ff")); EnableMulticast(); EnablePointToPoint(); } diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index 8196d7ba3..165bcb718 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -76,6 +76,14 @@ Eui48Address::IsMatchingType (const Address &address) { return address.CheckCompatible (GetType (), 6); } +Eui48Address::operator Address () +{ + return ConvertTo (); +} +Eui48Address::Eui48Address (const Address &address) +{ + *this = ConvertFrom (address); +} Address Eui48Address::ConvertTo (void) const { diff --git a/src/node/eui48-address.h b/src/node/eui48-address.h index 1d6c37da1..3e693f73c 100644 --- a/src/node/eui48-address.h +++ b/src/node/eui48-address.h @@ -36,28 +36,36 @@ public: * Copy the internal address to the input buffer. */ void CopyTo (uint8_t buffer[6]) const; + /** * \returns a new Address instance * * Convert an instance of this class to a polymorphic Address instance. */ - Address ConvertTo (void) const; - /** - * \returns true if the address matches, false otherwise. - */ - static bool IsMatchingType (const Address &address); + operator Address (); /** * \param address a polymorphic address * * Convert a polymorphic address to an Eui48Address instance. * The conversion performs a type check. */ - static Eui48Address ConvertFrom (const Address &address); + Eui48Address (const Address &address); + /** + * \returns true if the address matches, false otherwise. + */ + static bool IsMatchingType (const Address &address); /** * Allocate a new Eui48Address. */ static Eui48Address Allocate (void); private: + /** + * \returns a new Address instance + * + * Convert an instance of this class to a polymorphic Address instance. + */ + Address ConvertTo (void) const; + static Eui48Address ConvertFrom (const Address &address); static uint8_t GetType (void); uint8_t m_address[6]; }; From 497b9deadceef24de05b545b09523ffa26d325d5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 14:20:10 +0200 Subject: [PATCH 18/48] add implicit conversion to Ipv4Address --- src/node/ipv4-address.cc | 9 +++++++++ src/node/ipv4-address.h | 6 ++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/node/ipv4-address.cc b/src/node/ipv4-address.cc index aae53c65f..91d4d0bc1 100644 --- a/src/node/ipv4-address.cc +++ b/src/node/ipv4-address.cc @@ -211,6 +211,15 @@ Ipv4Address::IsMatchingType (const Address &address) { return address.CheckCompatible (GetType (), 4); } +Ipv4Address::operator Address () +{ + return ConvertTo (); +} +Ipv4Address::Ipv4Address (const Address &address) +{ + *this = ConvertFrom (address); +} + Address Ipv4Address::ConvertTo (void) const { diff --git a/src/node/ipv4-address.h b/src/node/ipv4-address.h index 8e8976a18..d17854742 100644 --- a/src/node/ipv4-address.h +++ b/src/node/ipv4-address.h @@ -105,14 +105,16 @@ public: Ipv4Address CombineMask (Ipv4Mask const &mask) const; static bool IsMatchingType (const Address &address); - Address ConvertTo (void) const; - static Ipv4Address ConvertFrom (const Address &address); + operator Address (); + Ipv4Address (const Address &address); static Ipv4Address GetZero (void); static Ipv4Address GetAny (void); static Ipv4Address GetBroadcast (void); static Ipv4Address GetLoopback (void); private: + Address ConvertTo (void) const; + static Ipv4Address ConvertFrom (const Address &address); static uint8_t GetType (void); uint32_t m_address; }; From f1b93271230a12cd02417211a8e54a08178c0391 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 30 Jul 2007 14:57:49 +0200 Subject: [PATCH 19/48] remove implicit conversion from Address to Eui48Address, to Ipv4Address and to InetSocketAddress --- src/devices/csma-cd/csma-cd-net-device.cc | 12 +++++------- src/internet-node/udp-socket.cc | 8 ++++---- src/node/eui48-address.h | 11 ++++++----- src/node/inet-socket-address.cc | 5 ----- src/node/inet-socket-address.h | 6 +++--- src/node/ipv4-address.cc | 4 ---- src/node/ipv4-address.h | 3 +-- 7 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/devices/csma-cd/csma-cd-net-device.cc b/src/devices/csma-cd/csma-cd-net-device.cc index c07b8a438..f5936cb8b 100644 --- a/src/devices/csma-cd/csma-cd-net-device.cc +++ b/src/devices/csma-cd/csma-cd-net-device.cc @@ -160,7 +160,7 @@ CsmaCdNetDevice::AddHeader (Packet& p, Eui48Address dest, } EthernetHeader header (false); EthernetTrailer trailer; - Eui48Address source = GetAddress (); + Eui48Address source = Eui48Address::ConvertFrom (GetAddress ()); header.SetSource(source); header.SetDestination(dest); @@ -201,10 +201,8 @@ CsmaCdNetDevice::ProcessHeader (Packet& p, uint16_t & param) trailer.CheckFcs(p); p.RemoveHeader(header); - Eui48Address broadcast = GetBroadcast (); - Eui48Address destination = GetAddress (); - if ((header.GetDestination() != broadcast) && - (header.GetDestination() != destination)) + if ((header.GetDestination() != GetBroadcast ()) && + (header.GetDestination() != GetAddress ())) { return false; } @@ -252,8 +250,8 @@ CsmaCdNetDevice::SendTo (Packet& p, const Address& dest, uint16_t protocolNumber if (!IsSendEnabled()) return false; - Eui48Address address = dest; - AddHeader(p, address, protocolNumber); + Eui48Address destination = Eui48Address::ConvertFrom (dest); + AddHeader(p, destination, protocolNumber); // Place the packet to be sent on the send queue if (m_queue->Enqueue(p) == false ) diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 438e59a53..01ac89c64 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -95,7 +95,7 @@ UdpSocket::Bind (const Address &address) { return ERROR_INVAL; } - InetSocketAddress transport = address; + InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); if (ipv4 == Ipv4Address::GetAny () && port == 0) @@ -151,7 +151,7 @@ UdpSocket::DoConnect(const Address & address, ns3::Callback > connectionFailed, ns3::Callback > halfClose) { - InetSocketAddress transport = address; + InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); m_defaultAddress = transport.GetIpv4 (); m_defaultPort = transport.GetPort (); if (!connectionSucceeded.IsNull ()) @@ -194,7 +194,7 @@ int UdpSocket::DoSendPacketTo (const Packet &p, const Address &address, ns3::Callback, uint32_t> dataSent) { - InetSocketAddress transport = address; + InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); return DoSendPacketTo (p, ipv4, port, dataSent); @@ -245,7 +245,7 @@ UdpSocket::DoSendTo(const Address &address, { p = Packet (buffer, size); } - InetSocketAddress transport = address; + InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); return DoSendPacketTo (p, ipv4, port, dataSent); diff --git a/src/node/eui48-address.h b/src/node/eui48-address.h index 3e693f73c..85f3e2202 100644 --- a/src/node/eui48-address.h +++ b/src/node/eui48-address.h @@ -45,11 +45,13 @@ public: operator Address (); /** * \param address a polymorphic address - * - * Convert a polymorphic address to an Eui48Address instance. - * The conversion performs a type check. + * \returns a new Eui48Address from the polymorphic address + * + * This function performs a type check and asserts if the + * type of the input address is not compatible with an + * Eui48Address. */ - Eui48Address (const Address &address); + static Eui48Address ConvertFrom (const Address &address); /** * \returns true if the address matches, false otherwise. */ @@ -65,7 +67,6 @@ private: * Convert an instance of this class to a polymorphic Address instance. */ Address ConvertTo (void) const; - static Eui48Address ConvertFrom (const Address &address); static uint8_t GetType (void); uint8_t m_address[6]; }; diff --git a/src/node/inet-socket-address.cc b/src/node/inet-socket-address.cc index 43c32dcd1..3b2ad5ba8 100644 --- a/src/node/inet-socket-address.cc +++ b/src/node/inet-socket-address.cc @@ -56,11 +56,6 @@ InetSocketAddress::operator Address () const return ConvertTo (); } -InetSocketAddress::InetSocketAddress (const Address &address) -{ - *this = ConvertFrom (address); -} - Address InetSocketAddress::ConvertTo (void) const { diff --git a/src/node/inet-socket-address.h b/src/node/inet-socket-address.h index c09a6c761..0a00172ba 100644 --- a/src/node/inet-socket-address.h +++ b/src/node/inet-socket-address.h @@ -78,13 +78,13 @@ public: /** * \param address the Address instance to convert from. * - * Construct an InetSocketAddress which corresponds to the input + * Returns an InetSocketAddress which corresponds to the input * Address */ - InetSocketAddress (const Address &address); + static InetSocketAddress ConvertFrom (const Address &address); private: Address ConvertTo (void) const; - static InetSocketAddress ConvertFrom (const Address &address); + static uint8_t GetType (void); Ipv4Address m_ipv4; uint16_t m_port; diff --git a/src/node/ipv4-address.cc b/src/node/ipv4-address.cc index 91d4d0bc1..4cf95aa22 100644 --- a/src/node/ipv4-address.cc +++ b/src/node/ipv4-address.cc @@ -215,10 +215,6 @@ Ipv4Address::operator Address () { return ConvertTo (); } -Ipv4Address::Ipv4Address (const Address &address) -{ - *this = ConvertFrom (address); -} Address Ipv4Address::ConvertTo (void) const diff --git a/src/node/ipv4-address.h b/src/node/ipv4-address.h index d17854742..a2f94592c 100644 --- a/src/node/ipv4-address.h +++ b/src/node/ipv4-address.h @@ -106,7 +106,7 @@ public: static bool IsMatchingType (const Address &address); operator Address (); - Ipv4Address (const Address &address); + static Ipv4Address ConvertFrom (const Address &address); static Ipv4Address GetZero (void); static Ipv4Address GetAny (void); @@ -114,7 +114,6 @@ public: static Ipv4Address GetLoopback (void); private: Address ConvertTo (void) const; - static Ipv4Address ConvertFrom (const Address &address); static uint8_t GetType (void); uint32_t m_address; }; From 7af432c3131955940fe659ccb32c6fa60b09b467 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 07:54:26 +0200 Subject: [PATCH 20/48] small cleanups --- src/node/node.h | 50 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/node/node.h b/src/node/node.h index 4e4e7b8c7..4492a23dd 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -1,29 +1,25 @@ -// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// All rights reserved. -// -// 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 -// -// Author: George F. Riley -// - -// Define the basic Node object for ns3. -// George F. Riley, Georgia Tech, Fall 2006 - -#ifndef I_NODE_H -#define I_NODE_H +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 Georgia Tech Research Corporation, INRIA + * + * 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 + * + * Authors: George F. Riley + * Mathieu Lacage + */ +#ifndef NODE_H +#define NODE_H #include @@ -170,4 +166,4 @@ private: } //namespace ns3 -#endif /* I_NODE_H */ +#endif /* NODE_H */ From b4237e51dcc22a8ca402fa41fcac6b41e57cd560 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 09:09:31 +0200 Subject: [PATCH 21/48] implement the Node::ProtocolHandler support. --- src/internet-node/arp-l3-protocol.cc | 12 +- src/internet-node/arp-l3-protocol.h | 7 +- src/internet-node/internet-node.cc | 44 ++----- src/internet-node/internet-node.h | 1 - src/internet-node/ipv4-l3-protocol.cc | 11 +- src/internet-node/ipv4-l3-protocol.h | 6 +- src/internet-node/ipv4-loopback-interface.cc | 6 +- src/internet-node/ipv4-private.cc | 67 ----------- src/internet-node/ipv4-private.h | 58 --------- src/internet-node/ipv4-static-routing.h | 1 - src/internet-node/l3-demux.cc | 90 -------------- src/internet-node/l3-demux.h | 93 --------------- src/internet-node/l3-protocol.cc | 54 --------- src/internet-node/l3-protocol.h | 73 ------------ src/internet-node/udp-l4-protocol.cc | 4 +- src/internet-node/wscript | 3 - src/node/node.cc | 117 +++++++++++++++---- src/node/node.h | 51 +++++++- 18 files changed, 174 insertions(+), 524 deletions(-) delete mode 100644 src/internet-node/ipv4-private.cc delete mode 100644 src/internet-node/ipv4-private.h delete mode 100644 src/internet-node/l3-demux.cc delete mode 100644 src/internet-node/l3-demux.h delete mode 100644 src/internet-node/l3-protocol.cc delete mode 100644 src/internet-node/l3-protocol.h diff --git a/src/internet-node/arp-l3-protocol.cc b/src/internet-node/arp-l3-protocol.cc index 947d5fc42..1e2d7030f 100644 --- a/src/internet-node/arp-l3-protocol.cc +++ b/src/internet-node/arp-l3-protocol.cc @@ -24,11 +24,11 @@ #include "ns3/node.h" #include "ns3/net-device.h" +#include "ipv4-l3-protocol.h" #include "arp-l3-protocol.h" #include "arp-header.h" #include "arp-cache.h" #include "ipv4-interface.h" -#include "ipv4-private.h" NS_DEBUG_COMPONENT_DEFINE ("ArpL3Protocol"); @@ -37,8 +37,7 @@ namespace ns3 { const uint16_t ArpL3Protocol::PROT_NUMBER = 0x0806; ArpL3Protocol::ArpL3Protocol (Ptr node) - : L3Protocol (PROT_NUMBER, 0/* XXX: correct version number ? */ ), - m_node (node) + : m_node (node) {} ArpL3Protocol::~ArpL3Protocol () @@ -53,7 +52,7 @@ ArpL3Protocol::DoDispose (void) } m_cacheList.clear (); m_node = 0; - L3Protocol::DoDispose (); + Object::DoDispose (); } TraceResolver * @@ -72,7 +71,7 @@ ArpL3Protocol::FindCache (Ptr device) return *i; } } - Ptr ipv4 = m_node->QueryInterface (Ipv4Private::iid); + Ptr ipv4 = m_node->QueryInterface (Ipv4L3Protocol::iid); Ipv4Interface *interface = ipv4->FindInterfaceForDevice (device); ArpCache * cache = new ArpCache (device, interface); NS_ASSERT (device->IsBroadcast ()); @@ -82,10 +81,11 @@ ArpL3Protocol::FindCache (Ptr device) } void -ArpL3Protocol::Receive(Packet& packet, Ptr device) +ArpL3Protocol::Receive(const Packet& p, uint16_t protocol, Ptr device) { ArpCache *cache = FindCache (device); ArpHeader arp; + Packet packet = p; packet.RemoveHeader (arp); NS_DEBUG ("ARP: received "<< (arp.IsRequest ()? "request" : "reply") << diff --git a/src/internet-node/arp-l3-protocol.h b/src/internet-node/arp-l3-protocol.h index 082bbebb5..ea61ec8af 100644 --- a/src/internet-node/arp-l3-protocol.h +++ b/src/internet-node/arp-l3-protocol.h @@ -25,7 +25,6 @@ #include "ns3/ipv4-address.h" #include "ns3/address.h" #include "ns3/ptr.h" -#include "l3-protocol.h" namespace ns3 { @@ -38,7 +37,7 @@ class TraceContext; /** * \brief An implementation of the ARP protocol */ -class ArpL3Protocol : public L3Protocol +class ArpL3Protocol : public Object { public: static const uint16_t PROT_NUMBER; @@ -47,13 +46,13 @@ public: * \param node The node which this ARP object is associated with */ ArpL3Protocol (Ptr node); - ~ArpL3Protocol (); + virtual ~ArpL3Protocol (); virtual TraceResolver *CreateTraceResolver (TraceContext const &context); /** * \brief Recieve a packet */ - virtual void Receive(Packet& p, Ptr device); + void Receive(const Packet& p, uint16_t protocol, Ptr device); /** * \brief Perform an ARP lookup * \param p diff --git a/src/internet-node/internet-node.cc b/src/internet-node/internet-node.cc index 0400e241f..128a3f332 100644 --- a/src/internet-node/internet-node.cc +++ b/src/internet-node/internet-node.cc @@ -23,8 +23,8 @@ #include "ns3/composite-trace-resolver.h" #include "ns3/net-device.h" +#include "ns3/callback.h" -#include "l3-demux.h" #include "ipv4-l4-demux.h" #include "internet-node.h" #include "udp-l4-protocol.h" @@ -33,7 +33,6 @@ #include "udp-impl.h" #include "arp-private.h" #include "ipv4-impl.h" -#include "ipv4-private.h" namespace ns3 { @@ -56,25 +55,25 @@ InternetNode::Construct (void) { Ptr ipv4 = Create (this); Ptr arp = Create (this); - Ptr udp = Create (this); + // XXX remove the PeekPointer below. + RegisterProtocolHandler (MakeCallback (&Ipv4L3Protocol::Receive, PeekPointer (ipv4)), + Ipv4L3Protocol::PROT_NUMBER, 0); + RegisterProtocolHandler (MakeCallback (&ArpL3Protocol::Receive, PeekPointer (arp)), + ArpL3Protocol::PROT_NUMBER, 0); + - Ptr l3Demux = Create (this); Ptr ipv4L4Demux = Create (this); - - l3Demux->Insert (ipv4); - l3Demux->Insert (arp); + Ptr udp = Create (this); ipv4L4Demux->Insert (udp); Ptr udpImpl = Create (udp); Ptr arpPrivate = Create (arp); Ptr ipv4Impl = Create (ipv4); - Ptr ipv4Private = Create (ipv4); - Object::AddInterface (ipv4Private); + Object::AddInterface (ipv4); Object::AddInterface (ipv4Impl); Object::AddInterface (arpPrivate); Object::AddInterface (udpImpl); - Object::AddInterface (l3Demux); Object::AddInterface (ipv4L4Demux); } @@ -83,9 +82,9 @@ TraceResolver * InternetNode::DoCreateTraceResolver (TraceContext const &context) { CompositeTraceResolver *resolver = new CompositeTraceResolver (context); - Ptr ipv4 = QueryInterface (Ipv4Private::iid); + Ptr ipv4 = QueryInterface (Ipv4L3Protocol::iid); resolver->Add ("ipv4", - MakeCallback (&Ipv4Private::CreateTraceResolver, PeekPointer (ipv4)), + MakeCallback (&Ipv4L3Protocol::CreateTraceResolver, PeekPointer (ipv4)), InternetNode::IPV4); return resolver; @@ -97,25 +96,4 @@ InternetNode::DoDispose() Node::DoDispose (); } -void -InternetNode::DoAddDevice (Ptr device) -{ - device->SetReceiveCallback (MakeCallback (&InternetNode::ReceiveFromDevice, this)); -} - -bool -InternetNode::ReceiveFromDevice (Ptr device, const Packet &p, uint16_t protocolNumber) const -{ - Ptr demux = QueryInterface (L3Demux::iid); - Ptr target = demux->GetProtocol (protocolNumber); - if (target != 0) - { - Packet packet = p; - target->Receive(packet, device); - return true; - } - return false; -} - - }//namespace ns3 diff --git a/src/internet-node/internet-node.h b/src/internet-node/internet-node.h index b577f6b6b..1d63edae5 100644 --- a/src/internet-node/internet-node.h +++ b/src/internet-node/internet-node.h @@ -46,7 +46,6 @@ public: protected: virtual void DoDispose(void); private: - virtual void DoAddDevice (Ptr device); virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context); bool ReceiveFromDevice (Ptr device, const Packet &p, uint16_t protocolNumber) const; void Construct (void); diff --git a/src/internet-node/ipv4-l3-protocol.cc b/src/internet-node/ipv4-l3-protocol.cc index 077acb3f7..5718c0b89 100644 --- a/src/internet-node/ipv4-l3-protocol.cc +++ b/src/internet-node/ipv4-l3-protocol.cc @@ -41,11 +41,11 @@ NS_DEBUG_COMPONENT_DEFINE ("Ipv4L3Protocol"); namespace ns3 { +const InterfaceId Ipv4L3Protocol::iid = MakeInterfaceId ("Ipv4L3Protocol", Object::iid); const uint16_t Ipv4L3Protocol::PROT_NUMBER = 0x0800; Ipv4L3Protocol::Ipv4L3Protocol(Ptr node) - : L3Protocol (PROT_NUMBER, 4), - m_nInterfaces (0), + : m_nInterfaces (0), m_defaultTtl (64), m_identification (0), m_node (node) @@ -66,9 +66,9 @@ Ipv4L3Protocol::DoDispose (void) } m_interfaces.clear (); m_node = 0; - L3Protocol::DoDispose (); m_staticRouting->Dispose (); m_staticRouting = 0; + Object::DoDispose (); } void @@ -240,18 +240,19 @@ Ipv4L3Protocol::FindInterfaceForDevice (Ptr device) } void -Ipv4L3Protocol::Receive(Packet& packet, Ptr device) +Ipv4L3Protocol::Receive(const Packet& p, uint16_t protocol, Ptr device) { uint32_t index = 0; for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++) { if ((*i)->GetDevice () == device) { - m_rxTrace (packet, index); + m_rxTrace (p, index); break; } index++; } + Packet packet = p; Ipv4Header ipHeader; packet.RemoveHeader (ipHeader); diff --git a/src/internet-node/ipv4-l3-protocol.h b/src/internet-node/ipv4-l3-protocol.h index b41b6aefa..200a001d4 100644 --- a/src/internet-node/ipv4-l3-protocol.h +++ b/src/internet-node/ipv4-l3-protocol.h @@ -30,7 +30,6 @@ #include "ipv4-header.h" #include "ns3/ptr.h" #include "ns3/ipv4.h" -#include "l3-protocol.h" #include "ipv4-static-routing.h" namespace ns3 { @@ -46,9 +45,10 @@ class TraceResolver; class TraceContext; -class Ipv4L3Protocol : public L3Protocol +class Ipv4L3Protocol : public Object { public: + static const InterfaceId iid; static const uint16_t PROT_NUMBER; enum TraceType { @@ -95,7 +95,7 @@ public: * - implement a per-NetDevice ARP cache * - send back arp replies on the right device */ - virtual void Receive(Packet& p, Ptr device); + void Receive(const Packet& p, uint16_t protocol, Ptr device); /** * \param packet packet to send diff --git a/src/internet-node/ipv4-loopback-interface.cc b/src/internet-node/ipv4-loopback-interface.cc index 906c0a001..320a32ce4 100644 --- a/src/internet-node/ipv4-loopback-interface.cc +++ b/src/internet-node/ipv4-loopback-interface.cc @@ -23,7 +23,7 @@ #include "ns3/net-device.h" #include "ns3/node.h" #include "ipv4-loopback-interface.h" -#include "ipv4-private.h" +#include "ipv4-l3-protocol.h" namespace ns3 { @@ -43,8 +43,8 @@ Ipv4LoopbackInterface::DoCreateTraceResolver (TraceContext const &context) void Ipv4LoopbackInterface::SendTo (Packet packet, Ipv4Address dest) { - Ptr ipv4 = m_node->QueryInterface (Ipv4Private::iid); - ipv4->Receive (packet, GetDevice ()); + Ptr ipv4 = m_node->QueryInterface (Ipv4L3Protocol::iid); + ipv4->Receive (packet, Ipv4L3Protocol::PROT_NUMBER, GetDevice ()); } }//namespace ns3 diff --git a/src/internet-node/ipv4-private.cc b/src/internet-node/ipv4-private.cc deleted file mode 100644 index 1b9314152..000000000 --- a/src/internet-node/ipv4-private.cc +++ /dev/null @@ -1,67 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 INRIA - * All rights reserved. - * - * 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 - * - * Author: Mathieu Lacage - */ -#include "ipv4-private.h" -#include "ipv4-l3-protocol.h" -#include "ns3/assert.h" -#include "ns3/net-device.h" - -namespace ns3 { - -const InterfaceId Ipv4Private::iid = MakeInterfaceId ("Ipv4Private", Object::iid); - -Ipv4Private::Ipv4Private (Ptr ipv4) - : m_ipv4 (ipv4) -{ - SetInterfaceId (Ipv4Private::iid); -} -Ipv4Private::~Ipv4Private () -{ - NS_ASSERT (m_ipv4 == 0); -} -TraceResolver * -Ipv4Private::CreateTraceResolver (TraceContext const &context) -{ - return m_ipv4->CreateTraceResolver (context); -} -void -Ipv4Private::Send (Packet const &packet, Ipv4Address source, - Ipv4Address destination, uint8_t protocol) -{ - m_ipv4->Send (packet, source, destination, protocol); -} -Ipv4Interface * -Ipv4Private::FindInterfaceForDevice (Ptrdevice) -{ - return m_ipv4->FindInterfaceForDevice (device); -} -void -Ipv4Private::Receive(Packet& p, Ptr device) -{ - m_ipv4->Receive (p, device); -} -void -Ipv4Private::DoDispose (void) -{ - m_ipv4 = 0; - Object::DoDispose (); -} - -} // namespace ns3 diff --git a/src/internet-node/ipv4-private.h b/src/internet-node/ipv4-private.h deleted file mode 100644 index 3208d0f1a..000000000 --- a/src/internet-node/ipv4-private.h +++ /dev/null @@ -1,58 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 INRIA - * All rights reserved. - * - * 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 - * - * Author: Mathieu Lacage - */ -#ifndef IPV4_PRIVATE_H -#define IPV4_PRIVATE_H - -#include "ns3/object.h" -#include "ns3/ipv4-address.h" -#include "ns3/ptr.h" -#include - -namespace ns3 { - -class Packet; -class Ipv4L3Protocol; -class TraceContext; -class TraceResolver; -class Ipv4Interface; -class NetDevice; - -class Ipv4Private : public Object -{ -public: - static const InterfaceId iid; - Ipv4Private (Ptr ipv4); - virtual ~Ipv4Private (); - - TraceResolver *CreateTraceResolver (TraceContext const &context); - void Send (Packet const &packet, Ipv4Address source, - Ipv4Address destination, uint8_t protocol); - Ipv4Interface *FindInterfaceForDevice (Ptrdevice); - void Receive(Packet& p, Ptr device); -protected: - virtual void DoDispose (void); -private: - Ptr m_ipv4; -}; - -} // namespace ns3 - -#endif /* IPV4_PRIVATE_H */ diff --git a/src/internet-node/ipv4-static-routing.h b/src/internet-node/ipv4-static-routing.h index f7e6d52ab..8462f55a7 100644 --- a/src/internet-node/ipv4-static-routing.h +++ b/src/internet-node/ipv4-static-routing.h @@ -31,7 +31,6 @@ #include "ipv4-header.h" #include "ns3/ptr.h" #include "ns3/ipv4.h" -#include "l3-protocol.h" namespace ns3 { diff --git a/src/internet-node/l3-demux.cc b/src/internet-node/l3-demux.cc deleted file mode 100644 index 6e8416a50..000000000 --- a/src/internet-node/l3-demux.cc +++ /dev/null @@ -1,90 +0,0 @@ -// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// All rights reserved. -// -// 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 -// -// Author: George F. Riley -// -// Implement the L3Protocols capability for ns3. -// George F. Riley, Georgia Tech, Fall 2006 -#include -#include -#include "ns3/composite-trace-resolver.h" -#include "ns3/node.h" -#include "l3-demux.h" -#include "l3-protocol.h" - -namespace ns3 { - -const InterfaceId L3Demux::iid = MakeInterfaceId ("L3Demux", Object::iid); - -L3Demux::L3Demux (Ptr node) - : m_node (node) -{ - SetInterfaceId (L3Demux::iid); -} - -L3Demux::~L3Demux() -{} - -void -L3Demux::DoDispose (void) -{ - for (L3Map_t::iterator i = m_protocols.begin(); i != m_protocols.end(); ++i) - { - i->second->Dispose (); - i->second = 0; - } - m_protocols.clear (); - m_node = 0; - Object::DoDispose (); -} - -TraceResolver * -L3Demux::CreateTraceResolver (TraceContext const &context) const -{ - CompositeTraceResolver *resolver = new CompositeTraceResolver (context); - for (L3Map_t::const_iterator i = m_protocols.begin(); i != m_protocols.end(); ++i) - { - std::string protValue; - std::ostringstream oss (protValue); - oss << i->second->GetProtocolNumber (); - ProtocolTraceType context = i->second->GetProtocolNumber (); - resolver->Add (protValue, - MakeCallback (&L3Protocol::CreateTraceResolver, PeekPointer (i->second)), - context); - } - return resolver; -} - -void L3Demux::Insert(Ptr p) -{ - m_protocols.insert(L3Map_t::value_type(p->GetProtocolNumber (), p)); -} - -Ptr -L3Demux::GetProtocol (int p) -{ // Look up a protocol by protocol number - L3Map_t::iterator i = m_protocols.find(p); - if (i == m_protocols.end()) - { - return 0; - } - return i->second; // Return the protocol -} - -} //namespace ns3 - diff --git a/src/internet-node/l3-demux.h b/src/internet-node/l3-demux.h deleted file mode 100644 index 43a3e1b7d..000000000 --- a/src/internet-node/l3-demux.h +++ /dev/null @@ -1,93 +0,0 @@ -// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// All rights reserved. -// -// 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 -// -// Author: George F. Riley -// -// Define the L3Protocols capability for ns3. -// George F. Riley, Georgia Tech, Fall 2006 - -// This object manages the different layer 3 protocols for any ns3 -// node that has this capability. - -#ifndef L3_DEMUX_H -#define L3_DEMUX_H - -#include -#include "ns3/object.h" -#include "ns3/ptr.h" - -namespace ns3 { - -class L3Protocol; -class Node; -class TraceResolver; -class TraceContext; - -/** - * \brief L3 Demux - */ -class L3Demux : public Object -{ -public: - static const InterfaceId iid; - typedef int ProtocolTraceType; - L3Demux(Ptr node); - virtual ~L3Demux(); - - /** - * \param context the trace context to use to construct the - * TraceResolver to return - * \returns a TraceResolver which can resolve all traces - * performed in this object. The caller must - * delete the returned object. - */ - TraceResolver *CreateTraceResolver (TraceContext const &context) const; - - - /** - * \param protocol a template for the protocol to add to this L3 Demux. - * - * Invoke Copy on the input template to get a copy of the input - * protocol which can be used on the Node on which this L3 Demux - * is running. The new L3Protocol is registered internally as - * a working L3 Protocol and returned from this method. - * The caller does not get ownership of the returned pointer. - */ - void Insert(Ptr protocol); - /** - * \param protocolNumber number of protocol to lookup - * in this L4 Demux - * \returns a matching L3 Protocol - * - * This method is typically called by lower layers - * to forward packets up the stack to the right protocol. - * It is also called from NodeImpl::GetIpv4 for example. - */ - Ptr GetProtocol (int protocolNumber); -protected: - virtual void DoDispose (void); -private: - typedef std::map > L3Map_t; - - Ptr m_node; - L3Map_t m_protocols; -}; - -} //namespace ns3 -#endif - diff --git a/src/internet-node/l3-protocol.cc b/src/internet-node/l3-protocol.cc deleted file mode 100644 index 71dbc2d46..000000000 --- a/src/internet-node/l3-protocol.cc +++ /dev/null @@ -1,54 +0,0 @@ -// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// All rights reserved. -// -// 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 -// -// Author: George F. Riley -// - -// NS3 - Layer 3 Protocol base class -// George F. Riley, Georgia Tech, Spring 2007 - -#include "l3-protocol.h" - - -namespace ns3 { - -L3Protocol::L3Protocol(int protocolNumber, int version) - : m_protocolNumber (protocolNumber), - m_version (version) -{} -L3Protocol::~L3Protocol () -{} - -int -L3Protocol::GetProtocolNumber (void) const -{ - return m_protocolNumber; -} -int -L3Protocol::GetVersion() const -{ - return m_version; -} - -void -L3Protocol::DoDispose (void) -{ - Object::DoDispose (); -} - -}//namespace ns3 diff --git a/src/internet-node/l3-protocol.h b/src/internet-node/l3-protocol.h deleted file mode 100644 index e0c2a469a..000000000 --- a/src/internet-node/l3-protocol.h +++ /dev/null @@ -1,73 +0,0 @@ -// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// All rights reserved. -// -// 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 -// -// Author: George F. Riley -// - -// NS3 - Layer 3 Protocol base class -// George F. Riley, Georgia Tech, Spring 2007 - -#ifndef L3_PROTOCOL_H -#define L3_PROTOCOL_H - -#include "ns3/object.h" -#include "ns3/ptr.h" - -namespace ns3 { - -class Packet; -class NetDevice; -class TraceResolver; -class TraceContext; - -/** - * ::Send is always defined in subclasses. - */ -class L3Protocol : public Object { -public: - L3Protocol(int protocolNumber, int version); - virtual ~L3Protocol (); - /** - * \return The protocol number of this Layer 3 protocol - */ - int GetProtocolNumber (void) const; - /** - * \return The version number of this protocol - */ - int GetVersion() const; - - virtual TraceResolver *CreateTraceResolver (TraceContext const &context) = 0; - /** - * Lower layer calls this method after calling L3Demux::Lookup - * The ARP subclass needs to know from which NetDevice this - * packet is coming to: - * - implement a per-NetDevice ARP cache - * - send back arp replies on the right device - */ - virtual void Receive(Packet& p, Ptr device) = 0; - -protected: - virtual void DoDispose (void); -private: - int m_protocolNumber; - int m_version; -}; - -} // Namespace ns3 - -#endif diff --git a/src/internet-node/udp-l4-protocol.cc b/src/internet-node/udp-l4-protocol.cc index 90fb782b9..f0ef8569b 100644 --- a/src/internet-node/udp-l4-protocol.cc +++ b/src/internet-node/udp-l4-protocol.cc @@ -29,8 +29,6 @@ #include "ipv4-end-point-demux.h" #include "ipv4-end-point.h" #include "ipv4-l3-protocol.h" -#include "ipv4-private.h" -#include "l3-demux.h" #include "udp-socket.h" namespace ns3 { @@ -138,7 +136,7 @@ UdpL4Protocol::Send (Packet packet, packet.AddHeader (udpHeader); - Ptr ipv4 = m_node->QueryInterface (Ipv4Private::iid); + Ptr ipv4 = m_node->QueryInterface (Ipv4L3Protocol::iid); if (ipv4 != 0) { ipv4->Send (packet, saddr, daddr, PROT_NUMBER); diff --git a/src/internet-node/wscript b/src/internet-node/wscript index 7a1b1c10e..b94277456 100644 --- a/src/internet-node/wscript +++ b/src/internet-node/wscript @@ -8,8 +8,6 @@ def build(bld): obj.uselib_local = ['ns3-node', 'ns3-applications'] obj.source = [ 'internet-node.cc', - 'l3-demux.cc', - 'l3-protocol.cc', 'ipv4-l4-demux.cc', 'ipv4-l4-protocol.cc', 'ipv4-header.cc', @@ -29,7 +27,6 @@ def build(bld): 'ipv4-end-point-demux.cc', 'arp-private.cc', 'ipv4-impl.cc', - 'ipv4-private.cc', 'ascii-trace.cc', 'pcap-trace.cc', 'udp-impl.cc', diff --git a/src/node/node.cc b/src/node/node.cc index fa33ff7df..5c3d7c6f3 100644 --- a/src/node/node.cc +++ b/src/node/node.cc @@ -1,32 +1,29 @@ -// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// All rights reserved. -// -// 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 -// -// Author: George F. Riley -// - -// Implement the basic Node object for ns3. -// George F. Riley, Georgia Tech, Fall 2006 - +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 Georgia Tech Research Corporation, INRIA + * + * 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 + * + * Authors: George F. Riley + * Mathieu Lacage + */ #include "node.h" #include "node-list.h" #include "net-device.h" #include "application.h" #include "ns3/simulator.h" +#include "ns3/empty-trace-resolver.h" namespace ns3{ @@ -74,8 +71,9 @@ Node::AddDevice (Ptr device) { uint32_t index = m_devices.size (); m_devices.push_back (device); - DoAddDevice (device); device->SetIfIndex(index); + device->SetReceiveCallback (MakeCallback (&Node::ReceiveFromDevice, this)); + NotifyDeviceAdded (device); return index; } Ptr @@ -129,4 +127,73 @@ void Node::DoDispose() Object::DoDispose (); } +TraceResolver * +Node::DoCreateTraceResolver (TraceContext const &context) +{ + return new EmptyTraceResolver (context); +} +void +Node::NotifyDeviceAdded (Ptr device) +{} + +void +Node::RegisterProtocolHandler (ProtocolHandler handler, + uint16_t protocolType, + Ptr device) +{ + struct Node::ProtocolHandlerEntry entry; + entry.handler = handler; + entry.isSpecificProtocol = true; + entry.protocol = protocolType; + entry.device = device; + m_handlers.push_back (entry); +} + +void +Node::RegisterProtocolHandler (ProtocolHandler handler, + Ptr device) +{ + struct Node::ProtocolHandlerEntry entry; + entry.handler = handler; + entry.isSpecificProtocol = false; + entry.protocol = 0; + entry.device = device; + m_handlers.push_back (entry); +} + +void +Node::UnregisterProtocolHandler (ProtocolHandler handler) +{ + for (ProtocolHandlerList::iterator i = m_handlers.begin (); + i != m_handlers.end (); i++) + { + if (i->handler.IsEqual (handler)) + { + m_handlers.erase (i); + break; + } + } +} + +bool +Node::ReceiveFromDevice (Ptr device, const Packet &packet, uint16_t protocol) +{ + bool found = false; + for (ProtocolHandlerList::iterator i = m_handlers.begin (); + i != m_handlers.end (); i++) + { + if (i->device == 0 || + (i->device != 0 && i->device == device)) + { + if (!i->isSpecificProtocol || + (i->isSpecificProtocol && i->protocol == protocol)) + { + i->handler (packet, protocol, device); + found = true; + } + } + } + return found; +} + }//namespace ns3 diff --git a/src/node/node.h b/src/node/node.h index 4492a23dd..2e3953458 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -24,6 +24,7 @@ #include #include "ns3/object.h" +#include "ns3/callback.h" namespace ns3 { @@ -31,6 +32,7 @@ class TraceContext; class TraceResolver; class NetDevice; class Application; +class Packet; /** * \brief A network Node. @@ -123,6 +125,41 @@ public: */ uint32_t GetNApplications (void) const; + /** + * A protocol handler + */ + typedef Callback > ProtocolHandler; + /** + * \param handler the handler to register + * \param protocolType the type of protocol this handler is + * interested in. + * \param device the device attached to this handler. If the + * value is zero, the handler is attached to all + * devices on this node. + */ + void RegisterProtocolHandler (ProtocolHandler handler, + uint16_t protocolType, + Ptr device); + /** + * \param handler the handler to register + * \param device the device attached to this handler. If the + * value is zero, the handler is attached to all + * devices on this node. + * + * Register a handler to receive all packets for all + * protocols. + */ + void RegisterProtocolHandler (ProtocolHandler handler, + Ptr device); + + /** + * \param handler the handler to unregister + * + * After this call returns, the input handler will never + * be invoked anymore. + */ + void UnregisterProtocolHandler (ProtocolHandler handler); + protected: /** * Must be invoked by subclasses only. @@ -147,7 +184,7 @@ private: * * Subclasses must implement this method. */ - virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context) = 0; + virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context); /** * \param device the device added to this Node. * @@ -156,12 +193,22 @@ private: * at this point to setup the node's receive function for * the NetDevice packets. */ - virtual void DoAddDevice (Ptr device) = 0; + virtual void NotifyDeviceAdded (Ptr device); + bool ReceiveFromDevice (Ptr device, const Packet &packet, uint16_t protocol); + + struct ProtocolHandlerEntry { + ProtocolHandler handler; + bool isSpecificProtocol; + uint16_t protocol; + Ptr device; + }; + typedef std::vector ProtocolHandlerList; uint32_t m_id; // Node id for this node uint32_t m_sid; // System id for this node std::vector > m_devices; std::vector > m_applications; + ProtocolHandlerList m_handlers; }; } //namespace ns3 From 8c80e6c2f8c3540374e53f6b170f26803eb276d7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 09:17:05 +0200 Subject: [PATCH 22/48] remove now-unused ArpPrivate class --- src/internet-node/arp-ipv4-interface.cc | 9 ++-- src/internet-node/arp-l3-protocol.cc | 1 + src/internet-node/arp-l3-protocol.h | 1 + src/internet-node/arp-private.cc | 56 ------------------------- src/internet-node/arp-private.h | 51 ---------------------- src/internet-node/internet-node.cc | 4 +- src/internet-node/wscript | 1 - 7 files changed, 7 insertions(+), 116 deletions(-) delete mode 100644 src/internet-node/arp-private.cc delete mode 100644 src/internet-node/arp-private.h diff --git a/src/internet-node/arp-ipv4-interface.cc b/src/internet-node/arp-ipv4-interface.cc index d5a9105fe..313bb5851 100644 --- a/src/internet-node/arp-ipv4-interface.cc +++ b/src/internet-node/arp-ipv4-interface.cc @@ -28,8 +28,8 @@ #include "ns3/address.h" #include "arp-ipv4-interface.h" -#include "arp-private.h" #include "ipv4-l3-protocol.h" +#include "arp-l3-protocol.h" namespace ns3 { @@ -60,18 +60,17 @@ ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest) NS_ASSERT (GetDevice () != 0); if (GetDevice ()->NeedsArp ()) { - Ptr arp = m_node->QueryInterface (ArpPrivate::iid); + Ptr arp = m_node->QueryInterface (ArpL3Protocol::iid); Address hardwareDestination; bool found; if (dest.IsBroadcast ()) { - hardwareDestination = GetDevice ()->GetBroadcast (); - found = true; + hardwareDestination = GetDevice ()->GetBroadcast (); + found = true; } else { - Ptr arp = m_node->QueryInterface (ArpPrivate::iid); found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination); } diff --git a/src/internet-node/arp-l3-protocol.cc b/src/internet-node/arp-l3-protocol.cc index 1e2d7030f..130f3a1b4 100644 --- a/src/internet-node/arp-l3-protocol.cc +++ b/src/internet-node/arp-l3-protocol.cc @@ -34,6 +34,7 @@ NS_DEBUG_COMPONENT_DEFINE ("ArpL3Protocol"); namespace ns3 { +const InterfaceId ArpL3Protocol::iid = MakeInterfaceId ("ArpL3Protocol", Object::iid); const uint16_t ArpL3Protocol::PROT_NUMBER = 0x0806; ArpL3Protocol::ArpL3Protocol (Ptr node) diff --git a/src/internet-node/arp-l3-protocol.h b/src/internet-node/arp-l3-protocol.h index ea61ec8af..d3ae7441d 100644 --- a/src/internet-node/arp-l3-protocol.h +++ b/src/internet-node/arp-l3-protocol.h @@ -40,6 +40,7 @@ class TraceContext; class ArpL3Protocol : public Object { public: + static const InterfaceId iid; static const uint16_t PROT_NUMBER; /** * \brief Constructor diff --git a/src/internet-node/arp-private.cc b/src/internet-node/arp-private.cc deleted file mode 100644 index 4f60dac99..000000000 --- a/src/internet-node/arp-private.cc +++ /dev/null @@ -1,56 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 INRIA - * All rights reserved. - * - * 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 - * - * Author: Mathieu Lacage - */ -#include "arp-private.h" -#include "arp-l3-protocol.h" -#include "ns3/assert.h" -#include "ns3/net-device.h" - -namespace ns3 { - -const InterfaceId ArpPrivate::iid = MakeInterfaceId ("ArpPrivate", Object::iid); - -ArpPrivate::ArpPrivate (Ptr arp) - : m_arp (arp) -{ - SetInterfaceId (ArpPrivate::iid); -} -ArpPrivate::~ArpPrivate () -{ - NS_ASSERT (m_arp == 0); -} - -bool -ArpPrivate::Lookup (Packet &p, Ipv4Address destination, - Ptr device, - Address *hardwareDestination) -{ - return m_arp->Lookup (p, destination, device, hardwareDestination); -} - -void -ArpPrivate::DoDispose (void) -{ - m_arp = 0; - Object::DoDispose (); -} - - -} // namespace ns3 diff --git a/src/internet-node/arp-private.h b/src/internet-node/arp-private.h deleted file mode 100644 index af0b8b7a3..000000000 --- a/src/internet-node/arp-private.h +++ /dev/null @@ -1,51 +0,0 @@ -/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2007 INRIA - * All rights reserved. - * - * 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 - * - * Author: Mathieu Lacage - */ -#ifndef ARP_PRIVATE_H -#define ARP_PRIVATE_H - -#include "ns3/object.h" -#include "ns3/ipv4-address.h" - -namespace ns3 { - -class NetDevice; -class Address; -class Packet; -class ArpL3Protocol; - -class ArpPrivate : public Object -{ -public: - static const InterfaceId iid; - ArpPrivate (Ptr arp); - virtual ~ArpPrivate (); - bool Lookup (Packet &p, Ipv4Address destination, - Ptr device, - Address *hardwareDestination); -protected: - virtual void DoDispose (void); -private: - Ptr m_arp; -}; - -} // namespace ns3 - -#endif /* ARP_PRIVATE_H */ diff --git a/src/internet-node/internet-node.cc b/src/internet-node/internet-node.cc index 128a3f332..fa0bb1471 100644 --- a/src/internet-node/internet-node.cc +++ b/src/internet-node/internet-node.cc @@ -31,7 +31,6 @@ #include "ipv4-l3-protocol.h" #include "arp-l3-protocol.h" #include "udp-impl.h" -#include "arp-private.h" #include "ipv4-impl.h" namespace ns3 { @@ -67,12 +66,11 @@ InternetNode::Construct (void) ipv4L4Demux->Insert (udp); Ptr udpImpl = Create (udp); - Ptr arpPrivate = Create (arp); Ptr ipv4Impl = Create (ipv4); Object::AddInterface (ipv4); + Object::AddInterface (arp); Object::AddInterface (ipv4Impl); - Object::AddInterface (arpPrivate); Object::AddInterface (udpImpl); Object::AddInterface (ipv4L4Demux); } diff --git a/src/internet-node/wscript b/src/internet-node/wscript index b94277456..e0af13434 100644 --- a/src/internet-node/wscript +++ b/src/internet-node/wscript @@ -25,7 +25,6 @@ def build(bld): 'ipv4-loopback-interface.cc', 'udp-socket.cc', 'ipv4-end-point-demux.cc', - 'arp-private.cc', 'ipv4-impl.cc', 'ascii-trace.cc', 'pcap-trace.cc', From 1454050eef632fc9ace836900cac488340631e04 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 09:21:40 +0200 Subject: [PATCH 23/48] forgot to set the interface id --- src/internet-node/arp-l3-protocol.cc | 4 +++- src/internet-node/ipv4-l3-protocol.cc | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/internet-node/arp-l3-protocol.cc b/src/internet-node/arp-l3-protocol.cc index 130f3a1b4..a18b2ab5d 100644 --- a/src/internet-node/arp-l3-protocol.cc +++ b/src/internet-node/arp-l3-protocol.cc @@ -39,7 +39,9 @@ const uint16_t ArpL3Protocol::PROT_NUMBER = 0x0806; ArpL3Protocol::ArpL3Protocol (Ptr node) : m_node (node) -{} +{ + SetInterfaceId (ArpL3Protocol::iid); +} ArpL3Protocol::~ArpL3Protocol () {} diff --git a/src/internet-node/ipv4-l3-protocol.cc b/src/internet-node/ipv4-l3-protocol.cc index 5718c0b89..eaf2734a3 100644 --- a/src/internet-node/ipv4-l3-protocol.cc +++ b/src/internet-node/ipv4-l3-protocol.cc @@ -50,6 +50,7 @@ Ipv4L3Protocol::Ipv4L3Protocol(Ptr node) m_identification (0), m_node (node) { + SetInterfaceId (Ipv4L3Protocol::iid); m_staticRouting = Create (); AddRoutingProtocol (m_staticRouting, 0); SetupLoopback (); From f7f981d5496962e26554594286b6ecefc9a70054 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 10:45:15 +0200 Subject: [PATCH 24/48] fix bugless in address allocation --- src/node/eui48-address.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index 8196d7ba3..5e552b59a 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -95,7 +95,7 @@ Eui48Address::Allocate (void) static uint64_t id = 0; id++; Eui48Address address; - address.m_address[0] = (id >> 48) & 0xff; + address.m_address[0] = (id >> 40) & 0xff; address.m_address[1] = (id >> 32) & 0xff; address.m_address[2] = (id >> 24) & 0xff; address.m_address[3] = (id >> 16) & 0xff; From af6bed499534d27ee0a226777d6f7b65cbd143b2 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 10:45:37 +0200 Subject: [PATCH 25/48] an eui 64 address type --- src/node/eui64-address.cc | 147 ++++++++++++++++++++++++++++++++++++++ src/node/eui64-address.h | 71 ++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 src/node/eui64-address.cc create mode 100644 src/node/eui64-address.h diff --git a/src/node/eui64-address.cc b/src/node/eui64-address.cc new file mode 100644 index 000000000..b883fb362 --- /dev/null +++ b/src/node/eui64-address.cc @@ -0,0 +1,147 @@ +#include "eui48-address.h" +#include "address.h" +#include "ns3/assert.h" +#include +#include + +namespace ns3 { + +#define ASCII_a (0x41) +#define ASCII_z (0x5a) +#define ASCII_A (0x61) +#define ASCII_Z (0x7a) +#define ASCII_COLON (0x3a) +#define ASCII_ZERO (0x30) + +static char +AsciiToLowCase (char c) +{ + if (c >= ASCII_a && c <= ASCII_z) { + return c; + } else if (c >= ASCII_A && c <= ASCII_Z) { + return c + (ASCII_a - ASCII_A); + } else { + return c; + } +} + + +Eui64Address::Eui64Address () +{ + memset (m_address, 0, 8); +} +Eui64Address::Eui64Address (const char *str) +{ + int i = 0; + while (*str != 0 && i < 8) + { + uint8_t byte = 0; + while (*str != ASCII_COLON && *str != 0) + { + byte <<= 4; + char low = AsciiToLowCase (*str); + if (low >= ASCII_a) + { + byte |= low - ASCII_a + 10; + } + else + { + byte |= low - ASCII_ZERO; + } + str++; + } + m_address[i] = byte; + i++; + if (*str == 0) + { + break; + } + str++; + } + NS_ASSERT (i == 6); +} +void +Eui64Address::CopyFrom (const uint8_t buffer[8]) +{ + memcpy (m_address, buffer, 8); +} +void +Eui64Address::CopyTo (uint8_t buffer[8]) const +{ + memcpy (buffer, m_address, 8); +} + +bool +Eui64Address::IsMatchingType (const Address &address) +{ + return address.CheckCompatible (GetType (), 8); +} +Address +Eui64Address::ConvertTo (void) const +{ + return Address (GetType (), m_address, 8); +} +Eui64Address +Eui64Address::ConvertFrom (const Address &address) +{ + NS_ASSERT (address.CheckCompatible (GetType (), 8)); + Eui64Address retval; + address.CopyTo (retval.m_address); + return retval; +} +Eui64Address +Eui64Address::Allocate (void) +{ + static uint64_t id = 0; + id++; + Eui64Address address; + address.m_address[0] = (id >> 56) & 0xff; + address.m_address[1] = (id >> 48) & 0xff; + address.m_address[2] = (id >> 40) & 0xff; + address.m_address[3] = (id >> 32) & 0xff; + address.m_address[4] = (id >> 24) & 0xff; + address.m_address[5] = (id >> 16) & 0xff; + address.m_address[6] = (id >> 8) & 0xff; + address.m_address[7] = (id >> 0) & 0xff; + return address; +} +uint8_t +Eui64Address::GetType (void) +{ + static uint8_t type = Address::Register (); + return type; +} + +bool operator == (const Eui64Address &a, const Eui64Address &b) +{ + uint8_t ada[8]; + uint8_t adb[8]; + a.CopyTo (ada); + b.CopyTo (adb); + return memcmp (ada, adb, 8) == 0; +} +bool operator != (const Eui64Address &a, const Eui64Address &b) +{ + return ! (a == b); +} + +std::ostream& operator<< (std::ostream& os, const Eui64Address & address) +{ + uint8_t ad[8]; + address.CopyTo (ad); + + os.setf (std::ios::hex, std::ios::basefield); + std::cout.fill('0'); + for (uint8_t i=0; i < 7; i++) + { + os << std::setw(2) << (uint32_t)ad[i] << ":"; + } + // Final byte not suffixed by ":" + os << std::setw(2) << (uint32_t)ad[7]; + os.setf (std::ios::dec, std::ios::basefield); + std::cout.fill(' '); + return os; +} + + +} // namespace ns3 diff --git a/src/node/eui64-address.h b/src/node/eui64-address.h new file mode 100644 index 000000000..fb2b38177 --- /dev/null +++ b/src/node/eui64-address.h @@ -0,0 +1,71 @@ +#ifndef EUI64_ADDRESS_H +#define EUI64_ADDRESS_H + +#include +#include + +namespace ns3 { + +class Address; + +/** + * \brief an EUI-48 address + * + * This class can contain 48 bit IEEE addresses. + */ +class Eui64Address +{ +public: + Eui64Address (); + /** + * \param str a string representing the new Eui64Address + * + * The format of the string is "xx:xx:xx:xx:xx:xx" + */ + Eui64Address (const char *str); + + /** + * \param buffer address in network order + * + * Copy the input address to our internal buffer. + */ + void CopyFrom (const uint8_t buffer[8]); + /** + * \param buffer address in network order + * + * Copy the internal address to the input buffer. + */ + void CopyTo (uint8_t buffer[8]) const; + /** + * \returns a new Address instance + * + * Convert an instance of this class to a polymorphic Address instance. + */ + Address ConvertTo (void) const; + /** + * \returns true if the address matches, false otherwise. + */ + static bool IsMatchingType (const Address &address); + /** + * \param address a polymorphic address + * + * Convert a polymorphic address to an Eui64Address instance. + * The conversion performs a type check. + */ + static Eui64Address ConvertFrom (const Address &address); + /** + * Allocate a new Eui64Address. + */ + static Eui64Address Allocate (void); +private: + static uint8_t GetType (void); + uint8_t m_address[8]; +}; + +bool operator == (const Eui64Address &a, const Eui64Address &b); +bool operator != (const Eui64Address &a, const Eui64Address &b); +std::ostream& operator<< (std::ostream& os, const Eui64Address & address); + +} // namespace ns3 + +#endif /* EUI64_ADDRESS_H */ From 530b65e51d961ed96f9d8931af708794f33f11ed Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 10:46:43 +0200 Subject: [PATCH 26/48] add license headers --- src/node/eui48-address.cc | 19 +++++++++++++++++++ src/node/eui48-address.h | 19 +++++++++++++++++++ src/node/eui64-address.cc | 19 +++++++++++++++++++ src/node/eui64-address.h | 19 +++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index 5e552b59a..096a1cc38 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -1,3 +1,22 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * 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 + * + * Author: Mathieu Lacage + */ #include "eui48-address.h" #include "address.h" #include "ns3/assert.h" diff --git a/src/node/eui48-address.h b/src/node/eui48-address.h index 1d6c37da1..3156782ef 100644 --- a/src/node/eui48-address.h +++ b/src/node/eui48-address.h @@ -1,3 +1,22 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * 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 + * + * Author: Mathieu Lacage + */ #ifndef EUI48_ADDRESS_H #define EUI48_ADDRESS_H diff --git a/src/node/eui64-address.cc b/src/node/eui64-address.cc index b883fb362..31b6d3399 100644 --- a/src/node/eui64-address.cc +++ b/src/node/eui64-address.cc @@ -1,3 +1,22 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * 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 + * + * Author: Mathieu Lacage + */ #include "eui48-address.h" #include "address.h" #include "ns3/assert.h" diff --git a/src/node/eui64-address.h b/src/node/eui64-address.h index fb2b38177..6b407bfa0 100644 --- a/src/node/eui64-address.h +++ b/src/node/eui64-address.h @@ -1,3 +1,22 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * 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 + * + * Author: Mathieu Lacage + */ #ifndef EUI64_ADDRESS_H #define EUI64_ADDRESS_H From 3d9d4ce7be89df74ed297196fa07a31f52dfb803 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 10:47:29 +0200 Subject: [PATCH 27/48] I wonder what the point of that code was: why should you test for zero _after_ using the pointer ? It needs to be tested _before_. --- src/internet-node/udp-socket.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index bc83c942c..bdbc08887 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -73,12 +73,12 @@ UdpSocket::Destroy (void) int UdpSocket::FinishBind (void) { - m_endPoint->SetRxCallback (MakeCallback (&UdpSocket::ForwardUp, this)); - m_endPoint->SetDestroyCallback (MakeCallback (&UdpSocket::Destroy, this)); if (m_endPoint == 0) { return -1; } + m_endPoint->SetRxCallback (MakeCallback (&UdpSocket::ForwardUp, this)); + m_endPoint->SetDestroyCallback (MakeCallback (&UdpSocket::Destroy, this)); return 0; } From 354e017a33b4c488b092430c0fb2a735e2e5a1fe Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 11:33:44 +0200 Subject: [PATCH 28/48] fix build --- src/node/eui64-address.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/eui64-address.cc b/src/node/eui64-address.cc index 31b6d3399..342fd4d91 100644 --- a/src/node/eui64-address.cc +++ b/src/node/eui64-address.cc @@ -17,7 +17,7 @@ * * Author: Mathieu Lacage */ -#include "eui48-address.h" +#include "eui64-address.h" #include "address.h" #include "ns3/assert.h" #include From f8baaaf61702bd6dce9a676b2902b223ba44ed1e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 11:42:10 +0200 Subject: [PATCH 29/48] extra Address API to be used by packet socket address --- src/node/address.cc | 21 ++++++++++++++++++++- src/node/address.h | 13 ++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/node/address.cc b/src/node/address.cc index d73f5790e..2979239c4 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -53,15 +53,34 @@ Address::CopyTo (uint8_t buffer[MAX_SIZE]) const memcpy (buffer, m_data, m_len); } void -Address::CopyFrom (uint8_t *buffer, uint8_t len) +Address::CopyAllTo (uint8_t *buffer, uint8_t len) const +{ + NS_ASSERT (len >= m_len + 2); + buffer[0] = m_type; + buffer[1] = m_len; + memcpy (buffer + 2, m_data, m_len); +} + +void +Address::CopyFrom (const uint8_t *buffer, uint8_t len) { NS_ASSERT (len <= MAX_SIZE); memcpy (m_data, buffer, len); m_len = len; } +void +Address::CopyAllFrom (const uint8_t *buffer, uint8_t len) +{ + NS_ASSERT (len >= 2); + m_type = buffer[0]; + m_len = buffer[1]; + NS_ASSERT (len >= m_len + 2); + memcpy (m_data, buffer + 2, m_len); +} bool Address::CheckCompatible (uint8_t type, uint8_t len) const { + NS_ASSERT (len <= MAX_SIZE); return m_len == len && (m_type == type || m_type == 0); } diff --git a/src/node/address.h b/src/node/address.h index e6bbec026..20ea4b46f 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -94,6 +94,11 @@ public: * \param buffer buffer to copy the address bytes to. */ void CopyTo (uint8_t buffer[MAX_SIZE]) const; + /** + * \param buffer buffer to copy the whole address data structure to + * \param len the size of the buffer + */ + void CopyAllTo (uint8_t *buffer, uint8_t len) const; /** * \param buffer pointer to a buffer of bytes which contain * a serialized representation of the address in network @@ -103,7 +108,13 @@ public: * Copy the input buffer to the internal buffer of this address * instance. */ - void CopyFrom (uint8_t *buffer, uint8_t len); + void CopyFrom (const uint8_t *buffer, uint8_t len); + /** + * \param buffer pointer to a buffer of bytes which contain + * a copy of all the members of this Address class. + * \param len the length of the buffer + */ + void CopyAllFrom (const uint8_t *buffer, uint8_t len); /** * \param type a type id as returned by Address::Register * \param len the length associated to this type id. From 39633a0f0992fa173aa9f30bb58228bac10188cd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 31 Jul 2007 11:42:25 +0200 Subject: [PATCH 30/48] packet socket address --- src/node/packet-socket-address.cc | 113 ++++++++++++++++++++++++++++++ src/node/packet-socket-address.h | 71 +++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 src/node/packet-socket-address.cc create mode 100644 src/node/packet-socket-address.h diff --git a/src/node/packet-socket-address.cc b/src/node/packet-socket-address.cc new file mode 100644 index 000000000..2d2288fca --- /dev/null +++ b/src/node/packet-socket-address.cc @@ -0,0 +1,113 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * 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 + * + * Author: Mathieu Lacage + */ +#include "packet-socket-address.h" +#include "net-device.h" + +namespace ns3 { + +PacketSocketAddress::PacketSocketAddress () +{} +void +PacketSocketAddress::SetProtocol (uint16_t protocol) +{ + m_protocol = protocol; +} +void +PacketSocketAddress::SetDevice (uint32_t index) +{ + m_device = index; +} +void +PacketSocketAddress::SetPhysicalAddress (const Address address) +{ + m_address = address; +} + +uint16_t +PacketSocketAddress::GetProtocol (void) const +{ + return m_protocol; +} +uint32_t +PacketSocketAddress::GetDevice (void) const +{ + return m_device; +} +Address +PacketSocketAddress::GetPhysicalAddress (void) const +{ + return m_address; +} + +Address +PacketSocketAddress::ConvertTo (void) const +{ + Address address; + uint8_t buffer[Address::MAX_SIZE]; + buffer[0] = m_protocol & 0xff; + buffer[1] = (m_protocol >> 8) & 0xff; + buffer[2] = (m_device >> 24) & 0xff; + buffer[3] = (m_device >> 16) & 0xff; + buffer[4] = (m_device >> 8) & 0xff; + buffer[5] = (m_device >> 0) & 0xff; + m_address.CopyAllTo (buffer + 6, Address::MAX_SIZE - 6); + return Address (GetType (), buffer, GetSize ()); +} +PacketSocketAddress +PacketSocketAddress::ConvertFrom (const Address &address) +{ + NS_ASSERT (IsMatchingType (address)); + uint8_t buffer[Address::MAX_SIZE]; + address.CopyTo (buffer); + uint16_t protocol = buffer[0] | (buffer[1] << 8); + uint32_t device = 0; + device |= buffer[2]; + device <<= 8; + device |= buffer[3]; + device <<= 8; + device |= buffer[4]; + device <<= 8; + device |= buffer[5]; + Address physical; + physical.CopyAllFrom (buffer + 6, Address::MAX_SIZE - 6); + PacketSocketAddress ad; + ad.SetProtocol (protocol); + ad.SetDevice (device); + ad.SetPhysicalAddress (physical); + return ad; +} +bool +PacketSocketAddress::IsMatchingType (const Address &address) +{ + return address.CheckCompatible (GetType (), GetSize ()); +} +uint8_t +PacketSocketAddress::GetType (void) +{ + static uint8_t type = Address::Register (); + return type; +} +uint8_t +PacketSocketAddress::GetSize (void) +{ + return 2 + sizeof (NetDevice *) + 1 + 1 + 8; +} + +} // namespace ns3 diff --git a/src/node/packet-socket-address.h b/src/node/packet-socket-address.h new file mode 100644 index 000000000..5e8265b4d --- /dev/null +++ b/src/node/packet-socket-address.h @@ -0,0 +1,71 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * + * 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 + * + * Author: Mathieu Lacage + */ +#ifndef PACKET_SOCKET_ADDRESS_H +#define PACKET_SOCKET_ADDRESS_H + +#include "address.h" +#include "eui48-address.h" +#include "eui64-address.h" + +namespace ns3 { + +class NetDevice; + +class PacketSocketAddress +{ + public: + PacketSocketAddress (); + void SetProtocol (uint16_t protocol); + void SetDevice (uint32_t index); + void SetPhysicalAddress (const Address address); + + uint16_t GetProtocol (void) const; + uint32_t GetDevice (void) const; + Address GetPhysicalAddress (void) const; + + /** + * \returns a new Address instance + * + * Convert an instance of this class to a polymorphic Address instance. + */ + Address ConvertTo (void) const; + /** + * \param address a polymorphic address + * + * Convert a polymorphic address to an Eui48Address instance. + * The conversion performs a type check. + */ + static PacketSocketAddress ConvertFrom (const Address &address); + /** + * \returns true if the address matches, false otherwise. + */ + static bool IsMatchingType (const Address &address); + private: + static uint8_t GetType (void); + static uint8_t GetSize (void); + uint16_t m_protocol; + uint32_t m_device; + Address m_address; +}; + + +} // namespace ns3 + +#endif /* PACKET_SOCKET_ADDRESS_H */ From e05f635db5cd521e3a737aaaaafda73b95727dfd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 08:58:18 +0200 Subject: [PATCH 31/48] rework the NetDevice <-> Node interface --- src/devices/csma-cd/csma-cd-net-device.cc | 62 +++++++++++++++---- src/devices/csma-cd/csma-cd-net-device.h | 2 +- .../point-to-point-net-device.cc | 6 +- src/internet-node/arp-l3-protocol.cc | 2 +- src/internet-node/arp-l3-protocol.h | 2 +- src/internet-node/ipv4-l3-protocol.cc | 2 +- src/internet-node/ipv4-l3-protocol.h | 2 +- src/internet-node/ipv4-loopback-interface.cc | 2 +- src/internet-node/udp-socket.cc | 32 +++++----- src/internet-node/udp-socket.h | 28 ++++----- src/node/net-device.cc | 13 ++-- src/node/net-device.h | 21 +++++-- src/node/node.cc | 45 +++++++------- src/node/node.h | 51 ++++++++------- src/node/socket.cc | 8 +-- src/node/socket.h | 22 ++++--- 16 files changed, 178 insertions(+), 122 deletions(-) diff --git a/src/devices/csma-cd/csma-cd-net-device.cc b/src/devices/csma-cd/csma-cd-net-device.cc index ea4855f05..3d2670fa4 100644 --- a/src/devices/csma-cd/csma-cd-net-device.cc +++ b/src/devices/csma-cd/csma-cd-net-device.cc @@ -457,26 +457,62 @@ CsmaCdNetDevice::AddQueue (Ptr q) } void -CsmaCdNetDevice::Receive (Packet& p) +CsmaCdNetDevice::Receive (const Packet& packet) { + EthernetHeader header (false); + EthernetTrailer trailer; + Eui48Address broadcast; + Eui48Address destination; + Packet p = packet; + NS_DEBUG ("CsmaCdNetDevice::Receive UID is (" << p.GetUid() << ")"); // Only receive if send side of net device is enabled if (!IsReceiveEnabled()) - return; - - uint16_t param = 0; - Packet packet = p; - - if (ProcessHeader(packet, param)) { - m_rxTrace (packet); - ForwardUp (packet, param); - } - else - { - m_dropTrace (packet); + goto drop; } + + if (m_encapMode == RAW) + { + ForwardUp (packet, 0, GetBroadcast ()); + goto drop; + } + p.RemoveTrailer(trailer); + trailer.CheckFcs(p); + p.RemoveHeader(header); + + broadcast = Eui48Address::ConvertFrom (GetBroadcast ()); + destination = Eui48Address::ConvertFrom (GetAddress ()); + if ((header.GetDestination() != broadcast) && + (header.GetDestination() != destination)) + { + // not for us. + goto drop; + } + + uint16_t protocol; + switch (m_encapMode) + { + case ETHERNET_V1: + case IP_ARP: + protocol = header.GetLengthType(); + break; + case LLC: { + LlcSnapHeader llc; + p.RemoveHeader (llc); + protocol = llc.GetType (); + } break; + case RAW: + NS_ASSERT (false); + break; + } + + m_rxTrace (p); + ForwardUp (p, protocol, header.GetSource ().ConvertTo ()); + return; + drop: + m_dropTrace (p); } Ptr diff --git a/src/devices/csma-cd/csma-cd-net-device.h b/src/devices/csma-cd/csma-cd-net-device.h index eb116f3e3..6b1341b89 100644 --- a/src/devices/csma-cd/csma-cd-net-device.h +++ b/src/devices/csma-cd/csma-cd-net-device.h @@ -173,7 +173,7 @@ enum CsmaCdEncapsulationMode { * @see CsmaCdChannel * \param p a reference to the received packet */ - void Receive (Packet& p); + void Receive (const Packet& p); bool IsSendEnabled (void); bool IsReceiveEnabled (void); 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 4b4157b26..b450421f5 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 @@ -220,12 +220,12 @@ void PointToPointNetDevice::AddQueue (Ptr q) void PointToPointNetDevice::Receive (Packet& p) { NS_DEBUG ("PointToPointNetDevice::Receive (" << &p << ")"); - uint16_t param = 0; + uint16_t protocol = 0; Packet packet = p; - ProcessHeader(packet, param); + ProcessHeader(packet, protocol); m_rxTrace (packet); - ForwardUp (packet, param); + ForwardUp (packet, protocol, GetBroadcast ()); } Ptr PointToPointNetDevice::GetQueue(void) const diff --git a/src/internet-node/arp-l3-protocol.cc b/src/internet-node/arp-l3-protocol.cc index a18b2ab5d..100f1df88 100644 --- a/src/internet-node/arp-l3-protocol.cc +++ b/src/internet-node/arp-l3-protocol.cc @@ -84,7 +84,7 @@ ArpL3Protocol::FindCache (Ptr device) } void -ArpL3Protocol::Receive(const Packet& p, uint16_t protocol, Ptr device) +ArpL3Protocol::Receive(Ptr device, const Packet& p, uint16_t protocol, const Address &from) { ArpCache *cache = FindCache (device); ArpHeader arp; diff --git a/src/internet-node/arp-l3-protocol.h b/src/internet-node/arp-l3-protocol.h index d3ae7441d..a2ea8227e 100644 --- a/src/internet-node/arp-l3-protocol.h +++ b/src/internet-node/arp-l3-protocol.h @@ -53,7 +53,7 @@ public: /** * \brief Recieve a packet */ - void Receive(const Packet& p, uint16_t protocol, Ptr device); + void Receive(Ptr device, const Packet& p, uint16_t protocol, const Address &from); /** * \brief Perform an ARP lookup * \param p diff --git a/src/internet-node/ipv4-l3-protocol.cc b/src/internet-node/ipv4-l3-protocol.cc index eaf2734a3..31c3935f5 100644 --- a/src/internet-node/ipv4-l3-protocol.cc +++ b/src/internet-node/ipv4-l3-protocol.cc @@ -241,7 +241,7 @@ Ipv4L3Protocol::FindInterfaceForDevice (Ptr device) } void -Ipv4L3Protocol::Receive(const Packet& p, uint16_t protocol, Ptr device) +Ipv4L3Protocol::Receive( Ptr device, const Packet& p, uint16_t protocol, const Address &from) { uint32_t index = 0; for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++) diff --git a/src/internet-node/ipv4-l3-protocol.h b/src/internet-node/ipv4-l3-protocol.h index 200a001d4..d9abe535a 100644 --- a/src/internet-node/ipv4-l3-protocol.h +++ b/src/internet-node/ipv4-l3-protocol.h @@ -95,7 +95,7 @@ public: * - implement a per-NetDevice ARP cache * - send back arp replies on the right device */ - void Receive(const Packet& p, uint16_t protocol, Ptr device); + void Receive( Ptr device, const Packet& p, uint16_t protocol, const Address &from); /** * \param packet packet to send diff --git a/src/internet-node/ipv4-loopback-interface.cc b/src/internet-node/ipv4-loopback-interface.cc index 320a32ce4..47ddc9ee2 100644 --- a/src/internet-node/ipv4-loopback-interface.cc +++ b/src/internet-node/ipv4-loopback-interface.cc @@ -44,7 +44,7 @@ void Ipv4LoopbackInterface::SendTo (Packet packet, Ipv4Address dest) { Ptr ipv4 = m_node->QueryInterface (Ipv4L3Protocol::iid); - ipv4->Receive (packet, Ipv4L3Protocol::PROT_NUMBER, GetDevice ()); + ipv4->Receive (GetDevice (), packet, Ipv4L3Protocol::PROT_NUMBER, GetDevice ()->GetAddress ()); } }//namespace ns3 diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index bdbc08887..61d32499e 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -136,20 +136,21 @@ UdpSocket::ShutdownRecv (void) return 0; } -void -UdpSocket::DoClose(ns3::Callback > closeCompleted) +int +UdpSocket::DoClose(Callback > closeCompleted) { // XXX: we should set the close state and check it in all API methods. if (!closeCompleted.IsNull ()) { closeCompleted (this); } + return 0; } -void +int UdpSocket::DoConnect(const Address & address, - ns3::Callback > connectionSucceeded, - ns3::Callback > connectionFailed, - ns3::Callback > halfClose) + Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose) { InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); m_defaultAddress = transport.GetIpv4 (); @@ -159,11 +160,12 @@ UdpSocket::DoConnect(const Address & address, connectionSucceeded (this); } m_connected = true; + return 0; } int -UdpSocket::DoAccept(ns3::Callback, const Address&> connectionRequest, - ns3::Callback, const Address&> newConnectionCreated, - ns3::Callback > closeRequested) +UdpSocket::DoAccept(Callback, const Address&> connectionRequest, + Callback, const Address&> newConnectionCreated, + Callback > closeRequested) { // calling accept on a udp socket is a programming error. m_errno = ERROR_OPNOTSUPP; @@ -172,7 +174,7 @@ UdpSocket::DoAccept(ns3::Callback, const Address&> connectionR int UdpSocket::DoSend (const uint8_t* buffer, uint32_t size, - ns3::Callback, uint32_t> dataSent) + Callback, uint32_t> dataSent) { if (!m_connected) { @@ -192,7 +194,7 @@ UdpSocket::DoSend (const uint8_t* buffer, } int UdpSocket::DoSendPacketTo (const Packet &p, const Address &address, - ns3::Callback, uint32_t> dataSent) + Callback, uint32_t> dataSent) { InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); @@ -201,7 +203,7 @@ UdpSocket::DoSendPacketTo (const Packet &p, const Address &address, } int UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address ipv4, uint16_t port, - ns3::Callback, uint32_t> dataSent) + Callback, uint32_t> dataSent) { if (m_endPoint == 0) { @@ -229,7 +231,7 @@ int UdpSocket::DoSendTo(const Address &address, const uint8_t *buffer, uint32_t size, - ns3::Callback, uint32_t> dataSent) + Callback, uint32_t> dataSent) { if (m_connected) { @@ -251,12 +253,12 @@ UdpSocket::DoSendTo(const Address &address, return DoSendPacketTo (p, ipv4, port, dataSent); } void -UdpSocket::DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Address&> callback) +UdpSocket::DoRecv(Callback, const uint8_t*, uint32_t,const Address&> callback) { m_rxCallback = callback; } void -UdpSocket::DoRecvDummy(ns3::Callback, uint32_t,const Address&> callback) +UdpSocket::DoRecvDummy(Callback, uint32_t,const Address&> callback) { m_dummyRxCallback = callback; } diff --git a/src/internet-node/udp-socket.h b/src/internet-node/udp-socket.h index 82d3cd6a3..8778aff84 100644 --- a/src/internet-node/udp-socket.h +++ b/src/internet-node/udp-socket.h @@ -51,23 +51,23 @@ public: virtual int ShutdownRecv (void); private: - virtual void DoClose(ns3::Callback > closeCompleted); - virtual void DoConnect(const Address & address, - ns3::Callback > connectionSucceeded, - ns3::Callback > connectionFailed, - ns3::Callback > halfClose); - virtual int DoAccept(ns3::Callback, const Address&> connectionRequest, - ns3::Callback, const Address&> newConnectionCreated, - ns3::Callback > closeRequested); + virtual int DoClose(Callback > closeCompleted); + virtual int DoConnect(const Address & address, + Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose); + virtual int DoAccept(Callback, const Address&> connectionRequest, + Callback, const Address&> newConnectionCreated, + Callback > closeRequested); virtual int DoSend (const uint8_t* buffer, uint32_t size, - ns3::Callback, uint32_t> dataSent); + Callback, uint32_t> dataSent); virtual int DoSendTo(const Address &address, const uint8_t *buffer, uint32_t size, - ns3::Callback, uint32_t> dataSent); - virtual void DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Address&>); - virtual void DoRecvDummy(ns3::Callback, uint32_t,const Address&>); + Callback, uint32_t> dataSent); + virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Address&>); + virtual void DoRecvDummy(Callback, uint32_t,const Address&>); private: friend class Udp; @@ -76,9 +76,9 @@ private: void ForwardUp (const Packet &p, Ipv4Address ipv4, uint16_t port); void Destroy (void); int DoSendPacketTo (const Packet &p, const Address &daddr, - ns3::Callback, uint32_t> dataSent); + Callback, uint32_t> dataSent); int DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, - ns3::Callback, uint32_t> dataSent); + Callback, uint32_t> dataSent); Ipv4EndPoint *m_endPoint; Ptr m_node; diff --git a/src/node/net-device.cc b/src/node/net-device.cc index a7ef06c7a..4fa2e8d51 100644 --- a/src/node/net-device.cc +++ b/src/node/net-device.cc @@ -197,18 +197,19 @@ NetDevice::GetChannel (void) const // Receive packets from below bool -NetDevice::ForwardUp(Packet& p, uint32_t param) +NetDevice::ForwardUp(const Packet& p, uint32_t param, const Address &from) { bool retval = false; - Packet packet = p; - NS_DEBUG ("NetDevice::ForwardUp: UID is " << packet.GetUid() + NS_DEBUG ("NetDevice::ForwardUp: UID is " << p.GetUid() << " device is: " << GetName()); if (!m_receiveCallback.IsNull ()) { - retval = m_receiveCallback (this, packet, param); - } else { + retval = m_receiveCallback (this, p, param, from); + } + else + { NS_DEBUG ("NetDevice::Receive call back is NULL"); } @@ -248,7 +249,7 @@ NetDevice::NeedsArp (void) const } void -NetDevice::SetReceiveCallback (Callback,const Packet &,uint16_t> cb) +NetDevice::SetReceiveCallback (ReceiveCallback cb) { m_receiveCallback = cb; } diff --git a/src/node/net-device.h b/src/node/net-device.h index 88109a894..dd590f39c 100644 --- a/src/node/net-device.h +++ b/src/node/net-device.h @@ -177,11 +177,24 @@ public: */ bool NeedsArp (void) const; + /** + * \param device a pointer to the net device which is calling this callback + * \param packet the packet received + * \param protocol the 16 bit protocol number associated with this packet. + * This protocol number is expected to be the same protocol number + * given to the Send method by the user on the sender side. + * \param address the address of the sender + * \returns true if the callback could handle the packet successfully, false + * otherwise. + */ + typedef Callback,const Packet &,uint16_t,const Address &> ReceiveCallback; + /** * \param cb callback to invoke whenever a packet has been received and must * be forwarded to the higher layers. + * */ - void SetReceiveCallback (Callback,const Packet &,uint16_t> cb); + void SetReceiveCallback (ReceiveCallback cb); protected: /** @@ -230,6 +243,7 @@ public: * \param p packet sent from below up to Network Device * \param param Extra parameter extracted from header and needed by * some protocols + * \param address the address of the sender of this packet. * \returns true if the packet was forwarded successfully, * false otherwise. * @@ -237,7 +251,7 @@ public: * forwards it to the higher layers by calling this method * which is responsible for passing it up to the Rx callback. */ - bool ForwardUp (Packet& p, uint32_t param); + bool ForwardUp (const Packet& p, uint32_t param, const Address &address); /** @@ -248,8 +262,6 @@ public: */ virtual void DoDispose (void); - Callback,const Packet &,uint16_t> m_receiveCallback; - private: /** * \param p packet to send @@ -297,6 +309,7 @@ public: bool m_isMulticast; bool m_isPointToPoint; Callback m_linkChangeCallback; + ReceiveCallback m_receiveCallback; }; }; // namespace ns3 diff --git a/src/node/node.cc b/src/node/node.cc index 5c3d7c6f3..7aaa0216a 100644 --- a/src/node/node.cc +++ b/src/node/node.cc @@ -22,6 +22,7 @@ #include "node-list.h" #include "net-device.h" #include "application.h" +#include "packet-socket-factory.h" #include "ns3/simulator.h" #include "ns3/empty-trace-resolver.h" @@ -33,16 +34,23 @@ Node::Node() : m_id(0), m_sid(0) { - SetInterfaceId (Node::iid); - m_id = NodeList::Add (this); + Construct (); } Node::Node(uint32_t sid) : m_id(0), m_sid(sid) { + Construct (); +} + +void +Node::Construct (void) +{ SetInterfaceId (Node::iid); m_id = NodeList::Add (this); + Ptr socketFactory = Create (); + AddInterface (socketFactory); } Node::~Node () @@ -69,8 +77,8 @@ Node::GetSystemId (void) const uint32_t Node::AddDevice (Ptr device) { - uint32_t index = m_devices.size (); m_devices.push_back (device); + uint32_t index = m_devices.size (); device->SetIfIndex(index); device->SetReceiveCallback (MakeCallback (&Node::ReceiveFromDevice, this)); NotifyDeviceAdded (device); @@ -79,7 +87,14 @@ Node::AddDevice (Ptr device) Ptr Node::GetDevice (uint32_t index) const { - return m_devices[index]; + if (index == 0) + { + return 0; + } + else + { + return m_devices[index - 1]; + } } uint32_t Node::GetNDevices (void) const @@ -143,24 +158,11 @@ Node::RegisterProtocolHandler (ProtocolHandler handler, { struct Node::ProtocolHandlerEntry entry; entry.handler = handler; - entry.isSpecificProtocol = true; entry.protocol = protocolType; entry.device = device; m_handlers.push_back (entry); } -void -Node::RegisterProtocolHandler (ProtocolHandler handler, - Ptr device) -{ - struct Node::ProtocolHandlerEntry entry; - entry.handler = handler; - entry.isSpecificProtocol = false; - entry.protocol = 0; - entry.device = device; - m_handlers.push_back (entry); -} - void Node::UnregisterProtocolHandler (ProtocolHandler handler) { @@ -176,7 +178,8 @@ Node::UnregisterProtocolHandler (ProtocolHandler handler) } bool -Node::ReceiveFromDevice (Ptr device, const Packet &packet, uint16_t protocol) +Node::ReceiveFromDevice (Ptr device, const Packet &packet, + uint16_t protocol, const Address &from) { bool found = false; for (ProtocolHandlerList::iterator i = m_handlers.begin (); @@ -185,10 +188,10 @@ Node::ReceiveFromDevice (Ptr device, const Packet &packet, uint16_t p if (i->device == 0 || (i->device != 0 && i->device == device)) { - if (!i->isSpecificProtocol || - (i->isSpecificProtocol && i->protocol == protocol)) + if (i->protocol == 0 || + i->protocol == protocol) { - i->handler (packet, protocol, device); + i->handler (device, packet, protocol, from); found = true; } } diff --git a/src/node/node.h b/src/node/node.h index 2e3953458..eea8089a5 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -33,6 +33,7 @@ class TraceResolver; class NetDevice; class Application; class Packet; +class Address; /** * \brief A network Node. @@ -56,6 +57,17 @@ class Node : public Object public: static const InterfaceId iid; + /** + * Must be invoked by subclasses only. + */ + Node(); + /** + * \param systemId a unique integer used for parallel simulations. + * + * Must be invoked by subclasses only. + */ + Node(uint32_t systemId); + virtual ~Node(); /** @@ -91,11 +103,15 @@ public: * Associate this device to this node. * This method is called automatically from NetDevice::NetDevice * so the user has little reason to call this method himself. + * The index returned is always non-zero. */ uint32_t AddDevice (Ptr device); /** * \param index the index of the requested NetDevice * \returns the requested NetDevice associated to this Node. + * + * The indexes used by the GetDevice method start at one and + * end at GetNDevices () */ Ptr GetDevice (uint32_t index) const; /** @@ -128,11 +144,15 @@ public: /** * A protocol handler */ - typedef Callback > ProtocolHandler; + typedef Callback, const Packet &,uint16_t,const Address &> ProtocolHandler; /** * \param handler the handler to register * \param protocolType the type of protocol this handler is - * interested in. + * interested in. This protocol type is a so-called + * EtherType, as registered here: + * http://standards.ieee.org/regauth/ethertype/eth.txt + * the value zero is interpreted as matching all + * protocols. * \param device the device attached to this handler. If the * value is zero, the handler is attached to all * devices on this node. @@ -140,18 +160,6 @@ public: void RegisterProtocolHandler (ProtocolHandler handler, uint16_t protocolType, Ptr device); - /** - * \param handler the handler to register - * \param device the device attached to this handler. If the - * value is zero, the handler is attached to all - * devices on this node. - * - * Register a handler to receive all packets for all - * protocols. - */ - void RegisterProtocolHandler (ProtocolHandler handler, - Ptr device); - /** * \param handler the handler to unregister * @@ -161,16 +169,6 @@ public: void UnregisterProtocolHandler (ProtocolHandler handler); protected: - /** - * Must be invoked by subclasses only. - */ - Node(); - /** - * \param systemId a unique integer used for parallel simulations. - * - * Must be invoked by subclasses only. - */ - Node(uint32_t systemId); /** * The dispose method. Subclasses must override this method * and must chain up to it by calling Node::DoDispose at the @@ -195,11 +193,12 @@ private: */ virtual void NotifyDeviceAdded (Ptr device); - bool ReceiveFromDevice (Ptr device, const Packet &packet, uint16_t protocol); + bool ReceiveFromDevice (Ptr device, const Packet &packet, + uint16_t protocol, const Address &from); + void Construct (void); struct ProtocolHandlerEntry { ProtocolHandler handler; - bool isSpecificProtocol; uint16_t protocol; Ptr device; }; diff --git a/src/node/socket.cc b/src/node/socket.cc index b8995cfea..e3e573ab6 100644 --- a/src/node/socket.cc +++ b/src/node/socket.cc @@ -5,19 +5,19 @@ namespace ns3 { Socket::~Socket () {} -void +int Socket::Close(Callback > closeCompleted) { - DoClose (closeCompleted); + return DoClose (closeCompleted); } -void +int Socket::Connect(const Address & address, Callback > connectionSucceeded, Callback > connectionFailed, Callback > halfClose) { - DoConnect (address, connectionSucceeded, connectionFailed, halfClose); + return DoConnect (address, connectionSucceeded, connectionFailed, halfClose); } int Socket::Accept(Callback, const Address&> connectionRequest, diff --git a/src/node/socket.h b/src/node/socket.h index 547ed4320..991fb01b1 100644 --- a/src/node/socket.h +++ b/src/node/socket.h @@ -52,7 +52,9 @@ public: ERROR_AGAIN, ERROR_SHUTDOWN, ERROR_OPNOTSUPP, + ERROR_AFNOSUPPORT, ERROR_INVAL, + ERROR_BADF, SOCKET_ERRNO_LAST }; @@ -92,7 +94,7 @@ public: * After the Close call, the socket is no longer valid, and cannot * safely be used for subsequent operations. */ - void Close(Callback > closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket)); + int Close(Callback > closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket)); /** * \returns zero on success, -1 on failure. @@ -122,10 +124,10 @@ public: * \param halfClose XXX When exactly is this callback invoked ? If it invoked when the * other side closes the connection ? Or when I call Close ? */ - void Connect(const Address &address, - Callback > connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket), - Callback > connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket), - Callback > halfClose = MakeCallback(&Socket::DummyCallbackVoidSocket)); + int Connect(const Address &address, + Callback > connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket), + Callback > connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket), + Callback > halfClose = MakeCallback(&Socket::DummyCallbackVoidSocket)); /** * \brief Accept connection requests from remote hosts @@ -201,11 +203,11 @@ public: MakeCallback (&Socket::DummyCallbackVoidSocketUi32Address)); private: - virtual void DoClose(Callback > closeCompleted) = 0; - virtual void DoConnect(const Address & address, - Callback > connectionSucceeded, - Callback > connectionFailed, - Callback > halfClose) = 0; + virtual int DoClose(Callback > closeCompleted) = 0; + virtual int DoConnect(const Address & address, + Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose) = 0; virtual int DoAccept(Callback, const Address&> connectionRequest, Callback, const Address&> newConnectionCreated, Callback > closeRequested) = 0; From 26875799868f3f91dd7e99628f71220ed3fd6002 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 09:01:54 +0200 Subject: [PATCH 32/48] add Address::IsInvalid --- src/node/address.cc | 6 ++++++ src/node/address.h | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/node/address.cc b/src/node/address.cc index 2979239c4..74993a0b4 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -40,6 +40,12 @@ Address::operator = (const Address &address) return *this; } +bool +Address::IsInvalid (void) const +{ + return m_len == 0 && m_type == 0; +} + uint8_t Address::GetLength (void) const { diff --git a/src/node/address.h b/src/node/address.h index 20ea4b46f..7d234fd72 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -86,6 +86,14 @@ public: Address (const Address & address); Address &operator = (const Address &address); + /** + * \returns true if this address is invalid, false otherwise. + * + * An address is invalid if and only if it was created + * through the default constructor and it was never + * re-initialized. + */ + bool IsInvalid (void); /** * \returns the length of the underlying address. */ From b835a6b564ea3db78c5d4bee000759015cccab66 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 09:02:03 +0200 Subject: [PATCH 33/48] a packet socket --- src/node/packet-socket-address.cc | 12 ++ src/node/packet-socket-address.h | 9 + src/node/packet-socket-factory.cc | 40 ++++ src/node/packet-socket-factory.h | 52 +++++ src/node/packet-socket.cc | 332 ++++++++++++++++++++++++++++++ src/node/packet-socket.h | 132 ++++++++++++ src/node/wscript | 7 + 7 files changed, 584 insertions(+) create mode 100644 src/node/packet-socket-factory.cc create mode 100644 src/node/packet-socket-factory.h create mode 100644 src/node/packet-socket.cc create mode 100644 src/node/packet-socket.h diff --git a/src/node/packet-socket-address.cc b/src/node/packet-socket-address.cc index 2d2288fca..ac5bded70 100644 --- a/src/node/packet-socket-address.cc +++ b/src/node/packet-socket-address.cc @@ -35,6 +35,18 @@ PacketSocketAddress::SetDevice (uint32_t index) m_device = index; } void +PacketSocketAddress::SetDevice (Ptr device) +{ + if (device == 0) + { + m_device = 0; + } + else + { + m_device = device->GetIfIndex (); + } +} +void PacketSocketAddress::SetPhysicalAddress (const Address address) { m_address = address; diff --git a/src/node/packet-socket-address.h b/src/node/packet-socket-address.h index 5e8265b4d..1e903d181 100644 --- a/src/node/packet-socket-address.h +++ b/src/node/packet-socket-address.h @@ -20,9 +20,11 @@ #ifndef PACKET_SOCKET_ADDRESS_H #define PACKET_SOCKET_ADDRESS_H +#include "ns3/ptr.h" #include "address.h" #include "eui48-address.h" #include "eui64-address.h" +#include "net-device.h" namespace ns3 { @@ -33,7 +35,14 @@ class PacketSocketAddress public: PacketSocketAddress (); void SetProtocol (uint16_t protocol); + /** + * \param index of NetDevice. + * + * index zero is reserved to identify _all_ + * Netdevices. + */ void SetDevice (uint32_t index); + void SetDevice (Ptr device); void SetPhysicalAddress (const Address address); uint16_t GetProtocol (void) const; diff --git a/src/node/packet-socket-factory.cc b/src/node/packet-socket-factory.cc new file mode 100644 index 000000000..3cd403a1a --- /dev/null +++ b/src/node/packet-socket-factory.cc @@ -0,0 +1,40 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 Emmanuelle Laprise + * All rights reserved. + * + * 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 + * + * Author: Emmanuelle Laprise + */ +#include "packet-socket-factory.h" +#include "node.h" + +namespace ns3 { + +const InterfaceId PacketSocketFactory::iid = MakeInterfaceId ("Packet", + SocketFactory::iid); + +PacketSocketFactory::PacketSocketFactory () +{ + SetInterfaceId (PacketSocketFactory::iid); +} + +Ptr PacketSocketFactory::CreateSocket (void) +{ + Ptr node = QueryInterface (Node::iid); + Ptr socket = Create (node); + return socket; +} +} // namespace ns3 diff --git a/src/node/packet-socket-factory.h b/src/node/packet-socket-factory.h new file mode 100644 index 000000000..0f052ce6e --- /dev/null +++ b/src/node/packet-socket-factory.h @@ -0,0 +1,52 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 Emmanuelle Laprise + * All rights reserved. + * + * 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 + * + * Author: Emmanuelle Laprise + */ +#ifndef PACKET_SOCKET_FACTORY_H +#define PACKET_SOCKET_FACTORY_H + +#include "socket-factory.h" +#include "packet-socket.h" + +namespace ns3 { + +class Socket; + +/** + * This can be used as an interface in a node in order for the node to + * generate PacketSockets that can connect to net devices. + */ +class PacketSocketFactory : public SocketFactory +{ +public: + static const InterfaceId iid; /// Interface identifier + + PacketSocketFactory (); + + /** + * Creates a PacketSocket and returns a pointer to it. + * + * \return a pointer to the created socket + */ + virtual Ptr CreateSocket (void); +}; + +} // namespace ns3 + +#endif /* PACKET_SOCKET_FACTORY_H */ diff --git a/src/node/packet-socket.cc b/src/node/packet-socket.cc new file mode 100644 index 000000000..9bce9bafa --- /dev/null +++ b/src/node/packet-socket.cc @@ -0,0 +1,332 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 Emmanuelle Laprise, INRIA + * + * 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 + * + * Authors: Emmanuelle Laprise + * Mathieu Lacage + */ + +#include "packet-socket.h" +#include "packet-socket-address.h" +#include "ns3/debug.h" +#include "ns3/node.h" + +NS_DEBUG_COMPONENT_DEFINE ("PacketSocket"); + +namespace ns3 { + +PacketSocket::PacketSocket (Ptr node) + : m_node (node) +{ + Init(); +} + +void +PacketSocket::Init() +{ + m_state = STATE_OPEN; + m_shutdownSend = false; + m_shutdownRecv = false; + m_errno = ERROR_NOTERROR; +} + +PacketSocket::~PacketSocket () +{} + +void +PacketSocket::DoDispose (void) +{ + m_device = 0; +} + +Ptr +PacketSocket::GetNode (void) const +{ + return m_node; +} + + +int +PacketSocket::Bind (void) +{ + PacketSocketAddress address; + address.SetProtocol (0); + address.SetDevice (0); + return DoBind (address); +} +int +PacketSocket::Bind (const Address &address) +{ + if (!PacketSocketAddress::IsMatchingType (address)) + { + m_errno = ERROR_INVAL; + return -1; + } + PacketSocketAddress ad = PacketSocketAddress::ConvertFrom (address); + return DoBind (ad); +} + +int +PacketSocket::DoBind (const PacketSocketAddress &address) +{ + if (m_state == STATE_BOUND || + m_state == STATE_CONNECTED) + { + m_errno = ERROR_INVAL; + return -1; + } + if (m_state == STATE_CLOSED) + { + m_errno = ERROR_BADF; + return -1; + } + Ptr dev = m_node->GetDevice (address.GetDevice ()); + m_node->RegisterProtocolHandler (MakeCallback (&PacketSocket::ForwardUp, this), + address.GetProtocol (), dev); + m_state = STATE_BOUND; + m_protocol = address.GetProtocol (); + m_device = address.GetDevice (); + return 0; +} + +enum Socket::SocketErrno +PacketSocket::GetErrno (void) const +{ + return m_errno; +} +int +PacketSocket::ShutdownSend (void) +{ + if (m_state == STATE_CLOSED) + { + m_errno = ERROR_BADF; + return -1; + } + m_shutdownSend = true; + return 0; +} +int +PacketSocket::ShutdownRecv (void) +{ + if (m_state == STATE_CLOSED) + { + m_errno = ERROR_BADF; + return -1; + } + m_shutdownRecv = false; + return 0; +} +int +PacketSocket::DoClose(ns3::Callback > closeCompleted) +{ + if (m_state == STATE_CLOSED) + { + m_errno = ERROR_BADF; + return -1; + } + if (!closeCompleted.IsNull ()) + { + closeCompleted (this); + } + m_state = STATE_CLOSED; + return 0; +} + +int +PacketSocket::DoConnect(const Address &ad, + ns3::Callback > connectionSucceeded, + ns3::Callback > connectionFailed, + ns3::Callback > halfClose) +{ + PacketSocketAddress address; + if (m_state == STATE_CLOSED) + { + m_errno = ERROR_BADF; + goto error; + } + if (m_state == STATE_OPEN) + { + // connect should happen _after_ bind. + m_errno = ERROR_INVAL; // generic error condition. + goto error; + } + if (m_state == STATE_CONNECTED) + { + m_errno = ERROR_ISCONN; + goto error; + } + if (!PacketSocketAddress::IsMatchingType (ad)) + { + m_errno = ERROR_AFNOSUPPORT; + goto error; + } + m_destAddr = ad; + m_state = STATE_CONNECTED; + if (!connectionSucceeded.IsNull ()) + { + connectionSucceeded (this); + } + return 0; + error: + if (!connectionFailed.IsNull ()) + { + connectionFailed (this); + } + return -1; +} + +int +PacketSocket::DoAccept(ns3::Callback, const Address &> connectionRequest, + ns3::Callback, const Address &> newConnectionCreated, + ns3::Callback > closeRequested) +{ + // calling accept on a packet socket is a programming error. + m_errno = ERROR_OPNOTSUPP; + return -1; +} + +int +PacketSocket::DoSend (const uint8_t* buffer, + uint32_t size, + ns3::Callback, uint32_t> dataSent) +{ + if (m_state == STATE_OPEN || + m_state == STATE_BOUND) + { + m_errno = ERROR_NOTCONN; + return -1; + } + return DoSendTo (m_destAddr, buffer, size, dataSent); +} + +int +PacketSocket::DoSendTo(const Address &address, + const uint8_t *buffer, + uint32_t size, + Callback, uint32_t> dataSent) +{ + PacketSocketAddress ad; + if (m_state == STATE_CLOSED) + { + m_errno = ERROR_BADF; + return -1; + } + if (m_state == STATE_OPEN) + { + // XXX should return another error here. + m_errno = ERROR_INVAL; + return -1; + } + if (m_shutdownSend) + { + m_errno = ERROR_SHUTDOWN; + return -1; + } + if (!PacketSocketAddress::IsMatchingType (address)) + { + m_errno = ERROR_AFNOSUPPORT; + return -1; + } + ad = PacketSocketAddress::ConvertFrom (address); + + Packet p; + if (buffer == 0) + { + p = Packet (size); + } + else + { + p = Packet (buffer, size); + } + + bool error = false; + Address dest = ad.GetPhysicalAddress (); + if (ad.GetDevice () == 0) + { + for (uint32_t i = 1; i <= m_node->GetNDevices (); i++) + { + Ptr device = m_node->GetDevice (i); + if (!device->Send (p, dest, ad.GetProtocol ())) + { + error = true; + } + } + } + else + { + Ptr device = m_node->GetDevice (ad.GetDevice ()); + if (!device->Send (p, dest, ad.GetProtocol ())) + { + error = true; + } + } + if (!error && !dataSent.IsNull ()) + { + dataSent (this, p.GetSize ()); + } + + if (error) + { + m_errno = ERROR_INVAL; + return -1; + } + else + { + return 0; + } +} + +void +PacketSocket::DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Address &> callback) +{ + m_rxCallback = callback; +} + +void +PacketSocket::DoRecvDummy(ns3::Callback, uint32_t, const Address &> callback) +{ + m_dummyRxCallback = callback; +} + +void +PacketSocket::ForwardUp (Ptr device, const Packet &packet, + uint16_t protocol, const Address &from) +{ + if (m_shutdownRecv) + { + return; + } + + Packet p = packet; + + PacketSocketAddress address; + address.SetPhysicalAddress (from); + address.SetDevice (device->GetIfIndex ()); + address.SetProtocol (protocol); + + NS_DEBUG ("PacketSocket::ForwardUp: UID is " << packet.GetUid() + << " PacketSocket " << this); + if (!m_dummyRxCallback.IsNull ()) + { + m_dummyRxCallback (this, p.GetSize (), address.ConvertTo ()); + } + if (!m_rxCallback.IsNull ()) + { + m_rxCallback (this, p.PeekData (), p.GetSize (), address.ConvertTo ()); + } +} + +}//namespace ns3 diff --git a/src/node/packet-socket.h b/src/node/packet-socket.h new file mode 100644 index 000000000..790420970 --- /dev/null +++ b/src/node/packet-socket.h @@ -0,0 +1,132 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 Emmanuelle Laprise, INRIA + * + * 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 + * + * Authors: Emmanuelle Laprise , + * Mathieu Lacage + */ +#ifndef PACKET_SOCKET_H +#define PACKET_SOCKET_H + +#include +#include "ns3/callback.h" +#include "ns3/ptr.h" +#include "ns3/socket.h" + +namespace ns3 { + +class Node; +class Packet; +class NetDevice; +class PacketSocketAddress; + +/** + * \brief A PacketSocket is a link between an application and a net device. + * + * A PacketSocket can be used to connect an application to a net + * device. The application provides the buffers of data, the socket + * conserts them to a raw packet and the net device then adds the + * protocol specific headers and trailers. This socket type + * is very similar to the linux and BSD "packet" sockets. + * + * Here is a summary of the semantics of this class: + * - Bind: Bind uses only the protocol and device fields of the + * PacketSocketAddress. If none are provided, Bind uses + * zero for both, which means that the socket is bound + * to all protocols on all devices on the node. + * + * - Connect: uses only the protocol, device and "physical address" + * field of the PacketSocketAddress. It is used to set the default + * destination address for outgoing packets. + * + * - Send: send the input packet to the underlying NetDevices + * with the default destination address. The socket must + * be bound and connected. + * + * - SendTo: uses the protocol, device, and "physical address" + * fields of the PacketSocketAddress. The device value is + * used to specialize the packet transmission to a single + * device, the protocol value specifies the protocol of this + * packet only and the "physical address" field is used to override the + * default destination address. The socket must be bound. + * + * - Recv: The address represents the address of the packer originator. + * The fields "physical address", device, and protocol are filled. + * + * - Accept: not allowed + */ +class PacketSocket : public Socket +{ +public: + PacketSocket (Ptr node); + virtual ~PacketSocket (); + + virtual enum SocketErrno GetErrno (void) const; + virtual Ptr GetNode (void) const; + virtual int Bind (void); + virtual int Bind (const Address & address); + virtual int ShutdownSend (void); + virtual int ShutdownRecv (void); + +private: + virtual int DoClose(Callback > closeCompleted); + virtual int DoConnect(const Address & address, + Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose); + virtual int DoAccept(Callback, const Address&> connectionRequest, + Callback, const Address&> newConnectionCreated, + Callback > closeRequested); + virtual int DoSend (const uint8_t* buffer, + uint32_t size, + Callback, uint32_t> dataSent); + virtual int DoSendTo(const Address &address, + const uint8_t *buffer, + uint32_t size, + Callback, uint32_t> dataSent); + virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Address&> receive); + virtual void DoRecvDummy(Callback, uint32_t,const Address&>); + +private: + void Init (void); + void ForwardUp (Ptr device, const Packet &packet, + uint16_t protocol, const Address &from); + int DoBind (const PacketSocketAddress &address); + virtual void DoDispose (void); + + enum State { + STATE_OPEN, + STATE_BOUND, // open and bound + STATE_CONNECTED, // open, bound and connected + STATE_CLOSED + }; + Ptr m_node; + Callback,uint32_t,const Address &> m_dummyRxCallback; + Callback,uint8_t const*,uint32_t, const Address &> m_rxCallback; + enum SocketErrno m_errno; + bool m_shutdownSend; + bool m_shutdownRecv; + enum State m_state; + uint16_t m_protocol; + uint32_t m_device; + Address m_destAddr; /// Default destination address +}; + +}//namespace ns3 + +#endif /* PACKET_SOCKET_H */ + + diff --git a/src/node/wscript b/src/node/wscript index 9cc3d3daf..17714f897 100644 --- a/src/node/wscript +++ b/src/node/wscript @@ -8,7 +8,9 @@ def build(bld): node.source = [ 'address.cc', 'eui48-address.cc', + 'eui64-address.cc', 'inet-socket-address.cc', + 'packet-socket-address.cc', 'node.cc', 'ipv4-address.cc', 'net-device.cc', @@ -23,6 +25,8 @@ def build(bld): 'node-list.cc', 'socket.cc', 'socket-factory.cc', + 'packet-socket-factory.cc', + 'packet-socket.cc', 'udp.cc', 'ipv4.cc', 'application.cc', @@ -32,7 +36,9 @@ def build(bld): headers.source = [ 'address.h', 'eui48-address.h', + 'eui64-address.h', 'inet-socket-address.h', + 'packet-socket-address.h', 'node.h', 'ipv4-address.h', 'net-device.h', @@ -47,6 +53,7 @@ def build(bld): 'node-list.h', 'socket.h', 'socket-factory.h', + 'packet-socket-factory.h', 'udp.h', 'ipv4.h', 'application.h', From e91a1198a05121280bcbd0918080b299e093d970 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 09:14:31 +0200 Subject: [PATCH 34/48] fix build: missing const --- src/node/address.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/address.h b/src/node/address.h index 7d234fd72..25bb282fe 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -93,7 +93,7 @@ public: * through the default constructor and it was never * re-initialized. */ - bool IsInvalid (void); + bool IsInvalid (void) const; /** * \returns the length of the underlying address. */ From 802ac99d0d564e265bd3beedae545faab2f8faf4 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 09:15:48 +0200 Subject: [PATCH 35/48] remove extra includes --- examples/csma-cd-one-subnet.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/examples/csma-cd-one-subnet.cc b/examples/csma-cd-one-subnet.cc index ceaf88810..e19ed7e63 100644 --- a/examples/csma-cd-one-subnet.cc +++ b/examples/csma-cd-one-subnet.cc @@ -58,11 +58,6 @@ #include "ns3/ipv4-route.h" #include "ns3/onoff-application.h" -#include "ns3/ascii-trace.h" - -#include "ns3/trace-context.h" -#include "ns3/trace-root.h" - using namespace ns3; From 492afc0f9d63718159b472ebe76361db32162ed7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 10:29:03 +0200 Subject: [PATCH 36/48] PacketSocketAddress serialization code was buggy. --- src/node/address.cc | 17 +++++++++++++---- src/node/address.h | 23 +++++++++++++++++++---- src/node/packet-socket-address.cc | 11 +++-------- src/node/packet-socket-address.h | 1 - 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/node/address.cc b/src/node/address.cc index 74993a0b4..33104083d 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -52,29 +52,32 @@ Address::GetLength (void) const NS_ASSERT (m_len <= MAX_SIZE); return m_len; } -void +uint32_t Address::CopyTo (uint8_t buffer[MAX_SIZE]) const { NS_ASSERT (m_len <= MAX_SIZE); memcpy (buffer, m_data, m_len); + return m_len; } -void +uint32_t Address::CopyAllTo (uint8_t *buffer, uint8_t len) const { NS_ASSERT (len >= m_len + 2); buffer[0] = m_type; buffer[1] = m_len; memcpy (buffer + 2, m_data, m_len); + return m_len + 2; } -void +uint32_t Address::CopyFrom (const uint8_t *buffer, uint8_t len) { NS_ASSERT (len <= MAX_SIZE); memcpy (m_data, buffer, len); m_len = len; + return m_len; } -void +uint32_t Address::CopyAllFrom (const uint8_t *buffer, uint8_t len) { NS_ASSERT (len >= 2); @@ -82,6 +85,7 @@ Address::CopyAllFrom (const uint8_t *buffer, uint8_t len) m_len = buffer[1]; NS_ASSERT (len >= m_len + 2); memcpy (m_data, buffer + 2, m_len); + return m_len + 2; } bool Address::CheckCompatible (uint8_t type, uint8_t len) const @@ -89,6 +93,11 @@ Address::CheckCompatible (uint8_t type, uint8_t len) const NS_ASSERT (len <= MAX_SIZE); return m_len == len && (m_type == type || m_type == 0); } +bool +Address::IsMatchingType (uint8_t type) const +{ + return m_type == type; +} uint8_t Address::Register (void) diff --git a/src/node/address.h b/src/node/address.h index 25bb282fe..0d3b0da83 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -100,29 +100,33 @@ public: uint8_t GetLength (void) const; /** * \param buffer buffer to copy the address bytes to. + * \returns the number of bytes copied. */ - void CopyTo (uint8_t buffer[MAX_SIZE]) const; + uint32_t CopyTo (uint8_t buffer[MAX_SIZE]) const; /** * \param buffer buffer to copy the whole address data structure to * \param len the size of the buffer + * \returns the number of bytes copied. */ - void CopyAllTo (uint8_t *buffer, uint8_t len) const; + uint32_t CopyAllTo (uint8_t *buffer, uint8_t len) const; /** * \param buffer pointer to a buffer of bytes which contain * a serialized representation of the address in network * byte order. * \param len length of buffer + * \returns the number of bytes copied. * * Copy the input buffer to the internal buffer of this address * instance. */ - void CopyFrom (const uint8_t *buffer, uint8_t len); + uint32_t CopyFrom (const uint8_t *buffer, uint8_t len); /** * \param buffer pointer to a buffer of bytes which contain * a copy of all the members of this Address class. * \param len the length of the buffer + * \returns the number of bytes copied. */ - void CopyAllFrom (const uint8_t *buffer, uint8_t len); + uint32_t CopyAllFrom (const uint8_t *buffer, uint8_t len); /** * \param type a type id as returned by Address::Register * \param len the length associated to this type id. @@ -131,6 +135,17 @@ public: * is compatible with the requested type, false otherwise. */ bool CheckCompatible (uint8_t type, uint8_t len) const; + /** + * \param type a type id as returned by Address::Register + * \returns true if the type of the address stored internally + * is compatible with the requested type, false otherwise. + * + * This method checks that the types are _exactly_ equal. + * This method is really used only by the PacketSocketAddress + * and there is little point in using it otherwise so, + * you have been warned: DO NOT USE THIS METHOD. + */ + bool IsMatchingType (uint8_t type) const; /** * Allocate a new type id for a new type of address. * \returns a new type id. diff --git a/src/node/packet-socket-address.cc b/src/node/packet-socket-address.cc index ac5bded70..2c5fcb7b8 100644 --- a/src/node/packet-socket-address.cc +++ b/src/node/packet-socket-address.cc @@ -79,8 +79,8 @@ PacketSocketAddress::ConvertTo (void) const buffer[3] = (m_device >> 16) & 0xff; buffer[4] = (m_device >> 8) & 0xff; buffer[5] = (m_device >> 0) & 0xff; - m_address.CopyAllTo (buffer + 6, Address::MAX_SIZE - 6); - return Address (GetType (), buffer, GetSize ()); + uint32_t copied = m_address.CopyAllTo (buffer + 6, Address::MAX_SIZE - 6); + return Address (GetType (), buffer, 6 + copied); } PacketSocketAddress PacketSocketAddress::ConvertFrom (const Address &address) @@ -108,7 +108,7 @@ PacketSocketAddress::ConvertFrom (const Address &address) bool PacketSocketAddress::IsMatchingType (const Address &address) { - return address.CheckCompatible (GetType (), GetSize ()); + return address.IsMatchingType (GetType ()); } uint8_t PacketSocketAddress::GetType (void) @@ -116,10 +116,5 @@ PacketSocketAddress::GetType (void) static uint8_t type = Address::Register (); return type; } -uint8_t -PacketSocketAddress::GetSize (void) -{ - return 2 + sizeof (NetDevice *) + 1 + 1 + 8; -} } // namespace ns3 diff --git a/src/node/packet-socket-address.h b/src/node/packet-socket-address.h index 1e903d181..ca138ad43 100644 --- a/src/node/packet-socket-address.h +++ b/src/node/packet-socket-address.h @@ -68,7 +68,6 @@ class PacketSocketAddress static bool IsMatchingType (const Address &address); private: static uint8_t GetType (void); - static uint8_t GetSize (void); uint16_t m_protocol; uint32_t m_device; Address m_address; From b4b0d52ffb74310b0caec867bd4f655507c24546 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 10:29:22 +0200 Subject: [PATCH 37/48] add an extra constructor --- src/devices/csma-cd/csma-cd-net-device.cc | 9 +++++++++ src/devices/csma-cd/csma-cd-net-device.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/devices/csma-cd/csma-cd-net-device.cc b/src/devices/csma-cd/csma-cd-net-device.cc index 3d2670fa4..538625ee5 100644 --- a/src/devices/csma-cd/csma-cd-net-device.cc +++ b/src/devices/csma-cd/csma-cd-net-device.cc @@ -35,6 +35,15 @@ NS_DEBUG_COMPONENT_DEFINE ("CsmaCdNetDevice"); namespace ns3 { +CsmaCdNetDevice::CsmaCdNetDevice (Ptr node) + : NetDevice (node, Eui48Address::Allocate ().ConvertTo ()), + m_bps (DataRate (0xffffffff)) +{ + NS_DEBUG ("CsmaCdNetDevice::CsmaCdNetDevice (" << node << ")"); + m_encapMode = IP_ARP; + Init(true, true); +} + CsmaCdNetDevice::CsmaCdNetDevice (Ptr node, Eui48Address addr, CsmaCdEncapsulationMode encapMode) : NetDevice(node, addr.ConvertTo ()), diff --git a/src/devices/csma-cd/csma-cd-net-device.h b/src/devices/csma-cd/csma-cd-net-device.h index 6b1341b89..05a327127 100644 --- a/src/devices/csma-cd/csma-cd-net-device.h +++ b/src/devices/csma-cd/csma-cd-net-device.h @@ -83,6 +83,7 @@ enum CsmaCdEncapsulationMode { LLC, /**< LLC packet encapsulation */ }; + CsmaCdNetDevice (Ptr node); /** * Construct a CsmaCdNetDevice * From a4c3028881afbd37b1a4e1648dba7e744a667024 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 10:29:40 +0200 Subject: [PATCH 38/48] example code. --- examples/csma-cd-packet-socket.cc | 136 ++++++++++++++++++++++++++++++ examples/wscript | 1 + 2 files changed, 137 insertions(+) create mode 100644 examples/csma-cd-packet-socket.cc diff --git a/examples/csma-cd-packet-socket.cc b/examples/csma-cd-packet-socket.cc new file mode 100644 index 000000000..9f3cc1877 --- /dev/null +++ b/examples/csma-cd-packet-socket.cc @@ -0,0 +1,136 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * 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 + */ + +// Port of ns-2/tcl/ex/simple.tcl to ns-3 +// +// Network topology +// +// n0 n1 n2 n3 +// | | | | +// ===================== +// +// - CBR/UDP flows from n0 to n1, and from n3 to n0 +// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec. +// (i.e., DataRate of 448,000 bps) +// - DropTail queues +// - Tracing of queues and packet receptions to file "csma-cd-one-subnet.tr" + +#include +#include +#include +#include + +#include "ns3/command-line.h" +#include "ns3/default-value.h" +#include "ns3/ptr.h" +#include "ns3/random-variable.h" +#include "ns3/debug.h" + +#include "ns3/simulator.h" +#include "ns3/nstime.h" +#include "ns3/data-rate.h" + +#include "ns3/ascii-trace.h" +#include "ns3/pcap-trace.h" +#include "ns3/internet-node.h" +#include "ns3/csma-cd-channel.h" +#include "ns3/csma-cd-net-device.h" +#include "ns3/eui48-address.h" +#include "ns3/packet-socket-address.h" +#include "ns3/socket.h" +#include "ns3/onoff-application.h" +#include "ns3/queue.h" + +using namespace ns3; + +static Ptr +CreateCsmaCdDevice (Ptr node, Ptr channel) +{ + Ptr device = Create (node); + device->Attach (channel); + Ptr queue = Queue::CreateDefault (); + device->AddQueue (queue); + return device; +} + + +int main (int argc, char *argv[]) +{ + CommandLine::Parse (argc, argv); + + // Here, we will explicitly create four nodes. In more sophisticated + // topologies, we could configure a node factory. + Ptr n0 = Create (); + Ptr n1 = Create (); + Ptr n2 = Create (); + Ptr n3 = Create (); + + // create the shared medium used by all csma/cd devices. + Ptr channel = Create (DataRate(5000000), MilliSeconds(2)); + + // use a helper function to connect our nodes to the shared channel. + Ptr n0If = CreateCsmaCdDevice (n0, channel); + Ptr n1If = CreateCsmaCdDevice (n1, channel); + Ptr n2If = CreateCsmaCdDevice (n2, channel); + Ptr n3If = CreateCsmaCdDevice (n3, channel); + + + // create the address which identifies n1 from n0 + PacketSocketAddress n0ToN1; + n0ToN1.SetDevice (n0If->GetIfIndex ()); // set outgoing interface for outgoing packets + n0ToN1.SetPhysicalAddress (n1If->GetAddress ()); // set destination address for outgoing packets + n0ToN1.SetProtocol (2); // set arbitrary protocol for outgoing packets + + // create the address which identifies n0 from n3 + PacketSocketAddress n3ToN0; + n3ToN0.SetDevice (n3If->GetIfIndex ()); + n3ToN0.SetPhysicalAddress (n0If->GetAddress ()); + n3ToN0.SetProtocol (3); + + // Create the OnOff application to send raw datagrams of size + // 210 bytes at a rate of 448 Kb/s + // from n0 to n1 + Ptr ooff = Create ( + n0, + n0ToN1.ConvertTo (), + "Packet", + ConstantVariable(1), + ConstantVariable(0)); + // Start the application + ooff->Start(Seconds(1.0)); + ooff->Stop (Seconds(10.0)); + + // Create a similar flow from n3 to n0, starting at time 1.1 seconds + ooff = Create ( + n3, + n3ToN0.ConvertTo (), + "Packet", + ConstantVariable(1), + ConstantVariable(0)); + // Start the application + ooff->Start(Seconds(1.1)); + ooff->Stop (Seconds(10.0)); + + // Configure tracing of all enqueue, dequeue, and NetDevice receive events + // Trace output will be sent to the csma-cd-packet-socket.tr file + AsciiTrace asciitrace ("csma-cd-packet-socket.tr"); + asciitrace.TraceAllNetDeviceRx (); + asciitrace.TraceAllQueues (); + + Simulator::Run (); + + Simulator::Destroy (); +} diff --git a/examples/wscript b/examples/wscript index e1b960ddc..f455a2c1b 100644 --- a/examples/wscript +++ b/examples/wscript @@ -11,4 +11,5 @@ def build(bld): obj = create_ns_prog('simple-point-to-point', 'simple-point-to-point.cc', deps=['point-to-point', 'internet-node']) obj = create_ns_prog('csma-cd-one-subnet', 'csma-cd-one-subnet.cc', deps=['csma-cd', 'internet-node']) + obj = create_ns_prog('csma-cd-packet-socket', 'csma-cd-packet-socket.cc', deps=['csma-cd', 'internet-node']) From dd3ca3aa63ba90e93d31fb3fa95bbb8e4be75f6e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 12:33:44 +0200 Subject: [PATCH 39/48] replace Node::DoCreateTraceResolver with Node::DoFillTraceResolver --- src/common/array-trace-resolver.h | 12 +++++----- src/internet-node/ascii-trace.cc | 4 ++-- src/internet-node/internet-node.cc | 14 +++++------- src/internet-node/internet-node.h | 2 +- src/internet-node/ipv4-l3-protocol.cc | 4 ++-- src/node/node-list.cc | 12 +++------- src/node/node.cc | 32 ++++++++++++++++++++------- src/node/node.h | 18 ++++++++++----- 8 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/common/array-trace-resolver.h b/src/common/array-trace-resolver.h index 572df6e57..2dc3409d6 100644 --- a/src/common/array-trace-resolver.h +++ b/src/common/array-trace-resolver.h @@ -81,11 +81,11 @@ public: */ ArrayTraceResolver (TraceContext const &context, Callback getSize, - Callback get); + Callback get); private: virtual TraceResolverList DoLookup (std::string id) const; Callback m_getSize; - Callback m_get; + Callback m_get; }; }//namespace ns3 @@ -108,7 +108,7 @@ ArrayTraceResolver::Index::operator uint32_t () template ArrayTraceResolver::ArrayTraceResolver (TraceContext const &context, Callback getSize, - Callback get) + Callback get) : TraceResolver (context), m_getSize (getSize), m_get (get) @@ -122,10 +122,10 @@ ArrayTraceResolver::DoLookup (std::string id) const { for (uint32_t i = 0; i < m_getSize (); i++) { - TraceContext context = GetContext (); + TraceContext context = GetContext (); typename ArrayTraceResolver::Index index = typename ArrayTraceResolver::Index (i); - context.Add (index); - list.push_back (m_get (i)->CreateTraceResolver (context)); + context.Add (index); + list.push_back (m_get (i)->CreateTraceResolver (context)); } } return list; diff --git a/src/internet-node/ascii-trace.cc b/src/internet-node/ascii-trace.cc index 1314f2150..33bcffc25 100644 --- a/src/internet-node/ascii-trace.cc +++ b/src/internet-node/ascii-trace.cc @@ -47,14 +47,14 @@ void AsciiTrace::TraceAllQueues (void) { Packet::EnableMetadata (); - TraceRoot::Connect ("/nodes/*/ipv4/interfaces/*/netdevice/queue/*", + TraceRoot::Connect ("/nodes/*/devices/*/queue/*", MakeCallback (&AsciiTrace::LogDevQueue, this)); } void AsciiTrace::TraceAllNetDeviceRx (void) { Packet::EnableMetadata (); - TraceRoot::Connect ("/nodes/*/ipv4/interfaces/*/netdevice/rx", + TraceRoot::Connect ("/nodes/*/devices/*/rx", MakeCallback (&AsciiTrace::LogDevRx, this)); } diff --git a/src/internet-node/internet-node.cc b/src/internet-node/internet-node.cc index fa0bb1471..ffb44b79b 100644 --- a/src/internet-node/internet-node.cc +++ b/src/internet-node/internet-node.cc @@ -76,16 +76,14 @@ InternetNode::Construct (void) } -TraceResolver * -InternetNode::DoCreateTraceResolver (TraceContext const &context) +void +InternetNode::DoFillTraceResolver (CompositeTraceResolver &resolver) { - CompositeTraceResolver *resolver = new CompositeTraceResolver (context); + Node::DoFillTraceResolver (resolver); Ptr ipv4 = QueryInterface (Ipv4L3Protocol::iid); - resolver->Add ("ipv4", - MakeCallback (&Ipv4L3Protocol::CreateTraceResolver, PeekPointer (ipv4)), - InternetNode::IPV4); - - return resolver; + resolver.Add ("ipv4", + MakeCallback (&Ipv4L3Protocol::CreateTraceResolver, PeekPointer (ipv4)), + InternetNode::IPV4); } void diff --git a/src/internet-node/internet-node.h b/src/internet-node/internet-node.h index 1d63edae5..6047a1717 100644 --- a/src/internet-node/internet-node.h +++ b/src/internet-node/internet-node.h @@ -46,7 +46,7 @@ public: protected: virtual void DoDispose(void); private: - virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context); + virtual void DoFillTraceResolver (CompositeTraceResolver &resolver); bool ReceiveFromDevice (Ptr device, const Packet &p, uint16_t protocolNumber) const; void Construct (void); }; diff --git a/src/internet-node/ipv4-l3-protocol.cc b/src/internet-node/ipv4-l3-protocol.cc index 31c3935f5..a012d814c 100644 --- a/src/internet-node/ipv4-l3-protocol.cc +++ b/src/internet-node/ipv4-l3-protocol.cc @@ -99,8 +99,8 @@ Ipv4L3Protocol::CreateTraceResolver (TraceContext const &context) TraceResolver * Ipv4L3Protocol::InterfacesCreateTraceResolver (TraceContext const &context) const { - ArrayTraceResolver *resolver = - new ArrayTraceResolver + ArrayTraceResolver *resolver = + new ArrayTraceResolver (context, MakeCallback (&Ipv4L3Protocol::GetNInterfaces, this), MakeCallback (&Ipv4L3Protocol::GetInterface, this)); diff --git a/src/node/node-list.cc b/src/node/node-list.cc index 4fed7eb5e..118e19585 100644 --- a/src/node/node-list.cc +++ b/src/node/node-list.cc @@ -53,7 +53,6 @@ public: NodeList::Iterator Begin (void); NodeList::Iterator End (void); TraceResolver *CreateTraceResolver (TraceContext const &context); - Node *PeekNode (uint32_t n); Ptr GetNode (uint32_t n); uint32_t GetNNodes (void); @@ -99,11 +98,6 @@ NodeListPriv::GetNNodes (void) { return m_nodes.size (); } -Node * -NodeListPriv::PeekNode (uint32_t n) -{ - return PeekPointer (m_nodes[n]); -} Ptr NodeListPriv::GetNode (uint32_t n) @@ -115,11 +109,11 @@ NodeListPriv::GetNode (uint32_t n) TraceResolver * NodeListPriv::CreateTraceResolver (TraceContext const &context) { - ArrayTraceResolver *resolver = - new ArrayTraceResolver + ArrayTraceResolver > *resolver = + new ArrayTraceResolver > (context, MakeCallback (&NodeListPriv::GetNNodes, this), - MakeCallback (&NodeListPriv::PeekNode, this)); + MakeCallback (&NodeListPriv::GetNode, this)); return resolver; } diff --git a/src/node/node.cc b/src/node/node.cc index 7aaa0216a..caad3d9e1 100644 --- a/src/node/node.cc +++ b/src/node/node.cc @@ -24,7 +24,7 @@ #include "application.h" #include "packet-socket-factory.h" #include "ns3/simulator.h" -#include "ns3/empty-trace-resolver.h" +#include "ns3/composite-trace-resolver.h" namespace ns3{ @@ -59,7 +59,9 @@ Node::~Node () TraceResolver * Node::CreateTraceResolver (TraceContext const &context) { - return DoCreateTraceResolver (context); + CompositeTraceResolver *resolver = new CompositeTraceResolver (context); + DoFillTraceResolver (*resolver); + return resolver; } uint32_t @@ -120,8 +122,27 @@ Node::GetNApplications (void) const return m_applications.size (); } +TraceResolver * +Node::CreateDevicesTraceResolver (const TraceContext &context) +{ + ArrayTraceResolver > *resolver = + new ArrayTraceResolver > (context, + MakeCallback (&Node::GetNDevices, this), + MakeCallback (&Node::GetDevice, this)); + + return resolver; +} -void Node::DoDispose() +void +Node::DoFillTraceResolver (CompositeTraceResolver &resolver) +{ + resolver.Add ("devices", + MakeCallback (&Node::CreateDevicesTraceResolver, this), + Node::DEVICES); +} + +void +Node::DoDispose() { for (std::vector >::iterator i = m_devices.begin (); i != m_devices.end (); i++) @@ -142,11 +163,6 @@ void Node::DoDispose() Object::DoDispose (); } -TraceResolver * -Node::DoCreateTraceResolver (TraceContext const &context) -{ - return new EmptyTraceResolver (context); -} void Node::NotifyDeviceAdded (Ptr device) {} diff --git a/src/node/node.h b/src/node/node.h index eea8089a5..e7e34d2c2 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -34,6 +34,7 @@ class NetDevice; class Application; class Packet; class Address; +class CompositeTraceResolver; /** * \brief A network Node. @@ -175,14 +176,17 @@ protected: * end of their own DoDispose method. */ virtual void DoDispose (void); -private: /** - * \param context the trace context - * \returns a trace resolver to the user. The user must delete it. + * \param resolver the resolver to store trace sources in. * - * Subclasses must implement this method. + * If a subclass wants to add new traces to a Node, it needs + * to override this method and record the new trace sources + * in the input resolver. Subclasses also _must_ chain up to + * their parent's DoFillTraceResolver method prior + * to recording they own trace sources. */ - virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context); + virtual void DoFillTraceResolver (CompositeTraceResolver &resolver); +private: /** * \param device the device added to this Node. * @@ -196,7 +200,11 @@ private: bool ReceiveFromDevice (Ptr device, const Packet &packet, uint16_t protocol, const Address &from); void Construct (void); + TraceResolver *CreateDevicesTraceResolver (const TraceContext &context); + enum TraceSource { + DEVICES + }; struct ProtocolHandlerEntry { ProtocolHandler handler; uint16_t protocol; From 707b722c36cf96cec95be75d12051f35cdce8b3a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 13:53:48 +0200 Subject: [PATCH 40/48] fix tracing --- examples/csma-cd-packet-socket.cc | 4 +-- src/internet-node/ascii-trace.cc | 52 ++++------------------------ src/internet-node/ascii-trace.h | 1 - src/internet-node/ipv4-l3-protocol.h | 2 +- src/node/node-list.h | 2 +- src/node/node.cc | 11 ++---- src/node/node.h | 2 ++ src/node/packet-socket-address.cc | 43 ++++++++++++++--------- src/node/packet-socket-address.h | 15 ++++---- src/node/packet-socket.cc | 37 ++++++++++++-------- src/node/packet-socket.h | 1 + 11 files changed, 70 insertions(+), 100 deletions(-) diff --git a/examples/csma-cd-packet-socket.cc b/examples/csma-cd-packet-socket.cc index 9f3cc1877..f19249789 100644 --- a/examples/csma-cd-packet-socket.cc +++ b/examples/csma-cd-packet-socket.cc @@ -90,13 +90,13 @@ int main (int argc, char *argv[]) // create the address which identifies n1 from n0 PacketSocketAddress n0ToN1; - n0ToN1.SetDevice (n0If->GetIfIndex ()); // set outgoing interface for outgoing packets + n0ToN1.SetSingleDevice (n0If->GetIfIndex ()); // set outgoing interface for outgoing packets n0ToN1.SetPhysicalAddress (n1If->GetAddress ()); // set destination address for outgoing packets n0ToN1.SetProtocol (2); // set arbitrary protocol for outgoing packets // create the address which identifies n0 from n3 PacketSocketAddress n3ToN0; - n3ToN0.SetDevice (n3If->GetIfIndex ()); + n3ToN0.SetSingleDevice (n3If->GetIfIndex ()); n3ToN0.SetPhysicalAddress (n0If->GetAddress ()); n3ToN0.SetProtocol (3); diff --git a/src/internet-node/ascii-trace.cc b/src/internet-node/ascii-trace.cc index 33bcffc25..b4e1430cb 100644 --- a/src/internet-node/ascii-trace.cc +++ b/src/internet-node/ascii-trace.cc @@ -26,12 +26,6 @@ #include "ns3/node.h" #include "ns3/queue.h" #include "ns3/node-list.h" -#include "ns3/llc-snap-header.h" - -#include "ipv4-l3-protocol.h" -#include "arp-header.h" -#include "udp-header.h" -#include "ipv4-header.h" namespace ns3 { @@ -58,40 +52,6 @@ AsciiTrace::TraceAllNetDeviceRx (void) MakeCallback (&AsciiTrace::LogDevRx, this)); } -void -AsciiTrace::PrintType (Packet const &packet) -{ - Packet p = packet; - LlcSnapHeader llc; - p.RemoveHeader (llc); - switch (llc.GetType ()) - { - case 0x0800: { - Ipv4Header ipv4; - p.RemoveHeader (ipv4); - if (ipv4.GetProtocol () == 17) - { - UdpHeader udp; - p.RemoveHeader (udp); - m_os << "udp size=" << p.GetSize (); - } - } break; - case 0x0806: { - ArpHeader arp; - p.RemoveHeader (arp); - m_os << "arp "; - if (arp.IsRequest ()) - { - m_os << "request"; - } - else - { - m_os << "reply "; - } - } break; - } -} - void AsciiTrace::LogDevQueue (TraceContext const &context, Packet const &packet) { @@ -113,9 +73,9 @@ AsciiTrace::LogDevQueue (TraceContext const &context, Packet const &packet) NodeList::NodeIndex nodeIndex; context.Get (nodeIndex); m_os << "node=" << NodeList::GetNode (nodeIndex)->GetId () << " "; - Ipv4L3Protocol::InterfaceIndex interfaceIndex; - context.Get (interfaceIndex); - m_os << "interface=" << interfaceIndex << " "; + Node::NetDeviceIndex deviceIndex; + context.Get (deviceIndex); + m_os << "device=" << deviceIndex << " "; m_os << "pkt-uid=" << packet.GetUid () << " "; packet.Print (m_os); m_os << std::endl; @@ -127,9 +87,9 @@ AsciiTrace::LogDevRx (TraceContext const &context, Packet &p) NodeList::NodeIndex nodeIndex; context.Get (nodeIndex); m_os << "node=" << NodeList::GetNode (nodeIndex)->GetId () << " "; - Ipv4L3Protocol::InterfaceIndex interfaceIndex; - context.Get (interfaceIndex); - m_os << "interface=" << interfaceIndex << " "; + Node::NetDeviceIndex deviceIndex; + context.Get (deviceIndex); + m_os << "device=" << deviceIndex << " "; m_os << "pkt-uid=" << p.GetUid () << " "; p.Print (m_os); m_os << std::endl; diff --git a/src/internet-node/ascii-trace.h b/src/internet-node/ascii-trace.h index 5d7554665..56b4195cb 100644 --- a/src/internet-node/ascii-trace.h +++ b/src/internet-node/ascii-trace.h @@ -37,7 +37,6 @@ public: void TraceAllQueues (void); void TraceAllNetDeviceRx (void); private: - void PrintType (Packet const &p); void LogDevQueue (TraceContext const &context, const Packet &p); void LogDevRx (TraceContext const &context, Packet &p); std::ofstream m_os; diff --git a/src/internet-node/ipv4-l3-protocol.h b/src/internet-node/ipv4-l3-protocol.h index d9abe535a..6130872cd 100644 --- a/src/internet-node/ipv4-l3-protocol.h +++ b/src/internet-node/ipv4-l3-protocol.h @@ -57,7 +57,7 @@ public: DROP, INTERFACES, }; - typedef ArrayTraceResolver::Index InterfaceIndex; + typedef ArrayTraceResolver::Index InterfaceIndex; Ipv4L3Protocol(Ptr node); virtual ~Ipv4L3Protocol (); diff --git a/src/node/node-list.h b/src/node/node-list.h index 607a47d71..cb4be3cc2 100644 --- a/src/node/node-list.h +++ b/src/node/node-list.h @@ -40,7 +40,7 @@ class TraceContext; class NodeList { public: - typedef ArrayTraceResolver::Index NodeIndex; + typedef ArrayTraceResolver >::Index NodeIndex; typedef std::vector< Ptr >::iterator Iterator; /** diff --git a/src/node/node.cc b/src/node/node.cc index caad3d9e1..05b61c096 100644 --- a/src/node/node.cc +++ b/src/node/node.cc @@ -79,8 +79,8 @@ Node::GetSystemId (void) const uint32_t Node::AddDevice (Ptr device) { - m_devices.push_back (device); uint32_t index = m_devices.size (); + m_devices.push_back (device); device->SetIfIndex(index); device->SetReceiveCallback (MakeCallback (&Node::ReceiveFromDevice, this)); NotifyDeviceAdded (device); @@ -89,14 +89,7 @@ Node::AddDevice (Ptr device) Ptr Node::GetDevice (uint32_t index) const { - if (index == 0) - { - return 0; - } - else - { - return m_devices[index - 1]; - } + return m_devices[index]; } uint32_t Node::GetNDevices (void) const diff --git a/src/node/node.h b/src/node/node.h index e7e34d2c2..e2e99dee3 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -25,6 +25,7 @@ #include "ns3/object.h" #include "ns3/callback.h" +#include "ns3/array-trace-resolver.h" namespace ns3 { @@ -57,6 +58,7 @@ class Node : public Object { public: static const InterfaceId iid; + typedef ArrayTraceResolver >::Index NetDeviceIndex; /** * Must be invoked by subclasses only. diff --git a/src/node/packet-socket-address.cc b/src/node/packet-socket-address.cc index 2c5fcb7b8..de7ae5980 100644 --- a/src/node/packet-socket-address.cc +++ b/src/node/packet-socket-address.cc @@ -29,22 +29,17 @@ PacketSocketAddress::SetProtocol (uint16_t protocol) { m_protocol = protocol; } -void -PacketSocketAddress::SetDevice (uint32_t index) +void +PacketSocketAddress::SetAllDevices (void) { - m_device = index; + m_isSingleDevice = false; + m_device = 0; } void -PacketSocketAddress::SetDevice (Ptr device) +PacketSocketAddress::SetSingleDevice (uint32_t index) { - if (device == 0) - { - m_device = 0; - } - else - { - m_device = device->GetIfIndex (); - } + m_isSingleDevice = true; + m_device = index; } void PacketSocketAddress::SetPhysicalAddress (const Address address) @@ -57,8 +52,13 @@ PacketSocketAddress::GetProtocol (void) const { return m_protocol; } +bool +PacketSocketAddress::IsSingleDevice (void) const +{ + return m_isSingleDevice; +} uint32_t -PacketSocketAddress::GetDevice (void) const +PacketSocketAddress::GetSingleDevice (void) const { return m_device; } @@ -79,8 +79,9 @@ PacketSocketAddress::ConvertTo (void) const buffer[3] = (m_device >> 16) & 0xff; buffer[4] = (m_device >> 8) & 0xff; buffer[5] = (m_device >> 0) & 0xff; - uint32_t copied = m_address.CopyAllTo (buffer + 6, Address::MAX_SIZE - 6); - return Address (GetType (), buffer, 6 + copied); + buffer[6] = m_isSingleDevice?1:0; + uint32_t copied = m_address.CopyAllTo (buffer + 7, Address::MAX_SIZE - 7); + return Address (GetType (), buffer, 7 + copied); } PacketSocketAddress PacketSocketAddress::ConvertFrom (const Address &address) @@ -97,11 +98,19 @@ PacketSocketAddress::ConvertFrom (const Address &address) device |= buffer[4]; device <<= 8; device |= buffer[5]; + bool isSingleDevice = (buffer[6] == 1)?true:false; Address physical; - physical.CopyAllFrom (buffer + 6, Address::MAX_SIZE - 6); + physical.CopyAllFrom (buffer + 7, Address::MAX_SIZE - 7); PacketSocketAddress ad; ad.SetProtocol (protocol); - ad.SetDevice (device); + if (isSingleDevice) + { + ad.SetSingleDevice (device); + } + else + { + ad.SetAllDevices (); + } ad.SetPhysicalAddress (physical); return ad; } diff --git a/src/node/packet-socket-address.h b/src/node/packet-socket-address.h index ca138ad43..ad313f8c4 100644 --- a/src/node/packet-socket-address.h +++ b/src/node/packet-socket-address.h @@ -35,18 +35,14 @@ class PacketSocketAddress public: PacketSocketAddress (); void SetProtocol (uint16_t protocol); - /** - * \param index of NetDevice. - * - * index zero is reserved to identify _all_ - * Netdevices. - */ - void SetDevice (uint32_t index); - void SetDevice (Ptr device); + + void SetAllDevices (void); + void SetSingleDevice (uint32_t device); void SetPhysicalAddress (const Address address); uint16_t GetProtocol (void) const; - uint32_t GetDevice (void) const; + uint32_t GetSingleDevice (void) const; + bool IsSingleDevice (void) const; Address GetPhysicalAddress (void) const; /** @@ -69,6 +65,7 @@ class PacketSocketAddress private: static uint8_t GetType (void); uint16_t m_protocol; + bool m_isSingleDevice; uint32_t m_device; Address m_address; }; diff --git a/src/node/packet-socket.cc b/src/node/packet-socket.cc index 9bce9bafa..e2c510e27 100644 --- a/src/node/packet-socket.cc +++ b/src/node/packet-socket.cc @@ -64,7 +64,7 @@ PacketSocket::Bind (void) { PacketSocketAddress address; address.SetProtocol (0); - address.SetDevice (0); + address.SetAllDevices (); return DoBind (address); } int @@ -93,12 +93,21 @@ PacketSocket::DoBind (const PacketSocketAddress &address) m_errno = ERROR_BADF; return -1; } - Ptr dev = m_node->GetDevice (address.GetDevice ()); + Ptr dev ; + if (address.IsSingleDevice ()) + { + dev = 0; + } + else + { + m_node->GetDevice (address.GetSingleDevice ()); + } m_node->RegisterProtocolHandler (MakeCallback (&PacketSocket::ForwardUp, this), address.GetProtocol (), dev); m_state = STATE_BOUND; m_protocol = address.GetProtocol (); - m_device = address.GetDevice (); + m_isSingleDevice = address.IsSingleDevice (); + m_device = address.GetSingleDevice (); return 0; } @@ -254,9 +263,17 @@ PacketSocket::DoSendTo(const Address &address, bool error = false; Address dest = ad.GetPhysicalAddress (); - if (ad.GetDevice () == 0) + if (ad.IsSingleDevice ()) { - for (uint32_t i = 1; i <= m_node->GetNDevices (); i++) + Ptr device = m_node->GetDevice (ad.GetSingleDevice ()); + if (!device->Send (p, dest, ad.GetProtocol ())) + { + error = true; + } + } + else + { + for (uint32_t i = 0; i < m_node->GetNDevices (); i++) { Ptr device = m_node->GetDevice (i); if (!device->Send (p, dest, ad.GetProtocol ())) @@ -265,14 +282,6 @@ PacketSocket::DoSendTo(const Address &address, } } } - else - { - Ptr device = m_node->GetDevice (ad.GetDevice ()); - if (!device->Send (p, dest, ad.GetProtocol ())) - { - error = true; - } - } if (!error && !dataSent.IsNull ()) { dataSent (this, p.GetSize ()); @@ -314,7 +323,7 @@ PacketSocket::ForwardUp (Ptr device, const Packet &packet, PacketSocketAddress address; address.SetPhysicalAddress (from); - address.SetDevice (device->GetIfIndex ()); + address.SetSingleDevice (device->GetIfIndex ()); address.SetProtocol (protocol); NS_DEBUG ("PacketSocket::ForwardUp: UID is " << packet.GetUid() diff --git a/src/node/packet-socket.h b/src/node/packet-socket.h index 790420970..f18279949 100644 --- a/src/node/packet-socket.h +++ b/src/node/packet-socket.h @@ -121,6 +121,7 @@ private: bool m_shutdownRecv; enum State m_state; uint16_t m_protocol; + bool m_isSingleDevice; uint32_t m_device; Address m_destAddr; /// Default destination address }; From b632880fdecd41c62100596b58be0545196dde78 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 13:54:09 +0200 Subject: [PATCH 41/48] update address size --- src/node/address.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/address.h b/src/node/address.h index 0d3b0da83..aedc940d4 100644 --- a/src/node/address.h +++ b/src/node/address.h @@ -63,7 +63,7 @@ public: * The maximum size of a byte buffer which * can be stored in an Address instance. */ - MAX_SIZE = 14 + MAX_SIZE = 30 }; /** From 5c19b67791e6f0714a0981a59f12782d227b75d0 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 5 Aug 2007 14:34:23 +0200 Subject: [PATCH 42/48] fix optimized build --- src/routing/global-routing/global-router-interface.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/routing/global-routing/global-router-interface.cc b/src/routing/global-routing/global-router-interface.cc index 24a84be06..e70d2ec04 100644 --- a/src/routing/global-routing/global-router-interface.cc +++ b/src/routing/global-routing/global-router-interface.cc @@ -535,8 +535,7 @@ GlobalRouter::GetLSA (uint32_t n, GlobalRouterLSA &lsa) const GlobalRouter::GetAdjacent(Ptr nd, Ptr ch) const { - uint32_t nDevices = ch->GetNDevices(); - NS_ASSERT_MSG(nDevices == 2, + NS_ASSERT_MSG(ch->GetNDevices() == 2, "GlobalRouter::GetAdjacent (): Channel with other than two devices"); // // This is a point to point channel with two endpoints. Get both of them. From fbbd5dc9a68c3901fb1b8d57cef5d608f22fbaff Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 6 Aug 2007 10:06:25 +0200 Subject: [PATCH 43/48] use the InetSocketAddress API for ip sockets --- examples/simple-global-routing.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/examples/simple-global-routing.cc b/examples/simple-global-routing.cc index 2a005589a..4398cbb6a 100644 --- a/examples/simple-global-routing.cc +++ b/examples/simple-global-routing.cc @@ -58,10 +58,10 @@ #include "ns3/internet-node.h" #include "ns3/point-to-point-channel.h" #include "ns3/point-to-point-net-device.h" -#include "ns3/mac-address.h" #include "ns3/ipv4-address.h" #include "ns3/ipv4.h" #include "ns3/socket.h" +#include "ns3/inet-socket-address.h" #include "ns3/ipv4-route.h" #include "ns3/point-to-point-topology.h" #include "ns3/onoff-application.h" @@ -82,7 +82,7 @@ int main (int argc, char *argv[]) DebugComponentEnable ("PointToPointChannel"); DebugComponentEnable ("PointToPointNetDevice"); DebugComponentEnable ("GlobalRouter"); - DebugComponentEnable ("GlobalRouteManager"); + DebugComponentEnable ("GlobalRouteMaager"); #endif // Set up some default values for the simulation. Use the Bind () @@ -143,8 +143,7 @@ int main (int argc, char *argv[]) // 210 bytes at a rate of 448 Kb/s Ptr ooff = Create ( n0, - Ipv4Address ("10.1.3.2"), - 80, + InetSocketAddress ("10.1.3.2", 80).ConvertTo (), "Udp", ConstantVariable (1), ConstantVariable (0)); @@ -155,8 +154,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, - Ipv4Address ("10.1.2.1"), - 80, + InetSocketAddress ("10.1.2.1", 80).ConvertTo (), "Udp", ConstantVariable (1), ConstantVariable (0)); From dcdca3338d34bc5d36be9b9f39a665de8cc39487 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 6 Aug 2007 10:09:18 +0200 Subject: [PATCH 44/48] fix memory leak --- src/routing/global-routing/global-route-manager-impl.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/routing/global-routing/global-route-manager-impl.cc b/src/routing/global-routing/global-route-manager-impl.cc index 03578cb96..142f613be 100644 --- a/src/routing/global-routing/global-route-manager-impl.cc +++ b/src/routing/global-routing/global-route-manager-impl.cc @@ -1191,6 +1191,7 @@ GlobalRouteManagerImpl::SPFVertexAddParent (SPFVertex* v) // --------------------------------------------------------------------------- #include "ns3/test.h" +#include "ns3/simulator.h" namespace ns3 { @@ -1392,6 +1393,9 @@ GlobalRouteManagerImplTest::RunTests (void) // because the NodeList is empty srm->DebugSPFCalculate (lsa0->GetLinkStateId ()); // node n0 + Simulator::Run (); + Simulator::Destroy (); + // This delete clears the srm, which deletes the LSDB, which clears // all of the LSAs, which each destroys the attached LinkRecords. delete srm; From 4946bbcf97c40b7960d465b7b33719204b5101b4 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 6 Aug 2007 10:29:19 +0200 Subject: [PATCH 45/48] main should return a value --- examples/simple-global-routing.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/simple-global-routing.cc b/examples/simple-global-routing.cc index 4398cbb6a..24bfcf72d 100644 --- a/examples/simple-global-routing.cc +++ b/examples/simple-global-routing.cc @@ -178,4 +178,6 @@ int main (int argc, char *argv[]) Simulator::Run (); Simulator::Destroy (); + + return 0; } From f750bcefab796d6136d74663af67dcdf20871639 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 6 Aug 2007 10:29:28 +0200 Subject: [PATCH 46/48] fix memory leak --- src/routing/global-routing/global-router-interface.cc | 7 +++++++ src/routing/global-routing/global-router-interface.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/routing/global-routing/global-router-interface.cc b/src/routing/global-routing/global-router-interface.cc index e70d2ec04..778d91bcc 100644 --- a/src/routing/global-routing/global-router-interface.cc +++ b/src/routing/global-routing/global-router-interface.cc @@ -332,6 +332,13 @@ GlobalRouter::~GlobalRouter () ClearLSAs(); } +void +GlobalRouter::DoDispose () +{ + m_node = 0; + Object::DoDispose (); +} + void GlobalRouter::ClearLSAs () { diff --git a/src/routing/global-routing/global-router-interface.h b/src/routing/global-routing/global-router-interface.h index b2927d631..737643d9e 100644 --- a/src/routing/global-routing/global-router-interface.h +++ b/src/routing/global-routing/global-router-interface.h @@ -565,6 +565,8 @@ protected: Ipv4Address m_routerId; private: + // inherited from Object + virtual void DoDispose (void); /** * @brief Global Router copy construction is disallowed. */ From 988d512b772f3d10af1f4b6b518bbcceffa7e0ce Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 6 Aug 2007 11:55:17 +0100 Subject: [PATCH 47/48] Correct misspelled BreakpointFallback function implementation; fixes OS X build. --- src/core/breakpoint.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/breakpoint.cc b/src/core/breakpoint.cc index c748792bc..41efcbb13 100644 --- a/src/core/breakpoint.cc +++ b/src/core/breakpoint.cc @@ -31,7 +31,7 @@ namespace ns3 { #ifdef HAVE_SIGNAL_H void -Breakpoint (void) +BreakpointFallback (void) { raise (SIGTRAP); } @@ -39,7 +39,7 @@ Breakpoint (void) #else void -Breakpoint (void) +BreakpointFallback (void) { int *a = 0; /** From caf9c39afa465ba2bc7b9cbd1b8290524ab94a5f Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 6 Aug 2007 15:45:29 +0200 Subject: [PATCH 48/48] make the previous merge actually work --- examples/simple-global-routing.cc | 4 ++-- src/devices/csma-cd/csma-cd-net-device.cc | 4 ++-- src/node/eui48-address.cc | 4 ---- src/node/eui64-address.cc | 17 +++++++---------- src/node/eui64-address.h | 21 +++++---------------- src/node/packet-socket-address.cc | 5 +++++ src/node/packet-socket-address.h | 3 ++- src/node/packet-socket.cc | 4 ++-- 8 files changed, 25 insertions(+), 37 deletions(-) diff --git a/examples/simple-global-routing.cc b/examples/simple-global-routing.cc index 24bfcf72d..3458bb84f 100644 --- a/examples/simple-global-routing.cc +++ b/examples/simple-global-routing.cc @@ -143,7 +143,7 @@ int main (int argc, char *argv[]) // 210 bytes at a rate of 448 Kb/s Ptr ooff = Create ( n0, - InetSocketAddress ("10.1.3.2", 80).ConvertTo (), + InetSocketAddress ("10.1.3.2", 80), "Udp", ConstantVariable (1), ConstantVariable (0)); @@ -154,7 +154,7 @@ int main (int argc, char *argv[]) // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, - InetSocketAddress ("10.1.2.1", 80).ConvertTo (), + InetSocketAddress ("10.1.2.1", 80), "Udp", ConstantVariable (1), ConstantVariable (0)); diff --git a/src/devices/csma-cd/csma-cd-net-device.cc b/src/devices/csma-cd/csma-cd-net-device.cc index 3d8919fcb..141daf68c 100644 --- a/src/devices/csma-cd/csma-cd-net-device.cc +++ b/src/devices/csma-cd/csma-cd-net-device.cc @@ -36,7 +36,7 @@ NS_DEBUG_COMPONENT_DEFINE ("CsmaCdNetDevice"); namespace ns3 { CsmaCdNetDevice::CsmaCdNetDevice (Ptr node) - : NetDevice (node, Eui48Address::Allocate ().ConvertTo ()), + : NetDevice (node, Eui48Address::Allocate ()), m_bps (DataRate (0xffffffff)) { NS_DEBUG ("CsmaCdNetDevice::CsmaCdNetDevice (" << node << ")"); @@ -516,7 +516,7 @@ CsmaCdNetDevice::Receive (const Packet& packet) } m_rxTrace (p); - ForwardUp (p, protocol, header.GetSource ().ConvertTo ()); + ForwardUp (p, protocol, header.GetSource ()); return; drop: m_dropTrace (p); diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index 4df16c829..d8b62257b 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -99,10 +99,6 @@ Eui48Address::operator Address () { return ConvertTo (); } -Eui48Address::Eui48Address (const Address &address) -{ - *this = ConvertFrom (address); -} Address Eui48Address::ConvertTo (void) const { diff --git a/src/node/eui64-address.cc b/src/node/eui64-address.cc index e29150542..3b91353fe 100644 --- a/src/node/eui64-address.cc +++ b/src/node/eui64-address.cc @@ -95,19 +95,10 @@ Eui64Address::IsMatchingType (const Address &address) { return address.CheckCompatible (GetType (), 8); } -Eui48Address::operator Address () +Eui64Address::operator Address () { return ConvertTo (); } -Eui48Address::Eui48Address (const Address &address) -{ - *this = ConvertFrom (address); -} -Address -Eui64Address::ConvertTo (void) const -{ - return Address (GetType (), m_address, 8); -} Eui64Address Eui64Address::ConvertFrom (const Address &address) { @@ -116,6 +107,12 @@ Eui64Address::ConvertFrom (const Address &address) address.CopyTo (retval.m_address); return retval; } +Address +Eui64Address::ConvertTo (void) const +{ + return Address (GetType (), m_address, 8); +} + Eui64Address Eui64Address::Allocate (void) { diff --git a/src/node/eui64-address.h b/src/node/eui64-address.h index f4ef91d48..98b930057 100644 --- a/src/node/eui64-address.h +++ b/src/node/eui64-address.h @@ -28,9 +28,9 @@ namespace ns3 { class Address; /** - * \brief an EUI-48 address + * \brief an EUI-64 address * - * This class can contain 48 bit IEEE addresses. + * This class can contain 64 bit IEEE addresses. */ class Eui64Address { @@ -63,30 +63,19 @@ public: operator Address (); /** * \param address a polymorphic address - * \returns a new Eui48Address from the polymorphic address + * \returns a new Eui64Address from the polymorphic address * * This function performs a type check and asserts if the * type of the input address is not compatible with an - * Eui48Address. + * Eui64Address. */ - static Eui48Address ConvertFrom (const Address &address); + static Eui64Address ConvertFrom (const Address &address); /** * \returns true if the address matches, false otherwise. */ static bool IsMatchingType (const Address &address); /** -<<<<<<< /auto/fugue/u/fugue/home/mlacage/code/ns-3-dev/src/node/eui64-address.h - * \param address a polymorphic address - * - * Convert a polymorphic address to an Eui64Address instance. - * The conversion performs a type check. - */ - static Eui64Address ConvertFrom (const Address &address); - /** * Allocate a new Eui64Address. -======= - * Allocate a new Eui48Address. ->>>>>>> /tmp/eui48-address.h~other.OBFjbL */ static Eui64Address Allocate (void); private: diff --git a/src/node/packet-socket-address.cc b/src/node/packet-socket-address.cc index de7ae5980..1b16b9cb5 100644 --- a/src/node/packet-socket-address.cc +++ b/src/node/packet-socket-address.cc @@ -68,6 +68,11 @@ PacketSocketAddress::GetPhysicalAddress (void) const return m_address; } +PacketSocketAddress::operator Address () const +{ + return ConvertTo (); +} + Address PacketSocketAddress::ConvertTo (void) const { diff --git a/src/node/packet-socket-address.h b/src/node/packet-socket-address.h index ad313f8c4..e280db8df 100644 --- a/src/node/packet-socket-address.h +++ b/src/node/packet-socket-address.h @@ -50,7 +50,7 @@ class PacketSocketAddress * * Convert an instance of this class to a polymorphic Address instance. */ - Address ConvertTo (void) const; + operator Address () const; /** * \param address a polymorphic address * @@ -64,6 +64,7 @@ class PacketSocketAddress static bool IsMatchingType (const Address &address); private: static uint8_t GetType (void); + Address ConvertTo (void) const; uint16_t m_protocol; bool m_isSingleDevice; uint32_t m_device; diff --git a/src/node/packet-socket.cc b/src/node/packet-socket.cc index e2c510e27..700f5f8b5 100644 --- a/src/node/packet-socket.cc +++ b/src/node/packet-socket.cc @@ -330,11 +330,11 @@ PacketSocket::ForwardUp (Ptr device, const Packet &packet, << " PacketSocket " << this); if (!m_dummyRxCallback.IsNull ()) { - m_dummyRxCallback (this, p.GetSize (), address.ConvertTo ()); + m_dummyRxCallback (this, p.GetSize (), address); } if (!m_rxCallback.IsNull ()) { - m_rxCallback (this, p.PeekData (), p.GetSize (), address.ConvertTo ()); + m_rxCallback (this, p.PeekData (), p.GetSize (), address); } }