From 2a32fc1e447b79bb5216fac2cd0242c40e74b96d Mon Sep 17 00:00:00 2001 From: Matt Goodall Date: Tue, 10 Jul 2012 10:35:39 +0100 Subject: [PATCH] Add home server from JID lookup helper. --- src/xmpp/dns.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/xmpp/dns.go diff --git a/src/xmpp/dns.go b/src/xmpp/dns.go new file mode 100644 index 0000000..b8015fe --- /dev/null +++ b/src/xmpp/dns.go @@ -0,0 +1,34 @@ +package xmpp + +import ( + "fmt" + "net" +) + +const ( + XMPP_CLIENT_PORT = 5222 +) + +// Perform a DNS SRV lookup and return an ordered list of "host:port" TCP +// addresses for the JID's home server. +func HomeServerAddrs(jid JID) (addr []string, err error) { + + // DNS lookup. + _, addrs, err := net.LookupSRV("xmpp-client", "tcp", jid.Domain) + if err != nil { + return + } + + // If there's nothing in DNS then assume the JID's domain and the standard + // port will work. + if len(addrs) == 0 { + addr = []string{fmt.Sprintf("%s:%d", jid.Domain, XMPP_CLIENT_PORT)} + return + } + + // Build list of "host:port" strings. + for _, a := range addrs { + addr = append(addr, fmt.Sprintf("%s:%d", a.Target, a.Port)) + } + return +}