Search in sources :

Example 1 with EthernetInterfaceConfigImpl

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

the class DhcpConfigWriter method writeKuraExtendedConfig.

private void writeKuraExtendedConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraExtendedProps) throws KuraException {
    boolean enabled = false;
    boolean passDns = false;
    List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = null;
    if (netInterfaceConfig instanceof EthernetInterfaceConfigImpl) {
        netInterfaceAddressConfigs = ((EthernetInterfaceConfigImpl) netInterfaceConfig).getNetInterfaceAddresses();
    } else if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
        netInterfaceAddressConfigs = ((WifiInterfaceConfigImpl) netInterfaceConfig).getNetInterfaceAddresses();
    } else {
        s_logger.error("not adding config for " + netInterfaceConfig.getName());
    }
    if (netInterfaceAddressConfigs != null && netInterfaceAddressConfigs.size() > 0) {
        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
            List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
            if (netConfigs != null && netConfigs.size() > 0) {
                for (int i = 0; i < netConfigs.size(); i++) {
                    NetConfig netConfig = netConfigs.get(i);
                    if (netConfig instanceof DhcpServerConfig4) {
                        enabled = ((DhcpServerConfig4) netConfig).isEnabled();
                        passDns = ((DhcpServerConfig4) netConfig).isPassDns();
                    }
                }
            }
        }
    }
    // set it all
    if (kuraExtendedProps == null) {
        s_logger.debug("kuraExtendedProps was null");
        kuraExtendedProps = new Properties();
    }
    StringBuilder sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.dhcpServer4.enabled");
    kuraExtendedProps.put(sb.toString(), Boolean.toString(enabled));
    sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.dhcpServer4.passDns");
    kuraExtendedProps.put(sb.toString(), Boolean.toString(passDns));
    // write it
    if (kuraExtendedProps != null && !kuraExtendedProps.isEmpty()) {
        try {
            KuranetConfig.storeProperties(kuraExtendedProps);
        } catch (Exception e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
}
Also used : DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) Properties(java.util.Properties) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) WifiInterfaceConfigImpl(org.eclipse.kura.core.net.WifiInterfaceConfigImpl) EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 2 with EthernetInterfaceConfigImpl

use of org.eclipse.kura.core.net.EthernetInterfaceConfigImpl 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 3 with EthernetInterfaceConfigImpl

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

the class EthernetMonitorServiceImpl method activate.

// ----------------------------------------------------------------
// 
// Activation APIs
// 
// ----------------------------------------------------------------
protected void activate(ComponentContext componentContext) {
    s_logger.debug("Activating EthernetMonitor Service...");
    Dictionary<String, String[]> d = new Hashtable<String, String[]>();
    d.put(EventConstants.EVENT_TOPIC, EVENT_TOPICS);
    componentContext.getBundleContext().registerService(EventHandler.class.getName(), this, d);
    this.m_routeService = RouteServiceImpl.getInstance();
    this.m_executor = Executors.newFixedThreadPool(2);
    // Get initial configurations
    try {
        NetworkConfiguration netConfiguration = this.m_netConfigService.getNetworkConfiguration();
        if (netConfiguration != null) {
            for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netConfiguration.getNetInterfaceConfigs()) {
                if (netInterfaceConfig instanceof EthernetInterfaceConfigImpl) {
                    s_logger.debug("Adding initial ethernet config for {}", netInterfaceConfig.getName());
                    EthernetInterfaceConfigImpl newEthernetConfig = (EthernetInterfaceConfigImpl) netInterfaceConfig;
                    this.m_networkConfiguration.put(netInterfaceConfig.getName(), newEthernetConfig);
                    this.m_newNetworkConfiguration.put(netInterfaceConfig.getName(), newEthernetConfig);
                }
            }
        }
    } catch (KuraException e) {
        s_logger.error("Could not update list of interfaces", e);
    }
    // Initialize monitors
    initializeMonitors();
    s_logger.debug("Done Activating EthernetMonitor Service...");
}
Also used : EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) Hashtable(java.util.Hashtable) KuraException(org.eclipse.kura.KuraException) EventHandler(org.osgi.service.event.EventHandler) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration)

