16#include <sys/socket.h>
17#include <netinet/in.h>
30using namespace std::chrono;
31namespace ph = std::placeholders;
36 int const err(getaddrinfo(hostname.c_str(), 0, 0, &
addrinfo_));
38 string const errmsg(gai_strerror(err));
59 if (family == AF_INET) {
60 struct sockaddr_in* sin4 =
61 reinterpret_cast<struct sockaddr_in*
>(res.
addrinfo_->ai_addr);
62 vector<uint8_t> in4(4);
63 memmove(&in4[0], &sin4->sin_addr, 4);
65 }
else if (family == AF_INET6) {
66 struct sockaddr_in6* sin6 =
67 reinterpret_cast<struct sockaddr_in6*
>(res.
addrinfo_->ai_addr);
68 vector<uint8_t> in6(16);
69 memmove(&in6[0], &sin6->sin6_addr, 16);
78 vector<uint8_t> bindest = dest.
toBytes();
79 vector<uint8_t> binsrc(bindest.size());
80 if (bindest.size() == 4) {
81 struct sockaddr_in dest4;
82 socklen_t len =
sizeof(dest4);
83 memset(&dest4, 0, len);
84 dest4.sin_family = AF_INET;
88 memmove(&dest4.sin_addr, &bindest[0], 4);
89 dest4.sin_port = htons(11812);
90 int fd = socket(PF_INET, SOCK_DGRAM, 0);
94 int err = connect(fd,
reinterpret_cast<const struct sockaddr*
>(&dest4),
97 static_cast<void>(close(fd));
100 struct sockaddr_in src4;
101 memset(&src4, 0, len);
102 err = getsockname(fd,
reinterpret_cast<struct sockaddr*
>(&src4), &len);
103 static_cast<void>(close(fd));
104 if ((err < 0) || (len !=
sizeof(src4))) {
107 memmove(&binsrc[0], &src4.sin_addr, 4);
109 }
else if (bindest.size() == 16) {
110 struct sockaddr_in6 dest6;
111 socklen_t len =
sizeof(dest6);
112 memset(&dest6, 0, len);
113 dest6.sin6_family = AF_INET6;
115 dest6.sin6_len = len;
117 memmove(&dest6.sin6_addr, &bindest[0], 16);
118 dest6.sin6_port = htons(11812);
119 int fd = socket(PF_INET6, SOCK_DGRAM, 0);
123 int err = connect(fd,
reinterpret_cast<const struct sockaddr*
>(&dest6),
126 static_cast<void>(close(fd));
129 struct sockaddr_in6 src6;
130 memset(&src6, 0, len);
131 err = getsockname(fd,
reinterpret_cast<struct sockaddr*
>(&src6), &len);
132 static_cast<void>(close(fd));
133 if ((err < 0) || (len !=
sizeof(src6))) {
136 memmove(&binsrc[0], &src6.sin6_addr, 16);
163 if (
static_cast<uint64_t
>(timeout) > numeric_limits<long>::max() / 1000) {
165 <<
" > " << std::numeric_limits<long>::max() / 1000);
167 timeout_ = (timeout == 0 ? 1 : timeout);
172 if (secret.empty()) {
178steady_clock::time_point
218 seconds secs = duration_cast<seconds>(delta);
219 result->set(
"deadtime-end",
static ElementPtr create(const Position &pos=ZERO_POSITION())
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
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.
The IOAddress class represents an IP addresses (version agnostic)
std::string toText() const
Convert the address to a string.
std::vector< uint8_t > toBytes() const
Return address as set of bytes.
short getFamily() const
Returns the address family.
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
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.
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
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.