|
| 1 | +package com.alibaba.dcm.internal; |
| 2 | + |
| 3 | + |
| 4 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 5 | +// |
| 6 | +// this source code file is copied from jdk sun.net.util.IPAddressUtil, |
| 7 | +// remove unused members. |
| 8 | +// |
| 9 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 10 | + |
| 11 | + |
| 12 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 13 | + |
| 14 | +@SuppressWarnings({"RedundantIfStatement", "PointlessBitwiseExpression"}) |
| 15 | +@SuppressFBWarnings({"SF_SWITCH_FALLTHROUGH", "SF_SWITCH_NO_DEFAULT"}) |
| 16 | +class IPAddressUtil { |
| 17 | + private static final int INADDR4SZ = 4; |
| 18 | + private static final int INADDR16SZ = 16; |
| 19 | + private static final int INT16SZ = 2; |
| 20 | + |
| 21 | + /* |
| 22 | + * Converts IPv4 address in its textual presentation form |
| 23 | + * into its numeric binary form. |
| 24 | + * |
| 25 | + * @param src a String representing an IPv4 address in standard format |
| 26 | + * @return a byte array representing the IPv4 numeric address |
| 27 | + */ |
| 28 | + @SuppressWarnings("fallthrough") |
| 29 | + static byte[] textToNumericFormatV4(String src) |
| 30 | + { |
| 31 | + byte[] res = new byte[INADDR4SZ]; |
| 32 | + |
| 33 | + long tmpValue = 0; |
| 34 | + int currByte = 0; |
| 35 | + boolean newOctet = true; |
| 36 | + |
| 37 | + int len = src.length(); |
| 38 | + if (len == 0 || len > 15) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + /* |
| 42 | + * When only one part is given, the value is stored directly in |
| 43 | + * the network address without any byte rearrangement. |
| 44 | + * |
| 45 | + * When a two part address is supplied, the last part is |
| 46 | + * interpreted as a 24-bit quantity and placed in the right |
| 47 | + * most three bytes of the network address. This makes the |
| 48 | + * two part address format convenient for specifying Class A |
| 49 | + * network addresses as net.host. |
| 50 | + * |
| 51 | + * When a three part address is specified, the last part is |
| 52 | + * interpreted as a 16-bit quantity and placed in the right |
| 53 | + * most two bytes of the network address. This makes the |
| 54 | + * three part address format convenient for specifying |
| 55 | + * Class B net- work addresses as 128.net.host. |
| 56 | + * |
| 57 | + * When four parts are specified, each is interpreted as a |
| 58 | + * byte of data and assigned, from left to right, to the |
| 59 | + * four bytes of an IPv4 address. |
| 60 | + * |
| 61 | + * We determine and parse the leading parts, if any, as single |
| 62 | + * byte values in one pass directly into the resulting byte[], |
| 63 | + * then the remainder is treated as a 8-to-32-bit entity and |
| 64 | + * translated into the remaining bytes in the array. |
| 65 | + */ |
| 66 | + for (int i = 0; i < len; i++) { |
| 67 | + char c = src.charAt(i); |
| 68 | + if (c == '.') { |
| 69 | + if (newOctet || tmpValue < 0 || tmpValue > 0xff || currByte == 3) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + res[currByte++] = (byte) (tmpValue & 0xff); |
| 73 | + tmpValue = 0; |
| 74 | + newOctet = true; |
| 75 | + } else { |
| 76 | + int digit = Character.digit(c, 10); |
| 77 | + if (digit < 0) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + tmpValue *= 10; |
| 81 | + tmpValue += digit; |
| 82 | + newOctet = false; |
| 83 | + } |
| 84 | + } |
| 85 | + if (newOctet || tmpValue < 0 || tmpValue >= (1L << ((4 - currByte) * 8))) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + switch (currByte) { |
| 89 | + case 0: |
| 90 | + res[0] = (byte) ((tmpValue >> 24) & 0xff); |
| 91 | + case 1: |
| 92 | + res[1] = (byte) ((tmpValue >> 16) & 0xff); |
| 93 | + case 2: |
| 94 | + res[2] = (byte) ((tmpValue >> 8) & 0xff); |
| 95 | + case 3: |
| 96 | + res[3] = (byte) ((tmpValue >> 0) & 0xff); |
| 97 | + } |
| 98 | + return res; |
| 99 | + } |
| 100 | + |
| 101 | + /* |
| 102 | + * Convert IPv6 presentation level address to network order binary form. |
| 103 | + * credit: |
| 104 | + * Converted from C code from Solaris 8 (inet_pton) |
| 105 | + * |
| 106 | + * Any component of the string following a per-cent % is ignored. |
| 107 | + * |
| 108 | + * @param src a String representing an IPv6 address in textual format |
| 109 | + * @return a byte array representing the IPv6 numeric address |
| 110 | + */ |
| 111 | + static byte[] textToNumericFormatV6(String src) |
| 112 | + { |
| 113 | + // Shortest valid string is "::", hence at least 2 chars |
| 114 | + if (src.length() < 2) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + int colonp; |
| 119 | + char ch; |
| 120 | + boolean saw_xdigit; |
| 121 | + int val; |
| 122 | + char[] srcb = src.toCharArray(); |
| 123 | + byte[] dst = new byte[INADDR16SZ]; |
| 124 | + |
| 125 | + int srcb_length = srcb.length; |
| 126 | + int pc = src.indexOf ('%'); |
| 127 | + if (pc == srcb_length -1) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + if (pc != -1) { |
| 132 | + srcb_length = pc; |
| 133 | + } |
| 134 | + |
| 135 | + colonp = -1; |
| 136 | + int i = 0, j = 0; |
| 137 | + /* Leading :: requires some special handling. */ |
| 138 | + if (srcb[i] == ':') |
| 139 | + if (srcb[++i] != ':') |
| 140 | + return null; |
| 141 | + int curtok = i; |
| 142 | + saw_xdigit = false; |
| 143 | + val = 0; |
| 144 | + while (i < srcb_length) { |
| 145 | + ch = srcb[i++]; |
| 146 | + int chval = Character.digit(ch, 16); |
| 147 | + if (chval != -1) { |
| 148 | + val <<= 4; |
| 149 | + val |= chval; |
| 150 | + if (val > 0xffff) |
| 151 | + return null; |
| 152 | + saw_xdigit = true; |
| 153 | + continue; |
| 154 | + } |
| 155 | + if (ch == ':') { |
| 156 | + curtok = i; |
| 157 | + if (!saw_xdigit) { |
| 158 | + if (colonp != -1) |
| 159 | + return null; |
| 160 | + colonp = j; |
| 161 | + continue; |
| 162 | + } else if (i == srcb_length) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + if (j + INT16SZ > INADDR16SZ) |
| 166 | + return null; |
| 167 | + dst[j++] = (byte) ((val >> 8) & 0xff); |
| 168 | + dst[j++] = (byte) (val & 0xff); |
| 169 | + saw_xdigit = false; |
| 170 | + val = 0; |
| 171 | + continue; |
| 172 | + } |
| 173 | + if (ch == '.' && ((j + INADDR4SZ) <= INADDR16SZ)) { |
| 174 | + String ia4 = src.substring(curtok, srcb_length); |
| 175 | + /* check this IPv4 address has 3 dots, ie. A.B.C.D */ |
| 176 | + int dot_count = 0, index=0; |
| 177 | + while ((index = ia4.indexOf ('.', index)) != -1) { |
| 178 | + dot_count ++; |
| 179 | + index ++; |
| 180 | + } |
| 181 | + if (dot_count != 3) { |
| 182 | + return null; |
| 183 | + } |
| 184 | + byte[] v4addr = textToNumericFormatV4(ia4); |
| 185 | + if (v4addr == null) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + for (int k = 0; k < INADDR4SZ; k++) { |
| 189 | + dst[j++] = v4addr[k]; |
| 190 | + } |
| 191 | + saw_xdigit = false; |
| 192 | + break; /* '\0' was seen by inet_pton4(). */ |
| 193 | + } |
| 194 | + return null; |
| 195 | + } |
| 196 | + if (saw_xdigit) { |
| 197 | + if (j + INT16SZ > INADDR16SZ) |
| 198 | + return null; |
| 199 | + dst[j++] = (byte) ((val >> 8) & 0xff); |
| 200 | + dst[j++] = (byte) (val & 0xff); |
| 201 | + } |
| 202 | + |
| 203 | + if (colonp != -1) { |
| 204 | + int n = j - colonp; |
| 205 | + |
| 206 | + if (j == INADDR16SZ) |
| 207 | + return null; |
| 208 | + for (i = 1; i <= n; i++) { |
| 209 | + dst[INADDR16SZ - i] = dst[colonp + n - i]; |
| 210 | + dst[colonp + n - i] = 0; |
| 211 | + } |
| 212 | + j = INADDR16SZ; |
| 213 | + } |
| 214 | + if (j != INADDR16SZ) |
| 215 | + return null; |
| 216 | + byte[] newdst = convertFromIPv4MappedAddress(dst); |
| 217 | + if (newdst != null) { |
| 218 | + return newdst; |
| 219 | + } else { |
| 220 | + return dst; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + /* |
| 225 | + * Convert IPv4-Mapped address to IPv4 address. Both input and |
| 226 | + * returned value are in network order binary form. |
| 227 | + * |
| 228 | + * @param src a String representing an IPv4-Mapped address in textual format |
| 229 | + * @return a byte array representing the IPv4 numeric address |
| 230 | + */ |
| 231 | + private static byte[] convertFromIPv4MappedAddress(byte[] addr) { |
| 232 | + if (isIPv4MappedAddress(addr)) { |
| 233 | + byte[] newAddr = new byte[INADDR4SZ]; |
| 234 | + System.arraycopy(addr, 12, newAddr, 0, INADDR4SZ); |
| 235 | + return newAddr; |
| 236 | + } |
| 237 | + return null; |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Utility routine to check if the InetAddress is an |
| 242 | + * IPv4 mapped IPv6 address. |
| 243 | + * |
| 244 | + * @return a <code>boolean</code> indicating if the InetAddress is |
| 245 | + * an IPv4 mapped IPv6 address; or false if address is IPv4 address. |
| 246 | + */ |
| 247 | + private static boolean isIPv4MappedAddress(byte[] addr) { |
| 248 | + if (addr.length < INADDR16SZ) { |
| 249 | + return false; |
| 250 | + } |
| 251 | + if ((addr[0] == 0x00) && (addr[1] == 0x00) && |
| 252 | + (addr[2] == 0x00) && (addr[3] == 0x00) && |
| 253 | + (addr[4] == 0x00) && (addr[5] == 0x00) && |
| 254 | + (addr[6] == 0x00) && (addr[7] == 0x00) && |
| 255 | + (addr[8] == 0x00) && (addr[9] == 0x00) && |
| 256 | + (addr[10] == (byte)0xff) && |
| 257 | + (addr[11] == (byte)0xff)) { |
| 258 | + return true; |
| 259 | + } |
| 260 | + return false; |
| 261 | + } |
| 262 | +} |
0 commit comments