[jdev] jso api for SRV lookup
Matt Tucker
matt at jivesoftware.com
Wed Feb 2 18:02:12 CST 2005
Veronica,
Here's a code snippet that uses JNDI to do the lookup:
Regards,
Matt
/**
* Utilty class to perform DNS lookups.
*
* @author Matt Tucker
*/
public class DNSUtil {
private static DirContext context;
static {
try {
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
context = new InitialDirContext(env);
}
catch (Exception e) {
Log.error(e);
}
}
/**
* Returns the host name and port that the specified XMPP server can
be
* reached at for server-to-server communication. A DNS lookup for a
SRV
* record in the form "_xmpp-server._tcp.example.com" is attempted,
according
* to section 14.4 of RFC 3920. If that lookup fails, a lookup in
the older form
* of "_jabber._tcp.example.com" is attempted since servers that
implement an
* older version of the protocol may be listed using that notation.
If that
* lookup fails as well, it's assumed that the XMPP server lives at
the
* host resolved by a DNS lookup at the specified domain on the
default port
* of 5269.<p>
*
* As an example, a lookup for "example.com" may return
"im.example.com:5269".
*
* @param domain the domain.
* @return a HostAddress, which encompasses the hostname and port
that the XMPP
* server can be reached at for the specified domain.
*/
public static HostAddress resolveXMPPDomain(String domain) {
if (context == null) {
return new HostAddress(domain, 5269);
}
String host = domain;
int port = 5269;
try {
Attributes dnsLookup =
context.getAttributes("_xmpp-server._tcp." + domain);
String srvRecord = (String)dnsLookup.get("SRV").get();
String [] srvRecordEntries = srvRecord.split(" ");
port =
Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]);
host = srvRecordEntries[srvRecordEntries.length-1];
}
catch (Exception e) {
// Attempt lookup with older "jabber" name.
try {
Attributes dnsLookup =
context.getAttributes("_jabber._tcp." + domain);
String srvRecord = (String)dnsLookup.get("SRV").get();
String [] srvRecordEntries = srvRecord.split(" ");
port =
Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]);
host = srvRecordEntries[srvRecordEntries.length-1];
}
catch (Exception e2) { }
}
// Host entries in DNS should end with a ".".
if (host.endsWith(".")) {
host = host.substring(0, host.length()-1);
}
return new HostAddress(domain, port);
}
/**
* Encapsulates a hostname and port.
*/
public static class HostAddress {
private String host;
private int port;
private HostAddress(String host, int port) {
this.host = host;
this.port = port;
}
/**
* Returns the hostname.
*
* @return the hostname.
*/
public String getHost() {
return host;
}
/**
* Returns the port.
*
* @return the port.
*/
public int getPort() {
return port;
}
public String toString() {
return host + ":" + port;
}
}
}
> -----Original Message-----
> From: jdev-bounces at jabber.org
> [mailto:jdev-bounces at jabber.org] On Behalf Of Veronica Chau
> Sent: Wednesday, February 02, 2005 2:29 PM
> To: jdev at jabber.org
> Subject: [jdev] jso api for SRV lookup
>
> Is there any api available to do the DNS SRV lookup?
>
> Thanks.
>
> Veronica
>
>
>
>
>
>
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail - You care about security. So do we.
> http://promotions.yahoo.com/new_mail
> _______________________________________________
> jdev mailing list
> jdev at jabber.org
> http://mail.jabber.org/mailman/listinfo/jdev
>
More information about the JDev
mailing list