aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0177_jingle_raw_udp.vala
blob: 200cdfa9e4b5ab969f24068e4b6fd339533c15e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using Gee;
using Xmpp.Xep;
using Xmpp;

namespace Xmpp.Xep.JingleRawUdp {

    public const string NS_URI = "urn:xmpp:jingle:transports:raw-udp:1";

    public delegate Gee.List<string> GetLocalIpAddresses();

    public class Module : XmppStreamModule, Jingle.Transport {
        public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0177_jingle_raw_udp");

        private GetLocalIpAddresses? get_local_ip_addresses_impl = null;

        public override void attach(XmppStream stream) {
            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) {
            stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
        }

        public override string get_ns() { return NS_URI; }
        public override string get_id() { return IDENTITY.id; }

        public async bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid) {
            return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI);
        }

        public string ns_uri{ get { return NS_URI; } }
        public Jingle.TransportType type_{ get { return Jingle.TransportType.DATAGRAM; } }
        public int priority { get { return 1; } }

        public void set_local_ip_address_handler(owned GetLocalIpAddresses get_local_ip_addresses) {
            get_local_ip_addresses_impl = (owned)get_local_ip_addresses;
        }

        public Gee.List<string> get_local_ip_addresses() {
            if (get_local_ip_addresses_impl == null) {
                return Gee.List.empty();
            }
            return get_local_ip_addresses_impl();
        }

        public Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) {
            return new TransportParameters(components, null);
        }

        public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError {
            return new TransportParameters(components, transport);
        }
    }

    public class TransportParameters : Jingle.TransportParameters, Object {
        public string ns_uri { get { return NS_URI; } }
        public uint8 components { get; }

        public Gee.List<Candidate> remote_candidates = new ArrayList<Candidate>();
        public Gee.List<Candidate> own_candidates = new ArrayList<Candidate>();

        public TransportParameters(uint8 components, StanzaNode? node = null) {
//            this.components = components;
            if (node != null) {
                foreach (StanzaNode candidate_node in node.get_subnodes("candidate")) {
                    Candidate candidate = new Candidate();
                    string component_str = candidate_node.get_attribute("component");
                    candidate.component = int.parse(component_str);
                    string generation_str = candidate_node.get_attribute("generation");
                    candidate.generation = int.parse(generation_str);
                    candidate.id = candidate_node.get_attribute("generation");
                    string ip_str = candidate_node.get_attribute("ip");
                    candidate.ip = new InetAddress.from_string(ip_str);
                    string port_str = candidate_node.get_attribute("port");
                    candidate.port = int.parse(port_str);

                    remote_candidates.add(candidate);
                }
            }
        }

        public void set_content(Jingle.Content content) {

        }

        public StanzaNode to_transport_stanza_node(string action_type) {
            StanzaNode transport_node = new StanzaNode.build("transport", NS_URI).add_self_xmlns();
            foreach (Candidate candidate in own_candidates) {
                transport_node.put_node(new StanzaNode.build("candidate", NS_URI)
                        .put_attribute("generation", candidate.generation.to_string())
                        .put_attribute("id", candidate.id)
                        .put_attribute("ip", candidate.ip.to_string())
                        .put_attribute("port", candidate.port.to_string()));
            }
            return transport_node;
        }

        public void handle_transport_accept(StanzaNode transport) throws Jingle.IqError {

        }

        public void handle_transport_info(StanzaNode transport) throws Jingle.IqError {

        }

        public void create_transport_connection(XmppStream stream, Jingle.Content content) {

        }
    }

    public class Candidate : Object {
        public int component { get; set; }
        public int generation { get; set; }
        public string id { get; set; }
        public InetAddress ip { get; set; }
        public uint port { get; set; }
    }
}