From 82e7cf4447d72c24af04c64c05eed35338455f35 Mon Sep 17 00:00:00 2001 From: hrxi Date: Sun, 23 Jun 2019 14:51:33 +0200 Subject: Add file receiving via Jingle This currently follows the same rules as HTTP file download for accepting files. --- .../xep/0261_jingle_in_band_bytestreams.vala | 72 +++++++++++----------- 1 file changed, 37 insertions(+), 35 deletions(-) (limited to 'xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala') diff --git a/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala b/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala index 57dbaaa3..dc2e8d7c 100644 --- a/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala @@ -5,12 +5,13 @@ namespace Xmpp.Xep.JingleInBandBytestreams { private const string NS_URI = "urn:xmpp:jingle:transports:ibb:1"; private const int DEFAULT_BLOCKSIZE = 4096; +private const int MAX_BLOCKSIZE = 65535; public class Module : Jingle.Transport, XmppStreamModule { public static Xmpp.ModuleIdentity IDENTITY = new Xmpp.ModuleIdentity(NS_URI, "0261_jingle_in_band_bytestreams"); public override void attach(XmppStream stream) { - stream.get_module(Jingle.Module.IDENTITY).add_transport(stream, this); + stream.get_module(Jingle.Module.IDENTITY).register_transport(this); stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); } public override void detach(XmppStream stream) { } @@ -23,52 +24,53 @@ public class Module : Jingle.Transport, XmppStreamModule { return result != null && result; } + public string transport_ns_uri() { + return NS_URI; + } public Jingle.TransportType transport_type() { return Jingle.TransportType.STREAMING; } - public StanzaNode to_transport_stanza_node() { - return new StanzaNode.build("transport", NS_URI) - .add_self_xmlns() - .put_attribute("block-size", DEFAULT_BLOCKSIZE.to_string()) - .put_attribute("sid", random_uuid()); + public Jingle.TransportParameters create_transport_parameters() { + return new Parameters(random_uuid(), DEFAULT_BLOCKSIZE); + } + public Jingle.TransportParameters parse_transport_parameters(StanzaNode transport) throws Jingle.IqError { + return Parameters.parse(transport); } +} - public Jingle.Connection? create_transport_connection(XmppStream stream, Jid peer_full_jid, StanzaNode content) throws Jingle.CreateConnectionError { - StanzaNode? transport = content.get_subnode("transport", NS_URI); - if (transport == null) { - return null; - } +class Parameters : Jingle.TransportParameters, Object { + public string sid { get; private set; } + public int block_size { get; private set; } + public Parameters(string sid, int block_size) { + this.sid = sid; + this.block_size = block_size; + } + public static Parameters parse(StanzaNode transport) throws Jingle.IqError { string? sid = transport.get_attribute("sid"); int block_size = transport.get_attribute_int("block-size"); - if (sid == null || block_size <= 0) { - throw new Jingle.CreateConnectionError.BAD_REQUEST("Invalid IBB parameters"); - } - if (block_size > DEFAULT_BLOCKSIZE) { - throw new Jingle.CreateConnectionError.NOT_ACCEPTABLE("Invalid IBB parameters: peer increased block size"); + if (sid == null || block_size <= 0 || block_size > MAX_BLOCKSIZE) { + throw new Jingle.IqError.BAD_REQUEST("missing or invalid sid or blocksize"); } - return new Connection(peer_full_jid, new InBandBytestreams.Connection(peer_full_jid, sid, block_size)); + return new Parameters(sid, block_size); } -} - -public class Connection : Jingle.Connection { - InBandBytestreams.Connection inner; - - public Connection(Jid full_jid, InBandBytestreams.Connection inner) { - base(full_jid); - inner.on_error.connect((stream, error) => on_error(stream, new Jingle.Error.TRANSPORT_ERROR(error))); - inner.on_data.connect((stream, data) => on_data(stream, data)); - inner.on_ready.connect((stream) => on_ready(stream)); - this.inner = inner; + public string transport_ns_uri() { + return NS_URI; } - - public override void connect(XmppStream stream) { - inner.connect(stream); + public StanzaNode to_transport_stanza_node() { + return new StanzaNode.build("transport", NS_URI) + .add_self_xmlns() + .put_attribute("block-size", block_size.to_string()) + .put_attribute("sid", sid); } - public override void send(XmppStream stream, uint8[] data) { - inner.send(stream, data); + public void update_transport(StanzaNode transport) throws Jingle.IqError { + Parameters other = Parameters.parse(transport); + if (other.sid != sid || other.block_size > block_size) { + throw new Jingle.IqError.NOT_ACCEPTABLE("invalid IBB sid or block_size"); + } + block_size = other.block_size; } - public override void close(XmppStream stream) { - inner.close(stream); + public IOStream create_transport_connection(XmppStream stream, Jid peer_full_jid, Jingle.Role role) { + return InBandBytestreams.Connection.create(stream, peer_full_jid, sid, block_size, role == Jingle.Role.INITIATOR); } } -- cgit v1.2.3-54-g00ecf