PcapPlusPlus  19.12
PcapFilter.h
Go to the documentation of this file.
1 #ifndef PCAPP_FILTER
2 #define PCAPP_FILTER
3 
4 #include <string>
5 #include <vector>
6 #include <memory>
7 #include "ProtocolType.h"
8 #include <stdint.h>
9 #include "ArpLayer.h"
10 
11 //Forward Declaration - used in GeneralFilter
12 struct bpf_program;
13 
34 namespace pcpp
35 {
36  //Forward Declartation - used in GeneralFilter
37  class RawPacket;
38 
42  typedef enum
43  {
45  SRC,
47  DST,
50  } Direction;
51 
52 
56  typedef enum
57  {
71 
72 
79  {
80  protected:
81  bpf_program* m_program;
82  std::string m_lastProgramString;
83 
87  void freeProgram();
88 
89  public:
94  virtual void parseToString(std::string& result) = 0;
95 
101  bool matchPacketWithFilter(RawPacket* rawPacket);
102 
103  GeneralFilter() : m_program(NULL) {}
104 
108  virtual ~GeneralFilter() { freeProgram(); }
109  };
110 
116  {
117  private:
118  const std::string m_filterStr;
119 
120  public:
121  BPFStringFilter(const std::string& filterStr) : m_filterStr(filterStr) {}
122 
123  virtual ~BPFStringFilter() {}
124 
130  virtual void parseToString(std::string& result);
131 
136  bool verifyFilter();
137  };
138 
139 
146  {
147  private:
148  Direction m_Dir;
149  protected:
150  void parseDirection(std::string& directionAsString);
151  Direction getDir() const { return m_Dir; }
152  IFilterWithDirection(Direction dir) { m_Dir = dir; }
153  public:
158  void setDirection(Direction dir) { m_Dir = dir; }
159  };
160 
161 
169  {
170  private:
171  FilterOperator m_Operator;
172  protected:
173  std::string parseOperator();
174  FilterOperator getOperator() const { return m_Operator; }
175  IFilterWithOperator(FilterOperator op) { m_Operator = op; }
176  public:
181  void setOperator(FilterOperator op) { m_Operator = op; }
182  };
183 
184 
185 
193  {
194  private:
195  std::string m_Address;
196  std::string m_IPv4Mask;
197  int m_Len;
198  void convertToIPAddressWithMask(std::string& ipAddrmodified, std::string& mask) const;
199  void convertToIPAddressWithLen(std::string& ipAddrmodified, int& len) const;
200  public:
207  IPFilter(const std::string& ipAddress, Direction dir) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(""), m_Len(0) {}
208 
218  IPFilter(const std::string& ipAddress, Direction dir, const std::string& ipv4Mask) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(ipv4Mask), m_Len(0) {}
219 
229  IPFilter(const std::string& ipAddress, Direction dir, int len) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(""), m_Len(len) {}
230 
231  void parseToString(std::string& result);
232 
238  void setAddr(const std::string& ipAddress) { m_Address = ipAddress; }
239 
244  void setMask(const std::string& ipv4Mask) { m_IPv4Mask = ipv4Mask; m_Len = 0; }
245 
250  void setLen(int len) { m_IPv4Mask = ""; m_Len = len; }
251  };
252 
253 
254 
262  {
263  private:
264  uint16_t m_IpID;
265  public:
271  IPv4IDFilter(uint16_t ipID, FilterOperator op) : IFilterWithOperator(op), m_IpID(ipID) {}
272 
273  void parseToString(std::string& result);
274 
279  void setIpID(uint16_t ipID) { m_IpID = ipID; }
280  };
281 
282 
283 
291  {
292  private:
293  uint16_t m_TotalLength;
294  public:
300  IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op) : IFilterWithOperator(op), m_TotalLength(totalLength) {}
301 
302  void parseToString(std::string& result);
303 
308  void setTotalLength(uint16_t totalLength) { m_TotalLength = totalLength; }
309  };
310 
311 
312 
319  {
320  private:
321  std::string m_Port;
322  void portToString(uint16_t portAsInt);
323  public:
329  PortFilter(uint16_t port, Direction dir);
330 
331  void parseToString(std::string& result);
332 
337  void setPort(uint16_t port) { portToString(port); }
338  };
339 
340 
341 
349  {
350  private:
351  uint16_t m_FromPort;
352  uint16_t m_ToPort;
353  public:
360  PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir) : IFilterWithDirection(dir), m_FromPort(fromPort), m_ToPort(toPort) {}
361 
362  void parseToString(std::string& result);
363 
368  void setFromPort(uint16_t fromPort) { m_FromPort = fromPort; }
369 
374  void setToPort(uint16_t toPort) { m_ToPort = toPort; }
375  };
376 
377 
378 
385  {
386  private:
387  MacAddress m_MacAddress;
388  public:
394  MacAddressFilter(MacAddress address, Direction dir) : IFilterWithDirection(dir), m_MacAddress(address) {}
395 
396  void parseToString(std::string& result);
397 
402  void setMacAddress(MacAddress address) { m_MacAddress = address; }
403  };
404 
405 
406 
414  {
415  private:
416  uint16_t m_EtherType;
417  public:
422  EtherTypeFilter(uint16_t etherType) : m_EtherType(etherType) {}
423 
424  void parseToString(std::string& result);
425 
430  void setEtherType(uint16_t etherType) { m_EtherType = etherType; }
431  };
432 
433 
434 
443  class AndFilter : public GeneralFilter
444  {
445  private:
446  std::vector<GeneralFilter*> m_FilterList;
447  public:
448 
453 
458  AndFilter(std::vector<GeneralFilter*>& filters);
459 
464  void addFilter(GeneralFilter* filter) { m_FilterList.push_back(filter); }
465 
470  void setFilters(std::vector<GeneralFilter*>& filters);
471 
472  void parseToString(std::string& result);
473  };
474 
475 
476 
485  class OrFilter : public GeneralFilter
486  {
487  private:
488  std::vector<GeneralFilter*> m_FilterList;
489  public:
490 
494  OrFilter() {}
495 
500  OrFilter(std::vector<GeneralFilter*>& filters);
501 
506  void addFilter(GeneralFilter* filter) { m_FilterList.push_back(filter); }
507 
508  void parseToString(std::string& result);
509  };
510 
511 
512 
518  class NotFilter : public GeneralFilter
519  {
520  private:
521  GeneralFilter* m_FilterToInverse;
522  public:
527  NotFilter(GeneralFilter* filterToInverse) { m_FilterToInverse = filterToInverse; }
528 
529  void parseToString(std::string& result);
530 
535  void setFilter(GeneralFilter* filterToInverse) { m_FilterToInverse = filterToInverse; }
536  };
537 
538 
539 
547  class ProtoFilter : public GeneralFilter
548  {
549  private:
550  ProtocolType m_Proto;
551  public:
557  ProtoFilter(ProtocolType proto) { m_Proto = proto; }
558 
559  void parseToString(std::string& result);
560 
566  void setProto(ProtocolType proto) { m_Proto = proto; }
567  };
568 
569 
570 
577  class ArpFilter : public GeneralFilter
578  {
579  private:
580  ArpOpcode m_OpCode;
581  public:
586  ArpFilter(ArpOpcode opCode) { m_OpCode = opCode; }
587 
588  void parseToString(std::string& result);
589 
594  void setOpCode(ArpOpcode opCode) { m_OpCode = opCode; }
595  };
596 
597 
598 
605  class VlanFilter : public GeneralFilter
606  {
607  private:
608  uint16_t m_VlanID;
609  public:
614  VlanFilter(uint16_t vlanId) : m_VlanID(vlanId) {}
615 
616  void parseToString(std::string& result);
617 
622  void setVlanID(uint16_t vlanId) { m_VlanID = vlanId; }
623  };
624 
625 
626 
633  {
634  public:
638  enum TcpFlags
639  {
641  tcpFin = 1,
643  tcpSyn = 2,
645  tcpRst = 4,
647  tcpPush = 8,
649  tcpAck = 16,
651  tcpUrg = 32
652  };
653 
659  {
664  };
665  private:
666  uint8_t m_TcpFlagsBitMask;
667  MatchOptions m_MatchOption;
668  public:
676  TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption) : m_TcpFlagsBitMask(tcpFlagBitMask), m_MatchOption(matchOption) {}
677 
684  void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption) { m_TcpFlagsBitMask = tcpFlagBitMask; m_MatchOption = matchOption; }
685 
686  void parseToString(std::string& result);
687  };
688 
689 
690 
697  {
698  private:
699  uint16_t m_WindowSize;
700  public:
707  TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op) : IFilterWithOperator(op), m_WindowSize(windowSize) {}
708 
709  void parseToString(std::string& result);
710 
715  void setWindowSize(uint16_t windowSize) { m_WindowSize = windowSize; }
716  };
717 
718 
719 
726  {
727  private:
728  uint16_t m_Length;
729  public:
736  UdpLengthFilter(uint16_t legnth, FilterOperator op) : IFilterWithOperator(op), m_Length(legnth) {}
737 
738  void parseToString(std::string& result);
739 
744  void setLength(uint16_t legnth) { m_Length = legnth; }
745  };
746 
747 } // namespace pcpp
748 
749 #endif
pcpp::IFilterWithDirection
Definition: PcapFilter.h:145
pcpp::ArpFilter::ArpFilter
ArpFilter(ArpOpcode opCode)
Definition: PcapFilter.h:586
pcpp::PortRangeFilter::parseToString
void parseToString(std::string &result)
pcpp::IPv4TotalLengthFilter::IPv4TotalLengthFilter
IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op)
Definition: PcapFilter.h:300
pcpp::IFilterWithOperator::setOperator
void setOperator(FilterOperator op)
Definition: PcapFilter.h:181
pcpp::TcpWindowSizeFilter
Definition: PcapFilter.h:696
pcpp::DST
Definition: PcapFilter.h:47
pcpp::EtherTypeFilter::setEtherType
void setEtherType(uint16_t etherType)
Definition: PcapFilter.h:430
pcpp::AndFilter::AndFilter
AndFilter()
Definition: PcapFilter.h:452
pcpp::ProtoFilter
Definition: PcapFilter.h:547
pcpp::UdpLengthFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter::setTcpFlagsBitMask
void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:684
pcpp::PortRangeFilter::setFromPort
void setFromPort(uint16_t fromPort)
Definition: PcapFilter.h:368
pcpp::TcpFlagsFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter::tcpSyn
Definition: PcapFilter.h:643
pcpp::IPv4IDFilter
Definition: PcapFilter.h:261
pcpp::IPv4IDFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter
Definition: PcapFilter.h:632
pcpp::TcpFlagsFilter::TcpFlagsFilter
TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:676
pcpp::VlanFilter::setVlanID
void setVlanID(uint16_t vlanId)
Definition: PcapFilter.h:622
pcpp::ArpOpcode
ArpOpcode
Definition: ArpLayer.h:47
pcpp::PortFilter::PortFilter
PortFilter(uint16_t port, Direction dir)
pcpp::ArpFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter::MatchAll
Definition: PcapFilter.h:661
pcpp::ArpFilter::setOpCode
void setOpCode(ArpOpcode opCode)
Definition: PcapFilter.h:594
pcpp::IPFilter::parseToString
void parseToString(std::string &result)
pcpp::AndFilter::setFilters
void setFilters(std::vector< GeneralFilter * > &filters)
pcpp::IPv4TotalLengthFilter::setTotalLength
void setTotalLength(uint16_t totalLength)
Definition: PcapFilter.h:308
pcpp::IPv4IDFilter::setIpID
void setIpID(uint16_t ipID)
Definition: PcapFilter.h:279
pcpp::GeneralFilter::matchPacketWithFilter
bool matchPacketWithFilter(RawPacket *rawPacket)
ArpLayer.h
pcpp::VlanFilter
Definition: PcapFilter.h:605
pcpp::UdpLengthFilter
Definition: PcapFilter.h:725
pcpp::IPv4TotalLengthFilter::parseToString
void parseToString(std::string &result)
pcpp::EtherTypeFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpWindowSizeFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter::tcpFin
Definition: PcapFilter.h:641
pcpp::IPFilter::setAddr
void setAddr(const std::string &ipAddress)
Definition: PcapFilter.h:238
pcpp::ProtoFilter::parseToString
void parseToString(std::string &result)
pcpp::SRC_OR_DST
Definition: PcapFilter.h:49
pcpp::IPv4TotalLengthFilter
Definition: PcapFilter.h:290
ProtocolType.h
pcpp::ProtoFilter::ProtoFilter
ProtoFilter(ProtocolType proto)
Definition: PcapFilter.h:557
pcpp::MacAddressFilter
Definition: PcapFilter.h:384
pcpp::TcpFlagsFilter::tcpPush
Definition: PcapFilter.h:647
pcpp::BPFStringFilter
Definition: PcapFilter.h:115
pcpp::GeneralFilter::~GeneralFilter
virtual ~GeneralFilter()
Definition: PcapFilter.h:108
pcpp::MacAddress
Definition: MacAddress.h:27
pcpp::GREATER_THAN
Definition: PcapFilter.h:63
pcpp::BPFStringFilter::verifyFilter
bool verifyFilter()
pcpp::UdpLengthFilter::setLength
void setLength(uint16_t legnth)
Definition: PcapFilter.h:744
pcpp::EtherTypeFilter::EtherTypeFilter
EtherTypeFilter(uint16_t etherType)
Definition: PcapFilter.h:422
pcpp::ProtocolType
ProtocolType
Definition: ProtocolType.h:16
pcpp::PortRangeFilter::setToPort
void setToPort(uint16_t toPort)
Definition: PcapFilter.h:374
pcpp::IFilterWithOperator
Definition: PcapFilter.h:168
pcpp::FilterOperator
FilterOperator
Definition: PcapFilter.h:56
pcpp::Direction
Direction
Definition: PcapFilter.h:42
pcpp::OrFilter::parseToString
void parseToString(std::string &result)
pcpp::IPFilter::IPFilter
IPFilter(const std::string &ipAddress, Direction dir)
Definition: PcapFilter.h:207
pcpp::NotFilter::NotFilter
NotFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:527
pcpp::PortFilter
Definition: PcapFilter.h:318
pcpp::IPFilter
Definition: PcapFilter.h:192
pcpp::NotFilter
Definition: PcapFilter.h:518
pcpp::IPFilter::setMask
void setMask(const std::string &ipv4Mask)
Definition: PcapFilter.h:244
pcpp::TcpFlagsFilter::MatchOneAtLeast
Definition: PcapFilter.h:663
pcpp::OrFilter
Definition: PcapFilter.h:485
pcpp::AndFilter::parseToString
void parseToString(std::string &result)
pcpp::IPv4IDFilter::IPv4IDFilter
IPv4IDFilter(uint16_t ipID, FilterOperator op)
Definition: PcapFilter.h:271
pcpp::PortFilter::parseToString
void parseToString(std::string &result)
pcpp::LESS_THAN
Definition: PcapFilter.h:67
pcpp::TcpWindowSizeFilter::setWindowSize
void setWindowSize(uint16_t windowSize)
Definition: PcapFilter.h:715
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::OrFilter::OrFilter
OrFilter()
Definition: PcapFilter.h:494
pcpp::VlanFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter::MatchOptions
MatchOptions
Definition: PcapFilter.h:658
pcpp::IPFilter::setLen
void setLen(int len)
Definition: PcapFilter.h:250
pcpp::MacAddressFilter::MacAddressFilter
MacAddressFilter(MacAddress address, Direction dir)
Definition: PcapFilter.h:394
pcpp::ArpFilter
Definition: PcapFilter.h:577
pcpp::IPFilter::IPFilter
IPFilter(const std::string &ipAddress, Direction dir, int len)
Definition: PcapFilter.h:229
pcpp::GeneralFilter::freeProgram
void freeProgram()
pcpp::PortRangeFilter
Definition: PcapFilter.h:348
pcpp::ProtoFilter::setProto
void setProto(ProtocolType proto)
Definition: PcapFilter.h:566
pcpp::NotFilter::parseToString
void parseToString(std::string &result)
pcpp::MacAddressFilter::parseToString
void parseToString(std::string &result)
pcpp::SRC
Definition: PcapFilter.h:45
pcpp::UdpLengthFilter::UdpLengthFilter
UdpLengthFilter(uint16_t legnth, FilterOperator op)
Definition: PcapFilter.h:736
pcpp::BPFStringFilter::parseToString
virtual void parseToString(std::string &result)
pcpp::PortRangeFilter::PortRangeFilter
PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir)
Definition: PcapFilter.h:360
pcpp::TcpWindowSizeFilter::TcpWindowSizeFilter
TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op)
Definition: PcapFilter.h:707
pcpp::MacAddressFilter::setMacAddress
void setMacAddress(MacAddress address)
Definition: PcapFilter.h:402
pcpp::IFilterWithDirection::setDirection
void setDirection(Direction dir)
Definition: PcapFilter.h:158
pcpp::EQUALS
Definition: PcapFilter.h:59
pcpp::EtherTypeFilter
Definition: PcapFilter.h:413
pcpp::TcpFlagsFilter::tcpAck
Definition: PcapFilter.h:649
pcpp::NotFilter::setFilter
void setFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:535
pcpp::NOT_EQUALS
Definition: PcapFilter.h:61
pcpp::TcpFlagsFilter::tcpUrg
Definition: PcapFilter.h:651
pcpp::IPFilter::IPFilter
IPFilter(const std::string &ipAddress, Direction dir, const std::string &ipv4Mask)
Definition: PcapFilter.h:218
pcpp::AndFilter::addFilter
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:464
pcpp::GeneralFilter
Definition: PcapFilter.h:78
pcpp::GREATER_OR_EQUAL
Definition: PcapFilter.h:65
pcpp::TcpFlagsFilter::TcpFlags
TcpFlags
Definition: PcapFilter.h:638
pcpp::GeneralFilter::parseToString
virtual void parseToString(std::string &result)=0
pcpp::TcpFlagsFilter::tcpRst
Definition: PcapFilter.h:645
pcpp::RawPacket
Definition: RawPacket.h:219
pcpp::VlanFilter::VlanFilter
VlanFilter(uint16_t vlanId)
Definition: PcapFilter.h:614
pcpp::AndFilter
Definition: PcapFilter.h:443
pcpp::PortFilter::setPort
void setPort(uint16_t port)
Definition: PcapFilter.h:337
pcpp::LESS_OR_EQUAL
Definition: PcapFilter.h:69
pcpp::OrFilter::addFilter
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:506