2 #include <netinet/in.h>
3 #include <sys/socket.h>
5 #include "ipv4-raw-socket-impl.h"
6 #include "ipv4-l3-protocol.h"
8 #include "ns3/ipv4-packet-info-tag.h"
9 #include "ns3/inet-socket-address.h"
11 #include "ns3/packet.h"
12 #include "ns3/uinteger.h"
13 #include "ns3/boolean.h"
20 NS_OBJECT_ENSURE_REGISTERED (Ipv4RawSocketImpl);
23 Ipv4RawSocketImpl::GetTypeId (
void)
25 static TypeId tid = TypeId (
"ns3::Ipv4RawSocketImpl")
27 .AddAttribute (
"Protocol",
"Protocol number to match.",
29 MakeUintegerAccessor (&Ipv4RawSocketImpl::m_protocol),
30 MakeUintegerChecker<uint16_t> ())
31 .AddAttribute (
"IcmpFilter",
32 "Any icmp header whose type field matches a bit in this filter is dropped. Type must be less than 32.",
34 MakeUintegerAccessor (&Ipv4RawSocketImpl::m_icmpFilter),
35 MakeUintegerChecker<uint32_t> ())
44 .AddAttribute (
"IpHeaderInclude",
45 "Include IP Header information (a.k.a setsockopt (IP_HDRINCL)).",
47 MakeBooleanAccessor (&Ipv4RawSocketImpl::m_iphdrincl),
48 MakeBooleanChecker ())
53 Ipv4RawSocketImpl::Ipv4RawSocketImpl ()
56 m_err = Socket::ERROR_NOTERROR;
61 m_shutdownSend =
false;
62 m_shutdownRecv =
false;
66 Ipv4RawSocketImpl::SetNode (Ptr<Node> node)
80 enum Socket::SocketErrno
87 enum Socket::SocketType
106 m_err = Socket::ERROR_INVAL;
140 ipv4->DeleteRawSocket (
this);
148 m_shutdownSend =
true;
155 m_shutdownRecv =
true;
164 m_err = Socket::ERROR_INVAL;
175 m_err = Socket::ERROR_OPNOTSUPP;
189 return SendTo (p, flags, to);
198 m_err = Socket::ERROR_INVAL;
209 if (ipv4->GetRoutingProtocol ())
223 SocketErrno errno_ = ERROR_NOTERROR;
228 int32_t index = ipv4->GetInterfaceForAddress (src);
230 oif = ipv4->GetNetDevice (index);
231 NS_LOG_LOGIC (
"Set index " << oif <<
"from source " << src);
235 route = ipv4->GetRoutingProtocol ()->RouteOutput (p, header, oif, errno_);
241 ipv4->Send (p, route->
GetSource (), dst, m_protocol, route);
245 ipv4->SendWithHeader (p, header, route);
247 NotifyDataSent (p->
GetSize ());
264 for (std::list<Data>::const_iterator i = m_recv.begin (); i != m_recv.end (); ++i)
266 rx += (i->packet)->GetSize ();
275 return RecvFrom (maxSize, flags, tmp);
286 struct Data data = m_recv.front ();
290 if (data.packet->
GetSize () > maxSize)
293 if (!(flags & MSG_PEEK))
297 m_recv.push_front (data);
304 Ipv4RawSocketImpl::SetProtocol (uint16_t protocol)
307 m_protocol = protocol;
311 Ipv4RawSocketImpl::ForwardUp (Ptr<const Packet> p, Ipv4Header ipHeader, Ptr<Ipv4Interface> incomingInterface)
322 if (boundNetDevice != incomingInterface->GetDevice())
331 ipHeader.GetProtocol () == m_protocol)
333 Ptr<Packet> copy = p->Copy ();
337 Ipv4PacketInfoTag tag;
338 copy->RemovePacketTag (tag);
339 tag.SetRecvIf (incomingInterface->GetDevice ()->GetIfIndex ());
340 copy->AddPacketTag (tag);
344 Icmpv4Header icmpHeader;
345 copy->PeekHeader (icmpHeader);
346 uint8_t type = icmpHeader.GetType ();
348 ((uint32_t(1) << type) & m_icmpFilter))
354 copy->AddHeader (ipHeader);
357 data.fromIp = ipHeader.GetSource ();
358 data.fromProtocol = ipHeader.GetProtocol ();
359 m_recv.push_back (data);
uint32_t RemoveHeader(Header &header)
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)
Send data to a specified peer.
Ipv4Address GetIpv4(void) const
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
virtual bool SetAllowBroadcast(bool allowBroadcast)
Configure whether broadcast datagram transmissions are allowed.
Ptr< Packet > Recv(void)
Read a single packet from the socket.
virtual int Send(Ptr< Packet > p, uint32_t flags)
Send data (or dummy data) to the remote host.
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
uint32_t GetSize(void) const
virtual uint32_t GetRxAvailable(void) const
virtual void DoDispose(void)
bool IsRecvPktInfo() const
Get status indicating whether enable/disable packet information to socket.
a polymophic address class
virtual int Close(void)
Close a socket.
Ptr< NetDevice > GetBoundNetDevice()
Returns socket's bound netdevice, if any.
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
void RemoveAtStart(uint32_t size)
virtual enum Socket::SocketErrno GetErrno(void) const
Ipv4Address GetSource(void) const
#define NS_LOG_LOGIC(msg)
Access to the Ipv4 forwarding table, interfaces, and configuration.
static InetSocketAddress ConvertFrom(const Address &address)
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)
Read a single packet from the socket and retrieve the sender address.
virtual int Bind6()
Allocate a local IPv6 endpoint for this socket.
virtual int Bind()
Allocate a local IPv4 endpoint for this socket.
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
virtual int ShutdownRecv(void)
virtual enum Socket::SocketType GetSocketType(void) const
virtual bool GetAllowBroadcast() const
Query whether broadcast datagram transmissions are allowed.
virtual void DoDispose(void)
Ipv4 addresses are stored in host order in this class.
virtual uint32_t GetTxAvailable(void) const
Returns the number of bytes which can be sent in a single call to Send.
virtual int Listen(void)
Listen for incoming connections.
#define NS_LOG_DEBUG(msg)
virtual Ptr< Node > GetNode(void) const
Ptr< T > GetObject(void) const
static bool IsMatchingType(const Address &address)
virtual int ShutdownSend(void)
virtual int GetSockName(Address &address) const