15
15
*/
16
16
package org .openqa .selenium .net ;
17
17
18
+ import static org .openqa .selenium .net .NetworkInterface .isIpv6 ;
19
+
18
20
import org .openqa .selenium .Platform ;
19
21
import org .openqa .selenium .WebDriverException ;
20
22
23
+ import java .net .Inet6Address ;
24
+ import java .net .InetAddress ;
21
25
import java .util .ArrayList ;
22
26
import java .util .Collections ;
23
27
import java .util .List ;
@@ -35,7 +39,7 @@ public NetworkUtils() {
35
39
}
36
40
37
41
public String getPrivateLocalAddress () {
38
- List <INetAddress > addresses = getLocalInterfaceAddress ();
42
+ List <InetAddress > addresses = getLocalInterfaceAddress ();
39
43
if (addresses .isEmpty ()) {
40
44
return "127.0.0.1" ;
41
45
}
@@ -47,7 +51,7 @@ public String getPrivateLocalAddress() {
47
51
* Used by the mobile emulators that refuse to access localhost or 127.0.0.1 The IP4/IP6
48
52
* requirements of this method are as-of-yet unspecified, but we return the string that is
49
53
* associated with the IP4 interface
50
- *
54
+ *
51
55
* @return A String representing the host name or non-loopback IP4 address of this machine.
52
56
*/
53
57
public String getNonLoopbackAddressOfThisMachine () {
@@ -56,12 +60,12 @@ public String getNonLoopbackAddressOfThisMachine() {
56
60
57
61
/**
58
62
* Returns a non-loopback IP4 hostname of the local host.
59
- *
63
+ *
60
64
* @return A string hostName
61
65
*/
62
- public INetAddress getIp4NonLoopbackAddressOfThisMachine () {
66
+ public InetAddress getIp4NonLoopbackAddressOfThisMachine () {
63
67
for (NetworkInterface iface : networkInterfaceProvider .getNetworkInterfaces ()) {
64
- final INetAddress ip4NonLoopback = iface .getIp4NonLoopBackOnly ();
68
+ final InetAddress ip4NonLoopback = iface .getIp4NonLoopBackOnly ();
65
69
if (ip4NonLoopback != null ) {
66
70
return ip4NonLoopback ;
67
71
}
@@ -73,7 +77,7 @@ public INetAddress getIp4NonLoopbackAddressOfThisMachine() {
73
77
* Returns a single address that is guaranteed to resolve to an ipv4 representation of localhost
74
78
* This may either be a hostname or an ip address, dependending if we can guarantee what that the
75
79
* hostname will resolve to ip4.
76
- *
80
+ *
77
81
* @return The address part og such an address
78
82
*/
79
83
public String obtainLoopbackIp4Address () {
@@ -90,7 +94,7 @@ public String obtainLoopbackIp4Address() {
90
94
if (Platform .getCurrent ().is (Platform .UNIX )) {
91
95
NetworkInterface linuxLoopback = networkInterfaceProvider .getLoInterface ();
92
96
if (linuxLoopback != null ) {
93
- final INetAddress netAddress = linuxLoopback .getIp4LoopbackOnly ();
97
+ final InetAddress netAddress = linuxLoopback .getIp4LoopbackOnly ();
94
98
if (netAddress != null ) {
95
99
return netAddress .getHostAddress ();
96
100
}
@@ -104,10 +108,10 @@ public String obtainLoopbackIp4Address() {
104
108
}
105
109
106
110
107
- private INetAddress grabFirstNetworkAddress () {
111
+ private InetAddress grabFirstNetworkAddress () {
108
112
NetworkInterface firstInterface =
109
113
networkInterfaceProvider .getNetworkInterfaces ().iterator ().next ();
110
- INetAddress firstAddress = null ;
114
+ InetAddress firstAddress = null ;
111
115
if (firstInterface != null ) {
112
116
firstAddress = firstInterface .getInetAddresses ().iterator ().next ();
113
117
}
@@ -121,7 +125,7 @@ private INetAddress grabFirstNetworkAddress() {
121
125
122
126
public String getIpOfLoopBackIp4 () {
123
127
for (NetworkInterface iface : networkInterfaceProvider .getNetworkInterfaces ()) {
124
- final INetAddress netAddress = iface .getIp4LoopbackOnly ();
128
+ final InetAddress netAddress = iface .getIp4LoopbackOnly ();
125
129
if (netAddress != null ) {
126
130
return netAddress .getHostAddress ();
127
131
}
@@ -138,13 +142,13 @@ private NetworkInterface getLoopBackAndIp4Only() {
138
142
return null ;
139
143
}
140
144
141
- private List <INetAddress > getLocalInterfaceAddress () {
142
- List <INetAddress > localAddresses = new ArrayList <INetAddress >();
145
+ private List <InetAddress > getLocalInterfaceAddress () {
146
+ List <InetAddress > localAddresses = new ArrayList <InetAddress >();
143
147
144
148
for (NetworkInterface iface : networkInterfaceProvider .getNetworkInterfaces ()) {
145
- for (INetAddress addr : iface .getInetAddresses ()) {
149
+ for (InetAddress addr : iface .getInetAddresses ()) {
146
150
// filter out Inet6 Addr Entries
147
- if (addr .isLoopbackAddress () && !addr . isIPv6Address ()) {
151
+ if (addr .isLoopbackAddress () && !isIpv6 ( addr )) {
148
152
localAddresses .add (addr );
149
153
}
150
154
}
@@ -156,8 +160,8 @@ private List<INetAddress> getLocalInterfaceAddress() {
156
160
if (Platform .getCurrent ().is (Platform .UNIX )) {
157
161
NetworkInterface linuxLoopback = networkInterfaceProvider .getLoInterface ();
158
162
if (linuxLoopback != null ) {
159
- for (INetAddress inetAddress : linuxLoopback .getInetAddresses ()) {
160
- if (!inetAddress . isIPv6Address ( )) {
163
+ for (InetAddress inetAddress : linuxLoopback .getInetAddresses ()) {
164
+ if (!isIpv6 ( inetAddress )) {
161
165
localAddresses .add (inetAddress );
162
166
}
163
167
}
@@ -197,8 +201,8 @@ private static void dumpToConsole(StringBuilder result, NetworkInterface inNetwo
197
201
dumpAddresses (result , inNetworkInterface .getInetAddresses ());
198
202
}
199
203
200
- private static void dumpAddresses (StringBuilder result , Iterable <INetAddress > inetAddresses ) {
201
- for (INetAddress address : inetAddresses ) {
204
+ private static void dumpAddresses (StringBuilder result , Iterable <InetAddress > inetAddresses ) {
205
+ for (InetAddress address : inetAddresses ) {
202
206
result .append (" address.getHostName() = " );
203
207
result .append (address .getHostName ());
204
208
result .append ("\n " );
0 commit comments