Kea: radius_accounting.h Source File

| Kea 3.2.0-git

radius_accounting.h
Go to the documentation of this file.
1// Copyright (C) 2020-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef RADIUS_ACCOUNTING_H
8#define RADIUS_ACCOUNTING_H
9
10#include <util/csv_file.h>
11#include <dhcpsrv/lease.h>
12#include <radius_service.h>
13#include <radius_request.h>
14#include <boost/multi_index_container.hpp>
15#include <boost/multi_index/member.hpp>
16#include <boost/multi_index/ordered_index.hpp>
17#include <boost/date_time/gregorian/gregorian_types.hpp>
18#include <boost/date_time/posix_time/posix_time_types.hpp>
19#include <mutex>
20
21namespace isc {
22namespace radius {
23
25 enum Event : uint16_t {
26 EVENT_CREATE = 0, //< A new lease was created (leaseX_select hooks).
27 EVENT_RENEW = 1, //< A lease was renewed (leaseX_renew hooks).
28 EVENT_REBIND = 2, //< A lease was rebound (lease6_rebind hook).
29 EVENT_EXPIRE = 3, //< A lease was expired (leaseX_expire hooks).
30 EVENT_RELEASE = 4, //< A lease was released (leaseX_release hooks).
31 EVENT_DECLINE = 5, //< A lease was declined (leaseX_decline hooks).
32 EVENT_ADD = 6, //< A command added a lease (command_processed hook).
33 EVENT_UPDATE = 7, //< A command updated a lease (command_processed hook).
34 EVENT_DEL = 8, //< A command deleted a lease (command_processed hook).
35};
36
41std::string eventToText(Event event);
42
48 class LeaseTS {
49public:
50
55 LeaseTS(const asiolink::IOAddress& addr,
56 const boost::posix_time::ptime& timestamp)
57 : addr_(addr), timestamp_(timestamp) {
58 }
59
61 const asiolink::IOAddress addr_;
62
64 const boost::posix_time::ptime timestamp_;
65};
66
68 struct TMAddressIndexTag { };
69
71 struct TMTimestampIndexTag { };
72
74typedef boost::multi_index_container<
75
77 LeaseTS,
78
79 // Start specification of indexes here.
80 boost::multi_index::indexed_by<
81
82 // First index is used to search by an address.
83 boost::multi_index::ordered_unique<
84 boost::multi_index::tag<TMAddressIndexTag>,
85 boost::multi_index::member<LeaseTS,
86 const asiolink::IOAddress,
87 &LeaseTS::addr_>
88 >,
89
90 // Second index is used to search by timestamp.
91 boost::multi_index::ordered_non_unique<
92 boost::multi_index::tag<TMTimestampIndexTag>,
93 boost::multi_index::member<LeaseTS,
94 const boost::posix_time::ptime,
95 &LeaseTS::timestamp_>
96 >
97 >
98> TMContainer;
99
101 typedef TMContainer::index<TMAddressIndexTag>::type TMContainerAddressIndex;
102
104 typedef TMContainer::index<TMTimestampIndexTag>::type TMContainerTimestampIndex;
105
109 class RadiusAcctEnv {
110public:
111
118 RadiusAcctEnv(std::string session_id, Event event, uint32_t subnet_id,
119 AttributesPtr send_attrs);
120
122 std::string session_id_;
123
125 Event event_;
126
128 uint32_t subnet_id_;
129
131 AttributesPtr send_attrs_;
132
134 bool finished_;
135};
136
138 class RadiusAcctHandler {
139public:
140
145 RadiusAcctHandler(RadiusAcctEnv env, const CallbackAcct& callback);
146
148 virtual ~RadiusAcctHandler();
149
151 void start();
152
154 static size_t getCounter();
155
157 RadiusAcctEnv env_;
158
159protected:
160
162 RadiusAsyncAcctPtr acct_;
163
164private:
165
167 static size_t counter_;
168
170 static std::mutex mutex_;
171};
172
174 typedef boost::shared_ptr<RadiusAcctHandler> RadiusAcctHandlerPtr;
175
177 typedef boost::shared_ptr<util::CSVFile> CSVFilePtr;
178
180 class RadiusAccounting : public RadiusService {
181public:
182
184 RadiusAccounting();
185
187 virtual ~RadiusAccounting() = default;
188
195 void init(const std::string& filename);
196
214 RadiusAcctHandlerPtr buildAcct(const dhcp::Lease4Ptr& lease, Event event);
215
232 RadiusAcctHandlerPtr buildAcct(const dhcp::Lease6Ptr& lease, Event event);
233
241 RadiusAcctHandlerPtr buildAcct4(const data::ConstElementPtr& arguments,
242 Event event);
243
251 RadiusAcctHandlerPtr buildAcct6(const data::ConstElementPtr& arguments,
252 Event event);
253
257 static void runAsync(RadiusAcctHandlerPtr handler);
258
263 static void terminate(RadiusAcctEnv env, int result);
264
270 boost::posix_time::ptime
271 getCreateTimestamp(const asiolink::IOAddress& addr, bool generate);
272
276 void eraseCreateTimestamp(const asiolink::IOAddress& addr);
277
281 bool loadFromFile();
282
287 void storeToFile();
288
292 void setIdleTimer();
293
295 static void IdleTimerCallback();
296
297protected:
298
300 std::string filename_;
301
303 TMContainer container_;
304
308 const boost::posix_time::ptime epoch_;
309
311 CSVFilePtr file_;
312
317 size_t record_count_;
318
320 std::mutex mutex_;
321};
322
323} // end of namespace isc::radius
324} // end of namespace isc
325
326#endif
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::radius::LeaseTS
Create timestamp entry.
Definition radius_accounting.h:48
isc::radius::LeaseTS::addr_
const asiolink::IOAddress addr_
Lease address.
Definition radius_accounting.h:61
isc::radius::LeaseTS::timestamp_
const boost::posix_time::ptime timestamp_
Create timestamp.
Definition radius_accounting.h:64
isc::radius::LeaseTS::LeaseTS
LeaseTS(const asiolink::IOAddress &addr, const boost::posix_time::ptime &timestamp)
Constructor.
Definition radius_accounting.h:55
isc::radius::RadiusAccounting::buildAcct6
RadiusAcctHandlerPtr buildAcct6(const data::ConstElementPtr &arguments, Event event)
Build RadiusAcct handler for Accounting-Request.
Definition radius_accounting.cc:571
isc::radius::RadiusAccounting::setIdleTimer
void setIdleTimer()
Set idle timer.
Definition radius_accounting.cc:991
isc::radius::RadiusAccounting::container_
TMContainer container_
The Create timestamp container which holds session history.
Definition radius_accounting.h:303
isc::radius::RadiusAccounting::buildAcct4
RadiusAcctHandlerPtr buildAcct4(const data::ConstElementPtr &arguments, Event event)
Build RadiusAcct handler for Accounting-Request.
Definition radius_accounting.cc:403
isc::radius::RadiusAccounting::eraseCreateTimestamp
void eraseCreateTimestamp(const asiolink::IOAddress &addr)
Erase create-timestamp entry to session history.
Definition radius_accounting.cc:823
isc::radius::RadiusAccounting::file_
CSVFilePtr file_
Pointer to the CSVFile.
Definition radius_accounting.h:311
isc::radius::RadiusAccounting::init
void init(const std::string &filename)
Initialize.
Definition radius_accounting.cc:103
isc::radius::RadiusAccounting::loadFromFile
bool loadFromFile()
Load create-timestamp entries from file.
Definition radius_accounting.cc:853
isc::radius::RadiusAccounting::runAsync
static void runAsync(RadiusAcctHandlerPtr handler)
Run asynchronously.
Definition radius_accounting.cc:752
isc::radius::RadiusAccounting::mutex_
std::mutex mutex_
Mutex to protect access to container_ and file_.
Definition radius_accounting.h:320
isc::radius::RadiusAccounting::RadiusAccounting
RadiusAccounting()
Constructor.
Definition radius_accounting.cc:98
isc::radius::RadiusAccounting::storeToFile
void storeToFile()
Store create-timestamp entries to a file.
Definition radius_accounting.cc:922
isc::radius::RadiusAccounting::record_count_
size_t record_count_
New record counter.
Definition radius_accounting.h:317
isc::radius::RadiusAccounting::buildAcct
RadiusAcctHandlerPtr buildAcct(const dhcp::Lease4Ptr &lease, Event event)
Build RadiusAcct handler for Accounting-Request - IPv4.
Definition radius_accounting.cc:131
isc::radius::RadiusAccounting::terminate
static void terminate(RadiusAcctEnv env, int result)
Termination callback.
Definition radius_accounting.cc:757
isc::radius::RadiusAccounting::epoch_
const boost::posix_time::ptime epoch_
Epoch to avoid too long values.
Definition radius_accounting.h:308
isc::radius::RadiusAccounting::IdleTimerCallback
static void IdleTimerCallback()
Idle timer callback.
Definition radius_accounting.cc:1008
isc::radius::RadiusAccounting::getCreateTimestamp
boost::posix_time::ptime getCreateTimestamp(const asiolink::IOAddress &addr, bool generate)
Get lease create-timestamp entry from session history.
Definition radius_accounting.cc:770
isc::radius::RadiusAccounting::filename_
std::string filename_
Create timestamps file name.
Definition radius_accounting.h:300
isc::radius::RadiusAccounting::~RadiusAccounting
virtual ~RadiusAccounting()=default
Destructor.
isc::radius::RadiusAcctEnv
Class of Radius accounting environments.
Definition radius_accounting.h:109
isc::radius::RadiusAcctEnv::RadiusAcctEnv
RadiusAcctEnv(std::string session_id, Event event, uint32_t subnet_id, AttributesPtr send_attrs)
Constructor.
Definition radius_accounting.cc:60
isc::radius::RadiusAcctEnv::session_id_
std::string session_id_
Session Id.
Definition radius_accounting.h:122
isc::radius::RadiusAcctEnv::finished_
bool finished_
Termination flag.
Definition radius_accounting.h:134
isc::radius::RadiusAcctEnv::send_attrs_
AttributesPtr send_attrs_
Attributes to send.
Definition radius_accounting.h:131
isc::radius::RadiusAcctEnv::subnet_id_
uint32_t subnet_id_
Subnet Id (aka client/NAS port).
Definition radius_accounting.h:128
isc::radius::RadiusAcctEnv::event_
Event event_
Event.
Definition radius_accounting.h:125
isc::radius::RadiusAcctHandler::RadiusAcctHandler
RadiusAcctHandler(RadiusAcctEnv env, const CallbackAcct &callback)
Constructor.
Definition radius_accounting.cc:67
isc::radius::RadiusAcctHandler::getCounter
static size_t getCounter()
Get instance counter.
Definition radius_accounting.cc:89
isc::radius::RadiusAcctHandler::env_
RadiusAcctEnv env_
Environment.
Definition radius_accounting.h:157
isc::radius::RadiusAcctHandler::start
void start()
Start communication.
Definition radius_accounting.cc:84
isc::radius::RadiusAcctHandler::~RadiusAcctHandler
virtual ~RadiusAcctHandler()
Destructor.
Definition radius_accounting.cc:76
isc::radius::RadiusAcctHandler::acct_
RadiusAsyncAcctPtr acct_
Pointer to the communication class.
Definition radius_accounting.h:162
isc::radius::RadiusService::RadiusService
RadiusService(const std::string &name)
Constructor.
Definition radius_service.cc:23
csv_file.h
lease.h
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::dhcp::Lease6Ptr
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:528
isc::dhcp::Lease4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
isc::radius
Definition cfg_attribute.cc:21
isc::radius::RadiusAcctHandlerPtr
boost::shared_ptr< RadiusAcctHandler > RadiusAcctHandlerPtr
Type of pointers to Radius accounting communication handler.
Definition radius_accounting.h:174
isc::radius::TMContainerAddressIndex
TMContainer::index< TMAddressIndexTag >::type TMContainerAddressIndex
First index type in the TMContainer.
Definition radius_accounting.h:101
isc::radius::AttributesPtr
boost::shared_ptr< Attributes > AttributesPtr
Shared pointers to attribute collection.
Definition client_attribute.h:963
isc::radius::TMContainer
boost::multi_index_container< LeaseTS, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< TMAddressIndexTag >, boost::multi_index::member< LeaseTS, const asiolink::IOAddress, &LeaseTS::addr_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< TMTimestampIndexTag >, boost::multi_index::member< LeaseTS, const boost::posix_time::ptime, &LeaseTS::timestamp_ > > > > TMContainer
Create timestamp container.
Definition radius_accounting.h:98
isc::radius::CallbackAcct
std::function< void(int)> CallbackAcct
Type of callback for accounting termination.
Definition radius_request.h:31
isc::radius::TMContainerTimestampIndex
TMContainer::index< TMTimestampIndexTag >::type TMContainerTimestampIndex
Second index type in the TMContainer.
Definition radius_accounting.h:104
isc::radius::Event
Event
Type of accounting events.
Definition radius_accounting.h:25
isc::radius::EVENT_DECLINE
@ EVENT_DECLINE
Definition radius_accounting.h:31
isc::radius::EVENT_UPDATE
@ EVENT_UPDATE
Definition radius_accounting.h:33
isc::radius::EVENT_EXPIRE
@ EVENT_EXPIRE
Definition radius_accounting.h:29
isc::radius::EVENT_ADD
@ EVENT_ADD
Definition radius_accounting.h:32
isc::radius::EVENT_RELEASE
@ EVENT_RELEASE
Definition radius_accounting.h:30
isc::radius::EVENT_CREATE
@ EVENT_CREATE
Definition radius_accounting.h:26
isc::radius::EVENT_DEL
@ EVENT_DEL
Definition radius_accounting.h:34
isc::radius::EVENT_RENEW
@ EVENT_RENEW
Definition radius_accounting.h:27
isc::radius::EVENT_REBIND
@ EVENT_REBIND
Definition radius_accounting.h:28
isc::radius::CSVFilePtr
boost::shared_ptr< util::CSVFile > CSVFilePtr
Type of pointers to CSV file.
Definition radius_accounting.h:177
isc::radius::RadiusAsyncAcctPtr
boost::shared_ptr< RadiusAsyncAcct > RadiusAsyncAcctPtr
Pointer to asynchronous accounting.
Definition radius_request.h:279
isc::radius::eventToText
string eventToText(Event event)
Translate an event to text.
Definition radius_accounting.cc:35
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
radius_request.h
radius_service.h
isc::radius::TMAddressIndexTag
Tag for the index for searching by address.
Definition radius_accounting.h:68
isc::radius::TMTimestampIndexTag
Tag for the index for searching by timestamp.
Definition radius_accounting.h:71