1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-02 17:51:06 +01:00
libtgvoip/os/windows/NetworkSocketWinsock.cpp

850 lines
23 KiB
C++
Raw Normal View History

//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#include <winsock2.h>
#include <ws2tcpip.h>
#include "NetworkSocketWinsock.h"
2020-01-22 17:30:47 +01:00
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
2017-05-11 05:21:04 +02:00
#else
#include <IPHlpApi.h>
2017-05-11 05:21:04 +02:00
#endif
#include <assert.h>
2020-01-22 12:43:51 +01:00
#include "../../tools/logging.h"
#include "../../VoIPController.h"
#include "WindowsSpecific.h"
2020-01-22 12:43:51 +01:00
#include "../../tools/Buffers.h"
using namespace tgvoip;
2020-01-22 17:30:47 +01:00
NetworkSocketWinsock::NetworkSocketWinsock(NetworkProtocol protocol) : NetworkSocket(protocol)
{
needUpdateNat64Prefix = true;
nat64Present = false;
switchToV6at = 0;
isV4Available = false;
closing = false;
fd = INVALID_SOCKET;
#ifdef TGVOIP_WINXP_COMPAT
2020-01-22 17:30:47 +01:00
DWORD version = GetVersion();
isAtLeastVista = LOBYTE(LOWORD(version)) >= 6; // Vista is 6.0, XP is 5.1 and 5.2
#else
2020-01-22 17:30:47 +01:00
isAtLeastVista = true;
#endif
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
LOGD("Initialized winsock, version %d.%d", wsaData.wHighVersion, wsaData.wVersion);
2020-01-22 17:30:47 +01:00
if (protocol == NetworkProtocol::TCP)
timeout = 10.0;
lastSuccessfulOperationTime = VoIPController::GetCurrentTime();
}
2020-01-22 17:30:47 +01:00
NetworkSocketWinsock::~NetworkSocketWinsock()
{
}
2020-01-22 17:30:47 +01:00
void NetworkSocketWinsock::SetMaxPriority()
{
}
2020-01-22 17:30:47 +01:00
void NetworkSocketWinsock::Send(NetworkPacket packet)
{
if (packet.IsEmpty() || (protocol == NetworkProtocol::UDP && packet.address.IsEmpty()))
{
LOGW("tried to send null packet");
return;
}
int res;
2020-01-22 17:30:47 +01:00
if (protocol == NetworkProtocol::UDP)
{
if (isAtLeastVista)
{
sockaddr_in6 addr;
2020-01-22 17:30:47 +01:00
if (!packet.address.isIPv6)
{
if (needUpdateNat64Prefix && !isV4Available && VoIPController::GetCurrentTime() > switchToV6at && switchToV6at != 0)
{
LOGV("Updating NAT64 prefix");
2020-01-22 17:30:47 +01:00
nat64Present = false;
addrinfo *addr0;
2020-01-22 17:30:47 +01:00
int res = getaddrinfo("ipv4only.arpa", NULL, NULL, &addr0);
if (res != 0)
{
2019-11-11 08:50:54 +01:00
LOGW("Error updating NAT64 prefix: %d / %s", res, gai_strerrorA(res));
2020-01-22 17:30:47 +01:00
}
else
{
addrinfo *addrPtr;
2020-01-22 17:30:47 +01:00
unsigned char *addr170 = NULL;
unsigned char *addr171 = NULL;
for (addrPtr = addr0; addrPtr; addrPtr = addrPtr->ai_next)
{
if (addrPtr->ai_family == AF_INET6)
{
sockaddr_in6 *translatedAddr = (sockaddr_in6 *)addrPtr->ai_addr;
uint32_t v4part = *((uint32_t *)&translatedAddr->sin6_addr.s6_addr[12]);
if (v4part == 0xAA0000C0 && !addr170)
{
addr170 = translatedAddr->sin6_addr.s6_addr;
}
2020-01-22 17:30:47 +01:00
if (v4part == 0xAB0000C0 && !addr171)
{
addr171 = translatedAddr->sin6_addr.s6_addr;
}
char buf[INET6_ADDRSTRLEN];
//LOGV("Got translated address: %s", inet_ntop(AF_INET6, &translatedAddr->sin6_addr, buf, sizeof(buf)));
}
}
2020-01-22 17:30:47 +01:00
if (addr170 && addr171 && memcmp(addr170, addr171, 12) == 0)
{
nat64Present = true;
memcpy(nat64Prefix, addr170, 12);
char buf[INET6_ADDRSTRLEN];
//LOGV("Found nat64 prefix from %s", inet_ntop(AF_INET6, addr170, buf, sizeof(buf)));
2020-01-22 17:30:47 +01:00
}
else
{
LOGV("Didn't find nat64");
}
freeaddrinfo(addr0);
}
2020-01-22 17:30:47 +01:00
needUpdateNat64Prefix = false;
}
memset(&addr, 0, sizeof(sockaddr_in6));
2020-01-22 17:30:47 +01:00
addr.sin6_family = AF_INET6;
*((uint32_t *)&addr.sin6_addr.s6_addr[12]) = packet.address.addr.ipv4;
if (nat64Present)
memcpy(addr.sin6_addr.s6_addr, nat64Prefix, 12);
else
2020-01-22 17:30:47 +01:00
addr.sin6_addr.s6_addr[11] = addr.sin6_addr.s6_addr[10] = 0xFF;
}
else
{
memcpy(addr.sin6_addr.s6_addr, packet.address.addr.ipv6, 16);
}
2020-01-22 17:30:47 +01:00
addr.sin6_port = htons(packet.port);
res = sendto(fd, (const char *)*packet.data, packet.data.Length(), 0, (const sockaddr *)&addr, sizeof(addr));
}
else
{
sockaddr_in addr;
2020-01-22 17:30:47 +01:00
addr.sin_addr.s_addr = packet.address.addr.ipv4;
addr.sin_port = htons(packet.port);
addr.sin_family = AF_INET;
res = sendto(fd, (const char *)*packet.data, packet.data.Length(), 0, (const sockaddr *)&addr, sizeof(addr));
}
}
2020-01-22 17:30:47 +01:00
else
{
res = send(fd, (const char *)*packet.data, packet.data.Length(), 0);
}
if (res == SOCKET_ERROR)
{
int error = WSAGetLastError();
if (error == WSAEWOULDBLOCK)
{
if (!pendingOutgoingPacket.IsEmpty())
{
LOGE("Got EAGAIN but there's already a pending packet");
2020-01-22 17:30:47 +01:00
failed = true;
}
else
{
LOGV("Socket %d not ready to send", fd);
2020-01-22 17:30:47 +01:00
pendingOutgoingPacket = std::move(packet);
readyToSend = false;
}
2020-01-22 17:30:47 +01:00
}
else
{
LOGE("error sending: %d / %s", error, WindowsSpecific::GetErrorMessage(error).c_str());
2020-01-22 17:30:47 +01:00
if (error == WSAENETUNREACH && !isV4Available && VoIPController::GetCurrentTime() < switchToV6at)
{
switchToV6at = VoIPController::GetCurrentTime();
LOGI("Network unreachable, trying NAT64");
}
}
2020-01-22 17:30:47 +01:00
}
else if (res < packet.data.Length() && protocol == NetworkProtocol::TCP)
{
if (!pendingOutgoingPacket.IsEmpty())
{
LOGE("send returned less than packet length but there's already a pending packet");
2020-01-22 17:30:47 +01:00
failed = true;
}
else
{
LOGV("Socket %d not ready to send", fd);
2020-01-22 17:30:47 +01:00
pendingOutgoingPacket = std::move(packet);
Buffer pdata = std::move(pendingOutgoingPacket.data);
pendingOutgoingPacket.data = Buffer::CopyOf(pdata, res, pdata.Length() - res);
readyToSend = false;
}
}
}
2020-01-22 17:30:47 +01:00
bool NetworkSocketWinsock::OnReadyToSend()
{
if (!pendingOutgoingPacket.IsEmpty())
{
Send(std::move(pendingOutgoingPacket));
2020-01-22 17:30:47 +01:00
pendingOutgoingPacket = std::move(NetworkPacket::Empty());
return false;
}
2020-01-22 17:30:47 +01:00
readyToSend = true;
return true;
}
2020-01-22 17:30:47 +01:00
NetworkPacket NetworkSocketWinsock::Receive(size_t maxLen)
{
if (maxLen == 0)
maxLen = UINT32_MAX;
if (protocol == NetworkProtocol::UDP)
{
if (isAtLeastVista)
{
int addrLen = sizeof(sockaddr_in6);
sockaddr_in6 srcAddr;
2020-01-22 17:30:47 +01:00
int res = recvfrom(fd, (char *)*recvBuf, std::min(recvBuf.Length(), maxLen), 0, (sockaddr *)&srcAddr, (socklen_t *)&addrLen);
if (res == SOCKET_ERROR)
{
int error = WSAGetLastError();
LOGE("error receiving %d / %s", error, WindowsSpecific::GetErrorMessage(error).c_str());
return NetworkPacket::Empty();
}
//LOGV("Received %d bytes from %s:%d at %.5lf", len, inet_ntoa(srcAddr.sin_addr), ntohs(srcAddr.sin_port), GetCurrentTime());
2020-01-22 17:30:47 +01:00
if (!isV4Available && IN6_IS_ADDR_V4MAPPED(&srcAddr.sin6_addr))
{
isV4Available = true;
LOGI("Detected IPv4 connectivity, will not try IPv6");
}
2020-01-22 17:30:47 +01:00
NetworkAddress addr = NetworkAddress::Empty();
if (IN6_IS_ADDR_V4MAPPED(&srcAddr.sin6_addr) || (nat64Present && memcmp(nat64Prefix, srcAddr.sin6_addr.s6_addr, 12) == 0))
{
in_addr v4addr = *((in_addr *)&srcAddr.sin6_addr.s6_addr[12]);
addr = NetworkAddress::IPv4(v4addr.s_addr);
}
else
{
addr = NetworkAddress::IPv6(srcAddr.sin6_addr.s6_addr);
}
return NetworkPacket{
Buffer::CopyOf(recvBuf, 0, (size_t)res),
addr,
ntohs(srcAddr.sin6_port),
2020-01-22 17:30:47 +01:00
NetworkProtocol::UDP};
}
else
{
int addrLen = sizeof(sockaddr_in);
sockaddr_in srcAddr;
2020-01-22 17:30:47 +01:00
int res = recvfrom(fd, (char *)*recvBuf, std::min(recvBuf.Length(), maxLen), 0, (sockaddr *)&srcAddr, (socklen_t *)&addrLen);
if (res == SOCKET_ERROR)
{
LOGE("error receiving %d", WSAGetLastError());
return NetworkPacket::Empty();
}
return NetworkPacket{
Buffer::CopyOf(recvBuf, 0, (size_t)res),
NetworkAddress::IPv4(srcAddr.sin_addr.s_addr),
ntohs(srcAddr.sin_port),
2020-01-22 17:30:47 +01:00
NetworkProtocol::UDP};
}
2020-01-22 17:30:47 +01:00
}
else if (protocol == NetworkProtocol::TCP)
{
int res = recv(fd, (char *)*recvBuf, std::min(recvBuf.Length(), maxLen), 0);
if (res == SOCKET_ERROR)
{
int error = WSAGetLastError();
LOGE("Error receiving from TCP socket: %d / %s", error, WindowsSpecific::GetErrorMessage(error).c_str());
2020-01-22 17:30:47 +01:00
failed = true;
return NetworkPacket::Empty();
2020-01-22 17:30:47 +01:00
}
else
{
return NetworkPacket{
Buffer::CopyOf(recvBuf, 0, (size_t)res),
tcpConnectedAddress,
tcpConnectedPort,
2020-01-22 17:30:47 +01:00
NetworkProtocol::TCP};
}
}
}
2020-01-22 17:30:47 +01:00
void NetworkSocketWinsock::Open()
{
if (protocol == NetworkProtocol::UDP)
{
fd = socket(isAtLeastVista ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd == INVALID_SOCKET)
{
int error = WSAGetLastError();
LOGE("error creating socket: %d", error);
2020-01-22 17:30:47 +01:00
failed = true;
return;
}
2019-11-11 08:50:54 +01:00
int res;
2020-01-22 17:30:47 +01:00
if (isAtLeastVista)
{
DWORD flag = 0;
res = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&flag, sizeof(flag));
if (res == SOCKET_ERROR)
{
LOGE("error enabling dual stack socket: %d", WSAGetLastError());
2020-01-22 17:30:47 +01:00
failed = true;
return;
}
}
2020-01-22 17:30:47 +01:00
u_long one = 1;
ioctlsocket(fd, FIONBIO, &one);
SetMaxPriority();
2020-01-22 17:30:47 +01:00
int tries = 0;
sockaddr *addr;
sockaddr_in addr4;
sockaddr_in6 addr6;
int addrLen;
2020-01-22 17:30:47 +01:00
if (isAtLeastVista)
{
//addr.sin6_addr.s_addr=0;
memset(&addr6, 0, sizeof(sockaddr_in6));
//addr.sin6_len=sizeof(sa_family_t);
2020-01-22 17:30:47 +01:00
addr6.sin6_family = AF_INET6;
addr = (sockaddr *)&addr6;
addrLen = sizeof(addr6);
}
else
{
sockaddr_in addr4;
2020-01-22 17:30:47 +01:00
addr4.sin_addr.s_addr = 0;
addr4.sin_family = AF_INET;
addr = (sockaddr *)&addr4;
addrLen = sizeof(addr4);
}
2020-01-22 17:30:47 +01:00
for (tries = 0; tries < 10; tries++)
{
uint16_t port = htons(GenerateLocalPort());
if (isAtLeastVista)
((sockaddr_in6 *)addr)->sin6_port = port;
else
2020-01-22 17:30:47 +01:00
((sockaddr_in *)addr)->sin_port = port;
res = ::bind(fd, addr, addrLen);
LOGV("trying bind to port %u", ntohs(port));
2020-01-22 17:30:47 +01:00
if (res < 0)
{
LOGE("error binding to port %u: %d / %s", ntohs(port), errno, strerror(errno));
2020-01-22 17:30:47 +01:00
}
else
{
break;
}
}
2020-01-22 17:30:47 +01:00
if (tries == 10)
{
if (isAtLeastVista)
((sockaddr_in6 *)addr)->sin6_port = 0;
else
2020-01-22 17:30:47 +01:00
((sockaddr_in *)addr)->sin_port = 0;
res = ::bind(fd, addr, addrLen);
if (res < 0)
{
LOGE("error binding to port %u: %d / %s", 0, errno, strerror(errno));
//SetState(STATE_FAILED);
return;
}
}
2020-01-22 17:30:47 +01:00
getsockname(fd, addr, (socklen_t *)&addrLen);
uint16_t localUdpPort;
2020-01-22 17:30:47 +01:00
if (isAtLeastVista)
localUdpPort = ntohs(((sockaddr_in6 *)addr)->sin6_port);
else
2020-01-22 17:30:47 +01:00
localUdpPort = ntohs(((sockaddr_in *)addr)->sin_port);
LOGD("Bound to local UDP port %u", localUdpPort);
2020-01-22 17:30:47 +01:00
needUpdateNat64Prefix = true;
isV4Available = false;
switchToV6at = VoIPController::GetCurrentTime() + ipv6Timeout;
}
}
2020-01-22 17:30:47 +01:00
void NetworkSocketWinsock::Close()
{
closing = true;
failed = true;
if (fd != INVALID_SOCKET)
2017-07-27 18:47:10 +02:00
closesocket(fd);
}
2020-01-22 17:30:47 +01:00
void NetworkSocketWinsock::OnActiveInterfaceChanged()
{
needUpdateNat64Prefix = true;
isV4Available = false;
switchToV6at = VoIPController::GetCurrentTime() + ipv6Timeout;
}
2020-01-22 17:30:47 +01:00
std::string NetworkSocketWinsock::GetLocalInterfaceInfo(NetworkAddress *v4addr, NetworkAddress *v6addr)
{
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
Windows::Networking::Connectivity::ConnectionProfile ^ profile = Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile();
if (profile)
{
Windows::Foundation::Collections::IVectorView<Windows::Networking::HostName ^> ^ hostnames = Windows::Networking::Connectivity::NetworkInformation::GetHostNames();
for (unsigned int i = 0; i < hostnames->Size; i++)
{
Windows::Networking::HostName ^ n = hostnames->GetAt(i);
if (n->Type != Windows::Networking::HostNameType::Ipv4 && n->Type != Windows::Networking::HostNameType::Ipv6)
2017-05-11 05:21:04 +02:00
continue;
2020-01-22 17:30:47 +01:00
if (n->IPInformation->NetworkAdapter->Equals(profile->NetworkAdapter))
{
if (v4addr && n->Type == Windows::Networking::HostNameType::Ipv4)
{
2017-05-11 05:21:04 +02:00
char buf[INET_ADDRSTRLEN];
WideCharToMultiByte(CP_UTF8, 0, n->RawName->Data(), -1, buf, sizeof(buf), NULL, NULL);
2020-01-22 17:30:47 +01:00
*v4addr = NetworkAddress::IPv4(buf);
}
else if (v6addr && n->Type == Windows::Networking::HostNameType::Ipv6)
{
2017-05-11 05:21:04 +02:00
char buf[INET6_ADDRSTRLEN];
WideCharToMultiByte(CP_UTF8, 0, n->RawName->Data(), -1, buf, sizeof(buf), NULL, NULL);
2020-01-22 17:30:47 +01:00
*v6addr = NetworkAddress::IPv6(buf);
2017-05-11 05:21:04 +02:00
}
}
}
char buf[128];
WideCharToMultiByte(CP_UTF8, 0, profile->NetworkAdapter->NetworkAdapterId.ToString()->Data(), -1, buf, sizeof(buf), NULL, NULL);
return std::string(buf);
}
return "";
#else
2020-01-22 17:30:47 +01:00
IP_ADAPTER_ADDRESSES *addrs = (IP_ADAPTER_ADDRESSES *)malloc(15 * 1024);
ULONG size = 15 * 1024;
ULONG flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME;
ULONG res = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, addrs, &size);
if (res == ERROR_BUFFER_OVERFLOW)
{
addrs = (IP_ADAPTER_ADDRESSES *)realloc(addrs, size);
res = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, addrs, &size);
}
2020-01-22 17:30:47 +01:00
ULONG bestMetric = 0;
std::string bestName("");
2020-01-22 17:30:47 +01:00
if (res == ERROR_SUCCESS)
{
IP_ADAPTER_ADDRESSES *current = addrs;
while (current)
{
char *name = current->AdapterName;
LOGV("Adapter '%s':", name);
2020-01-22 17:30:47 +01:00
IP_ADAPTER_UNICAST_ADDRESS *curAddr = current->FirstUnicastAddress;
if (current->OperStatus != IfOperStatusUp)
{
LOGV("-> (down)");
2020-01-22 17:30:47 +01:00
current = current->Next;
continue;
}
2020-01-22 17:30:47 +01:00
if (current->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
{
LOGV("-> (loopback)");
2020-01-22 17:30:47 +01:00
current = current->Next;
continue;
}
2020-01-22 17:30:47 +01:00
if (isAtLeastVista)
LOGV("v4 metric: %u, v6 metric: %u", current->Ipv4Metric, current->Ipv6Metric);
2020-01-22 17:30:47 +01:00
while (curAddr)
{
sockaddr *addr = curAddr->Address.lpSockaddr;
if (addr->sa_family == AF_INET && v4addr)
{
sockaddr_in *ipv4 = (sockaddr_in *)addr;
LOGV("-> V4: %s", V4AddressToString(ipv4->sin_addr.s_addr).c_str());
2020-01-22 17:30:47 +01:00
uint32_t ip = ntohl(ipv4->sin_addr.s_addr);
if ((ip & 0xFFFF0000) != 0xA9FE0000)
{
if (isAtLeastVista)
{
if (current->Ipv4Metric > bestMetric)
{
bestMetric = current->Ipv4Metric;
bestName = std::string(current->AdapterName);
*v4addr = NetworkAddress::IPv4(ipv4->sin_addr.s_addr);
}
2020-01-22 17:30:47 +01:00
}
else
{
bestName = std::string(current->AdapterName);
*v4addr = NetworkAddress::IPv4(ipv4->sin_addr.s_addr);
}
}
2020-01-22 17:30:47 +01:00
}
else if (addr->sa_family == AF_INET6 && v6addr)
{
sockaddr_in6 *ipv6 = (sockaddr_in6 *)addr;
LOGV("-> V6: %s", V6AddressToString(ipv6->sin6_addr.s6_addr).c_str());
2020-01-22 17:30:47 +01:00
if (!IN6_IS_ADDR_LINKLOCAL(&ipv6->sin6_addr))
{
*v6addr = NetworkAddress::IPv6(ipv6->sin6_addr.s6_addr);
}
}
2020-01-22 17:30:47 +01:00
curAddr = curAddr->Next;
}
2020-01-22 17:30:47 +01:00
current = current->Next;
}
}
free(addrs);
return bestName;
2017-05-11 05:21:04 +02:00
#endif
}
2020-01-22 17:30:47 +01:00
uint16_t NetworkSocketWinsock::GetLocalPort()
{
if (!isAtLeastVista)
{
sockaddr_in addr;
2020-01-22 17:30:47 +01:00
size_t addrLen = sizeof(sockaddr_in);
getsockname(fd, (sockaddr *)&addr, (socklen_t *)&addrLen);
return ntohs(addr.sin_port);
}
sockaddr_in6 addr;
2020-01-22 17:30:47 +01:00
size_t addrLen = sizeof(sockaddr_in6);
getsockname(fd, (sockaddr *)&addr, (socklen_t *)&addrLen);
return ntohs(addr.sin6_port);
}
2020-01-22 17:30:47 +01:00
std::string NetworkSocketWinsock::V4AddressToString(uint32_t address)
{
char buf[INET_ADDRSTRLEN];
sockaddr_in addr;
ZeroMemory(&addr, sizeof(addr));
2020-01-22 17:30:47 +01:00
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = address;
DWORD len = sizeof(buf);
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
wchar_t wbuf[INET_ADDRSTRLEN];
ZeroMemory(wbuf, sizeof(wbuf));
2020-01-22 17:30:47 +01:00
WSAAddressToStringW((sockaddr *)&addr, sizeof(addr), NULL, wbuf, &len);
WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, sizeof(buf), NULL, NULL);
#else
2020-01-22 17:30:47 +01:00
WSAAddressToStringA((sockaddr *)&addr, sizeof(addr), NULL, buf, &len);
#endif
return std::string(buf);
}
2020-01-22 17:30:47 +01:00
std::string NetworkSocketWinsock::V6AddressToString(const unsigned char *address)
{
char buf[INET6_ADDRSTRLEN];
sockaddr_in6 addr;
ZeroMemory(&addr, sizeof(addr));
2020-01-22 17:30:47 +01:00
addr.sin6_family = AF_INET6;
memcpy(addr.sin6_addr.s6_addr, address, 16);
2020-01-22 17:30:47 +01:00
DWORD len = sizeof(buf);
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
wchar_t wbuf[INET6_ADDRSTRLEN];
ZeroMemory(wbuf, sizeof(wbuf));
2020-01-22 17:30:47 +01:00
WSAAddressToStringW((sockaddr *)&addr, sizeof(addr), NULL, wbuf, &len);
WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, sizeof(buf), NULL, NULL);
#else
2020-01-22 17:30:47 +01:00
WSAAddressToStringA((sockaddr *)&addr, sizeof(addr), NULL, buf, &len);
#endif
return std::string(buf);
}
2020-01-22 17:30:47 +01:00
uint32_t NetworkSocketWinsock::StringToV4Address(std::string address)
{
sockaddr_in addr;
ZeroMemory(&addr, sizeof(addr));
2020-01-22 17:30:47 +01:00
addr.sin_family = AF_INET;
int size = sizeof(addr);
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
wchar_t buf[INET_ADDRSTRLEN];
MultiByteToWideChar(CP_UTF8, 0, address.c_str(), -1, buf, INET_ADDRSTRLEN);
2020-01-22 17:30:47 +01:00
WSAStringToAddressW(buf, AF_INET, NULL, (sockaddr *)&addr, &size);
#else
2020-01-22 17:30:47 +01:00
WSAStringToAddressA((char *)address.c_str(), AF_INET, NULL, (sockaddr *)&addr, &size);
#endif
return addr.sin_addr.s_addr;
}
2020-01-22 17:30:47 +01:00
void NetworkSocketWinsock::StringToV6Address(std::string address, unsigned char *out)
{
sockaddr_in6 addr;
ZeroMemory(&addr, sizeof(addr));
2020-01-22 17:30:47 +01:00
addr.sin6_family = AF_INET6;
int size = sizeof(addr);
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
wchar_t buf[INET6_ADDRSTRLEN];
MultiByteToWideChar(CP_UTF8, 0, address.c_str(), -1, buf, INET6_ADDRSTRLEN);
2020-01-22 17:30:47 +01:00
WSAStringToAddressW(buf, AF_INET, NULL, (sockaddr *)&addr, &size);
#else
2020-01-22 17:30:47 +01:00
WSAStringToAddressA((char *)address.c_str(), AF_INET, NULL, (sockaddr *)&addr, &size);
#endif
memcpy(out, addr.sin6_addr.s6_addr, 16);
}
2020-01-22 17:30:47 +01:00
void NetworkSocketWinsock::Connect(const NetworkAddress address, uint16_t port)
{
sockaddr_in v4;
sockaddr_in6 v6;
2020-01-22 17:30:47 +01:00
sockaddr *addr = NULL;
size_t addrLen = 0;
if (!address.isIPv6)
{
v4.sin_family = AF_INET;
v4.sin_addr.s_addr = address.addr.ipv4;
v4.sin_port = htons(port);
addr = reinterpret_cast<sockaddr *>(&v4);
addrLen = sizeof(v4);
}
else
{
v6.sin6_family = AF_INET6;
memcpy(v6.sin6_addr.s6_addr, address.addr.ipv6, 16);
2020-01-22 17:30:47 +01:00
v6.sin6_flowinfo = 0;
v6.sin6_scope_id = 0;
v6.sin6_port = htons(port);
addr = reinterpret_cast<sockaddr *>(&v6);
addrLen = sizeof(v6);
}
2020-01-22 17:30:47 +01:00
fd = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
if (fd == INVALID_SOCKET)
{
LOGE("Error creating TCP socket: %d", WSAGetLastError());
2020-01-22 17:30:47 +01:00
failed = true;
return;
}
2020-01-22 17:30:47 +01:00
u_long one = 1;
ioctlsocket(fd, FIONBIO, &one);
2020-01-22 17:30:47 +01:00
int opt = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const char *)&opt, sizeof(opt));
DWORD timeout = 5000;
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout));
timeout = 60000;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
int res = connect(fd, (const sockaddr *)addr, addrLen);
if (res != 0)
{
int error = WSAGetLastError();
if (error != WSAEINPROGRESS && error != WSAEWOULDBLOCK)
{
LOGW("error connecting TCP socket to %s:%u: %d / %s", address.ToString().c_str(), port, error, WindowsSpecific::GetErrorMessage(error).c_str());
closesocket(fd);
2020-01-22 17:30:47 +01:00
failed = true;
return;
}
}
2020-01-22 17:30:47 +01:00
tcpConnectedAddress = address;
tcpConnectedPort = port;
LOGI("successfully connected to %s:%d", tcpConnectedAddress.ToString().c_str(), tcpConnectedPort);
}
2020-01-22 17:30:47 +01:00
NetworkAddress NetworkSocketWinsock::ResolveDomainName(std::string name)
{
addrinfo *addr0;
NetworkAddress ret = NetworkAddress::Empty();
int res = getaddrinfo(name.c_str(), NULL, NULL, &addr0);
if (res != 0)
{
2019-11-11 08:50:54 +01:00
LOGW("Error updating NAT64 prefix: %d / %s", res, gai_strerrorA(res));
2020-01-22 17:30:47 +01:00
}
else
{
addrinfo *addrPtr;
for (addrPtr = addr0; addrPtr; addrPtr = addrPtr->ai_next)
{
if (addrPtr->ai_family == AF_INET)
{
sockaddr_in *addr = (sockaddr_in *)addrPtr->ai_addr;
ret = NetworkAddress::IPv4(addr->sin_addr.s_addr);
break;
}
}
freeaddrinfo(addr0);
}
return ret;
}
2020-01-22 17:30:47 +01:00
NetworkAddress NetworkSocketWinsock::GetConnectedAddress()
{
return tcpConnectedAddress;
}
2020-01-22 17:30:47 +01:00
uint16_t NetworkSocketWinsock::GetConnectedPort()
{
return tcpConnectedPort;
}
2020-01-22 17:30:47 +01:00
void NetworkSocketWinsock::SetTimeouts(int sendTimeout, int recvTimeout)
{
DWORD timeout = sendTimeout * 1000;
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout));
timeout = recvTimeout * 1000;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
}
2020-01-24 20:26:34 +01:00
bool NetworkSocketWinsock::Select(std::vector<std::shared_ptr<NetworkSocket>> &readFds, std::vector<std::shared_ptr<NetworkSocket>> &writeFds, std::vector<std::shared_ptr<NetworkSocket>> &errorFds, const std::unique_ptr<SocketSelectCanceller> &canceller)
2020-01-22 17:30:47 +01:00
{
fd_set readSet;
fd_set errorSet;
fd_set writeSet;
2020-01-22 17:30:47 +01:00
SocketSelectCancellerWin32 *canceller = dynamic_cast<SocketSelectCancellerWin32 *>(_canceller);
timeval timeout = {0, 10000};
bool anyFailed = false;
int res = 0;
2020-01-22 17:30:47 +01:00
do
{
FD_ZERO(&readSet);
FD_ZERO(&writeSet);
FD_ZERO(&errorSet);
2020-01-24 21:27:48 +01:00
for (auto itr = readFds.begin(); itr != readFds.end(); ++itr)
2020-01-22 17:30:47 +01:00
{
int sfd = GetDescriptorFromSocket(*itr);
if (sfd == 0)
{
LOGW("can't select on one of sockets because it's not a NetworkSocketWinsock instance");
continue;
}
FD_SET(sfd, &readSet);
}
2020-01-24 21:27:48 +01:00
for (const auto &s : writeFds)
2020-01-22 17:30:47 +01:00
{
int sfd = GetDescriptorFromSocket(s);
if (sfd == 0)
{
LOGW("can't select on one of sockets because it's not a NetworkSocketWinsock instance");
continue;
}
FD_SET(sfd, &writeSet);
}
2020-01-24 21:27:48 +01:00
for (auto itr = errorFds.begin(); itr != errorFds.end(); ++itr)
2020-01-22 17:30:47 +01:00
{
int sfd = GetDescriptorFromSocket(*itr);
if (sfd == 0)
{
LOGW("can't select on one of sockets because it's not a NetworkSocketWinsock instance");
continue;
}
2020-01-22 17:30:47 +01:00
if ((*itr)->timeout > 0 && VoIPController::GetCurrentTime() - (*itr)->lastSuccessfulOperationTime > (*itr)->timeout)
{
LOGW("Socket %d timed out", sfd);
2020-01-22 17:30:47 +01:00
(*itr)->failed = true;
}
anyFailed |= (*itr)->IsFailed();
FD_SET(sfd, &errorSet);
}
2020-01-22 17:30:47 +01:00
if (canceller && canceller->canceled)
break;
2020-01-22 17:30:47 +01:00
res = select(0, &readSet, &writeSet, &errorSet, &timeout);
//LOGV("select result %d", res);
2020-01-22 17:30:47 +01:00
if (res == SOCKET_ERROR)
LOGE("SELECT ERROR %d", WSAGetLastError());
2020-01-22 17:30:47 +01:00
} while (res == 0);
2020-01-22 17:30:47 +01:00
if (canceller && canceller->canceled && !anyFailed)
{
canceller->canceled = false;
return false;
2020-01-22 17:30:47 +01:00
}
else if (anyFailed)
{
FD_ZERO(&readSet);
FD_ZERO(&errorSet);
}
2020-01-24 21:27:48 +01:00
auto itr = readFds.begin();
2020-01-22 17:30:47 +01:00
while (itr != readFds.end())
{
int sfd = GetDescriptorFromSocket(*itr);
if (FD_ISSET(sfd, &readSet))
(*itr)->lastSuccessfulOperationTime = VoIPController::GetCurrentTime();
if (sfd == 0 || !FD_ISSET(sfd, &readSet) || !(*itr)->OnReadyToReceive())
{
itr = readFds.erase(itr);
}
else
{
++itr;
}
}
2020-01-22 17:30:47 +01:00
itr = writeFds.begin();
while (itr != writeFds.end())
{
int sfd = GetDescriptorFromSocket(*itr);
if (FD_ISSET(sfd, &writeSet))
{
(*itr)->lastSuccessfulOperationTime = VoIPController::GetCurrentTime();
LOGI("Socket %d is ready to send", sfd);
}
2020-01-22 17:30:47 +01:00
if (sfd == 0 || !FD_ISSET(sfd, &writeSet) || !(*itr)->OnReadyToSend())
{
itr = writeFds.erase(itr);
}
else
{
++itr;
}
}
2020-01-22 17:30:47 +01:00
itr = errorFds.begin();
while (itr != errorFds.end())
{
int sfd = GetDescriptorFromSocket(*itr);
if ((sfd == 0 || !FD_ISSET(sfd, &errorSet)) && !(*itr)->IsFailed())
{
itr = errorFds.erase(itr);
}
else
{
++itr;
}
}
//LOGV("select fds left: read=%d, error=%d", readFds.size(), errorFds.size());
2020-01-22 17:30:47 +01:00
return readFds.size() > 0 || errorFds.size() > 0;
}
2020-01-22 17:30:47 +01:00
SocketSelectCancellerWin32::SocketSelectCancellerWin32()
{
canceled = false;
}
2020-01-22 17:30:47 +01:00
SocketSelectCancellerWin32::~SocketSelectCancellerWin32()
{
}
2020-01-22 17:30:47 +01:00
void SocketSelectCancellerWin32::CancelSelect()
{
canceled = true;
}
2020-01-22 17:30:47 +01:00
int NetworkSocketWinsock::GetDescriptorFromSocket(NetworkSocket *socket)
{
NetworkSocketWinsock *sp = dynamic_cast<NetworkSocketWinsock *>(socket);
if (sp)
return sp->fd;
2020-01-22 17:30:47 +01:00
NetworkSocketWrapper *sw = dynamic_cast<NetworkSocketWrapper *>(socket);
if (sw)
return GetDescriptorFromSocket(sw->GetWrapped());
return 0;
}
2020-01-24 20:26:34 +01:00
inline int NetworkSocketWinsock::GetDescriptorFromSocket(const std::unique_ptr<NetworkSocket> &socket)
{
return GetDescriptorFromSocket(socket.get());
}