Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DhcpClient now accepts a NetDevice and not a Node
  • Loading branch information
TommyPec committed Jul 2, 2017
commit b068999bfbf4835e46a3838545c6a083cce9bbc2
29 changes: 12 additions & 17 deletions src/internet-apps/examples/dhcp-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,23 @@ main (int argc, char *argv[])
* Ipv4Address gateway = Ipv4Address ());
*
*/
DhcpServerHelper dhcp_server (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
Ipv4Address ("172.30.0.17"));
DhcpServerHelper dhcpServer (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
Ipv4Address ("172.30.0.17"));

ApplicationContainer ap_dhcp_server = dhcp_server.Install (router.Get (0));
ApplicationContainer ap_dhcp_server = dhcpServer.Install (router.Get (0));
ap_dhcp_server.Start (Seconds (1.0));
ap_dhcp_server.Stop (stopTime);

DhcpClientHelper dhcp_client (0);
ApplicationContainer ap_dhcp_client = dhcp_client.Install (nodes.Get (0));
ap_dhcp_client.Start (Seconds (1.0));
ap_dhcp_client.Stop (stopTime);
DhcpClientHelper dhcpClient;
NetDeviceContainer dhcpClientNetDevs;
dhcpClientNetDevs.Add (dev_net.Get (0));
dhcpClientNetDevs.Add (dev_net.Get (1));
dhcpClientNetDevs.Add (dev_net.Get (2));

DhcpClientHelper dhcp_client1 (0);
ApplicationContainer ap_dhcp_client1 = dhcp_client1.Install (nodes.Get (1));
ap_dhcp_client1.Start (Seconds (1.0));
ap_dhcp_client1.Stop (stopTime);

DhcpClientHelper dhcp_client2 (0);
ApplicationContainer ap_dhcp_client2 = dhcp_client2.Install (nodes.Get (2));
ap_dhcp_client2.Start (Seconds (1.0));
ap_dhcp_client2.Stop (stopTime);
ApplicationContainer dhcpClients = dhcpClient.Install (dhcpClientNetDevs);
dhcpClients.Start (Seconds (1.0));
dhcpClients.Stop (stopTime);

UdpEchoServerHelper echoServer (9);

Expand Down
25 changes: 19 additions & 6 deletions src/internet-apps/helper/dhcp-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@

