Search in sources :

Example 86 with InterfaceAddress

use of java.net.InterfaceAddress in project jmeter by apache.

the class ParallelResourcesAndIpSource method implementationsAndIps.

public static Iterable<Arguments> implementationsAndIps() throws SocketException {
    List<Arguments> res = new ArrayList<>();
    // Caveat: 127.0.0.1
    InetAddress local4 = null;
    InetAddress local6 = null;
    List<InetAddress> localIps = Collections.list(NetworkInterface.getNetworkInterfaces()).stream().filter(networkInterface -> {
        try {
            return networkInterface.isLoopback();
        } catch (SocketException e) {
            return false;
        }
    }).flatMap(ni -> ni.getInterfaceAddresses().stream()).map(InterfaceAddress::getAddress).collect(Collectors.toList());
    // We don't want to have accidental O(N^2) in case there are lots of source IPs
    // So we use just two for the source IP
    // So we shuffle them to use different ones every time
    Collections.shuffle(localIps);
    for (InetAddress localIp : localIps) {
        if (local4 == null && localIp instanceof Inet4Address) {
            local4 = localIp;
        } else if (local6 == null && localIp instanceof Inet6Address) {
            local6 = localIp;
        }
    }
    // This is to allow the variables for use in lambda below (the vars should be effectively final)
    InetAddress finalLocal4 = local4;
    InetAddress finalLocal6 = local6;
    // This is to make test names pretty
    localIps.sort(Comparator.comparing(InetAddress::toString));
    for (String impl : HTTPSamplerFactory.getImplementations()) {
        for (String targetHost : new String[] { "127.0.0.1", "[::1]", "localhost" }) {
            if ("localhost".equals(targetHost)) {
                // with the given source IP
                continue;
            }
            localIps.forEach(localIp -> {
                if (finalLocal4 != null) {
                    res.add(arguments(impl, targetHost, finalLocal4));
                }
                if (finalLocal6 != null) {
                    res.add(arguments(impl, targetHost, finalLocal6));
                }
            });
        }
    }
    return res;
}
Also used : JMeterContextExtension(org.apache.jmeter.util.JMeterContextExtension) BugId(org.apache.jmeter.testkit.BugId) SampleResult(org.apache.jmeter.samplers.SampleResult) ArrayList(java.util.ArrayList) WireMock(com.github.tomakehurst.wiremock.client.WireMock) InetAddress(java.net.InetAddress) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) SocketException(java.net.SocketException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) MethodSource(org.junit.jupiter.params.provider.MethodSource) InterfaceAddress(java.net.InterfaceAddress) WireMockExtension(org.apache.jmeter.wiremock.WireMockExtension) NetworkInterface(java.net.NetworkInterface) Arguments(org.junit.jupiter.params.provider.Arguments) Inet4Address(java.net.Inet4Address) Collectors(java.util.stream.Collectors) FunctionProperty(org.apache.jmeter.testelement.property.FunctionProperty) TestInfo(org.junit.jupiter.api.TestInfo) Inet6Address(java.net.Inet6Address) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Assumptions(org.junit.jupiter.api.Assumptions) LagartoBasedHtmlParser(org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser) Assertions(org.junit.jupiter.api.Assertions) Comparator(java.util.Comparator) Collections(java.util.Collections) HTTPConstants(org.apache.jmeter.protocol.http.util.HTTPConstants) SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) Arguments(org.junit.jupiter.params.provider.Arguments) ArrayList(java.util.ArrayList) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 87 with InterfaceAddress

use of java.net.InterfaceAddress in project cxf by apache.

the class WSDiscoveryClientTest method findIpv4Interface.

static NetworkInterface findIpv4Interface() throws Exception {
    Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
    List<NetworkInterface> possibles = new ArrayList<>();
    if (ifcs != null) {
        while (ifcs.hasMoreElements()) {
            NetworkInterface ni = ifcs.nextElement();
            if (ni.supportsMulticast() && ni.isUp()) {
                for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
                    if (ia.getAddress() instanceof java.net.Inet4Address && !ia.getAddress().isLoopbackAddress() && !ni.getDisplayName().startsWith("vnic")) {
                        possibles.add(ni);
                    }
                }
            }
        }
    }
    return possibles.isEmpty() ? null : possibles.get(possibles.size() - 1);
}
Also used : InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface)

Example 88 with InterfaceAddress

use of java.net.InterfaceAddress in project uavstack by uavorg.

the class HostNewworkInfo method getNetCardInfo.

