PcapPlusPlus  24.09
PcapFilter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <memory>
6 #include "ProtocolType.h"
7 #include <stdint.h>
8 #include "ArpLayer.h"
9 #include "RawPacket.h"
10 
11 // Forward Declaration - used in GeneralFilter
12 struct bpf_program;
13 
38 namespace pcpp
39 {
40  // Forward Declaration - used in GeneralFilter
41  class RawPacket;
42 
46  typedef enum
47  {
49  SRC,
51  DST,
55 
59  typedef enum
60  {
74 
75  namespace internal
76  {
82  {
83  void operator()(bpf_program* ptr) const;
84  };
85  } // namespace internal
86 
92  {
93  private:
94  std::string m_FilterStr;
95  LinkLayerType m_LinkType;
96  std::unique_ptr<bpf_program, internal::BpfProgramDeleter> m_Program;
97 
98  void freeProgram();
99 
100  public:
105 
111 
118 
126  bool setFilter(const std::string& filter, LinkLayerType linkType = LINKTYPE_ETHERNET);
127 
136  bool matchPacketWithFilter(const RawPacket* rawPacket);
137 
149  bool matchPacketWithFilter(const uint8_t* packetData, uint32_t packetDataLength, timespec packetTimestamp,
150  uint16_t linkType);
151  };
152 
160  {
161  protected:
162  BpfFilterWrapper m_BpfWrapper;
163 
164  public:
170  virtual void parseToString(std::string& result) = 0;
171 
178 
179  GeneralFilter()
180  {}
181 
185  virtual ~GeneralFilter()
186  {}
187  };
188 
194  {
195  private:
196  const std::string m_FilterStr;
197 
198  public:
199  explicit BPFStringFilter(const std::string& filterStr) : m_FilterStr(filterStr)
200  {}
201 
202  virtual ~BPFStringFilter()
203  {}
204 
210  void parseToString(std::string& result) override;
211 
216  bool verifyFilter();
217  };
218 
227  {
228  private:
229  Direction m_Dir;
230 
231  protected:
232  void parseDirection(std::string& directionAsString);
233  Direction getDir() const
234  {
235  return m_Dir;
236  }
237  explicit IFilterWithDirection(Direction dir)
238  {
239  m_Dir = dir;
240  }
241 
242  public:
248  {
249  m_Dir = dir;
250  }
251  };
252 
261  {
262  private:
263  FilterOperator m_Operator;
264 
265  protected:
266  std::string parseOperator();
267  FilterOperator getOperator() const
268  {
269  return m_Operator;
270  }
272  {
273  m_Operator = op;
274  }
275 
276  public:
282  {
283  m_Operator = op;
284  }
285  };
286 
294  {
295  private:
296  IPAddress m_Address;
297  IPNetwork m_Network;
298 
299  public:
306  IPFilter(const std::string& ipAddress, Direction dir) : IPFilter(IPAddress(ipAddress), dir)
307  {}
308 
314  IPFilter(const IPAddress& ipAddress, Direction dir)
315  : IFilterWithDirection(dir), m_Address(ipAddress), m_Network(ipAddress)
316  {}
317 
330  IPFilter(const std::string& ipAddress, Direction dir, const std::string& netmask)
331  : IPFilter(IPv4Address(ipAddress), dir, netmask)
332  {}
333 
345  IPFilter(const IPAddress& ipAddress, Direction dir, const std::string& netmask)
346  : IFilterWithDirection(dir), m_Address(ipAddress), m_Network(ipAddress, netmask)
347  {}
348 
361  IPFilter(const std::string& ipAddress, Direction dir, int len) : IPFilter(IPAddress(ipAddress), dir, len)
362  {}
363 
375  IPFilter(const IPAddress& ipAddress, Direction dir, int len)
376  : IFilterWithDirection(dir), m_Address(ipAddress), m_Network(ipAddress, len)
377  {}
378 
385  IPFilter(const IPNetwork& network, Direction dir)
386  : IFilterWithDirection(dir), m_Address(network.getNetworkPrefix()), m_Network(network)
387  {}
388 
389  void parseToString(std::string& result) override;
390 
395  void setNetwork(const IPNetwork& network)
396  {
397  m_Network = network;
398  m_Address = m_Network.getNetworkPrefix();
399  }
400 
406  void setAddr(const std::string& ipAddress)
407  {
408  this->setAddr(IPAddress(ipAddress));
409  }
410 
418  void setAddr(const IPAddress& ipAddress)
419  {
420  m_Address = ipAddress;
421  uint8_t newPrefixLen = m_Network.getPrefixLen();
422  if (m_Address.isIPv4() && newPrefixLen > 32u)
423  {
424  newPrefixLen = 32u;
425  }
426 
427  m_Network = IPNetwork(m_Address, newPrefixLen);
428  }
429 
440  void setMask(const std::string& netmask)
441  {
442  m_Network = IPNetwork(m_Address, netmask);
443  }
444 
448  void clearMask()
449  {
450  this->clearLen();
451  }
452 
459  void setLen(const int len)
460  {
461  m_Network = IPNetwork(m_Address, len);
462  }
463 
467  void clearLen()
468  {
469  m_Network = IPNetwork(m_Address);
470  }
471  };
472 
481  {
482  private:
483  uint16_t m_IpID;
484 
485  public:
491  IPv4IDFilter(uint16_t ipID, FilterOperator op) : IFilterWithOperator(op), m_IpID(ipID)
492  {}
493 
494  void parseToString(std::string& result) override;
495 
500  void setIpID(uint16_t ipID)
501  {
502  m_IpID = ipID;
503  }
504  };
505 
514  {
515  private:
516  uint16_t m_TotalLength;
517 
518  public:
524  IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op)
525  : IFilterWithOperator(op), m_TotalLength(totalLength)
526  {}
527 
528  void parseToString(std::string& result) override;
529 
534  void setTotalLength(uint16_t totalLength)
535  {
536  m_TotalLength = totalLength;
537  }
538  };
539 
547  {
548  private:
549  std::string m_Port;
550  void portToString(uint16_t portAsInt);
551 
552  public:
558  PortFilter(uint16_t port, Direction dir);
559 
560  void parseToString(std::string& result) override;
561 
566  void setPort(uint16_t port)
567  {
568  portToString(port);
569  }
570  };
571 
581  {
582  private:
583  uint16_t m_FromPort;
584  uint16_t m_ToPort;
585 
586  public:
593  PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir)
594  : IFilterWithDirection(dir), m_FromPort(fromPort), m_ToPort(toPort)
595  {}
596 
597  void parseToString(std::string& result) override;
598 
603  void setFromPort(uint16_t fromPort)
604  {
605  m_FromPort = fromPort;
606  }
607 
612  void setToPort(uint16_t toPort)
613  {
614  m_ToPort = toPort;
615  }
616  };
617 
626  {
627  private:
628  MacAddress m_MacAddress;
629 
630  public:
636  MacAddressFilter(MacAddress address, Direction dir) : IFilterWithDirection(dir), m_MacAddress(address)
637  {}
638 
639  void parseToString(std::string& result) override;
640 
645  void setMacAddress(MacAddress address)
646  {
647  m_MacAddress = address;
648  }
649  };
650 
659  {
660  private:
661  uint16_t m_EtherType;
662 
663  public:
668  explicit EtherTypeFilter(uint16_t etherType) : m_EtherType(etherType)
669  {}
670 
671  void parseToString(std::string& result) override;
672 
677  void setEtherType(uint16_t etherType)
678  {
679  m_EtherType = etherType;
680  }
681  };
682 
691  {
692  protected:
693  std::vector<GeneralFilter*> m_FilterList;
694 
695  public:
699  CompositeFilter() = default;
700 
705  explicit CompositeFilter(const std::vector<GeneralFilter*>& filters);
706 
711  void addFilter(GeneralFilter* filter)
712  {
713  m_FilterList.push_back(filter);
714  }
715 
721 
726  void setFilters(const std::vector<GeneralFilter*>& filters);
727 
732  {
733  m_FilterList.clear();
734  }
735  };
736 
741  {
743  AND,
745  OR,
746  };
747 
748  namespace internal
749  {
750  /* Could potentially be moved into CompositeLogicFilter as a private member function, with if constexpr when
751  * C++17 is the minimum supported standard.*/
756  template <CompositeLogicFilterOp op> constexpr const char* getCompositeLogicOpDelimiter() = delete;
757  template <> constexpr const char* getCompositeLogicOpDelimiter<CompositeLogicFilterOp::AND>()
758  {
759  return " and ";
760  };
761  template <> constexpr const char* getCompositeLogicOpDelimiter<CompositeLogicFilterOp::OR>()
762  {
763  return " or ";
764  };
765  } // namespace internal
766 
773  template <CompositeLogicFilterOp op> class CompositeLogicFilter : public CompositeFilter
774  {
775  public:
777 
778  void parseToString(std::string& result) override
779  {
780  result.clear();
781  for (auto it = m_FilterList.cbegin(); it != m_FilterList.cend(); ++it)
782  {
783  std::string innerFilter;
784  (*it)->parseToString(innerFilter);
785  result += '(' + innerFilter + ')';
786  if (m_FilterList.cend() - 1 != it)
787  {
788  result += internal::getCompositeLogicOpDelimiter<op>();
789  }
790  }
791  }
792  };
793 
804 
815 
822  class NotFilter : public GeneralFilter
823  {
824  private:
825  GeneralFilter* m_FilterToInverse;
826 
827  public:
832  explicit NotFilter(GeneralFilter* filterToInverse)
833  {
834  m_FilterToInverse = filterToInverse;
835  }
836 
837  void parseToString(std::string& result) override;
838 
843  void setFilter(GeneralFilter* filterToInverse)
844  {
845  m_FilterToInverse = filterToInverse;
846  }
847  };
848 
860  class ProtoFilter : public GeneralFilter
861  {
862  private:
863  ProtocolTypeFamily m_ProtoFamily;
864 
865  public:
871  explicit ProtoFilter(ProtocolType proto) : m_ProtoFamily(proto)
872  {}
873 
879  explicit ProtoFilter(ProtocolTypeFamily protoFamily) : m_ProtoFamily(protoFamily)
880  {}
881 
882  void parseToString(std::string& result) override;
883 
889  void setProto(ProtocolType proto)
890  {
891  m_ProtoFamily = proto;
892  }
893 
900  void setProto(ProtocolTypeFamily protoFamily)
901  {
902  m_ProtoFamily = protoFamily;
903  }
904  };
905 
913  class ArpFilter : public GeneralFilter
914  {
915  private:
916  ArpOpcode m_OpCode;
917 
918  public:
923  explicit ArpFilter(ArpOpcode opCode) : m_OpCode(opCode)
924  {}
925 
926  void parseToString(std::string& result) override;
927 
932  void setOpCode(ArpOpcode opCode)
933  {
934  m_OpCode = opCode;
935  }
936  };
937 
945  class VlanFilter : public GeneralFilter
946  {
947  private:
948  uint16_t m_VlanID;
949 
950  public:
955  explicit VlanFilter(uint16_t vlanId) : m_VlanID(vlanId)
956  {}
957 
958  void parseToString(std::string& result) override;
959 
964  void setVlanID(uint16_t vlanId)
965  {
966  m_VlanID = vlanId;
967  }
968  };
969 
977  {
978  public:
982  enum TcpFlags
983  {
985  tcpFin = 1,
987  tcpSyn = 2,
989  tcpRst = 4,
991  tcpPush = 8,
993  tcpAck = 16,
995  tcpUrg = 32
996  };
997 
1003  {
1008  };
1009 
1010  private:
1011  uint8_t m_TcpFlagsBitMask;
1012  MatchOptions m_MatchOption;
1013 
1014  public:
1023  TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption)
1024  : m_TcpFlagsBitMask(tcpFlagBitMask), m_MatchOption(matchOption)
1025  {}
1026 
1034  void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption)
1035  {
1036  m_TcpFlagsBitMask = tcpFlagBitMask;
1037  m_MatchOption = matchOption;
1038  }
1039 
1040  void parseToString(std::string& result) override;
1041  };
1042 
1050  {
1051  private:
1052  uint16_t m_WindowSize;
1053 
1054  public:
1061  TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op) : IFilterWithOperator(op), m_WindowSize(windowSize)
1062  {}
1063 
1064  void parseToString(std::string& result) override;
1065 
1070  void setWindowSize(uint16_t windowSize)
1071  {
1072  m_WindowSize = windowSize;
1073  }
1074  };
1075 
1083  {
1084  private:
1085  uint16_t m_Length;
1086 
1087  public:
1094  UdpLengthFilter(uint16_t length, FilterOperator op) : IFilterWithOperator(op), m_Length(length)
1095  {}
1096 
1097  void parseToString(std::string& result) override;
1098 
1103  void setLength(uint16_t length)
1104  {
1105  m_Length = length;
1106  }
1107  };
1108 
1109 } // namespace pcpp
constexpr const char * getCompositeLogicOpDelimiter()=delete
Definition: PcapFilter.h:914
ArpFilter(ArpOpcode opCode)
Definition: PcapFilter.h:923
void setOpCode(ArpOpcode opCode)
Definition: PcapFilter.h:932
void parseToString(std::string &result) override
Definition: PcapFilter.h:194
void parseToString(std::string &result) override
Definition: PcapFilter.h:92
bool matchPacketWithFilter(const RawPacket *rawPacket)
BpfFilterWrapper(const BpfFilterWrapper &other)
BpfFilterWrapper & operator=(const BpfFilterWrapper &other)
bool matchPacketWithFilter(const uint8_t *packetData, uint32_t packetDataLength, timespec packetTimestamp, uint16_t linkType)
bool setFilter(const std::string &filter, LinkLayerType linkType=LINKTYPE_ETHERNET)
Definition: PcapFilter.h:691
void removeFilter(GeneralFilter *filter)
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:711
CompositeFilter(const std::vector< GeneralFilter * > &filters)
void setFilters(const std::vector< GeneralFilter * > &filters)
void clearAllFilters()
Definition: PcapFilter.h:731
Definition: PcapFilter.h:774
void parseToString(std::string &result) override
Definition: PcapFilter.h:778
Definition: PcapFilter.h:659
EtherTypeFilter(uint16_t etherType)
Definition: PcapFilter.h:668
void setEtherType(uint16_t etherType)
Definition: PcapFilter.h:677
void parseToString(std::string &result) override
Definition: PcapFilter.h:160
bool matchPacketWithFilter(RawPacket *rawPacket)
virtual ~GeneralFilter()
Definition: PcapFilter.h:185
virtual void parseToString(std::string &result)=0
Definition: PcapFilter.h:227
void setDirection(Direction dir)
Definition: PcapFilter.h:247
Definition: PcapFilter.h:261
void setOperator(FilterOperator op)
Definition: PcapFilter.h:281
Definition: IpAddress.h:358
bool isIPv4() const
Definition: IpAddress.h:438
Definition: PcapFilter.h:294
void setMask(const std::string &netmask)
Definition: PcapFilter.h:440
void clearMask()
Definition: PcapFilter.h:448
void setAddr(const std::string &ipAddress)
Definition: PcapFilter.h:406
void setLen(const int len)
Definition: PcapFilter.h:459
IPFilter(const IPAddress &ipAddress, Direction dir, int len)
Definition: PcapFilter.h:375
IPFilter(const std::string &ipAddress, Direction dir, const std::string &netmask)
Definition: PcapFilter.h:330
IPFilter(const IPNetwork &network, Direction dir)
Definition: PcapFilter.h:385
void setNetwork(const IPNetwork &network)
Definition: PcapFilter.h:395
IPFilter(const std::string &ipAddress, Direction dir, int len)
Definition: PcapFilter.h:361
void clearLen()
Definition: PcapFilter.h:467
void parseToString(std::string &result) override
IPFilter(const std::string &ipAddress, Direction dir)
Definition: PcapFilter.h:306
IPFilter(const IPAddress &ipAddress, Direction dir)
Definition: PcapFilter.h:314
IPFilter(const IPAddress &ipAddress, Direction dir, const std::string &netmask)
Definition: PcapFilter.h:345
void setAddr(const IPAddress &ipAddress)
Definition: PcapFilter.h:418
Definition: IpAddress.h:790
IPAddress getNetworkPrefix() const
Definition: IpAddress.h:967
uint8_t getPrefixLen() const
Definition: IpAddress.h:949
Definition: IpAddress.h:32
Definition: PcapFilter.h:481
void parseToString(std::string &result) override
IPv4IDFilter(uint16_t ipID, FilterOperator op)
Definition: PcapFilter.h:491
void setIpID(uint16_t ipID)
Definition: PcapFilter.h:500
Definition: PcapFilter.h:514
void parseToString(std::string &result) override
void setTotalLength(uint16_t totalLength)
Definition: PcapFilter.h:534
IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op)
Definition: PcapFilter.h:524
Definition: PcapFilter.h:626
void parseToString(std::string &result) override
void setMacAddress(MacAddress address)
Definition: PcapFilter.h:645
MacAddressFilter(MacAddress address, Direction dir)
Definition: PcapFilter.h:636
Definition: MacAddress.h:25
Definition: PcapFilter.h:823
void setFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:843
void parseToString(std::string &result) override
NotFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:832
Definition: PcapFilter.h:547
PortFilter(uint16_t port, Direction dir)
void parseToString(std::string &result) override
void setPort(uint16_t port)
Definition: PcapFilter.h:566
Definition: PcapFilter.h:581
PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir)
Definition: PcapFilter.h:593
void setToPort(uint16_t toPort)
Definition: PcapFilter.h:612
void parseToString(std::string &result) override
void setFromPort(uint16_t fromPort)
Definition: PcapFilter.h:603
Definition: PcapFilter.h:861
void setProto(ProtocolType proto)
Definition: PcapFilter.h:889
ProtoFilter(ProtocolTypeFamily protoFamily)
Definition: PcapFilter.h:879
void setProto(ProtocolTypeFamily protoFamily)
Definition: PcapFilter.h:900
ProtoFilter(ProtocolType proto)
Definition: PcapFilter.h:871
void parseToString(std::string &result) override
Definition: RawPacket.h:269
Definition: PcapFilter.h:977
TcpFlags
Definition: PcapFilter.h:983
@ tcpSyn
Definition: PcapFilter.h:987
@ tcpFin
Definition: PcapFilter.h:985
@ tcpUrg
Definition: PcapFilter.h:995
@ tcpAck
Definition: PcapFilter.h:993
@ tcpRst
Definition: PcapFilter.h:989
@ tcpPush
Definition: PcapFilter.h:991
void parseToString(std::string &result) override
MatchOptions
Definition: PcapFilter.h:1003
@ MatchAll
Definition: PcapFilter.h:1005
@ MatchOneAtLeast
Definition: PcapFilter.h:1007
void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:1034
TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:1023
Definition: PcapFilter.h:1050
void setWindowSize(uint16_t windowSize)
Definition: PcapFilter.h:1070
void parseToString(std::string &result) override
TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op)
Definition: PcapFilter.h:1061
Definition: PcapFilter.h:1083
void parseToString(std::string &result) override
void setLength(uint16_t length)
Definition: PcapFilter.h:1103
UdpLengthFilter(uint16_t length, FilterOperator op)
Definition: PcapFilter.h:1094
Definition: PcapFilter.h:946
VlanFilter(uint16_t vlanId)
Definition: PcapFilter.h:955
void parseToString(std::string &result) override
void setVlanID(uint16_t vlanId)
Definition: PcapFilter.h:964
The main namespace for the PcapPlusPlus lib.
ArpOpcode
Definition: ArpLayer.h:50
uint8_t ProtocolType
Definition: ProtocolType.h:17
FilterOperator
Definition: PcapFilter.h:60
@ GREATER_THAN
Definition: PcapFilter.h:66
@ EQUALS
Definition: PcapFilter.h:62
@ GREATER_OR_EQUAL
Definition: PcapFilter.h:68
@ LESS_THAN
Definition: PcapFilter.h:70
@ NOT_EQUALS
Definition: PcapFilter.h:64
@ LESS_OR_EQUAL
Definition: PcapFilter.h:72
Direction
Definition: PcapFilter.h:47
@ SRC_OR_DST
Definition: PcapFilter.h:53
@ DST
Definition: PcapFilter.h:51
@ SRC
Definition: PcapFilter.h:49
CompositeLogicFilterOp
Definition: PcapFilter.h:741
uint32_t ProtocolTypeFamily
Definition: ProtocolType.h:23
LinkLayerType
Definition: RawPacket.h:25
@ LINKTYPE_ETHERNET
Definition: RawPacket.h:29
Definition: PcapFilter.h:82