mengdiwang / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

InetAddresses.isMappedIPv4Address(String) #536

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Considering that InetAddresses states:

 * <li>An IPv6 "IPv4 mapped" address, {@code "::ffff:192.168.0.1"}.<br/>
 *     {@code 00 00 00 00 00 00 00 00 00 00 ff ff c0 a8 00 01}
 * </ul>
 *
 * <p>A few notes about IPv6 "IPv4 mapped" addresses and their observed
 * use in Java.
 * <br><br>
 * "IPv4 mapped" addresses were originally a representation of IPv4
 * addresses for use on an IPv6 socket that could receive both IPv4
 * and IPv6 connections (by disabling the {@code IPV6_V6ONLY} socket
 * option on an IPv6 socket).  Yes, it's confusing.  Nevertheless,
 * these "mapped" addresses were never supposed to be seen on the
 * wire.  That assumption was dropped, some say mistakenly, in later
 * RFCs with the apparent aim of making IPv4-to-IPv6 transition simpler.
 *
 * <p>Technically one <i>can</i> create a 128bit IPv6 address with the wire
 * format of a "mapped" address, as shown above, and transmit it in an
 * IPv6 packet header.  However, Java's InetAddress creation methods
 * appear to adhere doggedly to the original intent of the "mapped"
 * address: all "mapped" addresses return {@link Inet4Address} objects.
 *
 * <p>For added safety, it is common for IPv6 network operators to filter
 * all packets where either the source or destination address appears to
 * be a "compat" or "mapped" address.  Filtering suggestions usually
 * recommend discarding any packets with source or destination addresses
 * in the invalid range {@code ::/3}, which includes both of these bizarre
 * address formats.  For more information on "bogons", including lists
 * of IPv6 bogon space 
(...)

and has:

  /**
   * Examines the Inet6Address to determine if it is an IPv6 address of one
   * of the specified address types that contain an embedded IPv4 address.
   *
   * <p>NOTE: ISATAP addresses are explicitly excluded from this method
   * due to their trivial spoofability.  With other transition addresses
   * spoofing involves (at least) infection of one's BGP routing table.
   *
   * @param ip {@link Inet6Address} to be examined for embedded IPv4
   *           client address.
   * @return {@code true} if there is an embedded IPv4 client address.
   * @since 7
   */
  public static boolean hasEmbeddedIPv4ClientAddress(Inet6Address ip) {
    return isCompatIPv4Address(ip) || is6to4Address(ip) ||
           isTeredoAddress(ip);
  }

It seems necessary to have a:
public static boolean isMappedIPv4Address(String ip); 
method.

Necessity appears because of the general java approach to treat IPv4mapped 
addresses as IPv4 therefore ipv6 format address would become an Inet4Address 
and no information about it's form could be retrieved.
This could cause a problems e.g.: for old ipv4 parsers (they would not accept a 
colon).

Original issue reported on code.google.com by krzyszto...@gmail.com on 31 Jan 2011 at 2:02

GoogleCodeExporter commented 9 years ago
(It's possible there's some good reason this method isn't there, but I don't 
know it offhand.)

Original comment by kevinb@google.com on 31 Jan 2011 at 9:56

GoogleCodeExporter commented 9 years ago
Hm... the current implementation converts the text into a byte[], then uses 
InetAddress.getByAddress(byte[]).

There's some magic in InetAddress.getByAddress() that auto-converts 
"::ffff:102:304" to "1.2.3.4".

We *could* bypass this magic by explicitly calling Inet6Address.getByAddress(), 
but I'm not sure if that's the right thing to do.

Original comment by pma...@google.com on 5 May 2011 at 11:05

GoogleCodeExporter commented 9 years ago
Last time I checked it was impossible to make Java instantiate such a thing.  
When I tried to pass the byte array into Inet6Address.getByAddress() I got back 
an Inet4Address (because getByAddress() returns an InetAddress).

If I'm wrong, that's good to know, but otherwise this is why there are no 
"mapped" methods.  That doesn't mean we shouldn't have a String evaluating 
method that checks the syntax; I think perhaps we should for completeness.  But 
regardless, I could never actually instantiate such a thing.

Original comment by ek@google.com on 6 May 2011 at 7:43

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 6:18

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 13 Jul 2011 at 7:44

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 1 Aug 2011 at 9:44

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 3 Aug 2011 at 7:45

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r609.

Original comment by guava.mi...@gmail.com on 4 Aug 2011 at 10:57

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09