diff --git a/libretroshare/src/pqi/p3connmgr.cc b/libretroshare/src/pqi/p3connmgr.cc index 85c1b06cc..ae5981f48 100644 --- a/libretroshare/src/pqi/p3connmgr.cc +++ b/libretroshare/src/pqi/p3connmgr.cc @@ -1578,14 +1578,8 @@ bool p3ConnectMgr::connectResult(std::string id, bool success, uint32_t flags, s return false; } - it->second.inConnAttempt = false; - if (success) { - /* remove other attempts */ - it->second.inConnAttempt = false; - netAssistFriend(id, false); - /* update address (will come although through from DISC) */ #ifdef CONN_DEBUG @@ -1600,10 +1594,16 @@ bool p3ConnectMgr::connectResult(std::string id, bool success, uint32_t flags, s it->second.actions |= RS_PEER_CONNECTED; it->second.lastcontact = time(NULL); /* time of connect */ it->second.connecttype = flags; + //used to send back to the peer it's own ext address + it->second.currentserveraddr = remote_peer_address; - if (remote_peer_address.sin_addr.s_addr != 0 - && !(remote_peer_address.sin_addr.s_addr == ownState.currentlocaladdr.sin_addr.s_addr) - && (!isLoopbackNet(&remote_peer_address.sin_addr)) + //add the ip address in the address list if we were in a connect attempt and the attempt address is the same as the connect result + if (it->second.inConnAttempt && + it->second.currentConnAddrAttempt.addr.sin_addr.s_addr == remote_peer_address.sin_addr.s_addr && + it->second.currentConnAddrAttempt.addr.sin_port == remote_peer_address.sin_port && + remote_peer_address.sin_addr.s_addr != 0 && + !(remote_peer_address.sin_addr.s_addr == ownState.currentlocaladdr.sin_addr.s_addr) && + (!isLoopbackNet(&remote_peer_address.sin_addr)) ) { IpAddressTimed ipLocalAddressTimed; ipLocalAddressTimed.ipAddr = remote_peer_address; @@ -1617,10 +1617,16 @@ bool p3ConnectMgr::connectResult(std::string id, bool success, uint32_t flags, s #endif } + /* remove other attempts */ + it->second.inConnAttempt = false; + it->second.connAddrs.clear(); + netAssistFriend(id, false); mStatusChanged = true; return true; } + it->second.inConnAttempt = false; + #ifdef CONN_DEBUG std::cerr << "p3ConnectMgr::connectResult() Disconnect/Fail: id: " << id << std::endl; std::cerr << " Success: " << success << " flags: " << flags << std::endl; diff --git a/libretroshare/src/pqi/p3connmgr.h b/libretroshare/src/pqi/p3connmgr.h index 9f9c4a2d5..0e81160ba 100644 --- a/libretroshare/src/pqi/p3connmgr.h +++ b/libretroshare/src/pqi/p3connmgr.h @@ -276,6 +276,10 @@ bool doNextAttempt(std::string id); p3tunnel* getP3tunnel(); void setP3tunnel(p3tunnel *p3tun); +bool getUpnpExtAddress(struct sockaddr_in &addr); +bool getStunExtAddress(struct sockaddr_in &addr); +bool getExtFinderExtAddress(struct sockaddr_in &addr); + protected: /****************** Internal Interface *******************/ @@ -346,10 +350,6 @@ void tickMonitors(); bool retryConnectTCP(std::string id); bool retryConnectNotify(std::string id); -bool getUpnpExtAddress(struct sockaddr_in &addr); -bool getStunExtAddress(struct sockaddr_in &addr); -bool getExtFinderExtAddress(struct sockaddr_in &addr); - /* temporary for testing */ //virtual void loadConfiguration() { return; } @@ -421,11 +421,13 @@ private: bool oldnetFlagStunOk; bool oldnetFlagExtraAddressCheckOk; +public: + peerConnectState ownState; + protected: void addPeer(std::string id, std::string name); /* tmp fn */ - peerConnectState ownState; std::list mStunList; std::map mFriendList; diff --git a/libretroshare/src/services/p3disc.cc b/libretroshare/src/services/p3disc.cc index 84198fa0a..08bfdb6b0 100644 --- a/libretroshare/src/services/p3disc.cc +++ b/libretroshare/src/services/p3disc.cc @@ -235,10 +235,10 @@ void p3disc::statusChange(const std::list &plist) if ((pit->state & RS_PEER_S_FRIEND) && (pit->actions & RS_PEER_CONNECTED)) { - /* send our details to them */ - sendOwnDetails(pit->id); /* 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); } } } @@ -761,7 +761,24 @@ void p3disc::recvPeerFriendMsg(RsDiscReply *item) /* send Own Ip list to connect manager. It will extract the external ip address from it */ if (peerId == 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)) + ) { + //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); + } + } + }