11 #include <sys/types.h>
12 #include <sys/socket.h>
16 #include <netinet/in.h>
29 #include <boost/noncopyable.hpp>
46 using namespace internal;
53 sizeof(
struct sockaddr_storage) * 2;
80 struct sockaddr_un sock_un_;
86 SocketSessionForwarder::SocketSessionForwarder(
const std::string& unix_file) :
91 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
96 if (
sizeof(impl.
sock_un_.sun_path) - 1 < unix_file.length()) {
98 "File name for a UNIX domain socket is too long: " <<
106 strncpy(impl.
sock_un_.sun_path, unix_file.c_str(),
107 sizeof(impl.
sock_un_.sun_path) - 1);
109 impl.
sock_un_len_ = offsetof(
struct sockaddr_un, sun_path) +
121 if (impl_->
fd_ != -1) {
129 if (impl_->
fd_ != -1) {
131 "endpoint " << impl_->
sock_un_.sun_path);
134 impl_->
fd_ = socket(AF_UNIX, SOCK_STREAM, 0);
135 if (impl_->
fd_ == -1) {
140 int fcntl_flags = fcntl(impl_->
fd_, F_GETFL, 0);
141 if (fcntl_flags != -1) {
142 fcntl_flags |= O_NONBLOCK;
143 fcntl_flags = fcntl(impl_->
fd_, F_SETFL, fcntl_flags);
145 if (fcntl_flags == -1) {
148 "Failed to make UNIX domain socket non blocking: " <<
154 socklen_t sndbuf_size_len =
sizeof(sndbuf_size);
155 if (getsockopt(impl_->
fd_, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
156 &sndbuf_size_len) == -1 ||
158 if (setsockopt(impl_->
fd_, SOL_SOCKET, SO_SNDBUF, &SOCKSESSION_BUFSIZE,
159 sizeof(SOCKSESSION_BUFSIZE)) == -1) {
162 "Failed to set send buffer size to " <<
163 SOCKSESSION_BUFSIZE);
170 "endpoint " << impl_->
sock_un_.sun_path <<
": " <<
177 if (impl_->
fd_ == -1) {
186 const struct sockaddr& local_end,
187 const struct sockaddr& remote_end,
188 const void* data,
size_t data_len)
190 if (impl_->
fd_ == -1) {
193 if ((local_end.sa_family != AF_INET && local_end.sa_family != AF_INET6) ||
194 (remote_end.sa_family != AF_INET && remote_end.sa_family != AF_INET6))
197 "AF_INET or AF_INET6; " <<
198 static_cast<int>(local_end.sa_family) <<
", " <<
199 static_cast<int>(remote_end.sa_family) <<
" given");
201 if (family != local_end.sa_family || family != remote_end.sa_family) {
203 << static_cast<int>(family) <<
"; "
204 << static_cast<int>(local_end.sa_family) <<
", "
205 << static_cast<int>(remote_end.sa_family) <<
" given");
207 if (data_len == 0 || data == NULL) {
210 if (data_len > MAX_DATASIZE) {
212 data_len <<
", must not exceed " << MAX_DATASIZE);
234 const uint32_t data_len32 =
static_cast<uint32_t
>(data_len);
240 const struct iovec iov[2] = {
242 {
const_cast<void*
>(data), data_len }
244 const int cc = writev(impl_->
fd_, iov, 2);
248 "Write failed in forwarding a socket session: " <<
252 "Incomplete write in forwarding a socket session: " << cc <<
258 const sockaddr* local_end,
259 const sockaddr* remote_end,
260 const void* data,
size_t data_len) :
261 sock_(sock), family_(family), type_(type), protocol_(protocol),
262 local_end_(local_end), remote_end_(remote_end),
263 data_(data), data_len_(data_len)
265 if (local_end == NULL || remote_end == NULL) {
286 if (setsockopt(
fd_, SOL_SOCKET, SO_RCVBUF, &SOCKSESSION_BUFSIZE,
287 sizeof(SOCKSESSION_BUFSIZE)) == -1) {
289 "Failed to set receive buffer size to " <<
290 SOCKSESSION_BUFSIZE);
317 readFail(
int actual_len,
int expected_len) {
318 if (expected_len < 0) {
320 "SocketSessionForwarder: " << strerror(errno));
322 isc_throw(SocketSessionError,
"Incomplete data from "
323 "SocketSessionForwarder: " << actual_len <<
"/" <<
330 struct ScopedSocket : boost::noncopyable {
331 ScopedSocket(
int fd) :
fd_(fd) {}
348 ScopedSocket passed_sock(
recv_fd(impl_->
fd_));
352 }
else if (passed_sock.fd_ < 0) {
357 const int cc_hlen = recv(impl_->
fd_, &header_len,
sizeof(header_len),
359 if (cc_hlen <
sizeof(header_len)) {
360 readFail(cc_hlen,
sizeof(header_len));
363 if (header_len > DEFAULT_HEADER_BUFLEN) {
369 const int cc_hdr = recv(impl_->
fd_, &impl_->
header_buf_[0], header_len,
371 if (cc_hdr < header_len) {
372 readFail(cc_hdr, header_len);
377 const int family =
static_cast<int>(ibuffer.
readUint32());
378 if (family != AF_INET && family != AF_INET6) {
380 "Unsupported address family is passed: " << family);
382 const int type =
static_cast<int>(ibuffer.
readUint32());
383 const int protocol =
static_cast<int>(ibuffer.
readUint32());
384 const socklen_t local_end_len = ibuffer.
readUint32();
385 const socklen_t endpoint_minlen = (family == AF_INET) ?
386 sizeof(
struct sockaddr_in) :
sizeof(
struct sockaddr_in6);
387 if (local_end_len < endpoint_minlen ||
388 local_end_len >
sizeof(impl_->
ss_local_)) {
393 const socklen_t remote_end_len = ibuffer.
readUint32();
394 if (remote_end_len < endpoint_minlen ||
400 if (family != impl_->
sa_local_->sa_family ||
403 static_cast<int>(impl_->
sa_local_->sa_family) <<
", " <<
404 static_cast<int>(impl_->
sa_remote_->sa_family) <<
405 " given, must be " << family);
408 if (data_len == 0 || data_len > MAX_DATASIZE) {
410 "Invalid socket session data size: " << data_len <<
411 ", must be > 0 and <= " << MAX_DATASIZE);
416 const int cc_data = recv(impl_->
fd_, &impl_->
data_buf_[0], data_len,
418 if (cc_data < data_len) {
419 readFail(cc_data, data_len);
422 return (
SocketSession(passed_sock.release(), family, type, protocol,
int recv_fd(const int sock)
Receives a file descriptor.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
vector< uint8_t > data_buf_
socklen_t getSALength(const struct sockaddr &sa)
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
struct sockaddr_un sock_un_
void skip(size_t len)
Insert a specified length of gap at the end of the buffer.
virtual void push(int sock, int family, int type, int protocol, const struct sockaddr &local_end, const struct sockaddr &remote_end, const void *data, size_t data_len)
Forward a socket session to the receiver.
struct sockaddr *const sa_remote_
struct sockaddr *const sa_local_
virtual ~SocketSessionForwarder()
The destructor.
struct sockaddr_storage ss_remote_
Support to transfer file descriptors between processes.
SocketSession(int sock, int family, int type, int protocol, const sockaddr *local_end, const sockaddr *remote_end, const void *data, size_t data_len)
The constructor.
An exception indicating general errors that takes place in the socket session related class objects...
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void writeUint16At(uint16_t data, size_t pos)
Write an unsigned 16-bit integer in host byte order at the specified position of the buffer in networ...
A generic exception that is thrown when an unexpected error condition occurs.
const size_t INITIAL_BUFSIZE
const size_t DEFAULT_HEADER_BUFLEN
int send_fd(const int sock, const int fd)
Sends a file descriptor.
struct sockaddr_storage ss_local_
void clear()
Clear buffer content.
A standard DNS module exception that is thrown if an out-of-range buffer operation is being performed...
vector< uint8_t > header_buf_
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
void writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order...
Defines the logger used by the top-level component of kea-dhcp-ddns.
const struct sockaddr * convertSockAddr(const SAType *sa)
SocketSession pop()
Receive a socket session from the forwarder.
const int SOCKSESSION_BUFSIZE
~SocketSessionReceiver()
The destructor.
size_t getLength() const
Return the length of data written in the buffer.
virtual void connectToReceiver()
Establish a connection to the receiver.
SocketSessionReceiver(int fd)
The constructor.
virtual void close()
Close the connection to the receiver.
const int FD_SYSTEM_ERROR