From d2f9f1c4f15c15ced4f565bc77d281620ada6387 Mon Sep 17 00:00:00 2001 From: joss17 Date: Wed, 11 Nov 2009 16:49:11 +0000 Subject: [PATCH] final code, working on local machine git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1811 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/dht/dht_bootstrap.cc | 2 +- libretroshare/src/dht/dht_check_peers.cc | 2 +- libretroshare/src/pqi/p3connmgr.cc | 46 +++++++++++++++----- libretroshare/src/pqi/p3connmgr.h | 3 +- libretroshare/src/pqi/p3dhtmgr.cc | 3 +- libretroshare/src/pqi/pqimonitor.cc | 2 +- libretroshare/src/pqi/pqimonitor.h | 5 ++- libretroshare/src/serialiser/rsdiscitems.cc | 48 +++++++++++++++++++++ libretroshare/src/serialiser/rsdiscitems.h | 6 ++- libretroshare/src/services/p3disc.cc | 5 ++- 10 files changed, 102 insertions(+), 20 deletions(-) diff --git a/libretroshare/src/dht/dht_bootstrap.cc b/libretroshare/src/dht/dht_bootstrap.cc index 476338a5d..4e66d11d3 100644 --- a/libretroshare/src/dht/dht_bootstrap.cc +++ b/libretroshare/src/dht/dht_bootstrap.cc @@ -126,7 +126,7 @@ void addPeer(std::string id) } virtual void peerStatus(std::string id, - struct sockaddr_in laddr, struct sockaddr_in raddr, + struct sockaddr_in laddr, struct sockaddr_in raddr, std::list ipAddressList, uint32_t type, uint32_t mode, uint32_t source) { } diff --git a/libretroshare/src/dht/dht_check_peers.cc b/libretroshare/src/dht/dht_check_peers.cc index 8db0ac51d..079d40c3b 100644 --- a/libretroshare/src/dht/dht_check_peers.cc +++ b/libretroshare/src/dht/dht_check_peers.cc @@ -125,7 +125,7 @@ void addPeer(std::string id) } virtual void peerStatus(std::string id, - struct sockaddr_in laddr, struct sockaddr_in raddr, + struct sockaddr_in laddr, struct sockaddr_in raddr, std::list ipAddressList, uint32_t type, uint32_t mode, uint32_t source) { diff --git a/libretroshare/src/pqi/p3connmgr.cc b/libretroshare/src/pqi/p3connmgr.cc index 386abb5fa..44cf67dfd 100644 --- a/libretroshare/src/pqi/p3connmgr.cc +++ b/libretroshare/src/pqi/p3connmgr.cc @@ -1528,12 +1528,12 @@ bool p3ConnectMgr::connectAttempt(std::string id, struct sockaddr_in &addr, it->second.connAddrs.pop_front(); -#ifdef CONN_DEBUG addr = it->second.currentConnAddrAttempt.addr; delay = it->second.currentConnAddrAttempt.delay; period = it->second.currentConnAddrAttempt.period; type = it->second.currentConnAddrAttempt.type; +#ifdef CONN_DEBUG std::cerr << "p3ConnectMgr::connectAttempt() Success: "; std::cerr << " id: " << id; std::cerr << std::endl; @@ -1668,7 +1668,7 @@ bool p3ConnectMgr::connectResult(std::string id, bool success, uint32_t flags) void p3ConnectMgr::peerStatus(std::string id, - struct sockaddr_in laddr, struct sockaddr_in raddr, + struct sockaddr_in laddr, struct sockaddr_in raddr, std::list ipDiscAddressList, uint32_t type, uint32_t flags, uint32_t source) { std::map::iterator it; @@ -1698,6 +1698,7 @@ void p3ConnectMgr::peerStatus(std::string id, std::cerr << " flags: " << flags; std::cerr << " source: " << source; std::cerr << std::endl; + peerConnectState::printIpAddressList(ipDiscAddressList); #endif { /* Log */ @@ -1772,6 +1773,10 @@ void p3ConnectMgr::peerStatus(std::string id, it->second.source = RS_CB_DISC; it->second.disc = details; + it->second.currentlocaladdr = laddr; + it->second.currentserveraddr = raddr; + it->second.updateIpAddressList(ipDiscAddressList); + if (flags & RS_NET_FLAGS_ONLINE) { it->second.actions |= RS_PEER_ONLINE; @@ -1796,6 +1801,7 @@ void p3ConnectMgr::peerStatus(std::string id, it->second.currentlocaladdr = laddr; it->second.currentserveraddr = raddr; + it->second.updateIpAddressList(ipDiscAddressList); it->second.state |= RS_PEER_S_ONLINE; it->second.lastavailable = now; @@ -3771,6 +3777,11 @@ void peerConnectState::updateIpAddressList(std::list ipTimedList } void peerConnectState::updateIpAddressList(IpAddressTimed ipTimed) { //purge old addresses to keep a small list +#ifdef CONN_DEBUG + std::cerr << "peerConnectState::updateIpAdressList() ip " << inet_ntoa(ipTimed.ipAddr.sin_addr); + std::cerr << ":" << ntohs(ipTimed.ipAddr.sin_port); + std::cerr << std::endl; +#endif if (ipTimed.ipAddr.sin_addr.s_addr == 0) { #ifdef CONN_DEBUG std::cerr << "peerConnectState::updateIpAdressList() ip parameter is 0.0.0.0, do nothing." << std::endl; @@ -3781,24 +3792,25 @@ void peerConnectState::updateIpAddressList(IpAddressTimed ipTimed) { //purge old //check if the ip list contains the current remote address of the connected peer bool found = false; std::list::iterator ipListIt; - for (ipListIt = ipAddressList.begin(); ipListIt!=(ipAddressList.end()); ipListIt++) { - if (ipListIt->ipAddr.sin_addr.s_addr == ipTimed.ipAddr.sin_addr.s_addr && ipListIt->ipAddr.sin_port == ipTimed.ipAddr.sin_port) { + for (ipListIt = ipAddressList.begin(); ipListIt!=(ipAddressList.end()) && !found; ipListIt++) { + if (is_same_address(*ipListIt, ipTimed)) { #ifdef CONN_DEBUG - std::cerr << "peerConnectState::updateIpAdressList() ip found in the list. Update seen time for : "; - std::cerr << inet_ntoa(ipListIt->ipAddr.sin_addr); - std::cerr << ":" << ntohs(ipListIt->ipAddr.sin_port); + std::cerr << "peerConnectState::updateIpAdressList() ip found in the list."; std::cerr << std::endl; #endif found = true; //update the seen time if (ipListIt->seenTime < ipTimed.seenTime) { ipListIt->seenTime = ipTimed.seenTime; + #ifdef CONN_DEBUG + std::cerr << "peerConnectState::updateIpAdressList() Update seen time to : " << ipTimed.seenTime << std::endl; + std::cerr << std::endl; + #endif } - break; } } - if (!found && (ipTimed.ipAddr.sin_addr.s_addr != 0)) { + if (!found) { //add the current addresses to the ip list #ifdef CONN_DEBUG std::cerr << "peerConnectState::updateIpAdressList() adding to the ip list the current remote addr : " << id << " address : "; @@ -3811,7 +3823,7 @@ void peerConnectState::updateIpAddressList(IpAddressTimed ipTimed) { //purge old } -void peerConnectState::printIpAddressList() { //purge old addresses to keep a small list +void peerConnectState::printIpAddressList() { #ifdef CONN_DEBUG std::cerr << "peerConnectState::printIpAdressList() current ip list for the peer : " << id; std::cerr << ", size : " << ipAddressList.size(); @@ -3826,3 +3838,17 @@ void peerConnectState::printIpAddressList() { //purge old addresses to keep a sm } } + +void peerConnectState::printIpAddressList(std::list ipTimedList) { +#ifdef CONN_DEBUG + std::cerr << "peerConnectState::printIpAdressList()" << std::endl; +#endif + for (std::list::iterator ipListIt = ipTimedList.begin(); ipListIt!=(ipTimedList.end()); ipListIt++) { +#ifdef CONN_DEBUG + std::cerr << inet_ntoa(ipListIt->ipAddr.sin_addr); + std::cerr << ":" << ntohs(ipListIt->ipAddr.sin_port); + std::cerr << " seenTime : " << ipListIt->seenTime << std::endl; +#endif + } + +} diff --git a/libretroshare/src/pqi/p3connmgr.h b/libretroshare/src/pqi/p3connmgr.h index 84a8652a9..d6a1ce729 100644 --- a/libretroshare/src/pqi/p3connmgr.h +++ b/libretroshare/src/pqi/p3connmgr.h @@ -149,6 +149,7 @@ class peerConnectState static bool compare_seen_time (IpAddressTimed first, IpAddressTimed second); static bool is_same_address (IpAddressTimed first, IpAddressTimed second); void printIpAddressList(); + static void printIpAddressList(std::list ipTimedList); //used to store current ip (for config and connection management) struct sockaddr_in currentlocaladdr; /* Mandatory */ @@ -255,7 +256,7 @@ void removeMonitor(pqiMonitor *mon); /******* overloaded from pqiConnectCb *************/ virtual void peerStatus(std::string id, - struct sockaddr_in laddr, struct sockaddr_in raddr, + struct sockaddr_in laddr, struct sockaddr_in raddr, std::list ipAddressList, uint32_t type, uint32_t flags, uint32_t source); virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr, uint32_t source); diff --git a/libretroshare/src/pqi/p3dhtmgr.cc b/libretroshare/src/pqi/p3dhtmgr.cc index d29cea438..c49550993 100644 --- a/libretroshare/src/pqi/p3dhtmgr.cc +++ b/libretroshare/src/pqi/p3dhtmgr.cc @@ -1756,7 +1756,8 @@ bool p3DhtMgr::dhtResultSearch(std::string idhash, if (doCb) { - mConnCb->peerStatus(ent.id, ent.laddr, ent.raddr, + std::list ipAddressList; + mConnCb->peerStatus(ent.id, ent.laddr, ent.raddr, ipAddressList, ent.type, 0, RS_CB_DHT); } diff --git a/libretroshare/src/pqi/pqimonitor.cc b/libretroshare/src/pqi/pqimonitor.cc index adefe766d..6f9d46087 100644 --- a/libretroshare/src/pqi/pqimonitor.cc +++ b/libretroshare/src/pqi/pqimonitor.cc @@ -44,7 +44,7 @@ pqiConnectCbDummy::~pqiConnectCbDummy() } void pqiConnectCbDummy::peerStatus(std::string id, - struct sockaddr_in laddr, struct sockaddr_in raddr, + struct sockaddr_in laddr, struct sockaddr_in raddr, std::list ipAddressList, uint32_t type, uint32_t mode, uint32_t source) { std::cerr << "pqiConnectCbDummy::peerStatus()"; diff --git a/libretroshare/src/pqi/pqimonitor.h b/libretroshare/src/pqi/pqimonitor.h index 86fd091ff..8e296457c 100644 --- a/libretroshare/src/pqi/pqimonitor.h +++ b/libretroshare/src/pqi/pqimonitor.h @@ -34,6 +34,7 @@ #include #include #include +#include "serialiser/rsconfigitems.h" /************** Define Type/Mode/Source ***************/ @@ -108,7 +109,7 @@ class pqiConnectCb public: virtual ~pqiConnectCb() { return; } virtual void peerStatus(std::string id, - struct sockaddr_in laddr, struct sockaddr_in raddr, + struct sockaddr_in laddr, struct sockaddr_in raddr, std::list ipAddressList, uint32_t type, uint32_t flags, uint32_t source) = 0; virtual void peerConnectRequest(std::string id, @@ -125,7 +126,7 @@ class pqiConnectCbDummy: public pqiConnectCb pqiConnectCbDummy(); virtual ~pqiConnectCbDummy(); virtual void peerStatus(std::string id, - struct sockaddr_in laddr, struct sockaddr_in raddr, + struct sockaddr_in laddr, struct sockaddr_in raddr, std::list ipAddressList, uint32_t type, uint32_t mode, uint32_t source); virtual void peerConnectRequest(std::string id, diff --git a/libretroshare/src/serialiser/rsdiscitems.cc b/libretroshare/src/serialiser/rsdiscitems.cc index b127e9df4..eb365a4cd 100644 --- a/libretroshare/src/serialiser/rsdiscitems.cc +++ b/libretroshare/src/serialiser/rsdiscitems.cc @@ -181,6 +181,11 @@ uint32_t RsDiscSerialiser::sizeItem(RsDiscOwnItem *item) s += 2; /* contact_tf */ s += 4; /* discFlags */ + //add the size of the ip list + int ipListSize = item->ipAddressList.size(); + s += ipListSize * GetTlvIpAddrPortV4Size(); + s += ipListSize * 8; //size of an uint64 + return s; } @@ -215,6 +220,13 @@ bool RsDiscSerialiser::serialiseItem(RsDiscOwnItem *item, void *data, uint32 ok &= setRawUInt16(data, tlvsize, &offset, item->contact_tf); ok &= setRawUInt32(data, tlvsize, &offset, item->discFlags); + //store the ip list + std::list::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); + } + if (offset != tlvsize) { ok = false; @@ -273,6 +285,18 @@ RsDiscOwnItem *RsDiscSerialiser::deserialiseOwnItem(void *data, uint32_t *pktsiz ok &= getRawUInt16(data, rssize, &offset, &(item->contact_tf)); ok &= getRawUInt32(data, rssize, &offset, &(item->discFlags)); + //get the ip adress list + std::list ipTimedList; + while (offset < rssize) { + IpAddressTimed ipTimed; + ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &ipTimed.ipAddr); + uint64_t time; + ok &= getRawUInt64(data, rssize, &offset, &time); + ipTimed.seenTime = time; + ipTimedList.push_back(ipTimed); + } + item->ipAddressList = ipTimedList; + if (offset != rssize) { #ifdef RSSERIAL_DEBUG @@ -353,6 +377,11 @@ uint32_t RsDiscSerialiser::sizeReply(RsDiscReply *item) s += GetTlvStringSize(item->aboutId); s += item->certDER.TlvSize(); + //add the size of the ip list + int ipListSize = item->ipAddressList.size(); + s += ipListSize * GetTlvIpAddrPortV4Size(); + s += ipListSize * 8; //size of an uint64 + return s; } @@ -389,6 +418,14 @@ bool RsDiscSerialiser::serialiseReply(RsDiscReply *item, void *data, uint32_ ok &= item->certDER.SetTlv(data, tlvsize, &offset); + + //store the ip list + std::list::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); + } + if (offset != tlvsize) { ok = false; @@ -451,6 +488,17 @@ RsDiscReply *RsDiscSerialiser::deserialiseReply(void *data, uint32_t *pktsize) TLV_TYPE_STR_PEERID, item->aboutId); ok &= item->certDER.GetTlv(data, rssize, &offset); + //get the ip adress list + std::list ipTimedList; + while (offset < rssize) { + IpAddressTimed ipTimed; + ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &ipTimed.ipAddr); + uint64_t time; + ok &= getRawUInt64(data, rssize, &offset, &time); + ipTimed.seenTime = time; + ipTimedList.push_back(ipTimed); + } + item->ipAddressList = ipTimedList; if (offset != rssize) { diff --git a/libretroshare/src/serialiser/rsdiscitems.h b/libretroshare/src/serialiser/rsdiscitems.h index dc9a255f5..da20f2fa9 100644 --- a/libretroshare/src/serialiser/rsdiscitems.h +++ b/libretroshare/src/serialiser/rsdiscitems.h @@ -58,6 +58,10 @@ class RsDiscOwnItem: public RsDiscItem virtual void clear(); virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); + //use for transmitting ip address list + std::list ipAddressList; + + //use for transmitting my own adress list struct sockaddr_in laddr; struct sockaddr_in saddr; @@ -81,7 +85,7 @@ virtual ~RsDiscReply(); virtual void clear(); virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); - //use for transmitting peer adress list + //use for transmitting ip address list std::list ipAddressList; //use for transmitting my own adress list diff --git a/libretroshare/src/services/p3disc.cc b/libretroshare/src/services/p3disc.cc index 741c3f69e..ab6ddfc4a 100644 --- a/libretroshare/src/services/p3disc.cc +++ b/libretroshare/src/services/p3disc.cc @@ -360,6 +360,7 @@ void p3disc::sendOwnDetails(std::string to) di -> PeerId(to); di -> laddr = detail.currentlocaladdr; di -> saddr = detail.currentserveraddr; + di -> ipAddressList = detail.getIpAddressList(); di -> contact_tf = 0; /* construct disc flags */ @@ -630,7 +631,7 @@ void p3disc::recvPeerOwnMsg(RsDiscOwnItem *item) flags |= RS_NET_FLAGS_STABLE_UDP; } - mConnMgr->peerStatus(item->PeerId(), item->laddr, item->saddr, + mConnMgr->peerStatus(item->PeerId(), item->laddr, item->saddr, item->ipAddressList, type, flags, RS_CB_PERSON); /* also add as potential stun buddy */ @@ -703,7 +704,7 @@ void p3disc::recvPeerFriendMsg(RsDiscReply *item) /* only valid certs, and not ourselves */ if ((loaded) && (peerId != mConnMgr->getOwnId())) { - mConnMgr->peerStatus(peerId, item->currentladdr, item->currentsaddr, type, flags, RS_CB_DISC); + mConnMgr->peerStatus(peerId, item->currentladdr, item->currentsaddr, item->ipAddressList, type, flags, RS_CB_DISC); std::string hashid1 = RsUtil::HashId(peerId, false); mConnMgr->stunStatus(hashid1, item->currentsaddr, type, RS_STUN_FRIEND_OF_FRIEND);