Kea 3.1.1
client_exchange.h
Go to the documentation of this file.
1// Copyright (C) 2023-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_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>
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>
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
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
55typedef boost::shared_ptr<Exchange> ExchangePtr;
56
58class Exchange : public boost::enable_shared_from_this<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
69
71 typedef std::function<void(const ExchangePtr ex)> Handler;
72
82 Exchange(const asiolink::IOServicePtr io_service,
83 const MessagePtr& request,
84 unsigned maxretries,
85 const Servers& servers,
86 Handler handler);
87
95 Exchange(const MessagePtr& request,
96 unsigned maxretries,
97 const Servers& servers);
98
100 virtual ~Exchange();
101
105 const std::string& getId() const {
106 return (identifier_);
107 }
108
112 int getRC() const {
113 return (rc_);
114 }
115
120 return (request_);
121 }
122
127 return (received_);
128 }
129
131 void logReplyMessages() const;
132
134 virtual void start();
135
137 virtual void shutdown();
138
139protected:
141 std::string identifier_;
142
145
147 bool sync_;
148
151
154
156 int rc_;
157
159 std::chrono::steady_clock::time_point start_time_;
160
162 boost::scoped_ptr<RadiusSocket> socket_;
163
165 boost::scoped_ptr<asiolink::UDPEndpoint> ep_;
166
169
172
177 size_t idx_;
178
181
184
187
189 std::vector<uint8_t> buffer_;
190
192 size_t size_;
193
195 unsigned retries_;
196
199 unsigned maxretries_;
200
203
205 std::list<size_t> postponed_;
206
209
211 boost::scoped_ptr<std::mutex> mutex_;
212
214 void createIdentifier();
215
217 void buildRequest();
218
220 void open();
221
223 virtual void shutdownInternal();
224
228 static void openNext(ExchangePtr ex) {
229 ex->open();
230 }
231
237 static void sentHandler(ExchangePtr ex,
238 const boost::system::error_code ec,
239 const size_t size);
240
246 static void receivedHandler(ExchangePtr ex,
247 const boost::system::error_code ec,
248 const size_t size);
249
251 void setTimer();
252
254 void cancelTimer();
255
259 static void timeoutHandler(ExchangePtr ex);
260
262 void terminate();
263};
264
265} // end of namespace isc::radius
266} // end of namespace isc
267
268#endif
std::list< size_t > postponed_
List of postponed server indexes.
asiolink::UDPSocket< const SocketCallback > RadiusSocket
Type of RADIUS UDP sockets.
Servers servers_
Servers (a copy which is what we need).
virtual ~Exchange()
Destructor.
MessagePtr getRequest() const
Get the request.
virtual void start()
Start.
Handler handler_
Termination handler.
MessagePtr request_
Request message.
Exchange(const asiolink::IOServicePtr io_service, const MessagePtr &request, unsigned maxretries, const Servers &servers, Handler handler)
Constructor.
size_t size_
Number of transmitted octests;.
MessagePtr getResponse() const
Get the response.
ServerPtr server_
Current server.
int getRC() const
Get the error code.
asiolink::IntervalTimerPtr timer_
Interval timer.
std::function< void(const ExchangePtr ex)> Handler
Termination handler.
bool sync_
Sync / async flag.
void buildRequest()
Build request.
static void sentHandler(ExchangePtr ex, const boost::system::error_code ec, const size_t size)
Sent handler.
static constexpr size_t BUF_LEN
Receive buffer size.
void createIdentifier()
Create identifier.
MessagePtr received_
Received message.
void terminate()
Terminate.
void setTimer()
Set timer.
boost::scoped_ptr< std::mutex > mutex_
State change mutex.
const std::string & getId() const
Get identifier.
int rc_
Error/return code.
bool started_
Started flag.
boost::scoped_ptr< asiolink::UDPEndpoint > ep_
UDP endpoint.
static void openNext(ExchangePtr ex)
Class open / open next.
std::chrono::steady_clock::time_point start_time_
Start time.
static void receivedHandler(ExchangePtr ex, const boost::system::error_code ec, const size_t size)
Received handler.
std::string identifier_
The identifier (random value in hexadecimal).
boost::scoped_ptr< RadiusSocket > socket_
Socket.
std::function< void(const boost::system::error_code ec, const size_t size)> SocketCallback
Type of UDP socket callback functions.
MessagePtr sent_
Sent message.
bool terminated_
Terminated flag.
virtual void shutdown()
Shutdown.
void open()
Instance open.
virtual void shutdownInternal()
Shutdown.
asiolink::IOServicePtr io_service_
IO service (argument for async or internal for sync).
static void timeoutHandler(ExchangePtr ex)
Timeout handler.
std::vector< uint8_t > buffer_
Buffer.
void logReplyMessages() const
Log reply messages.
void cancelTimer()
Cancel timer.
size_t idx_
Current server index.
unsigned retries_
Retry counter.
unsigned maxretries_
Maximum number of retries for a server.
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.
ExchangeRC
Exchange error codes.
boost::shared_ptr< Message > MessagePtr
Shared pointers to message.
Defines the logger used by the top-level component of kea-lfc.