A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
simple-net-device.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "simple-net-device.h"
21 #include "simple-channel.h"
22 #include "ns3/node.h"
23 #include "ns3/packet.h"
24 #include "ns3/log.h"
25 #include "ns3/pointer.h"
26 #include "ns3/error-model.h"
27 #include "ns3/trace-source-accessor.h"
28 
29 NS_LOG_COMPONENT_DEFINE ("SimpleNetDevice");
30 
31 namespace ns3 {
32 
33 NS_OBJECT_ENSURE_REGISTERED (SimpleNetDevice);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::SimpleNetDevice")
39  .SetParent<NetDevice> ()
40  .AddConstructor<SimpleNetDevice> ()
41  .AddAttribute ("ReceiveErrorModel",
42  "The receiver error model used to simulate packet loss",
43  PointerValue (),
44  MakePointerAccessor (&SimpleNetDevice::m_receiveErrorModel),
45  MakePointerChecker<ErrorModel> ())
46  .AddTraceSource ("PhyRxDrop",
47  "Trace source indicating a packet has been dropped by the device during reception",
49  ;
50  return tid;
51 }
52 
54  : m_channel (0),
55  m_node (0),
56  m_mtu (0xffff),
57  m_ifIndex (0)
58 {
59 }
60 
61 void
62 SimpleNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
63  Mac48Address to, Mac48Address from)
64 {
65  NS_LOG_FUNCTION (packet << protocol << to << from);
66  NetDevice::PacketType packetType;
67 
69  {
70  m_phyRxDropTrace (packet);
71  return;
72  }
73 
74  if (to == m_address)
75  {
76  packetType = NetDevice::PACKET_HOST;
77  }
78  else if (to.IsBroadcast ())
79  {
80  packetType = NetDevice::PACKET_HOST;
81  }
82  else if (to.IsGroup ())
83  {
84  packetType = NetDevice::PACKET_MULTICAST;
85  }
86  else
87  {
88  packetType = NetDevice::PACKET_OTHERHOST;
89  }
90  m_rxCallback (this, packet, protocol, from);
91  if (!m_promiscCallback.IsNull ())
92  {
93  m_promiscCallback (this, packet, protocol, from, to, packetType);
94  }
95 }
96 
97 void
99 {
100  m_channel = channel;
101  m_channel->Add (this);
102 }
103 
104 void
106 {
107  m_receiveErrorModel = em;
108 }
109 
110 void
111 SimpleNetDevice::SetIfIndex (const uint32_t index)
112 {
113  m_ifIndex = index;
114 }
115 uint32_t
117 {
118  return m_ifIndex;
119 }
122 {
123  return m_channel;
124 }
125 void
127 {
129 }
130 Address
132 {
133  //
134  // Implicit conversion from Mac48Address to Address
135  //
136  return m_address;
137 }
138 bool
139 SimpleNetDevice::SetMtu (const uint16_t mtu)
140 {
141  m_mtu = mtu;
142  return true;
143 }
144 uint16_t
146 {
147  return m_mtu;
148 }
149 bool
151 {
152  return true;
153 }
154 void
156 {}
157 bool
159 {
160  return true;
161 }
162 Address
164 {
165  return Mac48Address ("ff:ff:ff:ff:ff:ff");
166 }
167 bool
169 {
170  return false;
171 }
172 Address
174 {
175  return Mac48Address::GetMulticast (multicastGroup);
176 }
177 
179 {
180  return Mac48Address::GetMulticast (addr);
181 }
182 
183 bool
185 {
186  return false;
187 }
188 
189 bool
191 {
192  return false;
193 }
194 
195 bool
196 SimpleNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
197 {
198  NS_LOG_FUNCTION (packet << dest << protocolNumber);
200  m_channel->Send (packet, protocolNumber, to, m_address, this);
201  return true;
202 }
203 bool
204 SimpleNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
205 {
207  Mac48Address from = Mac48Address::ConvertFrom (source);
208  m_channel->Send (packet, protocolNumber, to, from, this);
209  return true;
210 }
211 
212 Ptr<Node>
214 {
215  return m_node;
216 }
217 void
219 {
220  m_node = node;
221 }
222 bool
224 {
225  return false;
226 }
227 void
229 {
230  m_rxCallback = cb;
231 }
232 
233 void
235 {
236  m_channel = 0;
237  m_node = 0;
240 }
241 
242 
243 void
245 {
246  m_promiscCallback = cb;
247 }
248 
249 bool
251 {
252  return true;
253 }
254 
255 } // namespace ns3