211 int scope_id; // 0 212 213 /** 214 * This will be set to true when the scope_id field contains a valid 215 * integer scope_id. 216 */ 217 boolean scope_id_set; // false 218 219 /** 220 * scoped interface. scope_id is derived from this as the scope_id of the first 221 * address whose scope is the same as this address for the named interface. 222 */ 223 NetworkInterface scope_ifname; // null 224 225 /** 226 * set if the object is constructed with a scoped 227 * interface instead of a numeric scope id. 228 */ 229 boolean scope_ifname_set; // false; 230 231 void setAddr(byte addr[]) { 232 if (addr.length == INADDRSZ) { // normal IPv6 address 233 System.arraycopy(addr, 0, ipaddress, 0, INADDRSZ); 234 } 235 } 236 237 void init(byte addr[], int scope_id) { 238 setAddr(addr); 239 240 if (scope_id >= 0) { 241 this.scope_id = scope_id; 242 this.scope_id_set = true; 243 } 244 } 245 246 void init(byte addr[], NetworkInterface nif) 247 throws UnknownHostException 248 { 249 setAddr(addr); 250 251 if (nif != null) { 252 this.scope_id = deriveNumericScope(ipaddress, nif); 253 this.scope_id_set = true; 254 this.scope_ifname = nif; 255 this.scope_ifname_set = true; 256 } 257 } 258 259 String getHostAddress() { 260 String s = numericToTextFormat(ipaddress); 261 if (scope_ifname != null) { /* must check this first */ 262 s = s + "%" + scope_ifname.getName(); 263 } else if (scope_id_set) { 264 s = s + "%" + scope_id; 265 } 266 return s; 365 && (ipaddress[1] & 0x0f) == 0x08); 366 } 367 } 368 369 private final transient Inet6AddressHolder holder6; 370 371 private static final long serialVersionUID = 6880410070516793377L; 372 373 // Perform native initialization 374 static { init(); } 375 376 Inet6Address() { 377 super(); 378 holder.init(null, IPv6); 379 holder6 = new Inet6AddressHolder(); 380 } 381 382 /* checking of value for scope_id should be done by caller 383 * scope_id must be >= 0, or -1 to indicate not being set 384 */ 385 Inet6Address(String hostName, byte addr[], int scope_id) { 386 holder.init(hostName, IPv6); 387 holder6 = new Inet6AddressHolder(); 388 holder6.init(addr, scope_id); 389 } 390 391 Inet6Address(String hostName, byte addr[]) { 392 holder6 = new Inet6AddressHolder(); 393 try { 394 initif (hostName, addr, null); 395 } catch (UnknownHostException e) {} /* cant happen if ifname is null */ 396 } 397 398 Inet6Address (String hostName, byte addr[], NetworkInterface nif) 399 throws UnknownHostException 400 { 401 holder6 = new Inet6AddressHolder(); 402 initif (hostName, addr, nif); 403 } 404 405 Inet6Address (String hostName, byte addr[], String ifname) 406 throws UnknownHostException 407 { 408 holder6 = new Inet6AddressHolder(); 409 initstr (hostName, addr, ifname); 410 } 411 412 /** 413 * Create an Inet6Address in the exact manner of {@link 414 * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is 415 * set to the value corresponding to the given interface for the address 416 * type specified in {@code addr}. The call will fail with an 417 * UnknownHostException if the given interface does not have a numeric 418 * scope_id assigned for the given address type (eg. link-local or site-local). 419 * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 420 * scoped addresses. 421 * 422 * @param host the specified host 423 * @param addr the raw IP address in network byte order 424 * @param nif an interface this address must be associated with. 425 * @return an Inet6Address object created from the raw IP address. 462 * 463 * @since 1.5 464 */ 465 public static Inet6Address getByAddress(String host, byte[] addr, 466 int scope_id) 467 throws UnknownHostException 468 { 469 if (host != null && host.length() > 0 && host.charAt(0) == '[') { 470 if (host.charAt(host.length()-1) == ']') { 471 host = host.substring(1, host.length() -1); 472 } 473 } 474 if (addr != null) { 475 if (addr.length == Inet6Address.INADDRSZ) { 476 return new Inet6Address(host, addr, scope_id); 477 } 478 } 479 throw new UnknownHostException("addr is of illegal length"); 480 } 481 482 private void initstr(String hostName, byte addr[], String ifname) 483 throws UnknownHostException 484 { 485 try { 486 NetworkInterface nif = NetworkInterface.getByName (ifname); 487 if (nif == null) { 488 throw new UnknownHostException ("no such interface " + ifname); 489 } 490 initif (hostName, addr, nif); 491 } catch (SocketException e) { 492 throw new UnknownHostException ("SocketException thrown" + ifname); 493 } 494 } 495 496 private void initif(String hostName, byte addr[], NetworkInterface nif) 497 throws UnknownHostException 498 { 499 int family = -1; 500 holder6.init(addr, nif); 501 502 if (addr.length == INADDRSZ) { // normal IPv6 address 503 family = IPv6; 504 } 505 holder.init(hostName, family); 506 } 507 508 /* check the two Ipv6 addresses and return false if they are both 509 * non global address types, but not the same. 510 * (ie. one is sitelocal and the other linklocal) 511 * return true otherwise. 512 */ 513 514 private static boolean isDifferentLocalAddressType( 515 byte[] thisAddr, byte[] otherAddr) { 516 | 211 int scope_id; // 0 212 213 /** 214 * This will be set to true when the scope_id field contains a valid 215 * integer scope_id. 216 */ 217 boolean scope_id_set; // false 218 219 /** 220 * scoped interface. scope_id is derived from this as the scope_id of the first 221 * address whose scope is the same as this address for the named interface. 222 */ 223 NetworkInterface scope_ifname; // null 224 225 /** 226 * set if the object is constructed with a scoped 227 * interface instead of a numeric scope id. 228 */ 229 boolean scope_ifname_set; // false; 230 231 void setAddr(byte[] addr) { 232 if (addr.length == INADDRSZ) { // normal IPv6 address 233 System.arraycopy(addr, 0, ipaddress, 0, INADDRSZ); 234 } 235 } 236 237 void init(byte[] addr, int scope_id) { 238 setAddr(addr); 239 240 if (scope_id >= 0) { 241 this.scope_id = scope_id; 242 this.scope_id_set = true; 243 } 244 } 245 246 void init(byte[] addr, NetworkInterface nif) 247 throws UnknownHostException 248 { 249 setAddr(addr); 250 251 if (nif != null) { 252 this.scope_id = deriveNumericScope(ipaddress, nif); 253 this.scope_id_set = true; 254 this.scope_ifname = nif; 255 this.scope_ifname_set = true; 256 } 257 } 258 259 String getHostAddress() { 260 String s = numericToTextFormat(ipaddress); 261 if (scope_ifname != null) { /* must check this first */ 262 s = s + "%" + scope_ifname.getName(); 263 } else if (scope_id_set) { 264 s = s + "%" + scope_id; 265 } 266 return s; 365 && (ipaddress[1] & 0x0f) == 0x08); 366 } 367 } 368 369 private final transient Inet6AddressHolder holder6; 370 371 private static final long serialVersionUID = 6880410070516793377L; 372 373 // Perform native initialization 374 static { init(); } 375 376 Inet6Address() { 377 super(); 378 holder.init(null, IPv6); 379 holder6 = new Inet6AddressHolder(); 380 } 381 382 /* checking of value for scope_id should be done by caller 383 * scope_id must be >= 0, or -1 to indicate not being set 384 */ 385 Inet6Address(String hostName, byte[] addr, int scope_id) { 386 holder.init(hostName, IPv6); 387 holder6 = new Inet6AddressHolder(); 388 holder6.init(addr, scope_id); 389 } 390 391 Inet6Address(String hostName, byte[] addr) { 392 holder6 = new Inet6AddressHolder(); 393 try { 394 initif (hostName, addr, null); 395 } catch (UnknownHostException e) {} /* cant happen if ifname is null */ 396 } 397 398 Inet6Address (String hostName, byte[] addr, NetworkInterface nif) 399 throws UnknownHostException 400 { 401 holder6 = new Inet6AddressHolder(); 402 initif (hostName, addr, nif); 403 } 404 405 Inet6Address (String hostName, byte[] addr, String ifname) 406 throws UnknownHostException 407 { 408 holder6 = new Inet6AddressHolder(); 409 initstr (hostName, addr, ifname); 410 } 411 412 /** 413 * Create an Inet6Address in the exact manner of {@link 414 * InetAddress#getByAddress(String,byte[])} except that the IPv6 scope_id is 415 * set to the value corresponding to the given interface for the address 416 * type specified in {@code addr}. The call will fail with an 417 * UnknownHostException if the given interface does not have a numeric 418 * scope_id assigned for the given address type (eg. link-local or site-local). 419 * See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6 420 * scoped addresses. 421 * 422 * @param host the specified host 423 * @param addr the raw IP address in network byte order 424 * @param nif an interface this address must be associated with. 425 * @return an Inet6Address object created from the raw IP address. 462 * 463 * @since 1.5 464 */ 465 public static Inet6Address getByAddress(String host, byte[] addr, 466 int scope_id) 467 throws UnknownHostException 468 { 469 if (host != null && host.length() > 0 && host.charAt(0) == '[') { 470 if (host.charAt(host.length()-1) == ']') { 471 host = host.substring(1, host.length() -1); 472 } 473 } 474 if (addr != null) { 475 if (addr.length == Inet6Address.INADDRSZ) { 476 return new Inet6Address(host, addr, scope_id); 477 } 478 } 479 throw new UnknownHostException("addr is of illegal length"); 480 } 481 482 private void initstr(String hostName, byte[] addr, String ifname) 483 throws UnknownHostException 484 { 485 try { 486 NetworkInterface nif = NetworkInterface.getByName (ifname); 487 if (nif == null) { 488 throw new UnknownHostException ("no such interface " + ifname); 489 } 490 initif (hostName, addr, nif); 491 } catch (SocketException e) { 492 throw new UnknownHostException ("SocketException thrown" + ifname); 493 } 494 } 495 496 private void initif(String hostName, byte[] addr, NetworkInterface nif) 497 throws UnknownHostException 498 { 499 int family = -1; 500 holder6.init(addr, nif); 501 502 if (addr.length == INADDRSZ) { // normal IPv6 address 503 family = IPv6; 504 } 505 holder.init(hostName, family); 506 } 507 508 /* check the two Ipv6 addresses and return false if they are both 509 * non global address types, but not the same. 510 * (ie. one is sitelocal and the other linklocal) 511 * return true otherwise. 512 */ 513 514 private static boolean isDifferentLocalAddressType( 515 byte[] thisAddr, byte[] otherAddr) { 516 |