PcapPlusPlus  21.05
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
41  {
42  public:
43 
48  RadiusAttribute(uint8_t* attrRawData) : TLVRecord(attrRawData) { }
49 
53  virtual ~RadiusAttribute() { }
54 
55  // implement abstract methods
56 
57  size_t getTotalSize() const
58  {
59  return (size_t)m_Data->recordLen;
60  }
61 
62  size_t getDataSize() const
63  {
64  return (size_t)m_Data->recordLen - 2*sizeof(uint8_t);
65  }
66  };
67 
68 
75  {
76  public:
77 
85  RadiusAttributeBuilder(uint8_t attrType, const uint8_t* attrValue, uint8_t attrValueLen) :
86  TLVRecordBuilder(attrType, attrValue, attrValueLen) { }
87 
94  RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue) :
95  TLVRecordBuilder(attrType, attrValue) { }
96 
103  RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue) :
104  TLVRecordBuilder(attrType, attrValue) { }
105 
112  RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue) :
113  TLVRecordBuilder(attrType, attrValue) { }
114 
121  RadiusAttributeBuilder(uint8_t attrType, const IPv4Address& attrValue) :
122  TLVRecordBuilder(attrType, attrValue) { }
123 
130  RadiusAttributeBuilder(uint8_t attrType, const std::string& attrValue) :
131  TLVRecordBuilder(attrType, attrValue) { }
132 
138  TLVRecordBuilder(other) { }
139 
145  {
146  TLVRecordBuilder::operator=(other);
147  return *this;
148  }
149 
154  RadiusAttribute build() const;
155  };
156 
157 
162  class RadiusLayer : public Layer
163  {
164  private:
165 
166  TLVRecordReader<RadiusAttribute> m_AttributeReader;
167 
168  uint8_t* getAttributesBasePtr() const { return m_Data + sizeof(radius_header); }
169 
170  RadiusAttribute addAttrAt(const RadiusAttributeBuilder& attrBuilder, int offset);
171 
172  public:
173 
181  RadiusLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) :
182  Layer(data, dataLen, prevLayer, packet)
183  { m_Protocol = Radius; }
184 
194  RadiusLayer(uint8_t code, uint8_t id, const uint8_t* authenticator, uint8_t authenticatorArrSize);
195 
205  RadiusLayer(uint8_t code, uint8_t id, const std::string authenticator);
206 
211 
216  radius_header* getRadiusHeader() const { return (radius_header*)m_Data; }
217 
221  std::string getAuthenticatorValue() const;
222 
227  void setAuthenticatorValue(const std::string& authValue);
228 
235  static std::string getRadiusMessageString(uint8_t radiusMessageCode);
236 
241  RadiusAttribute getFirstAttribute() const;
242 
250  RadiusAttribute getNextAttribute(RadiusAttribute& attr) const;
251 
258  RadiusAttribute getAttribute(uint8_t attrType) const;
259 
263  size_t getAttributeCount() const;
264 
271  RadiusAttribute addAttribute(const RadiusAttributeBuilder& attrBuilder);
272 
280  RadiusAttribute addAttributeAfter(const RadiusAttributeBuilder& attrBuilder, uint8_t prevAttrType);
281 
287  bool removeAttribute(uint8_t attrType);
288 
293  bool removeAllAttributes();
294 
301  static bool isDataValid(const uint8_t* udpData, size_t udpDataLen);
302 
307  static inline bool isRadiusPort(uint16_t port);
308 
309  // implement abstract methods
310 
314  size_t getHeaderLen() const;
315 
319  void parseNextLayer() {}
320 
324  void computeCalculateFields();
325 
326  std::string toString() const;
327 
329  };
330 
331 
332  // implementation of inline methods
333 
334  bool RadiusLayer::isRadiusPort(uint16_t port)
335  {
336  switch (port)
337  {
338  case 1812:
339  case 1813:
340  case 3799:
341  return true;
342  default:
343  return false;
344  }
345  } // isRadiusPort
346 
347 } // namespace pcpp
348 
349 #endif // PACKETPP_RADIUS_LAYER
uint8_t code
Definition: RadiusLayer.h:24
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:253
RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue)
Definition: RadiusLayer.h:112
RadiusAttributeBuilder(uint8_t attrType, const std::string &attrValue)
Definition: RadiusLayer.h:130
uint8_t id
Definition: RadiusLayer.h:26
Definition: TLVData.h:351
Definition: Layer.h:70
Definition: TLVData.h:23
Definition: Packet.h:26
radius_header * getRadiusHeader() const
Definition: RadiusLayer.h:216
RadiusLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: RadiusLayer.h:181
RadiusAttributeBuilder(uint8_t attrType, const uint8_t *attrValue, uint8_t attrValueLen)
Definition: RadiusLayer.h:85
size_t getTotalSize() const
Definition: RadiusLayer.h:57
Definition: RadiusLayer.h:40
RadiusAttributeBuilder(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:137
Definition: TLVData.h:197
RadiusAttributeBuilder & operator=(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:144
const ProtocolType Radius
Definition: ProtocolType.h:208
static bool isRadiusPort(uint16_t port)
Definition: RadiusLayer.h:334
uint8_t authenticator[16]
Definition: RadiusLayer.h:30
uint16_t length
Definition: RadiusLayer.h:28
~RadiusLayer()
Definition: RadiusLayer.h:210
Definition: IpAddress.h:26
virtual ~RadiusAttribute()
Definition: RadiusLayer.h:53
RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue)
Definition: RadiusLayer.h:94
RadiusAttributeBuilder(uint8_t attrType, const IPv4Address &attrValue)
Definition: RadiusLayer.h:121
RadiusAttribute(uint8_t *attrRawData)
Definition: RadiusLayer.h:48
OsiModelLayer getOsiModelLayer() const
Definition: RadiusLayer.h:328
Definition: ProtocolType.h:264
Definition: RadiusLayer.h:21
Definition: RadiusLayer.h:74
size_t getDataSize() const
Definition: RadiusLayer.h:62
RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue)
Definition: RadiusLayer.h:103
Definition: RadiusLayer.h:162
void parseNextLayer()
Definition: RadiusLayer.h:319