2017-12-22 18:38:30 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2017-12-22 18:38:30 +11:00
|
|
|
*
|
|
|
|
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "socket.hpp"
|
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/platform.hpp>
|
|
|
|
#include <cruft/util/posix/except.hpp>
|
2017-12-22 18:38:30 +11:00
|
|
|
|
2018-08-13 14:51:33 +10:00
|
|
|
#if !defined(PLATFORM_WIN32)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#else
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
2017-12-22 18:38:30 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::posix::socket;
|
2017-12-22 18:38:30 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
class lookup {
|
|
|
|
public:
|
2018-08-05 14:42:02 +10:00
|
|
|
lookup (const cruft::view<const char*> host, int port):
|
2017-12-22 18:38:30 +11:00
|
|
|
m_addresses (nullptr, freeaddrinfo)
|
|
|
|
{
|
|
|
|
struct ::addrinfo hints {};
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
std::string host;
|
|
|
|
std::string port;
|
|
|
|
} strings {
|
|
|
|
.host = std::string (std::begin (host), std::end (host)),
|
|
|
|
.port = std::to_string (port)
|
|
|
|
};
|
|
|
|
|
|
|
|
addrinfo* _addresses;
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::posix::eai::try_code (
|
2017-12-22 18:38:30 +11:00
|
|
|
getaddrinfo (
|
|
|
|
strings.host.c_str (),
|
|
|
|
strings.port.c_str (),
|
|
|
|
&hints,
|
|
|
|
&_addresses
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
m_addresses.reset (_addresses);
|
|
|
|
};
|
|
|
|
|
2017-12-30 13:40:04 +11:00
|
|
|
// TODO: use a wrapper that supports the increment operator
|
2017-12-22 18:38:30 +11:00
|
|
|
auto begin (void) const { return m_addresses.get (); }
|
|
|
|
auto end (void) const { return nullptr; }
|
|
|
|
|
|
|
|
private:
|
2017-12-26 17:27:36 +11:00
|
|
|
std::unique_ptr<addrinfo,void(*)(addrinfo*)> m_addresses;
|
2017-12-22 18:38:30 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
static cruft::posix::fd
|
|
|
|
connect_host (const cruft::view<const char*> host, int port)
|
2017-12-22 18:38:30 +11:00
|
|
|
{
|
2017-12-30 13:40:04 +11:00
|
|
|
lookup l {host,port};
|
|
|
|
for (auto _info = l.begin (); _info; _info = _info->ai_next) {
|
|
|
|
auto &info = *_info;
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::posix::fd sock {
|
2017-12-22 18:38:30 +11:00
|
|
|
socket (info.ai_family, info.ai_socktype, info.ai_protocol)
|
|
|
|
};
|
|
|
|
if (sock == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (connect (sock, info.ai_addr, info.ai_addrlen))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error ("unable to connect");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-12-26 17:27:36 +11:00
|
|
|
socket::socket (int _fd):
|
|
|
|
fd (_fd)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-12-22 18:38:30 +11:00
|
|
|
socket::socket (int domain, int type):
|
|
|
|
socket (domain, type, 0)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
socket::socket (int domain, int type, int protocol):
|
|
|
|
fd (::socket (domain, type, protocol))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-08-05 14:42:02 +10:00
|
|
|
socket::socket (cruft::view<const char*> host, int port):
|
2017-12-22 18:38:30 +11:00
|
|
|
fd (connect_host (host, port))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
socket::~socket ()
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2017-12-26 17:27:36 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-07-24 15:48:50 +10:00
|
|
|
socket::socket (socket &&rhs) noexcept:
|
2017-12-26 17:27:36 +11:00
|
|
|
fd (std::move (rhs))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
class socket&
|
2018-07-24 15:48:50 +10:00
|
|
|
socket::operator= (socket &&rhs) noexcept
|
2017-12-26 17:27:36 +11:00
|
|
|
{
|
|
|
|
fd::operator= (std::move (rhs));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-22 18:38:30 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2019-02-02 18:11:30 +11:00
|
|
|
socket::bind (sockaddr const &addr)
|
2018-07-27 18:29:30 +10:00
|
|
|
{
|
2019-02-02 18:11:30 +11:00
|
|
|
auto const size = addr.sa_family == AF_INET ? sizeof (sockaddr_in )
|
|
|
|
: addr.sa_family == AF_INET6 ? sizeof (sockaddr_in6)
|
|
|
|
: throw std::runtime_error ("Unhandled sockaddr type");
|
|
|
|
|
|
|
|
cruft::posix::error::try_call (
|
|
|
|
::bind,
|
|
|
|
native (),
|
|
|
|
&addr,
|
|
|
|
size
|
2018-07-27 18:29:30 +10:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-02 18:11:30 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
socket::bind (sockaddr_in const &addr)
|
|
|
|
{
|
|
|
|
return bind (reinterpret_cast<sockaddr const&> (addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-27 18:29:30 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
socket::connect (sockaddr_in const &addr)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::posix::error::try_code (
|
2018-07-27 18:29:30 +10:00
|
|
|
::connect (native (), reinterpret_cast<sockaddr const*>(&addr), sizeof (addr))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
socket::connect (cruft::view<const char*> host, int port)
|
2017-12-22 18:38:30 +11:00
|
|
|
{
|
2017-12-26 17:27:36 +11:00
|
|
|
const lookup l { host, port };
|
|
|
|
for (auto cursor = l.begin (); cursor != l.end (); cursor = cursor->ai_next) {
|
|
|
|
if (!::connect (*this, cursor->ai_addr, cursor->ai_addrlen))
|
2017-12-22 18:38:30 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error ("unable to reconnect");
|
|
|
|
}
|
2018-07-27 18:29:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename ByteT>
|
|
|
|
ssize_t
|
2018-08-05 14:42:02 +10:00
|
|
|
socket::sendto (cruft::view<const ByteT *> buffer, int flags, ::sockaddr_in const &dst)
|
2018-07-27 18:29:43 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::posix::error::try_value (
|
2018-07-27 18:29:43 +10:00
|
|
|
::sendto (
|
|
|
|
native (),
|
|
|
|
buffer.data (),
|
|
|
|
buffer.size (),
|
|
|
|
flags,
|
|
|
|
reinterpret_cast<sockaddr const*> (&dst),
|
|
|
|
sizeof (dst)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
template ssize_t socket::sendto (cruft::view<const char*>, int, sockaddr_in const&);
|
|
|
|
template ssize_t socket::sendto (cruft::view<const unsigned char*>, int, sockaddr_in const&);
|
|
|
|
template ssize_t socket::sendto (cruft::view<const std::byte*>, int, sockaddr_in const&);
|
2018-07-27 18:29:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename ByteT>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view<ByteT*>
|
|
|
|
socket::recvfrom (cruft::view<ByteT*> buffer, int flags)
|
2018-07-27 18:29:43 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
auto res = cruft::posix::error::try_value (
|
2018-07-27 18:29:43 +10:00
|
|
|
::recvfrom (native (), buffer.data (), buffer.size (), flags, nullptr, nullptr)
|
|
|
|
);
|
|
|
|
|
|
|
|
return { buffer.begin (), res };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
template cruft::view<char*> socket::recvfrom (cruft::view<char*> buffer, int flags);
|
|
|
|
template cruft::view<unsigned char*> socket::recvfrom (cruft::view<unsigned char*> buffer, int flags);
|
|
|
|
template cruft::view<std::byte*> socket::recvfrom (cruft::view<std::byte*> buffer, int flags);
|