EMANE  1.2.1
ethernetprotocolipv4rule.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015 - Adjacent Link LLC, Bridgewater, New Jersey
3  * Copyright (c) 2009 - DRS CenGen, LLC, Columbia, Maryland
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of DRS CenGen, LLC nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
36 #include "emane/net.h"
37 #include "emane/utils/netutils.h"
38 
39 #include <functional>
40 
41 
43  std::uint32_t u32Dst,
44  std::uint16_t u16Len,
45  std::uint8_t u8TOS,
46  std::uint8_t u8TTL,
47  const IPProtocolRules & rules) :
49  bCare_{false},
50  u32Src_{u32Src},
51  u32Dst_{u32Dst},
52  u16Len_{u16Len},
53  u8TOS_{u8TOS},
54  u8TTL_{u8TTL}
55 {
56  if((u32Src == 0) && (u32Dst == 0) && (u16Len == 0) && (u8TOS == 0) && (u8TTL == 0) && (rules.empty()))
57  {
58  bCare_ = false;
59  }
60  else
61  {
62  bCare_ = true;
63  }
64 }
65 
66 
68 {}
69 
70 
72  std::size_t len,
73  std::uint16_t u16Type)
74 {
75  // check type
76  if(u16Type != u16Type_)
77  {
78  return false;
79  }
80 
81  // check our attributes
82  if(bCare_ == false)
83  {
84  return true;
85  }
86 
87  // check buf len
88  if(len < Utils::IPV4_HEADER_LEN)
89  {
90  return false;
91  }
92 
93  const Utils::Ip4Header * pHdr = (Utils::Ip4Header *) buf;
94 
95  if(u32Src_ && (pHdr->u32Ipv4src != u32Src_))
96  {
97  return false;
98  }
99  if(u32Dst_ && (pHdr->u32Ipv4dst != u32Dst_))
100  {
101  return false;
102  }
103  if(u16Len_ && (pHdr->u16Ipv4len != u16Len_))
104  {
105  return false;
106  }
107  if(u8TOS_ && (pHdr->u8Ipv4tos != u8TOS_))
108  {
109  return false;
110  }
111  if(u8TTL_ && (pHdr->u8Ipv4hops != u8TTL_))
112  {
113  return false;
114  }
115 
116  if(rules_.empty())
117  {
118  // no rules, don't care
119  return 1;
120  }
121 
122  const int offset = ((pHdr->u8Ipv4vhl & 0x0F) << 2);
123 
124  return std::find_if(rules_.begin(),
125  rules_.end(),
126  std::bind(&Rule::match,
127  std::placeholders::_1,
128  static_cast<const std::uint8_t *>(buf) + offset,
129  len - offset, pHdr->u8Ipv4proto)) != rules_.end();
130 }
Definition of the IPv4 header.
Definition: netutils.h:139
std::uint16_t u16Ipv4len
Definition: netutils.h:143
std::list< IPProtocolRule * > IPProtocolRules
std::uint8_t u8Ipv4tos
Definition: netutils.h:142
std::uint32_t u32Ipv4dst
Definition: netutils.h:150
virtual bool match(const void *buf, std::size_t len, std::uint16_t u16Type)=0
std::uint32_t u32Ipv4src
Definition: netutils.h:149
const std::uint16_t IPV4_HEADER_LEN
ipv4 header len without options
Definition: netutils.h:158
constexpr std::uint16_t HTONS(std::uint16_t x)
Definition: net.h:125
EthernetProtocolIPv4Rule(std::uint32_t u32Src, std::uint32_t u32Dst, std::uint16_t u16Len, std::uint8_t u8TOS, std::uint8_t u8TTL, const IPProtocolRules &rules)
std::uint8_t u8Ipv4proto
Definition: netutils.h:147
bool match(const void *buf, std::size_t len, std::uint16_t u16Type) override
const std::uint16_t ETH_P_IPV4
Ethernet ipv4 protocol.
Definition: netutils.h:374
std::uint8_t u8Ipv4vhl
Definition: netutils.h:141
std::uint8_t u8Ipv4hops
Definition: netutils.h:146