| 166 | } |
| 167 | |
| 168 | bool static Socks4(const CService& addrDest, SOCKET& hSocket) { |
| 169 | LogPrint(BCLog::INFO, "SOCKS4 connecting %s\n", addrDest.ToString()); |
| 170 | if (!addrDest.IsIPv4()) { |
| 171 | closesocket(hSocket); |
| 172 | return ERRORMSG("Proxy destination is not IPv4"); |
| 173 | } |
| 174 | char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user"; |
| 175 | struct sockaddr_in addr; |
| 176 | socklen_t len = sizeof(addr); |
| 177 | if (!addrDest.GetSockAddr((struct sockaddr*)&addr, &len) || addr.sin_family != AF_INET) { |
| 178 | closesocket(hSocket); |
| 179 | return ERRORMSG("Cannot get proxy destination address"); |
| 180 | } |
| 181 | memcpy(pszSocks4IP + 2, &addr.sin_port, 2); |
| 182 | memcpy(pszSocks4IP + 4, &addr.sin_addr, 4); |
| 183 | char* pszSocks4 = pszSocks4IP; |
| 184 | int nSize = sizeof(pszSocks4IP); |
| 185 | |
| 186 | int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL); |
| 187 | if (ret != nSize) { |
| 188 | closesocket(hSocket); |
| 189 | return ERRORMSG("Error sending to proxy"); |
| 190 | } |
| 191 | char pchRet[8]; |
| 192 | if (recv(hSocket, pchRet, 8, 0) != 8) { |
| 193 | closesocket(hSocket); |
| 194 | return ERRORMSG("Error reading proxy response"); |
| 195 | } |
| 196 | if (pchRet[1] != 0x5a) { |
| 197 | closesocket(hSocket); |
| 198 | if (pchRet[1] != 0x5b) LogPrint(BCLog::INFO, "ERROR: Proxy returned error %d\n", pchRet[1]); |
| 199 | return false; |
| 200 | } |
| 201 | LogPrint(BCLog::INFO, "SOCKS4 connected %s\n", addrDest.ToString()); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | bool static Socks5(string strDest, int port, SOCKET& hSocket) { |
| 206 | LogPrint(BCLog::INFO, "SOCKS5 connecting %s\n", strDest); |
no test coverage detected