Commit 2fe8a6c1 authored by Tigran Mkrtchyan's avatar Tigran Mkrtchyan
Browse files

utils: remove methods implemented in nio-jrpc

parent 4d66596a
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.dcache.utils.net;
import java.net.InetAddress;
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.
* @author tigran
*/
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>
* IPv4 format:
* h1.h2.h3.h4.p1.p2
*
* The prefix "h1.h2.h3.h4" is the standard textual form for
* representing an IPv4 address, which is always four octets long.
* Assuming big-endian ordering, h1, h2, h3, and h4 are, respectively,
* the first through fourth octets each converted to ASCII-decimal. The
* suffix "p1.p2" is a textual form for representing a service port.
* Assuming big-endian ordering, p1 and p2 are, respectively, the first
* and second octets each converted to ASCII-decimal. For example, if a
* host, in big-endian order, has an address in hexadecimal of
* 0xC0000207 and there is a service listening on, in big-endian order,
* port 0xCB51 (decimal 52049), then the complete uaddr is
* "192.0.2.7.203.81".
*
* IPv6:
* x1:x2:x3:x4:x5:x6:x7:x8.p1.p2
* The suffix "p1.p2" is the service port, and is computed the same way
* as with uaddrs for transports over IPv4 (see Section 5.2.3.3). The
* prefix "x1:x2:x3:x4:x5:x6:x7:x8" is the preferred textual form for
* representing an IPv6 address as defined in Section 2.2 of RFC 4291
* Additionally, the two alternative forms specified in Section 2.2
* of RFC 4291 are also acceptable.
* </pre>
* @param uaddr
* @return socket address
* @throws UnknownHostException
*/
public static InetSocketAddress forUaddrString(String uaddr) throws UnknownHostException {
int secondPort = uaddr.lastIndexOf('.');
if( secondPort == -1 ) {
throw new IllegalArgumentException("address " + uaddr + " doesn't match rfc5665");
}
int firstPort = uaddr.lastIndexOf('.', secondPort -1);
if( secondPort == -1 ) {
throw new IllegalArgumentException("address " + uaddr + " doesn't match rfc5665");
}
InetAddress inetAddr = InetAddresses.forString(uaddr.substring(0, firstPort));
int p1 = Integer.parseInt(uaddr.substring(firstPort +1, secondPort));
int p2 = Integer.parseInt(uaddr.substring(secondPort +1));
int port = (p1 << 8) + p2;
return new InetSocketAddress(inetAddr, port);
}
/**
* 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) 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(host, port);
}
/**
* Convert {@link InetSocketAddress} to it's UADDR representation as defined in rfc5665.
* @param socketAddress
* @return uaddr.
*/
public static String uaddrOf(InetSocketAddress socketAddress) {
int port = socketAddress.getPort();
int port_part[] = new int[2];
port_part[0] = (port & 0xff00) >> 8;
port_part[1] = port & 0x00ff;
return socketAddress.getAddress().getHostAddress() +
"." + port_part[0] + "." + port_part[1];
}
/**
* Convert <code>hostname</code> and <code>port</code> into UADDR representation
* as defined in rfc5665.
* @param host
* @param port
* @return uaddr
*/
public static String uaddrOf(String host, int port) {
return uaddrOf( new InetSocketAddress(host, port));
}
/**
* Get netid type for given {@link InetAddress}.
*
* @param address
* @return string corresponding to netid.
* @throws IllegalArgumentException in case of address type is unsupported.
*/
public static String tcpNetidOf(InetAddress address) {
if( address instanceof Inet4Address ){
return "tcp";
}else if( address instanceof Inet6Address ){
return "tcp6";
}
throw new IllegalArgumentException("unsupported inet type: " + address.getClass().getName());
}
}
package org.dcache.utils.net;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.junit.Test;
import static org.junit.Assert.*;
public class InetSocketAddressesTest {
@Test
public void testLocalHostV4() throws Exception {
String uaddr = "127.0.0.1.203.81";
InetSocketAddress socketAddress = InetSocketAddresses.forUaddrString(uaddr);
assertEquals("port mismatch", 52049, socketAddress.getPort());
assertEquals("host mismatch", InetAddress.getByName("127.0.0.1"),
socketAddress.getAddress());
}
@Test
public void testLocalHostV6() throws Exception {
String uaddr = "::1.203.81";
InetSocketAddress socketAddress = InetSocketAddresses.forUaddrString(uaddr);
assertEquals("port mismatch", 52049, socketAddress.getPort());
assertEquals("host mismatch", InetAddress.getByName("::1"),
socketAddress.getAddress());
}
@Test
public void testLocalHostV4Revert() throws Exception {
String uaddr = "127.0.0.1.203.81";
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"),52049);
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