fixups for last commit

This commit is contained in:
Daniel Gultsch 2019-12-05 14:03:54 +01:00
parent 8263fb73e9
commit a3d639747a
3 changed files with 155 additions and 151 deletions

View File

@ -95,7 +95,11 @@ public class JingleConnection implements Transferable {
private OnIqPacketReceived responseListener = (account, packet) -> { private OnIqPacketReceived responseListener = (account, packet) -> {
if (packet.getType() != IqPacket.TYPE.RESULT) { if (packet.getType() != IqPacket.TYPE.RESULT) {
fail(IqParser.extractErrorMessage(packet)); if (mJingleStatus != JINGLE_STATUS_FAILED && mJingleStatus != JINGLE_STATUS_FINISHED) {
fail(IqParser.extractErrorMessage(packet));
} else {
Log.d(Config.LOGTAG,"ignoring late delivery of jingle packet to jingle session with status="+mJingleStatus+": "+packet.toString());
}
} }
}; };
private byte[] expectedHash = new byte[0]; private byte[] expectedHash = new byte[0];
@ -674,7 +678,7 @@ public class JingleConnection implements Transferable {
} }
private void sendAcceptIbb() { private void sendAcceptIbb() {
this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize); this.transport = new JingleInBandTransport(this, this.transportId, this.ibbBlockSize);
final JinglePacket packet = bootstrapPacket("session-accept"); final JinglePacket packet = bootstrapPacket("session-accept");
final Content content = new Content(contentCreator, contentName); final Content content = new Content(contentCreator, contentName);
content.setFileOffer(fileOffer, ftVersion); content.setFileOffer(fileOffer, ftVersion);
@ -734,7 +738,7 @@ public class JingleConnection implements Transferable {
} }
} }
respondToIq(packet, true); respondToIq(packet, true);
this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize); this.transport = new JingleInBandTransport(this, this.transportId, this.ibbBlockSize);
this.transport.connect(onIbbTransportConnected); this.transport.connect(onIbbTransportConnected);
} else { } else {
respondToIq(packet, false); respondToIq(packet, false);
@ -943,7 +947,7 @@ public class JingleConnection implements Transferable {
} }
} }
this.transportId = packet.getJingleContent().getTransportId(); this.transportId = packet.getJingleContent().getTransportId();
this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize); this.transport = new JingleInBandTransport(this, this.transportId, this.ibbBlockSize);
final JinglePacket answer = bootstrapPacket("transport-accept"); final JinglePacket answer = bootstrapPacket("transport-accept");
@ -994,7 +998,7 @@ public class JingleConnection implements Transferable {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in transport-accept"); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in transport-accept");
} }
} }
this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize); this.transport = new JingleInBandTransport(this, this.transportId, this.ibbBlockSize);
if (sid == null || !sid.equals(this.transportId)) { if (sid == null || !sid.equals(this.transportId)) {
Log.w(Config.LOGTAG, String.format("%s: sid in transport-accept (%s) did not match our sid (%s) ", account.getJid().asBareJid(), sid, transportId)); Log.w(Config.LOGTAG, String.format("%s: sid in transport-accept (%s) did not match our sid (%s) ", account.getJid().asBareJid(), sid, transportId));
@ -1015,7 +1019,7 @@ public class JingleConnection implements Transferable {
this.mJingleStatus = JINGLE_STATUS_FINISHED; this.mJingleStatus = JINGLE_STATUS_FINISHED;
this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED); this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED);
this.disconnectSocks5Connections(); this.disconnectSocks5Connections();
if (this.transport instanceof JingleInbandTransport) { if (this.transport instanceof JingleInBandTransport) {
this.transport.disconnect(); this.transport.disconnect();
} }
this.message.setTransferable(null); this.message.setTransferable(null);
@ -1033,7 +1037,7 @@ public class JingleConnection implements Transferable {
void abort(final String reason) { void abort(final String reason) {
this.disconnectSocks5Connections(); this.disconnectSocks5Connections();
if (this.transport instanceof JingleInbandTransport) { if (this.transport instanceof JingleInBandTransport) {
this.transport.disconnect(); this.transport.disconnect();
} }
sendSessionTerminate(reason); sendSessionTerminate(reason);
@ -1057,7 +1061,7 @@ public class JingleConnection implements Transferable {
private void fail(String errorMessage) { private void fail(String errorMessage) {
this.mJingleStatus = JINGLE_STATUS_FAILED; this.mJingleStatus = JINGLE_STATUS_FAILED;
this.disconnectSocks5Connections(); this.disconnectSocks5Connections();
if (this.transport instanceof JingleInbandTransport) { if (this.transport instanceof JingleInBandTransport) {
this.transport.disconnect(); this.transport.disconnect();
} }
FileBackend.close(mFileInputStream); FileBackend.close(mFileInputStream);

View File

@ -23,151 +23,151 @@ import eu.siacs.conversations.xmpp.stanzas.IqPacket;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
public class JingleConnectionManager extends AbstractConnectionManager { public class JingleConnectionManager extends AbstractConnectionManager {
private List<JingleConnection> connections = new CopyOnWriteArrayList<>(); private List<JingleConnection> connections = new CopyOnWriteArrayList<>();
private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>(); private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
@SuppressLint("TrulyRandom") @SuppressLint("TrulyRandom")
private SecureRandom random = new SecureRandom(); private SecureRandom random = new SecureRandom();
public JingleConnectionManager(XmppConnectionService service) { public JingleConnectionManager(XmppConnectionService service) {
super(service); super(service);
} }
public void deliverPacket(Account account, JinglePacket packet) { public void deliverPacket(Account account, JinglePacket packet) {
if (packet.isAction("session-initiate")) { if (packet.isAction("session-initiate")) {
JingleConnection connection = new JingleConnection(this); JingleConnection connection = new JingleConnection(this);
connection.init(account, packet); connection.init(account, packet);
connections.add(connection); connections.add(connection);
} else { } else {
for (JingleConnection connection : connections) { for (JingleConnection connection : connections) {
if (connection.getAccount() == account if (connection.getAccount() == account
&& connection.getSessionId().equals( && connection.getSessionId().equals(
packet.getSessionId()) packet.getSessionId())
&& connection.getCounterPart().equals(packet.getFrom())) { && connection.getCounterPart().equals(packet.getFrom())) {
connection.deliverPacket(packet); connection.deliverPacket(packet);
return; return;
} }
} }
IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR); Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
Element error = response.addChild("error"); IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
error.setAttribute("type", "cancel"); Element error = response.addChild("error");
error.addChild("item-not-found", error.setAttribute("type", "cancel");
"urn:ietf:params:xml:ns:xmpp-stanzas"); error.addChild("item-not-found",
error.addChild("unknown-session", "urn:xmpp:jingle:errors:1"); "urn:ietf:params:xml:ns:xmpp-stanzas");
account.getXmppConnection().sendIqPacket(response, null); error.addChild("unknown-session", "urn:xmpp:jingle:errors:1");
} account.getXmppConnection().sendIqPacket(response, null);
} }
}
public JingleConnection createNewConnection(Message message) { public JingleConnection createNewConnection(Message message) {
Transferable old = message.getTransferable(); Transferable old = message.getTransferable();
if (old != null) { if (old != null) {
old.cancel(); old.cancel();
} }
JingleConnection connection = new JingleConnection(this); JingleConnection connection = new JingleConnection(this);
mXmppConnectionService.markMessage(message,Message.STATUS_WAITING); mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
connection.init(message); connection.init(message);
this.connections.add(connection); this.connections.add(connection);
return connection; return connection;
} }
public JingleConnection createNewConnection(final JinglePacket packet) { public JingleConnection createNewConnection(final JinglePacket packet) {
JingleConnection connection = new JingleConnection(this); JingleConnection connection = new JingleConnection(this);
this.connections.add(connection); this.connections.add(connection);
return connection; return connection;
} }
public void finishConnection(JingleConnection connection) { public void finishConnection(JingleConnection connection) {
this.connections.remove(connection); this.connections.remove(connection);
} }
public void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) { public void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) {
if (Config.DISABLE_PROXY_LOOKUP) { if (Config.DISABLE_PROXY_LOOKUP) {
listener.onPrimaryCandidateFound(false, null); listener.onPrimaryCandidateFound(false, null);
return; return;
} }
if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) { if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS); final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
if (proxy != null) { if (proxy != null) {
IqPacket iq = new IqPacket(IqPacket.TYPE.GET); IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setTo(proxy); iq.setTo(proxy);
iq.query(Namespace.BYTE_STREAMS); iq.query(Namespace.BYTE_STREAMS);
account.getXmppConnection().sendIqPacket(iq,new OnIqPacketReceived() { account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS); Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
final String host = streamhost == null ? null : streamhost.getAttribute("host"); final String host = streamhost == null ? null : streamhost.getAttribute("host");
final String port = streamhost == null ? null : streamhost.getAttribute("port"); final String port = streamhost == null ? null : streamhost.getAttribute("port");
if (host != null && port != null) { if (host != null && port != null) {
try { try {
JingleCandidate candidate = new JingleCandidate(nextRandomId(), true); JingleCandidate candidate = new JingleCandidate(nextRandomId(), true);
candidate.setHost(host); candidate.setHost(host);
candidate.setPort(Integer.parseInt(port)); candidate.setPort(Integer.parseInt(port));
candidate.setType(JingleCandidate.TYPE_PROXY); candidate.setType(JingleCandidate.TYPE_PROXY);
candidate.setJid(proxy); candidate.setJid(proxy);
candidate.setPriority(655360 + (initiator ? 30 : 0)); candidate.setPriority(655360 + (initiator ? 30 : 0));
primaryCandidates.put(account.getJid().asBareJid(),candidate); primaryCandidates.put(account.getJid().asBareJid(), candidate);
listener.onPrimaryCandidateFound(true,candidate); listener.onPrimaryCandidateFound(true, candidate);
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
listener.onPrimaryCandidateFound(false,null); listener.onPrimaryCandidateFound(false, null);
return; return;
} }
} else { } else {
listener.onPrimaryCandidateFound(false,null); listener.onPrimaryCandidateFound(false, null);
} }
} }
}); });
} else { } else {
listener.onPrimaryCandidateFound(false, null); listener.onPrimaryCandidateFound(false, null);
} }
} else { } else {
listener.onPrimaryCandidateFound(true, listener.onPrimaryCandidateFound(true,
this.primaryCandidates.get(account.getJid().asBareJid())); this.primaryCandidates.get(account.getJid().asBareJid()));
} }
} }
public String nextRandomId() { public String nextRandomId() {
return new BigInteger(50, random).toString(32); return new BigInteger(50, random).toString(32);
} }
public void deliverIbbPacket(Account account, IqPacket packet) { public void deliverIbbPacket(Account account, IqPacket packet) {
String sid = null; String sid = null;
Element payload = null; Element payload = null;
if (packet.hasChild("open", "http://jabber.org/protocol/ibb")) { if (packet.hasChild("open", "http://jabber.org/protocol/ibb")) {
payload = packet.findChild("open", "http://jabber.org/protocol/ibb"); payload = packet.findChild("open", "http://jabber.org/protocol/ibb");
sid = payload.getAttribute("sid"); sid = payload.getAttribute("sid");
} else if (packet.hasChild("data", "http://jabber.org/protocol/ibb")) { } else if (packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
payload = packet.findChild("data", "http://jabber.org/protocol/ibb"); payload = packet.findChild("data", "http://jabber.org/protocol/ibb");
sid = payload.getAttribute("sid"); sid = payload.getAttribute("sid");
} else if (packet.hasChild("close","http://jabber.org/protocol/ibb")) { } else if (packet.hasChild("close", "http://jabber.org/protocol/ibb")) {
payload = packet.findChild("close", "http://jabber.org/protocol/ibb"); payload = packet.findChild("close", "http://jabber.org/protocol/ibb");
sid = payload.getAttribute("sid"); sid = payload.getAttribute("sid");
} }
if (sid != null) { if (sid != null) {
for (JingleConnection connection : connections) { for (JingleConnection connection : connections) {
if (connection.getAccount() == account if (connection.getAccount() == account
&& connection.hasTransportId(sid)) { && connection.hasTransportId(sid)) {
JingleTransport transport = connection.getTransport(); JingleTransport transport = connection.getTransport();
if (transport instanceof JingleInbandTransport) { if (transport instanceof JingleInBandTransport) {
JingleInbandTransport inbandTransport = (JingleInbandTransport) transport; JingleInBandTransport inbandTransport = (JingleInBandTransport) transport;
inbandTransport.deliverPayload(packet, payload); inbandTransport.deliverPayload(packet, payload);
return; return;
} }
} }
} }
Log.d(Config.LOGTAG,"couldn't deliver payload: " + payload.toString()); }
} else { Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
Log.d(Config.LOGTAG, "no sid found in incoming ibb packet"); account.getXmppConnection().sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
} }
}
public void cancelInTransmission() { public void cancelInTransmission() {
for (JingleConnection connection : this.connections) { for (JingleConnection connection : this.connections) {
if (connection.getJingleStatus() == JingleConnection.JINGLE_STATUS_TRANSMITTING) { if (connection.getJingleStatus() == JingleConnection.JINGLE_STATUS_TRANSMITTING) {
connection.abort("connectivity-error"); connection.abort("connectivity-error");
} }
} }
} }
} }

