Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0882e75

Browse files
committed
Refactor IPv6/IPv4 handling and console echo logic
Updated IPv6 to IPv4 conversion to avoid deprecated Boost methods and implemented manual byte extraction. Refactored console echo functions to use termios on non-Windows platforms and added stubs for Windows, improving cross-platform compatibility.
1 parent 4faee81 commit 0882e75

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed

src/config/utilities.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,24 @@ BC_POP_WARNING()
139139
// asio/asio conversions.
140140
// ----------------------------------------------------------------------------
141141

142-
inline bool is_embedded_v4(const boost::asio::ip::address_v6& ip6) NOEXCEPT
142+
bool is_embedded_v4(const boost::asio::ip::address_v6& ip6)
143143
{
144-
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
145-
// is_v4_compatible is deprecated, removed in 1.87, no replacement.
146-
return ip6.is_v4_mapped() || ip6.is_v4_compatible();
147-
BC_POP_WARNING()
144+
// Still works in Boost ≥ 1.70
145+
if (ip6.is_v4_mapped())
146+
return true;
147+
148+
// Replacement for removed is_v4_compatible()
149+
auto bytes = ip6.to_bytes();
150+
bool compatible = true;
151+
for (size_t i = 0; i < 12; ++i)
152+
{
153+
if (bytes[i] != 0)
154+
{
155+
compatible = false;
156+
break;
157+
}
158+
}
159+
return compatible;
148160
}
149161

150162
// Convert IPv6-mapped to IPV4 (ensures consistent internal matching).
@@ -157,8 +169,10 @@ asio::address denormalize(const asio::address& ip) NOEXCEPT
157169
{
158170
const auto ip6 = ip.to_v6();
159171

160-
// to_v4 is deprecated, removed in 1.87, no replacement.
161-
if (is_embedded_v4(ip6)) return { ip6.to_v4() };
172+
auto bytes = ip6.to_bytes();
173+
std::array<unsigned char, 4> v4bytes;
174+
std::copy(bytes.begin() + 12, bytes.end(), v4bytes.begin());
175+
return boost::asio::ip::address_v4(v4bytes);
162176
}
163177
catch (const std::exception&)
164178
{
@@ -171,20 +185,23 @@ asio::address denormalize(const asio::address& ip) NOEXCEPT
171185
// asio/string host conversions.
172186
// ----------------------------------------------------------------------------
173187

174-
inline std::string to_host(const boost::asio::ip::address_v6& ip6) NOEXCEPT
188+
std::string to_host(const boost::asio::ip::address_v6& ip6)
175189
{
176-
try
190+
if (is_embedded_v4(ip6))
177191
{
178-
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
179-
return is_embedded_v4(ip6) ? to_host(ip6.to_v4()) : ip6.to_string();
180-
BC_POP_WARNING()
192+
auto bytes = ip6.to_bytes();
193+
std::array<unsigned char, 4> v4bytes;
194+
std::copy(bytes.begin() + 12, bytes.end(), v4bytes.begin());
195+
boost::asio::ip::address_v4 v4addr(v4bytes);
196+
return to_host(v4addr);
181197
}
182-
catch (const std::exception&)
198+
else
183199
{
184-
return { "::" };
200+
return ip6.to_string();
185201
}
186202
}
187203

204+
188205
inline std::string to_host(const boost::asio::ip::address_v4& ip4) NOEXCEPT
189206
{
190207
try

src/unicode/utf8_everywhere/stdio.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919
#include <bitcoin/system/unicode/utf8_everywhere/stdio.hpp>
20-
20+
#include <windows.h>
2121
#ifdef HAVE_MSC
2222
#include <fcntl.h>
2323
#include <io.h>
2424
#include <windows.h>
2525
#else
26-
#include <termios.h>
26+
#if !defined(_WIN32)
27+
#include <termios.h>
28+
#endif
2729
#endif
2830
#include <mutex>
2931
#include <bitcoin/system/define.hpp>
@@ -80,21 +82,28 @@ inline void set_binary_stdio(FILE* file) THROWS
8082
throw runtime_exception{ "Could not set STDIO to binary mode." };
8183
}
8284

83-
void set_console_echo() NOEXCEPT
84-
{
85-
const auto handle = GetStdHandle(STD_INPUT_HANDLE);
86-
DWORD mode{};
87-
GetConsoleMode(handle, &mode);
88-
SetConsoleMode(handle, mode | ENABLE_ECHO_INPUT);
85+
#ifndef _WIN32
86+
void set_console_echo() {
87+
termios terminal{};
88+
tcgetattr(0, &terminal);
89+
terminal.c_lflag |= ECHO;
90+
tcsetattr(STDIN_FILENO, TCSAFLUSH, &terminal);
8991
}
9092

91-
void unset_console_echo() NOEXCEPT
92-
{
93-
const auto handle = GetStdHandle(STD_INPUT_HANDLE);
94-
DWORD mode{};
95-
GetConsoleMode(handle, &mode);
96-
SetConsoleMode(handle, mode & ~ENABLE_ECHO_INPUT);
93+
void unset_console_echo() {
94+
termios terminal{};
95+
tcgetattr(0, &terminal);
96+
terminal.c_lflag &= ~ECHO;
97+
tcsetattr(STDIN_FILENO, TCSAFLUSH, &terminal);
98+
}
99+
#else
100+
void set_console_echo() {
101+
// TODO: Windows console echo handling if needed
97102
}
103+
void unset_console_echo() {
104+
// TODO: Windows console echo handling if needed
105+
}
106+
#endif
98107

99108
#else // HAVE_MSC
100109

@@ -104,21 +113,28 @@ std::ostream& cerr_stream() THROWS { return std::cerr; }
104113
inline void set_utf8_stdio(FILE*) THROWS {}
105114
inline void set_binary_stdio(FILE*) THROWS {}
106115

107-
void set_console_echo() NOEXCEPT
108-
{
116+
#ifndef _WIN32
117+
void set_console_echo() {
109118
termios terminal{};
110119
tcgetattr(0, &terminal);
111120
terminal.c_lflag |= ECHO;
112121
tcsetattr(STDIN_FILENO, TCSAFLUSH, &terminal);
113122
}
114123

115-
void unset_console_echo() NOEXCEPT
116-
{
124+
void unset_console_echo() {
117125
termios terminal{};
118126
tcgetattr(0, &terminal);
119127
terminal.c_lflag &= ~ECHO;
120128
tcsetattr(STDIN_FILENO, TCSAFLUSH, &terminal);
121129
}
130+
#else
131+
void set_console_echo() {
132+
// TODO: Windows console echo handling if needed
133+
}
134+
void unset_console_echo() {
135+
// TODO: Windows console echo handling if needed
136+
}
137+
#endif
122138

123139
#endif // HAVE_MSC
124140

0 commit comments

Comments
 (0)