< prev index next >

src/java.base/share/classes/java/net/Inet6Address.java

Print this page




 346         }
 347 
 348         boolean isMCLinkLocal() {
 349             return ((ipaddress[0] & 0xff) == 0xff
 350                     && (ipaddress[1] & 0x0f) == 0x02);
 351         }
 352 
 353         boolean isMCSiteLocal() {
 354             return ((ipaddress[0] & 0xff) == 0xff
 355                     && (ipaddress[1] & 0x0f) == 0x05);
 356         }
 357 
 358         boolean isMCOrgLocal() {
 359             return ((ipaddress[0] & 0xff) == 0xff
 360                     && (ipaddress[1] & 0x0f) == 0x08);
 361         }
 362     }
 363 
 364     private final transient Inet6AddressHolder holder6;
 365 

 366     private static final long serialVersionUID = 6880410070516793377L;
 367 
 368     // Perform native initialization
 369     static { init(); }
 370 
 371     Inet6Address() {
 372         super();
 373         holder.init(null, IPv6);
 374         holder6 = new Inet6AddressHolder();
 375     }
 376 
 377     /* checking of value for scope_id should be done by caller
 378      * scope_id must be >= 0, or -1 to indicate not being set
 379      */
 380     Inet6Address(String hostName, byte addr[], int scope_id) {
 381         holder.init(hostName, IPv6);
 382         holder6 = new Inet6AddressHolder();
 383         holder6.init(addr, scope_id);
 384     }
 385 


 545             en = NetworkInterface.getNetworkInterfaces();
 546         } catch (SocketException e) {
 547             throw new UnknownHostException ("could not enumerate local network interfaces");
 548         }
 549         while (en.hasMoreElements()) {
 550             NetworkInterface ifc = en.nextElement();
 551             if (ifc.getName().equals (ifname)) {
 552                 return deriveNumericScope(holder6.ipaddress, ifc);
 553             }
 554         }
 555         throw new UnknownHostException ("No matching address found for interface : " +ifname);
 556     }
 557 
 558     /**
 559      * @serialField ipaddress byte[]
 560      * @serialField scope_id int
 561      * @serialField scope_id_set boolean
 562      * @serialField scope_ifname_set boolean
 563      * @serialField ifname String
 564      */
 565 
 566     private static final ObjectStreamField[] serialPersistentFields = {
 567          new ObjectStreamField("ipaddress", byte[].class),
 568          new ObjectStreamField("scope_id", int.class),
 569          new ObjectStreamField("scope_id_set", boolean.class),
 570          new ObjectStreamField("scope_ifname_set", boolean.class),
 571          new ObjectStreamField("ifname", String.class)
 572     };
 573 
 574     private static final jdk.internal.misc.Unsafe UNSAFE
 575             = jdk.internal.misc.Unsafe.getUnsafe();
 576     private static final long FIELDS_OFFSET = UNSAFE.objectFieldOffset(
 577                 Inet6Address.class, "holder6");
 578 
 579     /**
 580      * restore the state of this object from stream
 581      * including the scope information, only if the
 582      * scoped interface name is valid on this system
 583      */

 584     private void readObject(ObjectInputStream s)
 585         throws IOException, ClassNotFoundException {
 586         NetworkInterface scope_ifname = null;
 587 
 588         if (getClass().getClassLoader() != null) {
 589             throw new SecurityException ("invalid address type");
 590         }
 591 
 592         ObjectInputStream.GetField gf = s.readFields();
 593         byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]);
 594         int scope_id = gf.get("scope_id", -1);
 595         boolean scope_id_set = gf.get("scope_id_set", false);
 596         boolean scope_ifname_set = gf.get("scope_ifname_set", false);
 597         String ifname = (String)gf.get("ifname", null);
 598 
 599         if (ifname != null && !ifname.isEmpty()) {
 600             try {
 601                 scope_ifname = NetworkInterface.getByName(ifname);
 602                 if (scope_ifname == null) {
 603                     /* the interface does not exist on this system, so we clear


 627             throw new InvalidObjectException("invalid address length: "+
 628                                              ipaddress.length);
 629         }
 630 
 631         if (holder.getFamily() != IPv6) {
 632             throw new InvalidObjectException("invalid address family type");
 633         }
 634 
 635         Inet6AddressHolder h = new Inet6AddressHolder(
 636             ipaddress, scope_id, scope_id_set, scope_ifname, scope_ifname_set
 637         );
 638 
 639         UNSAFE.putReference(this, FIELDS_OFFSET, h);
 640     }
 641 
 642     /**
 643      * default behavior is overridden in order to write the
 644      * scope_ifname field as a String, rather than a NetworkInterface
 645      * which is not serializable
 646      */

 647     private synchronized void writeObject(ObjectOutputStream s)
 648         throws IOException
 649     {
 650             String ifname = null;
 651 
 652         if (holder6.scope_ifname != null) {
 653             ifname = holder6.scope_ifname.getName();
 654             holder6.scope_ifname_set = true;
 655         }
 656         ObjectOutputStream.PutField pfields = s.putFields();
 657         pfields.put("ipaddress", holder6.ipaddress);
 658         pfields.put("scope_id", holder6.scope_id);
 659         pfields.put("scope_id_set", holder6.scope_id_set);
 660         pfields.put("scope_ifname_set", holder6.scope_ifname_set);
 661         pfields.put("ifname", ifname);
 662         s.writeFields();
 663     }
 664 
 665     /**
 666      * Utility routine to check if the InetAddress is an IP multicast




 346         }
 347 
 348         boolean isMCLinkLocal() {
 349             return ((ipaddress[0] & 0xff) == 0xff
 350                     && (ipaddress[1] & 0x0f) == 0x02);
 351         }
 352 
 353         boolean isMCSiteLocal() {
 354             return ((ipaddress[0] & 0xff) == 0xff
 355                     && (ipaddress[1] & 0x0f) == 0x05);
 356         }
 357 
 358         boolean isMCOrgLocal() {
 359             return ((ipaddress[0] & 0xff) == 0xff
 360                     && (ipaddress[1] & 0x0f) == 0x08);
 361         }
 362     }
 363 
 364     private final transient Inet6AddressHolder holder6;
 365 
 366     @java.io.Serial
 367     private static final long serialVersionUID = 6880410070516793377L;
 368 
 369     // Perform native initialization
 370     static { init(); }
 371 
 372     Inet6Address() {
 373         super();
 374         holder.init(null, IPv6);
 375         holder6 = new Inet6AddressHolder();
 376     }
 377 
 378     /* checking of value for scope_id should be done by caller
 379      * scope_id must be >= 0, or -1 to indicate not being set
 380      */
 381     Inet6Address(String hostName, byte addr[], int scope_id) {
 382         holder.init(hostName, IPv6);
 383         holder6 = new Inet6AddressHolder();
 384         holder6.init(addr, scope_id);
 385     }
 386 


 546             en = NetworkInterface.getNetworkInterfaces();
 547         } catch (SocketException e) {
 548             throw new UnknownHostException ("could not enumerate local network interfaces");
 549         }
 550         while (en.hasMoreElements()) {
 551             NetworkInterface ifc = en.nextElement();
 552             if (ifc.getName().equals (ifname)) {
 553                 return deriveNumericScope(holder6.ipaddress, ifc);
 554             }
 555         }
 556         throw new UnknownHostException ("No matching address found for interface : " +ifname);
 557     }
 558 
 559     /**
 560      * @serialField ipaddress byte[]
 561      * @serialField scope_id int
 562      * @serialField scope_id_set boolean
 563      * @serialField scope_ifname_set boolean
 564      * @serialField ifname String
 565      */
 566     @java.io.Serial
 567     private static final ObjectStreamField[] serialPersistentFields = {
 568          new ObjectStreamField("ipaddress", byte[].class),
 569          new ObjectStreamField("scope_id", int.class),
 570          new ObjectStreamField("scope_id_set", boolean.class),
 571          new ObjectStreamField("scope_ifname_set", boolean.class),
 572          new ObjectStreamField("ifname", String.class)
 573     };
 574 
 575     private static final jdk.internal.misc.Unsafe UNSAFE
 576             = jdk.internal.misc.Unsafe.getUnsafe();
 577     private static final long FIELDS_OFFSET = UNSAFE.objectFieldOffset(
 578                 Inet6Address.class, "holder6");
 579 
 580     /**
 581      * restore the state of this object from stream
 582      * including the scope information, only if the
 583      * scoped interface name is valid on this system
 584      */
 585     @java.io.Serial
 586     private void readObject(ObjectInputStream s)
 587         throws IOException, ClassNotFoundException {
 588         NetworkInterface scope_ifname = null;
 589 
 590         if (getClass().getClassLoader() != null) {
 591             throw new SecurityException ("invalid address type");
 592         }
 593 
 594         ObjectInputStream.GetField gf = s.readFields();
 595         byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]);
 596         int scope_id = gf.get("scope_id", -1);
 597         boolean scope_id_set = gf.get("scope_id_set", false);
 598         boolean scope_ifname_set = gf.get("scope_ifname_set", false);
 599         String ifname = (String)gf.get("ifname", null);
 600 
 601         if (ifname != null && !ifname.isEmpty()) {
 602             try {
 603                 scope_ifname = NetworkInterface.getByName(ifname);
 604                 if (scope_ifname == null) {
 605                     /* the interface does not exist on this system, so we clear


 629             throw new InvalidObjectException("invalid address length: "+
 630                                              ipaddress.length);
 631         }
 632 
 633         if (holder.getFamily() != IPv6) {
 634             throw new InvalidObjectException("invalid address family type");
 635         }
 636 
 637         Inet6AddressHolder h = new Inet6AddressHolder(
 638             ipaddress, scope_id, scope_id_set, scope_ifname, scope_ifname_set
 639         );
 640 
 641         UNSAFE.putReference(this, FIELDS_OFFSET, h);
 642     }
 643 
 644     /**
 645      * default behavior is overridden in order to write the
 646      * scope_ifname field as a String, rather than a NetworkInterface
 647      * which is not serializable
 648      */
 649     @java.io.Serial
 650     private synchronized void writeObject(ObjectOutputStream s)
 651         throws IOException
 652     {
 653             String ifname = null;
 654 
 655         if (holder6.scope_ifname != null) {
 656             ifname = holder6.scope_ifname.getName();
 657             holder6.scope_ifname_set = true;
 658         }
 659         ObjectOutputStream.PutField pfields = s.putFields();
 660         pfields.put("ipaddress", holder6.ipaddress);
 661         pfields.put("scope_id", holder6.scope_id);
 662         pfields.put("scope_id_set", holder6.scope_id_set);
 663         pfields.put("scope_ifname_set", holder6.scope_ifname_set);
 664         pfields.put("ifname", ifname);
 665         s.writeFields();
 666     }
 667 
 668     /**
 669      * Utility routine to check if the InetAddress is an IP multicast


< prev index next >