Search in sources :

Example 11 with NetInterfaceAddress

use of org.eclipse.kura.net.NetInterfaceAddress in project kura by eclipse.

the class EmulatedNetworkServiceImpl method getNetworkInterfaces.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public List<NetInterface<? extends NetInterfaceAddress>> getNetworkInterfaces() throws KuraException {
    IPAddress netAddress = null;
    NetInterfaceAddressImpl addressImpl = null;
    List<NetInterfaceAddress> addresses = null;
    List<NetInterface<? extends NetInterfaceAddress>> interfaces = new ArrayList<NetInterface<? extends NetInterfaceAddress>>();
    EthernetInterfaceImpl ethInterface = null;
    java.net.NetworkInterface jnInterface = null;
    List<java.net.InterfaceAddress> jnInterfaceAddresses = null;
    Enumeration<java.net.NetworkInterface> jnInterfaces = null;
    try {
        jnInterfaces = java.net.NetworkInterface.getNetworkInterfaces();
        while (jnInterfaces.hasMoreElements()) {
            jnInterface = jnInterfaces.nextElement();
            ethInterface = new EthernetInterfaceImpl(jnInterface.getName());
            ethInterface.setVirtual(jnInterface.isVirtual());
            ethInterface.setState(NetInterfaceState.ACTIVATED);
            ethInterface.setAutoConnect(true);
            byte[] hwAddr = null;
            boolean isUp = false;
            boolean isLoop = false;
            int mtu = 0;
            boolean isP2p = false;
            boolean multi = false;
            try {
                hwAddr = jnInterface.getHardwareAddress();
                isUp = jnInterface.isUp();
                isLoop = jnInterface.isLoopback();
                mtu = jnInterface.getMTU();
                isP2p = jnInterface.isPointToPoint();
                multi = jnInterface.supportsMulticast();
            } catch (Exception e) {
                s_logger.warn("Exception while getting information for interface " + jnInterface.getName() + ": " + e.getMessage());
            }
            ethInterface.setHardwareAddress(hwAddr);
            ethInterface.setLinkUp(isUp);
            ethInterface.setLoopback(isLoop);
            ethInterface.setMTU(mtu);
            ethInterface.setPointToPoint(isP2p);
            ethInterface.setSupportsMulticast(multi);
            ethInterface.setUp(isUp);
            addresses = new ArrayList<NetInterfaceAddress>();
            jnInterfaceAddresses = jnInterface.getInterfaceAddresses();
            for (java.net.InterfaceAddress jnInterfaceAddress : jnInterfaceAddresses) {
                netAddress = IPAddress.getByAddress(jnInterfaceAddress.getAddress().getAddress());
                addressImpl = new NetInterfaceAddressImpl();
                addressImpl.setAddress(netAddress);
                if (jnInterfaceAddress.getBroadcast() != null) {
                    addressImpl.setBroadcast(IPAddress.getByAddress(jnInterfaceAddress.getBroadcast().getAddress()));
                }
                addressImpl.setNetworkPrefixLength(jnInterfaceAddress.getNetworkPrefixLength());
                addresses.add(addressImpl);
            }
            ethInterface.setNetInterfaceAddresses(addresses);
            interfaces.add(ethInterface);
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    return interfaces;
}
Also used : NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) ArrayList(java.util.ArrayList) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) SocketException(java.net.SocketException) KuraException(org.eclipse.kura.KuraException) EthernetInterfaceImpl(org.eclipse.kura.core.net.EthernetInterfaceImpl) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) NetInterfaceAddressImpl(org.eclipse.kura.core.net.NetInterfaceAddressImpl) IPAddress(org.eclipse.kura.net.IPAddress)

Example 12 with NetInterfaceAddress

use of org.eclipse.kura.net.NetInterfaceAddress in project kura by eclipse.

the class NetworkConfiguration method recomputeNetworkProperties.

