EMANE  1.2.1
ipprotocoludprule.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - 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 
35 
36 #include "ipprotocoludprule.h"
37 #include "emane/net.h"
38 #include "emane/utils/netutils.h"
39 
40 
42  std::uint16_t u16DstPort) :
44  bCare_{false},
45  u16SrcPort_{u16SrcPort},
46  u16DstPort_{u16DstPort}
47 {
48  if((u16SrcPort_ == 0) && (u16DstPort_ == 0))
49  {
50  bCare_ = false;
51  }
52  else
53  {
54  bCare_ = true;
55  }
56 }
57 
59 
60 bool
62  std::size_t len,
63  std::uint16_t u16Type)
64 {
65  // check type
66  if(u16Type != u8Type_)
67  {
68  return 0;
69  }
70 
71  // check our attributes
72  if(bCare_ == false)
73  {
74  return true;
75  }
76 
77  // check buf len
78  if(len < Utils::UDP_HEADER_LEN)
79  {
80  return false;
81  }
82 
83  const auto pHdr =
84  static_cast<const Utils::UdpHeader *>(buf);
85 
86  if(u16SrcPort_ && (pHdr->u16Udpsrc != u16SrcPort_))
87  {
88  return false;
89  }
90 
91  if(u16DstPort_ && (pHdr->u16Udpdst != u16DstPort_))
92  {
93  return false;
94  }
95 
96  // match
97  return true;
98 }
const std::uint16_t UDP_HEADER_LEN
udp header len
Definition: netutils.h:75
const std::uint8_t IP_PROTO_UDP
ip udp protocol
Definition: netutils.h:68
Definition of the UDP header.
Definition: netutils.h:54
bool match(const void *buf, std::size_t len, std::uint16_t u16Type) override
IPProtocolUDPRule(std::uint16_t u16SrcPort, std::uint16_t u16DstPort)