Fix build of Socket and InetAddress.

This commit is contained in:
Relintai 2024-01-13 08:59:48 +01:00
parent e5b7ff3491
commit df92131fa2
4 changed files with 337 additions and 430 deletions

View File

@ -10,41 +10,40 @@
#include "inet_address.h"
#include<cstring>
#include <cstdio>
#include <cstring>
#ifdef _WIN32
#ifdef __GNUC__
#define GCCWIN
// Mingw / gcc on windows
// #define _WIN32_WINNT 0x0501
#include <winsock2.h>
#include <ws2tcpip.h>
#define GCCWIN
// Mingw / gcc on windows
// #define _WIN32_WINNT 0x0501
#include <winsock2.h>
#include <ws2tcpip.h>
extern "C" {
WINSOCK_API_LINKAGE INT WSAAPI inet_pton( INT Family, PCSTR pszAddrString, PVOID pAddrBuf);
WINSOCK_API_LINKAGE PCSTR WSAAPI inet_ntop(INT Family, PVOID pAddr, PSTR pStringBuf, size_t StringBufSize);
}
extern "C" {
WINSOCK_API_LINKAGE INT WSAAPI inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf);
WINSOCK_API_LINKAGE PCSTR WSAAPI inet_ntop(INT Family, PVOID pAddr, PSTR pStringBuf, size_t StringBufSize);
}
#else
// Windows...
#include <winsock2.h>
#include <in6addr.h>
#include <ws2tcpip.h>
// Windows...
#include <in6addr.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
struct in6__addruint
{
union
{
u_char Byte[16];
u_short Word[8];
uint32_t __s6_addr32[4];
} uext;
struct in6__addruint {
union {
u_char Byte[16];
u_short Word[8];
uint32_t __s6_addr32[4];
} uext;
};
#else
#include <strings.h> // memset
#include <netinet/tcp.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <strings.h> // memset
#endif
// INADDR_ANY use (type)value casting.
@ -80,218 +79,182 @@ static const in_addr_t kInaddrLoopback = INADDR_ANY;
#endif
*/
String InetAddress::to_ip_port() const
{
char buf[64] = "";
uint16_t port = ntohs(_addr.sin_port);
snprintf(buf, sizeof(buf), ":%u", port);
String InetAddress::to_ip_port() const {
char buf[64] = "";
uint16_t port = ntohs(_addr.sin_port);
snprintf(buf, sizeof(buf), ":%u", port);
return to_ip() + String(buf);
return to_ip() + String(buf);
}
bool InetAddress::is_intranet_ip() const
{
if (_addr.sin_family == AF_INET)
{
uint32_t ip_addr = ntohl(_addr.sin_addr.s_addr);
if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) ||
(ip_addr >= 0xAC100000 && ip_addr <= 0xAC1FFFFF) ||
(ip_addr >= 0xC0A80000 && ip_addr <= 0xC0A8FFFF) ||
ip_addr == 0x7f000001)
bool InetAddress::is_intranet_ip() const {
if (_addr.sin_family == AF_INET) {
uint32_t ip_addr = ntohl(_addr.sin_addr.s_addr);
if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) ||
(ip_addr >= 0xAC100000 && ip_addr <= 0xAC1FFFFF) ||
(ip_addr >= 0xC0A80000 && ip_addr <= 0xC0A8FFFF) ||
ip_addr == 0x7f000001)
{
return true;
}
}
else
{
auto addrP = ip6_net_endian();
// Loopback ip
if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 &&
ntohl(*(addrP + 3)) == 1)
return true;
// Privated ip is prefixed by FEC0::/10 or FE80::/10, need testing
auto i32 = (ntohl(*addrP) & 0xffc00000);
if (i32 == 0xfec00000 || i32 == 0xfe800000)
return true;
if (*addrP == 0 && *(addrP + 1) == 0 && ntohl(*(addrP + 2)) == 0xffff)
{
// the IPv6 version of an IPv4 IP address
uint32_t ip_addr = ntohl(*(addrP + 3));
if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) ||
(ip_addr >= 0xAC100000 && ip_addr <= 0xAC1FFFFF) ||
(ip_addr >= 0xC0A80000 && ip_addr <= 0xC0A8FFFF) ||
ip_addr == 0x7f000001)
{
return true;
}
} else {
auto addrP = ip6_net_endian();
// Loopback ip
if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 &&
ntohl(*(addrP + 3)) == 1)
return true;
// Privated ip is prefixed by FEC0::/10 or FE80::/10, need testing
auto i32 = (ntohl(*addrP) & 0xffc00000);
if (i32 == 0xfec00000 || i32 == 0xfe800000)
return true;
if (*addrP == 0 && *(addrP + 1) == 0 && ntohl(*(addrP + 2)) == 0xffff) {
// the IPv6 version of an IPv4 IP address
uint32_t ip_addr = ntohl(*(addrP + 3));
if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) ||
(ip_addr >= 0xAC100000 && ip_addr <= 0xAC1FFFFF) ||
(ip_addr >= 0xC0A80000 && ip_addr <= 0xC0A8FFFF) ||
ip_addr == 0x7f000001)
{
return true;
}
}
}
return false;
{
return true;
}
}
}
return false;
}
bool InetAddress::is_loopback_ip() const
{
if (!is_ip_v6())
{
uint32_t ip_addr = ntohl(_addr.sin_addr.s_addr);
if (ip_addr == 0x7f000001)
{
return true;
}
}
else
{
auto addrP = ip6_net_endian();
if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 &&
ntohl(*(addrP + 3)) == 1)
return true;
// the IPv6 version of an IPv4 loopback address
if (*addrP == 0 && *(addrP + 1) == 0 && ntohl(*(addrP + 2)) == 0xffff &&
ntohl(*(addrP + 3)) == 0x7f000001)
return true;
}
return false;
bool InetAddress::is_loopback_ip() const {
if (!is_ip_v6()) {
uint32_t ip_addr = ntohl(_addr.sin_addr.s_addr);
if (ip_addr == 0x7f000001) {
return true;
}
} else {
auto addrP = ip6_net_endian();
if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 &&
ntohl(*(addrP + 3)) == 1)
return true;
// the IPv6 version of an IPv4 loopback address
if (*addrP == 0 && *(addrP + 1) == 0 && ntohl(*(addrP + 2)) == 0xffff &&
ntohl(*(addrP + 3)) == 0x7f000001)
return true;
}
return false;
}
const struct sockaddr *InetAddress::get_sock_addr() const
{
return static_cast<const struct sockaddr *>((void *)(&_addr6));
const struct sockaddr *InetAddress::get_sock_addr() const {
return static_cast<const struct sockaddr *>((void *)(&_addr6));
}
void InetAddress::set_sock_addr_inet6(const struct sockaddr_in6 &addr6)
{
_addr6 = addr6;
_is_ip_v6 = (_addr6.sin6_family == AF_INET6);
_is_unspecified = false;
void InetAddress::set_sock_addr_inet6(const struct sockaddr_in6 &addr6) {
_addr6 = addr6;
_is_ip_v6 = (_addr6.sin6_family == AF_INET6);
_is_unspecified = false;
}
sa_family_t InetAddress::family() const
{
return _addr.sin_family;
sa_family_t InetAddress::family() const {
return _addr.sin_family;
}
String InetAddress::to_ip() const
{
char buf[64];
if (_addr.sin_family == AF_INET)
{
String InetAddress::to_ip() const {
char buf[64];
if (_addr.sin_family == AF_INET) {
#if defined GCCWIN || (_MSC_VER && _MSC_VER >= 1900)
::inet_ntop(AF_INET, (PVOID)&_addr.sin_addr, buf, sizeof(buf));
::inet_ntop(AF_INET, (PVOID)&_addr.sin_addr, buf, sizeof(buf));
#else
::inet_ntop(AF_INET, &_addr.sin_addr, buf, sizeof(buf));
::inet_ntop(AF_INET, &_addr.sin_addr, buf, sizeof(buf));
#endif
}
else if (_addr.sin_family == AF_INET6)
{
} else if (_addr.sin_family == AF_INET6) {
#if defined GCCWIN || (_MSC_VER && _MSC_VER >= 1900)
::inet_ntop(AF_INET6, (PVOID)&_addr6.sin6_addr, buf, sizeof(buf));
::inet_ntop(AF_INET6, (PVOID)&_addr6.sin6_addr, buf, sizeof(buf));
#else
::inet_ntop(AF_INET6, &_addr6.sin6_addr, buf, sizeof(buf));
::inet_ntop(AF_INET6, &_addr6.sin6_addr, buf, sizeof(buf));
#endif
}
}
return buf;
return buf;
}
uint32_t InetAddress::ip_net_endian() const
{
// assert(family() == AF_INET);
return _addr.sin_addr.s_addr;
uint32_t InetAddress::ip_net_endian() const {
// assert(family() == AF_INET);
return _addr.sin_addr.s_addr;
}
const uint32_t *InetAddress::ip6_net_endian() const
{
const uint32_t *InetAddress::ip6_net_endian() const {
// assert(family() == AF_INET6);
#if defined __linux__ || defined __HAIKU__
return _addr6.sin6_addr.s6_addr32;
return _addr6.sin6_addr.s6_addr32;
#elif defined _WIN32
// TODO is this OK ?
const struct in6__addruint *_addrtemp =
reinterpret_cast<const struct in6__addruint *>(&_addr6.sin6_addr);
return (*_addrtemp).uext.__s6_addr32;
// TODO is this OK ?
const struct in6__addruint *_addrtemp =
reinterpret_cast<const struct in6__addruint *>(&_addr6.sin6_addr);
return (*_addrtemp).uext.__s6_addr32;
#else
return _addr6.sin6_addr.__u6_addr.__u6_addr32;
return _addr6.sin6_addr.__u6_addr.__u6_addr32;
#endif
}
uint16_t InetAddress::port_net_endian() const
{
return _addr.sin_port;
uint16_t InetAddress::port_net_endian() const {
return _addr.sin_port;
}
void InetAddress::set_port_net_endian(uint16_t port)
{
_addr.sin_port = port;
void InetAddress::set_port_net_endian(uint16_t port) {
_addr.sin_port = port;
}
inline bool InetAddress::is_unspecified() const
{
return _is_unspecified;
inline bool InetAddress::is_unspecified() const {
return _is_unspecified;
}
uint16_t InetAddress::to_port() const
{
return ntohs(port_net_endian());
uint16_t InetAddress::to_port() const {
return ntohs(port_net_endian());
}
bool InetAddress::is_ip_v6() const
{
return _is_ip_v6;
bool InetAddress::is_ip_v6() const {
return _is_ip_v6;
}
InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6)
: _is_ip_v6(ipv6)
{
if (ipv6)
{
memset(&_addr6, 0, sizeof(_addr6));
_addr6.sin6_family = AF_INET6;
InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6) :
_is_ip_v6(ipv6) {
if (ipv6) {
memset(&_addr6, 0, sizeof(_addr6));
_addr6.sin6_family = AF_INET6;
in6_addr ip = loopbackOnly ? in6addr_loopback : in6addr_any;
in6_addr ip = loopbackOnly ? in6addr_loopback : in6addr_any;
_addr6.sin6_addr = ip;
_addr6.sin6_port = htons(port);
}
else
{
memset(&_addr, 0, sizeof(_addr));
_addr.sin_family = AF_INET;
_addr6.sin6_addr = ip;
_addr6.sin6_port = htons(port);
} else {
memset(&_addr, 0, sizeof(_addr));
_addr.sin_family = AF_INET;
in_addr_t ip = loopbackOnly ? kInaddrLoopback : kInaddrAny;
in_addr_t ip = loopbackOnly ? kInaddrLoopback : kInaddrAny;
_addr.sin_addr.s_addr = htonl(ip);
_addr.sin_port = htons(port);
}
_is_unspecified = false;
_addr.sin_addr.s_addr = htonl(ip);
_addr.sin_port = htons(port);
}
_is_unspecified = false;
}
InetAddress::InetAddress(const String &ip, uint16_t port, bool ipv6)
: _is_ip_v6(ipv6)
{
if (ipv6)
{
memset(&_addr6, 0, sizeof(_addr6));
_addr6.sin6_family = AF_INET6;
_addr6.sin6_port = htons(port);
InetAddress::InetAddress(const String &ip, uint16_t port, bool ipv6) :
_is_ip_v6(ipv6) {
if (ipv6) {
memset(&_addr6, 0, sizeof(_addr6));
_addr6.sin6_family = AF_INET6;
_addr6.sin6_port = htons(port);
if (::inet_pton(AF_INET6, ip.c_str(), &_addr6.sin6_addr) <= 0)
{
return;
}
}
else
{
memset(&_addr, 0, sizeof(_addr));
_addr.sin_family = AF_INET;
_addr.sin_port = htons(port);
if (::inet_pton(AF_INET6, ip.utf8().get_data(), &_addr6.sin6_addr) <= 0) {
return;
}
} else {
memset(&_addr, 0, sizeof(_addr));
_addr.sin_family = AF_INET;
_addr.sin_port = htons(port);
if (::inet_pton(AF_INET, ip.c_str(), &_addr.sin_addr) <= 0)
{
return;
}
}
_is_unspecified = false;
if (::inet_pton(AF_INET, ip.utf8().get_data(), &_addr.sin_addr) <= 0) {
return;
}
}
_is_unspecified = false;
}

View File

@ -24,7 +24,7 @@
//On windows link to ws2_32
#include<inttypes.h>
#include <inttypes.h>
#ifdef _WIN32
#include <ws2tcpip.h>
@ -32,65 +32,58 @@ using sa_family_t = unsigned short;
using in_addr_t = uint32_t;
using uint16_t = unsigned short;
#else
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include "core/string.h"
#include <unordered_map>
#include <mutex>
class InetAddress
{
public:
#include "core/ustring.h"
sa_family_t family() const;
class InetAddress {
public:
sa_family_t family() const;
String to_ip() const;
String to_ip_port() const;
String to_ip() const;
String to_ip_port() const;
uint16_t to_port() const;
uint16_t to_port() const;
bool is_ip_v6() const;
bool is_intranet_ip() const;
bool is_loopback_ip() const;
bool is_ip_v6() const;
bool is_intranet_ip() const;
bool is_loopback_ip() const;
const struct sockaddr *get_sock_addr() const;
const struct sockaddr *get_sock_addr() const;
void set_sock_addr_inet6(const struct sockaddr_in6 &addr6);
void set_sock_addr_inet6(const struct sockaddr_in6 &addr6);
uint32_t ip_net_endian() const;
const uint32_t *ip6_net_endian() const;
uint32_t ip_net_endian() const;
const uint32_t *ip6_net_endian() const;
uint16_t port_net_endian() const;
uint16_t port_net_endian() const;
void set_port_net_endian(uint16_t port);
void set_port_net_endian(uint16_t port);
inline bool is_unspecified() const;
inline bool is_unspecified() const;
InetAddress(uint16_t port = 0, bool loopbackOnly = false, bool ipv6 = false);
InetAddress(const String &ip, uint16_t port, bool ipv6 = false);
InetAddress(uint16_t port = 0, bool loopbackOnly = false, bool ipv6 = false);
InetAddress(const String &ip, uint16_t port, bool ipv6 = false);
explicit InetAddress(const struct sockaddr_in &addr)
: _addr(addr), _is_unspecified(false)
{
}
explicit InetAddress(const struct sockaddr_in &addr) :
_addr(addr), _is_unspecified(false) {
}
explicit InetAddress(const struct sockaddr_in6 &addr)
: _addr6(addr), _is_ip_v6(true), _is_unspecified(false)
{
}
explicit InetAddress(const struct sockaddr_in6 &addr) :
_addr6(addr), _is_ip_v6(true), _is_unspecified(false) {
}
private:
union {
struct sockaddr_in _addr;
struct sockaddr_in6 _addr6;
};
private:
union
{
struct sockaddr_in _addr;
struct sockaddr_in6 _addr6;
};
bool _is_ip_v6 {false};
bool _is_unspecified {true};
bool _is_ip_v6{ false };
bool _is_unspecified{ true };
};
#endif

View File

@ -23,319 +23,273 @@
///usr/include/asm-generic/errno-base.h
//http://www.virtsync.com/c-error-codes-include-errno
#include<cerrno>
#include <cerrno>
#ifdef _WIN32
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#endif
void Socket::create_net_socket()
{
create(AF_INET);
#include "core/error_macros.h"
#include "core/ustring.h"
void Socket::create_net_socket() {
create(AF_INET);
}
void Socket::create(int family)
{
void Socket::create(int family) {
#ifdef __linux__
_socket = ::socket(family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP);
_socket = ::socket(family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP);
#else
_socket = static_cast<int>(::socket(family, SOCK_STREAM, IPPROTO_TCP));
_socket = static_cast<int>(::socket(family, SOCK_STREAM, IPPROTO_TCP));
#endif
}
// taken from muduo
void Socket::set_non_block_and_close_on_exit()
{
void Socket::set_non_block_and_close_on_exit() {
#ifdef _WIN32
// TODO how to set FD_CLOEXEC on windows? is it necessary?
u_long arg = 1;
auto ret = ioctlsocket(_socket, (long)FIONBIO, &arg);
// TODO how to set FD_CLOEXEC on windows? is it necessary?
u_long arg = 1;
auto ret = ioctlsocket(_socket, (long)FIONBIO, &arg);
if (ret)
{
LOG_ERR("ioctlsocket error");
}
if (ret) {
LOG_ERR("ioctlsocket error");
}
#else
// non-block
int flags = ::fcntl(_socket, F_GETFL, 0);
flags |= O_NONBLOCK;
int ret = ::fcntl(_socket, F_SETFL, flags);
// TODO check
// non-block
int flags = ::fcntl(_socket, F_GETFL, 0);
flags |= O_NONBLOCK;
int ret = ::fcntl(_socket, F_SETFL, flags);
// TODO check
// close-on-exec
flags = ::fcntl(_socket, F_GETFD, 0);
flags |= FD_CLOEXEC;
ret = ::fcntl(_socket, F_SETFD, flags);
// TODO check
// close-on-exec
flags = ::fcntl(_socket, F_GETFD, 0);
flags |= FD_CLOEXEC;
ret = ::fcntl(_socket, F_SETFD, flags);
// TODO check
(void)ret;
(void)ret;
#endif
}
int Socket::get_error()
{
int optval;
int Socket::get_error() {
int optval;
socklen_t optlen = static_cast<socklen_t>(sizeof optval);
socklen_t optlen = static_cast<socklen_t>(sizeof optval);
#ifdef _WIN32
if (::getsockopt(_socket, SOL_SOCKET, SO_ERROR, (char *)&optval, &optlen) < 0)
if (::getsockopt(_socket, SOL_SOCKET, SO_ERROR, (char *)&optval, &optlen) < 0)
#else
if (::getsockopt(_socket, SOL_SOCKET, SO_ERROR, &optval, &optlen) < 0)
if (::getsockopt(_socket, SOL_SOCKET, SO_ERROR, &optval, &optlen) < 0)
#endif
{
return errno;
}
else
{
return optval;
}
{
return errno;
} else {
return optval;
}
}
int Socket::connect(const InetAddress &addr)
{
if (addr.is_ip_v6())
{
return ::connect(_socket, addr.get_sock_addr(), static_cast<socklen_t>(sizeof(struct sockaddr_in6)));
}
else
{
return ::connect(_socket, addr.get_sock_addr(), static_cast<socklen_t>(sizeof(struct sockaddr_in)));
}
int Socket::connect(const InetAddress &addr) {
if (addr.is_ip_v6()) {
return ::connect(_socket, addr.get_sock_addr(), static_cast<socklen_t>(sizeof(struct sockaddr_in6)));
} else {
return ::connect(_socket, addr.get_sock_addr(), static_cast<socklen_t>(sizeof(struct sockaddr_in)));
}
}
bool Socket::is_self_connect()
{
struct sockaddr_in6 localaddr = get_local_addr();
struct sockaddr_in6 peeraddr = get_peer_addr();
bool Socket::is_self_connect() {
struct sockaddr_in6 localaddr = get_local_addr();
struct sockaddr_in6 peeraddr = get_peer_addr();
if (localaddr.sin6_family == AF_INET)
{
const struct sockaddr_in *laddr4 = reinterpret_cast<struct sockaddr_in *>(&localaddr);
const struct sockaddr_in *raddr4 = reinterpret_cast<struct sockaddr_in *>(&peeraddr);
return laddr4->sin_port == raddr4->sin_port && laddr4->sin_addr.s_addr == raddr4->sin_addr.s_addr;
}
else if (localaddr.sin6_family == AF_INET6)
{
return localaddr.sin6_port == peeraddr.sin6_port && memcmp(&localaddr.sin6_addr, &peeraddr.sin6_addr, sizeof localaddr.sin6_addr) == 0;
}
else
{
return false;
}
if (localaddr.sin6_family == AF_INET) {
const struct sockaddr_in *laddr4 = reinterpret_cast<struct sockaddr_in *>(&localaddr);
const struct sockaddr_in *raddr4 = reinterpret_cast<struct sockaddr_in *>(&peeraddr);
return laddr4->sin_port == raddr4->sin_port && laddr4->sin_addr.s_addr == raddr4->sin_addr.s_addr;
} else if (localaddr.sin6_family == AF_INET6) {
return localaddr.sin6_port == peeraddr.sin6_port && memcmp(&localaddr.sin6_addr, &peeraddr.sin6_addr, sizeof localaddr.sin6_addr) == 0;
} else {
return false;
}
}
void Socket::bind_address(const InetAddress &address)
{
ERR_FAIL_COND(_socket == 0);
void Socket::bind_address(const InetAddress &address) {
ERR_FAIL_COND(_socket == 0);
int ret;
if (address.is_ip_v6())
{
ret = ::bind(_socket, address.get_sock_addr(), sizeof(sockaddr_in6));
}
else
{
ret = ::bind(_socket, address.get_sock_addr(), sizeof(sockaddr_in));
}
if (ret != 0)
{
RLOG_ERR("Bind address failed:");
RLOG_ERR(address.to_ip_port());
int ret;
if (address.is_ip_v6()) {
ret = ::bind(_socket, address.get_sock_addr(), sizeof(sockaddr_in6));
} else {
ret = ::bind(_socket, address.get_sock_addr(), sizeof(sockaddr_in));
}
if (ret != 0) {
#ifdef _WIN32
exit(WSAGetLastError());
LOG_ERR("Bind address failed: " + address.to_ip_port() + " " + itos(WSAGetLastError()));
#else
exit(errno);
LOG_ERR("Bind address failed: " + address.to_ip_port() + " " + itos(errno));
#endif
}
}
}
void Socket::listen()
{
ERR_FAIL_COND(_socket == 0);
void Socket::listen() {
ERR_FAIL_COND(_socket == 0);
int ret = ::listen(_socket, SOMAXCONN);
if (ret < 0)
{
RLOG_ERR("listen failed");
exit(1);
}
int ret = ::listen(_socket, SOMAXCONN);
if (ret < 0) {
LOG_ERR("listen failed");
}
}
int Socket::accept(Socket *sock)
{
struct sockaddr_in6 addr6;
memset(&addr6, 0, sizeof(addr6));
socklen_t size = sizeof(addr6);
int Socket::accept(Socket *sock) {
struct sockaddr_in6 addr6;
memset(&addr6, 0, sizeof(addr6));
socklen_t size = sizeof(addr6);
#ifdef __linux__
int connfd = ::accept4(_socket, (struct sockaddr *)&addr6, &size, SOCK_NONBLOCK | SOCK_CLOEXEC);
int connfd = ::accept4(_socket, (struct sockaddr *)&addr6, &size, SOCK_NONBLOCK | SOCK_CLOEXEC);
#else
int connfd = static_cast<int>(::accept(_socket, (struct sockaddr *)&addr6, &size));
int connfd = static_cast<int>(::accept(_socket, (struct sockaddr *)&addr6, &size));
#endif
if (connfd >= 0)
{
sock->_socket = connfd;
sock->_address.set_sock_addr_inet6(addr6);
if (connfd >= 0) {
sock->_socket = connfd;
sock->_address.set_sock_addr_inet6(addr6);
#ifndef __linux__
sock->set_non_block_and_close_on_exit();
sock->set_non_block_and_close_on_exit();
#endif
}
}
return connfd;
return connfd;
}
void Socket::close_write()
{
void Socket::close_write() {
#ifndef _WIN32
if (::shutdown(_socket, SHUT_WR) < 0)
if (::shutdown(_socket, SHUT_WR) < 0)
#else
if (::shutdown(_socket, SD_SEND) < 0)
if (::shutdown(_socket, SD_SEND) < 0)
#endif
{
RLOG_ERR("sockets::shutdownwrite");
}
{
LOG_ERR("sockets::shutdownwrite");
}
}
int Socket::read(char *buffer, uint64_t len)
{
int Socket::read(char *buffer, uint64_t len) {
#ifndef _WIN32
return ::read(_socket, buffer, len);
return ::read(_socket, buffer, len);
#else
return recv(_socket, buffer, static_cast<int>(len), 0);
return recv(_socket, buffer, static_cast<int>(len), 0);
#endif
}
int Socket::send(const char *buffer, uint64_t len)
{
int Socket::send(const char *buffer, uint64_t len) {
#ifndef _WIN32
return write(_socket, buffer, len);
return write(_socket, buffer, len);
#else
errno = 0;
return ::send(_socket, buffer, static_cast<int>(len), 0);
errno = 0;
return ::send(_socket, buffer, static_cast<int>(len), 0);
#endif
}
void Socket::set_tcp_nodelay(bool on)
{
void Socket::set_tcp_nodelay(bool on) {
#ifdef _WIN32
char optval = on ? 1 : 0;
char optval = on ? 1 : 0;
#else
int optval = on ? 1 : 0;
int optval = on ? 1 : 0;
#endif
::setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, &optval, static_cast<socklen_t>(sizeof optval));
::setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, &optval, static_cast<socklen_t>(sizeof optval));
}
void Socket::set_reuse_addr(bool on)
{
void Socket::set_reuse_addr(bool on) {
#ifdef _WIN32
char optval = on ? 1 : 0;
char optval = on ? 1 : 0;
#else
int optval = on ? 1 : 0;
int optval = on ? 1 : 0;
#endif
::setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, &optval, static_cast<socklen_t>(sizeof optval));
::setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, &optval, static_cast<socklen_t>(sizeof optval));
}
void Socket::set_reuse_port(bool on)
{
void Socket::set_reuse_port(bool on) {
#ifdef SO_REUSEPORT
#ifdef _WIN32
char optval = on ? 1 : 0;
char optval = on ? 1 : 0;
#else
int optval = on ? 1 : 0;
int optval = on ? 1 : 0;
#endif
int ret = ::setsockopt(_socket, SOL_SOCKET, SO_REUSEPORT, &optval, static_cast<socklen_t>(sizeof optval));
int ret = ::setsockopt(_socket, SOL_SOCKET, SO_REUSEPORT, &optval, static_cast<socklen_t>(sizeof optval));
if (ret < 0 && on)
{
RLOG_ERR("SO_REUSEPORT failed.");
}
if (ret < 0 && on) {
LOG_ERR("SO_REUSEPORT failed.");
}
#else
if (on)
{
RLOG_ERR("SO_REUSEPORT is not supported.");
}
if (on) {
LOG_ERR("SO_REUSEPORT is not supported.");
}
#endif
}
void Socket::set_keep_alive(bool on)
{
void Socket::set_keep_alive(bool on) {
#ifdef _WIN32
char optval = on ? 1 : 0;
char optval = on ? 1 : 0;
#else
int optval = on ? 1 : 0;
int optval = on ? 1 : 0;
#endif
::setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, &optval, static_cast<socklen_t>(sizeof optval));
::setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, &optval, static_cast<socklen_t>(sizeof optval));
}
struct sockaddr_in6 Socket::get_local_addr()
{
struct sockaddr_in6 localaddr;
memset(&localaddr, 0, sizeof(localaddr));
socklen_t addrlen = static_cast<socklen_t>(sizeof localaddr);
struct sockaddr_in6 Socket::get_local_addr() {
struct sockaddr_in6 localaddr;
memset(&localaddr, 0, sizeof(localaddr));
socklen_t addrlen = static_cast<socklen_t>(sizeof localaddr);
if (::getsockname(_socket, static_cast<struct sockaddr *>((void *)(&localaddr)), &addrlen) < 0)
{
RLOG_ERR("sockets::getLocalAddr");
}
if (::getsockname(_socket, static_cast<struct sockaddr *>((void *)(&localaddr)), &addrlen) < 0) {
LOG_ERR("sockets::getLocalAddr");
}
return localaddr;
return localaddr;
}
struct sockaddr_in6 Socket::get_peer_addr()
{
struct sockaddr_in6 peeraddr;
memset(&peeraddr, 0, sizeof(peeraddr));
socklen_t addrlen = static_cast<socklen_t>(sizeof peeraddr);
struct sockaddr_in6 Socket::get_peer_addr() {
struct sockaddr_in6 peeraddr;
memset(&peeraddr, 0, sizeof(peeraddr));
socklen_t addrlen = static_cast<socklen_t>(sizeof peeraddr);
if (::getpeername(_socket, static_cast<struct sockaddr *>((void *)(&peeraddr)), &addrlen) < 0)
{
RLOG_ERR("sockets::getPeerAddr");
}
if (::getpeername(_socket, static_cast<struct sockaddr *>((void *)(&peeraddr)), &addrlen) < 0) {
LOG_ERR("sockets::getPeerAddr");
}
return peeraddr;
return peeraddr;
}
int Socket::global_init()
{
int Socket::global_init() {
#ifdef _WIN32
int r;
WSADATA wsa_data;
int r;
WSADATA wsa_data;
r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
return r;
return r;
#else
return 0;
return 0;
#endif
}
Socket::Socket()
{
_socket = 0;
Socket::Socket() {
_socket = 0;
}
Socket::Socket(int socketFD, const InetAddress &address)
{
_socket = socketFD;
_address = address;
Socket::Socket(int socketFD, const InetAddress &address) {
_socket = socketFD;
_address = address;
}
Socket::~Socket()
{
if (_socket >= 0)
{
Socket::~Socket() {
if (_socket >= 0) {
#ifndef _WIN32
close(_socket);
close(_socket);
#else
closesocket(_socket);
closesocket(_socket);
#endif
}
}
}

View File

@ -17,9 +17,6 @@
*
*/
#include "core/error_macros.h"
#include "core/string.h"
#ifndef _WIN32
#include <unistd.h>
#endif