private void setValidationListener(String prefName) { EditTextPreference edit_Pref = (EditTextPreference) getPreferenceScreen() .findPreference(prefName); edit_Pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { // put validation here.. if (((String) newValue).isEmpty() || InetAddressUtils.isIPv4Address((String) newValue)) { return true; } else { Commons.showMessage("ERROR:\nWrong IP format!", getActivity()); return false; } } }); }
public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress.getHostAddress(); } } } } catch (Exception ex) { Log.e("IP Address", ex.toString()); } return null; }
/** * Is the requested destination a dir router? * * @param sp {@link TCPStreamProperties} containing the destination infos * @return true if it is a directory server */ public boolean isDirServer(final TCPStreamProperties sp) { if (sp.getHostname() != null && InetAddressUtils.isIPv4Address(sp.getHostname())) { String[] octets = sp.getHostname().split("\\."); byte[] ip = new byte[4]; ip[0] = (byte) Integer.parseInt(octets[0]); ip[1] = (byte) Integer.parseInt(octets[1]); ip[2] = (byte) Integer.parseInt(octets[2]); ip[3] = (byte) Integer.parseInt(octets[3]); final Router router = getValidRouterByIpAddressAndDirPort(new TcpipNetAddress(ip, sp.getPort())); if (router != null && (router.isDirv2HSDir() || router.isDirv2V2dir())) { return true; } } return false; }
public InetAddress getAddr() { if (addr == null && hostname != null && !hostname.isEmpty() && InetAddressUtils.isIPv4Address(hostname)) { try { String [] octets = hostname.split("\\."); byte [] ip = new byte [4]; ip[0] = (byte) Integer.parseInt(octets[0]); ip[1] = (byte) Integer.parseInt(octets[1]); ip[2] = (byte) Integer.parseInt(octets[2]); ip[3] = (byte) Integer.parseInt(octets[3]); return InetAddress.getByAddress(ip); } catch (UnknownHostException e) { return addr; } } return addr; }
/** * 获取设备IP地址 * * @return 设备ip地址 */ public static String getLocalIpAddress(){ try{ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress.getHostAddress().toString(); } } } }catch (SocketException e) { // TODO: handle exception Log.v("Steel", "WifiPreference IpAddress---error-" + e.toString()); } return null; }
/** * ��ȡ����ip */ private String getLocalIpAddress() { try { List<NetworkInterface> interfaces = Collections .list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf .getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (isIPv4) { return sAddr; } } } } } catch (Exception ex) { ex.printStackTrace(); } return null; }
public String getIPAddress() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> inet = nif.getInetAddresses(); while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip .getHostAddress())) { return ip.getHostAddress(); } } } } catch (SocketException e) { Log.e("IP & PORT", "获取本地ip地址失败"); e.printStackTrace(); } return ipaddress; }
public String getIPAddress() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> inet = nif.getInetAddresses(); while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip .getHostAddress())) { return ipaddress = ip.getHostAddress(); } } } } catch (SocketException e) { Log.e("IP & PORT", "获取本地ip地址失败"); e.printStackTrace(); } return ipaddress; }
static boolean domainMatch(final String domain, final String host) { if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host)) { return false; } final String normalizedDomain = domain.startsWith(".") ? domain.substring(1) : domain; if (host.endsWith(normalizedDomain)) { final int prefix = host.length() - normalizedDomain.length(); // Either a full match or a prefix endidng with a '.' if (prefix == 0) { return true; } if (prefix > 1 && host.charAt(prefix - 1) == '.') { return true; } } return false; }
public static String getGPRSLocalIPAddress() { try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> enumIpAddr = nif.getInetAddresses(); while (enumIpAddr.hasMoreElements()) { InetAddress mInetAddress = enumIpAddr.nextElement(); if (!mInetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(mInetAddress.getHostAddress())) { return mInetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
public InetAddress getMulticastIp() { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (isIPv4) { sAddr = sAddr.substring(0, sAddr.lastIndexOf('.') + 1) + "255"; Log.d(Globals.TAG, "Multicast IP: " + sAddr); return InetAddress.getByName(sAddr); } } } } } catch (Exception e) { Log.d(Globals.TAG, "There was an error retrieving your IP. Aborted."); } return null; }
public boolean checkField() { if(!currencyBySpinner) { //check address String address = mAdress.getText().toString().trim(); if (!address.isEmpty()) { if (!InetAddressUtils.isIPv4Address(address) && !InetAddressUtils.isIPv6Address(address) && !Patterns.WEB_URL.matcher(address).matches()) { mAdress.setError(mContext.getString(R.string.invalid_peer_address)); return false; } } //check port if (mPort.getText().toString().trim().isEmpty()) { mPort.setError(mContext.getString(R.string.port_cannot_be_empty)); return false; } else if (Integer.parseInt(mPort.getText().toString()) <= 0 || 65535 <= Integer.parseInt(mPort.getText().toString())) { mPort.setError(mContext.getString(R.string.invalid_peer_port)); return false; } } return true; }
/** * Get IP address from first non-localhost interface * * @param useIPv4 * true=return ipv4, false=return ipv6 * @return address or empty string * @throws SocketException */ public static String getIPAddress(boolean useIPv4) throws SocketException { List<NetworkInterface> interfaces = Collections.list(NetworkInterface .getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { // drop ip6 port suffix int delim = sAddr.indexOf('%'); return delim < 0 ? sAddr : sAddr .substring(0, delim); } } } } } return ""; }
/** * Get IP address from first non-localhost interface * * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string */ public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ignored) { } // for now eat exceptions return ""; }
public static String getLocalHostIp() { String ipaddress = "127.0.0.1"; try { Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); // 遍历所用的网络接口 while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip Enumeration<InetAddress> inet = nif.getInetAddresses(); // 遍历每一个接口绑定的所有ip while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip .getHostAddress())) { return ipaddress = ip.getHostAddress(); } } } } catch (SocketException e) { return ipaddress; } return ipaddress; }
/** * Get IP address from first non-localhost interface * * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string */ public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
/** * 获取ipV4地址 * * @return 地址 */ public static String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress .getHostAddress())) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { } return null; }
/** * 得到本机ip地址 * @return */ public String getLocalHostIp(){ String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); // 遍历所用的网络接口 while (en.hasMoreElements()){ NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip Enumeration<InetAddress> inet = nif.getInetAddresses(); // 遍历每一个接口绑定的所有ip while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip.getHostAddress())){ ipaddress = ip.getHostAddress(); } } } }catch (SocketException e){ Log.e(TAG, "获取本地ip地址失败"); e.printStackTrace(); } Log.i(TAG, "本机IP:"+ ipaddress); return ipaddress; }
/** * Is the requested destination a dir router? * @param sp {@link TCPStreamProperties} containing the destination infos * @return true if it is a directory server */ public boolean isDirServer(final TCPStreamProperties sp) { if (sp.getHostname() != null && InetAddressUtils.isIPv4Address(sp.getHostname())) { String [] octets = sp.getHostname().split("\\."); byte [] ip = new byte [4]; ip[0] = (byte) Integer.parseInt(octets[0]); ip[1] = (byte) Integer.parseInt(octets[1]); ip[2] = (byte) Integer.parseInt(octets[2]); ip[3] = (byte) Integer.parseInt(octets[3]); final Router router = getValidRouterByIpAddressAndDirPort(new TcpipNetAddress(ip, sp.getPort())); if (router != null && (router.isDirv2HSDir() || router.isDirv2V2dir())) { return true; } } return false; }
public InetAddress getInetAddress() { try { Enumeration<NetworkInterface> networks; Enumeration<InetAddress> inets; NetworkInterface network; InetAddress inetAddress; for (networks = NetworkInterface.getNetworkInterfaces(); networks.hasMoreElements();) { network = networks.nextElement(); for (inets = network.getInetAddresses(); inets.hasMoreElements();) { inetAddress = inets.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress; } } } } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
public void verify( final String host, final X509Certificate cert) throws SSLException { final boolean ipv4 = InetAddressUtils.isIPv4Address(host); final boolean ipv6 = InetAddressUtils.isIPv6Address(host); final int subjectType = ipv4 || ipv6 ? IP_ADDRESS_TYPE : DNS_NAME_TYPE; final List<String> subjectAlts = extractSubjectAlts(cert, subjectType); if (subjectAlts != null && !subjectAlts.isEmpty()) { if (ipv4) { matchIPAddress(host, subjectAlts); } else if (ipv6) { matchIPv6Address(host, subjectAlts); } else { matchDNSName(host, subjectAlts, this.publicSuffixMatcher); } } else { // CN matching has been deprecated by rfc2818 and can be used // as fallback only when no subjectAlts are available final X500Principal subjectPrincipal = cert.getSubjectX500Principal(); final String cn = extractCN(subjectPrincipal.getName(X500Principal.RFC2253)); if (cn == null) { throw new SSLException("Certificate subject for <" + host + "> doesn't contain " + "a common name and does not have alternative names"); } matchCN(host, cn, this.publicSuffixMatcher); } }
private static void getLocalIPAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { System.out.println("getHostAddress" + inetAddress.getHostAddress()); } } } } catch (SocketException e) { e.printStackTrace(); } }
public static ArrayList<String> getLocalIpAddresses() { ArrayList<String> addresses = new ArrayList<String>(); try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); String ipv4 = inetAddress.getHostAddress(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4)) { addresses.add(inetAddress.getHostAddress().toString()); } } } return addresses; } catch (SocketException e) { e.printStackTrace(); } return addresses; }
/** * Get IP address from first non-localhost interface * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string */ @SuppressLint("DefaultLocale") public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim<0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
public static String getLocalIpAddressString() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (!intf.isLoopback() && intf.isUp() && intf.getName().equals("wlan0")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); String sAddr = inetAddress.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (isIPv4) { String ip = inetAddress.getHostAddress().toString(); return ip; } } } } } catch (Exception e) { Log.e(TAG, "failed", e); } return null; }
/** * * * @Title: getLocalHostIp * @Description: TODO 获取设备IP地址 * @author 李苜菲 * @return * @return String * @throws @date * 2015-8-29下午4:56:18 */ public static String getLocalHostIp() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); // 遍历所用的网络接口 while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip Enumeration<InetAddress> inet = nif.getInetAddresses(); // 遍历每一个接口绑定的所有ip while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip.getHostAddress())) { return ipaddress = ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return ipaddress; }
/** * Get IP address from first non-localhost interface. * true=return ipv4, false=return ipv6. * Returns address or empty string. */ public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim<0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
private String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()) ) { String ipAddr = inetAddress.getHostAddress(); return ipAddr; } } } } catch (SocketException ex) { //Log.d(TAG, ex.toString()); } return null; }
public static String getIPAddress(boolean useIPv4) throws SocketException { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { if (sAddr.startsWith("fe80") || sAddr.startsWith("FE80")) // skipping link-local addresses continue; int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim<0 ? sAddr : sAddr.substring(0, delim); } } } } } return ""; }
/** {@inheritDoc} */ public boolean run(final Context context, final Result proto, final Object argData) { Result nr = new Result(proto); nr.setCode("content"); nr.setDocument("rfc7483"); nr.setReference("3"); String ipv4Address = Utils.castToString(argData); boolean res = nr.setDetails((ipv4Address != null), "is string", "not string"); context.addResult(nr); if (!res) { return false; } Result nr2 = new Result(proto); res = nr2.setDetails(InetAddressUtils.isIPv4Address(ipv4Address), "valid", "invalid"); context.addResult(nr2); return res; }
/** {@inheritDoc} */ public boolean run(final Context context, final Result proto, final Object argData) { Result nr = new Result(proto); nr.setCode("content"); nr.setDocument("rfc7483"); nr.setReference("3"); String ipv6Address = Utils.castToString(argData); boolean res = nr.setDetails((ipv6Address != null), "is string", "not string"); context.addResult(nr); if (!res) { return false; } Result nr2 = new Result(proto); res = nr2.setDetails(InetAddressUtils.isIPv6Address(ipv6Address), "valid", "invalid"); context.addResult(nr2); return res; }
/** * 获得手机ip地址 * * @return null if faild to get ip */ public static String getPhoneIP() { try { String ipv4; ArrayList<NetworkInterface> mylist = Collections .list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface ni : mylist) { ArrayList<InetAddress> ialist = Collections.list(ni .getInetAddresses()); for (InetAddress address : ialist) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = address .getHostAddress())) { return ipv4; } } } } catch (SocketException ex) { return null; } return null; }
/** * Get IP address from first non-localhost interface * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string */ public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim<0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
public final void verify( final String host, final X509Certificate cert) throws SSLException { final boolean ipv4 = InetAddressUtils.isIPv4Address(host); final boolean ipv6 = InetAddressUtils.isIPv6Address(host); final int subjectType = ipv4 || ipv6 ? IP_ADDRESS_TYPE : DNS_NAME_TYPE; final List<String> subjectAlts = extractSubjectAlts(cert, subjectType); if (subjectAlts != null && !subjectAlts.isEmpty()) { if (ipv4) { matchIPAddress(host, subjectAlts); } else if (ipv6) { matchIPv6Address(host, subjectAlts); } else { matchDNSName(host, subjectAlts, this.publicSuffixMatcher); } } else { // CN matching has been deprecated by rfc2818 and can be used // as fallback only when no subjectAlts are available final X500Principal subjectPrincipal = cert.getSubjectX500Principal(); final String cn = extractCN(subjectPrincipal.getName(X500Principal.RFC2253)); if (cn == null) { throw new SSLException("Certificate subject for <" + host + "> doesn't contain " + "a common name and does not have alternative names"); } matchCN(host, cn, this.publicSuffixMatcher); } }