1 7 8 package java.net; 9 10 import java.security.AccessController ; 11 import java.io.ObjectInputStream ; 12 import java.io.IOException ; 13 import java.io.ObjectStreamException ; 14 import java.io.InvalidObjectException ; 15 import sun.security.action.*; 16 import java.util.Enumeration ; 17 18 149 150 public final 151 class Inet6Address extends InetAddress { 152 final static int INADDRSZ = 16; 153 154 157 private transient int cached_scope_id = 0; 158 159 164 byte[] ipaddress; 165 166 170 private int scope_id = 0; 171 172 176 private boolean scope_id_set = false; 177 178 182 private transient NetworkInterface scope_ifname = null; 183 184 188 private boolean scope_ifname_set = false; 189 190 private static final long serialVersionUID = 6880410070516793377L; 191 192 195 static { 196 init(); 197 } 198 199 Inet6Address() { 200 super(); 201 hostName = null; 202 ipaddress = new byte[INADDRSZ]; 203 family = IPv6; 204 } 205 206 209 Inet6Address(String hostName, byte addr[], int scope_id) { 210 this.hostName = hostName; 211 if (addr.length == INADDRSZ) { family = IPv6; 213 ipaddress = (byte[])addr.clone(); 214 } 215 if (scope_id >= 0) { 216 this.scope_id = scope_id; 217 scope_id_set = true; 218 } 219 } 220 221 Inet6Address(String hostName, byte addr[]) { 222 try { 223 initif (hostName, addr, null); 224 } catch (UnknownHostException e) {} 225 } 226 227 Inet6Address (String hostName, byte addr[], NetworkInterface nif) throws UnknownHostException { 228 initif (hostName, addr, nif); 229 } 230 231 Inet6Address (String hostName, byte addr[], String ifname) throws UnknownHostException { 232 initstr (hostName, addr, ifname); 233 } 234 235 253 254 public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif) 255 throws UnknownHostException { 256 if (host != null && host.length() > 0 && host.charAt(0) == '[') { 257 if (host.charAt(host.length()-1) == ']') { 258 host = host.substring(1, host.length() -1); 259 } 260 } 261 if (addr != null) { 262 if (addr.length == Inet6Address.INADDRSZ) { 263 return new Inet6Address (host, addr, nif); 264 } 265 } 266 throw new UnknownHostException ("addr is of illegal length"); 267 } 268 269 284 285 public static Inet6Address getByAddress(String host, byte[] addr, int scope_id) 286 throws UnknownHostException { 287 if (host != null && host.length() > 0 && host.charAt(0) == '[') { 288 if (host.charAt(host.length()-1) == ']') { 289 host = host.substring(1, host.length() -1); 290 } 291 } 292 if (addr != null) { 293 if (addr.length == Inet6Address.INADDRSZ) { 294 return new Inet6Address (host, addr, scope_id); 295 } 296 } 297 throw new UnknownHostException ("addr is of illegal length"); 298 } 299 300 private void initstr (String hostName, byte addr[], String ifname) throws UnknownHostException { 301 try { 302 NetworkInterface nif = NetworkInterface.getByName (ifname); 303 if (nif == null) { 304 throw new UnknownHostException ("no such interface " + ifname); 305 } 306 initif (hostName, addr, nif); 307 } catch (SocketException e) { 308 throw new UnknownHostException ("SocketException thrown" + ifname); 309 } 310 } 311 312 private void initif(String hostName, byte addr[],NetworkInterface nif) throws UnknownHostException { 313 this.hostName = hostName; 314 if (addr.length == INADDRSZ) { family = IPv6; 316 ipaddress = (byte[])addr.clone(); 317 } 318 if (nif != null) { 319 this.scope_ifname = nif; 320 scope_ifname_set = true; 321 scope_id = deriveNumericScope (nif); 322 scope_id_set = true; 323 } 324 } 325 326 331 private boolean differentLocalAddressTypes(Inet6Address other) { 332 333 if (isLinkLocalAddress() && !other.isLinkLocalAddress()) { 334 return false; 335 } 336 if (isSiteLocalAddress() && !other.isSiteLocalAddress()) { 337 return false; 338 } 339 return true; 340 } 341 342 private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException { 343 Enumeration addresses = ifc.getInetAddresses(); 344 while (addresses.hasMoreElements()) { 345 InetAddress address = (InetAddress )addresses.nextElement(); 346 if (!(address instanceof Inet6Address )) { 347 continue; 348 } 349 Inet6Address ia6_addr = (Inet6Address )address; 350 351 if (!differentLocalAddressTypes(ia6_addr)){ 352 353 continue; 354 } 355 356 return ia6_addr.scope_id; 357 } 358 throw new UnknownHostException ("no scope_id found"); 359 } 360 361 private int deriveNumericScope (String ifname) throws UnknownHostException { 362 Enumeration en; 363 try { 364 en = NetworkInterface.getNetworkInterfaces(); 365 } catch (SocketException e) { 366 throw new UnknownHostException ("could not enumerate local network interfaces"); 367 } 368 while (en.hasMoreElements()) { 369 NetworkInterface ifc = (NetworkInterface )en.nextElement(); 370 if (ifc.getName().equals (ifname)) { 371 Enumeration addresses = ifc.getInetAddresses(); 372 while (addresses.hasMoreElements()) { 373 InetAddress address = (InetAddress )addresses.nextElement(); 374 if (!(address instanceof Inet6Address )) { 375 continue; 376 } 377 Inet6Address ia6_addr = (Inet6Address )address; 378 379 if (!differentLocalAddressTypes(ia6_addr)){ 380 381 continue; 382 } 383 384 return ia6_addr.scope_id; 385 } 386 } 387 } 388 throw new UnknownHostException ("No matching address found for interface : " +ifname); 389 } 390 391 396 private void readObject(ObjectInputStream s) 397 throws IOException , ClassNotFoundException { 398 scope_ifname = null; 399 scope_ifname_set = false; 400 s.defaultReadObject(); 401 402 if (ifname != null && !"".equals (ifname)) { 403 try { 404 scope_ifname = NetworkInterface.getByName(ifname); 405 try { 406 scope_id = deriveNumericScope (scope_ifname); 407 } catch (UnknownHostException e) { 408 assert false; 410 } 411 } catch (SocketException e) {} 412 413 if (scope_ifname == null) { 414 416 scope_id_set = false; 417 scope_ifname_set = false; 418 scope_id = 0; 419 } 420 } 421 422 423 ipaddress = (byte[])ipaddress.clone(); 424 425 if (ipaddress.length != INADDRSZ) { 427 throw new InvalidObjectException ("invalid address length: "+ 428 ipaddress.length); 429 } 430 431 if (family != IPv6) { 432 throw new InvalidObjectException ("invalid address family type"); 433 } 434 } 435 436 445 public boolean isMulticastAddress() { 446 return ((ipaddress[0] & 0xff) == 0xff); 447 } 448 449 455 public boolean isAnyLocalAddress() { 456 byte test = 0x00; 457 for (int i = 0; i < INADDRSZ; i++) { 458 test |= ipaddress[i]; 459 } 460 return (test == 0x00); 461 } 462 463 470 public boolean isLoopbackAddress() { 471 byte test = 0x00; 472 for (int i = 0; i < 15; i++) { 473 test |= ipaddress[i]; 474 } 475 return (test == 0x00) && (ipaddress[15] == 0x01); 476 } 477 478 485 public boolean isLinkLocalAddress() { 486 return ((ipaddress[0] & 0xff) == 0xfe 487 && (ipaddress[1] & 0xc0) == 0x80); 488 } 489 490 497 public boolean isSiteLocalAddress() { 498 return ((ipaddress[0] & 0xff) == 0xfe 499 && (ipaddress[1] & 0xc0) == 0xc0); 500 } 501 502 510 public boolean isMCGlobal() { 511 return ((ipaddress[0] & 0xff) == 0xff 512 && (ipaddress[1] & 0x0f) == 0x0e); 513 } 514 515 523 public boolean isMCNodeLocal() { 524 return ((ipaddress[0] & 0xff) == 0xff 525 && (ipaddress[1] & 0x0f) == 0x01); 526 } 527 528 536 public boolean isMCLinkLocal() { 537 return ((ipaddress[0] & 0xff) == 0xff 538 && (ipaddress[1] & 0x0f) == 0x02); 539 } 540 541 549 public boolean isMCSiteLocal() { 550 return ((ipaddress[0] & 0xff) == 0xff 551 && (ipaddress[1] & 0x0f) == 0x05); 552 } 553 554 563 public boolean isMCOrgLocal() { 564 return ((ipaddress[0] & 0xff) == 0xff 565 && (ipaddress[1] & 0x0f) == 0x08); 566 } 567 568 575 public byte[] getAddress() { 576 return (byte[])ipaddress.clone(); 577 } 578 579 586 public int getScopeId () { 587 return scope_id; 588 } 589 590 597 public NetworkInterface getScopedInterface () { 598 return scope_ifname; 599 } 600 601 609 public String getHostAddress() { 610 String s = numericToTextFormat(ipaddress); 611 if (scope_ifname_set) { 612 s = s + "%" + scope_ifname.getName(); 613 } else if (scope_id_set) { 614 s = s + "%" + scope_id; 615 } 616 return s; 617 } 618 619 624 public int hashCode() { 625 if (ipaddress != null) { 626 627 int hash = 0; 628 int i=0; 629 while (i<INADDRSZ) { 630 int j=0; 631 int component=0; 632 while (j<4 && i<INADDRSZ) { 633 component = (component << 8) + ipaddress[i]; 634 j++; 635 i++; 636 } 637 hash += component; 638 } 639 return hash; 640 641 } else { 642 return 0; 643 } 644 } 645 646 662 public boolean equals(Object obj) { 663 if (obj == null || 664 !(obj instanceof Inet6Address )) 665 return false; 666 667 Inet6Address inetAddr = (Inet6Address )obj; 668 669 for (int i = 0; i < INADDRSZ; i++) { 670 if (ipaddress[i] != inetAddr.ipaddress[i]) 671 return false; 672 } 673 674 return true; 675 } 676 677 685 public boolean isIPv4CompatibleAddress() { 686 if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) && 687 (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) && 688 (ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) && 689 (ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) && 690 (ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) && 691 (ipaddress[10] == 0x00) && (ipaddress[11] == 0x00)) { 692 return true; 693 } 694 return false; 695 } 696 697 private final static int INT16SZ = 2; 699 707 static String numericToTextFormat(byte[] src) 708 { 709 StringBuffer sb = new StringBuffer (39); 710 for (int i = 0; i < (INADDRSZ / INT16SZ); i++) { 711 sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00) 712 | (src[(i<<1)+1] & 0xff))); 713 if (i < (INADDRSZ / INT16SZ) -1 ) { 714 sb.append(":"); 715 } 716 } 717 return sb.toString(); 718 } 719 720 723 private static native void init(); 724 725 728 private String ifname; 729 730 735 private synchronized void writeObject(java.io.ObjectOutputStream s) 736 throws IOException 737 { 738 if (scope_ifname_set) { 739 ifname = scope_ifname.getName(); 740 } 741 s.defaultWriteObject(); 742 } 743 } 744 745 | Popular Tags |