diff src/plugins/plugin_xep_0260.py @ 1758:a66d34353f34

plugin XEP-0260, XEP-0065: fixed session hash handling: with XEP-0260, we need to manage 2 hashes (one with SHA1(SID + Initiator JID + Responder JID) and the other with SHA1(SID + Responder JID + Initiator JID)). This was not handled correclty, resulting in transfer failure in several cases.
author Goffi <goffi@goffi.org>
date Thu, 17 Dec 2015 22:37:59 +0100
parents abd6d6f89006
children d17772b0fe22
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0260.py	Thu Dec 17 22:37:58 2015 +0100
+++ b/src/plugins/plugin_xep_0260.py	Thu Dec 17 22:37:59 2015 +0100
@@ -132,6 +132,7 @@
         transport_data = content_data['transport_data']
         sid = transport_data['sid'] = unicode(uuid.uuid4())
         session_hash = transport_data['session_hash'] = self._s5b.getSessionHash(client.jid, session['peer_jid'], sid)
+        transport_data['peer_session_hash'] = self._s5b.getSessionHash(session['peer_jid'], client.jid, sid) # requester and target are inversed for peer candidates
         transport_data['stream_d'] = self._s5b.registerHash(session_hash, None, profile)
         candidates = transport_data['candidates'] = yield self._s5b.getCandidates(profile)
         mode = 'tcp' # XXX: we only manage tcp for now
@@ -221,7 +222,7 @@
         else:
             if best_candidate.priority == peer_best_candidate.priority:
                 # same priority, we choose initiator one according to XEP-0260 ยง2.4 #4
-                log.debug(u"Candidates have same priority, we choose the initiator one")
+                log.debug(u"Candidates have same priority, we select the one choosed by initiator")
                 if session['initiator'] == client.jid:
                     choosed_candidate = best_candidate
                 else:
@@ -353,7 +354,6 @@
             assert 'peer_candidates' not in transport_data
             transport_data['peer_candidates'] = self._parseCandidates(transport_elt)
 
-        # elif action == self._j.A_START:
         elif action == self._j.A_START:
             session_hash = transport_data['session_hash']
             peer_candidates = transport_data['peer_candidates']
@@ -361,7 +361,8 @@
             self._s5b.associateFileObj(session_hash, file_obj, profile)
             stream_d = transport_data.pop('stream_d')
             stream_d.chainDeferred(content_data['finished_d'])
-            d = self._s5b.getBestCandidate(peer_candidates, session_hash, profile)
+            peer_session_hash = transport_data['peer_session_hash']
+            d = self._s5b.getBestCandidate(peer_candidates, session_hash, peer_session_hash, profile)
             d.addCallback(self._foundPeerCandidate, session, transport_data, content_name, client)
 
         elif action == self._j.A_SESSION_INITIATE:
@@ -369,12 +370,13 @@
             # and we give our candidates
             assert 'peer_candidates' not in transport_data
             sid = transport_data['sid'] = transport_elt['sid']
-            session_hash = transport_data['session_hash'] = self._s5b.getSessionHash(session['peer_jid'], client.jid, sid)
+            session_hash = transport_data['session_hash'] = self._s5b.getSessionHash(client.jid, session['peer_jid'], sid)
+            peer_session_hash = transport_data['peer_session_hash'] = self._s5b.getSessionHash(session['peer_jid'], client.jid, sid) # requester and target are inversed for peer candidates
             peer_candidates = transport_data['peer_candidates'] = self._parseCandidates(transport_elt)
             file_obj = content_data['file_obj']
             stream_d = self._s5b.registerHash(session_hash, file_obj, profile)
             stream_d.chainDeferred(content_data['finished_d'])
-            d = self._s5b.getBestCandidate(peer_candidates, session_hash, profile)
+            d = self._s5b.getBestCandidate(peer_candidates, session_hash, peer_session_hash, profile)
             d.addCallback(self._foundPeerCandidate, session, transport_data, content_name, client)
             candidates = yield self._s5b.getCandidates(profile)
             # we remove duplicate candidates
@@ -402,7 +404,7 @@
             if candidate_elt is None:
                 log.warning(u"Unexpected transport element: {}".format(transport_elt.toXml()))
         elif action == self._j.A_DESTROY:
-            # the transport is remplaced (fallback ?). We need mainly to stop kill XEP-0065 session
+            # the transport is replaced (fallback ?), We need mainly to kill XEP-0065 session.
             # note that sid argument is not necessary for sessions created by this plugin
             self._s5b.killSession(None, transport_data['session_hash'], None, client)
         else: