List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:to.sven.androidrccar.host.communication.impl.SocketConnector.java
/** * Returns the first non-local IPv4 address of the device. * @return IPv4 address as String or unknown, if no address is found. *//*from w w w . j a v a2 s .com*/ private String getLocalIpAddress() { try { for (NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InetAddress address : Collections.list(iface.getInetAddresses())) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(address.getHostAddress())) { return address.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e(LOG_TAG, ex.toString()); } return dc.getContext().getString(android.R.string.unknownName); }
From source file:org.apache.hadoop.net.DNS.java
/** * Returns all the IPs associated with the provided interface, if any, as * a list of InetAddress objects.//from w w w . j a v a2 s . c o m * * @param strInterface * The name of the network interface or sub-interface to query * (eg eth0 or eth0:0) or the string "default" * @param returnSubinterfaces * Whether to return IPs associated with subinterfaces of * the given interface * @return A list of all the IPs associated with the provided * interface. The local host IP is returned if the interface * name "default" is specified or there is an I/O error looking * for the given interface. * @throws UnknownHostException * If the given interface is invalid * */ public static List<InetAddress> getIPsAsInetAddressList(String strInterface, boolean returnSubinterfaces) throws UnknownHostException { if ("default".equals(strInterface)) { return Arrays.asList(InetAddress.getByName(cachedHostAddress)); } NetworkInterface netIf; try { netIf = NetworkInterface.getByName(strInterface); if (netIf == null) { netIf = getSubinterface(strInterface); } } catch (SocketException e) { LOG.warn("I/O error finding interface " + strInterface + ": " + e.getMessage()); return Arrays.asList(InetAddress.getByName(cachedHostAddress)); } if (netIf == null) { throw new UnknownHostException("No such interface " + strInterface); } // NB: Using a LinkedHashSet to preserve the order for callers // that depend on a particular element being 1st in the array. // For example, getDefaultIP always returns the first element. LinkedHashSet<InetAddress> allAddrs = new LinkedHashSet<InetAddress>(); allAddrs.addAll(Collections.list(netIf.getInetAddresses())); if (!returnSubinterfaces) { allAddrs.removeAll(getSubinterfaceInetAddrs(netIf)); } return new Vector<InetAddress>(allAddrs); }
From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java
private String readIP4AddressOfEmulator() throws SocketException { String inet4Address = "0.0.0.0"; for (Enumeration<NetworkInterface> iter = NetworkInterface.getNetworkInterfaces(); iter .hasMoreElements();) {//from w w w . j a v a 2 s .com NetworkInterface nic = iter.nextElement(); if (nic.getName().startsWith("eth")) { Enumeration<InetAddress> addresses = nic.getInetAddresses(); addresses.nextElement(); // skip first if (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); String concreteAddressString = address.getHostAddress().toUpperCase(Locale.getDefault()); if (InetAddressUtils.isIPv4Address(concreteAddressString)) { inet4Address = concreteAddressString; } } } } return inet4Address; }
From source file:com.hg.development.apps.messagenotifier_v1.Utils.Utility.java
/** * * @return adresse IP v4 actuelle du tlphone. * @throws SocketException/*w w w. j av a2 s .c o m*/ */ private String getLocalIpAddress() throws SocketException { String ipv4 = null; try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); if (interfaces.size() > 0) { for (NetworkInterface ni : interfaces) { List<InetAddress> ialist = Collections.list(ni.getInetAddresses()); if (ialist.size() > 0) { for (InetAddress address : ialist) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) ; } } } } } catch (SocketException ex) { throw ex; } finally { return ipv4; } }
From source file:com.adito.setup.forms.SystemInfoForm.java
/** * Get a list (as strings) of the nwrok interfaces that are available * on this host./* w ww .ja v a 2s. c o m*/ * * TODO This should be the interfaces that Adito is using. * * @return network interfaces */ public List getNetworkInterfaces() { Enumeration e = null; try { List niList = new ArrayList(); e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface netface = (NetworkInterface) e.nextElement(); Enumeration e2 = netface.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress ip = (InetAddress) e2.nextElement(); niList.add(netface.getName() + "=" + ip.toString()); } } return niList; } catch (SocketException e1) { return new ArrayList(); } }
From source file:org.apache.hadoop.hdfs.security.TestDelegationTokenForProxyUser.java
private void configureSuperUserIPAddresses(Configuration conf, String superUserShortName) throws IOException { ArrayList<String> ipList = new ArrayList<String>(); Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); while (netInterfaceList.hasMoreElements()) { NetworkInterface inf = netInterfaceList.nextElement(); Enumeration<InetAddress> addrList = inf.getInetAddresses(); while (addrList.hasMoreElements()) { InetAddress addr = addrList.nextElement(); ipList.add(addr.getHostAddress()); }//www. j a va 2 s . c o m } StringBuilder builder = new StringBuilder(); for (String ip : ipList) { builder.append(ip); builder.append(','); } builder.append("127.0.1.1,"); builder.append(InetAddress.getLocalHost().getCanonicalHostName()); LOG.info("Local Ip addresses: " + builder.toString()); conf.setStrings(ProxyUsers.getProxySuperuserIpConfKey(superUserShortName), builder.toString()); }
From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java
public String readIp4ApAddress() { try {//ww w. ja va 2 s. com for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { mLog.debug("found AP address [" + inetAddress.getHostAddress() + "]"); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { mLog.debug("failed to read ip address in access point mode"); } return null; }
From source file:Comman.Tool.java
public InetAddress getCurrentIp() { try {/*from w w w . j a v a 2 s . co m*/ Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement(); Enumeration<InetAddress> nias = ni.getInetAddresses(); while (nias.hasMoreElements()) { InetAddress ia = (InetAddress) nias.nextElement(); if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) { return ia; } } } } catch (SocketException e) { } return null; }
From source file:com.offbynull.portmapper.natpmp.NatPmpReceiver.java
/** * Start listening for NAT-PMP events. This method blocks until {@link #stop() } is called. * @param listener listener to notify of events * @throws IOException if socket error occurs * @throws NullPointerException if any argument is {@code null} *//*from w w w .jav a 2s . com*/ public void start(NatPmpEventListener listener) throws IOException { Validate.notNull(listener); MulticastSocket socket = null; try { final InetAddress group = InetAddress.getByName("224.0.0.1"); // NOPMD final int port = 5350; final InetSocketAddress groupAddress = new InetSocketAddress(group, port); socket = new MulticastSocket(port); if (!currentSocket.compareAndSet(null, socket)) { IOUtils.closeQuietly(socket); return; } socket.setReuseAddress(true); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface InetAddress addr = addrs.nextElement(); try { if (addr instanceof Inet4Address) { socket.joinGroup(groupAddress, networkInterface); } } catch (IOException ioe) { // NOPMD // occurs with certain interfaces // do nothing } } } ByteBuffer buffer = ByteBuffer.allocate(12); DatagramPacket data = new DatagramPacket(buffer.array(), buffer.capacity()); while (true) { buffer.clear(); socket.receive(data); buffer.position(data.getLength()); buffer.flip(); if (!data.getAddress().equals(gatewayAddress)) { // data isn't from our gateway, ignore continue; } if (buffer.remaining() != 12) { // data isn't the expected size, ignore continue; } int version = buffer.get(0); if (version != 0) { // data doesn't have the correct version, ignore continue; } int opcode = buffer.get(1) & 0xFF; if (opcode != 128) { // data doesn't have the correct op, ignore continue; } int resultCode = buffer.getShort(2) & 0xFFFF; switch (resultCode) { case 0: break; default: continue; // data doesn't have a successful result, ignore } listener.publicAddressUpdated(new ExternalAddressNatPmpResponse(buffer)); } } catch (IOException ioe) { if (currentSocket.get() == null) { return; // ioexception caused by interruption/stop, so just return without propogating error up } throw ioe; } finally { IOUtils.closeQuietly(socket); currentSocket.set(null); } }
From source file:com.commonsware.android.webserver.secure.WebServerService.java
private void raiseReadyEvent() { ServerStartedEvent event = new ServerStartedEvent(); try {/*from w ww. j a v a 2 s . c o m*/ for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces .hasMoreElements();) { NetworkInterface ni = enInterfaces.nextElement(); for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements();) { InetAddress addr = enAddresses.nextElement(); if (addr instanceof Inet4Address) { event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/"); } } } } catch (SocketException ex) { ex.printStackTrace(); } EventBus.getDefault().removeAllStickyEvents(); EventBus.getDefault().postSticky(event); }