1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.net.Inet4Address;
22 import java.net.Inet6Address;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.net.NetworkInterface;
26 import java.net.SocketException;
27 import java.util.Enumeration;
28
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30
31
32
33
34 @InterfaceAudience.Private
35 public class Addressing {
36 public static final String VALID_PORT_REGEX = "[\\d]+";
37 public static final String HOSTNAME_PORT_SEPARATOR = ":";
38
39
40
41
42
43 public static InetSocketAddress createInetSocketAddressFromHostAndPortStr(
44 final String hostAndPort) {
45 return new InetSocketAddress(parseHostname(hostAndPort), parsePort(hostAndPort));
46 }
47
48
49
50
51
52
53
54
55
56
57 public static String createHostAndPortStr(final String hostname, final int port) {
58 return hostname + HOSTNAME_PORT_SEPARATOR + port;
59 }
60
61
62
63
64
65 public static String parseHostname(final String hostAndPort) {
66 int colonIndex = hostAndPort.lastIndexOf(HOSTNAME_PORT_SEPARATOR);
67 if (colonIndex < 0) {
68 throw new IllegalArgumentException("Not a host:port pair: " + hostAndPort);
69 }
70 return hostAndPort.substring(0, colonIndex);
71 }
72
73
74
75
76
77 public static int parsePort(final String hostAndPort) {
78 int colonIndex = hostAndPort.lastIndexOf(HOSTNAME_PORT_SEPARATOR);
79 if (colonIndex < 0) {
80 throw new IllegalArgumentException("Not a host:port pair: " + hostAndPort);
81 }
82 return Integer.parseInt(hostAndPort.substring(colonIndex + 1));
83 }
84
85 public static InetAddress getIpAddress() throws SocketException {
86 return getIpAddress(new AddressSelectionCondition() {
87 @Override
88 public boolean isAcceptableAddress(InetAddress addr) {
89 return addr instanceof Inet4Address || addr instanceof Inet6Address;
90 }
91 });
92 }
93
94 public static InetAddress getIp4Address() throws SocketException {
95 return getIpAddress(new AddressSelectionCondition() {
96 @Override
97 public boolean isAcceptableAddress(InetAddress addr) {
98 return addr instanceof Inet4Address;
99 }
100 });
101 }
102
103 public static InetAddress getIp6Address() throws SocketException {
104 return getIpAddress(new AddressSelectionCondition() {
105 @Override
106 public boolean isAcceptableAddress(InetAddress addr) {
107 return addr instanceof Inet6Address;
108 }
109 });
110 }
111
112 private static InetAddress getIpAddress(AddressSelectionCondition condition) throws
113 SocketException {
114
115
116
117 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
118 while (interfaces.hasMoreElements()) {
119 NetworkInterface current = interfaces.nextElement();
120 if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue;
121 Enumeration<InetAddress> addresses = current.getInetAddresses();
122 while (addresses.hasMoreElements()) {
123 InetAddress addr = addresses.nextElement();
124 if (addr.isLoopbackAddress()) continue;
125 if (condition.isAcceptableAddress(addr)) {
126 return addr;
127 }
128 }
129 }
130
131 throw new SocketException("Can't get our ip address, interfaces are: " + interfaces);
132 }
133
134
135
136
137
138
139
140 public static boolean isLocalAddress(InetAddress addr) {
141
142 boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();
143
144
145 if (!local) {
146 try {
147 local = NetworkInterface.getByInetAddress(addr) != null;
148 } catch (SocketException e) {
149 local = false;
150 }
151 }
152 return local;
153 }
154
155
156
157
158 public interface AddressSelectionCondition{
159
160
161
162
163
164 public boolean isAcceptableAddress(InetAddress address);
165 }
166 }