Merge pull request #47 from csoler/v0.6-TorControl3

fixed bug in friendserver randomly dropping some incoming packets
This commit is contained in:
csoler 2022-09-28 09:27:19 +02:00 committed by GitHub
commit ea985af7db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 18 deletions

View File

@ -20,6 +20,8 @@
* *
******************************************************************************/
#include <netinet/tcp.h>
#include "pqi/pqithreadstreamer.h"
#include "retroshare/rspeers.h"
#include "retroshare/rsnotify.h"
@ -29,6 +31,8 @@
#include "pqi/pqifdbin.h"
#include "pqi/pqiproxy.h"
#define DEBUG_FSCLIENT
bool FsClient::requestFriends(const std::string& address,uint16_t port,
const std::string& proxy_address,uint16_t proxy_port,
uint32_t reqs,
@ -127,7 +131,7 @@ void FsClient::handleServerResponse(RsFriendServerServerResponseItem *item,std::
bool FsClient::sendItem(const std::string& server_address,uint16_t server_port,
const std::string& proxy_address,uint16_t proxy_port,
RsItem *item,std::list<RsItem*>& response)
RsFriendServerItem *item,std::list<RsItem*>& response)
{
// open a connection
@ -145,6 +149,9 @@ bool FsClient::sendItem(const std::string& server_address,uint16_t server_port,
return 1;
}
int flags=1;
setsockopt(CreateSocket,SOL_SOCKET,TCP_NODELAY,(char*)&flags,sizeof(flags));
ipOfServer.sin_family = AF_INET;
ipOfServer.sin_port = htons(proxy_port);
ipOfServer.sin_addr.s_addr = inet_addr(proxy_address.c_str());
@ -182,26 +189,26 @@ bool FsClient::sendItem(const std::string& server_address,uint16_t server_port,
pqithreadstreamer p(this,rss,RsPeerId(),bio,BIN_FLAGS_READABLE | BIN_FLAGS_WRITEABLE | BIN_FLAGS_NO_CLOSE);
p.start();
uint32_t ss;
p.SendItem(item,ss);
RsDbg() << "Item sent. Waiting for response..." ;
RsDbg() << "Sending item. size=" << fss->size(item) << ". Waiting for response..." ;
// Now attempt to read and deserialize anything that comes back from that connexion until it gets closed by the server.
uint32_t ss;
p.SendItem(item,ss);
while(true)
{
p.tick(); // ticks bio
RsItem *item = GetItem();
RsItem *ritem = GetItem();
#ifdef DEBUG_FSCLIENT
RsDbg() << "Ticking for response...";
#endif
if(item)
if(ritem)
{
response.push_back(item);
response.push_back(ritem);
std::cerr << "Got a response item: " << std::endl;
std::cerr << *item << std::endl;
std::cerr << *ritem << std::endl;
RsDbg() << "End of transmission. " ;
break;
@ -232,6 +239,8 @@ bool FsClient::checkProxyConnection(const std::string& onion_address,uint16_t po
RsErr() << "Socket not created";
return false;
}
int flags=1;
setsockopt(CreateSocket,SOL_SOCKET,TCP_NODELAY,(char*)&flags,sizeof(flags));
ipOfServer.sin_family = AF_INET;
ipOfServer.sin_port = htons(proxy_port);

View File

@ -48,7 +48,7 @@ protected:
private:
bool sendItem(const std::string &server_address, uint16_t server_port,
const std::string &proxy_address, uint16_t proxy_port,
RsItem *item, std::list<RsItem *> &response);
RsFriendServerItem *item, std::list<RsItem *> &response);
void handleServerResponse(RsFriendServerServerResponseItem *item, std::map<std::string, bool> &friend_certificates);

View File

@ -24,6 +24,10 @@
#include "util/rsfile.h"
#include "pqi/pqifdbin.h"
#ifdef DEBUG_FS_BIN
#include "util/rsprint.h"
#endif
RsFdBinInterface::RsFdBinInterface(int file_descriptor, bool is_socket)
: mCLintConnt(file_descriptor),mIsSocket(is_socket),mIsActive(false)
{
@ -99,7 +103,7 @@ int RsFdBinInterface::read_pending()
if(readbytes == 0)
{
RsDbg() << "Reached END of the stream!" ;
RsDbg() << "Closing!" ;
RsDbg() << "Closing socket!" ;
close();
return mTotalInBufferBytes;
@ -125,8 +129,8 @@ int RsFdBinInterface::read_pending()
if(readbytes > 0)
{
#ifdef DEBUG_FS_BIN
RsDbg() << "Received the following bytes: " << RsUtil::BinToHex( reinterpret_cast<unsigned char*>(inBuffer),readbytes,50) << std::endl;
RsDbg() << "Received the following bytes: " << std::string(inBuffer,readbytes) << std::endl;
RsDbg() << "Received the following bytes: size=" << readbytes << " len=" << RsUtil::BinToHex( reinterpret_cast<unsigned char*>(inBuffer),readbytes,50) << std::endl;
RsDbg() << "Received the following bytes: size=" << readbytes << " len=" << std::string(inBuffer,readbytes) << std::endl;
#endif
void *ptr = malloc(readbytes);
@ -248,6 +252,9 @@ int RsFdBinInterface::readdata(void *data, int len)
if(in_buffer.empty())
{
mTotalInBufferBytes -= total_len;
#ifdef DEBUG_FS_BIN
std::cerr << "RsFdBinInterface -- READ --- len=" << total_len << " data=" << RsUtil::BinToHex((uint8_t*)data,total_len)<< std::endl;
#endif
return total_len;
}
@ -255,16 +262,22 @@ int RsFdBinInterface::readdata(void *data, int len)
if(total_len + in_buffer.front().second > len)
{
memcpy(&(static_cast<unsigned char *>(data)[total_len]),in_buffer.front().first,len - total_len);
int bytes_in = len - total_len;
int bytes_out = in_buffer.front().second - bytes_in;
void *ptr = malloc(in_buffer.front().second - (len - total_len));
memcpy(ptr,&(static_cast<unsigned char*>(in_buffer.front().first)[len - total_len]),in_buffer.front().second - (len - total_len));
memcpy(&(static_cast<unsigned char *>(data)[total_len]),in_buffer.front().first,bytes_in);
void *ptr = malloc(bytes_out);
memcpy(ptr,&(static_cast<unsigned char*>(in_buffer.front().first)[bytes_in]),bytes_out);
free(in_buffer.front().first);
in_buffer.front().first = ptr;
in_buffer.front().second -= len-total_len;
in_buffer.front().second -= bytes_in;
mTotalInBufferBytes -= len;
#ifdef DEBUG_FS_BIN
std::cerr << "RsFdBinInterface -- READ --- len=" << len << " data=" << RsUtil::BinToHex((uint8_t*)data,len)<< std::endl;
#endif
return len;
}
else // copy everything
@ -278,6 +291,9 @@ int RsFdBinInterface::readdata(void *data, int len)
}
}
mTotalInBufferBytes -= len;
#ifdef DEBUG_FS_BIN
std::cerr << "RsFdBinInterface -- READ --- len=" << len << " data=" << RsUtil::BinToHex((uint8_t*)data,len)<< std::endl;
#endif
return len;
}
@ -285,6 +301,9 @@ int RsFdBinInterface::senddata(void *data, int len)
{
// shouldn't we better send in multiple packets, similarly to how we read?
#ifdef DEBUG_FS_BIN
std::cerr << "RsFdBinInterface -- SENDING --- len=" << len << " data=" << RsUtil::BinToHex((uint8_t*)data,len)<< std::endl;
#endif
if(len == 0)
{
RsErr() << "Calling FsBioInterface::senddata() with null size or null data pointer";
@ -332,9 +351,11 @@ bool RsFdBinInterface::cansend(uint32_t)
int RsFdBinInterface::close()
{
RsDbg() << "Stopping network interface" << std::endl;
if(moretoread(0) || moretowrite(0))
RsWarn() << "Interface still has " << mTotalInBufferBytes << " / " << mTotalOutBufferBytes << "bytes in/out buffers" << std::endl;
mIsActive = false;
mCLintConnt = 0;
clean();
return 1;
}