Example 4 with EthernetInterfaceConfigImpl

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

the class EthernetMonitorServiceImpl method handleEvent.

// On a network config change event, verify the change was for ethernet and add a new ethernet config
@Override
public void handleEvent(Event event) {
    String topic = event.getTopic();
    s_logger.debug("handleEvent - topic: {}", topic);
    if (topic.equals(NetworkConfigurationChangeEvent.NETWORK_EVENT_CONFIG_CHANGE_TOPIC)) {
        NetworkConfigurationChangeEvent netConfigChangedEvent = (NetworkConfigurationChangeEvent) event;
        String[] propNames = netConfigChangedEvent.getPropertyNames();
        if (propNames != null && propNames.length > 0) {
            Map<String, Object> props = new HashMap<String, Object>();
            for (String propName : propNames) {
                Object prop = netConfigChangedEvent.getProperty(propName);
                if (prop != null) {
                    props.put(propName, prop);
                } else {
                }
            }
            try {
                NetworkConfiguration newNetworkConfig = new NetworkConfiguration(props);
                if (newNetworkConfig != null) {
                    for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : newNetworkConfig.getNetInterfaceConfigs()) {
                        if (netInterfaceConfig instanceof EthernetInterfaceConfigImpl) {
                            s_logger.debug("Adding new ethernet config for {}", netInterfaceConfig.getName());
                            EthernetInterfaceConfigImpl newEthernetConfig = (EthernetInterfaceConfigImpl) netInterfaceConfig;
                            this.m_newNetworkConfiguration.put(netInterfaceConfig.getName(), newEthernetConfig);
                            if (isEthernetEnabled(newEthernetConfig)) {
                                startMonitor(netInterfaceConfig.getName());
                            }
                        }
                    }
                }
            } catch (Exception e) {
                s_logger.warn("Error during Ethernet Monitor handle event", e);
            }
        }
    }
}
Also used : EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) NetworkConfigurationChangeEvent(org.eclipse.kura.net.admin.event.NetworkConfigurationChangeEvent) HashMap(java.util.HashMap) NetworkConfiguration(org.eclipse.kura.core.net.NetworkConfiguration) KuraException(org.eclipse.kura.KuraException)

Example 5 with EthernetInterfaceConfigImpl

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

the class EthernetMonitorServiceImpl method monitor.

