From 9b79d70fa88fce7ec5ade004377406a15b10067a Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 26 Dec 2011 17:39:38 +0000 Subject: [PATCH] debugging of connectivity challenge git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-ChatLobby@4736 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/serialiser/rsmsgitems.cc | 10 +--- libretroshare/src/services/p3chatservice.cc | 56 +++++++++++---------- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/libretroshare/src/serialiser/rsmsgitems.cc b/libretroshare/src/serialiser/rsmsgitems.cc index 11dc36692..76839bca1 100644 --- a/libretroshare/src/serialiser/rsmsgitems.cc +++ b/libretroshare/src/serialiser/rsmsgitems.cc @@ -183,6 +183,7 @@ RsItem *RsChatSerialiser::deserialise(void *data, uint32_t *pktsize) case RS_PKT_SUBTYPE_CHAT_AVATAR: return new RsChatAvatarItem(data,*pktsize) ; case RS_PKT_SUBTYPE_CHAT_LOBBY_MSG: return new RsChatLobbyMsgItem(data,*pktsize) ; case RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE: return new RsChatLobbyInviteItem(data,*pktsize) ; + case RS_PKT_SUBTYPE_CHAT_LOBBY_CHALLENGE: return new RsChatLobbyConnectChallengeItem(data,*pktsize) ; default: std::cerr << "Unknown packet type in chat!" << std::endl ; return NULL ; @@ -333,15 +334,6 @@ bool RsChatLobbyMsgItem::serialise(void *data, uint32_t& pktsize) ok &= setRawUInt64(data, tlvsize, &offset, msg_id); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, nick); -#ifdef CHAT_DEBUG - std::cerr << "Serialized the following message:" << std::endl; - std::cerr << "========== BEGIN MESSAGE =========" << std::endl; - for(uint32_t i=0;i::const_iterator it(_chat_lobbys.begin()) ;it!=_chat_lobbys.end();++it) { - std::cerr << " Lobby id\t: " << it->first << std::endl; - std::cerr << " Lobby name\t: " << it->second.lobby_name << std::endl; - std::cerr << " nick name\t: " << it->second.nick_name << std::endl; + std::cerr << " Lobby id\t\t: " << std::hex << it->first << std::dec << std::endl; + std::cerr << " Lobby name\t\t: " << it->second.lobby_name << std::endl; + std::cerr << " nick name\t\t: " << it->second.nick_name << std::endl; std::cerr << " Lobby peer id\t: " << it->second.virtual_peer_id << std::endl; + std::cerr << " Challenge count\t: " << it->second.connexion_challenge_count << std::endl; + std::cerr << " Cached messages\t: " << it->second.msg_cache.size() << std::endl; + + for(std::map::const_iterator it2(it->second.msg_cache.begin());it2!=it->second.msg_cache.end();++it2) + std::cerr << " " << std::hex << it2->first << std::dec << " time=" << it2->second << std::endl; + std::cerr << " Participating friends: " << std::endl; for(std::set::const_iterator it2(it->second.participating_friends.begin());it2!=it->second.participating_friends.end();++it2) @@ -273,15 +279,13 @@ void p3ChatService::locked_printDebugInfo() const std::cerr << "Recorded lobby names: " << std::endl; for( std::map::const_iterator it(_lobby_ids.begin()) ;it!=_lobby_ids.end();++it) - std::cerr << " \"" << it->first << "\" id = " << it->second << std::endl; + std::cerr << " \"" << it->first << "\" id = " << std::hex << it->second << std::dec << std::endl; } bool p3ChatService::isLobbyId(const std::string& id,ChatLobbyId& lobby_id) { RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/ - locked_printDebugInfo() ; // debug - std::map::const_iterator it(_lobby_ids.find(id)) ; if(it != _lobby_ids.end()) @@ -464,6 +468,7 @@ void p3ChatService::checkAndRedirectMsgToLobby(RsChatMsgItem *ci) if(it == _chat_lobbys.end()) { std::cerr << "(EE) p3ChatService::checkAndRedirectMsgToLobby(): RsItem is a lobby item, but the id is not known!!" << std::endl; + ci->PeerId(std::string()) ; return ; } vpeer_id = it->second.virtual_peer_id ; @@ -1285,6 +1290,7 @@ bool p3ChatService::recvLobbyChat(RsChatLobbyMsgItem *item) { RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/ + locked_printDebugInfo() ; // debug std::cerr << "Handling ChatLobbyMsg " << std::hex << item->msg_id << ", lobby id " << item->lobby_id << ", from peer id " << item->PeerId() << std::endl; // send upward for display @@ -1346,20 +1352,20 @@ bool p3ChatService::sendLobbyChat(const std::wstring& msg, const ChatLobbyId& lo { RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/ - std::cerr << "Sending chat lobby message to lobby " << lobby_id << std::endl; + std::cerr << "Sending chat lobby message to lobby " << std::hex << lobby_id << std::dec << std::endl; std::cerr << "msg:" << std::endl; std::wcerr << msg << std::endl; // get a pointer to the info for that chat lobby. // - std::map::const_iterator it(_chat_lobbys.find(lobby_id)) ; + std::map::iterator it(_chat_lobbys.find(lobby_id)) ; if(it == _chat_lobbys.end()) { std::cerr << "Chatlobby for id " << std::hex << lobby_id << " has no record. This is a serious error!!" << std::dec << std::endl; return false ; } - const ChatLobbyEntry& lobby(it->second) ; + ChatLobbyEntry& lobby(it->second) ; RsChatLobbyMsgItem item ; @@ -1367,6 +1373,8 @@ bool p3ChatService::sendLobbyChat(const std::wstring& msg, const ChatLobbyId& lo // do { item.msg_id = RSRandom::random_u64(); } while( lobby.msg_cache.find(item.msg_id) != lobby.msg_cache.end() ) ; + lobby.msg_cache[item.msg_id] = time(NULL) ; // put the msg in cache! + item.lobby_id = lobby_id ; item.nick = lobby.nick_name ; @@ -1387,6 +1395,7 @@ bool p3ChatService::sendLobbyChat(const std::wstring& msg, const ChatLobbyId& lo sendItem(sitem); } + locked_printDebugInfo() ; // debug return true ; } @@ -1403,9 +1412,13 @@ void p3ChatService::handleConnectionChallenge(RsChatLobbyConnectChallengeItem *i { RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/ - for(std::map::const_iterator it(_chat_lobbys.begin());it!=_chat_lobbys.end();++it) - for(std::map::const_iterator it2(it->second.msg_cache.begin());it2!=it->second.msg_cache.end();++it2) - if(makeConnexionChallengeCode(it->first,it2->first) == item->challenge_code) + for(std::map::const_iterator it(_chat_lobbys.begin());it!=_chat_lobbys.end() && !found;++it) + for(std::map::const_iterator it2(it->second.msg_cache.begin());it2!=it->second.msg_cache.end() && !found;++it2) + { + uint64_t code = makeConnexionChallengeCode(it->first,it2->first) ; + std::cerr << " Lobby_id = 0x" << std::hex << it->first << ", msg_id = 0x" << it2->first << ": code = 0x" << code << std::dec << std::endl ; + + if(code == item->challenge_code) { std::cerr << " Challenge accepted for lobby " << std::hex << it->first << ", for chat msg " << it2->first << std::dec << std::endl ; std::cerr << " Sending connection request to peer " << item->PeerId() << std::endl; @@ -1413,6 +1426,7 @@ void p3ChatService::handleConnectionChallenge(RsChatLobbyConnectChallengeItem *i lobby_id = it->first ; found = true ; } + } } if(found) @@ -1441,10 +1455,11 @@ void p3ChatService::sendConnectionChallenge(ChatLobbyId lobby_id) uint64_t code = 0 ; for(std::map::const_iterator it2(it->second.msg_cache.begin());it2!=it->second.msg_cache.end();++it2) - if(it2->second + 20 < now) // any msg not older than 20 seconds is fine. + if(it2->second + 20 > now) // any msg not older than 20 seconds is fine. { - std::cerr << " Using msg id 0x" << std::hex << it2->first << ", challenge code = " << code << std::dec << std::endl; code = makeConnexionChallengeCode(lobby_id,it2->first) ; + std::cerr << " Using msg id 0x" << std::hex << it2->first << ", challenge code = " << code << std::dec << std::endl; + break ; } if(code == 0) @@ -1471,18 +1486,7 @@ void p3ChatService::sendConnectionChallenge(ChatLobbyId lobby_id) uint64_t p3ChatService::makeConnexionChallengeCode(ChatLobbyId lobby_id,ChatLobbyMsgId msg_id) { - uint64_t a = lobby_id ; - uint64_t b = msg_id ; - - for(int i=0;i<10;++i) - { - a = ((a+0x3eb57bac44980ab2) ^ (b+0x11278ea3b205aa4e)) + 0x928eeba8 ; - b = ((~b) << 4) + (a & 0xffff) ; - } - - std::cerr << "Making connection challenge id: lobby_id=0x" << std::hex << lobby_id << ", msg=0x" << msg_id << ", code = " << a << std::dec << std::endl ; - - return a ; + return ((uint64_t)lobby_id) ^ (uint64_t)msg_id ; } void p3ChatService::getChatLobbyList(std::list& linfos)