author | Tom Henderson <tomh@tomh.org> |
Thu, 28 May 2009 21:37:25 -0700 | |
changeset 4472 | e20a31541404 |
parent 4375 | db81fdcb06e7 |
child 4567 | 32ca321e4fe7 |
permissions | -rw-r--r-- |
3820 | 1 |
#include "ipv4-raw-socket-impl.h" |
2 |
#include "ipv4-l3-protocol.h" |
|
3 |
#include "icmpv4.h" |
|
4 |
#include "ns3/inet-socket-address.h" |
|
5 |
#include "ns3/node.h" |
|
6 |
#include "ns3/packet.h" |
|
7 |
#include "ns3/uinteger.h" |
|
8 |
#include "ns3/log.h" |
|
9 |
||
10 |
NS_LOG_COMPONENT_DEFINE ("Ipv4RawSocketImpl"); |
|
11 |
||
12 |
namespace ns3 { |
|
13 |
||
14 |
NS_OBJECT_ENSURE_REGISTERED (Ipv4RawSocketImpl); |
|
15 |
||
16 |
TypeId |
|
17 |
Ipv4RawSocketImpl::GetTypeId (void) |
|
18 |
{ |
|
19 |
static TypeId tid = TypeId ("ns3::Ipv4RawSocketImpl") |
|
20 |
.SetParent<Socket> () |
|
21 |
.AddAttribute ("Protocol", "Protocol number to match.", |
|
22 |
UintegerValue (0), |
|
23 |
MakeUintegerAccessor (&Ipv4RawSocketImpl::m_protocol), |
|
24 |
MakeUintegerChecker<uint16_t> ()) |
|
25 |
.AddAttribute ("IcmpFilter", |
|
26 |
"Any icmp header whose type field matches a bit in this filter is dropped.", |
|
27 |
UintegerValue (0), |
|
28 |
MakeUintegerAccessor (&Ipv4RawSocketImpl::m_icmpFilter), |
|
29 |
MakeUintegerChecker<uint32_t> ()) |
|
30 |
; |
|
31 |
return tid; |
|
32 |
} |
|
33 |
||
34 |
Ipv4RawSocketImpl::Ipv4RawSocketImpl () |
|
35 |
{ |
|
36 |
NS_LOG_FUNCTION (this); |
|
37 |
m_err = Socket::ERROR_NOTERROR; |
|
38 |
m_node = 0; |
|
39 |
m_src = Ipv4Address::GetAny (); |
|
40 |
m_dst = Ipv4Address::GetAny (); |
|
41 |
m_protocol = 0; |
|
42 |
m_shutdownSend = false; |
|
43 |
m_shutdownRecv = false; |
|
44 |
} |
|
45 |
||
46 |
void |
|
47 |
Ipv4RawSocketImpl::SetNode (Ptr<Node> node) |
|
48 |
{ |
|
49 |
m_node = node; |
|
50 |
} |
|
51 |
||
52 |
void |
|
53 |
Ipv4RawSocketImpl::DoDispose (void) |
|
54 |
{ |
|
55 |
NS_LOG_FUNCTION (this); |
|
56 |
m_node = 0; |
|
57 |
Socket::DoDispose (); |
|
58 |
} |
|
59 |
||
60 |
enum Socket::SocketErrno |
|
61 |
Ipv4RawSocketImpl::GetErrno (void) const |
|
62 |
{ |
|
63 |
NS_LOG_FUNCTION (this); |
|
64 |
return m_err; |
|
65 |
} |
|
66 |
Ptr<Node> |
|
67 |
Ipv4RawSocketImpl::GetNode (void) const |
|
68 |
{ |
|
69 |
NS_LOG_FUNCTION (this); |
|
70 |
return m_node; |
|
71 |
} |
|
72 |
int |
|
73 |
Ipv4RawSocketImpl::Bind (const Address &address) |
|
74 |
{ |
|
75 |
NS_LOG_FUNCTION (this << address); |
|
76 |
if (!InetSocketAddress::IsMatchingType (address)) |
|
77 |
{ |
|
78 |
m_err = Socket::ERROR_INVAL; |
|
79 |
return -1; |
|
80 |
} |
|
81 |
InetSocketAddress ad = InetSocketAddress::ConvertFrom (address); |
|
82 |
m_src = ad.GetIpv4 (); |
|
83 |
return 0; |
|
84 |
} |
|
85 |
int |
|
86 |
Ipv4RawSocketImpl::Bind (void) |
|
87 |
{ |
|
88 |
NS_LOG_FUNCTION (this); |
|
89 |
m_src = Ipv4Address::GetAny (); |
|
90 |
return 0; |
|
91 |
} |
|
92 |
int |
|
93 |
Ipv4RawSocketImpl::GetSockName (Address &address) const |
|
94 |
{ |
|
95 |
address = InetSocketAddress (m_src, 0); |
|
96 |
return 0; |
|
97 |
} |
|
98 |
int |
|
99 |
Ipv4RawSocketImpl::Close (void) |
|
100 |
{ |
|
101 |
NS_LOG_FUNCTION (this); |
|
102 |
Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> (); |
|
103 |
if (ipv4 != 0) |
|
104 |
{ |
|
105 |
ipv4->DeleteRawSocket (this); |
|
106 |
} |
|
107 |
return 0; |
|
108 |
} |
|
109 |
int |
|
110 |
Ipv4RawSocketImpl::ShutdownSend (void) |
|
111 |
{ |
|
112 |
NS_LOG_FUNCTION (this); |
|
113 |
m_shutdownSend = true; |
|
114 |
return 0; |
|
115 |
} |
|
116 |
int |
|
117 |
Ipv4RawSocketImpl::ShutdownRecv (void) |
|
118 |
{ |
|
119 |
NS_LOG_FUNCTION (this); |
|
120 |
m_shutdownRecv = true; |
|
121 |
return 0; |
|
122 |
} |
|
123 |
int |
|
124 |
Ipv4RawSocketImpl::Connect (const Address &address) |
|
125 |
{ |
|
126 |
NS_LOG_FUNCTION (this << address); |
|
127 |
if (!InetSocketAddress::IsMatchingType (address)) |
|
128 |
{ |
|
129 |
m_err = Socket::ERROR_INVAL; |
|
130 |
return -1; |
|
131 |
} |
|
132 |
InetSocketAddress ad = InetSocketAddress::ConvertFrom (address); |
|
133 |
m_dst = ad.GetIpv4 (); |
|
134 |
return 0; |
|
135 |
} |
|
136 |
int |
|
137 |
Ipv4RawSocketImpl::Listen (void) |
|
138 |
{ |
|
139 |
NS_LOG_FUNCTION (this); |
|
140 |
m_err = Socket::ERROR_OPNOTSUPP; |
|
141 |
return -1; |
|
142 |
} |
|
143 |
uint32_t |
|
144 |
Ipv4RawSocketImpl::GetTxAvailable (void) const |
|
145 |
{ |
|
146 |
NS_LOG_FUNCTION (this); |
|
147 |
return 0xffffffff; |
|
148 |
} |
|
149 |
int |
|
150 |
Ipv4RawSocketImpl::Send (Ptr<Packet> p, uint32_t flags) |
|
151 |
{ |
|
152 |
NS_LOG_FUNCTION (this << p << flags); |
|
153 |
InetSocketAddress to = InetSocketAddress (m_dst, m_protocol); |
|
154 |
return SendTo (p, flags, to); |
|
155 |
} |
|
156 |
int |
|
157 |
Ipv4RawSocketImpl::SendTo (Ptr<Packet> p, uint32_t flags, |
|
158 |
const Address &toAddress) |
|
159 |
{ |
|
160 |
NS_LOG_FUNCTION (this << p << flags << toAddress); |
|
161 |
if (!InetSocketAddress::IsMatchingType (toAddress)) |
|
162 |
{ |
|
163 |
m_err = Socket::ERROR_INVAL; |
|
164 |
return -1; |
|
165 |
} |
|
166 |
if (m_shutdownSend) |
|
167 |
{ |
|
168 |
return 0; |
|
169 |
} |
|
170 |
InetSocketAddress ad = InetSocketAddress::ConvertFrom (toAddress); |
|
171 |
Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> (); |
|
172 |
Ipv4Address dst = ad.GetIpv4 (); |
|
4472
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
173 |
if (ipv4->GetRoutingProtocol ()) |
3820 | 174 |
{ |
4472
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
175 |
Ipv4Header header; |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
176 |
header.SetDestination (dst); |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
177 |
SocketErrno errno = ERROR_NOTERROR;; |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
178 |
Ptr<Ipv4Route> route; |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
179 |
uint32_t oif = 0; //specify non-zero if bound to a source address |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
180 |
// TBD-- we could cache the route and just check its validity |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
181 |
route = ipv4->GetRoutingProtocol ()->RouteOutput (header, oif, errno); |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
182 |
if (route != 0) |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
183 |
{ |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
184 |
NS_LOG_LOGIC ("Route exists"); |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
185 |
ipv4->Send (p, route->GetSource (), dst, m_protocol, route); |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
186 |
} |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
187 |
else |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
188 |
{ |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
189 |
NS_LOG_DEBUG ("dropped because no outgoing route."); |
e20a31541404
src/ and utils/ changes for IPv4 routing rework
Tom Henderson <tomh@tomh.org>
parents:
4375
diff
changeset
|
190 |
} |
3820 | 191 |
} |
192 |
return 0; |
|
193 |
} |
|
194 |
uint32_t |
|
195 |
Ipv4RawSocketImpl::GetRxAvailable (void) const |
|
196 |
{ |
|
197 |
NS_LOG_FUNCTION (this); |
|
198 |
uint32_t rx = 0; |
|
199 |
for (std::list<Data>::const_iterator i = m_recv.begin (); i != m_recv.end (); ++i) |
|
200 |
{ |
|
201 |
rx += (i->packet)->GetSize (); |
|
202 |
} |
|
203 |
return rx; |
|
204 |
} |
|
205 |
Ptr<Packet> |
|
206 |
Ipv4RawSocketImpl::Recv (uint32_t maxSize, uint32_t flags) |
|
207 |
{ |
|
208 |
NS_LOG_FUNCTION (this << maxSize << flags); |
|
209 |
Address tmp; |
|
210 |
return RecvFrom (maxSize, flags, tmp); |
|
211 |
} |
|
212 |
Ptr<Packet> |
|
213 |
Ipv4RawSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags, |
|
214 |
Address &fromAddress) |
|
215 |
{ |
|
216 |
NS_LOG_FUNCTION (this << maxSize << flags << fromAddress); |
|
217 |
if (m_recv.empty ()) |
|
218 |
{ |
|
219 |
return 0; |
|
220 |
} |
|
221 |
struct Data data = m_recv.front (); |
|
222 |
m_recv.pop_front (); |
|
223 |
if (data.packet->GetSize () > maxSize) |
|
224 |
{ |
|
225 |
Ptr<Packet> first = data.packet->CreateFragment (0, maxSize); |
|
226 |
data.packet->RemoveAtStart (maxSize); |
|
227 |
m_recv.push_front (data); |
|
228 |
return first; |
|
229 |
} |
|
230 |
InetSocketAddress inet = InetSocketAddress (data.fromIp, data.fromProtocol); |
|
231 |
fromAddress = inet; |
|
232 |
return data.packet; |
|
233 |
} |
|
234 |
||
235 |
void |
|
236 |
Ipv4RawSocketImpl::SetProtocol (uint16_t protocol) |
|
237 |
{ |
|
238 |
NS_LOG_FUNCTION (this << protocol); |
|
239 |
m_protocol = protocol; |
|
240 |
} |
|
241 |
||
242 |
bool |
|
243 |
Ipv4RawSocketImpl::ForwardUp (Ptr<const Packet> p, Ipv4Header ipHeader, Ptr<NetDevice> device) |
|
244 |
{ |
|
245 |
NS_LOG_FUNCTION (this << *p << ipHeader << device); |
|
246 |
if (m_shutdownRecv) |
|
247 |
{ |
|
248 |
return false; |
|
249 |
} |
|
250 |
if ((m_src == Ipv4Address::GetAny () || ipHeader.GetDestination () == m_src) && |
|
251 |
(m_dst == Ipv4Address::GetAny () || ipHeader.GetSource () == m_dst) && |
|
252 |
ipHeader.GetProtocol () == m_protocol) |
|
253 |
{ |
|
254 |
Ptr<Packet> copy = p->Copy (); |
|
255 |
if (m_protocol == 1) |
|
256 |
{ |
|
257 |
Icmpv4Header icmpHeader; |
|
258 |
copy->PeekHeader (icmpHeader); |
|
259 |
uint8_t type = icmpHeader.GetType (); |
|
260 |
if (type < 32 && |
|
261 |
((1 << type) & m_icmpFilter)) |
|
262 |
{ |
|
263 |
// filter out icmp packet. |
|
264 |
return false; |
|
265 |
} |
|
266 |
} |
|
267 |
copy->AddHeader (ipHeader); |
|
268 |
struct Data data; |
|
269 |
data.packet = copy; |
|
270 |
data.fromIp = ipHeader.GetSource (); |
|
271 |
data.fromProtocol = ipHeader.GetProtocol (); |
|
272 |
m_recv.push_back (data); |
|
273 |
NotifyDataRecv (); |
|
274 |
return true; |
|
275 |
} |
|
276 |
return false; |
|
277 |
} |
|
278 |
||
279 |
} // namespace ns3 |