Fix receiving handling in BT UDP SOCKS5 proxy

pull/1857/head
myl7 2021-12-05 21:42:44 +08:00
parent be6243f0b7
commit 71b652f8a1
No known key found for this signature in database
GPG Key ID: 04F1013B67177C88
1 changed files with 4 additions and 4 deletions

View File

@ -165,7 +165,7 @@ ssize_t DHTConnectionSocksProxyImpl::receiveMessage(unsigned char* data,
uint16_t& port) uint16_t& port)
{ {
Endpoint remoteEndpoint; Endpoint remoteEndpoint;
size_t resLen = len + family_ == AF_INET ? 10 : 22; size_t resLen = len + (family_ == AF_INET ? 10 : 22);
std::string buf; std::string buf;
buf.resize(resLen); buf.resize(resLen);
ssize_t length = getSocket()->readDataFrom(&buf[0], resLen, remoteEndpoint); ssize_t length = getSocket()->readDataFrom(&buf[0], resLen, remoteEndpoint);
@ -175,14 +175,14 @@ ssize_t DHTConnectionSocksProxyImpl::receiveMessage(unsigned char* data,
// unencapsulate SOCKS5 UDP header if has // unencapsulate SOCKS5 UDP header if has
if (length > (family_ == AF_INET ? 10 : 22) && if (length > (family_ == AF_INET ? 10 : 22) &&
buf.substr(0, 3) == "\x00\x00\x00" && buf.substr(0, 3) == std::string("\x00\x00\x00", 3) &&
buf[3] == (family_ == AF_INET ? '\x01' : '\x04')) { buf[3] == (family_ == AF_INET ? '\x01' : '\x04')) {
if (family_ == AF_INET) { if (family_ == AF_INET) {
char addrBuf[20]; char addrBuf[20];
inetNtop(AF_INET, &buf[4], addrBuf, 20); inetNtop(AF_INET, &buf[4], addrBuf, 20);
host = std::string(addrBuf); host = std::string(addrBuf);
port = ntohs(*(reinterpret_cast<uint16_t*>(&buf[8]))); port = ntohs(*(reinterpret_cast<uint16_t*>(&buf[8])));
memcpy(data, buf.c_str(), length - 10); memcpy(data, &buf[10], length - 10);
return length - 10; return length - 10;
} }
else { else {
@ -190,7 +190,7 @@ ssize_t DHTConnectionSocksProxyImpl::receiveMessage(unsigned char* data,
inetNtop(AF_INET6, &buf[4], addrBuf, 50); inetNtop(AF_INET6, &buf[4], addrBuf, 50);
host = std::string(addrBuf); host = std::string(addrBuf);
port = ntohs(*(reinterpret_cast<uint16_t*>(&buf[20]))); port = ntohs(*(reinterpret_cast<uint16_t*>(&buf[20])));
memcpy(data, buf.c_str(), length - 22); memcpy(data, &buf[22], length - 22);
return length - 22; return length - 22;
} }
} }