namespace ns3 {

DhcpClientHelper::DhcpClientHelper (uint32_t device)
DhcpClientHelper::DhcpClientHelper ()
{
m_factory.SetTypeId (DhcpClient::GetTypeId ());
SetAttribute ("NetDevice", UintegerValue (device));
}

DhcpServerHelper::DhcpServerHelper (Ipv4Address pool_addr, Ipv4Mask pool_mask,
Expand Down Expand Up @@ -62,19 +61,33 @@ void DhcpServerHelper::SetAttribute (
m_factory.Set (name, value);
}

ApplicationContainer DhcpClientHelper::Install (Ptr<Node> node) const
ApplicationContainer DhcpClientHelper::Install (Ptr<NetDevice> netDevice) const
{
return ApplicationContainer (InstallPriv (node));
return ApplicationContainer (InstallPriv (netDevice));
}

ApplicationContainer DhcpClientHelper::Install (NetDeviceContainer netDevices) const
{
ApplicationContainer apps;
for (NetDeviceContainer::Iterator i = netDevices.Begin (); i != netDevices.End (); ++i)
{
apps.Add (InstallPriv (*i));
}
return apps;
}

ApplicationContainer DhcpServerHelper::Install (Ptr<Node> node) const
{
return ApplicationContainer (InstallPriv (node));
}

Ptr<Application> DhcpClientHelper::InstallPriv (Ptr<Node> node) const
Ptr<Application> DhcpClientHelper::InstallPriv (Ptr<NetDevice> netDevice) const
{
Ptr<Application> app = m_factory.Create<DhcpClient> ();
Ptr<Node> node = netDevice->GetNode ();
NS_ASSERT_MSG (node != 0, "DhcpClientHelper: NetDevice must be installed in a node before adding DhcpClient.");

Ptr<DhcpClient> app = DynamicCast <DhcpClient> (m_factory.Create<DhcpClient> ());
app->SetDhcpClientNetDevice (netDevice);
node->AddApplication (app);

return app;
Expand Down
18 changes: 12 additions & 6 deletions src/internet-apps/helper/dhcp-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include <stdint.h>
#include "ns3/application-container.h"
#include "ns3/node-container.h"
#include "ns3/net-device-container.h"
#include "ns3/object-factory.h"
#include "ns3/ipv4-address.h"

Expand All @@ -44,9 +44,8 @@ class DhcpClientHelper
public:
/**
* \brief Constructor of client helper
* \param device The interface on which DHCP client has to be installed
*/
DhcpClientHelper (uint32_t device);
DhcpClientHelper ();

/**
* \brief Function to set DHCP client attributes
Expand All @@ -57,18 +56,25 @@ class DhcpClientHelper

/**
* \brief Function to install DHCP client of a node
* \param node The node on which DHCP client application has to be installed
* \param netDevice The NetDevice on which DHCP client application has to be installed
* \return The application container with DHCP client installed
*/
ApplicationContainer Install (Ptr<Node> node) const;
ApplicationContainer Install (Ptr<NetDevice> netDevice) const;

/**
* \brief Function to install DHCP client of a node
* \param netDevices The NetDevices on which DHCP client application has to be installed
* \return The application container with DHCP client installed
*/
ApplicationContainer Install (NetDeviceContainer netDevices) const;

private:
/**
* \brief Function to install DHCP client on a node
* \param node The node on which DHCP client application has to be installed
* \return The pointer to the installed DHCP client
*/
Ptr<Application> InstallPriv (Ptr<Node> node) const;
Ptr<Application> InstallPriv (Ptr<NetDevice> netDevice) const;
ObjectFactory m_factory; //!< The subset of ns3::object for setting attributes
};

Expand Down
53 changes: 39 additions & 14 deletions src/internet-apps/model/dhcp-client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ DhcpClient::GetTypeId (void)
.SetParent<Application> ()
.AddConstructor<DhcpClient> ()
.SetGroupName ("Internet-Apps")
.AddAttribute ("NetDevice", "Index of netdevice of the node for DHCP",
UintegerValue (0),
MakeUintegerAccessor (&DhcpClient::m_device),
MakeUintegerChecker<uint32_t> ())
.AddAttribute ("RTRS", "Time for retransmission of Discover message",
TimeValue (Seconds (5)),
MakeTimeAccessor (&DhcpClient::m_rtrs),
Expand Down Expand Up @@ -90,9 +86,24 @@ DhcpClient::GetTypeId (void)
return tid;
}

DhcpClient::DhcpClient () : m_server (Ipv4Address::GetAny ())
DhcpClient::DhcpClient ()
{
NS_LOG_FUNCTION_NOARGS ();
m_server = Ipv4Address::GetAny ();
m_socket = 0;
m_refreshEvent = EventId ();
m_requestEvent = EventId ();
m_discoverEvent = EventId ();
m_rebindEvent = EventId ();
m_nextOfferEvent = EventId ();
m_timeout = EventId ();
}

DhcpClient::DhcpClient (Ptr<NetDevice> netDevice)
{
NS_LOG_FUNCTION_NOARGS ();
m_device = netDevice;
m_server = Ipv4Address::GetAny ();
m_socket = 0;
m_refreshEvent = EventId ();
m_requestEvent = EventId ();
Expand All @@ -107,6 +118,17 @@ DhcpClient::~DhcpClient ()
NS_LOG_FUNCTION_NOARGS ();
}

Ptr<NetDevice> DhcpClient::GetDhcpClientNetDevice (void)
{
return m_device;
}


void DhcpClient::SetDhcpClientNetDevice (Ptr<NetDevice> netDevice)
{
m_device = netDevice;
}

Ipv4Address DhcpClient::GetDhcpServer (void)
{
return m_server;
Expand All @@ -116,6 +138,9 @@ void
DhcpClient::DoDispose (void)
{
NS_LOG_FUNCTION_NOARGS ();

m_device = 0;

Application::DoDispose ();
}

Expand All @@ -135,11 +160,11 @@ DhcpClient::StartApplication (void)
m_myAddress = Ipv4Address ("0.0.0.0");
m_gateway = Ipv4Address ("0.0.0.0");
Ptr<Ipv4> ipv4 = GetNode ()->GetObject<Ipv4> ();
uint32_t ifIndex = ipv4->GetInterfaceForDevice (GetNode ()->GetDevice (m_device));
uint32_t ifIndex = ipv4->GetInterfaceForDevice (m_device);

// We need to cleanup the type from the stored chaddr, or later we'll fail to compare it.
// Moreover, the length is always 16, because chaddr is 16 bytes.
Address myAddress = GetNode ()->GetDevice (m_device)->GetAddress ();
Address myAddress = m_device->GetAddress ();
NS_LOG_INFO ("My address is " << myAddress);
uint8_t addr[Address::MAX_SIZE];
uint32_t len = myAddress.CopyTo (addr);
Expand All @@ -166,11 +191,11 @@ DhcpClient::StartApplication (void)
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 68);
m_socket->SetAllowBroadcast (true);
m_socket->Bind (local);
m_socket->BindToNetDevice (GetNode ()->GetDevice (m_device));
m_socket->BindToNetDevice (m_device);
}
m_socket->SetRecvCallback (MakeCallback (&DhcpClient::NetHandler, this));

GetNode ()->GetDevice (m_device)->AddLinkChangeCallback (MakeCallback (&DhcpClient::LinkStateHandler, this));
m_device->AddLinkChangeCallback (MakeCallback (&DhcpClient::LinkStateHandler, this));
Boot ();

}
Expand All @@ -187,7 +212,7 @@ DhcpClient::StopApplication ()
Simulator::Remove (m_nextOfferEvent);
Ptr<Ipv4> ipv4 = GetNode ()->GetObject<Ipv4> ();

