00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "ns3/packet.h"
00023 #include "ns3/log.h"
00024 #include "ns3/node.h"
00025 #include "ns3/net-device.h"
00026 #include "ns3/address.h"
00027 #include "ns3/pointer.h"
00028
00029 #include "arp-ipv4-interface.h"
00030 #include "ipv4-l3-protocol.h"
00031 #include "arp-l3-protocol.h"
00032 #include "arp-cache.h"
00033
00034 NS_LOG_COMPONENT_DEFINE ("ArpIpv4Interface");
00035
00036 namespace ns3 {
00037
00038 TypeId
00039 ArpIpv4Interface::GetTypeId (void)
00040 {
00041 static TypeId tid = TypeId ("ns3::ArpIpv4Interface")
00042 .SetParent<Ipv4Interface> ()
00043 .AddAttribute ("ArpCache",
00044 "The arp cache for this ipv4 interface",
00045 PointerValue (0),
00046 MakePointerAccessor (&ArpIpv4Interface::m_cache),
00047 MakePointerChecker<ArpIpv4Interface> ())
00048 ;
00049 return tid;
00050 }
00051
00052 ArpIpv4Interface::ArpIpv4Interface ()
00053 : m_node (0),
00054 m_device (0)
00055 {
00056 NS_LOG_FUNCTION (this);
00057 }
00058
00059 ArpIpv4Interface::~ArpIpv4Interface ()
00060 {
00061 NS_LOG_FUNCTION (this);
00062 }
00063
00064 void
00065 ArpIpv4Interface::DoDispose (void)
00066 {
00067 NS_LOG_FUNCTION (this);
00068 m_device = 0;
00069 m_cache = 0;
00070 Ipv4Interface::DoDispose ();
00071 }
00072
00073 void
00074 ArpIpv4Interface::SetNode (Ptr<Node> node)
00075 {
00076 m_node = node;
00077 DoSetup ();
00078 }
00079 void
00080 ArpIpv4Interface::SetDevice (Ptr<NetDevice> device)
00081 {
00082 m_device = device;
00083 DoSetup ();
00084 }
00085
00086 Ptr<NetDevice>
00087 ArpIpv4Interface::GetDevice (void) const
00088 {
00089 return m_device;
00090 }
00091
00092 void
00093 ArpIpv4Interface::DoSetup (void)
00094 {
00095 if (m_node == 0 || m_device == 0)
00096 {
00097 return;
00098 }
00099 Ptr<ArpL3Protocol> arp = m_node->GetObject<ArpL3Protocol> ();
00100 m_cache = arp->CreateCache (m_device, this);
00101 }
00102
00103 void
00104 ArpIpv4Interface::SendTo (Ptr<Packet> p, Ipv4Address dest)
00105 {
00106 NS_LOG_FUNCTION (this << p << dest);
00107
00108 NS_ASSERT (GetDevice () != 0);
00109 if (dest == GetAddress ())
00110 {
00111 Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
00112
00113 ipv4->Receive (0, p, Ipv4L3Protocol::PROT_NUMBER,
00114 GetDevice ()->GetBroadcast (),
00115 GetDevice ()->GetBroadcast (),
00116 NetDevice::PACKET_HOST
00117 );
00118 return;
00119 }
00120 if (m_device->NeedsArp ())
00121 {
00122 NS_LOG_LOGIC ("Needs ARP");
00123 Ptr<ArpL3Protocol> arp =
00124 m_node->GetObject<ArpL3Protocol> ();
00125 Address hardwareDestination;
00126 bool found;
00127
00128 if (dest.IsBroadcast () ||
00129 dest.IsSubnetDirectedBroadcast (GetNetworkMask ()) )
00130 {
00131 NS_LOG_LOGIC ("IsBroadcast");
00132 hardwareDestination = GetDevice ()->GetBroadcast ();
00133 found = true;
00134 }
00135 else if (dest.IsMulticast ())
00136 {
00137 NS_LOG_LOGIC ("IsMulticast");
00138 NS_ASSERT_MSG(GetDevice ()->IsMulticast (),
00139 "ArpIpv4Interface::SendTo (): Sending multicast packet over "
00140 "non-multicast device");
00141
00142 hardwareDestination = GetDevice ()->GetMulticast(dest);
00143 found = true;
00144 }
00145 else
00146 {
00147 NS_LOG_LOGIC ("ARP Lookup");
00148 found = arp->Lookup (p, dest, GetDevice (), m_cache, &hardwareDestination);
00149 }
00150
00151 if (found)
00152 {
00153 NS_LOG_LOGIC ("Address Resolved. Send.");
00154 GetDevice ()->Send (p, hardwareDestination,
00155 Ipv4L3Protocol::PROT_NUMBER);
00156 }
00157 }
00158 else
00159 {
00160 NS_LOG_LOGIC ("Doesn't need ARP");
00161 GetDevice ()->Send (p, GetDevice ()->GetBroadcast (),
00162 Ipv4L3Protocol::PROT_NUMBER);
00163 }
00164 }
00165
00166 }