JingleConnection.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.util.Base64;
   4import android.util.Log;
   5import android.util.Pair;
   6
   7import java.io.FileInputStream;
   8import java.io.FileNotFoundException;
   9import java.io.IOException;
  10import java.io.InputStream;
  11import java.io.OutputStream;
  12import java.util.ArrayList;
  13import java.util.Arrays;
  14import java.util.Iterator;
  15import java.util.List;
  16import java.util.Locale;
  17import java.util.Map.Entry;
  18import java.util.concurrent.ConcurrentHashMap;
  19
  20import eu.siacs.conversations.Config;
  21import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  22import eu.siacs.conversations.crypto.axolotl.OnMessageCreatedCallback;
  23import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
  24import eu.siacs.conversations.entities.Account;
  25import eu.siacs.conversations.entities.Conversation;
  26import eu.siacs.conversations.entities.DownloadableFile;
  27import eu.siacs.conversations.entities.Message;
  28import eu.siacs.conversations.entities.Presence;
  29import eu.siacs.conversations.entities.ServiceDiscoveryResult;
  30import eu.siacs.conversations.entities.Transferable;
  31import eu.siacs.conversations.entities.TransferablePlaceholder;
  32import eu.siacs.conversations.parser.IqParser;
  33import eu.siacs.conversations.persistance.FileBackend;
  34import eu.siacs.conversations.services.AbstractConnectionManager;
  35import eu.siacs.conversations.services.XmppConnectionService;
  36import eu.siacs.conversations.utils.CryptoHelper;
  37import eu.siacs.conversations.xml.Element;
  38import eu.siacs.conversations.xmpp.OnIqPacketReceived;
  39import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
  40import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  41import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  42import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  43import rocks.xmpp.addr.Jid;
  44
  45public class JingleConnection implements Transferable {
  46
  47	private JingleConnectionManager mJingleConnectionManager;
  48	private XmppConnectionService mXmppConnectionService;
  49
  50	private static final int JINGLE_STATUS_OFFERED = -1;
  51	protected static final int JINGLE_STATUS_INITIATED = 0;
  52	protected static final int JINGLE_STATUS_ACCEPTED = 1;
  53	protected static final int JINGLE_STATUS_FINISHED = 4;
  54	protected static final int JINGLE_STATUS_TRANSMITTING = 5;
  55	protected static final int JINGLE_STATUS_FAILED = 99;
  56
  57	private Content.Version ftVersion = Content.Version.FT_3;
  58
  59	private int ibbBlockSize = 8192;
  60
  61	private int mJingleStatus = JINGLE_STATUS_OFFERED;
  62	private int mStatus = Transferable.STATUS_UNKNOWN;
  63	private Message message;
  64	private String sessionId;
  65	private Account account;
  66	private Jid initiator;
  67	private Jid responder;
  68	private List<JingleCandidate> candidates = new ArrayList<>();
  69	private ConcurrentHashMap<String, JingleSocks5Transport> connections = new ConcurrentHashMap<>();
  70
  71	private String transportId;
  72	private Element fileOffer;
  73	private DownloadableFile file = null;
  74
  75	private String contentName;
  76	private String contentCreator;
  77
  78	private int mProgress = 0;
  79
  80	private boolean receivedCandidate = false;
  81	private boolean sentCandidate = false;
  82
  83	private boolean acceptedAutomatically = false;
  84	private boolean cancelled = false;
  85
  86	private XmppAxolotlMessage mXmppAxolotlMessage;
  87
  88	private JingleTransport transport = null;
  89
  90	private OutputStream mFileOutputStream;
  91	private InputStream mFileInputStream;
  92
  93	private OnIqPacketReceived responseListener = (account, packet) -> {
  94		if (packet.getType() != IqPacket.TYPE.RESULT) {
  95			fail(IqParser.extractErrorMessage(packet));
  96		}
  97	};
  98	private byte[] expectedHash = new byte[0];
  99
 100	private boolean responding() {
 101		return responder != null && responder.equals(account.getJid());
 102	}
 103
 104	private boolean initiating() {
 105		return initiator.equals(account.getJid());
 106	}
 107
 108	final OnFileTransmissionStatusChanged onFileTransmissionStatusChanged = new OnFileTransmissionStatusChanged() {
 109
 110		@Override
 111		public void onFileTransmitted(DownloadableFile file) {
 112			if (responding()) {
 113				if (expectedHash.length > 0 && !Arrays.equals(expectedHash,file.getSha1Sum())) {
 114					Log.d(Config.LOGTAG,account.getJid().asBareJid()+": hashes did not match");
 115				}
 116				sendSuccess();
 117				mXmppConnectionService.getFileBackend().updateFileParams(message);
 118				mXmppConnectionService.databaseBackend.createMessage(message);
 119				mXmppConnectionService.markMessage(message,Message.STATUS_RECEIVED);
 120				if (acceptedAutomatically) {
 121					message.markUnread();
 122					if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 123						account.getPgpDecryptionService().decrypt(message, true);
 124					} else {
 125						JingleConnection.this.mXmppConnectionService.getNotificationService().push(message);
 126					}
 127				}
 128			} else {
 129				if (ftVersion == Content.Version.FT_5) { //older Conversations will break when receiving a session-info
 130					sendHash();
 131				}
 132				if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 133					account.getPgpDecryptionService().decrypt(message, false);
 134				}
 135				if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
 136					file.delete();
 137				}
 138			}
 139			Log.d(Config.LOGTAG,"successfully transmitted file:" + file.getAbsolutePath()+" ("+ CryptoHelper.bytesToHex(file.getSha1Sum())+")");
 140			if (message.getEncryption() != Message.ENCRYPTION_PGP) {
 141				mXmppConnectionService.getFileBackend().updateMediaScanner(file);
 142			}
 143		}
 144
 145		@Override
 146		public void onFileTransferAborted() {
 147			JingleConnection.this.sendCancel();
 148			JingleConnection.this.fail();
 149		}
 150	};
 151
 152	InputStream getFileInputStream() {
 153		return this.mFileInputStream;
 154	}
 155
 156	OutputStream getFileOutputStream() throws IOException {
 157		if (this.file == null) {
 158			Log.d(Config.LOGTAG,"file object was not assigned");
 159			return null;
 160		}
 161		this.file.getParentFile().mkdirs();
 162		this.file.createNewFile();
 163		this.mFileOutputStream = AbstractConnectionManager.createOutputStream(this.file);
 164		return this.mFileOutputStream;
 165	}
 166
 167	private OnProxyActivated onProxyActivated = new OnProxyActivated() {
 168
 169		@Override
 170		public void success() {
 171			if (initiator.equals(account.getJid())) {
 172				Log.d(Config.LOGTAG, "we were initiating. sending file");
 173				transport.send(file, onFileTransmissionStatusChanged);
 174			} else {
 175				transport.receive(file, onFileTransmissionStatusChanged);
 176				Log.d(Config.LOGTAG, "we were responding. receiving file");
 177			}
 178		}
 179
 180		@Override
 181		public void failed() {
 182			Log.d(Config.LOGTAG, "proxy activation failed");
 183		}
 184	};
 185
 186	public JingleConnection(JingleConnectionManager mJingleConnectionManager) {
 187		this.mJingleConnectionManager = mJingleConnectionManager;
 188		this.mXmppConnectionService = mJingleConnectionManager
 189				.getXmppConnectionService();
 190	}
 191
 192	public String getSessionId() {
 193		return this.sessionId;
 194	}
 195
 196	public Account getAccount() {
 197		return this.account;
 198	}
 199
 200	public Jid getCounterPart() {
 201		return this.message.getCounterpart();
 202	}
 203
 204	public void deliverPacket(JinglePacket packet) {
 205		boolean returnResult = true;
 206		if (packet.isAction("session-terminate")) {
 207			Reason reason = packet.getReason();
 208			if (reason != null) {
 209				if (reason.hasChild("cancel")) {
 210					this.fail();
 211				} else if (reason.hasChild("success")) {
 212					this.receiveSuccess();
 213				} else {
 214					this.fail();
 215				}
 216			} else {
 217				this.fail();
 218			}
 219		} else if (packet.isAction("session-accept")) {
 220			returnResult = receiveAccept(packet);
 221		} else if (packet.isAction("session-info")) {
 222			returnResult = true;
 223			Element checksum = packet.getChecksum();
 224			Element file = checksum == null ? null : checksum.findChild("file");
 225			Element hash = file == null ? null : file.findChild("hash","urn:xmpp:hashes:2");
 226			if (hash != null && "sha-1".equalsIgnoreCase(hash.getAttribute("algo"))) {
 227				try {
 228					this.expectedHash = Base64.decode(hash.getContent(), Base64.DEFAULT);
 229				} catch (Exception e) {
 230					this.expectedHash = new byte[0];
 231				}
 232			}
 233		} else if (packet.isAction("transport-info")) {
 234			returnResult = receiveTransportInfo(packet);
 235		} else if (packet.isAction("transport-replace")) {
 236			if (packet.getJingleContent().hasIbbTransport()) {
 237				returnResult = this.receiveFallbackToIbb(packet);
 238			} else {
 239				returnResult = false;
 240				Log.d(Config.LOGTAG, "trying to fallback to something unknown"
 241						+ packet.toString());
 242			}
 243		} else if (packet.isAction("transport-accept")) {
 244			returnResult = this.receiveTransportAccept(packet);
 245		} else {
 246			Log.d(Config.LOGTAG, "packet arrived in connection. action was "
 247					+ packet.getAction());
 248			returnResult = false;
 249		}
 250		IqPacket response;
 251		if (returnResult) {
 252			response = packet.generateResponse(IqPacket.TYPE.RESULT);
 253
 254		} else {
 255			response = packet.generateResponse(IqPacket.TYPE.ERROR);
 256		}
 257		mXmppConnectionService.sendIqPacket(account,response,null);
 258	}
 259
 260	public void init(final Message message) {
 261		if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 262			Conversation conversation = (Conversation) message.getConversation();
 263			conversation.getAccount().getAxolotlService().prepareKeyTransportMessage(conversation, new OnMessageCreatedCallback() {
 264				@Override
 265				public void run(XmppAxolotlMessage xmppAxolotlMessage) {
 266					if (xmppAxolotlMessage != null) {
 267						init(message, xmppAxolotlMessage);
 268					} else {
 269						fail();
 270					}
 271				}
 272			});
 273		} else {
 274			init(message, null);
 275		}
 276	}
 277
 278	private void init(Message message, XmppAxolotlMessage xmppAxolotlMessage) {
 279		this.mXmppAxolotlMessage = xmppAxolotlMessage;
 280		this.contentCreator = "initiator";
 281		this.contentName = this.mJingleConnectionManager.nextRandomId();
 282		this.message = message;
 283		this.account = message.getConversation().getAccount();
 284		upgradeNamespace();
 285		this.message.setTransferable(this);
 286		this.mStatus = Transferable.STATUS_UPLOADING;
 287		this.initiator = this.account.getJid();
 288		this.responder = this.message.getCounterpart();
 289		this.sessionId = this.mJingleConnectionManager.nextRandomId();
 290		this.transportId = this.mJingleConnectionManager.nextRandomId();
 291		if (this.candidates.size() > 0) {
 292			this.sendInitRequest();
 293		} else {
 294			this.mJingleConnectionManager.getPrimaryCandidate(account,
 295					new OnPrimaryCandidateFound() {
 296
 297						@Override
 298						public void onPrimaryCandidateFound(boolean success,
 299															final JingleCandidate candidate) {
 300							if (success) {
 301								final JingleSocks5Transport socksConnection = new JingleSocks5Transport(
 302										JingleConnection.this, candidate);
 303								connections.put(candidate.getCid(),
 304										socksConnection);
 305								socksConnection
 306										.connect(new OnTransportConnected() {
 307
 308											@Override
 309											public void failed() {
 310												Log.d(Config.LOGTAG,
 311														"connection to our own primary candidete failed");
 312												sendInitRequest();
 313											}
 314
 315											@Override
 316											public void established() {
 317												Log.d(Config.LOGTAG,
 318														"successfully connected to our own primary candidate");
 319												mergeCandidate(candidate);
 320												sendInitRequest();
 321											}
 322										});
 323								mergeCandidate(candidate);
 324							} else {
 325								Log.d(Config.LOGTAG, "no primary candidate of our own was found");
 326								sendInitRequest();
 327							}
 328						}
 329					});
 330		}
 331
 332	}
 333
 334	private void upgradeNamespace() {
 335		Jid jid = this.message.getCounterpart();
 336		String resource = jid != null ?jid.getResource() : null;
 337		if (resource != null) {
 338			Presence presence = this.account.getRoster().getContact(jid).getPresences().getPresences().get(resource);
 339			ServiceDiscoveryResult result = presence != null ? presence.getServiceDiscoveryResult() : null;
 340			if (result != null) {
 341				List<String> features = result.getFeatures();
 342				if (features.contains(Content.Version.FT_5.getNamespace())) {
 343					this.ftVersion = Content.Version.FT_5;
 344				} else if (features.contains(Content.Version.FT_4.getNamespace())) {
 345					this.ftVersion = Content.Version.FT_4;
 346				}
 347			}
 348		}
 349	}
 350
 351	public void init(Account account, JinglePacket packet) {
 352		this.mJingleStatus = JINGLE_STATUS_INITIATED;
 353		Conversation conversation = this.mXmppConnectionService
 354				.findOrCreateConversation(account,
 355						packet.getFrom().asBareJid(), false, false);
 356		this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
 357		this.message.setStatus(Message.STATUS_RECEIVED);
 358		this.mStatus = Transferable.STATUS_OFFER;
 359		this.message.setTransferable(this);
 360        final Jid from = packet.getFrom();
 361		this.message.setCounterpart(from);
 362		this.account = account;
 363		this.initiator = packet.getFrom();
 364		this.responder = this.account.getJid();
 365		this.sessionId = packet.getSessionId();
 366		Content content = packet.getJingleContent();
 367		this.contentCreator = content.getAttribute("creator");
 368		this.contentName = content.getAttribute("name");
 369		this.transportId = content.getTransportId();
 370		this.mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
 371		this.ftVersion = content.getVersion();
 372		if (ftVersion == null) {
 373			this.sendCancel();
 374			this.fail();
 375			return;
 376		}
 377		this.fileOffer = content.getFileOffer(this.ftVersion);
 378
 379		mXmppConnectionService.sendIqPacket(account,packet.generateResponse(IqPacket.TYPE.RESULT),null);
 380
 381		if (fileOffer != null) {
 382			Element encrypted = fileOffer.findChild("encrypted", AxolotlService.PEP_PREFIX);
 383			if (encrypted != null) {
 384				this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().asBareJid());
 385			}
 386			Element fileSize = fileOffer.findChild("size");
 387			Element fileNameElement = fileOffer.findChild("name");
 388			if (fileNameElement != null) {
 389				String[] filename = fileNameElement.getContent()
 390						.toLowerCase(Locale.US).toLowerCase().split("\\.");
 391				String extension = filename[filename.length - 1];
 392				if (VALID_IMAGE_EXTENSIONS.contains(extension)) {
 393					message.setType(Message.TYPE_IMAGE);
 394					message.setRelativeFilePath(message.getUuid()+"."+extension);
 395				} else if (VALID_CRYPTO_EXTENSIONS.contains(
 396						filename[filename.length - 1])) {
 397					if (filename.length == 3) {
 398						extension = filename[filename.length - 2];
 399						if (VALID_IMAGE_EXTENSIONS.contains(extension)) {
 400							message.setType(Message.TYPE_IMAGE);
 401							message.setRelativeFilePath(message.getUuid()+"."+extension);
 402						} else {
 403							message.setType(Message.TYPE_FILE);
 404						}
 405						message.setEncryption(Message.ENCRYPTION_PGP);
 406					}
 407				} else {
 408					message.setType(Message.TYPE_FILE);
 409				}
 410				if (message.getType() == Message.TYPE_FILE) {
 411					String suffix = "";
 412					if (!fileNameElement.getContent().isEmpty()) {
 413						String parts[] = fileNameElement.getContent().split("/");
 414						suffix = parts[parts.length - 1];
 415						if (message.getEncryption() == Message.ENCRYPTION_PGP && (suffix.endsWith(".pgp") || suffix.endsWith(".gpg"))) {
 416							suffix = suffix.substring(0,suffix.length() - 4);
 417						}
 418					}
 419					message.setRelativeFilePath(message.getUuid()+"_"+suffix);
 420				}
 421				long size = Long.parseLong(fileSize.getContent());
 422				message.setBody(Long.toString(size));
 423				conversation.add(message);
 424				mJingleConnectionManager.updateConversationUi(true);
 425				if (mJingleConnectionManager.hasStoragePermission()
 426						&& size < this.mJingleConnectionManager.getAutoAcceptFileSize()
 427						&& mXmppConnectionService.isDataSaverDisabled()) {
 428					Log.d(Config.LOGTAG, "auto accepting file from "+ packet.getFrom());
 429					this.acceptedAutomatically = true;
 430					this.sendAccept();
 431				} else {
 432					message.markUnread();
 433					Log.d(Config.LOGTAG,
 434							"not auto accepting new file offer with size: "
 435									+ size
 436									+ " allowed size:"
 437									+ this.mJingleConnectionManager
 438											.getAutoAcceptFileSize());
 439					this.mXmppConnectionService.getNotificationService().push(message);
 440				}
 441				this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
 442				if (mXmppAxolotlMessage != null) {
 443					XmppAxolotlMessage.XmppAxolotlKeyTransportMessage transportMessage = account.getAxolotlService().processReceivingKeyTransportMessage(mXmppAxolotlMessage, false);
 444					if (transportMessage != null) {
 445						message.setEncryption(Message.ENCRYPTION_AXOLOTL);
 446						this.file.setKey(transportMessage.getKey());
 447						this.file.setIv(transportMessage.getIv());
 448						message.setFingerprint(transportMessage.getFingerprint());
 449					} else {
 450						Log.d(Config.LOGTAG,"could not process KeyTransportMessage");
 451					}
 452				}
 453				this.file.setExpectedSize(size);
 454				message.resetFileParams();
 455				Log.d(Config.LOGTAG, "receiving file: expecting size of " + this.file.getExpectedSize());
 456			} else {
 457				this.sendCancel();
 458				this.fail();
 459			}
 460		} else {
 461			this.sendCancel();
 462			this.fail();
 463		}
 464	}
 465
 466	private void sendInitRequest() {
 467		JinglePacket packet = this.bootstrapPacket("session-initiate");
 468		Content content = new Content(this.contentCreator, this.contentName);
 469		if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
 470			content.setTransportId(this.transportId);
 471			this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
 472			if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 473				this.file.setKey(mXmppAxolotlMessage.getInnerKey());
 474				this.file.setIv(mXmppAxolotlMessage.getIV());
 475				this.file.setExpectedSize(file.getSize() + 16);
 476				content.setFileOffer(this.file, false, this.ftVersion).addChild(mXmppAxolotlMessage.toElement());
 477			} else {
 478				this.file.setExpectedSize(file.getSize());
 479				content.setFileOffer(this.file, false, this.ftVersion);
 480			}
 481			message.resetFileParams();
 482			try {
 483				this.mFileInputStream = new FileInputStream(file);
 484			} catch (FileNotFoundException e) {
 485				abort();
 486				return;
 487			}
 488			content.setTransportId(this.transportId);
 489			content.socks5transport().setChildren(getCandidatesAsElements());
 490			packet.setContent(content);
 491			this.sendJinglePacket(packet,new OnIqPacketReceived() {
 492
 493				@Override
 494				public void onIqPacketReceived(Account account, IqPacket packet) {
 495					if (packet.getType() == IqPacket.TYPE.RESULT) {
 496						Log.d(Config.LOGTAG,account.getJid().asBareJid()+": other party received offer");
 497						if (mJingleStatus == JINGLE_STATUS_OFFERED) {
 498							mJingleStatus = JINGLE_STATUS_INITIATED;
 499							mXmppConnectionService.markMessage(message, Message.STATUS_OFFERED);
 500						} else {
 501							Log.d(Config.LOGTAG,"received ack for offer when status was "+mJingleStatus);
 502						}
 503					} else {
 504						fail(IqParser.extractErrorMessage(packet));
 505					}
 506				}
 507			});
 508
 509		}
 510	}
 511
 512	private void sendHash() {
 513		JinglePacket packet = this.bootstrapPacket("session-info");
 514		packet.addChecksum(file.getSha1Sum(),ftVersion.getNamespace());
 515		this.sendJinglePacket(packet);
 516	}
 517
 518	private List<Element> getCandidatesAsElements() {
 519		List<Element> elements = new ArrayList<>();
 520		for (JingleCandidate c : this.candidates) {
 521			if (c.isOurs()) {
 522				elements.add(c.toElement());
 523			}
 524		}
 525		return elements;
 526	}
 527
 528	private void sendAccept() {
 529		mJingleStatus = JINGLE_STATUS_ACCEPTED;
 530		this.mStatus = Transferable.STATUS_DOWNLOADING;
 531		this.mJingleConnectionManager.updateConversationUi(true);
 532		this.mJingleConnectionManager.getPrimaryCandidate(this.account, new OnPrimaryCandidateFound() {
 533			@Override
 534			public void onPrimaryCandidateFound(boolean success, final JingleCandidate candidate) {
 535				final JinglePacket packet = bootstrapPacket("session-accept");
 536				final Content content = new Content(contentCreator,contentName);
 537				content.setFileOffer(fileOffer, ftVersion);
 538				content.setTransportId(transportId);
 539				if (success && candidate != null && !equalCandidateExists(candidate)) {
 540					final JingleSocks5Transport socksConnection = new JingleSocks5Transport(
 541							JingleConnection.this,
 542							candidate);
 543					connections.put(candidate.getCid(), socksConnection);
 544					socksConnection.connect(new OnTransportConnected() {
 545
 546						@Override
 547						public void failed() {
 548							Log.d(Config.LOGTAG,"connection to our own primary candidate failed");
 549							content.socks5transport().setChildren(getCandidatesAsElements());
 550							packet.setContent(content);
 551							sendJinglePacket(packet);
 552							connectNextCandidate();
 553						}
 554
 555						@Override
 556						public void established() {
 557							Log.d(Config.LOGTAG, "connected to primary candidate");
 558							mergeCandidate(candidate);
 559							content.socks5transport().setChildren(getCandidatesAsElements());
 560							packet.setContent(content);
 561							sendJinglePacket(packet);
 562							connectNextCandidate();
 563						}
 564					});
 565				} else {
 566					Log.d(Config.LOGTAG,"did not find a primary candidate for ourself");
 567					content.socks5transport().setChildren(getCandidatesAsElements());
 568					packet.setContent(content);
 569					sendJinglePacket(packet);
 570					connectNextCandidate();
 571				}
 572			}
 573		});
 574	}
 575
 576	private JinglePacket bootstrapPacket(String action) {
 577		JinglePacket packet = new JinglePacket();
 578		packet.setAction(action);
 579		packet.setFrom(account.getJid());
 580		packet.setTo(this.message.getCounterpart());
 581		packet.setSessionId(this.sessionId);
 582		packet.setInitiator(this.initiator);
 583		return packet;
 584	}
 585
 586	private void sendJinglePacket(JinglePacket packet) {
 587		mXmppConnectionService.sendIqPacket(account,packet,responseListener);
 588	}
 589
 590	private void sendJinglePacket(JinglePacket packet, OnIqPacketReceived callback) {
 591		mXmppConnectionService.sendIqPacket(account,packet,callback);
 592	}
 593
 594	private boolean receiveAccept(JinglePacket packet) {
 595		Content content = packet.getJingleContent();
 596		mergeCandidates(JingleCandidate.parse(content.socks5transport()
 597				.getChildren()));
 598		this.mJingleStatus = JINGLE_STATUS_ACCEPTED;
 599		mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
 600		this.connectNextCandidate();
 601		return true;
 602	}
 603
 604	private boolean receiveTransportInfo(JinglePacket packet) {
 605		Content content = packet.getJingleContent();
 606		if (content.hasSocks5Transport()) {
 607			if (content.socks5transport().hasChild("activated")) {
 608				if ((this.transport != null) && (this.transport instanceof JingleSocks5Transport)) {
 609					onProxyActivated.success();
 610				} else {
 611					String cid = content.socks5transport().findChild("activated").getAttribute("cid");
 612					Log.d(Config.LOGTAG, "received proxy activated (" + cid
 613							+ ")prior to choosing our own transport");
 614					JingleSocks5Transport connection = this.connections.get(cid);
 615					if (connection != null) {
 616						connection.setActivated(true);
 617					} else {
 618						Log.d(Config.LOGTAG, "activated connection not found");
 619						this.sendCancel();
 620						this.fail();
 621					}
 622				}
 623				return true;
 624			} else if (content.socks5transport().hasChild("proxy-error")) {
 625				onProxyActivated.failed();
 626				return true;
 627			} else if (content.socks5transport().hasChild("candidate-error")) {
 628				Log.d(Config.LOGTAG, "received candidate error");
 629				this.receivedCandidate = true;
 630				if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
 631					this.connect();
 632				}
 633				return true;
 634			} else if (content.socks5transport().hasChild("candidate-used")) {
 635				String cid = content.socks5transport()
 636						.findChild("candidate-used").getAttribute("cid");
 637				if (cid != null) {
 638					Log.d(Config.LOGTAG, "candidate used by counterpart:" + cid);
 639					JingleCandidate candidate = getCandidate(cid);
 640					if (candidate == null) {
 641						Log.d(Config.LOGTAG,"could not find candidate with cid="+cid);
 642						return false;
 643					}
 644					candidate.flagAsUsedByCounterpart();
 645					this.receivedCandidate = true;
 646					if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
 647						this.connect();
 648					} else {
 649						Log.d(Config.LOGTAG, "ignoring because file is already in transmission or we haven't sent our candidate yet status="+mJingleStatus+" sentCandidate="+Boolean.toString(sentCandidate));
 650					}
 651					return true;
 652				} else {
 653					return false;
 654				}
 655			} else {
 656				return false;
 657			}
 658		} else {
 659			return true;
 660		}
 661	}
 662
 663	private void connect() {
 664		final JingleSocks5Transport connection = chooseConnection();
 665		this.transport = connection;
 666		if (connection == null) {
 667			Log.d(Config.LOGTAG, "could not find suitable candidate");
 668			this.disconnectSocks5Connections();
 669			if (initiating()) {
 670				this.sendFallbackToIbb();
 671			}
 672		} else {
 673			this.mJingleStatus = JINGLE_STATUS_TRANSMITTING;
 674			if (connection.needsActivation()) {
 675				if (connection.getCandidate().isOurs()) {
 676					final String sid;
 677					if (ftVersion == Content.Version.FT_3) {
 678						Log.d(Config.LOGTAG,account.getJid().asBareJid()+": use session ID instead of transport ID to activate proxy");
 679						sid = getSessionId();
 680					} else {
 681						sid = getTransportId();
 682					}
 683					Log.d(Config.LOGTAG, "candidate "
 684							+ connection.getCandidate().getCid()
 685							+ " was our proxy. going to activate");
 686					IqPacket activation = new IqPacket(IqPacket.TYPE.SET);
 687					activation.setTo(connection.getCandidate().getJid());
 688					activation.query("http://jabber.org/protocol/bytestreams")
 689							.setAttribute("sid", sid);
 690					activation.query().addChild("activate")
 691							.setContent(this.getCounterPart().toString());
 692					mXmppConnectionService.sendIqPacket(account,activation,
 693							new OnIqPacketReceived() {
 694
 695								@Override
 696								public void onIqPacketReceived(Account account,
 697										IqPacket packet) {
 698									if (packet.getType() != IqPacket.TYPE.RESULT) {
 699										onProxyActivated.failed();
 700									} else {
 701										onProxyActivated.success();
 702										sendProxyActivated(connection.getCandidate().getCid());
 703									}
 704								}
 705							});
 706				} else {
 707					Log.d(Config.LOGTAG,
 708							"candidate "
 709									+ connection.getCandidate().getCid()
 710									+ " was a proxy. waiting for other party to activate");
 711				}
 712			} else {
 713				if (initiating()) {
 714					Log.d(Config.LOGTAG, "we were initiating. sending file");
 715					connection.send(file, onFileTransmissionStatusChanged);
 716				} else {
 717					Log.d(Config.LOGTAG, "we were responding. receiving file");
 718					connection.receive(file, onFileTransmissionStatusChanged);
 719				}
 720			}
 721		}
 722	}
 723
 724	private JingleSocks5Transport chooseConnection() {
 725		JingleSocks5Transport connection = null;
 726		for (Entry<String, JingleSocks5Transport> cursor : connections
 727				.entrySet()) {
 728			JingleSocks5Transport currentConnection = cursor.getValue();
 729			// Log.d(Config.LOGTAG,"comparing candidate: "+currentConnection.getCandidate().toString());
 730			if (currentConnection.isEstablished()
 731					&& (currentConnection.getCandidate().isUsedByCounterpart() || (!currentConnection
 732							.getCandidate().isOurs()))) {
 733				// Log.d(Config.LOGTAG,"is usable");
 734				if (connection == null) {
 735					connection = currentConnection;
 736				} else {
 737					if (connection.getCandidate().getPriority() < currentConnection
 738							.getCandidate().getPriority()) {
 739						connection = currentConnection;
 740					} else if (connection.getCandidate().getPriority() == currentConnection
 741							.getCandidate().getPriority()) {
 742						// Log.d(Config.LOGTAG,"found two candidates with same priority");
 743						if (initiating()) {
 744							if (currentConnection.getCandidate().isOurs()) {
 745								connection = currentConnection;
 746							}
 747						} else {
 748							if (!currentConnection.getCandidate().isOurs()) {
 749								connection = currentConnection;
 750							}
 751						}
 752					}
 753				}
 754			}
 755		}
 756		return connection;
 757	}
 758
 759	private void sendSuccess() {
 760		JinglePacket packet = bootstrapPacket("session-terminate");
 761		Reason reason = new Reason();
 762		reason.addChild("success");
 763		packet.setReason(reason);
 764		this.sendJinglePacket(packet);
 765		this.disconnectSocks5Connections();
 766		this.mJingleStatus = JINGLE_STATUS_FINISHED;
 767		this.message.setStatus(Message.STATUS_RECEIVED);
 768		this.message.setTransferable(null);
 769		this.mXmppConnectionService.updateMessage(message, false);
 770		this.mJingleConnectionManager.finishConnection(this);
 771	}
 772
 773	private void sendFallbackToIbb() {
 774		Log.d(Config.LOGTAG, account.getJid().asBareJid()+": sending fallback to ibb");
 775		JinglePacket packet = this.bootstrapPacket("transport-replace");
 776		Content content = new Content(this.contentCreator, this.contentName);
 777		this.transportId = this.mJingleConnectionManager.nextRandomId();
 778		content.setTransportId(this.transportId);
 779		content.ibbTransport().setAttribute("block-size",
 780				Integer.toString(this.ibbBlockSize));
 781		packet.setContent(content);
 782		this.sendJinglePacket(packet);
 783	}
 784
 785	OnTransportConnected onIbbTransportConnected = new OnTransportConnected() {
 786		@Override
 787		public void failed() {
 788			Log.d(Config.LOGTAG, "ibb open failed");
 789		}
 790
 791		@Override
 792		public void established() {
 793			JingleConnection.this.transport.send(file, onFileTransmissionStatusChanged);
 794		}
 795	};
 796
 797	private boolean receiveFallbackToIbb(JinglePacket packet) {
 798		Log.d(Config.LOGTAG, "receiving fallack to ibb");
 799		String receivedBlockSize = packet.getJingleContent().ibbTransport()
 800				.getAttribute("block-size");
 801		if (receivedBlockSize != null) {
 802			int bs = Integer.parseInt(receivedBlockSize);
 803			if (bs > this.ibbBlockSize) {
 804				this.ibbBlockSize = bs;
 805			}
 806		}
 807		this.transportId = packet.getJingleContent().getTransportId();
 808		this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
 809
 810		JinglePacket answer = bootstrapPacket("transport-accept");
 811		Content content = new Content("initiator", "a-file-offer");
 812		content.setTransportId(this.transportId);
 813		content.ibbTransport().setAttribute("block-size",this.ibbBlockSize);
 814		answer.setContent(content);
 815
 816
 817		if (initiating()) {
 818			this.sendJinglePacket(answer, new OnIqPacketReceived() {
 819				@Override
 820				public void onIqPacketReceived(Account account, IqPacket packet) {
 821					if (packet.getType() == IqPacket.TYPE.RESULT) {
 822						Log.d(Config.LOGTAG, account.getJid().asBareJid() + " recipient ACKed our transport-accept. creating ibb");
 823						transport.connect(onIbbTransportConnected);
 824					}
 825				}
 826			});
 827		} else {
 828			this.transport.receive(file, onFileTransmissionStatusChanged);
 829			this.sendJinglePacket(answer);
 830		}
 831		return true;
 832	}
 833
 834	private boolean receiveTransportAccept(JinglePacket packet) {
 835		if (packet.getJingleContent().hasIbbTransport()) {
 836			String receivedBlockSize = packet.getJingleContent().ibbTransport()
 837					.getAttribute("block-size");
 838			if (receivedBlockSize != null) {
 839				int bs = Integer.parseInt(receivedBlockSize);
 840				if (bs > this.ibbBlockSize) {
 841					this.ibbBlockSize = bs;
 842				}
 843			}
 844			this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
 845
 846			//might be receive instead if we are not initiating
 847			if (initiating()) {
 848				this.transport.connect(onIbbTransportConnected);
 849			} else {
 850				this.transport.receive(file, onFileTransmissionStatusChanged);
 851			}
 852			return true;
 853		} else {
 854			return false;
 855		}
 856	}
 857
 858	private void receiveSuccess() {
 859		if (initiating()) {
 860			this.mJingleStatus = JINGLE_STATUS_FINISHED;
 861			this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED);
 862			this.disconnectSocks5Connections();
 863			if (this.transport instanceof JingleInbandTransport) {
 864				this.transport.disconnect();
 865			}
 866			this.message.setTransferable(null);
 867			this.mJingleConnectionManager.finishConnection(this);
 868		} else {
 869			Log.d(Config.LOGTAG,account.getJid().asBareJid()+": received session-terminate/success while responding");
 870		}
 871	}
 872	@Override
 873	public void cancel() {
 874		this.cancelled = true;
 875		abort();
 876	}
 877
 878	public void abort() {
 879		this.disconnectSocks5Connections();
 880		if (this.transport instanceof JingleInbandTransport) {
 881			this.transport.disconnect();
 882		}
 883		this.sendCancel();
 884		this.mJingleConnectionManager.finishConnection(this);
 885		if (responding()) {
 886			this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
 887			if (this.file!=null) {
 888				file.delete();
 889			}
 890			this.mJingleConnectionManager.updateConversationUi(true);
 891		} else {
 892			this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : null);
 893			this.message.setTransferable(null);
 894		}
 895	}
 896
 897	private void fail() {
 898		fail(null);
 899	}
 900
 901	private void fail(String errorMessage) {
 902		this.mJingleStatus = JINGLE_STATUS_FAILED;
 903		this.disconnectSocks5Connections();
 904		if (this.transport instanceof JingleInbandTransport) {
 905			this.transport.disconnect();
 906		}
 907		FileBackend.close(mFileInputStream);
 908		FileBackend.close(mFileOutputStream);
 909		if (this.message != null) {
 910			if (responding()) {
 911				this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
 912				if (this.file!=null) {
 913					file.delete();
 914				}
 915				this.mJingleConnectionManager.updateConversationUi(true);
 916			} else {
 917				this.mXmppConnectionService.markMessage(this.message,
 918						Message.STATUS_SEND_FAILED,
 919						cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage);
 920				this.message.setTransferable(null);
 921			}
 922		}
 923		this.mJingleConnectionManager.finishConnection(this);
 924	}
 925
 926	private void sendCancel() {
 927		JinglePacket packet = bootstrapPacket("session-terminate");
 928		Reason reason = new Reason();
 929		reason.addChild("cancel");
 930		packet.setReason(reason);
 931		this.sendJinglePacket(packet);
 932	}
 933
 934	private void connectNextCandidate() {
 935		for (JingleCandidate candidate : this.candidates) {
 936			if ((!connections.containsKey(candidate.getCid()) && (!candidate
 937					.isOurs()))) {
 938				this.connectWithCandidate(candidate);
 939				return;
 940			}
 941		}
 942		this.sendCandidateError();
 943	}
 944
 945	private void connectWithCandidate(final JingleCandidate candidate) {
 946		final JingleSocks5Transport socksConnection = new JingleSocks5Transport(
 947				this, candidate);
 948		connections.put(candidate.getCid(), socksConnection);
 949		socksConnection.connect(new OnTransportConnected() {
 950
 951			@Override
 952			public void failed() {
 953				Log.d(Config.LOGTAG,
 954						"connection failed with " + candidate.getHost() + ":"
 955								+ candidate.getPort());
 956				connectNextCandidate();
 957			}
 958
 959			@Override
 960			public void established() {
 961				Log.d(Config.LOGTAG,
 962						"established connection with " + candidate.getHost()
 963								+ ":" + candidate.getPort());
 964				sendCandidateUsed(candidate.getCid());
 965			}
 966		});
 967	}
 968
 969	private void disconnectSocks5Connections() {
 970		Iterator<Entry<String, JingleSocks5Transport>> it = this.connections
 971				.entrySet().iterator();
 972		while (it.hasNext()) {
 973			Entry<String, JingleSocks5Transport> pairs = it.next();
 974			pairs.getValue().disconnect();
 975			it.remove();
 976		}
 977	}
 978
 979	private void sendProxyActivated(String cid) {
 980		JinglePacket packet = bootstrapPacket("transport-info");
 981		Content content = new Content(this.contentCreator, this.contentName);
 982		content.setTransportId(this.transportId);
 983		content.socks5transport().addChild("activated")
 984				.setAttribute("cid", cid);
 985		packet.setContent(content);
 986		this.sendJinglePacket(packet);
 987	}
 988
 989	private void sendCandidateUsed(final String cid) {
 990		JinglePacket packet = bootstrapPacket("transport-info");
 991		Content content = new Content(this.contentCreator, this.contentName);
 992		content.setTransportId(this.transportId);
 993		content.socks5transport().addChild("candidate-used").setAttribute("cid", cid);
 994		packet.setContent(content);
 995		this.sentCandidate = true;
 996		if ((receivedCandidate) && (mJingleStatus == JINGLE_STATUS_ACCEPTED)) {
 997			connect();
 998		}
 999		this.sendJinglePacket(packet);
1000	}
1001
1002	private void sendCandidateError() {
1003		JinglePacket packet = bootstrapPacket("transport-info");
1004		Content content = new Content(this.contentCreator, this.contentName);
1005		content.setTransportId(this.transportId);
1006		content.socks5transport().addChild("candidate-error");
1007		packet.setContent(content);
1008		this.sentCandidate = true;
1009		if (receivedCandidate && mJingleStatus == JINGLE_STATUS_ACCEPTED) {
1010			connect();
1011		}
1012		this.sendJinglePacket(packet);
1013	}
1014
1015	public int getJingleStatus() {
1016		return this.mJingleStatus;
1017	}
1018
1019	private boolean equalCandidateExists(JingleCandidate candidate) {
1020		for (JingleCandidate c : this.candidates) {
1021			if (c.equalValues(candidate)) {
1022				return true;
1023			}
1024		}
1025		return false;
1026	}
1027
1028	private void mergeCandidate(JingleCandidate candidate) {
1029		for (JingleCandidate c : this.candidates) {
1030			if (c.equals(candidate)) {
1031				return;
1032			}
1033		}
1034		this.candidates.add(candidate);
1035	}
1036
1037	private void mergeCandidates(List<JingleCandidate> candidates) {
1038		for (JingleCandidate c : candidates) {
1039			mergeCandidate(c);
1040		}
1041	}
1042
1043	private JingleCandidate getCandidate(String cid) {
1044		for (JingleCandidate c : this.candidates) {
1045			if (c.getCid().equals(cid)) {
1046				return c;
1047			}
1048		}
1049		return null;
1050	}
1051
1052	public void updateProgress(int i) {
1053		this.mProgress = i;
1054		mJingleConnectionManager.updateConversationUi(false);
1055	}
1056
1057	public String getTransportId() {
1058		return this.transportId;
1059	}
1060
1061	public Content.Version getFtVersion() {
1062		return this.ftVersion;
1063	}
1064
1065	interface OnProxyActivated {
1066		void success();
1067
1068		void failed();
1069	}
1070
1071	public boolean hasTransportId(String sid) {
1072		return sid.equals(this.transportId);
1073	}
1074
1075	public JingleTransport getTransport() {
1076		return this.transport;
1077	}
1078
1079	public boolean start() {
1080		if (account.getStatus() == Account.State.ONLINE) {
1081			if (mJingleStatus == JINGLE_STATUS_INITIATED) {
1082				new Thread(new Runnable() {
1083
1084					@Override
1085					public void run() {
1086						sendAccept();
1087					}
1088				}).start();
1089			}
1090			return true;
1091		} else {
1092			return false;
1093		}
1094	}
1095
1096	@Override
1097	public int getStatus() {
1098		return this.mStatus;
1099	}
1100
1101	@Override
1102	public long getFileSize() {
1103		if (this.file != null) {
1104			return this.file.getExpectedSize();
1105		} else {
1106			return 0;
1107		}
1108	}
1109
1110	@Override
1111	public int getProgress() {
1112		return this.mProgress;
1113	}
1114
1115	public AbstractConnectionManager getConnectionManager() {
1116		return this.mJingleConnectionManager;
1117	}
1118}