Kea 3.3.4
client_server.cc
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#include <config.h>
8
13
14#include <cerrno>
15#include <chrono>
16#include <limits>
17#include <sstream>
18
19#include <netdb.h>
20#include <netinet/in.h>
21#include <radius_log.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24
25using namespace isc;
26using namespace isc::asiolink;
27using namespace isc::data;
28using namespace isc::util;
29using namespace std;
30using namespace std::chrono;
31namespace ph = std::placeholders;
32
34struct AddrInfo {
35 AddrInfo(string const& hostname) : addrinfo_(0) {
36 int const err(getaddrinfo(hostname.c_str(), 0, 0, &addrinfo_));
37 if (err != 0) {
38 string const errmsg(gai_strerror(err));
39 isc_throw(BadValue, "failed to resolve '" << hostname << "': " << errmsg);
40 }
41 if (!addrinfo_) {
42 isc_throw(BadValue, "getaddrinfo('" << hostname << ") returned null");
43 }
44 if (!addrinfo_->ai_addr) {
45 freeaddrinfo(addrinfo_);
46 isc_throw(BadValue, "getaddrinfo('" << hostname << ") returned null address");
47 }
48 }
50 freeaddrinfo(addrinfo_);
51 }
52 struct addrinfo *addrinfo_;
53};
54
55
56namespace isc {
57namespace radius {
58
59string
60protocolToText(const int proto) {
61 ostringstream result;
62 switch (proto) {
63 case PW_PROTO_UDP:
64 return ("UDP");
65 case PW_PROTO_TCP:
66 return ("TCP");
67 case PW_PROTO_TLS:
68 return ("TLS");
69 default:
70 result << "unknown transport protocol " << proto;
71 return (result.str());
72 }
73}
74
76Server::getAddress(const string& name) {
77 AddrInfo res(name);
78 short family = res.addrinfo_->ai_family;
79 if (family == AF_INET) {
80 struct sockaddr_in* sin4 =
81 reinterpret_cast<struct sockaddr_in*>(res.addrinfo_->ai_addr);
82 vector<uint8_t> in4(4);
83 memmove(&in4[0], &sin4->sin_addr, 4);
84 return (IOAddress::fromBytes(AF_INET, &in4[0]));
85 } else if (family == AF_INET6) {
86 struct sockaddr_in6* sin6 =
87 reinterpret_cast<struct sockaddr_in6*>(res.addrinfo_->ai_addr);
88 vector<uint8_t> in6(16);
89 memmove(&in6[0], &sin6->sin6_addr, 16);
90 return (IOAddress::fromBytes(AF_INET6, &in6[0]));
91 }
92 isc_throw(BadValue, "getaddrinfo('" << name << ") returned unknown family "
93 << family);
94}
95
98 vector<uint8_t> bindest = dest.toBytes();
99 vector<uint8_t> binsrc(bindest.size());
100 if (bindest.size() == 4) {
101 struct sockaddr_in dest4;
102 socklen_t len = sizeof(dest4);
103 memset(&dest4, 0, len);
104 dest4.sin_family = AF_INET;
105#ifdef HAVE_SA_LEN
106 dest4.sin_len = len;
107#endif
108 memmove(&dest4.sin_addr, &bindest[0], 4);
109 dest4.sin_port = htons(11812);
110 int fd = socket(PF_INET, SOCK_DGRAM, 0);
111 if (fd < 0) {
112 isc_throw(Unexpected, "socket() failed: " << strerror(errno));
113 }
114 int err = connect(fd, reinterpret_cast<const struct sockaddr*>(&dest4),
115 len);
116 if (err < 0) {
117 static_cast<void>(close(fd));
118 isc_throw(Unexpected, "connect() failed: " << strerror(errno));
119 }
120 struct sockaddr_in src4;
121 memset(&src4, 0, len);
122 err = getsockname(fd, reinterpret_cast<struct sockaddr*>(&src4), &len);
123 static_cast<void>(close(fd));
124 if ((err < 0) || (len != sizeof(src4))) {
125 isc_throw(Unexpected, "getsockname() failed: " << strerror(errno));
126 }
127 memmove(&binsrc[0], &src4.sin_addr, 4);
128 return (IOAddress::fromBytes(AF_INET, &binsrc[0]));
129 } else if (bindest.size() == 16) {
130 struct sockaddr_in6 dest6;
131 socklen_t len = sizeof(dest6);
132 memset(&dest6, 0, len);
133 dest6.sin6_family = AF_INET6;
134#ifdef HAVE_SA_LEN
135 dest6.sin6_len = len;
136#endif
137 memmove(&dest6.sin6_addr, &bindest[0], 16);
138 dest6.sin6_port = htons(11812);
139 int fd = socket(PF_INET6, SOCK_DGRAM, 0);
140 if (fd < 0) {
141 isc_throw(Unexpected, "socket() failed: " << strerror(errno));
142 }
143 int err = connect(fd, reinterpret_cast<const struct sockaddr*>(&dest6),
144 len);
145 if (err < 0) {
146 static_cast<void>(close(fd));
147 isc_throw(Unexpected, "connect() failed: " << strerror(errno));
148 }
149 struct sockaddr_in6 src6;
150 memset(&src6, 0, len);
151 err = getsockname(fd, reinterpret_cast<struct sockaddr*>(&src6), &len);
152 static_cast<void>(close(fd));
153 if ((err < 0) || (len != sizeof(src6))) {
154 isc_throw(Unexpected, "getsockname() failed: " << strerror(errno));
155 }
156 memmove(&binsrc[0], &src6.sin6_addr, 16);
157 return (IOAddress::fromBytes(AF_INET6, &binsrc[0]));
158 } else {
159 isc_throw(Unexpected, "address length is not 4 nor 16: "
160 << bindest.size());
161 }
162}
163
165 if (!secret_.empty()) {
166 memset(&secret_[0], 0, secret_.size());
167 }
168 secret_.clear();
169}
170
171
172void
174 if (peer_addr_.getFamily() != local_addr.getFamily()) {
175 isc_throw(BadValue, "address family mismatch: peer "
176 << peer_addr_.toText() << ", local " << local_addr.toText());
177 }
178 local_addr_ = local_addr;
179}
180
181void
182Server::setTimeout(unsigned timeout) {
183 if (static_cast<uint64_t>(timeout) > numeric_limits<long>::max() / 1000) {
184 isc_throw(OutOfRange, "too large timeout " << timeout
185 << " > " << std::numeric_limits<long>::max() / 1000);
186 }
187 timeout_ = (timeout == 0 ? 1 : timeout);
188}
189
190void
191Server::setSecret(const string& secret) {
192 if (secret.empty()) {
193 isc_throw(BadValue, "empty secret");
194 }
195 secret_ = secret;
196}
197
198steady_clock::time_point
201
202 return (deadtime_end_);
203}
204
205void
206Server::setDeadtimeEnd(const steady_clock::time_point& deadtime_end) {
208
209 deadtime_end_ = deadtime_end;
210}
211
215
216 // Peer address.
217 result->set("peer-address", Element::create(peer_addr_.toText()));
218
219 // Port.
220 result->set("peer-port", Element::create(peer_port_));
221
222 // Local address.
223 result->set("local-address", Element::create(local_addr_.toText()));
224
225 // TLS context.
226 if (tls_context_) {
227 result->set("tls-context", Element::create(true));
228 }
229
230 // Secret.
231 result->set("secret", Element::create(secret_));
232
233 // Timeout.
234 result->set("timeout", Element::create(static_cast<long long>(timeout_)));
235
236 // Deadtime.
237 result->set("deadtime", Element::create(static_cast<long long>(deadtime_)));
238
239 if (deadtime_ != 0) {
241
242 auto delta = deadtime_end_ - steady_clock().now();
243 seconds secs = duration_cast<seconds>(delta);
244 result->set("deadtime-end",
245 Element::create(static_cast<long long>(secs.count())));
246 }
247 return (result);
248}
249
250} // end of namespace isc::radius
251} // end of namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:300
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:355
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
A generic exception that is thrown when an unexpected error condition occurs.
void setTimeout(const unsigned timeout)
Set timeout.
asiolink::IOAddress local_addr_
Local address.
boost::scoped_ptr< std::mutex > mutex_
Mutex to protect the state.
void setSecret(const std::string &secret)
Set secret.
void setLocalAddress(const asiolink::IOAddress &local_addr)
Set local address.
const asiolink::IOAddress peer_addr_
Peer address.
data::ElementPtr toElement() const override
Unparse server.
unsigned deadtime_
Deadtime i.e. hold-down delay.
virtual ~Server()
Destructor.
static asiolink::IOAddress getSrcAddress(const asiolink::IOAddress &dest)
Get the source address from a destination address.
std::chrono::steady_clock::time_point deadtime_end_
Deadtime end i.e.
std::chrono::steady_clock::time_point getDeadtimeEnd() const
Get deadtime end.
static asiolink::IOAddress getAddress(const std::string &name)
Get an address from a name.
unsigned timeout_
Timeout.
asiolink::TlsContextPtr tls_context_
TLS context.
void setDeadtimeEnd(const std::chrono::steady_clock::time_point &deadtime_end)
Set deadtime end.
std::string secret_
Secret.
uint16_t peer_port_
Peer port.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
string protocolToText(const int proto)
Transport protocol to text.
Defines the logger used by the top-level component of kea-lfc.
RAII wrapper over struct addrinfo.
AddrInfo(string const &hostname)
struct addrinfo * addrinfo_
RAII lock object to protect the code in the same scope with a mutex.