Untitled
unknown
c_cpp
9 months ago
6.1 kB
2
Indexable
void ConnectionSocket::openConnection(std::string address, uint16_t port, std::string secret, bool ipv6, int32_t networkType) { currentNetworkType = networkType; isIpv6 = ipv6; currentAddress = address; currentPort = port; waitingForHostResolve = ""; adjustWriteOpAfterResolve = false; tlsState = 0; ConnectionsManager::getInstance(instanceNum).attachConnection(this); memset(&socketAddress, 0, sizeof(sockaddr_in)); memset(&socketAddress6, 0, sizeof(sockaddr_in6)); std::string *proxyAddress = &overrideProxyAddress; std::string *proxySecret = &overrideProxySecret; uint16_t proxyPort = overrideProxyPort; if (proxyAddress->empty()) { proxyAddress = &ConnectionsManager::getInstance(instanceNum).proxyAddress; proxyPort = ConnectionsManager::getInstance(instanceNum).proxyPort; proxySecret = &ConnectionsManager::getInstance(instanceNum).proxySecret; } if (!proxyAddress->empty()) { if (LOGS_ENABLED) DEBUG_D("connection(%p) connecting via proxy %s:%d secret[%d]", this, proxyAddress->c_str(), proxyPort, (int) proxySecret->size()); if ((socketFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if (LOGS_ENABLED) DEBUG_E("connection(%p) can't create proxy socket", this); closeSocket(1, -1); return; } uint32_t tempBuffLength; if (proxySecret->empty()) { proxyAuthState = 1; tempBuffLength = 1024; } else if (proxySecret->size() > 17 && (*proxySecret)[0] == '\xee') { proxyAuthState = 10; currentSecret = proxySecret->substr(1, 16); currentSecretDomain = proxySecret->substr(17); tempBuffLength = 65 * 1024; } else { proxyAuthState = 0; tempBuffLength = 0; } if (tempBuffLength > 0) { if (tempBuffer == nullptr || tempBuffer->length < tempBuffLength) { if (tempBuffer != nullptr) { delete tempBuffer; } tempBuffer = new ByteArray(tempBuffLength); } } socketAddress.sin_family = AF_INET; socketAddress.sin_port = htons(proxyPort); bool continueCheckAddress; if (inet_pton(AF_INET, proxyAddress->c_str(), &socketAddress.sin_addr.s_addr) != 1) { continueCheckAddress = true; if (LOGS_ENABLED) DEBUG_D("connection(%p) not ipv4 address %s", this, proxyAddress->c_str()); } else { ipv6 = false; continueCheckAddress = false; } if (continueCheckAddress) { if (inet_pton(AF_INET6, proxyAddress->c_str(), &socketAddress6.sin6_addr.s6_addr) != 1) { continueCheckAddress = true; if (LOGS_ENABLED) DEBUG_D("connection(%p) not ipv6 address %s", this, proxyAddress->c_str()); } else { ipv6 = true; continueCheckAddress = false; } if (continueCheckAddress) { #ifdef USE_DELEGATE_HOST_RESOLVE waitingForHostResolve = *proxyAddress; ConnectionsManager::getInstance(instanceNum).delegate->getHostByName(*proxyAddress, instanceNum, this); return; #else struct hostent *he; if ((he = gethostbyname(proxyAddress->c_str())) == nullptr) { if (LOGS_ENABLED) DEBUG_E("connection(%p) can't resolve host %s address", this, proxyAddress->c_str()); closeSocket(1, -1); return; } struct in_addr **addr_list = (struct in_addr **) he->h_addr_list; if (addr_list[0] != nullptr) { socketAddress.sin_addr.s_addr = addr_list[0]->s_addr; if (LOGS_ENABLED) DEBUG_D("connection(%p) resolved host %s address %x", this, proxyAddress->c_str(), addr_list[0]->s_addr); ipv6 = false; } else { if (LOGS_ENABLED) DEBUG_E("connection(%p) can't resolve host %s address", this, proxyAddress->c_str()); closeSocket(1, -1); return; } #endif } } } else { proxyAuthState = 0; if ((socketFd = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0)) < 0) { if (LOGS_ENABLED) DEBUG_E("connection(%p) can't create socket", this); closeSocket(1, -1); return; } if (ipv6) { socketAddress6.sin6_family = AF_INET6; socketAddress6.sin6_port = htons(port); if (inet_pton(AF_INET6, address.c_str(), &socketAddress6.sin6_addr.s6_addr) != 1) { if (LOGS_ENABLED) DEBUG_E("connection(%p) bad ipv6 %s", this, address.c_str()); closeSocket(1, -1); return; } } else { socketAddress.sin_family = AF_INET; socketAddress.sin_port = htons(port); if (inet_pton(AF_INET, address.c_str(), &socketAddress.sin_addr.s_addr) != 1) { if (LOGS_ENABLED) DEBUG_E("connection(%p) bad ipv4 %s", this, address.c_str()); closeSocket(1, -1); return; } } uint32_t tempBuffLength; if (secret.size() > 17 && secret[0] == '\xee') { proxyAuthState = 10; currentSecret = secret.substr(1, 16); currentSecretDomain = secret.substr(17); tempBuffLength = 65 * 1024; } else { proxyAuthState = 0; tempBuffLength = 0; } if (tempBuffLength > 0) { if (tempBuffer == nullptr || tempBuffer->length < tempBuffLength) { if (tempBuffer != nullptr) { delete tempBuffer; } tempBuffer = new ByteArray(tempBuffLength); } } } openConnectionInternal(ipv6); }
Editor is loading...
Leave a Comment