anotherim/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java

175 lines
6.1 KiB
Java
Raw Normal View History

package eu.siacs.conversations.xmpp.jingle;
2015-07-20 12:26:29 +00:00
import android.annotation.SuppressLint;
import android.util.Log;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
2015-07-20 12:26:29 +00:00
2014-08-31 14:28:21 +00:00
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Message;
2015-07-20 12:26:29 +00:00
import eu.siacs.conversations.entities.Transferable;
2014-10-14 10:02:48 +00:00
import eu.siacs.conversations.services.AbstractConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService;
2017-03-01 12:01:46 +00:00
import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
2018-03-05 17:30:40 +00:00
import rocks.xmpp.addr.Jid;
2014-10-13 23:06:45 +00:00
public class JingleConnectionManager extends AbstractConnectionManager {
2014-11-06 19:10:03 +00:00
private List<JingleConnection> connections = new CopyOnWriteArrayList<>();
2014-11-06 19:10:03 +00:00
private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
@SuppressLint("TrulyRandom")
private SecureRandom random = new SecureRandom();
public JingleConnectionManager(XmppConnectionService service) {
2014-10-13 23:06:45 +00:00
super(service);
}
public void deliverPacket(Account account, JinglePacket packet) {
if (packet.isAction("session-initiate")) {
JingleConnection connection = new JingleConnection(this);
2014-08-31 14:28:21 +00:00
connection.init(account, packet);
connections.add(connection);
} else {
for (JingleConnection connection : connections) {
2014-10-21 17:43:34 +00:00
if (connection.getAccount() == account
2014-08-31 14:28:21 +00:00
&& connection.getSessionId().equals(
packet.getSessionId())
&& connection.getCounterPart().equals(packet.getFrom())) {
connection.deliverPacket(packet);
return;
}
}
2014-12-30 13:16:25 +00:00
IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
2014-10-29 10:31:03 +00:00
Element error = response.addChild("error");
error.setAttribute("type", "cancel");
error.addChild("item-not-found",
"urn:ietf:params:xml:ns:xmpp-stanzas");
error.addChild("unknown-session", "urn:xmpp:jingle:errors:1");
account.getXmppConnection().sendIqPacket(response, null);
}
}
public JingleConnection createNewConnection(Message message) {
2015-07-10 13:11:03 +00:00
Transferable old = message.getTransferable();
if (old != null) {
old.cancel();
}
JingleConnection connection = new JingleConnection(this);
mXmppConnectionService.markMessage(message,Message.STATUS_WAITING);
connection.init(message);
this.connections.add(connection);
return connection;
}
2014-11-06 19:10:03 +00:00
public JingleConnection createNewConnection(final JinglePacket packet) {
JingleConnection connection = new JingleConnection(this);
this.connections.add(connection);
return connection;
}
2014-08-31 14:28:21 +00:00
public void finishConnection(JingleConnection connection) {
this.connections.remove(connection);
}
public void getPrimaryCandidate(Account account,
final OnPrimaryCandidateFound listener) {
if (Config.DISABLE_PROXY_LOOKUP) {
2014-11-14 13:04:34 +00:00
listener.onPrimaryCandidateFound(false, null);
return;
}
2018-03-05 17:30:40 +00:00
if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
2017-03-01 12:01:46 +00:00
final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
if (proxy != null) {
2014-12-30 13:16:25 +00:00
IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setTo(proxy);
2017-03-01 12:01:46 +00:00
iq.query(Namespace.BYTE_STREAMS);
account.getXmppConnection().sendIqPacket(iq,new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
2017-03-01 12:01:46 +00:00
Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
final String host = streamhost == null ? null : streamhost.getAttribute("host");
final String port = streamhost == null ? null : streamhost.getAttribute("port");
if (host != null && port != null) {
try {
JingleCandidate candidate = new JingleCandidate(nextRandomId(), true);
candidate.setHost(host);
candidate.setPort(Integer.parseInt(port));
candidate.setType(JingleCandidate.TYPE_PROXY);
candidate.setJid(proxy);
candidate.setPriority(655360 + 65535);
2018-03-05 17:30:40 +00:00
primaryCandidates.put(account.getJid().asBareJid(),candidate);
listener.onPrimaryCandidateFound(true,candidate);
} catch (final NumberFormatException e) {
listener.onPrimaryCandidateFound(false,null);
return;
}
} else {
listener.onPrimaryCandidateFound(false,null);
}
}
});
} else {
listener.onPrimaryCandidateFound(false, null);
}
} else {
listener.onPrimaryCandidateFound(true,
2018-03-05 17:30:40 +00:00
this.primaryCandidates.get(account.getJid().asBareJid()));
}
}
public String nextRandomId() {
return new BigInteger(50, random).toString(32);
}
2014-08-31 14:28:21 +00:00
2014-04-22 11:11:53 +00:00
public void deliverIbbPacket(Account account, IqPacket packet) {
String sid = null;
Element payload = null;
2014-08-31 14:28:21 +00:00
if (packet.hasChild("open", "http://jabber.org/protocol/ibb")) {
payload = packet.findChild("open", "http://jabber.org/protocol/ibb");
2014-04-22 11:11:53 +00:00
sid = payload.getAttribute("sid");
2014-08-31 14:28:21 +00:00
} else if (packet.hasChild("data", "http://jabber.org/protocol/ibb")) {
payload = packet.findChild("data", "http://jabber.org/protocol/ibb");
2014-04-22 11:11:53 +00:00
sid = payload.getAttribute("sid");
2017-01-20 21:37:50 +00:00
} else if (packet.hasChild("close","http://jabber.org/protocol/ibb")) {
payload = packet.findChild("close", "http://jabber.org/protocol/ibb");
sid = payload.getAttribute("sid");
2014-04-22 11:11:53 +00:00
}
2014-08-31 14:28:21 +00:00
if (sid != null) {
2014-04-22 11:11:53 +00:00
for (JingleConnection connection : connections) {
if (connection.getAccount() == account
&& connection.hasTransportId(sid)) {
2014-04-22 11:11:53 +00:00
JingleTransport transport = connection.getTransport();
if (transport instanceof JingleInbandTransport) {
JingleInbandTransport inbandTransport = (JingleInbandTransport) transport;
2014-08-31 14:28:21 +00:00
inbandTransport.deliverPayload(packet, payload);
2014-04-22 11:11:53 +00:00
return;
}
}
}
Log.d(Config.LOGTAG,"couldn't deliver payload: " + payload.toString());
2014-04-22 11:11:53 +00:00
} else {
Log.d(Config.LOGTAG, "no sid found in incoming ibb packet");
2014-04-22 11:11:53 +00:00
}
}
2014-08-31 14:28:21 +00:00
public void cancelInTransmission() {
2014-08-31 14:28:21 +00:00
for (JingleConnection connection : this.connections) {
if (connection.getJingleStatus() == JingleConnection.JINGLE_STATUS_TRANSMITTING) {
2018-10-08 08:31:41 +00:00
connection.abort();
}
}
}
}