1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
crate::ix!();
//-------------------------------------------[.cpp/bitcoin/src/netbase.h]
lazy_static!{
/*
extern int nConnectTimeout;
extern bool fNameLookup;
*/
}
/**
| -timeout default
|
*/
pub const DEFAULT_CONNECT_TIMEOUT: i32 = 5000;
/**
| -dns default
|
*/
pub const DEFAULT_NAME_LOOKUP: i32 = 1;
bitflags!{
pub struct ConnectionDirection: u32 {
const None = 0;
const In = 1 << 0;
const Out = 1 << 1;
const Both = Self::In.bits | Self::Out.bits;
}
}
///--------------------
pub struct ProxyType {
pub proxy: Service,
pub randomize_credentials: bool,
}
impl Default for ProxyType {
fn default() -> Self {
todo!();
/*
: randomize_credentials(false),
*/
}
}
impl ProxyType {
pub fn new(
proxy: &Service,
randomize_credentials: Option<bool>) -> Self {
let randomize_credentials: bool =
randomize_credentials.unwrap_or(false);
todo!();
/*
: proxy(_proxy),
: randomize_credentials(_randomize_credentials),
*/
}
pub fn is_valid(&self) -> bool {
todo!();
/*
return proxy.IsValid();
*/
}
}
/**
| Credentials for proxy authentication
|
*/
pub struct ProxyCredentials
{
pub username: String,
pub password: String,
}
/**
| Socket factory. Defaults to `CreateSockTCP()`,
| but can be overridden by unit tests.
|
*/
lazy_static!{
/*
extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
*/
}
//-------------------------------------------[.cpp/bitcoin/src/netbase.cpp]
/* ------------------- Settings ------------------- */
#[derive(Default)]
pub struct ProxyInfo {
proxy_info: [ProxyType; Network::NET_MAX as usize],
name_proxy: ProxyType,
}
lazy_static!{
pub static ref G_PROXYINFO: Arc<Mutex<ProxyInfo>> = Arc::new(Mutex::new(ProxyInfo::default()));
pub static ref N_CONNECT_TIMEOUT: i32 = DEFAULT_CONNECT_TIMEOUT;
pub static ref NAME_LOOKUP: Atomic<bool> = Atomic::new(DEFAULT_NAME_LOOKUP != 0);
pub static ref INTERRUPT_SOCKS5_RECV: Atomic<bool> = Atomic::new(false);
// Need ample time for negotiation for very
// slow proxies such as Tor (milliseconds)
pub static ref G_SOCKS5_RECV_TIMEOUT: i32 = 20 * 1000;
}
/**
| Wrapper for getaddrinfo(3). Do not
| use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.
|
*/
pub fn wrapped_get_addr_info(
name: &String,
allow_lookup: bool) -> Vec<NetAddr> {
todo!();
/*
addrinfo ai_hint{};
// We want a TCP port, which is a streaming socket type
ai_hint.ai_socktype = SOCK_STREAM;
ai_hint.ai_protocol = IPPROTO_TCP;
// We don't care which address family (IPv4 or IPv6) is returned
ai_hint.ai_family = AF_UNSPEC;
// If we allow lookups of hostnames, use the AI_ADDRCONFIG flag to only
// return addresses whose family we have an address configured for.
//
// If we don't allow lookups, then use the AI_NUMERICHOST flag for
// getaddrinfo to only decode numerical network addresses and suppress
// hostname lookups.
ai_hint.ai_flags = allow_lookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
addrinfo* ai_res{nullptr};
const int n_err{getaddrinfo(name.c_str(), nullptr, &ai_hint, &ai_res)};
if (n_err != 0) {
return {};
}
// Traverse the linked list starting with ai_trav.
addrinfo* ai_trav{ai_res};
std::vector<CNetAddr> resolved_addresses;
while (ai_trav != nullptr) {
if (ai_trav->ai_family == AF_INET) {
assert(ai_trav->ai_addrlen >= sizeof(sockaddr_in));
resolved_addresses.emplace_back(reinterpret_cast<sockaddr_in*>(ai_trav->ai_addr)->sin_addr);
}
if (ai_trav->ai_family == AF_INET6) {
assert(ai_trav->ai_addrlen >= sizeof(sockaddr_in6));
const sockaddr_in6* s6{reinterpret_cast<sockaddr_in6*>(ai_trav->ai_addr)};
resolved_addresses.emplace_back(s6->sin6_addr, s6->sin6_scope_id);
}
ai_trav = ai_trav->ai_next;
}
freeaddrinfo(ai_res);
return resolved_addresses;
*/
}
pub fn parse_network(net_in: &String) -> Network {
todo!();
/*
std::string net = ToLower(net_in);
if (net == "ipv4") return NET_IPV4;
if (net == "ipv6") return NET_IPV6;
if (net == "onion") return NET_ONION;
if (net == "tor") {
LogPrintf("Warning: net name 'tor' is deprecated and will be removed in the future. You should use 'onion' instead.\n");
return NET_ONION;
}
if (net == "i2p") {
return NET_I2P;
}
return NET_UNROUTABLE;
*/
}
pub fn get_network_name(net: Network) -> String {
todo!();
/*
switch (net) {
case NET_UNROUTABLE: return "not_publicly_routable";
case NET_IPV4: return "ipv4";
case NET_IPV6: return "ipv6";
case NET_ONION: return "onion";
case NET_I2P: return "i2p";
case NET_CJDNS: return "cjdns";
case NET_INTERNAL: return "internal";
case NET_MAX: assert(false);
} // no default case, so the compiler can warn about missing cases
assert(false);
*/
}
/**
| Return a vector of publicly routable
| Network names; optionally append NET_UNROUTABLE.
|
*/
pub fn get_network_names(append_unroutable: Option<bool>) -> Vec<String> {
let append_unroutable: bool = append_unroutable.unwrap_or(false);
todo!();
/*
std::vector<std::string> names;
for (int n = 0; n < NET_MAX; ++n) {
const enum Network network{static_cast<Network>(n)};
if (network == NET_UNROUTABLE || network == NET_CJDNS || network == NET_INTERNAL) continue;
names.emplace_back(GetNetworkName(network));
}
if (append_unroutable) {
names.emplace_back(GetNetworkName(NET_UNROUTABLE));
}
return names;
*/
}
/**
| SOCKS version
|
*/
#[repr(u8)]
pub enum SOCKSVersion {
SOCKS4 = 0x04,
SOCKS5 = 0x05
}
/**
| Values defined for METHOD in RFC1928
|
*/
#[repr(u8)]
pub enum SOCKS5Method {
/**
| No authentication required
|
*/
NOAUTH = 0x00,
/**
| GSSAPI
|
*/
GSSAPI = 0x01,
/**
| Username/password
|
*/
USER_PASS = 0x02,
/**
| No acceptable methods
|
*/
NO_ACCEPTABLE = 0xff,
}
/**
| Values defined for CMD in RFC1928
|
*/
#[repr(u8)]
pub enum SOCKS5Command {
CONNECT = 0x01,
BIND = 0x02,
UDP_ASSOCIATE = 0x03
}
/**
| Values defined for REP in RFC1928
|
*/
#[repr(u8)]
pub enum SOCKS5Reply {
/**
| Succeeded
|
*/
SUCCEEDED = 0x00,
/**
| General failure
|
*/
GENFAILURE = 0x01,
/**
| Connection not allowed by ruleset
|
*/
NOTALLOWED = 0x02,
/**
| Network unreachable
|
*/
NETUNREACHABLE = 0x03,
/**
| Network unreachable
|
*/
HOSTUNREACHABLE = 0x04,
/**
| Connection refused
|
*/
CONNREFUSED = 0x05,
/**
| TTL expired
|
*/
TTLEXPIRED = 0x06,
/**
| Command not supported
|
*/
CMDUNSUPPORTED = 0x07,
/**
| Address type not supported
|
*/
ATYPEUNSUPPORTED = 0x08,
}
/**
| Values defined for ATYPE in RFC1928
|
*/
#[repr(u8)]
pub enum SOCKS5Atyp {
IPV4 = 0x01,
DOMAINNAME = 0x03,
IPV6 = 0x04,
}
/**
| Status codes that can be returned by
| InterruptibleRecv
|
*/
pub enum IntrRecvError {
OK,
Timeout,
Disconnected,
NetworkError,
Interrupted
}
/**
| Try to read a specified number of bytes
| from a socket. Please read the "see also"
| section for more detail.
|
| -----------
| @param data
|
| The buffer where the read bytes should
| be stored.
| ----------
| @param len
|
| The number of bytes to read into the specified
| buffer.
| ----------
| @param timeout
|
| The total timeout in milliseconds for
| this read.
| ----------
| @param sock
|
| The socket (has to be in non-blocking
| mode) from which to read bytes.
|
| -----------
| @return
|
| An IntrRecvError indicating the resulting
| status of this read.
|
| IntrRecvError::OK only if all of the
| specified number of bytes were read.
| @see This function can be interrupted
| by calling InterruptSocks5(bool).
|
| Sockets can be made non-blocking with
| SetSocketNonBlocking(const
|
| Socket&, bool).
|
*/
pub fn interruptible_recv(
data: *mut u8,
len: usize,
timeout: i32,
sock: &Sock) -> IntrRecvError {
todo!();
/*
int64_t curTime = GetTimeMillis();
int64_t endTime = curTime + timeout;
while (len > 0 && curTime < endTime) {
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
if (ret > 0) {
len -= ret;
data += ret;
} else if (ret == 0) { // Unexpected disconnection
return IntrRecvError::Disconnected;
} else { // Other error or blocking
int nErr = WSAGetLastError();
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
// Only wait at most MAX_WAIT_FOR_IO at a time, unless
// we're approaching the end of the specified total timeout
const auto remaining = std::chrono::milliseconds{endTime - curTime};
const auto timeout = std::min(remaining, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
if (!sock.Wait(timeout, Sock::RECV)) {
return IntrRecvError::NetworkError;
}
} else {
return IntrRecvError::NetworkError;
}
}
if (interruptSocks5Recv)
return IntrRecvError::Interrupted;
curTime = GetTimeMillis();
}
return len == 0 ? IntrRecvError::OK : IntrRecvError::Timeout;
*/
}
/**
| Convert SOCKS5 reply to an error message
|
*/
pub fn socks_5error_string(err: u8) -> String {
todo!();
/*
switch(err) {
case SOCKS5Reply::GENFAILURE:
return "general failure";
case SOCKS5Reply::NOTALLOWED:
return "connection not allowed";
case SOCKS5Reply::NETUNREACHABLE:
return "network unreachable";
case SOCKS5Reply::HOSTUNREACHABLE:
return "host unreachable";
case SOCKS5Reply::CONNREFUSED:
return "connection refused";
case SOCKS5Reply::TTLEXPIRED:
return "TTL expired";
case SOCKS5Reply::CMDUNSUPPORTED:
return "protocol error";
case SOCKS5Reply::ATYPEUNSUPPORTED:
return "address type not supported";
default:
return "unknown";
}
*/
}
/**
| Connect to a specified destination
| service through an already connected
|
| SOCKS5 proxy.
|
| -----------
| @note
|
| The specified SOCKS5 proxy socket must
| already be connected to the
|
| SOCKS5 proxy. @see <a href="https://www.ietf.org/rfc/rfc1928.txt">RFC1928:
| SOCKS Protocol
|
| Version 5</a>
|
| -----------
| @param strDest
|
| The destination fully-qualified domain
| name.
| ----------
| @param port
|
| The destination port.
| ----------
| @param auth
|
| The credentials with which to authenticate
| with the specified
|
| SOCKS5 proxy.
| ----------
| @param socket
|
| The SOCKS5 proxy socket.
|
| -----------
| @return
|
| Whether or not the operation succeeded.
|
*/
pub fn socks5(
str_dest: &String,
port: u16,
auth: *const ProxyCredentials,
sock: &Sock) -> bool {
todo!();
/*
IntrRecvError recvr;
LogPrint(BCLog::NET, "SOCKS5 connecting %s\n", strDest);
if (strDest.size() > 255) {
return error("Hostname too long");
}
// Construct the version identifier/method selection message
std::vector<uint8_t> vSocks5Init;
vSocks5Init.push_back(SOCKSVersion::SOCKS5); // We want the SOCK5 protocol
if (auth) {
vSocks5Init.push_back(0x02); // 2 method identifiers follow...
vSocks5Init.push_back(SOCKS5Method::NOAUTH);
vSocks5Init.push_back(SOCKS5Method::USER_PASS);
} else {
vSocks5Init.push_back(0x01); // 1 method identifier follows...
vSocks5Init.push_back(SOCKS5Method::NOAUTH);
}
ssize_t ret = sock.Send(vSocks5Init.data(), vSocks5Init.size(), MSG_NOSIGNAL);
if (ret != (ssize_t)vSocks5Init.size()) {
return error("Error sending to proxy");
}
uint8_t pchRet1[2];
if ((recvr = InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
return false;
}
if (pchRet1[0] != SOCKSVersion::SOCKS5) {
return error("Proxy failed to initialize");
}
if (pchRet1[1] == SOCKS5Method::USER_PASS && auth) {
// Perform username/password authentication (as described in RFC1929)
std::vector<uint8_t> vAuth;
vAuth.push_back(0x01); // Current (and only) version of user/pass subnegotiation
if (auth->username.size() > 255 || auth->password.size() > 255)
return error("Proxy username or password too long");
vAuth.push_back(auth->username.size());
vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end());
vAuth.push_back(auth->password.size());
vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end());
ret = sock.Send(vAuth.data(), vAuth.size(), MSG_NOSIGNAL);
if (ret != (ssize_t)vAuth.size()) {
return error("Error sending authentication to proxy");
}
LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
uint8_t pchRetA[2];
if ((recvr = InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
return error("Error reading proxy authentication response");
}
if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
return error("Proxy authentication unsuccessful");
}
} else if (pchRet1[1] == SOCKS5Method::NOAUTH) {
// Perform no authentication
} else {
return error("Proxy requested wrong authentication method %02x", pchRet1[1]);
}
std::vector<uint8_t> vSocks5;
vSocks5.push_back(SOCKSVersion::SOCKS5); // VER protocol version
vSocks5.push_back(SOCKS5Command::CONNECT); // CMD CONNECT
vSocks5.push_back(0x00); // RSV Reserved must be 0
vSocks5.push_back(SOCKS5Atyp::DOMAINNAME); // ATYP DOMAINNAME
vSocks5.push_back(strDest.size()); // Length<=255 is checked at beginning of function
vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end());
vSocks5.push_back((port >> 8) & 0xFF);
vSocks5.push_back((port >> 0) & 0xFF);
ret = sock.Send(vSocks5.data(), vSocks5.size(), MSG_NOSIGNAL);
if (ret != (ssize_t)vSocks5.size()) {
return error("Error sending to proxy");
}
uint8_t pchRet2[4];
if ((recvr = InterruptibleRecv(pchRet2, 4, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
if (recvr == IntrRecvError::Timeout) {
/* If a timeout happens here, this effectively means we timed out while connecting
* to the remote node. This is very common for Tor, so do not print an
* error message. */
return false;
} else {
return error("Error while reading proxy response");
}
}
if (pchRet2[0] != SOCKSVersion::SOCKS5) {
return error("Proxy failed to accept request");
}
if (pchRet2[1] != SOCKS5Reply::SUCCEEDED) {
// Failures to connect to a peer that are not proxy errors
LogPrintf("Socks5() connect to %s:%d failed: %s\n", strDest, port, Socks5ErrorString(pchRet2[1]));
return false;
}
if (pchRet2[2] != 0x00) { // Reserved field must be 0
return error("Error: malformed proxy response");
}
uint8_t pchRet3[256];
switch (pchRet2[3])
{
case SOCKS5Atyp::IPV4: recvr = InterruptibleRecv(pchRet3, 4, g_socks5_recv_timeout, sock); break;
case SOCKS5Atyp::IPV6: recvr = InterruptibleRecv(pchRet3, 16, g_socks5_recv_timeout, sock); break;
case SOCKS5Atyp::DOMAINNAME:
{
recvr = InterruptibleRecv(pchRet3, 1, g_socks5_recv_timeout, sock);
if (recvr != IntrRecvError::OK) {
return error("Error reading from proxy");
}
int nRecv = pchRet3[0];
recvr = InterruptibleRecv(pchRet3, nRecv, g_socks5_recv_timeout, sock);
break;
}
default: return error("Error: malformed proxy response");
}
if (recvr != IntrRecvError::OK) {
return error("Error reading from proxy");
}
if ((recvr = InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
return error("Error reading from proxy");
}
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest);
return true;
*/
}
/**
| Create a TCP socket in the given address
| family.
|
| -----------
| @param[in] address_family
|
| The socket is created in the same address
| family as this address.
|
| -----------
| @return
|
| pointer to the created Sock object or
| unique_ptr that owns nothing in case
| of failure
|
*/
pub fn create_socktcp(address_family: &Service) -> Option<Box<Sock>> {
todo!();
/*
// Create a sockaddr from the specified service.
struct sockaddr_storage sockaddr;
socklen_t len = sizeof(sockaddr);
if (!address_family.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
LogPrintf("Cannot create socket for %s: unsupported network\n", address_family.ToString());
return nullptr;
}
// Create a TCP socket in the address family of the specified service.
Socket hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
if (hSocket == INVALID_SOCKET) {
return nullptr;
}
// Ensure that waiting for I/O on this socket won't result in undefined
// behavior.
if (!IsSelectableSocket(hSocket)) {
CloseSocket(hSocket);
LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
return nullptr;
}
#ifdef SO_NOSIGPIPE
int set = 1;
// Set the no-sigpipe option on the socket for BSD systems, other UNIXes
// should use the MSG_NOSIGNAL flag for every send.
setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (c_void*)&set, sizeof(int));
#endif
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
SetSocketNoDelay(hSocket);
// Set the non-blocking option on the socket.
if (!SetSocketNonBlocking(hSocket, true)) {
CloseSocket(hSocket);
LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError()));
return nullptr;
}
return std::make_unique<Sock>(hSocket);
*/
}
lazy_static!{
/*
std::function<std::unique_ptr<Sock>(const CService&)> CreateSock = CreateSockTCP;
*/
}
pub fn log_connect_failure<Args>(
manual_connection: bool,
fmt: *const u8,
args: &Args) {
todo!();
/*
std::string error_message = tfm::format(fmt, args...);
if (manual_connection) {
LogPrintf("%s\n", error_message);
} else {
LogPrint(BCLog::NET, "%s\n", error_message);
}
*/
}
/**
| Try to connect to the specified service
| on the specified socket.
|
| -----------
| @param addrConnect
|
| The service to which to connect.
| ----------
| @param sock
|
| The socket on which to connect.
| ----------
| @param nTimeout
|
| Wait this many milliseconds for the
| connection to be established.
| ----------
| @param manual_connection
|
| Whether or not the connection was manually
| requested (e.g. through the addnode
| RPC)
|
| -----------
| @return
|
| Whether or not a connection was successfully
| made.
|
*/
pub fn connect_socket_directly(
addr_connect: &Service,
sock: &Sock,
n_timeout: i32,
manual_connection: bool) -> bool {
todo!();
/*
// Create a sockaddr from the specified service.
struct sockaddr_storage sockaddr;
socklen_t len = sizeof(sockaddr);
if (sock.Get() == INVALID_SOCKET) {
LogPrintf("Cannot connect to %s: invalid socket\n", addrConnect.ToString());
return false;
}
if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
LogPrintf("Cannot connect to %s: unsupported network\n", addrConnect.ToString());
return false;
}
// Connect to the addrConnect service on the hSocket socket.
if (sock.Connect(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
int nErr = WSAGetLastError();
// WSAEINVAL is here because some legacy version of winsock uses it
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
{
// Connection didn't actually fail, but is being established
// asynchronously. Thus, use async I/O api (select/poll)
// synchronously to check for successful connection with a timeout.
const Sock::Event requested = Sock::RECV | Sock::SEND;
Sock::Event occurred;
if (!sock.Wait(std::chrono::milliseconds{nTimeout}, requested, &occurred)) {
LogPrintf("wait for connect to %s failed: %s\n",
addrConnect.ToString(),
NetworkErrorString(WSAGetLastError()));
return false;
} else if (occurred == 0) {
LogPrint(BCLog::NET, "connection attempt to %s timed out\n", addrConnect.ToString());
return false;
}
// Even if the wait was successful, the connect might not
// have been successful. The reason for this failure is hidden away
// in the SO_ERROR for the socket in modern systems. We read it into
// sockerr here.
int sockerr;
socklen_t sockerr_len = sizeof(sockerr);
if (sock.GetSockOpt(SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&sockerr, &sockerr_len) ==
SOCKET_ERROR) {
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
return false;
}
if (sockerr != 0) {
LogConnectFailure(manual_connection,
"connect() to %s failed after wait: %s",
addrConnect.ToString(),
NetworkErrorString(sockerr));
return false;
}
}
#ifdef WIN32
else if (WSAGetLastError() != WSAEISCONN)
#else
else
#endif
{
LogConnectFailure(manual_connection, "connect() to %s failed: %s", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
return false;
}
}
return true;
*/
}
pub fn set_proxy(
net: Network,
addr_proxy: &ProxyType) -> bool {
todo!();
/*
assert(net >= 0 && net < NET_MAX);
if (!addrProxy.IsValid())
return false;
LOCK(g_proxyinfo_mutex);
proxyInfo[net] = addrProxy;
return true;
*/
}
pub fn get_proxy(
net: Network,
proxy_info_out: &mut ProxyType) -> bool {
todo!();
/*
assert(net >= 0 && net < NET_MAX);
LOCK(g_proxyinfo_mutex);
if (!proxyInfo[net].IsValid())
return false;
proxyInfoOut = proxyInfo[net];
return true;
*/
}
/**
| Set the name proxy to use for all connections
| to nodes specified by a hostname. After
| setting this proxy, connecting to a
| node specified by a hostname won't result
| in a local lookup of said hostname, rather,
| connect to the node by asking the name
| proxy for a proxy connection to the hostname,
| effectively delegating the hostname
| lookup to the specified proxy.
|
| This delegation increases privacy
| for those who set the name proxy as they
| no longer leak their external hostname
| queries to their DNS servers.
|
| -----------
| @note
|
| SOCKS5's support for UDP-over-SOCKS5
| has been considered, but no SOCK5 server
| in common use (most notably Tor) actually
| implements UDP support, and a DNS resolver
| is beyond the scope of this project.
|
| -----------
| @return
|
| Whether or not the operation succeeded.
|
*/
pub fn set_name_proxy(addr_proxy: &ProxyType) -> bool {
todo!();
/*
if (!addrProxy.IsValid())
return false;
LOCK(g_proxyinfo_mutex);
nameProxy = addrProxy;
return true;
*/
}
pub fn get_name_proxy(name_proxy_out: &mut ProxyType) -> bool {
todo!();
/*
LOCK(g_proxyinfo_mutex);
if(!nameProxy.IsValid())
return false;
nameProxyOut = nameProxy;
return true;
*/
}
pub fn have_name_proxy() -> bool {
todo!();
/*
LOCK(g_proxyinfo_mutex);
return nameProxy.IsValid();
*/
}
pub fn is_proxy(addr: &NetAddr) -> bool {
todo!();
/*
LOCK(g_proxyinfo_mutex);
for (int i = 0; i < NET_MAX; i++) {
if (addr == static_cast<CNetAddr>(proxyInfo[i].proxy))
return true;
}
return false;
*/
}
/**
| Connect to a specified destination
| service through a SOCKS5 proxy by first
| connecting to the SOCKS5 proxy.
|
| -----------
| @param proxy
|
| The SOCKS5 proxy.
| ----------
| @param strDest
|
| The destination service to which to
| connect.
| ----------
| @param port
|
| The destination port.
| ----------
| @param sock
|
| The socket on which to connect to the
| SOCKS5 proxy.
| ----------
| @param nTimeout
|
| Wait this many milliseconds for the
| connection to the SOCKS5 proxy to be
| established.
| ----------
| @param[out] outProxyConnectionFailed
|
| Whether or not the connection to the
|
| SOCKS5 proxy failed.
|
| -----------
| @return
|
| Whether or not the operation succeeded.
|
*/
pub fn connect_through_proxy(
proxy: &ProxyType,
str_dest: &String,
port: u16,
sock: &Sock,
n_timeout: i32,
out_proxy_connection_failed: &mut bool) -> bool {
todo!();
/*
// first connect to proxy server
if (!ConnectSocketDirectly(proxy.proxy, sock, nTimeout, true)) {
outProxyConnectionFailed = true;
return false;
}
// do socks negotiation
if (proxy.randomize_credentials) {
ProxyCredentials random_auth;
static std::atomic_int counter(0);
random_auth.username = random_auth.password = strprintf("%i", counter++);
if (!Socks5(strDest, port, &random_auth, sock)) {
return false;
}
} else {
if (!Socks5(strDest, port, 0, sock)) {
return false;
}
}
return true;
*/
}
/**
| Disable or enable blocking-mode for
| a socket
|
*/
pub fn set_socket_non_blocking(
h_socket: &CSocket,
non_blocking: bool) -> bool {
todo!();
/*
if (fNonBlocking) {
#ifdef WIN32
u_long nOne = 1;
if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR) {
#else
int fFlags = fcntl(hSocket, F_GETFL, 0);
if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) {
#endif
return false;
}
} else {
#ifdef WIN32
u_long nZero = 0;
if (ioctlsocket(hSocket, FIONBIO, &nZero) == SOCKET_ERROR) {
#else
int fFlags = fcntl(hSocket, F_GETFL, 0);
if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) {
#endif
return false;
}
}
return true;
*/
}
/**
| Set the TCP_NODELAY flag on a socket
|
*/
pub fn set_socket_no_delay(h_socket: &CSocket) -> bool {
todo!();
/*
int set = 1;
int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
return rc == 0;
*/
}