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
DHCP sever now listen only on the appropriate interface.
Also fixed some bugs (or weird coding, at best) in server and
headers
  • Loading branch information
TommyPec committed Jun 30, 2017
commit 37978e1763b737ab72e01642583958ac0e369c1d
22 changes: 16 additions & 6 deletions src/internet-apps/model/dhcp-client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,19 @@ DhcpClient::StartApplication (void)
m_gateway = Ipv4Address ("0.0.0.0");
Ptr<Ipv4> ipv4 = GetNode ()->GetObject<Ipv4> ();
uint32_t ifIndex = ipv4->GetInterfaceForDevice (GetNode ()->GetDevice (m_device));
uint32_t i;

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;
}

bool found = false;
for (i = 0; i < ipv4->GetNAddresses (ifIndex); i++)
for (uint32_t i = 0; i < ipv4->GetNAddresses (ifIndex); i++)
{
if (ipv4->GetAddress (ifIndex,i).GetLocal () == m_myAddress)
{
Expand Down Expand Up @@ -244,7 +254,7 @@ void DhcpClient::NetHandler (Ptr<Socket> socket)
{
return;
}
if (header.GetChaddr () != GetNode ()->GetDevice (m_device)->GetAddress ())
if (header.GetChaddr () != m_chaddr)
{
return;
}
Expand Down Expand Up @@ -275,7 +285,7 @@ void DhcpClient::Boot (void)
header.SetTran (m_tran);
header.SetType (DhcpHeader::DHCPDISCOVER);
header.SetTime ();
header.SetChaddr (GetNode ()->GetDevice (m_device)->GetAddress ());
header.SetChaddr (m_chaddr);
packet->AddHeader (header);

if ((m_socket->SendTo (packet, 0, InetSocketAddress (Ipv4Address ("255.255.255.255"), DHCP_PEER_PORT))) >= 0)
Expand Down Expand Up @@ -336,7 +346,7 @@ void DhcpClient::Request (void)
header.SetTime ();
header.SetTran (m_tran);
header.SetReq (m_offeredAddress);
header.SetChaddr (GetNode ()->GetDevice (m_device)->GetAddress ());
header.SetChaddr (m_chaddr);
packet->AddHeader (header);
m_socket->SendTo (packet, 0, InetSocketAddress (Ipv4Address ("255.255.255.255"), DHCP_PEER_PORT));
m_state = WAIT_ACK;
Expand All @@ -353,7 +363,7 @@ void DhcpClient::Request (void)
header.SetType (DhcpHeader::DHCPREQ);
header.SetReq (m_myAddress);
m_offeredAddress = m_myAddress;
header.SetChaddr (GetNode ()->GetDevice (m_device)->GetAddress ());
header.SetChaddr (m_chaddr);
packet->AddHeader (header);
if ((m_socket->SendTo (packet, 0, InetSocketAddress (m_remoteAddress, DHCP_PEER_PORT))) >= 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/internet-apps/model/dhcp-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +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.
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
23 changes: 14 additions & 9 deletions src/internet-apps/model/dhcp-header.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,14 @@ void DhcpHeader::SetTime ()
m_secs = (uint16_t) Simulator::Now ().GetSeconds ();
}

void DhcpHeader::SetChaddr (Address addr)
void DhcpHeader::SetChaddr (uint128_t addr)
{
addr.CopyAllTo (m_chaddr, 16);
m_chaddr = addr;
}

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

void DhcpHeader::SetYiaddr (Ipv4Address addr)
Expand Down Expand Up @@ -286,7 +284,8 @@ DhcpHeader::Serialize (Buffer::Iterator start) const
WriteTo (i, m_yiAddr);
WriteTo (i, m_siAddr);
WriteTo (i, m_giAddr);
i.Write (m_chaddr,16);
i.WriteU64 (m_chaddr >> 64);
i.WriteU64 (m_chaddr & 0xFFFFFFFF);
i.Write (m_sname,64);
i.Write (m_file,128);
i.Write (m_magic_cookie,4);
Expand Down Expand Up @@ -361,7 +360,13 @@ uint32_t DhcpHeader::Deserialize (Buffer::Iterator start)
ReadFrom (i,m_yiAddr);
ReadFrom (i,m_siAddr);
ReadFrom (i,m_giAddr);
i.Read (m_chaddr, 16);

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

i.Read (m_sname, 64);
i.Read (m_file,128);
i.Read (m_magic_cookie,4);
Expand Down Expand Up @@ -505,4 +510,4 @@ uint32_t DhcpHeader::Deserialize (Buffer::Iterator start)
return m_len;
}

} // namespace ns3
} // namespace ns3
6 changes: 3 additions & 3 deletions src/internet-apps/model/dhcp-header.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ class DhcpHeader : public Header
* \brief Set the Address of the device
* \param addr Address of the device
*/
void SetChaddr (Address addr);
void SetChaddr (uint128_t addr);

/**
* \brief Get the Address of the client
* \return Address of the client
*/
Address GetChaddr (void);
uint128_t GetChaddr (void);

/**
* \brief Set the IPv4Address of the client
Expand Down Expand Up @@ -287,7 +287,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
uint8_t m_chaddr[16]; //!< The address identifier
uint128_t m_chaddr; //!< 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
Loading