int32_t ifIndex = ipv4->GetInterfaceForDevice (GetNode ()->GetDevice (m_device));
int32_t ifIndex = ipv4->GetInterfaceForDevice (m_device);
for (uint32_t i = 0; i < ipv4->GetNAddresses (ifIndex); i++)
{
if (ipv4->GetAddress (ifIndex,i).GetLocal () == m_myAddress)
Expand All @@ -204,7 +229,7 @@ DhcpClient::StopApplication ()
void DhcpClient::LinkStateHandler (void)
{
NS_LOG_FUNCTION_NOARGS ();
if (GetNode ()->GetDevice (m_device)->IsLinkUp ())
if (m_device->IsLinkUp ())
{
NS_LOG_INFO ("Link up at " << Simulator::Now ().As (Time::S));
m_socket->SetRecvCallback (MakeCallback (&DhcpClient::NetHandler, this));
Expand All @@ -219,7 +244,7 @@ void DhcpClient::LinkStateHandler (void)
m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); //stop receiving on this socket !!!

Ptr<Ipv4> ipv4MN = GetNode ()->GetObject<Ipv4> ();
int32_t ifIndex = ipv4MN->GetInterfaceForDevice (GetNode ()->GetDevice (m_device));
int32_t ifIndex = ipv4MN->GetInterfaceForDevice (m_device);

for (uint32_t i = 0; i < ipv4MN->GetNAddresses (ifIndex); i++)
{
Expand Down Expand Up @@ -384,7 +409,7 @@ void DhcpClient::AcceptAck (DhcpHeader header, Address from)
Simulator::Remove (m_timeout);
NS_LOG_INFO ("DHCP ACK received");
Ptr<Ipv4> ipv4 = GetNode ()->GetObject<Ipv4> ();
int32_t ifIndex = ipv4->GetInterfaceForDevice (GetNode ()->GetDevice (m_device));
int32_t ifIndex = ipv4->GetInterfaceForDevice (m_device);

for (uint32_t i = 0; i < ipv4->GetNAddresses (ifIndex); i++)
{
Expand Down Expand Up @@ -439,7 +464,7 @@ void DhcpClient::RemoveAndStart ()
Simulator::Remove (m_timeout);

Ptr<Ipv4> ipv4MN = GetNode ()->GetObject<Ipv4> ();
int32_t ifIndex = ipv4MN->GetInterfaceForDevice (GetNode ()->GetDevice (m_device));
int32_t ifIndex = ipv4MN->GetInterfaceForDevice (m_device);

for (uint32_t i = 0; i < ipv4MN->GetNAddresses (ifIndex); i++)
{
Expand Down
20 changes: 19 additions & 1 deletion src/internet-apps/model/dhcp-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,29 @@ class DhcpClient : public Application
*/
DhcpClient ();

/**
* \brief Constructor
* \param netDevice the NetDevice DHCP should work on
*/
DhcpClient (Ptr<NetDevice> netDevice);

/**
* \brief Destructor
*/
virtual ~DhcpClient ();

/**
* \brief Get the the NetDevice DHCP should work on
* \return the NetDevice DHCP should work on
*/
Ptr<NetDevice> GetDhcpClientNetDevice (void);

/**
* \brief Set the NetDevice DHCP should work on
* \param netDevice the NetDevice DHCP should work on
*/
void SetDhcpClientNetDevice (Ptr<NetDevice> netDevice);

/**
* \brief Get the IPv4Address of current DHCP server
* \return Ipv4Address of current DHCP server
Expand Down Expand Up @@ -150,7 +168,7 @@ class DhcpClient : public Application
void RemoveAndStart ();

uint8_t m_state; //!< State of the DHCP client
uint32_t m_device; //!< Device identifier
Ptr<NetDevice> m_device; //!< NetDevice pointer
Ptr<Socket> m_socket; //!< Socket for remote communication
Ipv4Address m_remoteAddress; //!< Initially set to 255.255.255.255 to start DHCP
Ipv4Address m_offeredAddress; //!< Address offered to the client
Expand Down
13 changes: 8 additions & 5 deletions src/internet-apps/test/dhcp-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,16 @@ DhcpTestCase::DoRun (void)
dhcpServerApps.Start (Seconds (1.0));
dhcpServerApps.Stop (Seconds (20.0));

DhcpClientHelper dhcpClientHelper (0);
ApplicationContainer dhcpClientApps = dhcpClientHelper.Install (nodes.Get (0));
dhcpClientApps.Start (Seconds (1.0));
dhcpClientApps.Stop (Seconds (20.0));
DhcpClientHelper dhcpClient;
NetDeviceContainer dhcpClientNetDevs;
dhcpClientNetDevs.Add (devNet.Get (0));

ApplicationContainer dhcpClients = dhcpClient.Install (dhcpClientNetDevs);
dhcpClients.Start (Seconds (1.0));
dhcpClients.Stop (Seconds (20.0));

dhcpClientApps.Get(0)->TraceConnectWithoutContext ("NewLease", MakeCallback(&DhcpTestCase::LeaseObtained, this));

dhcpClients.Get(0)->TraceConnectWithoutContext ("NewLease", MakeCallback(&DhcpTestCase::LeaseObtained, this));

Simulator::Stop (Seconds (21.0));

Expand Down