18 #include "ns3/icmpv4.h" 19 #include "ns3/assert.h" 21 #include "ns3/ipv4-address.h" 22 #include "ns3/socket.h" 23 #include "ns3/uinteger.h" 24 #include "ns3/boolean.h" 25 #include "ns3/inet-socket-address.h" 26 #include "ns3/packet.h" 27 #include "ns3/trace-source-accessor.h" 40 .SetGroupName(
"Internet-Apps")
42 .AddAttribute (
"Remote",
43 "The address of the machine we want to ping.",
47 .AddAttribute (
"Verbose",
48 "Produce usual output.",
52 .AddAttribute (
"Interval",
"Wait interval seconds between sending each packet.",
56 .AddAttribute (
"Size",
"The number of data bytes to be sent, real packet will be 8 (ICMP) + 20 (IP) bytes longer.",
59 MakeUintegerChecker<uint32_t> (16))
60 .AddTraceSource (
"Rtt",
61 "The rtt calculated by the ping.",
63 "ns3::Time::TracedCallback");
127 uint32_t recvSize = p->
GetSize ();
139 uint32_t * buf =
new uint32_t [
m_size];
146 Read32 ((
const uint8_t *) &buf[0], nodeId);
147 Read32 ((
const uint8_t *) &buf[1], appId);
149 if (nodeId ==
GetNode ()->GetId () &&
152 Time sendTime = i->second;
163 std::cout << recvSize <<
" bytes from " << realFrom.
GetIpv4 () <<
":" 165 <<
" ttl=" << (unsigned)ipv4.GetTtl ()
182 buffer[0] = (
data >> 0) & 0xff;
183 buffer[1] = (
data >> 8) & 0xff;
184 buffer[2] = (
data >> 16) & 0xff;
185 buffer[3] = (
data >> 24) & 0xff;
193 data = (buffer[3] << 24) + (buffer[2] << 16) + (buffer[1] << 8) + buffer[0];
206 echo.SetIdentifier (0);
215 for (uint32_t i = 0; i <
m_size; ++i)
data[i] = 0;
225 echo.SetData (dataPacket);
249 std::cout <<
"PING " <<
m_remote <<
" 56(84) bytes of data.\n";
282 std::ostringstream os;
284 os <<
"--- " <<
m_remote <<
" ping statistics ---\n" 285 <<
m_seq <<
" packets transmitted, " <<
m_recv <<
" received, " 293 std::cout << os.str ();
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
uint32_t GetApplicationId(void) const
Return the application ID in the node.
virtual void StartApplication(void)
Application specific startup code.
void Write32(uint8_t *buffer, const uint32_t data)
Writes data to buffer in little-endian format.
Simulation virtual time values and global simulation resolution.
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
double Avg() const
Sample average.
AttributeValue implementation for Boolean.
uint32_t GetId(void) const
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
uint32_t GetData(uint8_t payload[]) const
Get the Echo data.
uint32_t GetDataSize(void) const
Get the Echo data size.
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
static bool ChecksumEnabled(void)
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
TracedCallback< Time > m_traceRtt
TracedCallback for RTT measured by ICMP ECHOs.
V4Ping()
create a pinger applications
a polymophic address class
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
virtual void DoDispose(void)
Destructor implementation.
double Stddev() const
Standard deviation.
void Read32(const uint8_t *buffer, uint32_t &data)
Writes data from a little-endian formatted buffer to data.
Time m_started
Start time to report total ping time.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
static TypeId GetTypeId(void)
Get the type ID.
The base class for all ns3 applications.
AttributeValue implementation for Time.
void Receive(Ptr< Socket > socket)
Receive an ICMP Echo.
Ptr< const AttributeChecker > MakeIpv4AddressChecker(void)
Hold an unsigned integer type.
Ptr< Application > GetApplication(uint32_t index) const
Retrieve the index-th Application associated to this node.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Ptr< Node > GetNode() const
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
Destructor implementation.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
std::map< uint16_t, Time > m_sent
All sent but not answered packets. Map icmp seqno -> when sent.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
bool m_verbose
produce ping-style output if true
Ptr< const AttributeChecker > MakeBooleanChecker(void)
uint16_t GetPort(void) const
uint16_t m_seq
ICMP ECHO sequence number.
AttributeValue implementation for Ipv4Address.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
static Time Now(void)
Return the current simulation virtual time.
EventId m_next
Next packet will be sent.
uint16_t GetSequenceNumber(void) const
Get the Echo sequence number.
virtual void StopApplication(void)
Application specific shutdown code.
void Send()
Send one Ping (ICMP ECHO) to the destination.
int64_t GetMilliSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
an application which sends one ICMP ECHO request, waits for a REPLYs and reports the calculated RTT...
uint16_t GetIdentifier(void) const
Get the Echo identifier.
void SetSequenceNumber(uint16_t seq)
Set the Echo sequence number.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Time Seconds(double value)
Construct a Time in the indicated unit.
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
uint32_t GetNApplications(void) const
Time m_interval
Wait interval seconds between sending each packet.
uint32_t m_recv
received packets counter
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
Ipv4Address m_remote
Remote address.
virtual int Close(void)=0
Close a socket.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
uint32_t Count() const
Sample size.
static bool IsMatchingType(const Address &address)
uint32_t m_size
Specifies the number of data bytes to be sent.
void AddHeader(const Header &header)
Add header to this packet.
Average< double > m_avgRtt
Average rtt is ms.
Ipv4Address GetIpv4(void) const
Ptr< Socket > m_socket
The socket we send packets from.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
void Update(T const &x)
Add new sample.