@Nullable public static String ipOrNull(@Nullable String ip) { if (ip == null || ip.isEmpty()) return null; if ("::1".equals(ip) || "127.0.0.1".equals(ip)) return ip; // special-case localhost IpFamily format = detectFamily(ip); if (format == IpFamily.IPv4Embedded) { ip = ip.substring(ip.lastIndexOf(':') + 1); } else if (format == IpFamily.Unknown) { ip = null; } return ip; }
/** * The text representation of the primary IP address associated with this service. Ex. * 192.168.99.100 or 2001:db8::c001. Defaults to a link local IP. * * @see #localServiceName(String) * @see #localPort(int) * @since 5.2 */ public Builder localIp(String localIp) { String maybeIp = IpLiteral.ipOrNull(localIp); if (maybeIp == null) throw new IllegalArgumentException(localIp + " is not a valid IP"); this.localIp = maybeIp; return this; }
if (hasDot) return IpFamily.Unknown; // Colons must not appear after dots. hasColon = true; } else if (notHex(c)) { return IpFamily.Unknown; // Everything else must be a decimal or hex digit. if (hasDot) { int lastColonIndex = ipString.lastIndexOf(':'); if (!isValidIpV4Address(ipString, lastColonIndex + 1, ipString.length())) { return IpFamily.Unknown; } else if (hasDot && isValidIpV4Address(ipString, 0, ipString.length())) { return IpFamily.IPv4;
private static boolean isValidIpV4Address(String ip, int from, int toExcluded) { int len = toExcluded - from; int i; return len <= 15 && len >= 7 && (i = ip.indexOf('.', from + 1)) > 0 && isValidIpV4Word(ip, from, i) && (i = ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) && (i = ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) && isValidIpV4Word(ip, i + 1, toExcluded); }
private static boolean isValidIpV4Word(CharSequence word, int from, int toExclusive) { int len = toExclusive - from; char c0, c1, c2; if (len < 1 || len > 3) return false; c0 = word.charAt(from); if (len == 3) { return (c1 = word.charAt(from + 1)) >= '0' && (c2 = word.charAt(from + 2)) >= '0' && ((c0 <= '1' && c1 <= '9' && c2 <= '9') || (c0 == '2' && c1 <= '5' && (c2 <= '5' || (c1 < '5' && c2 <= '9')))); } return c0 <= '9' && (len == 1 || isValidNumericChar(word.charAt(from + 1))); }
private static boolean isValidIpV4Address(String ip, int from, int toExcluded) { int len = toExcluded - from; int i; return len <= 15 && len >= 7 && (i = ip.indexOf('.', from + 1)) > 0 && isValidIpV4Word(ip, from, i) && (i = ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) && (i = ip.indexOf('.', from = i + 2)) > 0 && isValidIpV4Word(ip, from - 1, i) && isValidIpV4Word(ip, i + 1, toExcluded); }
private static boolean isValidIpV4Word(CharSequence word, int from, int toExclusive) { int len = toExclusive - from; char c0, c1, c2; if (len < 1 || len > 3) return false; c0 = word.charAt(from); if (len == 3) { return (c1 = word.charAt(from + 1)) >= '0' && (c2 = word.charAt(from + 2)) >= '0' && ((c0 <= '1' && c1 <= '9' && c2 <= '9') || (c0 == '2' && c1 <= '5' && (c2 <= '5' || (c1 < '5' && c2 <= '9')))); } return c0 <= '9' && (len == 1 || isValidNumericChar(word.charAt(from + 1))); }
if (hasDot) return IpFamily.Unknown; // Colons must not appear after dots. hasColon = true; } else if (notHex(c)) { return IpFamily.Unknown; // Everything else must be a decimal or hex digit. if (hasDot) { int lastColonIndex = ipString.lastIndexOf(':'); if (!isValidIpV4Address(ipString, lastColonIndex + 1, ipString.length())) { return IpFamily.Unknown; } else if (hasDot && isValidIpV4Address(ipString, 0, ipString.length())) { return IpFamily.IPv4;
/** @see #localIp() */ public boolean localIp(@Nullable String localIp) { this.localIp = IpLiteral.ipOrNull(localIp); return true; }
@Nullable public static String ipOrNull(@Nullable String ip) { if (ip == null || ip.isEmpty()) return null; if ("::1".equals(ip) || "127.0.0.1".equals(ip)) return ip; // special-case localhost IpFamily format = detectFamily(ip); if (format == IpFamily.IPv4Embedded) { ip = ip.substring(ip.lastIndexOf(':') + 1); } else if (format == IpFamily.Unknown) { ip = null; } return ip; }
/** @see brave.Span#remoteIpAndPort(String, int) */ public boolean remoteIpAndPort(@Nullable String remoteIp, int remotePort) { if (remoteIp == null) return false; this.remoteIp = IpLiteral.ipOrNull(remoteIp); if (this.remoteIp == null) return false; if (remotePort > 0xffff) throw new IllegalArgumentException("invalid port " + remotePort); if (remotePort < 0) remotePort = 0; this.remotePort = remotePort; return true; }
/** * The text representation of the primary IP address associated with this service. Ex. * 192.168.99.100 or 2001:db8::c001. Defaults to a link local IP. * * @see #localServiceName(String) * @see #localPort(int) * @since 5.2 */ public Builder localIp(String localIp) { String maybeIp = IpLiteral.ipOrNull(localIp); if (maybeIp == null) throw new IllegalArgumentException(localIp + " is not a valid IP"); this.localIp = maybeIp; return this; }
/** @see #localIp() */ public boolean localIp(@Nullable String localIp) { this.localIp = IpLiteral.ipOrNull(localIp); return true; }
/** @see brave.Span#remoteIpAndPort(String, int) */ public boolean remoteIpAndPort(@Nullable String remoteIp, int remotePort) { if (remoteIp == null) return false; this.remoteIp = IpLiteral.ipOrNull(remoteIp); if (this.remoteIp == null) return false; if (remotePort > 0xffff) throw new IllegalArgumentException("invalid port " + remotePort); if (remotePort < 0) remotePort = 0; this.remotePort = remotePort; return true; }