Socket and InetAddress cleanups.

This commit is contained in:
Relintai 2024-01-13 09:48:27 +01:00
parent df92131fa2
commit fba59b99b9
4 changed files with 156 additions and 100 deletions

View File

@ -13,7 +13,7 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#ifdef _WIN32 #if defined(_WIN64) || defined(_WIN32)
#ifdef __GNUC__ #ifdef __GNUC__
#define GCCWIN #define GCCWIN
@ -102,12 +102,16 @@ bool InetAddress::is_intranet_ip() const {
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 &&
ntohl(*(addrP + 3)) == 1) ntohl(*(addrP + 3)) == 1) {
return true; return true;
}
// Privated ip is prefixed by FEC0::/10 or FE80::/10, need testing // Privated ip is prefixed by FEC0::/10 or FE80::/10, need testing
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));
@ -133,12 +137,14 @@ bool InetAddress::is_loopback_ip() const {
} 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) {
return true; return true;
}
// the IPv6 version of an IPv4 loopback address // the IPv6 version of an IPv4 loopback address
if (*addrP == 0 && *(addrP + 1) == 0 && ntohl(*(addrP + 2)) == 0xffff && if (*addrP == 0 && *(addrP + 1) == 0 && ntohl(*(addrP + 2)) == 0xffff &&
ntohl(*(addrP + 3)) == 0x7f000001) ntohl(*(addrP + 3)) == 0x7f000001) {
return true; return true;
}
} }
return false; return false;
} }
@ -185,7 +191,7 @@ 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(_WIN64) || 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);
@ -215,8 +221,9 @@ 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;
@ -234,11 +241,13 @@ InetAddress::InetAddress(uint16_t port, bool loopbackOnly, bool ipv6) :
_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)); memset(&_addr6, 0, sizeof(_addr6));
_addr6.sin6_family = AF_INET6; _addr6.sin6_family = AF_INET6;
@ -256,5 +265,6 @@ InetAddress::InetAddress(const String &ip, uint16_t port, bool ipv6) :
return; return;
} }
} }
_is_unspecified = false; _is_unspecified = false;
} }

View File

@ -24,9 +24,9 @@
//On windows link to ws2_32 //On windows link to ws2_32
#include <inttypes.h> #include "int_types.h"
#ifdef _WIN32 #if defined(_WIN64) || defined(_WIN32)
#include <ws2tcpip.h> #include <ws2tcpip.h>
using sa_family_t = unsigned short; using sa_family_t = unsigned short;
using in_addr_t = uint32_t; using in_addr_t = uint32_t;
@ -37,7 +37,9 @@ using uint16_t = unsigned short;
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
//--STRIP
#include "core/ustring.h" #include "core/ustring.h"
//--STRIP
class InetAddress { class InetAddress {
public: public:
@ -68,12 +70,15 @@ public:
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:
@ -82,8 +87,8 @@ private:
struct sockaddr_in6 _addr6; struct sockaddr_in6 _addr6;
}; };
bool _is_ip_v6{ false }; bool _is_ip_v6;
bool _is_unspecified{ true }; bool _is_unspecified;
}; };
#endif #endif

View File

