Kea 3.3.3
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
12#include <asiolink/io_address.h>
13#include <asiolink/io_service.h>
14#include <asiolink/udp_socket.h>
15#include <cc/cfg_to_element.h>
16#include <cc/data.h>
21
22#include <chrono>
23#include <cstdint>
24#include <functional>
25#include <list>
26#include <mutex>
27#include <string>
28#include <vector>
29
30#include <boost/enable_shared_from_this.hpp>
31#include <boost/scoped_ptr.hpp>
32#include <boost/shared_ptr.hpp>
33
34namespace isc {
35namespace radius {
36
40enum ExchangeRC : int16_t {
41 BADRESP_RC = -2, // Got a bad response.
42 ERROR_RC = -1, // General error.
43 OK_RC = 0, // OK: no error.
44 TIMEOUT_RC = 1, // Timeout occurred.
45 REJECT_RC = 2, // Got an Access-Reject response.
46 READBLOCK_RC = 3 // Get AGAIN or EWOULDBLOCK.
47};
48
52std::string exchangeRCtoText(const int rc);
53
55class Exchange;
56
58typedef boost::shared_ptr<Exchange> ExchangePtr;
59
61class Exchange {
62public:
64 static constexpr size_t BUF_LEN = 8192;
65
67 typedef std::function<void(const boost::system::error_code ec,
68 const size_t size)> SocketCallback;
69
72
74 typedef std::function<void(const ExchangePtr ex)> Handler;
75
86 static ExchangePtr create(const asiolink::IOServicePtr io_service,
87 const MessagePtr& request,
88 unsigned maxretries,
89 const Servers& servers,
90 Handler handler,
91 RadiusProtocol protocol = PW_PROTO_UDP);
92
100 static ExchangePtr create(const MessagePtr& request,
101 unsigned maxretries,
102 const Servers& servers);
103
105 virtual ~Exchange() = default;
106
110 const std::string& getId() const {
111 return (identifier_);
112 }
113
117 int getRC() const {
118 return (rc_);
119 }
120
125 return (request_);
126 }
127
132 return (received_);
133 }
134
136 void logReplyMessages() const;
137
139 virtual void start() = 0;
140
142 virtual void shutdown() = 0;
143
145 void processResponse();
146
147protected:
156 Exchange(const MessagePtr& request,
157 unsigned maxretries,
158 const Servers& servers,
159 Handler handler);
160
168 Exchange(const MessagePtr& request,
169 unsigned maxretries,
170 const Servers& servers);
171
173 std::string identifier_;
174
176 bool sync_;
177
179 int rc_;
180
183
186
189
192 unsigned maxretries_;
193
196
199
201 void createIdentifier();
202
207 void buildRequest(const ServerPtr& server,
208 std::chrono::steady_clock::time_point start_time);
209};
210
212class UdpExchange;
213
215typedef boost::shared_ptr<UdpExchange> UdpExchangePtr;
216
218class UdpExchange : public Exchange,
219 public boost::enable_shared_from_this<UdpExchange> {
220public:
230 UdpExchange(const asiolink::IOServicePtr io_service,
231 const MessagePtr& request,
232 unsigned maxretries,
233 const Servers& servers,
234 Handler handler);
235
243 UdpExchange(const MessagePtr& request,
244 unsigned maxretries,
245 const Servers& servers);
246
248 virtual ~UdpExchange();
249
251 virtual void start();
252
254 virtual void shutdown();
255
256protected:
259
262
265
267 std::chrono::steady_clock::time_point start_time_;
268
270 boost::scoped_ptr<RadiusSocket> socket_;
271
273 boost::scoped_ptr<asiolink::UDPEndpoint> ep_;
274
277
280
285 size_t idx_;
286
288 std::vector<uint8_t> buffer_;
289
291 size_t size_;
292
294 unsigned retries_;
295
297 std::list<size_t> postponed_;
298
300 boost::scoped_ptr<std::mutex> mutex_;
301
303 void buildRequest();
304
306 void open();
307
309 virtual void shutdownInternal();
310
314 static void openNext(UdpExchangePtr ex) {
315 ex->open();
316 }
317
323 static void sentHandler(UdpExchangePtr ex,
324 const boost::system::error_code ec,
325 const size_t size);
326
332 static void receivedHandler(UdpExchangePtr ex,
333 const boost::system::error_code ec,
334 const size_t size);
335
337 void setTimer();
338
340 void cancelTimer();
341
345 static void timeoutHandler(UdpExchangePtr ex);
346
348 void terminate();
349};
350
352class TcpExchange;
353
355typedef boost::shared_ptr<TcpExchange> TcpExchangePtr;
356
358class TcpExchange : public Exchange,
359 public boost::enable_shared_from_this<TcpExchange> {
360public:
369 TcpExchange(const MessagePtr& request,
370 unsigned maxretries,
371 const Servers& servers,
372 Handler handler);
373
375
377 virtual ~TcpExchange() = default;
378
380 virtual void start();
381
383 virtual void shutdown();
384
385protected:
387 std::chrono::steady_clock::time_point start_time_;
388
391
394
396 void buildRequest();
397
404 static void RequestHandler(TcpExchangePtr ex,
405 const boost::system::error_code& ec,
406 const isc::tcp::WireDataPtr& response,
407 const std::string& error_msg);
408
414 static int CompleteCheck(const isc::tcp::WireDataPtr& response,
415 std::string& error_msg);
416};
417
418} // end of namespace isc::radius
419} // end of namespace isc
420
421#endif
asiolink::UDPSocket< const SocketCallback > RadiusSocket
Type of RADIUS UDP sockets.
Servers servers_
Servers (a copy which is what we need).
MessagePtr getRequest() const
Get the request.
Handler handler_
Termination handler.
MessagePtr request_
Request message.
Exchange(const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler)
Constructor.
MessagePtr getResponse() const
Get the response.
int getRC() const
Get the error code.
std::function< void(const ExchangePtr ex)> Handler
Termination handler.
bool sync_
Sync / async flag.
void processResponse()
Process response.
static constexpr size_t BUF_LEN
Receive buffer size.
void createIdentifier()
Create identifier.
MessagePtr received_
Received message.
virtual ~Exchange()=default
Destructor.
const std::string & getId() const
Get identifier.
int rc_
Error/return code.
std::string identifier_
The identifier (random value in hexadecimal).
virtual void start()=0
Start.
std::function< void(const boost::system::error_code ec, const size_t size)> SocketCallback
Type of UDP socket callback functions.
MessagePtr sent_
Sent message.
void buildRequest(const ServerPtr &server, std::chrono::steady_clock::time_point start_time)
Build request.
static ExchangePtr create(const asiolink::IOServicePtr io_service, const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler, RadiusProtocol protocol=PW_PROTO_UDP)
Factory.
void logReplyMessages() const
Log reply messages.
virtual void shutdown()=0
Shutdown.
unsigned maxretries_
Maximum number of retries for a server.
isc::tcp::WireDataPtr response_
Response wire data.
TcpExchange(const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler)
Constructor.
virtual void shutdown()
Shutdown.
void buildRequest()
Build request.
static int CompleteCheck(const isc::tcp::WireDataPtr &response, std::string &error_msg)
Complete check.
virtual ~TcpExchange()=default
Destructor.
std::chrono::steady_clock::time_point start_time_
Start time.
virtual void start()
Start.
ServerPtr server_
Current server.
static void RequestHandler(TcpExchangePtr ex, const boost::system::error_code &ec, const isc::tcp::WireDataPtr &response, const std::string &error_msg)
Request handler.
RADIUS/UDP Exchange.
asiolink::IOServicePtr io_service_
IO service (argument for async or internal for sync).
boost::scoped_ptr< asiolink::UDPEndpoint > ep_
UDP endpoint.
static void receivedHandler(UdpExchangePtr ex, const boost::system::error_code ec, const size_t size)
Received handler.
void cancelTimer()
Cancel timer.
asiolink::IntervalTimerPtr timer_
Interval timer.
size_t size_
Number of transmitted octests;.
void open()
Instance open.
static void timeoutHandler(UdpExchangePtr ex)
Timeout handler.
virtual ~UdpExchange()
Destructor.
bool started_
Started flag.
virtual void shutdown()
Shutdown.
std::chrono::steady_clock::time_point start_time_
Start time.
std::list< size_t > postponed_
List of postponed server indexes.
void buildRequest()
Build request.
virtual void shutdownInternal()
Shutdown.
size_t idx_
Current server index.
ServerPtr server_
Current server.
virtual void start()
Start.
std::vector< uint8_t > buffer_
Buffer.
static void openNext(UdpExchangePtr ex)
Class open / open next.
boost::scoped_ptr< RadiusSocket > socket_
Socket.
bool terminated_
Terminated flag.
UdpExchange(const asiolink::IOServicePtr io_service, const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler)
Constructor.
unsigned retries_
Retry counter.
static void sentHandler(UdpExchangePtr ex, const boost::system::error_code ec, const size_t size)
Sent handler.
boost::scoped_ptr< std::mutex > mutex_
State change mutex.
ExchangeRC
Exchange error codes.
boost::shared_ptr< TcpExchange > TcpExchangePtr
Type of shared pointers to RADIUS/TCP exchange object.
string exchangeRCtoText(const int rc)
ExchangeRC value -> name function.
std::vector< ServerPtr > Servers
Type of RADIUS server collection.
boost::shared_ptr< Exchange > ExchangePtr
Type of shared pointers to RADIUS exchange object.
boost::shared_ptr< Server > ServerPtr
Type of shared pointers to a RADIUS server object.
boost::shared_ptr< UdpExchange > UdpExchangePtr
Type of shared pointers to RADIUS/UDP exchange object.
boost::shared_ptr< Message > MessagePtr
Shared pointers to message.
RadiusProtocol
Transport protocols.
boost::shared_ptr< WireData > WireDataPtr
Definition wire_data.h:18
Defines the logger used by the top-level component of kea-lfc.