2010-06-06 17:22:52 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 3 only.
|
|
|
|
##
|
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
Handles Jingle File Transfer (XEP 0234)
|
|
|
|
"""
|
|
|
|
|
|
|
|
import gajim
|
|
|
|
import xmpp
|
|
|
|
from jingle_content import contents, JingleContent
|
2012-01-08 20:44:15 +01:00
|
|
|
from jingle_transport import JingleTransportICEUDP, JingleTransportSocks5
|
|
|
|
from jingle_transport import JingleTransportIBB, TransportType
|
2010-07-03 10:22:47 +02:00
|
|
|
from common import helpers
|
2011-11-18 05:55:44 +01:00
|
|
|
from common.socks5 import Socks5ReceiverClient, Socks5SenderClient
|
2011-06-24 18:24:42 +02:00
|
|
|
from common.connection_handlers_events import FileRequestReceivedEvent
|
2012-01-16 01:37:00 +01:00
|
|
|
import threading
|
2010-06-06 17:22:52 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.c.jingle_ft')
|
|
|
|
|
2010-07-15 07:38:53 +02:00
|
|
|
STATE_NOT_STARTED = 0
|
|
|
|
STATE_INITIALIZED = 1
|
|
|
|
STATE_ACCEPTED = 2
|
|
|
|
STATE_TRANSPORT_INFO = 3
|
|
|
|
STATE_PROXY_ACTIVATED = 4
|
2011-08-15 05:59:39 +02:00
|
|
|
# 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
|
|
|
|
|
2010-06-06 17:22:52 +02:00
|
|
|
|
|
|
|
class JingleFileTransfer(JingleContent):
|
2010-08-24 23:12:34 +02:00
|
|
|
def __init__(self, session, transport=None, file_props=None,
|
|
|
|
use_security=False):
|
2010-06-06 17:22:52 +02:00
|
|
|
JingleContent.__init__(self, session, transport)
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-06-19 16:52:17 +02:00
|
|
|
log.info("transport value: %s" % transport)
|
2010-08-24 23:12:34 +02:00
|
|
|
|
|
|
|
# events we might be interested in
|
2010-06-06 17:22:52 +02:00
|
|
|
self.callbacks['session-initiate'] += [self.__on_session_initiate]
|
2012-01-16 01:37:00 +01:00
|
|
|
self.callbacks['session-initiate-sent'] += [self.__on_session_initiate_sent]
|
2010-08-26 10:36:58 +02:00
|
|
|
self.callbacks['content-add'] += [self.__on_session_initiate]
|
2010-06-06 17:22:52 +02:00
|
|
|
self.callbacks['session-accept'] += [self.__on_session_accept]
|
2012-01-16 01:37:00 +01:00
|
|
|
self.callbacks['session-terminate'] += [self.__on_session_terminate]
|
|
|
|
self.callbacks['session-info'] += [self.__on_session_info]
|
2010-06-06 17:22:52 +02:00
|
|
|
self.callbacks['transport-accept'] += [self.__on_transport_accept]
|
2010-08-24 23:12:34 +02:00
|
|
|
self.callbacks['transport-replace'] += [self.__on_transport_replace]
|
2012-01-07 05:18:50 +01:00
|
|
|
self.callbacks['session-accept-sent'] += [self.__transport_setup]
|
2010-08-24 23:12:34 +02:00
|
|
|
# fallback transport method
|
2010-06-06 17:22:52 +02:00
|
|
|
self.callbacks['transport-reject'] += [self.__on_transport_reject]
|
|
|
|
self.callbacks['transport-info'] += [self.__on_transport_info]
|
2010-07-03 10:22:47 +02:00
|
|
|
self.callbacks['iq-result'] += [self.__on_iq_result]
|
2010-06-06 17:22:52 +02:00
|
|
|
|
2010-07-15 07:38:53 +02:00
|
|
|
self.state = STATE_NOT_STARTED
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-07-22 09:05:06 +02:00
|
|
|
self.use_security = use_security
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-06-06 17:22:52 +02:00
|
|
|
self.file_props = file_props
|
2010-06-14 15:21:22 +02:00
|
|
|
if file_props is None:
|
2010-06-08 15:24:41 +02:00
|
|
|
self.weinitiate = False
|
|
|
|
else:
|
|
|
|
self.weinitiate = True
|
2010-06-06 17:22:52 +02:00
|
|
|
|
2010-06-14 15:21:22 +02:00
|
|
|
if self.file_props is not None:
|
2010-06-14 14:41:24 +02:00
|
|
|
self.file_props['sender'] = session.ourjid
|
2010-07-03 10:22:47 +02:00
|
|
|
self.file_props['receiver'] = session.peerjid
|
2010-06-14 14:41:24 +02:00
|
|
|
self.file_props['session-type'] = 'jingle'
|
2010-08-25 12:05:14 +02:00
|
|
|
self.file_props['session-sid'] = session.sid
|
2010-06-14 14:41:24 +02:00
|
|
|
self.file_props['transfered_size'] = []
|
|
|
|
|
2010-08-24 23:12:34 +02:00
|
|
|
log.info("FT request: %s" % file_props)
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2010-06-14 15:21:22 +02:00
|
|
|
if transport is None:
|
2010-06-19 16:52:17 +02:00
|
|
|
self.transport = JingleTransportSocks5()
|
2011-07-18 00:28:38 +02:00
|
|
|
self.transport.set_connection(session.connection)
|
|
|
|
self.transport.set_file_props(self.file_props)
|
|
|
|
self.transport.set_our_jid(session.ourjid)
|
2010-06-19 16:52:17 +02:00
|
|
|
log.info('ourjid: %s' % session.ourjid)
|
2010-06-08 15:24:41 +02:00
|
|
|
|
2010-08-25 12:05:14 +02:00
|
|
|
if self.file_props is not None:
|
|
|
|
self.file_props['sid'] = self.transport.sid
|
|
|
|
|
2010-06-08 15:24:41 +02:00
|
|
|
self.session = session
|
2010-06-14 14:41:24 +02:00
|
|
|
self.media = 'file'
|
2011-08-15 05:59:39 +02:00
|
|
|
self.nominated_cand = {}
|
2012-01-16 01:37:00 +01:00
|
|
|
|
2012-01-24 04:28:07 +01:00
|
|
|
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-06-06 17:22:52 +02:00
|
|
|
def __on_session_initiate(self, stanza, content, error, action):
|
2011-06-24 18:24:42 +02:00
|
|
|
gajim.nec.push_incoming_event(FileRequestReceivedEvent(None,
|
|
|
|
conn=self.session.connection, stanza=stanza, jingle_content=content,
|
|
|
|
FT_content=self))
|
2012-01-16 01:37:00 +01:00
|
|
|
def __on_session_initiate_sent(self, stanza, content, error, action):
|
|
|
|
# Calculate file_hash in a new thread
|
|
|
|
self.hashThread = threading.Thread(target=self.__calcHash)
|
|
|
|
self.hashThread.start()
|
|
|
|
|
|
|
|
def __calcHash(self):
|
2012-01-24 04:28:07 +01:00
|
|
|
if self.session.hash_algo == None:
|
2012-01-16 01:37:00 +01:00
|
|
|
return
|
|
|
|
try:
|
2012-01-24 22:51:26 +01:00
|
|
|
file_ = open(self.file_props['file-name'], 'r')
|
2012-01-16 01:37:00 +01:00
|
|
|
except:
|
2012-01-24 22:51:26 +01:00
|
|
|
# can't open file
|
2012-01-16 01:37:00 +01:00
|
|
|
return
|
|
|
|
h = xmpp.Hashes()
|
2012-01-24 22:51:26 +01:00
|
|
|
hash_ = h.calculateHash(self.session.hash_algo, file_)
|
|
|
|
if not hash_:
|
|
|
|
# Hash alogrithm not supported
|
|
|
|
return
|
2012-01-24 16:04:14 +01:00
|
|
|
self.file_props['hash'] = hash_
|
|
|
|
h.addHash(hash_, self.session.hash_algo)
|
2012-01-16 01:37:00 +01:00
|
|
|
checksum = xmpp.Node(tag='checksum',
|
|
|
|
payload=[xmpp.Node(tag='file', payload=[h])])
|
|
|
|
checksum.setNamespace(xmpp.NS_JINGLE_FILE_TRANSFER)
|
|
|
|
# Send hash in a session info
|
|
|
|
self.session.__session_info(checksum )
|
|
|
|
|
|
|
|
|
2010-06-06 17:22:52 +02:00
|
|
|
def __on_session_accept(self, stanza, content, error, action):
|
|
|
|
log.info("__on_session_accept")
|
2011-07-22 22:15:34 +02:00
|
|
|
con = self.session.connection
|
2010-07-22 10:20:14 +02:00
|
|
|
security = content.getTag('security')
|
|
|
|
if not security: # responder can not verify our fingerprint
|
|
|
|
self.use_security = False
|
2011-08-24 10:42:16 +02:00
|
|
|
|
|
|
|
|
2011-06-29 00:31:07 +02:00
|
|
|
if self.state == STATE_TRANSPORT_REPLACE:
|
|
|
|
# We ack the session accept
|
|
|
|
response = stanza.buildReply('result')
|
2011-12-29 14:08:35 +01:00
|
|
|
response.delChild(response.getQuery())
|
2011-06-29 00:31:07 +02:00
|
|
|
con.connection.send(response)
|
|
|
|
# We send the file
|
2012-01-07 05:18:50 +01:00
|
|
|
self.__start_IBB_transfer(con)
|
2011-06-29 00:31:07 +02:00
|
|
|
raise xmpp.NodeProcessed
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-07-22 22:15:34 +02:00
|
|
|
self.file_props['streamhosts'] = self.transport.remote_candidates
|
|
|
|
for host in self.file_props['streamhosts']:
|
2012-01-08 20:44:15 +01:00
|
|
|
host['initiator'] = self.session.initiator
|
|
|
|
host['target'] = self.session.responder
|
|
|
|
host['sid'] = self.file_props['sid']
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-07-22 22:15:34 +02:00
|
|
|
response = stanza.buildReply('result')
|
2011-12-29 14:08:35 +01:00
|
|
|
response.delChild(response.getQuery())
|
2011-07-22 22:15:34 +02:00
|
|
|
con.connection.send(response)
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-07-22 22:15:34 +02:00
|
|
|
if not gajim.socks5queue.get_file_props(
|
|
|
|
self.session.connection.name, self.file_props['sid']):
|
|
|
|
gajim.socks5queue.add_file_props(self.session.connection.name,
|
2012-01-08 20:44:15 +01:00
|
|
|
self.file_props)
|
2011-07-22 22:15:34 +02:00
|
|
|
fingerprint = None
|
|
|
|
if self.use_security:
|
|
|
|
fingerprint = 'client'
|
2012-01-07 05:18:50 +01:00
|
|
|
if self.transport.type == TransportType.SOCKS5:
|
|
|
|
gajim.socks5queue.connect_to_hosts(self.session.connection.name,
|
2012-01-08 20:44:15 +01:00
|
|
|
self.file_props['sid'], self.send_candidate_used,
|
|
|
|
self._on_connect_error, fingerprint=fingerprint,
|
|
|
|
receiving=False)
|
2012-01-07 05:18:50 +01:00
|
|
|
elif self.transport.type == TransportType.IBB:
|
|
|
|
self.state = STATE_TRANSFERING
|
|
|
|
self.__start_IBB_transfer(self.session.connection)
|
2011-07-22 22:15:34 +02:00
|
|
|
raise xmpp.NodeProcessed
|
2010-06-06 17:22:52 +02:00
|
|
|
|
|
|
|
def __on_session_terminate(self, stanza, content, error, action):
|
|
|
|
log.info("__on_session_terminate")
|
|
|
|
|
2012-01-16 01:37:00 +01:00
|
|
|
def __on_session_info(self, stanza, content, error, action):
|
|
|
|
pass
|
|
|
|
|
2010-06-06 17:22:52 +02:00
|
|
|
def __on_transport_accept(self, stanza, content, error, action):
|
|
|
|
log.info("__on_transport_accept")
|
|
|
|
|
|
|
|
def __on_transport_replace(self, stanza, content, error, action):
|
|
|
|
log.info("__on_transport_replace")
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-06-06 17:22:52 +02:00
|
|
|
def __on_transport_reject(self, stanza, content, error, action):
|
|
|
|
log.info("__on_transport_reject")
|
|
|
|
|
|
|
|
def __on_transport_info(self, stanza, content, error, action):
|
|
|
|
log.info("__on_transport_info")
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2011-07-05 20:05:16 +02:00
|
|
|
if content.getTag('transport').getTag('candidate-error'):
|
2011-08-15 05:59:39 +02:00
|
|
|
self.nominated_cand['peer-cand'] = False
|
|
|
|
if self.state == STATE_CAND_SENT_PENDING_REPLY:
|
|
|
|
if not self.nominated_cand['our-cand'] and \
|
|
|
|
not self.nominated_cand['peer-cand']:
|
|
|
|
if not self.weinitiate:
|
|
|
|
return
|
|
|
|
self.session.transport_replace()
|
2011-08-24 23:53:36 +02:00
|
|
|
else:
|
|
|
|
response = stanza.buildReply('result')
|
2011-12-29 14:08:35 +01:00
|
|
|
response.delChild(response.getQuery())
|
2011-08-24 23:53:36 +02:00
|
|
|
self.session.connection.connection.send(response)
|
|
|
|
self.start_transfer()
|
|
|
|
raise xmpp.NodeProcessed
|
2011-08-15 05:59:39 +02:00
|
|
|
else:
|
|
|
|
self.state = STATE_CAND_RECEIVED_PENDING_REPLY
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-07-05 20:05:16 +02:00
|
|
|
return
|
2011-10-29 06:09:45 +02:00
|
|
|
|
|
|
|
if content.getTag('transport').getTag('activated'):
|
|
|
|
self.state = STATE_TRANSFERING
|
|
|
|
jid = gajim.get_jid_without_resource(self.session.ourjid)
|
|
|
|
gajim.socks5queue.send_file(self.file_props,
|
|
|
|
self.session.connection.name, 'client')
|
|
|
|
return
|
|
|
|
|
2010-08-24 23:12:34 +02:00
|
|
|
streamhost_cid = content.getTag('transport').getTag('candidate-used').\
|
|
|
|
getAttr('cid')
|
2010-07-13 04:38:31 +02:00
|
|
|
streamhost_used = None
|
|
|
|
for cand in self.transport.candidates:
|
|
|
|
if cand['candidate_id'] == streamhost_cid:
|
|
|
|
streamhost_used = cand
|
|
|
|
break
|
|
|
|
if streamhost_used == None:
|
|
|
|
log.info("unknow streamhost")
|
|
|
|
return
|
2011-08-15 05:59:39 +02:00
|
|
|
# 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')
|
2011-12-29 14:08:35 +01:00
|
|
|
response.delChild(response.getQuery())
|
2011-08-15 05:59:39 +02:00
|
|
|
self.session.connection.connection.send(response)
|
2011-08-24 10:42:16 +02:00
|
|
|
self.start_transfer()
|
2011-08-15 05:59:39 +02:00
|
|
|
raise xmpp.NodeProcessed
|
2010-07-13 04:38:31 +02:00
|
|
|
else:
|
2011-08-15 05:59:39 +02:00
|
|
|
self.state = STATE_CAND_RECEIVED_PENDING_REPLY
|
2011-08-24 10:42:16 +02:00
|
|
|
|
|
|
|
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-07-03 10:22:47 +02:00
|
|
|
def __on_iq_result(self, stanza, content, error, action):
|
|
|
|
log.info("__on_iq_result")
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-07-15 07:38:53 +02:00
|
|
|
if self.weinitiate and self.state == STATE_NOT_STARTED:
|
|
|
|
self.state = STATE_INITIALIZED
|
2010-08-24 23:12:34 +02:00
|
|
|
self.session.connection.files_props[self.file_props['sid']] = \
|
|
|
|
self.file_props
|
2011-08-09 01:43:07 +02:00
|
|
|
# Listen on configured port for file transfer
|
|
|
|
self._listen_host()
|
2012-01-07 05:18:50 +01:00
|
|
|
|
2010-08-24 23:12:34 +02:00
|
|
|
elif not self.weinitiate and self.state == STATE_NOT_STARTED:
|
|
|
|
# session-accept iq-result
|
2010-08-26 16:56:08 +02:00
|
|
|
if not self.negotiated:
|
2010-08-26 13:18:16 +02:00
|
|
|
return
|
2010-07-15 07:38:53 +02:00
|
|
|
self.state = STATE_ACCEPTED
|
2010-08-24 23:12:34 +02:00
|
|
|
if not gajim.socks5queue.get_file_props(
|
|
|
|
self.session.connection.name, self.file_props['sid']):
|
|
|
|
gajim.socks5queue.add_file_props(self.session.connection.name,
|
|
|
|
self.file_props)
|
2010-08-24 23:24:18 +02:00
|
|
|
fingerprint = None
|
|
|
|
if self.use_security:
|
|
|
|
fingerprint = 'client'
|
2010-08-24 23:12:34 +02:00
|
|
|
gajim.socks5queue.connect_to_hosts(self.session.connection.name,
|
|
|
|
self.file_props['sid'], self.send_candidate_used,
|
2010-08-24 23:24:18 +02:00
|
|
|
self._on_connect_error, fingerprint=fingerprint)
|
2010-08-24 23:12:34 +02:00
|
|
|
elif not self.weinitiate and self.state == STATE_ACCEPTED:
|
|
|
|
# transport-info iq-result
|
2010-07-15 07:38:53 +02:00
|
|
|
self.state = STATE_TRANSPORT_INFO
|
2010-08-24 23:12:34 +02:00
|
|
|
elif self.weinitiate and self.state == STATE_INITIALIZED:
|
|
|
|
# proxy activated
|
2010-07-15 07:38:53 +02:00
|
|
|
self.state = STATE_PROXY_ACTIVATED
|
2011-08-15 05:59:39 +02:00
|
|
|
elif self.state == STATE_CAND_SENT_AND_RECEIVED:
|
|
|
|
if not self.nominated_cand['our-cand'] and \
|
2011-08-24 10:42:16 +02:00
|
|
|
not self.nominated_cand['peer-cand']:
|
|
|
|
if not self.weinitiate:
|
2011-08-15 05:59:39 +02:00
|
|
|
return
|
2011-08-24 10:42:16 +02:00
|
|
|
self.session.transport_replace()
|
|
|
|
return
|
2011-08-15 05:59:39 +02:00
|
|
|
# initiate transfer
|
2011-08-24 10:42:16 +02:00
|
|
|
self.start_transfer()
|
2012-01-07 05:18:50 +01:00
|
|
|
|
|
|
|
def __start_IBB_transfer(self, con):
|
|
|
|
con.files_props[self.file_props['sid']] = self.file_props
|
|
|
|
fp = open(self.file_props['file-name'], 'r')
|
2012-01-08 20:44:15 +01:00
|
|
|
con.OpenStream( self.transport.sid, self.session.peerjid, fp,
|
|
|
|
blocksize=4096)
|
2012-01-07 05:18:50 +01:00
|
|
|
|
2012-01-08 20:44:15 +01:00
|
|
|
def __transport_setup(self, stanza=None, content=None, error=None,
|
|
|
|
action=None):
|
2012-01-07 05:18:50 +01:00
|
|
|
# Sets up a few transport specific things for the file transfer
|
|
|
|
if self.transport.type == TransportType.SOCKS5:
|
|
|
|
self._listen_host()
|
|
|
|
|
|
|
|
if self.transport.type == TransportType.IBB:
|
|
|
|
self.state = STATE_TRANSFERING
|
|
|
|
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2010-07-05 07:46:53 +02:00
|
|
|
def send_candidate_used(self, streamhost):
|
|
|
|
"""
|
|
|
|
send candidate-used stanza
|
|
|
|
"""
|
2010-08-24 23:12:34 +02:00
|
|
|
log.info('send_candidate_used')
|
2010-07-05 07:46:53 +02:00
|
|
|
if streamhost is None:
|
|
|
|
return
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
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
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2010-07-05 07:46:53 +02:00
|
|
|
content = xmpp.Node('content')
|
|
|
|
content.setAttr('creator', 'initiator')
|
2010-08-26 10:36:58 +02:00
|
|
|
content.setAttr('name', self.name)
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-07-05 07:46:53 +02:00
|
|
|
transport = xmpp.Node('transport')
|
2010-08-25 12:05:14 +02:00
|
|
|
transport.setNamespace(xmpp.NS_JINGLE_BYTESTREAM)
|
|
|
|
transport.setAttr('sid', self.transport.sid)
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-07-05 07:46:53 +02:00
|
|
|
candidateused = xmpp.Node('candidate-used')
|
|
|
|
candidateused.setAttr('cid', streamhost['cid'])
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-07-05 07:46:53 +02:00
|
|
|
transport.addChild(node=candidateused)
|
|
|
|
content.addChild(node=transport)
|
2010-06-06 17:22:52 +02:00
|
|
|
|
2010-07-05 07:46:53 +02:00
|
|
|
self.session.send_transport_info(content)
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
|
2011-10-29 06:09:45 +02:00
|
|
|
def _on_connect_error(self, sid):
|
2011-08-15 05:59:39 +02:00
|
|
|
self.nominated_cand['our-cand'] = False
|
|
|
|
self.send_error_candidate()
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
if self.state == STATE_CAND_RECEIVED_PENDING_REPLY:
|
|
|
|
self.state = STATE_CAND_SENT_AND_RECEIVED
|
|
|
|
else:
|
|
|
|
self.state = STATE_CAND_SENT_PENDING_REPLY
|
2011-08-24 10:42:16 +02:00
|
|
|
|
|
|
|
|
2010-08-24 23:12:34 +02:00
|
|
|
log.info('connect error, sid=' + sid)
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2010-06-06 17:22:52 +02:00
|
|
|
def _fill_content(self, content):
|
2010-08-24 23:12:34 +02:00
|
|
|
description_node = xmpp.simplexml.Node(
|
|
|
|
tag=xmpp.NS_JINGLE_FILE_TRANSFER + ' description')
|
2010-06-08 10:39:44 +02:00
|
|
|
|
|
|
|
sioffer = xmpp.simplexml.Node(tag='offer')
|
|
|
|
file_tag = sioffer.setTag('file', namespace=xmpp.NS_FILE)
|
|
|
|
file_tag.setAttr('name', self.file_props['name'])
|
|
|
|
file_tag.setAttr('size', self.file_props['size'])
|
|
|
|
desc = file_tag.setTag('desc')
|
|
|
|
if 'desc' in self.file_props:
|
|
|
|
desc.setData(self.file_props['desc'])
|
|
|
|
|
|
|
|
description_node.addChild(node=sioffer)
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-07-22 09:05:06 +02:00
|
|
|
if self.use_security:
|
2010-08-24 23:12:34 +02:00
|
|
|
security = xmpp.simplexml.Node(
|
|
|
|
tag=xmpp.NS_JINGLE_XTLS + ' security')
|
2010-07-22 09:05:06 +02:00
|
|
|
# TODO: add fingerprint element
|
|
|
|
for m in ('x509', ): # supported authentication methods
|
|
|
|
method = xmpp.simplexml.Node(tag='method')
|
|
|
|
method.setAttr('name', m)
|
|
|
|
security.addChild(node=method)
|
|
|
|
content.addChild(node=security)
|
2010-06-08 10:39:44 +02:00
|
|
|
|
|
|
|
content.addChild(node=description_node)
|
2010-08-24 23:12:34 +02:00
|
|
|
|
2010-07-03 10:22:47 +02:00
|
|
|
def _store_socks5_sid(self, sid, hash_id):
|
|
|
|
# callback from socsk5queue.start_listener
|
|
|
|
self.file_props['hash'] = hash_id
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2012-01-07 05:18:50 +01:00
|
|
|
def _listen_host(self):
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
receiver = self.file_props['receiver']
|
|
|
|
sender = self.file_props['sender']
|
|
|
|
sha_str = helpers.get_auth_sha(self.file_props['sid'], sender,
|
2012-01-08 20:44:15 +01:00
|
|
|
receiver)
|
2011-08-09 01:43:07 +02:00
|
|
|
self.file_props['sha_str'] = sha_str
|
|
|
|
|
|
|
|
port = gajim.config.get('file_transfers_port')
|
|
|
|
|
|
|
|
fingerprint = None
|
|
|
|
if self.use_security:
|
|
|
|
fingerprint = 'server'
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
if self.weinitiate:
|
|
|
|
listener = gajim.socks5queue.start_listener(port, sha_str,
|
2012-01-08 20:44:15 +01:00
|
|
|
self._store_socks5_sid, self.file_props,
|
|
|
|
fingerprint=fingerprint, type='sender')
|
2011-08-09 01:43:07 +02:00
|
|
|
else:
|
|
|
|
listener = gajim.socks5queue.start_listener(port, sha_str,
|
2012-01-08 20:44:15 +01:00
|
|
|
self._store_socks5_sid, self.file_props,
|
|
|
|
fingerprint=fingerprint, type='receiver')
|
2011-08-09 01:43:07 +02:00
|
|
|
|
|
|
|
if not listener:
|
2012-01-08 20:44:15 +01:00
|
|
|
# send error message, notify the user
|
2011-08-09 01:43:07 +02:00
|
|
|
return
|
2011-08-15 05:59:39 +02:00
|
|
|
def isOurCandUsed(self):
|
2011-08-22 01:02:58 +02:00
|
|
|
'''
|
|
|
|
If this method returns true then the candidate we nominated will be
|
|
|
|
used, if false, the candidate nominated by peer will be used
|
|
|
|
'''
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
if self.nominated_cand['peer-cand'] == False:
|
|
|
|
return True
|
|
|
|
if self.nominated_cand['our-cand'] == False:
|
|
|
|
return False
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
peer_pr = int(self.nominated_cand['peer-cand']['priority'])
|
|
|
|
our_pr = int(self.nominated_cand['our-cand']['priority'])
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
if peer_pr != our_pr:
|
2011-08-23 05:57:29 +02:00
|
|
|
return our_pr > peer_pr
|
2011-08-15 05:59:39 +02:00
|
|
|
else:
|
2011-08-22 01:02:58 +02:00
|
|
|
return self.weinitiate
|
2011-08-24 10:42:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def start_transfer(self):
|
2012-01-16 01:37:00 +01:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
self.state = STATE_TRANSFERING
|
2011-10-29 20:31:06 +02:00
|
|
|
|
|
|
|
# It tells wether we start the transfer as client or server
|
2011-12-23 03:52:37 +01:00
|
|
|
mode = None
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
if self.isOurCandUsed():
|
2011-12-23 03:52:37 +01:00
|
|
|
mode = 'client'
|
2011-08-24 10:42:16 +02:00
|
|
|
streamhost_used = self.nominated_cand['our-cand']
|
2011-08-15 05:59:39 +02:00
|
|
|
else:
|
2011-12-23 03:52:37 +01:00
|
|
|
mode = 'server'
|
2011-08-24 10:42:16 +02:00
|
|
|
streamhost_used = self.nominated_cand['peer-cand']
|
2011-10-29 06:09:45 +02:00
|
|
|
|
|
|
|
if streamhost_used['type'] == 'proxy':
|
|
|
|
self.file_props['is_a_proxy'] = True
|
2011-11-02 00:09:33 +01:00
|
|
|
if self.weinitiate:
|
|
|
|
self.file_props['proxy_sender'] = streamhost_used['initiator']
|
|
|
|
self.file_props['proxy_receiver'] = streamhost_used['target']
|
|
|
|
else:
|
|
|
|
self.file_props['proxy_sender'] = streamhost_used['target']
|
|
|
|
self.file_props['proxy_receiver'] = streamhost_used['initiator']
|
2011-10-29 06:09:45 +02:00
|
|
|
|
|
|
|
if not self.weinitiate and streamhost_used['type'] == 'proxy':
|
|
|
|
r = gajim.socks5queue.readers
|
|
|
|
for reader in r:
|
|
|
|
if r[reader].host == streamhost_used['host'] and \
|
|
|
|
r[reader].connected:
|
|
|
|
return
|
2011-11-02 00:09:33 +01:00
|
|
|
|
|
|
|
if self.weinitiate and streamhost_used['type'] == 'proxy':
|
|
|
|
s = gajim.socks5queue.senders
|
|
|
|
for sender in s:
|
|
|
|
if s[sender].host == streamhost_used['host'] and \
|
|
|
|
s[sender].connected:
|
|
|
|
return
|
|
|
|
|
2011-10-29 06:09:45 +02:00
|
|
|
if streamhost_used['type'] == 'proxy':
|
2011-08-15 05:59:39 +02:00
|
|
|
self.file_props['streamhost-used'] = True
|
2011-10-29 06:09:45 +02:00
|
|
|
streamhost_used['sid'] = self.file_props['sid']
|
|
|
|
self.file_props['streamhosts'] = []
|
|
|
|
self.file_props['streamhosts'].append(streamhost_used)
|
|
|
|
self.file_props['proxyhosts'] = []
|
|
|
|
self.file_props['proxyhosts'].append(streamhost_used)
|
2011-11-02 00:09:33 +01:00
|
|
|
|
|
|
|
if self.weinitiate:
|
|
|
|
gajim.socks5queue.idx += 1
|
|
|
|
idx = gajim.socks5queue.idx
|
2011-11-18 05:55:44 +01:00
|
|
|
sockobj = Socks5SenderClient(gajim.idlequeue, idx,
|
2012-01-08 20:44:15 +01:00
|
|
|
gajim.socks5queue, _sock=None,
|
|
|
|
host=str(streamhost_used['host']),
|
|
|
|
port=int(streamhost_used['port']), fingerprint=None,
|
|
|
|
connected=False, file_props=self.file_props)
|
2011-11-02 00:09:33 +01:00
|
|
|
else:
|
2011-11-18 05:55:44 +01:00
|
|
|
sockobj = Socks5ReceiverClient(gajim.idlequeue, streamhost_used,
|
|
|
|
sid=self.file_props['sid'],
|
2011-11-02 00:09:33 +01:00
|
|
|
file_props=self.file_props, fingerprint=None)
|
2011-10-29 06:09:45 +02:00
|
|
|
sockobj.proxy = True
|
2012-01-08 20:44:15 +01:00
|
|
|
sockobj.streamhost = streamhost_used
|
|
|
|
gajim.socks5queue.add_sockobj(self.session.connection.name,
|
|
|
|
sockobj, 'sender')
|
2011-10-29 06:09:45 +02:00
|
|
|
streamhost_used['idx'] = sockobj.queue_idx
|
|
|
|
# If we offered the nominated candidate used, we activate
|
|
|
|
# the proxy
|
|
|
|
if not self.isOurCandUsed():
|
2011-08-15 05:59:39 +02:00
|
|
|
gajim.socks5queue.on_success[self.file_props['sid']] = \
|
2011-10-29 06:09:45 +02:00
|
|
|
self.transport._on_proxy_auth_ok
|
|
|
|
# TODO: add on failure
|
2011-08-15 05:59:39 +02:00
|
|
|
else:
|
|
|
|
jid = gajim.get_jid_without_resource(self.session.ourjid)
|
|
|
|
gajim.socks5queue.send_file(self.file_props,
|
2011-12-23 03:52:37 +01:00
|
|
|
self.session.connection.name, mode)
|
2011-08-24 10:42:16 +02:00
|
|
|
|
2010-06-08 10:39:44 +02:00
|
|
|
def get_content(desc):
|
|
|
|
return JingleFileTransfer
|
|
|
|
|
|
|
|
contents[xmpp.NS_JINGLE_FILE_TRANSFER] = get_content
|