merged with latest trunk

This commit is contained in:
csoler 2016-01-01 15:15:19 -05:00
commit 6ecd2991e7
2342 changed files with 29868 additions and 19573 deletions

File diff suppressed because it is too large Load diff

View file

@ -25,35 +25,43 @@
#pragma once
#include <turtle/turtleclientservice.h>
#include <chat/rschatitems.h>
#include <retroshare/rsmsgs.h>
#include <retroshare/rsgxstunnel.h>
class RsGixs ;
static const uint32_t DISTANT_CHAT_AES_KEY_SIZE = 16 ;
class DistantChatService: public RsTurtleClientService
class DistantChatService: public RsGxsTunnelService::RsGxsTunnelClientService
{
public:
DistantChatService(RsGixs *pids)
: mGixs(pids), mDistantChatMtx("distant chat")
{
mTurtle = NULL ;
}
void flush() ;
virtual void connectToTurtleRouter(p3turtle *) ;
// So, public interface only uses DistandChatPeerId, but internally, this is converted into a RsGxsTunnelService::RsGxsTunnelId
DistantChatService() ;
virtual void triggerConfigSave()=0 ;
bool processLoadListItem(const RsItem *item) ;
void addToSaveList(std::list<RsItem*>& list) const;
// Creates the invite if the public key of the distant peer is available.
// Om success, stores the invite in the map above, so that we can respond to tunnel requests.
//
bool initiateDistantChatConnexion(const RsGxsId& to_gxs_id,const RsGxsId &from_gxs_id, uint32_t &error_code) ;
bool closeDistantChatConnexion(const RsGxsId& pid) ;
virtual bool getDistantChatStatus(const RsGxsId &gxs_id,uint32_t &status, RsGxsId *from_gxs_id=NULL) ;
bool initiateDistantChatConnexion(const RsGxsId& to_gxs_id, const RsGxsId &from_gxs_id, DistantChatPeerId& dcpid, uint32_t &error_code) ;
bool closeDistantChatConnexion(const DistantChatPeerId &tunnel_id) ;
// Sets flags to only allow connexion from some people.
uint32_t getDistantChatPermissionFlags() ;
bool setDistantChatPermissionFlags(uint32_t flags) ;
// Returns the status of a distant chat contact. The contact is defined by the tunnel id (turned into a DistantChatPeerId) because
// each pair of talking GXS id needs to be treated separately
virtual bool getDistantChatStatus(const DistantChatPeerId &tunnel_id, DistantChatPeerInfo& cinfo) ;
// derived in p3ChatService
// derived in p3ChatService, so as to pass down some info
virtual void handleIncomingItem(RsItem *) = 0;
virtual bool handleRecvChatMsgItem(RsChatMsgItem *ci)=0 ;
@ -62,78 +70,33 @@ public:
void handleRecvChatStatusItem(RsChatStatusItem *cs) ;
private:
class DistantChatPeerInfo
struct DistantChatContact
{
public:
DistantChatPeerInfo() : last_contact(0), last_keep_alive_sent(0), status(0), direction(0)
{
memset(aes_key, 0, DISTANT_CHAT_AES_KEY_SIZE);
}
time_t last_contact ; // used to keep track of working connexion
time_t last_keep_alive_sent ; // last time we sent a keep alive packet.
unsigned char aes_key[DISTANT_CHAT_AES_KEY_SIZE] ;
uint32_t status ; // info: do we have a tunnel ?
RsPeerId virtual_peer_id; // given by the turtle router. Identifies the tunnel.
RsGxsId own_gxs_id ; // gxs id we're using to talk.
RsTurtleGenericTunnelItem::Direction direction ; // specifiec wether we are client(managing the tunnel) or server.
RsGxsId from_id ;
RsGxsId to_id ;
};
class DistantChatDHInfo
{
public:
DistantChatDHInfo() : dh(0), direction(0), status(0) {}
DH *dh ;
RsGxsId gxs_id ;
RsTurtleGenericTunnelItem::Direction direction ;
uint32_t status ;
TurtleFileHash hash ;
};
// This maps contains the current peers to talk to with distant chat.
//
std::map<RsGxsId, DistantChatPeerInfo> _distant_chat_contacts ; // current peers we can talk to
std::map<RsPeerId,DistantChatDHInfo> _distant_chat_virtual_peer_ids ; // current virtual peers. Used to figure out tunnels, etc.
std::map<DistantChatPeerId, DistantChatContact> mDistantChatContacts ; // current peers we can talk to
// List of items to be sent asap. Used to store items that we cannot pass directly to
// sendTurtleData(), because of Mutex protection.
// Permission handling
uint32_t mDistantChatPermissions ;
std::list<RsChatItem*> pendingDistantChatItems ;
// Overloaded from RsGxsTunnelClientService
public:
virtual void connectToGxsTunnelService(RsGxsTunnelService *tunnel_service) ;
private:
virtual bool acceptDataFromPeer(const RsGxsId& gxs_id,const RsGxsTunnelService::RsGxsTunnelId& tunnel_id) ;
virtual void notifyTunnelStatus(const RsGxsTunnelService::RsGxsTunnelId& tunnel_id,uint32_t tunnel_status) ;
virtual void receiveData(const RsGxsTunnelService::RsGxsTunnelId& id,unsigned char *data,uint32_t data_size) ;
// Overloaded from RsTurtleClientService
virtual bool handleTunnelRequest(const RsFileHash &hash,const RsPeerId& peer_id) ;
virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const RsFileHash& hash,const RsPeerId& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ;
void addVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&,RsTurtleGenericTunnelItem::Direction dir) ;
void removeVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ;
void markDistantChatAsClosed(const RsGxsId &gxs_id) ;
void startClientDistantChatConnection(const RsGxsId &to_gxs_id,const RsGxsId& from_gxs_id) ;
void locked_restartDHSession(const RsPeerId &virtual_peer_id, const RsGxsId &own_gxs_id) ;
//bool getHashFromVirtualPeerId(const TurtleVirtualPeerId& pid,RsFileHash& hash) ;
static TurtleFileHash hashFromGxsId(const RsGxsId& destination) ;
static RsGxsId gxsIdFromHash(const TurtleFileHash& sum) ;
void handleRecvDHPublicKey(RsChatDHPublicKeyItem *item) ;
bool locked_sendDHPublicKey(const DH *dh, const RsGxsId &own_gxs_id, const RsPeerId &virtual_peer_id) ;
bool locked_initDHSessionKey(DH *&dh);
DistantChatPeerId virtualPeerIdFromHash(const TurtleFileHash& hash ) ; // ... and to a hash for p3turtle
// Utility functions
void sendTurtleData(RsChatItem *) ;
void sendEncryptedTurtleData(const uint8_t *buff,uint32_t rssize,const RsGxsId &gxs_id) ;
bool handleEncryptedData(const uint8_t *data_bytes,uint32_t data_size,const TurtleFileHash& hash,const RsPeerId& virtual_peer_id) ;
static TurtleFileHash hashFromVirtualPeerId(const DistantChatPeerId& peerId) ; // converts IDs so that we can talk to RsPeerId from outside
p3turtle *mTurtle ;
RsGixs *mGixs ;
// Utility functions.
void markDistantChatAsClosed(const DistantChatPeerId& dcpid) ;
RsGxsTunnelService *mGxsTunnels ;
RsMutex mDistantChatMtx ;
};

View file

@ -34,6 +34,7 @@
#include "pqi/p3historymgr.h"
#include "retroshare/rspeers.h"
#include "retroshare/rsiface.h"
#include "retroshare/rsreputations.h"
#include "retroshare/rsidentity.h"
#include "rsserver/p3face.h"
#include "gxs/rsgixs.h"
@ -168,13 +169,27 @@ bool DistributedChatService::handleRecvChatLobbyMsgItem(RsChatMsgItem *ci)
{
RsIdentityDetails details;
if(!rsIdentity->getIdDetails(cli->signature.keyId,details) || !details.mPgpKnown)
if(!rsIdentity->getIdDetails(cli->signature.keyId,details))
{
#ifdef DEBUG_CHAT_LOBBIES
std::cerr << "(WW) cannot get ID " << cli->signature.keyId << " for checking signature of lobby item." << std::endl;
#endif
return false;
}
if(!(details.mFlags & RS_IDENTITY_FLAGS_PGP_LINKED))
{
std::cerr << "(WW) Received a lobby msg/item that is not PGP-authed (id=" << cli->signature.keyId << "), whereas the lobby flags require it. Rejecting!" << std::endl;
return false ;
}
}
if(rsReputations->isIdentityBanned(cli->signature.keyId))
{
std::cerr << "(WW) Received lobby msg/item from banned identity " << cli->signature.keyId << ". Dropping it." << std::endl;
return false ;
}
if(!bounceLobbyObject(cli,cli->PeerId())) // forwards the message to friends, keeps track of subscribers, etc.
return false;
@ -665,13 +680,26 @@ void DistributedChatService::handleRecvChatLobbyEventItem(RsChatLobbyEventItem *
{
RsIdentityDetails details;
if(!rsIdentity->getIdDetails(item->signature.keyId,details) || !details.mPgpKnown)
if(!rsIdentity->getIdDetails(item->signature.keyId,details))
{
#ifdef DEBUG_CHAT_LOBBIES
std::cerr << "(WW) cannot get ID " << item->signature.keyId << " for checking signature of lobby item." << std::endl;
#endif
return ;
}
if(!(details.mFlags & RS_IDENTITY_FLAGS_PGP_LINKED))
{
std::cerr << "(WW) Received a lobby msg/item that is not PGP-authed (ID=" << item->signature.keyId << "), whereas the lobby flags require it. Rejecting!" << std::endl;
return ;
}
}
if(rsReputations->isIdentityBanned(item->signature.keyId))
{
std::cerr << "(WW) Received lobby msg/item from banned identity " << item->signature.keyId << ". Dropping it." << std::endl;
return ;
}
addTimeShiftStatistics((int)now - (int)item->sendTime) ;
if(now+100 > (time_t) item->sendTime + MAX_KEEP_MSG_RECORD) // the message is older than the max cache keep minus 100 seconds ! It's too old, and is going to make an echo!
@ -1260,7 +1288,7 @@ void DistributedChatService::handleRecvLobbyInvite(RsChatLobbyInviteItem *item)
else
std::cerr << " : Match!" << std::endl;
std::cerr << " Addign new friend " << item->PeerId() << " to lobby." << std::endl;
std::cerr << " Adding new friend " << item->PeerId() << " to lobby." << std::endl;
#endif
it->second.participating_friends.insert(item->PeerId()) ;
@ -1699,7 +1727,7 @@ bool DistributedChatService::setIdentityForChatLobby(const ChatLobbyId& lobby_id
// Only send a nickname change event if the two Identities are not anonymous
if(rsIdentity->getIdDetails(nick,det1) && rsIdentity->getIdDetails(it->second.gxs_id,det2) && det1.mPgpLinked && det2.mPgpLinked)
if(rsIdentity->getIdDetails(nick,det1) && rsIdentity->getIdDetails(it->second.gxs_id,det2) && (det1.mFlags & det2.mFlags & RS_IDENTITY_FLAGS_PGP_LINKED))
sendLobbyStatusPeerChangedNickname(lobby_id, nick.toStdString()) ;
}
@ -1870,24 +1898,23 @@ void DistributedChatService::addToSaveList(std::list<RsItem*>& list) const
bool DistributedChatService::processLoadListItem(const RsItem *item)
{
const RsConfigKeyValueSet *vitem = NULL ;
_default_identity.clear() ;
if(NULL != (vitem = dynamic_cast<const RsConfigKeyValueSet*>(item)))
for(std::list<RsTlvKeyValue>::const_iterator kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); ++kit)
if(kit->key == "DEFAULT_IDENTITY")
{
{
#ifdef DEBUG_CHAT_LOBBIES
std::cerr << "Loaded config default nick name for distributed chat: " << kit->value << std::endl ;
std::cerr << "Loaded config default nick name for distributed chat: " << kit->value << std::endl ;
#endif
if (!kit->value.empty())
{
_default_identity = RsGxsId(kit->value) ;
if(_default_identity.isNull())
std::cerr << "ERROR: default identity is malformed." << std::endl;
}
if (!kit->value.empty())
{
_default_identity = RsGxsId(kit->value) ;
if(_default_identity.isNull())
std::cerr << "ERROR: default identity is malformed." << std::endl;
}
return true;
}
return true;
}
const RsChatLobbyConfigItem *clci = NULL ;

View file

@ -54,7 +54,7 @@ static const uint32_t MAX_AVATAR_JPEG_SIZE = 32767; // Maximum size
// Images are 96x96, which makes approx. 27000 bytes uncompressed.
p3ChatService::p3ChatService(p3ServiceControl *sc,p3IdService *pids, p3LinkMgr *lm, p3HistoryMgr *historyMgr)
:DistantChatService(pids),DistributedChatService(getServiceInfo().mServiceType,sc,historyMgr,pids), mChatMtx("p3ChatService"),mServiceCtrl(sc), mLinkMgr(lm) , mHistoryMgr(historyMgr)
: DistributedChatService(getServiceInfo().mServiceType,sc,historyMgr,pids), mChatMtx("p3ChatService"),mServiceCtrl(sc), mLinkMgr(lm) , mHistoryMgr(historyMgr)
{
_serializer = new RsChatSerialiser() ;
@ -86,7 +86,7 @@ int p3ChatService::tick()
receiveChatQueue();
DistributedChatService::flush() ;
DistantChatService::flush() ;
//DistantChatService::flush() ;
return 0;
}
@ -217,24 +217,25 @@ void p3ChatService::sendStatusString(const ChatId& id , const std::string& statu
sendLobbyStatusString(id.toLobbyId(),status_string) ;
else if(id.isBroadcast())
sendGroupChatStatusString(status_string);
else if(id.isPeerId() || id.isGxsId())
{
RsChatStatusItem *cs = new RsChatStatusItem ;
else if(id.isPeerId() || id.isDistantChatId())
{
RsChatStatusItem *cs = new RsChatStatusItem ;
cs->status_string = status_string ;
cs->flags = RS_CHAT_FLAG_PRIVATE ;
RsPeerId vpid;
if(id.isGxsId())
vpid = RsPeerId(id.toGxsId());
else
vpid = id.toPeerId();
cs->PeerId(vpid);
cs->status_string = status_string ;
cs->flags = RS_CHAT_FLAG_PRIVATE ;
RsPeerId vpid;
if(id.isDistantChatId())
vpid = RsPeerId(id.toDistantChatId());
else
vpid = id.toPeerId();
cs->PeerId(vpid);
#ifdef CHAT_DEBUG
std::cerr << "sending chat status packet:" << std::endl ;
cs->print(std::cerr) ;
std::cerr << "sending chat status packet:" << std::endl ;
cs->print(std::cerr) ;
#endif
sendChatItem(cs);
sendChatItem(cs);
}
else
{
@ -284,12 +285,14 @@ void p3ChatService::checkSizeAndSendMessage(RsChatMsgItem *msg)
bool p3ChatService::isOnline(const RsPeerId& pid)
{
// check if the id is a tunnel id or a peer id.
uint32_t status ;
if(getDistantChatStatus(RsGxsId(pid),status))
return status == RS_DISTANT_CHAT_STATUS_CAN_TALK ;
// check if the id is a tunnel id or a peer id.
DistantChatPeerInfo dcpinfo;
if(getDistantChatStatus(DistantChatPeerId(pid),dcpinfo))
return dcpinfo.status == RS_DISTANT_CHAT_STATUS_CAN_TALK ;
else
return mServiceCtrl->isPeerConnected(getServiceInfo().mServiceType, pid);
return mServiceCtrl->isPeerConnected(getServiceInfo().mServiceType, pid);
}
bool p3ChatService::sendChat(ChatId destination, std::string msg)
@ -301,7 +304,7 @@ bool p3ChatService::sendChat(ChatId destination, std::string msg)
sendPublicChat(msg);
return true;
}
else if(destination.isPeerId()==false && destination.isGxsId()==false)
else if(destination.isPeerId()==false && destination.isDistantChatId()==false)
{
std::cerr << "p3ChatService::sendChat() Error: chat id type not handled. Is it empty?" << std::endl;
return false;
@ -313,8 +316,8 @@ bool p3ChatService::sendChat(ChatId destination, std::string msg)
#endif
RsPeerId vpid;
if(destination.isGxsId())
vpid = RsPeerId(destination.toGxsId()); // convert to virtual peer id
if(destination.isDistantChatId())
vpid = RsPeerId(destination.toDistantChatId()); // convert to virtual peer id
else
vpid = destination.toPeerId();
@ -378,7 +381,12 @@ bool p3ChatService::sendChat(ChatId destination, std::string msg)
#endif
RsServer::notify()->notifyChatMessage(message);
mHistoryMgr->addMessage(message);
// cyril: history is temporarily diabled for distant chat, since we need to store the full tunnel ID, but then
// at loading time, the ID is not known so that chat window shows 00000000 as a peer.
if(!message.chat_id.isDistantChatId())
mHistoryMgr->addMessage(message);
checkSizeAndSendMessage(ci);
@ -497,11 +505,11 @@ void p3ChatService::handleIncomingItem(RsItem *item)
return ; // don't delete! It's handled by handleRecvChatMsgItem in some specific cases only.
}
if(DistantChatService::handleRecvItem(dynamic_cast<RsChatItem*>(item)))
{
delete item ;
return ;
}
// if(DistantChatService::handleRecvItem(dynamic_cast<RsChatItem*>(item)))
// {
// delete item ;
// return ;
// }
if(DistributedChatService::handleRecvItem(dynamic_cast<RsChatItem*>(item)))
{
@ -749,7 +757,13 @@ bool p3ChatService::handleRecvChatMsgItem(RsChatMsgItem *ci)
cm.incoming = true;
cm.online = true;
RsServer::notify()->notifyChatMessage(cm);
mHistoryMgr->addMessage(cm);
// cyril: history is temporarily diabled for distant chat, since we need to store the full tunnel ID, but then
// at loading time, the ID is not known so that chat window shows 00000000 as a peer.
if(!cm.chat_id.isDistantChatId())
mHistoryMgr->addMessage(cm);
return true ;
}
@ -766,7 +780,7 @@ void p3ChatService::handleRecvChatStatusItem(RsChatStatusItem *cs)
std::cerr << "Received status string \"" << cs->status_string << "\"" << std::endl ;
#endif
uint32_t status;
DistantChatPeerInfo dcpinfo;
if(cs->flags & RS_CHAT_FLAG_REQUEST_CUSTOM_STATE) // no state here just a request.
sendCustomState(cs->PeerId()) ;
@ -782,9 +796,9 @@ void p3ChatService::handleRecvChatStatusItem(RsChatStatusItem *cs)
#endif
sendCustomStateRequest(cs->PeerId()) ;
}
else if(DistantChatService::getDistantChatStatus(RsGxsId(cs->PeerId()), status))
else if(DistantChatService::getDistantChatStatus(DistantChatPeerId(cs->PeerId()), dcpinfo))
{
RsServer::notify()->notifyChatStatus(ChatId(RsGxsId(cs->PeerId())), cs->status_string) ;
RsServer::notify()->notifyChatStatus(ChatId(DistantChatPeerId(cs->PeerId())), cs->status_string) ;
}
else if(cs->flags & RS_CHAT_FLAG_PRIVATE)
{
@ -816,9 +830,9 @@ void p3ChatService::initChatMessage(RsChatMsgItem *c, ChatMessage &m)
return;
}
uint32_t status;
if(DistantChatService::getDistantChatStatus(RsGxsId(c->PeerId()), status))
m.chat_id = ChatId(RsGxsId(c->PeerId()));
DistantChatPeerInfo dcpinfo;
if(DistantChatService::getDistantChatStatus(DistantChatPeerId(c->PeerId()), dcpinfo))
m.chat_id = ChatId(DistantChatPeerId(c->PeerId()));
if (c -> chatFlags & RS_CHAT_FLAG_PRIVATE)
m.chatflags |= RS_CHAT_PRIVATE;
@ -1178,16 +1192,14 @@ bool p3ChatService::loadList(std::list<RsItem*>& load)
continue;
}
if(DistributedChatService::processLoadListItem(*it))
{
delete *it;
continue;
}
DistributedChatService::processLoadListItem(*it) ;
DistantChatService::processLoadListItem(*it) ;
// delete unknown items
delete *it;
}
load.clear() ;
return true;
}
@ -1224,6 +1236,7 @@ bool p3ChatService::saveList(bool& cleanup, std::list<RsItem*>& list)
}
DistributedChatService::addToSaveList(list) ;
DistantChatService::addToSaveList(list) ;
return true;
}

View file

@ -53,186 +53,186 @@ typedef RsPeerId ChatLobbyVirtualPeerId ;
*/
class p3ChatService: public p3Service, public DistantChatService, public DistributedChatService, public p3Config, public pqiServiceMonitor
{
public:
p3ChatService(p3ServiceControl *cs, p3IdService *pids,p3LinkMgr *cm, p3HistoryMgr *historyMgr);
public:
p3ChatService(p3ServiceControl *cs, p3IdService *pids,p3LinkMgr *cm, p3HistoryMgr *historyMgr);
virtual RsServiceInfo getServiceInfo();
virtual RsServiceInfo getServiceInfo();
/***** overloaded from p3Service *****/
/*!
/***** overloaded from p3Service *****/
/*!
* This retrieves all chat msg items and also (important!)
* processes chat-status items that are in service item queue. chat msg item requests are also processed and not returned
* (important! also) notifications sent to notify base on receipt avatar, immediate status and custom status
* : notifyCustomState, notifyChatStatus, notifyPeerHasNewAvatar
* @see NotifyBase
*/
virtual int tick();
virtual int tick();
/*************** pqiMonitor callback ***********************/
virtual void statusChange(const std::list<pqiServicePeer> &plist);
/*************** pqiMonitor callback ***********************/
virtual void statusChange(const std::list<pqiServicePeer> &plist);
/*!
/*!
* public chat sent to all peers
*/
void sendPublicChat(const std::string &msg);
void sendPublicChat(const std::string &msg);
/********* RsMsgs ***********/
/*!
* Send a chat message.
* @param destination where to send the chat message
* @param msg the message
* @see ChatId
*/
bool sendChat(ChatId destination, std::string msg);
/********* RsMsgs ***********/
/*!
* Send a chat message.
* @param destination where to send the chat message
* @param msg the message
* @see ChatId
*/
bool sendChat(ChatId destination, std::string msg);
/*!
/*!
* chat is sent to specifc peer
* @param id peer to send chat msg to
*/
bool sendPrivateChat(const RsPeerId &id, const std::string &msg);
bool sendPrivateChat(const RsPeerId &id, const std::string &msg);
/*!
/*!
* can be used to send 'immediate' status msgs, these status updates are meant for immediate use by peer (not saved by rs)
* e.g currently used to update user when a peer 'is typing' during a chat
*/
void sendStatusString(const ChatId& peer_id,const std::string& status_str) ;
void sendStatusString(const ChatId& peer_id,const std::string& status_str) ;
/*!
/*!
* send to all peers online
*@see sendStatusString()
*/
void sendGroupChatStatusString(const std::string& status_str) ;
void sendGroupChatStatusString(const std::string& status_str) ;
/*!
/*!
* this retrieves custom status for a peers, generate a requests to the peer
* @param peer_id the id of the peer you want status string for
*/
std::string getCustomStateString(const RsPeerId& peer_id) ;
std::string getCustomStateString(const RsPeerId& peer_id) ;
/*!
/*!
* sets the client's custom status, generates 'status available' item sent to all online peers
*/
void setOwnCustomStateString(const std::string&) ;
void setOwnCustomStateString(const std::string&) ;
/*!
/*!
* @return client's custom string
*/
std::string getOwnCustomStateString() ;
std::string getOwnCustomStateString() ;
/*! gets the peer's avatar in jpeg format, if available. Null otherwise. Also asks the peer to send
/*! gets the peer's avatar in jpeg format, if available. Null otherwise. Also asks the peer to send
* its avatar, if not already available. Creates a new unsigned char array. It's the caller's
* responsibility to delete this ones used.
*/
void getAvatarJpegData(const RsPeerId& peer_id,unsigned char *& data,int& size) ;
void getAvatarJpegData(const RsPeerId& peer_id,unsigned char *& data,int& size) ;
/*!
/*!
* Sets the avatar data and size for client's account
* @param data is copied, so should be destroyed by the caller
*/
void setOwnAvatarJpegData(const unsigned char *data,int size) ;
void setOwnAvatarJpegData(const unsigned char *data,int size) ;
/*!
/*!
* Gets the avatar data for clients account
* data is in jpeg format
*/
void getOwnAvatarJpegData(unsigned char *& data,int& size) ;
void getOwnAvatarJpegData(unsigned char *& data,int& size) ;
/*!
/*!
* Return the max message size for security forwarding
* @param type RS_CHAT_TYPE_...
* return 0 unlimited
*/
static uint32_t getMaxMessageSecuritySize(int type);
static uint32_t getMaxMessageSecuritySize(int type);
/*!
/*!
* Checks message security, especially remove billion laughs attacks
*/
static bool checkForMessageSecurity(RsChatMsgItem *) ;
static bool checkForMessageSecurity(RsChatMsgItem *) ;
/*!
/*!
* @param clear private chat queue
*/
bool clearPrivateChatQueue(bool incoming, const RsPeerId &id);
bool clearPrivateChatQueue(bool incoming, const RsPeerId &id);
protected:
/************* from p3Config *******************/
virtual RsSerialiser *setupSerialiser() ;
protected:
/************* from p3Config *******************/
virtual RsSerialiser *setupSerialiser() ;
/*!
/*!
* chat msg items and custom status are saved
*/
virtual bool saveList(bool& cleanup, std::list<RsItem*>&) ;
virtual void saveDone();
virtual bool loadList(std::list<RsItem*>& load) ;
virtual bool saveList(bool& cleanup, std::list<RsItem*>&) ;
virtual void saveDone();
virtual bool loadList(std::list<RsItem*>& load) ;
// accepts virtual peer id
bool isOnline(const RsPeerId &pid) ;
// accepts virtual peer id
bool isOnline(const RsPeerId &pid) ;
/// This is to be used by subclasses/parents to call IndicateConfigChanged()
virtual void triggerConfigSave() { IndicateConfigChanged() ; }
/// Same, for storing messages in incoming list
virtual void locked_storeIncomingMsg(RsChatMsgItem *) ;
/// This is to be used by subclasses/parents to call IndicateConfigChanged()
virtual void triggerConfigSave() { IndicateConfigChanged() ; }
/// Same, for storing messages in incoming list
virtual void locked_storeIncomingMsg(RsChatMsgItem *) ;
private:
RsMutex mChatMtx;
private:
RsMutex mChatMtx;
class AvatarInfo ;
class StateStringInfo ;
class AvatarInfo ;
class StateStringInfo ;
// Receive chat queue
void receiveChatQueue();
void handleIncomingItem(RsItem *); // called by the former, and turtle handler for incoming encrypted items
// Receive chat queue
void receiveChatQueue();
void handleIncomingItem(RsItem *); // called by the former, and turtle handler for incoming encrypted items
virtual void sendChatItem(RsChatItem *) ;
virtual void sendChatItem(RsChatItem *) ;
void initChatMessage(RsChatMsgItem *c, ChatMessage& msg);
void initChatMessage(RsChatMsgItem *c, ChatMessage& msg);
/// Send avatar info to peer in jpeg format.
void sendAvatarJpegData(const RsPeerId& peer_id) ;
/// Send avatar info to peer in jpeg format.
void sendAvatarJpegData(const RsPeerId& peer_id) ;
/// Send custom state info to peer
void sendCustomState(const RsPeerId& peer_id);
/// Send custom state info to peer
void sendCustomState(const RsPeerId& peer_id);
/// Receive the avatar in a chat item, with RS_CHAT_RECEIVE_AVATAR flag.
void receiveAvatarJpegData(RsChatAvatarItem *ci) ; // new method
void receiveStateString(const RsPeerId& id,const std::string& s) ;
/// Receive the avatar in a chat item, with RS_CHAT_RECEIVE_AVATAR flag.
void receiveAvatarJpegData(RsChatAvatarItem *ci) ; // new method
void receiveStateString(const RsPeerId& id,const std::string& s) ;
/// methods for handling various Chat items.
bool handleRecvChatMsgItem(RsChatMsgItem *item) ; // returns false if the item should be deleted.
void handleRecvChatStatusItem(RsChatStatusItem *item) ;
void handleRecvChatAvatarItem(RsChatAvatarItem *item) ;
/// methods for handling various Chat items.
bool handleRecvChatMsgItem(RsChatMsgItem *item) ; // returns false if the item should be deleted.
void handleRecvChatStatusItem(RsChatStatusItem *item) ;
void handleRecvChatAvatarItem(RsChatAvatarItem *item) ;
/// Sends a request for an avatar to the peer of given id
void sendAvatarRequest(const RsPeerId& peer_id) ;
/// Sends a request for an avatar to the peer of given id
void sendAvatarRequest(const RsPeerId& peer_id) ;
/// Send a request for custom status string
void sendCustomStateRequest(const RsPeerId& peer_id);
/// Send a request for custom status string
void sendCustomStateRequest(const RsPeerId& peer_id);
/// called as a proxy to sendItem(). Possibly splits item into multiple items of size lower than the maximum item size.
//void checkSizeAndSendMessage(RsChatLobbyMsgItem *item) ;
void checkSizeAndSendMessage(RsChatMsgItem *item) ; // keep for compatibility for a few weeks.
/// called as a proxy to sendItem(). Possibly splits item into multiple items of size lower than the maximum item size.
//void checkSizeAndSendMessage(RsChatLobbyMsgItem *item) ;
void checkSizeAndSendMessage(RsChatMsgItem *item) ; // keep for compatibility for a few weeks.
/// Called when a RsChatMsgItem is received. The item may be collapsed with any waiting partial chat item from the same peer.
bool locked_checkAndRebuildPartialMessage(RsChatMsgItem *) ;
/// Called when a RsChatMsgItem is received. The item may be collapsed with any waiting partial chat item from the same peer.
bool locked_checkAndRebuildPartialMessage(RsChatMsgItem *) ;
RsChatAvatarItem *makeOwnAvatarItem() ;
RsChatStatusItem *makeOwnCustomStateStringItem() ;
RsChatAvatarItem *makeOwnAvatarItem() ;
RsChatStatusItem *makeOwnCustomStateStringItem() ;
p3ServiceControl *mServiceCtrl;
p3LinkMgr *mLinkMgr;
p3HistoryMgr *mHistoryMgr;
p3ServiceControl *mServiceCtrl;
p3LinkMgr *mLinkMgr;
p3HistoryMgr *mHistoryMgr;
std::list<RsChatMsgItem *> privateOutgoingList; // messages waiting to be send when peer comes online
std::list<RsChatMsgItem *> privateOutgoingList; // messages waiting to be send when peer comes online
AvatarInfo *_own_avatar ;
std::map<RsPeerId,AvatarInfo *> _avatars ;
std::map<RsPeerId,RsChatMsgItem *> _pendingPartialMessages ;
AvatarInfo *_own_avatar ;
std::map<RsPeerId,AvatarInfo *> _avatars ;
std::map<RsPeerId,RsChatMsgItem *> _pendingPartialMessages ;
std::string _custom_status_string ;
std::map<RsPeerId,StateStringInfo> _state_strings ;
std::string _custom_status_string ;
std::map<RsPeerId,StateStringInfo> _state_strings ;
RsChatSerialiser *_serializer ;
RsChatSerialiser *_serializer ;
};
class p3ChatService::StateStringInfo

View file

@ -51,18 +51,6 @@ std::ostream& RsChatMsgItem::print(std::ostream &out, uint16_t indent)
printRsItemEnd(out, "RsChatMsgItem", indent);
return out;
}
std::ostream& RsChatDHPublicKeyItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsChatDHPublicKeyItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << " Signature Key ID: " << signature.keyId << std::endl ;
out << " Public Key ID: " << gxs_key.keyId << std::endl ;
printRsItemEnd(out, "RsChatMsgItem", indent);
return out;
}
std::ostream& RsChatLobbyListItem::print(std::ostream &out, uint16_t indent)
{
@ -277,7 +265,6 @@ RsItem *RsChatSerialiser::deserialise(void *data, uint32_t *pktsize)
case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST_REQUEST: return new RsChatLobbyListRequestItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST: return new RsChatLobbyListItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_CONFIG: return new RsChatLobbyConfigItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_DISTANT_CHAT_DH_PUBLIC_KEY: return new RsChatDHPublicKeyItem(data,*pktsize) ;
default:
std::cerr << "Unknown packet type in chat!" << std::endl ;
return NULL ;
@ -437,17 +424,6 @@ uint32_t RsChatLobbyConfigItem::serial_size()
return s;
}
uint32_t RsChatDHPublicKeyItem::serial_size()
{
uint32_t s = 8 ; // header
s += 4 ; // BN size
s += BN_num_bytes(public_key) ; // public_key
s += signature.TlvSize() ; // signature
s += gxs_key.TlvSize() ; // gxs_key
return s ;
}
/*************************************************************************/
RsChatAvatarItem::~RsChatAvatarItem()
@ -459,41 +435,6 @@ RsChatAvatarItem::~RsChatAvatarItem()
}
}
bool RsChatDHPublicKeyItem::serialise(void *data,uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
/* skip the header */
offset += 8;
uint32_t s = BN_num_bytes(public_key) ;
ok &= setRawUInt32(data, tlvsize, &offset, s);
BN_bn2bin(public_key,&((unsigned char *)data)[offset]) ;
offset += s ;
ok &= signature.SetTlv(data, tlvsize, &offset);
ok &= gxs_key.SetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsChatDHPublicKeyItem::serialiseItem() Size Error! offset=" << offset << ", tlvsize=" << tlvsize << std::endl;
}
return ok ;
}
/* serialise the data to the buffer */
bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize)
{
@ -995,28 +936,6 @@ bool RsChatLobbyConfigItem::serialise(void *data, uint32_t& pktsize)
return ok;
}
RsChatDHPublicKeyItem::RsChatDHPublicKeyItem(void *data,uint32_t /*size*/)
: RsChatItem(RS_PKT_SUBTYPE_DISTANT_CHAT_DH_PUBLIC_KEY)
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
uint32_t s=0 ;
/* get mandatory parts first */
ok &= getRawUInt32(data, rssize, &offset, &s);
public_key = BN_bin2bn(&((unsigned char *)data)[offset],s,NULL) ;
offset += s ;
ok &= signature.GetTlv(data, rssize, &offset) ;
ok &= gxs_key.GetTlv(data, rssize, &offset) ;
if (offset != rssize)
std::cerr << "RsChatDHPublicKeyItem::() Size error while deserializing." << std::endl ;
if (!ok)
std::cerr << "RsChatDHPublicKeyItem::() Unknown error while deserializing." << std::endl ;
}
RsChatMsgItem::RsChatMsgItem(void *data,uint32_t /*size*/,uint8_t subtype)
: RsChatItem(subtype)
{

View file

@ -48,6 +48,7 @@ const uint32_t RS_CHAT_FLAG_LOBBY = 0x0200;
const uint32_t RS_CHAT_FLAG_CLOSING_DISTANT_CONNECTION = 0x0400;
const uint32_t RS_CHAT_FLAG_ACK_DISTANT_CONNECTION = 0x0800;
const uint32_t RS_CHAT_FLAG_KEEP_ALIVE = 0x1000;
const uint32_t RS_CHAT_FLAG_CONNEXION_REFUSED = 0x2000;
const uint32_t RS_CHATMSG_CONFIGFLAG_INCOMING = 0x0001;

View file

@ -1052,6 +1052,7 @@ bool CacheStrapper::loadList(std::list<RsItem *>& load)
delete (*it);
}
}
load.clear() ;
/* assemble a list of dirs to clean (union of cache dirs) */
std::list<std::string> cacheDirs;

View file

@ -175,6 +175,7 @@ HashCache::HashCache(const std::string& path)
if(!f->good())
{
delete f ;
std::cerr << "Unencrypted file cache not present either." << std::endl;
return ;
}

View file

@ -33,7 +33,11 @@
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#ifdef __MAC_10_10
#include <unordered_set>
#else
#include <tr1/unordered_set>
#endif
#include <iomanip>
#include <fstream>
#include <sys/stat.h>
@ -54,8 +58,11 @@ static const char FILE_CACHE_SEPARATOR_CHAR = '|' ;
****/
static RsMutex FIndexPtrMtx("FIndexPtrMtx") ;
#ifdef __MAC_10_10
std::unordered_set<void*> FileIndex::_pointers ;
#else
std::tr1::unordered_set<void*> FileIndex::_pointers ;
#endif
void FileIndex::registerEntry(void*p)
{
RsStackMutex m(FIndexPtrMtx) ;
@ -780,7 +787,7 @@ int FileIndex::loadIndex(const std::string& filename, const RsFileHash& expected
fclose(file);
return 0;
}
int bytesread = 0 ;
uint64_t bytesread = 0 ;
if(size != (bytesread = fread(compressed_data,1,size,file)))
{
std::cerr << "FileIndex::loadIndex(): can't read " << size << " bytes from file " << filename << ". Only " << bytesread << " actually read." << std::endl;
@ -1089,7 +1096,7 @@ int FileIndex::saveIndex(const std::string& filename, RsFileHash &fileHash, uint
std::cerr << "FileIndex::saveIndex error opening file for writting: " << filename << ". Giving up." << std::endl;
return 0;
}
int outwritten ;
uint32_t outwritten ;
if(compressed_data_size != (outwritten=fwrite(compressed_data,1,compressed_data_size,file)))
{
@ -1370,7 +1377,7 @@ void *FileIndex::findRef(const std::string& fpath) const
std::cerr << "FileIndex::updateFileEntry() NULL parent";
std::cerr << std::endl;
//#endif
return false;
return NULL;
}
std::cerr << "Found parent directory: " << std::endl;
std::cerr << " parent.name = " << parent->name << std::endl;

View file

@ -27,7 +27,11 @@
#include <string>
#include <map>
#include <set>
#if __MAC_10_10
#include <unordered_set>
#else
#include <tr1/unordered_set>
#endif
#include <list>
#include <vector>
#include <stdint.h>
@ -246,7 +250,11 @@ class FileIndex
PersonEntry *root;
#ifdef __MAC_10_10
static std::unordered_set<void*> _pointers ;
#else
static std::tr1::unordered_set<void*> _pointers ;
#endif
static void registerEntry(void*p) ;
static void unregisterEntry(void*p) ;
static bool isValid(void*p) ;

View file

@ -279,6 +279,11 @@ bool p3BitDht::loadList(std::list<RsItem *>& load)
/* error */
std::cerr << "p3BitDht::loadList() Error only expecting 1 item";
std::cerr << std::endl;
for(std::list<RsItem*>::iterator it=load.begin();it!=load.end();++it)
delete *it ;
load.clear() ;
return false;
}
RsItem *item = load.front();
@ -290,6 +295,7 @@ bool p3BitDht::loadList(std::list<RsItem *>& load)
/* error */
std::cerr << "p3BitDht::loadList() Error expecting item = config";
std::cerr << std::endl;
delete item ;
return false;
}
@ -393,6 +399,7 @@ bool p3BitDht::loadList(std::list<RsItem *>& load)
setRelayMode(mode);
}
load.clear() ;
return true;
}

View file

@ -96,7 +96,7 @@ void ChunkMap::setAvailabilityMap(const CompressedChunkMap& map)
// do some sanity check
//
if( (((int)_map.size()-1)>>5) >= map._map.size() )
if( (((int)_map.size()-1)>>5) >= (int)map._map.size() )
{
std::cerr << "ChunkMap::setPeerAvailabilityMap: Compressed chunkmap received is too small or corrupted." << std::endl;
return ;

View file

@ -2224,6 +2224,7 @@ bool ftController::loadList(std::list<RsItem *>& load)
/* cleanup */
delete (*it);
}
load.clear() ;
return true;
}

View file

@ -328,6 +328,8 @@ bool ftFiMonitor::loadList(std::list<RsItem *>& load)
if(sscanf(mit->second.c_str(),"%d",&t) == 1)
setWatchPeriod(t);
}
delete *it ;
continue ;
}
RsFileConfigItem *fi = dynamic_cast<RsFileConfigItem *>(*it);
@ -348,10 +350,13 @@ bool ftFiMonitor::loadList(std::list<RsItem *>& load)
info.shareflags &= ~DIR_FLAGS_NETWORK_WIDE_GROUPS ; // disabling this flag for know, for consistency reasons
dirList.push_back(info) ;
delete *it ;
}
/* set directories */
setSharedDirectories(dirList);
load.clear() ;
return true;
}

View file

@ -484,6 +484,7 @@ bool ftExtraList::loadList(std::list<RsItem *>& load)
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
}
load.clear() ;
return true;
}

View file

@ -42,37 +42,41 @@ static const uint32_t GROUTER_CLIENT_SERVICE_DATA_STATUS_FAILED = 0x0002 ; //
class GRouterClientService
{
public:
// This method is called by the turtle router to send data that comes out of a turtle tunnel.
// The turtle router stays responsible for the memory management of data. Most of the time the
// data chunk is a serialized item to be de-serialized by the client service.
//
// Parameters:
// item : global router item. Handled by the client service.
// destination_key : key that is associated with this item. Can be useful for the client.
//
// GRouter stays owner of the item, so the client should not delete it!
//
virtual void receiveGRouterData(const RsGxsId& destination_key,const RsGxsId& /*signing_key*/, GRouterServiceId &/*client_id*/, uint8_t */*data*/, uint32_t /*data_size*/)
{
std::cerr << "!!!!!! Received Data from global router, but the client service is not handling it !!!!!!!!!!" << std::endl ;
std::cerr << " destination key_id = " << destination_key.toStdString() << std::endl;
}
public:
// This method is called by the turtle router to send data that comes out of a turtle tunnel.
// The turtle router stays responsible for the memory management of data. Most of the time the
// data chunk is a serialized item to be de-serialized by the client service.
//
// Parameters:
// item : global router item. Handled by the client service.
// destination_key : key that is associated with this item. Can be useful for the client.
//
// GRouter stays owner of the item, so the client should not delete it!
//
virtual void receiveGRouterData(const RsGxsId& destination_key,const RsGxsId& /*signing_key*/, GRouterServiceId &/*client_id*/, uint8_t */*data*/, uint32_t /*data_size*/)
{
std::cerr << "!!!!!! Received Data from global router, but the client service is not handling it !!!!!!!!!!" << std::endl ;
std::cerr << " destination key_id = " << destination_key.toStdString() << std::endl;
}
// This method is called by the global router when a message has been received, or cannot be sent, etc.
//
virtual void notifyDataStatus(const GRouterMsgPropagationId& received_id,uint32_t data_status)
{
std::cerr << "!!!!!! Received Data status from global router, but the client service is not handling it !!!!!!!!!!" << std::endl ;
std::cerr << " message ID = " << received_id << std::endl;
std::cerr << " data status = " << data_status << std::endl;
}
// This method is called by the global router when a message has been received, or cannot be sent, etc.
//
virtual void notifyDataStatus(const GRouterMsgPropagationId& received_id,uint32_t data_status)
{
std::cerr << "!!!!!! Received Data status from global router, but the client service is not handling it !!!!!!!!!!" << std::endl ;
std::cerr << " message ID = " << received_id << std::endl;
std::cerr << " data status = " << data_status << std::endl;
}
// This function is mandatory. It should do two things:
// 1 - keep a pointer to the global router, so as to be able to send data (e.g. copy pt into a local variable)
// 2 - call pt->registerTunnelService(this), so that the TR knows that service and can send back information to it.
//
virtual void connectToGlobalRouter(p3GRouter *pt) = 0 ;
// This function is mandatory. It should do two things:
// 1 - keep a pointer to the global router, so as to be able to send data (e.g. copy pt into a local variable)
// 2 - call pt->registerTunnelService(this), so that the TR knows that service and can send back information to it.
//
virtual void connectToGlobalRouter(p3GRouter *pt) = 0 ;
// should be derived to determine wether the client accepts data from this peer or not. If not, the data is dropped.
virtual bool acceptDataFromPeer(const RsGxsId& gxs_id) =0;
};

View file

@ -45,17 +45,18 @@ RsItem *RsGRouterSerialiser::deserialise(void *data, uint32_t *pktsize)
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_GROUTER_DATA: return deserialise_RsGRouterGenericDataItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK: return deserialise_RsGRouterTransactionChunkItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN: return deserialise_RsGRouterTransactionAcknItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT: return deserialise_RsGRouterSignedReceiptItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES: return deserialise_RsGRouterMatrixCluesItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST: return deserialise_RsGRouterMatrixFriendListItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO: return deserialise_RsGRouterRoutingInfoItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_DATA: return deserialise_RsGRouterGenericDataItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK: return deserialise_RsGRouterTransactionChunkItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN: return deserialise_RsGRouterTransactionAcknItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT: return deserialise_RsGRouterSignedReceiptItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES: return deserialise_RsGRouterMatrixCluesItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_MATRIX_TRACK: return deserialise_RsGRouterMatrixTrackItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST: return deserialise_RsGRouterMatrixFriendListItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO: return deserialise_RsGRouterRoutingInfoItem (data, *pktsize);
default:
std::cerr << "RsGRouterSerialiser::deserialise(): Could not de-serialise item. SubPacket id = " << std::hex << getRsItemSubType(rstype) << " id = " << rstype << std::dec << std::endl;
return NULL;
default:
std::cerr << "RsGRouterSerialiser::deserialise(): Could not de-serialise item. SubPacket id = " << std::hex << getRsItemSubType(rstype) << " id = " << rstype << std::dec << std::endl;
return NULL;
}
return NULL;
}
@ -269,6 +270,28 @@ RsGRouterMatrixFriendListItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixF
return item;
}
RsGRouterMatrixTrackItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixTrackItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
RsGRouterMatrixTrackItem *item = new RsGRouterMatrixTrackItem() ;
ok &= item->provider_id.deserialise(data, pktsize, offset) ;
ok &= item->message_id.deserialise(data,pktsize,offset) ;
ok &= getRawTimeT(data, pktsize, &offset, item->time_stamp) ;
if (offset != rssize || !ok)
{
std::cerr << __PRETTY_FUNCTION__ << ": error while deserialising! Item will be dropped." << std::endl;
delete item;
return NULL ;
}
return item;
}
RsGRouterMatrixCluesItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixCluesItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
@ -559,6 +582,17 @@ uint32_t RsGRouterMatrixFriendListItem::serial_size() const
return s ;
}
uint32_t RsGRouterMatrixTrackItem::serial_size() const
{
uint32_t s = 8 ; // header
s += 8 ; // time_stamp
s += RsPeerId::SIZE_IN_BYTES; // provider_id
s += RsMessageId::SIZE_IN_BYTES; // message_id
return s ;
}
uint32_t RsGRouterRoutingInfoItem::serial_size() const
{
uint32_t s = 8 ; // header
@ -637,6 +671,26 @@ bool RsGRouterMatrixCluesItem::serialise(void *data,uint32_t& size) const
return ok;
}
bool RsGRouterMatrixTrackItem::serialise(void *data,uint32_t& size) const
{
uint32_t tlvsize,offset=0;
bool ok = true;
if(!serialise_header(data,size,tlvsize,offset))
return false ;
ok &= provider_id.serialise(data, tlvsize, offset) ;
ok &= message_id.serialise(data,tlvsize,offset) ;
ok &= setRawTimeT(data, tlvsize, &offset, time_stamp) ;
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsGRouterMatrixTrackItem::serialisedata() size error! " << std::endl;
}
return ok;
}
bool FriendTrialRecord::deserialise(void *data,uint32_t& offset,uint32_t size)
{
bool ok = true ;
@ -701,6 +755,7 @@ bool RsGRouterRoutingInfoItem::serialise(void *data,uint32_t& size) const
// ------------------------------------- IO --------------------------------------- //
// -----------------------------------------------------------------------------------//
//
std::ostream& RsGRouterSignedReceiptItem::print(std::ostream& o, uint16_t)
{
o << "RsGRouterReceiptItem:" << std::endl ;
@ -746,6 +801,15 @@ std::ostream& RsGRouterRoutingInfoItem::print(std::ostream& o, uint16_t)
return o ;
}
std::ostream& RsGRouterMatrixTrackItem::print(std::ostream& o, uint16_t)
{
o << "RsGRouterMatrixTrackItem:" << std::endl ;
o << " provider_id: " << provider_id << std::endl;
o << " message_id: " << message_id << std::endl;
o << " time_stamp: " << time_stamp << std::endl;
return o ;
}
std::ostream& RsGRouterMatrixCluesItem::print(std::ostream& o, uint16_t)
{
o << "RsGRouterMatrixCluesItem:" << std::endl ;

View file

@ -33,20 +33,19 @@
#include "retroshare/rsgrouter.h"
#include "groutermatrix.h"
const uint8_t RS_PKT_SUBTYPE_GROUTER_PUBLISH_KEY = 0x01 ; // used to publish a key
const uint8_t RS_PKT_SUBTYPE_GROUTER_ACK_deprecated = 0x03 ; // don't use!
const uint8_t RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT_deprecated = 0x04 ; // don't use!
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA_deprecated = 0x05 ; // don't use!
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA_deprecated2 = 0x06 ; // don't use!
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA = 0x07 ; // used to send data to a destination (Signed by source)
const uint8_t RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT = 0x08 ; // long-distance acknowledgement of data received
const uint8_t RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK = 0x10 ; // chunk of data. Used internally.
const uint8_t RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN = 0x11 ; // acknowledge for finished transaction. Not necessary, but increases fiability.
const uint8_t RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES = 0x80 ; // item to save matrix clues
const uint8_t RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST = 0x82 ; // item to save friend lists
const uint8_t RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO = 0x93 ; //
const uint8_t RS_PKT_SUBTYPE_GROUTER_PUBLISH_KEY = 0x01 ; // used to publish a key
const uint8_t RS_PKT_SUBTYPE_GROUTER_ACK_deprecated = 0x03 ; // don't use!
const uint8_t RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT_deprecated = 0x04 ; // don't use!
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA_deprecated = 0x05 ; // don't use!
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA_deprecated2 = 0x06 ; // don't use!
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA = 0x07 ; // used to send data to a destination (Signed by source)
const uint8_t RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT = 0x08 ; // long-distance acknowledgement of data received
const uint8_t RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK = 0x10 ; // chunk of data. Used internally.
const uint8_t RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN = 0x11 ; // acknowledge for finished transaction. Not necessary, but increases fiability.
const uint8_t RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES = 0x80 ; // item to save matrix clues
const uint8_t RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST = 0x82 ; // item to save friend lists
const uint8_t RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO = 0x93 ; //
const uint8_t RS_PKT_SUBTYPE_GROUTER_MATRIX_TRACK = 0x94 ; // item to save matrix track info
const uint8_t QOS_PRIORITY_RS_GROUTER = 4 ; // relevant for items that travel through friends
@ -243,6 +242,24 @@ class RsGRouterMatrixCluesItem: public RsGRouterItem
std::list<RoutingMatrixHitEntry> clues ;
};
class RsGRouterMatrixTrackItem: public RsGRouterItem
{
public:
RsGRouterMatrixTrackItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_MATRIX_TRACK)
{ setPriorityLevel(0) ; } // this item is never sent through the network
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
// packet data
//
RsGxsMessageId message_id ;
RsPeerId provider_id ;
time_t time_stamp ;
};
class RsGRouterMatrixFriendListItem: public RsGRouterItem
{
public:
@ -317,6 +334,7 @@ private:
RsGRouterTransactionAcknItem *deserialise_RsGRouterTransactionAcknItem(void *data,uint32_t size) const ;
RsGRouterSignedReceiptItem *deserialise_RsGRouterSignedReceiptItem(void *data,uint32_t size) const ;
RsGRouterMatrixCluesItem *deserialise_RsGRouterMatrixCluesItem(void *data,uint32_t size) const ;
RsGRouterMatrixTrackItem *deserialise_RsGRouterMatrixTrackItem(void *data,uint32_t size) const ;
RsGRouterMatrixFriendListItem *deserialise_RsGRouterMatrixFriendListItem(void *data,uint32_t size) const ;
RsGRouterRoutingInfoItem *deserialise_RsGRouterRoutingInfoItem(void *data,uint32_t size) const ;
};

View file

@ -34,6 +34,48 @@ GRouterMatrix::GRouterMatrix()
_proba_need_updating = true ;
}
bool GRouterMatrix::addTrackingInfo(const RsGxsMessageId& mid,const RsPeerId& source_friend)
{
time_t now = time(NULL) ;
RoutingTrackEntry rte ;
rte.friend_id = source_friend ;
rte.time_stamp = now ;
_tracking_clues[mid] = rte ;
#ifdef ROUTING_MATRIX_DEBUG
std::cerr << "GRouterMatrix::addTrackingInfo(): Added clue mid=" << mid << ", from " << source_friend << " ID=" << source_friend << std::endl;
#endif
return true ;
}
bool GRouterMatrix::cleanUp()
{
// remove all tracking entries that have become too old.
#ifdef ROUTING_MATRIX_DEBUG
std::cerr << "GRouterMatrix::cleanup()" << std::endl;
#endif
time_t now = time(NULL) ;
for(std::map<RsGxsMessageId,RoutingTrackEntry>::iterator it(_tracking_clues.begin());it!=_tracking_clues.end();)
if(it->second.time_stamp + RS_GROUTER_MAX_KEEP_TRACKING_CLUES < now)
{
#ifdef ROUTING_MATRIX_DEBUG
std::cerr << " removing old entry msgId=" << it->first << ", from id " << it->second.friend_id << ", obtained " << (now - it->second.time_stamp) << " secs ago." << std::endl;
#endif
std::map<RsGxsMessageId,RoutingTrackEntry>::iterator tmp(it) ;
++tmp ;
_tracking_clues.erase(it) ;
it=tmp ;
}
else
++it ;
return true ;
}
bool GRouterMatrix::addRoutingClue(const GRouterKeyId& key_id,const RsPeerId& source_friend,float weight)
{
// 1 - get the friend index.
@ -115,7 +157,19 @@ void GRouterMatrix::getListOfKnownKeys(std::vector<GRouterKeyId>& key_ids) const
key_ids.clear() ;
for(std::map<GRouterKeyId,std::vector<float> >::const_iterator it(_time_combined_hits.begin());it!=_time_combined_hits.end();++it)
key_ids.push_back(it->first) ;
key_ids.push_back(it->first) ;
}
bool GRouterMatrix::getTrackingInfo(const RsGxsMessageId& mid, RsPeerId &source_friend)
{
std::map<RsGxsMessageId,RoutingTrackEntry>::const_iterator it = _tracking_clues.find(mid) ;
if(it == _tracking_clues.end())
return false ;
source_friend = it->second.friend_id;
return true ;
}
void GRouterMatrix::debugDump() const
@ -143,6 +197,10 @@ void GRouterMatrix::debugDump() const
std::cerr << it->second[i] << " " ;
std::cerr << std::endl;
}
std::cerr << " Tracking clues: " << std::endl;
for(std::map<RsGxsMessageId, RoutingTrackEntry>::const_iterator it(_tracking_clues.begin());it!=_tracking_clues.end();++it)
std::cerr << " " << it->first << ": from " << it->second.friend_id << " " << now - it->second.time_stamp << " secs ago." << std::endl;
}
bool GRouterMatrix::computeRoutingProbabilities(const GRouterKeyId& key_id, const std::vector<RsPeerId>& friends, std::vector<float>& probas) const
@ -241,55 +299,78 @@ bool GRouterMatrix::saveList(std::list<RsItem*>& items)
std::cerr << " GRoutingMatrix::saveList()" << std::endl;
#endif
RsGRouterMatrixFriendListItem *item = new RsGRouterMatrixFriendListItem ;
RsGRouterMatrixFriendListItem *item = new RsGRouterMatrixFriendListItem ;
item->reverse_friend_indices = _reverse_friend_indices ;
items.push_back(item) ;
item->reverse_friend_indices = _reverse_friend_indices ;
items.push_back(item) ;
for(std::map<GRouterKeyId,std::list<RoutingMatrixHitEntry> >::const_iterator it(_routing_clues.begin());it!=_routing_clues.end();++it)
{
RsGRouterMatrixCluesItem *item = new RsGRouterMatrixCluesItem ;
for(std::map<GRouterKeyId,std::list<RoutingMatrixHitEntry> >::const_iterator it(_routing_clues.begin());it!=_routing_clues.end();++it)
{
RsGRouterMatrixCluesItem *item = new RsGRouterMatrixCluesItem ;
item->destination_key = it->first ;
item->clues = it->second ;
item->destination_key = it->first ;
item->clues = it->second ;
items.push_back(item) ;
}
items.push_back(item) ;
}
return true ;
for(std::map<RsGxsMessageId,RoutingTrackEntry>::const_iterator it(_tracking_clues.begin());it!=_tracking_clues.end();++it)
{
RsGRouterMatrixTrackItem *item = new RsGRouterMatrixTrackItem ;
item->provider_id = it->second.friend_id ;
item->time_stamp = it->second.time_stamp ;
item->message_id = it->first ;
items.push_back(item) ;
}
return true ;
}
bool GRouterMatrix::loadList(std::list<RsItem*>& items)
{
RsGRouterMatrixFriendListItem *itm1 = NULL ;
RsGRouterMatrixCluesItem *itm2 = NULL ;
RsGRouterMatrixFriendListItem *itm1 = NULL ;
RsGRouterMatrixCluesItem *itm2 = NULL ;
RsGRouterMatrixTrackItem *itm3 = NULL ;
#ifdef ROUTING_MATRIX_DEBUG
std::cerr << " GRoutingMatrix::loadList()" << std::endl;
#endif
for(std::list<RsItem*>::const_iterator it(items.begin());it!=items.end();++it)
{
if(NULL != (itm2 = dynamic_cast<RsGRouterMatrixCluesItem*>(*it)))
{
for(std::list<RsItem*>::const_iterator it(items.begin());it!=items.end();++it)
{
if(NULL != (itm3 = dynamic_cast<RsGRouterMatrixTrackItem*>(*it)))
{
#ifdef ROUTING_MATRIX_DEBUG
std::cerr << " initing routing clues." << std::endl;
std::cerr << " initing tracking clues." << std::endl;
#endif
RoutingTrackEntry rte ;
rte.friend_id = itm3->provider_id ;
rte.time_stamp = itm3->time_stamp ;
_tracking_clues[itm3->message_id] = rte;
}
if(NULL != (itm2 = dynamic_cast<RsGRouterMatrixCluesItem*>(*it)))
{
#ifdef ROUTING_MATRIX_DEBUG
std::cerr << " initing routing clues." << std::endl;
#endif
_routing_clues[itm2->destination_key] = itm2->clues ;
_proba_need_updating = true ; // notifies to re-compute all the info.
}
if(NULL != (itm1 = dynamic_cast<RsGRouterMatrixFriendListItem*>(*it)))
{
_reverse_friend_indices = itm1->reverse_friend_indices ;
_friend_indices.clear() ;
_routing_clues[itm2->destination_key] = itm2->clues ;
_proba_need_updating = true ; // notifies to re-compute all the info.
}
if(NULL != (itm1 = dynamic_cast<RsGRouterMatrixFriendListItem*>(*it)))
{
_reverse_friend_indices = itm1->reverse_friend_indices ;
_friend_indices.clear() ;
for(uint32_t i=0;i<_reverse_friend_indices.size();++i)
_friend_indices[_reverse_friend_indices[i]] = i ;
for(uint32_t i=0;i<_reverse_friend_indices.size();++i)
_friend_indices[_reverse_friend_indices[i]] = i ;
_proba_need_updating = true ; // notifies to re-compute all the info.
}
}
_proba_need_updating = true ; // notifies to re-compute all the info.
}
}
return true ;
return true ;
}

View file

@ -42,6 +42,12 @@ struct RoutingMatrixHitEntry
time_t time_stamp ;
};
struct RoutingTrackEntry
{
RsPeerId friend_id ; // not the full key. Gets too big otherwise!
time_t time_stamp ;
};
class GRouterMatrix
{
public:
@ -61,15 +67,19 @@ class GRouterMatrix
// Record one routing clue. The events can possibly be merged in time buckets.
//
bool addRoutingClue(const GRouterKeyId& id,const RsPeerId& source_friend,float weight) ;
bool addTrackingInfo(const RsGxsMessageId& id,const RsPeerId& source_friend) ;
bool saveList(std::list<RsItem*>& items) ;
bool loadList(std::list<RsItem*>& items) ;
bool cleanUp() ;
// Dump info in terminal.
//
void debugDump() const ;
void getListOfKnownKeys(std::vector<GRouterKeyId>& key_ids) const ;
bool getTrackingInfo(const RsGxsMessageId& id,RsPeerId& source_friend);
private:
// returns the friend id, possibly creating a new id.
//
@ -81,8 +91,9 @@ class GRouterMatrix
// List of events received and computed routing probabilities
//
std::map<GRouterKeyId, std::list<RoutingMatrixHitEntry> > _routing_clues ; // received routing clues. Should be saved.
std::map<GRouterKeyId, std::vector<float> > _time_combined_hits ; // hit matrix after time-convolution filter
std::map<GRouterKeyId, std::list<RoutingMatrixHitEntry> > _routing_clues ; // received routing clues. Should be saved.
std::map<GRouterKeyId, std::vector<float> > _time_combined_hits ; // hit matrix after time-convolution filter
std::map<RsGxsMessageId,RoutingTrackEntry> _tracking_clues ; // who provided the most recent messages
// This is used to avoid re-computing probas when new events have been received.
//
@ -93,6 +104,5 @@ class GRouterMatrix
//
std::map<RsPeerId,uint32_t> _friend_indices ; // index for each friend to lookup in the routing matrix Not saved.
std::vector<RsPeerId> _reverse_friend_indices ;// SSLid corresponding to each friend index. Saved.
};

View file

@ -37,9 +37,10 @@ class RsGRouterSignedReceiptItem ;
static const uint16_t GROUTER_CLIENT_ID_MESSAGES = 0x1001 ;
static const uint32_t RS_GROUTER_MATRIX_MAX_HIT_ENTRIES = 10 ; // max number of clues to store
static const uint32_t RS_GROUTER_MATRIX_MIN_TIME_BETWEEN_HITS = 60 ; // can be set to up to half the publish time interval. Prevents flooding routes.
static const uint32_t RS_GROUTER_MIN_CONFIG_SAVE_PERIOD = 5 ; // at most save config every 5 seconds
static const uint32_t RS_GROUTER_MATRIX_MAX_HIT_ENTRIES = 10 ; // max number of clues to store
static const uint32_t RS_GROUTER_MATRIX_MIN_TIME_BETWEEN_HITS = 60 ; // can be set to up to half the publish time interval. Prevents flooding routes.
static const uint32_t RS_GROUTER_MIN_CONFIG_SAVE_PERIOD = 10 ; // at most save config every 10 seconds
static const uint32_t RS_GROUTER_MAX_KEEP_TRACKING_CLUES = 86400*10 ; // max time for which we keep record of tracking info: 10 days.
static const float RS_GROUTER_BASE_WEIGHT_ROUTED_MSG = 1.0f ; // base contribution of routed message clue to routing matrix
static const float RS_GROUTER_BASE_WEIGHT_GXS_PACKET = 0.1f ; // base contribution of GXS message to routing matrix
@ -56,7 +57,7 @@ static const uint32_t MAX_INACTIVE_DATA_PIPE_DELAY = 300 ; // cl
static const time_t RS_GROUTER_DEBUG_OUTPUT_PERIOD = 10 ; // Output everything
static const time_t RS_GROUTER_AUTOWASH_PERIOD = 10 ; // Autowash every minute. Not a costly operation.
static const time_t RS_GROUTER_MATRIX_UPDATE_PERIOD = 1 *10 ; // Check for key advertising every 10 minutes
static const time_t RS_GROUTER_MATRIX_UPDATE_PERIOD = 60*10 ; // Check for key advertising every 10 minutes
static const uint32_t GROUTER_ITEM_MAX_CACHE_KEEP_TIME = 2*86400 ; // Cached items are kept for 48 hours at most.
static const uint32_t RS_GROUTER_DATA_STATUS_UNKNOWN = 0x0000 ; // unknown. Unused.

View file

@ -252,6 +252,7 @@ int p3GRouter::tick()
_last_matrix_update_time = now ;
_routing_matrix.updateRoutingProbabilities() ; // This should be locked.
_routing_matrix.cleanUp() ; // This should be locked.
}
#ifdef GROUTER_DEBUG
@ -315,6 +316,7 @@ bool p3GRouter::registerKey(const RsGxsId& authentication_key,const GRouterServi
grouter_debug() << " Auth GXS Id : " << authentication_key << std::endl;
grouter_debug() << " Client id : " << std::hex << client_id << std::dec << std::endl;
grouter_debug() << " Description : " << info.description_string << std::endl;
grouter_debug() << " Hash : " << hash << std::endl;
#endif
return true ;
@ -509,6 +511,12 @@ void p3GRouter::receiveTurtleData(RsTurtleGenericTunnelItem *gitem,const RsFileH
RsItem *itm = RsGRouterSerialiser().deserialise(item->data_bytes,&item->data_size) ;
if(itm == NULL)
{
std::cerr << "(EE) p3GRouter::receiveTurtleData(): cannot de-serialise data. Somthing wrong in the format. Item data (size="<< item->data_size << "): " << RsUtil::BinToHex((char*)item->data_bytes,item->data_size) << std::endl;
return ;
}
itm->PeerId(virtual_peer_id) ;
// At this point we can have either a transaction chunk, or a transaction ACK.
@ -1653,10 +1661,13 @@ void p3GRouter::handleIncomingDataItem(RsGRouterGenericDataItem *data_item)
std::cerr << " notyfying client." << std::endl;
#endif
client->receiveGRouterData(decrypted_item->destination_key,decrypted_item->signature.keyId,service_id,decrypted_item->data_bytes,decrypted_item->data_size);
decrypted_item->data_bytes = NULL ;
decrypted_item->data_size = 0 ;
if(client->acceptDataFromPeer(decrypted_item->signature.keyId))
{
client->receiveGRouterData(decrypted_item->destination_key,decrypted_item->signature.keyId,service_id,decrypted_item->data_bytes,decrypted_item->data_size);
decrypted_item->data_bytes = NULL ;
decrypted_item->data_size = 0 ;
}
delete decrypted_item ;
}
@ -1673,10 +1684,14 @@ bool p3GRouter::locked_getClientAndServiceId(const TurtleFileHash& hash, const R
{
client = NULL ;
service_id = 0;
RsGxsId gxs_id ;
makeGxsIdAndClientId(hash,gxs_id,service_id) ;
if(!locked_getGxsIdAndClientId(hash,gxs_id,service_id))
{
std::cerr << " p3GRouter::ERROR: locked_getGxsIdAndClientId(): no key registered for hash " << hash << std::endl;
return false ;
}
if(gxs_id != destination_key)
{
std::cerr << " ERROR: verification (destination) GXS key " << destination_key << " does not match key from hash " << gxs_id << std::endl;
@ -1698,6 +1713,15 @@ bool p3GRouter::locked_getClientAndServiceId(const TurtleFileHash& hash, const R
return true ;
}
void p3GRouter::addTrackingInfo(const RsGxsMessageId& mid,const RsPeerId& peer_id)
{
RS_STACK_MUTEX(grMtx) ;
#ifdef GROUTER_DEBUG
grouter_debug() << "Received new routing clue for key " << mid << " from peer " << peer_id << std::endl;
#endif
_routing_matrix.addTrackingInfo(mid,peer_id) ;
_changed = true ;
}
void p3GRouter::addRoutingClue(const GRouterKeyId& id,const RsPeerId& peer_id)
{
RS_STACK_MUTEX(grMtx) ;
@ -1705,6 +1729,7 @@ void p3GRouter::addRoutingClue(const GRouterKeyId& id,const RsPeerId& peer_id)
grouter_debug() << "Received new routing clue for key " << id << " from peer " << peer_id << std::endl;
#endif
_routing_matrix.addRoutingClue(id,peer_id,RS_GROUTER_BASE_WEIGHT_GXS_PACKET) ;
_changed = true ;
}
bool p3GRouter::registerClientService(const GRouterServiceId& id,GRouterClientService *service)
@ -2002,15 +2027,25 @@ Sha1CheckSum p3GRouter::makeTunnelHash(const RsGxsId& destination,const GRouterS
bytes[18] = (client >> 8) & 0xff ;
bytes[19] = client & 0xff ;
return Sha1CheckSum(bytes) ;
return RsDirUtil::sha1sum(bytes,20) ;
}
void p3GRouter::makeGxsIdAndClientId(const TurtleFileHash& sum,RsGxsId& gxs_id,GRouterServiceId& client_id)
bool p3GRouter::locked_getGxsIdAndClientId(const TurtleFileHash& sum,RsGxsId& gxs_id,GRouterServiceId& client_id)
{
assert( gxs_id.SIZE_IN_BYTES == 16) ;
assert(Sha1CheckSum::SIZE_IN_BYTES == 20) ;
gxs_id = RsGxsId(sum.toByteArray());// takes the first 16 bytes
client_id = sum.toByteArray()[19] + (sum.toByteArray()[18] << 8) ;
//gxs_id = RsGxsId(sum.toByteArray());// takes the first 16 bytes
//client_id = sum.toByteArray()[19] + (sum.toByteArray()[18] << 8) ;
std::map<Sha1CheckSum, GRouterPublishedKeyInfo>::const_iterator it = _owned_key_ids.find(sum);
if(it == _owned_key_ids.end())
return false ;
gxs_id = it->second.authentication_key ;
client_id = it->second.service_id ;
return true ;
}
bool p3GRouter::loadList(std::list<RsItem*>& items)
{
@ -2060,6 +2095,7 @@ bool p3GRouter::loadList(std::list<RsItem*>& items)
#ifdef GROUTER_DEBUG
debugDump();
#endif
items.clear() ;
return true ;
}
bool p3GRouter::saveList(bool& cleanup,std::list<RsItem*>& items)
@ -2154,6 +2190,13 @@ bool p3GRouter::getRoutingCacheInfo(std::vector<GRouterRoutingCacheInfo>& infos)
return true ;
}
bool p3GRouter::getTrackingInfo(const RsGxsMessageId &mid, RsPeerId &provider_id)
{
RS_STACK_MUTEX(grMtx) ;
return _routing_matrix.getTrackingInfo(mid,provider_id) ;
}
// Dump everything
//
void p3GRouter::debugDump()
@ -2214,8 +2257,8 @@ void p3GRouter::debugDump()
grouter_debug() << " Routing matrix: " << std::endl;
// if(_debug_enabled)
// _routing_matrix.debugDump() ;
if(_debug_enabled)
_routing_matrix.debugDump() ;
}

View file

@ -130,6 +130,7 @@ public:
//===================================================//
virtual void addRoutingClue(const GRouterKeyId& id,const RsPeerId& peer_id) ;
virtual void addTrackingInfo(const RsGxsMessageId& mid,const RsPeerId& peer_id) ;
//===================================================//
// Client/server request services //
@ -167,6 +168,7 @@ public:
// - Cache state (memory size, etc)
//
virtual bool getRoutingCacheInfo(std::vector<GRouterRoutingCacheInfo>& info) ;
virtual bool getTrackingInfo(const RsGxsMessageId& mid, RsPeerId& provider_id) ;
//===================================================//
// Derived from p3Service //
@ -223,7 +225,9 @@ private:
void handleLowLevelTransactionAckItem(RsGRouterTransactionAcknItem*) ;
static Sha1CheckSum computeDataItemHash(RsGRouterGenericDataItem *data_item);
#ifdef __APPLE__
public:
#endif
class nullstream: public std::ostream {};
std::ostream& grouter_debug() const
@ -232,7 +236,9 @@ private:
return _debug_enabled?(std::cerr):null;
}
#ifdef __APPLE__
private:
#endif
void routePendingObjects() ;
void handleTunnels() ;
void autoWash() ;
@ -263,8 +269,8 @@ private:
bool decryptDataItem(RsGRouterGenericDataItem *item) ;
static Sha1CheckSum makeTunnelHash(const RsGxsId& destination,const GRouterServiceId& client);
static void makeGxsIdAndClientId(const TurtleFileHash &sum,RsGxsId& gxs_id,GRouterServiceId& client_id);
bool locked_getGxsIdAndClientId(const TurtleFileHash &sum,RsGxsId& gxs_id,GRouterServiceId& client_id);
bool locked_sendTransactionData(const RsPeerId& pid,const RsGRouterTransactionItem& item);
void locked_collectAvailableFriends(const GRouterKeyId &gxs_id,std::list<RsPeerId>& friend_peers, const std::set<RsPeerId>& incoming_routes,bool is_origin);
@ -300,7 +306,6 @@ private:
//
GRouterMatrix _routing_matrix ;
// Stores the keys which identify the router's node. For each key, a structure holds:
// - the client service
// - flags

View file

@ -27,6 +27,7 @@
#include "gxssecurity.h"
#include "pqi/authgpg.h"
#include "util/rsdir.h"
#include "util/rsmemory.h"
//#include "retroshare/rspeers.h"
/****
@ -749,7 +750,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
in_offset += size_net_ekl;
// Conservative limits to detect weird errors due to corrupted encoding.
if(eklen < 0 || eklen > 512 || eklen+in_offset > inlen)
if(eklen < 0 || eklen > 512 || eklen+in_offset > (int)inlen)
{
std::cerr << "Error while deserialising encryption key length: eklen = " << std::dec << eklen << ". Giving up decryption." << std::endl;
free(ek);
@ -809,104 +810,114 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
bool GxsSecurity::validateNxsGrp(const RsNxsGrp& grp, const RsTlvKeySignature& sign, const RsTlvSecurityKey& key)
{
#ifdef GXS_SECURITY_DEBUG
std::cerr << "GxsSecurity::validateNxsGrp()";
std::cerr << std::endl;
std::cerr << "RsNxsGrp :";
std::cerr << std::endl;
grp.print(std::cerr, 10);
std::cerr << std::endl;
std::cerr << "GxsSecurity::validateNxsGrp()";
std::cerr << std::endl;
std::cerr << "RsNxsGrp :";
std::cerr << std::endl;
grp.print(std::cerr, 10);
std::cerr << std::endl;
#endif
RsGxsGrpMetaData& grpMeta = *(grp.metaData);
RsGxsGrpMetaData& grpMeta = *(grp.metaData);
/********************* check signature *******************/
/********************* check signature *******************/
/* check signature timeperiod */
if ((grpMeta.mPublishTs < key.startTS) || (key.endTS != 0 && grpMeta.mPublishTs > key.endTS))
{
/* check signature timeperiod */
if ((grpMeta.mPublishTs < key.startTS) || (key.endTS != 0 && grpMeta.mPublishTs > key.endTS))
{
#ifdef GXS_SECURITY_DEBUG
std::cerr << " GxsSecurity::validateNxsMsg() TS out of range";
std::cerr << std::endl;
std::cerr << " GxsSecurity::validateNxsMsg() TS out of range";
std::cerr << std::endl;
#endif
return false;
}
return false;
}
/* decode key */
const unsigned char *keyptr = (const unsigned char *) key.keyData.bin_data;
long keylen = key.keyData.bin_len;
unsigned int siglen = sign.signData.bin_len;
unsigned char *sigbuf = (unsigned char *) sign.signData.bin_data;
/* decode key */
const unsigned char *keyptr = (const unsigned char *) key.keyData.bin_data;
long keylen = key.keyData.bin_len;
unsigned int siglen = sign.signData.bin_len;
unsigned char *sigbuf = (unsigned char *) sign.signData.bin_data;
#ifdef DISTRIB_DEBUG
std::cerr << "GxsSecurity::validateNxsMsg() Decode Key";
std::cerr << " keylen: " << keylen << " siglen: " << siglen;
std::cerr << std::endl;
std::cerr << "GxsSecurity::validateNxsMsg() Decode Key";
std::cerr << " keylen: " << keylen << " siglen: " << siglen;
std::cerr << std::endl;
#endif
/* extract admin key */
RSA *rsakey = (key.keyFlags & RSTLV_KEY_TYPE_FULL)? d2i_RSAPrivateKey(NULL, &(keyptr), keylen): d2i_RSAPublicKey(NULL, &(keyptr), keylen);
/* extract admin key */
RSA *rsakey = (key.keyFlags & RSTLV_KEY_TYPE_FULL)? d2i_RSAPrivateKey(NULL, &(keyptr), keylen): d2i_RSAPublicKey(NULL, &(keyptr), keylen);
if (!rsakey)
{
if (!rsakey)
{
#ifdef GXS_SECURITY_DEBUG
std::cerr << "GxsSecurity::validateNxsGrp()";
std::cerr << " Invalid RSA Key";
std::cerr << std::endl;
std::cerr << "GxsSecurity::validateNxsGrp()";
std::cerr << " Invalid RSA Key";
std::cerr << std::endl;
key.print(std::cerr, 10);
key.print(std::cerr, 10);
#endif
}
}
std::vector<uint32_t> api_versions_to_check ;
api_versions_to_check.push_back(RS_GXS_GRP_META_DATA_VERSION_ID_0002) ; // put newest first, for debug info purpose
api_versions_to_check.push_back(RS_GXS_GRP_META_DATA_VERSION_ID_0001) ;
RsTlvKeySignatureSet signSet = grpMeta.signSet;
grpMeta.signSet.TlvClear();
int signOk =0;
EVP_PKEY *signKey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(signKey, rsakey);
RsTlvKeySignatureSet signSet = grpMeta.signSet;
grpMeta.signSet.TlvClear();
for(uint32_t i=0;i<api_versions_to_check.size() && 0==signOk;++i)
{
uint32_t metaDataLen = grpMeta.serial_size(api_versions_to_check[i]);
uint32_t allGrpDataLen = metaDataLen + grp.grp.bin_len;
uint32_t metaDataLen = grpMeta.serial_size();
uint32_t allGrpDataLen = metaDataLen + grp.grp.bin_len;
char* metaData = new char[metaDataLen];
char* allGrpData = new char[allGrpDataLen]; // msgData + metaData
RsTemporaryMemory metaData(metaDataLen) ;
RsTemporaryMemory allGrpData(allGrpDataLen) ;// msgData + metaData
grpMeta.serialise(metaData, metaDataLen);
grpMeta.serialise(metaData, metaDataLen,api_versions_to_check[i]);
// copy msg data and meta in allmsgData buffer
memcpy(allGrpData, grp.grp.bin_data, grp.grp.bin_len);
memcpy(allGrpData+(grp.grp.bin_len), metaData, metaDataLen);
// copy msg data and meta in allmsgData buffer
memcpy(allGrpData, grp.grp.bin_data, grp.grp.bin_len);
memcpy(allGrpData+(grp.grp.bin_len), metaData, metaDataLen);
delete[] metaData ;
/* calc and check signature */
EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
EVP_PKEY *signKey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(signKey, rsakey);
EVP_VerifyInit(mdctx, EVP_sha1());
EVP_VerifyUpdate(mdctx, allGrpData, allGrpDataLen);
signOk = EVP_VerifyFinal(mdctx, sigbuf, siglen, signKey);
EVP_MD_CTX_destroy(mdctx);
/* calc and check signature */
EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
if(i>0)
std::cerr << "(WW) Checking group signature with old api version " << i+1 << " : tag " << std::hex << api_versions_to_check[i] << std::dec << " result: " << signOk << std::endl;
}
EVP_VerifyInit(mdctx, EVP_sha1());
EVP_VerifyUpdate(mdctx, allGrpData, allGrpDataLen);
int signOk = EVP_VerifyFinal(mdctx, sigbuf, siglen, signKey);
/* clean up */
EVP_PKEY_free(signKey);
delete[] allGrpData ;
// restore data
/* clean up */
EVP_PKEY_free(signKey);
EVP_MD_CTX_destroy(mdctx);
grpMeta.signSet = signSet;
grpMeta.signSet = signSet;
if (signOk == 1)
{
if (signOk == 1)
{
#ifdef GXS_SECURITY_DEBUG
std::cerr << "GxsSecurity::validateNxsGrp() Signature OK";
std::cerr << std::endl;
std::cerr << "GxsSecurity::validateNxsGrp() Signature OK";
std::cerr << std::endl;
#endif
return true;
}
return true;
}
#ifdef GXS_SECURITY_DEBUG
std::cerr << "GxsSecurity::validateNxsGrp() Signature invalid";
std::cerr << std::endl;
std::cerr << "GxsSecurity::validateNxsGrp() Signature invalid";
std::cerr << std::endl;
#endif
return false;
return false;
}

View file

@ -49,19 +49,19 @@
#define MSG_INDEX_GRPID std::string("INDEX_MESSAGES_GRPID")
// generic
#define KEY_NXS_DATA std::string("nxsData")
#define KEY_NXS_DATA_LEN std::string("nxsDataLen")
#define KEY_NXS_IDENTITY std::string("identity")
#define KEY_GRP_ID std::string("grpId")
#define KEY_ORIG_GRP_ID std::string("origGrpId")
#define KEY_PARENT_GRP_ID std::string("parentGrpId")
#define KEY_SIGN_SET std::string("signSet")
#define KEY_TIME_STAMP std::string("timeStamp")
#define KEY_NXS_FLAGS std::string("flags")
#define KEY_NXS_META std::string("meta")
#define KEY_NXS_DATA std::string("nxsData")
#define KEY_NXS_DATA_LEN std::string("nxsDataLen")
#define KEY_NXS_IDENTITY std::string("identity")
#define KEY_GRP_ID std::string("grpId")
#define KEY_ORIG_GRP_ID std::string("origGrpId")
#define KEY_PARENT_GRP_ID std::string("parentGrpId")
#define KEY_SIGN_SET std::string("signSet")
#define KEY_TIME_STAMP std::string("timeStamp")
#define KEY_NXS_FLAGS std::string("flags")
#define KEY_NXS_META std::string("meta")
#define KEY_NXS_SERV_STRING std::string("serv_str")
#define KEY_NXS_HASH std::string("hash")
#define KEY_RECV_TS std::string("recv_time_stamp")
#define KEY_NXS_HASH std::string("hash")
#define KEY_RECV_TS std::string("recv_time_stamp")
// remove later
#define KEY_NXS_FILE_OLD std::string("nxsFile")
@ -94,8 +94,8 @@
#define KEY_MSG_NAME std::string("msgName")
// msg local
#define KEY_MSG_STATUS std::string("msgStatus")
#define KEY_CHILD_TS std::string("childTs")
#define KEY_MSG_STATUS std::string("msgStatus")
#define KEY_CHILD_TS std::string("childTs")
// database release columns
#define KEY_DATABASE_RELEASE_ID std::string("id")
@ -129,22 +129,22 @@ RsDataService::RsDataService(const std::string &serviceDir, const std::string &d
initialise(isNewDatabase);
// for retrieving msg meta
mColMsgMeta_GrpId = addColumn(mMsgMetaColumns, KEY_GRP_ID);
mColMsgMeta_TimeStamp = addColumn(mMsgMetaColumns, KEY_TIME_STAMP);
mColMsgMeta_NxsFlags = addColumn(mMsgMetaColumns, KEY_NXS_FLAGS);
mColMsgMeta_SignSet = addColumn(mMsgMetaColumns, KEY_SIGN_SET);
mColMsgMeta_NxsIdentity = addColumn(mMsgMetaColumns, KEY_NXS_IDENTITY);
mColMsgMeta_NxsHash = addColumn(mMsgMetaColumns, KEY_NXS_HASH);
mColMsgMeta_MsgId = addColumn(mMsgMetaColumns, KEY_MSG_ID);
mColMsgMeta_OrigMsgId = addColumn(mMsgMetaColumns, KEY_ORIG_MSG_ID);
mColMsgMeta_MsgStatus = addColumn(mMsgMetaColumns, KEY_MSG_STATUS);
mColMsgMeta_ChildTs = addColumn(mMsgMetaColumns, KEY_CHILD_TS);
mColMsgMeta_MsgParentId = addColumn(mMsgMetaColumns, KEY_MSG_PARENT_ID);
mColMsgMeta_MsgThreadId = addColumn(mMsgMetaColumns, KEY_MSG_THREAD_ID);
mColMsgMeta_Name = addColumn(mMsgMetaColumns, KEY_MSG_NAME);
mColMsgMeta_GrpId = addColumn(mMsgMetaColumns, KEY_GRP_ID);
mColMsgMeta_TimeStamp = addColumn(mMsgMetaColumns, KEY_TIME_STAMP);
mColMsgMeta_NxsFlags = addColumn(mMsgMetaColumns, KEY_NXS_FLAGS);
mColMsgMeta_SignSet = addColumn(mMsgMetaColumns, KEY_SIGN_SET);
mColMsgMeta_NxsIdentity = addColumn(mMsgMetaColumns, KEY_NXS_IDENTITY);
mColMsgMeta_NxsHash = addColumn(mMsgMetaColumns, KEY_NXS_HASH);
mColMsgMeta_MsgId = addColumn(mMsgMetaColumns, KEY_MSG_ID);
mColMsgMeta_OrigMsgId = addColumn(mMsgMetaColumns, KEY_ORIG_MSG_ID);
mColMsgMeta_MsgStatus = addColumn(mMsgMetaColumns, KEY_MSG_STATUS);
mColMsgMeta_ChildTs = addColumn(mMsgMetaColumns, KEY_CHILD_TS);
mColMsgMeta_MsgParentId = addColumn(mMsgMetaColumns, KEY_MSG_PARENT_ID);
mColMsgMeta_MsgThreadId = addColumn(mMsgMetaColumns, KEY_MSG_THREAD_ID);
mColMsgMeta_Name = addColumn(mMsgMetaColumns, KEY_MSG_NAME);
mColMsgMeta_NxsServString = addColumn(mMsgMetaColumns, KEY_NXS_SERV_STRING);
mColMsgMeta_RecvTs = addColumn(mMsgMetaColumns, KEY_RECV_TS);
mColMsgMeta_NxsDataLen = addColumn(mMsgMetaColumns, KEY_NXS_DATA_LEN);
mColMsgMeta_RecvTs = addColumn(mMsgMetaColumns, KEY_RECV_TS);
mColMsgMeta_NxsDataLen = addColumn(mMsgMetaColumns, KEY_NXS_DATA_LEN);
// for retrieving actual data
mColMsg_GrpId = addColumn(mMsgColumns, KEY_GRP_ID);
@ -152,38 +152,48 @@ RsDataService::RsDataService(const std::string &serviceDir, const std::string &d
mColMsg_MetaData = addColumn(mMsgColumns, KEY_NXS_META);
mColMsg_MsgId = addColumn(mMsgColumns, KEY_MSG_ID);
// for retrieving msg data with meta
mMsgColumnsWithMeta = mMsgColumns;
mColMsg_WithMetaOffset = mMsgColumnsWithMeta.size();
mMsgColumnsWithMeta.insert(mMsgColumnsWithMeta.end(), mMsgMetaColumns.begin(), mMsgMetaColumns.end());
// for retrieving grp meta data
mColGrpMeta_GrpId = addColumn(mGrpMetaColumns, KEY_GRP_ID);
mColGrpMeta_TimeStamp = addColumn(mGrpMetaColumns, KEY_TIME_STAMP);
mColGrpMeta_NxsFlags = addColumn(mGrpMetaColumns, KEY_NXS_FLAGS);
mColGrpMeta_GrpId = addColumn(mGrpMetaColumns, KEY_GRP_ID);
mColGrpMeta_TimeStamp = addColumn(mGrpMetaColumns, KEY_TIME_STAMP);
mColGrpMeta_NxsFlags = addColumn(mGrpMetaColumns, KEY_NXS_FLAGS);
// mColGrpMeta_SignSet = addColumn(mGrpMetaColumns, KEY_SIGN_SET);
mColGrpMeta_NxsIdentity = addColumn(mGrpMetaColumns, KEY_NXS_IDENTITY);
mColGrpMeta_NxsHash = addColumn(mGrpMetaColumns, KEY_NXS_HASH);
mColGrpMeta_KeySet = addColumn(mGrpMetaColumns, KEY_KEY_SET);
mColGrpMeta_SubscrFlag = addColumn(mGrpMetaColumns, KEY_GRP_SUBCR_FLAG);
mColGrpMeta_Pop = addColumn(mGrpMetaColumns, KEY_GRP_POP);
mColGrpMeta_MsgCount = addColumn(mGrpMetaColumns, KEY_MSG_COUNT);
mColGrpMeta_Status = addColumn(mGrpMetaColumns, KEY_GRP_STATUS);
mColGrpMeta_Name = addColumn(mGrpMetaColumns, KEY_GRP_NAME);
mColGrpMeta_LastPost = addColumn(mGrpMetaColumns, KEY_GRP_LAST_POST);
mColGrpMeta_OrigGrpId = addColumn(mGrpMetaColumns, KEY_ORIG_GRP_ID);
mColGrpMeta_ServString = addColumn(mGrpMetaColumns, KEY_NXS_SERV_STRING);
mColGrpMeta_SignFlags = addColumn(mGrpMetaColumns, KEY_GRP_SIGN_FLAGS);
mColGrpMeta_CircleId = addColumn(mGrpMetaColumns, KEY_GRP_CIRCLE_ID);
mColGrpMeta_CircleType = addColumn(mGrpMetaColumns, KEY_GRP_CIRCLE_TYPE);
mColGrpMeta_NxsHash = addColumn(mGrpMetaColumns, KEY_NXS_HASH);
mColGrpMeta_KeySet = addColumn(mGrpMetaColumns, KEY_KEY_SET);
mColGrpMeta_SubscrFlag = addColumn(mGrpMetaColumns, KEY_GRP_SUBCR_FLAG);
mColGrpMeta_Pop = addColumn(mGrpMetaColumns, KEY_GRP_POP);
mColGrpMeta_MsgCount = addColumn(mGrpMetaColumns, KEY_MSG_COUNT);
mColGrpMeta_Status = addColumn(mGrpMetaColumns, KEY_GRP_STATUS);
mColGrpMeta_Name = addColumn(mGrpMetaColumns, KEY_GRP_NAME);
mColGrpMeta_LastPost = addColumn(mGrpMetaColumns, KEY_GRP_LAST_POST);
mColGrpMeta_OrigGrpId = addColumn(mGrpMetaColumns, KEY_ORIG_GRP_ID);
mColGrpMeta_ServString = addColumn(mGrpMetaColumns, KEY_NXS_SERV_STRING);
mColGrpMeta_SignFlags = addColumn(mGrpMetaColumns, KEY_GRP_SIGN_FLAGS);
mColGrpMeta_CircleId = addColumn(mGrpMetaColumns, KEY_GRP_CIRCLE_ID);
mColGrpMeta_CircleType = addColumn(mGrpMetaColumns, KEY_GRP_CIRCLE_TYPE);
mColGrpMeta_InternCircle = addColumn(mGrpMetaColumns, KEY_GRP_INTERNAL_CIRCLE);
mColGrpMeta_Originator = addColumn(mGrpMetaColumns, KEY_GRP_ORIGINATOR);
mColGrpMeta_Originator = addColumn(mGrpMetaColumns, KEY_GRP_ORIGINATOR);
mColGrpMeta_AuthenFlags = addColumn(mGrpMetaColumns, KEY_GRP_AUTHEN_FLAGS);
mColGrpMeta_ParentGrpId = addColumn(mGrpMetaColumns, KEY_PARENT_GRP_ID);
mColGrpMeta_RecvTs = addColumn(mGrpMetaColumns, KEY_RECV_TS);
mColGrpMeta_RepCutoff = addColumn(mGrpMetaColumns, KEY_GRP_REP_CUTOFF);
mColGrpMeta_NxsDataLen = addColumn(mGrpMetaColumns, KEY_NXS_DATA_LEN);
mColGrpMeta_RecvTs = addColumn(mGrpMetaColumns, KEY_RECV_TS);
mColGrpMeta_RepCutoff = addColumn(mGrpMetaColumns, KEY_GRP_REP_CUTOFF);
mColGrpMeta_NxsDataLen = addColumn(mGrpMetaColumns, KEY_NXS_DATA_LEN);
// for retrieving actual grp data
mColGrp_GrpId = addColumn(mGrpColumns, KEY_GRP_ID);
mColGrp_NxsData = addColumn(mGrpColumns, KEY_NXS_DATA);
mColGrp_MetaData = addColumn(mGrpColumns, KEY_NXS_META);
// for retrieving grp data with meta
mGrpColumnsWithMeta = mGrpColumns;
mColGrp_WithMetaOffset = mGrpColumnsWithMeta.size();
mGrpColumnsWithMeta.insert(mGrpColumnsWithMeta.end(), mGrpMetaColumns.begin(), mGrpMetaColumns.end());
// Group id columns
mColGrpId_GrpId = addColumn(mGrpIdColumn, KEY_GRP_ID);
@ -474,7 +484,7 @@ bool RsDataService::finishReleaseUpdate(int release, bool result)
return result;
}
RsGxsGrpMetaData* RsDataService::locked_getGrpMeta(RetroCursor &c)
RsGxsGrpMetaData* RsDataService::locked_getGrpMeta(RetroCursor &c, int colOffset)
{
#ifdef RS_DATA_SERVICE_DEBUG
std::cerr << "RsDataService::locked_getGrpMeta()";
@ -492,27 +502,27 @@ RsGxsGrpMetaData* RsDataService::locked_getGrpMeta(RetroCursor &c)
// grpId
std::string tempId;
c.getString(mColGrpMeta_GrpId, tempId);
c.getString(mColGrpMeta_GrpId + colOffset, tempId);
grpMeta->mGroupId = RsGxsGroupId(tempId);
c.getString(mColGrpMeta_NxsIdentity, tempId);
c.getString(mColGrpMeta_NxsIdentity + colOffset, tempId);
grpMeta->mAuthorId = RsGxsId(tempId);
c.getString(mColGrpMeta_Name, grpMeta->mGroupName);
c.getString(mColGrpMeta_OrigGrpId, tempId);
c.getString(mColGrpMeta_Name + colOffset, grpMeta->mGroupName);
c.getString(mColGrpMeta_OrigGrpId + colOffset, tempId);
grpMeta->mOrigGrpId = RsGxsGroupId(tempId);
c.getString(mColGrpMeta_ServString, grpMeta->mServiceString);
c.getString(mColGrpMeta_ServString + colOffset, grpMeta->mServiceString);
std::string temp;
c.getString(mColGrpMeta_NxsHash, temp);
c.getString(mColGrpMeta_NxsHash + colOffset, temp);
grpMeta->mHash = RsFileHash(temp);
grpMeta->mReputationCutOff = c.getInt32(mColGrpMeta_RepCutoff);
grpMeta->mSignFlags = c.getInt32(mColGrpMeta_SignFlags);
grpMeta->mReputationCutOff = c.getInt32(mColGrpMeta_RepCutoff + colOffset);
grpMeta->mSignFlags = c.getInt32(mColGrpMeta_SignFlags + colOffset);
grpMeta->mPublishTs = c.getInt32(mColGrpMeta_TimeStamp);
grpMeta->mGroupFlags = c.getInt32(mColGrpMeta_NxsFlags);
grpMeta->mGrpSize = c.getInt32(mColGrpMeta_NxsDataLen);
grpMeta->mPublishTs = c.getInt32(mColGrpMeta_TimeStamp + colOffset);
grpMeta->mGroupFlags = c.getInt32(mColGrpMeta_NxsFlags + colOffset);
grpMeta->mGrpSize = c.getInt32(mColGrpMeta_NxsDataLen + colOffset);
offset = 0; data = NULL; data_len = 0;
data = (char*)c.getData(mColGrpMeta_KeySet, data_len);
data = (char*)c.getData(mColGrpMeta_KeySet + colOffset, data_len);
if(data)
ok &= grpMeta->keys.GetTlv(data, data_len, &offset);
@ -520,22 +530,22 @@ RsGxsGrpMetaData* RsDataService::locked_getGrpMeta(RetroCursor &c)
grpMeta->keys.TlvClear() ;
// local meta
grpMeta->mSubscribeFlags = c.getInt32(mColGrpMeta_SubscrFlag);
grpMeta->mPop = c.getInt32(mColGrpMeta_Pop);
grpMeta->mVisibleMsgCount = c.getInt32(mColGrpMeta_MsgCount);
grpMeta->mLastPost = c.getInt32(mColGrpMeta_LastPost);
grpMeta->mGroupStatus = c.getInt32(mColGrpMeta_Status);
grpMeta->mSubscribeFlags = c.getInt32(mColGrpMeta_SubscrFlag + colOffset);
grpMeta->mPop = c.getInt32(mColGrpMeta_Pop + colOffset);
grpMeta->mVisibleMsgCount = c.getInt32(mColGrpMeta_MsgCount + colOffset);
grpMeta->mLastPost = c.getInt32(mColGrpMeta_LastPost + colOffset);
grpMeta->mGroupStatus = c.getInt32(mColGrpMeta_Status + colOffset);
c.getString(mColGrpMeta_CircleId, tempId);
c.getString(mColGrpMeta_CircleId + colOffset, tempId);
grpMeta->mCircleId = RsGxsCircleId(tempId);
grpMeta->mCircleType = c.getInt32(mColGrpMeta_CircleType);
c.getString(mColGrpMeta_InternCircle, tempId);
grpMeta->mCircleType = c.getInt32(mColGrpMeta_CircleType + colOffset);
c.getString(mColGrpMeta_InternCircle + colOffset, tempId);
grpMeta->mInternalCircle = RsGxsCircleId(tempId);
std::string s ; c.getString(mColGrpMeta_Originator, s) ;
std::string s ; c.getString(mColGrpMeta_Originator + colOffset, s) ;
grpMeta->mOriginator = RsPeerId(s);
grpMeta->mAuthenFlags = c.getInt32(mColGrpMeta_AuthenFlags);
grpMeta->mRecvTS = c.getInt32(mColGrpMeta_RecvTs);
grpMeta->mAuthenFlags = c.getInt32(mColGrpMeta_AuthenFlags + colOffset);
grpMeta->mRecvTS = c.getInt32(mColGrpMeta_RecvTs + colOffset);
c.getString(mColGrpMeta_ParentGrpId, tempId);
@ -591,7 +601,7 @@ RsNxsGrp* RsDataService::locked_getGroup(RetroCursor &c)
return NULL;
}
RsGxsMsgMetaData* RsDataService::locked_getMsgMeta(RetroCursor &c)
RsGxsMsgMetaData* RsDataService::locked_getMsgMeta(RetroCursor &c, int colOffset)
{
RsGxsMsgMetaData* msgMeta = new RsGxsMsgMetaData();
@ -602,44 +612,43 @@ RsGxsMsgMetaData* RsDataService::locked_getMsgMeta(RetroCursor &c)
char* data = NULL;
std::string gId;
c.getString(mColMsgMeta_GrpId, gId);
c.getString(mColMsgMeta_GrpId + colOffset, gId);
msgMeta->mGroupId = RsGxsGroupId(gId);
std::string temp;
c.getString(mColMsgMeta_MsgId, temp);
c.getString(mColMsgMeta_MsgId + colOffset, temp);
msgMeta->mMsgId = RsGxsMessageId(temp);
// without these, a msg is meaningless
ok &= (!msgMeta->mGroupId.isNull()) && (!msgMeta->mMsgId.isNull());
c.getString(mColMsgMeta_OrigMsgId, temp);
c.getString(mColMsgMeta_OrigMsgId + colOffset, temp);
msgMeta->mOrigMsgId = RsGxsMessageId(temp);
c.getString(mColMsgMeta_NxsIdentity, temp);
c.getString(mColMsgMeta_NxsIdentity + colOffset, temp);
msgMeta->mAuthorId = RsGxsId(temp);
c.getString(mColMsgMeta_Name, msgMeta->mMsgName);
c.getString(mColMsgMeta_NxsServString, msgMeta->mServiceString);
c.getString(mColMsgMeta_Name + colOffset, msgMeta->mMsgName);
c.getString(mColMsgMeta_NxsServString + colOffset, msgMeta->mServiceString);
c.getString(mColMsgMeta_NxsHash, temp);
c.getString(mColMsgMeta_NxsHash + colOffset, temp);
msgMeta->mHash = RsFileHash(temp);
msgMeta->recvTS = c.getInt32(mColMsgMeta_RecvTs);
msgMeta->recvTS = c.getInt32(mColMsgMeta_RecvTs + colOffset);
offset = 0;
data = (char*)c.getData(mColMsgMeta_SignSet, data_len);
data = (char*)c.getData(mColMsgMeta_SignSet + colOffset, data_len);
msgMeta->signSet.GetTlv(data, data_len, &offset);
msgMeta->mMsgSize = c.getInt32(mColMsgMeta_NxsDataLen);
msgMeta->mMsgSize = c.getInt32(mColMsgMeta_NxsDataLen + colOffset);
msgMeta->mMsgFlags = c.getInt32(mColMsgMeta_NxsFlags);
msgMeta->mPublishTs = c.getInt32(mColMsgMeta_TimeStamp);
msgMeta->mMsgFlags = c.getInt32(mColMsgMeta_NxsFlags + colOffset);
msgMeta->mPublishTs = c.getInt32(mColMsgMeta_TimeStamp + colOffset);
offset = 0; data_len = 0;
// thread and parent id
c.getString(mColMsgMeta_MsgThreadId, temp);
c.getString(mColMsgMeta_MsgThreadId + colOffset, temp);
msgMeta->mThreadId = RsGxsMessageId(temp);
c.getString(mColMsgMeta_MsgParentId, temp);
c.getString(mColMsgMeta_MsgParentId + colOffset, temp);
msgMeta->mParentId = RsGxsMessageId(temp);
// local meta
msgMeta->mMsgStatus = c.getInt32(mColMsgMeta_MsgStatus);
msgMeta->mChildTs = c.getInt32(mColMsgMeta_ChildTs);
msgMeta->mMsgStatus = c.getInt32(mColMsgMeta_MsgStatus + colOffset);
msgMeta->mChildTs = c.getInt32(mColMsgMeta_ChildTs + colOffset);
if(ok)
return msgMeta;
@ -1020,13 +1029,13 @@ int RsDataService::retrieveNxsGrps(std::map<RsGxsGroupId, RsNxsGrp *> &grp, bool
if(grp.empty()){
RsStackMutex stack(mDbMutex);
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, mGrpColumns, "", "");
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, withMeta ? mGrpColumnsWithMeta : mGrpColumns, "", "");
if(c)
{
std::vector<RsNxsGrp*> grps;
locked_retrieveGroups(c, grps);
locked_retrieveGroups(c, grps, withMeta ? mColGrp_WithMetaOffset : 0);
std::vector<RsNxsGrp*>::iterator vit = grps.begin();
#ifdef RS_DATA_SERVICE_DEBUG_TIME
@ -1051,12 +1060,12 @@ int RsDataService::retrieveNxsGrps(std::map<RsGxsGroupId, RsNxsGrp *> &grp, bool
for(; mit != grp.end(); ++mit)
{
const RsGxsGroupId& grpId = mit->first;
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, mGrpColumns, "grpId='" + grpId.toStdString() + "'", "");
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, withMeta ? mGrpColumnsWithMeta : mGrpColumns, "grpId='" + grpId.toStdString() + "'", "");
if(c)
{
std::vector<RsNxsGrp*> grps;
locked_retrieveGroups(c, grps);
locked_retrieveGroups(c, grps, withMeta ? mColGrp_WithMetaOffset : 0);
if(!grps.empty())
{
@ -1085,38 +1094,10 @@ int RsDataService::retrieveNxsGrps(std::map<RsGxsGroupId, RsNxsGrp *> &grp, bool
std::cerr << "RsDataService::retrieveNxsGrps() " << mDbName << ", Requests: " << requestedGroups << ", Results: " << resultCount << ", Time: " << timer.duration() << std::endl;
#endif
if(withMeta && !grp.empty())
{
std::map<RsGxsGroupId, RsGxsGrpMetaData*> metaMap;
std::map<RsGxsGroupId, RsNxsGrp *>::iterator mit = grp.begin();
for(; mit != grp.end(); ++mit)
metaMap.insert(std::make_pair(mit->first, (RsGxsGrpMetaData*)(NULL)));
retrieveGxsGrpMetaData(metaMap);
mit = grp.begin();
for(; mit != grp.end(); ++mit)
{
RsNxsGrp* grpPtr = grp[mit->first];
grpPtr->metaData = metaMap[mit->first];
#ifdef RS_DATA_SERVICE_DEBUG
std::cerr << "RsDataService::retrieveNxsGrps() GrpId: " << mit->first.toStdString();
std::cerr << " CircleType: " << (uint32_t) grpPtr->metaData->mCircleType;
std::cerr << " CircleId: " << grpPtr->metaData->mCircleId.toStdString();
std::cerr << std::endl;
#endif
}
#ifdef RS_DATA_SERVICE_DEBUG_TIME
std::cerr << "RsDataService::retrieveNxsGrps() " << mDbName << ", Time with meta: " << timer.duration() << std::endl;
#endif
}
return 1;
}
void RsDataService::locked_retrieveGroups(RetroCursor* c, std::vector<RsNxsGrp*>& grps){
void RsDataService::locked_retrieveGroups(RetroCursor* c, std::vector<RsNxsGrp*>& grps, int metaOffset){
if(c){
bool valid = c->moveToFirst();
@ -1127,6 +1108,9 @@ void RsDataService::locked_retrieveGroups(RetroCursor* c, std::vector<RsNxsGrp*>
// only add the latest grp info
if(g)
{
if (metaOffset) {
g->metaData = locked_getGrpMeta(*c, metaOffset);
}
grps.push_back(g);
}
valid = c->moveToNext();
@ -1143,8 +1127,6 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b
GxsMsgReq::const_iterator mit = reqIds.begin();
GxsMsgReq metaReqIds;// collects metaReqIds if needed
for(; mit != reqIds.end(); ++mit)
{
@ -1158,15 +1140,11 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b
RsStackMutex stack(mDbMutex);
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", "");
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, withMeta ? mMsgColumnsWithMeta : mMsgColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", "");
if(c)
{
locked_retrieveMessages(c, msgSet);
#ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount += msgSet.size();
#endif
locked_retrieveMessages(c, msgSet, withMeta ? mColMsg_WithMetaOffset : 0);
}
delete c;
@ -1180,36 +1158,24 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b
RsStackMutex stack(mDbMutex);
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgColumns, KEY_GRP_ID+ "='" + grpId.toStdString()
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, withMeta ? mMsgColumnsWithMeta : mMsgColumns, KEY_GRP_ID+ "='" + grpId.toStdString()
+ "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", "");
if(c)
{
locked_retrieveMessages(c, msgSet);
#ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount += c->getResultCount();
#endif
locked_retrieveMessages(c, msgSet, withMeta ? mColMsg_WithMetaOffset : 0);
}
delete c;
}
}
#ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount += msgSet.size();
#endif
msg[grpId] = msgSet;
if(withMeta)
{
std::vector<RsGxsMessageId> msgIds;
std::vector<RsNxsMsg*>::iterator lit = msgSet.begin(),
lit_end = msgSet.end();
for(; lit != lit_end; ++lit)
msgIds.push_back( (*lit)->msgId );
metaReqIds[grpId] = msgIds;
}
msgSet.clear();
}
@ -1217,73 +1183,19 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b
std::cerr << "RsDataService::retrieveNxsMsgs() " << mDbName << ", Requests: " << reqIds.size() << ", Results: " << resultCount << ", Time: " << timer.duration() << std::endl;
#endif
// tres expensive !?
if(withMeta)
{
GxsMsgMetaResult metaResult;
// request with meta ids so there is no chance of
// a mem leak being left over
retrieveGxsMsgMetaData(metaReqIds, metaResult);
GxsMsgResult::iterator mit2 = msg.begin(), mit2_end = msg.end();
for(; mit2 != mit2_end; ++mit2)
{
const RsGxsGroupId& grpId = mit2->first;
std::vector<RsNxsMsg*>& msgV = msg[grpId];
std::vector<RsNxsMsg*>::iterator lit = msgV.begin(),
lit_end = msgV.end();
// as retrieval only attempts to retrieve what was found this elimiates chance
// of a memory fault as all are assigned
for(; lit != lit_end; ++lit)
{
std::vector<RsGxsMsgMetaData*>& msgMetaV = metaResult[grpId];
std::vector<RsGxsMsgMetaData*>::iterator meta_lit = msgMetaV.begin();
RsNxsMsg* msgPtr = *lit;
for(; meta_lit != msgMetaV.end(); )
{
RsGxsMsgMetaData* meta = *meta_lit;
if(meta->mMsgId == msgPtr->msgId)
{
msgPtr->metaData = meta;
meta_lit = msgMetaV.erase(meta_lit);
}else{
++meta_lit;
}
}
}
std::vector<RsGxsMsgMetaData*>& msgMetaV = metaResult[grpId];
std::vector<RsGxsMsgMetaData*>::iterator meta_lit;
// clean up just in case, should not go in here
for(meta_lit = msgMetaV.begin(); meta_lit !=
msgMetaV.end(); )
{
RsGxsMsgMetaData* meta = *meta_lit;
delete meta;
meta_lit = msgMetaV.erase(meta_lit);
}
}
#ifdef RS_DATA_SERVICE_DEBUG_TIME
std::cerr << "RsDataService::retrieveNxsMsgs() " << mDbName << ", Time with meta: " << timer.duration() << std::endl;
#endif
}
return 1;
}
void RsDataService::locked_retrieveMessages(RetroCursor *c, std::vector<RsNxsMsg *> &msgs)
void RsDataService::locked_retrieveMessages(RetroCursor *c, std::vector<RsNxsMsg *> &msgs, int metaOffset)
{
bool valid = c->moveToFirst();
while(valid){
RsNxsMsg* m = locked_getMessage(*c);
if(m){
if (metaOffset) {
m->metaData = locked_getMsgMeta(*c, metaOffset);
}
msgs.push_back(m);
}
@ -1318,10 +1230,6 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes
if (c)
{
locked_retrieveMsgMeta(c, metaSet);
#ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount += metaSet.size();
#endif
}
}else{
@ -1336,14 +1244,14 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes
if (c)
{
locked_retrieveMsgMeta(c, metaSet);
#ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount += c->getResultCount();
#endif
}
}
}
#ifdef RS_DATA_SERVICE_DEBUG_TIME
resultCount += metaSet.size();
#endif
msgMeta[grpId] = metaSet;
}
@ -1361,7 +1269,7 @@ void RsDataService::locked_retrieveMsgMeta(RetroCursor *c, std::vector<RsGxsMsgM
{
bool valid = c->moveToFirst();
while(valid){
RsGxsMsgMetaData* m = locked_getMsgMeta(*c);
RsGxsMsgMetaData* m = locked_getMsgMeta(*c, 0);
if(m != NULL)
msgMeta.push_back(m);
@ -1402,7 +1310,7 @@ int RsDataService::retrieveGxsGrpMetaData(std::map<RsGxsGroupId, RsGxsGrpMetaDat
while(valid)
{
RsGxsGrpMetaData* g = locked_getGrpMeta(*c);
RsGxsGrpMetaData* g = locked_getGrpMeta(*c, 0);
if(g)
{
grp[g->mGroupId] = g;
@ -1431,7 +1339,7 @@ int RsDataService::retrieveGxsGrpMetaData(std::map<RsGxsGroupId, RsGxsGrpMetaDat
while(valid)
{
RsGxsGrpMetaData* g = locked_getGrpMeta(*c);
RsGxsGrpMetaData* g = locked_getGrpMeta(*c, 0);
if(g)
{

View file

@ -181,14 +181,15 @@ private:
* @param c cursor to result set
* @param msgs messages retrieved from cursor are stored here
*/
void locked_retrieveMessages(RetroCursor* c, std::vector<RsNxsMsg*>& msgs);
void locked_retrieveMessages(RetroCursor* c, std::vector<RsNxsMsg*>& msgs, int metaOffset);
/*!
* Retrieves all the grp results from a cursor
* @param c cursor to result set
* @param grps groups retrieved from cursor are stored here
* @param withMeta this initialise the metaData member of the nxsgroups retrieved
*/
void locked_retrieveGroups(RetroCursor* c, std::vector<RsNxsGrp*>& grps);
void locked_retrieveGroups(RetroCursor* c, std::vector<RsNxsGrp*>& grps, int metaOffset);
/*!
* Retrieves all the msg meta results from a cursor
@ -201,13 +202,13 @@ private:
* extracts a msg meta item from a cursor at its
* current position
*/
RsGxsMsgMetaData* locked_getMsgMeta(RetroCursor& c);
RsGxsMsgMetaData* locked_getMsgMeta(RetroCursor& c, int colOffset);
/*!
* extracts a grp meta item from a cursor at its
* current position
*/
RsGxsGrpMetaData* locked_getGrpMeta(RetroCursor& c);
RsGxsGrpMetaData* locked_getGrpMeta(RetroCursor& c, int colOffset);
/*!
* extracts a msg item from a cursor at its
@ -257,10 +258,12 @@ private:
std::list<std::string> mMsgColumns;
std::list<std::string> mMsgMetaColumns;
std::list<std::string> mMsgColumnsWithMeta;
std::list<std::string> mMsgIdColumn;
std::list<std::string> mGrpColumns;
std::list<std::string> mGrpMetaColumns;
std::list<std::string> mGrpColumnsWithMeta;
std::list<std::string> mGrpIdColumn;
// Message meta column
@ -287,6 +290,9 @@ private:
int mColMsg_MetaData;
int mColMsg_MsgId;
// Message columns with meta
int mColMsg_WithMetaOffset;
// Group meta columns
int mColGrpMeta_GrpId;
int mColGrpMeta_TimeStamp;
@ -319,6 +325,9 @@ private:
int mColGrp_NxsData;
int mColGrp_MetaData;
// Group columns with meta
int mColGrp_WithMetaOffset;
// Group id columns
int mColGrpId_GrpId;

File diff suppressed because it is too large Load diff

View file

@ -739,12 +739,13 @@ private:
/*!
* Attempts to validate msg signatures
* @param msg message to be validated
* @param grpFlag the flag for the group the message belongs to
* @param grpFlag the distribution flag for the group the message belongs to
* @param grpFlag the signature flag for the group the message belongs to
* @param grpKeySet the key set user has for the message's group
* @return VALIDATE_SUCCESS for success, VALIDATE_FAIL for fail,
* VALIDATE_ID_SIGN_NOT_AVAIL for Id sign key not avail (but requested)
*/
int validateMsg(RsNxsMsg* msg, const uint32_t& grpFlag, RsTlvSecurityKeySet& grpKeySet);
int validateMsg(RsNxsMsg* msg, const uint32_t& grpFlag, const uint32_t &signFlag, RsTlvSecurityKeySet& grpKeySet);
/*!
* Attempts to validate group signatures
@ -862,7 +863,7 @@ private:
std::vector<GroupDeletePublish> mGroupDeletePublish;
std::map<RsGxsId,std::set<RsPeerId> > mRoutingClues ;
std::list<std::pair<RsGxsMessageId,RsPeerId> > mTrackingClues ;
};
#endif // RSGENEXCHANGE_H

View file

@ -162,6 +162,7 @@ public:
*/
virtual bool getKey(const RsGxsId &id, RsTlvSecurityKey &key) = 0;
virtual bool getPrivateKey(const RsGxsId &id, RsTlvSecurityKey &key) = 0; // For signing outgoing messages.
virtual bool getIdDetails(const RsGxsId& id, RsIdentityDetails& details) = 0 ; // Proxy function so that we get p3Identity info from Gxs
};
class GixsReputation

View file

@ -33,7 +33,7 @@ RsGxsGrpMetaData::RsGxsGrpMetaData()
clear();
}
uint32_t RsGxsGrpMetaData::serial_size()
uint32_t RsGxsGrpMetaData::serial_size(uint32_t api_version)
{
uint32_t s = 8; // header size
@ -46,10 +46,15 @@ uint32_t RsGxsGrpMetaData::serial_size()
s += GetTlvStringSize(mServiceString);
s += signSet.TlvSize();
s += keys.TlvSize();
s += 4; // for mCircleType
s += 4; // for mCircleType
s += mCircleId.serial_size();
s += 4; // mAuthenFlag
s += mParentGrpId.serial_size();
s += 4; // mAuthenFlag
s += mParentGrpId.serial_size(); // mParentGroupId
if(api_version == RS_GXS_GRP_META_DATA_VERSION_ID_0002)
s += 4; // mSignFlag
else if(api_version != RS_GXS_GRP_META_DATA_VERSION_ID_0001)
std::cerr << "(EE) wrong/unknown API version " << api_version << " requested in RsGxsGrpMetaData::serial_size()" << std::endl;
return s;
}
@ -91,10 +96,9 @@ void RsGxsGrpMetaData::clear(){
mHash.clear() ;
}
bool RsGxsGrpMetaData::serialise(void *data, uint32_t &pktsize)
bool RsGxsGrpMetaData::serialise(void *data, uint32_t &pktsize,uint32_t api_version)
{
uint32_t tlvsize = serial_size() ;
uint32_t tlvsize = serial_size(api_version) ;
uint32_t offset = 0;
if (pktsize < tlvsize)
@ -104,7 +108,7 @@ bool RsGxsGrpMetaData::serialise(void *data, uint32_t &pktsize)
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, 0, tlvsize);
ok &= setRsItemHeader(data, tlvsize, api_version, tlvsize);
#ifdef GXS_DEBUG
std::cerr << "RsGxsGrpMetaData serialise()" << std::endl;
@ -130,6 +134,8 @@ bool RsGxsGrpMetaData::serialise(void *data, uint32_t &pktsize)
ok &= signSet.SetTlv(data, tlvsize, &offset);
ok &= keys.SetTlv(data, tlvsize, &offset);
if(api_version == RS_GXS_GRP_META_DATA_VERSION_ID_0002)
ok &= setRawUInt32(data, tlvsize, &offset, mSignFlags); // new in API v2. Was previously missing. Kept in the end for backward compatibility
return ok;
}
@ -154,12 +160,31 @@ bool RsGxsGrpMetaData::deserialise(void *data, uint32_t &pktsize)
ok &= getRawUInt32(data, pktsize, &offset, &mPublishTs);
ok &= getRawUInt32(data, pktsize, &offset, &mCircleType);
ok &= getRawUInt32(data, pktsize, &offset, &mAuthenFlags);
ok &= mAuthorId.deserialise(data, pktsize, offset);
ok &= GetTlvString(data, pktsize, &offset, 0, mServiceString);
ok &= mCircleId.deserialise(data, pktsize, offset);
ok &= signSet.GetTlv(data, pktsize, &offset);
ok &= keys.GetTlv(data, pktsize, &offset);
switch(getRsItemId(data))
{
case RS_GXS_GRP_META_DATA_VERSION_ID_0002: ok &= getRawUInt32(data, pktsize, &offset, &mSignFlags); // current API
break ;
case RS_GXS_GRP_META_DATA_VERSION_ID_0001: mSignFlags = 0; // old API. Do not leave this uninitialised!
break ;
default:
std::cerr << "(EE) RsGxsGrpMetaData::deserialise(): ERROR: unknown API version " << std::hex << getRsItemId(data) << std::dec << std::endl;
}
if(offset != pktsize)
{
std::cerr << "(EE) RsGxsGrpMetaData::deserialise(): ERROR: unmatched size " << offset << ", expected: " << pktsize << std::dec << std::endl;
return false ;
}
#ifdef DROP_NON_CANONICAL_ITEMS
if(mGroupName.length() > RsGxsGrpMetaData::MAX_ALLOWED_STRING_SIZE)
{
@ -202,8 +227,8 @@ uint32_t RsGxsMsgMetaData::serial_size()
s += signSet.TlvSize();
s += GetTlvStringSize(mMsgName);
s += 4;
s += 4;
s += 4; // mPublishTS
s += 4; // mMsgFlags
return s;
}
@ -239,7 +264,7 @@ bool RsGxsMsgMetaData::serialise(void *data, uint32_t *size)
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, 0, tlvsize);
ok &= setRsItemHeader(data, tlvsize, RS_GXS_MSG_META_DATA_VERSION_ID_0002, tlvsize);
#ifdef GXS_DEBUG
std::cerr << "RsGxsGrpMetaData serialise()" << std::endl;

View file

@ -37,6 +37,13 @@
class RsGroupMetaData;
class RsMsgMetaData;
static const uint32_t RS_GXS_GRP_META_DATA_VERSION_ID_0001 = 0x0000 ; // change this, and keep old values if the content changes
static const uint32_t RS_GXS_GRP_META_DATA_VERSION_ID_0002 = 0xaf01 ; // current API
static const uint32_t RS_GXS_MSG_META_DATA_VERSION_ID_0002 = 0x0000 ; // current API
static const uint32_t RS_GXS_GRP_META_DATA_CURRENT_API_VERSION = RS_GXS_GRP_META_DATA_VERSION_ID_0002;
class RsGxsGrpMetaData
{
public:
@ -44,15 +51,15 @@ public:
RsGxsGrpMetaData();
bool deserialise(void *data, uint32_t &pktsize);
bool serialise(void* data, uint32_t &pktsize);
uint32_t serial_size();
bool serialise(void* data, uint32_t &pktsize, uint32_t api_version);
uint32_t serial_size(uint32_t api_version);
void clear();
void operator =(const RsGroupMetaData& rMeta);
RsGxsGroupId mGroupId;
RsGxsGroupId mOrigGrpId;
std::string mGroupName;
uint32_t mGroupFlags;
uint32_t mGroupFlags; // GXS_SERV::FLAG_PRIVACY_RESTRICTED | GXS_SERV::FLAG_PRIVACY_PRIVATE | GXS_SERV::FLAG_PRIVACY_PUBLIC
uint32_t mPublishTs;
uint32_t mSignFlags;
RsGxsId mAuthorId;
@ -123,7 +130,6 @@ public:
uint32_t recvTS;
RsFileHash mHash;
bool validated;
};

View file

@ -448,7 +448,7 @@ bool RsGxsDataAccess::getGroupData(const uint32_t& token, std::list<RsNxsGrp*>&
if(gmreq)
{
grpData = gmreq->mGroupData;
grpData.swap(gmreq->mGroupData);
gmreq->mGroupData.clear();
locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE);
}else{
@ -480,7 +480,7 @@ bool RsGxsDataAccess::getMsgData(const uint32_t& token, NxsMsgDataResult& msgDat
if(mdreq)
{
msgData = mdreq->mMsgData;
msgData.swap(mdreq->mMsgData);
mdreq->mMsgData.clear();
locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE);
}
@ -517,7 +517,7 @@ bool RsGxsDataAccess::getMsgRelatedData(const uint32_t &token, NxsMsgRelatedData
if(mrireq)
{
msgData = mrireq->mMsgDataResult;
msgData.swap(mrireq->mMsgDataResult);
mrireq->mMsgDataResult.clear();
locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE);
}
@ -551,7 +551,7 @@ bool RsGxsDataAccess::getMsgSummary(const uint32_t& token, GxsMsgMetaResult& msg
if(mmreq)
{
msgInfo = mmreq->mMsgMetaData;
msgInfo.swap(mmreq->mMsgMetaData);
mmreq->mMsgMetaData.clear();
locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE);
@ -591,7 +591,7 @@ bool RsGxsDataAccess::getMsgRelatedSummary(const uint32_t &token, MsgRelatedMeta
if(mrireq)
{
msgMeta = mrireq->mMsgMetaResult;
msgMeta.swap(mrireq->mMsgMetaResult);
mrireq->mMsgMetaResult.clear();
locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE);
}
@ -630,7 +630,7 @@ bool RsGxsDataAccess::getMsgRelatedList(const uint32_t &token, MsgRelatedIdResul
if(mrireq)
{
msgIds = mrireq->mMsgIdResult;
msgIds.swap(mrireq->mMsgIdResult);
mrireq->mMsgIdResult.clear();
locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE);
}
@ -664,7 +664,7 @@ bool RsGxsDataAccess::getMsgList(const uint32_t& token, GxsMsgIdResult& msgIds)
if(mireq)
{
msgIds = mireq->mMsgIdResult;
msgIds.swap(mireq->mMsgIdResult);
mireq->mMsgIdResult.clear();
locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE);
}
@ -697,7 +697,7 @@ bool RsGxsDataAccess::getGroupList(const uint32_t& token, std::list<RsGxsGroupId
if(gireq)
{
groupIds = gireq->mGroupIdResult;
groupIds.swap(gireq->mGroupIdResult);
gireq->mGroupIdResult.clear();
locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE);
@ -1032,31 +1032,25 @@ bool RsGxsDataAccess::getGroupList(const std::list<RsGxsGroupId>& grpIdsIn, cons
bool RsGxsDataAccess::getMsgData(MsgDataReq* req)
{
GxsMsgResult result;
GxsMsgReq msgIdOut;
// filter based on options
getMsgList(req->mMsgIds, req->Options, msgIdOut);
mDataStore->retrieveNxsMsgs(msgIdOut, result, true, true);
mDataStore->retrieveNxsMsgs(msgIdOut, req->mMsgData, true, true);
req->mMsgData = result;
return true;
}
bool RsGxsDataAccess::getMsgSummary(MsgMetaReq* req)
{
GxsMsgMetaResult result;
GxsMsgReq msgIdOut;
// filter based on options
getMsgList(req->mMsgIds, req->Options, msgIdOut);
mDataStore->retrieveGxsMsgMetaData(msgIdOut, result);
req->mMsgMetaData = result;
mDataStore->retrieveGxsMsgMetaData(msgIdOut, req->mMsgMetaData);
return true;
}
@ -1558,7 +1552,7 @@ bool RsGxsDataAccess::getGroupStatistic(GroupStatisticRequest *req)
req->mGroupStatistic.mNumChildMsgsNew = 0;
req->mGroupStatistic.mNumChildMsgsUnread = 0;
for(int i = 0; i < msgMetaV.size(); ++i)
for(uint32_t i = 0; i < msgMetaV.size(); ++i)
{
RsGxsMsgMetaData* m = msgMetaV[i];
req->mGroupStatistic.mTotalSizeOfMsgs += m->mMsgSize + m->serial_size();
@ -1609,7 +1603,7 @@ bool RsGxsDataAccess::getServiceStatistic(ServiceStatisticRequest *req)
for(; mit != grpMeta.end(); ++mit)
{
RsGxsGrpMetaData* m = mit->second;
req->mServiceStatistic.mSizeOfGrps += m->mGrpSize + m->serial_size();
req->mServiceStatistic.mSizeOfGrps += m->mGrpSize + m->serial_size(RS_GXS_GRP_META_DATA_CURRENT_API_VERSION);
if (IS_GROUP_SUBSCRIBED(m->mSubscribeFlags))
{

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -99,7 +99,7 @@ bool MsgRespPending::accepted()
GixsReputation rep;
if(getAuthorRep(rep, entry.mAuthorId, mPeerId))
{
if(rep.score > mCutOff)
if(rep.score >= mCutOff)
{
entry.mPassedVetting = true;
count++;
@ -134,7 +134,7 @@ bool GrpRespPending::accepted()
if(getAuthorRep(rep, entry.mAuthorId, mPeerId))
{
if(rep.score > mCutOff)
if(rep.score >= mCutOff)
{
entry.mPassedVetting = true;
count++;

View file

@ -27,8 +27,13 @@
#include "rsgxsutil.h"
#include "retroshare/rsgxsflags.h"
#include "retroshare/rspeers.h"
#include "pqi/pqihash.h"
#include "gxs/rsgixs.h"
static const uint32_t MAX_GXS_IDS_REQUESTS_NET = 10 ; // max number of requests from cache/net (avoids killing the system!)
//#define GXSUTIL_DEBUG 1
RsGxsMessageCleanUp::RsGxsMessageCleanUp(RsGeneralDataService* const dataService, uint32_t messageStorePeriod, uint32_t chunkSize)
: mDs(dataService), MESSAGE_STORE_PERIOD(messageStorePeriod), CHUNK_SIZE(chunkSize)
@ -106,9 +111,8 @@ bool RsGxsMessageCleanUp::clean()
return mGrpMeta.empty();
}
RsGxsIntegrityCheck::RsGxsIntegrityCheck(
RsGeneralDataService* const dataService) :
mDs(dataService), mDone(false), mIntegrityMutex("integrity")
RsGxsIntegrityCheck::RsGxsIntegrityCheck(RsGeneralDataService* const dataService, RsGixs *gixs) :
mDs(dataService), mDone(false), mIntegrityMutex("integrity"),mGixs(gixs)
{ }
void RsGxsIntegrityCheck::run()
@ -118,122 +122,206 @@ void RsGxsIntegrityCheck::run()
bool RsGxsIntegrityCheck::check()
{
// first take out all the groups
std::map<RsGxsGroupId, RsNxsGrp*> grp;
mDs->retrieveNxsGrps(grp, true, true);
std::vector<RsGxsGroupId> grpsToDel;
GxsMsgReq msgIds;
GxsMsgReq grps;
// first take out all the groups
std::map<RsGxsGroupId, RsNxsGrp*> grp;
mDs->retrieveNxsGrps(grp, true, true);
std::vector<RsGxsGroupId> grpsToDel;
GxsMsgReq msgIds;
GxsMsgReq grps;
std::set<RsGxsId> used_gxs_ids ;
std::set<RsGxsGroupId> subscribed_groups ;
// compute hash and compare to stored value, if it fails then simply add it
// to list
std::map<RsGxsGroupId, RsNxsGrp*>::iterator git = grp.begin();
for(; git != grp.end(); ++git)
{
RsNxsGrp* grp = git->second;
RsFileHash currHash;
pqihash pHash;
pHash.addData(grp->grp.bin_data, grp->grp.bin_len);
pHash.Complete(currHash);
// compute hash and compare to stored value, if it fails then simply add it
// to list
std::map<RsGxsGroupId, RsNxsGrp*>::iterator git = grp.begin();
for(; git != grp.end(); ++git)
{
RsNxsGrp* grp = git->second;
RsFileHash currHash;
pqihash pHash;
pHash.addData(grp->grp.bin_data, grp->grp.bin_len);
pHash.Complete(currHash);
if(currHash == grp->metaData->mHash)
{
// get all message ids of group
if (mDs->retrieveMsgIds(grp->grpId, msgIds[grp->grpId]) == 1)
{
// store the group for retrieveNxsMsgs
grps[grp->grpId];
}
else
{
msgIds.erase(msgIds.find(grp->grpId));
// grpsToDel.push_back(grp->grpId);
}
}
else
{
grpsToDel.push_back(grp->grpId);
}
delete grp;
}
if(currHash == grp->metaData->mHash)
{
// get all message ids of group
if (mDs->retrieveMsgIds(grp->grpId, msgIds[grp->grpId]) == 1)
{
// store the group for retrieveNxsMsgs
grps[grp->grpId];
mDs->removeGroups(grpsToDel);
if(grp->metaData->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED)
{
subscribed_groups.insert(git->first) ;
// now messages
GxsMsgReq msgsToDel;
GxsMsgResult msgs;
if(!grp->metaData->mAuthorId.isNull())
{
#ifdef GXSUTIL_DEBUG
std::cerr << "TimeStamping group authors' key ID " << grp->metaData->mAuthorId << " in group ID " << grp->grpId << std::endl;
#endif
mDs->retrieveNxsMsgs(grps, msgs, false, true);
used_gxs_ids.insert(grp->metaData->mAuthorId) ;
}
}
}
else
{
msgIds.erase(msgIds.find(grp->grpId));
// grpsToDel.push_back(grp->grpId);
}
// check msg ids and messages
GxsMsgReq::iterator msgIdsIt;
for (msgIdsIt = msgIds.begin(); msgIdsIt != msgIds.end(); ++msgIdsIt)
{
const RsGxsGroupId& grpId = msgIdsIt->first;
std::vector<RsGxsMessageId> &msgIdV = msgIdsIt->second;
}
else
{
grpsToDel.push_back(grp->grpId);
}
delete grp;
}
std::vector<RsGxsMessageId>::iterator msgIdIt;
for (msgIdIt = msgIdV.begin(); msgIdIt != msgIdV.end(); ++msgIdIt)
{
const RsGxsMessageId& msgId = *msgIdIt;
std::vector<RsNxsMsg*> &nxsMsgV = msgs[grpId];
mDs->removeGroups(grpsToDel);
std::vector<RsNxsMsg*>::iterator nxsMsgIt;
for (nxsMsgIt = nxsMsgV.begin(); nxsMsgIt != nxsMsgV.end(); ++nxsMsgIt)
{
RsNxsMsg *nxsMsg = *nxsMsgIt;
if (nxsMsg && msgId == nxsMsg->msgId)
{
break;
}
}
// now messages
GxsMsgReq msgsToDel;
GxsMsgResult msgs;
if (nxsMsgIt == nxsMsgV.end())
{
msgsToDel[grpId].push_back(msgId);
}
}
}
mDs->retrieveNxsMsgs(grps, msgs, false, true);
GxsMsgResult::iterator mit = msgs.begin();
// check msg ids and messages
GxsMsgReq::iterator msgIdsIt;
for (msgIdsIt = msgIds.begin(); msgIdsIt != msgIds.end(); ++msgIdsIt)
{
const RsGxsGroupId& grpId = msgIdsIt->first;
std::vector<RsGxsMessageId> &msgIdV = msgIdsIt->second;
for(; mit != msgs.end(); ++mit)
{
std::vector<RsNxsMsg*>& msgV = mit->second;
std::vector<RsNxsMsg*>::iterator vit = msgV.begin();
std::vector<RsGxsMessageId>::iterator msgIdIt;
for (msgIdIt = msgIdV.begin(); msgIdIt != msgIdV.end(); ++msgIdIt)
{
const RsGxsMessageId& msgId = *msgIdIt;
std::vector<RsNxsMsg*> &nxsMsgV = msgs[grpId];
for(; vit != msgV.end(); ++vit)
{
RsNxsMsg* msg = *vit;
RsFileHash currHash;
pqihash pHash;
pHash.addData(msg->msg.bin_data, msg->msg.bin_len);
pHash.Complete(currHash);
std::vector<RsNxsMsg*>::iterator nxsMsgIt;
for (nxsMsgIt = nxsMsgV.begin(); nxsMsgIt != nxsMsgV.end(); ++nxsMsgIt)
{
RsNxsMsg *nxsMsg = *nxsMsgIt;
if (nxsMsg && msgId == nxsMsg->msgId)
{
break;
}
}
if(msg->metaData == NULL || currHash != msg->metaData->mHash)
{
std::cerr << "(EE) deleting message data with wrong hash or null meta data. meta=" << (void*)msg->metaData << std::endl;
msgsToDel[msg->grpId].push_back(msg->msgId);
}
if (nxsMsgIt == nxsMsgV.end())
{
msgsToDel[grpId].push_back(msgId);
}
}
}
delete msg;
}
}
GxsMsgResult::iterator mit = msgs.begin();
mDs->removeMsgs(msgsToDel);
for(; mit != msgs.end(); ++mit)
{
std::vector<RsNxsMsg*>& msgV = mit->second;
std::vector<RsNxsMsg*>::iterator vit = msgV.begin();
RsStackMutex stack(mIntegrityMutex);
mDone = true;
for(; vit != msgV.end(); ++vit)
{
RsNxsMsg* msg = *vit;
RsFileHash currHash;
pqihash pHash;
pHash.addData(msg->msg.bin_data, msg->msg.bin_len);
pHash.Complete(currHash);
std::vector<RsGxsGroupId>::iterator grpIt;
for(grpIt = grpsToDel.begin(); grpIt != grpsToDel.end(); ++grpIt)
{
mDeletedGrps.push_back(*grpIt);
}
mDeletedMsgs = msgsToDel;
if(msg->metaData == NULL || currHash != msg->metaData->mHash)
{
std::cerr << "(EE) deleting message data with wrong hash or null meta data. meta=" << (void*)msg->metaData << std::endl;
msgsToDel[msg->grpId].push_back(msg->msgId);
}
else if(!msg->metaData->mAuthorId.isNull() && subscribed_groups.find(msg->metaData->mGroupId)!=subscribed_groups.end())
{
#ifdef GXSUTIL_DEBUG
std::cerr << "TimeStamping message authors' key ID " << msg->metaData->mAuthorId << " in message " << msg->msgId << ", group ID " << msg->grpId<< std::endl;
#endif
used_gxs_ids.insert(msg->metaData->mAuthorId) ;
}
return true;
delete msg;
}
}
mDs->removeMsgs(msgsToDel);
RsStackMutex stack(mIntegrityMutex);
mDone = true;
std::vector<RsGxsGroupId>::iterator grpIt;
for(grpIt = grpsToDel.begin(); grpIt != grpsToDel.end(); ++grpIt)
{
mDeletedGrps.push_back(*grpIt);
}
mDeletedMsgs = msgsToDel;
#ifdef GXSUTIL_DEBUG
std::cerr << "At end of pass, this is the list used GXS ids: " << std::endl;
std::cerr << " requesting them to GXS identity service to enforce loading." << std::endl;
#endif
std::list<RsPeerId> connected_friends ;
rsPeers->getOnlineList(connected_friends) ;
std::vector<RsGxsId> gxs_ids ;
for(std::set<RsGxsId>::const_iterator it(used_gxs_ids.begin());it!=used_gxs_ids.end();++it)
{
gxs_ids.push_back(*it) ;
#ifdef GXSUTIL_DEBUG
std::cerr << " " << *it << std::endl;
#endif
}
int nb_requested_not_in_cache = 0;
#ifdef GXSUTIL_DEBUG
std::cerr << " issuing random get on friends for non existing IDs" << std::endl;
#endif
// now request a cache update for them, which triggers downloading from friends, if missing.
for(;nb_requested_not_in_cache<MAX_GXS_IDS_REQUESTS_NET && gxs_ids.size()>0;)
{
uint32_t n = RSRandom::random_u32() % gxs_ids.size() ;
#ifdef GXSUTIL_DEBUG
std::cerr << " requesting ID " << gxs_ids[n] ;
#endif
if(!mGixs->haveKey(gxs_ids[n])) // checks if we have it already in the cache (conservative way to ensure that we atually have it)
{
mGixs->requestKey(gxs_ids[n],connected_friends);
++nb_requested_not_in_cache ;
#ifdef GXSUTIL_DEBUG
std::cerr << " ... from cache/net" << std::endl;
#endif
}
else
{
#ifdef GXSUTIL_DEBUG
std::cerr << " ... already in cache" << std::endl;
#endif
// Note: we could time_stamp even in the case where the id is not cached. Anyway, it's not really a problem here, since IDs have a high chance of
// behing eventually stamped.
mGixs->timeStampKey(gxs_ids[n]) ;
}
gxs_ids[n] = gxs_ids[gxs_ids.size()-1] ;
gxs_ids.pop_back() ;
}
#ifdef GXSUTIL_DEBUG
std::cerr << " total actual cache requests: "<< nb_requested_not_in_cache << std::endl;
#endif
return true;
}
bool RsGxsIntegrityCheck::isDone()

View file

@ -30,6 +30,8 @@
#include "serialiser/rsnxsitems.h"
#include "rsgds.h"
class RsGixs ;
/*!
* Handy function for cleaning out meta result containers
* @param container
@ -40,10 +42,9 @@ void freeAndClearContainerResource(Container container)
typename Container::iterator meta_it = container.begin();
for(; meta_it != container.end(); ++meta_it)
{
delete meta_it->second;
if(meta_it->second != NULL)
delete meta_it->second;
}
container.clear();
}
@ -112,7 +113,7 @@ public:
* @param chunkSize
* @param sleepPeriod
*/
RsGxsIntegrityCheck(RsGeneralDataService* const dataService);
RsGxsIntegrityCheck(RsGeneralDataService* const dataService, RsGixs *gixs);
bool check();
@ -129,6 +130,8 @@ private:
RsMutex mIntegrityMutex;
std::list<RsGxsGroupId> mDeletedGrps;
std::map<RsGxsGroupId, std::vector<RsGxsMessageId> > mDeletedMsgs;
RsGixs *mGixs ;
};
class GroupUpdate

View file

@ -127,6 +127,13 @@ public:
*/
virtual int sharePublishKey(const RsGxsGroupId& grpId,const std::set<RsPeerId>& peers)=0 ;
/*!
* \brief rejectMessage
* Tells the network exchange service to not download this message again, at least for some time (maybe 24h or more)
* in order to avoid cluttering the network pipe with copied of this rejected message.
* \param msgId
*/
virtual void rejectMessage(const RsGxsMessageId& msgId) =0;
};
#endif // RSGNP_H

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,261 @@
/*
* libretroshare/src/chat: distantchat.h
*
* Services for RetroShare.
*
* Copyright 2015 by Cyril Soler
*
* 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 "csoler@users.sourceforge.net".
*
*/
#pragma once
// Generic tunnel service
//
// Preconditions:
// * the secured tunnel service takes care of:
// - tunnel health: tunnels are kept alive using special items, re-openned when necessary, etc.
// - transport: items are ACK-ed and re-sent if never received
// - encryption: items are all encrypted and authenticated using PFS(DH)+HMAC(sha1)+AES(128)
// * each tunnel is associated to a specific GXS id on both sides. Consequently, services that request tunnels from different IDs to a
// server for the same GXS id need to be handled correctly.
// * client services must register to the secured tunnel service if they want to use it.
// * multiple services can use the same tunnel. Items contain a service Id that is obtained when registering to the secured tunnel service.
//
// GUI
// * the GUI should show for each tunnel:
// - starting and ending GXS ids
// - tunnel status (DH ok, closed from distant peer, locally closed, etc)
// - amount of data that is transferred in the tunnel
// - number of pending items (and total size)
// - number ACKed items both ways.
//
// We can use an additional tab "Authenticated tunnels" in the statistics->turtle window for that purpose.
//
// Interaction with services:
//
// Services request tunnels from a given GXS id and to a given GXS id. When ready, they get a handle (type = RsGxsTunnelId)
//
// Services send data in the tunnel using the virtual peer id
//
// Data is send to a service ID (could be any existing service ID). The endpoint of the tunnel must register each service, in order to
// allow the data to be transmitted/sent from/to that service. Otherwise an error is issued.
//
// Encryption
// * the whole tunnel traffic is encrypted using AES-128 with random IV
// * a random key is established using DH key exchange for each connection (establishment of a new virtual peer)
// * encrypted items are authenticated with HMAC(sha1).
// * DH public keys are the only chunks of data that travel un-encrypted along the tunnel. They are
// signed to avoid any MITM interactions. No time-stamp is used in DH exchange since a replay attack would not work.
//
// Algorithms
//
// * we need two layers: the turtle layer, and the GXS id layer.
// - for each pair of GXS ids talking, a single turtle tunnel is used
// - that tunnel can be shared by multiple services using it.
// - services are responsoble for asking tunnels and also droppping them when unused.
// - at the turtle layer, the tunnel will be effectively closed only when no service uses it.
// * IDs
// TurtleVirtualPeerId:
// - Used by tunnel service for each turtle tunnel
// - one virtual peer ID per GXS tunnel
//
// GxsTunnelId:
// - one GxsTunnelId per couple of GXS ids. But we also need to allow multiple services to use the tunnel.
//
// * at the turtle layer:
// - accept virtual peers from turtle tunnel service. The hash for that VP only depends on the server GXS id at server side, which is our
// own ID at server side, and destination ID at client side. What happens if two different clients request to talk to the same GXS id? (same hash)
// They should use different virtual peers, so it should be ok.
//
// Turtle hash: [ 0 ---------------15 16---19 ]
// Destination Random
//
// We Use 16 bytes to target the exact destination of the hash. The source part is just 4 arbitrary bytes that need to be different for all source
// IDs that come from the same peer, which is quite likely to be sufficient. The real source of the tunnel will make itself known when sending the
// DH key.
//
// * at the GXS layer
// - we should be able to have as many tunnels as they are different couples of GXS ids to interact. That means the tunnel should be determined
// by a mix between our own GXS id and the GXS id we're talking to. That is what the TunnelVirtualPeer is.
//
//
// RequestTunnel(source_own_id,destination_id) -
// | |
// +---------------------------> p3Turtle::monitorTunnels( hash(destination_id) ) |
// | |
// [Turtle async work] -------------------+ | Turtle layer: one virtual peer id
// | | |
// handleTunnelRequest() <-----------------------------------------------+ | |
// | | |
// +---------------- keep record in _gxs_tunnel_virtual_peer_id, initiate DH exchange | -
// | |
// handleDHPublicKey() <-----------------------------------------------------------------------------+ |
// | |
// +---------------- update _gxs_tunnel_contacts[ tunnel_hash = hash(own_id, destination_id) ] | GxsTunnelId level
// | |
// +---------------- notify client service that Peer(destination_id, tunnel_hash) is ready to talk to |
// -
#include <turtle/turtleclientservice.h>
#include <retroshare/rsgxstunnel.h>
#include <services/p3service.h>
#include <gxstunnel/rsgxstunnelitems.h>
class RsGixs ;
static const uint32_t GXS_TUNNEL_AES_KEY_SIZE = 16 ;
class p3GxsTunnelService: public RsGxsTunnelService, public RsTurtleClientService, public p3Service
{
public:
p3GxsTunnelService(RsGixs *pids) ;
virtual void connectToTurtleRouter(p3turtle *) ;
// Creates the invite if the public key of the distant peer is available.
// Om success, stores the invite in the map above, so that we can respond to tunnel requests.
//
virtual bool requestSecuredTunnel(const RsGxsId& to_id,const RsGxsId& from_id,RsGxsTunnelId& tunnel_id,uint32_t service_id,uint32_t& error_code) ;
virtual bool closeExistingTunnel(const RsGxsTunnelId &tunnel_id,uint32_t service_id) ;
virtual bool getTunnelsInfo(std::vector<GxsTunnelInfo>& infos);
virtual bool getTunnelInfo(const RsGxsTunnelId& tunnel_id,GxsTunnelInfo& info);
virtual bool sendData(const RsGxsTunnelId& tunnel_id,uint32_t service_id,const uint8_t *data,uint32_t size) ;
virtual bool registerClientService(uint32_t service_id,RsGxsTunnelClientService *service) ;
// derived from p3service
virtual int tick();
virtual RsServiceInfo getServiceInfo();
private:
void flush() ;
virtual void handleIncomingItem(const RsGxsTunnelId &tunnel_id, RsGxsTunnelItem *) ;
class GxsTunnelPeerInfo
{
public:
GxsTunnelPeerInfo() : last_contact(0), last_keep_alive_sent(0), status(0), direction(0)
{
memset(aes_key, 0, GXS_TUNNEL_AES_KEY_SIZE);
total_sent = 0 ;
total_received = 0 ;
}
time_t last_contact ; // used to keep track of working connexion
time_t last_keep_alive_sent ; // last time we sent a keep alive packet.
unsigned char aes_key[GXS_TUNNEL_AES_KEY_SIZE] ;
uint32_t status ; // info: do we have a tunnel ?
RsPeerId virtual_peer_id; // given by the turtle router. Identifies the tunnel.
RsGxsId to_gxs_id; // gxs id we're talking to
RsGxsId own_gxs_id ; // gxs id we're using to talk.
RsTurtleGenericTunnelItem::Direction direction ; // specifiec wether we are client(managing the tunnel) or server.
TurtleFileHash hash ; // hash that is last used. This is necessary for handling tunnel establishment
std::set<uint32_t> client_services ;// services that used this tunnel
uint32_t total_sent ;
uint32_t total_received ;
};
class GxsTunnelDHInfo
{
public:
GxsTunnelDHInfo() : dh(0), direction(0), status(0) {}
DH *dh ;
RsGxsId gxs_id ;
RsGxsId own_gxs_id ;
RsGxsTunnelId tunnel_id ; // this is a proxy, since we cna always recompute that from the two previous values.
RsTurtleGenericTunnelItem::Direction direction ;
uint32_t status ;
TurtleFileHash hash ;
};
struct GxsTunnelData
{
RsGxsTunnelDataItem *data_item ;
time_t last_sending_attempt ;
};
// This maps contains the current peers to talk to with distant chat.
//
std::map<RsGxsTunnelId,GxsTunnelPeerInfo> _gxs_tunnel_contacts ; // current peers we can talk to
std::map<TurtleVirtualPeerId,GxsTunnelDHInfo> _gxs_tunnel_virtual_peer_ids ; // current virtual peers. Used to figure out tunnels, etc.
// List of items to be sent asap. Used to store items that we cannot pass directly to
// sendTurtleData(), because of Mutex protection.
std::map<uint64_t,GxsTunnelData> pendingGxsTunnelDataItems ; // items that need provable transport and encryption
std::list<RsGxsTunnelItem*> pendingGxsTunnelItems ; // items that do not need provable transport, yet need encryption
std::list<RsGxsTunnelDHPublicKeyItem*> pendingDHItems ;
// Overloaded from RsTurtleClientService
virtual bool handleTunnelRequest(const RsFileHash &hash,const RsPeerId& peer_id) ;
virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const RsFileHash& hash,const RsPeerId& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ;
void addVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&,RsTurtleGenericTunnelItem::Direction dir) ;
void removeVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ;
// session handling handles
void startClientGxsTunnelConnection(const RsGxsId &to_gxs_id, const RsGxsId& from_gxs_id, uint32_t service_id, RsGxsTunnelId &tunnel_id) ;
void locked_restartDHSession(const RsPeerId &virtual_peer_id, const RsGxsId &own_gxs_id) ;
// utility functions
static TurtleFileHash randomHashFromDestinationGxsId(const RsGxsId& destination) ;
static RsGxsId destinationGxsIdFromHash(const TurtleFileHash& sum) ;
// Cryptography management
void handleRecvDHPublicKey(RsGxsTunnelDHPublicKeyItem *item) ;
bool locked_sendDHPublicKey(const DH *dh, const RsGxsId& own_gxs_id, const RsPeerId& virtual_peer_id) ;
bool locked_initDHSessionKey(DH *&dh);
TurtleVirtualPeerId virtualPeerIdFromHash(const TurtleFileHash& hash) ; // ... and to a hash for p3turtle
RsGxsTunnelId makeGxsTunnelId(const RsGxsId &own_id, const RsGxsId &distant_id) const; // creates a unique ID from two GXS ids.
// item handling
void handleRecvStatusItem(const RsGxsTunnelId& id,RsGxsTunnelStatusItem *item) ;
void handleRecvTunnelDataItem(const RsGxsTunnelId& id,RsGxsTunnelDataItem *item) ;
void handleRecvTunnelDataAckItem(const RsGxsTunnelId &id, RsGxsTunnelDataAckItem *item);
// Comunication with Turtle service
bool locked_sendEncryptedTunnelData(RsGxsTunnelItem *item) ;
bool locked_sendClearTunnelData(RsGxsTunnelDHPublicKeyItem *item); // this limits the usage to DH items. Others should be encrypted!
bool handleEncryptedData(const uint8_t *data_bytes,uint32_t data_size,const TurtleFileHash& hash,const RsPeerId& virtual_peer_id) ;
// local data
p3turtle *mTurtle ;
RsGixs *mGixs ;
RsMutex mGxsTunnelMtx ;
uint64_t global_item_counter ;
std::map<uint32_t,RsGxsTunnelClientService*> mRegisteredServices ;
void debug_dump();
};

View file

@ -0,0 +1,486 @@
/*
* libretroshare/src/serialiser: rsbaseitems.cc
*
* RetroShare Serialiser.
*
* Copyright 2007-2008 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 <stdexcept>
#include <time.h>
#include "serialiser/rsbaseserial.h"
#include "serialiser/rstlvbase.h"
#include "util/rsprint.h"
#include "gxstunnel/rsgxstunnelitems.h"
//#define GXS_TUNNEL_ITEM_DEBUG 1
std::ostream& RsGxsTunnelDHPublicKeyItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsGxsTunnelDHPublicKeyItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << " Signature Key ID: " << signature.keyId << std::endl ;
out << " Public Key ID: " << gxs_key.keyId << std::endl ;
printRsItemEnd(out, "RsGxsTunnelMsgItem", indent);
return out;
}
std::ostream& RsGxsTunnelDataItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsGxsTunnelDataItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << " message id : " << std::hex << unique_item_counter << std::dec << std::endl ;
out << " service id : " << std::hex << service_id << std::dec << std::endl ;
out << " flags : " << std::hex << flags << std::dec << std::endl ;
out << " size : " << data_size << std::endl ;
out << " data : " << RsUtil::BinToHex(data,std::min(50u,data_size)) << ((data_size>50u)?"...":"") << std::endl ;
printRsItemEnd(out, "RsGxsTunnelDataItem", indent);
return out;
}
std::ostream& RsGxsTunnelDataAckItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsGxsTunnelDataItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << " message id : " << std::hex << unique_item_counter << std::dec << std::endl ;
printRsItemEnd(out, "RsGxsTunnelDataAckItem", indent);
return out;
}
std::ostream& RsGxsTunnelStatusItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsGxsTunnelDataItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << " flags : " << std::hex << status << std::dec << std::endl ;
printRsItemEnd(out, "RsGxsTunnelStatusItem", indent);
return out;
}
/*************************************************************************/
RsGxsTunnelDHPublicKeyItem::~RsGxsTunnelDHPublicKeyItem()
{
BN_free(public_key) ;
}
/*************************************************************************/
RsItem *RsGxsTunnelSerialiser::deserialise(void *data, uint32_t *pktsize)
{
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
#ifdef GXS_TUNNEL_ITEM_DEBUG
std::cerr << "deserializing packet..."<< std::endl ;
#endif
// look what we have...
if (*pktsize < rssize) /* check size */
{
std::cerr << "GxsTunnel deserialisation: not enough size: pktsize=" << *pktsize << ", rssize=" << rssize << std::endl ;
return NULL; /* not enough data */
}
/* set the packet length */
*pktsize = rssize;
/* ready to load */
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_GXS_TUNNEL != getRsItemService(rstype)))
{
#ifdef GXS_TUNNEL_ITEM_DEBUG
std::cerr << "GxsTunnel deserialisation: wrong type !" << std::endl ;
#endif
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_GXS_TUNNEL_DH_PUBLIC_KEY: return deserialise_RsGxsTunnelDHPublicKeyItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_GXS_TUNNEL_DATA: return deserialise_RsGxsTunnelDataItem (data,*pktsize) ;
case RS_PKT_SUBTYPE_GXS_TUNNEL_DATA_ACK: return deserialise_RsGxsTunnelDataAckItem (data,*pktsize) ;
case RS_PKT_SUBTYPE_GXS_TUNNEL_STATUS: return deserialise_RsGxsTunnelStatusItem (data,*pktsize) ;
default:
std::cerr << "Unknown packet type in chat!" << std::endl ;
return NULL ;
}
}
/*************************************************************************/
uint32_t RsGxsTunnelDHPublicKeyItem::serial_size()
{
uint32_t s = 8 ; // header
s += 4 ; // BN size
s += BN_num_bytes(public_key) ; // public_key
s += signature.TlvSize() ; // signature
s += gxs_key.TlvSize() ; // gxs_key
return s ;
}
uint32_t RsGxsTunnelDataItem::serial_size()
{
uint32_t s = 8 ; // header
s += 8 ; // counter
s += 4 ; // flags
s += 4 ; // service id
s += 4 ; // data_size
s += data_size; // data
return s ;
}
uint32_t RsGxsTunnelDataAckItem::serial_size()
{
uint32_t s = 8 ; // header
s += 8 ; // counter
return s ;
}
uint32_t RsGxsTunnelStatusItem::serial_size()
{
uint32_t s = 8 ; // header
s += 4 ; // flags
return s ;
}
/*************************************************************************/
bool RsGxsTunnelDHPublicKeyItem::serialise(void *data,uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
/* skip the header */
offset += 8;
uint32_t s = BN_num_bytes(public_key) ;
ok &= setRawUInt32(data, tlvsize, &offset, s);
BN_bn2bin(public_key,&((unsigned char *)data)[offset]) ;
offset += s ;
ok &= signature.SetTlv(data, tlvsize, &offset);
ok &= gxs_key.SetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsGxsTunnelDHPublicKeyItem::serialiseItem() Size Error! offset=" << offset << ", tlvsize=" << tlvsize << std::endl;
}
return ok ;
}
bool RsGxsTunnelStatusItem::serialise(void *data, uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
#ifdef GXS_TUNNEL_ITEM_DEBUG
std::cerr << "RsGxsTunnelSerialiser serialising chat status item." << std::endl;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Header: " << ok << std::endl;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt32(data, tlvsize, &offset, status);
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size Error! " << std::endl;
}
#ifdef GXS_TUNNEL_ITEM_DEBUG
std::cerr << "computed size: " << 256*((unsigned char*)data)[6]+((unsigned char*)data)[7] << std::endl ;
#endif
return ok;
}
bool RsGxsTunnelDataItem::serialise(void *dt, uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(dt, tlvsize, PacketId(), tlvsize);
#ifdef GXS_TUNNEL_ITEM_DEBUG
std::cerr << "RsGxsTunnelSerialiser serialising chat status item." << std::endl;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Header: " << ok << std::endl;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt64(dt, tlvsize, &offset, unique_item_counter);
ok &= setRawUInt32(dt, tlvsize, &offset, flags);
ok &= setRawUInt32(dt, tlvsize, &offset, service_id);
ok &= setRawUInt32(dt, tlvsize, &offset, data_size);
if(offset + data_size <= tlvsize)
{
memcpy(&((uint8_t*)dt)[offset],data,data_size) ;
offset += data_size ;
}
else
ok = false ;
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size Error! " << std::endl;
}
return ok;
}
bool RsGxsTunnelDataAckItem::serialise(void *data, uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
#ifdef GXS_TUNNEL_ITEM_DEBUG
std::cerr << "RsGxsTunnelSerialiser serialising chat status item." << std::endl;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Header: " << ok << std::endl;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt64(data, tlvsize, &offset, unique_item_counter);
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size Error! " << std::endl;
}
return ok;
}
/*************************************************************************/
RsGxsTunnelDHPublicKeyItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelDHPublicKeyItem(void *data,uint32_t /*size*/)
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
RsGxsTunnelDHPublicKeyItem *item = new RsGxsTunnelDHPublicKeyItem() ;
uint32_t s=0 ;
/* get mandatory parts first */
ok &= getRawUInt32(data, rssize, &offset, &s);
item->public_key = BN_bin2bn(&((unsigned char *)data)[offset],s,NULL) ;
offset += s ;
ok &= item->signature.GetTlv(data, rssize, &offset) ;
ok &= item->gxs_key.GetTlv(data, rssize, &offset) ;
if (offset != rssize)
{
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Size error while deserializing." << std::endl ;
delete item ;
return NULL ;
}
if (!ok)
{
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Unknown error while deserializing." << std::endl ;
delete item ;
return NULL ;
}
return item ;
}
RsGxsTunnelDataItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelDataItem(void *dat,uint32_t size)
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(dat);
bool ok = true ;
RsGxsTunnelDataItem *item = new RsGxsTunnelDataItem();
/* get mandatory parts first */
ok &= getRawUInt64(dat, rssize, &offset, &item->unique_item_counter);
ok &= getRawUInt32(dat, rssize, &offset, &item->flags);
ok &= getRawUInt32(dat, rssize, &offset, &item->service_id);
ok &= getRawUInt32(dat, rssize, &offset, &item->data_size);
if(offset + item->data_size <= size)
{
item->data = (unsigned char*)malloc(item->data_size) ;
if(dat == NULL)
{
delete item ;
return NULL ;
}
memcpy(item->data,&((uint8_t*)dat)[offset],item->data_size) ;
offset += item->data_size ;
}
else
ok = false ;
if (offset != rssize)
{
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Size error while deserializing." << std::endl ;
delete item ;
return NULL ;
}
if (!ok)
{
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Unknown error while deserializing." << std::endl ;
delete item ;
return NULL ;
}
return item ;
}
RsGxsTunnelDataAckItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelDataAckItem(void *dat,uint32_t /* size */)
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(dat);
bool ok = true ;
RsGxsTunnelDataAckItem *item = new RsGxsTunnelDataAckItem();
/* get mandatory parts first */
ok &= getRawUInt64(dat, rssize, &offset, &item->unique_item_counter);
if (offset != rssize)
{
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Size error while deserializing." << std::endl ;
delete item ;
return NULL ;
}
if (!ok)
{
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Unknown error while deserializing." << std::endl ;
delete item ;
return NULL ;
}
return item ;
}
RsGxsTunnelStatusItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelStatusItem(void *dat,uint32_t size)
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(dat);
bool ok = true ;
RsGxsTunnelStatusItem *item = new RsGxsTunnelStatusItem();
/* get mandatory parts first */
ok &= getRawUInt32(dat, rssize, &offset, &item->status);
if (offset != rssize)
{
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Size error while deserializing." << std::endl ;
delete item ;
return NULL ;
}
if (!ok)
{
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Unknown error while deserializing." << std::endl ;
delete item ;
return NULL ;
}
return item ;
}

View file

@ -0,0 +1,177 @@
/*
* libretroshare/src/serialiser: rschatitems.h
*
* RetroShare Serialiser.
*
* Copyright 2007-2008 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".
*
*/
#pragma once
#include <openssl/ssl.h>
#include "retroshare/rstypes.h"
#include "serialiser/rstlvkeys.h"
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvidset.h"
#include "serialiser/rstlvfileitem.h"
/* chat Flags */
const uint32_t RS_GXS_TUNNEL_FLAG_CLOSING_DISTANT_CONNECTION = 0x0400;
const uint32_t RS_GXS_TUNNEL_FLAG_ACK_DISTANT_CONNECTION = 0x0800;
const uint32_t RS_GXS_TUNNEL_FLAG_KEEP_ALIVE = 0x1000;
const uint8_t RS_PKT_SUBTYPE_GXS_TUNNEL_DATA = 0x01 ;
const uint8_t RS_PKT_SUBTYPE_GXS_TUNNEL_DH_PUBLIC_KEY = 0x02 ;
const uint8_t RS_PKT_SUBTYPE_GXS_TUNNEL_STATUS = 0x03 ;
const uint8_t RS_PKT_SUBTYPE_GXS_TUNNEL_DATA_ACK = 0x04 ;
typedef uint64_t GxsTunnelDHSessionId ;
class RsGxsTunnelItem: public RsItem
{
public:
RsGxsTunnelItem(uint8_t item_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_GXS_TUNNEL,item_subtype)
{
setPriorityLevel(QOS_PRIORITY_RS_CHAT_ITEM) ;
}
virtual ~RsGxsTunnelItem() {}
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ;
virtual bool serialise(void *data,uint32_t& size) = 0 ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() = 0 ; // deserialise is handled using a constructor
};
/*!
* For sending distant communication data. The item is not encrypted after being serialised, but the data it.
* The MAC is computed over encrypted data using the PFS key. All other items (except DH keys) are serialised, encrypted, and
* sent as data in a RsGxsTunnelDataItem.
*
* @see p3GxsTunnelService
*/
class RsGxsTunnelDataItem: public RsGxsTunnelItem
{
public:
RsGxsTunnelDataItem() :RsGxsTunnelItem(RS_PKT_SUBTYPE_GXS_TUNNEL_DATA) { data=NULL ;data_size=0; }
RsGxsTunnelDataItem(uint8_t subtype) :RsGxsTunnelItem(subtype) { data=NULL ;data_size=0; }
virtual ~RsGxsTunnelDataItem() {}
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
uint64_t unique_item_counter; // this allows to make the item unique
uint32_t flags; // mainly NEEDS_HACK?
uint32_t service_id ;
uint32_t data_size ; // encrypted data size
unsigned char *data ; // encrypted data
};
// Used to send status of connection. This can be closing orders, flushing orders, etc.
// These items are always sent encrypted.
class RsGxsTunnelStatusItem: public RsGxsTunnelItem
{
public:
RsGxsTunnelStatusItem() :RsGxsTunnelItem(RS_PKT_SUBTYPE_GXS_TUNNEL_STATUS) {}
RsGxsTunnelStatusItem(void *data,uint32_t size) ; // deserialization
virtual ~RsGxsTunnelStatusItem() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
uint32_t status ;
};
// Used to confirm reception of an encrypted item.
class RsGxsTunnelDataAckItem: public RsGxsTunnelItem
{
public:
RsGxsTunnelDataAckItem() :RsGxsTunnelItem(RS_PKT_SUBTYPE_GXS_TUNNEL_DATA_ACK) {}
RsGxsTunnelDataAckItem(void *data,uint32_t size) ; // deserialization
virtual ~RsGxsTunnelDataAckItem() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
uint64_t unique_item_counter ; // unique identifier for that item
};
// This class contains the public Diffie-Hellman parameters to be sent
// when performing a DH agreement over a distant chat tunnel.
//
class RsGxsTunnelDHPublicKeyItem: public RsGxsTunnelItem
{
public:
RsGxsTunnelDHPublicKeyItem() :RsGxsTunnelItem(RS_PKT_SUBTYPE_GXS_TUNNEL_DH_PUBLIC_KEY) {}
RsGxsTunnelDHPublicKeyItem(void *data,uint32_t size) ; // deserialization
virtual ~RsGxsTunnelDHPublicKeyItem() ;
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
// Private data to DH public key item
//
BIGNUM *public_key ;
RsTlvKeySignature signature ; // signs the public key in a row.
RsTlvSecurityKey gxs_key ; // public key of the signer
private:
// make the object non copy-able
RsGxsTunnelDHPublicKeyItem(const RsGxsTunnelDHPublicKeyItem&) : RsGxsTunnelItem(RS_PKT_SUBTYPE_GXS_TUNNEL_DH_PUBLIC_KEY) {}
const RsGxsTunnelDHPublicKeyItem& operator=(const RsGxsTunnelDHPublicKeyItem&) { return *this ;}
};
class RsGxsTunnelSerialiser: public RsSerialType
{
public:
RsGxsTunnelSerialiser() :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_GXS_TUNNEL) {}
virtual uint32_t size (RsItem *item)
{
return static_cast<RsGxsTunnelItem *>(item)->serial_size() ;
}
virtual bool serialise(RsItem *item, void *data, uint32_t *size)
{
return static_cast<RsGxsTunnelItem *>(item)->serialise(data,*size) ;
}
RsItem *deserialise(void *data, uint32_t *pktsize);
private:
static RsGxsTunnelDataAckItem *deserialise_RsGxsTunnelDataAckItem (void *data, uint32_t size) ;
static RsGxsTunnelDataItem *deserialise_RsGxsTunnelDataItem (void *data, uint32_t size) ;
static RsGxsTunnelStatusItem *deserialise_RsGxsTunnelStatusItem (void *data, uint32_t size) ;
static RsGxsTunnelDHPublicKeyItem *deserialise_RsGxsTunnelDHPublicKeyItem(void *data, uint32_t size) ;
};

File diff suppressed because it is too large Load diff

View file

@ -125,7 +125,7 @@ std::string PGPKeyManagement::makeArmouredKey(const unsigned char *keydata,size_
uint32_t crc = compute24bitsCRC((unsigned char *)keydata,key_size) ;
unsigned char tmp[3] = { (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff } ;
unsigned char tmp[3] = { uint8_t((crc >> 16) & 0xff), uint8_t((crc >> 8) & 0xff), uint8_t(crc & 0xff) } ;
std::string crc_string ;
Radix64::encode((const char *)tmp,3,crc_string) ;

View file

@ -20,8 +20,8 @@ class RsCacheService: public CacheSource, public CacheStore, public p3Config
// Functions from p3config
//
virtual RsSerialiser *setupSerialiser() { return NULL ; }
virtual bool saveList(bool&, std::list<RsItem*>&) { return false ;}
virtual bool loadList(std::list<RsItem*>&) { return false ;}
virtual bool saveList(bool&, std::list<RsItem*>&) =0;
virtual bool loadList(std::list<RsItem*>&) =0;
private:
uint32_t _tick_delay_in_seconds ;

View file

@ -17,8 +17,8 @@ class RsPQIService: public p3Service, public p3Config
// Functions from p3config
//
virtual RsSerialiser *setupSerialiser() { return NULL ; }
virtual bool saveList(bool&, std::list<RsItem*>&) { return false ;}
virtual bool loadList(std::list<RsItem*>&) { return false ;}
virtual bool saveList(bool&, std::list<RsItem*>&) =0 ;
virtual bool loadList(std::list<RsItem*>&) =0 ;
private:
uint32_t _tick_delay_in_seconds ;

View file

@ -1,77 +0,0 @@
RS_TOP_DIR = ..
##### Define any flags that are needed for this section #######
###############################################################
###############################################################
include $(RS_TOP_DIR)/scripts/config.mk
###############################################################
BASE_OBJ = pqisecurity.o pqinetwork.o
#pqi_base.o
LOOP_OBJ = pqiloopback.o
STREAM_OBJ = pqistreamer.o pqiarchive.o pqistore.o pqibin.o
MGR_OBJ = pqimonitor.o p3dhtmgr.o p3connmgr.o p3cfgmgr.o p3authmgr.o
GRP_OBJ = pqiperson.o pqihandler.o pqiservice.o pqipersongrp.o
SSL_OBJ = pqissl.o pqissllistener.o pqisslpersongrp.o cleanupxpgp.o
UDP_OBJ = pqissludp.o
OTHER_OBJ = p3notify.o
TESTOBJ = net_test.o dht_test.o net_test1.o netiface_test.o
#conn_test.o
TESTS = net_test dht_test net_test1 netiface_test
#conn_test
ifdef PQI_USE_XPGP
SSL_OBJ += authxpgp.o
TESTOBJ += xpgp_id.o
TESTS += xpgp_id
else
ifdef PQI_USE_SSLONLY
SSL_OBJ += authssl.o
else
SSL_OBJ += authssl.o authgpg.o
TESTOBJ += gpgme_tst.o
TESTS += gpgme_tst
endif
endif
RSOBJ = $(BASE_OBJ) $(LOOP_OBJ) \
$(STREAM_OBJ) \
$(MGR_OBJ) \
$(SSL_OBJ) \
$(UDP_OBJ) \
$(GRP_OBJ) \
$(OTHER_OBJ)
all: librs tests
gpgme_tst: gpgme_tst.o
$(CC) $(CFLAGS) -o gpgme_tst gpgme_tst.o $(LIBS)
xpgp_id: xpgp_id.o
$(CC) $(CFLAGS) -o xpgp_id xpgp_id.o $(LIBS)
dht_test: dht_test.o
$(CC) $(CFLAGS) -o dht_test dht_test.o $(LIBS)
conn_test: conn_test.o
$(CC) $(CFLAGS) -o conn_test conn_test.o $(LIBS)
net_test: net_test.o
$(CC) $(CFLAGS) -o net_test net_test.o $(LIBS)
net_test1: net_test1.o
$(CC) $(CFLAGS) -o net_test1 net_test1.o $(LIBS)
netiface_test: netiface_test.o
$(CC) $(CFLAGS) -o netiface_test netiface_test.o $(LIBS)
###############################################################
include $(RS_TOP_DIR)/scripts/rules.mk
###############################################################

View file

@ -715,6 +715,7 @@ bool AuthGPG::loadList(std::list<RsItem*>& load)
}
delete (*it);
}
load.clear() ;
return true;
}

View file

@ -198,7 +198,7 @@ void tls_cleanup()
CRYPTO_set_locking_callback(NULL);
CRYPTO_set_id_callback(NULL);
if (mutex_buf == NULL) {
if (mutex_buf != NULL) {
for (int i = 0; i < CRYPTO_num_locks(); i++) {
pthread_mutex_destroy(&mutex_buf[i]);
}
@ -1705,6 +1705,7 @@ bool AuthSSLimpl::loadList(std::list<RsItem*>& load)
}
delete (*it);
}
load.clear() ;
return true;
}

View file

@ -208,7 +208,7 @@ p3Config::p3Config()
}
bool p3Config::loadConfiguration(RsFileHash &loadHash)
bool p3Config::loadConfiguration(RsFileHash& /* loadHash */)
{
return loadConfig();
}
@ -304,7 +304,11 @@ bool p3Config::loadAttempt(const std::string& cfgFname,const std::string& signFn
/* set hash */
setHash(bio->gethash());
BinMemInterface *signbio = new BinMemInterface(1000, BIN_FLAGS_READABLE);
std::string signatureRead;
RsFileHash strHash(Hash());
AuthSSL::getAuthSSL()->SignData(strHash.toByteArray(), RsFileHash::SIZE_IN_BYTES, signatureRead);
BinMemInterface *signbio = new BinMemInterface(signatureRead.size(), BIN_FLAGS_READABLE);
if(!signbio->readfromfile(signFname.c_str()))
{
@ -314,10 +318,6 @@ bool p3Config::loadAttempt(const std::string& cfgFname,const std::string& signFn
std::string signatureStored((char *) signbio->memptr(), signbio->memsize());
std::string signatureRead;
RsFileHash strHash(Hash());
AuthSSL::getAuthSSL()->SignData(strHash.toByteArray(), RsFileHash::SIZE_IN_BYTES, signatureRead);
delete signbio;
if(signatureRead != signatureStored)

View file

@ -34,6 +34,10 @@
#include "rsserver/p3face.h"
#include "util/rsstring.h"
/****
* #define HISTMGR_DEBUG 1
***/
// clean too old messages every 5 minutes
//
#define MSG_HISTORY_CLEANING_PERIOD 300
@ -99,17 +103,14 @@ void p3HistoryMgr::addMessage(const ChatMessage& cm)
enabled = true;
}
if (cm.chat_id.isGxsId() && mPrivateEnable == true) {
if (cm.incoming) {
peerName = cm.chat_id.toGxsId().toStdString();
} else {
uint32_t status;
RsGxsId from_gxs_id;
if (rsMsgs->getDistantChatStatus(cm.chat_id.toGxsId(), status, &from_gxs_id))
peerName = from_gxs_id.toStdString();
}
enabled = true;
}
if(cm.chat_id.isDistantChatId())
{
uint32_t status;
DistantChatPeerInfo dcpinfo;
if (rsMsgs->getDistantChatStatus(cm.chat_id.toDistantChatId(), dcpinfo))
peerName = cm.chat_id.toPeerId().toStdString();
enabled = true;
}
if(enabled == false)
return;
@ -171,7 +172,9 @@ void p3HistoryMgr::cleanOldMessages()
{
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
#ifdef HISTMGR_DEBUG
std::cerr << "****** cleaning old messages." << std::endl;
#endif
time_t now = time(NULL) ;
bool changed = false ;
@ -185,7 +188,9 @@ void p3HistoryMgr::cleanOldMessages()
std::map<uint32_t, RsHistoryMsgItem*>::iterator lit2 = lit ;
++lit2 ;
#ifdef HISTMGR_DEBUG
std::cerr << " removing msg id " << lit->first << ", for peer id " << mit->first << std::endl;
#endif
delete lit->second ;
mit->second.erase(lit) ;
@ -201,7 +206,9 @@ void p3HistoryMgr::cleanOldMessages()
{
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit2 = mit ;
++mit2 ;
std::cerr << " removing peer id " << mit->first << ", since it has no messages" << std::endl;
#ifdef HISTMGR_DEBUG
std::cerr << " removing peer id " << mit->first << ", since it has no messages" << std::endl;
#endif
mMessages.erase(mit) ;
mit = mit2 ;
@ -300,13 +307,16 @@ bool p3HistoryMgr::loadList(std::list<RsItem*>& load)
RsHistoryMsgItem *msgItem;
std::list<RsItem*>::iterator it;
for (it = load.begin(); it != load.end(); ++it) {
for (it = load.begin(); it != load.end(); ++it)
{
if (NULL != (msgItem = dynamic_cast<RsHistoryMsgItem*>(*it))) {
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(msgItem->chatPeerId);
msgItem->msgId = nextMsgId++;
#ifdef HISTMGR_DEBUG
std::cerr << "Loading msg history item: peer id=" << msgItem->chatPeerId << "), msg id =" << msgItem->msgId << std::endl;
#endif
if (mit != mMessages.end()) {
mit->second.insert(std::make_pair(msgItem->msgId, msgItem));
@ -344,7 +354,9 @@ bool p3HistoryMgr::loadList(std::list<RsItem*>& load)
if (sscanf(kit->value.c_str(), "%d", &val) == 1)
mMaxStorageDurationSeconds = val ;
#ifdef HISTMGR_DEBUG
std::cerr << "Loaded max storage time for history = " << val << " seconds" << std::endl;
#endif
continue;
}
@ -370,6 +382,7 @@ bool p3HistoryMgr::loadList(std::list<RsItem*>& load)
delete (*it);
}
load.clear() ;
return true;
}
@ -397,8 +410,8 @@ bool p3HistoryMgr::chatIdToVirtualPeerId(ChatId chat_id, RsPeerId &peer_id)
return true;
}
if (chat_id.isGxsId()) {
peer_id = RsPeerId(chat_id.toGxsId());
if (chat_id.isDistantChatId()) {
peer_id = RsPeerId(chat_id.toDistantChatId());
return true;
}
@ -436,7 +449,7 @@ bool p3HistoryMgr::getMessages(const ChatId &chatId, std::list<HistoryMsg> &msgs
if (chatId.isLobbyId() && mLobbyEnable == true) {
enabled = true;
}
if (chatId.isGxsId() && mPrivateEnable == true) {
if (chatId.isDistantChatId() && mPrivateEnable == true) {
enabled = true;
}
@ -446,7 +459,9 @@ bool p3HistoryMgr::getMessages(const ChatId &chatId, std::list<HistoryMsg> &msgs
if(!chatIdToVirtualPeerId(chatId, chatPeerId))
return false;
#ifdef HISTMGR_DEBUG
std::cerr << "Getting history for virtual peer " << chatPeerId << std::endl;
#endif
uint32_t foundCount = 0;
@ -467,7 +482,9 @@ bool p3HistoryMgr::getMessages(const ChatId &chatId, std::list<HistoryMsg> &msgs
}
}
}
#ifdef HISTMGR_DEBUG
std::cerr << msgs.size() << " messages added." << std::endl;
#endif
return true;
}
@ -497,7 +514,9 @@ void p3HistoryMgr::clear(const ChatId &chatId)
if(!chatIdToVirtualPeerId(chatId, chatPeerId))
return;
#ifdef HISTMGR_DEBUG
std::cerr << "********** p3History::clear()called for virtual peer id " << chatPeerId << std::endl;
#endif
std::map<RsPeerId, std::map<uint32_t, RsHistoryMsgItem*> >::iterator mit = mMessages.find(chatPeerId);
if (mit == mMessages.end()) {
@ -523,7 +542,9 @@ void p3HistoryMgr::removeMessages(const std::list<uint32_t> &msgIds)
std::list<uint32_t> removedIds;
std::list<uint32_t>::iterator iit;
#ifdef HISTMGR_DEBUG
std::cerr << "********** p3History::removeMessages called()" << std::endl;
#endif
{
RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/
@ -536,7 +557,9 @@ void p3HistoryMgr::removeMessages(const std::list<uint32_t> &msgIds)
std::map<uint32_t, RsHistoryMsgItem*>::iterator lit = mit->second.find(*iit);
if (lit != mit->second.end())
{
#ifdef HISTMGR_DEBUG
std::cerr << "**** Removing " << mit->first << " msg id = " << lit->first << std::endl;
#endif
delete(lit->second);
mit->second.erase(lit);

View file

@ -964,24 +964,25 @@ bool p3LinkMgrIMPL::connectResult(const RsPeerId &id, bool success, bool isIncom
if (flags == RS_NET_CONN_UDP_ALL)
{
#ifdef LINKMGR_DEBUG
#endif
std::cerr << "p3LinkMgrIMPL::connectResult() Sending Feedback for UDP connection";
std::cerr << std::endl;
#endif
if (success)
{
#ifdef LINKMGR_DEBUG
#endif
std::cerr << "p3LinkMgrIMPL::connectResult() UDP Update CONNECTED to: " << id;
std::cerr << std::endl;
#endif
mNetMgr->netAssistStatusUpdate(id, NETMGR_DHT_FEEDBACK_CONNECTED);
}
else
{
#ifdef LINKMGR_DEBUG
#endif
std::cerr << "p3LinkMgrIMPL::connectResult() UDP Update FAILED to: " << id;
std::cerr << std::endl;
#endif
/* have no differentiation between failure and closed? */
mNetMgr->netAssistStatusUpdate(id, NETMGR_DHT_FEEDBACK_CONN_FAILED);
@ -1570,135 +1571,96 @@ bool p3LinkMgrIMPL::tryConnectUDP(const RsPeerId &id, const struct sockaddr_st
bool p3LinkMgrIMPL::retryConnectTCP(const RsPeerId &id)
/* push all available addresses onto the connect addr stack...
* with the following exceptions:
* - id is our own
* - id is not our friend
* - id is already connected
* - id is hidden but of an unkown type
* - we are hidden but id is not
*/
bool p3LinkMgrIMPL::retryConnectTCP(const RsPeerId &id)
{
/* Check if we should retry first */
{
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
/* push all available addresses onto the connect addr stack...
* with the following exceptions:
* - check local address, see if it is the same network as us
- check address age. don't add old ones
*/
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() id: " << id << std::endl;
#endif
if (id == getOwnId())
{
#ifdef LINKMGR_DEBUG
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::retryConnectTCP() Failed, connecting to own id: ");
#endif
return false;
}
/* look up the id */
std::map<RsPeerId, peerConnectState>::iterator it;
if (mFriendList.end() == (it = mFriendList.find(id)))
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Peer is not Friend" << std::endl;
#endif
return false;
}
/* if already connected -> done */
if (it->second.state & RS_PEER_S_CONNECTED)
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Peer Already Connected" << std::endl;
#endif
return false;
}
} /****** END of LOCKED ******/
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Getting Address from PeerMgr for : " << id;
std::cerr << std::endl;
#endif
/* If we reach here, must retry .... extract the required info from p3PeerMgr */
if (id == getOwnId()) return false;
/* first possibility - is it a hidden peer */
{
RS_STACK_MUTEX(mLinkMtx);
std::map<RsPeerId, peerConnectState>::iterator it = mFriendList.find(id);
if ( it == mFriendList.end() ) return false;
if ( it->second.state & RS_PEER_S_CONNECTED ) return false;
}
// Extract the required info from p3PeerMgr
// first possibility - is it a hidden peer
if (mPeerMgr->isHiddenPeer(id))
{
/* check for valid hidden type */
uint32_t type = mPeerMgr->getHiddenType(id);
if ( type & (~RS_HIDDEN_TYPE_MASK) ) return false;
/* then we just have one connect attempt via the Proxy */
struct sockaddr_storage proxy_addr;
std::string domain_addr;
uint16_t domain_port;
/* then we just have one connect attempt via the Proxy */
if (mPeerMgr->getProxyAddress(id, proxy_addr, domain_addr, domain_port))
if ( mPeerMgr->getProxyAddress(id, proxy_addr, domain_addr, domain_port) )
{
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
std::map<RsPeerId, peerConnectState>::iterator it;
if (mFriendList.end() != (it = mFriendList.find(id)))
RS_STACK_MUTEX(mLinkMtx);
std::map<RsPeerId, peerConnectState>::iterator it = mFriendList.find(id);
if (it != mFriendList.end())
{
locked_ConnectAttempt_ProxyAddress(&(it->second), proxy_addr, domain_addr, domain_port);
locked_ConnectAttempt_ProxyAddress(&(it->second), type, proxy_addr, domain_addr, domain_port);
return locked_ConnectAttempt_Complete(&(it->second));
}
}
return false;
}
if (mPeerMgr->isHidden())
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() isHidden(): no connection attempts for : " << id;
std::cerr << std::endl;
#endif
return false;
}
if (mPeerMgr->isHidden()) return false;
struct sockaddr_storage lAddr;
struct sockaddr_storage eAddr;
pqiIpAddrSet histAddrs;
std::string dyndns;
if (mPeerMgr->getConnectAddresses(id, lAddr, eAddr, histAddrs, dyndns))
{
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
RS_STACK_MUTEX(mLinkMtx);
std::map<RsPeerId, peerConnectState>::iterator it;
if (mFriendList.end() != (it = mFriendList.find(id)))
std::map<RsPeerId, peerConnectState>::iterator it = mFriendList.find(id);
if ( it != mFriendList.end() )
{
locked_ConnectAttempt_CurrentAddresses(&(it->second), lAddr, eAddr);
uint16_t dynPort = sockaddr_storage_port(eAddr);
if (!dynPort)
uint16_t dynPort = 0;
if (!sockaddr_storage_isnull(eAddr)) dynPort = sockaddr_storage_port(eAddr);
if (!dynPort && !sockaddr_storage_isnull(lAddr))
dynPort = sockaddr_storage_port(lAddr);
if (dynPort)
{
locked_ConnectAttempt_AddDynDNS(&(it->second), dyndns, dynPort);
}
locked_ConnectAttempt_HistoricalAddresses(&(it->second), histAddrs);
/* finish it off */
// finish it off
return locked_ConnectAttempt_Complete(&(it->second));
}
else
{
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to find friend data : " << id;
std::cerr << std::endl;
}
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to find friend data : " << id << std::endl;
}
else
{
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to addresses from PeerMgr for: " << id;
std::cerr << std::endl;
}
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to get addresses from PeerMgr for: " << id << std::endl;
return false;
}
#define MAX_TCP_ADDR_AGE (3600 * 24 * 14) // two weeks in seconds.
bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_storage &addr, time_t age)
bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_storage &addr, time_t age)
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr(";
@ -1719,13 +1681,8 @@ bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_storage &ad
return false;
}
bool isValid = sockaddr_storage_isValidNet(addr);
bool isLoopback = sockaddr_storage_isLoopbackNet(addr);
// bool isPrivate = sockaddr_storage_isPrivateNet(addr);
bool isExternal = sockaddr_storage_isExternalNet(addr);
/* if invalid - quick rejection */
if (!isValid)
if ( ! sockaddr_storage_isValidNet(addr) )
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - INVALID";
@ -1760,60 +1717,7 @@ bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_storage &ad
return false ;
}
/* if it is an external address, we'll accept it.
* - even it is meant to be a local address.
*/
if (isExternal)
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() ACCEPTING - EXTERNAL";
std::cerr << std::endl;
#endif
return true;
}
/* if loopback, then okay - probably proxy connection (or local testing).
*/
if (isLoopback)
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() ACCEPTING - LOOPBACK";
std::cerr << std::endl;
#endif
return true;
}
/* get here, it is private or loopback
* - can only connect to these addresses if we are on the same subnet.
- check net against our local address.
*/
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() Checking sameNet against: ";
std::cerr << sockaddr_storage_iptostring(mLocalAddress);
std::cerr << ")";
std::cerr << std::endl;
#endif
if (sockaddr_storage_samenet(mLocalAddress, addr))
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() ACCEPTING - PRIVATE & sameNET";
std::cerr << std::endl;
#endif
return true;
}
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - PRIVATE & !sameNET";
std::cerr << std::endl;
#endif
/* else it fails */
return false;
return true;
}
@ -2018,7 +1922,7 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std
}
void p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, const struct sockaddr_storage &proxy_addr, const std::string &domain_addr, uint16_t domain_port)
void p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, const uint32_t type, const struct sockaddr_storage &proxy_addr, const std::string &domain_addr, uint16_t domain_port)
{
#ifdef LINKMGR_DEBUG
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress() trying address: " << domain_addr << ":" << domain_port << std::endl;
@ -2026,7 +1930,22 @@ void p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress(peerConnectState *peer,
peerConnectAddress pca;
pca.addr = proxy_addr;
pca.type = RS_NET_CONN_TCP_HIDDEN;
switch (type) {
case RS_HIDDEN_TYPE_TOR:
pca.type = RS_NET_CONN_TCP_HIDDEN_TOR;
break;
case RS_HIDDEN_TYPE_I2P:
pca.type = RS_NET_CONN_TCP_HIDDEN_I2P;
break;
case RS_HIDDEN_TYPE_UNKNOWN:
default:
/**** THIS CASE SHOULD NOT BE TRIGGERED - since this function is called with a valid hidden type only ****/
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress() hidden type of addr: " << domain_addr << " is unkown -> THIS SHOULD NEVER HAPPEN!" << std::endl;
std::cerr << " - peer : " << peer->id << "(" << peer->name << ")" << std::endl;
std::cerr << " - proxy: " << sockaddr_storage_tostring(proxy_addr) << std::endl;
std::cerr << " - addr : " << domain_addr << ":" << domain_port << std::endl;
pca.type = RS_NET_CONN_TCP_UNKNOW_TOPOLOGY;
}
//for the delay, we add a random time and some more time when the friend list is big
pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY;

View file

@ -40,16 +40,17 @@ class DNSResolver ;
/* order of attempts ... */
const uint32_t RS_NET_CONN_TCP_ALL = 0x000f;
const uint32_t RS_NET_CONN_UDP_ALL = 0x00f0;
const uint32_t RS_NET_CONN_TCP_ALL = 0x00ff;
const uint32_t RS_NET_CONN_UDP_ALL = 0x0f00;
const uint32_t RS_NET_CONN_TCP_LOCAL = 0x0001;
const uint32_t RS_NET_CONN_TCP_EXTERNAL = 0x0002;
const uint32_t RS_NET_CONN_TCP_UNKNOW_TOPOLOGY = 0x0004;
const uint32_t RS_NET_CONN_TCP_HIDDEN = 0x0008;
const uint32_t RS_NET_CONN_TCP_HIDDEN_TOR = 0x0008;
const uint32_t RS_NET_CONN_TCP_HIDDEN_I2P = 0x0010;
const uint32_t RS_NET_CONN_UDP_DHT_SYNC = 0x0010;
const uint32_t RS_NET_CONN_UDP_PEER_SYNC = 0x0020; /* coming soon */
const uint32_t RS_NET_CONN_UDP_DHT_SYNC = 0x0100;
const uint32_t RS_NET_CONN_UDP_PEER_SYNC = 0x0200; /* coming soon */
// These are set in pqipersongroup.
const uint32_t RS_TCP_STD_TIMEOUT_PERIOD = 5; /* 5 seconds! */
@ -302,7 +303,7 @@ void locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, const struc
void locked_ConnectAttempt_HistoricalAddresses(peerConnectState *peer, const pqiIpAddrSet &ipAddrs);
void locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std::string dyndns, uint16_t dynPort);
void locked_ConnectAttempt_AddTunnel(peerConnectState *peer);
void locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, const struct sockaddr_storage &proxy_addr, const std::string &domain_addr, uint16_t domain_port);
void locked_ConnectAttempt_ProxyAddress(peerConnectState *peer, const uint32_t type, const struct sockaddr_storage &proxy_addr, const std::string &domain_addr, uint16_t domain_port);
bool locked_ConnectAttempt_Complete(peerConnectState *peer);

View file

@ -696,9 +696,9 @@ void p3NetMgrIMPL::netExtCheck()
bool isStable = false;
struct sockaddr_storage tmpip ;
std::map<sockaddr_storage,ZeroInt> address_votes ;
std::map<sockaddr_storage,ZeroInt> address_votes ;
/* check for External Address */
/* check for External Address */
/* in order of importance */
/* (1) UPnP -> which handles itself */
{
@ -708,35 +708,41 @@ void p3NetMgrIMPL::netExtCheck()
/* net Assist */
if (netAssistExtAddress(tmpip))
{
{
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied from netAssistExternalAddress()" << std::endl;
std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied from netAssistExternalAddress()" << std::endl;
#endif
if(sockaddr_storage_isValidNet(tmpip))
{
if(rsBanList->isAddressAccepted(tmpip,RSBANLIST_CHECKING_FLAGS_BLACKLIST))
{
// must be stable???
isStable = true;
mNetFlags.mExtAddr = tmpip;
mNetFlags.mExtAddrOk = true;
mNetFlags.mExtAddrStableOk = isStable;
if(sockaddr_storage_isValidNet(tmpip))
{
if(rsBanList->isAddressAccepted(tmpip,RSBANLIST_CHECKING_FLAGS_BLACKLIST))
{
// must be stable???
isStable = true;
//mNetFlags.mExtAddr = tmpip;
mNetFlags.mExtAddrOk = true;
mNetFlags.mExtAddrStableOk = isStable;
address_votes[tmpip].n++ ;
}
else
std::cerr << "(SS) netAssisExternalAddress returned wrong own IP " << sockaddr_storage_iptostring(tmpip) << " (banned). Rejecting." << std::endl;
}
address_votes[tmpip].n++ ;
std::cerr << "NetAssistAddress reported external address " << sockaddr_storage_iptostring(tmpip) << std::endl;
}
else
std::cerr << "(SS) netAssisExternalAddress returned banned own IP " << sockaddr_storage_iptostring(tmpip) << " (banned). Rejecting." << std::endl;
}
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
else
{
std::cerr << "p3NetMgrIMPL::netExtCheck() Bad Address supplied from netAssistExternalAddress()" << std::endl;
}
else
{
std::cerr << "p3NetMgrIMPL::netExtCheck() Bad Address supplied from netAssistExternalAddress()" << std::endl;
}
#endif
}
}
}
#ifdef ALLOW_DHT_STUNNER
// (cyril) I disabled this because it's pretty dangerous. The DHT can report a wrong address quite easily
// if the other DHT peers are not collaborating.
/* Next ask the DhtStunner */
{
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
@ -748,30 +754,31 @@ void p3NetMgrIMPL::netExtCheck()
if (mDhtStunner)
{
/* input network bits */
if (mDhtStunner->getExternalAddr(tmpaddr, isstable))
{
if(rsBanList->isAddressAccepted(tmpaddr,RSBANLIST_CHECKING_FLAGS_BLACKLIST))
{
// must be stable???
isStable = (isstable == 1);
mNetFlags.mExtAddr = tmpaddr;
mNetFlags.mExtAddrOk = true;
mNetFlags.mExtAddrStableOk = isStable;
/* input network bits */
if (mDhtStunner->getExternalAddr(tmpaddr, isstable))
{
if(rsBanList->isAddressAccepted(tmpaddr,RSBANLIST_CHECKING_FLAGS_BLACKLIST))
{
// must be stable???
isStable = (isstable == 1);
//mNetFlags.mExtAddr = tmpaddr;
mNetFlags.mExtAddrOk = true;
mNetFlags.mExtAddrStableOk = isStable;
address_votes[tmpaddr].n++ ;
address_votes[tmpaddr].n++ ;
#ifdef NETMGR_DEBUG_STATEBOX
std::cerr << "p3NetMgrIMPL::netExtCheck() From DhtStunner: ";
std::cerr << sockaddr_storage_tostring(tmpaddr);
std::cerr << " Stable: " << (uint32_t) isstable;
std::cerr << std::endl;
std::cerr << "p3NetMgrIMPL::netExtCheck() From DhtStunner: ";
std::cerr << sockaddr_storage_tostring(tmpaddr);
std::cerr << " Stable: " << (uint32_t) isstable;
std::cerr << std::endl;
#endif
}
else
std::cerr << "(SS) DHTStunner returned wrong own IP " << sockaddr_storage_iptostring(tmpaddr) << " (banned). Rejecting." << std::endl;
}
}
else
std::cerr << "(SS) DHTStunner returned wrong own IP " << sockaddr_storage_iptostring(tmpaddr) << " (banned). Rejecting." << std::endl;
}
}
}
#endif
/* otherwise ask ExtAddrFinder */
{
@ -782,119 +789,159 @@ void p3NetMgrIMPL::netExtCheck()
std::cerr << "p3NetMgrIMPL::netExtCheck() checking ExtAddrFinder" << std::endl;
#endif
bool extFinderOk = mExtAddrFinder->hasValidIP(tmpip);
if (extFinderOk)
if (extFinderOk)
{
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied by ExtAddrFinder" << std::endl;
#endif
/* best guess at port */
sockaddr_storage_setport(tmpip, sockaddr_storage_port(mLocalAddr));
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() ";
std::cerr << "ExtAddr: " << sockaddr_storage_tostring(tmpip);
std::cerr << std::endl;
#endif
mNetFlags.mExtAddr = tmpip;
//mNetFlags.mExtAddr = tmpip;
mNetFlags.mExtAddrOk = true;
address_votes[tmpip].n++ ;
address_votes[tmpip].n++ ;
/* XXX HACK TO FIX */
#warning "ALLOWING ExtAddrFinder -> ExtAddrStableOk = true (which it is not normally)"
mNetFlags.mExtAddrStableOk = true;
std::cerr << "ExtAddrFinder reported external address " << sockaddr_storage_iptostring(tmpip) << std::endl;
}
}
}
/* any other sources ??? */
/* also ask peer mgr. */
if (mPeerMgr)
{
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() checking mPeerMgr" << std::endl;
#endif
uint8_t isstable ; // unused
sockaddr_storage tmpaddr ;
if (mPeerMgr->getExtAddressReportedByFriends(tmpaddr, isstable))
{
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied by ExtAddrFinder" << std::endl;
#endif
/* best guess at port */
sockaddr_storage_setport(tmpaddr, sockaddr_storage_port(mLocalAddr));
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() ";
std::cerr << "ExtAddr: " << sockaddr_storage_tostring(tmpip);
std::cerr << std::endl;
#endif
//mNetFlags.mExtAddr = tmpaddr;
mNetFlags.mExtAddrOk = true;
mNetFlags.mExtAddrStableOk = isstable;
address_votes[tmpaddr].n++ ;
std::cerr << "PeerMgr reported external address " << sockaddr_storage_iptostring(tmpaddr) << std::endl;
}
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
else
std::cerr << " No reliable address returned." << std::endl;
#endif
}
/* any other sources ??? */
/* finalise address */
if (mNetFlags.mExtAddrOk)
{
// look at votes.
{
// look at votes.
std::cerr << "Figuring out ext addr from voting:" << std::endl;
uint32_t admax = 0 ;
std::cerr << "Figuring out ext addr from voting:" << std::endl;
uint32_t admax = 0 ;
for(std::map<sockaddr_storage,ZeroInt>::const_iterator it(address_votes.begin());it!=address_votes.end();++it)
{
std::cerr << " Vote: " << sockaddr_storage_iptostring(it->first) << " : " << it->second.n << " votes." ;
for(std::map<sockaddr_storage,ZeroInt>::const_iterator it(address_votes.begin());it!=address_votes.end();++it)
{
std::cerr << " Vote: " << sockaddr_storage_iptostring(it->first) << " : " << it->second.n << " votes." ;
if(it->second.n > admax)
{
mNetFlags.mExtAddr = it->first ;
admax = it->second.n ;
if(it->second.n > admax)
{
mNetFlags.mExtAddr = it->first ;
admax = it->second.n ;
std::cerr << " Kept!" << std::endl;
}
else
std::cerr << " Discarded." << std::endl;
}
std::cerr << " Kept!" << std::endl;
}
else
std::cerr << " Discarded." << std::endl;
}
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() ";
std::cerr << "ExtAddr: " << sockaddr_storage_tostring(mNetFlags.mExtAddr);
std::cerr << std::endl;
std::cerr << "p3NetMgrIMPL::netExtCheck() ";
std::cerr << "ExtAddr: " << sockaddr_storage_tostring(mNetFlags.mExtAddr);
std::cerr << std::endl;
#endif
//update ip address list
mExtAddr = mNetFlags.mExtAddr;
//update ip address list
mExtAddr = mNetFlags.mExtAddr;
mNetStatus = RS_NET_DONE;
netSetupDone = true;
mNetStatus = RS_NET_DONE;
netSetupDone = true;
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() Ext Ok: RS_NET_DONE" << std::endl;
std::cerr << "p3NetMgrIMPL::netExtCheck() Ext Ok: RS_NET_DONE" << std::endl;
#endif
if (!mNetFlags.mExtAddrStableOk)
{
if (!mNetFlags.mExtAddrStableOk)
{
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netUdpCheck() UDP Unstable :( ";
std::cerr << std::endl;
std::cerr << "p3NetMgrIMPL::netUdpCheck() We are unreachable";
std::cerr << std::endl;
std::cerr << "netMode => RS_NET_MODE_UNREACHABLE";
std::cerr << std::endl;
std::cerr << "p3NetMgrIMPL::netUdpCheck() UDP Unstable :( ";
std::cerr << std::endl;
std::cerr << "p3NetMgrIMPL::netUdpCheck() We are unreachable";
std::cerr << std::endl;
std::cerr << "netMode => RS_NET_MODE_UNREACHABLE";
std::cerr << std::endl;
#endif
// Due to the new UDP connections - we can still connect some of the time!
// So limit warning!
// Due to the new UDP connections - we can still connect some of the time!
// So limit warning!
//mNetMode &= ~(RS_NET_MODE_ACTUAL);
//mNetMode |= RS_NET_MODE_UNREACHABLE;
//mNetMode &= ~(RS_NET_MODE_ACTUAL);
//mNetMode |= RS_NET_MODE_UNREACHABLE;
/* send a system warning message */
//pqiNotify *notify = getPqiNotify();
//if (notify)
{
std::string title =
"Warning: Bad Firewall Configuration";
/* send a system warning message */
//pqiNotify *notify = getPqiNotify();
//if (notify)
{
std::string title =
"Warning: Bad Firewall Configuration";
std::string msg;
msg += " **** WARNING **** \n";
msg += "Retroshare has detected that you are behind";
msg += " a restrictive Firewall\n";
msg += "\n";
msg += "You will have limited connectivity to other firewalled peers\n";
msg += "\n";
msg += "You can fix this by:\n";
msg += " (1) opening an External Port\n";
msg += " (2) enabling UPnP, or\n";
msg += " (3) get a new (approved) Firewall/Router\n";
std::string msg;
msg += " **** WARNING **** \n";
msg += "Retroshare has detected that you are behind";
msg += " a restrictive Firewall\n";
msg += "\n";
msg += "You will have limited connectivity to other firewalled peers\n";
msg += "\n";
msg += "You can fix this by:\n";
msg += " (1) opening an External Port\n";
msg += " (2) enabling UPnP, or\n";
msg += " (3) get a new (approved) Firewall/Router\n";
//notify->AddSysMessage(0, RS_SYS_WARNING, title, msg);
//notify->AddSysMessage(0, RS_SYS_WARNING, title, msg);
std::cerr << msg << std::endl;
}
std::cerr << msg << std::endl;
}
}
}
}
}
if (mNetFlags.mExtAddrOk)
{
@ -950,7 +997,7 @@ void p3NetMgrIMPL::netExtCheck()
************************************** Interfaces *****************************************
**********************************************************************************************/
bool p3NetMgrIMPL::checkNetAddress()
bool p3NetMgrIMPL::checkNetAddress()
{
bool addrChanged = false;
bool validAddr = false;
@ -969,7 +1016,19 @@ bool p3NetMgrIMPL::checkNetAddress()
}
else
{
validAddr = getPreferredInterface(mLocalAddr, prefAddr);
// TODO: Sat Oct 24 15:51:24 CEST 2015 The fact of having just one local address is a flawed assumption, this should be redesigned soon.
std::list<sockaddr_storage> addrs;
std::list<sockaddr_storage>::iterator it;
if (getLocalAddresses(addrs))
for(it = addrs.begin(); (it != addrs.end() && !validAddr); ++it)
if(sockaddr_storage_isValidNet(*it) && !sockaddr_storage_isLoopbackNet(*it))
{
prefAddr = *it;
validAddr = true;
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cout << "p3NetMgrIMPL::checkNetAddress() prefAddr: " << sockaddr_storage_iptostring(prefAddr) << std::endl;
#endif
}
}
@ -978,7 +1037,6 @@ bool p3NetMgrIMPL::checkNetAddress()
{
#ifdef NETMGR_DEBUG_RESET
std::cerr << "p3NetMgrIMPL::checkNetAddress() no Valid Network Address, resetting network." << std::endl;
std::cerr << std::endl;
#endif
rslog(RSL_WARNING, p3netmgrzone, "p3NetMgr::checkNetAddress() No Valid Network Address, resetting network");
netReset();

View file

@ -326,6 +326,7 @@ private:
void netStatusReset_locked();
// TODO: Sat Oct 24 15:51:24 CEST 2015 The fact of having just two possible address is a flawed assumption, this should be redesigned soon.
struct sockaddr_storage mLocalAddr;
struct sockaddr_storage mExtAddr;

View file

@ -74,17 +74,20 @@ const uint32_t PEER_IP_CONNECT_STATE_MAX_LIST_SIZE = 4;
#define MIN_RETRY_PERIOD 140
static const std::string kConfigDefaultProxyServerIpAddr = "127.0.0.1";
static const uint16_t kConfigDefaultProxyServerPort = 9050; // standard port.
static const uint16_t kConfigDefaultProxyServerPortTor = 9050; // standard port.
static const uint16_t kConfigDefaultProxyServerPortI2P = 10; // there is no standard port though
static const std::string kConfigKeyExtIpFinder = "USE_EXTR_IP_FINDER";
static const std::string kConfigKeyProxyServerIpAddr = "PROXY_SERVER_IPADDR";
static const std::string kConfigKeyProxyServerPort = "PROXY_SERVER_PORT";
static const std::string kConfigKeyProxyServerIpAddrTor = "PROXY_SERVER_IPADDR";
static const std::string kConfigKeyProxyServerPortTor = "PROXY_SERVER_PORT";
static const std::string kConfigKeyProxyServerIpAddrI2P = "PROXY_SERVER_IPADDR_I2P";
static const std::string kConfigKeyProxyServerPortI2P = "PROXY_SERVER_PORT_I2P";
void printConnectState(std::ostream &out, peerState &peer);
peerState::peerState()
:netMode(RS_NET_MODE_UNKNOWN), vs_disc(RS_VS_DISC_FULL), vs_dht(RS_VS_DHT_FULL), lastcontact(0),
hiddenNode(false), hiddenPort(0)
hiddenNode(false), hiddenPort(0), hiddenType(RS_HIDDEN_TYPE_NONE)
{
sockaddr_storage_clear(localaddr);
sockaddr_storage_clear(serveraddr);
@ -130,13 +133,21 @@ p3PeerMgrIMPL::p3PeerMgrIMPL(const RsPeerId& ssl_own_id, const RsPgpId& gpg_own_
lastGroupId = 1;
// setup default ProxyServerAddress.
sockaddr_storage_clear(mProxyServerAddress);
sockaddr_storage_ipv4_aton(mProxyServerAddress,
// Tor
sockaddr_storage_clear(mProxyServerAddressTor);
sockaddr_storage_ipv4_aton(mProxyServerAddressTor,
kConfigDefaultProxyServerIpAddr.c_str());
sockaddr_storage_ipv4_setport(mProxyServerAddress,
kConfigDefaultProxyServerPort);
sockaddr_storage_ipv4_setport(mProxyServerAddressTor,
kConfigDefaultProxyServerPortTor);
// I2P
sockaddr_storage_clear(mProxyServerAddressI2P);
sockaddr_storage_ipv4_aton(mProxyServerAddressI2P,
kConfigDefaultProxyServerIpAddr.c_str());
sockaddr_storage_ipv4_setport(mProxyServerAddressI2P,
kConfigDefaultProxyServerPortI2P);
mProxyServerStatus = RS_NET_PROXY_STATUS_UNKNOWN ;
mProxyServerStatusTor = RS_NET_PROXY_STATUS_UNKNOWN ;
mProxyServerStatusI2P = RS_NET_PROXY_STATUS_UNKNOWN;
}
#ifdef PEER_DEBUG
@ -169,6 +180,7 @@ bool p3PeerMgrIMPL::setupHiddenNode(const std::string &hiddenAddress, const uint
mOwnState.hiddenNode = true;
mOwnState.hiddenPort = hiddenPort;
mOwnState.hiddenDomain = hiddenAddress;
mOwnState.hiddenType = hiddenDomainToHiddenType(hiddenAddress);
}
forceHiddenNode();
@ -188,6 +200,7 @@ bool p3PeerMgrIMPL::forceHiddenNode()
#endif
}
mOwnState.hiddenNode = true;
mOwnState.hiddenType = hiddenDomainToHiddenType(mOwnState.hiddenDomain);
// force external address - otherwise its invalid.
sockaddr_storage_clear(mOwnState.serveraddr);
@ -366,14 +379,49 @@ bool p3PeerMgrIMPL::getGpgId(const RsPeerId &ssl_id, RsPgpId &gpgId)
/**** HIDDEN STUFF ****/
bool p3PeerMgrIMPL::isHidden()
bool p3PeerMgrIMPL::isHidden()
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
RS_STACK_MUTEX(mPeerMtx);
return mOwnState.hiddenNode;
}
/**
* @brief checks the hidden type of the own peer.
* @param type type to check
* @return true when the peer has the same hidden type than type
*/
bool p3PeerMgrIMPL::isHidden(const uint32_t type)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
switch (type) {
case RS_HIDDEN_TYPE_TOR:
return mOwnState.hiddenType == RS_HIDDEN_TYPE_TOR;
break;
case RS_HIDDEN_TYPE_I2P:
return mOwnState.hiddenType == RS_HIDDEN_TYPE_I2P;
break;
default:
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::isHidden(" << type << ") unkown type -> false";
std::cerr << std::endl;
#endif
return false;
break;
}
}
bool p3PeerMgrIMPL::isHiddenPeer(const RsPeerId &ssl_id)
{
return isHiddenPeer(ssl_id, RS_HIDDEN_TYPE_NONE);
}
/**
* @brief checks the hidden type of a given ssl id. When type RS_HIDDEN_TYPE_NONE is choosen it returns the 'hiddenNode' value instead
* @param ssl_id to check
* @param type type to check. Use RS_HIDDEN_TYPE_NONE to check 'hiddenNode' value
* @return true when the peer has the same hidden type than type
*/
bool p3PeerMgrIMPL::isHiddenPeer(const RsPeerId &ssl_id, const uint32_t type)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
@ -394,9 +442,91 @@ bool p3PeerMgrIMPL::isHiddenPeer(const RsPeerId &ssl_id)
std::cerr << "p3PeerMgrIMPL::isHiddenPeer(" << ssl_id << ") = " << (it->second).hiddenNode;
std::cerr << std::endl;
#endif
return (it->second).hiddenNode;
switch (type) {
case RS_HIDDEN_TYPE_TOR:
return (it->second).hiddenType == RS_HIDDEN_TYPE_TOR;
break;
case RS_HIDDEN_TYPE_I2P:
return (it->second).hiddenType == RS_HIDDEN_TYPE_I2P;
break;
default:
return (it->second).hiddenNode;
break;
}
}
bool hasEnding (std::string const &fullString, std::string const &ending) {
if (fullString.length() < ending.length())
return false;
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
}
/**
* @brief resolves the hidden type (tor or i2p) from a domain
* @param domain to check
* @return RS_HIDDEN_TYPE_TOR, RS_HIDDEN_TYPE_I2P or RS_HIDDEN_TYPE_NONE
*
* Tor: ^[a-z2-7]{16}\.onion$
*
* I2P: There is more than one address:
* - pub. key in base64
* - hash in base32 ( ^[a-z2-7]{52}\.b32\.i2p$ )
* - "normal" .i2p domains
*/
uint32_t p3PeerMgrIMPL::hiddenDomainToHiddenType(const std::string &domain)
{
if(hasEnding(domain, ".onion"))
return RS_HIDDEN_TYPE_TOR;
if(hasEnding(domain, ".i2p"))
return RS_HIDDEN_TYPE_I2P;
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::hiddenDomainToHiddenType() unknown hidden type: " << domain;
std::cerr << std::endl;
#endif
return RS_HIDDEN_TYPE_UNKNOWN;
}
/**
* @brief returns the hidden type of a peer
* @param ssl_id peer id
* @return hidden type
*/
uint32_t p3PeerMgrIMPL::getHiddenType(const RsPeerId &ssl_id)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
if (ssl_id == AuthSSL::getAuthSSL()->OwnId())
return mOwnState.hiddenType;
/* check for existing */
std::map<RsPeerId, peerState>::iterator it;
it = mFriendList.find(ssl_id);
if (it == mFriendList.end())
{
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::getHiddenType(" << ssl_id << ") Missing Peer => false";
std::cerr << std::endl;
#endif
return false;
}
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::getHiddenType(" << ssl_id << ") = " << (it->second).hiddenType;
std::cerr << std::endl;
#endif
return (it->second).hiddenType;
}
/**
* @brief sets hidden domain and port for a given ssl ID
* @param ssl_id peer to set domain and port for
* @param domain_addr
* @param domain_port
* @return true on success
*/
bool p3PeerMgrIMPL::setHiddenDomainPort(const RsPeerId &ssl_id, const std::string &domain_addr, const uint16_t domain_port)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
@ -426,6 +556,7 @@ bool p3PeerMgrIMPL::setHiddenDomainPort(const RsPeerId &ssl_id, const std::strin
mOwnState.hiddenNode = true;
mOwnState.hiddenDomain = domain;
mOwnState.hiddenPort = domain_port;
mOwnState.hiddenType = hiddenDomainToHiddenType(domain);
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::setHiddenDomainPort() Set own State";
std::cerr << std::endl;
@ -448,6 +579,7 @@ bool p3PeerMgrIMPL::setHiddenDomainPort(const RsPeerId &ssl_id, const std::strin
it->second.hiddenDomain = domain;
it->second.hiddenPort = domain_port;
it->second.hiddenNode = true;
it->second.hiddenType = hiddenDomainToHiddenType(domain);
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::setHiddenDomainPort() Set Peers State";
std::cerr << std::endl;
@ -456,15 +588,40 @@ bool p3PeerMgrIMPL::setHiddenDomainPort(const RsPeerId &ssl_id, const std::strin
return true;
}
bool p3PeerMgrIMPL::setProxyServerAddress(const struct sockaddr_storage &proxy_addr)
/**
* @brief sets the proxy server address for a hidden service
* @param type hidden service type
* @param proxy_addr proxy address
* @return true on success
*/
bool p3PeerMgrIMPL::setProxyServerAddress(const uint32_t type, const struct sockaddr_storage &proxy_addr)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
if (!sockaddr_storage_same(mProxyServerAddress,proxy_addr))
{
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
mProxyServerAddress = proxy_addr;
switch (type) {
case RS_HIDDEN_TYPE_I2P:
if (!sockaddr_storage_same(mProxyServerAddressI2P, proxy_addr))
{
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
mProxyServerAddressI2P = proxy_addr;
}
break;
case RS_HIDDEN_TYPE_TOR:
if (!sockaddr_storage_same(mProxyServerAddressTor, proxy_addr))
{
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
mProxyServerAddressTor = proxy_addr;
}
break;
case RS_HIDDEN_TYPE_UNKNOWN:
default:
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::setProxyServerAddress() unknown hidden type " << type << " -> false";
std::cerr << std::endl;
#endif
return false;
}
return true;
}
@ -480,21 +637,71 @@ bool p3PeerMgrIMPL::resetOwnExternalAddressList()
return true ;
}
bool p3PeerMgrIMPL::getProxyServerStatus(uint32_t& proxy_status)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
proxy_status = mProxyServerStatus;
return true;
}
bool p3PeerMgrIMPL::getProxyServerAddress(struct sockaddr_storage &proxy_addr)
/**
* @brief returs proxy server status for a hidden service proxy
* @param type hidden service type
* @param proxy_status
* @return true on success
*/
bool p3PeerMgrIMPL::getProxyServerStatus(const uint32_t type, uint32_t& proxy_status)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
proxy_addr = mProxyServerAddress;
switch (type) {
case RS_HIDDEN_TYPE_I2P:
proxy_status = mProxyServerStatusI2P;
break;
case RS_HIDDEN_TYPE_TOR:
proxy_status = mProxyServerStatusTor;
break;
case RS_HIDDEN_TYPE_UNKNOWN:
default:
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::getProxyServerStatus() unknown hidden type " << type << " -> false";
std::cerr << std::endl;
#endif
return false;
}
return true;
}
/**
* @brief returs proxy server address for a hidden service proxy
* @param type hidden service type
* @param proxy_addr
* @return true on success
*/
bool p3PeerMgrIMPL::getProxyServerAddress(const uint32_t type, struct sockaddr_storage &proxy_addr)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
switch (type) {
case RS_HIDDEN_TYPE_I2P:
proxy_addr = mProxyServerAddressI2P;
break;
case RS_HIDDEN_TYPE_TOR:
proxy_addr = mProxyServerAddressTor;
break;
case RS_HIDDEN_TYPE_UNKNOWN:
default:
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::getProxyServerAddress() unknown hidden type " << type << " -> false";
std::cerr << std::endl;
#endif
return false;
}
return true;
}
/**
* @brief looks up the proxy address and domain/port that have to be used when connecting to a peer
* @param ssl_id peer to connect to
* @param proxy_addr proxy address to be used
* @param domain_addr domain to connect to
* @param domain_port port to connect to
* @return true on success
*/
bool p3PeerMgrIMPL::getProxyAddress(const RsPeerId &ssl_id, struct sockaddr_storage &proxy_addr, std::string &domain_addr, uint16_t &domain_port)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
@ -515,7 +722,21 @@ bool p3PeerMgrIMPL::getProxyAddress(const RsPeerId &ssl_id, struct sockaddr_stor
domain_addr = it->second.hiddenDomain;
domain_port = it->second.hiddenPort;
proxy_addr = mProxyServerAddress;
switch (it->second.hiddenType) {
case RS_HIDDEN_TYPE_I2P:
proxy_addr = mProxyServerAddressI2P;
break;
case RS_HIDDEN_TYPE_TOR:
proxy_addr = mProxyServerAddressTor;
break;
case RS_HIDDEN_TYPE_UNKNOWN:
default:
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::getProxyAddress() no valid hidden type (" << it->second.hiddenType << ") for peer id " << ssl_id << " -> false";
std::cerr << std::endl;
#endif
return false;
}
return true;
}
@ -805,7 +1026,7 @@ bool p3PeerMgrIMPL::removeFriend(const RsPgpId &id)
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
/* move to othersList */
bool success = false;
//bool success = false;
std::map<RsPeerId, peerState>::iterator it;
//remove ssl and gpg_ids
for(it = mFriendList.begin(); it != mFriendList.end(); ++it)
@ -822,7 +1043,7 @@ bool p3PeerMgrIMPL::removeFriend(const RsPgpId &id)
mOthersList[it->second.id] = peer;
mStatusChanged = true;
success = true;
//success = true;
}
}
@ -878,7 +1099,7 @@ bool p3PeerMgrIMPL::removeFriend(const RsPeerId &id, bool removePgpId)
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
/* move to othersList */
bool success = false;
//bool success = false;
std::map<RsPeerId, peerState>::iterator it;
//remove ssl and gpg_ids
for(it = mFriendList.begin(); it != mFriendList.end(); ++it)
@ -897,7 +1118,7 @@ bool p3PeerMgrIMPL::removeFriend(const RsPeerId &id, bool removePgpId)
mOthersList[id] = peer;
mStatusChanged = true;
success = true;
//success = true;
}
}
@ -1246,20 +1467,51 @@ bool p3PeerMgrIMPL::addCandidateForOwnExternalAddress(const RsPeerId &from, cons
// * emit a warnign when the address is unknown
// * if multiple peers report the same address => notify the LinkMgr that the external address had changed.
sockaddr_storage addr_filtered ;
sockaddr_storage_copyip(addr_filtered,addr) ;
sockaddr_storage addr_filtered ;
sockaddr_storage_clear(addr_filtered) ;
sockaddr_storage_copyip(addr_filtered,addr) ;
#ifdef PEER_DEBUG
std::cerr << "Own external address is " << sockaddr_storage_iptostring(addr_filtered) << ", as reported by friend " << from << std::endl;
std::cerr << "Own external address is " << sockaddr_storage_iptostring(addr_filtered) << ", as reported by friend " << from << std::endl;
#endif
if(!sockaddr_storage_isExternalNet(addr_filtered))
if(!sockaddr_storage_isExternalNet(addr_filtered))
{
#ifdef PEER_DEBUG
std::cerr << " address is not an external address. Returning false" << std::endl ;
#endif
return false ;
}
// Update a list of own IPs:
// - remove old values for that same peer
// - remove values for non connected peers
{
#ifdef PEER_DEBUG
std::cerr << " address is not an external address. Returning false" << std::endl ;
#endif
return false ;
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
mReportedOwnAddresses[from] = addr_filtered ;
for(std::map<RsPeerId,sockaddr_storage>::iterator it(mReportedOwnAddresses.begin());it!=mReportedOwnAddresses.end();)
if(!mLinkMgr->isOnline(it->first))
{
std::map<RsPeerId,sockaddr_storage>::iterator tmp(it) ;
++tmp ;
mReportedOwnAddresses.erase(it) ;
it=tmp ;
}
else
++it ;
sockaddr_storage current_best_ext_address_guess ;
uint32_t count ;
locked_computeCurrentBestOwnExtAddressCandidate(current_best_ext_address_guess,count) ;
std::cerr << "p3PeerMgr:: Current external address is calculated to be: " << sockaddr_storage_iptostring(current_best_ext_address_guess) << " (simultaneously reported by " << count << " peers)." << std::endl;
}
// now current
sockaddr_storage own_addr ;
@ -1282,10 +1534,56 @@ bool p3PeerMgrIMPL::addCandidateForOwnExternalAddress(const RsPeerId &from, cons
RsServer::notify()->AddFeedItem(RS_FEED_ITEM_SEC_IP_WRONG_EXTERNAL_IP_REPORTED, from.toStdString(), sockaddr_storage_iptostring(own_addr), sockaddr_storage_iptostring(addr));
}
// we could also sweep over all connected friends and see if some report a different address.
return true ;
}
bool p3PeerMgrIMPL::locked_computeCurrentBestOwnExtAddressCandidate(sockaddr_storage& addr, uint32_t& count)
{
std::map<sockaddr_storage,ZeroedInt> addr_counts ;
for(std::map<RsPeerId,sockaddr_storage>::iterator it(mReportedOwnAddresses.begin());it!=mReportedOwnAddresses.end();++it)
++addr_counts[it->second].n ;
#ifdef PEER_DEBUG
std::cerr << "Current ext addr statistics:" << std::endl;
#endif
count = 0 ;
for(std::map<sockaddr_storage,ZeroedInt>::const_iterator it(addr_counts.begin());it!=addr_counts.end();++it)
{
if(uint32_t(it->second.n) > count)
{
addr = it->first ;
count = it->second.n ;
}
#ifdef PEER_DEBUG
std::cerr << sockaddr_storage_iptostring(it->first) << " : " << it->second.n << std::endl;
#endif
}
return true ;
}
bool p3PeerMgrIMPL::getExtAddressReportedByFriends(sockaddr_storage &addr, uint8_t& isstable)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
uint32_t count ;
locked_computeCurrentBestOwnExtAddressCandidate(addr,count) ;
#ifdef PEER_DEBUG
std::cerr << "Estimation count = " << count << ". Trusted? = " << (count>=2) << std::endl;
#endif
return count >= 2 ;// 2 is not conservative enough. 3 should be probably better.
}
static bool cleanIpList(std::list<pqiIpAddress>& lst,const RsPeerId& pid,p3LinkMgr *link_mgr)
{
bool changed = false ;
@ -1620,9 +1918,10 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list<RsItem *>& saveData)
cleanup = false;
bool useExtAddrFinder = mNetMgr->getIPServersEnabled();
// Store Proxy Server.
struct sockaddr_storage proxy_addr;
getProxyServerAddress(proxy_addr);
/* gather these information before mPeerMtx is locked! */
struct sockaddr_storage proxy_addr_tor, proxy_addr_i2p;
getProxyServerAddress(RS_HIDDEN_TYPE_TOR, proxy_addr_tor);
getProxyServerAddress(RS_HIDDEN_TYPE_I2P, proxy_addr_i2p);
mPeerMtx.lock(); /****** MUTEX LOCKED *******/
@ -1733,17 +2032,33 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list<RsItem *>& saveData)
vitem->tlvkvs.pairs.push_back(kv) ;
// Store Proxy Server.
// Tor
#ifdef PEER_DEBUG
std::cerr << "Saving proxyServerAddress: " << sockaddr_storage_tostring(proxy_addr);
std::cerr << "Saving proxyServerAddress for Tor: " << sockaddr_storage_tostring(proxy_addr_tor);
std::cerr << std::endl;
#endif
kv.key = kConfigKeyProxyServerIpAddr;
kv.value = sockaddr_storage_iptostring(proxy_addr);
kv.key = kConfigKeyProxyServerIpAddrTor;
kv.value = sockaddr_storage_iptostring(proxy_addr_tor);
vitem->tlvkvs.pairs.push_back(kv) ;
kv.key = kConfigKeyProxyServerPort;
kv.value = sockaddr_storage_porttostring(proxy_addr);
kv.key = kConfigKeyProxyServerPortTor;
kv.value = sockaddr_storage_porttostring(proxy_addr_tor);
vitem->tlvkvs.pairs.push_back(kv) ;
// I2P
#ifdef PEER_DEBUG
std::cerr << "Saving proxyServerAddress for I2P: " << sockaddr_storage_tostring(proxy_addr_i2p);
std::cerr << std::endl;
#endif
kv.key = kConfigKeyProxyServerIpAddrI2P;
kv.value = sockaddr_storage_iptostring(proxy_addr_i2p);
vitem->tlvkvs.pairs.push_back(kv) ;
kv.key = kConfigKeyProxyServerPortI2P;
kv.value = sockaddr_storage_porttostring(proxy_addr_i2p);
vitem->tlvkvs.pairs.push_back(kv) ;
saveData.push_back(vitem);
@ -1779,8 +2094,10 @@ bool p3PeerMgrIMPL::loadList(std::list<RsItem *>& load)
// DEFAULTS.
bool useExtAddrFinder = true;
std::string proxyIpAddress = kConfigDefaultProxyServerIpAddr;
uint16_t proxyPort = kConfigDefaultProxyServerPort;
std::string proxyIpAddressTor = kConfigDefaultProxyServerIpAddr;
uint16_t proxyPortTor = kConfigDefaultProxyServerPortTor;
std::string proxyIpAddressI2P = kConfigDefaultProxyServerIpAddr;
uint16_t proxyPortI2P = kConfigDefaultProxyServerPortI2P;
if (load.empty()) {
std::cerr << "p3PeerMgrIMPL::loadList() list is empty, it may be a configuration problem." << std::endl;
@ -1876,20 +2193,38 @@ bool p3PeerMgrIMPL::loadList(std::list<RsItem *>& load)
std::cerr << "setting use_extr_addr_finder to " << useExtAddrFinder << std::endl ;
#endif
}
else if (kit->key == kConfigKeyProxyServerIpAddr)
// Tor
else if (kit->key == kConfigKeyProxyServerIpAddrTor)
{
proxyIpAddress = kit->value;
proxyIpAddressTor = kit->value;
#ifdef PEER_DEBUG
std::cerr << "Loaded proxyIpAddress: " << proxyIpAddress;
std::cerr << "Loaded proxyIpAddress for Tor: " << proxyIpAddressTor;
std::cerr << std::endl ;
#endif
}
else if (kit->key == kConfigKeyProxyServerPort)
else if (kit->key == kConfigKeyProxyServerPortTor)
{
proxyPort = atoi(kit->value.c_str());
proxyPortTor = atoi(kit->value.c_str());
#ifdef PEER_DEBUG
std::cerr << "Loaded proxyPort: " << proxyPort;
std::cerr << "Loaded proxyPort for Tor: " << proxyPortTor;
std::cerr << std::endl ;
#endif
}
// I2p
else if (kit->key == kConfigKeyProxyServerIpAddrI2P)
{
proxyIpAddressI2P = kit->value;
#ifdef PEER_DEBUG
std::cerr << "Loaded proxyIpAddress for I2P: " << proxyIpAddressI2P;
std::cerr << std::endl ;
#endif
}
else if (kit->key == kConfigKeyProxyServerPortI2P)
{
proxyPortI2P = atoi(kit->value.c_str());
#ifdef PEER_DEBUG
std::cerr << "Loaded proxyPort for I2P: " << proxyPortI2P;
std::cerr << std::endl ;
#endif
}
@ -2005,15 +2340,27 @@ bool p3PeerMgrIMPL::loadList(std::list<RsItem *>& load)
// Configure Proxy Server.
struct sockaddr_storage proxy_addr;
// Tor
sockaddr_storage_clear(proxy_addr);
sockaddr_storage_ipv4_aton(proxy_addr, proxyIpAddress.c_str());
sockaddr_storage_ipv4_setport(proxy_addr, proxyPort);
sockaddr_storage_ipv4_aton(proxy_addr, proxyIpAddressTor.c_str());
sockaddr_storage_ipv4_setport(proxy_addr, proxyPortTor);
if (sockaddr_storage_isValidNet(proxy_addr))
{
setProxyServerAddress(proxy_addr);
setProxyServerAddress(RS_HIDDEN_TYPE_TOR, proxy_addr);
}
// I2P
sockaddr_storage_clear(proxy_addr);
sockaddr_storage_ipv4_aton(proxy_addr, proxyIpAddressI2P.c_str());
sockaddr_storage_ipv4_setport(proxy_addr, proxyPortI2P);
if (sockaddr_storage_isValidNet(proxy_addr))
{
setProxyServerAddress(RS_HIDDEN_TYPE_I2P, proxy_addr);
}
load.clear() ;
return true;
}

View file

@ -90,6 +90,7 @@ class peerState
bool hiddenNode; /* all IP addresses / dyndns must be blank */
std::string hiddenDomain;
uint16_t hiddenPort;
uint32_t hiddenType;
std::string location;
std::string name;
@ -153,6 +154,7 @@ virtual bool setLocalAddress(const RsPeerId &id, const struct sockaddr_storage
virtual bool setExtAddress(const RsPeerId &id, const struct sockaddr_storage &addr) = 0;
virtual bool setDynDNS(const RsPeerId &id, const std::string &dyndns) = 0;
virtual bool addCandidateForOwnExternalAddress(const RsPeerId& from, const struct sockaddr_storage &addr) = 0;
virtual bool getExtAddressReportedByFriends(struct sockaddr_storage& addr,uint8_t& isstable) = 0;
virtual bool setNetworkMode(const RsPeerId &id, uint32_t netMode) = 0;
virtual bool setVisState(const RsPeerId &id, uint16_t vs_disc, uint16_t vs_dht) = 0;
@ -185,12 +187,16 @@ virtual bool getPeerName(const RsPeerId &ssl_id, std::string &name) = 0;
virtual bool getGpgId(const RsPeerId &sslId, RsPgpId &gpgId) = 0;
virtual uint32_t getConnectionType(const RsPeerId &sslId) = 0;
virtual bool setProxyServerAddress(const struct sockaddr_storage &proxy_addr) = 0;
virtual bool getProxyServerAddress(struct sockaddr_storage &proxy_addr) = 0;
virtual bool getProxyServerStatus(uint32_t& status) = 0;
virtual bool setProxyServerAddress(const uint32_t type, const struct sockaddr_storage &proxy_addr) = 0;
virtual bool getProxyServerAddress(const uint32_t type, struct sockaddr_storage &proxy_addr) = 0;
virtual bool getProxyServerStatus(const uint32_t type, uint32_t& status) = 0;
virtual bool isHidden() = 0;
virtual bool isHidden(const uint32_t type) = 0;
virtual bool isHiddenPeer(const RsPeerId &ssl_id) = 0;
virtual bool isHiddenPeer(const RsPeerId &ssl_id, const uint32_t type) = 0;
virtual bool getProxyAddress(const RsPeerId &ssl_id, struct sockaddr_storage &proxy_addr, std::string &domain_addr, uint16_t &domain_port) = 0;
virtual uint32_t hiddenDomainToHiddenType(const std::string &domain) = 0;
virtual uint32_t getHiddenType(const RsPeerId &ssl_id) = 0;
virtual int getFriendCount(bool ssl, bool online) = 0;
@ -200,6 +206,7 @@ virtual int getFriendCount(bool ssl, bool online) = 0;
// Single Use Function... shouldn't be here. used by p3serverconfig.cc
virtual bool haveOnceConnected() = 0;
virtual bool locked_computeCurrentBestOwnExtAddressCandidate(sockaddr_storage &addr, uint32_t &count)=0;
/*************************************************************************************************/
/*************************************************************************************************/
@ -256,6 +263,7 @@ virtual bool setLocalAddress(const RsPeerId &id, const struct sockaddr_storage
virtual bool setExtAddress(const RsPeerId &id, const struct sockaddr_storage &addr);
virtual bool setDynDNS(const RsPeerId &id, const std::string &dyndns);
virtual bool addCandidateForOwnExternalAddress(const RsPeerId& from, const struct sockaddr_storage &addr) ;
virtual bool getExtAddressReportedByFriends(struct sockaddr_storage& addr, uint8_t &isstable) ;
virtual bool setNetworkMode(const RsPeerId &id, uint32_t netMode);
virtual bool setVisState(const RsPeerId &id, uint16_t vs_disc, uint16_t vs_dht);
@ -288,12 +296,16 @@ virtual bool getPeerName(const RsPeerId& ssl_id, std::string& name);
virtual bool getGpgId(const RsPeerId& sslId, RsPgpId& gpgId);
virtual uint32_t getConnectionType(const RsPeerId& sslId);
virtual bool setProxyServerAddress(const struct sockaddr_storage &proxy_addr);
virtual bool getProxyServerAddress(struct sockaddr_storage &proxy_addr);
virtual bool getProxyServerStatus(uint32_t &proxy_status);
virtual bool setProxyServerAddress(const uint32_t type, const struct sockaddr_storage &proxy_addr);
virtual bool getProxyServerAddress(const uint32_t type, struct sockaddr_storage &proxy_addr);
virtual bool getProxyServerStatus(const uint32_t type, uint32_t &proxy_status);
virtual bool isHidden();
virtual bool isHiddenPeer(const RsPeerId& ssl_id);
virtual bool isHidden(const uint32_t type);
virtual bool isHiddenPeer(const RsPeerId &ssl_id);
virtual bool isHiddenPeer(const RsPeerId &ssl_id, const uint32_t type);
virtual bool getProxyAddress(const RsPeerId& ssl_id, struct sockaddr_storage &proxy_addr, std::string &domain_addr, uint16_t &domain_port);
virtual uint32_t hiddenDomainToHiddenType(const std::string &domain);
virtual uint32_t getHiddenType(const RsPeerId &ssl_id);
virtual int getFriendCount(bool ssl, bool online);
@ -327,6 +339,7 @@ int getConnectAddresses(const RsPeerId &id,
struct sockaddr_storage &lAddr, struct sockaddr_storage &eAddr,
pqiIpAddrSet &histAddrs, std::string &dyndns);
protected:
/* Internal Functions */
@ -335,6 +348,8 @@ bool removeBannedIps();
void printPeerLists(std::ostream &out);
virtual bool locked_computeCurrentBestOwnExtAddressCandidate(sockaddr_storage &addr, uint32_t &count);
protected:
/*****************************************************************/
/*********************** p3config ******************************/
@ -349,7 +364,7 @@ void printPeerLists(std::ostream &out);
p3LinkMgrIMPL *mLinkMgr;
p3NetMgrIMPL *mNetMgr;
private:
RsMutex mPeerMtx; /* protects below */
@ -362,6 +377,8 @@ private:
std::map<RsPeerId, peerState> mFriendList; // <SSLid , peerState>
std::map<RsPeerId, peerState> mOthersList;
std::map<RsPeerId,sockaddr_storage> mReportedOwnAddresses ;
std::list<RsPeerGroupItem *> groupList;
uint32_t lastGroupId;
@ -369,8 +386,10 @@ private:
std::map<RsPgpId, ServicePermissionFlags> mFriendsPermissionFlags ; // permission flags for each gpg key
struct sockaddr_storage mProxyServerAddress;
uint32_t mProxyServerStatus ;
struct sockaddr_storage mProxyServerAddressTor;
struct sockaddr_storage mProxyServerAddressI2P;
uint32_t mProxyServerStatusTor ;
uint32_t mProxyServerStatusI2P ;
};

View file

@ -148,7 +148,7 @@ public:
uint32_t tmp ;
ok &= getRawUInt32(data, rssize, &offset, &tmp);
for(int i=0;i<tmp && offset < rssize;++i)
for(uint32_t i=0;i<tmp && offset < rssize;++i)
{
RsPeerId peer_id ;
ok &= peer_id.deserialise(data,rssize,offset) ;
@ -157,7 +157,7 @@ public:
ok &= getRawUInt32(data, rssize, &offset, &tmp);
for(int i=0;i<tmp && offset < rssize;++i)
for(uint32_t i=0;i<tmp && offset < rssize;++i)
{
RsPeerId peer_id ;
ok &= peer_id.deserialise(data,rssize,offset) ;
@ -1180,12 +1180,13 @@ bool p3ServiceControl::loadList(std::list<RsItem *>& loadList)
{
RsServicePermissionItem *item = dynamic_cast<RsServicePermissionItem*>(*it) ;
if(item == NULL)
continue ;
mServicePermissionMap[item->mServiceId] = *item ;
if(item != NULL)
mServicePermissionMap[item->mServiceId] = *item ;
delete *it ;
}
loadList.clear() ;
return true;
}

View file

@ -225,18 +225,14 @@ class PQInterface: public RateInterface
/**
* also there are tick + person id functions.
*/
virtual int tick() { return 0; }
virtual int status() { return 0; }
virtual int tick() { return 0; }
virtual int status() { return 0; }
virtual const RsPeerId& PeerId() { return peerId; }
// the callback from NetInterface Connection Events.
virtual int notifyEvent(NetInterface *ni, int event, const struct sockaddr_storage &remote_peer_address)
{
(void) ni; /* remove unused parameter warnings */
(void) event; /* remove unused parameter warnings */
(void) remote_peer_address;
return 0;
}
virtual int notifyEvent(NetInterface * /*ni*/, int /*event*/,
const sockaddr_storage & /*remote_peer_address*/)
{ return 0; }
private:
@ -249,7 +245,8 @@ class PQInterface: public RateInterface
const uint32_t PQI_CONNECT_TCP = 0x0001;
const uint32_t PQI_CONNECT_UDP = 0x0002;
const uint32_t PQI_CONNECT_HIDDEN_TCP = 0x0004;
const uint32_t PQI_CONNECT_HIDDEN_TOR_TCP = 0x0004;
const uint32_t PQI_CONNECT_HIDDEN_I2P_TCP = 0x0008;
#define BIN_FLAGS_NO_CLOSE 0x0001
@ -266,57 +263,57 @@ const uint32_t PQI_CONNECT_HIDDEN_TCP = 0x0004;
class BinInterface
{
public:
BinInterface() { return; }
virtual ~BinInterface() { return; }
BinInterface() {}
virtual ~BinInterface() {}
/**
* To be called loop, for updating state
*/
virtual int tick() = 0;
/**
* To be called loop, for updating state
*/
virtual int tick() = 0;
/**
* Sends data to a prescribed location (implementation dependent)
*@param data what will be sent
*@param len the size of data pointed to in memory
*/
virtual int senddata(void *data, int len) = 0;
/**
* Sends data to a prescribed location (implementation dependent)
*@param data what will be sent
*@param len the size of data pointed to in memory
*/
virtual int senddata(void *data, int len) = 0;
/**
* reads data from a prescribed location (implementation dependent)
*@param data what will be sent
*@param len the size of data pointed to in memory
*/
virtual int readdata(void *data, int len) = 0;
/**
* reads data from a prescribed location (implementation dependent)
*@param data what will be sent
*@param len the size of data pointed to in memory
*/
virtual int readdata(void *data, int len) = 0;
/**
* Is more particular the case of the sending data through a socket (internet)
* moretoread and candsend, take a microsec timeout argument.
*
*/
virtual int netstatus() = 0;
virtual int isactive() = 0;
virtual bool moretoread(uint32_t usec) = 0;
virtual bool cansend(uint32_t usec) = 0;
/**
* Is more particular the case of the sending data through a socket (internet)
* moretoread and candsend, take a microsec timeout argument.
*
*/
virtual int netstatus() = 0;
virtual int isactive() = 0;
virtual bool moretoread(uint32_t usec) = 0;
virtual bool cansend(uint32_t usec) = 0;
/**
* method for streamer to shutdown bininterface
**/
virtual int close() = 0;
/**
* method for streamer to shutdown bininterface
**/
virtual int close() = 0;
/**
* If hashing data
**/
virtual RsFileHash gethash() = 0;
/**
* If hashing data
**/
virtual RsFileHash gethash() = 0;
/**
* Number of bytes read/sent
*/
virtual uint64_t bytecount() { return 0; }
/**
* Number of bytes read/sent
*/
virtual uint64_t bytecount() { return 0; }
/**
* used by pqistreamer to limit transfers
**/
virtual bool bandwidthLimited() { return true; }
/**
* used by pqistreamer to limit transfers
**/
virtual bool bandwidthLimited() { return true; }
};
@ -359,26 +356,30 @@ public:
/**
* @param p_in used to notify system of connect/disconnect events
*/
NetInterface(PQInterface *p_in, const RsPeerId& id)
:p(p_in), peerId(id) { return; }
NetInterface(PQInterface *p_in, const RsPeerId& id) : p(p_in), peerId(id) {}
virtual ~NetInterface()
{ return; }
virtual ~NetInterface() {}
virtual int connect(const struct sockaddr_storage &raddr) = 0;
virtual int listen() = 0;
virtual int stoplistening() = 0;
virtual int disconnect() = 0;
virtual int reset() = 0;
virtual const RsPeerId& PeerId() { return peerId; }
virtual int getConnectAddress(struct sockaddr_storage &raddr) = 0;
/* TODO
* The data entrypoint is connect(const struct sockaddr_storage &raddr)
* To generalize NetInterface we should have a more general type for raddr
* As an example a string containing an url or encoded like a domain name
*/
virtual int connect(const struct sockaddr_storage &raddr) = 0;
virtual bool connect_parameter(uint32_t type, uint32_t value) = 0;
virtual bool connect_parameter(uint32_t /* type */ , const std::string & /* value */ ) { return false; } // not generally used.
virtual bool connect_additional_address(uint32_t /*type*/, const struct sockaddr_storage & /*addr*/) { return false; } // only needed by udp.
virtual int listen() = 0;
virtual int stoplistening() = 0;
virtual int disconnect() = 0;
virtual int reset() = 0;
virtual const RsPeerId& PeerId() { return peerId; }
virtual int getConnectAddress(struct sockaddr_storage &raddr) = 0;
virtual bool connect_parameter(uint32_t type, uint32_t value) = 0;
virtual bool connect_parameter(uint32_t /* type */ , const std::string & /* value */ ) { return false; } // not generally used.
virtual bool connect_additional_address(uint32_t /*type*/, const struct sockaddr_storage & /*addr*/) { return false; } // only needed by udp.
protected:
PQInterface *parent() { return p; }
PQInterface *parent() { return p; }
private:
PQInterface *p;
@ -396,10 +397,9 @@ private:
class NetBinInterface: public NetInterface, public BinInterface
{
public:
NetBinInterface(PQInterface *parent, const RsPeerId& id)
:NetInterface(parent, id)
{ return; }
virtual ~NetBinInterface() { return; }
NetBinInterface(PQInterface *parent, const RsPeerId& id) :
NetInterface(parent, id) {}
virtual ~NetBinInterface() {}
};
#define CHAN_SIGN_SIZE 16

View file

@ -500,7 +500,7 @@ void printNetBinID(std::ostream &out, const RsPeerId& id, uint32_t t)
{
out << "TCP)";
}
else if (t == PQI_CONNECT_HIDDEN_TCP)
else if (t & (PQI_CONNECT_HIDDEN_TOR_TCP | PQI_CONNECT_HIDDEN_I2P_TCP))
{
out << "HTCP";
}
@ -544,7 +544,7 @@ int NetBinDummy::connect(const struct sockaddr_storage &raddr)
std::cerr << std::endl;
if (parent())
{
struct sockaddr_storage addr = raddr;
//struct sockaddr_storage addr = raddr;
parent()->notifyEvent(this, CONNECT_FAILED, raddr);
}
}

View file

@ -29,6 +29,34 @@
#include "util/rsstring.h"
#include <stdlib.h>
#include <time.h>
using std::dec;
#include <time.h>
#include <sys/time.h>
#ifdef WINDOWS_SYS
#include <sys/timeb.h>
#endif
//#define PQI_HDL_DEBUG_UR 1
#ifdef PQI_HDL_DEBUG_UR
static double getCurrentTS()
{
#ifndef WINDOWS_SYS
struct timeval cts_tmp;
gettimeofday(&cts_tmp, NULL);
double cts = (cts_tmp.tv_sec) + ((double) cts_tmp.tv_usec) / 1000000.0;
#else
struct _timeb timebuf;
_ftime( &timebuf);
double cts = (timebuf.time) + ((double) timebuf.millitm) / 1000.0;
#endif
return cts;
}
#endif
const int pqihandlerzone = 34283;
static const int PQI_HANDLER_NB_PRIORITY_LEVELS = 10 ;
@ -60,7 +88,7 @@ int pqihandler::tick()
{
int moreToTick = 0;
{
{
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
// tick all interfaces...
@ -206,10 +234,10 @@ int pqihandler::locked_HandleRsItem(RsItem *item, uint32_t& computed_size)
{
computed_size = 0 ;
std::map<RsPeerId, SearchModule *>::iterator it;
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
"pqihandler::HandleRsItem()");
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
"pqihandler::HandleRsItem() Sending to One Channel");
#ifdef DEBUG_TICK
std::cerr << "pqihandler::HandleRsItem() Sending to One Channel" << std::endl;
@ -245,7 +273,7 @@ int pqihandler::SendRsRawItem(RsRawItem *ns)
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "pqihandler::SendRsRawItem()");
// directly send item to streamers
return queueOutRsItem(ns) ;
}
@ -274,7 +302,7 @@ int pqihandler::locked_GetItems()
SearchModule *mod = (it -> second);
// check security... is output allowed.
if(0 < secpolicy_check((it -> second) -> sp,
if(0 < secpolicy_check((it -> second) -> sp,
0, PQI_INCOMING)) // PQI_ITEM_TYPE_ITEM, PQI_INCOMING))
{
// if yes... attempt to read.
@ -289,19 +317,19 @@ int pqihandler::locked_GetItems()
std::cerr << std::endl;
}
#ifdef RSITEM_DEBUG
#ifdef RSITEM_DEBUG
std::string out;
rs_sprintf(out, "pqihandler::GetItems() Incoming Item from: %p\n", mod -> pqi);
item -> print_string(out);
pqioutput(PQL_DEBUG_BASIC,
pqioutput(PQL_DEBUG_BASIC,
pqihandlerzone, out);
#endif
if (item->PeerId() != (mod->pqi)->PeerId())
{
/* ERROR */
pqioutput(PQL_ALERT,
pqioutput(PQL_ALERT,
pqihandlerzone, "ERROR PeerIds dont match!");
item->PeerId(mod->pqi->PeerId());
}
@ -338,7 +366,7 @@ void pqihandler::locked_SortnStoreItem(RsItem *item)
/* whole Version reserved for SERVICES/CACHES */
if (vers == RS_PKT_VERSION_SERVICE)
{
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
"SortnStore -> Service");
in_service.push_back(item);
item = NULL;
@ -440,11 +468,14 @@ int pqihandler::ExtractRates(std::map<RsPeerId, RsBwRates> &ratemap, RsBwRat
// internal fn to send updates
// internal fn to send updates
int pqihandler::UpdateRates()
{
#ifdef PQI_HDL_DEBUG_UR
uint64_t t_now;
#endif
std::map<RsPeerId, SearchModule *>::iterator it;
int num_sm = mods.size();
float avail_in = getMaxRate(true);
float avail_out = getMaxRate(false);
@ -455,28 +486,124 @@ int pqihandler::UpdateRates()
/* Lock once rates have been retrieved */
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
int num_sm = mods.size();
float used_bw_in_table[num_sm]; /* table of in bandwidth currently used by each module */
float used_bw_out_table[num_sm]; /* table of out bandwidth currently used by each module */
int effectiveUploadsSm = 0;
int effectiveDownloadsSm = 0;
// loop through modules to get the used bandwith and the number of modules that are affectively transfering
//std::cerr << " Looping through modules" << std::endl;
#ifdef PQI_HDL_DEBUG_UR
std::cerr << " Looping through modules" << std::endl;
#endif
int index = 0;
for(it = mods.begin(); it != mods.end(); ++it)
{
SearchModule *mod = (it -> second);
float crate_in = mod -> pqi -> getRate(true);
if (crate_in > 0.01 * avail_in || crate_in > 0.1)
if ((crate_in > 0.01 * avail_in) || (crate_in > 0.1))
{
++effectiveDownloadsSm;
}
float crate_out = mod -> pqi -> getRate(false);
if (crate_out > 0.01 * avail_out || crate_out > 0.1)
if ((crate_out > 0.01 * avail_out) || (crate_out > 0.1))
{
++effectiveUploadsSm;
}
used_bw_in += crate_in;
used_bw_out += crate_out;
/* fill the table of bandwidth */
used_bw_in_table[index] = crate_in;
used_bw_out_table[index] = crate_out;
++index;
}
#ifdef PQI_HDL_DEBUG_UR
t_now = 1000 * getCurrentTS();
std::cerr << dec << t_now << " pqihandler::UpdateRates(): Sorting used_bw_out_table: " << num_sm << " entries" << std::endl;
#endif
/* Sort the used bw in/out table in ascending order */
std::sort(used_bw_in_table, used_bw_in_table + num_sm);
std::sort(used_bw_out_table, used_bw_out_table + num_sm);
#ifdef PQI_HDL_DEBUG_UR
t_now = 1000 * getCurrentTS();
std::cerr << dec << t_now << " pqihandler::UpdateRates(): Done." << std::endl;
std::cerr << dec << t_now << " pqihandler::UpdateRates(): used_bw_out " << used_bw_out << std::endl;
#endif
/* Calculate the optimal out_max value, taking into account avail_out and the out bw requested by modules */
float out_remaining_bw = avail_out;
float out_max_bw = 0;
bool keep_going = true;
int mod_index = 0;
while (keep_going && (mod_index < num_sm)) {
float result = (num_sm - mod_index) * (used_bw_out_table[mod_index] - out_max_bw);
if (result > out_remaining_bw) {
/* There is not enough remaining out bw to satisfy all modules,
distribute the remaining out bw among modules, then exit */
out_max_bw += out_remaining_bw / (num_sm - mod_index);
out_remaining_bw = 0;
keep_going = false;
} else {
/* Grant the requested out bandwidth to all modules,
then recalculate the remaining out bandwidth */
out_remaining_bw -= result;
out_max_bw = used_bw_out_table[mod_index];
++mod_index;
}
}
#ifdef PQI_HDL_DEBUG_UR
t_now = 1000 * getCurrentTS();
std::cerr << dec << t_now << " pqihandler::UpdateRates(): mod_index " << mod_index << " out_max_bw " << out_max_bw << " remaining out bw " << out_remaining_bw << std::endl;
#endif
/* Allocate only half the remaining out bw, if any, to make it smoother */
out_max_bw = out_max_bw + out_remaining_bw / 2;
/* Calculate the optimal in_max value, taking into account avail_in and the in bw requested by modules */
float in_remaining_bw = avail_in;
float in_max_bw = 0;
keep_going = true;
mod_index = 0;
while (keep_going && mod_index < num_sm) {
float result = (num_sm - mod_index) * (used_bw_in_table[mod_index] - in_max_bw);
if (result > in_remaining_bw) {
/* There is not enough remaining in bw to satisfy all modules,
distribute the remaining in bw among modules, then exit */
in_max_bw += in_remaining_bw / (num_sm - mod_index);
in_remaining_bw = 0;
keep_going = false;
} else {
/* Grant the requested in bandwidth to all modules,
then recalculate the remaining in bandwidth */
in_remaining_bw -= result;
in_max_bw = used_bw_in_table[mod_index];
++mod_index;
}
}
#ifdef PQI_HDL_DEBUG_UR
t_now = 1000 * getCurrentTS();
std::cerr << dec << t_now << " pqihandler::UpdateRates(): mod_index " << mod_index << " in_max_bw " << in_max_bw << " remaining in bw " << in_remaining_bw << std::endl;
#endif
/* Allocate only half the remaining in bw, if any, to make it smoother */
in_max_bw = in_max_bw + in_remaining_bw / 2;
#ifdef DEBUG_QOS
// std::cerr << "Totals (In) Used B/W " << used_bw_in;
// std::cerr << " Available B/W " << avail_in;
@ -498,51 +625,35 @@ int pqihandler::UpdateRates()
max_out_effective = avail_out / effectiveUploadsSm;
}
//modify the outgoing rates if bandwith is not used well
float rate_out_modifier = 0;
if (used_bw_out / avail_out < 0.95) {
rate_out_modifier = 0.001 * avail_out;
} else if (used_bw_out / avail_out > 1.05) {
rate_out_modifier = - 0.001 * avail_out;
}
if (rate_out_modifier != 0) {
for(it = mods.begin(); it != mods.end(); ++it)
{
SearchModule *mod = (it -> second);
mod -> pqi -> setMaxRate(false, mod -> pqi -> getMaxRate(false) + rate_out_modifier);
}
//modify the in and out limit
#ifdef PQI_HDL_DEBUG_UR
t_now = 1000 * getCurrentTS();
std::cerr << dec << t_now << " pqihandler::UpdateRates(): setting new out_max " << out_max_bw << " in_max " << in_max_bw << std::endl;
#endif
for(it = mods.begin(); it != mods.end(); ++it)
{
SearchModule *mod = (it -> second);
mod -> pqi -> setMaxRate(true, in_max_bw);
mod -> pqi -> setMaxRate(false, out_max_bw);
}
//modify the incoming rates if bandwith is not used well
float rate_in_modifier = 0;
if (used_bw_in / avail_in < 0.95) {
rate_in_modifier = 0.001 * avail_in;
} else if (used_bw_in / avail_in > 1.05) {
rate_in_modifier = - 0.001 * avail_in;
}
if (rate_in_modifier != 0) {
for(it = mods.begin(); it != mods.end(); ++it)
{
SearchModule *mod = (it -> second);
mod -> pqi -> setMaxRate(true, mod -> pqi -> getMaxRate(true) + rate_in_modifier);
}
}
//cap the rates
for(it = mods.begin(); it != mods.end(); ++it)
{
SearchModule *mod = (it -> second);
if (mod -> pqi -> getMaxRate(false) < max_out_effective) {
mod -> pqi -> setMaxRate(false, max_out_effective);
mod -> pqi -> setMaxRate(false, max_out_effective);
}
if (mod -> pqi -> getMaxRate(false) > avail_out) {
mod -> pqi -> setMaxRate(false, avail_out);
mod -> pqi -> setMaxRate(false, avail_out);
}
if (mod -> pqi -> getMaxRate(true) < max_in_effective) {
mod -> pqi -> setMaxRate(true, max_in_effective);
mod -> pqi -> setMaxRate(true, max_in_effective);
}
if (mod -> pqi -> getMaxRate(true) > avail_in) {
mod -> pqi -> setMaxRate(true, avail_in);
mod -> pqi -> setMaxRate(true, avail_in);
}
}

View file

@ -31,21 +31,14 @@
class pqilistener
{
public:
pqilistener() { return; }
virtual ~pqilistener() { return; }
virtual int tick() { return 1; }
virtual int status() { return 1; }
virtual int setListenAddr(const struct sockaddr_storage &addr)
{
(void) addr; /* suppress unused parameter warning */
return 1;
}
virtual int setuplisten() { return 1; }
virtual int resetlisten() { return 1; }
public:
pqilistener() {}
virtual ~pqilistener() {}
virtual int tick() { return 1; }
virtual int status() { return 1; }
virtual int setListenAddr(const sockaddr_storage & /*addr*/) { return 1; }
virtual int setuplisten() { return 1; }
virtual int resetlisten() { return 1; }
};

View file

@ -38,7 +38,8 @@
#include "util/rsdebug.h"
#include "util/rsstring.h"
#include <iomanip>
#include "util/rsnet.h"
static const int pqinetzone = 96184;
/*****
@ -55,6 +56,11 @@ int errno;
#endif
#ifdef __HAIKU__
#include <sys/sockio.h>
#define IFF_RUNNING 0x0001
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
@ -144,78 +150,6 @@ std::string socket_errorType(int err)
return std::string("UNKNOWN ERROR CODE - ASK RS-DEVS TO ADD IT!");
}
#include <net/if.h>
#include <sys/ioctl.h>
bool getLocalInterfaces_ipv4(struct in_addr &/*routeAddr*/, std::list<struct in_addr> &addrs)
{
int sock = 0;
struct ifreq ifreq;
struct if_nameindex *iflist = if_nameindex();
struct if_nameindex *ifptr = iflist;
//need a socket for ioctl()
if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
pqioutput(PQL_ALERT, pqinetzone,
"Cannot Determine Local Addresses!");
return false;
}
if (!ifptr)
{
pqioutput(PQL_ALERT, pqinetzone,
"getLocalInterfaces(): ERROR if_nameindex == NULL");
return false;
}
// loop through the interfaces.
for(; ifptr->if_index != 0; ++ifptr)
{
//copy in the interface name to look up address of
strncpy(ifreq.ifr_name, ifptr->if_name, IF_NAMESIZE-1);// the -1 is here to ensure that there's enough room left to place a \0 termination byte
if(ioctl(sock, SIOCGIFADDR, &ifreq) != 0)
{
std::string out;
rs_sprintf(out, "Cannot Determine Address for Iface: %s", ifptr -> if_name);
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out);
}
else
{
struct sockaddr_in *aptr =
(struct sockaddr_in *) &ifreq.ifr_addr;
std::string astr =rs_inet_ntoa(aptr -> sin_addr);
std::string out;
rs_sprintf(out, "Iface: %s\n Address: %s", ifptr -> if_name, astr.c_str());
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out);
// Now check wether the interface is up and running. If not, we don't use it!!
//
if(ioctl(sock,SIOCGIFFLAGS,&ifreq) != 0)
{
std::cerr << "Could not get flags from interface " << ifptr -> if_name << std::endl ;
continue ;
}
#ifdef NET_DEBUG
std::cout << out.str() ;
std::cout << "flags = " << ifreq.ifr_flags << std::endl ;
#endif
if((ifreq.ifr_flags & IFF_UP) == 0) continue ;
if((ifreq.ifr_flags & IFF_RUNNING) == 0) continue ;
addrs.push_back(aptr->sin_addr);
}
}
// free socket -> or else run out of fds.
close(sock);
if_freenameindex(iflist);
return (addrs.size() > 0);
}
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#else
@ -322,80 +256,6 @@ std::string socket_errorType(int err)
return std::string("----WINDOWS OPERATING SYSTEM FAILURE----");
}
#include <iphlpapi.h>
//#include <iprtrmib.h>
// A function to determine the interfaces on your computer....
// No idea of how to do this in windows....
// see if it compiles.
bool getLocalInterfaces_ipv4(struct in_addr &routeAddr, std::list<struct in_addr> &addrs)
{
// Get the best interface for transport to routeAddr
// This interface should be first in list!
DWORD bestInterface;
if (GetBestInterface((IPAddr) routeAddr.s_addr, &bestInterface) != NO_ERROR)
{
bestInterface = 0;
}
/* USE MIB IPADDR Interface */
PMIB_IPADDRTABLE iptable = NULL;
DWORD dwSize = 0;
if (GetIpAddrTable(iptable, &dwSize, 0) != ERROR_INSUFFICIENT_BUFFER)
{
pqioutput(PQL_ALERT, pqinetzone, "Cannot Find Windoze Interfaces!");
exit(0);
}
iptable = (MIB_IPADDRTABLE *) malloc(dwSize);
GetIpAddrTable(iptable, &dwSize, 0);
struct in_addr addr;
for (unsigned int i = 0; i < iptable -> dwNumEntries; i++)
{
MIB_IPADDRROW &ipaddr = iptable->table[i];
std::string out;
addr.s_addr = ipaddr.dwAddr;
rs_sprintf(out, "Iface(%ld) => %s\n", ipaddr.dwIndex, rs_inet_ntoa(addr).c_str());
#if __MINGW_MAJOR_VERSION <= 3 && !defined(__MINGW64_VERSION_MAJOR)
unsigned short wType = ipaddr.unused2; // should be wType
#else
unsigned short wType = ipaddr.wType;
#endif
if (wType & MIB_IPADDR_DISCONNECTED)
{
pqioutput(PQL_DEBUG_BASIC, pqinetzone, "Interface disconnected, " + out);
continue;
}
if (wType & MIB_IPADDR_DELETED)
{
pqioutput(PQL_DEBUG_BASIC, pqinetzone, "Interface deleted, " + out);
continue;
}
if (ipaddr.dwIndex == bestInterface)
{
pqioutput(PQL_DEBUG_BASIC, pqinetzone, "Best address, " + out);
addrs.push_front(addr);
}
else
{
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out);
addrs.push_back(addr);
}
}
free (iptable);
return (addrs.size() > 0);
}
// implement the improved unix inet address fn.
// using old one.
int inet_aton(const char *name, struct in_addr *addr)
@ -403,478 +263,82 @@ int inet_aton(const char *name, struct in_addr *addr)
return (((*addr).s_addr = inet_addr(name)) != INADDR_NONE);
}
// This returns in Net Byte Order.
// NB: Linux man page claims it is in Host Byte order, but
// this is blatantly wrong!..... (for Debian anyway)
// Making this consistent with the Actual behavior (rather than documented).
in_addr_t inet_netof(struct in_addr addr)
{
return pqi_inet_netof(addr);
}
// This returns in Host Byte Order. (as the man page says)
// Again, to be consistent with Linux.
in_addr_t inet_network(const char *inet_name)
{
struct in_addr addr;
if (inet_aton(inet_name, &addr))
{
#ifdef NET_DEBUG
// std::cerr << "inet_network(" << inet_name << ") : ";
// std::cerr << rs_inet_ntoa(addr) << std::endl;
#endif
return ntohl(inet_netof(addr));
}
return 0xffffffff;
//return -1;
}
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#include <iostream>
#include <sys/types.h>
#ifdef WINDOWS_SYS
#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment(lib, "IPHLPAPI.lib")
#else // WINDOWS_SYS
#include <ifaddrs.h>
#include <net/if.h>
#endif // WINDOWS_SYS
// This returns in Net Byte Order.
// NB: Linux man page claims it is in Host Byte order, but
// this is blatantly wrong!..... (for Debian anyway)
// Making this consistent with the Actual behavior (rather than documented).
in_addr_t pqi_inet_netof(struct in_addr addr)
void getLocalAddressesFailed()
{
// decide if A class address.
unsigned long haddr = ntohl(addr.s_addr);
unsigned long abit = haddr & 0xff000000UL;
unsigned long bbit = haddr & 0xffff0000UL;
unsigned long cbit = haddr & 0xffffff00UL;
#ifdef NET_DEBUG
std::cerr << "inet_netof(" << rs_inet_ntoa(addr) << ") ";
#endif
if (!((haddr >> 31) | 0x0UL)) // MSB = 0
{
#ifdef NET_DEBUG
std::cerr << " Type A " << std::endl;
std::cerr << "\tShifted(31): " << (haddr >> 31);
std::cerr << " Xord(0x0UL): " <<
!((haddr >> 31) | 0x0UL) << std::endl;
#endif
return htonl(abit);
}
else if (!((haddr >> 30) ^ 0x2UL)) // 2MSBs = 10
{
#ifdef NET_DEBUG
std::cerr << " Type B " << std::endl;
std::cerr << "\tShifted(30): " << (haddr >> 30);
std::cerr << " Xord(0x2UL): " <<
!((haddr >> 30) | 0x2UL) << std::endl;
#endif
return htonl(bbit);
}
else if (!((haddr >> 29) ^ 0x6UL)) // 3MSBs = 110
{
#ifdef NET_DEBUG
std::cerr << " Type C " << std::endl;
std::cerr << "\tShifted(29): " << (haddr >> 29);
std::cerr << " Xord(0x6UL): " <<
!((haddr >> 29) | 0x6UL) << std::endl;
#endif
return htonl(cbit);
}
else if (!((haddr >> 28) ^ 0xeUL)) // 4MSBs = 1110
{
#ifdef NET_DEBUG
std::cerr << " Type Multicast " << std::endl;
std::cerr << "\tShifted(28): " << (haddr >> 28);
std::cerr << " Xord(0xeUL): " <<
!((haddr >> 29) | 0xeUL) << std::endl;
#endif
return addr.s_addr; // return full address.
}
else if (!((haddr >> 27) ^ 0x1eUL)) // 5MSBs = 11110
{
#ifdef NET_DEBUG
std::cerr << " Type Reserved " << std::endl;
std::cerr << "\tShifted(27): " << (haddr >> 27);
std::cerr << " Xord(0x1eUL): " <<
!((haddr >> 27) | 0x1eUL) << std::endl;
#endif
return addr.s_addr; // return full address.
}
return htonl(abit);
std::cerr << "FATAL ERROR: getLocalAddresses failed!" << std::endl;
exit(1);
}
int sockaddr_cmp(struct sockaddr_in &addr1, struct sockaddr_in &addr2 )
bool getLocalAddresses(std::list<sockaddr_storage> & addrs)
{
if (addr1.sin_family != addr2.sin_family)
return addr1.sin_family - addr2.sin_family;
if (addr1.sin_addr.s_addr != addr2.sin_addr.s_addr)
return (addr1.sin_addr.s_addr - addr2.sin_addr.s_addr);
if (addr1.sin_port != addr2.sin_port)
return (addr1.sin_port - addr2.sin_port);
return 0;
}
addrs.clear();
int inaddr_cmp(struct sockaddr_in addr1, struct sockaddr_in addr2 )
{
#ifdef NET_DEBUG
std::string out;
rs_sprintf(out, "inaddr_cmp(%s-%lu,%s-%lu)", rs_inet_ntoa(addr1.sin_addr).c_str(), addr1.sin_addr.s_addr, rs_inet_ntoa(addr2.sin_addr).c_str(), addr2.sin_addr.s_addr);
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out);
#endif
#ifdef WINDOWS_SYS
// Seems strange to me but M$ documentation suggests to allocate this way...
DWORD bf_size = 16000;
IP_ADAPTER_ADDRESSES* adapter_addresses = (IP_ADAPTER_ADDRESSES*) malloc(bf_size);
DWORD error = GetAdaptersAddresses(AF_UNSPEC,
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_DNS_SERVER |
GAA_FLAG_SKIP_FRIENDLY_NAME,
NULL,
adapter_addresses,
&bf_size);
if (error != ERROR_SUCCESS) getLocalAddressesFailed();
if (addr1.sin_addr.s_addr == addr2.sin_addr.s_addr)
IP_ADAPTER_ADDRESSES* adapter(NULL);
for(adapter = adapter_addresses; NULL != adapter; adapter = adapter->Next)
{
return 0;
IP_ADAPTER_UNICAST_ADDRESS* address;
for ( address = adapter->FirstUnicastAddress; address; address = address->Next)
{
sockaddr_storage tmp;
sockaddr_storage_clear(tmp);
if (sockaddr_storage_copyip(tmp, * reinterpret_cast<sockaddr_storage*>(address->Address.lpSockaddr)))
addrs.push_back(tmp);
}
}
if (addr1.sin_addr.s_addr < addr2.sin_addr.s_addr)
return -1;
return 1;
}
int inaddr_cmp(struct sockaddr_in addr1, unsigned long addr2)
{
#ifdef NET_DEBUG
struct in_addr inaddr_tmp;
inaddr_tmp.s_addr = addr2;
std::string out;
rs_sprintf(out, "inaddr_cmp2(%s vs %s /or/ %10x vs %10x)", rs_inet_ntoa(addr1.sin_addr).c_str(), rs_inet_ntoa(inaddr_tmp).c_str(), addr1.sin_addr.s_addr, addr2);
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out);
#endif
if (addr1.sin_addr.s_addr == addr2)
{
return 0;
}
if (addr1.sin_addr.s_addr < addr2)
return -1;
return 1;
}
bool getPreferredInterface_ipv4(in_addr &routeAddr, struct in_addr &prefAddr) // returns best addr.
{
std::list<struct in_addr> addrs;
std::list<struct in_addr>::iterator it;
struct in_addr addr_zero, addr_loop, addr_priv, addr_ext;
free(adapter_addresses);
#else // WINDOWS_SYS
struct ifaddrs *ifsaddrs, *ifa;
if(getifaddrs(&ifsaddrs) != 0) getLocalAddressesFailed();
for ( ifa = ifsaddrs; ifa; ifa = ifa->ifa_next )
if ( ifa->ifa_addr && (ifa->ifa_flags & IFF_UP) )
{
sockaddr_storage tmp;
sockaddr_storage_clear(tmp);
if (sockaddr_storage_copyip(tmp, * reinterpret_cast<sockaddr_storage*>(ifa->ifa_addr)))
addrs.push_back(tmp);
}
freeifaddrs(ifsaddrs);
#endif // WINDOWS_SYS
#ifdef NET_DEBUG
struct in_addr addr;
#endif
bool found_zero = false;
bool found_loopback = false;
bool found_priv = false;
bool found_ext = false;
if (!getLocalInterfaces_ipv4(routeAddr, addrs))
{
return false;
}
memset(&addr_zero, 0, sizeof(addr_zero));
memset(&addr_loop, 0, sizeof(addr_loop));
memset(&addr_priv, 0, sizeof(addr_priv));
memset(&addr_ext, 0, sizeof(addr_ext));
#ifdef NET_DEBUG
memset(&addr, 0, sizeof(addr));
#endif
// find the first of each of these.
// if ext - take first.
// if no ext -> first priv
// if no priv -> first loopback.
#ifdef NET_DEBUG
std::cerr << "getPreferredInterface() " << addrs.size() << " interfaces." << std::endl;
#endif
std::list<sockaddr_storage>::iterator it;
std::cout << "getLocalAddresses(...) returning: <" ;
for(it = addrs.begin(); it != addrs.end(); ++it)
{
struct in_addr addr = *it;
#ifdef NET_DEBUG
std::cerr << "Examining addr: " << rs_inet_ntoa(addr);
std::cerr << " => " << (uint32_t) addr.s_addr << std::endl ;
std::cout << sockaddr_storage_iptostring(*it) << ", ";
std::cout << ">" << std::endl;
#endif
// for windows silliness (returning 0.0.0.0 as valid addr!).
if (addr.s_addr == 0)
{
if (!found_zero)
{
#ifdef NET_DEBUG
std::cerr << "\tFound Zero Address" << std::endl ;
#endif
found_zero = true;
addr_zero = addr;
}
}
else if (isLoopbackNet(&addr))
{
if (!found_loopback)
{
#ifdef NET_DEBUG
std::cerr << "\tFound Loopback Address" << std::endl ;
#endif
found_loopback = true;
addr_loop = addr;
}
}
else if (isPrivateNet(&addr))
{
if (!found_priv)
{
#ifdef NET_DEBUG
std::cerr << "\tFound Private Address" << std::endl ;
#endif
found_priv = true;
addr_priv = addr;
}
}
else
{
if (!found_ext)
{
#ifdef NET_DEBUG
std::cerr << "\tFound Other Address (Ext?) " << std::endl ;
#endif
found_ext = true;
addr_ext = addr;
}
}
}
if(found_ext) // external address is best.
{
prefAddr = addr_ext;
return true;
}
if (found_priv)
{
prefAddr = addr_priv;
return true;
}
// next bit can happen under windows,
// a general address is still
// preferable to a loopback device.
if (found_zero)
{
prefAddr = addr_zero;
return true;
}
if (found_loopback)
{
prefAddr = addr_loop;
return true;
}
// shound be 255.255.255.255 (error).
prefAddr.s_addr = 0xffffffff;
return false;
return !addrs.empty();
}
bool getPreferredInterface(struct sockaddr_storage &existAddr, struct sockaddr_storage &prefAddr)
{
struct in_addr existing_addr;
struct in_addr pref_addr;
{
struct sockaddr_in *eaddr = (sockaddr_in *) &existAddr;
if (eaddr->sin_family != AF_INET)
{
std::cerr << "getPreferredInterface() ERROR only valid for IPv4 for now";
abort();
return false;
}
existing_addr = eaddr->sin_addr;
}
if (getPreferredInterface_ipv4(existing_addr, pref_addr))
{
/* store into prefAddr */
sockaddr_storage_clear(prefAddr);
struct sockaddr_in *addr = (sockaddr_in *) &prefAddr;
addr->sin_family = AF_INET;
addr->sin_addr = pref_addr;
addr->sin_port = htons(0);
return true;
}
return false;
}
bool getLocalInterfaces(struct sockaddr_storage &existAddr, std::list<struct sockaddr_storage> &addrs)
{
struct in_addr existing_addr;
std::list<struct in_addr> local_addrs;
{
struct sockaddr_in *eaddr = (sockaddr_in *) &existAddr;
if (eaddr->sin_family != AF_INET)
{
std::cerr << "getLocalInterfaces() ERROR only valid for IPv4 for now";
abort();
return false;
}
existing_addr = eaddr->sin_addr;
}
if (getLocalInterfaces_ipv4(existing_addr, local_addrs))
{
std::list<struct in_addr>::iterator it;
for(it = local_addrs.begin(); it != local_addrs.end(); ++it)
{
/* store into prefAddr */
sockaddr_storage localAddr;
sockaddr_storage_clear(localAddr);
struct sockaddr_in *addr = (sockaddr_in *) &localAddr;
addr->sin_family = AF_INET;
addr->sin_addr = *it;
addr->sin_port = htons(0);
addrs.push_back(localAddr);
}
return true;
}
return false;
}
bool sameNet(const struct in_addr *addr, const struct in_addr *addr2)
{
#ifdef NET_DEBUG
std::cerr << "sameNet: " << rs_inet_ntoa(*addr);
std::cerr << " VS " << rs_inet_ntoa(*addr2);
std::cerr << std::endl;
#endif
struct in_addr addrnet, addrnet2;
addrnet.s_addr = inet_netof(*addr);
addrnet2.s_addr = inet_netof(*addr2);
#ifdef NET_DEBUG
std::cerr << " (" << rs_inet_ntoa(addrnet);
std::cerr << " =?= " << rs_inet_ntoa(addrnet2);
std::cerr << ")" << std::endl;
#endif
in_addr_t address1 = htonl(addr->s_addr);
in_addr_t address2 = htonl(addr2->s_addr);
// handle case for private net: 172.16.0.0/12
if (address1>>20 == (172<<4 | 16>>4))
{
return (address1>>20 == address2>>20);
}
return (inet_netof(*addr) == inet_netof(*addr2));
}
bool isSameSubnet(struct in_addr *addr1, struct in_addr *addr2)
{
/*
* check that the (addr1 & 255.255.255.0) == (addr2 & 255.255.255.0)
*/
unsigned long a1 = ntohl(addr1->s_addr);
unsigned long a2 = ntohl(addr2->s_addr);
return ((a1 & 0xffffff00) == (a2 & 0xffffff00));
}
/* This just might be portable!!! will see!!!
* Unfortunately this is usable on winXP+, determined by: (_WIN32_WINNT >= 0x0501)
* but not older platforms.... which must use gethostbyname.
*
* include it for now.....
*/
bool LookupDNSAddr(std::string name, struct sockaddr_in &addr)
{
#if 1
char service[100];
struct addrinfo hints_st;
struct addrinfo *hints = &hints_st;
struct addrinfo *res;
hints -> ai_flags = 0; // (cygwin don;t like these) AI_ADDRCONFIG | AI_NUMERICSERV;
hints -> ai_family = AF_INET;
hints -> ai_socktype = 0;
hints -> ai_protocol = 0;
hints -> ai_addrlen = 0;
hints -> ai_addr = NULL;
hints -> ai_canonname = NULL;
hints -> ai_next = NULL;
/* get the port number */
sprintf(service, "%d", ntohs(addr.sin_port));
/* set it to IPV4 */
#ifdef NET_DEBUG
std::cerr << "LookupDNSAddr() name: " << name << " service: " << service << std::endl;
#endif
int err = 0;
if (0 != (err = getaddrinfo(name.c_str(), service, hints, &res)))
{
#ifdef NET_DEBUG
std::cerr << "LookupDNSAddr() getaddrinfo failed!" << std::endl;
std::cerr << "Error: " << gai_strerror(err) << std::endl;
#endif
return false;
}
if (res)
{
if (res->ai_family == AF_INET)
{
addr = *((struct sockaddr_in *) res->ai_addr);
freeaddrinfo(res);
#ifdef NET_DEBUG
std::cerr << "LookupDNSAddr() getaddrinfo found address" << std::endl;
std::cerr << "addr: " << rs_inet_ntoa(addr.sin_addr) << std::endl;
std::cerr << "port: " << ntohs(addr.sin_port) << std::endl;
#endif
return true;
}
freeaddrinfo(res);
}
#ifdef NET_DEBUG
std::cerr << "getaddrinfo failed - no address" << std::endl;
#endif
#endif
#ifdef NET_DEBUG
//std::cerr << "getaddrinfo disabled" << std::endl;
#endif
return false;
}
/*************************************************************
* Socket Library Wrapper Functions
* to get over the crapness of the windows.
@ -899,16 +363,14 @@ int unix_close(int fd)
return ret;
}
int unix_socket(int /*domain*/, int /*type*/, int /*protocol*/)
int unix_socket(int domain, int type, int protocol)
{
int osock = socket(PF_INET, SOCK_STREAM, 0);
/******************* WINDOWS SPECIFIC PART ******************/
#ifdef WINDOWS_SYS // WINDOWS
int osock = socket(domain, type, protocol);
#ifdef WINDOWS_SYS
#ifdef NET_DEBUG
std::cerr << "unix_socket()" << std::endl;
#endif
#endif // NET_DEBUG
if ((unsigned) osock == INVALID_SOCKET)
{
@ -916,8 +378,8 @@ int unix_socket(int /*domain*/, int /*type*/, int /*protocol*/)
osock = -1;
errno = WinToUnixError(WSAGetLastError());
}
#endif
/******************* WINDOWS SPECIFIC PART ******************/
#endif // WINDOWS_SYS
return osock;
}

View file

@ -38,6 +38,7 @@
#include <arpa/inet.h>
#include <sys/poll.h>
#include <errno.h>
//socket blocking/options.
#include <fcntl.h>
@ -48,9 +49,6 @@
#include "util/rsnet.h" /* more generic networking header */
// Some Network functions that are missing from windows.
in_addr_t inet_netof(struct in_addr addr);
in_addr_t inet_network(const char *inet_name);
int inet_aton(const char *name, struct in_addr *addr);
extern int errno; /* Define extern errno, to duplicate unix behaviour */
@ -93,24 +91,11 @@ extern int errno; /* Define extern errno, to duplicate unix behaviour */
#include <list>
// Same def - different functions...
void showSocketError(std::string &out);
std::string socket_errorType(int err);
int sockaddr_cmp(struct sockaddr_in &addr1, struct sockaddr_in &addr2 );
int inaddr_cmp(struct sockaddr_in addr1, struct sockaddr_in addr2 );
int inaddr_cmp(struct sockaddr_in addr1, unsigned long);
bool getPreferredInterface(struct sockaddr_storage &existAddr, struct sockaddr_storage &prefAddr); // returns best addr.
bool getLocalInterfaces(struct sockaddr_storage &existAddr, std::list<struct sockaddr_storage> &addrs); // returns all possible addrs.
// checks (addr1 & 255.255.255.0) == (addr2 & 255.255.255.0)
bool isSameSubnet(struct in_addr *addr1, struct in_addr *addr2);
bool sameNet(const struct in_addr *addr, const struct in_addr *addr2);
in_addr_t pqi_inet_netof(struct in_addr addr); // our implementation.
bool LookupDNSAddr(std::string name, struct sockaddr_in &addr);
bool getLocalAddresses(std::list<struct sockaddr_storage> & addrs);
/* universal socket interface */

View file

@ -38,23 +38,16 @@ const int pqipersonzone = 82371;
* #define PERSON_DEBUG 1
****/
pqiperson::pqiperson(const RsPeerId& id, pqipersongrp *pg)
:PQInterface(id), mNotifyMtx("pqiperson-notify"), mPersonMtx("pqiperson"),
active(false), activepqi(NULL),
inConnectAttempt(false), waittimes(0),
pqipg(pg)
{
/* must check id! */
return;
}
pqiperson::pqiperson(const RsPeerId& id, pqipersongrp *pg) :
PQInterface(id), mNotifyMtx("pqiperson-notify"), mPersonMtx("pqiperson"),
active(false), activepqi(NULL), inConnectAttempt(false), waittimes(0),
pqipg(pg) {} // TODO: must check id!
pqiperson::~pqiperson()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// clean up the children.
// clean up the childrens
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
@ -64,13 +57,11 @@ pqiperson::~pqiperson()
kids.clear();
}
// The PQInterface interface.
int pqiperson::SendItem(RsItem *i,uint32_t& serialized_size)
int pqiperson::SendItem(RsItem *i,uint32_t& serialized_size)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
if (active)
if(active)
{
// every outgoing item goes through this function, so try to not waste cpu cycles
// check if debug output is wanted, to avoid unecessary work
@ -103,10 +94,10 @@ int pqiperson::SendItem(RsItem *i,uint32_t& serialized_size)
RsItem *pqiperson::GetItem()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
if (active)
return activepqi -> GetItem();
return activepqi->GetItem();
// else not possible.
return NULL;
}
@ -114,59 +105,64 @@ RsItem *pqiperson::GetItem()
bool pqiperson::RecvItem(RsItem *item)
{
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::RecvItem()";
std::cerr << std::endl;
std::cerr << "pqiperson::RecvItem()" << std::endl;
#endif
return pqipg->recvItem((RsRawItem *) item);
}
int pqiperson::status()
int pqiperson::status()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
if (active)
return activepqi -> status();
return -1;
}
int pqiperson::receiveHeartbeat()
int pqiperson::receiveHeartbeat()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::receiveHeartbeat() from peer : "
<< PeerId().toStdString() << std::endl;
#endif
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::receiveHeartbeat() from peer : " + PeerId().toStdString());
lastHeartbeatReceived = time(NULL);
RS_STACK_MUTEX(mPersonMtx);
lastHeartbeatReceived = time(NULL);
return true ;
return 1;
}
// tick......
int pqiperson::tick()
{
int activeTick = 0;
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
//if lastHeartbeatReceived is 0, it might be not activated so don't do a net reset.
if (active && (lastHeartbeatReceived != 0) &&
(time(NULL) - lastHeartbeatReceived) > HEARTBEAT_REPEAT_TIME * 5)
RS_STACK_MUTEX(mPersonMtx);
//if lastHeartbeatReceived is 0, it might be not activated so don't do a net reset.
if ( active && (lastHeartbeatReceived != 0)
&& (time(NULL) - lastHeartbeatReceived) > HEARTBEAT_REPEAT_TIME * 5)
{
int ageLastIncoming = time(NULL) - activepqi->getLastIncomingTS();
std::string out = "pqiperson::tick() WARNING No heartbeat from: " + PeerId().toStdString();
//out << " assume dead. calling pqissl::reset(), LastHeartbeat was: ";
rs_sprintf_append(out, " LastHeartbeat was: %ld secs ago", time(NULL) - lastHeartbeatReceived);
rs_sprintf_append(out, " LastIncoming was: %d secs ago", ageLastIncoming);
pqioutput(PQL_WARNING, pqipersonzone, out);
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::tick() WARNING No heartbeat from: "
<< PeerId().toStdString() << " LastHeartbeat was: "
<< time(NULL) - lastHeartbeatReceived
<< "secs ago LastIncoming was: " << ageLastIncoming
<< "secs ago" << std::endl;
#endif
#define NO_PACKET_TIMEOUT 60
if (ageLastIncoming > NO_PACKET_TIMEOUT)
if (ageLastIncoming > 60) // Check timeout
{
out = "pqiperson::tick() " + PeerId().toStdString();
out += " No Heartbeat & No Packets -> assume dead. calling pqissl::reset()";
pqioutput(PQL_WARNING, pqipersonzone, out);
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::tick() " << PeerId().toStdString()
<< " No Heartbeat & No Packets -> assume dead."
<< "calling pqissl::reset()" << std::endl;
#endif
this->reset_locked();
}
@ -174,34 +170,31 @@ int pqiperson::tick()
{
std::string out = "pqiperson::tick() Id: " + PeerId().toStdString() + " ";
#ifdef PERSON_DEBUG
std::string statusStr = " inactive ";
if (active)
out += "***Active***";
else
out += ">>InActive<<";
out += "\n";
rs_sprintf_append(out, "Activepqi: %p inConnectAttempt: ", activepqi);
statusStr = " active ";
std::string connectStr = " Not Connecting ";
if (inConnectAttempt)
out += "In Connection Attempt";
else
out += " Not Connecting ";
out += "\n";
connectStr = " In Connection Attempt ";
std::cerr << "pqiperson::tick() Id: " << PeerId().toStdString()
<< "activepqi: " << activepqi << " inConnectAttempt:"
<< connectStr << std::endl;
#endif
// tick the children.
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
if (0 < (it->second) -> tick())
{
if (0 < (it->second)->tick())
activeTick = 1;
}
rs_sprintf_append(out, "\tTicking Child: %d\n", it->first);
#ifdef PERSON_DEBUG
std::cerr << "\tTicking Child: "<< it->first << std::endl;
#endif
}
pqioutput(PQL_DEBUG_ALL, pqipersonzone, out);
} // end of pqioutput.
}
}
// handle Notify Events that were generated.
@ -214,44 +207,41 @@ int pqiperson::tick()
// This is only used for out-of-band info....
// otherwise could get dangerous loops.
// - Actually, now we have - must store and process later.
int pqiperson::notifyEvent(NetInterface *ni, int newState, const struct sockaddr_storage &remote_peer_address)
int pqiperson::notifyEvent(NetInterface *ni, int newState,
const sockaddr_storage &remote_peer_address)
{
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::notifyEvent() adding event to Queue. newState=" << newState << " from IP = " << sockaddr_storage_tostring(remote_peer_address) << std::endl;
std::cerr << "pqiperson::notifyEvent() adding event to Queue. newState="
<< newState << " from IP = "
<< sockaddr_storage_tostring(remote_peer_address) << std::endl;
#endif
if (mPersonMtx.trylock())
{
handleNotifyEvent_locked(ni, newState, remote_peer_address);
mPersonMtx.unlock();
return 1;
}
RsStackMutex stack(mNotifyMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mNotifyMtx);
mNotifyQueue.push_back(NotifyData(ni, newState, remote_peer_address));
return 1;
}
void pqiperson::processNotifyEvents()
void pqiperson::processNotifyEvents()
{
NetInterface *ni;
int state;
struct sockaddr_storage addr;
sockaddr_storage addr;
while(1)
while(1) // While there is notification to handle
{
{
RsStackMutex stack(mNotifyMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mNotifyMtx);
if (mNotifyQueue.empty())
{
if(mNotifyQueue.empty())
return;
}
NotifyData &data = mNotifyQueue.front();
ni = data.mNi;
state = data.mState;
@ -260,23 +250,21 @@ void pqiperson::processNotifyEvents()
mNotifyQueue.pop_front();
}
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
handleNotifyEvent_locked(ni, state, addr);
}
return;
}
int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const struct sockaddr_storage &remote_peer_address)
int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState,
const sockaddr_storage &remote_peer_address)
{
{
std::string out = "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + "\n";
rs_sprintf_append(out, "Message: %d from: %p\n", newState, ni);
rs_sprintf_append(out, "Active pqi : %p", activepqi);
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out);
}
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::handleNotifyEvent_locked() Id: "
<< PeerId().toStdString() << " Message: " << newState
<< " from: " << ni << std::endl;
int i = 0;
#endif
/* find the pqi, */
pqiconnect *pqi = NULL;
@ -284,14 +272,14 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
std::map<uint32_t, pqiconnect *>::iterator it;
/* start again */
int i = 0;
for(it = kids.begin(); it != kids.end(); ++it)
{
std::string out;
rs_sprintf(out, "pqiperson::connectattempt() Kid# %d of %u\n", i, kids.size());
rs_sprintf_append(out, " type: %u in_ni: %p", it->first, ni);
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out);
i++;
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::handleNotifyEvent_locked() Kid# " << i
<< " of " << kids.size() << " type: " << it->first
<< " in_ni: " << ni << std::endl;
++i;
#endif
if ((it->second)->thisNetInterface(ni))
{
@ -302,7 +290,8 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
if (!pqi)
{
pqioutput(PQL_WARNING, pqipersonzone, "Unknown notfyEvent Source!");
std::cerr << "pqiperson::handleNotifyEvent_locked Unknown Event Source!"
<< std::endl;
return -1;
}
@ -311,12 +300,13 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
{
case CONNECT_RECEIVED:
case CONNECT_SUCCESS:
{
/* notify */
if (pqipg)
{
pqissl *ssl = dynamic_cast<pqissl*>(ni);
if(ssl != NULL)
if(ssl)
pqipg->notifyConnect(PeerId(), type, true, ssl->actAsServer(), remote_peer_address);
else
pqipg->notifyConnect(PeerId(), type, true, false, remote_peer_address);
@ -324,103 +314,99 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
if ((active) && (activepqi != pqi)) // already connected - trouble
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_SUCCESS+active-> activing new connection, shutting others");
// TODO: 2015/12/19 Is this block dead code?
std::cerr << "pqiperson::handleNotifyEvent_locked Id: "
<< PeerId().toStdString() << " CONNECT_SUCCESS+active->"
<< "activing new connection, shutting others"
<< std::endl;
// This is the RESET that's killing the connections.....
//activepqi -> reset();
//activepqi -> reset();
// this causes a recursive call back into this fn.
// which cleans up state.
// we only do this if its not going to mess with new conn.
}
/* now install a new one. */
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_SUCCESS->marking so! (resetting others)");
{
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::handleNotifyEvent_locked Id: "
<< PeerId().toStdString() << " CONNECT_SUCCESS->marking "
<< "so! (resetting others)" << std::endl;
#endif
// mark as active.
active = true;
lastHeartbeatReceived = 0;
lastHeartbeatReceived = 0;
activepqi = pqi;
inConnectAttempt = false;
inConnectAttempt = false;
activepqi->start(); // STARTUP THREAD.
/* reset all other children? (clear up long UDP attempt) */
// reset all other children (clear up long UDP attempt)
for(it = kids.begin(); it != kids.end(); ++it)
{
if (!(it->second)->thisNetInterface(ni))
{
std::string out;
rs_sprintf(out, "Resetting pqi ref : %p", &(it->second));
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out);
it->second->reset();
} else {
//std::cerr << "Active pqi : not resetting." << std::endl;
}
}
return 1;
}
break;
}
case CONNECT_UNREACHABLE:
case CONNECT_FIREWALLED:
case CONNECT_FAILED:
if (active)
{
if (active && (activepqi == pqi))
{
if (activepqi == pqi)
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_FAILED->marking so!");
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::handleNotifyEvent_locked Id: "
<< PeerId().toStdString()
<< " CONNECT_FAILED->marking so!" << std::endl;
#endif
activepqi->shutdown(); // STOP THREAD.
active = false;
activepqi = NULL;
}
else
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_FAILED-> from an unactive connection, don't flag the peer as not connected, just try next attempt !");
}
activepqi->shutdown(); // STOP THREAD.
active = false;
activepqi = NULL;
}
#ifdef PERSON_DEBUG
else
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_FAILED+NOT active -> try connect again");
}
std::cerr << "pqiperson::handleNotifyEvent_locked Id: "
<< PeerId().toStdString() + " CONNECT_FAILED-> from "
<< "an unactive connection, don't flag the peer as "
<< "not connected, just try next attempt !" << std::endl;
#endif
/* notify up */
if (pqipg)
{
pqipg->notifyConnect(PeerId(), type, false, false, remote_peer_address);
}
return 1;
break;
default:
break;
}
return -1;
default:
return -1;
}
}
/***************** Not PQInterface Fns ***********************/
int pqiperson::reset()
int pqiperson::reset()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
return reset_locked();
}
int pqiperson::reset_locked()
int pqiperson::reset_locked()
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::reset() resetting all pqiconnect for Id: " + PeerId().toStdString());
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::reset_locked() resetting all pqiconnect for Id: "
<< PeerId().toStdString() << std::endl;
#endif
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
(it->second) -> shutdown(); // STOP THREAD.
(it->second) -> reset();
}
}
activepqi = NULL;
active = false;
@ -429,17 +415,18 @@ int pqiperson::reset_locked()
return 1;
}
int pqiperson::fullstopthreads()
int pqiperson::fullstopthreads()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::fullstopthreads() for Id: "
<< PeerId().toStdString() << std::endl;
#endif
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::fullstopthreads() for Id: " + PeerId().toStdString());
RS_STACK_MUTEX(mPersonMtx);
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
(it->second) -> fullstop(); // WAIT FOR THREAD TO STOP.
}
(it->second)->fullstop(); // WAIT FOR THREAD TO STOP.
activepqi = NULL;
active = false;
@ -450,13 +437,12 @@ int pqiperson::fullstopthreads()
int pqiperson::addChildInterface(uint32_t type, pqiconnect *pqi)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::addChildInterface() : Id "
<< PeerId().toStdString() << " " << type << std::endl;
#endif
{
std::string out;
rs_sprintf(out, "pqiperson::addChildInterface() : Id %s %u", PeerId().toStdString().c_str(), type);
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out);
}
RS_STACK_MUTEX(mPersonMtx);
kids[type] = pqi;
return 1;
@ -466,91 +452,78 @@ int pqiperson::addChildInterface(uint32_t type, pqiconnect *pqi)
// functions to iterate over the connects and change state.
int pqiperson::listen()
int pqiperson::listen()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::listen() Id: " + PeerId().toStdString() << std::endl;
#endif
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, "pqiperson::listen() Id: " + PeerId().toStdString());
RS_STACK_MUTEX(mPersonMtx);
if (!active)
{
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
// set them all listening.
(it->second) -> listen();
}
(it->second)->listen();
}
return 1;
}
int pqiperson::stoplistening()
int pqiperson::stoplistening()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::stoplistening() Id: " + PeerId().toStdString()
<< std::endl;
#endif
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, "pqiperson::stoplistening() Id: " + PeerId().toStdString());
RS_STACK_MUTEX(mPersonMtx);
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
// set them all listening.
(it->second) -> stoplistening();
}
(it->second)->stoplistening();
return 1;
}
int pqiperson::connect(uint32_t type, const struct sockaddr_storage &raddr,
const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr,
uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags, uint32_t bandwidth,
const std::string &domain_addr, uint16_t domain_port)
int pqiperson::connect(uint32_t type, const sockaddr_storage &raddr,
const sockaddr_storage &proxyaddr,
const sockaddr_storage &srcaddr,
uint32_t delay, uint32_t period, uint32_t timeout,
uint32_t flags, uint32_t bandwidth,
const std::string &domain_addr, uint16_t domain_port)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::connect() Id: " << PeerId().toStdString()
<< " type: " << type << " addr: "
<< sockaddr_storage_tostring(raddr) << " proxyaddr: "
<< sockaddr_storage_tostring(proxyaddr) << " srcaddr: "
<< sockaddr_storage_tostring(srcaddr) << " delay: " << delay
<< " period: " << period << " timeout: " << timeout << " flags: "
<< flags << " bandwidth: " << bandwidth << std::endl;
#endif
{
std::string out = "pqiperson::connect() Id: " + PeerId().toStdString();
rs_sprintf_append(out, " type: %u", type);
out += " addr: ";
out += sockaddr_storage_tostring(raddr);
out += " proxyaddr: ";
out += sockaddr_storage_tostring(proxyaddr);
out += " srcaddr: ";
out += sockaddr_storage_tostring(srcaddr);
rs_sprintf_append(out, " delay: %u", delay);
rs_sprintf_append(out, " period: %u", period);
rs_sprintf_append(out, " timeout: %u", timeout);
rs_sprintf_append(out, " flags: %u", flags);
rs_sprintf_append(out, " bandwidth: %u", bandwidth);
//std::cerr << out.str();
pqioutput(PQL_WARNING, pqipersonzone, out);
}
RS_STACK_MUTEX(mPersonMtx);
std::map<uint32_t, pqiconnect *>::iterator it;
it = kids.find(type);
if (it == kids.end())
{
#ifdef PERSON_DEBUG
//pqioutput(PQL_DEBUG_BASIC, pqipersonzone, "pqiperson::connect() missing pqiconnect");
#endif
/* notify of fail! */
pqipg->notifyConnect(PeerId(), type, false, false, raddr);
return 0;
}
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::connect() WARNING, resetting for new connection attempt" << std::endl;
std::cerr << "pqiperson::connect() resetting for new connection attempt" << std::endl;
#endif
/* set the parameters */
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::connect reset() before connection attempt");
(it->second)->reset();
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::connect() WARNING, clearing rate cap" << std::endl;
std::cerr << "pqiperson::connect() clearing rate cap" << std::endl;
#endif
setRateCap_locked(0,0);
@ -583,61 +556,66 @@ int pqiperson::connect(uint32_t type, const struct sockaddr_storage &raddr,
}
void pqiperson::getRates(RsBwRates &rates)
void pqiperson::getRates(RsBwRates &rates)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// get the rate from the active one.
if ((!active) || (activepqi == NULL))
return;
activepqi -> getRates(rates);
activepqi->getRates(rates);
}
int pqiperson::gatherStatistics(std::list<RSTrafficClue>& out_lst,std::list<RSTrafficClue>& in_lst)
int pqiperson::gatherStatistics(std::list<RSTrafficClue>& out_lst,
std::list<RSTrafficClue>& in_lst)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// get the rate from the active one.
if ((!active) || (activepqi == NULL))
return 0;
return activepqi -> gatherStatistics(out_lst,in_lst);
return activepqi->gatherStatistics(out_lst, in_lst);
}
int pqiperson::getQueueSize(bool in)
int pqiperson::getQueueSize(bool in)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// get the rate from the active one.
if ((!active) || (activepqi == NULL))
return 0;
return activepqi -> getQueueSize(in);
return activepqi->getQueueSize(in);
}
bool pqiperson::getCryptoParams(RsPeerCryptoParams& params)
bool pqiperson::getCryptoParams(RsPeerCryptoParams & params)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
if(active && activepqi != NULL)
return activepqi->getCryptoParams(params) ;
return activepqi->getCryptoParams(params);
else
{
params.connexion_state = 0 ;
params.cipher_name.clear() ;
params.cipher_bits_1 = 0 ;
params.cipher_bits_2 = 0 ;
params.cipher_version.clear() ;
params.connexion_state = 0;
params.cipher_name.clear();
params.cipher_bits_1 = 0;
params.cipher_bits_2 = 0;
params.cipher_version.clear();
return false ;
}
}
bool pqiconnect::getCryptoParams(RsPeerCryptoParams& params)
bool pqiconnect::getCryptoParams(RsPeerCryptoParams & params)
{
pqissl *ssl = dynamic_cast<pqissl*>(ni) ;
pqissl *ssl = dynamic_cast<pqissl*>(ni);
if(ssl != NULL)
{
ssl->getCryptoParams(params) ;
return true ;
ssl->getCryptoParams(params);
return true;
}
else
{
@ -650,31 +628,30 @@ bool pqiconnect::getCryptoParams(RsPeerCryptoParams& params)
}
}
float pqiperson::getRate(bool in)
float pqiperson::getRate(bool in)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// get the rate from the active one.
if ((!active) || (activepqi == NULL))
return 0;
return activepqi -> getRate(in);
}
void pqiperson::setMaxRate(bool in, float val)
void pqiperson::setMaxRate(bool in, float val)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// set to all of them. (and us)
PQInterface::setMaxRate(in, val);
// clean up the children.
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
(it->second) -> setMaxRate(in, val);
}
}
void pqiperson::setRateCap(float val_in, float val_out)
void pqiperson::setRateCap(float val_in, float val_out)
{
// This methods might be called all the way down from pqiperson::tick() down
// to pqissludp while completing a UDP connexion, causing a deadlock.
@ -686,28 +663,30 @@ void pqiperson::setRateCap(float val_in, float val_out)
// The lock cannot be locked by the same thread between the first test and
// the "else" statement, so there is no possibility for this code to fail.
//
// We could actually put that code in RsMutex::lock()
// We could actually put that code in RsMutex::lock()?
// TODO: 2015/12/19 This code is already in RsMutex::lock() but is guarded
// by RSTHREAD_SELF_LOCKING_GUARD which is specifically unset in the header
// Why is that code guarded? Do it have an impact on performance?
// Or we should not get in the situation of trying to relock the mutex on
// the same thread NEVER?
if(pthread_equal(mPersonMtx.owner(),pthread_self())) // 1 - unlocked, or already locked by same thread
return setRateCap_locked(val_in, val_out); // -> do nothing
if(pthread_equal(mPersonMtx.owner(), pthread_self()))
// Unlocked, or already locked by same thread
setRateCap_locked(val_in, val_out);
else
{ // 2 - lock was free or locked by different thread => wait.
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
{
// Lock was free or locked by different thread => wait.
RS_STACK_MUTEX(mPersonMtx);
setRateCap_locked(val_in, val_out);
}
}
void pqiperson::setRateCap_locked(float val_in, float val_out)
void pqiperson::setRateCap_locked(float val_in, float val_out)
{
// set to all of them. (and us)
PQInterface::setRateCap(val_in, val_out);
// clean up the children.
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
(it->second) -> setRateCap(val_in, val_out);
}
(it->second)->setRateCap(val_in, val_out);
}

View file

@ -48,78 +48,55 @@ static const int HEARTBEAT_REPEAT_TIME = 5;
#include "pqi/pqiqosstreamer.h"
#include "pqi/pqithreadstreamer.h"
class pqiconnect: public pqiQoSstreamer, public NetInterface
class pqiconnect : public pqiQoSstreamer, public NetInterface
{
public:
pqiconnect(PQInterface *parent, RsSerialiser *rss, NetBinInterface *ni_in)
:pqiQoSstreamer(parent, rss, ni_in->PeerId(), ni_in, 0), // pqistreamer will cleanup NetInterface.
NetInterface(NULL, ni_in->PeerId()), // No need for callback
ni(ni_in)
{
if (!ni_in)
{
std::cerr << "pqiconnect::pqiconnect() NetInterface == NULL, FATAL!";
std::cerr << std::endl;
exit(1);
}
return;
}
pqiconnect(PQInterface *parent, RsSerialiser *rss, NetBinInterface *ni_in) :
pqiQoSstreamer(parent, rss, ni_in->PeerId(), ni_in, 0), // pqistreamer will cleanup NetInterface.
NetInterface(NULL, ni_in->PeerId()), // No need for callback
ni(ni_in) {}
virtual ~pqiconnect() { return; }
virtual bool getCryptoParams(RsPeerCryptoParams& params) ;
virtual ~pqiconnect() {}
virtual bool getCryptoParams(RsPeerCryptoParams& params);
// presents a virtual NetInterface -> passes to ni.
virtual int connect(const struct sockaddr_storage &raddr) { return ni->connect(raddr); }
virtual int listen() { return ni -> listen(); }
virtual int stoplistening() { return ni -> stoplistening(); }
virtual int reset() { return ni -> reset(); }
virtual int disconnect() { return ni -> reset(); }
virtual bool connect_parameter(uint32_t type, uint32_t value) { return ni -> connect_parameter(type, value);}
virtual bool connect_parameter(uint32_t type, std::string value) { return ni -> connect_parameter(type, value);}
virtual bool connect_additional_address(uint32_t type, const struct sockaddr_storage &addr) { return ni -> connect_additional_address(type, addr);}
virtual int getConnectAddress(struct sockaddr_storage &raddr){ return ni->getConnectAddress(raddr); }
virtual int connect(const struct sockaddr_storage &raddr) { return ni->connect(raddr); }
virtual int listen() { return ni->listen(); }
virtual int stoplistening() { return ni->stoplistening(); }
virtual int reset() { return ni->reset(); }
virtual int disconnect() { return ni->reset(); }
virtual bool connect_parameter(uint32_t type, uint32_t value) { return ni->connect_parameter(type, value);}
virtual bool connect_parameter(uint32_t type, std::string value) { return ni->connect_parameter(type, value);}
virtual bool connect_additional_address(uint32_t type, const struct sockaddr_storage &addr) { return ni->connect_additional_address(type, addr); }
virtual int getConnectAddress(struct sockaddr_storage &raddr){ return ni->getConnectAddress(raddr); }
// get the contact from the net side!
virtual const RsPeerId& PeerId()
{
if (ni)
{
return ni->PeerId();
}
else
{
return PQInterface::PeerId();
}
}
virtual const RsPeerId& PeerId() { return ni->PeerId(); }
// to check if our interface.
virtual bool thisNetInterface(NetInterface *ni_in) { return (ni_in == ni); }
virtual bool thisNetInterface(NetInterface *ni_in) { return (ni_in == ni); }
protected:
NetBinInterface *ni;
protected:
};
class pqipersongrp;
class NotifyData
{
public:
NotifyData()
:mNi(NULL), mState(0)
public:
NotifyData() : mNi(NULL), mState(0)
{
sockaddr_storage_clear(mAddr);
}
NotifyData(NetInterface *ni, int state, const struct sockaddr_storage &addr)
:mNi(ni), mState(state), mAddr(addr) { return; }
NotifyData(NetInterface *ni, int state, const sockaddr_storage &addr) :
mNi(ni), mState(state), mAddr(addr) {}
NetInterface *mNi;
int mState;
struct sockaddr_storage mAddr;
sockaddr_storage mAddr;
};
@ -127,82 +104,76 @@ class pqiperson: public PQInterface
{
public:
pqiperson(const RsPeerId& id, pqipersongrp *ppg);
virtual ~pqiperson(); // must clean up children.
virtual ~pqiperson(); // must clean up children.
// control of the connection.
int reset();
int listen();
int stoplistening();
int reset();
int listen();
int stoplistening();
int connect(uint32_t type, const struct sockaddr_storage &raddr,
const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr,
uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags, uint32_t bandwidth,
const std::string &domain_addr, uint16_t domain_port);
int connect(uint32_t type, const sockaddr_storage &raddr,
const sockaddr_storage &proxyaddr, const sockaddr_storage &srcaddr,
uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags,
uint32_t bandwidth, const std::string &domain_addr, uint16_t domain_port);
int fullstopthreads();
int fullstopthreads();
int receiveHeartbeat();
int receiveHeartbeat();
// add in connection method.
int addChildInterface(uint32_t type, pqiconnect *pqi);
int addChildInterface(uint32_t type, pqiconnect *pqi);
virtual bool getCryptoParams(RsPeerCryptoParams&) ;
virtual bool getCryptoParams(RsPeerCryptoParams&);
// The PQInterface interface.
virtual int SendItem(RsItem *,uint32_t& serialized_size);
virtual int SendItem(RsItem *item)
{
std::cerr << "Warning pqiperson::sendItem(RsItem*) should not be called. Plz call SendItem(RsItem *,uint32_t& serialized_size) instead." << std::endl;
uint32_t serialized_size ;
return SendItem(item,serialized_size) ;
}
virtual RsItem *GetItem();
virtual bool RecvItem(RsItem *item);
virtual int SendItem(RsItem *,uint32_t& serialized_size);
virtual int SendItem(RsItem *item)
{
std::cerr << "Warning pqiperson::sendItem(RsItem*) should not be called."
<< "Plz call SendItem(RsItem *,uint32_t& serialized_size) instead."
<< std::endl;
uint32_t serialized_size;
return SendItem(item, serialized_size);
}
virtual RsItem *GetItem();
virtual bool RecvItem(RsItem *item);
virtual int status();
virtual int tick();
virtual int status();
virtual int tick();
// overloaded callback function for the child - notify of a change.
virtual int notifyEvent(NetInterface *ni, int event, const struct sockaddr_storage &addr);
// overloaded callback function for the child - notify of a change.
virtual int notifyEvent(NetInterface *ni, int event, const struct sockaddr_storage &addr);
// PQInterface for rate control overloaded....
virtual int getQueueSize(bool in);
virtual void getRates(RsBwRates &rates);
virtual float getRate(bool in);
virtual void setMaxRate(bool in, float val);
virtual void setRateCap(float val_in, float val_out);
virtual int gatherStatistics(std::list<RSTrafficClue>& outqueue_lst,std::list<RSTrafficClue>& inqueue_lst) ;
// PQInterface for rate control overloaded....
virtual int getQueueSize(bool in);
virtual void getRates(RsBwRates &rates);
virtual float getRate(bool in);
virtual void setMaxRate(bool in, float val);
virtual void setRateCap(float val_in, float val_out);
virtual int gatherStatistics(std::list<RSTrafficClue>& outqueue_lst,
std::list<RSTrafficClue>& inqueue_lst);
private:
void processNotifyEvents();
int handleNotifyEvent_locked(NetInterface *ni, int event,
const sockaddr_storage &addr);
private:
void processNotifyEvents();
int handleNotifyEvent_locked(NetInterface *ni, int event, const struct sockaddr_storage &addr);
RsMutex mNotifyMtx; /**** LOCKS Notify Queue ****/
RsMutex mNotifyMtx; // LOCKS Notify Queue
std::list<NotifyData> mNotifyQueue;
RsMutex mPersonMtx; /**** LOCKS below ****/
RsMutex mPersonMtx; // LOCKS below
int reset_locked();
int reset_locked();
void setRateCap_locked(float val_in, float val_out);
void setRateCap_locked(float val_in, float val_out);
std::map<uint32_t, pqiconnect *> kids;
bool active;
pqiconnect *activepqi;
bool inConnectAttempt;
int waittimes;
time_t lastHeartbeatReceived;//use to track connection failure
private: /* Helper functions */
time_t lastHeartbeatReceived; // use to track connection failure
pqipersongrp *pqipg; /* parent for callback */
};
#endif

View file

@ -617,15 +617,19 @@ int pqipersongrp::connectPeer(const RsPeerId& id
uint32_t ptype;
if (type & RS_NET_CONN_TCP_ALL)
{
if (type == RS_NET_CONN_TCP_HIDDEN)
{
ptype = PQI_CONNECT_HIDDEN_TCP;
timeout = RS_TCP_HIDDEN_TIMEOUT_PERIOD;
}
else
{
switch (type) {
case RS_NET_CONN_TCP_HIDDEN_TOR:
ptype = PQI_CONNECT_HIDDEN_TOR_TCP;
timeout = RS_TCP_HIDDEN_TIMEOUT_PERIOD;
break;
case RS_NET_CONN_TCP_HIDDEN_I2P:
ptype = PQI_CONNECT_HIDDEN_I2P_TCP;
timeout = RS_TCP_HIDDEN_TIMEOUT_PERIOD;
break;
default:
ptype = PQI_CONNECT_TCP;
timeout = RS_TCP_STD_TIMEOUT_PERIOD;
timeout = RS_TCP_STD_TIMEOUT_PERIOD;
break;
}
#ifdef PGRP_DEBUG
std::cerr << " pqipersongrp::connectPeer() connecting with TCP: Timeout :" << timeout;

View file

@ -27,7 +27,7 @@ void pqiQoS::clear()
{
void *item ;
for(int i=0;i<_item_queues.size();++i)
for(uint32_t i=0;i<_item_queues.size();++i)
while( (item = _item_queues[i].pop()) != NULL)
free(item) ;
@ -46,7 +46,7 @@ void pqiQoS::print() const
void pqiQoS::in_rsItem(void *ptr,int priority)
{
if(priority >= _item_queues.size())
if(uint32_t(priority) >= _item_queues.size())
{
std::cerr << "pqiQoS::in_rsRawItem() ****Warning****: priority " << priority << " out of scope [0," << _item_queues.size()-1 << "]. Priority will be clamped to maximum value." << std::endl;
priority = _item_queues.size()-1 ;

View file

@ -103,7 +103,7 @@ pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm)
sslmode(PQISSL_ACTIVE), ssl_connection(NULL), sockfd(-1),
readpkt(NULL), pktlen(0), total_len(0),
attempt_ts(0),
sameLAN(false), n_read_zero(0), mReadZeroTS(0), ssl_connect_timeout(0),
n_read_zero(0), mReadZeroTS(0), ssl_connect_timeout(0),
mConnectDelay(0), mConnectTS(0),
mConnectTimeout(0), mTimeoutTS(0)
{
@ -255,7 +255,6 @@ int pqissl::reset_locked()
sockfd = -1;
waiting = WAITING_NOT;
ssl_connection = NULL;
sameLAN = false;
n_read_zero = 0;
mReadZeroTS = 0;
total_len = 0 ;
@ -286,54 +285,40 @@ int pqissl::reset_locked()
return 1;
}
bool pqissl::connect_parameter(uint32_t type, const std::string &value)
bool pqissl::connect_parameter(uint32_t type, uint32_t value)
{
RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/
/* remove unused parameter warnings */
(void) type;
(void) value;
return false;
}
bool pqissl::connect_parameter(uint32_t type, uint32_t value)
{
RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/
#ifdef PQISSL_LOG_DEBUG
{
std::string out = "pqissl::connect_parameter() Peer: " + PeerId();
rs_sprintf_append(out, " type: %u value: %u", type, value);
rslog(RSL_DEBUG_ALL, pqisslzone, out);
}
#ifdef PQISSL_LOG_DEBUG
std::cerr << "pqissl::connect_parameter() Peer: " << PeerId();
#endif
if (type == NET_PARAM_CONNECT_DELAY)
switch(type)
{
#ifdef PQISSL_LOG_DEBUG
std::string out = "pqissl::connect_parameter() Peer: " + PeerId();
rs_sprintf_append(out, " DELAY: %u", value);
rslog(RSL_DEBUG_BASIC, pqisslzone, out);
case NET_PARAM_CONNECT_DELAY:
{
#ifdef PQISSL_LOG_DEBUG
std::cerr << " DELAY: " << value << std::endl;
#endif
RS_STACK_MUTEX(mSslMtx);
mConnectDelay = value;
return true;
}
else if (type == NET_PARAM_CONNECT_TIMEOUT)
case NET_PARAM_CONNECT_TIMEOUT:
{
#ifdef PQISSL_LOG_DEBUG
std::string out = "pqissl::connect_parameter() Peer: " + PeerId();
rs_sprintf_append(out, " TIMEOUT: %u", value);
rslog(RSL_DEBUG_BASIC, pqisslzone, out);
#ifdef PQISSL_LOG_DEBUG
std::cerr << " TIMEOUT: " << value << std::endl;
#endif
RS_STACK_MUTEX(mSslMtx);
mConnectTimeout = value;
return true;
}
return false;
//return NetInterface::connect_parameter(type, value);
default:
{
#ifdef PQISSL_LOG_DEBUG
std::cerr << " type: " << type << " value: " << value << std::endl;
#endif
return false;
}
}
}
@ -1448,6 +1433,7 @@ int pqissl::accept_locked(SSL *ssl, int fd, const struct sockaddr_storage &forei
rslog(RSL_ALERT, pqisslzone,
"pqissl::accept() closing Previous/Existing ssl_connection");
SSL_shutdown(ssl_connection);
SSL_free (ssl_connection);
}
if ((sockfd > -1) && (sockfd != fd))
@ -1472,22 +1458,12 @@ int pqissl::accept_locked(SSL *ssl, int fd, const struct sockaddr_storage &forei
struct sockaddr_storage localaddr;
mLinkMgr->getLocalAddress(localaddr);
sameLAN = sockaddr_storage_samesubnet(remote_addr, localaddr);
{
std::string out = "pqissl::accept() SUCCESSFUL connection to: " + PeerId().toStdString();
out += " localaddr: " + sockaddr_storage_iptostring(localaddr);
out += " remoteaddr: " + sockaddr_storage_iptostring(remote_addr);
if (sameLAN)
{
out += " SAME LAN";
}
else
{
out += " DIFF LANs";
}
rslog(RSL_WARNING, pqisslzone, out);
}
@ -1961,10 +1937,7 @@ bool pqissl::cansend(uint32_t usec)
}
RsFileHash pqissl::gethash()
{
return RsFileHash() ;
}
RsFileHash pqissl::gethash() { return RsFileHash(); }
/********** End of Implementation of BinInterface ******************/

View file

@ -98,7 +98,7 @@ virtual int reset();
virtual int disconnect();
virtual int getConnectAddress(struct sockaddr_storage &raddr);
virtual bool connect_parameter(uint32_t type, const std::string &value);
virtual bool connect_parameter(uint32_t /*type*/, const std::string & /*value*/) { return false; }
virtual bool connect_parameter(uint32_t type, uint32_t value);
// BinInterface
@ -170,10 +170,13 @@ int Extract_Failed_SSL_Certificate(); // try to get cert anyway.
bool CheckConnectionTimeout();
//protected internal fns that are overloaded for udp case.
virtual int net_internal_close(int fd);
virtual int net_internal_SSL_set_fd(SSL *ssl, int fd);
virtual int net_internal_fcntl_nonblock(int fd);
/* Do we really need this ?
* It is very specific TCP+SSL stuff and unlikely to be reused.
* In fact we are overloading them in pqissludp case where they do different things or nothing.
*/
virtual int net_internal_close(int fd);
virtual int net_internal_SSL_set_fd(SSL *ssl, int fd);
virtual int net_internal_fcntl_nonblock(int fd);
/* data */
@ -196,8 +199,6 @@ virtual int net_internal_fcntl_nonblock(int fd);
int attempt_ts;
bool sameLAN; /* flag use to allow high-speed transfers */
int n_read_zero; /* a counter to determine if the connection is really dead */
time_t mReadZeroTS; /* timestamp of first READ_ZERO occurance */
@ -208,11 +209,9 @@ virtual int net_internal_fcntl_nonblock(int fd);
uint32_t mConnectTimeout;
time_t mTimeoutTS;
private:
// ssl only fns.
int connectInterface(const struct sockaddr_storage &addr);
int connectInterface(const struct sockaddr_storage &addr);
};

View file

@ -56,19 +56,17 @@ const int pqissllistenzone = 49787;
*/
pqissllistenbase::pqissllistenbase(const struct sockaddr_storage &addr, p3PeerMgr *pm)
:laddr(addr), active(false), mPeerMgr(pm)
pqissllistenbase::pqissllistenbase(const sockaddr_storage &addr, p3PeerMgr *pm)
: laddr(addr), active(false), mPeerMgr(pm)
{
if (!(AuthSSL::getAuthSSL()-> active())) {
pqioutput(PQL_ALERT, pqissllistenzone,
"SSL-CTX-CERT-ROOT not initialised!");
if (!(AuthSSL::getAuthSSL()-> active()))
{
pqioutput(PQL_ALERT, pqissllistenzone,
"SSL-CTX-CERT-ROOT not initialised!");
exit(1);
}
setuplisten();
return;
}
pqissllistenbase::~pqissllistenbase()
@ -123,6 +121,10 @@ int pqissllistenbase::setuplisten()
err = fcntl(lsock, F_SETFL, O_NONBLOCK);
if (err < 0)
{
shutdown(lsock,SHUT_RDWR) ;
close(lsock) ;
lsock = -1 ;
std::string out;
rs_sprintf(out, "Error: Cannot make socket NON-Blocking: %d", err);
pqioutput(PQL_ERROR, pqissllistenzone, out);
@ -145,6 +147,9 @@ int pqissllistenbase::setuplisten()
unsigned long int on = 1;
if (0 != (err = ioctlsocket(lsock, FIONBIO, &on)))
{
closesocket(lsock) ;
lsock = -1 ;
std::string out;
rs_sprintf(out, "pqissllistenbase::setuplisten() Error: Cannot make socket NON-Blocking: %d\n", err);
out += "Socket Error: " + socket_errorType(WSAGetLastError());
@ -723,18 +728,7 @@ int pqissllistenbase::isSSLActive(int /*fd*/, SSL *ssl)
*
*/
pqissllistener::pqissllistener(const struct sockaddr_storage &addr, p3PeerMgr *lm)
:pqissllistenbase(addr, lm)
{
return;
}
pqissllistener::~pqissllistener()
{
return;
}
int pqissllistener::addlistenaddr(const RsPeerId& id, pqissl *acc)
int pqissllistener::addlistenaddr(const RsPeerId& id, pqissl *acc)
{
std::map<RsPeerId, pqissl *>::iterator it;
@ -748,7 +742,6 @@ int pqissllistener::addlistenaddr(const RsPeerId& id, pqissl *acc)
pqioutput(PQL_DEBUG_ALERT, pqissllistenzone, out);
return -1;
}
}
@ -785,7 +778,7 @@ int pqissllistener::removeListenPort(const RsPeerId& id)
}
int pqissllistener::status()
int pqissllistener::status()
{
pqissllistenbase::status();
// print certificates we are listening for.

View file

@ -49,13 +49,12 @@ class p3PeerMgr;
class AcceptedSSL
{
public:
public:
int mFd;
SSL *mSSL;
RsPeerId mPeerId;
struct sockaddr_storage mAddr;
sockaddr_storage mAddr;
time_t mAcceptTS;
};
@ -64,26 +63,22 @@ class AcceptedSSL
class pqissllistenbase: public pqilistener
{
public:
public:
pqissllistenbase(const struct sockaddr_storage &addr, p3PeerMgr *pm);
virtual ~pqissllistenbase();
/*************************************/
/* LISTENER INTERFACE */
virtual int tick();
virtual int status();
virtual int setListenAddr(const struct sockaddr_storage &addr);
virtual int setuplisten();
virtual int resetlisten();
/*************************************/
pqissllistenbase(const struct sockaddr_storage &addr, p3PeerMgr *pm);
virtual ~pqissllistenbase();
/*************************************/
/* LISTENER INTERFACE **/
virtual int tick();
virtual int status();
virtual int setListenAddr(const struct sockaddr_storage &addr);
virtual int setuplisten();
virtual int resetlisten();
/*************************************/
int acceptconnection();
int continueaccepts();
int finaliseAccepts();
int acceptconnection();
int continueaccepts();
int finaliseAccepts();
struct IncomingSSLInfo
{
@ -95,52 +90,44 @@ int finaliseAccepts();
};
// fn to get cert, anyway
int continueSSL(IncomingSSLInfo&, bool);
int closeConnection(int fd, SSL *ssl);
int isSSLActive(int fd, SSL *ssl);
int continueSSL(IncomingSSLInfo&, bool);
int closeConnection(int fd, SSL *ssl);
int isSSLActive(int fd, SSL *ssl);
virtual int completeConnection(int sockfd, IncomingSSLInfo&) = 0;
virtual int finaliseConnection(int fd, SSL *ssl, const RsPeerId& peerId, const struct sockaddr_storage &raddr) = 0;
protected:
virtual int completeConnection(int sockfd, IncomingSSLInfo&) = 0;
virtual int finaliseConnection(int fd, SSL *ssl, const RsPeerId& peerId,
const sockaddr_storage &raddr) = 0;
protected:
struct sockaddr_storage laddr;
std::list<AcceptedSSL> accepted_ssl;
private:
int Extract_Failed_SSL_Certificate(const IncomingSSLInfo&);
bool active;
int lsock;
std::list<IncomingSSLInfo> incoming_ssl ;
protected:
p3PeerMgr *mPeerMgr;
private:
int Extract_Failed_SSL_Certificate(const IncomingSSLInfo&);
bool active;
int lsock;
std::list<IncomingSSLInfo> incoming_ssl ;
};
class pqissllistener: public pqissllistenbase
{
public:
public:
pqissllistener(const struct sockaddr_storage &addr, p3PeerMgr *pm) :
pqissllistenbase(addr, pm) {}
virtual ~pqissllistener() {}
pqissllistener(const struct sockaddr_storage &addr, p3PeerMgr *pm);
virtual ~pqissllistener();
int addlistenaddr(const RsPeerId& id, pqissl *acc);
int removeListenPort(const RsPeerId& id);
int addlistenaddr(const RsPeerId& id, pqissl *acc);
int removeListenPort(const RsPeerId& id);
virtual int status();
virtual int completeConnection(int sockfd, IncomingSSLInfo&);
virtual int finaliseConnection(int fd, SSL *ssl, const RsPeerId& peerId,
const sockaddr_storage &raddr);
//virtual int tick();
virtual int status();
virtual int completeConnection(int sockfd, IncomingSSLInfo&);
virtual int finaliseConnection(int fd, SSL *ssl, const RsPeerId& peerId, const struct sockaddr_storage &raddr);
private:
std::map<RsPeerId, pqissl *> listenaddr;
private:
std::map<RsPeerId, pqissl*> listenaddr;
};

View file

@ -91,7 +91,36 @@ pqiperson * pqisslpersongrp::locked_createPerson(const RsPeerId& id, pqilistener
pqiconnect *pqisc = new pqiconnect(pqip, rss, pqis);
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_TCP, pqisc);
/* first select type based on peer */
uint32_t typePeer = mPeerMgr->getHiddenType(id);
switch (typePeer) {
case RS_HIDDEN_TYPE_TOR:
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_TOR_TCP, pqisc);
break;
case RS_HIDDEN_TYPE_I2P:
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqisc);
break;
default:
/* peer is not a hidden one but we are */
/* select type based on ourselves */
uint32_t typeOwn = mPeerMgr->getHiddenType(AuthSSL::getAuthSSL()->OwnId());
switch (typeOwn) {
case RS_HIDDEN_TYPE_I2P:
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqisc);
break;
default:
/* this case shouldn't happen! */
std::cerr << "pqisslpersongrp::locked_createPerson WARNING INVALID HIDDEN TYPES - THIS SHOULD NOT HAPPEN!" << std::endl;
std::cerr << " - ID: " << id << std::endl;
std::cerr << " - mPeerMgr->isHidden(): " << mPeerMgr->isHidden() << std::endl;
std::cerr << " - mPeerMgr->isHiddenPeer(id): " << mPeerMgr->isHiddenPeer(id) << std::endl;
std::cerr << " - hidden types: peer=" << typePeer << " own=" << typeOwn << std::endl;
std::cerr << " --> falling back to Tor" << std::endl;
case RS_HIDDEN_TYPE_TOR:
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_TOR_TCP, pqisc);
break;
}
}
}
else
{

View file

@ -51,22 +51,22 @@ static const uint32_t PQI_SSLUDP_DEF_CONN_PERIOD = 300; /* 5 minutes? */
/********** PQI SSL UDP STUFF **************************************/
pqissludp::pqissludp(PQInterface *parent, p3LinkMgr *lm)
:pqissl(NULL, parent, lm), tou_bio(NULL),
listen_checktime(0), mConnectPeriod(PQI_SSLUDP_DEF_CONN_PERIOD), mConnectFlags(0), mConnectBandwidth(0)
pqissludp::pqissludp(PQInterface *parent, p3LinkMgr *lm) :
pqissl(NULL, parent, lm), tou_bio(NULL), listen_checktime(0),
mConnectPeriod(PQI_SSLUDP_DEF_CONN_PERIOD), mConnectFlags(0),
mConnectBandwidth(0)
{
RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/
RS_STACK_MUTEX(mSslMtx);
sockaddr_storage_clear(remote_addr);
sockaddr_storage_clear(mConnectProxyAddr);
sockaddr_storage_clear(mConnectSrcAddr);
return;
}
pqissludp::~pqissludp()
{
rslog(RSL_ALERT, pqissludpzone,
rslog(RSL_ALERT, pqissludpzone,
"pqissludp::~pqissludp -> destroying pqissludp");
/* must call reset from here, so that the
@ -76,16 +76,13 @@ pqissludp::~pqissludp()
* This means that reset() will be called twice, but this should
* be harmless.
*/
stoplistening(); /* remove from p3proxy listenqueue */
reset();
stoplistening(); /* remove from p3proxy listenqueue */
reset();
RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/
RS_STACK_MUTEX(mSslMtx);
if (tou_bio) // this should be in the reset?
{
BIO_free(tou_bio);
}
return;
}
int pqissludp::reset_locked()
@ -153,9 +150,9 @@ int pqissludp::attach()
// The Address determination is done centrally
int pqissludp::Initiate_Connection()
int pqissludp::Initiate_Connection()
{
int err;
int err=0;
attach(); /* open socket */
//remote_addr.sin_family = AF_INET;
@ -283,7 +280,7 @@ int pqissludp::Initiate_Connection()
proxyaddr.sin_port = pap->sin_port;
remoteaddr.sin_port = rap->sin_port;
tou_connect_via_relay(sockfd, &srcaddr, &proxyaddr, &remoteaddr);
err = tou_connect_via_relay(sockfd, &srcaddr, &proxyaddr, &remoteaddr);
}
@ -349,7 +346,7 @@ int pqissludp::Initiate_Connection()
}
/********* VERY DIFFERENT **********/
int pqissludp::Basic_Connection_Complete()
int pqissludp::Basic_Connection_Complete()
{
rslog(RSL_DEBUG_BASIC, pqissludpzone,
"pqissludp::Basic_Connection_Complete()...");
@ -449,15 +446,6 @@ int pqissludp::net_internal_fcntl_nonblock(int /*fd*/)
}
/* These are identical to pqinetssl version */
//int pqissludp::status()
int pqissludp::tick()
{
pqissl::tick();
return 1;
}
// listen fns call the udpproxy.
int pqissludp::listen()
{

View file

@ -45,7 +45,7 @@
class pqissludp;
class cert;
/* This provides a NetBinInterface, which is
/* This provides a NetBinInterface, which is
* primarily inherited from pqissl.
* fns declared here are different -> all others are identical.
*/
@ -53,49 +53,48 @@ class cert;
class pqissludp: public pqissl
{
public:
pqissludp(PQInterface *parent, p3LinkMgr *lm);
pqissludp(PQInterface *parent, p3LinkMgr *lm);
virtual ~pqissludp();
virtual ~pqissludp();
// NetInterface.
// listen fns call the udpproxy.
virtual int listen();
virtual int stoplistening();
virtual int tick();
virtual int listen();
virtual int stoplistening();
virtual bool connect_parameter(uint32_t type, uint32_t value);
virtual bool connect_additional_address(uint32_t type, const struct sockaddr_storage &addr);
virtual bool connect_parameter(uint32_t type, uint32_t value);
virtual bool connect_additional_address(uint32_t type, const struct sockaddr_storage &addr);
// BinInterface.
// These are reimplemented.
virtual bool moretoread(uint32_t usec);
virtual bool cansend(uint32_t usec);
virtual bool moretoread(uint32_t usec);
virtual bool cansend(uint32_t usec);
/* UDP always through firewalls -> always bandwidth Limited */
virtual bool bandwidthLimited() { return true; }
virtual bool bandwidthLimited() { return true; }
protected:
// pqissludp specific.
// called to initiate a connection;
int attach();
int attach();
virtual int reset_locked();
virtual int reset_locked();
virtual int Initiate_Connection();
virtual int Basic_Connection_Complete();
virtual int Initiate_Connection();
virtual int Basic_Connection_Complete();
//protected internal fns that are overloaded for udp case.
virtual int net_internal_close(int fd);
virtual int net_internal_SSL_set_fd(SSL *ssl, int fd);
virtual int net_internal_fcntl_nonblock(int fd);
/* Do we really need this ?
* It is very specific UDP+ToU+SSL stuff and unlikely to be reused.
* In fact we are overloading them here becase they are very do different of pqissl.
*/
virtual int net_internal_close(int fd);
virtual int net_internal_SSL_set_fd(SSL *ssl, int fd);
virtual int net_internal_fcntl_nonblock(int fd);
private:
BIO *tou_bio; // specific to ssludp.
//int remote_timeout;
//int proxy_timeout;
long listen_checktime;
uint32_t mConnectPeriod;

View file

@ -54,7 +54,7 @@ const int PQISTREAM_ABS_MAX = 100000000; /* 100 MB/sec (actually per loop) */
pqistreamer::pqistreamer(RsSerialiser *rss, const RsPeerId& id, BinInterface *bio_in, int bio_flags_in)
:PQInterface(id), mStreamerMtx("pqistreamer"),
mBio(bio_in), mBio_flags(bio_flags_in), mRsSerialiser(rss),
mPkt_wpending(NULL),
mPkt_wpending(NULL), mPkt_wpending_size(0),
mTotalRead(0), mTotalSent(0),
mCurrRead(0), mCurrSent(0),
mAvgReadCount(0), mAvgSentCount(0)
@ -117,6 +117,7 @@ pqistreamer::~pqistreamer()
{
free(mPkt_wpending);
mPkt_wpending = NULL;
mPkt_wpending_size = 0 ;
}
free_rpend_locked();
@ -416,104 +417,140 @@ time_t pqistreamer::getLastIncomingTS()
int pqistreamer::handleoutgoing_locked()
{
#ifdef DEBUG_PQISTREAMER
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::handleoutgoing_locked()");
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::handleoutgoing_locked()");
#endif
int maxbytes = outAllowedBytes_locked();
int sentbytes = 0;
int len;
int ss;
// std::cerr << "pqistreamer: maxbytes=" << maxbytes<< std::endl ;
int maxbytes = outAllowedBytes_locked();
int sentbytes = 0;
// std::cerr << "pqistreamer: maxbytes=" << maxbytes<< std::endl ;
std::list<void *>::iterator it;
std::list<void *>::iterator it;
// if not connection, or cannot send anything... pause.
if (!(mBio->isactive()))
{
/* if we are not active - clear anything in the queues. */
locked_clear_out_queue() ;
// if not connection, or cannot send anything... pause.
if (!(mBio->isactive()))
{
/* if we are not active - clear anything in the queues. */
locked_clear_out_queue() ;
/* also remove the pending packets */
if (mPkt_wpending)
{
free(mPkt_wpending);
mPkt_wpending = NULL;
}
/* also remove the pending packets */
if (mPkt_wpending)
{
free(mPkt_wpending);
mPkt_wpending = NULL;
mPkt_wpending_size = 0 ;
}
outSentBytes_locked(sentbytes);
return 0;
}
outSentBytes_locked(sentbytes);
return 0;
}
// a very simple round robin
// a very simple round robin
bool sent = true;
while(sent) // catch if all items sent.
{
sent = false;
bool sent = true;
int nsent = 0 ;
while(sent) // catch if all items sent.
{
sent = false;
if ((!(mBio->cansend(0))) || (maxbytes < sentbytes))
{
if ((!(mBio->cansend(0))) || (maxbytes < sentbytes))
{
#ifdef DEBUG_TRANSFERS
if (maxbytes < sentbytes)
{
std::cerr << "pqistreamer::handleoutgoing_locked() Stopped sending sentbytes > maxbytes. Sent " << sentbytes << " bytes ";
std::cerr << std::endl;
}
else
{
std::cerr << "pqistreamer::handleoutgoing_locked() Stopped sending at cansend() is false";
std::cerr << std::endl;
}
if (maxbytes < sentbytes)
{
std::cerr << "pqistreamer::handleoutgoing_locked() Stopped sending sentbytes > maxbytes. Sent " << sentbytes << " bytes ";
std::cerr << std::endl;
}
else
{
std::cerr << "pqistreamer::handleoutgoing_locked() Stopped sending at cansend() is false";
std::cerr << std::endl;
}
#endif
outSentBytes_locked(sentbytes);
return 0;
}
outSentBytes_locked(sentbytes);
return 0;
}
#define GROUP_OUTGOING_PACKETS 1
// send a out_pkt., else send out_data. unless
// there is a pending packet.
if (!mPkt_wpending)
#ifdef GROUP_OUTGOING_PACKETS
{
void *dta;
mPkt_wpending_size = 0 ;
int k=0;
while(mPkt_wpending_size < maxbytes && (dta = locked_pop_out_data())!=NULL )
{
uint32_t s = getRsItemSize(dta);
mPkt_wpending = realloc(mPkt_wpending,s+mPkt_wpending_size) ;
memcpy( &((char*)mPkt_wpending)[mPkt_wpending_size],dta,s) ;
free(dta);
mPkt_wpending_size += s ;
++k ;
}
#ifdef DEBUG_PQISTREAMER
if(k > 1)
std::cerr << "Packed " << k << " packets into " << mPkt_wpending_size << " bytes." << std::endl;
#endif
}
#else
{
void *dta = locked_pop_out_data() ;
// send a out_pkt., else send out_data. unless
// there is a pending packet.
if (!mPkt_wpending)
mPkt_wpending = locked_pop_out_data() ;
if (mPkt_wpending)
if(dta != NULL)
{
// write packet.
len = getRsItemSize(mPkt_wpending);
#ifdef DEBUG_PQISTREAMER
std::cout << "Sending Out Pkt of size " << len << " !" << std::endl;
#endif
if (len != (ss = mBio->senddata(mPkt_wpending, len)))
{
#ifdef DEBUG_PQISTREAMER
std::string out;
rs_sprintf(out, "Problems with Send Data! (only %d bytes sent, total pkt size=%d)", ss, len);
// std::cerr << out << std::endl ;
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out);
#endif
outSentBytes_locked(sentbytes);
// pkt_wpending will kept til next time.
// ensuring exactly the same data is written (openSSL requirement).
return -1;
}
#ifdef DEBUG_TRANSFERS
std::cerr << "pqistreamer::handleoutgoing_locked() Sent Packet len: " << len << " @ " << RsUtil::AccurateTimeString();
std::cerr << std::endl;
#endif
free(mPkt_wpending);
mPkt_wpending = NULL;
sentbytes += len;
sent = true;
mPkt_wpending = dta ;
mPkt_wpending_size = getRsItemSize(dta);
}
}
outSentBytes_locked(sentbytes);
return 1;
#endif
if (mPkt_wpending)
{
// write packet.
#ifdef DEBUG_PQISTREAMER
std::cout << "Sending Out Pkt of size " << mPkt_wpending_size << " !" << std::endl;
#endif
int ss=0;
if (mPkt_wpending_size != (ss = mBio->senddata(mPkt_wpending, mPkt_wpending_size)))
{
#ifdef DEBUG_PQISTREAMER
std::string out;
rs_sprintf(out, "Problems with Send Data! (only %d bytes sent, total pkt size=%d)", ss, mPkt_wpending_size);
// std::cerr << out << std::endl ;
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out);
#endif
outSentBytes_locked(sentbytes);
// pkt_wpending will kept til next time.
// ensuring exactly the same data is written (openSSL requirement).
return -1;
}
++nsent;
#ifdef DEBUG_TRANSFERS
std::cerr << "pqistreamer::handleoutgoing_locked() Sent Packet len: " << mPkt_wpending_size << " @ " << RsUtil::AccurateTimeString();
std::cerr << std::endl;
#endif
sentbytes += mPkt_wpending_size;
free(mPkt_wpending);
mPkt_wpending = NULL;
mPkt_wpending_size = 0 ;
sent = true;
}
}
#ifdef DEBUG_PQISTREAMER
if(nsent > 0)
std::cerr << "nsent = " << nsent << ", total bytes=" << sentbytes << std::endl;
#endif
outSentBytes_locked(sentbytes);
return 1;
}

View file

@ -113,6 +113,7 @@ class pqistreamer: public PQInterface
RsSerialiser *mRsSerialiser;
void *mPkt_wpending; // storage for pending packet to write.
uint32_t mPkt_wpending_size; // ... and its size.
void allocate_rpend_locked(); // use these two functions to allocate/free the buffer below
void free_rpend_locked();

View file

@ -72,102 +72,79 @@ X509_REQ *GenerateX509Req(
std::string loc, std::string state, std::string country,
int nbits_in, std::string &errString)
{
/* generate request */
X509_REQ *req=X509_REQ_new();
/* generate request */
X509_REQ *req=X509_REQ_new();
// setup output.
BIO *bio_out = NULL;
bio_out = BIO_new(BIO_s_file());
BIO_set_fp(bio_out,stdout,BIO_NOCLOSE);
// setup output.
BIO *bio_out = BIO_new(BIO_s_file());
BIO_set_fp(bio_out,stdout,BIO_NOCLOSE);
EVP_PKEY *pkey = NULL;
EVP_PKEY *pkey = NULL;
X509_NAME *x509_name = NULL ;
RSA *rsa = NULL ;
// first generate a key....
if ((pkey=EVP_PKEY_new()) == NULL)
{
fprintf(stderr,"GenerateX509Req: Couldn't Create Key\n");
errString = "Couldn't Create Key";
return 0;
}
try
{
// first generate a key....
if ((pkey=EVP_PKEY_new()) == NULL)
{
fprintf(stderr,"GenerateX509Req: Couldn't Create Key\n");
throw std::runtime_error("Couldn't Create Key") ;
}
int nbits = 2048;
unsigned long e = 0x10001;
int nbits = 2048;
unsigned long e = 0x10001;
if ((nbits_in >= 512) && (nbits_in <= 4096))
{
nbits = nbits_in;
}
else
{
fprintf(stderr,"GenerateX509Req: strange num of nbits: %d\n", nbits_in);
fprintf(stderr,"GenerateX509Req: reverting to %d\n", nbits);
}
if ((nbits_in >= 512) && (nbits_in <= 4096))
{
nbits = nbits_in;
}
else
{
fprintf(stderr,"GenerateX509Req: strange num of nbits: %d\n", nbits_in);
fprintf(stderr,"GenerateX509Req: reverting to %d\n", nbits);
}
rsa = RSA_generate_key(nbits, e, NULL, NULL);
RSA *rsa = RSA_generate_key(nbits, e, NULL, NULL);
if ((rsa == NULL) || !EVP_PKEY_assign_RSA(pkey, rsa))
{
if(rsa) RSA_free(rsa);
fprintf(stderr,"GenerateX509Req: Couldn't Generate RSA Key!\n");
errString = "Couldn't generate RSA Key";
return 0;
}
if ((rsa == NULL) || !EVP_PKEY_assign_RSA(pkey, rsa))
throw std::runtime_error("Couldn't generate RSA Key");
// open the file.
FILE *out;
if (NULL == (out = RsDirUtil::rs_fopen(pkey_file.c_str(), "w")))
throw std::runtime_error("Couldn't Create Key File \"" + pkey_file + "\"");
// open the file.
FILE *out;
if (NULL == (out = RsDirUtil::rs_fopen(pkey_file.c_str(), "w")))
{
fprintf(stderr,"GenerateX509Req: Couldn't Create Key File!");
fprintf(stderr," : %s\n", pkey_file.c_str());
const EVP_CIPHER *cipher = EVP_des_ede3_cbc();
errString = "Couldn't Create Key File";
return 0;
}
if (!PEM_write_PrivateKey(out,pkey,cipher, NULL,0,NULL,(void *) passwd.c_str()))
{
fclose(out) ;
throw std::runtime_error("Couldn't Save Private Key to file \""+pkey_file+"\"");
}
const EVP_CIPHER *cipher = EVP_des_ede3_cbc();
fclose(out);
if (!PEM_write_PrivateKey(out,pkey,cipher,
NULL,0,NULL,(void *) passwd.c_str()))
{
fprintf(stderr,"GenerateX509Req() Couldn't Save Private Key");
fprintf(stderr," : %s\n", pkey_file.c_str());
// We have now created a private key....
std::cerr << "GenerateX509Req() Saved Private Key to file \"" << pkey_file << "\"" << std::endl;
errString = "Couldn't Save Private Key File";
return 0;
}
fclose(out);
/********** Test Loading the private Key.... ************/
FILE *tst_in = NULL;
EVP_PKEY *tst_pkey = NULL;
// We have now created a private key....
fprintf(stderr,"GenerateX509Req() Saved Private Key");
fprintf(stderr," : %s\n", pkey_file.c_str());
if (NULL == (tst_in = RsDirUtil::rs_fopen(pkey_file.c_str(), "rb")))
throw std::runtime_error("GenerateX509Req() Couldn't Open Private Key file \""+pkey_file+"\"") ;
/********** Test Loading the private Key.... ************/
FILE *tst_in = NULL;
EVP_PKEY *tst_pkey = NULL;
if (NULL == (tst_in = RsDirUtil::rs_fopen(pkey_file.c_str(), "rb")))
{
fprintf(stderr,"GenerateX509Req() Couldn't Open Private Key");
fprintf(stderr," : %s\n", pkey_file.c_str());
if (NULL == (tst_pkey = PEM_read_PrivateKey(tst_in,NULL,NULL,(void *) passwd.c_str())))
{
fclose(tst_in);
throw std::runtime_error("GenerateX509Req() Couldn't read Private Key file \""+pkey_file+"\"") ;
}
errString = "Couldn't Open Private Key";
return 0;
}
fclose(tst_in);
EVP_PKEY_free(tst_pkey);
if (NULL == (tst_pkey =
PEM_read_PrivateKey(tst_in,NULL,NULL,(void *) passwd.c_str())))
{
fprintf(stderr,"GenerateX509Req() Couldn't Read Private Key");
fprintf(stderr," : %s\n", pkey_file.c_str());
errString = "Couldn't Read Private Key";
return 0;
}
fclose(tst_in);
EVP_PKEY_free(tst_pkey);
/********** Test Loading the private Key.... ************/
/* Fill in details: fields.
/* Fill in details: fields.
req->req_info;
req->req_info->enc;
req->req_info->version;
@ -175,108 +152,88 @@ X509_REQ *GenerateX509Req(
req->req_info->pubkey;
****************************/
long version = 0x00;
unsigned long chtype = MBSTRING_UTF8;
X509_NAME *x509_name = X509_NAME_new();
long version = 0x00;
unsigned long chtype = MBSTRING_UTF8;
x509_name = X509_NAME_new();
// fill in the request.
// fill in the request.
/**** X509_REQ -> Version ********************************/
if (!X509_REQ_set_version(req,version)) /* version 1 */
{
fprintf(stderr,"GenerateX509Req(): Couldn't Set Version!\n");
/**** X509_REQ -> Version ********************************/
if(!X509_REQ_set_version(req,version)) /* version 1 */
throw std::runtime_error("GenerateX509Req(): Couldn't Set SSL certificate Version!");
errString = "Couldn't Set Version";
return 0;
}
/**** X509_REQ -> Version ********************************/
/**** X509_REQ -> Key ********************************/
/**** X509_REQ -> Version ********************************/
/**** X509_REQ -> Key ********************************/
if (!X509_REQ_set_pubkey(req,pkey))
{
fprintf(stderr,"GenerateX509Req() Couldn't Set PUBKEY !\n");
if (!X509_REQ_set_pubkey(req,pkey))
throw std::runtime_error("GenerateX509Req(): Couldn't Set SSL certificate PUBKEY!");
errString = "Couldn't Set PubKey";
return 0;
}
/**** SUBJECT ********************************/
// create the name.
/**** SUBJECT ********************************/
// create the name.
// fields to add.
// commonName CN
// emailAddress (none)
// organizationName O
// localityName L
// stateOrProvinceName ST
// countryName C
// fields to add.
// commonName CN
// emailAddress (none)
// organizationName O
// localityName L
// stateOrProvinceName ST
// countryName C
if (0 == strlen(name.c_str()))
throw std::runtime_error("No name! Aborting.") ;
if (0 < strlen(name.c_str()))
{
X509_NAME_add_entry_by_txt(x509_name, "CN", chtype,
(unsigned char *) name.c_str(), -1, -1, 0);
}
else
{
fprintf(stderr,"GenerateX509Req(): No Name -> Not creating X509 Cert Req\n");
errString = "No Name, Aborting";
return 0;
}
X509_NAME_add_entry_by_txt(x509_name, "CN", chtype, (unsigned char *) name.c_str(), -1, -1, 0);
if (0 < strlen(email.c_str()))
{
//X509_NAME_add_entry_by_txt(x509_name, "Email", 0,
// (unsigned char *) ui -> gen_email -> value(), -1, -1, 0);
X509_NAME_add_entry_by_NID(x509_name, 48, 0,
(unsigned char *) email.c_str(), -1, -1, 0);
}
if (0 < strlen(email.c_str()))
X509_NAME_add_entry_by_NID(x509_name, 48, 0, (unsigned char *) email.c_str(), -1, -1, 0);
if (0 < strlen(org.c_str()))
{
X509_NAME_add_entry_by_txt(x509_name, "O", chtype,
(unsigned char *) org.c_str(), -1, -1, 0);
}
if (0 < strlen(org.c_str()))
X509_NAME_add_entry_by_txt(x509_name, "O", chtype, (unsigned char *) org.c_str(), -1, -1, 0);
if (0 < strlen(loc.c_str()))
{
X509_NAME_add_entry_by_txt(x509_name, "L", chtype,
(unsigned char *) loc.c_str(), -1, -1, 0);
}
if (0 < strlen(loc.c_str()))
X509_NAME_add_entry_by_txt(x509_name, "L", chtype, (unsigned char *) loc.c_str(), -1, -1, 0);
if (0 < strlen(state.c_str()))
{
X509_NAME_add_entry_by_txt(x509_name, "ST", chtype,
(unsigned char *) state.c_str(), -1, -1, 0);
}
if (0 < strlen(state.c_str()))
X509_NAME_add_entry_by_txt(x509_name, "ST", chtype, (unsigned char *) state.c_str(), -1, -1, 0);
if (0 < strlen(country.c_str()))
{
X509_NAME_add_entry_by_txt(x509_name, "C", chtype,
(unsigned char *) country.c_str(), -1, -1, 0);
}
if (0 < strlen(country.c_str()))
X509_NAME_add_entry_by_txt(x509_name, "C", chtype, (unsigned char *) country.c_str(), -1, -1, 0);
if (!X509_REQ_set_subject_name(req,x509_name))
{
fprintf(stderr,"GenerateX509Req() Couldn't Set Name to Request!\n");
X509_NAME_free(x509_name);
if (!X509_REQ_set_subject_name(req,x509_name))
throw std::runtime_error("GenerateX509Req() Couldn't Set Name to Request!");
errString = "Couldn't Set Name";
return 0;
}
X509_NAME_free(x509_name);
X509_NAME_free(x509_name);
/**** SUBJECT ********************************/
/**** SUBJECT ********************************/
if (!X509_REQ_sign(req,pkey,EVP_sha1()))
{
fprintf(stderr,"GenerateX509Req() Failed to Sign REQ\n");
if (!X509_REQ_sign(req,pkey,EVP_sha1()))
throw std::runtime_error("GenerateX509Req() Failed to sign REQ");
errString = "Couldn't Sign Req";
return 0;
}
errString = "No Error";
errString = "No Error";
return req;
return req;
}
catch(std::exception& e)
{
std::cerr << "(EE) Key creation failed: " << e.what() << std::endl;
errString = e.what() ;
req = NULL ;
}
if(rsa)
RSA_free(rsa);
if(x509_name)
X509_NAME_free(x509_name);
if(bio_out)
BIO_free_all(bio_out) ;
if(pkey)
EVP_PKEY_free(pkey);
return req ;
}
#define SERIAL_RAND_BITS 64

View file

@ -1,25 +0,0 @@
RS_TOP_DIR = ..
##### Define any flags that are needed for this section #######
###############################################################
###############################################################
include $(RS_TOP_DIR)/scripts/config.mk
###############################################################
OBJ = notifytxt.o retroshare.o
#TESTOBJ =
TESTS = retroshare-nogui
all: tests
retroshare-nogui: $(OBJ)
$(CC) $(CFLAGS) -o retroshare-nogui $(OBJ) $(LIBS)
###############################################################
include $(RS_TOP_DIR)/scripts/rules.mk
###############################################################

View file

@ -47,7 +47,12 @@ template<int n> class t_RsFlags32
friend std::ostream& operator<<(std::ostream& o,const t_RsFlags32<n>& f) // friendly print with 0 and I
{
for(int i=31;i>=0;--i) { o << ( (f._bits&(1<<i))?"I":"0") ; if(i%8==0) o << " " ; }
for(int i=31;i>=0;--i) {
std::string res = f._bits&(1<<i)?"I":"0" ;
std::string blank = " " ;
o << res ;
if(i%8==0) o << blank ;
}
return o ;
}
private:

View file

@ -102,6 +102,9 @@ public:
//===================================================//
virtual void addRoutingClue(const GRouterKeyId& destination, const RsPeerId& source) =0;
virtual void addTrackingInfo(const RsGxsMessageId& mid,const RsPeerId& peer_id) =0;
virtual bool getTrackingInfo(const RsGxsMessageId& mid, RsPeerId& provider_id) =0;
};
// To access the GRouter from anywhere

View file

@ -18,48 +18,36 @@ namespace GXS_SERV {
/** START privacy **/
static const uint32_t FLAG_PRIVACY_MASK = 0x0000000f;
// pub key encrypted
static const uint32_t FLAG_PRIVACY_PRIVATE = 0x00000001;
// publish private key needed to publish
static const uint32_t FLAG_PRIVACY_RESTRICTED = 0x00000002;
// anyone can publish, publish key pair not needed
static const uint32_t FLAG_PRIVACY_PUBLIC = 0x00000004;
static const uint32_t FLAG_PRIVACY_MASK = 0x0000000f;
static const uint32_t FLAG_PRIVACY_PRIVATE = 0x00000001; // pub key encrypted
static const uint32_t FLAG_PRIVACY_RESTRICTED = 0x00000002; // publish private key needed to publish
static const uint32_t FLAG_PRIVACY_PUBLIC = 0x00000004; // anyone can publish, publish key pair not needed
/** END privacy **/
/** START authentication **/
static const uint32_t FLAG_AUTHEN_MASK = 0x000000f0;
// identity
static const uint32_t FLAG_AUTHEN_IDENTITY = 0x000000010;
// publish key
static const uint32_t FLAG_AUTHEN_PUBLISH = 0x000000020;
// admin key
static const uint32_t FLAG_AUTHEN_ADMIN = 0x00000040;
// pgp sign identity
static const uint32_t FLAG_AUTHEN_PGP_IDENTITY = 0x00000080;
/** END authentication **/
/** START author authentication flags **/
static const uint32_t FLAG_AUTHOR_AUTHENTICATION_MASK = 0x0000ff00;
static const uint32_t FLAG_AUTHOR_AUTHENTICATION_NONE = 0x00000000;
static const uint32_t FLAG_AUTHOR_AUTHENTICATION_GPG = 0x00000100;
static const uint32_t FLAG_AUTHOR_AUTHENTICATION_REQUIRED = 0x00000200;
static const uint32_t FLAG_AUTHOR_AUTHENTICATION_IFNOPUBSIGN = 0x00000400;
static const uint32_t FLAG_AUTHOR_AUTHENTICATION_TRACK_MESSAGES = 0x00000800;
static const uint32_t FLAG_GROUP_SIGN_PUBLISH_MASK = 0x000000ff;
static const uint32_t FLAG_GROUP_SIGN_PUBLISH_ENCRYPTED = 0x00000001;
static const uint32_t FLAG_GROUP_SIGN_PUBLISH_ALLSIGNED = 0x00000002;
static const uint32_t FLAG_GROUP_SIGN_PUBLISH_THREADHEAD = 0x00000004;
static const uint32_t FLAG_GROUP_SIGN_PUBLISH_NONEREQ = 0x00000008;
/** START msg authentication flags **/
static const uint8_t MSG_AUTHEN_MASK = 0x0f;
static const uint8_t MSG_AUTHEN_ROOT_PUBLISH_SIGN = 0x01;
static const uint8_t MSG_AUTHEN_MASK = 0x0f;
static const uint8_t MSG_AUTHEN_ROOT_PUBLISH_SIGN = 0x01;
static const uint8_t MSG_AUTHEN_CHILD_PUBLISH_SIGN = 0x02;
static const uint8_t MSG_AUTHEN_ROOT_AUTHOR_SIGN = 0x04;
static const uint8_t MSG_AUTHEN_CHILD_AUTHOR_SIGN = 0x08;
static const uint8_t MSG_AUTHEN_ROOT_AUTHOR_SIGN = 0x04;
static const uint8_t MSG_AUTHEN_CHILD_AUTHOR_SIGN = 0x08;
/** END msg authentication flags **/
@ -71,12 +59,9 @@ namespace GXS_SERV {
/** START Subscription Flags. (LOCAL) **/
static const uint32_t GROUP_SUBSCRIBE_ADMIN = 0x01;
static const uint32_t GROUP_SUBSCRIBE_PUBLISH = 0x02;
static const uint32_t GROUP_SUBSCRIBE_SUBSCRIBED = 0x04;
static const uint32_t GROUP_SUBSCRIBE_ADMIN = 0x01;
static const uint32_t GROUP_SUBSCRIBE_PUBLISH = 0x02;
static const uint32_t GROUP_SUBSCRIBE_SUBSCRIBED = 0x04;
static const uint32_t GROUP_SUBSCRIBE_NOT_SUBSCRIBED = 0x08;
/*!
@ -95,17 +80,12 @@ namespace GXS_SERV {
*
* NOTE: RsGxsCommentService uses 0x000f0000.
*/
static const uint32_t GXS_MSG_STATUS_GEN_MASK = 0x0000ffff;
static const uint32_t GXS_MSG_STATUS_GEN_MASK = 0x0000ffff;
static const uint32_t GXS_MSG_STATUS_UNPROCESSED = 0x00000001;
static const uint32_t GXS_MSG_STATUS_GUI_UNREAD = 0x00000002;
static const uint32_t GXS_MSG_STATUS_GUI_NEW = 0x00000004;
static const uint32_t GXS_MSG_STATUS_KEEP = 0x00000008;
static const uint32_t GXS_MSG_STATUS_DELETE = 0x000000020;
static const uint32_t GXS_MSG_STATUS_GUI_UNREAD = 0x00000002;
static const uint32_t GXS_MSG_STATUS_GUI_NEW = 0x00000004;
static const uint32_t GXS_MSG_STATUS_KEEP = 0x00000008;
static const uint32_t GXS_MSG_STATUS_DELETE = 0x00000020;
/** END GXS Msg status flags **/
@ -123,10 +103,13 @@ namespace GXS_SERV {
#define IS_MSG_NEW(status) (status & GXS_SERV::GXS_MSG_STATUS_GUI_NEW)
#define IS_MSG_UNREAD(status) (status & GXS_SERV::GXS_MSG_STATUS_GUI_UNREAD)
#define IS_GROUP_PGP_AUTHED(signFlags) (signFlags & GXS_SERV::FLAG_AUTHOR_AUTHENTICATION_GPG)
#define IS_GROUP_MESSAGE_TRACKING(signFlags) (signFlags & GXS_SERV::FLAG_AUTHOR_AUTHENTICATION_TRACK_MESSAGES)
#define IS_GROUP_ADMIN(subscribeFlags) (subscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN)
#define IS_GROUP_PUBLISHER(subscribeFlags) (subscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_PUBLISH)
#define IS_GROUP_PUBLISHER(subscribeFlags) (subscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_PUBLISH)
#define IS_GROUP_SUBSCRIBED(subscribeFlags) (subscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED)
#define IS_GROUP_NOT_SUBSCRIBED(subscribeFlags) (subscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_NOT_SUBSCRIBED)
#define IS_GROUP_NOT_SUBSCRIBED(subscribeFlags) (subscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_NOT_SUBSCRIBED)
#define IS_MSG_UNPROCESSED(status) (status & GXS_SERV::GXS_MSG_STATUS_UNPROCESSED)

View file

@ -0,0 +1,136 @@
/*
* libretroshare/src/services: rsgrouter.h
*
* Services for RetroShare.
*
* Copyright 2013 by Cyril Soler
*
* 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 "csoler@users.sourceforge.net".
*
*/
#pragma once
#include "util/rsdir.h"
#include "retroshare/rsids.h"
#include "retroshare/rsturtle.h"
#include "retroshare/rsgxsifacetypes.h"
class RsGxsTunnelService
{
public:
typedef GXSTunnelId RsGxsTunnelId ;
enum {
RS_GXS_TUNNEL_ERROR_NO_ERROR = 0x0000,
RS_GXS_TUNNEL_ERROR_UNKNOWN_GXS_ID = 0x0001
};
enum {
RS_GXS_TUNNEL_STATUS_UNKNOWN = 0x00,
RS_GXS_TUNNEL_STATUS_TUNNEL_DN = 0x01,
RS_GXS_TUNNEL_STATUS_CAN_TALK = 0x02,
RS_GXS_TUNNEL_STATUS_REMOTELY_CLOSED = 0x03
};
class RsGxsTunnelClientService
{
public:
// The client should derive this in order to handle notifications from the tunnel service.
// This cannot be ignored because the client needs to know when the tunnel is active.
virtual void notifyTunnelStatus(const RsGxsTunnelId& tunnel_id,uint32_t tunnel_status) =0;
// Data obtained from the corresponding GXS id. The memory ownership is transferred to the client, which
// is responsible to free it using free() once used.
virtual void receiveData(const RsGxsTunnelId& id,unsigned char *data,uint32_t data_size) =0;
// Used by the creator of the service to supply a pointer to the GXS tunnel service for it to be able to send data etc.
virtual void connectToGxsTunnelService(RsGxsTunnelService *tunnel_service) =0;
// Gives feedback about type of data that is allowed in. For security reasons, this always needs to be re-derived (Clients can return true on default)
virtual bool acceptDataFromPeer(const RsGxsId& gxs_id,const RsGxsTunnelId& tunnel_id) = 0 ;
};
class GxsTunnelInfo
{
public:
// Tunnel information
RsGxsTunnelId tunnel_id ;
RsGxsId destination_gxs_id ; // GXS Id we're talking to
RsGxsId source_gxs_id ; // GXS Id we're using to talk
uint32_t tunnel_status ; // active, requested, DH pending, etc.
uint32_t total_size_sent ; // total bytes sent through that tunnel since openned (including management).
uint32_t total_size_received ; // total bytes received through that tunnel since openned (including management).
// Data packets
uint32_t pending_data_packets; // number of packets not acknowledged by other side, still on their way. Should be 0 unless something bad happens.
uint32_t total_data_packets_sent ; // total number of data packets sent (does not include tunnel management)
uint32_t total_data_packets_received ; // total number of data packets received (does not include tunnel management)
};
// This is the interface file for the secured tunnel service
//
//===================================================//
// Debugging info //
//===================================================//
virtual bool getTunnelsInfo(std::vector<GxsTunnelInfo>& infos) =0;
virtual bool getTunnelInfo(const RsGxsTunnelId& tunnel_id,GxsTunnelInfo& info) =0;
// retrieve the routing probabilities
//===================================================//
// Communication to other services. //
//===================================================//
// Register a new client service. The service ID needs to be unique, and it's the coder's resonsibility to use an ID that is not used elsewhere
// for the same purpose.
virtual bool registerClientService(uint32_t service_id,RsGxsTunnelClientService *service) =0;
// Asks for a tunnel. The service will request it to turtle router, and exchange a AES key using DH.
// When the tunnel is secured, the client---here supplied as argument---will be notified. He can
// then send data into the tunnel. The same tunnel may be used by different clients.
// The service id is passed on so that the client is notified when the tunnel is up.
virtual bool requestSecuredTunnel(const RsGxsId& to_id,const RsGxsId& from_id,RsGxsTunnelId& tunnel_id,uint32_t service_id,uint32_t& error_code) =0 ;
// Data is sent through the established tunnel, possibly multiple times, until reception is acknowledged. If the tunnel does not exist, the item is rejected and
// an error is issued. In any case, the memory ownership of the data is *not* transferred to the tunnel service, so the client should delete it afterwards, if needed.
virtual bool sendData(const RsGxsTunnelId& tunnel_id, uint32_t client_service_id, const uint8_t *data, uint32_t data_size) =0;
// Removes any established tunnel to this GXS id. This makes the tunnel refuse further data, but the tunnel will be however kept alive
// until all pending data is flushed. All clients attached to the tunnel will be notified that the tunnel gets closed.
virtual bool closeExistingTunnel(const RsGxsTunnelId& to_id,uint32_t service_id) =0;
//===================================================//
// Routage feedback from other services //
//===================================================//
};
// To access the GRouter from anywhere
//
extern RsGxsTunnelService *rsGxsTunnel ;

View file

@ -32,6 +32,7 @@
#include "retroshare/rstokenservice.h"
#include "retroshare/rsgxsifacehelper.h"
#include "retroshare/rsreputations.h"
#include "retroshare/rsids.h"
#include "serialiser/rstlvimage.h"
#include "retroshare/rsgxscommon.h"
@ -64,6 +65,11 @@ extern RsIdentity *rsIdentity;
std::string rsIdTypeToString(uint32_t idtype);
static const uint32_t RS_IDENTITY_FLAGS_IS_A_CONTACT = 0x0001;
static const uint32_t RS_IDENTITY_FLAGS_PGP_LINKED = 0x0002;
static const uint32_t RS_IDENTITY_FLAGS_PGP_KNOWN = 0x0004;
static const uint32_t RS_IDENTITY_FLAGS_IS_OWN_ID = 0x0008;
class GxsReputation
{
public:
@ -114,8 +120,9 @@ class RsGxsIdGroup
time_t mLastUsageTS ;
// Not Serialised - for GUI's benefit.
bool mPgpKnown;
RsPgpId mPgpId;
bool mPgpKnown;
bool mIsAContact; // change that into flags one day
RsPgpId mPgpId;
GxsReputation mReputation;
};
@ -153,30 +160,31 @@ class RsRecognTagDetails
bool is_pending;
};
class RsIdentityDetails
{
public:
RsIdentityDetails()
:mIsOwnId(false), mPgpLinked(false), mPgpKnown(false),
mReputation(), mLastUsageTS(0) { return; }
: mFlags(0), mLastUsageTS(0) { return; }
RsGxsId mId;
// identity details.
std::string mNickname;
bool mIsOwnId;
uint32_t mFlags ;
// PGP Stuff.
bool mPgpLinked;
bool mPgpKnown;
RsPgpId mPgpId;
// Recogn details.
std::list<RsRecognTag> mRecognTags;
// reputation details.
GxsReputation mReputation;
// Cyril: Reputation details. At some point we might want to merge information
// between the two into a single global score. Since the old reputation system
// is not finished yet, I leave this in place. We should decide what to do with it.
GxsReputation mReputation_oldSystem; // this is the old "mReputation" field, which apparently is not used.
RsReputations::ReputationInfo mReputation;
// avatar
RsGxsImage mAvatar ;
@ -214,7 +222,7 @@ public:
/********************************************************************************************/
/********************************************************************************************/
// For Other Services....
// It should be impossible for them to get a message which we don't have the identity.
// Its a major error if we don't have the identity.
@ -241,6 +249,9 @@ virtual bool parseRecognTag(const RsGxsId &id, const std::string &nickname,
virtual bool getRecognTagRequest(const RsGxsId &id, const std::string &comment,
uint16_t tag_class, uint16_t tag_type, std::string &tag) = 0;
virtual bool setAsRegularContact(const RsGxsId& id,bool is_a_contact) = 0 ;
virtual bool isARegularContact(const RsGxsId& id) = 0 ;
// Specific RsIdentity Functions....
/* Specific Service Data */
/* We expose these initially for testing / GUI purposes.

View file

@ -209,15 +209,17 @@ static const int SHA1_SIZE = 20 ;
// These constants are random, but should be different, in order to make the various IDs incompatible with each other.
//
static const uint32_t RS_GENERIC_ID_SSL_ID_TYPE = 0x0001 ;
static const uint32_t RS_GENERIC_ID_PGP_ID_TYPE = 0x0002 ;
static const uint32_t RS_GENERIC_ID_SHA1_ID_TYPE = 0x0003 ;
static const uint32_t RS_GENERIC_ID_PGP_FINGERPRINT_TYPE = 0x0004 ;
static const uint32_t RS_GENERIC_ID_GXS_GROUP_ID_TYPE = 0x0005 ;
static const uint32_t RS_GENERIC_ID_GXS_ID_TYPE = 0x0006 ;
static const uint32_t RS_GENERIC_ID_GXS_MSG_ID_TYPE = 0x0007 ;
static const uint32_t RS_GENERIC_ID_GXS_CIRCLE_ID_TYPE = 0x0008 ;
static const uint32_t RS_GENERIC_ID_GROUTER_ID_TYPE = 0x0009 ;
static const uint32_t RS_GENERIC_ID_SSL_ID_TYPE = 0x0001 ;
static const uint32_t RS_GENERIC_ID_PGP_ID_TYPE = 0x0002 ;
static const uint32_t RS_GENERIC_ID_SHA1_ID_TYPE = 0x0003 ;
static const uint32_t RS_GENERIC_ID_PGP_FINGERPRINT_TYPE = 0x0004 ;
static const uint32_t RS_GENERIC_ID_GXS_GROUP_ID_TYPE = 0x0005 ;
static const uint32_t RS_GENERIC_ID_GXS_ID_TYPE = 0x0006 ;
static const uint32_t RS_GENERIC_ID_GXS_MSG_ID_TYPE = 0x0007 ;
static const uint32_t RS_GENERIC_ID_GXS_CIRCLE_ID_TYPE = 0x0008 ;
static const uint32_t RS_GENERIC_ID_GROUTER_ID_TYPE = 0x0009 ;
static const uint32_t RS_GENERIC_ID_GXS_TUNNEL_ID_TYPE = 0x0010 ;
static const uint32_t RS_GENERIC_ID_GXS_DISTANT_CHAT_ID_TYPE = 0x0011 ;
typedef t_RsGenericIdType< SSL_ID_SIZE , false, RS_GENERIC_ID_SSL_ID_TYPE> SSLIdType ;
typedef t_RsGenericIdType< PGP_KEY_ID_SIZE , true, RS_GENERIC_ID_PGP_ID_TYPE> PGPIdType ;
@ -227,4 +229,6 @@ typedef t_RsGenericIdType< PGP_KEY_FINGERPRINT_SIZE, true, RS_GENERIC_ID_PGP_F
typedef t_RsGenericIdType< CERT_SIGN_LEN , false, RS_GENERIC_ID_GXS_GROUP_ID_TYPE > GXSGroupId ;
typedef t_RsGenericIdType< CERT_SIGN_LEN , false, RS_GENERIC_ID_GXS_ID_TYPE > GXSId ;
typedef t_RsGenericIdType< CERT_SIGN_LEN , false, RS_GENERIC_ID_GXS_CIRCLE_ID_TYPE > GXSCircleId ;
typedef t_RsGenericIdType< SSL_ID_SIZE , false, RS_GENERIC_ID_GXS_TUNNEL_ID_TYPE > GXSTunnelId ;
typedef t_RsGenericIdType< SSL_ID_SIZE , false, RS_GENERIC_ID_GXS_DISTANT_CHAT_ID_TYPE > DistantChatPeerId ;

View file

@ -99,9 +99,6 @@ typedef uint64_t ChatLobbyId ;
typedef uint64_t ChatLobbyMsgId ;
typedef std::string ChatLobbyNickName ;
typedef RsPeerId DistantChatPeerId ;
//typedef GRouterKeyId DistantMsgPeerId ;
typedef uint64_t MessageId ;
@ -253,12 +250,10 @@ public:
#define RS_CHAT_PRIVATE 0x0002
#define RS_CHAT_AVATAR_AVAILABLE 0x0004
#define RS_DISTANT_CHAT_STATUS_UNKNOWN 0x0000
#define RS_DISTANT_CHAT_STATUS_TUNNEL_DN 0x0001
#define RS_DISTANT_CHAT_STATUS_TUNNEL_OK 0x0002
#define RS_DISTANT_CHAT_STATUS_CAN_TALK 0x0003
#define RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED 0x0004
#define RS_DISTANT_CHAT_STATUS_WAITING_DH 0x0005
#define RS_DISTANT_CHAT_STATUS_UNKNOWN 0x0000
#define RS_DISTANT_CHAT_STATUS_TUNNEL_DN 0x0001
#define RS_DISTANT_CHAT_STATUS_CAN_TALK 0x0002
#define RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED 0x0003
#define RS_DISTANT_CHAT_ERROR_NO_ERROR 0x0000
#define RS_DISTANT_CHAT_ERROR_DECRYPTION_FAILED 0x0001
@ -269,6 +264,24 @@ public:
#define RS_DISTANT_CHAT_FLAG_SIGNED 0x0001
#define RS_DISTANT_CHAT_FLAG_SIGNATURE_OK 0x0002
// flags to define who we accept to talk to. Each flag *removes* some people.
#define RS_DISTANT_MESSAGING_CONTACT_PERMISSION_FLAG_FILTER_NONE 0x0000
#define RS_DISTANT_MESSAGING_CONTACT_PERMISSION_FLAG_FILTER_NON_CONTACTS 0x0001
#define RS_DISTANT_MESSAGING_CONTACT_PERMISSION_FLAG_FILTER_EVERYBODY 0x0002
#define RS_DISTANT_CHAT_CONTACT_PERMISSION_FLAG_FILTER_NONE 0x0000
#define RS_DISTANT_CHAT_CONTACT_PERMISSION_FLAG_FILTER_NON_CONTACTS 0x0001
#define RS_DISTANT_CHAT_CONTACT_PERMISSION_FLAG_FILTER_EVERYBODY 0x0002
struct DistantChatPeerInfo
{
RsGxsId to_id ;
RsGxsId own_id ;
DistantChatPeerId peer_id ; // this is the tunnel id actually
uint32_t status ; // see the values in rsmsgs.h
};
// Identifier for an chat endpoint like
// neighbour peer, distant peer, chatlobby, broadcast
class ChatId
@ -276,8 +289,8 @@ class ChatId
public:
ChatId();
explicit ChatId(RsPeerId id);
explicit ChatId(RsGxsId id);
explicit ChatId(ChatLobbyId id);
explicit ChatId(DistantChatPeerId id);
explicit ChatId(std::string str);
static ChatId makeBroadcastId();
@ -289,13 +302,14 @@ public:
bool isNotSet() const;
bool isPeerId() const;
bool isGxsId() const;
bool isDistantChatId() const;
bool isLobbyId() const;
bool isBroadcast() const;
RsPeerId toPeerId() const;
RsGxsId toGxsId() const;
ChatLobbyId toLobbyId() const;
DistantChatPeerId toDistantChatId() const;
// for the very specific case of transfering a status string
// from the chatservice to the gui,
@ -311,7 +325,7 @@ private:
Type type;
RsPeerId peer_id;
RsGxsId gxs_id;
DistantChatPeerId distant_chat_id;
ChatLobbyId lobby_id;
};
@ -372,15 +386,6 @@ class ChatLobbyInfo
time_t last_activity ; // last recorded activity. Useful for removing dead lobbies.
};
struct DistantChatInviteInfo
{
DistantChatPeerId pid ; // pid to contact the invite and refer to it.
std::string encrypted_radix64_string ; // encrypted radix string used to for the chat link
RsPgpId destination_pgp_id ; // pgp is of the destination of the chat link
time_t time_of_validity ; // time when te invite becomes unusable
uint32_t invite_flags ; // used to keep track of wether signature was ok or not.
};
std::ostream &operator<<(std::ostream &out, const Rs::Msgs::MessageInfo &info);
class RsMsgs;
@ -430,9 +435,9 @@ virtual bool resetMessageStandardTagTypes(Rs::Msgs::MsgTagType& tags) = 0;
/* Private distant messages */
/****************************************/
virtual void enableDistantMessaging(bool b) = 0;
virtual bool distantMessagingEnabled() = 0;
virtual uint32_t getDistantMessagingPermissionFlags()=0 ;
virtual void setDistantMessagingPermissionFlags(uint32_t flags)=0 ;
/****************************************/
/* Chat */
/****************************************/
@ -481,9 +486,12 @@ virtual ChatLobbyId createChatLobby(const std::string& lobby_name,const RsGxsId&
/* Distant chat */
/****************************************/
virtual bool initiateDistantChatConnexion(const RsGxsId& to_pid,const RsGxsId& from_pid,uint32_t& error_code) = 0;
virtual bool getDistantChatStatus(const RsGxsId& gxs_id,uint32_t& status, RsGxsId *from_gxs_id = NULL) = 0;
virtual bool closeDistantChatConnexion(const RsGxsId& pid) = 0;
virtual uint32_t getDistantChatPermissionFlags()=0 ;
virtual bool setDistantChatPermissionFlags(uint32_t flags)=0 ;
virtual bool initiateDistantChatConnexion(const RsGxsId& to_pid,const RsGxsId& from_pid,DistantChatPeerId& pid,uint32_t& error_code) = 0;
virtual bool getDistantChatStatus(const DistantChatPeerId& pid,DistantChatPeerInfo& info)=0;
virtual bool closeDistantChatConnexion(const DistantChatPeerId& pid)=0;
};

View file

@ -35,12 +35,16 @@
#include <retroshare/rsids.h>
/* The Main Interface Class - for information about your Peers
* A peer is another RS instance, means associated with an SSL certificate
* A same GPG person can have multiple peer running with different SSL certs signed by the same GPG key
* Thus a peer have SSL cert details, and also the parent GPG details
*/
* A peer is another RS instance, means associated with an SSL certificate
* A same GPG person can have multiple peer running with different SSL certs signed by the same GPG key
* Thus a peer have SSL cert details, and also the parent GPG details
*/
class RsPeers;
extern RsPeers *rsPeers;
extern RsPeers *rsPeers;
/* TODO: 2015/12/31 As for type safetyness all those constant must be declared as enum!
* C++ now supports typed enum so there is no ambiguity in serialization size
*/
/* Trust Levels. Should be the same values than what is declared in PGPHandler.h */
@ -63,6 +67,14 @@ const uint32_t RS_NETMODE_EXT = 0x0003;
const uint32_t RS_NETMODE_HIDDEN = 0x0004;
const uint32_t RS_NETMODE_UNREACHABLE = 0x0005;
/* Hidden Type */
const uint32_t RS_HIDDEN_TYPE_NONE = 0x0000;
const uint32_t RS_HIDDEN_TYPE_UNKNOWN = 0x0001;
const uint32_t RS_HIDDEN_TYPE_TOR = 0x0002;
const uint32_t RS_HIDDEN_TYPE_I2P = 0x0004;
/* mask to match all valid hidden types */
const uint32_t RS_HIDDEN_TYPE_MASK = RS_HIDDEN_TYPE_I2P | RS_HIDDEN_TYPE_TOR;
/* Visibility */
const uint32_t RS_VS_DISC_OFF = 0x0000;
const uint32_t RS_VS_DISC_MINIMAL = 0x0001;
@ -96,7 +108,8 @@ const uint32_t RS_PEER_CONNECTSTATE_TRYING_UDP = 3;
const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_TCP = 4;
const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_UDP = 5;
const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_TOR = 6;
const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN = 7;
const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_I2P = 7;
const uint32_t RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN = 8;
/* Error codes for certificate cleaning and cert parsing. Numbers should not overlap. */
@ -207,7 +220,7 @@ class RsPeerDetails
RsPgpId issuer;
PGPFingerprintType fpr; /* pgp fingerprint */
std::string authcode; // (cyril) what is this used for ?????
std::string authcode; // TODO: 2015/12/31 (cyril) what is this used for ?????
std::list<RsPgpId> gpgSigners;
uint32_t trustLvl;
@ -222,57 +235,59 @@ class RsPeerDetails
ServicePermissionFlags service_perm_flags ;
/* Network details (only valid if friend) */
uint32_t state;
bool actAsServer;
uint32_t state;
bool actAsServer;
std::string connectAddr ; // current address if connected.
uint16_t connectPort ;
// TODO: 2015/12/31 to take advantage of multiple connection this must be
// replaced by a set of addresses
std::string connectAddr ; // current address if connected.
uint16_t connectPort ;
// Hidden Node details.
bool isHiddenNode;
std::string hiddenNodeAddress;
uint16_t hiddenNodePort;
bool isHiddenNode;
std::string hiddenNodeAddress;
uint16_t hiddenNodePort;
uint32_t hiddenType;
// Filled in for Standard Node.
std::string localAddr;
uint16_t localPort;
std::string extAddr;
uint16_t extPort;
std::string dyndns;
std::list<std::string> ipAddressList;
std::string localAddr;
uint16_t localPort;
std::string extAddr;
uint16_t extPort;
std::string dyndns;
std::list<std::string> ipAddressList;
uint32_t netMode;
uint32_t netMode;
/* vis State */
uint16_t vs_disc;
uint16_t vs_dht;
uint16_t vs_disc;
uint16_t vs_dht;
/* basic stats */
uint32_t lastConnect; /* how long ago */
uint32_t lastUsed; /* how long ago since last used: signature verif, connect attempt, etc */
uint32_t connectState; /* RS_PEER_CONNECTSTATE_... */
std::string connectStateString; /* Additional string like ip address */
uint32_t connectPeriod;
bool foundDHT;
uint32_t lastConnect; /* how long ago */
uint32_t lastUsed; /* how long ago since last used: signature verif, connect attempt, etc */
uint32_t connectState; /* RS_PEER_CONNECTSTATE_... */
std::string connectStateString; /* Additional string like ip address */
uint32_t connectPeriod;
bool foundDHT;
/* have we been denied */
bool wasDeniedConnection;
time_t deniedTS;
bool wasDeniedConnection;
time_t deniedTS;
/* linkType */
uint32_t linkType;
uint32_t linkType;
};
// This class is used to get info about crytographic algorithms used with a
// particular peer.
//
class RsPeerCryptoParams
{
public:
int connexion_state ;
std::string cipher_name ;
int cipher_bits_1 ;
int cipher_bits_2 ;
std::string cipher_version ;
public:
int connexion_state;
std::string cipher_name;
int cipher_bits_1;
int cipher_bits_2;
std::string cipher_version;
};
class RsGroupInfo
@ -282,131 +297,137 @@ public:
std::string id;
std::string name;
uint32_t flag;
uint32_t flag;
std::set<RsPgpId> peerIds;
std::set<RsPgpId> peerIds;
};
std::ostream &operator<<(std::ostream &out, const RsPeerDetails &detail);
/* TODO: 2015/12/31 this class seems foundamental for RetroShare code
* understanding must document it as soon as possible
*/
class RsPeers
{
public:
public:
RsPeers() { return; }
virtual ~RsPeers() { return; }
RsPeers() {}
virtual ~RsPeers() {}
/* Updates ... */
// not implemented
//virtual bool FriendsChanged() = 0;
//virtual bool OthersChanged() = 0;
// TODO: 2015/12/31 is this dead code?
/* Updates ... */
// not implemented
//virtual bool FriendsChanged() = 0;
//virtual bool OthersChanged() = 0;
/* Peer Details (Net & Auth) */
virtual const RsPeerId& getOwnId() = 0;
/* Peer Details (Net & Auth) */
virtual const RsPeerId& getOwnId() = 0;
virtual bool haveSecretKey(const RsPgpId& gpg_id) = 0 ;
virtual bool haveSecretKey(const RsPgpId& gpg_id) = 0 ;
virtual bool getOnlineList(std::list<RsPeerId> &ssl_ids) = 0;
virtual bool getFriendList(std::list<RsPeerId> &ssl_ids) = 0;
virtual bool getPeerCount (unsigned int *pnFriendCount, unsigned int *pnnOnlineCount, bool ssl) = 0;
virtual bool getOnlineList(std::list<RsPeerId> &ssl_ids) = 0;
virtual bool getFriendList(std::list<RsPeerId> &ssl_ids) = 0;
virtual bool getPeerCount (unsigned int *pnFriendCount, unsigned int *pnnOnlineCount, bool ssl) = 0;
virtual bool isOnline(const RsPeerId &ssl_id) = 0;
virtual bool isFriend(const RsPeerId &ssl_id) = 0;
virtual bool isGPGAccepted(const RsPgpId &gpg_id_is_friend) = 0; //
virtual std::string getPeerName(const RsPeerId &ssl_id) = 0;
virtual std::string getGPGName(const RsPgpId& gpg_id) = 0;
virtual bool getPeerDetails(const RsPeerId& ssl_id, RsPeerDetails &d) = 0;
virtual bool getGPGDetails(const RsPgpId& gpg_id, RsPeerDetails &d) = 0;
virtual bool isOnline(const RsPeerId &ssl_id) = 0;
virtual bool isFriend(const RsPeerId &ssl_id) = 0;
virtual bool isGPGAccepted(const RsPgpId &gpg_id_is_friend) = 0;
virtual std::string getPeerName(const RsPeerId &ssl_id) = 0;
virtual std::string getGPGName(const RsPgpId& gpg_id) = 0;
virtual bool getPeerDetails(const RsPeerId& ssl_id, RsPeerDetails &d) = 0;
virtual bool getGPGDetails(const RsPgpId& gpg_id, RsPeerDetails &d) = 0;
/* Using PGP Ids */
virtual const RsPgpId& getGPGOwnId() = 0;
virtual RsPgpId getGPGId(const RsPeerId& sslid) = 0; //return the gpg id of the given ssl id
virtual bool isKeySupported(const RsPgpId& gpg_ids) = 0;
virtual bool getGPGAcceptedList(std::list<RsPgpId> &gpg_ids) = 0;
virtual bool getGPGSignedList(std::list<RsPgpId> &gpg_ids) = 0;//friends that we accpet to connect with but we don't want to sign their gpg key
virtual bool getGPGValidList(std::list<RsPgpId> &gpg_ids) = 0;
virtual bool getGPGAllList(std::list<RsPgpId> &gpg_ids) = 0;
virtual bool getAssociatedSSLIds(const RsPgpId& gpg_id, std::list<RsPeerId>& ids) = 0;
virtual bool gpgSignData(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen) = 0;
/* Using PGP Ids */
virtual const RsPgpId& getGPGOwnId() = 0;
virtual RsPgpId getGPGId(const RsPeerId& sslid) = 0; //return the gpg id of the given ssl id
virtual bool isKeySupported(const RsPgpId& gpg_ids) = 0;
virtual bool getGPGAcceptedList(std::list<RsPgpId> &gpg_ids) = 0;
virtual bool getGPGSignedList(std::list<RsPgpId> &gpg_ids) = 0;//friends that we accpet to connect with but we don't want to sign their gpg key
virtual bool getGPGValidList(std::list<RsPgpId> &gpg_ids) = 0;
virtual bool getGPGAllList(std::list<RsPgpId> &gpg_ids) = 0;
virtual bool getAssociatedSSLIds(const RsPgpId& gpg_id, std::list<RsPeerId>& ids) = 0;
virtual bool gpgSignData(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen) = 0;
/* Add/Remove Friends */
virtual bool addFriend(const RsPeerId &ssl_id, const RsPgpId &gpg_id,ServicePermissionFlags flags = RS_NODE_PERM_DEFAULT) = 0;
virtual bool removeFriend(const RsPgpId& pgp_id) = 0;
virtual bool removeFriendLocation(const RsPeerId& sslId) = 0;
/* Add/Remove Friends */
virtual bool addFriend(const RsPeerId &ssl_id, const RsPgpId &gpg_id,ServicePermissionFlags flags = RS_NODE_PERM_DEFAULT) = 0;
virtual bool removeFriend(const RsPgpId& pgp_id) = 0;
virtual bool removeFriendLocation(const RsPeerId& sslId) = 0;
/* keyring management */
virtual bool removeKeysFromPGPKeyring(const std::set<RsPgpId>& pgp_ids,std::string& backup_file,uint32_t& error_code)=0 ;
/* keyring management */
virtual bool removeKeysFromPGPKeyring(const std::set<RsPgpId>& pgp_ids,std::string& backup_file,uint32_t& error_code) = 0;
/* Network Stuff */
virtual bool connectAttempt(const RsPeerId& ssl_id) = 0;
virtual bool setLocation(const RsPeerId &ssl_id, const std::string &location) = 0;//location is shown in the gui to differentiate ssl certs
/* Network Stuff */
virtual bool connectAttempt(const RsPeerId& ssl_id) = 0;
virtual bool setLocation(const RsPeerId &ssl_id, const std::string &location) = 0; // location is shown in the gui to differentiate ssl certs
virtual bool setHiddenNode(const RsPeerId &id, const std::string &hidden_node_address) = 0;
virtual bool setHiddenNode(const RsPeerId &id, const std::string &address, uint16_t port) = 0;
virtual bool setHiddenNode(const RsPeerId &id, const std::string &hidden_node_address) = 0;
virtual bool setHiddenNode(const RsPeerId &id, const std::string &address, uint16_t port) = 0;
virtual bool setLocalAddress(const RsPeerId &ssl_id, const std::string &addr, uint16_t port) = 0;
virtual bool setExtAddress( const RsPeerId &ssl_id, const std::string &addr, uint16_t port) = 0;
virtual bool setDynDNS(const RsPeerId &id, const std::string &addr) = 0;
virtual bool setNetworkMode(const RsPeerId &ssl_id, uint32_t netMode) = 0;
virtual bool setVisState(const RsPeerId &ssl_id, uint16_t vs_disc, uint16_t vs_dht) = 0;
virtual bool setLocalAddress(const RsPeerId &ssl_id, const std::string &addr, uint16_t port) = 0;
virtual bool setExtAddress( const RsPeerId &ssl_id, const std::string &addr, uint16_t port) = 0;
virtual bool setDynDNS(const RsPeerId &id, const std::string &addr) = 0;
virtual bool setNetworkMode(const RsPeerId &ssl_id, uint32_t netMode) = 0;
virtual bool setVisState(const RsPeerId &ssl_id, uint16_t vs_disc, uint16_t vs_dht) = 0;
virtual bool getProxyServer(std::string &addr, uint16_t &port,uint32_t& status_flags) = 0;
virtual bool setProxyServer(const std::string &addr, const uint16_t port) = 0;
virtual bool getProxyServer(const uint32_t type, std::string &addr, uint16_t &port,uint32_t& status_flags) = 0;
virtual bool setProxyServer(const uint32_t type, const std::string &addr, const uint16_t port) = 0;
virtual void getIPServersList(std::list<std::string>& ip_servers) = 0;
virtual void allowServerIPDetermination(bool) = 0;
virtual bool resetOwnExternalAddressList() = 0;
virtual bool getAllowServerIPDetermination() = 0 ;
virtual void getIPServersList(std::list<std::string>& ip_servers) = 0;
virtual void allowServerIPDetermination(bool) = 0;
virtual bool resetOwnExternalAddressList() = 0;
virtual bool getAllowServerIPDetermination() = 0 ;
/* Auth Stuff */
virtual std::string GetRetroshareInvite(const RsPeerId& ssl_id,bool include_signatures) = 0;
virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) = 0;
virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum) = 0 ;
virtual std::string GetRetroshareInvite(bool include_signatures) = 0;
virtual bool hasExportMinimal() = 0 ;
/* Auth Stuff */
virtual std::string GetRetroshareInvite(const RsPeerId& ssl_id,bool include_signatures) = 0;
virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) = 0;
virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum) = 0;
virtual std::string GetRetroshareInvite(bool include_signatures) = 0;
virtual bool hasExportMinimal() = 0;
// Add keys to the keyring
virtual bool loadCertificateFromString(const std::string& cert, RsPeerId& ssl_id,RsPgpId& pgp_id, std::string& error_string) = 0;
// Add keys to the keyring
virtual bool loadCertificateFromString(const std::string& cert, RsPeerId& ssl_id,RsPgpId& pgp_id, std::string& error_string) = 0;
// Gets the GPG details, but does not add the key to the keyring.
virtual bool loadDetailsFromStringCert(const std::string& certGPG, RsPeerDetails &pd,uint32_t& error_code) = 0;
// Gets the GPG details, but does not add the key to the keyring.
virtual bool loadDetailsFromStringCert(const std::string& certGPG, RsPeerDetails &pd,uint32_t& error_code) = 0;
// Certificate utils
virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert,int& error_code) = 0;
virtual bool saveCertificateToFile(const RsPeerId& id, const std::string &fname) = 0;
virtual std::string saveCertificateToString(const RsPeerId &id) = 0;
// Certificate utils
virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert,int& error_code) = 0;
virtual bool saveCertificateToFile(const RsPeerId& id, const std::string &fname) = 0;
virtual std::string saveCertificateToString(const RsPeerId &id) = 0;
virtual bool signGPGCertificate(const RsPgpId &gpg_id) = 0;
virtual bool trustGPGCertificate(const RsPgpId &gpg_id, uint32_t trustlvl) = 0;
virtual bool signGPGCertificate(const RsPgpId &gpg_id) = 0;
virtual bool trustGPGCertificate(const RsPgpId &gpg_id, uint32_t trustlvl) = 0;
/* Group Stuff */
virtual bool addGroup(RsGroupInfo &groupInfo) = 0;
virtual bool editGroup(const std::string &groupId, RsGroupInfo &groupInfo) = 0;
virtual bool removeGroup(const std::string &groupId) = 0;
virtual bool getGroupInfo(const std::string &groupId, RsGroupInfo &groupInfo) = 0;
virtual bool getGroupInfoList(std::list<RsGroupInfo> &groupInfoList) = 0;
// groupId == "" && assign == false -> remove from all groups
virtual bool assignPeerToGroup(const std::string &groupId, const RsPgpId& peerId, bool assign) = 0;
virtual bool assignPeersToGroup(const std::string &groupId, const std::list<RsPgpId> &peerIds, bool assign) = 0;
/* Group Stuff */
virtual bool addGroup(RsGroupInfo &groupInfo) = 0;
virtual bool editGroup(const std::string &groupId, RsGroupInfo &groupInfo) = 0;
virtual bool removeGroup(const std::string &groupId) = 0;
virtual bool getGroupInfo(const std::string &groupId, RsGroupInfo &groupInfo) = 0;
virtual bool getGroupInfoList(std::list<RsGroupInfo> &groupInfoList) = 0;
// groupId == "" && assign == false -> remove from all groups
virtual bool assignPeerToGroup(const std::string &groupId, const RsPgpId& peerId, bool assign) = 0;
virtual bool assignPeersToGroup(const std::string &groupId, const std::list<RsPgpId> &peerIds, bool assign) = 0;
/* Group sharing permission */
/* Group sharing permission */
// Given
// - the peer id
// - the permission flags of a given hash, e.g. a combination of
// RS_DIR_FLAGS_NETWORK_WIDE_OTHERS, RS_DIR_FLAGS_NETWORK_WIDE_GROUPS, RS_DIR_FLAGS_BROWSABLE_OTHERS and RS_DIR_FLAGS_BROWSABLE_GROUPS
// - the parent groups of the file
//
// ... computes the sharing file permission hint flags set for this peer, that is a combination of
// RS_FILE_HINTS_NETWORK_WIDE and RS_FILE_HINTS_BROWSABLE.
//
virtual FileSearchFlags computePeerPermissionFlags(const RsPeerId& peer_id,FileStorageFlags file_sharing_flags,const std::list<std::string>& file_parent_groups) = 0;
// Given
// - the peer id
// - the permission flags of a given hash, e.g. a combination of
// RS_DIR_FLAGS_NETWORK_WIDE_OTHERS, RS_DIR_FLAGS_NETWORK_WIDE_GROUPS, RS_DIR_FLAGS_BROWSABLE_OTHERS and RS_DIR_FLAGS_BROWSABLE_GROUPS
// - the parent groups of the file
//
// ... computes the sharing file permission hint flags set for this peer, that is a combination of
// RS_FILE_HINTS_NETWORK_WIDE and RS_FILE_HINTS_BROWSABLE.
//
virtual FileSearchFlags computePeerPermissionFlags(
const RsPeerId& peer_id, FileStorageFlags file_sharing_flags,
const std::list<std::string>& file_parent_groups) = 0;
/* Service permission flags */
/* Service permission flags */
virtual ServicePermissionFlags servicePermissionFlags(const RsPgpId& gpg_id) = 0;
virtual ServicePermissionFlags servicePermissionFlags(const RsPeerId& ssl_id) = 0;
virtual void setServicePermissionFlags(const RsPgpId& gpg_id,const ServicePermissionFlags& flags) = 0;
virtual ServicePermissionFlags servicePermissionFlags(const RsPgpId& gpg_id) = 0;
virtual ServicePermissionFlags servicePermissionFlags(const RsPeerId& ssl_id) = 0;
virtual void setServicePermissionFlags(const RsPgpId& gpg_id,const ServicePermissionFlags& flags) = 0;
};
#endif

View file

@ -40,13 +40,16 @@ extern RsPluginHandler *rsPlugins ;
class p3Service ;
class RsServiceControl ;
class RsReputations ;
class RsTurtle ;
class RsGxsTunnelService ;
class RsDht ;
class RsDisc ;
class RsMsgs ;
class RsGxsForums;
class RsGxsChannels;
class RsNotify;
class RsServiceControl;
class p3LinkMgr ;
class MainPage ;
class QIcon ;
@ -73,6 +76,12 @@ class RsGcxs;
class PgpAuxUtils;
class p3Config;
namespace resource_api
{
class ResourceRouter;
class StateTokenServer;
}
// Plugin API version. Not used yet, but will be in the future the
// main value that decides for compatibility.
//
@ -106,6 +115,8 @@ public:
RsUtil::inited_ptr<RsDisc> mDisc;
RsUtil::inited_ptr<RsDht> mDht;
RsUtil::inited_ptr<RsNotify> mNotify;
RsUtil::inited_ptr<RsServiceControl> mServiceControl;
RsUtil::inited_ptr<RsPluginHandler> mPluginHandler;
// gxs
std::string mGxsDir;
@ -116,6 +127,8 @@ public:
RsUtil::inited_ptr<PgpAuxUtils> mPgpAuxUtils;
RsUtil::inited_ptr<RsGxsForums> mGxsForums;
RsUtil::inited_ptr<RsGxsChannels> mGxsChannels;
RsUtil::inited_ptr<RsGxsTunnelService> mGxsTunnels;
RsUtil::inited_ptr<RsReputations> mReputations;
};
class RsPlugin
@ -143,6 +156,12 @@ class RsPlugin
virtual p3Config *p3_config() const { return NULL ; }
virtual uint16_t rs_service_id() const { return 0 ; }
// creates a new resource api handler object. ownership is transferred to the caller.
// the caller should supply a statetokenserver, and keep it valid until destruction
// the plugin should return a entry point name. this is to make the entry point name independent from file names
virtual resource_api::ResourceRouter* new_resource_api_handler(const RsPlugInInterfaces& ifaces, resource_api::StateTokenServer* sts, std::string &entrypoint) const { return 0;}
// Shutdown
virtual void stop() {}

View file

@ -0,0 +1,61 @@
/*
* libretroshare/src/services: rsreputation.h
*
* Services for RetroShare.
*
* Copyright 2015 by Cyril Soler
*
* 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 "csoler@users.sourceforge.net".
*
*/
#pragma once
#include "retroshare/rsids.h"
#include "retroshare/rsgxsifacetypes.h"
class RsReputations
{
public:
static const float REPUTATION_THRESHOLD_ANTI_SPAM = 1.4f ;
static const float REPUTATION_THRESHOLD_DEFAULT = 1.0f ;
// This is the interface file for the reputation system
//
enum Opinion { OPINION_NEGATIVE = 0, OPINION_NEUTRAL = 1, OPINION_POSITIVE = 2 } ;
enum Assessment { ASSESSMENT_BAD = 0, ASSESSMENT_OK = 1 } ;
struct ReputationInfo
{
RsReputations::Opinion mOwnOpinion ;
float mOverallReputationScore ;
float mFriendAverage ;
RsReputations::Assessment mAssessment; // this should help clients in taking decisions
};
virtual bool setOwnOpinion(const RsGxsId& key_id, const Opinion& op) =0;
virtual bool getReputationInfo(const RsGxsId& id,ReputationInfo& info) =0 ;
// This one is a proxy designed to allow fast checking of a GXS id.
// it basically returns true if assessment is not ASSESSMENT_OK
virtual bool isIdentityBanned(const RsGxsId& id) =0;
};
// To access reputations from anywhere
//
extern RsReputations *rsReputations ;

View file

@ -1,35 +0,0 @@
RS_TOP_DIR = ..
##### Define any flags that are needed for this section #######
###############################################################
###############################################################
include $(RS_TOP_DIR)/scripts/config.mk
###############################################################
RSOBJ = rsinit.o \
p3peers.o \
p3rank.o \
p3photo.o \
p3msgs.o \
p3blog.o \
p3discovery.o \
p3face-server.o \
p3face-config.o \
p3face-msgs.o \
rsiface.o \
rstypes.o
#TESTOBJ =
#TESTS =
all: librs tests
#tlvbase_test : tlvbase_test.o
# $(CC) $(CFLAGS) -o tlvbase_test tlvbase_test.o $(OBJ) $(LIBS)
###############################################################
include $(RS_TOP_DIR)/scripts/rules.mk
###############################################################

View file

@ -39,6 +39,7 @@
#include "util/rsthreads.h"
#include "chat/p3chatservice.h"
#include "gxstunnel/p3gxstunnel.h"
#include "services/p3msgservice.h"
#include "services/p3statusservice.h"
@ -160,6 +161,7 @@ class RsServer: public RsControl, public RsTickingThread
p3MsgService *msgSrv;
p3ChatService *chatSrv;
p3StatusService *mStatusSrv;
p3GxsTunnelService *mGxsTunnels;
// This list contains all threaded services. It will be used to shut them down properly.

View file

@ -65,11 +65,11 @@ ChatId::ChatId(RsPeerId id):
peer_id = id;
}
ChatId::ChatId(RsGxsId id):
ChatId::ChatId(DistantChatPeerId id):
lobby_id(0)
{
type = TYPE_PRIVATE_DISTANT;
gxs_id = id;
distant_chat_id = id;
}
ChatId::ChatId(ChatLobbyId id):
@ -93,7 +93,7 @@ ChatId::ChatId(std::string str):
else if(str[0] == 'D')
{
type = TYPE_PRIVATE_DISTANT;
gxs_id == GXSId(str.substr(1));
distant_chat_id == DistantChatPeerId(str.substr(1));
}
else if(str[0] == 'L')
{
@ -143,7 +143,7 @@ std::string ChatId::toStdString() const
else if(type == TYPE_PRIVATE_DISTANT)
{
str += "D";
str += gxs_id.toStdString();
str += distant_chat_id.toStdString();
}
else if(type == TYPE_LOBBY)
{
@ -186,7 +186,7 @@ bool ChatId::operator <(const ChatId& other) const
case TYPE_PRIVATE:
return peer_id < other.peer_id;
case TYPE_PRIVATE_DISTANT:
return gxs_id < other.gxs_id;
return distant_chat_id < other.distant_chat_id;
case TYPE_LOBBY:
return lobby_id < other.lobby_id;
case TYPE_BROADCAST:
@ -210,7 +210,7 @@ bool ChatId::isSameEndpoint(const ChatId &other) const
case TYPE_PRIVATE:
return peer_id == other.peer_id;
case TYPE_PRIVATE_DISTANT:
return gxs_id == other.gxs_id;
return distant_chat_id == other.distant_chat_id;
case TYPE_LOBBY:
return lobby_id == other.lobby_id;
case TYPE_BROADCAST:
@ -229,7 +229,7 @@ bool ChatId::isPeerId() const
{
return type == TYPE_PRIVATE;
}
bool ChatId::isGxsId() const
bool ChatId::isDistantChatId() const
{
return type == TYPE_PRIVATE_DISTANT;
}
@ -251,14 +251,15 @@ RsPeerId ChatId::toPeerId() const
return RsPeerId();
}
}
RsGxsId ChatId::toGxsId() const
DistantChatPeerId ChatId::toDistantChatId() const
{
if(type == TYPE_PRIVATE_DISTANT)
return gxs_id;
return distant_chat_id;
else
{
std::cerr << "ChatId Warning: conversation to RsGxsId requested, but type is different. Current value=\"" << toStdString() << "\"" << std::endl;
return RsGxsId();
std::cerr << "ChatId Warning: conversation to DistantChatPeerId requested, but type is different. Current value=\"" << toStdString() << "\"" << std::endl;
return DistantChatPeerId();
}
}
ChatLobbyId ChatId::toLobbyId() const
@ -278,6 +279,16 @@ bool p3Msgs::getMessageSummaries(std::list<MsgInfoSummary> &msgList)
}
uint32_t p3Msgs::getDistantMessagingPermissionFlags()
{
return mMsgSrv->getDistantMessagingPermissionFlags();
}
void p3Msgs::setDistantMessagingPermissionFlags(uint32_t flags)
{
return mMsgSrv->setDistantMessagingPermissionFlags(flags);
}
bool p3Msgs::getMessage(const std::string &mid, MessageInfo &msg)
{
@ -297,15 +308,6 @@ bool p3Msgs::MessageSend(MessageInfo &info)
return mMsgSrv->MessageSend(info);
}
void p3Msgs::enableDistantMessaging(bool b)
{
mMsgSrv->enableDistantMessaging(b);
}
bool p3Msgs::distantMessagingEnabled()
{
return mMsgSrv->distantMessagingEnabled();
}
bool p3Msgs::SystemMessage(const std::string &title, const std::string &message, uint32_t systemFlag)
{
return mMsgSrv->SystemMessage(title, message, systemFlag);
@ -523,16 +525,24 @@ void p3Msgs::getPendingChatLobbyInvites(std::list<ChatLobbyInvite>& invites)
{
mChatSrv->getPendingChatLobbyInvites(invites) ;
}
bool p3Msgs::initiateDistantChatConnexion(const RsGxsId& to_gxs_id,const RsGxsId& from_gxs_id,uint32_t& error_code)
bool p3Msgs::initiateDistantChatConnexion(const RsGxsId& to_gxs_id,const RsGxsId& from_gxs_id,DistantChatPeerId& pid,uint32_t& error_code)
{
return mChatSrv->initiateDistantChatConnexion(to_gxs_id,from_gxs_id,error_code) ;
return mChatSrv->initiateDistantChatConnexion(to_gxs_id,from_gxs_id,pid,error_code) ;
}
bool p3Msgs::getDistantChatStatus(const RsGxsId &gxs_id,uint32_t &status,RsGxsId *from_gxs_id)
bool p3Msgs::getDistantChatStatus(const DistantChatPeerId& pid,DistantChatPeerInfo& info)
{
return mChatSrv->getDistantChatStatus(gxs_id,status,from_gxs_id) ;
return mChatSrv->getDistantChatStatus(pid,info) ;
}
bool p3Msgs::closeDistantChatConnexion(const RsGxsId& pid)
bool p3Msgs::closeDistantChatConnexion(const DistantChatPeerId &pid)
{
return mChatSrv->closeDistantChatConnexion(pid) ;
}
bool p3Msgs::setDistantChatPermissionFlags(uint32_t flags)
{
return mChatSrv->setDistantChatPermissionFlags(flags) ;
}
uint32_t p3Msgs::getDistantChatPermissionFlags()
{
return mChatSrv->getDistantChatPermissionFlags() ;
}

View file

@ -80,8 +80,8 @@ class p3Msgs: public RsMsgs
virtual bool resetMessageStandardTagTypes(Rs::Msgs::MsgTagType& tags);
virtual void enableDistantMessaging(bool b) ;
virtual bool distantMessagingEnabled() ;
virtual uint32_t getDistantMessagingPermissionFlags() ;
virtual void setDistantMessagingPermissionFlags(uint32_t flags) ;
/*!
* gets avatar from peer, image data in jpeg format
@ -155,10 +155,13 @@ class p3Msgs: public RsMsgs
virtual bool getLobbyAutoSubscribe(const ChatLobbyId& lobby_id);
virtual ChatLobbyId createChatLobby(const std::string& lobby_name,const RsGxsId& lobby_identity,const std::string& lobby_topic,const std::set<RsPeerId>& invited_friends,ChatLobbyFlags privacy_type) ;
virtual bool initiateDistantChatConnexion(const RsGxsId& to_gxs_id,const RsGxsId& from_gxs_id,uint32_t& error_code) ;
virtual bool getDistantChatStatus(const RsGxsId& gxs_id,uint32_t& status, RsGxsId *from_gxs_id=NULL) ;
virtual bool closeDistantChatConnexion(const RsGxsId &pid) ;
virtual bool initiateDistantChatConnexion(const RsGxsId& to_gxs_id, const RsGxsId& from_gxs_id, DistantChatPeerId &pid, uint32_t& error_code) ;
virtual bool getDistantChatStatus(const DistantChatPeerId& gxs_id,DistantChatPeerInfo& info);
virtual bool closeDistantChatConnexion(const DistantChatPeerId &pid) ;
virtual uint32_t getDistantChatPermissionFlags() ;
virtual bool setDistantChatPermissionFlags(uint32_t flags) ;
private:
p3MsgService *mMsgSrv;

View file

@ -243,21 +243,19 @@ bool p3Peers::isFriend(const RsPeerId &ssl_id)
bool p3Peers::haveSecretKey(const RsPgpId& id)
{
return AuthGPG::getAuthGPG()->haveSecretKey(id) ;
return AuthGPG::getAuthGPG()->haveSecretKey(id);
}
/* There are too many dependancies of this function
* to shift it immeidately
*/
bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
{
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::getPeerDetails() called for id : " << id << std::endl;
#endif
// NOW Only for SSL Details.
RsPeerId sOwnId = AuthSSL::getAuthSSL()->OwnId();
peerState ps;
@ -271,27 +269,11 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::getPeerDetails() ERROR not an SSL Id: " << id << std::endl;
#endif
return false ;
return false;
}
// bool res = getGPGDetails(id, d);
//
// d.isOnlyGPGdetail = true;
//
// if(id.length() == 16)
// d.service_perm_flags = mPeerMgr->servicePermissionFlags(id) ;
// else if(id.length() == 32)
// d.service_perm_flags = mPeerMgr->servicePermissionFlags(id) ;
// else
// {
// std::cerr << "p3Peers::getPeerDetails() ERROR not an correct Id: " << id << std::endl;
// d.service_perm_flags = RS_SERVICE_PERM_NONE ;
// }
//
// return res ;
/* get from gpg (first), to fill in the sign and trust details */
/* don't retrun now, we've got fill in the ssl and connection info */
/* don't return now, we've got fill in the ssl and connection info */
getGPGDetails(ps.gpg_id, d);
d.isOnlyGPGdetail = false;
@ -299,7 +281,7 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
d.id = id;
d.location = ps.location;
d.service_perm_flags = mPeerMgr->servicePermissionFlags(ps.gpg_id) ;
d.service_perm_flags = mPeerMgr->servicePermissionFlags(ps.gpg_id);
/* generate */
d.authcode = "AUTHCODE";
@ -313,6 +295,7 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
d.isHiddenNode = true;
d.hiddenNodeAddress = ps.hiddenDomain;
d.hiddenNodePort = ps.hiddenPort;
d.hiddenType = ps.hiddenType;
d.localAddr = sockaddr_storage_iptostring(ps.localaddr);
d.localPort = sockaddr_storage_port(ps.localaddr);
d.extAddr = "hidden";
@ -324,11 +307,30 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
d.isHiddenNode = false;
d.hiddenNodeAddress = "";
d.hiddenNodePort = 0;
d.hiddenType = RS_HIDDEN_TYPE_NONE;
if (sockaddr_storage_isnull(ps.localaddr))
{
d.localAddr = "INVALID_IP";
d.localPort = 0;
}
else
{
d.localAddr = sockaddr_storage_iptostring(ps.localaddr);
d.localPort = sockaddr_storage_port(ps.localaddr);
}
if (sockaddr_storage_isnull(ps.serveraddr))
{
d.extAddr = "INVALID_IP";
d.extPort = 0;
}
else
{
d.extAddr = sockaddr_storage_iptostring(ps.serveraddr);
d.extPort = sockaddr_storage_port(ps.serveraddr);
}
d.localAddr = sockaddr_storage_iptostring(ps.localaddr);
d.localPort = sockaddr_storage_port(ps.localaddr);
d.extAddr = sockaddr_storage_iptostring(ps.serveraddr);
d.extPort = sockaddr_storage_port(ps.serveraddr);
d.dyndns = ps.dyndns;
std::list<pqiIpAddress>::iterator it;
@ -435,20 +437,79 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
}
else if (pcs.state & RS_PEER_S_CONNECTED)
{
if(isProxyAddress(pcs.connectaddr) || mPeerMgr->isHidden())
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TOR;
else if (pcs.connecttype == RS_NET_CONN_TCP_ALL)
{
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TCP;
}
else if (pcs.connecttype == RS_NET_CONN_UDP_ALL)
{
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UDP;
}
else
{
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN;
}
/* peer is connected - determine how and set proper connectState */
if(mPeerMgr->isHidden())
{
uint32_t type;
/* hidden location */
/* use connection direction to determine connection type */
if(pcs.actAsServer)
{
/* incoming connection */
/* use own type to set connectState */
type = mPeerMgr->getHiddenType(AuthSSL::getAuthSSL()->OwnId());
switch (type) {
case RS_HIDDEN_TYPE_TOR:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TOR;
break;
case RS_HIDDEN_TYPE_I2P:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_I2P;
break;
default:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN;
break;
}
}
else
{
/* outgoing connection */
/* use peer hidden type to set connectState */
switch (ps.hiddenType) {
case RS_HIDDEN_TYPE_TOR:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TOR;
break;
case RS_HIDDEN_TYPE_I2P:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_I2P;
break;
default:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN;
break;
}
}
}
else if (ps.hiddenType & RS_HIDDEN_TYPE_MASK)
{
/* hidden peer */
/* use hidden type to set connectState */
switch (ps.hiddenType) {
case RS_HIDDEN_TYPE_TOR:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TOR;
break;
case RS_HIDDEN_TYPE_I2P:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_I2P;
break;
default:
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN;
break;
}
}
else
{
/* peer and we are normal nodes */
/* use normal detection to set connectState */
if (pcs.connecttype == RS_NET_CONN_TCP_ALL)
{
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_TCP;
}
else if (pcs.connecttype == RS_NET_CONN_UDP_ALL)
{
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UDP;
}
else
{
d.connectState = RS_PEER_CONNECTSTATE_CONNECTED_UNKNOWN;
}
}
}
d.wasDeniedConnection = pcs.wasDeniedConnection;
@ -457,13 +518,13 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d)
return true;
}
bool p3Peers::isProxyAddress(const sockaddr_storage& addr)
bool p3Peers::isProxyAddress(const uint32_t type, const sockaddr_storage& addr)
{
uint16_t port ;
std::string string_addr;
uint32_t status ;
uint32_t status ;
if(!getProxyServer(string_addr, port, status))
if(!getProxyServer(type, string_addr, port, status))
return false ;
return sockaddr_storage_iptostring(addr)==string_addr && sockaddr_storage_port(addr)==port ;
@ -923,21 +984,21 @@ bool p3Peers::setVisState(const RsPeerId &id, uint16_t vs_disc, uint16_t vs_dht)
return mPeerMgr->setVisState(id, vs_disc, vs_dht);
}
bool p3Peers::getProxyServer(std::string &addr, uint16_t &port, uint32_t &status)
bool p3Peers::getProxyServer(const uint32_t type, std::string &addr, uint16_t &port, uint32_t &status)
{
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::getProxyServer()" << std::endl;
#endif
struct sockaddr_storage proxy_addr;
mPeerMgr->getProxyServerAddress(proxy_addr);
mPeerMgr->getProxyServerAddress(type, proxy_addr);
addr = sockaddr_storage_iptostring(proxy_addr);
port = sockaddr_storage_port(proxy_addr);
mPeerMgr->getProxyServerStatus(status);
mPeerMgr->getProxyServerStatus(type, status);
return true;
}
bool p3Peers::setProxyServer(const std::string &addr_str, const uint16_t port)
bool p3Peers::setProxyServer(const uint32_t type, const std::string &addr_str, const uint16_t port)
{
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::setProxyServer() " << std::endl;
@ -958,7 +1019,7 @@ bool p3Peers::setProxyServer(const std::string &addr_str, const uint16_t port)
#endif
/********************************** WINDOWS/UNIX SPECIFIC PART *******************/
{
return mPeerMgr->setProxyServerAddress(addr);
return mPeerMgr->setProxyServerAddress(type, addr);
}
else
{
@ -1017,7 +1078,7 @@ bool p3Peers::GetPGPBase64StringAndCheckSum( const RsPgpId& gpg_id,
uint32_t crc = PGPKeyManagement::compute24bitsCRC((unsigned char *)mem_block,mem_block_size) ;
unsigned char tmp[3] = { (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff } ;
unsigned char tmp[3] = { uint8_t((crc >> 16) & 0xff), uint8_t((crc >> 8) & 0xff), uint8_t(crc & 0xff) } ;
Radix64::encode((const char *)tmp,3,gpg_base64_checksum) ;
delete[] mem_block ;
@ -1107,6 +1168,7 @@ bool p3Peers::loadDetailsFromStringCert(const std::string &certstr, RsPeerDetai
{
pd.hiddenNodeAddress = domain;
pd.hiddenNodePort = port;
pd.hiddenType = mPeerMgr->hiddenDomainToHiddenType(domain);
}
}
else
@ -1311,7 +1373,7 @@ RsPeerDetails::RsPeerDetails()
hasSignedMe(false),accept_connection(false),
state(0),localAddr(""),localPort(0),extAddr(""),extPort(0),netMode(0),vs_disc(0), vs_dht(0),
lastConnect(0),connectState(0),connectStateString(""),connectPeriod(0),foundDHT(false),
wasDeniedConnection(false), deniedTS(0)
wasDeniedConnection(false), deniedTS(0), hiddenType(RS_HIDDEN_TYPE_NONE)
{
}

View file

@ -35,118 +35,115 @@ struct sockaddr_storage;
class p3Peers: public RsPeers
{
public:
public:
p3Peers(p3LinkMgr *lm, p3PeerMgr *pm, p3NetMgr *nm);
virtual ~p3Peers() { return; }
p3Peers(p3LinkMgr *lm, p3PeerMgr *pm, p3NetMgr *nm);
virtual ~p3Peers() {}
/* Updates ... */
virtual bool FriendsChanged();
virtual bool OthersChanged();
virtual bool FriendsChanged();
virtual bool OthersChanged();
/* Peer Details (Net & Auth) */
virtual const RsPeerId& getOwnId();
virtual const RsPeerId& getOwnId();
virtual bool haveSecretKey(const RsPgpId& gpg_id) ;
virtual bool haveSecretKey(const RsPgpId& gpg_id) ;
virtual bool getOnlineList(std::list<RsPeerId> &ids);
virtual bool getFriendList(std::list<RsPeerId> &ids);
virtual bool getPeerCount (unsigned int *friendCount, unsigned int *onlineCount, bool ssl);
virtual bool getOnlineList(std::list<RsPeerId> &ids);
virtual bool getFriendList(std::list<RsPeerId> &ids);
//virtual bool getOthersList(std::list<std::string> &ids);
virtual bool getPeerCount (unsigned int *friendCount, unsigned int *onlineCount, bool ssl);
virtual bool isOnline(const RsPeerId &id);
virtual bool isFriend(const RsPeerId &id);
virtual bool isGPGAccepted(const RsPgpId &gpg_id_is_friend);
virtual std::string getGPGName(const RsPgpId &gpg_id);
virtual std::string getPeerName(const RsPeerId& ssl_or_gpg_id);
virtual bool getPeerDetails(const RsPeerId& ssl_or_gpg_id, RsPeerDetails &d);
virtual bool isOnline(const RsPeerId &id);
virtual bool isFriend(const RsPeerId &id);
virtual bool isGPGAccepted(const RsPgpId &gpg_id_is_friend); //
virtual std::string getGPGName(const RsPgpId &gpg_id);
virtual std::string getPeerName(const RsPeerId& ssl_or_gpg_id);
virtual bool getPeerDetails(const RsPeerId& ssl_or_gpg_id, RsPeerDetails &d);
/* Using PGP Ids */
virtual const RsPgpId& getGPGOwnId();
virtual RsPgpId getGPGId(const RsPeerId &ssl_id);
virtual bool isKeySupported(const RsPgpId& ids);
virtual bool getGPGAcceptedList(std::list<RsPgpId> &ids);
virtual bool getGPGSignedList(std::list<RsPgpId> &ids);
virtual bool getGPGValidList(std::list<RsPgpId> &ids);
virtual bool getGPGAllList(std::list<RsPgpId> &ids);
virtual bool getGPGDetails(const RsPgpId &id, RsPeerDetails &d);
virtual bool getAssociatedSSLIds(const RsPgpId& gpg_id, std::list<RsPeerId> &ids);
virtual bool gpgSignData(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen) ;
/* Using PGP Ids */
virtual const RsPgpId& getGPGOwnId();
virtual RsPgpId getGPGId(const RsPeerId &ssl_id);
virtual bool isKeySupported(const RsPgpId& ids);
virtual bool getGPGAcceptedList(std::list<RsPgpId> &ids);
virtual bool getGPGSignedList(std::list<RsPgpId> &ids);
virtual bool getGPGValidList(std::list<RsPgpId> &ids);
virtual bool getGPGAllList(std::list<RsPgpId> &ids);
virtual bool getGPGDetails(const RsPgpId &id, RsPeerDetails &d);
virtual bool getAssociatedSSLIds(const RsPgpId& gpg_id, std::list<RsPeerId> &ids);
virtual bool gpgSignData(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen) ;
/* Add/Remove Friends */
virtual bool addFriend(const RsPeerId &ssl_id, const RsPgpId &gpg_id,ServicePermissionFlags flags = RS_NODE_PERM_DEFAULT);
virtual bool removeFriend(const RsPgpId& gpgid);
virtual bool removeFriendLocation(const RsPeerId& sslId);
virtual bool addFriend(const RsPeerId &ssl_id, const RsPgpId &gpg_id,ServicePermissionFlags flags = RS_NODE_PERM_DEFAULT);
virtual bool removeFriend(const RsPgpId& gpgid);
virtual bool removeFriendLocation(const RsPeerId& sslId);
/* keyring management */
virtual bool removeKeysFromPGPKeyring(const std::set<RsPgpId> &pgp_ids,std::string& backup_file,uint32_t& error_code);
/* keyring management */
virtual bool removeKeysFromPGPKeyring(const std::set<RsPgpId> &pgp_ids,std::string& backup_file,uint32_t& error_code);
/* Network Stuff */
virtual bool connectAttempt(const RsPeerId &id);
virtual bool setLocation(const RsPeerId &ssl_id, const std::string &location);//location is shown in the gui to differentiate ssl certs
virtual bool setHiddenNode(const RsPeerId &id, const std::string &hidden_node_address);
virtual bool setHiddenNode(const RsPeerId &id, const std::string &address, uint16_t port);
virtual bool connectAttempt(const RsPeerId &id);
virtual bool setLocation(const RsPeerId &ssl_id, const std::string &location);//location is shown in the gui to differentiate ssl certs
virtual bool setHiddenNode(const RsPeerId &id, const std::string &hidden_node_address);
virtual bool setHiddenNode(const RsPeerId &id, const std::string &address, uint16_t port);
virtual bool setLocalAddress(const RsPeerId &id, const std::string &addr, uint16_t port);
virtual bool setExtAddress(const RsPeerId &id, const std::string &addr, uint16_t port);
virtual bool setDynDNS(const RsPeerId &id, const std::string &dyndns);
virtual bool setNetworkMode(const RsPeerId &id, uint32_t netMode);
virtual bool setVisState(const RsPeerId &id, uint16_t vs_disc, uint16_t vs_dht);
virtual bool setLocalAddress(const RsPeerId &id, const std::string &addr, uint16_t port);
virtual bool setExtAddress(const RsPeerId &id, const std::string &addr, uint16_t port);
virtual bool setDynDNS(const RsPeerId &id, const std::string &dyndns);
virtual bool setNetworkMode(const RsPeerId &id, uint32_t netMode);
virtual bool setVisState(const RsPeerId &id, uint16_t vs_disc, uint16_t vs_dht);
virtual bool getProxyServer(std::string &addr, uint16_t &port,uint32_t& status);
virtual bool setProxyServer(const std::string &addr, const uint16_t port);
virtual bool isProxyAddress(const sockaddr_storage&);
virtual bool getProxyServer(const uint32_t type, std::string &addr, uint16_t &port,uint32_t& status);
virtual bool setProxyServer(const uint32_t type, const std::string &addr, const uint16_t port);
virtual bool isProxyAddress(const uint32_t type, const sockaddr_storage &addr);
virtual void getIPServersList(std::list<std::string>& ip_servers) ;
virtual void allowServerIPDetermination(bool) ;
virtual bool getAllowServerIPDetermination() ;
virtual bool resetOwnExternalAddressList() ;
virtual void getIPServersList(std::list<std::string>& ip_servers);
virtual void allowServerIPDetermination(bool);
virtual bool getAllowServerIPDetermination();
virtual bool resetOwnExternalAddressList();
/* Auth Stuff */
// Get the invitation (GPG cert + local/ext address + SSL id for the given peer)
virtual std::string GetRetroshareInvite(const RsPeerId& ssl_id,bool include_signatures);
virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) ;
// Get the invitation (GPG cert + local/ext address + SSL id for the given peer)
virtual std::string GetRetroshareInvite(const RsPeerId& ssl_id,bool include_signatures);
virtual std::string getPGPKey(const RsPgpId& pgp_id,bool include_signatures) ;
// same but for own id
virtual std::string GetRetroshareInvite(bool include_signatures);
virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum) ;
// same but for own id
virtual std::string GetRetroshareInvite(bool include_signatures);
virtual bool GetPGPBase64StringAndCheckSum(const RsPgpId& gpg_id,std::string& gpg_base64_string,std::string& gpg_base64_checksum);
virtual bool hasExportMinimal() ;
virtual bool hasExportMinimal();
virtual bool loadCertificateFromString(const std::string& cert, RsPeerId& ssl_id,RsPgpId& pgp_id, std::string& error_string);
virtual bool loadDetailsFromStringCert(const std::string &cert, RsPeerDetails &pd, uint32_t& error_code);
virtual bool loadCertificateFromString(const std::string& cert, RsPeerId& ssl_id,RsPgpId& pgp_id, std::string& error_string);
virtual bool loadDetailsFromStringCert(const std::string &cert, RsPeerDetails &pd, uint32_t& error_code);
virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert,int& error_code);
virtual bool saveCertificateToFile(const RsPeerId &id, const std::string &fname);
virtual std::string saveCertificateToString(const RsPeerId &id);
virtual bool cleanCertificate(const std::string &certstr, std::string &cleanCert,int& error_code);
virtual bool saveCertificateToFile(const RsPeerId &id, const std::string &fname);
virtual std::string saveCertificateToString(const RsPeerId &id);
virtual bool signGPGCertificate(const RsPgpId &id);
virtual bool trustGPGCertificate(const RsPgpId &id, uint32_t trustlvl);
virtual bool signGPGCertificate(const RsPgpId &id);
virtual bool trustGPGCertificate(const RsPgpId &id, uint32_t trustlvl);
/* Group Stuff */
virtual bool addGroup(RsGroupInfo &groupInfo);
virtual bool editGroup(const std::string &groupId, RsGroupInfo &groupInfo);
virtual bool removeGroup(const std::string &groupId);
virtual bool getGroupInfo(const std::string &groupId, RsGroupInfo &groupInfo);
virtual bool getGroupInfoList(std::list<RsGroupInfo> &groupInfoList);
virtual bool assignPeerToGroup(const std::string &groupId, const RsPgpId &peerId, bool assign);
virtual bool assignPeersToGroup(const std::string &groupId, const std::list<RsPgpId>& peerIds, bool assign);
virtual bool addGroup(RsGroupInfo &groupInfo);
virtual bool editGroup(const std::string &groupId, RsGroupInfo &groupInfo);
virtual bool removeGroup(const std::string &groupId);
virtual bool getGroupInfo(const std::string &groupId, RsGroupInfo &groupInfo);
virtual bool getGroupInfoList(std::list<RsGroupInfo> &groupInfoList);
virtual bool assignPeerToGroup(const std::string &groupId, const RsPgpId &peerId, bool assign);
virtual bool assignPeersToGroup(const std::string &groupId, const std::list<RsPgpId>& peerIds, bool assign);
virtual FileSearchFlags computePeerPermissionFlags(const RsPeerId& peer_id,FileStorageFlags share_flags,const std::list<std::string>& parent_groups) ;
virtual FileSearchFlags computePeerPermissionFlags(const RsPeerId& peer_id,FileStorageFlags share_flags,const std::list<std::string>& parent_groups);
// service permission stuff
// service permission stuff
virtual ServicePermissionFlags servicePermissionFlags(const RsPgpId& gpg_id) ;
virtual ServicePermissionFlags servicePermissionFlags(const RsPeerId & ssl_id) ;
virtual void setServicePermissionFlags(const RsPgpId& gpg_id,const ServicePermissionFlags& flags) ;
virtual ServicePermissionFlags servicePermissionFlags(const RsPgpId& gpg_id);
virtual ServicePermissionFlags servicePermissionFlags(const RsPeerId & ssl_id);
virtual void setServicePermissionFlags(const RsPgpId& gpg_id,const ServicePermissionFlags& flags);
private:
private:
p3LinkMgr *mLinkMgr;
p3PeerMgr *mPeerMgr;
p3NetMgr *mNetMgr;
};
#endif

View file

@ -994,7 +994,7 @@ bool RsAccountsDetail::GenerateSSLCertificate(const RsPgpId& pgp_id, const s
return false;
}
int nbits = 2048;
int nbits = 4096;
std::string pgp_name = AuthGPG::getAuthGPG()->getGPGName(pgp_id);

View file

@ -56,6 +56,8 @@
#include <openssl/rand.h>
#include <fcntl.h>
#include <gxstunnel/p3gxstunnel.h>
#define ENABLE_GROUTER
#if (defined(__unix__) || defined(unix)) && !defined(USG)
@ -264,7 +266,7 @@ bool doPortRestrictions = false;
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS
int RsInit::InitRetroShare(int argc, char **argv, bool strictCheck)
int RsInit::InitRetroShare(int argc, char **argv, bool /* strictCheck */)
{
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
#else
@ -365,7 +367,11 @@ int RsInit::InitRetroShare(int argcIgnored, char **argvIgnored, bool strictCheck
#ifdef LOCALNET_TESTING
>> parameter('R',"restrict-port" ,portRestrictions ,"port1-port2","Apply port restriction" ,false)
#endif
#ifdef __APPLE__
>> help('h',"help","Display this Help") ;
#else
>> help() ;
#endif
as.defaultErrorHandling(true) ;
@ -819,6 +825,7 @@ bool RsInit::SetHiddenLocation(const std::string& hiddenaddress, uint16_t port)
RsFiles *rsFiles = NULL;
RsTurtle *rsTurtle = NULL ;
RsReputations *rsReputations = NULL ;
#ifdef ENABLE_GROUTER
RsGRouter *rsGRouter = NULL ;
#endif
@ -847,6 +854,7 @@ RsGRouter *rsGRouter = NULL ;
#endif
#endif
#include "services/p3gxsreputation.h"
#include "services/p3serviceinfo.h"
#include "services/p3heartbeat.h"
#include "services/p3discovery2.h"
@ -1251,7 +1259,7 @@ int RsServer::StartupRetroShare()
std::vector<std::string> plugins_directories ;
#ifndef WINDOWS_SYS
plugins_directories.push_back(std::string(LIB_DIR) + "/retroshare/extensions6/") ;
plugins_directories.push_back(std::string(PLUGIN_DIR)) ;
#endif
std::string extensions_dir = rsAccounts->PathBaseDirectory() + "/extensions6/" ;
plugins_directories.push_back(extensions_dir) ;
@ -1357,6 +1365,11 @@ int RsServer::StartupRetroShare()
mPosted->setNetworkExchangeService(posted_ns) ;
/**** Reputation system ****/
p3GxsReputation *mReputations = new p3GxsReputation(mLinkMgr) ;
rsReputations = mReputations ;
/**** Wiki GXS service ****/
@ -1364,9 +1377,9 @@ int RsServer::StartupRetroShare()
RS_SERVICE_GXS_TYPE_WIKI,
NULL, rsInitConfig->gxs_passwd);
#ifdef RS_USE_WIKI
p3Wiki *mWiki = new p3Wiki(wiki_ds, NULL, mGxsIdService);
// create GXS photo service
// create GXS wiki service
RsGxsNetService* wiki_ns = new RsGxsNetService(
RS_SERVICE_GXS_TYPE_WIKI, wiki_ds, nxsMgr,
mWiki, mWiki->getServiceInfo(),
@ -1374,6 +1387,7 @@ int RsServer::StartupRetroShare()
pgpAuxUtils);
mWiki->setNetworkExchangeService(wiki_ns) ;
#endif
/**** Forum GXS service ****/
@ -1443,7 +1457,9 @@ int RsServer::StartupRetroShare()
pqih->addService(gxsid_ns, true);
pqih->addService(gxscircles_ns, true);
pqih->addService(posted_ns, true);
#ifdef RS_USE_WIKI
pqih->addService(wiki_ns, true);
#endif
pqih->addService(gxsforums_ns, true);
pqih->addService(gxschannels_ns, true);
//pqih->addService(photo_ns, true);
@ -1472,13 +1488,17 @@ int RsServer::StartupRetroShare()
pqih -> addService(tr,true);
pqih -> addService(ftserver,true);
mGxsTunnels = new p3GxsTunnelService(mGxsIdService) ;
mGxsTunnels->connectToTurtleRouter(tr) ;
rsGxsTunnel = mGxsTunnels;
rsDisc = mDisc;
rsMsgs = new p3Msgs(msgSrv, chatSrv);
// connect components to turtle router.
ftserver->connectToTurtleRouter(tr) ;
chatSrv->connectToTurtleRouter(tr) ;
chatSrv->connectToGxsTunnelService(mGxsTunnels) ;
gr->connectToTurtleRouter(tr) ;
#ifdef ENABLE_GROUTER
msgSrv->connectToGlobalRouter(gr) ;
@ -1489,8 +1509,9 @@ int RsServer::StartupRetroShare()
pqih -> addService(mDisc,true);
pqih -> addService(msgSrv,true);
pqih -> addService(chatSrv,true);
pqih -> addService(mStatusSrv,true);
pqih -> addService(mStatusSrv,true);
pqih -> addService(mGxsTunnels,true);
pqih -> addService(mReputations,true);
// set interfaces for plugins
//
@ -1502,6 +1523,8 @@ int RsServer::StartupRetroShare()
interfaces.mDisc = rsDisc;
interfaces.mDht = rsDht;
interfaces.mNotify = mNotify;
interfaces.mServiceControl = serviceCtrl;
interfaces.mPluginHandler = mPluginsManager;
// gxs
interfaces.mGxsDir = currGxsDir;
interfaces.mIdentity = mGxsIdService;
@ -1511,6 +1534,9 @@ int RsServer::StartupRetroShare()
interfaces.mPgpAuxUtils = pgpAuxUtils;
interfaces.mGxsForums = mGxsForums;
interfaces.mGxsChannels = mGxsChannels;
interfaces.mGxsTunnels = mGxsTunnels;
interfaces.mReputations = mReputations;
mPluginsManager->setInterfaces(interfaces);
// now add plugin objects inside the loop:
@ -1598,12 +1624,13 @@ int RsServer::StartupRetroShare()
mConfigMgr->addConfiguration("peers.cfg", mPeerMgr);
mConfigMgr->addConfiguration("general.cfg", mGeneralConfig);
mConfigMgr->addConfiguration("msgs.cfg", msgSrv);
mConfigMgr->addConfiguration("chat.cfg", chatSrv);
mConfigMgr->addConfiguration("p3History.cfg", mHistoryMgr);
mConfigMgr->addConfiguration("p3Status.cfg", mStatusSrv);
mConfigMgr->addConfiguration("turtle.cfg", tr);
mConfigMgr->addConfiguration("banlist.cfg", mBanList);
mConfigMgr->addConfiguration("servicecontrol.cfg", serviceCtrl);
mConfigMgr->addConfiguration("chat.cfg", chatSrv);
mConfigMgr->addConfiguration("p3History.cfg", mHistoryMgr);
mConfigMgr->addConfiguration("p3Status.cfg", mStatusSrv);
mConfigMgr->addConfiguration("turtle.cfg", tr);
mConfigMgr->addConfiguration("banlist.cfg", mBanList);
mConfigMgr->addConfiguration("servicecontrol.cfg", serviceCtrl);
mConfigMgr->addConfiguration("reputations.cfg", mReputations);
#ifdef ENABLE_GROUTER
mConfigMgr->addConfiguration("grouter.cfg", gr);
#endif
@ -1619,7 +1646,9 @@ int RsServer::StartupRetroShare()
mConfigMgr->addConfiguration("gxschannels.cfg", gxschannels_ns);
mConfigMgr->addConfiguration("gxscircles.cfg", gxscircles_ns);
mConfigMgr->addConfiguration("posted.cfg", posted_ns);
#ifdef RS_USE_WIKI
mConfigMgr->addConfiguration("wiki.cfg", wiki_ns);
#endif
//mConfigMgr->addConfiguration("photo.cfg", photo_ns);
//mConfigMgr->addConfiguration("wire.cfg", wire_ns);
#endif
@ -1728,7 +1757,9 @@ int RsServer::StartupRetroShare()
// Must Set the GXS pointers before starting threads.
rsIdentity = mGxsIdService;
rsGxsCircles = mGxsCircles;
#if RS_USE_WIKI
rsWiki = mWiki;
#endif
rsPosted = mPosted;
rsGxsForums = mGxsForums;
rsGxsChannels = mGxsChannels;
@ -1739,7 +1770,9 @@ int RsServer::StartupRetroShare()
startServiceThread(mGxsIdService);
startServiceThread(mGxsCircles);
startServiceThread(mPosted);
#if RS_USE_WIKI
startServiceThread(mWiki);
#endif
startServiceThread(mGxsForums);
startServiceThread(mGxsChannels);
@ -1750,7 +1783,9 @@ int RsServer::StartupRetroShare()
startServiceThread(gxsid_ns);
startServiceThread(gxscircles_ns);
startServiceThread(posted_ns);
#if RS_USE_WIKI
startServiceThread(wiki_ns);
#endif
startServiceThread(gxsforums_ns);
startServiceThread(gxschannels_ns);

View file

@ -4,7 +4,6 @@
#include "rsloginhandler.h"
#include "util/rsdir.h"
#include "rsaccounts.h"
#if defined(UBUNTU) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <gnome-keyring-1/gnome-keyring.h>
@ -118,6 +117,7 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd
std::cerr << "RsTryAutoLogin()" << std::endl;
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef __HAIKU__
#ifndef WINDOWS_SYS /* UNIX */
#if defined(UBUNTU) || defined(__FreeBSD__) || defined(__OpenBSD__)
@ -145,8 +145,9 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd
void *passwordData = NULL;
UInt32 passwordLength = 0;
const char *userId = ssl_id.c_str();
UInt32 uidLength = strlen(ssl_id.c_str());
std::string idtemp = ssl_id.toStdString();
const char *userId = idtemp.c_str();
UInt32 uidLength = strlen(userId);
SecKeychainItemRef itemRef = NULL;
OSStatus status = SecKeychainFindGenericPassword (
@ -348,6 +349,7 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd
LocalFree(DataOut.pbData);
return isDecrypt;
#endif
#endif
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
@ -360,6 +362,7 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s
std::cerr << "RsStoreAutoLogin()" << std::endl;
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef __HAIKU__
#ifndef WINDOWS_SYS /* UNIX */
#if defined(UBUNTU) || defined(__FreeBSD__) || defined(__OpenBSD__)
if(GNOME_KEYRING_RESULT_OK == gnome_keyring_store_password_sync(&my_schema, NULL, (gchar*)("RetroShare password for SSL Id "+ssl_id.toStdString()).c_str(),(gchar*)ssl_passwd.c_str(),"RetroShare SSL Id",ssl_id.toStdString().c_str(),NULL))
@ -381,8 +384,9 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s
const void *password = ssl_passwd.c_str();
UInt32 passwordLength = strlen(ssl_passwd.c_str());
const char *userid = ssl_id.c_str();
UInt32 uidLength = strlen(ssl_id.c_str());
std::string idtemp = ssl_id.toStdString();
const char *userid = idtemp.c_str();
UInt32 uidLength = strlen(userid);
OSStatus status = SecKeychainAddGenericPassword (
NULL, // default keychain
@ -517,6 +521,7 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
return false;
#endif
}
bool RsLoginHandler::clearAutoLogin(const RsPeerId& ssl_id)
@ -540,8 +545,9 @@ bool RsLoginHandler::clearAutoLogin(const RsPeerId& ssl_id)
void *passwordData = NULL;
UInt32 passwordLength = 0;
const char *userId = ssl_id.c_str();
UInt32 uidLength = strlen(ssl_id.c_str());
std::string idtemp = ssl_id.toStdString();
const char *userId = idtemp.c_str();
UInt32 uidLength = strlen(userId);
SecKeychainItemRef itemRef = NULL;
OSStatus status = SecKeychainFindGenericPassword (

View file

@ -101,6 +101,8 @@ uint32_t RsGxsIdLocalInfoItem::serial_size()
uint32_t s = 8 ; // header
s += 4 ; // number of items
s += mTimeStamps.size() * (RsGxsId::SIZE_IN_BYTES + 8) ;
s += 4 ; // number of contacts
s += mContacts.size() * RsGxsId::SIZE_IN_BYTES ;
return s;
}
@ -176,6 +178,11 @@ bool RsGxsIdLocalInfoItem::serialise(void *data, uint32_t& size)
ok &= it->first.serialise(data,tlvsize,offset) ;
ok &= setRawTimeT(data,tlvsize,&offset,it->second) ;
}
ok &= setRawUInt32(data, tlvsize, &offset, mContacts.size()) ;
for(std::set<RsGxsId>::const_iterator it(mContacts.begin());it!=mContacts.end();++it)
ok &= (*it).serialise(data,tlvsize,offset) ;
if(offset != tlvsize)
{
#ifdef GXSID_DEBUG
@ -386,6 +393,19 @@ RsGxsIdLocalInfoItem *RsGxsIdSerialiser::deserialise_GxsIdLocalInfoItem(void *da
item->mTimeStamps[gxsid] = TS ;
}
if (offset < rssize) // backward compatibility, making that section optional.
{
ok &= getRawUInt32(data, rssize, &offset, &n) ;
RsGxsId gxsid ;
for(int i=0;ok && i<n;++i)
{
ok &= gxsid.deserialise(data,rssize,offset) ;
item->mContacts.insert(gxsid) ;
}
}
if (offset != rssize)
{
#ifdef GXSID_DEBUG

Some files were not shown because too many files have changed in this diff Show more