// ---------------------------------------------------------------
// 
// Private Methods
// 
// ---------------------------------------------------------------
private void recomputeNetworkProperties() {
    Map<String, Object> properties = new HashMap<String, Object>();
    String netIfPrefix = null;
    String netIfReadOnlyPrefix = null;
    String netIfConfigPrefix = null;
    StringBuilder sbPrefix = null;
    StringBuilder sbInterfaces = new StringBuilder();
    if (this.m_modifiedInterfaceNames != null && !this.m_modifiedInterfaceNames.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        String prefix = "";
        for (String interfaceName : this.m_modifiedInterfaceNames) {
            sb.append(prefix);
            prefix = ",";
            sb.append(interfaceName);
        }
        String result = sb.toString();
        s_logger.debug("Set modified interface names: {}", result);
        properties.put("modified.interface.names", result);
    }
    Iterator<String> it = this.m_netInterfaceConfigs.keySet().iterator();
    while (it.hasNext()) {
        NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_netInterfaceConfigs.get(it.next());
        // add the interface to the list of interface found in the platform
        if (sbInterfaces.length() != 0) {
            sbInterfaces.append(",");
        }
        sbInterfaces.append(netInterfaceConfig.getName());
        // build the prefixes for all the properties associated with this interface
        sbPrefix = new StringBuilder("net.interface.").append(netInterfaceConfig.getName()).append(".");
        netIfReadOnlyPrefix = sbPrefix.toString();
        netIfPrefix = sbPrefix.append("config.").toString();
        netIfConfigPrefix = sbPrefix.toString();
        // add the properties of the interface
        properties.put(netIfReadOnlyPrefix + "type", netInterfaceConfig.getType().toString());
        properties.put(netIfPrefix + "name", netInterfaceConfig.getName());
        if (netInterfaceConfig.getState() != null) {
            properties.put(netIfPrefix + "state", netInterfaceConfig.getState().toString());
        }
        properties.put(netIfPrefix + "autoconnect", netInterfaceConfig.isAutoConnect());
        properties.put(netIfPrefix + "mtu", netInterfaceConfig.getMTU());
        properties.put(netIfReadOnlyPrefix + "driver", netInterfaceConfig.getDriver());
        properties.put(netIfReadOnlyPrefix + "driver.version", netInterfaceConfig.getDriverVersion());
        properties.put(netIfReadOnlyPrefix + "firmware.version", netInterfaceConfig.getFirmwareVersion());
        properties.put(netIfReadOnlyPrefix + "mac", NetUtil.hardwareAddressToString(netInterfaceConfig.getHardwareAddress()));
        properties.put(netIfReadOnlyPrefix + "loopback", netInterfaceConfig.isLoopback());
        properties.put(netIfReadOnlyPrefix + "ptp", netInterfaceConfig.isPointToPoint());
        properties.put(netIfReadOnlyPrefix + "up", netInterfaceConfig.isUp());
        properties.put(netIfReadOnlyPrefix + "virtual", netInterfaceConfig.isVirtual());
        // usb
        if (netInterfaceConfig.getUsbDevice() != null) {
            UsbDevice usbDev = netInterfaceConfig.getUsbDevice();
            properties.put(netIfReadOnlyPrefix + "usb.vendor.id", usbDev.getVendorId());
            properties.put(netIfReadOnlyPrefix + "usb.vendor.name", usbDev.getManufacturerName());
            properties.put(netIfReadOnlyPrefix + "usb.product.id", usbDev.getProductId());
            properties.put(netIfReadOnlyPrefix + "usb.product.name", usbDev.getProductName());
            properties.put(netIfReadOnlyPrefix + "usb.busNumber", usbDev.getUsbBusNumber());
            properties.put(netIfReadOnlyPrefix + "usb.devicePath", usbDev.getUsbDevicePath());
        }
        // custom readonly props for Ethernet and Wifi
        if (netInterfaceConfig instanceof EthernetInterfaceConfigImpl) {
            properties.put(netIfReadOnlyPrefix + "eth.link.up", ((EthernetInterfaceConfigImpl) netInterfaceConfig).isLinkUp());
        } else if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
            EnumSet<Capability> capabilities = ((WifiInterfaceConfigImpl) netInterfaceConfig).getCapabilities();
            if (capabilities != null && !capabilities.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                for (Capability capability : capabilities) {
                    sb.append(capability.toString());
                    sb.append(",");
                }
                String capabilitiesString = sb.toString();
                capabilitiesString = capabilitiesString.substring(0, capabilitiesString.length() - 1);
                properties.put(netIfReadOnlyPrefix + "wifi.capabilities", capabilitiesString);
            }
        }
        // add wifi properties
        if (netInterfaceConfig.getType() == NetInterfaceType.WIFI) {
            // capabilities
            StringBuilder sbCapabilities = new StringBuilder();
            EnumSet<Capability> capabilities = ((WifiInterface) netInterfaceConfig).getCapabilities();
            if (capabilities != null) {
                Iterator<Capability> it2 = ((WifiInterface) netInterfaceConfig).getCapabilities().iterator();
                while (it2.hasNext()) {
                    sbCapabilities.append(it2.next().name()).append(" ");
                }
                properties.put(netIfReadOnlyPrefix + "wifi.capabilities", sbCapabilities.toString());
            }
        }
        // add modem properties
        if (netInterfaceConfig.getType() == NetInterfaceType.MODEM) {
            String delim;
            // revision
            StringBuffer revisionIdBuf = new StringBuffer();
            String[] revisionId = ((ModemInterface<?>) netInterfaceConfig).getRevisionId();
            if (revisionId != null) {
                delim = null;
                for (String rev : revisionId) {
                    if (delim != null) {
                        revisionIdBuf.append(delim);
                    }
                    revisionIdBuf.append(rev);
                    delim = ",";
                }
            }
            // technology types
            StringBuffer techTypesBuf = new StringBuffer();
            List<ModemTechnologyType> techTypes = ((ModemInterface<?>) netInterfaceConfig).getTechnologyTypes();
            if (techTypes != null) {
                delim = null;
                for (ModemTechnologyType techType : techTypes) {
                    if (delim != null) {
                        techTypesBuf.append(delim);
                    }
                    techTypesBuf.append(techType.toString());
                    delim = ",";
                }
            }
            ModemPowerMode powerMode = ModemPowerMode.UNKNOWN;
            if (((ModemInterface<?>) netInterfaceConfig).getPowerMode() != null) {
                powerMode = ((ModemInterface<?>) netInterfaceConfig).getPowerMode();
            }
            properties.put(netIfReadOnlyPrefix + "manufacturer", ((ModemInterface<?>) netInterfaceConfig).getManufacturer());
            properties.put(netIfReadOnlyPrefix + "model", ((ModemInterface<?>) netInterfaceConfig).getModel());
            properties.put(netIfReadOnlyPrefix + "revisionId", revisionIdBuf.toString());
            properties.put(netIfReadOnlyPrefix + "serialNum", ((ModemInterface<?>) netInterfaceConfig).getSerialNumber());
            properties.put(netIfReadOnlyPrefix + "technologyTypes", techTypesBuf.toString());
            properties.put(netIfConfigPrefix + "identifier", ((ModemInterface<?>) netInterfaceConfig).getModemIdentifier());
            properties.put(netIfConfigPrefix + "powerMode", powerMode.toString());
            properties.put(netIfConfigPrefix + "pppNum", ((ModemInterface<?>) netInterfaceConfig).getPppNum());
            properties.put(netIfConfigPrefix + "poweredOn", ((ModemInterface<?>) netInterfaceConfig).isPoweredOn());
        }
        for (NetInterfaceAddress nia : netInterfaceConfig.getNetInterfaceAddresses()) {
            String typePrefix = "ip4.";
            if (nia != null) {
                if (nia.getAddress() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "address", nia.getAddress().getHostAddress());
                }
                if (nia.getBroadcast() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "broadcast", nia.getBroadcast().getHostAddress());
                }
                if (nia.getGateway() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "gateway", nia.getGateway().getHostAddress());
                }
                if (nia.getNetmask() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "netmask", nia.getNetmask().getHostAddress());
                }
                if (nia.getNetmask() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "prefix", Short.valueOf(nia.getNetworkPrefixLength()));
                }
                if (nia.getDnsServers() != null) {
                    StringBuilder dnsServers = new StringBuilder();
                    for (IPAddress dnsServer : nia.getDnsServers()) {
                        if (dnsServers.length() != 0) {
                            dnsServers.append(",");
                        }
                        dnsServers.append(dnsServer);
                    }
                    properties.put(netIfReadOnlyPrefix + typePrefix + "dnsServers", dnsServers.toString());
                }
                // Wifi interface address
                if (nia instanceof WifiInterfaceAddress) {
                    // access point
                    WifiAccessPoint wap = ((WifiInterfaceAddress) nia).getWifiAccessPoint();
                    if (wap != null) {
                    /*
                             * TODO: need fields to reflect current state?
                             * properties.put(sbNetIfPrefix+"wifi.ssid", wap.getSSID());
                             * properties.put(sbNetIfPrefix+"wifi.mode", wap.getMode());
                             */
                    }
                    long bitrate = ((WifiInterfaceAddress) nia).getBitrate();
                    properties.put(netIfReadOnlyPrefix + "wifi.bitrate", Long.valueOf(bitrate));
                    WifiMode wifiMode;
                    if (((WifiInterfaceAddress) nia).getMode() != null) {
                        wifiMode = ((WifiInterfaceAddress) nia).getMode();
                    } else {
                        wifiMode = WifiMode.UNKNOWN;
                    }
                    properties.put(netIfPrefix + "wifi.mode", wifiMode.toString());
                }
                // Modem interface address
                if (nia instanceof ModemInterfaceAddress) {
                    if (((ModemInterfaceAddress) nia).getConnectionType() != null) {
                        properties.put(netIfConfigPrefix + "connection.type", ((ModemInterfaceAddress) nia).getConnectionType().toString());
                    }
                    if (((ModemInterfaceAddress) nia).getConnectionStatus() != null) {
                        properties.put(netIfConfigPrefix + "connection.status", ((ModemInterfaceAddress) nia).getConnectionStatus().toString());
                    }
                }
            }
        }
        // add the properties of the network configurations associated to the interface
        List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
        s_logger.trace("netInterfaceAddressConfigs.size() for {}: {}", netInterfaceConfig.getName(), netInterfaceAddressConfigs.size());
        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
            List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
            if (netConfigs != null) {
                s_logger.trace("netConfigs.size(): {}", netConfigs.size());
                for (NetConfig netConfig : netConfigs) {
                    if (netConfig instanceof WifiConfig) {
                        s_logger.trace("adding netconfig WifiConfigIP4 for {}", netInterfaceConfig.getName());
                        addWifiConfigIP4Properties((WifiConfig) netConfig, netIfConfigPrefix, properties);
                    } else if (netConfig instanceof ModemConfig) {
                        s_logger.trace("adding netconfig ModemConfig for {}", netInterfaceConfig.getName());
                        addModemConfigProperties((ModemConfig) netConfig, netIfConfigPrefix, properties);
                    } else if (netConfig instanceof NetConfigIP4) {
                        s_logger.trace("adding netconfig NetConfigIP4 for {}", netInterfaceConfig.getName());
                        addNetConfigIP4Properties((NetConfigIP4) netConfig, netIfConfigPrefix, properties);
                    /*
                             * Iterator<String> it2 = properties.keySet().iterator();
                             * while(it2.hasNext()) {
                             * String key = it2.next();
                             * System.out.println("\t\t\t"+key+"="+properties.get(key));
                             * }
                             */
                    } else if (netConfig instanceof NetConfigIP6) {
                        s_logger.trace("adding netconfig NetConfigIP6 for {}", netInterfaceConfig.getName());
                        addNetConfigIP6Properties((NetConfigIP6) netConfig, netIfConfigPrefix, properties);
                    /*
                             * Iterator<String> it = properties.keySet().iterator();
                             * while(it.hasNext()) {
                             * String key = it.next();
                             * System.out.println("\t\t\t"+key+"="+properties.get(key));
                             * }
                             */
                    } else if (netConfig instanceof DhcpServerConfig4) {
                        s_logger.trace("adding netconfig DhcpServerConfig4 for {}", netInterfaceConfig.getName());
                        addDhcpServerConfig4((DhcpServerConfig4) netConfig, netIfConfigPrefix, properties);
                    } else if (netConfig instanceof FirewallAutoNatConfig) {
                        s_logger.trace("adding netconfig FirewallNatConfig for {}", netInterfaceConfig.getName());
                        addFirewallNatConfig((FirewallAutoNatConfig) netConfig, netIfConfigPrefix, properties);
                    }
                }
            }
        }
    }
    properties.put("net.interfaces", sbInterfaces.toString());
    this.m_properties = properties;
}
Also used : FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) HashMap(java.util.HashMap) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) UsbDevice(org.eclipse.kura.usb.UsbDevice) WifiInterface(org.eclipse.kura.net.wifi.WifiInterface) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) WifiMode(org.eclipse.kura.net.wifi.WifiMode) ModemTechnologyType(org.eclipse.kura.net.modem.ModemTechnologyType) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) ModemInterface(org.eclipse.kura.net.modem.ModemInterface) Capability(org.eclipse.kura.net.wifi.WifiInterface.Capability) EnumSet(java.util.EnumSet) ModemPowerMode(org.eclipse.kura.net.modem.ModemPowerMode) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceAddress(org.eclipse.kura.net.wifi.WifiInterfaceAddress) IPAddress(org.eclipse.kura.net.IPAddress) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress)

