| 1 |
using Gee; |
| 2 |
using Xmpp.Xep; |
| 3 |
using Xmpp; |
| 4 |
|
| 5 |
namespace Xmpp.Xep.JingleRawUdp { |
| 6 |
|
| 7 |
public const string NS_URI = "urn:xmpp:jingle:transports:raw-udp:1"; |
| 8 |
|
| 9 |
public delegate Gee.List<string> GetLocalIpAddresses(); |
| 10 |
|
| 11 |
public class Module : XmppStreamModule, Jingle.Transport { |
| 12 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0177_jingle_raw_udp"); |
| 13 |
|
| 14 |
private GetLocalIpAddresses? get_local_ip_addresses_impl = null; |
| 15 |
|
| 16 |
public override void attach(XmppStream stream) { |
| 17 |
stream.get_module(Jingle.Module.IDENTITY).register_transport(this); |
| 18 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); |
| 19 |
} |
| 20 |
public override void detach(XmppStream stream) { |
| 21 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); |
| 22 |
} |
| 23 |
|
| 24 |
public override string get_ns() { return NS_URI; } |
| 25 |
public override string get_id() { return IDENTITY.id; } |
| 26 |
|
| 27 |
public async bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid) { |
| 28 |
return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); |
| 29 |
} |
| 30 |
|
| 31 |
public string ns_uri{ get { return NS_URI; } } |
| 32 |
public Jingle.TransportType type_{ get { return Jingle.TransportType.DATAGRAM; } } |
| 33 |
public int priority { get { return 1; } } |
| 34 |
|
| 35 |
public void set_local_ip_address_handler(owned GetLocalIpAddresses get_local_ip_addresses) { |
| 36 |
get_local_ip_addresses_impl = (owned)get_local_ip_addresses; |
| 37 |
} |
| 38 |
|
| 39 |
public Gee.List<string> get_local_ip_addresses() { |
| 40 |
if (get_local_ip_addresses_impl == null) { |
| 41 |
return Gee.List.empty(); |
| 42 |
} |
| 43 |
return get_local_ip_addresses_impl(); |
| 44 |
} |
| 45 |
|
| 46 |
public Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) { |
| 47 |
return new TransportParameters(components, null); |
| 48 |
} |
| 49 |
|
| 50 |
public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { |
| 51 |
return new TransportParameters(components, transport); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public class TransportParameters : Jingle.TransportParameters, Object { |
| 56 |
public string ns_uri { get { return NS_URI; } } |
| 57 |
public uint8 components { get; } |
| 58 |
|
| 59 |
public Gee.List<Candidate> remote_candidates = new ArrayList<Candidate>(); |
| 60 |
public Gee.List<Candidate> own_candidates = new ArrayList<Candidate>(); |
| 61 |
|
| 62 |
public TransportParameters(uint8 components, StanzaNode? node = null) { |
| 63 |
// this.components = components; |
| 64 |
if (node != null) { |
| 65 |
foreach (StanzaNode candidate_node in node.get_subnodes("candidate")) { |
| 66 |
Candidate candidate = new Candidate(); |
| 67 |
string component_str = candidate_node.get_attribute("component"); |
| 68 |
candidate.component = int.parse(component_str); |
| 69 |
string generation_str = candidate_node.get_attribute("generation"); |
| 70 |
candidate.generation = int.parse(generation_str); |
| 71 |
candidate.id = candidate_node.get_attribute("generation"); |
| 72 |
string ip_str = candidate_node.get_attribute("ip"); |
| 73 |
candidate.ip = new InetAddress.from_string(ip_str); |
| 74 |
string port_str = candidate_node.get_attribute("port"); |
| 75 |
candidate.port = int.parse(port_str); |
| 76 |
|
| 77 |
remote_candidates.add(candidate); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public void set_content(Jingle.Content content) { |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
public StanzaNode to_transport_stanza_node(string action_type) { |
| 87 |
StanzaNode transport_node = new StanzaNode.build("transport", NS_URI).add_self_xmlns(); |
| 88 |
foreach (Candidate candidate in own_candidates) { |
| 89 |
transport_node.put_node(new StanzaNode.build("candidate", NS_URI) |
| 90 |
.put_attribute("generation", candidate.generation.to_string()) |
| 91 |
.put_attribute("id", candidate.id) |
| 92 |
.put_attribute("ip", candidate.ip.to_string()) |
| 93 |
.put_attribute("port", candidate.port.to_string())); |
| 94 |
} |
| 95 |
return transport_node; |
| 96 |
} |
| 97 |
|
| 98 |
public void handle_transport_accept(StanzaNode transport) throws Jingle.IqError { |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
public void handle_transport_info(StanzaNode transport) throws Jingle.IqError { |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
public void create_transport_connection(XmppStream stream, Jingle.Content content) { |
| 107 |
|
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
public class Candidate : Object { |
| 112 |
public int component { get; set; } |
| 113 |
public int generation { get; set; } |
| 114 |
public string id { get; set; } |
| 115 |
public InetAddress ip { get; set; } |
| 116 |
public uint port { get; set; } |
| 117 |
} |
| 118 |
} |