Skip to content

Commit

Permalink
internet: Set the TOS for ICMP Echo Requests/Replies
Browse files Browse the repository at this point in the history
  • Loading branch information
stavallo authored and Stefano Avallone committed Jan 15, 2024
1 parent 2c8c6cc commit d5f9dc2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/internet-apps/model/ping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ Ping::GetTypeId()
TimeValue(Seconds(1)),
MakeTimeAccessor(&Ping::m_timeout),
MakeTimeChecker())
.AddAttribute("Tos",
"The Type of Service used to send the ICMP Echo Requests. "
"All 8 bits of the TOS byte are set (including ECN bits).",
UintegerValue(0),
MakeUintegerAccessor(&Ping::m_tos),
MakeUintegerChecker<uint8_t>())
.AddTraceSource("Tx",
"The sequence number and ICMP echo response packet.",
MakeTraceSourceAccessor(&Ping::m_txTrace),
Expand Down Expand Up @@ -455,8 +461,9 @@ Ping::Send()
header.EnableChecksum();
}
p->AddHeader(header);
returnValue =
m_socket->SendTo(p, 0, InetSocketAddress(Ipv4Address::ConvertFrom(m_destination), 0));
auto dest = InetSocketAddress(Ipv4Address::ConvertFrom(m_destination), 0);
dest.SetTos(m_tos);
returnValue = m_socket->SendTo(p, 0, dest);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/internet-apps/model/ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class Ping : public Application
uint32_t m_size{56};
/// The socket we send packets from
Ptr<Socket> m_socket;
/// The Type of Service carried by ICMP ECHOs
uint8_t m_tos;
/// ICMP ECHO sequence number
uint16_t m_seq{0};
/// Callbacks for tracing the packet Tx events
Expand Down
10 changes: 7 additions & 3 deletions src/internet/model/icmpv4-l4-protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,18 @@ void
Icmpv4L4Protocol::HandleEcho(Ptr<Packet> p,
Icmpv4Header header,
Ipv4Address source,
Ipv4Address destination)
Ipv4Address destination,
uint8_t tos)
{
NS_LOG_FUNCTION(this << p << header << source << destination);
NS_LOG_FUNCTION(this << p << header << source << destination << tos);

Ptr<Packet> reply = Create<Packet>();
Icmpv4Echo echo;
p->RemoveHeader(echo);
reply->AddHeader(echo);
SocketIpTosTag ipTosTag;
ipTosTag.SetTos(tos);
reply->ReplacePacketTag(ipTosTag);
SendMessage(reply, destination, source, Icmpv4Header::ICMPV4_ECHO_REPLY, 0, nullptr);
}

Expand Down Expand Up @@ -327,7 +331,7 @@ Icmpv4L4Protocol::Receive(Ptr<Packet> p,
}
}
}
HandleEcho(p, icmp, header.GetSource(), dst);
HandleEcho(p, icmp, header.GetSource(), dst, header.GetTos());
break;
}
case Icmpv4Header::ICMPV4_DEST_UNREACH:
Expand Down
4 changes: 3 additions & 1 deletion src/internet/model/icmpv4-l4-protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ class Icmpv4L4Protocol : public IpL4Protocol
* \param header the IP header
* \param source the source address
* \param destination the destination address
* \param tos the type of service
*/
void HandleEcho(Ptr<Packet> p,
Icmpv4Header header,
Ipv4Address source,
Ipv4Address destination);
Ipv4Address destination,
uint8_t tos);
/**
* \brief Handles an incoming ICMP Destination Unreachable packet
* \param p the packet
Expand Down

0 comments on commit d5f9dc2

Please sign in to comment.