JingleConnection.java

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