Kea 3.1.1
radius_accounting.h
Go to the documentation of this file.
1// Copyright (C) 2020-2025 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
25typedef enum {
26 EVENT_CREATE, //< A new lease was created (leaseX_select hooks).
27 EVENT_RENEW, //< A lease was renewed (leaseX_renew hooks).
28 EVENT_REBIND, //< A lease was rebound (lease6_rebind hook).
29 EVENT_EXPIRE, //< A lease was expired (leaseX_expire hooks).
30 EVENT_RELEASE, //< A lease was released (leaseX_release hooks).
31 EVENT_DECLINE, //< A lease was declined (leaseX_decline hooks).
32 EVENT_ADD, //< A command added a lease (command_processed hook).
33 EVENT_UPDATE, //< A command updated a lease (command_processed hook).
34 EVENT_DEL //< A command deleted a lease (command_processed hook).
35} Event;
36
41std::string eventToText(Event event);
42
48class LeaseTS {
49public:
50
56 const boost::posix_time::ptime& timestamp)
57 : addr_(addr), timestamp_(timestamp) {
58 }
59
62
64 const boost::posix_time::ptime timestamp_;
65};
66
69
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,
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,
96 >
97 >
99
101typedef TMContainer::index<TMAddressIndexTag>::type TMContainerAddressIndex;
102
104typedef TMContainer::index<TMTimestampIndexTag>::type TMContainerTimestampIndex;
105
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
126
128 uint32_t subnet_id_;
129
132
135};
136
139public:
140
145 RadiusAcctHandler(RadiusAcctEnv env, const CallbackAcct& callback);
146
148 virtual ~RadiusAcctHandler();
149
151 void start();
152
154 static size_t getCounter();
155
158
159protected:
160
163
164private:
165
167 static size_t counter_;
168
170 static std::mutex mutex_;
171};
172
174typedef boost::shared_ptr<RadiusAcctHandler> RadiusAcctHandlerPtr;
175
177typedef boost::shared_ptr<util::CSVFile> CSVFilePtr;
178
181public:
182
185
187 virtual ~RadiusAccounting() = default;
188
195 void init(const std::string& filename);
196
215
233
242 Event event);
243
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
277
281 bool loadFromFile();
282
287 void storeToFile();
288
289protected:
290
292 std::string filename_;
293
296
300 const boost::posix_time::ptime epoch_;
301
304
310
312 std::mutex mutex_;
313};
314
315} // end of namespace isc::radius
316} // end of namespace isc
317
318#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.
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:29
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:528
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:315
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.