PcapPlusPlus  23.09
RadiusLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_RADIUS_LAYER
2 #define PACKETPP_RADIUS_LAYER
3 
4 #include "Layer.h"
5 #include "TLVData.h"
6 
8 
13 namespace pcpp
14 {
15 
20 #pragma pack(push, 1)
22  {
24  uint8_t code;
26  uint8_t id;
28  uint16_t length;
30  uint8_t authenticator[16];
31  };
32 #pragma pack(pop)
33 
34 
40  class RadiusAttribute : public TLVRecord<uint8_t, uint8_t>
41  {
42  public:
43 
48  explicit RadiusAttribute(uint8_t* attrRawData) : TLVRecord(attrRawData) { }
49 
53  virtual ~RadiusAttribute() { }
54 
55  // implement abstract methods
56 
57  size_t getTotalSize() const
58  {
59  if (m_Data == nullptr)
60  return 0;
61 
62  return (size_t)m_Data->recordLen;
63  }
64 
65  size_t getDataSize() const
66  {
67  if (m_Data == nullptr)
68  return 0;
69 
70  return (size_t)m_Data->recordLen - 2*sizeof(uint8_t);
71  }
72  };
73 
74 
81  {
82  public:
83 
91  RadiusAttributeBuilder(uint8_t attrType, const uint8_t* attrValue, uint8_t attrValueLen) :
92  TLVRecordBuilder(attrType, attrValue, attrValueLen) { }
93 
100  RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue) :
101  TLVRecordBuilder(attrType, attrValue) { }
102 
109  RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue) :
110  TLVRecordBuilder(attrType, attrValue) { }
111 
118  RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue) :
119  TLVRecordBuilder(attrType, attrValue) { }
120 
127  RadiusAttributeBuilder(uint8_t attrType, const IPv4Address& attrValue) :
128  TLVRecordBuilder(attrType, attrValue) { }
129 
136  RadiusAttributeBuilder(uint8_t attrType, const std::string& attrValue) :
137  TLVRecordBuilder(attrType, attrValue) { }
138 
144  TLVRecordBuilder(other) { }
145 
151  {
152  TLVRecordBuilder::operator=(other);
153  return *this;
154  }
155 
160  RadiusAttribute build() const;
161  };
162 
163 
168  class RadiusLayer : public Layer
169  {
170  private:
171 
172  TLVRecordReader<RadiusAttribute> m_AttributeReader;
173 
174  uint8_t* getAttributesBasePtr() const { return m_Data + sizeof(radius_header); }
175 
176  RadiusAttribute addAttrAt(const RadiusAttributeBuilder& attrBuilder, int offset);
177 
178  public:
179 
187  RadiusLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) :
188  Layer(data, dataLen, prevLayer, packet)
189  { m_Protocol = Radius; }
190 
200  RadiusLayer(uint8_t code, uint8_t id, const uint8_t* authenticator, uint8_t authenticatorArrSize);
201 
211  RadiusLayer(uint8_t code, uint8_t id, const std::string &authenticator);
212 
217 
222  radius_header* getRadiusHeader() const { return (radius_header*)m_Data; }
223 
227  std::string getAuthenticatorValue() const;
228 
233  void setAuthenticatorValue(const std::string& authValue);
234 
241  static std::string getRadiusMessageString(uint8_t radiusMessageCode);
242 
247  RadiusAttribute getFirstAttribute() const;
248 
256  RadiusAttribute getNextAttribute(RadiusAttribute& attr) const;
257 
264  RadiusAttribute getAttribute(uint8_t attrType) const;
265 
269  size_t getAttributeCount() const;
270 
277  RadiusAttribute addAttribute(const RadiusAttributeBuilder& attrBuilder);
278 
286  RadiusAttribute addAttributeAfter(const RadiusAttributeBuilder& attrBuilder, uint8_t prevAttrType);
287 
293  bool removeAttribute(uint8_t attrType);
294 
299  bool removeAllAttributes();
300 
307  static bool isDataValid(const uint8_t* udpData, size_t udpDataLen);
308 
313  static inline bool isRadiusPort(uint16_t port);
314 
315  // implement abstract methods
316 
320  size_t getHeaderLen() const;
321 
325  void parseNextLayer() {}
326 
330  void computeCalculateFields();
331 
332  std::string toString() const;
333 
335  };
336 
337 
338  // implementation of inline methods
339 
340  bool RadiusLayer::isRadiusPort(uint16_t port)
341  {
342  switch (port)
343  {
344  case 1812:
345  case 1813:
346  case 3799:
347  return true;
348  default:
349  return false;
350  }
351  } // isRadiusPort
352 
353 } // namespace pcpp
354 
355 #endif // PACKETPP_RADIUS_LAYER
uint8_t code
Definition: RadiusLayer.h:24
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue)
Definition: RadiusLayer.h:118
RadiusAttributeBuilder(uint8_t attrType, const std::string &attrValue)
Definition: RadiusLayer.h:136
uint8_t id
Definition: RadiusLayer.h:26
Definition: ProtocolType.h:348
Definition: TLVData.h:376
Definition: Layer.h:70
Definition: TLVData.h:24
Definition: Packet.h:26
radius_header * getRadiusHeader() const
Definition: RadiusLayer.h:222
RadiusLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: RadiusLayer.h:187
RadiusAttributeBuilder(uint8_t attrType, const uint8_t *attrValue, uint8_t attrValueLen)
Definition: RadiusLayer.h:91
size_t getTotalSize() const
Definition: RadiusLayer.h:57
Definition: RadiusLayer.h:40
RadiusAttributeBuilder(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:143
Definition: TLVData.h:217
RadiusAttributeBuilder & operator=(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:150
const ProtocolType Radius
Definition: ProtocolType.h:208
static bool isRadiusPort(uint16_t port)
Definition: RadiusLayer.h:340
uint8_t authenticator[16]
Definition: RadiusLayer.h:30
uint16_t length
Definition: RadiusLayer.h:28
~RadiusLayer()
Definition: RadiusLayer.h:216
Definition: IpAddress.h:42
virtual ~RadiusAttribute()
Definition: RadiusLayer.h:53
RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue)
Definition: RadiusLayer.h:100
RadiusAttributeBuilder(uint8_t attrType, const IPv4Address &attrValue)
Definition: RadiusLayer.h:127
RadiusAttribute(uint8_t *attrRawData)
Definition: RadiusLayer.h:48
OsiModelLayer getOsiModelLayer() const
Definition: RadiusLayer.h:334
Definition: RadiusLayer.h:21
Definition: RadiusLayer.h:80
size_t getDataSize() const
Definition: RadiusLayer.h:65
RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue)
Definition: RadiusLayer.h:109
Definition: RadiusLayer.h:168
void parseNextLayer()
Definition: RadiusLayer.h:325