1 /*
  2  * Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.net.util;
 27 
 28 import java.io.IOException;
 29 import java.io.UncheckedIOException;
 30 import java.net.Inet6Address;
 31 import java.net.InetAddress;
 32 import java.net.InetSocketAddress;
 33 import java.net.NetworkInterface;
 34 import java.net.SocketException;
 35 import java.net.URL;
 36 import java.security.AccessController;
 37 import java.security.PrivilegedExceptionAction;
 38 import java.security.PrivilegedActionException;
 39 import java.util.Arrays;
 40 import java.util.List;
 41 import java.util.concurrent.ConcurrentHashMap;
 42 import java.util.stream.Collectors;
 43 
 44 public class IPAddressUtil {
 45     private static final int INADDR4SZ = 4;
 46     private static final int INADDR16SZ = 16;
 47     private static final int INT16SZ = 2;
 48 
 49     /*
 50      * Converts IPv4 address in its textual presentation form
 51      * into its numeric binary form.
 52      *
 53      * @param src a String representing an IPv4 address in standard format
 54      * @return a byte array representing the IPv4 numeric address
 55      */
 56     @SuppressWarnings("fallthrough")
 57     public static byte[] textToNumericFormatV4(String src)
 58     {
 59         byte[] res = new byte[INADDR4SZ];
 60 
 61         long tmpValue = 0;
 62         int currByte = 0;
 63         boolean newOctet = true;
 64 
 65         int len = src.length();
 66         if (len == 0 || len > 15) {
 67             return null;
 68         }
 69         /*
 70          * When only one part is given, the value is stored directly in
 71          * the network address without any byte rearrangement.
 72          *
 73          * When a two part address is supplied, the last part is
 74          * interpreted as a 24-bit quantity and placed in the right
 75          * most three bytes of the network address. This makes the
 76          * two part address format convenient for specifying Class A
 77          * network addresses as net.host.
 78          *
 79          * When a three part address is specified, the last part is
 80          * interpreted as a 16-bit quantity and placed in the right
 81          * most two bytes of the network address. This makes the
 82          * three part address format convenient for specifying
 83          * Class B net- work addresses as 128.net.host.
 84          *
 85          * When four parts are specified, each is interpreted as a
 86          * byte of data and assigned, from left to right, to the
 87          * four bytes of an IPv4 address.
 88          *
 89          * We determine and parse the leading parts, if any, as single
 90          * byte values in one pass directly into the resulting byte[],
 91          * then the remainder is treated as a 8-to-32-bit entity and
 92          * translated into the remaining bytes in the array.
 93          */
 94         for (int i = 0; i < len; i++) {
 95             char c = src.charAt(i);
 96             if (c == '.') {
 97                 if (newOctet || tmpValue < 0 || tmpValue > 0xff || currByte == 3) {
 98                     return null;
 99                 }
100                 res[currByte++] = (byte) (tmpValue & 0xff);
101                 tmpValue = 0;
102                 newOctet = true;
103             } else {
104                 int digit = Character.digit(c, 10);
105                 if (digit < 0) {
106                     return null;
107                 }
108                 tmpValue *= 10;
109                 tmpValue += digit;
110                 newOctet = false;
111             }
112         }
113         if (newOctet || tmpValue < 0 || tmpValue >= (1L << ((4 - currByte) * 8))) {
114             return null;
115         }
116         switch (currByte) {
117             case 0:
118                 res[0] = (byte) ((tmpValue >> 24) & 0xff);
119             case 1:
120                 res[1] = (byte) ((tmpValue >> 16) & 0xff);
121             case 2:
122                 res[2] = (byte) ((tmpValue >>  8) & 0xff);
123             case 3:
124                 res[3] = (byte) ((tmpValue >>  0) & 0xff);
125         }
126         return res;
127     }
128 
129     /*
130      * Convert IPv6 presentation level address to network order binary form.
131      * credit:
132      *  Converted from C code from Solaris 8 (inet_pton)
133      *
134      * Any component of the string following a per-cent % is ignored.
135      *
136      * @param src a String representing an IPv6 address in textual format
137      * @return a byte array representing the IPv6 numeric address
138      */
139     public static byte[] textToNumericFormatV6(String src)
140     {
141         // Shortest valid string is "::", hence at least 2 chars
142         if (src.length() < 2) {
143             return null;
144         }
145 
146         int colonp;
147         char ch;
148         boolean saw_xdigit;
149         int val;
150         char[] srcb = src.toCharArray();
151         byte[] dst = new byte[INADDR16SZ];
152 
153         int srcb_length = srcb.length;
154         int pc = src.indexOf ('%');
155         if (pc == srcb_length -1) {
156             return null;
157         }
158 
159         if (pc != -1) {
160             srcb_length = pc;
161         }
162 
163         colonp = -1;
164         int i = 0, j = 0;
165         /* Leading :: requires some special handling. */
166         if (srcb[i] == ':')
167             if (srcb[++i] != ':')
168                 return null;
169         int curtok = i;
170         saw_xdigit = false;
171         val = 0;
172         while (i < srcb_length) {
173             ch = srcb[i++];
174             int chval = Character.digit(ch, 16);
175             if (chval != -1) {
176                 val <<= 4;
177                 val |= chval;
178                 if (val > 0xffff)
179                     return null;
180                 saw_xdigit = true;
181                 continue;
182             }
183             if (ch == ':') {
184                 curtok = i;
185                 if (!saw_xdigit) {
186                     if (colonp != -1)
187                         return null;
188                     colonp = j;
189                     continue;
190                 } else if (i == srcb_length) {
191                     return null;
192                 }
193                 if (j + INT16SZ > INADDR16SZ)
194                     return null;
195                 dst[j++] = (byte) ((val >> 8) & 0xff);
196                 dst[j++] = (byte) (val & 0xff);
197                 saw_xdigit = false;
198                 val = 0;
199                 continue;
200             }
201             if (ch == '.' && ((j + INADDR4SZ) <= INADDR16SZ)) {
202                 String ia4 = src.substring(curtok, srcb_length);
203                 /* check this IPv4 address has 3 dots, i.e. A.B.C.D */
204                 int dot_count = 0, index=0;
205                 while ((index = ia4.indexOf ('.', index)) != -1) {
206                     dot_count ++;
207                     index ++;
208                 }
209                 if (dot_count != 3) {
210                     return null;
211                 }
212                 byte[] v4addr = textToNumericFormatV4(ia4);
213                 if (v4addr == null) {
214                     return null;
215                 }
216                 for (int k = 0; k < INADDR4SZ; k++) {
217                     dst[j++] = v4addr[k];
218                 }
219                 saw_xdigit = false;
220                 break;  /* '\0' was seen by inet_pton4(). */
221             }
222             return null;
223         }
224         if (saw_xdigit) {
225             if (j + INT16SZ > INADDR16SZ)
226                 return null;
227             dst[j++] = (byte) ((val >> 8) & 0xff);
228             dst[j++] = (byte) (val & 0xff);
229         }
230 
231         if (colonp != -1) {
232             int n = j - colonp;
233 
234             if (j == INADDR16SZ)
235                 return null;
236             for (i = 1; i <= n; i++) {
237                 dst[INADDR16SZ - i] = dst[colonp + n - i];
238                 dst[colonp + n - i] = 0;
239             }
240             j = INADDR16SZ;
241         }
242         if (j != INADDR16SZ)
243             return null;
244         byte[] newdst = convertFromIPv4MappedAddress(dst);
245         if (newdst != null) {
246             return newdst;
247         } else {
248             return dst;
249         }
250     }
251 
252     /**
253      * @param src a String representing an IPv4 address in textual format
254      * @return a boolean indicating whether src is an IPv4 literal address
255      */
256     public static boolean isIPv4LiteralAddress(String src) {
257         return textToNumericFormatV4(src) != null;
258     }
259 
260     /**
261      * @param src a String representing an IPv6 address in textual format
262      * @return a boolean indicating whether src is an IPv6 literal address
263      */
264     public static boolean isIPv6LiteralAddress(String src) {
265         return textToNumericFormatV6(src) != null;
266     }
267 
268     /*
269      * Convert IPv4-Mapped address to IPv4 address. Both input and
270      * returned value are in network order binary form.
271      *
272      * @param src a String representing an IPv4-Mapped address in textual format
273      * @return a byte array representing the IPv4 numeric address
274      */
275     public static byte[] convertFromIPv4MappedAddress(byte[] addr) {
276         if (isIPv4MappedAddress(addr)) {
277             byte[] newAddr = new byte[INADDR4SZ];
278             System.arraycopy(addr, 12, newAddr, 0, INADDR4SZ);
279             return newAddr;
280         }
281         return null;
282     }
283 
284 
285     /**
286      * Returns the address value, as an int, of an IPv4-mapped IPv6 address.
287      */
288     public static int intFromIPv4MappedAddress(byte[] addr) {
289         assert isIPv4MappedAddress(addr);
290         return ((addr[12] & 0xFF) << 24) |
291                ((addr[13] & 0xFF) << 16) |
292                ((addr[14] & 0xFF) << 8)  |
293                 (addr[15] & 0xFF);
294     }
295 
296     /**
297      * Utility routine to check if the InetAddress is an
298      * IPv4 mapped IPv6 address.
299      *
300      * @return a <code>boolean</code> indicating if the InetAddress is
301      * an IPv4 mapped IPv6 address; or false if address is IPv4 address.
302      */
303     public static boolean isIPv4MappedAddress(byte[] addr) {
304         if (addr.length < INADDR16SZ) {
305             return false;
306         }
307         if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
308             (addr[2] == 0x00) && (addr[3] == 0x00) &&
309             (addr[4] == 0x00) && (addr[5] == 0x00) &&
310             (addr[6] == 0x00) && (addr[7] == 0x00) &&
311             (addr[8] == 0x00) && (addr[9] == 0x00) &&
312             (addr[10] == (byte)0xff) &&
313             (addr[11] == (byte)0xff))  {
314             return true;
315         }
316         return false;
317     }
318     /**
319      * Mapping from unscoped local Inet(6)Address to the same address
320      * including the correct scope-id, determined from NetworkInterface.
321      */
322     private final static ConcurrentHashMap<InetAddress,InetAddress>
323         cache = new ConcurrentHashMap<>();
324 
325     /**
326      * Returns a scoped version of the supplied local, link-local ipv6 address
327      * if that scope-id can be determined from local NetworkInterfaces.
328      * If the address already has a scope-id or if the address is not local, ipv6
329      * or link local, then the original address is returned.
330      *
331      * @param address
332      * @exception SocketException if the given ipv6 link local address is found
333      *            on more than one local interface
334      * @return
335      */
336     public static InetAddress toScopedAddress(InetAddress address)
337         throws SocketException {
338 
339         if (address instanceof Inet6Address && address.isLinkLocalAddress()
340             && ((Inet6Address) address).getScopeId() == 0) {
341 
342             InetAddress cached = null;
343             try {
344                 cached = cache.computeIfAbsent(address, k -> findScopedAddress(k));
345             } catch (UncheckedIOException e) {
346                 throw (SocketException)e.getCause();
347             }
348             return cached != null ? cached : address;
349         } else {
350             return address;
351         }
352     }
353 
354     /**
355      * Same as above for InetSocketAddress
356      */
357     public static InetSocketAddress toScopedAddress(InetSocketAddress address)
358         throws SocketException {
359         InetAddress addr;
360         InetAddress orig = address.getAddress();
361         if ((addr = toScopedAddress(orig)) == orig) {
362             return address;
363         } else {
364             return new InetSocketAddress(addr, address.getPort());
365         }
366     }
367 
368     private static InetAddress findScopedAddress(InetAddress address) {
369         PrivilegedExceptionAction<List<InetAddress>> pa = () -> NetworkInterface.networkInterfaces()
370                 .flatMap(NetworkInterface::inetAddresses)
371                 .filter(a -> (a instanceof Inet6Address)
372                         && address.equals(a)
373                         && ((Inet6Address) a).getScopeId() != 0)
374                 .collect(Collectors.toList());
375         List<InetAddress> result;
376         try {
377             result = AccessController.doPrivileged(pa);
378             var sz = result.size();
379             if (sz == 0)
380                 return null;
381             if (sz > 1)
382                 throw new UncheckedIOException(new SocketException(
383                     "Duplicate link local addresses: must specify scope-id"));
384             return result.get(0);
385         } catch (PrivilegedActionException pae) {
386             return null;
387         }
388     }
389 
390     // See java.net.URI for more details on how to generate these
391     // masks.
392     //
393     // square brackets
394     private static final long L_IPV6_DELIMS = 0x0L; // "[]"
395     private static final long H_IPV6_DELIMS = 0x28000000L; // "[]"
396     // RFC 3986 gen-delims
397     private static final long L_GEN_DELIMS = 0x8400800800000000L; // ":/?#[]@"
398     private static final long H_GEN_DELIMS = 0x28000001L; // ":/?#[]@"
399     // These gen-delims can appear in authority
400     private static final long L_AUTH_DELIMS = 0x400000000000000L; // "@[]:"
401     private static final long H_AUTH_DELIMS = 0x28000001L; // "@[]:"
402     // colon is allowed in userinfo
403     private static final long L_COLON = 0x400000000000000L; // ":"
404     private static final long H_COLON = 0x0L; // ":"
405     // slash should be encoded in authority
406     private static final long L_SLASH = 0x800000000000L; // "/"
407     private static final long H_SLASH = 0x0L; // "/"
408     // backslash should always be encoded
409     private static final long L_BACKSLASH = 0x0L; // "\"
410     private static final long H_BACKSLASH = 0x10000000L; // "\"
411     // ASCII chars 0-31 + 127 - various controls + CRLF + TAB
412     private static final long L_NON_PRINTABLE = 0xffffffffL;
413     private static final long H_NON_PRINTABLE = 0x8000000000000000L;
414     // All of the above
415     private static final long L_EXCLUDE = 0x84008008ffffffffL;
416     private static final long H_EXCLUDE = 0x8000000038000001L;
417 
418     private static final char[] OTHERS = {
419             8263,8264,8265,8448,8449,8453,8454,10868,
420             65109,65110,65119,65131,65283,65295,65306,65311,65312
421     };
422 
423     // Tell whether the given character is found by the given mask pair
424     public static boolean match(char c, long lowMask, long highMask) {
425         if (c < 64)
426             return ((1L << c) & lowMask) != 0;
427         if (c < 128)
428             return ((1L << (c - 64)) & highMask) != 0;
429         return false; // other non ASCII characters are not filtered
430     }
431 
432     // returns -1 if the string doesn't contain any characters
433     // from the mask, the index of the first such character found
434     // otherwise.
435     public static int scan(String s, long lowMask, long highMask) {
436         int i = -1, len;
437         if (s == null || (len = s.length()) == 0) return -1;
438         boolean match = false;
439         while (++i < len && !(match = match(s.charAt(i), lowMask, highMask)));
440         if (match) return i;
441         return -1;
442     }
443 
444     public static int scan(String s, long lowMask, long highMask, char[] others) {
445         int i = -1, len;
446         if (s == null || (len = s.length()) == 0) return -1;
447         boolean match = false;
448         char c, c0 = others[0];
449         while (++i < len && !(match = match((c=s.charAt(i)), lowMask, highMask))) {
450             if (c >= c0 && (Arrays.binarySearch(others, c) > -1)) {
451                 match = true; break;
452             }
453         }
454         if (match) return i;
455 
456         return -1;
457     }
458 
459     private static String describeChar(char c) {
460         if (c < 32 || c == 127) {
461             if (c == '\n') return "LF";
462             if (c == '\r') return "CR";
463             return "control char (code=" + (int)c + ")";
464         }
465         if (c == '\\') return "'\\'";
466         return "'" + c + "'";
467     }
468 
469     private static String checkUserInfo(String str) {
470         // colon is permitted in user info
471         int index = scan(str, L_EXCLUDE & ~L_COLON,
472                 H_EXCLUDE & ~H_COLON);
473         if (index >= 0) {
474             return "Illegal character found in user-info: "
475                     + describeChar(str.charAt(index));
476         }
477         return null;
478     }
479 
480     private static String checkHost(String str) {
481         int index;
482         if (str.startsWith("[") && str.endsWith("]")) {
483             str = str.substring(1, str.length() - 1);
484             if (isIPv6LiteralAddress(str)) {
485                 index = str.indexOf('%');
486                 if (index >= 0) {
487                     index = scan(str = str.substring(index),
488                             L_NON_PRINTABLE | L_IPV6_DELIMS,
489                             H_NON_PRINTABLE | H_IPV6_DELIMS);
490                     if (index >= 0) {
491                         return "Illegal character found in IPv6 scoped address: "
492                                 + describeChar(str.charAt(index));
493                     }
494                 }
495                 return null;
496             }
497             return "Unrecognized IPv6 address format";
498         } else {
499             index = scan(str, L_EXCLUDE, H_EXCLUDE);
500             if (index >= 0) {
501                 return "Illegal character found in host: "
502                         + describeChar(str.charAt(index));
503             }
504         }
505         return null;
506     }
507 
508     private static String checkAuth(String str) {
509         int index = scan(str,
510                 L_EXCLUDE & ~L_AUTH_DELIMS,
511                 H_EXCLUDE & ~H_AUTH_DELIMS);
512         if (index >= 0) {
513             return "Illegal character found in authority: "
514                     + describeChar(str.charAt(index));
515         }
516         return null;
517     }
518 
519     // check authority of hierarchical URL. Appropriate for
520     // HTTP-like protocol handlers
521     public static String checkAuthority(URL url) {
522         String s, u, h;
523         if (url == null) return null;
524         if ((s = checkUserInfo(u = url.getUserInfo())) != null) {
525             return s;
526         }
527         if ((s = checkHost(h = url.getHost())) != null) {
528             return s;
529         }
530         if (h == null && u == null) {
531             return checkAuth(url.getAuthority());
532         }
533         return null;
534     }
535 
536     // minimal syntax checks - deeper check may be performed
537     // by the appropriate protocol handler
538     public static String checkExternalForm(URL url) {
539         String s;
540         if (url == null) return null;
541         int index = scan(s = url.getUserInfo(),
542                 L_NON_PRINTABLE | L_SLASH,
543                 H_NON_PRINTABLE | H_SLASH);
544         if (index >= 0) {
545             return "Illegal character found in authority: "
546                     + describeChar(s.charAt(index));
547         }
548         if ((s = checkHostString(url.getHost())) != null) {
549             return s;
550         }
551         return null;
552     }
553 
554     public static String checkHostString(String host) {
555         if (host == null) return null;
556         int index = scan(host,
557                 L_NON_PRINTABLE | L_SLASH,
558                 H_NON_PRINTABLE | H_SLASH,
559                 OTHERS);
560         if (index >= 0) {
561             return "Illegal character found in host: "
562                     + describeChar(host.charAt(index));
563         }
564         return null;
565     }
566 }