22 #include "ns3/assert.h"
23 #include "ns3/packet.h"
25 #include "ns3/boolean.h"
26 #include "ns3/object-vector.h"
28 #include "ns3/ipv4-route.h"
29 #include "ns3/ipv6-route.h"
30 #include "ns3/ipv6-header.h"
32 #include "udp-l4-protocol.h"
33 #include "udp-header.h"
34 #include "udp-socket-factory-impl.h"
35 #include "ipv4-end-point-demux.h"
36 #include "ipv4-end-point.h"
37 #include "ipv6-end-point-demux.h"
38 #include "ipv6-end-point.h"
39 #include "ipv4-l3-protocol.h"
40 #include "ipv6-l3-protocol.h"
41 #include "udp-socket-impl.h"
47 NS_OBJECT_ENSURE_REGISTERED (UdpL4Protocol);
50 const uint8_t UdpL4Protocol::PROT_NUMBER = 17;
53 UdpL4Protocol::GetTypeId (
void)
55 static TypeId tid = TypeId (
"ns3::UdpL4Protocol")
56 .SetParent<IpL4Protocol> ()
57 .AddConstructor<UdpL4Protocol> ()
58 .AddAttribute (
"SocketList",
"The list of sockets associated to this protocol.",
60 MakeObjectVectorAccessor (&UdpL4Protocol::m_sockets),
61 MakeObjectVectorChecker<UdpSocketImpl> ())
66 UdpL4Protocol::UdpL4Protocol ()
67 : m_endPoints (new Ipv4EndPointDemux ()), m_endPoints6 (new Ipv6EndPointDemux ())
72 UdpL4Protocol::~UdpL4Protocol ()
78 UdpL4Protocol::SetNode (Ptr<Node> node)
92 Ptr<Node> node = this->GetObject<Node> ();
93 Ptr<Ipv4> ipv4 = this->GetObject<Ipv4> ();
98 if ((node != 0) && (ipv4 != 0 || ipv6 != 0))
100 this->SetNode (node);
102 udpFactory->SetUdp (
this);
112 if (ipv4 != 0 && m_downTarget.IsNull())
117 if (ipv6 != 0 && m_downTarget6.IsNull())
136 for (std::vector<
Ptr<UdpSocketImpl> >::iterator i = m_sockets.begin (); i != m_sockets.end (); i++)
142 if (m_endPoints != 0)
147 if (m_endPoints6 != 0)
153 m_downTarget.Nullify ();
154 m_downTarget6.Nullify ();
166 socket->SetNode (m_node);
167 socket->SetUdp (
this);
168 m_sockets.push_back (socket);
173 UdpL4Protocol::Allocate (
void)
176 return m_endPoints->Allocate ();
180 UdpL4Protocol::Allocate (Ipv4Address address)
183 return m_endPoints->Allocate (address);
187 UdpL4Protocol::Allocate (uint16_t port)
190 return m_endPoints->Allocate (port);
194 UdpL4Protocol::Allocate (Ipv4Address address, uint16_t port)
197 return m_endPoints->Allocate (address, port);
200 UdpL4Protocol::Allocate (Ipv4Address localAddress, uint16_t localPort,
201 Ipv4Address peerAddress, uint16_t peerPort)
203 NS_LOG_FUNCTION (
this << localAddress << localPort << peerAddress << peerPort);
204 return m_endPoints->Allocate (localAddress, localPort,
205 peerAddress, peerPort);
209 UdpL4Protocol::DeAllocate (Ipv4EndPoint *endPoint)
212 m_endPoints->DeAllocate (endPoint);
216 UdpL4Protocol::Allocate6 (
void)
223 UdpL4Protocol::Allocate6 (Ipv6Address address)
226 return m_endPoints6->
Allocate (address);
230 UdpL4Protocol::Allocate6 (uint16_t port)
233 return m_endPoints6->
Allocate (port);
237 UdpL4Protocol::Allocate6 (Ipv6Address address, uint16_t port)
240 return m_endPoints6->
Allocate (address, port);
243 UdpL4Protocol::Allocate6 (Ipv6Address localAddress, uint16_t localPort,
244 Ipv6Address peerAddress, uint16_t peerPort)
246 NS_LOG_FUNCTION (
this << localAddress << localPort << peerAddress << peerPort);
247 return m_endPoints6->
Allocate (localAddress, localPort,
248 peerAddress, peerPort);
252 UdpL4Protocol::DeAllocate (Ipv6EndPoint *endPoint)
260 uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo,
262 const uint8_t payload[8])
264 NS_LOG_FUNCTION (
this << icmpSource << icmpTtl << icmpType << icmpCode << icmpInfo
265 << payloadSource << payloadDestination);
267 src = payload[0] << 8;
269 dst = payload[2] << 8;
272 Ipv4EndPoint *endPoint = m_endPoints->SimpleLookup (payloadSource, src, payloadDestination, dst);
275 endPoint->ForwardIcmp (icmpSource, icmpTtl, icmpType, icmpCode, icmpInfo);
279 NS_LOG_DEBUG (
"no endpoint found source=" << payloadSource <<
280 ", destination="<<payloadDestination<<
281 ", src=" << src <<
", dst=" << dst);
287 uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo,
289 const uint8_t payload[8])
291 NS_LOG_FUNCTION (
this << icmpSource << icmpTtl << icmpType << icmpCode << icmpInfo
292 << payloadSource << payloadDestination);
294 src = payload[0] << 8;
296 dst = payload[2] << 8;
302 endPoint->
ForwardIcmp (icmpSource, icmpTtl, icmpType, icmpCode, icmpInfo);
306 NS_LOG_DEBUG (
"no endpoint found source=" << payloadSource <<
307 ", destination="<<payloadDestination<<
308 ", src=" << src <<
", dst=" << dst);
312 enum IpL4Protocol::RxStatus
336 return IpL4Protocol::RX_CSUM_FAILED;
340 Ipv4EndPointDemux::EndPoints endPoints =
343 if (endPoints.empty ())
345 if (this->GetObject<Ipv6L3Protocol> () != 0)
347 NS_LOG_LOGIC (
" No Ipv4 endpoints matched on UdpL4Protocol, trying Ipv6 "<<
this);
354 return (this->
Receive (packet, ipv6Header, fakeInterface));
358 return IpL4Protocol::RX_ENDPOINT_UNREACH;
362 for (Ipv4EndPointDemux::EndPointsI endPoint = endPoints.begin ();
363 endPoint != endPoints.end (); endPoint++)
368 return IpL4Protocol::RX_OK;
371 enum IpL4Protocol::RxStatus
390 return IpL4Protocol::RX_CSUM_FAILED;
394 Ipv6EndPointDemux::EndPoints endPoints =
397 if (endPoints.empty ())
400 return IpL4Protocol::RX_ENDPOINT_UNREACH;
402 for (Ipv6EndPointDemux::EndPointsI endPoint = endPoints.begin ();
403 endPoint != endPoints.end (); endPoint++)
405 (*endPoint)->ForwardUp (packet->
Copy (), header, udpHeader.GetSourcePort ());
407 return IpL4Protocol::RX_OK;
413 uint16_t sport, uint16_t dport)
430 m_downTarget (packet, saddr, daddr, PROT_NUMBER, 0);
438 NS_LOG_FUNCTION (
this << packet << saddr << daddr << sport << dport << route);
453 m_downTarget (packet, saddr, daddr, PROT_NUMBER, route);
458 Ipv6Address saddr, Ipv6Address daddr,
459 uint16_t sport, uint16_t dport)
467 udpHeader.InitializeChecksum (saddr,
471 udpHeader.SetDestinationPort (dport);
472 udpHeader.SetSourcePort (sport);
474 packet->AddHeader (udpHeader);
476 m_downTarget6 (packet, saddr, daddr, PROT_NUMBER, 0);
481 Ipv6Address saddr, Ipv6Address daddr,
482 uint16_t sport, uint16_t dport, Ptr<Ipv6Route> route)
484 NS_LOG_FUNCTION (
this << packet << saddr << daddr << sport << dport << route);
489 udpHeader.EnableChecksums ();
490 udpHeader.InitializeChecksum (saddr,
494 udpHeader.SetDestinationPort (dport);
495 udpHeader.SetSourcePort (sport);
497 packet->AddHeader (udpHeader);
499 m_downTarget6 (packet, saddr, daddr, PROT_NUMBER, route);
506 m_downTarget = callback;
519 m_downTarget6 = callback;
522 IpL4Protocol::DownTargetCallback6
523 UdpL4Protocol::GetDownTarget6 (
void)
const
525 return m_downTarget6;
uint32_t RemoveHeader(Header &header)
Ptr< Socket > CreateSocket(void)
#define NS_LOG_FUNCTION(parameters)
virtual void DoDispose(void)
void Send(Ptr< Packet > packet, Ipv6Address source, Ipv6Address destination, uint8_t protocol, Ptr< Ipv6Route > route)
Higher-level layers call this method to send a packet down the stack to the MAC and PHY layers...
IPv6 layer implementation.
EndPoints Lookup(Ipv6Address dst, uint16_t dport, Ipv6Address src, uint16_t sport, Ptr< Ipv6Interface > incomingInterface)
lookup for a match with all the parameters.
virtual void SetDownTarget(IpL4Protocol::DownTargetCallback cb)
static bool ChecksumEnabled(void)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual void DoDispose(void)
#define NS_LOG_FUNCTION_NOARGS()
virtual int GetProtocolNumber(void) const
virtual enum IpL4Protocol::RxStatus Receive(Ptr< Packet > p, Ipv4Header const &header, Ptr< Ipv4Interface > interface)
Receive a packet up the protocol stack.
Ipv6EndPoint * Allocate(void)
Allocate a Ipv6EndPoint.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void AggregateObject(Ptr< Object > other)
#define NS_LOG_LOGIC(msg)
void DeAllocate(Ipv6EndPoint *endPoint)
Remove a end point.
virtual void NotifyNewAggregate(void)
Ptr< Packet > Copy(void) const
uint32_t PeekHeader(Header &header) const
virtual void ReceiveIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo, Ipv4Address payloadSource, Ipv4Address payloadDestination, const uint8_t payload[8])
Receive an ICMP packet.
Ipv6EndPoint * SimpleLookup(Ipv6Address dst, uint16_t dport, Ipv6Address src, uint16_t sport)
Simple lookup for a four-tuple match.
virtual void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)=0
virtual IpL4Protocol::DownTargetCallback GetDownTarget(void) const
Describes an IPv6 address.
Ipv4 addresses are stored in host order in this class.
virtual void NotifyNewAggregate()
#define NS_LOG_DEBUG(msg)
An IPv6 end point, four tuples identification.
void Send(Ptr< Packet > packet, Ipv4Address saddr, Ipv4Address daddr, uint16_t sport, uint16_t dport)
Send a packet via UDP.
void ForwardIcmp(Ipv6Address src, uint8_t ttl, uint8_t type, uint8_t code, uint32_t info)
Function called from an L4Protocol implementation to notify an endpoint of an icmp message reception...
static Ipv6Address MakeIpv4MappedAddress(Ipv4Address addr)
Make the Ipv4-mapped IPv6 address.
Ptr< T > GetObject(void) const
bool IsIpv4MappedAddress()
If the address is an IPv4-mapped address.
void AddHeader(const Header &header)
A representation of an internet endpoint/connection.