/**
 * @return {wlan0={ips={"192.168.1.109":{"bcast":"192.168.1.255","mask":"255.255.255.0"}}, mac=28:C6:3F:C4:29:23}}
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public Map getNetCardInfo() {
    Map netCardInfos = new HashMap<String, Map<String, String>>();
    Enumeration<NetworkInterface> netInterfaces;
    try {
        netInterfaces = NetworkInterface.getNetworkInterfaces();
        while (netInterfaces.hasMoreElements()) {
            NetworkInterface ni = netInterfaces.nextElement();
            // if (ni.isVirtual()) {
            // continue;
            // }
            Map ipInfos = new HashMap<String, Map<String, String>>();
            List<InterfaceAddress> addresses = ni.getInterfaceAddresses();
            for (InterfaceAddress ia : addresses) {
                if (ia.getAddress().isLoopbackAddress() || ia.getAddress().getHostAddress().indexOf(":") != -1) {
                    continue;
                }
                Map ipInfo = new HashMap<String, String>();
                InetAddress bcast = ia.getBroadcast();
                ipInfo.put("bcast", bcast == null ? "UNKNOWN" : bcast.getHostAddress());
                ipInfo.put("mask", NetmaskLengthToNetmask(ia.getNetworkPrefixLength()));
                ipInfos.put(ia.getAddress().getHostAddress(), ipInfo);
            }
            if (!ipInfos.isEmpty()) {
                Map netCardInfo = new HashMap<String, Map<String, String>>();
                netCardInfo.put("ips", JSONHelper.toString(ipInfos));
                netCardInfo.put("mac", getMacAddressAsString(ni));
                netCardInfos.put(ni.getName(), netCardInfo);
            }
        }
    } catch (SocketException e) {
    // ignore
    }
    return netCardInfos;
}
Also used : SocketException(java.net.SocketException) HashMap(java.util.HashMap) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) Map(java.util.Map) HashMap(java.util.HashMap) InetAddress(java.net.InetAddress)

Example 89 with InterfaceAddress

use of java.net.InterfaceAddress in project kcanotify by antest1.

the class Util method getNetworkInfo.

public static String getNetworkInfo(Context context) {
    StringBuilder sb = new StringBuilder();
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ani = cm.getActiveNetworkInfo();
    List<NetworkInfo> listNI = new ArrayList<>();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        listNI.addAll(Arrays.asList(cm.getAllNetworkInfo()));
    else
        for (Network network : cm.getAllNetworks()) {
            NetworkInfo ni = cm.getNetworkInfo(network);
            if (ni != null)
                listNI.add(ni);
        }
    for (NetworkInfo ni : listNI) {
        sb.append(ni.getTypeName()).append('/').append(ni.getSubtypeName()).append(' ').append(ni.getDetailedState()).append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo()).append(ni.getType() == ConnectivityManager.TYPE_MOBILE ? " " + Util.getNetworkGeneration(ni.getSubtype()) : "").append(ni.isRoaming() ? " R" : "").append(ani != null && ni.getType() == ani.getType() && ni.getSubtype() == ani.getSubtype() ? " *" : "").append("\r\n");
    }
    try {
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        if (nis != null)
            while (nis.hasMoreElements()) {
                NetworkInterface ni = nis.nextElement();
                if (ni != null && !"lo".equals(ni.getName())) {
                    List<InterfaceAddress> ias = ni.getInterfaceAddresses();
                    if (ias != null)
                        for (InterfaceAddress ia : ias) sb.append(ni.getName()).append(' ').append(ia.getAddress().getHostAddress()).append('/').append(ia.getNetworkPrefixLength()).append(' ').append(ni.getMTU()).append("\r\n");
                }
            }
    } catch (Throwable ex) {
        sb.append(ex.toString()).append("\r\n");
    }
    if (sb.length() > 2)
        sb.setLength(sb.length() - 2);
    return sb.toString();
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) InterfaceAddress(java.net.InterfaceAddress) Network(android.net.Network) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) ArrayList(java.util.ArrayList) List(java.util.List)

Example 90 with InterfaceAddress

use of java.net.InterfaceAddress in project wechat by dllwh.

the class SystemHelper method getHostIP.

/**
 * @方法描述: 获取主机IP
 * @return
 */
public static String getHostIP() {
    String localip = "";
    InetAddress ip = null;
    List<InterfaceAddress> addressList = null;
    try {
        // 获得本机的所有网络接口
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        while (nets.hasMoreElements()) {
            NetworkInterface netint = (NetworkInterface) nets.nextElement();
            addressList = netint.getInterfaceAddresses();
            for (InterfaceAddress interfaceAddress : addressList) {
                ip = interfaceAddress.getAddress();
                if (ip instanceof Inet4Address) {
                    // 只关心 IPv4 地址
                    if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                        localip = ip.getHostAddress();
                    } else {
                        localip += ip;
                    }
                }
            }
        }
        localip = localip.replaceAll("null", "");
    } catch (Exception e) {
        System.out.println("获取服务器IP出错");
    }
    return localip;
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

InterfaceAddress (java.net.InterfaceAddress)104 NetworkInterface (java.net.NetworkInterface)69 InetAddress (java.net.InetAddress)60 SocketException (java.net.SocketException)27 Inet4Address (java.net.Inet4Address)26 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)9 Inet6Address (java.net.Inet6Address)8 UnknownHostException (java.net.UnknownHostException)7 NotifyListener (net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener)7 Command (com.android.server.NativeDaemonConnector.Command)6 LinkAddress (android.net.LinkAddress)5 DatagramPacket (java.net.DatagramPacket)5 List (java.util.List)5 InetSocketAddress (java.net.InetSocketAddress)4 SharedPreferences (android.content.SharedPreferences)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Enumeration (java.util.Enumeration)3 LinkedList (java.util.LinkedList)3