2007-07-31 09:09:31 +02:00
|
|
|
/* -*- 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<riley@ece.gatech.edu>
|
|
|
|
|
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
|
|
|
|
*/
|
2007-06-04 16:21:05 +02:00
|
|
|
#include "node.h"
|
2007-03-20 06:28:24 -07:00
|
|
|
#include "node-list.h"
|
2007-05-02 09:16:58 +02:00
|
|
|
#include "net-device.h"
|
2007-05-13 09:46:38 +02:00
|
|
|
#include "application.h"
|
2007-10-01 14:15:56 +02:00
|
|
|
#include "ns3/packet.h"
|
2007-05-02 10:33:39 +02:00
|
|
|
#include "ns3/simulator.h"
|
2008-02-26 18:03:59 +01:00
|
|
|
#include "ns3/object-vector.h"
|
|
|
|
|
#include "ns3/uinteger.h"
|
2008-07-16 16:06:50 +01:00
|
|
|
#include "ns3/log.h"
|
2008-08-06 16:06:44 +01:00
|
|
|
#include "ns3/assert.h"
|
2008-07-16 16:06:50 +01:00
|
|
|
|
|
|
|
|
NS_LOG_COMPONENT_DEFINE ("Node");
|
2007-02-08 11:11:24 +01:00
|
|
|
|
|
|
|
|
namespace ns3{
|
|
|
|
|
|
2008-01-03 11:39:45 +01:00
|
|
|
NS_OBJECT_ENSURE_REGISTERED (Node);
|
|
|
|
|
|
2008-01-15 12:36:22 +01:00
|
|
|
TypeId
|
2008-01-15 12:43:07 +01:00
|
|
|
Node::GetTypeId (void)
|
2008-01-02 10:33:39 +01:00
|
|
|
{
|
2008-03-13 12:56:49 -07:00
|
|
|
static TypeId tid = TypeId ("ns3::Node")
|
2008-02-26 18:03:59 +01:00
|
|
|
.SetParent<Object> ()
|
|
|
|
|
.AddAttribute ("DeviceList", "The list of devices associated to this Node.",
|
2008-04-17 13:42:25 -07:00
|
|
|
ObjectVectorValue (),
|
2008-02-26 18:03:59 +01:00
|
|
|
MakeObjectVectorAccessor (&Node::m_devices),
|
2008-04-09 14:58:52 -07:00
|
|
|
MakeObjectVectorChecker<NetDevice> ())
|
2008-02-26 18:03:59 +01:00
|
|
|
.AddAttribute ("ApplicationList", "The list of applications associated to this Node.",
|
2008-04-17 13:42:25 -07:00
|
|
|
ObjectVectorValue (),
|
2008-02-26 18:03:59 +01:00
|
|
|
MakeObjectVectorAccessor (&Node::m_applications),
|
2008-04-09 14:58:52 -07:00
|
|
|
MakeObjectVectorChecker<Application> ())
|
2008-02-26 18:03:59 +01:00
|
|
|
.AddAttribute ("Id", "The id (unique integer) of this Node.",
|
|
|
|
|
TypeId::ATTR_GET, // allow only getting it.
|
2008-04-17 13:42:25 -07:00
|
|
|
UintegerValue (0),
|
2008-02-26 18:03:59 +01:00
|
|
|
MakeUintegerAccessor (&Node::m_id),
|
|
|
|
|
MakeUintegerChecker<uint32_t> ())
|
|
|
|
|
;
|
2008-01-15 12:44:09 +01:00
|
|
|
return tid;
|
2008-01-02 10:33:39 +01:00
|
|
|
}
|
2007-05-03 11:08:13 +02:00
|
|
|
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::Node()
|
2007-05-25 10:56:03 +02:00
|
|
|
: m_id(0),
|
2007-05-02 13:44:41 +02:00
|
|
|
m_sid(0)
|
2007-02-12 16:01:18 +01:00
|
|
|
{
|
2007-08-01 08:58:18 +02:00
|
|
|
Construct ();
|
2007-03-19 22:19:38 -07:00
|
|
|
}
|
|
|
|
|
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::Node(uint32_t sid)
|
2007-05-25 10:56:03 +02:00
|
|
|
: m_id(0),
|
2007-05-02 13:44:41 +02:00
|
|
|
m_sid(sid)
|
2007-03-19 22:19:38 -07:00
|
|
|
{
|
2007-08-01 08:58:18 +02:00
|
|
|
Construct ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Node::Construct (void)
|
|
|
|
|
{
|
2007-05-02 13:44:41 +02:00
|
|
|
m_id = NodeList::Add (this);
|
2007-02-12 16:01:18 +01:00
|
|
|
}
|
2007-02-08 11:11:24 +01:00
|
|
|
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::~Node ()
|
2007-02-08 11:11:24 +01:00
|
|
|
{}
|
|
|
|
|
|
2007-02-12 16:01:18 +01:00
|
|
|
uint32_t
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::GetId (void) const
|
2007-02-12 16:01:18 +01:00
|
|
|
{
|
|
|
|
|
return m_id;
|
|
|
|
|
}
|
2007-03-21 23:17:11 -07:00
|
|
|
|
2007-02-12 16:01:18 +01:00
|
|
|
uint32_t
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::GetSystemId (void) const
|
2007-02-12 16:01:18 +01:00
|
|
|
{
|
|
|
|
|
return m_sid;
|
|
|
|
|
}
|
2007-02-08 11:11:24 +01:00
|
|
|
|
2007-04-30 10:16:04 +02:00
|
|
|
uint32_t
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::AddDevice (Ptr<NetDevice> device)
|
2007-04-30 10:16:04 +02:00
|
|
|
{
|
2007-08-01 08:58:18 +02:00
|
|
|
uint32_t index = m_devices.size ();
|
2007-08-01 13:53:48 +02:00
|
|
|
m_devices.push_back (device);
|
2008-03-13 11:10:38 -07:00
|
|
|
device->SetNode (this);
|
2007-05-04 12:17:14 -07:00
|
|
|
device->SetIfIndex(index);
|
2008-07-16 16:06:50 +01:00
|
|
|
device->SetReceiveCallback (MakeCallback (&Node::NonPromiscReceiveFromDevice, this));
|
2007-07-31 09:09:31 +02:00
|
|
|
NotifyDeviceAdded (device);
|
2007-04-30 10:16:04 +02:00
|
|
|
return index;
|
|
|
|
|
}
|
2007-05-10 20:19:26 +02:00
|
|
|
Ptr<NetDevice>
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::GetDevice (uint32_t index) const
|
2007-04-30 10:16:04 +02:00
|
|
|
{
|
2008-08-06 16:11:44 +01:00
|
|
|
NS_ASSERT_MSG (index < m_devices.size (), "Device index " << index <<
|
2008-08-06 16:06:44 +01:00
|
|
|
" is out of range (only have " << m_devices.size () << " devices).");
|
2007-08-01 13:53:48 +02:00
|
|
|
return m_devices[index];
|
2007-04-30 10:16:04 +02:00
|
|
|
}
|
|
|
|
|
uint32_t
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::GetNDevices (void) const
|
2007-04-30 10:16:04 +02:00
|
|
|
{
|
|
|
|
|
return m_devices.size ();
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-13 09:46:38 +02:00
|
|
|
uint32_t
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::AddApplication (Ptr<Application> application)
|
2007-05-13 09:46:38 +02:00
|
|
|
{
|
|
|
|
|
uint32_t index = m_applications.size ();
|
|
|
|
|
m_applications.push_back (application);
|
2008-03-13 11:10:38 -07:00
|
|
|
application->SetNode (this);
|
2007-05-13 09:46:38 +02:00
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
Ptr<Application>
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::GetApplication (uint32_t index) const
|
2007-05-13 09:46:38 +02:00
|
|
|
{
|
2008-08-06 16:11:44 +01:00
|
|
|
NS_ASSERT_MSG (index < m_applications.size (), "Application index " << index <<
|
2008-08-06 16:06:44 +01:00
|
|
|
" is out of range (only have " << m_applications.size () << " applications).");
|
2007-05-13 09:46:38 +02:00
|
|
|
return m_applications[index];
|
|
|
|
|
}
|
|
|
|
|
uint32_t
|
2007-06-04 16:17:01 +02:00
|
|
|
Node::GetNApplications (void) const
|
2007-05-13 09:46:38 +02:00
|
|
|
{
|
|
|
|
|
return m_applications.size ();
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-01 12:33:44 +02:00
|
|
|
void
|
|
|
|
|
Node::DoDispose()
|
2007-05-02 09:10:19 +02:00
|
|
|
{
|
2007-05-10 20:19:26 +02:00
|
|
|
for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
|
2007-05-02 09:16:58 +02:00
|
|
|
i != m_devices.end (); i++)
|
|
|
|
|
{
|
2007-05-10 20:19:26 +02:00
|
|
|
Ptr<NetDevice> device = *i;
|
2007-05-02 09:16:58 +02:00
|
|
|
device->Dispose ();
|
2007-05-10 20:19:26 +02:00
|
|
|
*i = 0;
|
2007-05-02 09:16:58 +02:00
|
|
|
}
|
2007-05-03 13:11:50 +02:00
|
|
|
m_devices.clear ();
|
2007-05-13 09:46:38 +02:00
|
|
|
for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
|
|
|
|
|
i != m_applications.end (); i++)
|
|
|
|
|
{
|
|
|
|
|
Ptr<Application> application = *i;
|
|
|
|
|
application->Dispose ();
|
|
|
|
|
*i = 0;
|
|
|
|
|
}
|
|
|
|
|
m_applications.clear ();
|
2007-05-25 10:56:03 +02:00
|
|
|
Object::DoDispose ();
|
2007-05-02 09:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
2007-07-31 09:09:31 +02:00
|
|
|
void
|
|
|
|
|
Node::NotifyDeviceAdded (Ptr<NetDevice> device)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Node::RegisterProtocolHandler (ProtocolHandler handler,
|
|
|
|
|
uint16_t protocolType,
|
2008-07-16 16:06:50 +01:00
|
|
|
Ptr<NetDevice> device,
|
|
|
|
|
bool promiscuous)
|
2007-07-31 09:09:31 +02:00
|
|
|
{
|
|
|
|
|
struct Node::ProtocolHandlerEntry entry;
|
|
|
|
|
entry.handler = handler;
|
|
|
|
|
entry.protocol = protocolType;
|
|
|
|
|
entry.device = device;
|
2008-07-16 16:06:50 +01:00
|
|
|
entry.promiscuous = promiscuous;
|
|
|
|
|
|
|
|
|
|
// On demand enable promiscuous mode in netdevices
|
|
|
|
|
if (promiscuous)
|
|
|
|
|
{
|
|
|
|
|
if (device == 0)
|
|
|
|
|
{
|
|
|
|
|
for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
|
|
|
|
|
i != m_devices.end (); i++)
|
|
|
|
|
{
|
|
|
|
|
Ptr<NetDevice> dev = *i;
|
2008-09-02 11:28:03 -07:00
|
|
|
if (dev->SupportsSendFrom ())
|
2008-07-16 16:06:50 +01:00
|
|
|
{
|
|
|
|
|
dev->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-09-02 11:28:03 -07:00
|
|
|
if (device->SupportsSendFrom ())
|
2008-07-16 16:06:50 +01:00
|
|
|
{
|
|
|
|
|
device->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_WARN ("Protocol handler request promiscuous mode for a specific netdevice,"
|
|
|
|
|
" but netdevice does not support promiscuous mode.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-31 09:09:31 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-16 16:06:50 +01:00
|
|
|
bool
|
2008-08-25 09:13:05 -07:00
|
|
|
Node::PromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
|
2008-07-16 16:06:50 +01:00
|
|
|
const Address &from, const Address &to, NetDevice::PacketType packetType)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION(device->GetName ());
|
|
|
|
|
return ReceiveFromDevice (device, packet, protocol, from, to, packetType, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2008-08-25 09:13:05 -07:00
|
|
|
Node::NonPromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
|
2008-07-16 16:06:50 +01:00
|
|
|
const Address &from)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION(device->GetName ());
|
|
|
|
|
return ReceiveFromDevice (device, packet, protocol, from, from, NetDevice::PacketType (0), false);
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-31 09:09:31 +02:00
|
|
|
bool
|
2008-08-25 09:13:05 -07:00
|
|
|
Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
|
2008-07-16 16:06:50 +01:00
|
|
|
const Address &from, const Address &to, NetDevice::PacketType packetType, bool promiscuous)
|
2007-07-31 09:09:31 +02:00
|
|
|
{
|
2008-07-16 16:06:50 +01:00
|
|
|
NS_LOG_FUNCTION(device->GetName ());
|
2007-07-31 09:09:31 +02:00
|
|
|
bool found = false;
|
2008-05-14 18:15:26 +01:00
|
|
|
|
2007-07-31 09:09:31 +02:00
|
|
|
for (ProtocolHandlerList::iterator i = m_handlers.begin ();
|
|
|
|
|
i != m_handlers.end (); i++)
|
|
|
|
|
{
|
|
|
|
|
if (i->device == 0 ||
|
|
|
|
|
(i->device != 0 && i->device == device))
|
|
|
|
|
{
|
2007-08-01 08:58:18 +02:00
|
|
|
if (i->protocol == 0 ||
|
|
|
|
|
i->protocol == protocol)
|
2007-07-31 09:09:31 +02:00
|
|
|
{
|
2008-07-16 16:06:50 +01:00
|
|
|
if (promiscuous == i->promiscuous)
|
|
|
|
|
{
|
2008-08-27 11:39:31 -07:00
|
|
|
i->handler (device, packet, protocol, from, to, packetType);
|
2008-07-16 16:06:50 +01:00
|
|
|
found = true;
|
|
|
|
|
}
|
2008-06-30 19:25:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-08 11:11:24 +01:00
|
|
|
}//namespace ns3
|