View File

@ -20,20 +20,20 @@ import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.stanzas.IqPacket;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
public class JingleInbandTransport extends JingleTransport { public class JingleInBandTransport extends JingleTransport {
private Account account; private final Account account;
private Jid counterpart; private final Jid counterpart;
private int blockSize; private final int blockSize;
private int seq = 0; private int seq = 0;
private String sessionId; private final String sessionId;
private boolean established = false; private boolean established = false;
private boolean connected = true; private boolean connected = true;
private DownloadableFile file; private DownloadableFile file;
private JingleConnection connection; private final JingleConnection connection;
private InputStream fileInputStream = null; private InputStream fileInputStream = null;
private InputStream innerInputStream = null; private InputStream innerInputStream = null;
@ -60,11 +60,11 @@ public class JingleInbandTransport extends JingleTransport {
} }
}; };
public JingleInbandTransport(final JingleConnection connection, final String sid, final int blocksize) { JingleInBandTransport(final JingleConnection connection, final String sid, final int blockSize) {
this.connection = connection; this.connection = connection;
this.account = connection.getAccount(); this.account = connection.getAccount();
this.counterpart = connection.getCounterPart(); this.counterpart = connection.getCounterPart();
this.blockSize = blocksize; this.blockSize = blockSize;
this.sessionId = sid; this.sessionId = sid;
} }
@ -224,7 +224,7 @@ public class JingleInbandTransport extends JingleTransport {
} }
} }
public void deliverPayload(IqPacket packet, Element payload) { void deliverPayload(IqPacket packet, Element payload) {
if (payload.getName().equals("open")) { if (payload.getName().equals("open")) {
if (!established) { if (!established) {
established = true; established = true;