private InetAddress[] calcBindAddresses(final String addressString, boolean enforceBind)
{
ArrayList<InetAddress> addrs = new ArrayList<InetAddress>();
Pattern addressSplitter = Pattern.compile(";");
Pattern interfaceSplitter = Pattern.compile("[\\]\\[]");
String[] tokens = addressSplitter.split(addressString);
addressLoop:
for(int i=0;i<tokens.length;i++)
{
String currentAddress = tokens[i];
currentAddress = currentAddress.trim();
if ( currentAddress.length() == 0 ){
continue;
}
InetAddress parsedAddress = null;
try
{ // literal ipv4 or ipv6 address
if(currentAddress.indexOf('.') != -1 || currentAddress.indexOf(':') != -1)
parsedAddress = InetAddress.getByName(currentAddress);
} catch (Exception e)
{ // ignore, could be an interface name containing a ':'
}
if(parsedAddress != null)
{
try
{
// allow wildcard address as 1st address, otherwise only interface addresses
if((!parsedAddress.isAnyLocalAddress() || addrs.size() > 0) && NetworkInterface.getByInetAddress(parsedAddress) == null)
continue;
} catch (SocketException e)
{
Debug.printStackTrace(e);
continue;
}
addrs.add(parsedAddress);
continue;
}
// interface name
String[] ifaces = interfaceSplitter.split(currentAddress);
NetworkInterface netInterface = null;
try
{
netInterface = NetworkInterface.getByName(ifaces[0]);