add the serializing of ip adress list, and the process to ad ip to the list when a connection is made to a peer

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1806 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
joss17 2009-11-11 16:44:51 +00:00
parent 74e20960c2
commit 9a6ff52da1
3 changed files with 130 additions and 1 deletions

View File

@ -1593,6 +1593,82 @@ bool p3ConnectMgr::connectResult(std::string id, bool success, uint32_t flags)
it->second.lastcontact = time(NULL); /* time of connect */
it->second.connecttype = flags;
//check if the ip list contains the current remote address of the connected peer
//TODO : we update both internal and external tcp adress, we should try to determinate wich one was use for this connection
bool found = false;
std::list<IpAddressTimed>::iterator ipListIt;
for (ipListIt = it->second.remoteaddrList.begin(); ipListIt!=(it->second.remoteaddrList.end()); ipListIt++) {
if (ipListIt->ipAddr.sin_addr.s_addr == it->second.currentserveraddr.sin_addr.s_addr && ipListIt->ipAddr.sin_port == it->second.currentserveraddr.sin_port) {
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::connectResult() remote 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 << std::endl;
#endif
found = true;
//update the seen time
ipListIt->seenTime = time_t(NULL);
break;
}
}
if (!found && (it->second.currentserveraddr.sin_addr.s_addr != 0)) {
//add the current addresses to the ip list
IpAddressTimed ipAdress;
ipAdress.seenTime = time_t(NULL);
ipAdress.ipAddr = it->second.currentserveraddr;
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::connectResult() adding to the ip list the current remote addr : " << id;
std::cerr << inet_ntoa(it->second.currentserveraddr.sin_addr);
std::cerr << ":" << ntohs(it->second.currentserveraddr.sin_port);
std::cerr << std::endl;
#endif
it->second.remoteaddrList.push_back(ipAdress);
}
//check if the list contains the current local address of the connected peer
found = false;
for (ipListIt = it->second.remoteaddrList.begin(); ipListIt!=(it->second.remoteaddrList.end()); ipListIt++) {
if (ipListIt->ipAddr.sin_addr.s_addr == it->second.currentlocaladdr.sin_addr.s_addr && ipListIt->ipAddr.sin_port == it->second.currentlocaladdr.sin_port) {
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::connectResult() remote 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 << std::endl;
#endif
found = true;
//update the seen time
ipListIt->seenTime = time_t(NULL);
break;
}
}
if (!found && (it->second.currentlocaladdr.sin_addr.s_addr != 0)) {
//add the current addresses to the ip list
IpAddressTimed ipAdress;
ipAdress.seenTime = time_t(NULL);
ipAdress.ipAddr = it->second.currentlocaladdr;
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::connectResult() adding to the ip list the current local addr : " << id;
std::cerr << inet_ntoa(it->second.currentlocaladdr.sin_addr);
std::cerr << ":" << ntohs(it->second.currentlocaladdr.sin_port);
std::cerr << std::endl;
#endif
it->second.remoteaddrList.push_back(ipAdress);
}
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::connectResult() current ip list for the peer : " << id;
std::cerr << ", size : " << it->second.remoteaddrList.size();
std::cerr << ", adresses : " << std::endl;
#endif
for (ipListIt = it->second.remoteaddrList.begin(); ipListIt!=(it->second.remoteaddrList.end()); ipListIt++) {
#ifdef CONN_DEBUG
std::cerr << inet_ntoa(ipListIt->ipAddr.sin_addr);
std::cerr << ":" << ntohs(ipListIt->ipAddr.sin_port);
std::cerr << std::endl;
#endif
}
return true;
}
@ -2858,6 +2934,32 @@ bool p3ConnectMgr::setExtAddress(std::string id, struct sockaddr_in addr)
return true;
}
bool p3ConnectMgr::setAddressList(std::string id, std::list<IpAddressTimed> IpAddressTimedList)
{
RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/
/* check if it is a friend */
std::map<std::string, peerConnectState>::iterator it;
if (mFriendList.end() == (it = mFriendList.find(id)))
{
if (mOthersList.end() == (it = mOthersList.find(id)))
{
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::setLocalAddress() cannot add addres info : peer id not found in friend list ";
std::cerr << " id: " << id;
std::cerr << std::endl;
#endif
return false;
}
}
/* "it" points to peer */
it->second.remoteaddrList = IpAddressTimedList;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
return true;
}
bool p3ConnectMgr::setNetworkMode(std::string id, uint32_t netMode)
{
if (id == mAuthMgr->OwnId())
@ -3269,6 +3371,7 @@ bool p3ConnectMgr::loadList(std::list<RsItem *> load)
addFriend(pitem->pid, pitem->netMode, pitem->visState, pitem->lastContact);
setLocalAddress(pitem->pid, pitem->currentlocaladdr);
setExtAddress(pitem->pid, pitem->currentremoteaddr);
setAddressList(pitem->pid, pitem->remoteaddrList);
}
}
else if (sitem)

View File

@ -210,6 +210,8 @@ bool getNetStatusExtraAddressCheckOk();
void setOwnNetConfig(uint32_t netMode, uint32_t visState);
bool setLocalAddress(std::string id, struct sockaddr_in addr);
bool setExtAddress(std::string id, struct sockaddr_in addr);
bool setAddressList(std::string id, std::list<IpAddressTimed> IpAddressTimedList);
bool setNetworkMode(std::string id, uint32_t netMode);
bool setVisState(std::string id, uint32_t visState);

View File

@ -763,6 +763,11 @@ uint32_t RsPeerConfigSerialiser::sizeNet(RsPeerNetItem *i)
s += GetTlvIpAddrPortV4Size(); /* localaddr */
s += GetTlvIpAddrPortV4Size(); /* remoteaddr */
//add the size of the ip list
int ipListSize = i->remoteaddrList.size();
s += ipListSize * GetTlvIpAddrPortV4Size();
s += ipListSize * 8; //size of an uint64
return s;
}
@ -785,7 +790,7 @@ bool RsPeerConfigSerialiser::serialiseNet(RsPeerNetItem *item, void *data, uint3
#ifdef RSSERIAL_DEBUG
std::cerr << "RsPeerConfigSerialiser::serialiseNet() Header: " << ok << std::endl;
std::cerr << "RsPeerConfigSerialiser::serialiseNet() Header: " << tlvsize << std::endl;
std::cerr << "RsPeerConfigSerialiser::serialiseNet() Header test: " << tlvsize << std::endl;
#endif
/* skip the header */
@ -799,6 +804,13 @@ bool RsPeerConfigSerialiser::serialiseNet(RsPeerNetItem *item, void *data, uint3
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_LOCAL, &(item->currentlocaladdr));
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(item->currentremoteaddr));
//store the ip list
std::list<IpAddressTimed>::iterator ipListIt;
for (ipListIt = item->remoteaddrList.begin(); ipListIt!=(item->remoteaddrList.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;
@ -850,6 +862,18 @@ RsPeerNetItem *RsPeerConfigSerialiser::deserialiseNet(void *data, uint32_t *size
ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_LOCAL, &(item->currentlocaladdr));
ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &(item->currentremoteaddr));
//get the ip adress list
std::list<IpAddressTimed> 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->remoteaddrList = ipTimedList;
if (offset != rssize)
{