Search in sources :

Example 1 with LoopbackInterfaceImpl

use of org.eclipse.kura.core.net.LoopbackInterfaceImpl in project kura by eclipse.

the class GenericNetworkInterface method getCurrentConfig.

protected static NetInterfaceConfig<?> getCurrentConfig(String interfaceName, NetInterfaceType type, NetInterfaceStatus status, boolean dhcpServerEnabled, boolean passDns, Properties kuraProps) throws KuraException {
    try {
        NetInterfaceConfig<?> netInterfaceConfig = null;
        boolean autoConnect = false;
        int mtu = -1;
        boolean dhcp = false;
        IP4Address address = null;
        String ipAddress = null;
        String prefixString = null;
        String netmask = null;
        String gateway = null;
        boolean interfaceEnabled = false;
        if (kuraProps != null) {
            String onBoot = kuraProps.getProperty("ONBOOT");
            if ("yes".equals(onBoot)) {
                autoConnect = true;
                // we are enabled - just not sure if for LAN or WAN
                if (status == NetInterfaceStatus.netIPv4StatusUnknown) {
                    interfaceEnabled = true;
                }
            } else {
                autoConnect = false;
            }
            // override MTU with what is in config if it is present
            String stringMtu = kuraProps.getProperty("MTU");
            if (stringMtu == null) {
                try {
                    mtu = LinuxNetworkUtil.getCurrentMtu(interfaceName);
                } catch (KuraException e) {
                    // just assume ???
                    if (interfaceName.equals("lo")) {
                        mtu = 16436;
                    } else {
                        mtu = 1500;
                    }
                }
            } else {
                mtu = Short.parseShort(stringMtu);
            }
            // get the bootproto
            String bootproto = kuraProps.getProperty("BOOTPROTO");
            if (bootproto == null) {
                bootproto = "static";
            }
            // get the defroute
            String defroute = kuraProps.getProperty("DEFROUTE");
            if (defroute == null) {
                defroute = "no";
            }
            if (interfaceEnabled) {
                if (defroute.equals("yes")) {
                    status = NetInterfaceStatus.netIPv4StatusEnabledWAN;
                } else {
                    status = NetInterfaceStatus.netIPv4StatusEnabledLAN;
                }
            }
            // check for dhcp or static configuration
            try {
                ipAddress = kuraProps.getProperty("IPADDR");
                prefixString = kuraProps.getProperty("PREFIX");
                netmask = kuraProps.getProperty("NETMASK");
                kuraProps.getProperty("BROADCAST");
                try {
                    gateway = kuraProps.getProperty("GATEWAY");
                    s_logger.debug("got gateway for {}: {}", interfaceName, gateway);
                } catch (Exception e) {
                    s_logger.warn("missing gateway stanza for " + interfaceName);
                }
                if (bootproto.equals("dhcp")) {
                    s_logger.debug("currently set for DHCP");
                    dhcp = true;
                    ipAddress = null;
                    netmask = null;
                } else {
                    s_logger.debug("currently set for static address");
                    dhcp = false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName);
            }
            if (ipAddress != null && !ipAddress.isEmpty()) {
                address = (IP4Address) IPAddress.parseHostAddress(ipAddress);
            }
            // make sure at least prefix or netmask is present if static
            if (!dhcp && prefixString == null && netmask == null) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName + " must contain NETMASK and/or PREFIX");
            }
        }
        ConnectionInfo conInfo = new ConnectionInfoImpl(interfaceName);
        LinuxDns dnsService = LinuxDns.getInstance();
        // note - we only add the fields we need/care about from a configuration standpoint
        if (type == NetInterfaceType.LOOPBACK) {
            s_logger.debug("Adding a Loopback interface");
            netInterfaceConfig = new LoopbackInterfaceConfigImpl(interfaceName);
            ((LoopbackInterfaceImpl<?>) netInterfaceConfig).setMTU(mtu);
            // loopback autoConnect
            ((LoopbackInterfaceImpl<?>) netInterfaceConfig).setAutoConnect(true);
            // should always be true?
            ((LoopbackInterfaceImpl<?>) netInterfaceConfig).setLoopback(true);
            List<NetInterfaceAddressConfig> netInterfaceAddressConfigs = new ArrayList<NetInterfaceAddressConfig>();
            List<NetInterfaceAddress> netInterfaceAddresses = new ArrayList<NetInterfaceAddress>();
            NetInterfaceAddressConfigImpl netInterfaceAddressConfig = new NetInterfaceAddressConfigImpl();
            netInterfaceAddressConfigs.add(netInterfaceAddressConfig);
            netInterfaceAddresses.add(netInterfaceAddressConfig);
            LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
            if (ifconfig != null && ifconfig.isUp()) {
                netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
                netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
                netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
                netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
                netInterfaceAddressConfig.setGateway(conInfo.getGateway());
                if (dhcp) {
                    netInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress()));
                } else {
                    netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
                }
            }
            ((LoopbackInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(netInterfaceAddressConfigs);
            List<NetConfig> netConfigs = new ArrayList<NetConfig>();
            netInterfaceAddressConfig.setNetConfigs(netConfigs);
            // FIXME - hardcoded
            NetConfig netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusEnabledLAN, true);
            ((NetConfigIP4) netConfig).setAddress(address);
            ((NetConfigIP4) netConfig).setDhcp(dhcp);
            ((NetConfigIP4) netConfig).setDnsServers(null);
            ((NetConfigIP4) netConfig).setDomains(null);
            ((NetConfigIP4) netConfig).setGateway(null);
            ((NetConfigIP4) netConfig).setNetworkPrefixLength((short) 8);
            ((NetConfigIP4) netConfig).setSubnetMask((IP4Address) IPAddress.parseHostAddress("255.0.0.0"));
            ((NetConfigIP4) netConfig).setWinsServers(null);
            netConfigs.add(netConfig);
        } else if (type == NetInterfaceType.ETHERNET) {
            s_logger.debug("Adding an Ethernet interface - {}", interfaceName);
            netInterfaceConfig = new EthernetInterfaceConfigImpl(interfaceName);
            ((EthernetInterfaceImpl<?>) netInterfaceConfig).setMTU(mtu);
            ((EthernetInterfaceImpl<?>) netInterfaceConfig).setAutoConnect(autoConnect);
            ((EthernetInterfaceImpl<?>) netInterfaceConfig).setLoopback(false);
            List<NetInterfaceAddressConfig> netInterfaceAddressConfigs = new ArrayList<NetInterfaceAddressConfig>();
            List<NetInterfaceAddress> netInterfaceAddresses = new ArrayList<NetInterfaceAddress>();
            NetInterfaceAddressConfigImpl netInterfaceAddressConfig = new NetInterfaceAddressConfigImpl();
            netInterfaceAddressConfigs.add(netInterfaceAddressConfig);
            netInterfaceAddresses.add(netInterfaceAddressConfig);
            LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
            if (ifconfig != null) {
                ((EthernetInterfaceImpl<?>) netInterfaceConfig).setHardwareAddress(ifconfig.getMacAddressBytes());
                if (ifconfig.isUp()) {
                    try {
                        netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
                        netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
                        netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
                        netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
                        netInterfaceAddressConfig.setGateway(conInfo.getGateway());
                        if (dhcp) {
                            netInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress()));
                        } else {
                            netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
                        }
                    } catch (KuraException e) {
                        s_logger.warn("The interface went down " + interfaceName + " not including current state in status because it is not up");
                        netInterfaceAddressConfig.setAddress(null);
                        netInterfaceAddressConfig.setBroadcast(null);
                        netInterfaceAddressConfig.setNetmask(null);
                        netInterfaceAddressConfig.setNetworkPrefixLength((short) -1);
                        netInterfaceAddressConfig.setGateway(null);
                        netInterfaceAddressConfig.setDnsServers(null);
                    }
                }
            }
            ((EthernetInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(netInterfaceAddressConfigs);
            // add the config
            List<NetConfig> netConfigs = new ArrayList<NetConfig>();
            netInterfaceAddressConfig.setNetConfigs(netConfigs);
            NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
            setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
            netConfigs.add(netConfig);
            if (dhcpServerEnabled) {
                // add DHCP server configuration to the list
                DhcpServerImpl dhcpServer = DhcpServerFactory.getInstance(interfaceName, dhcpServerEnabled, passDns);
                DhcpServerConfig4 dhcpServerConfig = dhcpServer.getDhcpServerConfig(dhcpServerEnabled, passDns);
                if (dhcpServerConfig != null) {
                    netConfigs.add(dhcpServerConfig);
                }
            }
        } else if (type == NetInterfaceType.WIFI) {
            s_logger.debug("Adding a Wireless interface - {}", interfaceName);
            WifiInterfaceConfigImpl wifiInterfaceConfig = new WifiInterfaceConfigImpl(interfaceName);
            netInterfaceConfig = wifiInterfaceConfig;
            wifiInterfaceConfig.setMTU(mtu);
            wifiInterfaceConfig.setAutoConnect(autoConnect);
            wifiInterfaceConfig.setLoopback(false);
            List<WifiInterfaceAddressConfig> wifiInterfaceAddressConfigs = new ArrayList<WifiInterfaceAddressConfig>();
            List<WifiInterfaceAddress> wifiInterfaceAddresses = new ArrayList<WifiInterfaceAddress>();
            WifiInterfaceAddressConfigImpl wifiInterfaceAddressConfig = new WifiInterfaceAddressConfigImpl();
            wifiInterfaceAddressConfigs.add(wifiInterfaceAddressConfig);
            wifiInterfaceAddresses.add(wifiInterfaceAddressConfig);
            String currentSSID = LinuxNetworkUtil.getSSID(interfaceName);
            LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
            if (ifconfig != null) {
                wifiInterfaceConfig.setHardwareAddress(ifconfig.getMacAddressBytes());
                if (ifconfig.isUp()) {
                    wifiInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
                    wifiInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
                    String currentNetmask = ifconfig.getInetMask();
                    if (currentNetmask != null) {
                        wifiInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(currentNetmask));
                        wifiInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(currentNetmask));
                    }
                    wifiInterfaceAddressConfig.setBitrate(LinuxNetworkUtil.getWifiBitrate(interfaceName));
                    wifiInterfaceAddressConfig.setGateway(conInfo.getGateway());
                    if (dhcp) {
                        wifiInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, wifiInterfaceAddressConfig.getAddress()));
                    } else {
                        wifiInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
                    }
                    WifiAccessPointImpl ap = null;
                    if (currentSSID != null) {
                        s_logger.debug("Adding access point SSID: {}", currentSSID);
                        ap = new WifiAccessPointImpl(currentSSID);
                        // TODO: fill in other info
                        ap.setMode(WifiMode.INFRA);
                        List<Long> bitrate = new ArrayList<Long>();
                        bitrate.add(54000000L);
                        ap.setBitrate(bitrate);
                        ap.setFrequency(12345);
                        ap.setHardwareAddress("20AA4B8A6442".getBytes());
                        ap.setRsnSecurity(EnumSet.allOf(WifiSecurity.class));
                        ap.setStrength(1234);
                        ap.setWpaSecurity(EnumSet.allOf(WifiSecurity.class));
                    }
                    wifiInterfaceAddressConfig.setWifiAccessPoint(ap);
                }
            }
            // mode
            WifiMode wifiMode = WifiMode.UNKNOWN;
            s_logger.debug("Get WifiMode...");
            try {
                // get from config file
                String mode = kuraProps.getProperty("MODE");
                if (mode != null) {
                    s_logger.debug("Getting wifi mode from {}", kuraFile.getAbsolutePath());
                    if (mode.equalsIgnoreCase("Managed")) {
                        wifiMode = WifiMode.INFRA;
                    } else if (mode.equalsIgnoreCase("Master")) {
                        wifiMode = WifiMode.MASTER;
                    } else if (mode.equalsIgnoreCase("Ad-Hoc")) {
                        wifiMode = WifiMode.ADHOC;
                    } else {
                        wifiMode = WifiMode.valueOf(mode);
                    }
                } else {
                    // get current setting using iwconfig
                    s_logger.debug("Getting wifi mode from iwconfig");
                    wifiMode = LinuxNetworkUtil.getWifiMode(interfaceName);
                }
            } catch (Exception e) {
            // leave as unknown
            }
            s_logger.debug("Current WifiMode: {}", wifiMode);
            wifiInterfaceAddressConfig.setMode(wifiMode);
            wifiInterfaceConfig.setNetInterfaceAddresses(wifiInterfaceAddressConfigs);
            // TODO: fix
            wifiInterfaceConfig.setCapabilities(EnumSet.allOf(Capability.class));
            // add the configs - one for client (managed) mode, one for access point (master) mode
            List<NetConfig> netConfigs = new ArrayList<NetConfig>();
            wifiInterfaceAddressConfig.setNetConfigs(netConfigs);
            // get the NetConfig
            NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
            setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
            netConfigs.add(netConfig);
            // get the wpa_supplicant configuration
            WifiConfig wifiClientConfig = new WifiConfig();
            setWifiClientConfig(interfaceName, wifiClientConfig, wifiMode);
            // get the hostapd configuration
            WifiConfig wifiAPConfig = new WifiConfig();
            setWifiAccessPointConfig(wifiAPConfig);
            // add WiFi configurations to the list
            netConfigs.add(wifiClientConfig);
            netConfigs.add(wifiAPConfig);
            if (dhcpServerEnabled) {
                // add DHCP server configuration to the list
                DhcpServerImpl dhcpServer = DhcpServerFactory.getInstance(interfaceName, dhcpServerEnabled, passDns);
                DhcpServerConfig4 dhcpServerConfig = dhcpServer.getDhcpServerConfig(dhcpServerEnabled, passDns);
                if (dhcpServerConfig != null) {
                    netConfigs.add(dhcpServerConfig);
                }
            }
        } else if (type == NetInterfaceType.MODEM) {
            s_logger.debug("Adding a Modem interface");
            netInterfaceConfig = new ModemInterfaceConfigImpl(interfaceName);
            ((ModemInterfaceConfigImpl) netInterfaceConfig).setMTU(mtu);
            ((ModemInterfaceConfigImpl) netInterfaceConfig).setAutoConnect(autoConnect);
            ((ModemInterfaceConfigImpl) netInterfaceConfig).setLoopback(false);
            ((ModemInterfaceConfigImpl) netInterfaceConfig).setPointToPoint(true);
            List<ModemInterfaceAddressConfig> modemInterfaceAddressConfigs = new ArrayList<ModemInterfaceAddressConfig>();
            List<ModemInterfaceAddress> netInterfaceAddresses = new ArrayList<ModemInterfaceAddress>();
            ModemInterfaceAddressConfigImpl netInterfaceAddressConfig = new ModemInterfaceAddressConfigImpl();
            modemInterfaceAddressConfigs.add(netInterfaceAddressConfig);
            netInterfaceAddresses.add(netInterfaceAddressConfig);
            LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
            if (ifconfig != null) {
                ((ModemInterfaceConfigImpl) netInterfaceConfig).setHardwareAddress(ifconfig.getMacAddressBytes());
                if (ifconfig.isUp()) {
                    netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
                    netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
                    netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
                    netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
                    netInterfaceAddressConfig.setGateway(conInfo.getGateway());
                    netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
                }
            }
            ((ModemInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(modemInterfaceAddressConfigs);
            // add the config
            List<NetConfig> netConfigs = new ArrayList<NetConfig>();
            netInterfaceAddressConfig.setNetConfigs(netConfigs);
            NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
            setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
            netConfigs.add(netConfig);
        } else {
            s_logger.warn("Unsupported Type: " + type);
        }
        return netInterfaceConfig;
    } catch (UnknownHostException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : ModemInterfaceAddressConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceAddressConfigImpl) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) ArrayList(java.util.ArrayList) WifiInterfaceAddressConfig(org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig) LinuxDns(org.eclipse.kura.linux.net.dns.LinuxDns) KuraException(org.eclipse.kura.KuraException) WifiMode(org.eclipse.kura.net.wifi.WifiMode) ModemInterfaceAddressConfig(org.eclipse.kura.net.modem.ModemInterfaceAddressConfig) ArrayList(java.util.ArrayList) List(java.util.List) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl) Capability(org.eclipse.kura.net.wifi.WifiInterface.Capability) IP4Address(org.eclipse.kura.net.IP4Address) DhcpServerImpl(org.eclipse.kura.linux.net.dhcp.DhcpServerImpl) EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) WifiAccessPointImpl(org.eclipse.kura.core.net.WifiAccessPointImpl) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetConfig(org.eclipse.kura.net.NetConfig) ConnectionInfo(org.eclipse.kura.net.ConnectionInfo) WifiInterfaceAddress(org.eclipse.kura.net.wifi.WifiInterfaceAddress) NetInterfaceAddressConfigImpl(org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) LoopbackInterfaceConfigImpl(org.eclipse.kura.core.net.LoopbackInterfaceConfigImpl) UnknownHostException(java.net.UnknownHostException) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) KuraException(org.eclipse.kura.KuraException) UnknownHostException(java.net.UnknownHostException) WifiInterfaceConfigImpl(org.eclipse.kura.core.net.WifiInterfaceConfigImpl) LoopbackInterfaceImpl(org.eclipse.kura.core.net.LoopbackInterfaceImpl) ModemInterfaceConfigImpl(org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress) ConnectionInfoImpl(org.eclipse.kura.linux.net.ConnectionInfoImpl)

