~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/net/IPFinder.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
13
 */
 
14
 
 
15
package games.strategy.net;
 
16
 
 
17
/**
 
18
    @author Georlge El-Haddad
 
19
    nekromancer@users.sourceforge.net
 
20
 
 
21
   Utility class for finding the local ip address of a machine with multiple network interfaces.
 
22
 
 
23
   This class will discard any InetAddress whose isLoobackAddresS() returns true.
 
24
   It will also discard any InetAddress whose isLinkLocalAddress() returns true.
 
25
 
 
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.
 
34
 
 
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.
 
39
 
 
40
 */
 
41
 
 
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;
 
49
 
 
50
public class IPFinder {
 
51
 
 
52
        /**
 
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).
 
58
 
 
59
           @exception java.net.SocketException       required by InetAddress
 
60
           @exception java.net.UnknownHostException  required for getLocalHost()
 
61
 
 
62
           @return    java.net.InetAddress           the ip address to use
 
63
        */
 
64
        public static InetAddress findInetAddress() throws SocketException, UnknownHostException
 
65
        {
 
66
       
 
67
                Enumeration enum1 = NetworkInterface.getNetworkInterfaces();
 
68
 
 
69
                // Test if null, no point taking a performance hit by
 
70
                // letting the JVM check for a NullPointerException.
 
71
                if(enum1 == null) 
 
72
        {
 
73
                        InetAddress ip1 = InetAddress.getLocalHost();
 
74
                        return ip1;
 
75
                }
 
76
        
 
77
        List<InetAddress> allButLoopback = new ArrayList<InetAddress>();
 
78
                
 
79
                while (enum1.hasMoreElements()) 
 
80
        {
 
81
            NetworkInterface netface = (NetworkInterface)enum1.nextElement();
 
82
            Enumeration enum2 = netface.getInetAddresses();
 
83
            while (enum2.hasMoreElements()) 
 
84
            {
 
85
                InetAddress ip2 = (InetAddress) enum2.nextElement();
 
86
                if(!ip2.isLoopbackAddress())
 
87
                {
 
88
                    allButLoopback.add(ip2);
 
89
                }
 
90
            }
 
91
        }
 
92
                
 
93
        //try to find one that is not private and ip4
 
94
        for(InetAddress address : allButLoopback) 
 
95
        {
 
96
            if(address.getAddress().length == 4 && !isPrivateNetworkAddress(address)) 
 
97
            {
 
98
                return address;
 
99
            }
 
100
        }
 
101
        
 
102
        //try to find one that is not private
 
103
        for(InetAddress address : allButLoopback) 
 
104
        {
 
105
            if(!isPrivateNetworkAddress(address)) 
 
106
            {
 
107
                return address;
 
108
            }
 
109
        }
 
110
 
 
111
        //try to find one that is not link local
 
112
        for(InetAddress address : allButLoopback) 
 
113
        {
 
114
            if(!address.isLinkLocalAddress())
 
115
            {
 
116
                return address;
 
117
            }
 
118
        }
 
119
 
 
120
        
 
121
        //all else fails, return localhost
 
122
        return InetAddress.getLocalHost();
 
123
 
 
124
        }//end static findInetAddress()
 
125
    
 
126
    
 
127
    private static boolean isPrivateNetworkAddress(InetAddress address) {
 
128
        
 
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;
 
136
        
 
137
        
 
138
        byte[] bytes = address.getAddress();
 
139
        //ip 4
 
140
        if(bytes.length == 4)
 
141
        {
 
142
            //http://en.wikipedia.org/wiki/Private_network
 
143
            if(
 
144
              (bytes[0] == 10) ||
 
145
              (bytes[0] == _172 && bytes[1] >= 16 && bytes[1] <= 31) ||
 
146
              (bytes[0] == _192 &&  bytes[1] == _168 ) ||
 
147
              (bytes[0] == _169 &&  bytes[1] == _254 )
 
148
              )
 
149
                return true;
 
150
        }
 
151
        //ip 6
 
152
        else {
 
153
            //http://en.wikipedia.org/wiki/IPv6#Addressing
 
154
            if(
 
155
                (bytes[0] ==  _252 && bytes[1] == 0) ||
 
156
                 bytes[0] ==  _254
 
157
              )
 
158
                {
 
159
                return true;
 
160
            }
 
161
        }
 
162
        
 
163
        return false;
 
164
        
 
165
    }
 
166
 
 
167
    
 
168
    public static void main(String[] args) throws SocketException, UnknownHostException {
 
169
       
 
170
       Enumeration enum1 = NetworkInterface.getNetworkInterfaces();
 
171
       while (enum1.hasMoreElements()) 
 
172
       {
 
173
           
 
174
           NetworkInterface netface = (NetworkInterface)enum1.nextElement();
 
175
           System.out.println("interface:" + netface);
 
176
           Enumeration enum2 = netface.getInetAddresses();
 
177
           while (enum2.hasMoreElements()) 
 
178
           {
 
179
               InetAddress ip2 = (InetAddress) enum2.nextElement();
 
180
               System.out.println(" address:" + ip2 + " is private:" + isPrivateNetworkAddress(ip2) + " is loopback:" + ip2.isLoopbackAddress());
 
181
           }
 
182
           System.out.println("----------------------");
 
183
       }
 
184
           
 
185
       System.out.println();
 
186
       System.out.println("Found:" + findInetAddress());
 
187
    }
 
188
}//end class IPFinder
 
189
 
 
190