diff options
author | George Hazan <ghazan@miranda.im> | 2022-12-29 21:07:22 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2022-12-29 21:07:22 +0300 |
commit | c73345aa0f2ebaf669b0c599a5eb736877be14df (patch) | |
tree | 7cf153e4da10cb150b3907f96ae92a64f4059b8a /protocols/Telegram | |
parent | 5fcdd94995c1673556b59e25067cecf7c3c3bf27 (diff) |
Telegram: one more XP compatibility fix
Diffstat (limited to 'protocols/Telegram')
5 files changed, 267 insertions, 10 deletions
diff --git a/protocols/Telegram/tdlib/td/tdutils/td/utils/port/IPAddress.cpp b/protocols/Telegram/tdlib/td/tdutils/td/utils/port/IPAddress.cpp index 32c26403d5..efb2fbabef 100644 --- a/protocols/Telegram/tdlib/td/tdutils/td/utils/port/IPAddress.cpp +++ b/protocols/Telegram/tdlib/td/tdutils/td/utils/port/IPAddress.cpp @@ -4,7 +4,6 @@ // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#define _WINSOCK_DEPRECATED_NO_WARNINGS // we need to use inet_addr instead of inet_pton #include "td/utils/port/IPAddress.h" @@ -17,6 +16,7 @@ #include "td/utils/Slice.h" #include "td/utils/SliceBuilder.h" #include "td/utils/utf8.h" +#include "td/utils/port/inet_ntop.h" #if TD_WINDOWS #include "td/utils/port/wstring_convert.h" @@ -179,7 +179,7 @@ static CSlice get_ip_str(int family, const void *addr) { static TD_THREAD_LOCAL char *buf; init_thread_local<char[]>(buf, buf_size); - const char *res = inet_ntop(family, + const char *res = inet_ntop_(family, #if TD_WINDOWS const_cast<PVOID>(addr), #else @@ -320,11 +320,11 @@ Status IPAddress::init_ipv6_port(CSlice ipv6, int port) { std::memset(&ipv6_addr_, 0, sizeof(ipv6_addr_)); ipv6_addr_.sin6_family = AF_INET6; ipv6_addr_.sin6_port = htons(static_cast<uint16>(port)); - int err = inet_pton(AF_INET6, ipv6.c_str(), &ipv6_addr_.sin6_addr); + int err = inet_pton_(AF_INET6, ipv6.c_str(), &ipv6_addr_.sin6_addr); if (err == 0) { - return Status::Error(PSLICE() << "Failed inet_pton(AF_INET6, " << ipv6 << ")"); + return Status::Error(PSLICE() << "Failed inet_pton_(AF_INET6, " << ipv6 << ")"); } else if (err == -1) { - return OS_SOCKET_ERROR(PSLICE() << "Failed inet_pton(AF_INET6, " << ipv6 << ")"); + return OS_SOCKET_ERROR(PSLICE() << "Failed inet_pton_(AF_INET6, " << ipv6 << ")"); } is_valid_ = true; return Status::OK(); @@ -342,11 +342,11 @@ Status IPAddress::init_ipv4_port(CSlice ipv4, int port) { std::memset(&ipv4_addr_, 0, sizeof(ipv4_addr_)); ipv4_addr_.sin_family = AF_INET; ipv4_addr_.sin_port = htons(static_cast<uint16>(port)); - int err = inet_pton(AF_INET, ipv4.c_str(), &ipv4_addr_.sin_addr); + int err = inet_pton_(AF_INET, ipv4.c_str(), &ipv4_addr_.sin_addr); if (err == 0) { - return Status::Error(PSLICE() << "Failed inet_pton(AF_INET, " << ipv4 << ")"); + return Status::Error(PSLICE() << "Failed inet_pton_(AF_INET, " << ipv4 << ")"); } else if (err == -1) { - return OS_SOCKET_ERROR(PSLICE() << "Failed inet_pton(AF_INET, " << ipv4 << ")"); + return OS_SOCKET_ERROR(PSLICE() << "Failed inet_pton_(AF_INET, " << ipv4 << ")"); } is_valid_ = true; return Status::OK(); @@ -365,7 +365,7 @@ Result<IPAddress> IPAddress::get_ip_address(CSlice host) { } Result<IPAddress> IPAddress::get_ipv4_address(CSlice host) { - // sometimes inet_addr allows much more valid IPv4 hosts than inet_pton, + // sometimes inet_addr allows much more valid IPv4 hosts than inet_pton_, // like 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001 auto ipv4_numeric_addr = inet_addr(host.c_str()); if (ipv4_numeric_addr == INADDR_NONE) { @@ -416,7 +416,7 @@ Status IPAddress::init_host_port(CSlice host, CSlice port, bool prefer_ipv6) { return init_ipv6_port(host, port_int == 0 ? 1 : port_int); } - // some getaddrinfo implementations use inet_pton instead of inet_aton and support only decimal-dotted IPv4 form, + // some getaddrinfo implementations use inet_pton_ instead of inet_aton and support only decimal-dotted IPv4 form, // and so doesn't recognize 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001 as valid IPv4 addresses auto ipv4_numeric_addr = inet_addr(host.c_str()); if (ipv4_numeric_addr != INADDR_NONE) { diff --git a/protocols/Telegram/tdlib/td/tdutils/td/utils/port/inet_ntop.cpp b/protocols/Telegram/tdlib/td/tdutils/td/utils/port/inet_ntop.cpp new file mode 100644 index 0000000000..df018cb3f0 --- /dev/null +++ b/protocols/Telegram/tdlib/td/tdutils/td/utils/port/inet_ntop.cpp @@ -0,0 +1,220 @@ +#include "td/utils/common.h" + +/* + * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +static const char * +inet_ntop_v4 (const void *src, char *dst, size_t size) +{ + const char digits[] = "0123456789"; + int i; + struct in_addr *addr = (struct in_addr *)src; + u_long a = ntohl(addr->s_addr); + const char *orig_dst = dst; + + if (size < INET_ADDRSTRLEN) { + errno = ENOSPC; + return NULL; + } + for (i = 0; i < 4; ++i) { + int n = (a >> (24 - i * 8)) & 0xFF; + int non_zerop = 0; + + if (non_zerop || n / 100 > 0) { + *dst++ = digits[n / 100]; + n %= 100; + non_zerop = 1; + } + if (non_zerop || n / 10 > 0) { + *dst++ = digits[n / 10]; + n %= 10; + non_zerop = 1; + } + *dst++ = digits[n]; + if (i != 3) + *dst++ = '.'; + } + *dst++ = '\0'; + return orig_dst; +} + +static const char * +inet_ntop_v6 (const void *src, char *dst, size_t size) +{ + const char xdigits[] = "0123456789abcdef"; + int i; + const struct in6_addr *addr = (struct in6_addr *)src; + const u_char *ptr = addr->s6_addr; + const char *orig_dst = dst; + int compressed = 0; + + if (size < INET6_ADDRSTRLEN) { + errno = ENOSPC; + return NULL; + } + for (i = 0; i < 8; ++i) { + int non_zerop = 0; + + if (compressed == 0 && + ptr[0] == 0 && ptr[1] == 0 && + i <= 5 && + ptr[2] == 0 && ptr[3] == 0 && + ptr[4] == 0 && ptr[5] == 0) { + + compressed = 1; + + if (i == 0) + *dst++ = ':'; + *dst++ = ':'; + + for (ptr += 6, i += 3; + i < 8 && ptr[0] == 0 && ptr[1] == 0; + ++i, ptr += 2); + + if (i >= 8) + break; + } + + if (non_zerop || (ptr[0] >> 4)) { + *dst++ = xdigits[ptr[0] >> 4]; + non_zerop = 1; + } + if (non_zerop || (ptr[0] & 0x0F)) { + *dst++ = xdigits[ptr[0] & 0x0F]; + non_zerop = 1; + } + if (non_zerop || (ptr[1] >> 4)) { + *dst++ = xdigits[ptr[1] >> 4]; + non_zerop = 1; + } + *dst++ = xdigits[ptr[1] & 0x0F]; + if (i != 7) + *dst++ = ':'; + ptr += 2; + } + *dst++ = '\0'; + return orig_dst; +} + +LPCSTR WSAAPI inet_ntop_(INT af, PVOID src, LPSTR dst, size_t size) +{ + LPCSTR pdst; + if (!dst) + { + SetLastError( STATUS_INVALID_PARAMETER ); + return NULL; + } + switch (af) { + case AF_INET : + pdst = inet_ntop_v4 (src, dst, size); + + case AF_INET6 : + pdst = inet_ntop_v6 (src, dst, size); + + default: + WSASetLastError( WSAEAFNOSUPPORT ); + return NULL; + } + + if (!pdst) SetLastError( STATUS_INVALID_PARAMETER ); + return pdst; +} + +INT WSAAPI inet_pton_(INT af, LPCSTR csrc, PVOID dst) +{ + char * src; + + if (!dst || !csrc) + { + WSASetLastError(WSAEFAULT); + return SOCKET_ERROR; + } + + if (csrc == NULL || (src = strdup(csrc)) == NULL) { + SetLastError( ENOMEM ); + return 0; + } + + switch (af) { + case AF_INET: + { + struct sockaddr_in si4; + INT r; + INT s = sizeof(si4); + + si4.sin_family = AF_INET; + r = WSAStringToAddressA(src, AF_INET, NULL, (LPSOCKADDR) &si4, &s); + free(src); + src = NULL; + + if (r == 0) { + memcpy(dst, &si4.sin_addr, sizeof(si4.sin_addr)); + return 1; + } + } + break; + + case AF_INET6: + { + struct sockaddr_in6 si6; + INT r; + INT s = sizeof(si6); + + si6.sin6_family = AF_INET6; + r = WSAStringToAddressA(src, AF_INET6, NULL, (LPSOCKADDR) &si6, &s); + free(src); + src = NULL; + + if (r == 0) { + memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr)); + return 1; + } + } + break; + + default: + WSASetLastError( WSAEAFNOSUPPORT ); + return SOCKET_ERROR; + } + + /* the call failed */ + { + int le = WSAGetLastError(); + + if (le == WSAEINVAL) + return SOCKET_ERROR; + + WSASetLastError(le); + return -1; + } +} diff --git a/protocols/Telegram/tdlib/td/tdutils/td/utils/port/inet_ntop.h b/protocols/Telegram/tdlib/td/tdutils/td/utils/port/inet_ntop.h new file mode 100644 index 0000000000..6df7ec06b1 --- /dev/null +++ b/protocols/Telegram/tdlib/td/tdutils/td/utils/port/inet_ntop.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +LPCSTR WSAAPI inet_ntop_(INT af, PVOID src, LPSTR dst, size_t size); +INT WSAAPI inet_pton_(INT af, LPCSTR csrc, PVOID dst); diff --git a/protocols/Telegram/tdlib/tdutils.vcxproj b/protocols/Telegram/tdlib/tdutils.vcxproj index 43238525c0..5a24473132 100644 --- a/protocols/Telegram/tdlib/tdutils.vcxproj +++ b/protocols/Telegram/tdlib/tdutils.vcxproj @@ -54,6 +54,7 @@ <ClCompile Include="td\tdutils\td\utils\port\detail\Iocp.cpp" /> <ClCompile Include="td\tdutils\td\utils\port\detail\NativeFd.cpp" /> <ClCompile Include="td\tdutils\td\utils\port\FileFd.cpp" /> + <ClCompile Include="td\tdutils\td\utils\port\inet_ntop.cpp" /> <ClCompile Include="td\tdutils\td\utils\port\IPAddress.cpp" /> <ClCompile Include="td\tdutils\td\utils\port\MemoryMapping.cpp" /> <ClCompile Include="td\tdutils\td\utils\port\path.cpp" /> diff --git a/protocols/Telegram/tdlib/tdutils.vcxproj.filters b/protocols/Telegram/tdlib/tdutils.vcxproj.filters index f51361d15e..5aa66dd4d0 100644 --- a/protocols/Telegram/tdlib/tdutils.vcxproj.filters +++ b/protocols/Telegram/tdlib/tdutils.vcxproj.filters @@ -76,6 +76,7 @@ <ClCompile Include="td\tdutils\td\utils\port\detail\Iocp.cpp" /> <ClCompile Include="td\tdutils\td\utils\port\detail\NativeFd.cpp" /> <ClCompile Include="td\tdutils\td\utils\port\RwMutex.cpp" /> + <ClCompile Include="td\tdutils\td\utils\port\inet_ntop.cpp" /> </ItemGroup> <ItemGroup> <ClInclude Include="td\tdutils\td\utils\port\Clocks.h" /> |