mirror of
https://github.com/Relintai/sfw.git
synced 2024-11-08 07:52:09 +01:00
Fix build of Socket and InetAddress.
This commit is contained in:
parent
e5b7ff3491
commit
df92131fa2
@ -10,6 +10,7 @@
|
||||
|
||||
#include "inet_address.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#ifdef _WIN32
|
||||
@ -27,24 +28,22 @@
|
||||
}
|
||||
#else
|
||||
// Windows...
|
||||
#include <winsock2.h>
|
||||
#include <in6addr.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
struct in6__addruint
|
||||
{
|
||||
union
|
||||
{
|
||||
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,8 +79,7 @@ static const in_addr_t kInaddrLoopback = INADDR_ANY;
|
||||
#endif
|
||||
*/
|
||||
|
||||
String InetAddress::to_ip_port() const
|
||||
{
|
||||
String InetAddress::to_ip_port() const {
|
||||
char buf[64] = "";
|
||||
uint16_t port = ntohs(_addr.sin_port);
|
||||
snprintf(buf, sizeof(buf), ":%u", port);
|
||||
@ -89,10 +87,8 @@ String InetAddress::to_ip_port() const
|
||||
return to_ip() + String(buf);
|
||||
}
|
||||
|
||||
bool InetAddress::is_intranet_ip() const
|
||||
{
|
||||
if (_addr.sin_family == AF_INET)
|
||||
{
|
||||
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) ||
|
||||
@ -102,9 +98,7 @@ bool InetAddress::is_intranet_ip() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
auto addrP = ip6_net_endian();
|
||||
// Loopback ip
|
||||
if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 &&
|
||||
@ -114,8 +108,7 @@ bool InetAddress::is_intranet_ip() const
|
||||
auto i32 = (ntohl(*addrP) & 0xffc00000);
|
||||
if (i32 == 0xfec00000 || i32 == 0xfe800000)
|
||||
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
|
||||
uint32_t ip_addr = ntohl(*(addrP + 3));
|
||||
if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) ||
|
||||
@ -131,18 +124,13 @@ bool InetAddress::is_intranet_ip() const
|
||||
return false;
|
||||
}
|
||||
|
||||
bool InetAddress::is_loopback_ip() const
|
||||
{
|
||||
if (!is_ip_v6())
|
||||
{
|
||||
bool InetAddress::is_loopback_ip() const {
|
||||
if (!is_ip_v6()) {
|
||||
uint32_t ip_addr = ntohl(_addr.sin_addr.s_addr);
|
||||
if (ip_addr == 0x7f000001)
|
||||
{
|
||||
if (ip_addr == 0x7f000001) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
auto addrP = ip6_net_endian();
|
||||
if (*addrP == 0 && *(addrP + 1) == 0 && *(addrP + 2) == 0 &&
|
||||
ntohl(*(addrP + 3)) == 1)
|
||||
@ -155,36 +143,29 @@ bool InetAddress::is_loopback_ip() const
|
||||
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));
|
||||
}
|
||||
|
||||
void InetAddress::set_sock_addr_inet6(const struct sockaddr_in6 &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;
|
||||
}
|
||||
|
||||
sa_family_t InetAddress::family() const
|
||||
{
|
||||
sa_family_t InetAddress::family() const {
|
||||
return _addr.sin_family;
|
||||
}
|
||||
|
||||
String InetAddress::to_ip() const
|
||||
{
|
||||
String InetAddress::to_ip() const {
|
||||
char buf[64];
|
||||
if (_addr.sin_family == AF_INET)
|
||||
{
|
||||
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));
|
||||
#else
|
||||
::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));
|
||||
#else
|
||||
@ -195,14 +176,12 @@ String InetAddress::to_ip() const
|
||||
return buf;
|
||||
}
|
||||
|
||||
uint32_t InetAddress::ip_net_endian() const
|
||||
{
|
||||
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;
|
||||
@ -216,36 +195,29 @@ const uint32_t *InetAddress::ip6_net_endian() const
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t InetAddress::port_net_endian() const
|
||||
{
|
||||
uint16_t InetAddress::port_net_endian() const {
|
||||
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;
|
||||
}
|
||||
|
||||
inline bool InetAddress::is_unspecified() const
|
||||
{
|
||||
inline bool InetAddress::is_unspecified() const {
|
||||
return _is_unspecified;
|
||||
}
|
||||
|
||||
uint16_t InetAddress::to_port() const
|
||||
{
|
||||
uint16_t InetAddress::to_port() const {
|
||||
return ntohs(port_net_endian());
|
||||
}
|
||||
|
||||
bool InetAddress::is_ip_v6() const
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
||||
@ -253,9 +225,7 @@ InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6)
|
||||
|
||||
_addr6.sin6_addr = ip;
|
||||
_addr6.sin6_port = htons(port);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
memset(&_addr, 0, sizeof(_addr));
|
||||
_addr.sin_family = AF_INET;
|
||||
|
||||
@ -267,31 +237,24 @@ InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6)
|
||||
_is_unspecified = false;
|
||||
}
|
||||
|
||||
InetAddress::InetAddress(const String &ip, uint16_t port, bool ipv6)
|
||||
: _is_ip_v6(ipv6)
|
||||
{
|
||||
if (ipv6)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (::inet_pton(AF_INET6, ip.utf8().get_data(), &_addr6.sin6_addr) <= 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} 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)
|
||||
{
|
||||
if (::inet_pton(AF_INET, ip.utf8().get_data(), &_addr.sin_addr) <= 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
_is_unspecified = false;
|
||||
}
|
||||
|
||||
|
@ -32,18 +32,15 @@ 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
|
||||
{
|
||||
#include "core/ustring.h"
|
||||
|
||||
class InetAddress {
|
||||
public:
|
||||
|
||||
sa_family_t family() const;
|
||||
|
||||
String to_ip() const;
|
||||
@ -71,20 +68,16 @@ class InetAddress
|
||||
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
|
||||
{
|
||||
union {
|
||||
struct sockaddr_in _addr;
|
||||
struct sockaddr_in6 _addr6;
|
||||
};
|
||||
|
@ -28,17 +28,18 @@
|
||||
#ifdef _WIN32
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
void Socket::create_net_socket()
|
||||
{
|
||||
#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);
|
||||
#else
|
||||
@ -47,15 +48,13 @@ void Socket::create(int family)
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
if (ret)
|
||||
{
|
||||
if (ret) {
|
||||
LOG_ERR("ioctlsocket error");
|
||||
}
|
||||
#else
|
||||
@ -75,8 +74,7 @@ void Socket::set_non_block_and_close_on_exit()
|
||||
#endif
|
||||
}
|
||||
|
||||
int Socket::get_error()
|
||||
{
|
||||
int Socket::get_error() {
|
||||
int optval;
|
||||
|
||||
socklen_t optlen = static_cast<socklen_t>(sizeof optval);
|
||||
@ -88,88 +86,63 @@ int Socket::get_error()
|
||||
#endif
|
||||
{
|
||||
return errno;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return optval;
|
||||
}
|
||||
}
|
||||
|
||||
int Socket::connect(const InetAddress &addr)
|
||||
{
|
||||
if (addr.is_ip_v6())
|
||||
{
|
||||
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
|
||||
{
|
||||
} 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 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 *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)
|
||||
{
|
||||
} 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
|
||||
{
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::bind_address(const InetAddress &address)
|
||||
{
|
||||
void Socket::bind_address(const InetAddress &address) {
|
||||
ERR_FAIL_COND(_socket == 0);
|
||||
|
||||
int ret;
|
||||
if (address.is_ip_v6())
|
||||
{
|
||||
if (address.is_ip_v6()) {
|
||||
ret = ::bind(_socket, address.get_sock_addr(), sizeof(sockaddr_in6));
|
||||
}
|
||||
else
|
||||
{
|
||||
} 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
|
||||
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()
|
||||
{
|
||||
void Socket::listen() {
|
||||
ERR_FAIL_COND(_socket == 0);
|
||||
|
||||
int ret = ::listen(_socket, SOMAXCONN);
|
||||
if (ret < 0)
|
||||
{
|
||||
RLOG_ERR("listen failed");
|
||||
|
||||
exit(1);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("listen failed");
|
||||
}
|
||||
}
|
||||
|
||||
int Socket::accept(Socket *sock)
|
||||
{
|
||||
int Socket::accept(Socket *sock) {
|
||||
struct sockaddr_in6 addr6;
|
||||
memset(&addr6, 0, 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));
|
||||
#endif
|
||||
|
||||
if (connfd >= 0)
|
||||
{
|
||||
if (connfd >= 0) {
|
||||
sock->_socket = connfd;
|
||||
sock->_address.set_sock_addr_inet6(addr6);
|
||||
#ifndef __linux__
|
||||
@ -192,20 +164,18 @@ int Socket::accept(Socket *sock)
|
||||
return connfd;
|
||||
}
|
||||
|
||||
void Socket::close_write()
|
||||
{
|
||||
void Socket::close_write() {
|
||||
#ifndef _WIN32
|
||||
if (::shutdown(_socket, SHUT_WR) < 0)
|
||||
#else
|
||||
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);
|
||||
#else
|
||||
@ -213,8 +183,7 @@ int Socket::read(char *buffer, uint64_t len)
|
||||
#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);
|
||||
#else
|
||||
@ -223,8 +192,7 @@ int Socket::send(const char *buffer, uint64_t len)
|
||||
#endif
|
||||
}
|
||||
|
||||
void Socket::set_tcp_nodelay(bool on)
|
||||
{
|
||||
void Socket::set_tcp_nodelay(bool on) {
|
||||
#ifdef _WIN32
|
||||
char optval = on ? 1 : 0;
|
||||
#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));
|
||||
}
|
||||
|
||||
void Socket::set_reuse_addr(bool on)
|
||||
{
|
||||
void Socket::set_reuse_addr(bool on) {
|
||||
#ifdef _WIN32
|
||||
char optval = on ? 1 : 0;
|
||||
#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));
|
||||
}
|
||||
|
||||
void Socket::set_reuse_port(bool on)
|
||||
{
|
||||
void Socket::set_reuse_port(bool on) {
|
||||
#ifdef SO_REUSEPORT
|
||||
#ifdef _WIN32
|
||||
char optval = on ? 1 : 0;
|
||||
@ -253,20 +219,17 @@ void Socket::set_reuse_port(bool on)
|
||||
#endif
|
||||
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;
|
||||
#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));
|
||||
}
|
||||
|
||||
struct sockaddr_in6 Socket::get_local_addr()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
struct sockaddr_in6 Socket::get_peer_addr()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
int Socket::global_init()
|
||||
{
|
||||
int Socket::global_init() {
|
||||
#ifdef _WIN32
|
||||
int r;
|
||||
WSADATA wsa_data;
|
||||
@ -317,21 +275,17 @@ int Socket::global_init()
|
||||
#endif
|
||||
}
|
||||
|
||||
Socket::Socket()
|
||||
{
|
||||
Socket::Socket() {
|
||||
_socket = 0;
|
||||
}
|
||||
|
||||
Socket::Socket(int socketFD, const InetAddress &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);
|
||||
#else
|
||||
|
@ -17,9 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "core/error_macros.h"
|
||||
#include "core/string.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user