1 /* 2 * Copyright 2018 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.services; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 import static com.google.common.base.Preconditions.checkNotNull; 21 22 import com.google.common.primitives.Ints; 23 import java.net.Inet4Address; 24 import java.net.Inet6Address; 25 import java.net.InetAddress; 26 import java.util.Arrays; 27 28 // This is copied from guava 20.0 because it is a @Beta api 29 final class InetAddressUtil { 30 private static final int IPV6_PART_COUNT = 8; 31 toAddrString(InetAddress ip)32 public static String toAddrString(InetAddress ip) { 33 checkNotNull(ip); 34 if (ip instanceof Inet4Address) { 35 // For IPv4, Java's formatting is good enough. 36 return ip.getHostAddress(); 37 } 38 checkArgument(ip instanceof Inet6Address); 39 byte[] bytes = ip.getAddress(); 40 int[] hextets = new int[IPV6_PART_COUNT]; 41 for (int i = 0; i < hextets.length; i++) { 42 hextets[i] = Ints.fromBytes((byte) 0, (byte) 0, bytes[2 * i], bytes[2 * i + 1]); 43 } 44 compressLongestRunOfZeroes(hextets); 45 return hextetsToIPv6String(hextets); 46 } 47 compressLongestRunOfZeroes(int[] hextets)48 private static void compressLongestRunOfZeroes(int[] hextets) { 49 int bestRunStart = -1; 50 int bestRunLength = -1; 51 int runStart = -1; 52 for (int i = 0; i < hextets.length + 1; i++) { 53 if (i < hextets.length && hextets[i] == 0) { 54 if (runStart < 0) { 55 runStart = i; 56 } 57 } else if (runStart >= 0) { 58 int runLength = i - runStart; 59 if (runLength > bestRunLength) { 60 bestRunStart = runStart; 61 bestRunLength = runLength; 62 } 63 runStart = -1; 64 } 65 } 66 if (bestRunLength >= 2) { 67 Arrays.fill(hextets, bestRunStart, bestRunStart + bestRunLength, -1); 68 } 69 } 70 hextetsToIPv6String(int[] hextets)71 private static String hextetsToIPv6String(int[] hextets) { 72 // While scanning the array, handle these state transitions: 73 // start->num => "num" start->gap => "::" 74 // num->num => ":num" num->gap => "::" 75 // gap->num => "num" gap->gap => "" 76 StringBuilder buf = new StringBuilder(39); 77 boolean lastWasNumber = false; 78 for (int i = 0; i < hextets.length; i++) { 79 boolean thisIsNumber = hextets[i] >= 0; 80 if (thisIsNumber) { 81 if (lastWasNumber) { 82 buf.append(':'); 83 } 84 buf.append(Integer.toHexString(hextets[i])); 85 } else { 86 if (i == 0 || lastWasNumber) { 87 buf.append("::"); 88 } 89 } 90 lastWasNumber = thisIsNumber; 91 } 92 return buf.toString(); 93 } 94 }