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 
414  void parseToString(std::string& result);
415  };
416 
417 
418 
427  class OrFilter : public GeneralFilter
428  {
429  private:
430  std::vector<GeneralFilter*> m_FilterList;
431  public:
432 
436  OrFilter() {}
437 
442  OrFilter(std::vector<GeneralFilter*>& filters);
443 
448  void addFilter(GeneralFilter* filter) { m_FilterList.push_back(filter); }
449 
450  void parseToString(std::string& result);
451  };
452 
453 
454 
460  class NotFilter : public GeneralFilter
461  {
462  private:
463  GeneralFilter* m_FilterToInverse;
464  public:
469  NotFilter(GeneralFilter* filterToInverse) { m_FilterToInverse = filterToInverse; }
470 
471  void parseToString(std::string& result);
472 
477  void setFilter(GeneralFilter* filterToInverse) { m_FilterToInverse = filterToInverse; }
478  };
479 
480 
481 
488  class ProtoFilter : public GeneralFilter
489  {
490  private:
491  ProtocolType m_Proto;
492  public:
498  ProtoFilter(ProtocolType proto) { m_Proto = proto; }
499 
500  void parseToString(std::string& result);
501 
507  void setProto(ProtocolType proto) { m_Proto = proto; }
508  };
509 
510 
511 
518  class ArpFilter : public GeneralFilter
519  {
520  private:
521  ArpOpcode m_OpCode;
522  public:
527  ArpFilter(ArpOpcode opCode) { m_OpCode = opCode; }
528 
529  void parseToString(std::string& result);
530 
535  void setOpCode(ArpOpcode opCode) { m_OpCode = opCode; }
536  };
537 
538 
539 
546  class VlanFilter : public GeneralFilter
547  {
548  private:
549  uint16_t m_VlanID;
550  public:
555  VlanFilter(uint16_t vlanId) : m_VlanID(vlanId) {}
556 
557  void parseToString(std::string& result);
558 
563  void setVlanID(uint16_t vlanId) { m_VlanID = vlanId; }
564  };
565 
566 
567 
574  {
575  public:
579  enum TcpFlags
580  {
582  tcpFin = 1,
584  tcpSyn = 2,
586  tcpRst = 4,
588  tcpPush = 8,
590  tcpAck = 16,
592  tcpUrg = 32
593  };
594 
600  {
605  };
606  private:
607  uint8_t m_TcpFlagsBitMask;
608  MatchOptions m_MatchOption;
609  public:
617  TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption) : m_TcpFlagsBitMask(tcpFlagBitMask), m_MatchOption(matchOption) {}
618 
625  void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption) { m_TcpFlagsBitMask = tcpFlagBitMask; m_MatchOption = matchOption; }
626 
627  void parseToString(std::string& result);
628  };
629 
630 
631 
638  {
639  private:
640  uint16_t m_WindowSize;
641  public:
648  TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op) : IFilterWithOperator(op), m_WindowSize(windowSize) {}
649 
650  void parseToString(std::string& result);
651 
656  void setWindowSize(uint16_t windowSize) { m_WindowSize = windowSize; }
657  };
658 
659 
660 
667  {
668  private:
669  uint16_t m_Length;
670  public:
677  UdpLengthFilter(uint16_t legnth, FilterOperator op) : IFilterWithOperator(op), m_Length(legnth) {}
678 
679  void parseToString(std::string& result);
680 
685  void setLength(uint16_t legnth) { m_Length = legnth; }
686  };
687 
688 } // namespace pcpp
689 
690 #endif
Definition: PcapFilter.h:59
Definition: PcapFilter.h:602
Definition: PcapFilter.h:604
IPFilter(const std::string &ipAddress, Direction dir)
Definition: PcapFilter.h:155
Definition: PcapFilter.h:586
void setOperator(FilterOperator op)
Definition: PcapFilter.h:129
Definition: PcapFilter.h:53
IpV4IDFilter(uint16_t ipID, FilterOperator op)
Definition: PcapFilter.h:219
void setEtherType(uint16_t etherType)
Definition: PcapFilter.h:378
Definition: PcapFilter.h:546
Definition: PcapFilter.h:39
ProtoFilter(ProtocolType proto)
Definition: PcapFilter.h:498
Definition: PcapFilter.h:140
Definition: PcapFilter.h:460
Definition: PcapFilter.h:588
Definition: PcapFilter.h:63
Definition: PcapFilter.h:590
ArpFilter(ArpOpcode opCode)
Definition: PcapFilter.h:527
PortFilter(uint16_t port, Direction dir)
void setTcpFlagsBitMask(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:625
void setMask(const std::string &ipv4Mask)
Definition: PcapFilter.h:192
void setMacAddress(MacAddress address)
Definition: PcapFilter.h:350
void setAddr(const std::string &ipAddress)
Definition: PcapFilter.h:186
OrFilter()
Definition: PcapFilter.h:436
Definition: PcapFilter.h:332
Definition: PcapFilter.h:637
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:448
Direction
Definition: PcapFilter.h:36
Definition: PcapFilter.h:518
Definition: PcapFilter.h:55
TcpFlags
Definition: PcapFilter.h:579
Definition: PcapFilter.h:427
The main namespace for the PcapPlusPlus lib.
void addFilter(GeneralFilter *filter)
Definition: PcapFilter.h:412
void parseToString(std::string &result)
Definition: PcapFilter.h:72
virtual void parseToString(std::string &result)=0
void setVlanID(uint16_t vlanId)
Definition: PcapFilter.h:563
void parseToString(std::string &result)
EtherTypeFilter(uint16_t etherType)
Definition: PcapFilter.h:370
ArpOpcode
Definition: ArpLayer.h:47
Definition: PcapFilter.h:573
void parseToString(std::string &result)
Definition: PcapFilter.h:57
Definition: PcapFilter.h:41
void setToPort(uint16_t toPort)
Definition: PcapFilter.h:322
void setPort(uint16_t port)
Definition: PcapFilter.h:285
Definition: PcapFilter.h:238
Definition: PcapFilter.h:116
void parseToString(std::string &result)
VlanFilter(uint16_t vlanId)
Definition: PcapFilter.h:555
Definition: PcapFilter.h:582
void parseToString(std::string &result)
void setIpID(uint16_t ipID)
Definition: PcapFilter.h:227
Definition: PcapFilter.h:391
Definition: PcapFilter.h:296
Definition: PcapFilter.h:209
void setOpCode(ArpOpcode opCode)
Definition: PcapFilter.h:535
Definition: PcapFilter.h:93
void setFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:477
Definition: MacAddress.h:21
virtual ~GeneralFilter()
MatchOptions
Definition: PcapFilter.h:599
void parseToString(std::string &result)
Definition: PcapFilter.h:361
PortRangeFilter(uint16_t fromPort, uint16_t toPort, Direction dir)
Definition: PcapFilter.h:308
void parseToString(std::string &result)
void parseToString(std::string &result)
TcpWindowSizeFilter(uint16_t windowSize, FilterOperator op)
Definition: PcapFilter.h:648
Definition: PcapFilter.h:266
void parseToString(std::string &result)
ProtocolType
Definition: ProtocolType.h:16
void setFromPort(uint16_t fromPort)
Definition: PcapFilter.h:316
IPFilter(const std::string &ipAddress, Direction dir, const std::string &ipv4Mask)
Definition: PcapFilter.h:166
FilterOperator
Definition: PcapFilter.h:50
void parseToString(std::string &result)
void parseToString(std::string &result)
Definition: PcapFilter.h:592
void parseToString(std::string &result)
Definition: PcapFilter.h:488
void parseToString(std::string &result)
void setTotalLength(uint16_t totalLength)
Definition: PcapFilter.h:256
Definition: PcapFilter.h:61
void parseToString(std::string &result)
void parseToString(std::string &result)
void setDirection(Direction dir)
Definition: PcapFilter.h:106
IpV4TotalLengthFilter(uint16_t totalLength, FilterOperator op)
Definition: PcapFilter.h:248
void setWindowSize(uint16_t windowSize)
Definition: PcapFilter.h:656
Definition: PcapFilter.h:584
IPFilter(const std::string &ipAddress, Direction dir, int len)
Definition: PcapFilter.h:177
void parseToString(std::string &result)
Definition: PcapFilter.h:43
MacAddressFilter(MacAddress address, Direction dir)
Definition: PcapFilter.h:342
TcpFlagsFilter(uint8_t tcpFlagBitMask, MatchOptions matchOption)
Definition: PcapFilter.h:617
Definition: PcapFilter.h:666
void setLength(uint16_t legnth)
Definition: PcapFilter.h:685
UdpLengthFilter(uint16_t legnth, FilterOperator op)
Definition: PcapFilter.h:677
void setLen(int len)
Definition: PcapFilter.h:198
AndFilter()
Definition: PcapFilter.h:400
NotFilter(GeneralFilter *filterToInverse)
Definition: PcapFilter.h:469
void setProto(ProtocolType proto)
Definition: PcapFilter.h:507