1 module hunt.net.secure.conscrypt.AddressUtils;
2 
3 import hunt.text.Common;
4 import hunt.text.Pattern;
5 
6 /**
7  * Utilities to check whether IP addresses meet some criteria.
8  */
9 final class AddressUtils {
10     /*
11      * Regex that matches valid IPv4 and IPv6 addresses.
12      */
13     private enum string IP_PATTERN = "^(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|"
14             ~ "[01]?[0-9]?[0-9]))|(?i:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|" 
15             ~ "(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|"
16             ~ "[1-9]?[0-9])){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|" 
17             ~ "2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|"
18             ~ "(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|"
19             ~ "2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|"
20             ~ "(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|"
21             ~ "2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|"
22             ~ "(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|"
23             ~ "2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|"
24             ~ ":))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|"
25             ~ "2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|"
26             ~ "(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|"
27             ~ "[1-9]?[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:)))$";
28 
29     private static Pattern ipPattern;
30 
31     private this() {}
32 
33     /**
34      * Returns true when the supplied hostname is valid for SNI purposes.
35      */
36     static bool isValidSniHostname(string sniHostname) {
37         if (sniHostname is null) {
38             return false;
39         }
40 
41 // TODO: Tasks pending completion -@zxp at 8/3/2018, 11:41:38 AM
42 // 
43         return true;
44 
45         // Must be a FQDN that does not have a trailing dot.
46         // return (sniHostname.equalsIgnoreCase("localhost")
47         //             || sniHostname.indexOf('.') != -1)
48         //         // && !Platform.isLiteralIpAddress(sniHostname)
49         //         && !sniHostname.endsWith(".")
50         //         && sniHostname.indexOf('\0') == -1;
51     }
52 
53     /**
54      * Returns true if the supplied hostname is an literal IP address.
55      */
56     // static bool isLiteralIpAddress(string hostname) {
57     //     /* This is here for backwards compatibility for pre-Honeycomb devices. */
58     //     Pattern ipPattern = AddressUtils.ipPattern;
59     //     if (ipPattern is null) {
60     //         AddressUtils.ipPattern = ipPattern = Pattern.compile(IP_PATTERN);
61     //     }
62     //     return ipPattern.matcher(hostname).matches();
63     // }
64 }
65