Search in sources :

Example 6 with NetInterfaceAddress

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

the class NetworkServiceImpl method getNetworkInterface.

public NetInterface<? extends NetInterfaceAddress> getNetworkInterface(String interfaceName) throws KuraException {
    // ignore redpine vlan interface
    if (interfaceName.startsWith("rpine")) {
        s_logger.debug("Ignoring redpine vlan interface.");
        return null;
    }
    // ignore usb0 for beaglebone
    if (interfaceName.startsWith("usb0") && "beaglebone".equals(System.getProperty("target.device"))) {
        s_logger.debug("Ignoring usb0 for beaglebone.");
        return null;
    }
    LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
    if (ifconfig == null) {
        s_logger.debug("Ignoring {} interface.", interfaceName);
        return null;
    }
    NetInterfaceType type = ifconfig.getType();
    boolean isUp = ifconfig.isUp();
    if (type == NetInterfaceType.UNKNOWN) {
        if (interfaceName.matches(UNCONFIGURED_MODEM_REGEX)) {
            // If the interface name is in a form such as "1-3.4", assume it is a modem
            type = NetInterfaceType.MODEM;
        } else if (this.m_serialModem != null && interfaceName.equals(this.m_serialModem.getProductName())) {
            type = NetInterfaceType.MODEM;
        }
    }
    if (type == NetInterfaceType.ETHERNET) {
        EthernetInterfaceImpl<NetInterfaceAddress> netInterface = new EthernetInterfaceImpl<NetInterfaceAddress>(interfaceName);
        Map<String, String> driver = LinuxNetworkUtil.getEthernetDriver(interfaceName);
        netInterface.setDriver(driver.get("name"));
        netInterface.setDriverVersion(driver.get("version"));
        netInterface.setFirmwareVersion(driver.get("firmware"));
        netInterface.setAutoConnect(LinuxNetworkUtil.isAutoConnect(interfaceName));
        netInterface.setHardwareAddress(ifconfig.getMacAddressBytes());
        netInterface.setMTU(ifconfig.getMtu());
        netInterface.setSupportsMulticast(ifconfig.isMulticast());
        netInterface.setLinkUp(LinuxNetworkUtil.isLinkUp(type, interfaceName));
        netInterface.setLoopback(false);
        netInterface.setPointToPoint(false);
        netInterface.setUp(isUp);
        netInterface.setVirtual(isVirtual());
        netInterface.setUsbDevice(getUsbDevice(interfaceName));
        netInterface.setState(getState(interfaceName, isUp));
        netInterface.setNetInterfaceAddresses(getNetInterfaceAddresses(interfaceName, type, isUp));
        return netInterface;
    } else if (type == NetInterfaceType.LOOPBACK) {
        LoopbackInterfaceImpl<NetInterfaceAddress> netInterface = new LoopbackInterfaceImpl<NetInterfaceAddress>(interfaceName);
        netInterface.setDriver(getDriver());
        netInterface.setDriverVersion(getDriverVersion());
        netInterface.setFirmwareVersion(getFirmwareVersion());
        netInterface.setAutoConnect(LinuxNetworkUtil.isAutoConnect(interfaceName));
        netInterface.setHardwareAddress(new byte[] { 0, 0, 0, 0, 0, 0 });
        netInterface.setLoopback(true);
        netInterface.setMTU(ifconfig.getMtu());
        netInterface.setSupportsMulticast(ifconfig.isMulticast());
        netInterface.setPointToPoint(false);
        netInterface.setUp(isUp);
        netInterface.setVirtual(false);
        netInterface.setUsbDevice(null);
        netInterface.setState(getState(interfaceName, isUp));
        netInterface.setNetInterfaceAddresses(getNetInterfaceAddresses(interfaceName, type, isUp));
        return netInterface;
    } else if (type == NetInterfaceType.WIFI) {
        WifiInterfaceImpl<WifiInterfaceAddress> wifiInterface = new WifiInterfaceImpl<WifiInterfaceAddress>(interfaceName);
        Map<String, String> driver = LinuxNetworkUtil.getEthernetDriver(interfaceName);
        wifiInterface.setDriver(driver.get("name"));
        wifiInterface.setDriverVersion(driver.get("version"));
        wifiInterface.setFirmwareVersion(driver.get("firmware"));
        wifiInterface.setAutoConnect(LinuxNetworkUtil.isAutoConnect(interfaceName));
        wifiInterface.setHardwareAddress(ifconfig.getMacAddressBytes());
        wifiInterface.setMTU(ifconfig.getMtu());
        wifiInterface.setSupportsMulticast(ifconfig.isMulticast());
        // FIXME:MS Add linkUp in the AbstractNetInterface and populate accordingly
        // wifiInterface.setLinkUp(LinuxNetworkUtil.isLinkUp(type, interfaceName));
        wifiInterface.setLoopback(false);
        wifiInterface.setPointToPoint(false);
        wifiInterface.setUp(isUp);
        wifiInterface.setVirtual(isVirtual());
        wifiInterface.setUsbDevice(getUsbDevice(interfaceName));
        wifiInterface.setState(getState(interfaceName, isUp));
        wifiInterface.setNetInterfaceAddresses(getWifiInterfaceAddresses(interfaceName, isUp));
        wifiInterface.setCapabilities(LinuxNetworkUtil.getWifiCapabilities(interfaceName));
        return wifiInterface;
    } else if (type == NetInterfaceType.MODEM) {
        ModemDevice modemDevice = null;
        if (interfaceName.startsWith("ppp")) {
            // already connected - find the corresponding usb device
            modemDevice = this.m_usbModems.get(getModemUsbPort(interfaceName));
            if (modemDevice == null && this.m_serialModem != null) {
                modemDevice = this.m_serialModem;
            }
        } else if (interfaceName.matches(UNCONFIGURED_MODEM_REGEX)) {
            // the interface name is in the form of a usb port i.e. "1-3.4"
            modemDevice = this.m_usbModems.get(interfaceName);
        } else if (this.m_serialModem != null && interfaceName.equals(this.m_serialModem.getProductName())) {
            modemDevice = this.m_serialModem;
        }
        return modemDevice != null ? getModemInterface(interfaceName, isUp, modemDevice) : null;
    } else {
        if (interfaceName.startsWith("can")) {
            s_logger.trace("Ignoring CAN interface: {}", interfaceName);
        } else if (interfaceName.startsWith("ppp")) {
            s_logger.debug("Ignoring unconfigured ppp interface: {}", interfaceName);
        } else {
            s_logger.debug("Unsupported network type - not adding to network devices: {} of type: ", interfaceName, type.toString());
        }
        return null;
    }
}
Also used : WifiInterfaceImpl(org.eclipse.kura.core.net.WifiInterfaceImpl) ModemDevice(org.eclipse.kura.net.modem.ModemDevice) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) SerialModemDevice(org.eclipse.kura.net.modem.SerialModemDevice) LinuxIfconfig(org.eclipse.kura.linux.net.util.LinuxIfconfig) EthernetInterfaceImpl(org.eclipse.kura.core.net.EthernetInterfaceImpl) LoopbackInterfaceImpl(org.eclipse.kura.core.net.LoopbackInterfaceImpl) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) WifiInterfaceAddress(org.eclipse.kura.net.wifi.WifiInterfaceAddress)