Example 13 with NetInterfaceAddress

use of org.eclipse.kura.net.NetInterfaceAddress in project kura by eclipse.

the class NetworkServiceTest method testActiveInterfaces.

@TestTarget(targetPlatforms = { TestTarget.PLATFORM_ALL })
@Test
public void testActiveInterfaces() throws Exception {
    List<NetInterface<? extends NetInterfaceAddress>> interfaces = networkService.getActiveNetworkInterfaces();
    assertNotNull(interfaces);
    System.out.println("number of active interfaces: " + interfaces.size());
    assertTrue(interfaces.size() > 0);
    for (NetInterface<? extends NetInterfaceAddress> ni : interfaces) {
        System.out.println("network interface name: " + ni.getName());
        System.out.println("network interface type: " + ni.getType());
        System.out.println("network interface mac: " + getMacFromHwAddress(ni.getHardwareAddress()));
        System.out.println("network interface state: " + ni.getState());
        assertNotNull(ni.getName());
        assertFalse(ni.getName().isEmpty());
        assertNotNull(ni.getType());
        assertNotNull(ni.getState());
        assertNotNull(ni.getNetInterfaceAddresses());
        for (NetInterfaceAddress ia : ni.getNetInterfaceAddresses()) {
            if (ia.getAddress() != null) {
                System.out.println("   network interface address: " + ia.getAddress().getHostAddress());
            }
        }
    }
}
Also used : NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) TestTarget(org.eclipse.kura.test.annotation.TestTarget) Test(org.junit.Test)

