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,6 +10,7 @@
#include "inet_address.h" #include "inet_address.h"
#include <cstdio>
#include <cstring> #include <cstring>
#ifdef _WIN32 #ifdef _WIN32
@ -27,24 +28,22 @@
} }
#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_char Byte[16];
u_short Word[8]; u_short Word[8];
uint32_t __s6_addr32[4]; uint32_t __s6_addr32[4];
} uext; } 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,8 +79,7 @@ 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);
@ -89,10 +87,8 @@ String InetAddress::to_ip_port() const
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); uint32_t ip_addr = ntohl(_addr.sin_addr.s_addr);
if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) || if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) ||
(ip_addr >= 0xAC100000 && ip_addr <= 0xAC1FFFFF) || (ip_addr >= 0xAC100000 && ip_addr <= 0xAC1FFFFF) ||
@ -102,9 +98,7 @@ bool InetAddress::is_intranet_ip() const
{ {
return true; return true;
} }
} } else {
else
{
auto addrP = ip6_net_endian(); auto addrP = ip6_net_endian();
// Loopback ip // Loopback ip
if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 && if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 &&
@ -114,8 +108,7 @@ bool InetAddress::is_intranet_ip() const
auto i32 = (ntohl(*addrP) & 0xffc00000); auto i32 = (ntohl(*addrP) & 0xffc00000);
if (i32 == 0xfec00000 || i32 == 0xfe800000) if (i32 == 0xfec00000 || i32 == 0xfe800000)
return true; return true;
if (*addrP == 0 && *(addrP + 1) == 0 && ntohl(*(addrP + 2)) == 0xffff) if (*addrP == 0 && *(addrP + 1) == 0 && ntohl(*(addrP + 2)) == 0xffff) {
{
// the IPv6 version of an IPv4 IP address // the IPv6 version of an IPv4 IP address
uint32_t ip_addr = ntohl(*(addrP + 3)); uint32_t ip_addr = ntohl(*(addrP + 3));
if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) || if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) ||
@ -131,18 +124,13 @@ bool InetAddress::is_intranet_ip() const
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); uint32_t ip_addr = ntohl(_addr.sin_addr.s_addr);
if (ip_addr == 0x7f000001) if (ip_addr == 0x7f000001) {
{
return true; return true;
} }
} } else {
else
{
auto addrP = ip6_net_endian(); auto addrP = ip6_net_endian();
if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 && if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 &&
ntohl(*(addrP + 3)) == 1) ntohl(*(addrP + 3)) == 1)
@ -155,36 +143,29 @@ bool InetAddress::is_loopback_ip() const
return false; 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
@ -195,14 +176,12 @@ String InetAddress::to_ip() const
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;
@ -216,36 +195,29 @@ const uint32_t *InetAddress::ip6_net_endian() const
#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)); memset(&_addr6, 0, sizeof(_addr6));
_addr6.sin6_family = AF_INET6; _addr6.sin6_family = AF_INET6;
@ -253,9 +225,7 @@ InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6)
_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)); memset(&_addr, 0, sizeof(_addr));
_addr.sin_family = AF_INET; _addr.sin_family = AF_INET;
@ -267,31 +237,24 @@ InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6)
_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)); memset(&_addr6, 0, sizeof(_addr6));
_addr6.sin6_family = AF_INET6; _addr6.sin6_family = AF_INET6;
_addr6.sin6_port = htons(port); _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 {
else
{
memset(&_addr, 0, sizeof(_addr)); memset(&_addr, 0, sizeof(_addr));
_addr.sin_family = AF_INET; _addr.sin_family = AF_INET;
_addr.sin_port = htons(port); _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

@ -32,18 +32,15 @@ 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"
{
class InetAddress {
public: public:
sa_family_t family() const; sa_family_t family() const;
String to_ip() const; String to_ip() const;
@ -71,20 +68,16 @@ class InetAddress
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: private:
union union {
{
struct sockaddr_in _addr; struct sockaddr_in _addr;
struct sockaddr_in6 _addr6; struct sockaddr_in6 _addr6;
}; };

View File

@ -28,17 +28,18 @@
#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"
void Socket::create_net_socket() {
create(AF_INET); 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
@ -47,15 +48,13 @@ void Socket::create(int family)
} }
// 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
@ -75,8 +74,7 @@ void Socket::set_non_block_and_close_on_exit()
#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);
@ -88,88 +86,63 @@ int Socket::get_error()
#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))); return ::connect(_socket, addr.get_sock_addr(), static_cast<socklen_t>(sizeof(struct sockaddr_in6)));
} } else {
else
{
return ::connect(_socket, addr.get_sock_addr(), static_cast<socklen_t>(sizeof(struct sockaddr_in))); 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) {
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; return localaddr.sin6_port == peeraddr.sin6_port && memcmp(&localaddr.sin6_addr, &peeraddr.sin6_addr, sizeof localaddr.sin6_addr) == 0;
} } else {
else
{
return false; 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 {
else
{
ret = ::bind(_socket, address.get_sock_addr(), sizeof(sockaddr_in)); ret = ::bind(_socket, address.get_sock_addr(), sizeof(sockaddr_in));
} }
if (ret != 0) if (ret != 0) {
{
RLOG_ERR("Bind address failed:");
RLOG_ERR(address.to_ip_port());
#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);
@ -180,8 +153,7 @@ int Socket::accept(Socket *sock)
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__
@ -192,20 +164,18 @@ int Socket::accept(Socket *sock)
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
@ -213,8 +183,7 @@ int Socket::read(char *buffer, uint64_t len)
#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
@ -223,8 +192,7 @@ int Socket::send(const char *buffer, uint64_t len)
#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
@ -233,8 +201,7 @@ void Socket::set_tcp_nodelay(bool on)
::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
@ -243,8 +210,7 @@ void Socket::set_reuse_addr(bool on)
::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;
@ -253,20 +219,17 @@ void Socket::set_reuse_port(bool on)
#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
@ -275,36 +238,31 @@ void Socket::set_keep_alive(bool on)
::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;
@ -317,21 +275,17 @@ int Socket::global_init()
#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

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