2014-04-08 21:15:55 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
2015-07-20 12:26:29 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
2014-04-19 00:19:26 +00:00
|
|
|
import java.util.HashMap;
|
2020-04-01 08:45:03 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2015-07-20 12:26:29 +00:00
|
|
|
|
2014-08-31 14:28:21 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-04-08 21:15:55 +00:00
|
|
|
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;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
2020-04-01 08:45:03 +00:00
|
|
|
import eu.siacs.conversations.xml.Namespace;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
2020-04-01 16:35:36 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
|
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
|
2014-04-08 21:15:55 +00:00
|
|
|
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-04-08 21:15:55 +00:00
|
|
|
|
2014-10-13 23:06:45 +00:00
|
|
|
public class JingleConnectionManager extends AbstractConnectionManager {
|
2020-04-01 08:45:03 +00:00
|
|
|
private Map<AbstractJingleConnection.Id, AbstractJingleConnection> connections = new ConcurrentHashMap<>();
|
2019-12-05 13:03:54 +00:00
|
|
|
|
|
|
|
private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
|
|
|
|
|
|
|
|
public JingleConnectionManager(XmppConnectionService service) {
|
|
|
|
super(service);
|
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
public void deliverPacket(final Account account, final JinglePacket packet) {
|
|
|
|
final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, packet);
|
2020-04-02 14:29:33 +00:00
|
|
|
final AbstractJingleConnection existingJingleConnection = connections.get(id);
|
|
|
|
if (existingJingleConnection != null) {
|
|
|
|
existingJingleConnection.deliverPacket(packet);
|
|
|
|
} else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) {
|
2020-04-03 06:16:55 +00:00
|
|
|
final Jid from = packet.getFrom();
|
2020-04-01 16:35:36 +00:00
|
|
|
final Content content = packet.getJingleContent();
|
|
|
|
final String descriptionNamespace = content == null ? null : content.getDescriptionNamespace();
|
|
|
|
final AbstractJingleConnection connection;
|
|
|
|
if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) {
|
2020-04-03 06:16:55 +00:00
|
|
|
connection = new JingleFileTransferConnection(this, id, from);
|
2020-04-02 14:29:33 +00:00
|
|
|
} else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace)) {
|
2020-04-03 06:16:55 +00:00
|
|
|
connection = new JingleRtpConnection(this, id, from);
|
2020-04-01 16:35:36 +00:00
|
|
|
} else {
|
|
|
|
//TODO return feature-not-implemented
|
|
|
|
return;
|
|
|
|
}
|
2020-04-01 08:45:03 +00:00
|
|
|
connections.put(id, connection);
|
2020-04-01 16:35:36 +00:00
|
|
|
connection.deliverPacket(packet);
|
2019-12-05 13:03:54 +00:00
|
|
|
} else {
|
2020-04-02 14:29:33 +00:00
|
|
|
Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
|
|
|
|
final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
|
|
|
|
final 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 void deliverMessage(final Account account, final Jid to, final Jid from, final Element message) {
|
|
|
|
Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
|
|
|
|
final String sessionId = message.getAttribute("id");
|
|
|
|
if (sessionId == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Jid with;
|
|
|
|
if (account.getJid().asBareJid().equals(from.asBareJid())) {
|
|
|
|
with = to;
|
|
|
|
} else {
|
|
|
|
with = from;
|
|
|
|
}
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received jingle message from " + from + " with=" + with + " " + message);
|
|
|
|
final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with, sessionId);
|
|
|
|
final AbstractJingleConnection existingJingleConnection = connections.get(id);
|
|
|
|
if (existingJingleConnection != null) {
|
|
|
|
if (existingJingleConnection instanceof JingleRtpConnection) {
|
2020-04-03 08:46:42 +00:00
|
|
|
((JingleRtpConnection) existingJingleConnection).deliveryMessage(from, message);
|
2020-04-02 14:29:33 +00:00
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + existingJingleConnection.getClass().getName() + " does not support jingle messages");
|
|
|
|
}
|
|
|
|
} else if ("propose".equals(message.getName())) {
|
|
|
|
final Element description = message.findChild("description");
|
|
|
|
final String namespace = description == null ? null : description.getNamespace();
|
|
|
|
if (Namespace.JINGLE_APPS_RTP.equals(namespace)) {
|
2020-04-03 06:16:55 +00:00
|
|
|
final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, with);
|
2020-04-02 14:29:33 +00:00
|
|
|
this.connections.put(id, rtpConnection);
|
2020-04-03 08:46:42 +00:00
|
|
|
rtpConnection.deliveryMessage(from, message);
|
2020-04-01 08:45:03 +00:00
|
|
|
} else {
|
2020-04-02 14:29:33 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to react to proposed " + namespace + " session");
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
2020-04-02 14:29:33 +00:00
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retrieved out of order jingle message");
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
2020-04-02 14:29:33 +00:00
|
|
|
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
public void startJingleFileTransfer(final Message message) {
|
|
|
|
Preconditions.checkArgument(message.isFileOrImage(), "Message is not of type file or image");
|
|
|
|
final Transferable old = message.getTransferable();
|
2019-12-05 13:03:54 +00:00
|
|
|
if (old != null) {
|
|
|
|
old.cancel();
|
|
|
|
}
|
2020-04-03 06:16:55 +00:00
|
|
|
final Account account = message.getConversation().getAccount();
|
2020-04-01 08:45:03 +00:00
|
|
|
final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
|
2020-04-03 06:16:55 +00:00
|
|
|
final JingleFileTransferConnection connection = new JingleFileTransferConnection(this, id, account.getJid());
|
2019-12-05 13:03:54 +00:00
|
|
|
mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
|
2020-04-01 08:45:03 +00:00
|
|
|
this.connections.put(id, connection);
|
2020-04-03 06:16:55 +00:00
|
|
|
connection.init(message);
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
void finishConnection(final AbstractJingleConnection connection) {
|
|
|
|
this.connections.remove(connection.getId());
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) {
|
2019-12-05 13:03:54 +00:00
|
|
|
if (Config.DISABLE_PROXY_LOOKUP) {
|
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
|
|
|
|
final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
|
|
|
|
if (proxy != null) {
|
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
|
|
|
|
iq.setTo(proxy);
|
|
|
|
iq.query(Namespace.BYTE_STREAMS);
|
|
|
|
account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2020-04-01 08:45:03 +00:00
|
|
|
final Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
|
2019-12-05 13:03:54 +00:00
|
|
|
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 + (initiator ? 30 : 0));
|
|
|
|
primaryCandidates.put(account.getJid().asBareJid(), candidate);
|
|
|
|
listener.onPrimaryCandidateFound(true, candidate);
|
|
|
|
} catch (final NumberFormatException e) {
|
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
listener.onPrimaryCandidateFound(true,
|
|
|
|
this.primaryCandidates.get(account.getJid().asBareJid()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
static String nextRandomId() {
|
|
|
|
return UUID.randomUUID().toString();
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void deliverIbbPacket(Account account, IqPacket packet) {
|
2020-04-01 13:17:38 +00:00
|
|
|
final String sid;
|
|
|
|
final Element payload;
|
2020-04-01 08:45:03 +00:00
|
|
|
if (packet.hasChild("open", Namespace.IBB)) {
|
|
|
|
payload = packet.findChild("open", Namespace.IBB);
|
2019-12-05 13:03:54 +00:00
|
|
|
sid = payload.getAttribute("sid");
|
2020-04-01 08:45:03 +00:00
|
|
|
} else if (packet.hasChild("data", Namespace.IBB)) {
|
|
|
|
payload = packet.findChild("data", Namespace.IBB);
|
2019-12-05 13:03:54 +00:00
|
|
|
sid = payload.getAttribute("sid");
|
2020-04-01 08:45:03 +00:00
|
|
|
} else if (packet.hasChild("close", Namespace.IBB)) {
|
|
|
|
payload = packet.findChild("close", Namespace.IBB);
|
2019-12-05 13:03:54 +00:00
|
|
|
sid = payload.getAttribute("sid");
|
2020-04-01 13:17:38 +00:00
|
|
|
} else {
|
|
|
|
payload = null;
|
|
|
|
sid = null;
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
if (sid != null) {
|
2020-04-01 08:45:03 +00:00
|
|
|
for (final AbstractJingleConnection connection : this.connections.values()) {
|
|
|
|
if (connection instanceof JingleFileTransferConnection) {
|
|
|
|
final JingleFileTransferConnection fileTransfer = (JingleFileTransferConnection) connection;
|
|
|
|
final JingleTransport transport = fileTransfer.getTransport();
|
2019-12-05 13:03:54 +00:00
|
|
|
if (transport instanceof JingleInBandTransport) {
|
2020-04-01 13:17:38 +00:00
|
|
|
final JingleInBandTransport inBandTransport = (JingleInBandTransport) transport;
|
|
|
|
if (inBandTransport.matches(account, sid)) {
|
|
|
|
inBandTransport.deliverPayload(packet, payload);
|
|
|
|
}
|
2019-12-05 13:03:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
|
|
|
|
account.getXmppConnection().sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelInTransmission() {
|
2020-04-01 08:45:03 +00:00
|
|
|
for (AbstractJingleConnection connection : this.connections.values()) {
|
|
|
|
/*if (connection.getJingleStatus() == JingleFileTransferConnection.JINGLE_STATUS_TRANSMITTING) {
|
2019-12-05 13:03:54 +00:00
|
|
|
connection.abort("connectivity-error");
|
2020-04-01 08:45:03 +00:00
|
|
|
}*/
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|