@ -16,7 +16,7 @@
* *
*/ */
#ifndef _WIN32 #if !defined(_WIN64) && !defined(_WIN32)
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <fcntl.h> #include <fcntl.h>
@ -25,15 +25,17 @@
//http://www.virtsync.com/c-error-codes-include-errno //http://www.virtsync.com/c-error-codes-include-errno
#include <cerrno> #include <cerrno>
#ifdef _WIN32 #if defined(_WIN64) || defined(_WIN32)
#include <ws2tcpip.h> #include <ws2tcpip.h>
#else #else
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
//--STRIP
#include "core/error_macros.h" #include "core/error_macros.h"
#include "core/ustring.h" #include "core/ustring.h"
//--STRIP
void Socket::create_net_socket() { void Socket::create_net_socket() {
create(AF_INET); create(AF_INET);
@ -47,16 +49,35 @@ void Socket::create(int family) {
#endif #endif
} }
void Socket::close_socket() {
if (!_socket) {
return;
}
#if !defined(_WIN64) && !defined(_WIN32)
close(_socket);
#else
closesocket(_socket);
#endif
_socket = 0;
}
// taken from muduo // taken from muduo
void Socket::set_non_block_and_close_on_exit() { int Socket::set_non_block_and_close_on_exit() {
#ifdef _WIN32 ERR_FAIL_COND_V(_socket == 0, -1);
#if defined(_WIN64) || defined(_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");
return -1;
} }
return 0;
#else #else
// non-block // non-block
int flags = ::fcntl(_socket, F_GETFL, 0); int flags = ::fcntl(_socket, F_GETFL, 0);
@ -70,16 +91,18 @@ void Socket::set_non_block_and_close_on_exit() {
ret = ::fcntl(_socket, F_SETFD, flags); ret = ::fcntl(_socket, F_SETFD, flags);
// TODO check // TODO check
(void)ret; return ret;
#endif #endif
} }
int Socket::get_error() { int Socket::get_error() {
ERR_FAIL_COND_V(_socket == 0, -1);
int optval; int optval;
socklen_t optlen = static_cast<socklen_t>(sizeof optval); socklen_t optlen = static_cast<socklen_t>(sizeof optval);
#ifdef _WIN32 #if defined(_WIN64) || defined(_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)
@ -92,6 +115,8 @@ int Socket::get_error() {
} }
int Socket::connect(const InetAddress &addr) { int Socket::connect(const InetAddress &addr) {
ERR_FAIL_COND_V(_socket == 0, -1);
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 {
@ -100,6 +125,8 @@ int Socket::connect(const InetAddress &addr) {
} }
bool Socket::is_self_connect() { bool Socket::is_self_connect() {
ERR_FAIL_COND_V(_socket == 0, false);
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();
@ -114,8 +141,8 @@ bool Socket::is_self_connect() {
} }
} }
void Socket::bind_address(const InetAddress &address) { int Socket::bind_address(const InetAddress &address) {
ERR_FAIL_COND(_socket == 0); ERR_FAIL_COND_V(_socket == 0, -1);
int ret; int ret;
if (address.is_ip_v6()) { if (address.is_ip_v6()) {
@ -125,24 +152,23 @@ void Socket::bind_address(const InetAddress &address) {
} }
if (ret != 0) { if (ret != 0) {
#ifdef _WIN32 #if defined(_WIN64) || defined(_WIN32)
LOG_ERR("Bind address failed: " + address.to_ip_port() + " " + itos(WSAGetLastError())); return WSAGetLastError();
#else #else
LOG_ERR("Bind address failed: " + address.to_ip_port() + " " + itos(errno)); return errno;
#endif #endif
} }
} }
void Socket::listen() { int Socket::listen() {
ERR_FAIL_COND(_socket == 0); ERR_FAIL_COND_V(_socket == 0, -1);
int ret = ::listen(_socket, SOMAXCONN); return ::listen(_socket, SOMAXCONN);
if (ret < 0) {
LOG_ERR("listen failed");
}
} }
int Socket::accept(Socket *sock) { int Socket::accept(Socket *sock) {
ERR_FAIL_COND_V(!sock, -1);
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);
@ -164,19 +190,20 @@ int Socket::accept(Socket *sock) {
return connfd; return connfd;
} }
void Socket::close_write() { int Socket::close_write() {
#ifndef _WIN32 ERR_FAIL_COND_V(_socket == 0, -1);
if (::shutdown(_socket, SHUT_WR) < 0)
#if !defined(_WIN64) && !defined(_WIN32)
return ::shutdown(_socket, SHUT_WR);
#else #else
if (::shutdown(_socket, SD_SEND) < 0) return ::shutdown(_socket, SD_SEND);
#endif #endif
{
LOG_ERR("sockets::shutdownwrite");
}
} }
int Socket::read(char *buffer, uint64_t len) { int Socket::read(char *buffer, uint64_t len) {
#ifndef _WIN32 //ERR_FAIL_COND_V(_socket == 0, -1);
#if !defined(_WIN64) && !defined(_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);
@ -184,7 +211,9 @@ int Socket::read(char *buffer, uint64_t len) {
} }
int Socket::send(const char *buffer, uint64_t len) { int Socket::send(const char *buffer, uint64_t len) {
#ifndef _WIN32 //ERR_FAIL_COND_V(_socket == 0, -1);
#if !defined(_WIN64) && !defined(_WIN32)
return write(_socket, buffer, len); return write(_socket, buffer, len);
#else #else
errno = 0; errno = 0;
@ -193,7 +222,9 @@ int Socket::send(const char *buffer, uint64_t len) {
} }
void Socket::set_tcp_nodelay(bool on) { void Socket::set_tcp_nodelay(bool on) {
#ifdef _WIN32 ERR_FAIL_COND(_socket == 0);
#if defined(_WIN64) || defined(_WIN32)
char optval = on ? 1 : 0; char optval = on ? 1 : 0;
#else #else
int optval = on ? 1 : 0; int optval = on ? 1 : 0;
@ -202,7 +233,9 @@ void Socket::set_tcp_nodelay(bool on) {
} }
void Socket::set_reuse_addr(bool on) { void Socket::set_reuse_addr(bool on) {
#ifdef _WIN32 ERR_FAIL_COND(_socket == 0);
#if defined(_WIN64) || defined(_WIN32)
char optval = on ? 1 : 0; char optval = on ? 1 : 0;
#else #else
int optval = on ? 1 : 0; int optval = on ? 1 : 0;
@ -210,27 +243,32 @@ 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) { int Socket::set_reuse_port(bool on) {
ERR_FAIL_COND_V(_socket == 0, -1);
#ifdef SO_REUSEPORT #ifdef SO_REUSEPORT
#ifdef _WIN32 #if defined(_WIN64) || defined(_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) { return ret;
LOG_ERR("SO_REUSEPORT failed.");
}
#else #else
if (on) { if (on) {
LOG_ERR("SO_REUSEPORT is not supported."); //LOG_ERR("SO_REUSEPORT is not supported.");
return -1;
} }
return 0;
#endif #endif
} }
void Socket::set_keep_alive(bool on) { void Socket::set_keep_alive(bool on) {
#ifdef _WIN32 ERR_FAIL_COND(_socket == 0);
#if defined(_WIN64) || defined(_WIN32)
char optval = on ? 1 : 0; char optval = on ? 1 : 0;
#else #else
int optval = on ? 1 : 0; int optval = on ? 1 : 0;
@ -238,32 +276,41 @@ 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(int *r_err) {
struct sockaddr_in6 localaddr; struct sockaddr_in6 localaddr;
ERR_FAIL_COND_V(_socket == 0, 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) { int err = ::getsockname(_socket, static_cast<struct sockaddr *>((void *)(&localaddr)), &addrlen);
LOG_ERR("sockets::getLocalAddr");
if (r_err) {
*r_err = err;
} }
return localaddr; return localaddr;
} }
struct sockaddr_in6 Socket::get_peer_addr() { struct sockaddr_in6 Socket::get_peer_addr(int *r_err) {
struct sockaddr_in6 peeraddr; struct sockaddr_in6 peeraddr;
ERR_FAIL_COND_V(_socket == 0, 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) { int err = ::getpeername(_socket, static_cast<struct sockaddr *>((void *)(&peeraddr)), &addrlen);
LOG_ERR("sockets::getPeerAddr");
if (r_err) {
*r_err = err;
} }
return peeraddr; return peeraddr;
} }
int Socket::global_init() { int Socket::global_init() {
#ifdef _WIN32 #if defined(_WIN64) || defined(_WIN32)
int r; int r;
WSADATA wsa_data; WSADATA wsa_data;
@ -286,10 +333,6 @@ Socket::Socket(int socketFD, const InetAddress &address) {
Socket::~Socket() { Socket::~Socket() {
if (_socket >= 0) { if (_socket >= 0) {
#ifndef _WIN32 close_socket();
close(_socket);
#else
closesocket(_socket);
#endif
} }
} }

View File

@ -17,48 +17,46 @@
* *
*/ */
#ifndef _WIN32
#include <unistd.h>
#endif
#include <fcntl.h>
#include "inet_address.h" #include "inet_address.h"
class Socket class Socket {
{
public: public:
void create_net_socket(); void create_net_socket();
void create(int family); void create(int family);
void set_non_block_and_close_on_exit(); void close_socket();
int get_error();
int connect(const InetAddress &address);
bool is_self_connect();
void bind_address(const InetAddress &address);
void listen();
int accept(Socket *sock);
void close_write();
int read(char *buffer, uint64_t len);
int send(const char *buffer, uint64_t len);
void set_tcp_nodelay(bool on); int connect(const InetAddress &address);
void set_reuse_addr(bool on); int bind_address(const InetAddress &address);
void set_reuse_port(bool on); int listen();
void set_keep_alive(bool on); int accept(Socket *sock);
struct sockaddr_in6 get_local_addr(); int close_write();
struct sockaddr_in6 get_peer_addr();
static int global_init(); int read(char *buffer, uint64_t len);
int send(const char *buffer, uint64_t len);
Socket(); bool is_self_connect();
Socket(int socketFD, const InetAddress &address);
~Socket();
int _socket; void set_tcp_nodelay(bool on);
InetAddress _address; void set_reuse_addr(bool on);
int set_reuse_port(bool on);
void set_keep_alive(bool on);
int set_non_block_and_close_on_exit();
int get_error();
struct sockaddr_in6 get_local_addr(int *r_err = NULL);
struct sockaddr_in6 get_peer_addr(int *r_err = NULL);
static int global_init();
Socket();
Socket(int socketFD, const InetAddress &address);
~Socket();
int _socket;
InetAddress _address;
}; };
#endif // SOCKET_H #endif // SOCKET_H