Kea: client_exchange.h Source File

| Kea 3.2.0-git

client_exchange.h
Go to the documentation of this file.
1// Copyright (C) 2023-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_CLIENT_EXCHANGE_H
8#define RADIUS_CLIENT_EXCHANGE_H
9
10#include <client_attribute.h>
11#include <client_message.h>
12#include <client_server.h>
13#include <asiolink/asio_wrapper.h>
14#include <asiolink/interval_timer.h>
15#include <asiolink/io_address.h>
16#include <asiolink/io_service.h>
17#include <asiolink/udp_socket.h>
18#include <cc/cfg_to_element.h>
19#include <cc/data.h>
20#include <exceptions/exceptions.h>
21#include <boost/enable_shared_from_this.hpp>
22#include <boost/scoped_ptr.hpp>
23#include <boost/shared_ptr.hpp>
24#include <chrono>
25#include <functional>
26#include <list>
27#include <mutex>
28#include <string>
29#include <vector>
30
31namespace isc {
32namespace radius {
33
37 enum ExchangeRC {
38 BADRESP_RC = -2, // Got a bad response.
39 ERROR_RC = -1, // General error.
40 OK_RC = 0, // OK: no error.
41 TIMEOUT_RC = 1, // Timeout occurred.
42 REJECT_RC = 2, // Got an Access-Reject response.
43 READBLOCK_RC = 3 // Get AGAIN or EWOULDBLOCK.
44};
45
49std::string exchangeRCtoText(const int rc);
50
52class Exchange;
53
55 typedef boost::shared_ptr<Exchange> ExchangePtr;
56
58 class Exchange {
59public:
61 static constexpr size_t BUF_LEN = 8192;
62
64 typedef std::function<void(const boost::system::error_code ec,
65 const size_t size)> SocketCallback;
66
68 typedef asiolink::UDPSocket<const SocketCallback> RadiusSocket;
69
71 typedef std::function<void(const ExchangePtr ex)> Handler;
72
83 static ExchangePtr create(const asiolink::IOServicePtr io_service,
84 const MessagePtr& request,
85 unsigned maxretries,
86 const Servers& servers,
87 Handler handler,
88 RadiusProtocol protocol = PW_PROTO_UDP);
89
97 static ExchangePtr create(const MessagePtr& request,
98 unsigned maxretries,
99 const Servers& servers);
100
102 virtual ~Exchange() = default;
103
107 const std::string& getId() const {
108 return (identifier_);
109 }
110
114 int getRC() const {
115 return (rc_);
116 }
117
121 MessagePtr getRequest() const {
122 return (request_);
123 }
124
128 MessagePtr getResponse() const {
129 return (received_);
130 }
131
133 void logReplyMessages() const;
134
136 virtual void start() = 0;
137
139 virtual void shutdown() = 0;
140
142 void processResponse();
143
144protected:
153 Exchange(const MessagePtr& request,
154 unsigned maxretries,
155 const Servers& servers,
156 Handler handler);
157
165 Exchange(const MessagePtr& request,
166 unsigned maxretries,
167 const Servers& servers);
168
170 std::string identifier_;
171
173 bool sync_;
174
176 int rc_;
177
179 MessagePtr request_;
180
182 MessagePtr sent_;
183
185 MessagePtr received_;
186
189 unsigned maxretries_;
190
192 Servers servers_;
193
195 Handler handler_;
196
198 void createIdentifier();
199
204 void buildRequest(const ServerPtr& server,
205 std::chrono::steady_clock::time_point start_time);
206};
207
209class UdpExchange;
210
212 typedef boost::shared_ptr<UdpExchange> UdpExchangePtr;
213
215 class UdpExchange : public Exchange,
216 public boost::enable_shared_from_this<UdpExchange> {
217public:
227 UdpExchange(const asiolink::IOServicePtr io_service,
228 const MessagePtr& request,
229 unsigned maxretries,
230 const Servers& servers,
231 Handler handler);
232
240 UdpExchange(const MessagePtr& request,
241 unsigned maxretries,
242 const Servers& servers);
243
245 virtual ~UdpExchange();
246
248 virtual void start();
249
251 virtual void shutdown();
252
253protected:
255 asiolink::IOServicePtr io_service_;
256
258 bool started_;
259
261 bool terminated_;
262
264 std::chrono::steady_clock::time_point start_time_;
265
267 boost::scoped_ptr<RadiusSocket> socket_;
268
270 boost::scoped_ptr<asiolink::UDPEndpoint> ep_;
271
273 asiolink::IntervalTimerPtr timer_;
274
276 ServerPtr server_;
277
282 size_t idx_;
283
285 std::vector<uint8_t> buffer_;
286
288 size_t size_;
289
291 unsigned retries_;
292
294 std::list<size_t> postponed_;
295
297 boost::scoped_ptr<std::mutex> mutex_;
298
300 void buildRequest();
301
303 void open();
304
306 virtual void shutdownInternal();
307
311 static void openNext(UdpExchangePtr ex) {
312 ex->open();
313 }
314
320 static void sentHandler(UdpExchangePtr ex,
321 const boost::system::error_code ec,
322 const size_t size);
323
329 static void receivedHandler(UdpExchangePtr ex,
330 const boost::system::error_code ec,
331 const size_t size);
332
334 void setTimer();
335
337 void cancelTimer();
338
342 static void timeoutHandler(UdpExchangePtr ex);
343
345 void terminate();
346};
347
349class TcpExchange;
350
352 typedef boost::shared_ptr<TcpExchange> TcpExchangePtr;
353
355 class TcpExchange : public Exchange,
356 public boost::enable_shared_from_this<TcpExchange> {
357public:
366 TcpExchange(const MessagePtr& request,
367 unsigned maxretries,
368 const Servers& servers,
369 Handler handler);
370
372
374 virtual ~TcpExchange() = default;
375
377 virtual void start();
378
380 virtual void shutdown();
381
382protected:
384 std::chrono::steady_clock::time_point start_time_;
385
387 ServerPtr server_;
388
390 isc::tcp::WireDataPtr response_;
391
393 void buildRequest();
394
401 static void RequestHandler(TcpExchangePtr ex,
402 const boost::system::error_code& ec,
403 const isc::tcp::WireDataPtr& response,
404 const std::string& error_msg);
405
411 static int CompleteCheck(const isc::tcp::WireDataPtr& response,
412 std::string& error_msg);
413};
414
415} // end of namespace isc::radius
416} // end of namespace isc
417
418#endif
asio_wrapper.h
cfg_to_element.h
isc::asiolink::UDPSocket
The UDPSocket class is a concrete derived class of IOAsioSocket that represents a UDP socket.
Definition udp_socket.h:35
isc::radius::Exchange::RadiusSocket
asiolink::UDPSocket< const SocketCallback > RadiusSocket
Type of RADIUS UDP sockets.
Definition client_exchange.h:68
isc::radius::Exchange::servers_
Servers servers_
Servers (a copy which is what we need).
Definition client_exchange.h:192
isc::radius::Exchange::getRequest
MessagePtr getRequest() const
Get the request.
Definition client_exchange.h:121
isc::radius::Exchange::handler_
Handler handler_
Termination handler.
Definition client_exchange.h:195
isc::radius::Exchange::request_
MessagePtr request_
Request message.
Definition client_exchange.h:179
isc::radius::Exchange::Exchange
Exchange(const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler)
Constructor.
Definition client_exchange.cc:59
isc::radius::Exchange::getResponse
MessagePtr getResponse() const
Get the response.
Definition client_exchange.h:128
isc::radius::Exchange::getRC
int getRC() const
Get the error code.
Definition client_exchange.h:114
isc::radius::Exchange::Handler
std::function< void(const ExchangePtr ex)> Handler
Termination handler.
Definition client_exchange.h:71
isc::radius::Exchange::sync_
bool sync_
Sync / async flag.
Definition client_exchange.h:173
isc::radius::Exchange::processResponse
void processResponse()
Process response.
Definition client_exchange.cc:585
isc::radius::Exchange::BUF_LEN
static constexpr size_t BUF_LEN
Receive buffer size.
Definition client_exchange.h:61
isc::radius::Exchange::createIdentifier
void createIdentifier()
Create identifier.
Definition client_exchange.cc:157
isc::radius::Exchange::received_
MessagePtr received_
Received message.
Definition client_exchange.h:185
isc::radius::Exchange::~Exchange
virtual ~Exchange()=default
Destructor.
isc::radius::Exchange::getId
const std::string & getId() const
Get identifier.
Definition client_exchange.h:107
isc::radius::Exchange::rc_
int rc_
Error/return code.
Definition client_exchange.h:176
isc::radius::Exchange::identifier_
std::string identifier_
The identifier (random value in hexadecimal).
Definition client_exchange.h:170
isc::radius::Exchange::start
virtual void start()=0
Start.
isc::radius::Exchange::SocketCallback
std::function< void(const boost::system::error_code ec, const size_t size)> SocketCallback
Type of UDP socket callback functions.
Definition client_exchange.h:65
isc::radius::Exchange::sent_
MessagePtr sent_
Sent message.
Definition client_exchange.h:182
isc::radius::Exchange::buildRequest
void buildRequest(const ServerPtr &server, std::chrono::steady_clock::time_point start_time)
Build request.
Definition client_exchange.cc:247
isc::radius::Exchange::create
static ExchangePtr create(const asiolink::IOServicePtr io_service, const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler, RadiusProtocol protocol=PW_PROTO_UDP)
Factory.
Definition client_exchange.cc:94
isc::radius::Exchange::logReplyMessages
void logReplyMessages() const
Log reply messages.
Definition client_exchange.cc:169
isc::radius::Exchange::shutdown
virtual void shutdown()=0
Shutdown.
isc::radius::Exchange::maxretries_
unsigned maxretries_
Maximum number of retries for a server.
Definition client_exchange.h:189
isc::radius::TcpExchange::response_
isc::tcp::WireDataPtr response_
Response wire data.
Definition client_exchange.h:390
isc::radius::TcpExchange::TcpExchange
TcpExchange(const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler)
Constructor.
Definition client_exchange.cc:812
isc::radius::TcpExchange::shutdown
virtual void shutdown()
Shutdown.
Definition client_exchange.cc:890
isc::radius::TcpExchange::buildRequest
void buildRequest()
Build request.
Definition client_exchange.cc:320
isc::radius::TcpExchange::CompleteCheck
static int CompleteCheck(const isc::tcp::WireDataPtr &response, std::string &error_msg)
Complete check.
Definition client_exchange.cc:959
isc::radius::TcpExchange::~TcpExchange
virtual ~TcpExchange()=default
Destructor.
isc::radius::TcpExchange::start_time_
std::chrono::steady_clock::time_point start_time_
Start time.
Definition client_exchange.h:384
isc::radius::TcpExchange::start
virtual void start()
Start.
Definition client_exchange.cc:823
isc::radius::TcpExchange::server_
ServerPtr server_
Current server.
Definition client_exchange.h:387
isc::radius::TcpExchange::RequestHandler
static void RequestHandler(TcpExchangePtr ex, const boost::system::error_code &ec, const isc::tcp::WireDataPtr &response, const std::string &error_msg)
Request handler.
Definition client_exchange.cc:895
isc::radius::UdpExchange
RADIUS/UDP Exchange.
Definition client_exchange.h:216
isc::radius::UdpExchange::io_service_
asiolink::IOServicePtr io_service_
IO service (argument for async or internal for sync).
Definition client_exchange.h:255
isc::radius::UdpExchange::ep_
boost::scoped_ptr< asiolink::UDPEndpoint > ep_
UDP endpoint.
Definition client_exchange.h:270
isc::radius::UdpExchange::receivedHandler
static void receivedHandler(UdpExchangePtr ex, const boost::system::error_code ec, const size_t size)
Received handler.
Definition client_exchange.cc:673
isc::radius::UdpExchange::cancelTimer
void cancelTimer()
Cancel timer.
Definition client_exchange.cc:793
isc::radius::UdpExchange::timer_
asiolink::IntervalTimerPtr timer_
Interval timer.
Definition client_exchange.h:273
isc::radius::UdpExchange::size_
size_t size_
Number of transmitted octests;.
Definition client_exchange.h:288
isc::radius::UdpExchange::open
void open()
Instance open.
Definition client_exchange.cc:325
isc::radius::UdpExchange::timeoutHandler
static void timeoutHandler(UdpExchangePtr ex)
Timeout handler.
Definition client_exchange.cc:801
isc::radius::UdpExchange::~UdpExchange
virtual ~UdpExchange()
Destructor.
Definition client_exchange.cc:144
isc::radius::UdpExchange::started_
bool started_
Started flag.
Definition client_exchange.h:258
isc::radius::UdpExchange::shutdown
virtual void shutdown()
Shutdown.
Definition client_exchange.cc:217
isc::radius::UdpExchange::start_time_
std::chrono::steady_clock::time_point start_time_
Start time.
Definition client_exchange.h:264
isc::radius::UdpExchange::setTimer
void setTimer()
Set timer.
Definition client_exchange.cc:785
isc::radius::UdpExchange::postponed_
std::list< size_t > postponed_
List of postponed server indexes.
Definition client_exchange.h:294
isc::radius::UdpExchange::buildRequest
void buildRequest()
Build request.
Definition client_exchange.cc:315
isc::radius::UdpExchange::shutdownInternal
virtual void shutdownInternal()
Shutdown.
Definition client_exchange.cc:224
isc::radius::UdpExchange::idx_
size_t idx_
Current server index.
Definition client_exchange.h:282
isc::radius::UdpExchange::server_
ServerPtr server_
Current server.
Definition client_exchange.h:276
isc::radius::UdpExchange::start
virtual void start()
Start.
Definition client_exchange.cc:189
isc::radius::UdpExchange::buffer_
std::vector< uint8_t > buffer_
Buffer.
Definition client_exchange.h:285
isc::radius::UdpExchange::openNext
static void openNext(UdpExchangePtr ex)
Class open / open next.
Definition client_exchange.h:311
isc::radius::UdpExchange::socket_
boost::scoped_ptr< RadiusSocket > socket_
Socket.
Definition client_exchange.h:267
isc::radius::UdpExchange::terminated_
bool terminated_
Terminated flag.
Definition client_exchange.h:261
isc::radius::UdpExchange::UdpExchange
UdpExchange(const asiolink::IOServicePtr io_service, const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler)
Constructor.
Definition client_exchange.cc:116
isc::radius::UdpExchange::retries_
unsigned retries_
Retry counter.
Definition client_exchange.h:291
isc::radius::UdpExchange::terminate
void terminate()
Terminate.
Definition client_exchange.cc:734
isc::radius::UdpExchange::sentHandler
static void sentHandler(UdpExchangePtr ex, const boost::system::error_code ec, const size_t size)
Sent handler.
Definition client_exchange.cc:538
isc::radius::UdpExchange::mutex_
boost::scoped_ptr< std::mutex > mutex_
State change mutex.
Definition client_exchange.h:297
client_attribute.h
client_message.h
client_server.h
data.h
exceptions.h
interval_timer.h
io_address.h
io_service.h
isc::asiolink::IntervalTimerPtr
boost::shared_ptr< isc::asiolink::IntervalTimer > IntervalTimerPtr
Definition interval_timer.h:147
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::radius
Definition cfg_attribute.cc:21
isc::radius::TcpExchangePtr
boost::shared_ptr< TcpExchange > TcpExchangePtr
Type of shared pointers to RADIUS/TCP exchange object.
Definition client_exchange.h:352
isc::radius::exchangeRCtoText
string exchangeRCtoText(const int rc)
ExchangeRC value -> name function.
Definition client_exchange.cc:38
isc::radius::RadiusProtocol
RadiusProtocol
Transport protocols.
Definition client_server.h:44
isc::radius::PW_PROTO_UDP
@ PW_PROTO_UDP
Definition client_server.h:45
isc::radius::Servers
std::vector< ServerPtr > Servers
Type of RADIUS server collection.
Definition client_server.h:228
isc::radius::ExchangePtr
boost::shared_ptr< Exchange > ExchangePtr
Type of shared pointers to RADIUS exchange object.
Definition client_exchange.h:55
isc::radius::ServerPtr
boost::shared_ptr< Server > ServerPtr
Type of shared pointers to a RADIUS server object.
Definition client_server.h:225
isc::radius::ExchangeRC
ExchangeRC
Exchange error codes.
Definition client_exchange.h:37
isc::radius::BADRESP_RC
@ BADRESP_RC
Definition client_exchange.h:38
isc::radius::TIMEOUT_RC
@ TIMEOUT_RC
Definition client_exchange.h:41
isc::radius::READBLOCK_RC
@ READBLOCK_RC
Definition client_exchange.h:43
isc::radius::ERROR_RC
@ ERROR_RC
Definition client_exchange.h:39
isc::radius::REJECT_RC
@ REJECT_RC
Definition client_exchange.h:42
isc::radius::OK_RC
@ OK_RC
Definition client_exchange.h:40
isc::radius::UdpExchangePtr
boost::shared_ptr< UdpExchange > UdpExchangePtr
Type of shared pointers to RADIUS/UDP exchange object.
Definition client_exchange.h:212
isc::radius::MessagePtr
boost::shared_ptr< Message > MessagePtr
Shared pointers to message.
Definition client_message.h:262
isc::tcp::WireDataPtr
boost::shared_ptr< WireData > WireDataPtr
Definition wire_data.h:18
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
udp_socket.h