private void monitor(String interfaceName) {
    synchronized (s_lock) {
        try {
            List<? extends NetInterfaceAddressConfig> new_niacs = null;
            List<? extends NetInterfaceAddressConfig> cur_niacs = null;
            InterfaceState currentInterfaceState = null;
            boolean interfaceEnabled = false;
            boolean isDhcpClient = false;
            IP4Address staticGateway = null;
            boolean dhcpServerEnabled = false;
            // IPAddress dhcpServerSubnet = null;
            // short dhcpServerPrefix = -1;
            boolean postStatusChangeEvent = false;
            EthernetInterfaceConfigImpl currentInterfaceConfig = this.m_networkConfiguration.get(interfaceName);
            EthernetInterfaceConfigImpl newInterfaceConfig = this.m_newNetworkConfiguration.get(interfaceName);
            // FIXME:MC it should be possible to refactor this under the InterfaceState to avoid dual checks
            if (!LinuxNetworkUtil.isUp(interfaceName)) {
                LinuxNetworkUtil.bringUpDeletingAddress(interfaceName);
            }
            // If a new configuration exists, compare it to the existing configuration
            if (newInterfaceConfig != null) {
                // Get all configurations for the interface
                new_niacs = newInterfaceConfig.getNetInterfaceAddresses();
                if (currentInterfaceConfig != null) {
                    cur_niacs = currentInterfaceConfig.getNetInterfaceAddresses();
                }
                if (isConfigChanged(new_niacs, cur_niacs)) {
                    s_logger.info("Found a new Ethernet network configuration for {}", interfaceName);
                    // Disable the interface to be reconfigured below
                    disableInterface(interfaceName);
                    // Set the current config to the new config
                    this.m_networkConfiguration.put(interfaceName, newInterfaceConfig);
                    currentInterfaceConfig = newInterfaceConfig;
                    // Post a status change event - not to be confusd with the Config Change that I am consuming
                    postStatusChangeEvent = true;
                }
                this.m_newNetworkConfiguration.remove(interfaceName);
            }
            // Monitor for status changes and ensure dhcp server is running when enabled
            interfaceEnabled = isEthernetEnabled(currentInterfaceConfig);
            InterfaceState prevInterfaceState = this.m_interfaceState.get(interfaceName);
            // FIXME:MC Deprecate this constructor and prefer the one with the explicit parameters
            // (String interfaceName, boolean up, boolean link, IPAddress ipAddress)
            // It will save a call to determine the iface type and it will keep InterfaceState
            // as a state object as it should be. Maybe introduce an InterfaceStateBuilder.
            currentInterfaceState = new InterfaceState(NetInterfaceType.ETHERNET, interfaceName);
            if (!currentInterfaceState.equals(prevInterfaceState)) {
                postStatusChangeEvent = true;
            }
            // Find if DHCP server or DHCP client mode is enabled
            if (currentInterfaceConfig != null) {
                NetInterfaceStatus netInterfaceStatus = getStatus(currentInterfaceConfig);
                cur_niacs = currentInterfaceConfig.getNetInterfaceAddresses();
                if (cur_niacs != null && cur_niacs.size() > 0) {
                    for (NetInterfaceAddressConfig niac : cur_niacs) {
                        List<NetConfig> netConfigs = niac.getConfigs();
                        if (netConfigs != null && netConfigs.size() > 0) {
                            for (NetConfig netConfig : netConfigs) {
                                if (netConfig instanceof DhcpServerConfig4) {
                                    // only enable if Enabled for LAN
                                    if (netInterfaceStatus.equals(NetInterfaceStatus.netIPv4StatusEnabledLAN)) {
                                        dhcpServerEnabled = ((DhcpServerConfig4) netConfig).isEnabled();
                                    // dhcpServerSubnet = ((DhcpServerConfig4) netConfig).getSubnet();
                                    // dhcpServerPrefix = ((DhcpServerConfig4) netConfig).getPrefix();
                                    } else {
                                        s_logger.trace("Not enabling DHCP server for {} since it is set to {}", interfaceName, netInterfaceStatus);
                                    }
                                } else if (netConfig instanceof NetConfigIP4) {
                                    isDhcpClient = ((NetConfigIP4) netConfig).isDhcp();
                                    staticGateway = ((NetConfigIP4) netConfig).getGateway();
                                }
                            }
                        }
                    }
                } else {
                    s_logger.debug("No current net interface addresses for {}", interfaceName);
                }
            } else {
                s_logger.debug("Current interface config is null for {}", interfaceName);
            }
            // Enable/disable based on configuration and current status
            boolean interfaceStateChanged = false;
            if (interfaceEnabled) {
                if (currentInterfaceState.isUp()) {
                    if (!currentInterfaceState.isLinkUp()) {
                        s_logger.debug("link is down - disabling {}", interfaceName);
                        disableInterface(interfaceName);
                        interfaceStateChanged = true;
                    }
                } else {
                    // State is currently down
                    if (currentInterfaceState.isLinkUp()) {
                        s_logger.debug("link is up - enabling {}", interfaceName);
                        this.m_netAdminService.enableInterface(interfaceName, isDhcpClient);
                        interfaceStateChanged = true;
                    }
                }
            } else {
                if (currentInterfaceState.isUp()) {
                    s_logger.debug("{} is currently up - disable interface", interfaceName);
                    disableInterface(interfaceName);
                    interfaceStateChanged = true;
                }
            }
            // FIXME: reload the configuration IFF one of above enable/disable happened
            if (interfaceStateChanged) {
                currentInterfaceState = new InterfaceState(NetInterfaceType.ETHERNET, interfaceName);
            }
            // Manage the DHCP server and validate routes
            if (currentInterfaceState != null && currentInterfaceState.isUp() && currentInterfaceState.isLinkUp()) {
                NetInterfaceStatus netInterfaceStatus = getStatus(currentInterfaceConfig);
                if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusEnabledWAN) {
                    // This should be the default gateway - make sure it is
                    boolean found = false;
                    RouteConfig[] routes = this.m_routeService.getRoutes();
                    if (routes != null && routes.length > 0) {
                        for (RouteConfig route : routes) {
                            if (route.getInterfaceName().equals(interfaceName) && route.getDestination().equals(IPAddress.parseHostAddress("0.0.0.0")) && !route.getGateway().equals(IPAddress.parseHostAddress("0.0.0.0"))) {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found) {
                        if (isDhcpClient || staticGateway != null) {
                            // disable the interface and reenable - something didn't happen at initialization as it
                            // was supposed to
                            s_logger.error("WAN interface " + interfaceName + " did not have a route setting it as the default gateway, restarting it");
                            this.m_netAdminService.disableInterface(interfaceName);
                            this.m_netAdminService.enableInterface(interfaceName, isDhcpClient);
                        }
                    }
                } else if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusEnabledLAN) {
                    if (isDhcpClient) {
                        RouteService rs = RouteServiceImpl.getInstance();
                        RouteConfig rconf = rs.getDefaultRoute(interfaceName);
                        if (rconf != null) {
                            s_logger.debug("{} is configured for LAN/DHCP - removing GATEWAY route ...", rconf.getInterfaceName());
                            rs.removeStaticRoute(rconf.getDestination(), rconf.getGateway(), rconf.getNetmask(), rconf.getInterfaceName());
                        }
                    }
                }
                if (dhcpServerEnabled && !DhcpServerManager.isRunning(interfaceName)) {
                    s_logger.debug("Starting DHCP server for {}", interfaceName);
                    this.m_netAdminService.manageDhcpServer(interfaceName, true);
                }
            } else if (DhcpServerManager.isRunning(interfaceName)) {
                s_logger.debug("Stopping DHCP server for {}", interfaceName);
                this.m_netAdminService.manageDhcpServer(interfaceName, false);
            }
            // post event if there were any changes
            if (postStatusChangeEvent) {
                s_logger.debug("Posting NetworkStatusChangeEvent for {}: {}", interfaceName, currentInterfaceState);
                this.m_eventAdmin.postEvent(new NetworkStatusChangeEvent(interfaceName, currentInterfaceState, null));
                this.m_interfaceState.put(interfaceName, currentInterfaceState);
            }
            // If the interface is disabled in Denali, stop the monitor
            if (!interfaceEnabled) {
                s_logger.debug("{} is disabled - stopping monitor", interfaceName);
                stopMonitor(interfaceName);
            }
        } catch (Exception e) {
            s_logger.warn("Error during Ethernet Monitor", e);
        }
    }
}
Also used : IP4Address(org.eclipse.kura.net.IP4Address) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) RouteConfig(org.eclipse.kura.net.route.RouteConfig) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) KuraException(org.eclipse.kura.KuraException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) EthernetInterfaceConfigImpl(org.eclipse.kura.core.net.EthernetInterfaceConfigImpl) NetworkStatusChangeEvent(org.eclipse.kura.net.admin.event.NetworkStatusChangeEvent) NetConfig(org.eclipse.kura.net.NetConfig) RouteService(org.eclipse.kura.linux.net.route.RouteService) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Aggregations

KuraException (org.eclipse.kura.KuraException)6 EthernetInterfaceConfigImpl (org.eclipse.kura.core.net.EthernetInterfaceConfigImpl)6 NetworkConfiguration (org.eclipse.kura.core.net.NetworkConfiguration)3 WifiInterfaceConfigImpl (org.eclipse.kura.core.net.WifiInterfaceConfigImpl)3 NetConfig (org.eclipse.kura.net.NetConfig)3 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)3 DhcpServerConfig4 (org.eclipse.kura.net.dhcp.DhcpServerConfig4)3 HashMap (java.util.HashMap)2 LoopbackInterfaceConfigImpl (org.eclipse.kura.core.net.LoopbackInterfaceConfigImpl)2 ModemInterfaceConfigImpl (org.eclipse.kura.core.net.modem.ModemInterfaceConfigImpl)2 IP4Address (org.eclipse.kura.net.IP4Address)2 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)2 NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Properties (java.util.Properties)1 LoopbackInterfaceImpl (org.eclipse.kura.core.net.LoopbackInterfaceImpl)1