Example 7 with NetInterfaceAddress

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

the class NetworkServiceImpl method getActiveNetworkInterfaces.

@Override
public List<NetInterface<? extends NetInterfaceAddress>> getActiveNetworkInterfaces() throws KuraException {
    List<NetInterface<? extends NetInterfaceAddress>> interfaces = getNetworkInterfaces();
    if (interfaces != null) {
        for (int i = 0; i < interfaces.size(); i++) {
            NetInterface<? extends NetInterfaceAddress> iface = interfaces.get(i);
            if (!LinuxNetworkUtil.hasAddress(iface.getName())) {
                s_logger.debug("removing interface {} because it is not up", iface.getName());
                interfaces.remove(i);
                i--;
            }
        }
    }
    return interfaces;
}
Also used : NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint)

Example 8 with NetInterfaceAddress

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

the class NetworkServiceImpl method getNetworkInterfaces.

@Override
public List<NetInterface<? extends NetInterfaceAddress>> getNetworkInterfaces() throws KuraException {
    s_logger.trace("getNetworkInterfaces()");
    List<NetInterface<? extends NetInterfaceAddress>> netInterfaces = new ArrayList<NetInterface<? extends NetInterfaceAddress>>();
    List<String> interfaceNames = getAllNetworkInterfaceNames();
    for (String interfaceName : interfaceNames) {
        try {
            NetInterface<? extends NetInterfaceAddress> netInterface = getNetworkInterface(interfaceName);
            if (netInterface != null) {
                netInterfaces.add(netInterface);
            }
        } catch (KuraException e) {
            s_logger.error("Can't get network interface info for {} :: exception - {}", interfaceName, e.toString());
        }
    }
    // Return an entry for non-connected modems (those w/o a ppp interface)
    Iterator<String> it = this.m_addedModems.iterator();
    while (it.hasNext()) {
        String modemId = it.next();
        UsbModemDevice usbModem = this.m_usbModems.get(modemId);
        if (usbModem != null) {
            // only add if there is not already a ppp interface for this modem
            boolean addModem = true;
            for (NetInterface<?> netInterface : netInterfaces) {
                UsbDevice usbDevice = netInterface.getUsbDevice();
                if (usbDevice != null) {
                    if (usbDevice.getUsbPort().equals(usbModem.getUsbPort())) {
                        addModem = false;
                        break;
                    }
                }
            }
            if (addModem) {
                netInterfaces.add(getModemInterface(usbModem.getUsbPort(), false, usbModem));
            }
        } else {
            // for Serial modem
            if (this.m_serialModem != null) {
                // only add if there is not already a ppp interface for this modem
                boolean addModem = true;
                for (NetInterface<?> netInterface : netInterfaces) {
                    String iface = netInterface.getName();
                    if (iface != null && iface.startsWith("ppp")) {
                        ModemInterface<ModemInterfaceAddress> pppModemInterface = getModemInterface(iface, false, this.m_serialModem);
                        ModemInterface<ModemInterfaceAddress> serialModemInterface = getModemInterface(this.m_serialModem.getProductName(), false, this.m_serialModem);
                        if (pppModemInterface != null && serialModemInterface != null) {
                            String pppModel = pppModemInterface.getModel();
                            String serialModel = serialModemInterface.getModel();
                            if (pppModel != null && pppModel.equals(serialModel)) {
                                addModem = false;
                                break;
                            }
                        }
                    }
                }
                if (addModem) {
                    netInterfaces.add(getModemInterface(this.m_serialModem.getProductName(), false, this.m_serialModem));
                }
            }
        }
    }
    return netInterfaces;
}
Also used : ArrayList(java.util.ArrayList) UsbDevice(org.eclipse.kura.usb.UsbDevice) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress)

