PcapPlusPlus  Next
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 
31 
34 namespace pcpp
35 {
36  // Forward Declaration - used in GeneralFilter
37  class RawPacket;
38 
40  typedef enum
41  {
43  SRC,
45  DST,
49 
51  typedef enum
52  {
66 
67  namespace internal
68  {
72  {
73  void operator()(bpf_program* ptr) const;
74  };
75  } // namespace internal
76 
80  {
81  private:
82  std::string m_FilterStr;
83  LinkLayerType m_LinkType;
84  std::unique_ptr<bpf_program, internal::BpfProgramDeleter> m_Program;
85 
86  void freeProgram();
87 
88  public:
91 
95 
100 
106  bool setFilter(const std::string& filter, LinkLayerType linkType = LINKTYPE_ETHERNET);
107 
114  bool matchPacketWithFilter(const RawPacket* rawPacket);
115 
125  bool matchPacketWithFilter(const uint8_t* packetData, uint32_t packetDataLength, timespec packetTimestamp,
126  uint16_t linkType);
127  };
128 
134  {
135  protected:
136  BpfFilterWrapper m_BpfWrapper;
137 
138  public:
142  virtual void parseToString(std::string& result) = 0;
143 
148 
149  GeneralFilter()
150  {}
151 
153  virtual ~GeneralFilter() = default;
154  };
155 
159  {
160  private:
161  const std::string m_FilterStr;
162 
163  public:
164  explicit BPFStringFilter(const std::string& filterStr) : m_FilterStr(filterStr)
165  {}
166 
167  virtual ~BPFStringFilter()
168  {}
169 
173  void parseToString(std::string& result) override;
174 
177  bool verifyFilter();
178  };
179 
186  {
187  private:
188  Direction m_Dir;
189 
190  protected:
191  void parseDirection(std::string& directionAsString);
192  Direction getDir() const
193  {
194  return m_Dir;
195  }
196  explicit IFilterWithDirection(Direction dir)
197  {
198  m_Dir = dir;
199  }
200 
201  public:
205  {
206  m_Dir = dir;
207  }
208  };
209 
216  {
217  private:
218  FilterOperator m_Operator;
219 
220  protected:
221  std::string parseOperator();
222  FilterOperator getOperator() const
223  {
224  return m_Operator;
225  }
227  {
228  m_Operator = op;
229  }
230 
231  public:
235  {
236  m_Operator = op;
237  }
238  };
239 
245  {
246  private:
247  IPAddress m_Address;
248  IPNetwork m_Network;
249 
250  public:
256  IPFilter(const std::string& ipAddress, Direction dir) : IPFilter(IPAddress(ipAddress), dir)
257  {}
258 
262  IPFilter(const IPAddress& ipAddress, Direction dir)
263  : IFilterWithDirection(dir), m_Address(ipAddress), m_Network(ipAddress)
264  {}
265 
276  IPFilter(const std::string& ipAddress, Direction dir, const std::string& netmask)
277  : IPFilter(IPv4Address(ipAddress), dir, netmask)
278  {}
279 
289  IPFilter(const IPAddress& ipAddress, Direction dir, const std::string& netmask)
290  : IFilterWithDirection(dir), m_Address(ipAddress), m_Network(ipAddress, netmask)
291  {}
292 
303  IPFilter(const std::string& ipAddress, Direction dir, int len) : IPFilter(IPAddress(ipAddress), dir, len)
304  {}
305 
315  IPFilter(const IPAddress& ipAddress, Direction dir, int len)
316  : IFilterWithDirection(dir), m_Address(ipAddress), m_Network(ipAddress, len)
317  {}
318 
323  IPFilter(const IPNetwork& network, Direction dir)
324  : IFilterWithDirection(dir), m_Address(network.getNetworkPrefix()), m_Network(network)
325  {}
326 
327  void parseToString(std::string& result) override;
328 
331  void setNetwork(const IPNetwork& network)
332  {
333  m_Network = network;
334  m_Address = m_Network.getNetworkPrefix();
335  }
336 
340  void setAddr(const std::string& ipAddress)
341  {
342  this->setAddr(IPAddress(ipAddress));
343  }
344 
350  void setAddr(const IPAddress& ipAddress)
351  {
352  m_Address = ipAddress;
353  uint8_t newPrefixLen = m_Network.getPrefixLen();
354  if (m_Address.isIPv4() && newPrefixLen > 32u)
355  {
356  newPrefixLen = 32u;
357  }
358 
359  m_Network = IPNetwork(m_Address, newPrefixLen);
360  }
361 
370  void setMask(const std::string& netmask)
371  {
372  m_Network = IPNetwork(m_Address, netmask);
373  }
374 
376  void clearMask()
377  {
378  this->clearLen();
379  }
380 
385  void setLen(const int len)
386  {
387  m_Network = IPNetwork(m_Address, len);
388  }
389 
391  void clearLen()
392  {
393  m_Network = IPNetwork(m_Address);
394  }
395  };
396 
403  {
404  private:
405  uint16_t m_IpID;
406 
407  public:
411  IPv4IDFilter(uint16_t ipID, FilterOperator op) : IFilterWithOperator(op), m_IpID(ipID)
412  {}
413 
414  void parseToString(std::string& result) override;
415 
418  void setIpID(uint16_t ipID)
419  {
420  m_IpID = ipID;
421  }
422  };
423 
430  {
431  private:
432  uint16_t m_TotalLength;
433 
434  public:
438  IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op)
439  : IFilterWithOperator(op), m_TotalLength(totalLength)
440  {}
441 
442  void parseToString(std::string& result) override;
443 
446  void setTotalLength(uint16_t totalLength)
447  {
448  m_TotalLength = totalLength;
449  }
450  };
451 
457  {
458  private:
459  std::string m_Port;
460  void portToString(uint16_t portAsInt);
461 
462  public:
466  PortFilter(uint16_t port, Direction dir);
467 
468  void parseToString(std::string& result) override;
469 
472  void setPort(uint16_t port)
473  {
474  portToString(port);
475  }
476  };
477 
485  {
486  private:
487  uint16_t m_FromPort;
488  uint16_t m_ToPort;
489 
490  public:
495  PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir)
496  : IFilterWithDirection(dir), m_FromPort(fromPort), m_ToPort(toPort)
497  {}
498 
499  void parseToString(std::string& result) override;
500 
503  void setFromPort(uint16_t fromPort)
504  {
505  m_FromPort = fromPort;
506  }
507 
510  void setToPort(uint16_t toPort)
511  {
512  m_ToPort = toPort;
513  }
514  };
515 
522  {
523  private:
524  MacAddress m_MacAddress;
525 
526  public:
530  MacAddressFilter(MacAddress address, Direction dir) : IFilterWithDirection(dir), m_MacAddress(address)
531  {}
532 
533  void parseToString(std::string& result) override;
534 
537  void setMacAddress(MacAddress address)
538  {
539  m_MacAddress = address;
540  }
541  };
542 
549  {
550  private:
551  uint16_t m_EtherType;
552 
553  public:
556  explicit EtherTypeFilter(uint16_t etherType) : m_EtherType(etherType)
557  {}
558 
559  void parseToString(std::string& result) override;
560 
563  void setEtherType(uint16_t etherType)
564  {
565  m_EtherType = etherType;
566  }
567  };
568 
575  {
576  protected:
577  std::vector<GeneralFilter*> m_FilterList;
578 
579  public:
581  CompositeFilter() = default;
582 
585  explicit CompositeFilter(const std::vector<GeneralFilter*>& filters);
586 
589  void addFilter(GeneralFilter* filter)
590  {
591  m_FilterList.push_back(filter);
592  }
593 
597 
600  void setFilters(const std::vector<GeneralFilter*>& filters);
601 
604  {
605  m_FilterList.clear();
606  }
607  };
608 
611  {
613  AND,
615  OR,
616  };
617 
618  namespace internal
619  {
620  // Could potentially be moved into CompositeLogicFilter as a private member function, with if constexpr when
621  // C++17 is the minimum supported standard.
622 
625  template <CompositeLogicFilterOp op> constexpr const char* getCompositeLogicOpDelimiter() = delete;
626  template <> constexpr const char* getCompositeLogicOpDelimiter<CompositeLogicFilterOp::AND>()
627  {
628  return " and ";
629  };
630  template <> constexpr const char* getCompositeLogicOpDelimiter<CompositeLogicFilterOp::OR>()
631  {
632  return " or ";
633  };
634  } // namespace internal
635 
640  template <CompositeLogicFilterOp op> class CompositeLogicFilter : public CompositeFilter
641  {
642  public:
644 
645  void parseToString(std::string& result) override
646  {
647  result.clear();
648  for (auto it = m_FilterList.cbegin(); it != m_FilterList.cend(); ++it)
649  {
650  std::string innerFilter;
651  (*it)->parseToString(innerFilter);
652  result += '(' + innerFilter + ')';
653  if (m_FilterList.cend() - 1 != it)
654  {
655  result += internal::getCompositeLogicOpDelimiter<op>();
656  }
657  }
658  }
659  };
660 
669 
678 
683  class NotFilter : public GeneralFilter
684  {
685  private:
686  GeneralFilter* m_FilterToInverse;
687 
688  public:
691  explicit NotFilter(GeneralFilter* filterToInverse)
692  {
693  m_FilterToInverse = filterToInverse;
694  }
695 
696  void parseToString(std::string& result) override;
697 
700  void setFilter(GeneralFilter* filterToInverse)
701  {
702  m_FilterToInverse = filterToInverse;
703  }
704  };
705 
715  class ProtoFilter : public GeneralFilter
716  {
717  private:
718  ProtocolTypeFamily m_ProtoFamily;
719 
720  public:
724  explicit ProtoFilter(ProtocolType proto) : m_ProtoFamily(proto)
725  {}
726 
730  explicit ProtoFilter(ProtocolTypeFamily protoFamily) : m_ProtoFamily(protoFamily)
731  {}
732 
733  void parseToString(std::string& result) override;
734 
738  void setProto(ProtocolType proto)
739  {
740  m_ProtoFamily = proto;
741  }
742 
747  void setProto(ProtocolTypeFamily protoFamily)
748  {
749  m_ProtoFamily = protoFamily;
750  }
751  };
752 
758  class ArpFilter : public GeneralFilter
759  {
760  private:
761  ArpOpcode m_OpCode;
762 
763  public:
766  explicit ArpFilter(ArpOpcode opCode) : m_OpCode(opCode)
767  {}
768 
769  void parseToString(std::string& result) override;
770 
773  void setOpCode(ArpOpcode opCode)
774  {
775  m_OpCode = opCode;
776  }
777  };
778 
784  class VlanFilter : public GeneralFilter
785  {
786  private:
787  uint16_t m_VlanID;
788 
789  public:
792  explicit VlanFilter(uint16_t vlanId) : m_VlanID(vlanId)
793  {}
794 
795  void parseToString(std::string& result) override;
796 
799  void setVlanID(uint16_t vlanId)
800  {
801  m_VlanID = vlanId;
802  }
803  };
804 
810  {
811  public:
813  enum TcpFlags
814  {
816  tcpFin = 1,
818  tcpSyn = 2,
820  tcpRst = 4,
822  tcpPush = 8,
824  tcpAck = 16,
826  tcpUrg = 32
827  };
828 
832  {
837  };
838 
839  private:
840  uint8_t m_TcpFlagsBitMask;
841  MatchOptions m_MatchOption;
842 
843  public:
850  TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption)
851  : m_TcpFlagsBitMask(tcpFlagBitMask), m_MatchOption(matchOption)
852  {}
853 
859  void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption)
860  {
861  m_TcpFlagsBitMask = tcpFlagBitMask;
862  m_MatchOption = matchOption;
863  }
864 
865  void parseToString(std::string& result) override;
866  };
867 
873  {
874  private:
875  uint16_t m_WindowSize;
876 
877  public:
882  TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op) : IFilterWithOperator(op), m_WindowSize(windowSize)
883  {}
884 
885  void parseToString(std::string& result) override;
886 
889  void setWindowSize(uint16_t windowSize)
890  {
891  m_WindowSize = windowSize;
892  }
893  };
894 
900  {
901  private:
902  uint16_t m_Length;
903 
904  public:
909  UdpLengthFilter(uint16_t length, FilterOperator op) : IFilterWithOperator(op), m_Length(length)
910  {}
911 
912  void parseToString(std::string& result) override;
913 
916  void setLength(uint16_t length)
917  {
918  m_Length = length;
919  }
920  };
921 } // namespace pcpp
constexpr const char * getCompositeLogicOpDelimiter()=delete
Definition: PcapFilter.h:759
ArpFilter(ArpOpcode opCode)
Definition: PcapFilter.h:766
void setOpCode(ArpOpcode opCode)
Definition: PcapFilter.h:773
void parseToString(std::string &result) override
Definition: PcapFilter.h:159
void parseToString(std::string &result) override
Definition: PcapFilter.h:80
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)
BpfFilterWrapper()
A c'tor for this class.
bool setFilter(const std::string &filter, LinkLayerType linkType=LINKTYPE_ETHERNET)
Definition: PcapFilter.h:575
CompositeFilter()=default
An empty constructor for this class. Use addFilter() to add filters to the composite filter.
void removeFilter(GeneralFilter *filter)
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:589
CompositeFilter(const std::vector< GeneralFilter * > &filters)
void setFilters(const std::vector< GeneralFilter * > &filters)
void clearAllFilters()
Remove all filters from the composite filter.
Definition: PcapFilter.h:603
Definition: PcapFilter.h:641
void parseToString(std::string &result) override
Definition: PcapFilter.h:645
Definition: PcapFilter.h:549
EtherTypeFilter(uint16_t etherType)
Definition: PcapFilter.h:556
void setEtherType(uint16_t etherType)
Definition: PcapFilter.h:563
void parseToString(std::string &result) override
Definition: PcapFilter.h:134
bool matchPacketWithFilter(RawPacket *rawPacket)
virtual ~GeneralFilter()=default
Virtual destructor, frees the bpf program.
virtual void parseToString(std::string &result)=0
Definition: PcapFilter.h:186
void setDirection(Direction dir)
Definition: PcapFilter.h:204
Definition: PcapFilter.h:216
void setOperator(FilterOperator op)
Definition: PcapFilter.h:234
Definition: IpAddress.h:276
bool isIPv4() const
Definition: IpAddress.h:331
Definition: PcapFilter.h:245
void setMask(const std::string &netmask)
Definition: PcapFilter.h:370
void clearMask()
Clears the subnet mask.
Definition: PcapFilter.h:376
void setAddr(const std::string &ipAddress)
Definition: PcapFilter.h:340
void setLen(const int len)
Definition: PcapFilter.h:385
IPFilter(const IPAddress &ipAddress, Direction dir, int len)
Definition: PcapFilter.h:315
IPFilter(const std::string &ipAddress, Direction dir, const std::string &netmask)
Definition: PcapFilter.h:276
IPFilter(const IPNetwork &network, Direction dir)
Definition: PcapFilter.h:323
void setNetwork(const IPNetwork &network)
Definition: PcapFilter.h:331
IPFilter(const std::string &ipAddress, Direction dir, int len)
Definition: PcapFilter.h:303
void clearLen()
Clears the subnet mask length.
Definition: PcapFilter.h:391
void parseToString(std::string &result) override
IPFilter(const std::string &ipAddress, Direction dir)
Definition: PcapFilter.h:256
IPFilter(const IPAddress &ipAddress, Direction dir)
Definition: PcapFilter.h:262
IPFilter(const IPAddress &ipAddress, Direction dir, const std::string &netmask)
Definition: PcapFilter.h:289
void setAddr(const IPAddress &ipAddress)
Definition: PcapFilter.h:350
Definition: IpAddress.h:607
IPAddress getNetworkPrefix() const
Definition: IpAddress.h:743
uint8_t getPrefixLen() const
Definition: IpAddress.h:729
Definition: IpAddress.h:28
Definition: PcapFilter.h:403
void parseToString(std::string &result) override
IPv4IDFilter(uint16_t ipID, FilterOperator op)
Definition: PcapFilter.h:411
void setIpID(uint16_t ipID)
Definition: PcapFilter.h:418
Definition: PcapFilter.h:430
void parseToString(std::string &result) override
void setTotalLength(uint16_t totalLength)
Definition: PcapFilter.h:446
IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op)
Definition: PcapFilter.h:438
Definition: PcapFilter.h:522
void parseToString(std::string &result) override
void setMacAddress(MacAddress address)
Definition: PcapFilter.h:537
MacAddressFilter(MacAddress address, Direction dir)
Definition: PcapFilter.h:530
Definition: MacAddress.h:21
Definition: PcapFilter.h:684
void setFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:700
void parseToString(std::string &result) override
NotFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:691
Definition: PcapFilter.h:457
PortFilter(uint16_t port, Direction dir)
void parseToString(std::string &result) override
void setPort(uint16_t port)
Definition: PcapFilter.h:472
Definition: PcapFilter.h:485
PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir)
Definition: PcapFilter.h:495
void setToPort(uint16_t toPort)
Definition: PcapFilter.h:510
void parseToString(std::string &result) override
void setFromPort(uint16_t fromPort)
Definition: PcapFilter.h:503
Definition: PcapFilter.h:716
void setProto(ProtocolType proto)
Definition: PcapFilter.h:738
ProtoFilter(ProtocolTypeFamily protoFamily)
Definition: PcapFilter.h:730
void setProto(ProtocolTypeFamily protoFamily)
Definition: PcapFilter.h:747
ProtoFilter(ProtocolType proto)
Definition: PcapFilter.h:724
void parseToString(std::string &result) override
Definition: RawPacket.h:269
Definition: PcapFilter.h:810
TcpFlags
An enum of all TCP flags that can be use in the filter.
Definition: PcapFilter.h:814
@ tcpSyn
TCP SYN flag.
Definition: PcapFilter.h:818
@ tcpFin
TCP FIN flag.
Definition: PcapFilter.h:816
@ tcpUrg
TCP URG flag.
Definition: PcapFilter.h:826
@ tcpAck
TCP ACK flag.
Definition: PcapFilter.h:824
@ tcpRst
TCP RST flag.
Definition: PcapFilter.h:820
@ tcpPush
TCP PSH flag.
Definition: PcapFilter.h:822
void parseToString(std::string &result) override
MatchOptions
Definition: PcapFilter.h:832
@ MatchAll
Match only packets that contain all flags defined in the filter.
Definition: PcapFilter.h:834
@ MatchOneAtLeast
Match packets that contain at least one of the flags defined in the filter.
Definition: PcapFilter.h:836
void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:859
TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:850
Definition: PcapFilter.h:873
void setWindowSize(uint16_t windowSize)
Definition: PcapFilter.h:889
void parseToString(std::string &result) override
TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op)
Definition: PcapFilter.h:882
Definition: PcapFilter.h:900
void parseToString(std::string &result) override
void setLength(uint16_t length)
Definition: PcapFilter.h:916
UdpLengthFilter(uint16_t length, FilterOperator op)
Definition: PcapFilter.h:909
Definition: PcapFilter.h:785
VlanFilter(uint16_t vlanId)
Definition: PcapFilter.h:792
void parseToString(std::string &result) override
void setVlanID(uint16_t vlanId)
Definition: PcapFilter.h:799
The main namespace for the PcapPlusPlus lib.
ArpOpcode
Definition: ArpLayer.h:50
uint8_t ProtocolType
Definition: ProtocolType.h:17
FilterOperator
Supported operators enum.
Definition: PcapFilter.h:52
@ GREATER_THAN
Greater than.
Definition: PcapFilter.h:58
@ EQUALS
Equals.
Definition: PcapFilter.h:54
@ GREATER_OR_EQUAL
Greater or equal.
Definition: PcapFilter.h:60
@ LESS_THAN
Less than.
Definition: PcapFilter.h:62
@ NOT_EQUALS
Not equals.
Definition: PcapFilter.h:56
@ LESS_OR_EQUAL
Less or equal.
Definition: PcapFilter.h:64
Direction
An enum that contains direction (source or destination)
Definition: PcapFilter.h:41
@ SRC_OR_DST
Source or destination.
Definition: PcapFilter.h:47
@ DST
Destination.
Definition: PcapFilter.h:45
@ SRC
Source.
Definition: PcapFilter.h:43
CompositeLogicFilterOp
Supported composite logic filter operators enum.
Definition: PcapFilter.h:611
@ OR
Logical OR operation.
@ AND
Logical AND operation.
uint32_t ProtocolTypeFamily
Definition: ProtocolType.h:23
LinkLayerType
Definition: RawPacket.h:25
@ LINKTYPE_ETHERNET
Definition: RawPacket.h:29
Definition: PcapFilter.h:72