* @param transport the transport protocol
* @throws IllegalStateException in case no local socket was found
*/
public static Socket getLocalSocket(SipTransports transport)
throws IllegalStateException {
Socket localAddress = null;
InetSocketAddress localSAddr = null;
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER,
"Get first suitable local SipBindingCtx for TCP.");
}
SipBindingCtx ctx = SipBindingResolver.instance().
getActiveInternalContext(transport);
if (ctx != null) {
TargetTuple tt = ctx.getTargetTupleForProtocol(transport);
if (tt != null) {
localSAddr = tt.getSocketAddress();
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER,
"Suitable local SipBindingCtx found, using: " + ctx +
", resolved address: " + localSAddr);
}
}
}
if (localSAddr != null) {
String addr = localSAddr.getAddress().getHostAddress();
if ("0.0.0.0".equals(addr)) {
try {
addr = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException ex) {
throw new IllegalStateException("Can't resolve local address.",
ex);
}
}
localAddress = new Socket(addr, localSAddr.getPort());
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER,
"Local address resolved to: " + localAddress);
}
} else {
if (System.getProperty("clb.unittest.localsocket") != null) {
return new Socket("127.0.0.1", 5060);
}
throw new IllegalStateException("Can't resolve local address for '" +
transport +
"', there exists no listener for that transport; check your sip-listener configuration!");