A Discrete-Event Network Simulator
API
ipcs-classifier-record.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007,2008, 2009 INRIA, UDcast
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  * Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19  */
20 #include "ipcs-classifier-record.h"
21 #include <stdint.h>
22 #include "ns3/ipv4-address.h"
23 #include "wimax-tlv.h"
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("IpcsClassifierRecord");
28 
30 {
31  m_priority = 255;
32  m_priority = 0;
33  m_index = 0;
34  m_tosLow = 0;
35  m_tosHigh = 0;
36  m_tosMask = 0;
37  m_cid = 0;
38  m_protocol.push_back (6); // tcp
39  m_protocol.push_back (17); // udp
40  AddSrcAddr (Ipv4Address ("0.0.0.0"), Ipv4Mask ("0.0.0.0"));
41  AddDstAddr (Ipv4Address ("0.0.0.0"), Ipv4Mask ("0.0.0.0"));
42  AddSrcPortRange (0, 65535);
43  AddDstPortRange (0, 65535);
44 }
45 
47 {
48 }
49 
51 {
54  m_priority = 0;
55  m_index = 0;
56  m_tosLow = 0;
57  m_tosHigh = 0;
58  m_tosMask = 0;
59  m_cid = 0;
60  for (std::vector<Tlv*>::const_iterator iter = rules->Begin (); iter != rules->End (); ++iter)
61  {
62  switch ((*iter)->GetType ())
63  {
65  {
66  m_priority = ((U8TlvValue*)((*iter)->PeekValue ()))->GetValue ();
67  break;
68  }
70  {
71  NS_FATAL_ERROR ("ToS Not implemented-- please implement and contribute a patch");
72  break;
73  }
75  {
76  ProtocolTlvValue * list = (ProtocolTlvValue *)(*iter)->PeekValue ();
77  for (std::vector<uint8_t>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
78  {
79  AddProtocol (*iter2);
80  }
81  break;
82  }
84  {
85  Ipv4AddressTlvValue * list = (Ipv4AddressTlvValue *)(*iter)->PeekValue ();
86  for (std::vector<Ipv4AddressTlvValue::ipv4Addr>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
87  {
88  AddSrcAddr ((*iter2).Address, (*iter2).Mask);
89  }
90  break;
91  }
93  {
94  Ipv4AddressTlvValue * list = (Ipv4AddressTlvValue *)(*iter)->PeekValue ();
95  for (std::vector<Ipv4AddressTlvValue::ipv4Addr>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
96  {
97  AddDstAddr ((*iter2).Address, (*iter2).Mask);
98  }
99  break;
100  }
102  {
103  PortRangeTlvValue * list = (PortRangeTlvValue *)(*iter)->PeekValue ();
104  for (std::vector<PortRangeTlvValue::PortRange>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
105  {
106  AddSrcPortRange ((*iter2).PortLow, (*iter2).PortHigh);
107  }
108  break;
109  }
111  {
112  PortRangeTlvValue * list = (PortRangeTlvValue *)(*iter)->PeekValue ();
113  for (std::vector<PortRangeTlvValue::PortRange>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
114  {
115  AddDstPortRange ((*iter2).PortLow, (*iter2).PortHigh);
116  }
117  break;
118  }
120  {
121  m_index = ((U16TlvValue*)((*iter)->PeekValue ()))->GetValue ();
122  break;
123  }
124  }
125  }
126 }
127 
129  Ipv4Mask SrcMask,
130  Ipv4Address DstAddress,
131  Ipv4Mask DstMask,
132  uint16_t SrcPortLow,
133  uint16_t SrcPortHigh,
134  uint16_t DstPortLow,
135  uint16_t DstPortHigh,
136  uint8_t protocol,
137  uint8_t priority)
138 {
139  m_priority = priority;
140  m_protocol.push_back (protocol);
141  AddSrcAddr (SrcAddress, SrcMask);
142  AddDstAddr (DstAddress, DstMask);
143  AddSrcPortRange (SrcPortLow, SrcPortHigh);
144  AddDstPortRange (DstPortLow, DstPortHigh);
145  m_index = 0;
146  m_tosLow = 0;
147  m_tosHigh = 0;
148  m_tosMask = 0;
149  m_cid = 0;
150 }
151 
152 void
154 {
155  struct ipv4Addr tmp;
156  tmp.Address = srcAddress;
157  tmp.Mask = srcMask;
158  m_srcAddr.push_back (tmp);
159 }
160 void
162 {
163  struct ipv4Addr tmp;
164  tmp.Address = dstAddress;
165  tmp.Mask = dstMask;
166  m_dstAddr.push_back (tmp);
167 }
168 void
169 IpcsClassifierRecord::AddSrcPortRange (uint16_t srcPortLow, uint16_t srcPortHigh)
170 {
171  struct PortRange tmp;
172  tmp.PortLow = srcPortLow;
173  tmp.PortHigh = srcPortHigh;
174  m_srcPortRange.push_back (tmp);
175 
176 }
177 void
178 IpcsClassifierRecord::AddDstPortRange (uint16_t dstPortLow, uint16_t dstPortHigh)
179 {
180  struct PortRange tmp;
181  tmp.PortLow = dstPortLow;
182  tmp.PortHigh = dstPortHigh;
183  m_dstPortRange.push_back (tmp);
184 }
185 void
187 {
188  m_protocol.push_back (proto);
189 }
190 void
192 {
193  m_priority = prio;
194 }
195 void
197 {
198  m_cid = cid;
199 }
200 void
202 {
203  m_index = index;
204 }
205 
206 uint16_t
208 {
209  return m_index;
210 }
211 uint16_t
213 {
214  return m_cid;
215 }
216 uint8_t
218 {
219  return m_priority;
220 }
221 
222 bool
224 {
225  for (std::vector<struct ipv4Addr>::const_iterator iter = m_srcAddr.begin (); iter != m_srcAddr.end (); ++iter)
226  {
227  NS_LOG_INFO ("src addr check match: pkt=" << srcAddress << " cls=" << (*iter).Address << "/" << (*iter).Mask);
228  if (srcAddress.CombineMask ((*iter).Mask) == (*iter).Address)
229  {
230  return true;
231  }
232  }
233  NS_LOG_INFO ("NOT OK!");
234  return false;
235 }
236 bool
238 {
239 
240  for (std::vector<struct ipv4Addr>::const_iterator iter = m_dstAddr.begin (); iter != m_dstAddr.end (); ++iter)
241  {
242  NS_LOG_INFO ("dst addr check match: pkt=" << dstAddress << " cls=" << (*iter).Address << "/" << (*iter).Mask);
243  if (dstAddress.CombineMask ((*iter).Mask) == (*iter).Address)
244  {
245  return true;
246  }
247  }
248  NS_LOG_INFO ("NOT OK!");
249  return false;
250 }
251 bool
253 {
254  for (std::vector<struct PortRange>::const_iterator iter = m_srcPortRange.begin (); iter != m_srcPortRange.end (); ++iter)
255  {
256  NS_LOG_INFO ("src port check match: pkt=" << port << " cls= [" << (*iter).PortLow << " TO " << (*iter).PortHigh
257  << "]");
258  if (port >= (*iter).PortLow && port <= (*iter).PortHigh)
259  {
260  return true;
261  }
262  }
263  NS_LOG_INFO ("NOT OK!");
264  return false;
265 }
266 bool
268 {
269  for (std::vector<struct PortRange>::const_iterator iter = m_dstPortRange.begin (); iter != m_dstPortRange.end (); ++iter)
270  {
271  NS_LOG_INFO ("dst port check match: pkt=" << port << " cls= [" << (*iter).PortLow << " TO " << (*iter).PortHigh
272  << "]");
273  if (port >= (*iter).PortLow && port <= (*iter).PortHigh)
274  {
275  return true;
276  }
277  }
278  NS_LOG_INFO ("NOT OK!");
279  return false;
280 }
281 bool
283 {
284  for (std::vector<uint8_t>::const_iterator iter = m_protocol.begin (); iter != m_protocol.end (); ++iter)
285  {
286  NS_LOG_INFO ("proto check match: pkt=" << (uint16_t) proto << " cls=" << (uint16_t) proto);
287  if (proto == (*iter))
288  {
289  return true;
290  }
291  }
292  NS_LOG_INFO ("NOT OK!");
293  return false;
294 }
295 bool
297  Ipv4Address dstAddress,
298  uint16_t srcPort,
299  uint16_t dstPort,
300  uint8_t proto) const
301 {
302  return (CheckMatchProtocol (proto) && CheckMatchDstPort (dstPort) && CheckMatchSrcPort (srcPort)
303  && CheckMatchDstAddr (dstAddress) && CheckMatchSrcAddr (srcAddress));
304 }
305 
306 Tlv
308 {
309  Ipv4AddressTlvValue ipv4AddrValSrc;
310  for (std::vector<struct ipv4Addr>::const_iterator iter = m_srcAddr.begin (); iter != m_srcAddr.end (); ++iter)
311  {
312  ipv4AddrValSrc.Add ((*iter).Address, (*iter).Mask);
313  }
314 
315  Ipv4AddressTlvValue ipv4AddrValDst;
316  for (std::vector<struct ipv4Addr>::const_iterator iter = m_dstAddr.begin (); iter != m_dstAddr.end (); ++iter)
317  {
318  ipv4AddrValDst.Add ((*iter).Address, (*iter).Mask);
319  }
320 
321  ProtocolTlvValue protoVal;
322  for (std::vector<uint8_t>::const_iterator iter = m_protocol.begin (); iter != m_protocol.end (); ++iter)
323  {
324  protoVal.Add ((*iter));
325  }
326 
327  PortRangeTlvValue portValueSrc;
328  for (std::vector<struct PortRange>::const_iterator iter = m_srcPortRange.begin (); iter != m_srcPortRange.end (); ++iter)
329  {
330  portValueSrc.Add ((*iter).PortLow, (*iter).PortHigh);
331  }
332 
333  PortRangeTlvValue portValueDst;
334  for (std::vector<struct PortRange>::const_iterator iter = m_dstPortRange.begin (); iter != m_dstPortRange.end (); ++iter)
335  {
336  portValueDst.Add ((*iter).PortLow, (*iter).PortHigh);
337  }
338 
341  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::Protocol, protoVal.GetSerializedSize (), protoVal));
342  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::IP_src, ipv4AddrValSrc.GetSerializedSize (), ipv4AddrValSrc));
343  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::IP_dst, ipv4AddrValDst.GetSerializedSize (), ipv4AddrValDst));
344  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::Port_src, portValueSrc.GetSerializedSize (), portValueSrc));
345  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::Port_dst, portValueDst.GetSerializedSize (), portValueDst));
347 
348  Tlv tmp_tlv (CsParamVectorTlvValue::Packet_Classification_Rule, ClassVectVal.GetSerializedSize (), ClassVectVal);
349 
350  return tmp_tlv;
351 }
352 
353 } // namespace ns3
std::vector< struct PortRange > m_srcPortRange
surce port range
U16TlvValue class.
Definition: wimax-tlv.h:209
Iterator Begin() const
Begin iterator.
Definition: wimax-tlv.cc:998
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
ProtocolTlvValue class.
Definition: wimax-tlv.h:516
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
Iterator End() const
End iterator.
Definition: wimax-tlv.cc:1083
virtual uint32_t GetSerializedSize(void) const
Get serialized size in bytes.
Definition: wimax-tlv.cc:891
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
bool CheckMatchProtocol(uint8_t proto) const
Check match protocol function.
void Add(uint16_t portLow, uint16_t portHigh)
Add a range.
Definition: wimax-tlv.cc:918
virtual uint32_t GetSerializedSize(void) const
Get serialized size in bytes.
Definition: wimax-tlv.cc:251
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual uint32_t GetSerializedSize(void) const
Get serialized size in bytes.
Definition: wimax-tlv.cc:965
Ipv4AddressTlvValue class.
Definition: wimax-tlv.h:556
bool CheckMatchSrcAddr(Ipv4Address srcAddress) const
Check match source address function.
This class implements the Type-Len-Value structure channel encodings as described by "IEEE Standard f...
Definition: wimax-tlv.h:83
uint16_t port
Definition: dsdv-manet.cc:44
std::vector< uint8_t > m_protocol
protocol
void Add(const Tlv &val)
Add a TLV.
Definition: wimax-tlv.cc:284
virtual uint32_t GetSerializedSize(void) const
Get serialized size in bytes.
Definition: wimax-tlv.cc:1038
void AddSrcAddr(Ipv4Address srcAddress, Ipv4Mask srcMask)
add a new source ip address to the classifier
std::vector< struct ipv4Addr > m_dstAddr
destination address
void AddSrcPortRange(uint16_t srcPortLow, uint16_t srcPortHigh)
add a range of source port to the classifier
U8TlvValue class.
Definition: wimax-tlv.h:170
std::vector< struct PortRange > m_dstPortRange
destination port range
void AddDstAddr(Ipv4Address dstAddress, Ipv4Mask dstMask)
add a new destination ip address to the classifier
bool CheckMatchDstPort(uint16_t dstPort) const
Check match destination port function.
void SetCid(uint16_t cid)
Set the cid associated to this classifier.
Iterator End() const
End iterator.
Definition: wimax-tlv.cc:1004
void SetPriority(uint8_t prio)
Set the priority of this classifier.
#define list
uint8_t GetType(void) const
Get type value.
Definition: wimax-tlv.cc:214
void Add(uint8_t protocol)
Add protocol number.
Definition: wimax-tlv.cc:992
bool CheckMatchSrcPort(uint16_t srcPort) const
Check match source port function.
bool CheckMatchDstAddr(Ipv4Address dstAddress) const
Check match destination address function.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TlvValue * PeekValue(void)
Peek value.
Definition: wimax-tlv.cc:224
Iterator Begin() const
Begin iterator.
Definition: wimax-tlv.cc:1077
std::vector< struct ipv4Addr > m_srcAddr
source address
this class implements the classifier descriptor as a tlv vector
Definition: wimax-tlv.h:397
void AddDstPortRange(uint16_t dstPortLow, uint16_t dstPortHigh)
add a range of destination port to the classifier
PortRangeTlvValue class.
Definition: wimax-tlv.h:470
#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:90
Iterator End() const
End iterator.
Definition: wimax-tlv.cc:932
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void AddProtocol(uint8_t proto)
add a protocol to the classifier
void SetIndex(uint16_t index)
Set the index of the classifier.
void Add(Ipv4Address address, Ipv4Mask Mask)
Add IPv4 address and mask.
Definition: wimax-tlv.cc:1068
Tlv ToTlv(void) const
Creates a TLV from this classifier.
Iterator Begin() const
Begin iterator.
Definition: wimax-tlv.cc:926