Support for SIP SRV

cusax-fix
Damian Minkov 18 years ago
parent 5b460b156d
commit 7f551fb1bb

@ -316,7 +316,7 @@ private void connectAndLogin(SecurityAuthority authority, int reasonCode)
// check to see is there SRV records for this server domain
try
{
String hosts[] =
InetSocketAddress hosts[] =
NetworkUtils.getSRVRecords(
"_xmpp-client._tcp." + serviceName);
@ -324,7 +324,7 @@ private void connectAndLogin(SecurityAuthority authority, int reasonCode)
{
logger.trace("Will set server address from SRV records "
+ hosts[0]);
serverAddress = hosts[0];
serverAddress = hosts[0].getHostName();
}
}
catch (ParseException ex1)

@ -0,0 +1,70 @@
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.protocol.sip;
import java.net.*;
import javax.sip.*;
import javax.sip.address.*;
import gov.nist.core.net.*;
import gov.nist.javax.sip.stack.*;
import net.java.sip.communicator.util.*;
/**
* Lookup for SRV records for given host. If nothing found
* the original host is returned this way when a Socket
* is constructed another dns lookup will be made for the A record.
*
* @author Damian Minkov
*/
public class AddressResolverImpl
implements AddressResolver
{
private static final Logger logger
= Logger.getLogger(AddressResolverImpl.class);
public Hop resolveAddress(Hop inputAddress)
{
try
{
String lookupStr = null;
if(inputAddress.getTransport().equalsIgnoreCase(ListeningPoint.UDP))
lookupStr = "_sip._udp." + inputAddress.getHost();
else if(inputAddress.getTransport().equalsIgnoreCase(ListeningPoint.TCP))
lookupStr = "_sip._tcp." + inputAddress.getHost();
else if(inputAddress.getTransport().equalsIgnoreCase(ListeningPoint.TLS))
lookupStr = "_sips._tcp." + inputAddress.getHost();
InetSocketAddress hosts[] = NetworkUtils.getSRVRecords(lookupStr);
if(hosts != null && hosts.length > 0)
{
logger.trace("Will set server address from SRV records "
+ hosts[0]);
return new HopImpl(
hosts[0].getHostName(),
hosts[0].getPort(),
inputAddress.getTransport());
}
}
catch (Exception ex)
{
logger.error("Domain not resolved " + ex.getMessage());
}
if (inputAddress.getPort() != -1)
return inputAddress;
else
{
return new HopImpl(inputAddress.getHost(),
MessageProcessor.getDefaultPort(
inputAddress.getTransport()),inputAddress.getTransport());
}
}
}

@ -21,6 +21,7 @@
import gov.nist.javax.sip.header.*;
import gov.nist.javax.sip.address.*;
import gov.nist.javax.sip.message.*;
import gov.nist.javax.sip.stack.*;
import net.java.sip.communicator.impl.protocol.sip.security.*;
/**
@ -845,7 +846,7 @@ protected void initialize(String sipAddress,
//init the security manager
this.sipSecurityManager = new SipSecurityManager(accountID);
sipSecurityManager.setHeaderFactory(headerFactory);
isInitialized = true;
}
}
@ -915,6 +916,17 @@ private void initListeningPoints(int preferredPortNumber)
tlsJainSipProvider
= jainSipStack.createSipProvider(tlsListeningPoint);
tlsJainSipProvider.addSipListener(this);
// set our custom address resolver managing SRV records
AddressResolverImpl addressResolver =
new AddressResolverImpl();
((SIPTransactionStack)udpJainSipProvider.getSipStack()).
setAddressResolver(addressResolver);
((SIPTransactionStack)tcpJainSipProvider.getSipStack()).
setAddressResolver(addressResolver);
((SIPTransactionStack)tlsJainSipProvider.getSipStack()).
setAddressResolver(addressResolver);
}
catch (ObjectInUseException ex)
{
@ -1767,7 +1779,7 @@ private void initRegistrarConnection(SipAccountID accountID)
.get(ProtocolProviderFactory.USER_ID);
registrarAddressStr = userID.substring( userID.indexOf("@")+1);
}
InetAddress registrarAddress = null;
try
@ -1955,7 +1967,40 @@ private void initOutboundProxy(SipAccountID accountID,
try
{
proxyAddress = InetAddress.getByName(proxyAddressStr);
// first check for srv records exists
try
{
String lookupStr = null;
String proxyTransport = (String) accountID.getAccountProperties()
.get(ProtocolProviderFactory.PREFERRED_TRANSPORT);
if(proxyTransport == null)
proxyTransport = getDefaultTransport();
if(proxyTransport.equalsIgnoreCase(ListeningPoint.UDP))
lookupStr = "_sip._udp." + proxyAddressStr;
else if(proxyTransport.equalsIgnoreCase(ListeningPoint.TCP))
lookupStr = "_sip._tcp." + proxyAddressStr;
else if(proxyTransport.equalsIgnoreCase(ListeningPoint.TLS))
lookupStr = "_sips._tcp." + proxyAddressStr;
InetSocketAddress hosts[] = NetworkUtils.getSRVRecords(lookupStr);
if(hosts != null && hosts.length > 0)
{
logger.trace("Will set server address from SRV records "
+ hosts[0]);
proxyAddressStr = hosts[0].getHostName();
}
}
catch (Exception ex)
{
// no SRV record or error looking for it
}
proxyAddress = InetAddress.getByName(proxyAddressStr);
// We should set here the property to indicate that the proxy
// address is validated. When we load stored accounts we check

@ -154,11 +154,11 @@ public static boolean isIPv6Address(String address)
* The records are ordered against the SRV record priority
* @param domain the name of the domain we'd like to resolve (_proto._tcp
* included).
* @return an array of Strings containing records returned by the DNS
* server.
* @throws ParseException if <tt>domain</tt> is not a valide domain name.
* @return an array of InetSocketAddress containing records returned by the DNS
* server - address and port .
* @throws ParseException if <tt>domain</tt> is not a valid domain name.
*/
public static String[] getSRVRecords(String domain)
public static InetSocketAddress[] getSRVRecords(String domain)
throws ParseException
{
Record[] records = null;
@ -202,10 +202,11 @@ public int compare(Object o1, Object o2)
});
/* put sorted host names in an array, get rid of any trailing '.' */
String[] sortedHostNames = new String[pvhn.length];
InetSocketAddress[] sortedHostNames = new InetSocketAddress[pvhn.length];
for (int i = 0; i < pvhn.length; i++)
{
sortedHostNames[i] = pvhn[i][3];
sortedHostNames[i] =
new InetSocketAddress(pvhn[i][3], Integer.valueOf(pvhn[i][2]));
}
if (logger.isTraceEnabled())

Loading…
Cancel
Save