Debian Codegraph @

xmpp-vala/src/core/stream_connect.vala

raw
1 namespace Xmpp {
2
3 private class SrvTargetInfo {
4 public string host { get; set; }
5 public uint16 port { get; set; }
6 public string service { get; set; }
7 public uint16 priority { get; set; }
8 }
9
10 public class XmppStreamResult {
11 public TlsXmppStream? stream { get; set; }
12 public TlsCertificateFlags? tls_errors { get; set; }
13 public IOError? io_error { get; set; }
14 }
15
16 public async XmppStreamResult establish_stream(Jid bare_jid, Gee.List<XmppStreamModule> modules, string? log_options, owned TlsXmppStream.OnInvalidCert on_invalid_cert) {
17 Jid remote = bare_jid.domain_jid;
18 TlsXmppStream.OnInvalidCertWrapper on_invalid_cert_wrapper = new TlsXmppStream.OnInvalidCertWrapper(on_invalid_cert);
19
20 //Lookup xmpp-client and xmpps-client SRV records
21 GLib.List<SrvTargetInfo>? targets = new GLib.List<SrvTargetInfo>();
22 GLibFixes.Resolver resolver = GLibFixes.Resolver.get_default();
23 try {
24 GLib.List<SrvTarget> xmpp_services = yield resolver.lookup_service_async("xmpp-client", "tcp", remote.to_string(), null);
25 foreach (SrvTarget service in xmpp_services) {
26 targets.append(new SrvTargetInfo() { host=service.get_hostname(), port=service.get_port(), service="xmpp-client", priority=service.get_priority()});
27 }
28 } catch (Error e) {
29 debug("Got no xmpp-client DNS records for %s: %s", remote.to_string(), e.message);
30 }
31 try {
32 GLib.List<SrvTarget> xmpp_services = yield resolver.lookup_service_async("xmpps-client", "tcp", remote.to_string(), null);
33 foreach (SrvTarget service in xmpp_services) {
34 targets.append(new SrvTargetInfo() { host=service.get_hostname(), port=service.get_port(), service="xmpps-client", priority=service.get_priority()});
35 }
36 } catch (Error e) {
37 debug("Got no xmpps-client DNS records for %s: %s", remote.to_string(), e.message);
38 }
39
40 targets.sort((a, b) => {
41 return a.priority - b.priority;
42 });
43
44 // Add fallback connection
45 bool should_add_fallback = true;
46 foreach (SrvTargetInfo target in targets) {
47 if (target.service == "xmpp-client" && target.port == 5222 && target.host == remote.to_string()) {
48 should_add_fallback = false;
49 }
50 }
51 if (should_add_fallback) {
52 targets.append(new SrvTargetInfo() { host=remote.to_string(), port=5222, service="xmpp-client", priority=uint16.MAX});
53 }
54
55 // Try all connection options from lowest to highest priority
56 TlsXmppStream? stream = null;
57 TlsCertificateFlags? tls_errors = null;
58 IOError? io_error = null;
59 uint connection_timeout_id = 0;
60 foreach (SrvTargetInfo target in targets) {
61 try {
62 if (target.service == "xmpp-client") {
63 stream = new StartTlsXmppStream(remote, target.host, target.port, on_invalid_cert_wrapper);
64 } else {
65 stream = new DirectTlsXmppStream(remote, target.host, target.port, on_invalid_cert_wrapper);
66 }
67 stream.log = new XmppLog(bare_jid.to_string(), log_options);
68
69 foreach (XmppStreamModule module in modules) {
70 stream.add_module(module);
71 }
72
73 connection_timeout_id = Timeout.add_seconds(30, () => {
74 warning("Connection attempt timed out");
75 stream.disconnect();
76 return Source.REMOVE;
77 });
78
79 yield stream.connect();
80
81 Source.remove(connection_timeout_id);
82 connection_timeout_id = 0;
83
84 return new XmppStreamResult() { stream=stream };
85 } catch (IOError e) {
86 warning("Could not establish XMPP session with %s:%i: %s", target.host, target.port, e.message);
87
88 if (stream != null) {
89 if (connection_timeout_id != 0) {
90 Source.remove(connection_timeout_id);
91 connection_timeout_id = 0;
92 }
93 if (stream.errors != null) {
94 tls_errors = stream.errors;
95 }
96 io_error = e;
97 stream.detach_modules();
98 }
99 }
100 }
101
102 return new XmppStreamResult() { io_error=io_error, tls_errors=tls_errors };
103 }
104 }