implementation of p3disc with new gpg key management

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2026 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
joss17 2010-01-13 21:33:54 +00:00
parent 442577d9fc
commit 115a80045e
8 changed files with 217 additions and 526 deletions

View File

@ -158,6 +158,7 @@ virtual bool getSSLChildListOfGPGId(std::string gpg_id, std::list<std::string
/* Add/Remove Friends */
virtual bool addFriend(std::string ssl_id, std::string gpg_id) = 0;
virtual bool addDummyFriend(std::string gpg_id) = 0; //we want to add a empty ssl friend for this gpg id
virtual bool isDummyFriend(std::string ssl_id) = 0;
virtual bool removeFriend(std::string id) = 0;
/* Network Stuff */

View File

@ -523,12 +523,15 @@ bool p3Peers::getSSLChildListOfGPGId(std::string gpg_id, std::list<std::string>
mConnMgr->getFriendList(friendsIds);
peerConnectState pcs;
for (std::list<std::string>::iterator it = friendsIds.begin(); it != friendsIds.end(); it++) {
mConnMgr->getFriendNetStatus(*it, pcs);
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::getSSLChildListOfGPGId() iterating over friends status :id : " << gpg_id;
std::cerr << "p3Peers::getSSLChildListOfGPGId() iterating over friends id : " << gpg_id;
std::cerr << std::endl;
#endif
if (mConnMgr->getFriendNetStatus(*it, pcs) && pcs.gpg_id == gpg_id) {
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::getSSLChildListOfGPGId() adding ssl id : " << pcs.id;
std::cerr << std::endl;
#endif
ids.push_back(pcs.id);
}
}
@ -572,7 +575,7 @@ std::string p3Peers::getGPGId(std::string sslid_or_gpgid)
if (mConnMgr->getFriendNetStatus(sslid_or_gpgid, pcs)) {
return pcs.gpg_id;
} else {
if ( AuthGPG::getAuthGPG()->isGPGValid(sslid_or_gpgid)) {
if ( AuthGPG::getAuthGPG()->isGPGId(sslid_or_gpgid)) {
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::getPGPId() given id is already an gpg id : " << sslid_or_gpgid;
std::cerr << std::endl;
@ -615,6 +618,21 @@ bool p3Peers::addDummyFriend(std::string gpg_id)
}
}
bool p3Peers::isDummyFriend(std::string ssl_id) {
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::isDummyFriend() called" << std::endl;
#endif
RsPeerDetails details;
bool ret = false;
if (getPeerDetails(ssl_id, details)) {
ret = (details.id == ("dummy" + details.gpg_id));
}
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::isDummyFriend() return : " << ret << std::endl;
#endif
return ret;
}
bool p3Peers::removeFriend(std::string id)
{
#ifdef P3PEERS_DEBUG

View File

@ -67,6 +67,7 @@ virtual bool getSSLChildListOfGPGId(std::string gpg_id, std::list<std::string
/* Add/Remove Friends */
virtual bool addFriend(std::string ssl_id, std::string gpg_id);
virtual bool addDummyFriend(std::string gpg_id); //we want to add a empty ssl friend for this gpg id
virtual bool isDummyFriend(std::string ssl_id);
virtual bool removeFriend(std::string ssl_id);
/* Network Stuff */

View File

@ -364,13 +364,9 @@ RsDiscReply::~RsDiscReply()
void RsDiscReply::clear()
{
memset(&currentladdr, 0, sizeof(currentladdr));
memset(&currentsaddr, 0, sizeof(currentladdr));
contact_tf = 0;
discFlags = 0;
aboutId.clear();
certGPG.clear();
ipAddressList.clear();
rsPeerList.clear();
}
std::ostream &RsDiscReply::print(std::ostream &out, uint16_t indent)
@ -378,21 +374,6 @@ std::ostream &RsDiscReply::print(std::ostream &out, uint16_t indent)
printRsItemBase(out, "RsDiscReply", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Local Address: " << inet_ntoa(currentladdr.sin_addr);
out << " Port: " << ntohs(currentladdr.sin_port) << std::endl;
printIndent(out, int_Indent);
out << "Server Address: " << inet_ntoa(currentsaddr.sin_addr);
out << " Port: " << ntohs(currentsaddr.sin_port) << std::endl;
printIndent(out, int_Indent);
out << "Contact TimeFrame: " << contact_tf;
out << std::endl;
printIndent(out, int_Indent);
out << "DiscFlags: " << discFlags << std::endl;
printIndent(out, int_Indent);
out << "AboutId: " << aboutId << std::endl;
@ -400,13 +381,10 @@ std::ostream &RsDiscReply::print(std::ostream &out, uint16_t indent)
out << "certGPG: " << certGPG << std::endl;
printIndent(out, int_Indent);
out << "IpAddressListSize: " << ipAddressList.size() << std::endl;
printIndent(out, int_Indent);
out << "RsDiscOwnItem::print() IpAddressList: " << std::endl;
for (std::list<IpAddressTimed>::iterator ipListIt = ipAddressList.begin(); ipListIt!=(ipAddressList.end()); ipListIt++) {
printIndent(out, int_Indent);
out << inet_ntoa(ipListIt->ipAddr.sin_addr) << ":" << ntohs(ipListIt->ipAddr.sin_port) << " seenTime : " << ipListIt->seenTime << std::endl;
out << "RsDiscReply::print() RsPeerNetItem list : " << std::endl;
for (std::list<RsPeerNetItem>::iterator pitemIt = rsPeerList.begin(); pitemIt!=(rsPeerList.end()); pitemIt++) {
printIndent(out, int_Indent);
pitemIt->print(std::cerr, indent);
}
printRsItemEnd(out, "RsDiscReply", indent);
@ -417,18 +395,14 @@ std::ostream &RsDiscReply::print(std::ostream &out, uint16_t indent)
uint32_t RsDiscSerialiser::sizeReply(RsDiscReply *item)
{
uint32_t s = 8; /* header */
s += GetTlvIpAddrPortV4Size(); /* laddr */
s += GetTlvIpAddrPortV4Size(); /* saddr */
s += 2; /* connect_tr */
s += 4; /* discFlags */
s += GetTlvStringSize(item->aboutId);
s += GetTlvStringSize(item->certGPG);
s += 4; /* ipaddress list size */
//add the size of the ip list
int ipListSize = item->ipAddressList.size();
s += ipListSize * GetTlvIpAddrPortV4Size();
s += ipListSize * 8; //size of an uint64
RsPeerConfigSerialiser *rss = new RsPeerConfigSerialiser();
for (std::list<RsPeerNetItem>::iterator it = item->rsPeerList.begin(); it != item->rsPeerList.end(); it++){
RsPeerNetItem pitem = *it;
s += rss->size(&pitem);
}
return s;
}
@ -457,26 +431,23 @@ bool RsDiscSerialiser::serialiseReply(RsDiscReply *item, void *data, uint32_
offset += 8;
/* add mandatory parts first */
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_LOCAL, &(item->currentladdr));
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(item->currentsaddr));
ok &= setRawUInt16(data, tlvsize, &offset, item->contact_tf);
ok &= setRawUInt32(data, tlvsize, &offset, item->discFlags);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->aboutId);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_CERT_GPG, item->certGPG);
ok &= setRawUInt32(data, tlvsize, &offset, item->ipAddressList.size());
//store the ip list
std::list<IpAddressTimed>::iterator ipListIt;
for (ipListIt = item->ipAddressList.begin(); ipListIt!=(item->ipAddressList.end()); ipListIt++) {
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(ipListIt->ipAddr));
ok &= setRawUInt64(data, tlvsize, &offset, ipListIt->seenTime);
RsPeerConfigSerialiser *rss = new RsPeerConfigSerialiser();
std::list<RsPeerNetItem>::iterator pitemIt;
for (pitemIt = item->rsPeerList.begin(); pitemIt!=(item->rsPeerList.end()); pitemIt++) {
void *pitemData = malloc(16000);
uint32_t size = 16000;
RsPeerNetItem pitem = *pitemIt;
ok &= rss->serialise(&pitem, pitemData, &size);
memcpy(data + offset, pitemData, size);
free(pitemData);
offset += size;
}
if (offset != tlvsize)
{
if (offset != tlvsize) {
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDiscSerialiser::serialiseReply() Size Error! " << std::endl;
@ -526,41 +497,22 @@ RsDiscReply *RsDiscSerialiser::deserialiseReply(void *data, uint32_t *pktsize)
offset += 8;
/* get mandatory parts first */
ok &= GetTlvIpAddrPortV4(data, rssize, &offset,
TLV_TYPE_IPV4_LOCAL, &(item->currentladdr));
ok &= GetTlvIpAddrPortV4(data, rssize, &offset,
TLV_TYPE_IPV4_REMOTE, &(item->currentsaddr));
ok &= getRawUInt16(data, rssize, &offset, &(item->contact_tf));
ok &= getRawUInt32(data, rssize, &offset, &(item->discFlags));
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->aboutId);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_CERT_GPG, item->certGPG);
ok &= GetTlvString(data, rssize, &offset,
TLV_TYPE_STR_PEERID, item->aboutId);
ok &= GetTlvString(data, rssize, &offset,
TLV_TYPE_STR_CERT_GPG, item->certGPG);
uint32_t listSize;
ok &= getRawUInt32(data, rssize, &offset, &listSize);
//get the ip adress list
int count = 0;
std::list<IpAddressTimed> ipTimedList;
while (offset < rssize && count < (int)listSize) {
count++;
IpAddressTimed ipTimed;
sockaddr_clear(&ipTimed.ipAddr);
ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &ipTimed.ipAddr);
if (!ok) { break; }
uint64_t time = 0;
ok &= getRawUInt64(data, rssize, &offset, &time);
if (!ok) { break; }
ipTimed.seenTime = time;
ipTimedList.push_back(ipTimed);
//get the peernet address list
RsPeerConfigSerialiser *rss = new RsPeerConfigSerialiser();
std::list<RsPeerNetItem> rsPeerNetItemList;
while (offset < rssize) {
void *peerNetdata = malloc(16000);
uint32_t peerNetSize = (*pktsize) - offset;
memcpy(peerNetdata, data + offset, peerNetSize);
RsPeerNetItem *rsPeerNetItem = (RsPeerNetItem*)rss->deserialise(peerNetdata, &peerNetSize);
offset += peerNetSize;
item->rsPeerList.push_back(*rsPeerNetItem);
}
item->ipAddressList = ipTimedList;
if (offset != rssize)
{
if (offset != rssize) {
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDiscSerialiser::deserialiseReply() offset != rssize" << std::endl;
#endif
@ -569,8 +521,7 @@ RsDiscReply *RsDiscSerialiser::deserialiseReply(void *data, uint32_t *pktsize)
return NULL;
}
if (!ok)
{
if (!ok) {
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDiscSerialiser::deserialiseReply() ok = false" << std::endl;
#endif
@ -593,7 +544,7 @@ RsDiscIssuer::~RsDiscIssuer()
void RsDiscIssuer::clear()
{
issuerCert = "";
certGPG.clear();
}
std::ostream &RsDiscIssuer::print(std::ostream &out, uint16_t indent)
@ -602,7 +553,7 @@ std::ostream &RsDiscIssuer::print(std::ostream &out, uint16_t indent)
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Cert String: " << issuerCert << std::endl;
out << "GPG Cert String: " << certGPG << std::endl;
printRsItemEnd(out, "RsDiscIssuer", indent);
return out;
@ -613,7 +564,7 @@ uint32_t RsDiscSerialiser::sizeIssuer(RsDiscIssuer *item)
{
uint32_t s = 8; /* header */
s += 4; /* size in RawString() */
s += item->issuerCert.length();
s += item->certGPG.length();
return s;
}
@ -642,7 +593,7 @@ bool RsDiscSerialiser::serialiseIssuer(RsDiscIssuer *item, void *data, uint3
offset += 8;
/* add mandatory parts first */
ok &= setRawString(data, tlvsize, &offset, item->issuerCert);
ok &= setRawString(data, tlvsize, &offset, item->certGPG);
if (offset != tlvsize)
{
@ -697,7 +648,7 @@ RsDiscIssuer *RsDiscSerialiser::deserialiseIssuer(void *data, uint32_t *pktsize)
offset += 8;
/* get mandatory parts first */
ok &= getRawString(data, rssize, &offset, item->issuerCert);
ok &= getRawString(data, rssize, &offset, item->certGPG);
if (offset != rssize)
{

View File

@ -86,18 +86,18 @@ virtual void clear();
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
//use for transmitting ip address list
std::list<IpAddressTimed> ipAddressList;
std::list<RsPeerNetItem> rsPeerList;
//use for transmitting my own adress list
struct sockaddr_in currentladdr;
struct sockaddr_in currentsaddr;
// struct sockaddr_in currentladdr;
// struct sockaddr_in currentsaddr;
// time frame of recent connections.
uint16_t contact_tf;
//uint16_t contact_tf;
// flags...
uint32_t discFlags;
//uint32_t discFlags;
std::string aboutId;
std::string aboutId;
std::string certGPG;
};
@ -114,7 +114,7 @@ virtual ~RsDiscIssuer();
virtual void clear();
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
std::string issuerCert;
std::string certGPG;
};
class RsDiscVersion: public RsDiscItem

View File

@ -181,32 +181,27 @@ int p3disc::handleIncoming()
// if discovery reply then respond if haven't already.
if (NULL != (dri = dynamic_cast<RsDiscReply *> (item)))
{
if (NULL != (dri = dynamic_cast<RsDiscReply *> (item))) {
recvPeerFriendMsg(dri);
recvPeerDetails(dri);
nhandled++;
}
#ifdef RS_USE_PGPSSL
else if (NULL != (dii = dynamic_cast<RsDiscIssuer *> (item)))
{
else if (NULL != (dii = dynamic_cast<RsDiscIssuer *> (item))) {
recvPeerIssuerMsg(dii);
//recvPeerIssuerMsg(dii);
nhandled++;
}
#endif
else if (NULL != (dvi = dynamic_cast<RsDiscVersion *> (item)))
{
else if (NULL != (dvi = dynamic_cast<RsDiscVersion *> (item))) {
recvPeerVersionMsg(dvi);
nhandled++;
}
else if (NULL != (dio = dynamic_cast<RsDiscOwnItem *> (item))) /* Ping */
{
recvPeerOwnMsg(dio);
else if (NULL != (dio = dynamic_cast<RsDiscOwnItem *> (item))) /* Ping */ {
//recvPeerOwnMsg(dio);
nhandled++;
}
else if (NULL != (dta = dynamic_cast<RsDiscHeartbeat *> (item)))
{
else if (NULL != (dta = dynamic_cast<RsDiscHeartbeat *> (item))) {
recvHeartbeatMsg(dta);
return 1;
}
@ -225,26 +220,20 @@ void p3disc::statusChange(const std::list<pqipeer> &plist)
std::cerr << std::endl;
#endif
/* get a list of all online peers */
std::list<std::string> onlineIds;
mConnMgr->getOnlineList(onlineIds);
std::list<pqipeer>::const_iterator pit;
/* if any have switched to 'connected' then we notify */
for(pit = plist.begin(); pit != plist.end(); pit++)
{
if ((pit->state & RS_PEER_S_FRIEND) &&
(pit->actions & RS_PEER_CONNECTED))
{
for(pit = plist.begin(); pit != plist.end(); pit++) {
if ((pit->state & RS_PEER_S_FRIEND) && (pit->actions & RS_PEER_CONNECTED)) {
/* send their own details to them. Usefull for ext ip address detection */
sendPeerDetails(pit->id, pit->id);
/* send our details to them */
sendOwnDetails(pit->id);
// sendPeerDetails(pit->id, pit->id);
// /* send our details to them */
sendOwnVersion(pit->id);
sendAllInfoToPeer(pit->id);
}
}
}
void p3disc::respondToPeer(std::string id)
void p3disc::sendAllInfoToPeer(std::string id)
{
/* get a peer lists */
@ -253,171 +242,38 @@ void p3disc::respondToPeer(std::string id)
std::cerr << std::endl;
#endif
std::list<std::string> friendIds;
std::list<std::string> onlineIds;
std::list<std::string>::iterator it;
std::list<std::string> friendIds;
std::list<std::string>::iterator friendIdsIt;
std::set<std::string> gpgIds;;
mConnMgr->getFriendList(friendIds);
mConnMgr->getOnlineList(onlineIds);
/* Check that they have DISC on */
{
/* get details */
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(id, detail))
{
/* major error! */
return;
}
if (detail.visState & RS_VIS_STATE_NODISC)
{
/* don't have DISC enabled */
return;
}
}
rsPeers->getFriendList(friendIds);
/* send them a list of all friend's details */
for(it = friendIds.begin(); it != friendIds.end(); it++)
{
for(friendIdsIt = friendIds.begin(); friendIdsIt != friendIds.end(); friendIdsIt++) {
/* get details */
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(*it, detail))
{
if (!mConnMgr->getFriendNetStatus(*friendIdsIt, detail)) {
/* major error! */
continue;
}
if (!(detail.visState & RS_VIS_STATE_NODISC))
{
/* send issuer certs ... only do this for friends at initial connections,
no need to do with onlineId list.
*/
sendPeerIssuer(id, *it);
sendPeerDetails(id, *it); /* (dest (to), source (cert)) */
if (!(detail.visState & RS_VIS_STATE_NODISC)) {
gpgIds.insert(detail.gpg_id);
}
}
/* send their details to all online peers */
for(it = onlineIds.begin(); it != onlineIds.end(); it++)
{
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(*it, detail))
{
/* major error! */
continue;
}
if (!(detail.visState & RS_VIS_STATE_NODISC))
{
sendPeerDetails(*it, id); /* (dest (to), source (cert)) */
}
}
}
/*************************************************************************************/
/* Output Network Msgs */
/*************************************************************************************/
void p3disc::sendOwnDetails(std::string to)
{
/* setup:
* IP local / external
* availability (TCP LOCAL / EXT, UDP ...)
*/
// Then send message.
{
#ifdef P3DISC_DEBUG
std::ostringstream out;
out << "p3disc::sendOwnDetails()";
out << "Constructing a RsDiscItem Message!" << std::endl;
out << "Sending to: " << to;
std::cerr << out.str() << std::endl;
#endif
}
// Construct a message
RsDiscOwnItem *di = new RsDiscOwnItem();
/* components:
* laddr
* saddr
* contact_tf
* discFlags
*/
peerConnectState detail;
if (!mConnMgr->getOwnNetStatus(detail))
{
/* major error! */
return;
}
// Fill the message
di -> PeerId(to);
di -> laddr = detail.currentlocaladdr;
di -> saddr = detail.currentserveraddr;
#ifdef P3DISC_DEBUG
std::cerr << "p3disc::sendOwnDetails() detail.currentlocaladdr.sin_addr : " << inet_ntoa(detail.currentlocaladdr.sin_addr) << ":" << ntohs(detail.currentlocaladdr.sin_port) << std::endl;
std::cerr << "p3disc::sendOwnDetails() detail.currentserveraddr.sin_addr : " << inet_ntoa(detail.currentserveraddr.sin_addr) << ":" << ntohs(detail.currentlocaladdr.sin_port) << std::endl;
#endif
di -> ipAddressList.clear();
std::list<IpAddressTimed> ipAddressListTemp = detail.getIpAddressList();
for ( std::list<IpAddressTimed>::iterator ipListIt = ipAddressListTemp.begin(); ipListIt!= ipAddressListTemp.end(); ipListIt++) {
IpAddressTimed ipAddress;
ipAddress.ipAddr = ipListIt->ipAddr;
ipAddress.seenTime = ipListIt->seenTime;
di -> ipAddressList.push_back(ipAddress);
}
di -> contact_tf = 0;
//add own info
gpgIds.insert(rsPeers->getGPGOwnId());
/* construct disc flags */
di -> discFlags = 0;
if (!(detail.visState & RS_VIS_STATE_NODISC))
{
di->discFlags |= P3DISC_FLAGS_USE_DISC;
}
if (!(detail.visState & RS_VIS_STATE_NODHT))
{
di->discFlags |= P3DISC_FLAGS_USE_DHT;
}
if ((detail.netMode & RS_NET_MODE_EXT) ||
(detail.netMode & RS_NET_MODE_UPNP))
{
di->discFlags |= P3DISC_FLAGS_EXTERNAL_ADDR;
}
else if (detail.netMode & RS_NET_MODE_UDP)
{
di->discFlags |= P3DISC_FLAGS_STABLE_UDP;
}
// set flag - request for version
di->discFlags |= P3DISC_FLAGS_ASK_VERSION;
di->discFlags |= P3DISC_FLAGS_OWN_DETAILS;
#ifdef P3DISC_DEBUG
di->print(std::cerr, 5);
#endif
/* send msg */
sendItem(di);
//send details for each gpg Ids
std::set<std::string>::iterator gpgIdsIt;
for (gpgIdsIt = gpgIds.begin(); gpgIdsIt != gpgIds.end(); gpgIdsIt++) {
sendPeerDetails(id, *gpgIdsIt);
}
}
/* (dest (to), source (cert)) */
void p3disc::sendPeerDetails(std::string to, std::string about)
{
/* setup:
* Certificate.
* IP local / external
* availability ...
* last connect (0) if online.
*/
/* send it off */
void p3disc::sendPeerDetails(std::string to, std::string about) {
{
#ifdef P3DISC_DEBUG
std::ostringstream out;
@ -428,13 +284,7 @@ void p3disc::sendPeerDetails(std::string to, std::string about)
#endif
}
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(about, detail))
{
/* major error! */
return;
}
about = rsPeers->getGPGId(about);
// Construct a message
RsDiscReply *di = new RsDiscReply();
@ -443,58 +293,52 @@ void p3disc::sendPeerDetails(std::string to, std::string about)
// Set Target as input cert.
di -> PeerId(to);
di -> aboutId = about;
di -> certGPG = AuthGPG::getAuthGPG()->SaveCertificateToString(about);
// set the ip addresse list.
di -> ipAddressList.clear();
std::list<IpAddressTimed> ipAddressListTemp = detail.getIpAddressList();
for ( std::list<IpAddressTimed>::iterator ipListIt = ipAddressListTemp.begin(); ipListIt!= ipAddressListTemp.end(); ipListIt++) {
IpAddressTimed ipAddress;
ipAddress.ipAddr = ipListIt->ipAddr;
ipAddress.seenTime = ipListIt->seenTime;
di -> ipAddressList.push_back(ipAddress);
std::list<std::string> sslChilds;
rsPeers->getSSLChildListOfGPGId(about, sslChilds);
for (std::list<std::string>::iterator sslChildIt = sslChilds.begin(); sslChildIt != sslChilds.end(); sslChildIt++) {
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(*sslChildIt, detail)) {
continue;
}
RsPeerNetItem *rsPeerNetItem = new RsPeerNetItem();
rsPeerNetItem->clear();
rsPeerNetItem->pid = detail.id;
rsPeerNetItem->gpg_id = detail.gpg_id;
rsPeerNetItem->location = detail.location;
rsPeerNetItem->netMode = detail.netMode;
rsPeerNetItem->visState = detail.visState;
rsPeerNetItem->lastContact = detail.lastcontact;
rsPeerNetItem->currentlocaladdr = detail.currentlocaladdr;
rsPeerNetItem->currentremoteaddr = detail.currentserveraddr;
rsPeerNetItem->ipAddressList = detail.getIpAddressList();
di->rsPeerList.push_back(*rsPeerNetItem);
}
di -> currentladdr = detail.currentlocaladdr;
di -> currentsaddr = detail.currentserveraddr;
if (detail.state & RS_PEER_S_CONNECTED)
{
di -> contact_tf = 0;
}
else
{
di -> contact_tf = convertTDeltaToTRange(time(NULL) - detail.lastcontact);
}
//send own details
if (about == rsPeers->getGPGOwnId()) {
peerConnectState detail;
if (mConnMgr->getOwnNetStatus(detail)) {
RsPeerNetItem *rsPeerNetItem = new RsPeerNetItem();
rsPeerNetItem->clear();
rsPeerNetItem->pid = detail.id;
rsPeerNetItem->gpg_id = detail.gpg_id;
rsPeerNetItem->location = detail.location;
rsPeerNetItem->netMode = detail.netMode;
rsPeerNetItem->visState = detail.visState;
rsPeerNetItem->lastContact = time(NULL);
rsPeerNetItem->currentlocaladdr = detail.currentlocaladdr;
rsPeerNetItem->currentremoteaddr = detail.currentserveraddr;
rsPeerNetItem->ipAddressList = detail.getIpAddressList();
/* construct disc flags */
di->discFlags = 0;
/* NOTE we should not be sending packet if NODISC is set....
* checked elsewhere... so don't check.
*/
di->discFlags |= P3DISC_FLAGS_USE_DISC;
if (!(detail.visState & RS_VIS_STATE_NODHT))
{
di->discFlags |= P3DISC_FLAGS_USE_DHT;
}
if (detail.netMode & RS_NET_MODE_EXT)
{
di->discFlags |= P3DISC_FLAGS_EXTERNAL_ADDR;
}
else if (detail.netMode & RS_NET_MODE_UDP)
{
di->discFlags |= P3DISC_FLAGS_STABLE_UDP;
}
if (detail.state & RS_PEER_S_CONNECTED)
{
di->discFlags |= P3DISC_FLAGS_PEER_ONLINE;
}
uint32_t certLen = 0;
di -> certGPG = AuthGPG::getAuthGPG()->SaveCertificateToString(about);
di->rsPeerList.push_back(*rsPeerNetItem);
}
}
// Send off message
#ifdef P3DISC_DEBUG
@ -502,53 +346,6 @@ void p3disc::sendPeerDetails(std::string to, std::string about)
#endif
sendItem(di);
#ifdef P3DISC_DEBUG
std::cerr << "Sent DI Message" << std::endl;
#endif
}
/* (dest (to), source (cert)) */
void p3disc::sendPeerIssuer(std::string to, std::string about)
{
/* this is just a straight certificate (normally pgp).
* but can get quite big (>100K) so will use new packet type.
*/
/* send it off */
{
#ifdef P3DISC_DEBUG
std::ostringstream out;
out << "p3disc::sendPeerIssuer()";
out << " Sending details of: " << about;
out << " to: " << to << std::endl;
std::cerr << out.str() << std::endl;
#endif
}
std::string aboutIssuerId = rsPeers->getGPGId(about);
if (aboutIssuerId == "")
{
/* major error! */
return;
}
// Construct a message
RsDiscIssuer *di = new RsDiscIssuer();
// Fill the message
// Set Target as input cert.
di -> PeerId(to);
di -> issuerCert = AuthGPG::getAuthGPG()->SaveCertificateToString(aboutIssuerId);
#ifdef P3DISC_DEBUG
std::cerr << "Saved certificate to string in RsDiscIssuer. " << std::endl ;
#endif
// Send off message
sendItem(di);
#ifdef P3DISC_DEBUG
std::cerr << "Sent DI Message" << std::endl;
#endif
@ -599,168 +396,95 @@ void p3disc::sendHeartbeat(std::string to)
#endif
}
/*************************************************************************************/
/* Input Network Msgs */
/*************************************************************************************/
void p3disc::recvPeerOwnMsg(RsDiscOwnItem *item)
{
#ifdef P3DISC_DEBUG
std::cerr << "p3disc::recvPeerOwnMsg() From: " << item->PeerId() << std::endl;
#endif
/* tells us their exact address (mConnectMgr can ignore if it looks wrong) */
uint32_t type = 0;
uint32_t flags = 0;
/* translate flags */
if (item->discFlags & P3DISC_FLAGS_USE_DISC)
{
flags |= RS_NET_FLAGS_USE_DISC;
}
if (item->discFlags & P3DISC_FLAGS_USE_DHT)
{
flags |= RS_NET_FLAGS_USE_DHT;
}
if (item->discFlags & P3DISC_FLAGS_PEER_ONLINE)
{
flags |= RS_NET_FLAGS_ONLINE;
}
/* generate type */
type = RS_NET_CONN_TCP_LOCAL;
if (item->discFlags & P3DISC_FLAGS_EXTERNAL_ADDR)
{
type |= RS_NET_CONN_TCP_EXTERNAL;
flags |= RS_NET_FLAGS_EXTERNAL_ADDR;
}
if (item->discFlags & P3DISC_FLAGS_STABLE_UDP)
{
type |= RS_NET_CONN_UDP;
flags |= RS_NET_FLAGS_STABLE_UDP;
}
mConnMgr->peerStatus(item->PeerId(), item->laddr, item->saddr, item->ipAddressList,
type, flags, RS_CB_PERSON);
/* also add as potential stun buddy */
std::string hashid1 = RsUtil::HashId(item->PeerId(), false);
mConnMgr->stunStatus(hashid1, item->saddr, type,
RS_STUN_ONLINE | RS_STUN_FRIEND);
/* now reply with all details */
respondToPeer(item->PeerId());
/*sending rs versio if i was asked*/
if (item->discFlags & P3DISC_FLAGS_ASK_VERSION)
{
sendOwnVersion(item->PeerId());
}
addDiscoveryData(item->PeerId(), item->PeerId(), item->laddr, item->saddr, item->discFlags, time(NULL));
/* cleanup (handled by caller) */
}
void p3disc::recvPeerFriendMsg(RsDiscReply *item)
void p3disc::recvPeerDetails(RsDiscReply *item)
{
#ifdef P3DISC_DEBUG
std::cerr << "p3disc::recvPeerFriendMsg() From: " << item->PeerId();
std::cerr << " About " << item->aboutId;
std::cerr << std::endl;
std::cerr << "p3disc::recvPeerFriendMsg() From: " << item->PeerId() << " About " << item->aboutId << std::endl;
#endif
std::string certGpgId;
AuthGPG::getAuthGPG()->LoadCertificateFromString(item->certGPG, certGpgId);
if (item->aboutId != certGpgId) {
#ifdef P3DISC_DEBUG
std::cerr << "p3disc::recvPeerFriendMsg() Error : about id is not the same as gpg id." << std::endl;
#endif
return;
}
/* tells us their exact address (mConnectMgr can ignore if it looks wrong) */
uint32_t type = 0;
uint32_t flags = 0;
for (std::list<RsPeerNetItem>::iterator pitem = item->rsPeerList.begin(); pitem != item->rsPeerList.end(); pitem++) {
//don't update dummy friends
if (rsPeers->isDummyFriend(pitem->pid)) {
continue;
}
/* translate flags */
if (item->discFlags & P3DISC_FLAGS_USE_DISC) flags |= RS_NET_FLAGS_USE_DISC;
if (item->discFlags & P3DISC_FLAGS_USE_DHT) flags |= RS_NET_FLAGS_USE_DHT;
if (item->discFlags & P3DISC_FLAGS_PEER_ONLINE) flags |= RS_NET_FLAGS_ONLINE;
if (item->discFlags & P3DISC_FLAGS_PEER_TRUSTS_ME)
{
std::cerr << " Found a peer that trust me: " << item->aboutId << " (" << rsPeers->getPeerName(item->aboutId) << ")" << std::endl ;
flags |= RS_NET_FLAGS_TRUSTS_ME;
}
addDiscoveryData(item->PeerId(), pitem->pid, pitem->currentlocaladdr, pitem->currentremoteaddr, 0, time(NULL));
/* generate type */
type = RS_NET_CONN_TCP_LOCAL;
if (item->discFlags & P3DISC_FLAGS_EXTERNAL_ADDR)
{
type |= RS_NET_CONN_TCP_EXTERNAL;
flags |= RS_NET_FLAGS_EXTERNAL_ADDR;
}
if (item->discFlags & P3DISC_FLAGS_STABLE_UDP)
{
type |= RS_NET_CONN_UDP;
flags |= RS_NET_FLAGS_STABLE_UDP;
}
/* only valid certs, and not ourselves */
if ((item->aboutId != mConnMgr->getOwnId()))
{
mConnMgr->peerStatus(item->aboutId, item->currentladdr, item->currentsaddr, item->ipAddressList, type, flags, RS_CB_DISC);
std::string hashid1 = RsUtil::HashId(item->aboutId, false);
mConnMgr->stunStatus(hashid1, item->currentsaddr, type, RS_STUN_FRIEND_OF_FRIEND);
}
/* send Own Ip list to connect manager. It will extract the external ip address from it */
if (item->aboutId == mConnMgr->getOwnId())
{
//setAddressList might also set our own external address
mConnMgr->setAddressList(mConnMgr->getOwnId(), item->ipAddressList);
if (item->currentsaddr.sin_addr.s_addr != 0 && item->currentsaddr.sin_port != 0 &&
item->currentsaddr.sin_addr.s_addr != 1 && item->currentsaddr.sin_port != 1 &&
std::string(inet_ntoa(item->currentsaddr.sin_addr)) != "1.1.1.1" &&
(!isLoopbackNet(&item->currentsaddr.sin_addr)) &&
(!isPrivateNet(&item->currentsaddr.sin_addr))
#ifdef P3DISC_DEBUG
std::cerr << "pp3disc::recvPeerFriendMsg() Peer Config Item:" << std::endl;
pitem->print(std::cerr, 10);
std::cerr << std::endl;
#endif
if (pitem->pid != rsPeers->getOwnId()) {
mConnMgr->addFriend(pitem->pid, pitem->gpg_id, pitem->netMode, pitem->visState, 0);
RsPeerDetails storedDetails;
rsPeers->getPeerDetails(pitem->pid, storedDetails);
if ( (!(storedDetails.state & RS_PEER_CONNECTED) && storedDetails.lastConnect < (pitem->lastContact - 10000))
|| item->PeerId() == pitem->pid) { //update if it's fresh info or if it's from the peer itself
//their info is fresher than ours (there is a 10000 seconds margin), update ours
mConnMgr->setLocalAddress(pitem->pid, pitem->currentlocaladdr);
mConnMgr->setExtAddress(pitem->pid, pitem->currentremoteaddr);
mConnMgr->setVisState(pitem->pid, pitem->visState);
mConnMgr->setNetworkMode(pitem->pid, pitem->netMode);
if (storedDetails.location == "") {
mConnMgr->setLocation(pitem->pid, pitem->location);
}
}
} else {
if (pitem->currentremoteaddr.sin_addr.s_addr != 0 && pitem->currentremoteaddr.sin_port != 0 &&
pitem->currentremoteaddr.sin_addr.s_addr != 1 && pitem->currentremoteaddr.sin_port != 1 &&
std::string(inet_ntoa(pitem->currentremoteaddr.sin_addr)) != "1.1.1.1" &&
(!isLoopbackNet(&pitem->currentremoteaddr.sin_addr)) &&
(!isPrivateNet(&pitem->currentremoteaddr.sin_addr))
) {
//the current server address given by the peer looks nice, let's use it for our own ext address if needed
sockaddr_in tempAddr;
if (!mConnMgr->getExtFinderExtAddress(tempAddr) && !mConnMgr->getUpnpExtAddress(tempAddr)) {
//don't change the port, just the ip
item->currentsaddr.sin_port = mConnMgr->ownState.currentserveraddr.sin_port;
mConnMgr->setExtAddress(mConnMgr->getOwnId(), item->currentsaddr);
pitem->currentremoteaddr.sin_port = mConnMgr->ownState.currentserveraddr.sin_port;
mConnMgr->setExtAddress(mConnMgr->getOwnId(), pitem->currentremoteaddr);
}
}
}
//allways update address list
mConnMgr->setAddressList(pitem->pid, pitem->ipAddressList);
}
addDiscoveryData(item->PeerId(), item->aboutId, item->currentladdr, item->currentsaddr, item->discFlags, time(NULL));
rsicontrol->getNotify().notifyListChange(NOTIFY_LIST_NEIGHBOURS, NOTIFY_TYPE_MOD);
/* cleanup (handled by caller) */
/* cleanup (handled by caller) */
}
void p3disc::recvPeerIssuerMsg(RsDiscIssuer *item)
{
#ifdef P3DISC_DEBUG
std::cerr << "p3disc::recvPeerIssuerMsg() From: " << item->PeerId();
std::cerr << std::endl;
std::cerr << "p3disc::recvPeerIssuerMsg() Cert: " << item->issuerCert;
std::cerr << std::endl;
#endif
/* tells us their exact address (mConnectMgr can ignore if it looks wrong) */
/* load certificate */
std::string gpgId;
bool loaded = AuthGPG::getAuthGPG()->LoadCertificateFromString(item->issuerCert, gpgId);
/* cleanup (handled by caller) */
return;
}
//void p3disc::recvPeerIssuerMsg(RsDiscIssuer *item)
//{
//
//#ifdef P3DISC_DEBUG
// std::cerr << "p3disc::recvPeerIssuerMsg() From: " << item->PeerId();
// std::cerr << std::endl;
// std::cerr << "p3disc::recvPeerIssuerMsg() Cert: " << item->issuerCert;
// std::cerr << std::endl;
//#endif
//
// /* tells us their exact address (mConnectMgr can ignore if it looks wrong) */
//
// /* load certificate */
// std::string gpgId;
// bool loaded = AuthGPG::getAuthGPG()->LoadCertificateFromString(item->issuerCert, gpgId);
//
// /* cleanup (handled by caller) */
//
// return;
//}
void p3disc::recvPeerVersionMsg(RsDiscVersion *item)
{
@ -790,8 +514,6 @@ void p3disc::recvHeartbeatMsg(RsDiscHeartbeat *item)
/*************************************************************************************/
/* Storing Network Graph */
/*************************************************************************************/
int p3disc::addDiscoveryData(std::string fromId, std::string aboutId, struct sockaddr_in laddr, struct sockaddr_in raddr, uint32_t flags, time_t ts)
{
RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/
@ -853,7 +575,6 @@ int p3disc::addDiscoveryData(std::string fromId, std::string aboutId, struct soc
/*************************************************************************************/
/* Extracting Network Graph Details */
/*************************************************************************************/
bool p3disc::potentialproxies(std::string id, std::list<std::string> &proxyIds)
{
/* find id -> and extract the neighbour_of ids */

View File

@ -94,20 +94,20 @@ void getversions(std::map<std::string, std::string> &versions);
private:
void respondToPeer(std::string id);
void sendAllInfoToPeer(std::string id);
/* Network Output */
void sendOwnDetails(std::string to);
//void sendOwnDetails(std::string to);
void sendOwnVersion(std::string to);
void sendPeerDetails(std::string to, std::string about);
void sendPeerIssuer(std::string to, std::string about);
//void sendPeerIssuer(std::string to, std::string about);
void sendHeartbeat(std::string to);
/* Network Input */
int handleIncoming();
void recvPeerOwnMsg(RsDiscOwnItem *item);
void recvPeerFriendMsg(RsDiscReply *item);
void recvPeerIssuerMsg(RsDiscIssuer *item);
//void recvPeerOwnMsg(RsDiscOwnItem *item);
void recvPeerDetails(RsDiscReply *item);
//void recvPeerIssuerMsg(RsDiscIssuer *item);
void recvPeerVersionMsg(RsDiscVersion *item);
void recvHeartbeatMsg(RsDiscHeartbeat *item);

View File

@ -256,7 +256,6 @@ TextPage::TextPage(QWidget *parent)
"certificate into the box below" )) ;
friendCertEdit = new QTextEdit;
registerField("aaabbb", friendCertEdit, "plainText");//, "textChanged");
//=== add all widgets to one layout
textPageLayout = new QVBoxLayout();