Skip to content

Commit bc10c8b

Browse files
committed
Improved loopback detection. fixes issues 1181.
Patch by Kowalczyk Jerome, applied unmodified
1 parent 3e1393a commit bc10c8b

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

java/client/src/org/openqa/selenium/net/NetworkInterface.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,50 @@
11
/*
2-
Copyright 2007-2010 Selenium committers
2+
Copyright 2007-2010 WebDriver committers
3+
Copyright 2007-2010 Google Inc.
34
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
78
8-
http://www.apache.org/licenses/LICENSE-2.0
9+
http://www.apache.org/licenses/LICENSE-2.0
910
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
1516
*/
16-
1717
package org.openqa.selenium.net;
1818

1919
import java.net.InetAddress;
20+
import java.net.SocketException;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Collections;
2324
import java.util.Enumeration;
2425
import java.util.Iterator;
2526
import java.util.List;
27+
import java.util.logging.Level;
28+
import java.util.logging.Logger;
2629

2730
public class NetworkInterface {
31+
2832
private final String name;
2933
private final Iterable<INetAddress> inetAddresses;
34+
private boolean isLoopback;
3035

3136
public NetworkInterface(java.net.NetworkInterface networkInterface) {
3237
this(networkInterface.getName(), asIterableAddr(networkInterface.getInetAddresses()));
38+
try {
39+
// Issue 1181 : determine wheter this NetworkInterface instance is loopback
40+
// from java.net.NetworkInterface API
41+
this.isLoopback = networkInterface.isLoopback();
42+
} catch (SocketException ex) {
43+
Logger.getLogger(NetworkInterface.class.getName()).log(Level.WARNING, null, ex);
44+
// If an SocketException is caught, determine wheter this NetworkInterface
45+
// instance is loopack from computation from its inetAddresses
46+
this.isLoopback = isLoopBackFromINetAddresses(asIterableAddr(networkInterface.getInetAddresses()));
47+
}
3348
}
3449

3550
NetworkInterface(String name, Iterable<INetAddress> inetAddresses) {
@@ -42,12 +57,15 @@ public boolean isIp4AddressBindingOnly() {
4257
}
4358

4459
public boolean isLoopBack() {
60+
return isLoopback;
61+
}
62+
63+
public final boolean isLoopBackFromINetAddresses(Iterable<INetAddress> inetAddresses) {
4564
// Let's hope there's no such thing as network interfaces with mixed addresses ;)
4665
Iterator<INetAddress> iterator = inetAddresses.iterator();
4766
return iterator.hasNext() && iterator.next().isLoopbackAddress();
4867
}
4968

50-
5169
public INetAddress getIp4LoopbackOnly() {
5270
// Goes by the wildly unscientific assumption that if there are more than one set of
5371
// loopback addresses, firefox will bind to the last one we get.
@@ -56,6 +74,10 @@ public INetAddress getIp4LoopbackOnly() {
5674
// algorithm until it works.
5775
// See NetworkUtilsTest#testOpenSuseBoxIssue1181
5876
INetAddress lastFound = null;
77+
// Issue 1181
78+
if (!isLoopback) {
79+
return lastFound;
80+
}
5981
for (INetAddress inetAddress : inetAddresses) {
6082
if (inetAddress.isLoopbackAddress() && inetAddress.isIPv4Address()) {
6183
lastFound = inetAddress;

0 commit comments

Comments
 (0)