2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
6
* This program is distributed in the hope that it will be useful,
7
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
* GNU General Public License for more details.
10
* You should have received a copy of the GNU General Public License
11
* along with this program; if not, write to the Free Software
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
package games.strategy.net;
18
@author Georlge El-Haddad
19
nekromancer@users.sourceforge.net
21
Utility class for finding the local ip address of a machine with multiple network interfaces.
23
This class will discard any InetAddress whose isLoobackAddresS() returns true.
24
It will also discard any InetAddress whose isLinkLocalAddress() returns true.
26
On most systems the IP addres it uses for internet communication will NOT be
27
a LinkLocalAddress. Even if your system goes through a gateway, the standard
28
192.168.0.1 address will be valid (not link local and not loopback). It is up
29
to the user to tell his/her opponents the IP address of his/her gateway to
30
connect to. And it is their responsibility to make sure they have port forwarding
31
and IP masquarading set properly. TripleA will be bound to their local address
32
and all packets will be routed through the gateway. Opponents will be bound to the
33
gateway address. In essence it should all work.
35
IF the game is run on the system that is acting as the dedicated gateway, many IPs
36
will be found as valid. The 1st IP that will be detected will be used. According to
37
some tests, the 1st ip tends to be the IP used by the gateway to connect to the net.
38
This means that TripleA will still work.
42
import java.net.InetAddress;
43
import java.net.NetworkInterface;
44
import java.net.SocketException;
45
import java.net.UnknownHostException;
46
import java.util.ArrayList;
47
import java.util.Enumeration;
48
import java.util.List;
50
public class IPFinder {
53
We iterate through an enumeration of network interfaces on the machine
54
and picks the first IP that is not a loopback and not a link local and not private.
55
In the case of IRIX computers connected on a LAN through a central
56
gateway running java off a telnet session will result in a null
57
network interface (patched below).
59
@exception java.net.SocketException required by InetAddress
60
@exception java.net.UnknownHostException required for getLocalHost()
62
@return java.net.InetAddress the ip address to use
64
public static InetAddress findInetAddress() throws SocketException, UnknownHostException
67
Enumeration enum1 = NetworkInterface.getNetworkInterfaces();
69
// Test if null, no point taking a performance hit by
70
// letting the JVM check for a NullPointerException.
73
InetAddress ip1 = InetAddress.getLocalHost();
77
List<InetAddress> allButLoopback = new ArrayList<InetAddress>();
79
while (enum1.hasMoreElements())
81
NetworkInterface netface = (NetworkInterface)enum1.nextElement();
82
Enumeration enum2 = netface.getInetAddresses();
83
while (enum2.hasMoreElements())
85
InetAddress ip2 = (InetAddress) enum2.nextElement();
86
if(!ip2.isLoopbackAddress())
88
allButLoopback.add(ip2);
93
//try to find one that is not private and ip4
94
for(InetAddress address : allButLoopback)
96
if(address.getAddress().length == 4 && !isPrivateNetworkAddress(address))
102
//try to find one that is not private
103
for(InetAddress address : allButLoopback)
105
if(!isPrivateNetworkAddress(address))
111
//try to find one that is not link local
112
for(InetAddress address : allButLoopback)
114
if(!address.isLinkLocalAddress())
121
//all else fails, return localhost
122
return InetAddress.getLocalHost();
124
}//end static findInetAddress()
127
private static boolean isPrivateNetworkAddress(InetAddress address) {
129
//stupid java signed byte type
130
final byte _192 = (byte) 0xC0;
131
final byte _172 = (byte) 0xAC;
132
final byte _168 = (byte) 0xA8;
133
final byte _169 = (byte) 0xA9;
134
final byte _252 = (byte) 0xFC;
135
final byte _254 = (byte) 0xFE;
138
byte[] bytes = address.getAddress();
140
if(bytes.length == 4)
142
//http://en.wikipedia.org/wiki/Private_network
145
(bytes[0] == _172 && bytes[1] >= 16 && bytes[1] <= 31) ||
146
(bytes[0] == _192 && bytes[1] == _168 ) ||
147
(bytes[0] == _169 && bytes[1] == _254 )
153
//http://en.wikipedia.org/wiki/IPv6#Addressing
155
(bytes[0] == _252 && bytes[1] == 0) ||
168
public static void main(String[] args) throws SocketException, UnknownHostException {
170
Enumeration enum1 = NetworkInterface.getNetworkInterfaces();
171
while (enum1.hasMoreElements())
174
NetworkInterface netface = (NetworkInterface)enum1.nextElement();
175
System.out.println("interface:" + netface);
176
Enumeration enum2 = netface.getInetAddresses();
177
while (enum2.hasMoreElements())
179
InetAddress ip2 = (InetAddress) enum2.nextElement();
180
System.out.println(" address:" + ip2 + " is private:" + isPrivateNetworkAddress(ip2) + " is loopback:" + ip2.isLoopbackAddress());
182
System.out.println("----------------------");
185
System.out.println();
186
System.out.println("Found:" + findInetAddress());
188
}//end class IPFinder