/** * @param ip String * @return boolean */ public static boolean isValidIP(String ip) { if (ip == null) { return false; } if (ip.isEmpty()) { return false; } return InetAddressUtils.isIPv4Address(ip) || InetAddressUtils.isIPv6Address(ip); }
/** * Name: getIPAddress Description: Iterates through the device's IPAddresses and returns first * non-local IPAddress * * @return String containing the first non-local IPAddress of the device. */ public InetAddress getIPAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface networkInterface = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress; } } } return InetAddress.getByName("127.0.0.1"); } catch (SocketException ex) { ex.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } return null; }
/** Validate the flow, only the source / destination are both IP of vms are valid flow for now */ public static boolean validateFlow(String endpointIp1, String endpointIp2) { if (InetAddressUtils.isIPv4Address(endpointIp1) && InetAddressUtils.isIPv4Address(endpointIp2)) { // Both Endpoint are IP address String ipUuid1 = DiscoveryUtil.getIpUuid(endpointIp1); VMTRootObject vmt1 = rg.getObject(ipUuid1); String ipUuid2 = DiscoveryUtil.getIpUuid(endpointIp2); VMTRootObject vmt2 = rg.getObject(ipUuid2); if (vmt1 != null && vmt2 != null) { // Both IP objects have been created(meaning the ServiceEntity is vm) return true; } } return false; }
/** * @param ip InetAddress * @return boolean */ public static boolean isValidIPV4(InetAddress ip) { if (ip == null) { return false; } return InetAddressUtils.isIPv4Address(ip.getHostAddress()); }
private InetAddress getWiFiIP() { Enumeration<NetworkInterface> interfaces; try { interfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { interfaces = Collections.emptyEnumeration(); } while (interfaces.hasMoreElements()) { NetworkInterface inter = interfaces.nextElement(); if (inter.getDisplayName().equals("wlan0")) { List<InetAddress> addresses = Collections.list(inter.getInetAddresses()); for (InetAddress address : addresses) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(address.getHostAddress())) { return address; } } } } InetAddress adr = null; try { adr = InetAddress.getByName("0.0.0.0"); } catch (UnknownHostException e) { adr = null; e.printStackTrace(); } return adr; }
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 ""; }
/** * Get IP address from first non-localhost interface * * @param ipv4 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 ""; }
@Scheduled(fixedDelay = Integer.MAX_VALUE) public void startListening() { if (disable) { return; } log.info("Starting UPNP Discovery Listener"); try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort); MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT); ) { InetSocketAddress socketAddress = new InetSocketAddress(UPNP_MULTICAST_ADDRESS, UPNP_DISCOVERY_PORT); Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); while (ifs.hasMoreElements()) { NetworkInterface xface = ifs.nextElement(); Enumeration<InetAddress> addrs = xface.getInetAddresses(); String name = xface.getName(); int IPsPerNic = 0; while (addrs.hasMoreElements()) { InetAddress addr = addrs.nextElement(); log.debug(name + " ... has addr " + addr); if (InetAddressUtils.isIPv4Address(addr.getHostAddress())) { IPsPerNic++; } } log.debug("Checking " + name + " to our interface set"); if (IPsPerNic > 0) { upnpMulticastSocket.joinGroup(socketAddress, xface); log.debug("Adding " + name + " to our interface set"); } } while (true) { // trigger shutdown here byte[] buf = new byte[1024]; DatagramPacket packet = new DatagramPacket(buf, buf.length); upnpMulticastSocket.receive(packet); String packetString = new String(packet.getData()); if (isSSDPDiscovery(packetString)) { log.debug( "Got SSDP Discovery packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort()); sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort()); } } } catch (IOException e) { log.error("UpnpListener encountered an error. Shutting down", e); ConfigurableApplicationContext context = (ConfigurableApplicationContext) UpnpListener.this.applicationContext; context.close(); } log.info("UPNP Discovery Listener Stopped"); }
/** * 获取本地IP地址 * * @return */ public static String getLocalIpAddress() { try { String ipv4; List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface ni : nilist) { List<InetAddress> ialist = Collections.list(ni.getInetAddresses()); for (InetAddress address : ialist) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) { return ipv4; } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
/* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text_dialog); TextView title = (TextView) findViewById(R.id.dialogPromptTitle); title.setText(R.string.enterIpAddress); InetAddress currentIP = Util.getLocalIpAddress(); // Send back to main menu if not connected if (currentIP == null) { setResult(RESULT_CANCELED); finish(); } // If not IPv4, set as IPv6 if ((currentIP != null) && !InetAddressUtils.isIPv4Address(currentIP.getHostAddress())) { isIPv4 = false; } final EditText textView = (EditText) findViewById(R.id.dialogPromptTextbox); if (isIPv4) { textView.setHint(R.string.ipv4Address); } else { textView.setHint(R.string.ipv6Address); textView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); } textView.setOnEditorActionListener(this); // create button for the view Button ok = (Button) findViewById(R.id.ok); ok.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { addressEntered(); } }); }
// get phone ip public static String getIp() { 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 e) { e.printStackTrace(); } return null; }
/** * 获取本机的ip * * @return */ 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 ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return ipaddress; }
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() && !inetAddress.isLinkLocalAddress() && // inetAddress.isSiteLocalAddress() ) { if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { String ipAddr = inetAddress.getHostAddress(); return ipAddr; } } } } catch (SocketException ex) { Log.d(TAG, ex.toString()); } return null; }
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()) { String ip = inetAddress.getHostAddress(); if (InetAddressUtils.isIPv4Address(ip)) { Log.w(LOGTAG, "local IP: " + ip); return ip; } } } } } catch (SocketException ex) { Log.e(LOGTAG, ex.toString()); } return "127.0.0.1"; }
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 // Utils.log("WifiPreference IpAddress---error-" + e.toString()); e.printStackTrace(); } return null; }
public void searchOrGoToUrl(String text, SESSIONTYPE sessionType) { DDGControlVar.mCleanSearchBar = false; savedState = false; DDGControlVar.mDuckDuckGoContainer.sessionType = sessionType; if (DDGControlVar.mDuckDuckGoContainer.sessionType == SESSIONTYPE.SESSION_FEED) { showFeed(DDGControlVar.currentFeedObject); return; } if (text != null && text.length() > 0) { java.net.URL searchAsUrl = null; String modifiedText = null; try { searchAsUrl = new URL(text); searchAsUrl.toURI(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); searchAsUrl = null; } if (searchAsUrl == null && !InetAddressUtils.isIPv4Address(text) && !InetAddressUtils.isIPv6Address(text)) { modifiedText = "http://" + text; try { searchAsUrl = new URL(modifiedText); searchAsUrl.toURI(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); searchAsUrl = null; } } // We use the . check to determine if this is a single word or not... // if it doesn't contain a . plus domain (2 more characters) it won't be a URL, even if it's // valid, like http://test if (searchAsUrl != null) { if (modifiedText != null) { // Show the modified url text if (modifiedText.contains(".") && modifiedText.length() > (modifiedText.indexOf(".") + 2)) { showWebUrl(modifiedText); } else { searchWebTerm(text); } } else { if (text.contains(".") && text.length() > (text.indexOf(".") + 2)) { // Show the url text showWebUrl(text); } else { searchWebTerm(text); } } } else { searchWebTerm(text); } } }
/** * @param ip InetAddress * @return boolean */ public static boolean isValidIP(InetAddress ip) { return ip != null && (InetAddressUtils.isIPv4Address(ip.getHostAddress()) || InetAddressUtils.isIPv6Address(ip.getHostAddress())); }
private static boolean isIPAddress(final String hostname) { return hostname != null && (InetAddressUtils.isIPv4Address(hostname) || InetAddressUtils.isIPv6Address(hostname)); }