2007-11-14 22:18:48 -05:00
|
|
|
/*
|
|
|
|
* "$Id: pqibin.cc,v 1.4 2007-02-18 21:46:49 rmf24 Exp $"
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2006 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pqi/pqibin.h"
|
2010-09-18 15:09:11 -04:00
|
|
|
#include "pqi/authssl.h"
|
2010-07-10 16:34:03 -04:00
|
|
|
#include "util/rsnet.h"
|
2011-04-03 19:11:38 -04:00
|
|
|
#include "util/rsdir.h"
|
2010-07-10 16:34:03 -04:00
|
|
|
|
2011-01-20 12:25:30 -05:00
|
|
|
// #define DEBUG_PQIBIN
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
BinFileInterface::BinFileInterface(const char *fname, int flags)
|
2008-02-05 08:45:04 -05:00
|
|
|
:bin_flags(flags), buf(NULL), hash(NULL), bcount(0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* if read + write - open r+ */
|
|
|
|
if ((bin_flags & BIN_FLAGS_READABLE) &&
|
|
|
|
(bin_flags & BIN_FLAGS_WRITEABLE))
|
|
|
|
{
|
2011-04-03 19:11:38 -04:00
|
|
|
buf = RsDirUtil::rs_fopen(fname, "rb+");
|
2007-11-14 22:18:48 -05:00
|
|
|
/* if the file don't exist */
|
|
|
|
if (!buf)
|
|
|
|
{
|
2011-04-03 19:11:38 -04:00
|
|
|
buf = RsDirUtil::rs_fopen(fname, "wb+");
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (bin_flags & BIN_FLAGS_READABLE)
|
|
|
|
{
|
2011-04-03 19:11:38 -04:00
|
|
|
buf = RsDirUtil::rs_fopen(fname, "rb");
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
else if (bin_flags & BIN_FLAGS_WRITEABLE)
|
|
|
|
{
|
|
|
|
// This is enough to remove old file in Linux...
|
|
|
|
// but not in windows.... (what to do)
|
2011-04-03 19:11:38 -04:00
|
|
|
buf = RsDirUtil::rs_fopen(fname, "wb");
|
2007-11-14 22:18:48 -05:00
|
|
|
fflush(buf); /* this might help windows! */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* not read or write! */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
{
|
2011-01-20 12:25:30 -05:00
|
|
|
#ifdef DEBUG_PQIBIN
|
|
|
|
std::cerr << "BinFileInterface: " << (void*)this << ": openned file " << fname << std::endl;
|
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
/* no problem */
|
|
|
|
|
|
|
|
/* go to the end */
|
|
|
|
fseek(buf, 0L, SEEK_END);
|
|
|
|
/* get size */
|
|
|
|
size = ftell(buf);
|
|
|
|
/* back to the start */
|
|
|
|
fseek(buf, 0L, SEEK_SET);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash = new pqihash();
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BinFileInterface::~BinFileInterface()
|
|
|
|
{
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
fclose(buf);
|
2011-01-20 12:25:30 -05:00
|
|
|
#ifdef DEBUG_PQIBIN
|
|
|
|
std::cerr << "BinFileInterface: " << (void*)this << ": closed file " << std::endl;
|
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-04-02 09:55:45 -04:00
|
|
|
|
|
|
|
if (hash)
|
|
|
|
{
|
|
|
|
delete hash;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-03-31 10:06:59 -04:00
|
|
|
|
|
|
|
int BinFileInterface::close()
|
|
|
|
{
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
fclose(buf);
|
2011-01-20 12:25:30 -05:00
|
|
|
#ifdef DISTRIB_DEBUG
|
|
|
|
std::cerr << "BinFileInterface: " << (void*)this << ": closed file " << std::endl;
|
|
|
|
#endif
|
2008-03-31 10:06:59 -04:00
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
int BinFileInterface::senddata(void *data, int len)
|
|
|
|
{
|
|
|
|
if (!buf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (1 != fwrite(data, len, 1, buf))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash->addData(data, len);
|
2008-02-05 08:45:04 -05:00
|
|
|
bcount += len;
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BinFileInterface::readdata(void *data, int len)
|
|
|
|
{
|
|
|
|
if (!buf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (1 != fread(data, len, 1, buf))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash->addData(data, len);
|
2008-02-05 08:45:04 -05:00
|
|
|
bcount += len;
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
RsFileHash BinFileInterface::gethash()
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
RsFileHash hashstr;
|
2008-01-25 01:36:40 -05:00
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash->Complete(hashstr);
|
|
|
|
}
|
|
|
|
return hashstr;
|
|
|
|
}
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
uint64_t BinFileInterface::bytecount()
|
|
|
|
{
|
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
return bcount;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-09-18 15:09:11 -04:00
|
|
|
|
|
|
|
int BinFileInterface::getFileSize()
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BinEncryptedFileInterface::BinEncryptedFileInterface(const char* fname, int flags)
|
|
|
|
: BinFileInterface(fname, flags), data(NULL), haveData(false), sizeData(0), cpyCount(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BinEncryptedFileInterface::~BinEncryptedFileInterface()
|
|
|
|
{
|
|
|
|
if((sizeData > 0) && data != NULL)
|
|
|
|
{
|
2014-08-25 17:07:07 -04:00
|
|
|
free(data);
|
2010-09-18 15:09:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int BinEncryptedFileInterface::senddata(void* data, int len)
|
|
|
|
{
|
|
|
|
|
2014-08-25 17:07:07 -04:00
|
|
|
void* encrytedData = NULL;
|
2010-09-18 15:09:11 -04:00
|
|
|
int encDataLen = 0;
|
|
|
|
|
|
|
|
// encrypt using own ssl public key
|
|
|
|
if(len > 0)
|
|
|
|
AuthSSL::getAuthSSL()->encrypt((void*&)encrytedData, encDataLen, data, len, AuthSSL::getAuthSSL()->OwnId());
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
if((encDataLen > 0) && (encrytedData != NULL))
|
|
|
|
{
|
2014-08-25 17:07:07 -04:00
|
|
|
BinFileInterface::senddata((unsigned char *)encrytedData, encDataLen);
|
|
|
|
free(encrytedData);
|
2010-09-18 15:09:11 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int BinEncryptedFileInterface::readdata(void* data, int len)
|
|
|
|
{
|
|
|
|
// to respect the inherited behavior of BinInterface
|
|
|
|
// the whole file is read and decryped and store to be read by subsequent calls
|
|
|
|
char* encryptedData = NULL;
|
|
|
|
int encrypDataLen = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if(!haveData) // read whole data for first call, or first call after close()
|
|
|
|
{
|
|
|
|
|
|
|
|
encrypDataLen = BinFileInterface::getFileSize();
|
|
|
|
encryptedData = new char[encrypDataLen];
|
|
|
|
|
|
|
|
// make sure assign was successful
|
|
|
|
if(encryptedData == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
if(-1 == BinFileInterface::readdata(encryptedData, encrypDataLen))
|
2012-07-13 18:10:52 -04:00
|
|
|
{
|
|
|
|
delete[] encryptedData;
|
2010-09-18 15:09:11 -04:00
|
|
|
return -1;
|
2012-07-13 18:10:52 -04:00
|
|
|
}
|
2010-09-18 15:09:11 -04:00
|
|
|
|
|
|
|
if((encrypDataLen > 0) && (encryptedData != NULL))
|
|
|
|
{
|
|
|
|
if(!AuthSSL::getAuthSSL()->decrypt((void*&)(this->data), sizeData, encryptedData, encrypDataLen))
|
2012-07-13 18:10:52 -04:00
|
|
|
{
|
|
|
|
delete[] encryptedData;
|
2010-09-18 15:09:11 -04:00
|
|
|
return -1;
|
2012-07-13 18:10:52 -04:00
|
|
|
}
|
2010-09-18 15:09:11 -04:00
|
|
|
|
|
|
|
haveData = true;
|
|
|
|
delete[] encryptedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(len <= sizeData)
|
|
|
|
{
|
|
|
|
memcpy(data, this->data, len);
|
|
|
|
cpyCount += len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "BinEncryptedFileInterface::readData(): Error, Asking for more data than present" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
if((cpyCount + len) <= sizeData)
|
|
|
|
{
|
|
|
|
memcpy(data, (void *) ((this->data) + cpyCount), len);
|
|
|
|
cpyCount += len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "BinEncryptedFileInterface::readData(): Error, Asking for more data than present" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BinEncryptedFileInterface::close()
|
|
|
|
{
|
|
|
|
if(data != NULL)
|
|
|
|
{
|
2014-08-25 17:07:07 -04:00
|
|
|
free(data);
|
2010-09-18 15:09:11 -04:00
|
|
|
sizeData = 0;
|
|
|
|
haveData = false;
|
|
|
|
cpyCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BinFileInterface::close();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t BinEncryptedFileInterface::bytecount()
|
|
|
|
{
|
|
|
|
return cpyCount;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
bool BinEncryptedFileInterface::moretoread(uint32_t /* usec */)
|
2010-09-18 15:09:11 -04:00
|
|
|
{
|
|
|
|
if(haveData)
|
|
|
|
return (cpyCount < sizeData);
|
|
|
|
else
|
|
|
|
return cpyCount < getFileSize();
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
BinMemInterface::BinMemInterface(int defsize, int flags)
|
|
|
|
:bin_flags(flags), buf(NULL), size(defsize),
|
2008-02-05 08:45:04 -05:00
|
|
|
recvsize(0), readloc(0), hash(NULL), bcount(0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
buf = malloc(defsize);
|
2008-01-25 01:36:40 -05:00
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash = new pqihash();
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2008-02-08 07:39:40 -05:00
|
|
|
|
|
|
|
BinMemInterface::BinMemInterface(const void *data, const int defsize, int flags)
|
|
|
|
:bin_flags(flags), buf(NULL), size(defsize),
|
|
|
|
recvsize(0), readloc(0), hash(NULL), bcount(0)
|
|
|
|
{
|
|
|
|
buf = malloc(defsize);
|
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash = new pqihash();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* just remove the const
|
|
|
|
* *BAD* but senddata don't change it anyway
|
|
|
|
*/
|
|
|
|
senddata((void *) data, defsize);
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
BinMemInterface::~BinMemInterface()
|
2008-04-02 09:55:45 -04:00
|
|
|
{
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
if (hash)
|
|
|
|
delete hash;
|
|
|
|
return;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
int BinMemInterface::close()
|
|
|
|
{
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
free(buf);
|
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
size = 0;
|
|
|
|
recvsize = 0;
|
|
|
|
readloc = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
/* some fns to mess with the memory */
|
|
|
|
int BinMemInterface::fseek(int loc)
|
|
|
|
{
|
|
|
|
if (loc <= recvsize)
|
|
|
|
{
|
|
|
|
readloc = loc;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-08 07:39:40 -05:00
|
|
|
int BinMemInterface::senddata(void *data, const int len)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
if(recvsize + len > size)
|
|
|
|
{
|
|
|
|
/* resize? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy((char *) buf + recvsize, data, len);
|
2008-01-25 01:36:40 -05:00
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash->addData(data, len);
|
2008-02-05 08:45:04 -05:00
|
|
|
bcount += len;
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
recvsize += len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int BinMemInterface::readdata(void *data, int len)
|
|
|
|
{
|
|
|
|
if(readloc + len > recvsize)
|
|
|
|
{
|
|
|
|
/* no more stuff? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(data, (char *) buf + readloc, len);
|
2008-01-25 01:36:40 -05:00
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash->addData(data, len);
|
2008-02-05 08:45:04 -05:00
|
|
|
bcount += len;
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
readloc += len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
RsFileHash BinMemInterface::gethash()
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
RsFileHash hashstr;
|
2008-01-25 01:36:40 -05:00
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
hash->Complete(hashstr);
|
|
|
|
}
|
|
|
|
return hashstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
uint64_t BinMemInterface::bytecount()
|
|
|
|
{
|
|
|
|
if (bin_flags & BIN_FLAGS_HASH_DATA)
|
|
|
|
{
|
|
|
|
return bcount;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-08 07:39:40 -05:00
|
|
|
bool BinMemInterface::writetofile(const char *fname)
|
|
|
|
{
|
2011-04-03 19:11:38 -04:00
|
|
|
FILE *fd = RsDirUtil::rs_fopen(fname, "wb");
|
2008-02-08 07:39:40 -05:00
|
|
|
if (!fd)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (1 != fwrite(buf, recvsize, 1, fd))
|
|
|
|
{
|
|
|
|
fclose(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BinMemInterface::readfromfile(const char *fname)
|
|
|
|
{
|
2011-04-03 19:11:38 -04:00
|
|
|
FILE *fd = RsDirUtil::rs_fopen(fname, "rb");
|
2008-02-08 07:39:40 -05:00
|
|
|
if (!fd)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get size */
|
|
|
|
::fseek(fd, 0L, SEEK_END);
|
|
|
|
int fsize = ftell(fd);
|
|
|
|
|
|
|
|
if (fsize > size)
|
|
|
|
{
|
|
|
|
/* not enough room */
|
|
|
|
std::cerr << "BinMemInterface::readfromfile() not enough room";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
::fseek(fd, 0L, SEEK_SET);
|
|
|
|
if (1 != fread(buf, fsize, 1, fd))
|
|
|
|
{
|
|
|
|
/* not enough room */
|
|
|
|
std::cerr << "BinMemInterface::readfromfile() failed fread";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
recvsize = fsize;
|
|
|
|
readloc = 0;
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2008-02-05 08:45:04 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
void printNetBinID(std::ostream &out, const RsPeerId& id, uint32_t t)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
out << "NetBinId(" << id << ",";
|
|
|
|
if (t == PQI_CONNECT_TCP)
|
|
|
|
{
|
|
|
|
out << "TCP)";
|
|
|
|
}
|
2015-09-15 11:10:48 -04:00
|
|
|
else if (t & (PQI_CONNECT_HIDDEN_TOR_TCP | PQI_CONNECT_HIDDEN_I2P_TCP))
|
2013-09-03 09:35:39 -04:00
|
|
|
{
|
|
|
|
out << "HTCP";
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
out << "UDP)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************** NetBinDummy ******************************
|
|
|
|
* A test framework,
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pqi/pqiperson.h"
|
|
|
|
|
|
|
|
const uint32_t DEFAULT_DUMMY_DELTA = 5;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
NetBinDummy::NetBinDummy(PQInterface *parent, const RsPeerId& id, uint32_t t)
|
|
|
|
:NetBinInterface(parent, id), type(t), dummyConnected(false), toConnect(false), connectDelta(DEFAULT_DUMMY_DELTA)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Net Interface
|
2013-09-13 10:35:19 -04:00
|
|
|
int NetBinDummy::connect(const struct sockaddr_storage &raddr)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::connect(";
|
2013-09-13 10:35:19 -04:00
|
|
|
std::cerr << sockaddr_storage_tostring(raddr);
|
2008-01-25 01:36:40 -05:00
|
|
|
std::cerr << ") ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "NetBinDummy::dummyConnect = true!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (type == PQI_CONNECT_TCP)
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy:: Not connecting TCP";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
if (parent())
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
struct sockaddr_storage addr = raddr;
|
|
|
|
parent()->notifyEvent(this, CONNECT_FAILED, raddr);
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!dummyConnected)
|
|
|
|
{
|
|
|
|
toConnect = true;
|
|
|
|
connectTS = time(NULL) + connectDelta;
|
|
|
|
std::cerr << "NetBinDummy::toConnect = true, connect in: ";
|
|
|
|
std::cerr << connectDelta << " secs";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy:: Already Connected!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetBinDummy::listen()
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::connect() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetBinDummy::stoplistening()
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::stoplistening() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetBinDummy::disconnect()
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::disconnect() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "NetBinDummy::dummyConnect = false!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
dummyConnected = false;
|
|
|
|
|
|
|
|
if (parent())
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
sockaddr_storage_clear(addr);
|
|
|
|
|
|
|
|
parent()->notifyEvent(this, CONNECT_FAILED, addr);
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetBinDummy::reset()
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::reset() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
disconnect();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Bin Interface.
|
|
|
|
int NetBinDummy::tick()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (toConnect)
|
|
|
|
{
|
|
|
|
if (connectTS < time(NULL))
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::tick() dummyConnected = true ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
dummyConnected = true;
|
|
|
|
toConnect = false;
|
|
|
|
if (parent())
|
2013-10-01 06:11:34 -04:00
|
|
|
{
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
sockaddr_storage_clear(addr);
|
|
|
|
|
|
|
|
parent()->notifyEvent(this, CONNECT_SUCCESS, addr);
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::tick() toConnect ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
int NetBinDummy::senddata(void */*data*/, int len)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::senddata() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (dummyConnected)
|
|
|
|
return len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
int NetBinDummy::readdata(void */*data*/, int /*len*/)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::readdata() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetBinDummy::netstatus()
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::netstatus() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NetBinDummy::isactive()
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::isactive() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
if (dummyConnected)
|
|
|
|
std::cerr << " true ";
|
|
|
|
else
|
|
|
|
std::cerr << " false ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return dummyConnected;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
bool NetBinDummy::moretoread(uint32_t /* usec */)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::moretoread() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
bool NetBinDummy::cansend(uint32_t /* usec */)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::cansend() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return dummyConnected;
|
|
|
|
}
|
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
int NetBinDummy::close()
|
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::close() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
RsFileHash NetBinDummy::gethash()
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
std::cerr << "NetBinDummy::gethash() ";
|
|
|
|
printNetBinID(std::cerr, PeerId(), type);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
return RsFileHash();
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
|