shared Mac48 multicast code
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 25 Aug 2008 10:02:52 -0700
changeset 3549 4eaf02702f17
parent 3548 e5ab96db540e
child 3550 42988e5cce5e
shared Mac48 multicast code
src/devices/csma/csma-net-device.cc
src/devices/wifi/wifi-net-device.cc
src/node/mac48-address.cc
src/node/mac48-address.h
src/node/simple-net-device.cc
--- a/src/devices/csma/csma-net-device.cc	Mon Aug 25 09:13:05 2008 -0700
+++ b/src/devices/csma/csma-net-device.cc	Mon Aug 25 10:02:52 2008 -0700
@@ -670,27 +670,6 @@
   NS_LOG_LOGIC ("Pkt source is " << header.GetSource ());
   NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ());
 
-  //
-  // An IP host group address is mapped to an Ethernet multicast address
-  // by placing the low-order 23-bits of the IP address into the low-order
-  // 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex).
-  //
-  // We are going to receive all packets destined to any multicast address,
-  // which means clearing the low-order 23 bits the header destination 
-  //
-  Mac48Address mcDest;
-  uint8_t      mcBuf[6];
-
-  header.GetDestination ().CopyTo (mcBuf);
-  mcBuf[3] &= 0x80;
-  mcBuf[4] = 0;
-  mcBuf[5] = 0;
-  mcDest.CopyFrom (mcBuf);
-
-  Mac48Address multicast = Mac48Address::ConvertFrom (GetMulticast ());
-  Mac48Address broadcast = Mac48Address::ConvertFrom (GetBroadcast ());
-  Mac48Address destination = Mac48Address::ConvertFrom (GetAddress ());
-
   if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt (packet) )
     {
       NS_LOG_LOGIC ("Dropping pkt due to error model ");
@@ -723,17 +702,17 @@
 
       PacketType packetType;
       
-      if (header.GetDestination () == broadcast)
+      if (header.GetDestination ().IsBroadcast ())
         {
           packetType = PACKET_BROADCAST;
           m_rxTrace (originalPacket);
         }
-      else if (mcDest == multicast)
+      else if (header.GetDestination ().IsMulticast ())
         {
           packetType = PACKET_MULTICAST;          
           m_rxTrace (originalPacket);
         }
-      else if (header.GetDestination () == destination)
+      else if (header.GetDestination () == m_address)
         {
           packetType = PACKET_HOST;
           m_rxTrace (originalPacket);
@@ -855,65 +834,24 @@
 CsmaNetDevice::GetMulticast (void) const
 {
   NS_LOG_FUNCTION_NOARGS ();
-  return Mac48Address ("01:00:5e:00:00:00");
+  return Mac48Address::GetMulticastPrefix ();
 }
 
   Address 
 CsmaNetDevice::MakeMulticastAddress (Ipv4Address multicastGroup) const
 {
   NS_LOG_FUNCTION (multicastGroup);
-  //
-  // First, get the generic multicast address.
-  //
-  Address hardwareDestination = GetMulticast ();
 
-  NS_LOG_LOGIC ("Device multicast address: " << hardwareDestination);
-
-  //
-  // It's our address, and we know we're playing with an EUI-48 address here
-  // primarily since we know that by construction, but also since the parameter
-  // is an Ipv4Address.
-  //
-  Mac48Address etherAddr = Mac48Address::ConvertFrom (hardwareDestination);
-
-  //
-  // We now have the multicast address in an abstract 48-bit container.  We 
-  // need to pull it out so we can play with it.  When we're done, we have the 
-  // high order bits in etherBuffer[0], etc.
-  //
-  uint8_t etherBuffer[6];
-  etherAddr.CopyTo (etherBuffer);
-
-  //
-  // Now we need to pull the raw bits out of the Ipv4 destination address.
-  //
-  uint8_t ipBuffer[4];
-  multicastGroup.Serialize (ipBuffer);
-
-  //
-  // RFC 1112 says that an Ipv4 host group address is mapped to an EUI-48
-  // multicast address by placing the low-order 23-bits of the IP address into 
-  // the low-order 23 bits of the Ethernet multicast address 
-  // 01-00-5E-00-00-00 (hex). 
-  //
-  etherBuffer[3] |= ipBuffer[1] & 0x7f;
-  etherBuffer[4] = ipBuffer[2];
-  etherBuffer[5] = ipBuffer[3];
-
-  //
-  // Now, etherBuffer has the desired ethernet multicast address.  We have to
-  // suck these bits back into the Mac48Address,
-  //
-  etherAddr.CopyFrom (etherBuffer);
+  Mac48Address ad = Mac48Address::GetMulticast (multicastGroup);
 
   //
   // Implicit conversion (operator Address ()) is defined for Mac48Address, so
   // use it by just returning the EUI-48 address which is automagically converted
   // to an Address.
   //
-  NS_LOG_LOGIC ("multicast address is " << etherAddr);
+  NS_LOG_LOGIC ("multicast address is " << ad);
 
-  return etherAddr;
+  return ad;
 }
 
   bool 
--- a/src/devices/wifi/wifi-net-device.cc	Mon Aug 25 09:13:05 2008 -0700
+++ b/src/devices/wifi/wifi-net-device.cc	Mon Aug 25 10:02:52 2008 -0700
@@ -255,12 +255,12 @@
 Address 
 WifiNetDevice::GetMulticast (void) const
 {
-  return Mac48Address ("01:00:5e:00:00:00");
+  return Mac48Address::GetMulticastPrefix ();
 }
 Address 
 WifiNetDevice::MakeMulticastAddress (Ipv4Address multicastGroup) const
 {
-  return GetMulticast ();
+  return Mac48Address::GetMulticast (multicastGroup);
 }
 bool 
 WifiNetDevice::IsPointToPoint (void) const
--- a/src/node/mac48-address.cc	Mon Aug 25 09:13:05 2008 -0700
+++ b/src/node/mac48-address.cc	Mon Aug 25 10:02:52 2008 -0700
@@ -144,6 +144,18 @@
 bool 
 Mac48Address::IsMulticast (void) const
 {
+  uint8_t mcBuf[6];
+  CopyTo (mcBuf);
+  mcBuf[3] &= 0x80;
+  mcBuf[4] = 0;
+  mcBuf[5] = 0;
+  Mac48Address prefix;
+  prefix.CopyFrom (mcBuf);
+  return prefix == Mac48Address::GetMulticastPrefix ();
+}
+bool 
+Mac48Address::IsGroup (void) const
+{
   return (m_address[0] & 0x01) == 0x01;
 }
 Mac48Address
@@ -152,6 +164,48 @@
   static Mac48Address broadcast = Mac48Address ("ff:ff:ff:ff:ff:ff");
   return broadcast;
 }
+Mac48Address 
+Mac48Address::GetMulticastPrefix (void)
+{
+  static Mac48Address multicast = Mac48Address ("01:00:5e:00:00:00");
+  return multicast;
+}
+Mac48Address 
+Mac48Address::GetMulticast (Ipv4Address multicastGroup)
+{
+  Mac48Address etherAddr = Mac48Address::GetMulticastPrefix ();
+  //
+  // We now have the multicast address in an abstract 48-bit container.  We 
+  // need to pull it out so we can play with it.  When we're done, we have the 
+  // high order bits in etherBuffer[0], etc.
+  //
+  uint8_t etherBuffer[6];
+  etherAddr.CopyTo (etherBuffer);
+
+  //
+  // Now we need to pull the raw bits out of the Ipv4 destination address.
+  //
+  uint8_t ipBuffer[4];
+  multicastGroup.Serialize (ipBuffer);
+
+  //
+  // RFC 1112 says that an Ipv4 host group address is mapped to an EUI-48
+  // multicast address by placing the low-order 23-bits of the IP address into 
+  // the low-order 23 bits of the Ethernet multicast address 
+  // 01-00-5E-00-00-00 (hex). 
+  //
+  etherBuffer[3] |= ipBuffer[1] & 0x7f;
+  etherBuffer[4] = ipBuffer[2];
+  etherBuffer[5] = ipBuffer[3];
+
+  //
+  // Now, etherBuffer has the desired ethernet multicast address.  We have to
+  // suck these bits back into the Mac48Address,
+  //
+  Mac48Address result;
+  result.CopyFrom (etherBuffer);
+  return result;
+}
 
 bool operator == (const Mac48Address &a, const Mac48Address &b)
 {
--- a/src/node/mac48-address.h	Mon Aug 25 09:13:05 2008 -0700
+++ b/src/node/mac48-address.h	Mon Aug 25 10:02:52 2008 -0700
@@ -24,6 +24,7 @@
 #include <ostream>
 #include "ns3/attribute.h"
 #include "ns3/attribute-helper.h"
+#include "ipv4-address.h"
 
 namespace ns3 {
 
@@ -92,12 +93,25 @@
    * \returns true if this is a multicast address, false otherwise.
    */
   bool IsMulticast (void) const;
+  /**
+   * \returns true if the group bit is set, false otherwise.
+   */
+  bool IsGroup (void) const;
 
   /**
    * \returns the broadcast address
    */
   static Mac48Address GetBroadcast (void);
 
+  /**
+   * \returns a multicast address
+   */
+  static Mac48Address GetMulticast (Ipv4Address address);
+
+  /**
+   * \returns the multicast prefix (01:00:5e:00:00:00).
+   */
+  static Mac48Address GetMulticastPrefix (void);
 private:
   /**
    * \returns a new Address instance
--- a/src/node/simple-net-device.cc	Mon Aug 25 09:13:05 2008 -0700
+++ b/src/node/simple-net-device.cc	Mon Aug 25 10:02:52 2008 -0700
@@ -142,12 +142,12 @@
 Address 
 SimpleNetDevice::GetMulticast (void) const
 {
-  return Mac48Address ("01:00:5e:00:00:00");
+  return Mac48Address::GetMulticastPrefix ();
 }
 Address 
 SimpleNetDevice::MakeMulticastAddress (Ipv4Address multicastGroup) const
 {
-  return Mac48Address ("01:00:5e:00:00:00");
+  return Mac48Address::GetMulticast (multicastGroup);
 }
 bool 
 SimpleNetDevice::IsPointToPoint (void) const