1 7 8 package java.net; 9 10 import java.net.SocketException ; 11 import java.util.Enumeration ; 12 import java.util.NoSuchElementException ; 13 import sun.security.action.*; 14 import java.security.AccessController ; 15 16 26 public final class NetworkInterface { 27 private String name; 28 private String displayName; 29 private int index; 30 private InetAddress addrs[]; 31 32 static { 33 AccessController.doPrivileged(new LoadLibraryAction("net")); 34 init(); 35 } 36 37 43 NetworkInterface() { 44 } 45 46 NetworkInterface(String name, int index, InetAddress [] addrs) { 47 this.name = name; 48 this.index = index; 49 this.addrs = addrs; 50 } 51 52 57 public String getName() { 58 return name; 59 } 60 61 72 public Enumeration <InetAddress > getInetAddresses() { 73 74 class checkedAddresses implements Enumeration <InetAddress > { 75 76 private int i=0, count=0; 77 private InetAddress local_addrs[]; 78 79 checkedAddresses() { 80 local_addrs = new InetAddress [addrs.length]; 81 82 SecurityManager sec = System.getSecurityManager(); 83 for (int j=0; j<addrs.length; j++) { 84 try { 85 if (sec != null) { 86 sec.checkConnect(addrs[j].getHostAddress(), -1); 87 } 88 local_addrs[count++] = addrs[j]; 89 } catch (SecurityException e) { } 90 } 91 92 } 93 94 public InetAddress nextElement() { 95 if (i < count) { 96 return local_addrs[i++]; 97 } else { 98 throw new NoSuchElementException (); 99 } 100 } 101 102 public boolean hasMoreElements() { 103 return (i < count); 104 } 105 } 106 return new checkedAddresses(); 107 108 } 109 110 115 int getIndex() { 116 return index; 117 } 118 119 127 public String getDisplayName() { 128 return displayName; 129 } 130 131 147 public static NetworkInterface getByName(String name) throws SocketException { 148 if (name == null) 149 throw new NullPointerException (); 150 return getByName0(name); 151 } 152 153 160 native static NetworkInterface getByIndex(int index) 161 throws SocketException ; 162 163 185 public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException { 186 if (addr == null) 187 throw new NullPointerException (); 188 return getByInetAddress0(addr); 189 } 190 191 201 202 public static Enumeration <NetworkInterface > getNetworkInterfaces() 203 throws SocketException { 204 final NetworkInterface [] netifs = getAll(); 205 206 if (netifs == null) 208 return null; 209 210 return new Enumeration <NetworkInterface >() { 211 private int i = 0; 212 public NetworkInterface nextElement() { 213 if (netifs != null && i < netifs.length) { 214 NetworkInterface netif = netifs[i++]; 215 return netif; 216 } else { 217 throw new NoSuchElementException (); 218 } 219 } 220 221 public boolean hasMoreElements() { 222 return (netifs != null && i < netifs.length); 223 } 224 }; 225 } 226 227 private native static NetworkInterface [] getAll() 228 throws SocketException ; 229 230 private native static NetworkInterface getByName0(String name) 231 throws SocketException ; 232 233 private native static NetworkInterface getByInetAddress0(InetAddress addr) 234 throws SocketException ; 235 236 237 251 public boolean equals(Object obj) { 252 if ((obj == null) || !(obj instanceof NetworkInterface )) { 253 return false; 254 } 255 NetworkInterface netIF = (NetworkInterface )obj; 256 if (name != null ) { 257 if (netIF.getName() != null) { 258 if (!name.equals(netIF.getName())) { 259 return false; 260 } 261 } else { 262 return false; 263 } 264 } else { 265 if (netIF.getName() != null) { 266 return false; 267 } 268 } 269 Enumeration newAddrs = netIF.getInetAddresses(); 270 int i = 0; 271 for (i = 0; newAddrs.hasMoreElements();newAddrs.nextElement(), i++); 272 if (addrs == null) { 273 if (i != 0) { 274 return false; 275 } 276 } else { 277 280 int count = 0; 281 Enumeration e = getInetAddresses(); 282 for (; e.hasMoreElements(); count++) { 283 e.nextElement(); 284 } 285 if (i != count) { 286 return false; 287 } 288 } 289 newAddrs = netIF.getInetAddresses(); 290 for (; newAddrs.hasMoreElements();) { 291 boolean equal = false; 292 Enumeration thisAddrs = getInetAddresses(); 293 InetAddress newAddr = (InetAddress )newAddrs.nextElement(); 294 for (; thisAddrs.hasMoreElements();) { 295 InetAddress thisAddr = (InetAddress )thisAddrs.nextElement(); 296 if (thisAddr.equals(newAddr)) { 297 equal = true; 298 } 299 } 300 if (!equal) { 301 return false; 302 } 303 } 304 return true; 305 } 306 307 public int hashCode() { 308 int count = 0; 309 if (addrs != null) { 310 for (int i = 0; i < addrs.length; i++) { 311 count += addrs[i].hashCode(); 312 } 313 } 314 return count; 315 } 316 317 public String toString() { 318 String result = "name:"; 319 result += name == null? "null": name; 320 if (displayName != null) { 321 result += " (" + displayName + ")"; 322 } 323 result += " index: "+index+" addresses:\n"; 324 for (Enumeration e = getInetAddresses(); e.hasMoreElements(); ) { 325 InetAddress addr = (InetAddress )e.nextElement(); 326 result += addr+";\n"; 327 } 328 return result; 329 } 330 private static native void init(); 331 332 } 333 | Popular Tags |