PcapPlusPlus  20.08
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 #include "RawPacket.h"
11 
12 //Forward Declaration - used in GeneralFilter
13 struct bpf_program;
14 
35 namespace pcpp
36 {
37  //Forward Declartation - used in GeneralFilter
38  class RawPacket;
39 
43  typedef enum
44  {
46  SRC,
48  DST,
51  } Direction;
52 
53 
57  typedef enum
58  {
72 
73 
80  {
81  protected:
82  bpf_program* m_Program;
83  std::string m_LastProgramString;
84  pcpp::LinkLayerType m_LastLinkLayerType;
85 
89  void freeProgram();
90 
91  public:
96  virtual void parseToString(std::string& result) = 0;
97 
103  bool matchPacketWithFilter(RawPacket* rawPacket);
104 
105  GeneralFilter() : m_Program(NULL), m_LastProgramString(), m_LastLinkLayerType(pcpp::LINKTYPE_ETHERNET) {}
106 
110  virtual ~GeneralFilter() { freeProgram(); }
111  };
112 
118  {
119  private:
120  const std::string m_FilterStr;
121 
122  public:
123  BPFStringFilter(const std::string& filterStr) : m_FilterStr(filterStr) {}
124 
125  virtual ~BPFStringFilter() {}
126 
132  virtual void parseToString(std::string& result);
133 
138  bool verifyFilter();
139  };
140 
141 
148  {
149  private:
150  Direction m_Dir;
151  protected:
152  void parseDirection(std::string& directionAsString);
153  Direction getDir() const { return m_Dir; }
154  IFilterWithDirection(Direction dir) { m_Dir = dir; }
155  public:
160  void setDirection(Direction dir) { m_Dir = dir; }
161  };
162 
163 
171  {
172  private:
173  FilterOperator m_Operator;
174  protected:
175  std::string parseOperator();
176  FilterOperator getOperator() const { return m_Operator; }
177  IFilterWithOperator(FilterOperator op) { m_Operator = op; }
178  public:
183  void setOperator(FilterOperator op) { m_Operator = op; }
184  };
185 
186 
187 
195  {
196  private:
197  std::string m_Address;
198  std::string m_IPv4Mask;
199  int m_Len;
200  void convertToIPAddressWithMask(std::string& ipAddrmodified, std::string& mask) const;
201  void convertToIPAddressWithLen(std::string& ipAddrmodified) const;
202  public:
209  IPFilter(const std::string& ipAddress, Direction dir) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(""), m_Len(0) {}
210 
220  IPFilter(const std::string& ipAddress, Direction dir, const std::string& ipv4Mask) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(ipv4Mask), m_Len(0) {}
221 
231  IPFilter(const std::string& ipAddress, Direction dir, int len) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(""), m_Len(len) {}
232 
233  void parseToString(std::string& result);
234 
240  void setAddr(const std::string& ipAddress) { m_Address = ipAddress; }
241 
246  void setMask(const std::string& ipv4Mask) { m_IPv4Mask = ipv4Mask; m_Len = 0; }
247 
252  void setLen(int len) { m_IPv4Mask = ""; m_Len = len; }
253  };
254 
255 
256 
264  {
265  private:
266  uint16_t m_IpID;
267  public:
273  IPv4IDFilter(uint16_t ipID, FilterOperator op) : IFilterWithOperator(op), m_IpID(ipID) {}
274 
275  void parseToString(std::string& result);
276 
281  void setIpID(uint16_t ipID) { m_IpID = ipID; }
282  };
283 
284 
285 
293  {
294  private:
295  uint16_t m_TotalLength;
296  public:
302  IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op) : IFilterWithOperator(op), m_TotalLength(totalLength) {}
303 
304  void parseToString(std::string& result);
305 
310  void setTotalLength(uint16_t totalLength) { m_TotalLength = totalLength; }
311  };
312 
313 
314 
321  {
322  private:
323  std::string m_Port;
324  void portToString(uint16_t portAsInt);
325  public:
331  PortFilter(uint16_t port, Direction dir);
332 
333  void parseToString(std::string& result);
334 
339  void setPort(uint16_t port) { portToString(port); }
340  };
341 
342 
343 
351  {
352  private:
353  uint16_t m_FromPort;
354  uint16_t m_ToPort;
355  public:
362  PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir) : IFilterWithDirection(dir), m_FromPort(fromPort), m_ToPort(toPort) {}
363 
364  void parseToString(std::string& result);
365 
370  void setFromPort(uint16_t fromPort) { m_FromPort = fromPort; }
371 
376  void setToPort(uint16_t toPort) { m_ToPort = toPort; }
377  };
378 
379 
380 
387  {
388  private:
389  MacAddress m_MacAddress;
390  public:
396  MacAddressFilter(MacAddress address, Direction dir) : IFilterWithDirection(dir), m_MacAddress(address) {}
397 
398  void parseToString(std::string& result);
399 
404  void setMacAddress(MacAddress address) { m_MacAddress = address; }
405  };
406 
407 
408 
416  {
417  private:
418  uint16_t m_EtherType;
419  public:
424  EtherTypeFilter(uint16_t etherType) : m_EtherType(etherType) {}
425 
426  void parseToString(std::string& result);
427 
432  void setEtherType(uint16_t etherType) { m_EtherType = etherType; }
433  };
434 
435 
436 
445  class AndFilter : public GeneralFilter
446  {
447  private:
448  std::vector<GeneralFilter*> m_FilterList;
449  public:
450 
455 
460  AndFilter(std::vector<GeneralFilter*>& filters);
461 
466  void addFilter(GeneralFilter* filter) { m_FilterList.push_back(filter); }
467 
472  void setFilters(std::vector<GeneralFilter*>& filters);
473 
474  void parseToString(std::string& result);
475  };
476 
477 
478 
487  class OrFilter : public GeneralFilter
488  {
489  private:
490  std::vector<GeneralFilter*> m_FilterList;
491  public:
492 
496  OrFilter() {}
497 
502  OrFilter(std::vector<GeneralFilter*>& filters);
503 
508  void addFilter(GeneralFilter* filter) { m_FilterList.push_back(filter); }
509 
510  void parseToString(std::string& result);
511  };
512 
513 
514 
520  class NotFilter : public GeneralFilter
521  {
522  private:
523  GeneralFilter* m_FilterToInverse;
524  public:
529  NotFilter(GeneralFilter* filterToInverse) { m_FilterToInverse = filterToInverse; }
530 
531  void parseToString(std::string& result);
532 
537  void setFilter(GeneralFilter* filterToInverse) { m_FilterToInverse = filterToInverse; }
538  };
539 
540 
541 
549  class ProtoFilter : public GeneralFilter
550  {
551  private:
552  ProtocolType m_Proto;
553  public:
559  ProtoFilter(ProtocolType proto) { m_Proto = proto; }
560 
561  void parseToString(std::string& result);
562 
568  void setProto(ProtocolType proto) { m_Proto = proto; }
569  };
570 
571 
572 
579  class ArpFilter : public GeneralFilter
580  {
581  private:
582  ArpOpcode m_OpCode;
583  public:
588  ArpFilter(ArpOpcode opCode) { m_OpCode = opCode; }
589 
590  void parseToString(std::string& result);
591 
596  void setOpCode(ArpOpcode opCode) { m_OpCode = opCode; }
597  };
598 
599 
600 
607  class VlanFilter : public GeneralFilter
608  {
609  private:
610  uint16_t m_VlanID;
611  public:
616  VlanFilter(uint16_t vlanId) : m_VlanID(vlanId) {}
617 
618  void parseToString(std::string& result);
619 
624  void setVlanID(uint16_t vlanId) { m_VlanID = vlanId; }
625  };
626 
627 
628 
635  {
636  public:
640  enum TcpFlags
641  {
643  tcpFin = 1,
645  tcpSyn = 2,
647  tcpRst = 4,
649  tcpPush = 8,
651  tcpAck = 16,
653  tcpUrg = 32
654  };
655 
661  {
666  };
667  private:
668  uint8_t m_TcpFlagsBitMask;
669  MatchOptions m_MatchOption;
670  public:
678  TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption) : m_TcpFlagsBitMask(tcpFlagBitMask), m_MatchOption(matchOption) {}
679 
686  void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption) { m_TcpFlagsBitMask = tcpFlagBitMask; m_MatchOption = matchOption; }
687 
688  void parseToString(std::string& result);
689  };
690 
691 
692 
699  {
700  private:
701  uint16_t m_WindowSize;
702  public:
709  TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op) : IFilterWithOperator(op), m_WindowSize(windowSize) {}
710 
711  void parseToString(std::string& result);
712 
717  void setWindowSize(uint16_t windowSize) { m_WindowSize = windowSize; }
718  };
719 
720 
721 
728  {
729  private:
730  uint16_t m_Length;
731  public:
738  UdpLengthFilter(uint16_t legnth, FilterOperator op) : IFilterWithOperator(op), m_Length(legnth) {}
739 
740  void parseToString(std::string& result);
741 
746  void setLength(uint16_t legnth) { m_Length = legnth; }
747  };
748 
749 } // namespace pcpp
750 
751 #endif
pcpp::IFilterWithDirection
Definition: PcapFilter.h:147
pcpp::ArpFilter::ArpFilter
ArpFilter(ArpOpcode opCode)
Definition: PcapFilter.h:588
pcpp::PortRangeFilter::parseToString
void parseToString(std::string &result)
pcpp::IPv4TotalLengthFilter::IPv4TotalLengthFilter
IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op)
Definition: PcapFilter.h:302
pcpp::IFilterWithOperator::setOperator
void setOperator(FilterOperator op)
Definition: PcapFilter.h:183
pcpp::TcpWindowSizeFilter
Definition: PcapFilter.h:698
pcpp::DST
@ DST
Definition: PcapFilter.h:48
pcpp::EtherTypeFilter::setEtherType
void setEtherType(uint16_t etherType)
Definition: PcapFilter.h:432
pcpp::AndFilter::AndFilter
AndFilter()
Definition: PcapFilter.h:454
pcpp::ProtoFilter
Definition: PcapFilter.h:549
pcpp::UdpLengthFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter::setTcpFlagsBitMask
void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:686
pcpp::PortRangeFilter::setFromPort
void setFromPort(uint16_t fromPort)
Definition: PcapFilter.h:370
pcpp::TcpFlagsFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter::tcpSyn
@ tcpSyn
Definition: PcapFilter.h:645
pcpp::IPv4IDFilter
Definition: PcapFilter.h:263
pcpp::IPv4IDFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter
Definition: PcapFilter.h:634
pcpp::LINKTYPE_ETHERNET
@ LINKTYPE_ETHERNET
Definition: RawPacket.h:30
pcpp::TcpFlagsFilter::TcpFlagsFilter
TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:678
pcpp::VlanFilter::setVlanID
void setVlanID(uint16_t vlanId)
Definition: PcapFilter.h:624
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
@ MatchAll
Definition: PcapFilter.h:663
pcpp::ArpFilter::setOpCode
void setOpCode(ArpOpcode opCode)
Definition: PcapFilter.h:596
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:310
pcpp::IPv4IDFilter::setIpID
void setIpID(uint16_t ipID)
Definition: PcapFilter.h:281
pcpp::GeneralFilter::matchPacketWithFilter
bool matchPacketWithFilter(RawPacket *rawPacket)
ArpLayer.h
pcpp::VlanFilter
Definition: PcapFilter.h:607
pcpp::UdpLengthFilter
Definition: PcapFilter.h:727
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
@ tcpFin
Definition: PcapFilter.h:643
pcpp::IPFilter::setAddr
void setAddr(const std::string &ipAddress)
Definition: PcapFilter.h:240
pcpp::ProtoFilter::parseToString
void parseToString(std::string &result)
pcpp::SRC_OR_DST
@ SRC_OR_DST
Definition: PcapFilter.h:50
pcpp::IPv4TotalLengthFilter
Definition: PcapFilter.h:292
ProtocolType.h
pcpp::ProtoFilter::ProtoFilter
ProtoFilter(ProtocolType proto)
Definition: PcapFilter.h:559
pcpp::MacAddressFilter
Definition: PcapFilter.h:386
pcpp::TcpFlagsFilter::tcpPush
@ tcpPush
Definition: PcapFilter.h:649
pcpp::BPFStringFilter
Definition: PcapFilter.h:117
pcpp::GeneralFilter::~GeneralFilter
virtual ~GeneralFilter()
Definition: PcapFilter.h:110
RawPacket.h
pcpp::MacAddress
Definition: MacAddress.h:27
pcpp::GREATER_THAN
@ GREATER_THAN
Definition: PcapFilter.h:64
pcpp::BPFStringFilter::verifyFilter
bool verifyFilter()
pcpp::UdpLengthFilter::setLength
void setLength(uint16_t legnth)
Definition: PcapFilter.h:746
pcpp::EtherTypeFilter::EtherTypeFilter
EtherTypeFilter(uint16_t etherType)
Definition: PcapFilter.h:424
pcpp::ProtocolType
uint64_t ProtocolType
Definition: ProtocolType.h:18
pcpp::PortRangeFilter::setToPort
void setToPort(uint16_t toPort)
Definition: PcapFilter.h:376
pcpp::IFilterWithOperator
Definition: PcapFilter.h:170
pcpp::FilterOperator
FilterOperator
Definition: PcapFilter.h:57
pcpp::Direction
Direction
Definition: PcapFilter.h:43
pcpp::OrFilter::parseToString
void parseToString(std::string &result)
pcpp::IPFilter::IPFilter
IPFilter(const std::string &ipAddress, Direction dir)
Definition: PcapFilter.h:209
pcpp::NotFilter::NotFilter
NotFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:529
pcpp::PortFilter
Definition: PcapFilter.h:320
pcpp::IPFilter
Definition: PcapFilter.h:194
pcpp::NotFilter
Definition: PcapFilter.h:520
pcpp::IPFilter::setMask
void setMask(const std::string &ipv4Mask)
Definition: PcapFilter.h:246
pcpp::TcpFlagsFilter::MatchOneAtLeast
@ MatchOneAtLeast
Definition: PcapFilter.h:665
pcpp::OrFilter
Definition: PcapFilter.h:487
pcpp::AndFilter::parseToString
void parseToString(std::string &result)
pcpp::IPv4IDFilter::IPv4IDFilter
IPv4IDFilter(uint16_t ipID, FilterOperator op)
Definition: PcapFilter.h:273
pcpp::PortFilter::parseToString
void parseToString(std::string &result)
pcpp::LESS_THAN
@ LESS_THAN
Definition: PcapFilter.h:68
pcpp::TcpWindowSizeFilter::setWindowSize
void setWindowSize(uint16_t windowSize)
Definition: PcapFilter.h:717
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::OrFilter::OrFilter
OrFilter()
Definition: PcapFilter.h:496
pcpp::VlanFilter::parseToString
void parseToString(std::string &result)
pcpp::TcpFlagsFilter::MatchOptions
MatchOptions
Definition: PcapFilter.h:660
pcpp::IPFilter::setLen
void setLen(int len)
Definition: PcapFilter.h:252
pcpp::MacAddressFilter::MacAddressFilter
MacAddressFilter(MacAddress address, Direction dir)
Definition: PcapFilter.h:396
pcpp::ArpFilter
Definition: PcapFilter.h:579
pcpp::IPFilter::IPFilter
IPFilter(const std::string &ipAddress, Direction dir, int len)
Definition: PcapFilter.h:231
pcpp::LinkLayerType
LinkLayerType
Definition: RawPacket.h:25
pcpp::GeneralFilter::freeProgram
void freeProgram()
pcpp::PortRangeFilter
Definition: PcapFilter.h:350
pcpp::ProtoFilter::setProto
void setProto(ProtocolType proto)
Definition: PcapFilter.h:568
pcpp::NotFilter::parseToString
void parseToString(std::string &result)
pcpp::MacAddressFilter::parseToString
void parseToString(std::string &result)
pcpp::SRC
@ SRC
Definition: PcapFilter.h:46
pcpp::UdpLengthFilter::UdpLengthFilter
UdpLengthFilter(uint16_t legnth, FilterOperator op)
Definition: PcapFilter.h:738
pcpp::BPFStringFilter::parseToString
virtual void parseToString(std::string &result)
pcpp::PortRangeFilter::PortRangeFilter
PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir)
Definition: PcapFilter.h:362
pcpp::TcpWindowSizeFilter::TcpWindowSizeFilter
TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op)
Definition: PcapFilter.h:709
pcpp::MacAddressFilter::setMacAddress
void setMacAddress(MacAddress address)
Definition: PcapFilter.h:404
pcpp::IFilterWithDirection::setDirection
void setDirection(Direction dir)
Definition: PcapFilter.h:160
pcpp::EQUALS
@ EQUALS
Definition: PcapFilter.h:60
pcpp::EtherTypeFilter
Definition: PcapFilter.h:415
pcpp::TcpFlagsFilter::tcpAck
@ tcpAck
Definition: PcapFilter.h:651
pcpp::NotFilter::setFilter
void setFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:537
pcpp::NOT_EQUALS
@ NOT_EQUALS
Definition: PcapFilter.h:62
pcpp::TcpFlagsFilter::tcpUrg
@ tcpUrg
Definition: PcapFilter.h:653
pcpp::IPFilter::IPFilter
IPFilter(const std::string &ipAddress, Direction dir, const std::string &ipv4Mask)
Definition: PcapFilter.h:220
pcpp::AndFilter::addFilter
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:466
pcpp::GeneralFilter
Definition: PcapFilter.h:79
pcpp::GREATER_OR_EQUAL
@ GREATER_OR_EQUAL
Definition: PcapFilter.h:66
pcpp::TcpFlagsFilter::TcpFlags
TcpFlags
Definition: PcapFilter.h:640
pcpp::GeneralFilter::parseToString
virtual void parseToString(std::string &result)=0
pcpp::TcpFlagsFilter::tcpRst
@ tcpRst
Definition: PcapFilter.h:647
pcpp::RawPacket
Definition: RawPacket.h:252
pcpp::VlanFilter::VlanFilter
VlanFilter(uint16_t vlanId)
Definition: PcapFilter.h:616
pcpp::AndFilter
Definition: PcapFilter.h:445
pcpp::PortFilter::setPort
void setPort(uint16_t port)
Definition: PcapFilter.h:339
pcpp::LESS_OR_EQUAL
@ LESS_OR_EQUAL
Definition: PcapFilter.h:70
pcpp::OrFilter::addFilter
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:508