PcapPlusPlus  Next
RadiusLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "TLVData.h"
5 
7 
10 namespace pcpp
11 {
14 #pragma pack(push, 1)
16  {
18  uint8_t code;
20  uint8_t id;
22  uint16_t length;
24  uint8_t authenticator[16];
25  };
26 #pragma pack(pop)
27  static_assert(sizeof(radius_header) == 20, "radius_header size is not 20 bytes");
28 
32  class RadiusAttribute : public TLVRecord<uint8_t, uint8_t>
33  {
34  public:
37  explicit RadiusAttribute(uint8_t* attrRawData) : TLVRecord(attrRawData)
38  {}
39 
41  ~RadiusAttribute() override = default;
42 
43  // implement abstract methods
44 
45  size_t getTotalSize() const override
46  {
47  if (m_Data == nullptr)
48  return 0;
49 
50  return static_cast<size_t>(m_Data->recordLen);
51  }
52 
53  size_t getDataSize() const override
54  {
55  if (m_Data == nullptr)
56  return 0;
57 
58  return static_cast<size_t>(m_Data->recordLen) - 2 * sizeof(uint8_t);
59  }
60  };
61 
66  {
67  public:
74  RadiusAttributeBuilder(uint8_t attrType, const uint8_t* attrValue, uint8_t attrValueLen)
75  : TLVRecordBuilder(attrType, attrValue, attrValueLen)
76  {}
77 
82  RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue) : TLVRecordBuilder(attrType, attrValue)
83  {}
84 
89  RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue) : TLVRecordBuilder(attrType, attrValue)
90  {}
91 
96  RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue) : TLVRecordBuilder(attrType, attrValue)
97  {}
98 
103  RadiusAttributeBuilder(uint8_t attrType, const IPv4Address& attrValue) : TLVRecordBuilder(attrType, attrValue)
104  {}
105 
110  RadiusAttributeBuilder(uint8_t attrType, const std::string& attrValue) : TLVRecordBuilder(attrType, attrValue)
111  {}
112 
116  {}
117 
121  {
122  TLVRecordBuilder::operator=(other);
123  return *this;
124  }
125 
129  };
130 
133  class RadiusLayer : public Layer
134  {
135  private:
136  TLVRecordReader<RadiusAttribute> m_AttributeReader;
137 
138  uint8_t* getAttributesBasePtr() const
139  {
140  return m_Data + sizeof(radius_header);
141  }
142 
143  RadiusAttribute addAttrAt(const RadiusAttributeBuilder& attrBuilder, int offset);
144 
145  public:
151  RadiusLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
152  : Layer(data, dataLen, prevLayer, packet, Radius)
153  {}
154 
163  RadiusLayer(uint8_t code, uint8_t id, const uint8_t* authenticator, uint8_t authenticatorArrSize);
164 
172  RadiusLayer(uint8_t code, uint8_t id, const std::string& authenticator);
173 
175  ~RadiusLayer() override = default;
176 
181  {
182  return reinterpret_cast<radius_header*>(m_Data);
183  }
184 
186  std::string getAuthenticatorValue() const;
187 
190  void setAuthenticatorValue(const std::string& authValue);
191 
196  static std::string getRadiusMessageString(uint8_t radiusMessageCode);
197 
201 
208 
213  RadiusAttribute getAttribute(uint8_t attrType) const;
214 
216  size_t getAttributeCount() const;
217 
223 
229  RadiusAttribute addAttributeAfter(const RadiusAttributeBuilder& attrBuilder, uint8_t prevAttrType);
230 
235  bool removeAttribute(uint8_t attrType);
236 
240 
245  static bool isDataValid(const uint8_t* udpData, size_t udpDataLen);
246 
249  static inline bool isRadiusPort(uint16_t port);
250 
251  // implement abstract methods
252 
254  size_t getHeaderLen() const override;
255 
257  void parseNextLayer() override
258  {}
259 
261  void computeCalculateFields() override;
262 
263  std::string toString() const override;
264 
266  {
268  }
269  };
270 
271  // implementation of inline methods
272 
273  bool RadiusLayer::isRadiusPort(uint16_t port)
274  {
275  switch (port)
276  {
277  case 1812:
278  case 1813:
279  case 3799:
280  return true;
281  default:
282  return false;
283  }
284  } // isRadiusPort
285 
286 } // namespace pcpp
Definition: IpAddress.h:28
Definition: Layer.h:60
Definition: Packet.h:22
Definition: RadiusLayer.h:66
RadiusAttributeBuilder & operator=(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:120
RadiusAttribute build() const
RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue)
Definition: RadiusLayer.h:82
RadiusAttributeBuilder(uint8_t attrType, const std::string &attrValue)
Definition: RadiusLayer.h:110
RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue)
Definition: RadiusLayer.h:96
RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue)
Definition: RadiusLayer.h:89
RadiusAttributeBuilder(uint8_t attrType, const IPv4Address &attrValue)
Definition: RadiusLayer.h:103
RadiusAttributeBuilder(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:115
RadiusAttributeBuilder(uint8_t attrType, const uint8_t *attrValue, uint8_t attrValueLen)
Definition: RadiusLayer.h:74
Definition: RadiusLayer.h:33
~RadiusAttribute() override=default
A d'tor for this class, currently does nothing.
RadiusAttribute(uint8_t *attrRawData)
Definition: RadiusLayer.h:37
size_t getTotalSize() const override
Definition: RadiusLayer.h:45
size_t getDataSize() const override
Definition: RadiusLayer.h:53
Definition: RadiusLayer.h:134
size_t getHeaderLen() const override
RadiusAttribute getAttribute(uint8_t attrType) const
bool removeAllAttributes()
RadiusAttribute getNextAttribute(RadiusAttribute &attr) const
radius_header * getRadiusHeader() const
Definition: RadiusLayer.h:180
RadiusLayer(uint8_t code, uint8_t id, const uint8_t *authenticator, uint8_t authenticatorArrSize)
~RadiusLayer() override=default
A d'tor for this layer, currently does nothing.
RadiusAttribute getFirstAttribute() const
static bool isDataValid(const uint8_t *udpData, size_t udpDataLen)
RadiusAttribute addAttributeAfter(const RadiusAttributeBuilder &attrBuilder, uint8_t prevAttrType)
std::string getAuthenticatorValue() const
void setAuthenticatorValue(const std::string &authValue)
RadiusLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: RadiusLayer.h:151
RadiusAttribute addAttribute(const RadiusAttributeBuilder &attrBuilder)
size_t getAttributeCount() const
std::string toString() const override
bool removeAttribute(uint8_t attrType)
void parseNextLayer() override
Does nothing for this layer, RADIUS is always last.
Definition: RadiusLayer.h:257
static std::string getRadiusMessageString(uint8_t radiusMessageCode)
OsiModelLayer getOsiModelLayer() const override
Definition: RadiusLayer.h:265
RadiusLayer(uint8_t code, uint8_t id, const std::string &authenticator)
void computeCalculateFields() override
Calculate and store the value of radius_header::length according to the layer size.
static bool isRadiusPort(uint16_t port)
Definition: RadiusLayer.h:273
Definition: TLVData.h:357
Definition: TLVData.h:19
Definition: TLVData.h:204
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:239
const ProtocolType Radius
RADIUS protocol.
Definition: ProtocolType.h:131
Definition: RadiusLayer.h:16
uint8_t authenticator[16]
Used to authenticate the reply from the RADIUS server and to encrypt passwords.
Definition: RadiusLayer.h:24
uint16_t length
RADIUS message length.
Definition: RadiusLayer.h:22
uint8_t id
RADIUS message ID.
Definition: RadiusLayer.h:20
uint8_t code
RADIUS message code.
Definition: RadiusLayer.h:18