| 1 |
using Xmpp; |
| 2 |
using Xmpp.Xep; |
| 3 |
|
| 4 |
namespace Xmpp.Xep.JingleInBandBytestreams { |
| 5 |
|
| 6 |
private const string NS_URI = "urn:xmpp:jingle:transports:ibb:1"; |
| 7 |
private const int DEFAULT_BLOCKSIZE = 4096; |
| 8 |
private const int MAX_BLOCKSIZE = 65535; |
| 9 |
|
| 10 |
public class Module : Jingle.Transport, XmppStreamModule { |
| 11 |
public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0261_jingle_in_band_bytestreams"); |
| 12 |
|
| 13 |
public override void attach(XmppStream stream) { |
| 14 |
stream.get_module(Jingle.Module.IDENTITY).register_transport(this); |
| 15 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); |
| 16 |
} |
| 17 |
public override void detach(XmppStream stream) { |
| 18 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); |
| 19 |
} |
| 20 |
|
| 21 |
public override string get_ns() { return NS_URI; } |
| 22 |
public override string get_id() { return IDENTITY.id; } |
| 23 |
|
| 24 |
public async bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid) { |
| 25 |
return components == 1 && yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); |
| 26 |
} |
| 27 |
|
| 28 |
public string ns_uri { get { return NS_URI; } } |
| 29 |
public Jingle.TransportType type_ { get { return Jingle.TransportType.STREAMING; } } |
| 30 |
public int priority { get { return 0; } } |
| 31 |
public Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) { |
| 32 |
assert(components == 1); |
| 33 |
return new Parameters.create(peer_full_jid, random_uuid()); |
| 34 |
} |
| 35 |
public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { |
| 36 |
return Parameters.parse(peer_full_jid, transport); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
class Parameters : Jingle.TransportParameters, Object { |
| 41 |
public string ns_uri { get { return NS_URI; } } |
| 42 |
public uint8 components { get { return 1; } } |
| 43 |
public Jingle.Role role { get; private set; } |
| 44 |
public Jid peer_full_jid { get; private set; } |
| 45 |
public string sid { get; private set; } |
| 46 |
public int block_size { get; private set; } |
| 47 |
|
| 48 |
private Parameters(Jingle.Role role, Jid peer_full_jid, string sid, int block_size) { |
| 49 |
this.role = role; |
| 50 |
this.peer_full_jid = peer_full_jid; |
| 51 |
this.sid = sid; |
| 52 |
this.block_size = block_size; |
| 53 |
} |
| 54 |
|
| 55 |
public Parameters.create(Jid peer_full_jid, string sid) { |
| 56 |
this(Jingle.Role.INITIATOR, peer_full_jid, sid, DEFAULT_BLOCKSIZE); |
| 57 |
} |
| 58 |
|
| 59 |
public static Parameters parse(Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { |
| 60 |
string? sid = transport.get_attribute("sid"); |
| 61 |
int block_size = transport.get_attribute_int("block-size"); |
| 62 |
if (sid == null || block_size <= 0 || block_size > MAX_BLOCKSIZE) { |
| 63 |
throw new Jingle.IqError.BAD_REQUEST("missing or invalid sid or blocksize"); |
| 64 |
} |
| 65 |
return new Parameters(Jingle.Role.RESPONDER, peer_full_jid, sid, block_size); |
| 66 |
} |
| 67 |
|
| 68 |
public string transport_ns_uri() { |
| 69 |
return NS_URI; |
| 70 |
} |
| 71 |
|
| 72 |
public void set_content(Jingle.Content content) { |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public StanzaNode to_transport_stanza_node(string action_type) { |
| 77 |
return new StanzaNode.build("transport", NS_URI) |
| 78 |
.add_self_xmlns() |
| 79 |
.put_attribute("block-size", block_size.to_string()) |
| 80 |
.put_attribute("sid", sid); |
| 81 |
} |
| 82 |
|
| 83 |
public void handle_transport_accept(StanzaNode transport) throws Jingle.IqError { |
| 84 |
Parameters other = Parameters.parse(peer_full_jid, transport); |
| 85 |
if (other.sid != sid || other.block_size > block_size) { |
| 86 |
throw new Jingle.IqError.NOT_ACCEPTABLE("invalid IBB sid or block_size"); |
| 87 |
} |
| 88 |
block_size = other.block_size; |
| 89 |
} |
| 90 |
|
| 91 |
public void handle_transport_info(StanzaNode transport) throws Jingle.IqError { |
| 92 |
throw new Jingle.IqError.UNSUPPORTED_INFO("transport-info not supported for IBBs"); |
| 93 |
} |
| 94 |
|
| 95 |
public void create_transport_connection(XmppStream stream, Jingle.Content content) { |
| 96 |
IOStream iostream = InBandBytestreams.Connection.create(stream, peer_full_jid, sid, block_size, role == Jingle.Role.INITIATOR); |
| 97 |
Jingle.StreamingConnection connection = new Jingle.StreamingConnection(); |
| 98 |
if (content.security_params != null) { |
| 99 |
iostream = content.security_params.wrap_stream(iostream); |
| 100 |
} |
| 101 |
connection.set_stream.begin(iostream); |
| 102 |
debug("set transport conn ibb"); |
| 103 |
content.set_transport_connection(connection, 1); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
} |