Kea 3.3.3
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 <dhcpsrv/lease.h>
13#include <util/csv_file.h>
14
15#include <cstdint>
16#include <mutex>
17
18#include <boost/date_time/gregorian/gregorian_types.hpp>
19#include <boost/date_time/posix_time/posix_time_types.hpp>
20#include <boost/multi_index/member.hpp>
21#include <boost/multi_index/ordered_index.hpp>
22#include <boost/multi_index_container.hpp>
23
24namespace isc {
25namespace radius {
26
28enum Event : uint16_t {
29 EVENT_CREATE = 0, //< A new lease was created (leaseX_select hooks).
30 EVENT_RENEW = 1, //< A lease was renewed (leaseX_renew hooks).
31 EVENT_REBIND = 2, //< A lease was rebound (lease6_rebind hook).
32 EVENT_EXPIRE = 3, //< A lease was expired (leaseX_expire hooks).
33 EVENT_RELEASE = 4, //< A lease was released (leaseX_release hooks).
34 EVENT_DECLINE = 5, //< A lease was declined (leaseX_decline hooks).
35 EVENT_ADD = 6, //< A command added a lease (command_processed hook).
36 EVENT_UPDATE = 7, //< A command updated a lease (command_processed hook).
37 EVENT_DEL = 8, //< A command deleted a lease (command_processed hook).
38};
39
44std::string eventToText(Event event);
45
51class LeaseTS {
52public:
53
59 const boost::posix_time::ptime& timestamp)
60 : addr_(addr), timestamp_(timestamp) {
61 }
62
65
67 const boost::posix_time::ptime timestamp_;
68};
69
72
75
77typedef boost::multi_index_container<
78
80 LeaseTS,
81
82 // Start specification of indexes here.
83 boost::multi_index::indexed_by<
84
85 // First index is used to search by an address.
86 boost::multi_index::ordered_unique<
87 boost::multi_index::tag<TMAddressIndexTag>,
88 boost::multi_index::member<LeaseTS,
91 >,
92
93 // Second index is used to search by timestamp.
94 boost::multi_index::ordered_non_unique<
95 boost::multi_index::tag<TMTimestampIndexTag>,
96 boost::multi_index::member<LeaseTS,
97 const boost::posix_time::ptime,
99 >
100 >
102
104typedef TMContainer::index<TMAddressIndexTag>::type TMContainerAddressIndex;
105
107typedef TMContainer::index<TMTimestampIndexTag>::type TMContainerTimestampIndex;
108
113public:
114
121 RadiusAcctEnv(std::string session_id, Event event, uint32_t subnet_id,
122 AttributesPtr send_attrs);
123
125 std::string session_id_;
126
129
131 uint32_t subnet_id_;
132
135
138};
139
142public:
143
148 RadiusAcctHandler(RadiusAcctEnv env, const CallbackAcct& callback);
149
151 virtual ~RadiusAcctHandler();
152
154 void start();
155
157 static size_t getCounter();
158
161
162protected:
163
166
167private:
168
170 static size_t counter_;
171
173 static std::mutex mutex_;
174};
175
177typedef boost::shared_ptr<RadiusAcctHandler> RadiusAcctHandlerPtr;
178
180typedef boost::shared_ptr<util::CSVFile> CSVFilePtr;
181
184public:
185
188
190 virtual ~RadiusAccounting() = default;
191
198 void init(const std::string& filename);
199
218
236
245 Event event);
246
255 Event event);
256
260 static void runAsync(RadiusAcctHandlerPtr handler);
261
266 static void terminate(RadiusAcctEnv env, int result);
267
273 boost::posix_time::ptime
274 getCreateTimestamp(const asiolink::IOAddress& addr, bool generate);
275
280
284 bool loadFromFile();
285
290 void storeToFile();
291
295 void setIdleTimer();
296
298 static void IdleTimerCallback();
299
300protected:
301
303 std::string filename_;
304
307
311 const boost::posix_time::ptime epoch_;
312
315
321
323 std::mutex mutex_;
324};
325
326} // end of namespace isc::radius
327} // end of namespace isc
328
329#endif
Create timestamp entry.
const asiolink::IOAddress addr_
Lease address.
const boost::posix_time::ptime timestamp_
Create timestamp.
LeaseTS(const asiolink::IOAddress &addr, const boost::posix_time::ptime &timestamp)
Constructor.
RadiusAcctHandlerPtr buildAcct6(const data::ConstElementPtr &arguments, Event event)
Build RadiusAcct handler for Accounting-Request.
TMContainer container_
The Create timestamp container which holds session history.
RadiusAcctHandlerPtr buildAcct4(const data::ConstElementPtr &arguments, Event event)
Build RadiusAcct handler for Accounting-Request.
void eraseCreateTimestamp(const asiolink::IOAddress &addr)
Erase create-timestamp entry to session history.
CSVFilePtr file_
Pointer to the CSVFile.
void init(const std::string &filename)
Initialize.
bool loadFromFile()
Load create-timestamp entries from file.
static void runAsync(RadiusAcctHandlerPtr handler)
Run asynchronously.
std::mutex mutex_
Mutex to protect access to container_ and file_.
void storeToFile()
Store create-timestamp entries to a file.
size_t record_count_
New record counter.
RadiusAcctHandlerPtr buildAcct(const dhcp::Lease4Ptr &lease, Event event)
Build RadiusAcct handler for Accounting-Request - IPv4.
static void terminate(RadiusAcctEnv env, int result)
Termination callback.
const boost::posix_time::ptime epoch_
Epoch to avoid too long values.
static void IdleTimerCallback()
Idle timer callback.
boost::posix_time::ptime getCreateTimestamp(const asiolink::IOAddress &addr, bool generate)
Get lease create-timestamp entry from session history.
std::string filename_
Create timestamps file name.
virtual ~RadiusAccounting()=default
Destructor.
Class of Radius accounting environments.
RadiusAcctEnv(std::string session_id, Event event, uint32_t subnet_id, AttributesPtr send_attrs)
Constructor.
std::string session_id_
Session Id.
bool finished_
Termination flag.
AttributesPtr send_attrs_
Attributes to send.
uint32_t subnet_id_
Subnet Id (aka client/NAS port).
RadiusAcctHandler(RadiusAcctEnv env, const CallbackAcct &callback)
Constructor.
static size_t getCounter()
Get instance counter.
RadiusAcctEnv env_
Environment.
void start()
Start communication.
virtual ~RadiusAcctHandler()
Destructor.
RadiusAsyncAcctPtr acct_
Pointer to the communication class.
RadiusService(const std::string &name)
Constructor.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:556
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:343
boost::shared_ptr< RadiusAcctHandler > RadiusAcctHandlerPtr
Type of pointers to Radius accounting communication handler.
TMContainer::index< TMAddressIndexTag >::type TMContainerAddressIndex
First index type in the TMContainer.
boost::shared_ptr< Attributes > AttributesPtr
Shared pointers to attribute collection.
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.
std::function< void(int)> CallbackAcct
Type of callback for accounting termination.
TMContainer::index< TMTimestampIndexTag >::type TMContainerTimestampIndex
Second index type in the TMContainer.
Event
Type of accounting events.
boost::shared_ptr< util::CSVFile > CSVFilePtr
Type of pointers to CSV file.
boost::shared_ptr< RadiusAsyncAcct > RadiusAsyncAcctPtr
Pointer to asynchronous accounting.
string eventToText(Event event)
Translate an event to text.
Defines the logger used by the top-level component of kea-lfc.
Tag for the index for searching by address.
Tag for the index for searching by timestamp.