1
1
/*
2
- Copyright 2007-2010 Selenium committers
2
+ Copyright 2007-2010 WebDriver committers
3
+ Copyright 2007-2010 Google Inc.
3
4
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
7
8
8
- http://www.apache.org/licenses/LICENSE-2.0
9
+ http://www.apache.org/licenses/LICENSE-2.0
9
10
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.
15
16
*/
16
-
17
17
package org .openqa .selenium .net ;
18
18
19
19
import java .net .InetAddress ;
20
+ import java .net .SocketException ;
20
21
import java .util .ArrayList ;
21
22
import java .util .Arrays ;
22
23
import java .util .Collections ;
23
24
import java .util .Enumeration ;
24
25
import java .util .Iterator ;
25
26
import java .util .List ;
27
+ import java .util .logging .Level ;
28
+ import java .util .logging .Logger ;
26
29
27
30
public class NetworkInterface {
31
+
28
32
private final String name ;
29
33
private final Iterable <INetAddress > inetAddresses ;
34
+ private boolean isLoopback ;
30
35
31
36
public NetworkInterface (java .net .NetworkInterface networkInterface ) {
32
37
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
+ }
33
48
}
34
49
35
50
NetworkInterface (String name , Iterable <INetAddress > inetAddresses ) {
@@ -42,12 +57,15 @@ public boolean isIp4AddressBindingOnly() {
42
57
}
43
58
44
59
public boolean isLoopBack () {
60
+ return isLoopback ;
61
+ }
62
+
63
+ public final boolean isLoopBackFromINetAddresses (Iterable <INetAddress > inetAddresses ) {
45
64
// Let's hope there's no such thing as network interfaces with mixed addresses ;)
46
65
Iterator <INetAddress > iterator = inetAddresses .iterator ();
47
66
return iterator .hasNext () && iterator .next ().isLoopbackAddress ();
48
67
}
49
68
50
-
51
69
public INetAddress getIp4LoopbackOnly () {
52
70
// Goes by the wildly unscientific assumption that if there are more than one set of
53
71
// loopback addresses, firefox will bind to the last one we get.
@@ -56,6 +74,10 @@ public INetAddress getIp4LoopbackOnly() {
56
74
// algorithm until it works.
57
75
// See NetworkUtilsTest#testOpenSuseBoxIssue1181
58
76
INetAddress lastFound = null ;
77
+ // Issue 1181
78
+ if (!isLoopback ) {
79
+ return lastFound ;
80
+ }
59
81
for (INetAddress inetAddress : inetAddresses ) {
60
82
if (inetAddress .isLoopbackAddress () && inetAddress .isIPv4Address ()) {
61
83
lastFound = inetAddress ;
0 commit comments