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

View File

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

View File

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

View File

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