PcapPlusPlus
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 "ProtocolType.h"
7 #include <stdint.h>
8 #include "ArpLayer.h"
9 
30 namespace pcpp
31 {
32 
36  typedef enum
37  {
39  SRC,
41  DST,
44  } Direction;
45 
46 
50  typedef enum
51  {
65 
66 
73  {
74  public:
79  virtual void parseToString(std::string& result) = 0;
80 
84  virtual ~GeneralFilter();
85  };
86 
87 
94  {
95  private:
96  Direction m_Dir;
97  protected:
98  void parseDirection(std::string& directionAsString);
99  inline Direction getDir() { return m_Dir; }
100  IFilterWithDirection(Direction dir) { m_Dir = dir; }
101  public:
106  void setDirection(Direction dir) { m_Dir = dir; }
107  };
108 
109 
117  {
118  private:
119  FilterOperator m_Operator;
120  protected:
121  std::string parseOperator();
122  inline FilterOperator getOperator() { return m_Operator; }
123  IFilterWithOperator(FilterOperator op) { m_Operator = op; }
124  public:
129  void setOperator(FilterOperator op) { m_Operator = op; }
130  };
131 
132 
133 
141  {
142  private:
143  std::string m_Address;
144  std::string m_IPv4Mask;
145  int m_Len;
146  void convertToIPAddressWithMask(std::string& ipAddrmodified, std::string& mask);
147  void convertToIPAddressWithLen(std::string& ipAddrmodified, int& len);
148  public:
155  IPFilter(const std::string& ipAddress, Direction dir) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(""), m_Len(0) {}
156 
166  IPFilter(const std::string& ipAddress, Direction dir, const std::string& ipv4Mask) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(ipv4Mask), m_Len(0) {}
167 
177  IPFilter(const std::string& ipAddress, Direction dir, int len) : IFilterWithDirection(dir), m_Address(ipAddress), m_IPv4Mask(""), m_Len(len) {}
178 
179  void parseToString(std::string& result);
180 
186  void setAddr(const std::string& ipAddress) { m_Address = ipAddress; }
187 
192  void setMask(const std::string& ipv4Mask) { m_IPv4Mask = ipv4Mask; m_Len = 0; }
193 
198  void setLen(int len) { m_IPv4Mask = ""; m_Len = len; }
199  };
200 
201 
202 
210  {
211  private:
212  uint16_t m_IpID;
213  public:
219  IPv4IDFilter(uint16_t ipID, FilterOperator op) : IFilterWithOperator(op), m_IpID(ipID) {}
220 
221  void parseToString(std::string& result);
222 
227  void setIpID(uint16_t ipID) { m_IpID = ipID; }
228  };
229 
230 
231 
239  {
240  private:
241  uint16_t m_TotalLength;
242  public:
248  IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op) : IFilterWithOperator(op), m_TotalLength(totalLength) {}
249 
250  void parseToString(std::string& result);
251 
256  void setTotalLength(uint16_t totalLength) { m_TotalLength = totalLength; }
257  };
258 
259 
260 
267  {
268  private:
269  std::string m_Port;
270  void portToString(uint16_t portAsInt);
271  public:
277  PortFilter(uint16_t port, Direction dir);
278 
279  void parseToString(std::string& result);
280 
285  void setPort(uint16_t port) { portToString(port); }
286  };
287 
288 
289 
297  {
298  private:
299  uint16_t m_FromPort;
300  uint16_t m_ToPort;
301  public:
308  PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir) : IFilterWithDirection(dir), m_FromPort(fromPort), m_ToPort(toPort) {}
309 
310  void parseToString(std::string& result);
311 
316  void setFromPort(uint16_t fromPort) { m_FromPort = fromPort; }
317 
322  void setToPort(uint16_t toPort) { m_ToPort = toPort; }
323  };
324 
325 
326 
333  {
334  private:
335  MacAddress m_MacAddress;
336  public:
342  MacAddressFilter(MacAddress address, Direction dir) : IFilterWithDirection(dir), m_MacAddress(address) {}
343 
344  void parseToString(std::string& result);
345 
350  void setMacAddress(MacAddress address) { m_MacAddress = address; }
351  };
352 
353 
354 
362  {
363  private:
364  uint16_t m_EtherType;
365  public:
370  EtherTypeFilter(uint16_t etherType) : m_EtherType(etherType) {}
371 
372  void parseToString(std::string& result);
373 
378  void setEtherType(uint16_t etherType) { m_EtherType = etherType; }
379  };
380 
381 
382 
391  class AndFilter : public GeneralFilter
392  {
393  private:
394  std::vector<GeneralFilter*> m_FilterList;
395  public:
396 
401 
406  AndFilter(std::vector<GeneralFilter*>& filters);
407 
412  void addFilter(GeneralFilter* filter) { m_FilterList.push_back(filter); }
413 
418  void setFilters(std::vector<GeneralFilter*>& filters);
419 
420  void parseToString(std::string& result);
421  };
422 
423 
424 
433  class OrFilter : public GeneralFilter
434  {
435  private:
436  std::vector<GeneralFilter*> m_FilterList;
437  public:
438 
442  OrFilter() {}
443 
448  OrFilter(std::vector<GeneralFilter*>& filters);
449 
454  void addFilter(GeneralFilter* filter) { m_FilterList.push_back(filter); }
455 
456  void parseToString(std::string& result);
457  };
458 
459 
460 
466  class NotFilter : public GeneralFilter
467  {
468  private:
469  GeneralFilter* m_FilterToInverse;
470  public:
475  NotFilter(GeneralFilter* filterToInverse) { m_FilterToInverse = filterToInverse; }
476 
477  void parseToString(std::string& result);
478 
483  void setFilter(GeneralFilter* filterToInverse) { m_FilterToInverse = filterToInverse; }
484  };
485 
486 
487 
495  class ProtoFilter : public GeneralFilter
496  {
497  private:
498  ProtocolType m_Proto;
499  public:
505  ProtoFilter(ProtocolType proto) { m_Proto = proto; }
506 
507  void parseToString(std::string& result);
508 
514  void setProto(ProtocolType proto) { m_Proto = proto; }
515  };
516 
517 
518 
525  class ArpFilter : public GeneralFilter
526  {
527  private:
528  ArpOpcode m_OpCode;
529  public:
534  ArpFilter(ArpOpcode opCode) { m_OpCode = opCode; }
535 
536  void parseToString(std::string& result);
537 
542  void setOpCode(ArpOpcode opCode) { m_OpCode = opCode; }
543  };
544 
545 
546 
553  class VlanFilter : public GeneralFilter
554  {
555  private:
556  uint16_t m_VlanID;
557  public:
562  VlanFilter(uint16_t vlanId) : m_VlanID(vlanId) {}
563 
564  void parseToString(std::string& result);
565 
570  void setVlanID(uint16_t vlanId) { m_VlanID = vlanId; }
571  };
572 
573 
574 
581  {
582  public:
586  enum TcpFlags
587  {
589  tcpFin = 1,
591  tcpSyn = 2,
593  tcpRst = 4,
595  tcpPush = 8,
597  tcpAck = 16,
599  tcpUrg = 32
600  };
601 
607  {
612  };
613  private:
614  uint8_t m_TcpFlagsBitMask;
615  MatchOptions m_MatchOption;
616  public:
624  TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption) : m_TcpFlagsBitMask(tcpFlagBitMask), m_MatchOption(matchOption) {}
625 
632  void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption) { m_TcpFlagsBitMask = tcpFlagBitMask; m_MatchOption = matchOption; }
633 
634  void parseToString(std::string& result);
635  };
636 
637 
638 
645  {
646  private:
647  uint16_t m_WindowSize;
648  public:
655  TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op) : IFilterWithOperator(op), m_WindowSize(windowSize) {}
656 
657  void parseToString(std::string& result);
658 
663  void setWindowSize(uint16_t windowSize) { m_WindowSize = windowSize; }
664  };
665 
666 
667 
674  {
675  private:
676  uint16_t m_Length;
677  public:
684  UdpLengthFilter(uint16_t legnth, FilterOperator op) : IFilterWithOperator(op), m_Length(legnth) {}
685 
686  void parseToString(std::string& result);
687 
692  void setLength(uint16_t legnth) { m_Length = legnth; }
693  };
694 
695 } // namespace pcpp
696 
697 #endif
void parseToString(std::string &result)
void setOperator(FilterOperator op)
Definition: PcapFilter.h:129
IPFilter(const std::string &ipAddress, Direction dir, const std::string &ipv4Mask)
Definition: PcapFilter.h:166
Definition: PcapFilter.h:609
Definition: PcapFilter.h:593
void parseToString(std::string &result)
OrFilter()
Definition: PcapFilter.h:442
Definition: PcapFilter.h:595
void setMacAddress(MacAddress address)
Definition: PcapFilter.h:350
void setProto(ProtocolType proto)
Definition: PcapFilter.h:514
void parseToString(std::string &result)
void setVlanID(uint16_t vlanId)
Definition: PcapFilter.h:570
Definition: PcapFilter.h:611
void setFromPort(uint16_t fromPort)
Definition: PcapFilter.h:316
IPv4TotalLengthFilter(uint16_t totalLength, FilterOperator op)
Definition: PcapFilter.h:248
void parseToString(std::string &result)
void setTotalLength(uint16_t totalLength)
Definition: PcapFilter.h:256
Definition: PcapFilter.h:209
NotFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:475
Definition: PcapFilter.h:93
void setIpID(uint16_t ipID)
Definition: PcapFilter.h:227
Definition: PcapFilter.h:63
void setOpCode(ArpOpcode opCode)
Definition: PcapFilter.h:542
void parseToString(std::string &result)
Definition: PcapFilter.h:361
Definition: MacAddress.h:21
Definition: PcapFilter.h:57
Definition: PcapFilter.h:266
void setEtherType(uint16_t etherType)
Definition: PcapFilter.h:378
void parseToString(std::string &result)
Definition: PcapFilter.h:55
void setAddr(const std::string &ipAddress)
Definition: PcapFilter.h:186
void parseToString(std::string &result)
Definition: PcapFilter.h:597
EtherTypeFilter(uint16_t etherType)
Definition: PcapFilter.h:370
IPFilter(const std::string &ipAddress, Direction dir, int len)
Definition: PcapFilter.h:177
AndFilter()
Definition: PcapFilter.h:400
IPv4IDFilter(uint16_t ipID, FilterOperator op)
Definition: PcapFilter.h:219
Definition: PcapFilter.h:525
virtual ~GeneralFilter()
PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir)
Definition: PcapFilter.h:308
Definition: PcapFilter.h:433
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:454
Definition: PcapFilter.h:39
Direction
Definition: PcapFilter.h:36
VlanFilter(uint16_t vlanId)
Definition: PcapFilter.h:562
Definition: PcapFilter.h:59
void parseToString(std::string &result)
Definition: PcapFilter.h:61
void parseToString(std::string &result)
TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op)
Definition: PcapFilter.h:655
void parseToString(std::string &result)
Definition: PcapFilter.h:140
Definition: PcapFilter.h:580
Definition: PcapFilter.h:72
void parseToString(std::string &result)
void parseToString(std::string &result)
void setDirection(Direction dir)
Definition: PcapFilter.h:106
MatchOptions
Definition: PcapFilter.h:606
void parseToString(std::string &result)
Definition: PcapFilter.h:589
TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:624
Definition: PcapFilter.h:332
ArpOpcode
Definition: ArpLayer.h:47
void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:632
Definition: PcapFilter.h:238
Definition: PcapFilter.h:53
Definition: PcapFilter.h:116
void setFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:483
void setLength(uint16_t legnth)
Definition: PcapFilter.h:692
ProtocolType
Definition: ProtocolType.h:16
UdpLengthFilter(uint16_t legnth, FilterOperator op)
Definition: PcapFilter.h:684
Definition: PcapFilter.h:495
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:412
void setLen(int len)
Definition: PcapFilter.h:198
void parseToString(std::string &result)
Definition: PcapFilter.h:391
Definition: PcapFilter.h:41
void setFilters(std::vector< GeneralFilter *> &filters)
Definition: PcapFilter.h:296
Definition: PcapFilter.h:43
TcpFlags
Definition: PcapFilter.h:586
void parseToString(std::string &result)
FilterOperator
Definition: PcapFilter.h:50
Definition: PcapFilter.h:673
void setToPort(uint16_t toPort)
Definition: PcapFilter.h:322
ProtoFilter(ProtocolType proto)
Definition: PcapFilter.h:505
void parseToString(std::string &result)
Definition: PcapFilter.h:553
Definition: PcapFilter.h:466
IPFilter(const std::string &ipAddress, Direction dir)
Definition: PcapFilter.h:155
virtual void parseToString(std::string &result)=0
void setPort(uint16_t port)
Definition: PcapFilter.h:285
Definition: PcapFilter.h:599
The main namespace for the PcapPlusPlus lib.
PortFilter(uint16_t port, Direction dir)
Definition: PcapFilter.h:591
Definition: PcapFilter.h:644
ArpFilter(ArpOpcode opCode)
Definition: PcapFilter.h:534
MacAddressFilter(MacAddress address, Direction dir)
Definition: PcapFilter.h:342
void setWindowSize(uint16_t windowSize)
Definition: PcapFilter.h:663
void setMask(const std::string &ipv4Mask)
Definition: PcapFilter.h:192