1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2007 INRIA |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License version 2 as |
|
7 * published by the Free Software Foundation; |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 * |
|
18 * Authors: |
|
19 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>, |
|
20 */ |
|
21 |
|
22 #include "ns3/packet.h" |
|
23 #include "ns3/log.h" |
|
24 #include "ns3/node.h" |
|
25 #include "ns3/net-device.h" |
|
26 #include "ns3/address.h" |
|
27 #include "ns3/pointer.h" |
|
28 |
|
29 #include "arp-ipv4-interface.h" |
|
30 #include "ipv4-l3-protocol.h" |
|
31 #include "arp-l3-protocol.h" |
|
32 #include "arp-cache.h" |
|
33 |
|
34 NS_LOG_COMPONENT_DEFINE ("ArpIpv4Interface"); |
|
35 |
|
36 namespace ns3 { |
|
37 |
|
38 TypeId |
|
39 ArpIpv4Interface::GetTypeId (void) |
|
40 { |
|
41 static TypeId tid = TypeId ("ns3::ArpIpv4Interface") |
|
42 .SetParent<Ipv4Interface> () |
|
43 .AddAttribute ("ArpCache", |
|
44 "The arp cache for this ipv4 interface", |
|
45 PointerValue (0), |
|
46 MakePointerAccessor (&ArpIpv4Interface::m_cache), |
|
47 MakePointerChecker<ArpIpv4Interface> ()) |
|
48 ; |
|
49 return tid; |
|
50 } |
|
51 |
|
52 ArpIpv4Interface::ArpIpv4Interface () |
|
53 : m_node (0), |
|
54 m_device (0) |
|
55 { |
|
56 NS_LOG_FUNCTION (this); |
|
57 } |
|
58 |
|
59 ArpIpv4Interface::~ArpIpv4Interface () |
|
60 { |
|
61 NS_LOG_FUNCTION (this); |
|
62 } |
|
63 |
|
64 void |
|
65 ArpIpv4Interface::DoDispose (void) |
|
66 { |
|
67 NS_LOG_FUNCTION (this); |
|
68 m_device = 0; |
|
69 m_cache = 0; |
|
70 Ipv4Interface::DoDispose (); |
|
71 } |
|
72 |
|
73 void |
|
74 ArpIpv4Interface::SetNode (Ptr<Node> node) |
|
75 { |
|
76 m_node = node; |
|
77 DoSetup (); |
|
78 } |
|
79 void |
|
80 ArpIpv4Interface::SetDevice (Ptr<NetDevice> device) |
|
81 { |
|
82 m_device = device; |
|
83 DoSetup (); |
|
84 } |
|
85 |
|
86 Ptr<NetDevice> |
|
87 ArpIpv4Interface::GetDevice (void) const |
|
88 { |
|
89 return m_device; |
|
90 } |
|
91 |
|
92 void |
|
93 ArpIpv4Interface::DoSetup (void) |
|
94 { |
|
95 if (m_node == 0 || m_device == 0) |
|
96 { |
|
97 return; |
|
98 } |
|
99 Ptr<ArpL3Protocol> arp = m_node->GetObject<ArpL3Protocol> (); |
|
100 m_cache = arp->CreateCache (m_device, this); |
|
101 } |
|
102 |
|
103 void |
|
104 ArpIpv4Interface::SendTo (Ptr<Packet> p, Ipv4Address dest) |
|
105 { |
|
106 NS_LOG_FUNCTION (this << p << dest); |
|
107 |
|
108 NS_ASSERT (GetDevice () != 0); |
|
109 // XXX multi-address case |
|
110 if (dest == GetAddress (0).GetLocal ()) |
|
111 { |
|
112 Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> (); |
|
113 |
|
114 ipv4->Receive (0, p, Ipv4L3Protocol::PROT_NUMBER, |
|
115 GetDevice ()->GetBroadcast (), |
|
116 GetDevice ()->GetBroadcast (), |
|
117 NetDevice::PACKET_HOST // note: linux uses PACKET_LOOPBACK here |
|
118 ); |
|
119 return; |
|
120 } |
|
121 if (m_device->NeedsArp ()) |
|
122 { |
|
123 NS_LOG_LOGIC ("Needs ARP"); |
|
124 Ptr<ArpL3Protocol> arp = |
|
125 m_node->GetObject<ArpL3Protocol> (); |
|
126 Address hardwareDestination; |
|
127 bool found; |
|
128 // XXX multi-address case |
|
129 if (dest.IsBroadcast () || |
|
130 dest.IsSubnetDirectedBroadcast (GetAddress (0).GetMask ()) ) |
|
131 { |
|
132 NS_LOG_LOGIC ("IsBroadcast"); |
|
133 hardwareDestination = GetDevice ()->GetBroadcast (); |
|
134 found = true; |
|
135 } |
|
136 else if (dest.IsMulticast ()) |
|
137 { |
|
138 NS_LOG_LOGIC ("IsMulticast"); |
|
139 NS_ASSERT_MSG(GetDevice ()->IsMulticast (), |
|
140 "ArpIpv4Interface::SendTo (): Sending multicast packet over " |
|
141 "non-multicast device"); |
|
142 |
|
143 hardwareDestination = GetDevice ()->GetMulticast(dest); |
|
144 found = true; |
|
145 } |
|
146 else |
|
147 { |
|
148 NS_LOG_LOGIC ("ARP Lookup"); |
|
149 found = arp->Lookup (p, dest, GetDevice (), m_cache, &hardwareDestination); |
|
150 } |
|
151 |
|
152 if (found) |
|
153 { |
|
154 NS_LOG_LOGIC ("Address Resolved. Send."); |
|
155 GetDevice ()->Send (p, hardwareDestination, |
|
156 Ipv4L3Protocol::PROT_NUMBER); |
|
157 } |
|
158 } |
|
159 else |
|
160 { |
|
161 NS_LOG_LOGIC ("Doesn't need ARP"); |
|
162 GetDevice ()->Send (p, GetDevice ()->GetBroadcast (), |
|
163 Ipv4L3Protocol::PROT_NUMBER); |
|
164 } |
|
165 } |
|
166 |
|
167 }//namespace ns3 |
|