A Discrete-Event Network Simulator
API
ipcs-classifier-record.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007,2008, 2009 INRIA, UDcast
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
18  */
19 #include "ipcs-classifier-record.h"
20 
21 #include "wimax-tlv.h"
22 
23 #include "ns3/ipv4-address.h"
24 
25 #include <stdint.h>
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("IpcsClassifierRecord");
31 
33 {
34  m_priority = 255;
35  m_priority = 0;
36  m_index = 0;
37  m_tosLow = 0;
38  m_tosHigh = 0;
39  m_tosMask = 0;
40  m_cid = 0;
41  m_protocol.push_back(6); // tcp
42  m_protocol.push_back(17); // udp
43  AddSrcAddr(Ipv4Address("0.0.0.0"), Ipv4Mask("0.0.0.0"));
44  AddDstAddr(Ipv4Address("0.0.0.0"), Ipv4Mask("0.0.0.0"));
45  AddSrcPortRange(0, 65535);
46  AddDstPortRange(0, 65535);
47 }
48 
50 {
51 }
52 
54 {
56  "Invalid TLV");
59  m_priority = 0;
60  m_index = 0;
61  m_tosLow = 0;
62  m_tosHigh = 0;
63  m_tosMask = 0;
64  m_cid = 0;
65  for (auto iter = rules->Begin(); iter != rules->End(); ++iter)
66  {
67  switch ((*iter)->GetType())
68  {
70  m_priority = ((U8TlvValue*)((*iter)->PeekValue()))->GetValue();
71  break;
72  }
74  NS_FATAL_ERROR("ToS Not implemented-- please implement and contribute a patch");
75  break;
76  }
78  auto list = (ProtocolTlvValue*)(*iter)->PeekValue();
79  for (auto iter2 = list->Begin(); iter2 != list->End(); ++iter2)
80  {
81  AddProtocol(*iter2);
82  }
83  break;
84  }
86  auto list = (Ipv4AddressTlvValue*)(*iter)->PeekValue();
87  for (auto iter2 = list->Begin(); iter2 != list->End(); ++iter2)
88  {
89  AddSrcAddr((*iter2).Address, (*iter2).Mask);
90  }
91  break;
92  }
94  auto list = (Ipv4AddressTlvValue*)(*iter)->PeekValue();
95  for (auto iter2 = list->Begin(); iter2 != list->End(); ++iter2)
96  {
97  AddDstAddr((*iter2).Address, (*iter2).Mask);
98  }
99  break;
100  }
102  auto list = (PortRangeTlvValue*)(*iter)->PeekValue();
103  for (auto iter2 = list->Begin(); iter2 != list->End(); ++iter2)
104  {
105  AddSrcPortRange((*iter2).PortLow, (*iter2).PortHigh);
106  }
107  break;
108  }
110  auto list = (PortRangeTlvValue*)(*iter)->PeekValue();
111  for (auto iter2 = list->Begin(); iter2 != list->End(); ++iter2)
112  {
113  AddDstPortRange((*iter2).PortLow, (*iter2).PortHigh);
114  }
115  break;
116  }
118  m_index = ((U16TlvValue*)((*iter)->PeekValue()))->GetValue();
119  break;
120  }
121  }
122  }
123 }
124 
126  Ipv4Mask SrcMask,
127  Ipv4Address DstAddress,
128  Ipv4Mask DstMask,
129  uint16_t SrcPortLow,
130  uint16_t SrcPortHigh,
131  uint16_t DstPortLow,
132  uint16_t DstPortHigh,
133  uint8_t protocol,
134  uint8_t priority)
135 {
136  m_priority = priority;
137  m_protocol.push_back(protocol);
138  AddSrcAddr(SrcAddress, SrcMask);
139  AddDstAddr(DstAddress, DstMask);
140  AddSrcPortRange(SrcPortLow, SrcPortHigh);
141  AddDstPortRange(DstPortLow, DstPortHigh);
142  m_index = 0;
143  m_tosLow = 0;
144  m_tosHigh = 0;
145  m_tosMask = 0;
146  m_cid = 0;
147 }
148 
149 void
151 {
152  Ipv4Addr tmp;
153  tmp.Address = srcAddress;
154  tmp.Mask = srcMask;
155  m_srcAddr.push_back(tmp);
156 }
157 
158 void
160 {
161  Ipv4Addr tmp;
162  tmp.Address = dstAddress;
163  tmp.Mask = dstMask;
164  m_dstAddr.push_back(tmp);
165 }
166 
167 void
168 IpcsClassifierRecord::AddSrcPortRange(uint16_t srcPortLow, uint16_t srcPortHigh)
169 {
170  PortRange tmp;
171  tmp.PortLow = srcPortLow;
172  tmp.PortHigh = srcPortHigh;
173  m_srcPortRange.push_back(tmp);
174 }
175 
176 void
177 IpcsClassifierRecord::AddDstPortRange(uint16_t dstPortLow, uint16_t dstPortHigh)
178 {
179  PortRange tmp;
180  tmp.PortLow = dstPortLow;
181  tmp.PortHigh = dstPortHigh;
182  m_dstPortRange.push_back(tmp);
183 }
184 
185 void
187 {
188  m_protocol.push_back(proto);
189 }
190 
191 void
193 {
194  m_priority = prio;
195 }
196 
197 void
199 {
200  m_cid = cid;
201 }
202 
203 void
205 {
206  m_index = index;
207 }
208 
209 uint16_t
211 {
212  return m_index;
213 }
214 
215 uint16_t
217 {
218  return m_cid;
219 }
220 
221 uint8_t
223 {
224  return m_priority;
225 }
226 
227 bool
229 {
230  for (auto iter = m_srcAddr.begin(); iter != m_srcAddr.end(); ++iter)
231  {
232  NS_LOG_INFO("src addr check match: pkt=" << srcAddress << " cls=" << (*iter).Address << "/"
233  << (*iter).Mask);
234  if (srcAddress.CombineMask((*iter).Mask) == (*iter).Address)
235  {
236  return true;
237  }
238  }
239  NS_LOG_INFO("NOT OK!");
240  return false;
241 }
242 
243 bool
245 {
246  for (auto iter = m_dstAddr.begin(); iter != m_dstAddr.end(); ++iter)
247  {
248  NS_LOG_INFO("dst addr check match: pkt=" << dstAddress << " cls=" << (*iter).Address << "/"
249  << (*iter).Mask);
250  if (dstAddress.CombineMask((*iter).Mask) == (*iter).Address)
251  {
252  return true;
253  }
254  }
255  NS_LOG_INFO("NOT OK!");
256  return false;
257 }
258 
259 bool
261 {
262  for (auto iter = m_srcPortRange.begin(); iter != m_srcPortRange.end(); ++iter)
263  {
264  NS_LOG_INFO("src port check match: pkt=" << port << " cls= [" << (*iter).PortLow << " TO "
265  << (*iter).PortHigh << "]");
266  if (port >= (*iter).PortLow && port <= (*iter).PortHigh)
267  {
268  return true;
269  }
270  }
271  NS_LOG_INFO("NOT OK!");
272  return false;
273 }
274 
275 bool
277 {
278  for (auto iter = m_dstPortRange.begin(); iter != m_dstPortRange.end(); ++iter)
279  {
280  NS_LOG_INFO("dst port check match: pkt=" << port << " cls= [" << (*iter).PortLow << " TO "
281  << (*iter).PortHigh << "]");
282  if (port >= (*iter).PortLow && port <= (*iter).PortHigh)
283  {
284  return true;
285  }
286  }
287  NS_LOG_INFO("NOT OK!");
288  return false;
289 }
290 
291 bool
293 {
294  for (auto iter = m_protocol.begin(); iter != m_protocol.end(); ++iter)
295  {
296  NS_LOG_INFO("proto check match: pkt=" << (uint16_t)proto << " cls=" << (uint16_t)proto);
297  if (proto == (*iter))
298  {
299  return true;
300  }
301  }
302  NS_LOG_INFO("NOT OK!");
303  return false;
304 }
305 
306 bool
308  Ipv4Address dstAddress,
309  uint16_t srcPort,
310  uint16_t dstPort,
311  uint8_t proto) const
312 {
313  return (CheckMatchProtocol(proto) && CheckMatchDstPort(dstPort) && CheckMatchSrcPort(srcPort) &&
314  CheckMatchDstAddr(dstAddress) && CheckMatchSrcAddr(srcAddress));
315 }
316 
317 Tlv
319 {
320  Ipv4AddressTlvValue ipv4AddrValSrc;
321  for (auto iter = m_srcAddr.begin(); iter != m_srcAddr.end(); ++iter)
322  {
323  ipv4AddrValSrc.Add((*iter).Address, (*iter).Mask);
324  }
325 
326  Ipv4AddressTlvValue ipv4AddrValDst;
327  for (auto iter = m_dstAddr.begin(); iter != m_dstAddr.end(); ++iter)
328  {
329  ipv4AddrValDst.Add((*iter).Address, (*iter).Mask);
330  }
331 
332  ProtocolTlvValue protoVal;
333  for (auto iter = m_protocol.begin(); iter != m_protocol.end(); ++iter)
334  {
335  protoVal.Add(*iter);
336  }
337 
338  PortRangeTlvValue portValueSrc;
339  for (auto iter = m_srcPortRange.begin(); iter != m_srcPortRange.end(); ++iter)
340  {
341  portValueSrc.Add((*iter).PortLow, (*iter).PortHigh);
342  }
343 
344  PortRangeTlvValue portValueDst;
345  for (auto iter = m_dstPortRange.begin(); iter != m_dstPortRange.end(); ++iter)
346  {
347  portValueDst.Add((*iter).PortLow, (*iter).PortHigh);
348  }
349 
352  ClassVectVal.Add(
355  ipv4AddrValSrc.GetSerializedSize(),
356  ipv4AddrValSrc));
358  ipv4AddrValDst.GetSerializedSize(),
359  ipv4AddrValDst));
361  portValueSrc.GetSerializedSize(),
362  portValueSrc));
364  portValueDst.GetSerializedSize(),
365  portValueDst));
367 
369  ClassVectVal.GetSerializedSize(),
370  ClassVectVal);
371 
372  return tmp_tlv;
373 }
374 
375 } // namespace ns3
this class implements the classifier descriptor as a tlv vector
Definition: wimax-tlv.h:408
void SetPriority(uint8_t prio)
Set the priority of this classifier.
std::vector< PortRange > m_dstPortRange
destination port range
bool CheckMatchSrcAddr(Ipv4Address srcAddress) const
Check match source address function.
bool CheckMatchProtocol(uint8_t proto) const
Check match protocol function.
void SetIndex(uint16_t index)
Set the index of the classifier.
void AddDstAddr(Ipv4Address dstAddress, Ipv4Mask dstMask)
add a new destination ip address to the classifier
bool CheckMatchSrcPort(uint16_t srcPort) const
Check match source port function.
std::vector< PortRange > m_srcPortRange
source port range
void AddSrcPortRange(uint16_t srcPortLow, uint16_t srcPortHigh)
add a range of source port to the classifier
std::vector< Ipv4Addr > m_dstAddr
destination address
bool CheckMatchDstAddr(Ipv4Address dstAddress) const
Check match destination address function.
Tlv ToTlv() const
Creates a TLV from this classifier.
bool CheckMatch(Ipv4Address srcAddress, Ipv4Address dstAddress, uint16_t srcPort, uint16_t dstPort, uint8_t proto) const
check if a packets can be used with this classifier
void SetCid(uint16_t cid)
Set the cid associated to this classifier.
void AddDstPortRange(uint16_t dstPortLow, uint16_t dstPortHigh)
add a range of destination port to the classifier
std::vector< uint8_t > m_protocol
protocol
void AddSrcAddr(Ipv4Address srcAddress, Ipv4Mask srcMask)
add a new source ip address to the classifier
std::vector< Ipv4Addr > m_srcAddr
source address
void AddProtocol(uint8_t proto)
add a protocol to the classifier
bool CheckMatchDstPort(uint16_t dstPort) const
Check match destination port function.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Ipv4Address CombineMask(const Ipv4Mask &mask) const
Combine this address with a network mask.
Ipv4AddressTlvValue class.
Definition: wimax-tlv.h:573
void Add(Ipv4Address address, Ipv4Mask mask)
Add IPv4 address and mask.
Definition: wimax-tlv.cc:1062
uint32_t GetSerializedSize() const override
Get serialized size in bytes.
Definition: wimax-tlv.cc:1032
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
PortRangeTlvValue class.
Definition: wimax-tlv.h:484
uint32_t GetSerializedSize() const override
Get serialized size in bytes.
Definition: wimax-tlv.cc:880
void Add(uint16_t portLow, uint16_t portHigh)
Add a range.
Definition: wimax-tlv.cc:910
ProtocolTlvValue class.
Definition: wimax-tlv.h:532
void Add(uint8_t protocol)
Add protocol number.
Definition: wimax-tlv.cc:986
uint32_t GetSerializedSize() const override
Get serialized size in bytes.
Definition: wimax-tlv.cc:959
This class implements the Type-Len-Value structure channel encodings as described by "IEEE Standard f...
Definition: wimax-tlv.h:87
uint8_t GetType() const
Get type value.
Definition: wimax-tlv.cc:211
TlvValue * PeekValue()
Peek value.
Definition: wimax-tlv.cc:223
U16TlvValue class.
Definition: wimax-tlv.h:215
U8TlvValue class.
Definition: wimax-tlv.h:175
uint32_t GetSerializedSize() const override
Get serialized size in bytes.
Definition: wimax-tlv.cc:251
Iterator End() const
End iterator.
Definition: wimax-tlv.cc:278
Iterator Begin() const
Begin iterator.
Definition: wimax-tlv.cc:272
void Add(const Tlv &val)
Add a TLV.
Definition: wimax-tlv.cc:284
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define list