Example 2 with LoopbackInterfaceImpl

use of org.eclipse.kura.core.net.LoopbackInterfaceImpl 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)

Aggregations

LoopbackInterfaceImpl (org.eclipse.kura.core.net.LoopbackInterfaceImpl)2 NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)2 WifiInterfaceAddress (org.eclipse.kura.net.wifi.WifiInterfaceAddress)2 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 KuraException (org.eclipse.kura.KuraException)1 EthernetInterfaceConfigImpl (org.eclipse.kura.core.net.EthernetInterfaceConfigImpl)1 EthernetInterfaceImpl (org.eclipse.kura.core.net.EthernetInterfaceImpl)1 LoopbackInterfaceConfigImpl (org.eclipse.kura.core.net.LoopbackInterfaceConfigImpl)1 NetInterfaceAddressConfigImpl (org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl)1 WifiAccessPointImpl (org.eclipse.kura.core.net.WifiAccessPointImpl)1 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)1 WifiInterfaceConfigImpl (org.eclipse.kura.core.net.WifiInterfaceConfigImpl)1 WifiInterfaceImpl (org.eclipse.kura.core.net.WifiInterfaceImpl)1 ModemInterfaceAddressConfigImpl (org.eclipse.kura.core.net.modem.ModemInterfaceAddressConfigImpl)1 ModemInterfaceConfigImpl (org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl)1 ConnectionInfoImpl (org.eclipse.kura.linux.net.ConnectionInfoImpl)1 DhcpServerImpl (org.eclipse.kura.linux.net.dhcp.DhcpServerImpl)1 LinuxDns (org.eclipse.kura.linux.net.dns.LinuxDns)1