Commit c674f241 authored by Tigran Mkrtchyan's avatar Tigran Mkrtchyan
Browse files

utils: handle ipv6 in host:port fo InetSocketAddress conversion.

parent ee9e6702
......@@ -21,6 +21,9 @@ import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.net.InetAddresses;
/**
* Utility class for InetSocketAddress manipulations.
......@@ -31,6 +34,9 @@ public class InetSocketAddresses {
/* utility class. No instances are allowed */
private InetSocketAddresses() {}
// TODO: switch to guava r10 when it released
private static final Pattern BRACKET_PATTERN = Pattern.compile("^\\[(.*:.*)\\](?::(\\d*))?$");
/**
* Convert UADDR string into {@link InetSocketAddress} as defined in rfc5665.
* <pre>
......@@ -87,17 +93,46 @@ public class InetSocketAddresses {
/**
* Convert a {@link String} in a form <code>host:port</code>
* into corresponding {@link InetSocketAddress}.
* The host can be in the one following notations:
* <pre>
* ipv4:port
* [ipv6]:port
* hostname:port
* </pre>
* @param address
* @return socketAddress
* @throws IllegalArgumentException if <code>address</code> doesn't match expected format.
*/
public static InetSocketAddress inetAddressOf(String address) {
int colom = address.indexOf(":");
if (colom < 0) {
public static InetSocketAddress inetAddressOf(String address) throws IllegalArgumentException {
String host;
String portStr;
int port;
if( address.charAt(0) == '[' ){
Matcher matcher = BRACKET_PATTERN.matcher(address);
if( !matcher.matches() || matcher.groupCount() != 2 ){
throw new IllegalArgumentException("invalid host:port format");
}
host = matcher.group(1);
portStr = matcher.group(2);
}else{
int colom = address.indexOf(":");
if( colom < 0 ){
throw new IllegalArgumentException("invalid host:port format");
}
host = address.substring(0, colom);
portStr = address.substring(colom + 1);
}
try{
port = Integer.parseInt(portStr);
}catch( NumberFormatException e ){
throw new IllegalArgumentException("invalid host:port format");
}
return new InetSocketAddress(address.substring(0, colom),
Integer.parseInt(address.substring(colom + 1)));
return new InetSocketAddress(host, port);
}
/**
......
package org.dcache.utils.net;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
......@@ -31,7 +32,26 @@ public class InetSocketAddressesTest {
String uaddr = "127.0.0.1.203.81";
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"),52049);
assertEquals("reverce convertion failed", uaddr,
assertEquals("reverse conversion failed", uaddr,
InetSocketAddresses.uaddrOf(socketAddress));
}
@Test
public void testHostAndPortIpv4() throws Exception {
String hostAndPort = "127.0.0.1:1111";
InetSocketAddress address = InetSocketAddresses.inetAddressOf(hostAndPort);
assertEquals(InetAddress.getByName("127.0.0.1"), address.getAddress());
assertEquals(1111, address.getPort());
}
@Test
public void testHostAndPortIpv6() throws Exception {
String hostAndPort = "[fe80::21c:c0ff:fea0:caf4]:1111";
InetSocketAddress address = InetSocketAddresses.inetAddressOf(hostAndPort);
assertEquals( InetAddress.getByName("fe80::21c:c0ff:fea0:caf4"), address.getAddress());
assertEquals(1111, address.getPort());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment