changeset ae34122435e2 in /home/hg/repos/gajim
details:http://hg.gajim.org/gajim?cmd=changeset;node=ae34122435e2 description: Wait until candidate negociation ends; Socks5 connection problem fixed
diffstat:
src/common/jingle_ft.py | 158 +++++++++++++++++++++++++++++++++++++---------- src/common/socks5.py | 141 +++++++++++++++++++++++++++-------------- 2 files changed, 217 insertions(+), 82 deletions(-)
diffs (truncated from 571 to 300 lines):
diff -r 4655081ec8d0 -r ae34122435e2 src/common/jingle_ft.py --- a/src/common/jingle_ft.py Sun Aug 14 23:49:23 2011 -0400 +++ b/src/common/jingle_ft.py Sun Aug 14 23:59:39 2011 -0400 @@ -35,7 +35,17 @@ STATE_ACCEPTED = 2 STATE_TRANSPORT_INFO = 3 STATE_PROXY_ACTIVATED = 4 -STATE_TRANSPORT_REPLACE = 5 +# We send the candidates and we are waiting for a reply +STATE_CAND_SENT_PENDING_REPLY = 5 +# We received the candidates and we are waiting to reply +STATE_CAND_RECEIVED_PENDING_REPLY = 6 +# We have sent and received the candidates +# This also includes any candidate-error received or sent +STATE_CAND_SENT_AND_RECEIVED = 7 +# We are transfering the file +STATE_TRANSFERING = 8 +STATE_TRANSPORT_REPLACE = 9 +
class JingleFileTransfer(JingleContent): def __init__(self, session, transport=None, file_props=None, @@ -88,7 +98,7 @@
self.session = session self.media = 'file' - + self.nominated_cand = {}
def __on_session_initiate(self, stanza, content, error, action): gajim.nec.push_incoming_event(FileRequestReceivedEvent(None, @@ -152,10 +162,20 @@ def __on_transport_info(self, stanza, content, error, action): log.info("__on_transport_info")
- if not self.weinitiate: # proxy activated from initiator - return + #if not self.weinitiate: # proxy activated from initiator + # return if content.getTag('transport').getTag('candidate-error'): - self.session.transport_replace() + self.nominated_cand['peer-cand'] = False + if self.state == STATE_CAND_SENT_PENDING_REPLY: + #self.state = STATE_CAND_SENT_AND_RECEIVED + if not self.nominated_cand['our-cand'] and \ + not self.nominated_cand['peer-cand']: + if not self.weinitiate: + return + self.session.transport_replace() + else: + self.state = STATE_CAND_RECEIVED_PENDING_REPLY + return streamhost_cid = content.getTag('transport').getTag('candidate-used').\ getAttr('cid') @@ -167,29 +187,17 @@ if streamhost_used == None: log.info("unknow streamhost") return - if streamhost_used['type'] == 'proxy': - self.file_props['streamhost-used'] = True - for proxy in self.file_props['proxyhosts']: - if proxy['host'] == streamhost_used['host'] and \ - proxy['port'] == streamhost_used['port'] and \ - proxy['jid'] == streamhost_used['jid']: - host_used = proxy - break - if 'streamhosts' not in self.file_props: - self.file_props['streamhosts'] = [] - self.file_props['streamhosts'].append(streamhost_used) - self.file_props['is_a_proxy'] = True - receiver = Socks5Receiver(gajim.idlequeue, streamhost_used, - self.file_props['sid'], self.file_props) - gajim.socks5queue.add_receiver(self.session.connection.name, - receiver) - streamhost_used['idx'] = receiver.queue_idx - gajim.socks5queue.on_success[self.file_props['sid']] = \ - self.transport._on_proxy_auth_ok + # We save the candidate nominated by peer + self.nominated_cand['peer-cand'] = streamhost_used + if self.state == STATE_CAND_SENT_PENDING_REPLY: + response = stanza.buildReply('result') + self.session.connection.connection.send(response) + self.start_transfer(streamhost_used) + raise xmpp.NodeProcessed else: - jid = gajim.get_jid_without_resource(self.session.ourjid) - gajim.socks5queue.send_file(self.file_props, - self.session.connection.name) + self.state = STATE_CAND_RECEIVED_PENDING_REPLY + +
def __on_iq_result(self, stanza, content, error, action): log.info("__on_iq_result") @@ -221,6 +229,16 @@ elif self.weinitiate and self.state == STATE_INITIALIZED: # proxy activated self.state = STATE_PROXY_ACTIVATED + elif self.state == STATE_CAND_SENT_AND_RECEIVED: + + if not self.nominated_cand['our-cand'] and \ + not self.nominated_cand['peer-cand']: + if not self.weinitiate: + return + self.session.transport_replace() + return + # initiate transfer + self.start_transfer(None)
def send_candidate_used(self, streamhost): """ @@ -229,7 +247,13 @@ log.info('send_candidate_used') if streamhost is None: return - + + self.nominated_cand['our-cand'] = streamhost + if self.state == STATE_CAND_RECEIVED_PENDING_REPLY: + self.state = STATE_CAND_SENT_AND_RECEIVED + else: + self.state = STATE_CAND_SENT_PENDING_REPLY + content = xmpp.Node('content') content.setAttr('creator', 'initiator') content.setAttr('name', self.name) @@ -246,9 +270,16 @@
self.session.send_transport_info(content)
+ def _on_connect_error(self, to, _id, sid, code=404): - if code == 404 and self.file_props['sid'] == sid: - self.send_error_candidate() + self.nominated_cand['our-cand'] = False + self.send_error_candidate() + + if self.state == STATE_CAND_RECEIVED_PENDING_REPLY: + self.state = STATE_CAND_SENT_AND_RECEIVED + else: + self.state = STATE_CAND_SENT_PENDING_REPLY +
log.info('connect error, sid=' + sid)
@@ -297,20 +328,81 @@ fingerprint = None if self.use_security: fingerprint = 'server' - return + if self.weinitiate: listener = gajim.socks5queue.start_listener(port, sha_str, - self._store_socks5_sid, self.file_props['sid'], + self._store_socks5_sid, self.file_props, fingerprint=fingerprint, type='sender') else: listener = gajim.socks5queue.start_listener(port, sha_str, - self._store_socks5_sid, self.file_props['sid'], + self._store_socks5_sid, self.file_props, fingerprint=fingerprint, type='receiver')
if not listener: # send error message, notify the user return + def isOurCandUsed(self):
+ if self.nominated_cand['peer-cand'] == False: + return True + if self.nominated_cand['our-cand'] == False: + return False + + peer_pr = int(self.nominated_cand['peer-cand']['priority']) + our_pr = int(self.nominated_cand['our-cand']['priority']) + + if peer_pr != our_pr: + if peer_pr > our_pr: + # Choose peer host + return False + else: + # Choose our host + return True + else: + if self.weinitiate: + # Choose our host + return True + else: + # Choose peer host + return False + + + + def start_transfer(self, streamhost_used): + + self.state = STATE_TRANSFERING + + if self.isOurCandUsed(): + print 'our' + else: + print 'peer' + + + # FIXME if streamhost_used is none where do we get the proxy host + if streamhost_used and streamhost_used['type'] == 'proxy': + self.file_props['streamhost-used'] = True + for proxy in self.file_props['proxyhosts']: + if proxy['host'] == streamhost_used['host'] and \ + proxy['port'] == streamhost_used['port'] and \ + proxy['jid'] == streamhost_used['jid']: + host_used = proxy + break + if 'streamhosts' not in self.file_props: + self.file_props['streamhosts'] = [] + self.file_props['streamhosts'].append(streamhost_used) + self.file_props['is_a_proxy'] = True + receiver = Socks5Receiver(gajim.idlequeue, streamhost_used, + self.file_props['sid'], self.file_props) + gajim.socks5queue.add_receiver(self.session.connection.name, + receiver) + streamhost_used['idx'] = receiver.queue_idx + gajim.socks5queue.on_success[self.file_props['sid']] = \ + self.transport._on_proxy_auth_ok + else: + jid = gajim.get_jid_without_resource(self.session.ourjid) + gajim.socks5queue.send_file(self.file_props, + self.session.connection.name) + def get_content(desc): return JingleFileTransfer
diff -r 4655081ec8d0 -r ae34122435e2 src/common/socks5.py --- a/src/common/socks5.py Sun Aug 14 23:49:23 2011 -0400 +++ b/src/common/socks5.py Sun Aug 14 23:59:39 2011 -0400 @@ -84,16 +84,18 @@ self.on_success = {} # {id: cb} self.on_failure = {} # {id: cb}
- def start_listener(self, port, sha_str, sha_handler, sid, fingerprint=None, + def start_listener(self, port, sha_str, sha_handler, fp, fingerprint=None, type='sender'): """ Start waiting for incomming connections on (host, port) and do a socks5 authentication using sid for generated SHA """ + sid = fp['sid'] self.type = type # It says whether we are sending or receiving self.sha_handlers[sha_str] = (sha_handler, sid) if self.listener is None: - self.listener = Socks5Listener(self.idlequeue, port, fingerprint=fingerprint) + self.listener = Socks5Listener(self.idlequeue, port, fp, + fingerprint=fingerprint) self.listener.queue = self self.listener.bind() if self.listener.started is False: @@ -130,7 +132,7 @@ self.on_failure[sid] = on_failure file_props = self.files_props[account][sid] file_props['failure_cb'] = on_failure - + # add streamhosts to the queue for streamhost in file_props['streamhosts']: if 'type' in streamhost and streamhost['type'] == 'proxy': @@ -149,8 +151,8 @@ file_props['sha_str'], self, mode='client' , _sock=None, host=str(streamhost['host']), port=int(streamhost['port']), fingerprint=fp, - connected=False) - socks5obj.file_props = file_props + connected=False, file_props=file_props) + #socks5obj.file_props = file_props socks5obj.streamhost = streamhost self.add_sockobj(account, socks5obj, type='sender')
@@ -234,7 +236,7 @@ return # failure_cb exists - this means that it has never been called if 'failure_cb' in file_props and file_props['failure_cb']: - file_props['failure_cb'](streamhost['initiator'], streamhost['idx'], + file_props['failure_cb'](streamhost['initiator'], None, file_props['sid'], code = 404) del(file_props['failure_cb'])
@@ -258,14 +260,6 @@ return 1 return None
- def get_file_from_sender(self, file_props, account): - if file_props is None: - return - if 'hash' in file_props and file_props['hash'] in self.senders: - sender = self.senders[file_props['hash']] - sender.account = account - result = self.get_file_contents(0) - self.process_result(result, sender)
def result_sha(self, sha_str, idx): if sha_str in self.sha_handlers: @@ -313,7 +307,9 @@