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
Store chaddr as an Addess, because it is MUCH easer to handle.
If it's nice but complex, then it's not nice.
  • Loading branch information
TommyPec committed Jul 1, 2017
commit e522173fdfec5d42cc9d738870bd013132a261e2
55 changes: 28 additions & 27 deletions src/internet-apps/examples/dhcp-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ main (int argc, char *argv[])

// GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));

if (verbose)
{
LogComponentEnable ("DhcpClient", LOG_LEVEL_INFO);
LogComponentEnable ("DhcpServer", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
}
if (verbose)
{
LogComponentEnable ("DhcpClient", LOG_LEVEL_ALL);
LogComponentEnable ("DhcpServer", LOG_LEVEL_ALL);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
}

Time stopTime = Seconds (20);

NS_LOG_INFO ("Create nodes.");
NodeContainer nodes;
Expand Down Expand Up @@ -100,30 +102,29 @@ main (int argc, char *argv[])
staticRoutingA->AddNetworkRouteTo (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.1.1"), 1);


uint32_t ifIndex;
Ptr<Ipv4> ipv4MN = net.Get (0)->GetObject<Ipv4> ();
uint32_t ifIndex = ipv4MN->AddInterface (dev_net.Get (0));
ifIndex = ipv4MN->AddInterface (dev_net.Get (0));
ipv4MN->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("0.0.0.0"), Ipv4Mask ("/32")));
ipv4MN->SetForwarding (ifIndex, true);
// ipv4MN->SetForwarding (ifIndex, true);
ipv4MN->SetUp (ifIndex);

Ptr<Ipv4> ipv4MN1 = net.Get (1)->GetObject<Ipv4> ();
uint32_t ifIndex1 = ipv4MN1->AddInterface (dev_net.Get (1));
ipv4MN1->AddAddress (ifIndex1, Ipv4InterfaceAddress (Ipv4Address ("0.0.0.0"), Ipv4Mask ("/32")));
ipv4MN1->SetForwarding (ifIndex1, true);
ipv4MN1->SetUp (ifIndex1);
ifIndex = ipv4MN1->AddInterface (dev_net.Get (1));
ipv4MN1->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("0.0.0.0"), Ipv4Mask ("/32")));
// ipv4MN1->SetForwarding (ifIndex, true);
ipv4MN1->SetUp (ifIndex);

Ptr<Ipv4> ipv4MN2 = net.Get (2)->GetObject<Ipv4> ();
uint32_t ifIndex2 = ipv4MN2->AddInterface (dev_net.Get (2));
ipv4MN2->AddAddress (ifIndex2, Ipv4InterfaceAddress (Ipv4Address ("0.0.0.0"), Ipv4Mask ("/32")));
ipv4MN2->SetForwarding (ifIndex2, true);
ipv4MN2->SetUp (ifIndex2);
ifIndex = ipv4MN2->AddInterface (dev_net.Get (2));
ipv4MN2->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("0.0.0.0"), Ipv4Mask ("/32")));
// ipv4MN2->SetForwarding (ifIndex, true);
ipv4MN2->SetUp (ifIndex);

Ptr<Ipv4> ipv4Router = net.Get (3)->GetObject<Ipv4> ();
ifIndex = ipv4Router->AddInterface (dev_net.Get (3));
ipv4Router->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.12"), Ipv4Mask ("/0"))); // need to remove this workaround
ipv4Router->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.12"), Ipv4Mask ("/24")));
ipv4Router->SetForwarding (ifIndex, true);
// ipv4Router->SetForwarding (ifIndex, true);
ipv4Router->SetUp (ifIndex);

Ptr<Ipv4> ipv4Router1 = net.Get (4)->GetObject<Ipv4> ();
Expand All @@ -147,28 +148,28 @@ main (int argc, char *argv[])

ApplicationContainer ap_dhcp_server = dhcp_server.Install (router.Get (0));
ap_dhcp_server.Start (Seconds (1.0));
ap_dhcp_server.Stop (Seconds (100.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 (Seconds (100.0));
ap_dhcp_client.Stop (stopTime);

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 (Seconds (100.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 (Seconds (100.0));
ap_dhcp_client2.Stop (stopTime);

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (p2pNodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (100.0));
serverApps.Stop (stopTime);

UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
Expand All @@ -177,9 +178,9 @@ main (int argc, char *argv[])

ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (100.0));
clientApps.Stop (stopTime);

Simulator::Stop (Seconds (110.0));
Simulator::Stop (stopTime + Seconds (10.0));

if (tracing)
{
Expand Down
18 changes: 9 additions & 9 deletions src/internet-apps/model/dhcp-client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ DhcpClient::StartApplication (void)
Ptr<Ipv4> ipv4 = GetNode ()->GetObject<Ipv4> ();
uint32_t ifIndex = ipv4->GetInterfaceForDevice (GetNode ()->GetDevice (m_device));

uint8_t buffer[Address::MAX_SIZE];
uint32_t len = GetNode ()->GetDevice (m_device)->GetAddress ().CopyTo (buffer);

m_chaddr = 0;
for (uint32_t j=len; j>0; j--)
{
m_chaddr = buffer[j-1];
m_chaddr <<= 8;
}
// 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 ();
NS_LOG_INFO ("My address is " << myAddress);
uint8_t addr[Address::MAX_SIZE];
uint32_t len = myAddress.CopyTo (addr);
NS_ASSERT_MSG (len <= 16, "DHCP client can not handle a chaddr larger than 16 bytes");
m_chaddr.CopyFrom (addr, 16);
NS_LOG_INFO ("My m_chaddr is " << m_chaddr);

bool found = false;
for (uint32_t i = 0; i < ipv4->GetNAddresses (ifIndex); i++)
Expand Down
2 changes: 1 addition & 1 deletion src/internet-apps/model/dhcp-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class DhcpClient : public Application
Ipv4Address m_remoteAddress; //!< Initially set to 255.255.255.255 to start DHCP
Ipv4Address m_offeredAddress; //!< Address offered to the client
Ipv4Address m_myAddress; //!< Address assigned to the client
uint128_t m_chaddr; //!< chaddr of the interface.
Address m_chaddr; //!< chaddr of the interface (stored as an Address for convenience).
Ipv4Mask m_myMask; //!< Mask of the address assigned
Ipv4Address m_server; //!< Address of the DHCP server
Ipv4Address m_gateway; //!< Address of the gateway
Expand Down
50 changes: 27 additions & 23 deletions src/internet-apps/model/dhcp-header.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,25 @@ void DhcpHeader::SetTime ()
m_secs = (uint16_t) Simulator::Now ().GetSeconds ();
}

void DhcpHeader::SetChaddr (uint128_t addr)
void DhcpHeader::SetChaddr (Address addr)
{
m_chaddr = addr;
std::memset (m_chaddr, 0, 16);
NS_ASSERT_MSG (addr.GetLength () <= 16, "Address length too big");
addr.CopyTo (m_chaddr);
}

uint128_t DhcpHeader::GetChaddr ()
void DhcpHeader::SetChaddr (uint8_t* addr, uint8_t len)
{
return m_chaddr;
std::memset (m_chaddr, 0, 16);
NS_ASSERT_MSG (len <= 16, "Address length too big");
std::memcpy (m_chaddr, addr, len);
}

Address DhcpHeader::GetChaddr ()
{
Address addr;
addr.CopyFrom (m_chaddr, 16);
return addr;
}

void DhcpHeader::SetYiaddr (Ipv4Address addr)
Expand Down Expand Up @@ -274,18 +285,17 @@ DhcpHeader::Serialize (Buffer::Iterator start) const
{
Buffer::Iterator i = start;
i.WriteU8 (m_bootp);
i.WriteU8 ( m_hType);
i.WriteU8 ( m_hLen);
i.WriteU8 ( m_hops);
i.WriteU32 ( m_xid);
i.WriteHtonU16 ( m_secs);
i.WriteU8 (m_hType);
i.WriteU8 (m_hLen);
i.WriteU8 (m_hops);
i.WriteU32 (m_xid);
i.WriteHtonU16 (m_secs);
i.WriteU16 ( m_flags);
WriteTo (i, m_ciAddr);
WriteTo (i, m_yiAddr);
WriteTo (i, m_siAddr);
WriteTo (i, m_giAddr);
i.WriteU64 (m_chaddr >> 64);
i.WriteU64 (m_chaddr & 0xFFFFFFFF);
i.Write (m_chaddr, 16);
i.Write (m_sname,64);
i.Write (m_file,128);
i.Write (m_magic_cookie,4);
Expand Down Expand Up @@ -357,19 +367,13 @@ uint32_t DhcpHeader::Deserialize (Buffer::Iterator start)
m_secs = i.ReadNtohU16 ();
m_flags = i.ReadU16 ();
ReadFrom (i, m_ciAddr);
ReadFrom (i,m_yiAddr);
ReadFrom (i,m_siAddr);
ReadFrom (i,m_giAddr);

uint128_t tmp;
tmp = i.ReadU64 ();
m_chaddr = tmp << 64;
tmp = i.ReadU64 ();
m_chaddr |= tmp;

ReadFrom (i, m_yiAddr);
ReadFrom (i, m_siAddr);
ReadFrom (i, m_giAddr);
i.Read (m_chaddr, 16);
i.Read (m_sname, 64);
i.Read (m_file,128);
i.Read (m_magic_cookie,4);
i.Read (m_file, 128);
i.Read (m_magic_cookie, 4);
if ( m_magic_cookie[0] != 99 || m_magic_cookie[1] != 130 || m_magic_cookie[2] != 83 || m_magic_cookie[3] != 99)
{
NS_LOG_WARN ("Malformed Packet");
Expand Down
21 changes: 17 additions & 4 deletions src/internet-apps/model/dhcp-header.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,30 @@ class DhcpHeader : public Header
*/
void SetTime ();

/**
* \brief Set the Address of the device.
*
* Only the relevant bits are considered (i.e., not the type and length)
*
* \param addr Address of the device
*/
void SetChaddr (Address addr);

/**
* \brief Set the Address of the device
* \param addr Address of the device
* \param len Address length
*/
void SetChaddr (uint128_t addr);
void SetChaddr (uint8_t* addr, uint8_t len);

/**
* \brief Get the Address of the client
* \brief Get the Address of the client.
*
* Note: the address is always 16-bytes long.
*
* \return Address of the client
*/
uint128_t GetChaddr (void);
Address GetChaddr (void);

/**
* \brief Set the IPv4Address of the client
Expand Down Expand Up @@ -287,7 +300,7 @@ class DhcpHeader : public Header
uint32_t m_len; //!< The length of the header
uint16_t m_secs; //!< Seconds elapsed
uint16_t m_flags; //!< BOOTP flags
uint128_t m_chaddr; //!< The address identifier
uint8_t m_chaddr[16]; //!< The address identifier
Ipv4Address m_yiAddr; //!< Your (client) IP address
Ipv4Address m_ciAddr; //!< The IP address of the client
Ipv4Address m_siAddr; //!< Next Server IP address
Expand Down
16 changes: 8 additions & 8 deletions src/internet-apps/model/dhcp-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ void DhcpServer::StartApplication (void)

NS_ASSERT_MSG (m_minAddress < m_maxAddress,"Invalid Address range");

m_nextAddressSeq = 0;
Ipv4Address myOwnAddress;

if (m_socket)
Expand Down Expand Up @@ -146,7 +145,7 @@ void DhcpServer::StartApplication (void)
// set infinite GRANTED_LEASED_TIME for my address

myOwnAddress = ipv4->GetAddress (ifIndex, addrIndex).GetLocal ();
m_leasedAddresses[0] = std::make_pair (myOwnAddress, 0xffffffff);
m_leasedAddresses[Address ()] = std::make_pair (myOwnAddress, 0xffffffff);
break;
}
}
Expand All @@ -162,9 +161,10 @@ void DhcpServer::StartApplication (void)
uint32_t range = m_maxAddress.Get () - m_minAddress.Get () + 1;
for (uint32_t searchSeq = 0; searchSeq < range; searchSeq ++)
{
Ipv4Address poolAddress = Ipv4Address (m_minAddress.Get () + m_nextAddressSeq);
Ipv4Address poolAddress = Ipv4Address (m_minAddress.Get () + searchSeq);
if (poolAddress != myOwnAddress)
{
NS_LOG_LOGIC ("Adding " << poolAddress << " to the pool");
m_availableAddresses.push_back (poolAddress);
}
}
Expand Down Expand Up @@ -199,7 +199,7 @@ void DhcpServer::TimerHandler ()
if (i->second.second == 0)
{
NS_LOG_INFO ("Address leased state expired, address removed - " <<
"chaddr: " << int(i->first) <<
"chaddr: " << i->first <<
"IP address " << i->second.first);
i->second.second = 0;
m_expiredAddresses.push_front (i->first);
Expand Down Expand Up @@ -243,7 +243,7 @@ void DhcpServer::NetHandler (Ptr<Socket> socket)
void DhcpServer::SendOffer (Ptr<NetDevice> iDev, DhcpHeader header, InetSocketAddress from)
{
DhcpHeader newDhcpHeader;
uint128_t sourceChaddr = header.GetChaddr ();
Address sourceChaddr = header.GetChaddr ();
uint32_t tran = header.GetTran ();
Ptr<Packet> packet = 0;
bool found = false;
Expand All @@ -257,7 +257,7 @@ void DhcpServer::SendOffer (Ptr<NetDevice> iDev, DhcpHeader header, InetSocketAd
// We know this client from some time ago
if (m_leasedAddresses[sourceChaddr].second != 0)
{
NS_LOG_LOGIC ("This client is sending a DISCOVER but it has still a lease active - perhaps it didn't shut down gracefully: " << int (sourceChaddr));
NS_LOG_LOGIC ("This client is sending a DISCOVER but it has still a lease active - perhaps it didn't shut down gracefully: " << sourceChaddr);
}
else
{
Expand All @@ -281,7 +281,7 @@ void DhcpServer::SendOffer (Ptr<NetDevice> iDev, DhcpHeader header, InetSocketAd
// there's still hope: reuse the old ones.
if (!m_expiredAddresses.empty ())
{
uint128_t oldestChaddr = m_expiredAddresses.back ();
Address oldestChaddr = m_expiredAddresses.back ();
m_expiredAddresses.pop_back ();
offeredAddress = m_leasedAddresses[oldestChaddr].first;
m_leasedAddresses.erase (oldestChaddr);
Expand Down Expand Up @@ -390,7 +390,7 @@ void DhcpServer::SendOffer (Ptr<NetDevice> iDev, DhcpHeader header, InetSocketAd
void DhcpServer::SendAck (Ptr<NetDevice> iDev, DhcpHeader header, InetSocketAddress from)
{
DhcpHeader newDhcpHeader;
uint128_t sourceChaddr = header.GetChaddr ();
Address sourceChaddr = header.GetChaddr ();
uint32_t tran = header.GetTran ();
Ptr<Packet> packet = 0;
Ipv4Address address = header.GetReq ();
Expand Down
13 changes: 6 additions & 7 deletions src/internet-apps/model/dhcp-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,25 @@ class DhcpServer : public Application
Ipv4Address m_gateway; //!< The gateway address

/// Leased address container - chaddr + IP addr / lease time
typedef std::map<uint128_t, std::pair<Ipv4Address, uint32_t> > LeasedAddress;
typedef std::map<Address, std::pair<Ipv4Address, uint32_t> > LeasedAddress;
/// Leased address iterator - chaddr + IP addr / lease time
typedef std::map<uint128_t, std::pair<Ipv4Address, uint32_t> >::iterator LeasedAddressIter;
typedef std::map<Address, std::pair<Ipv4Address, uint32_t> >::iterator LeasedAddressIter;
/// Leased address const iterator - chaddr + IP addr / lease time
typedef std::map<uint128_t, std::pair<Ipv4Address, uint32_t> >::const_iterator LeasedAddressCIter;
typedef std::map<Address, std::pair<Ipv4Address, uint32_t> >::const_iterator LeasedAddressCIter;

/// Expired address container - chaddr
typedef std::list<uint128_t> ExpiredAddress;
typedef std::list<Address> ExpiredAddress;
/// Expired address iterator - chaddr
typedef std::list<uint128_t>::iterator ExpiredAddressIter;
typedef std::list<Address>::iterator ExpiredAddressIter;
/// Expired address const iterator - chaddr
typedef std::list<uint128_t>::const_iterator ExpiredAddressCIter;
typedef std::list<Address>::const_iterator ExpiredAddressCIter;

/// Available address container - IP addr
typedef std::list<Ipv4Address> AvailableAddress;

LeasedAddress m_leasedAddresses; //!< Leased address and their status (cache memory)
ExpiredAddress m_expiredAddresses; //!< Expired addresses to be reused (chaddr of the clients)
AvailableAddress m_availableAddresses; //!< Available addresses to be used (IP addresses)
uint32_t m_nextAddressSeq; //!< The next address in the sequence which can be allocated
Time m_lease; //!< The granted lease time for an address
Time m_renew; //!< The renewal time for an address
Time m_rebind; //!< The rebinding time for an address
Expand Down
6 changes: 0 additions & 6 deletions src/internet-apps/test/dhcp-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,8 @@ DhcpTestCase::DoRun (void)
ipv4MN->SetUp (ifIndex);

// Setup IPv4 addresses and forwarding
// Ipv4AddressHelper ipv4;
//
// ipv4.SetBase (Ipv4Address ("172.30.0.0"), Ipv4Mask ("255.255.255.0"));
// Ipv4InterfaceContainer iic1 = ipv4.Assign (devNet.Get (1));

Ptr<Ipv4> ipv4Router = net.Get (1)->GetObject<Ipv4> ();
ifIndex = ipv4Router->AddInterface (devNet.Get (1));
ipv4Router->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.1"), Ipv4Mask ("/0"))); // need to remove this workaround
ipv4Router->AddAddress (ifIndex, Ipv4InterfaceAddress (Ipv4Address ("172.30.0.1"), Ipv4Mask ("/24")));
ipv4Router->SetForwarding (ifIndex, true);
ipv4Router->SetUp (ifIndex);
Expand Down