Example 14 with NetInterfaceAddress

use of org.eclipse.kura.net.NetInterfaceAddress in project kura by eclipse.

the class NetworkServiceTest method testAllInterfaces.

@TestTarget(targetPlatforms = { TestTarget.PLATFORM_ALL })
@Test
public void testAllInterfaces() throws Exception {
    List<NetInterface<? extends NetInterfaceAddress>> interfaces = networkService.getNetworkInterfaces();
    assertNotNull(interfaces);
    System.out.println("number of interfaces: " + interfaces.size());
    assertTrue(interfaces.size() > 0);
    for (NetInterface<? extends NetInterfaceAddress> ni : interfaces) {
        System.out.println("network interface name: " + ni.getName());
        System.out.println("network interface type: " + ni.getType());
        System.out.println("network interface mac: " + getMacFromHwAddress(ni.getHardwareAddress()));
        System.out.println("network interface state: " + ni.getState());
        assertNotNull(ni.getName());
        assertFalse(ni.getName().isEmpty());
        assertNotNull(ni.getType());
        assertNotNull(ni.getState());
        assertNotNull(ni.getNetInterfaceAddresses());
        for (NetInterfaceAddress ia : ni.getNetInterfaceAddresses()) {
            if (ia.getAddress() != null) {
                System.out.println("   network interface address: " + ia.getAddress().getHostAddress());
            }
        }
    }
}
Also used : NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) TestTarget(org.eclipse.kura.test.annotation.TestTarget) Test(org.junit.Test)

Aggregations

NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)14 NetInterface (org.eclipse.kura.net.NetInterface)9 ArrayList (java.util.ArrayList)7 KuraException (org.eclipse.kura.KuraException)7 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)5 SocketException (java.net.SocketException)3 UnknownHostException (java.net.UnknownHostException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 EthernetInterfaceImpl (org.eclipse.kura.core.net.EthernetInterfaceImpl)3 NetInterfaceAddressImpl (org.eclipse.kura.core.net.NetInterfaceAddressImpl)3 IPAddress (org.eclipse.kura.net.IPAddress)3 NetConfig (org.eclipse.kura.net.NetConfig)3 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)3 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)3 ModemInterfaceAddress (org.eclipse.kura.net.modem.ModemInterfaceAddress)3 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)3 WifiInterfaceAddress (org.eclipse.kura.net.wifi.WifiInterfaceAddress)3 EthernetInterfaceConfigImpl (org.eclipse.kura.core.net.EthernetInterfaceConfigImpl)2 LoopbackInterfaceConfigImpl (org.eclipse.kura.core.net.LoopbackInterfaceConfigImpl)2