use of java.net.NetworkInterface in project platformlayer by platformlayer.
the class InetAddressUtils method getLocalAddresses.
public static List<InetAddress> getLocalAddresses() {
List<InetAddress> addresses = Lists.newArrayList();
Enumeration<NetworkInterface> networkInterfaces;
try {
networkInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
throw new IllegalStateException("Error reading network addresses", e);
}
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress : interfaceAddresses) {
InetAddress address = interfaceAddress.getAddress();
addresses.add(address);
}
}
return addresses;
}
use of java.net.NetworkInterface in project chromeview by pwnall.
the class AndroidNetworkLibrary method getNetworkList.
/**
* @return the network interfaces list (if any) string. The items in
* the list string are delimited by a semicolon ";", each item
* is a network interface name and address pair and formatted
* as "name,address". e.g.
* eth0,10.0.0.2;eth0,fe80::5054:ff:fe12:3456
* represents a network list string which containts two items.
*/
@CalledByNative
public static String getNetworkList() {
Enumeration<NetworkInterface> list = null;
try {
list = NetworkInterface.getNetworkInterfaces();
if (list == null)
return "";
} catch (SocketException e) {
Log.w(TAG, "Unable to get network interfaces: " + e);
return "";
}
StringBuilder result = new StringBuilder();
while (list.hasMoreElements()) {
NetworkInterface netIf = list.nextElement();
try {
// Skip loopback interfaces, and ones which are down.
if (!netIf.isUp() || netIf.isLoopback())
continue;
Enumeration<InetAddress> addressList = netIf.getInetAddresses();
while (addressList.hasMoreElements()) {
InetAddress address = addressList.nextElement();
// Skip loopback addresses configured on non-loopback interfaces.
if (address.isLoopbackAddress())
continue;
StringBuilder addressString = new StringBuilder();
addressString.append(netIf.getName());
addressString.append(",");
String ipAddress = address.getHostAddress();
if (address instanceof Inet6Address && ipAddress.contains("%")) {
ipAddress = ipAddress.substring(0, ipAddress.lastIndexOf("%"));
}
addressString.append(ipAddress);
if (result.length() != 0)
result.append(";");
result.append(addressString.toString());
}
} catch (SocketException e) {
continue;
}
}
return result.toString();
}
use of java.net.NetworkInterface in project robovm by robovm.
the class OldDatagramSocketTest method test_sendLjava_net_DatagramPacket.
public void test_sendLjava_net_DatagramPacket() throws Exception {
// Test for method void
// java.net.DatagramSocket.send(java.net.DatagramPacket)
int[] ports = Support_PortManager.getNextPortsForUDP(2);
final int portNumber = ports[0];
class TestDGSend implements Runnable {
Thread pThread;
public TestDGSend(Thread t) {
pThread = t;
}
public void run() {
try {
byte[] rbuf = new byte[1000];
sds = new DatagramSocket(portNumber);
DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length);
sds.setSoTimeout(6000);
sds.receive(sdp);
retval = new String(rbuf, 0, testString.length());
pThread.interrupt();
} catch (java.io.InterruptedIOException e) {
System.out.println("Recv operation timed out");
pThread.interrupt();
ds.close();
} catch (Exception e) {
System.out.println("Failed to establish Dgram server: " + e);
}
}
}
try {
new Thread(new TestDGSend(Thread.currentThread()), "DGServer").start();
ds = new java.net.DatagramSocket(ports[1]);
dp = new DatagramPacket(testString.getBytes(), testString.length(), InetAddress.getLocalHost(), portNumber);
// Wait to allow send to occur
try {
Thread.sleep(500);
ds.send(dp);
Thread.sleep(5000);
} catch (InterruptedException e) {
ds.close();
assertTrue("Incorrect data sent: " + retval, retval.equals(testString));
}
} catch (Exception e) {
fail("Exception during send test : " + e.getMessage());
} finally {
ds.close();
}
/*
SecurityManager sm = new SecurityManager() {
public void checkPermission(Permission perm) {
}
public void checkMulticast(InetAddress maddr) {
throw new SecurityException();
}
public void checkConnect(String host,
int port) {
throw new SecurityException();
}
};
try {
ds = new java.net.DatagramSocket(ports[1]);
dp = new DatagramPacket(testString.getBytes(), testString.length(),
InetAddress.getLocalHost(), portNumber);
SecurityManager oldSm = System.getSecurityManager();
System.setSecurityManager(sm);
try {
ds.send(dp);
fail("SecurityException should be thrown.");
} catch (SecurityException e) {
// expected
} catch (SocketException e) {
fail("SocketException was thrown.");
} finally {
System.setSecurityManager(oldSm);
}
} catch(Exception e) {
fail("Unexpected exception was thrown: " + e.getMessage());
}
*/
DatagramSocket socket = null;
try {
byte[] rbuf = new byte[1000];
DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
SocketAddress address = new InetSocketAddress(portNumber);
DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(false);
socket = channel.socket();
socket.send(rdp);
fail("IllegalBlockingModeException was not thrown.");
} catch (IllegalBlockingModeException ibme) {
//expected
} catch (IOException ioe) {
fail("IOException was thrown: " + ioe.getMessage());
} finally {
socket.close();
}
//Regression for HARMONY-1118
class testDatagramSocket extends DatagramSocket {
public testDatagramSocket(DatagramSocketImpl impl) {
super(impl);
}
}
class testDatagramSocketImpl extends DatagramSocketImpl {
protected void create() throws SocketException {
}
protected void bind(int arg0, InetAddress arg1) throws SocketException {
}
protected void send(DatagramPacket arg0) throws IOException {
}
protected int peek(InetAddress arg0) throws IOException {
return 0;
}
protected int peekData(DatagramPacket arg0) throws IOException {
return 0;
}
protected void receive(DatagramPacket arg0) throws IOException {
}
protected void setTTL(byte arg0) throws IOException {
}
protected byte getTTL() throws IOException {
return 0;
}
protected void setTimeToLive(int arg0) throws IOException {
}
protected int getTimeToLive() throws IOException {
return 0;
}
protected void join(InetAddress arg0) throws IOException {
}
protected void leave(InetAddress arg0) throws IOException {
}
protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
}
protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
}
protected void close() {
}
public void setOption(int arg0, Object arg1) throws SocketException {
}
public Object getOption(int arg0) throws SocketException {
return null;
}
}
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), 0);
//no exception expected for next line
new testDatagramSocket(new testDatagramSocketImpl()).send(new DatagramPacket(new byte[272], 3, sa));
// Regression test for Harmony-2938
InetAddress i = InetAddress.getByName("127.0.0.1");
DatagramSocket d = new DatagramSocket(0, i);
try {
d.send(new DatagramPacket(new byte[] { 1 }, 1));
fail("should throw NPE.");
} catch (NullPointerException e) {
// expected;
} finally {
d.close();
}
}
use of java.net.NetworkInterface in project robovm by robovm.
the class NetworkInterfaceTest method testIPv6.
// http://code.google.com/p/android/issues/detail?id=13784
public void testIPv6() throws Exception {
NetworkInterface lo = NetworkInterface.getByName(LO);
Set<InetAddress> actual = new HashSet<InetAddress>(Collections.list(lo.getInetAddresses()));
// RoboVM note: The original test checks that lo has exactly 2 addresses
// (127.0.0.1 and ::1) but on Darwin we get an extra one (fe80::1) so
// we now check for those 2 and ignore everything else.
assertTrue(actual.contains(Inet4Address.LOOPBACK));
assertTrue(actual.contains(Inet6Address.getByAddress("localhost", new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, null)));
}
use of java.net.NetworkInterface in project robovm by robovm.
the class InetAddressTest method test_isReachable.
public void test_isReachable() throws Exception {
// http://code.google.com/p/android/issues/detail?id=20203
String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af" + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737" + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300" + "00000274000e7777772e676f6f676c652e636f6d";
InetAddress inetAddress = InetAddress.getByName("www.google.com");
new SerializationTester<InetAddress>(inetAddress, s) {
@Override
protected void verify(InetAddress deserialized) throws Exception {
deserialized.isReachable(500);
for (NetworkInterface nif : Collections.list(NetworkInterface.getNetworkInterfaces())) {
deserialized.isReachable(nif, 20, 500);
}
}
@Override
protected boolean equals(InetAddress a, InetAddress b) {
return a.getHostName().equals(b.getHostName());
}
}.test();
}
Aggregations