2014-04-08 21:15:55 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.security.SecureRandom;
|
2014-04-19 00:19:26 +00:00
|
|
|
import java.util.HashMap;
|
2014-04-08 21:15:55 +00:00
|
|
|
import java.util.List;
|
2014-07-11 12:26:02 +00:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
import android.annotation.SuppressLint;
|
2014-04-08 21:15:55 +00:00
|
|
|
import android.util.Log;
|
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;
|
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;
|
|
|
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
2014-11-06 19:10:03 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
|
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-04-10 12:12:08 +00:00
|
|
|
|
2014-11-06 19:10:03 +00:00
|
|
|
private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-07-11 12:26:02 +00:00
|
|
|
@SuppressLint("TrulyRandom")
|
2014-04-08 21:15:55 +00:00
|
|
|
private SecureRandom random = new SecureRandom();
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
public JingleConnectionManager(XmppConnectionService service) {
|
2014-10-13 23:06:45 +00:00
|
|
|
super(service);
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
public void deliverPacket(Account account, JinglePacket packet) {
|
2014-04-13 09:32:45 +00:00
|
|
|
if (packet.isAction("session-initiate")) {
|
|
|
|
JingleConnection connection = new JingleConnection(this);
|
2014-08-31 14:28:21 +00:00
|
|
|
connection.init(account, packet);
|
2014-04-13 19:10:36 +00:00
|
|
|
connections.add(connection);
|
2014-04-13 09:32:45 +00:00
|
|
|
} 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())) {
|
2014-04-13 09:32:45 +00:00
|
|
|
connection.deliverPacket(packet);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
}
|
2014-10-29 10:31:03 +00:00
|
|
|
IqPacket response = packet.generateRespone(IqPacket.TYPE_ERROR);
|
|
|
|
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);
|
2014-04-10 12:12:08 +00:00
|
|
|
}
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
public JingleConnection createNewConnection(Message message) {
|
|
|
|
JingleConnection connection = new JingleConnection(this);
|
|
|
|
connection.init(message);
|
2014-05-13 12:49:09 +00:00
|
|
|
this.connections.add(connection);
|
2014-04-08 21:15:55 +00:00
|
|
|
return connection;
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-11-06 19:10:03 +00:00
|
|
|
public JingleConnection createNewConnection(final JinglePacket packet) {
|
2014-04-10 12:12:08 +00:00
|
|
|
JingleConnection connection = new JingleConnection(this);
|
2014-05-13 12:49:09 +00:00
|
|
|
this.connections.add(connection);
|
2014-04-10 12:12:08 +00:00
|
|
|
return connection;
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-05-13 12:49:09 +00:00
|
|
|
public void finishConnection(JingleConnection connection) {
|
|
|
|
this.connections.remove(connection);
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-11 19:13:09 +00:00
|
|
|
public void getPrimaryCandidate(Account account,
|
|
|
|
final OnPrimaryCandidateFound listener) {
|
|
|
|
if (!this.primaryCandidates.containsKey(account.getJid())) {
|
2014-04-08 21:15:55 +00:00
|
|
|
String xmlns = "http://jabber.org/protocol/bytestreams";
|
2014-04-10 12:12:08 +00:00
|
|
|
final String proxy = account.getXmppConnection()
|
|
|
|
.findDiscoItemByFeature(xmlns);
|
|
|
|
if (proxy != null) {
|
2014-04-08 21:15:55 +00:00
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
|
2014-11-06 19:10:03 +00:00
|
|
|
iq.setAttribute("to", proxy);
|
2014-04-08 21:15:55 +00:00
|
|
|
iq.query(xmlns);
|
2014-04-10 12:12:08 +00:00
|
|
|
account.getXmppConnection().sendIqPacket(iq,
|
|
|
|
new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account,
|
|
|
|
IqPacket packet) {
|
|
|
|
Element streamhost = packet
|
|
|
|
.query()
|
|
|
|
.findChild("streamhost",
|
|
|
|
"http://jabber.org/protocol/bytestreams");
|
|
|
|
if (streamhost != null) {
|
2014-08-31 14:28:21 +00:00
|
|
|
JingleCandidate candidate = new JingleCandidate(
|
|
|
|
nextRandomId(), true);
|
|
|
|
candidate.setHost(streamhost
|
|
|
|
.getAttribute("host"));
|
|
|
|
candidate.setPort(Integer
|
|
|
|
.parseInt(streamhost
|
|
|
|
.getAttribute("port")));
|
|
|
|
candidate
|
|
|
|
.setType(JingleCandidate.TYPE_PROXY);
|
2014-11-06 19:10:03 +00:00
|
|
|
try {
|
|
|
|
candidate.setJid(Jid.fromString(proxy));
|
|
|
|
} catch (final InvalidJidException e) {
|
|
|
|
candidate.setJid(null);
|
|
|
|
}
|
|
|
|
candidate.setPriority(655360 + 65535);
|
2014-04-11 19:13:09 +00:00
|
|
|
primaryCandidates.put(account.getJid(),
|
|
|
|
candidate);
|
|
|
|
listener.onPrimaryCandidateFound(true,
|
|
|
|
candidate);
|
2014-04-10 12:12:08 +00:00
|
|
|
} else {
|
2014-04-11 19:13:09 +00:00
|
|
|
listener.onPrimaryCandidateFound(false,
|
2014-04-10 12:12:08 +00:00
|
|
|
null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-04-08 21:15:55 +00:00
|
|
|
} else {
|
2014-04-11 19:13:09 +00:00
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
} else {
|
2014-04-11 19:13:09 +00:00
|
|
|
listener.onPrimaryCandidateFound(true,
|
|
|
|
this.primaryCandidates.get(account.getJid()));
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-10 12:12:08 +00:00
|
|
|
|
2014-04-08 21:15:55 +00:00
|
|
|
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");
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
if (sid != null) {
|
2014-04-22 11:11:53 +00:00
|
|
|
for (JingleConnection connection : connections) {
|
2014-10-22 11:06:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG,
|
|
|
|
"couldnt deliver payload: " + payload.toString());
|
2014-04-22 11:11:53 +00:00
|
|
|
} else {
|
2014-08-31 14:28:21 +00:00
|
|
|
Log.d(Config.LOGTAG, "no sid found in incomming ibb packet");
|
2014-04-22 11:11:53 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-06-29 11:44:59 +00:00
|
|
|
public void cancelInTransmission() {
|
2014-08-31 14:28:21 +00:00
|
|
|
for (JingleConnection connection : this.connections) {
|
2014-10-15 17:32:12 +00:00
|
|
|
if (connection.getJingleStatus() == JingleConnection.JINGLE_STATUS_TRANSMITTING) {
|
2014-06-29 11:44:59 +00:00
|
|
|
connection.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|