|
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2005 INRIA |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License version 2 as |
|
7 * published by the Free Software Foundation; |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 * |
|
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
19 */ |
|
20 |
|
21 #include "ns3/log.h" |
|
22 #include "ns3/assert.h" |
|
23 #include "ns3/packet.h" |
|
24 #include "ns3/node.h" |
|
25 |
|
26 #include "udp-l4-protocol.h" |
|
27 #include "udp-header.h" |
|
28 #include "ipv4-end-point-demux.h" |
|
29 #include "ipv4-end-point.h" |
|
30 #include "ipv4-l3-protocol.h" |
|
31 #include "udp-socket-impl.h" |
|
32 |
|
33 NS_LOG_COMPONENT_DEFINE ("UdpL4Protocol"); |
|
34 |
|
35 namespace ns3 { |
|
36 |
|
37 NS_OBJECT_ENSURE_REGISTERED (UdpL4Protocol); |
|
38 |
|
39 /* see http://www.iana.org/assignments/protocol-numbers */ |
|
40 const uint8_t UdpL4Protocol::PROT_NUMBER = 17; |
|
41 |
|
42 TypeId |
|
43 UdpL4Protocol::GetTypeId (void) |
|
44 { |
|
45 static TypeId tid = TypeId ("ns3::UdpL4Protocol") |
|
46 .SetParent<Ipv4L4Protocol> () |
|
47 .AddConstructor<UdpL4Protocol> () |
|
48 ; |
|
49 return tid; |
|
50 } |
|
51 |
|
52 UdpL4Protocol::UdpL4Protocol () |
|
53 : m_endPoints (new Ipv4EndPointDemux ()) |
|
54 { |
|
55 NS_LOG_FUNCTION_NOARGS (); |
|
56 } |
|
57 |
|
58 UdpL4Protocol::~UdpL4Protocol () |
|
59 { |
|
60 NS_LOG_FUNCTION_NOARGS (); |
|
61 } |
|
62 |
|
63 void |
|
64 UdpL4Protocol::SetNode (Ptr<Node> node) |
|
65 { |
|
66 m_node = node; |
|
67 } |
|
68 |
|
69 int |
|
70 UdpL4Protocol::GetProtocolNumber (void) const |
|
71 { |
|
72 return PROT_NUMBER; |
|
73 } |
|
74 int |
|
75 UdpL4Protocol::GetVersion (void) const |
|
76 { |
|
77 return 2; |
|
78 } |
|
79 |
|
80 |
|
81 void |
|
82 UdpL4Protocol::DoDispose (void) |
|
83 { |
|
84 NS_LOG_FUNCTION_NOARGS (); |
|
85 if (m_endPoints != 0) |
|
86 { |
|
87 delete m_endPoints; |
|
88 m_endPoints = 0; |
|
89 } |
|
90 m_node = 0; |
|
91 Ipv4L4Protocol::DoDispose (); |
|
92 } |
|
93 |
|
94 Ptr<Socket> |
|
95 UdpL4Protocol::CreateSocket (void) |
|
96 { |
|
97 NS_LOG_FUNCTION_NOARGS (); |
|
98 Ptr<UdpSocketImpl> socket = CreateObject<UdpSocketImpl> (); |
|
99 socket->SetNode (m_node); |
|
100 socket->SetUdp (this); |
|
101 return socket; |
|
102 } |
|
103 |
|
104 Ipv4EndPoint * |
|
105 UdpL4Protocol::Allocate (void) |
|
106 { |
|
107 NS_LOG_FUNCTION_NOARGS (); |
|
108 return m_endPoints->Allocate (); |
|
109 } |
|
110 |
|
111 Ipv4EndPoint * |
|
112 UdpL4Protocol::Allocate (Ipv4Address address) |
|
113 { |
|
114 NS_LOG_FUNCTION (this << address); |
|
115 return m_endPoints->Allocate (address); |
|
116 } |
|
117 |
|
118 Ipv4EndPoint * |
|
119 UdpL4Protocol::Allocate (uint16_t port) |
|
120 { |
|
121 NS_LOG_FUNCTION (this << port); |
|
122 return m_endPoints->Allocate (port); |
|
123 } |
|
124 |
|
125 Ipv4EndPoint * |
|
126 UdpL4Protocol::Allocate (Ipv4Address address, uint16_t port) |
|
127 { |
|
128 NS_LOG_FUNCTION (this << address << port); |
|
129 return m_endPoints->Allocate (address, port); |
|
130 } |
|
131 Ipv4EndPoint * |
|
132 UdpL4Protocol::Allocate (Ipv4Address localAddress, uint16_t localPort, |
|
133 Ipv4Address peerAddress, uint16_t peerPort) |
|
134 { |
|
135 NS_LOG_FUNCTION (this << localAddress << localPort << peerAddress << peerPort); |
|
136 return m_endPoints->Allocate (localAddress, localPort, |
|
137 peerAddress, peerPort); |
|
138 } |
|
139 |
|
140 void |
|
141 UdpL4Protocol::DeAllocate (Ipv4EndPoint *endPoint) |
|
142 { |
|
143 NS_LOG_FUNCTION (this << endPoint); |
|
144 m_endPoints->DeAllocate (endPoint); |
|
145 } |
|
146 |
|
147 void |
|
148 UdpL4Protocol::Receive(Ptr<Packet> packet, |
|
149 Ipv4Address const &source, |
|
150 Ipv4Address const &destination, |
|
151 Ptr<Ipv4Interface> interface) |
|
152 { |
|
153 NS_LOG_FUNCTION (this << packet << source << destination); |
|
154 |
|
155 UdpHeader udpHeader; |
|
156 packet->RemoveHeader (udpHeader); |
|
157 Ipv4EndPointDemux::EndPoints endPoints = |
|
158 m_endPoints->Lookup (destination, udpHeader.GetDestinationPort (), |
|
159 source, udpHeader.GetSourcePort (), interface); |
|
160 for (Ipv4EndPointDemux::EndPointsI endPoint = endPoints.begin (); |
|
161 endPoint != endPoints.end (); endPoint++) |
|
162 { |
|
163 (*endPoint)->ForwardUp (packet->Copy (), source, udpHeader.GetSourcePort ()); |
|
164 } |
|
165 } |
|
166 |
|
167 void |
|
168 UdpL4Protocol::Send (Ptr<Packet> packet, |
|
169 Ipv4Address saddr, Ipv4Address daddr, |
|
170 uint16_t sport, uint16_t dport) |
|
171 { |
|
172 NS_LOG_FUNCTION (this << packet << saddr << daddr << sport << dport); |
|
173 |
|
174 UdpHeader udpHeader; |
|
175 udpHeader.SetDestinationPort (dport); |
|
176 udpHeader.SetSourcePort (sport); |
|
177 udpHeader.SetPayloadSize (packet->GetSize ()); |
|
178 udpHeader.InitializeChecksum (saddr, |
|
179 daddr, |
|
180 PROT_NUMBER); |
|
181 |
|
182 packet->AddHeader (udpHeader); |
|
183 |
|
184 Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> (); |
|
185 if (ipv4 != 0) |
|
186 { |
|
187 NS_LOG_LOGIC ("Sending to IP"); |
|
188 ipv4->Send (packet, saddr, daddr, PROT_NUMBER); |
|
189 } |
|
190 } |
|
191 |
|
192 |
|
193 }; // namespace ns3 |
|
194 |