Example 9 with NetInterfaceAddress

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

the class NetworkConfigurationServiceImpl method getNetworkConfiguration.

// @Override
// FIXME:MC Introducing a short lived cache will make startup much faster.
@Override
public synchronized NetworkConfiguration getNetworkConfiguration() throws KuraException {
    NetworkConfiguration networkConfiguration = new NetworkConfiguration();
    // Get the current values
    List<NetInterface<? extends NetInterfaceAddress>> allNetworkInterfaces = this.m_networkService.getNetworkInterfaces();
    Map<String, NetInterface<? extends NetInterfaceAddress>> allNetworkInterfacesMap = new HashMap<String, NetInterface<? extends NetInterfaceAddress>>();
    Map<String, NetInterface<? extends NetInterfaceAddress>> activeNetworkInterfacesMap = new HashMap<String, NetInterface<? extends NetInterfaceAddress>>();
    for (NetInterface<? extends NetInterfaceAddress> netInterface : allNetworkInterfaces) {
        allNetworkInterfacesMap.put(netInterface.getName(), netInterface);
        if (netInterface.isUp()) {
            activeNetworkInterfacesMap.put(netInterface.getName(), netInterface);
        }
    }
    // Create the NetInterfaceConfig objects
    if (allNetworkInterfacesMap.keySet() != null) {
        for (NetInterface<? extends NetInterfaceAddress> netInterface : allNetworkInterfacesMap.values()) {
            String interfaceName = netInterface.getName();
            try {
                // ignore mon interface
                if (interfaceName.startsWith("mon.")) {
                    continue;
                }
                // ignore redpine vlan interface
                if (interfaceName.startsWith("rpine")) {
                    continue;
                }
                // ignore usb0 for beaglebone
                if (interfaceName.startsWith("usb0") && System.getProperty("target.device").equals("beaglebone")) {
                    continue;
                }
                NetInterfaceType type = netInterface.getType();
                if (type == NetInterfaceType.UNKNOWN) {
                    if (interfaceName.matches(UNCONFIGURED_MODEM_REGEX)) {
                        // If the interface name is in a form such as "1-3.4", assume it is a modem
                        type = NetInterfaceType.MODEM;
                    } else {
                        SupportedSerialModemInfo serialModemInfo = SupportedSerialModemsInfo.getModem();
                        if (serialModemInfo != null && serialModemInfo.getModemName().equals(interfaceName)) {
                            type = NetInterfaceType.MODEM;
                        }
                    }
                }
                s_logger.debug("Getting config for {} type: {}", interfaceName, type);
                switch(type) {
                    case LOOPBACK:
                        LoopbackInterface<? extends NetInterfaceAddress> activeLoopInterface = (LoopbackInterface<? extends NetInterfaceAddress>) netInterface;
                        LoopbackInterfaceConfigImpl loopbackInterfaceConfig = null;
                        loopbackInterfaceConfig = new LoopbackInterfaceConfigImpl(activeLoopInterface);
                        networkConfiguration.addNetInterfaceConfig(loopbackInterfaceConfig);
                        break;
                    case ETHERNET:
                        EthernetInterface<? extends NetInterfaceAddress> activeEthInterface = (EthernetInterface<? extends NetInterfaceAddress>) netInterface;
                        EthernetInterfaceConfigImpl ethernetInterfaceConfig = null;
                        ethernetInterfaceConfig = new EthernetInterfaceConfigImpl(activeEthInterface);
                        networkConfiguration.addNetInterfaceConfig(ethernetInterfaceConfig);
                        break;
                    case WIFI:
                        WifiInterfaceImpl<? extends NetInterfaceAddress> activeWifiInterface = (WifiInterfaceImpl<? extends NetInterfaceAddress>) netInterface;
                        WifiInterfaceConfigImpl wifiInterfaceConfig = null;
                        wifiInterfaceConfig = new WifiInterfaceConfigImpl(activeWifiInterface);
                        networkConfiguration.addNetInterfaceConfig(wifiInterfaceConfig);
                        break;
                    case MODEM:
                        ModemInterfaceImpl<? extends NetInterfaceAddress> activeModemInterface = (ModemInterfaceImpl<? extends NetInterfaceAddress>) netInterface;
                        addPropertiesInModemInterface(activeModemInterface);
                        ModemInterfaceConfigImpl modemInterfaceConfig = null;
                        modemInterfaceConfig = new ModemInterfaceConfigImpl(activeModemInterface);
                        networkConfiguration.addNetInterfaceConfig(modemInterfaceConfig);
                        break;
                    case UNKNOWN:
                        s_logger.debug("Found interface of unknown type in current configuration: {}. Ignoring it.", interfaceName);
                        break;
                    default:
                        s_logger.debug("Unsupported type: {} - not adding to configuration. Ignoring it.", type);
                }
            } catch (Exception e) {
                s_logger.warn("Error fetching information for network interface: {}", interfaceName, e);
            }
        }
    }
    // populate the NetInterfaceConfigs
    for (NetworkConfigurationVisitor visitor : this.m_readVisitors) {
        networkConfiguration.accept(visitor);
    }
    return networkConfiguration;
}
Also used : HashMap(java.util.HashMap) SupportedSerialModemInfo(org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo) WifiInterfaceImpl(org.eclipse.kura.core.net.WifiInterfaceImpl) KuraException(org.eclipse.kura.KuraException) ModemInterfaceImpl(org.eclipse.kura.core.net.modem.ModemInterfaceImpl) WifiInterfaceConfigImpl(org.eclipse.kura.core.net.WifiInterfaceConfigImpl) EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) LoopbackInterface(org.eclipse.kura.net.LoopbackInterface) ModemInterfaceConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration) NetworkConfigurationVisitor(org.eclipse.kura.core.net.NetworkConfigurationVisitor) LoopbackInterfaceConfigImpl(org.eclipse.kura.core.net.LoopbackInterfaceConfigImpl) EthernetInterface(org.eclipse.kura.net.EthernetInterface)

Example 10 with NetInterfaceAddress

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

the class EmulatedNetworkServiceImpl method getActiveNetworkInterfaces.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public List<NetInterface<? extends NetInterfaceAddress>> getActiveNetworkInterfaces() 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()) {
            try {
                jnInterface = jnInterfaces.nextElement();
                if (jnInterface.isUp() && !jnInterface.isVirtual() && !jnInterface.isLoopback() && !jnInterface.isPointToPoint() && jnInterface.getHardwareAddress() != null && !jnInterface.getInterfaceAddresses().isEmpty()) {
                    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);
                    }
                    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 (SocketException se) {
                s_logger.warn("Exception while getting information for interface " + jnInterface.getName(), se);
            }
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    return interfaces;
}
Also used : SocketException(java.net.SocketException) 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)

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