Search in sources :

Example 6 with NetInterfaceStatus

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

the class PppConfigReader method getNetConfigIP4.

private static NetConfigIP4 getNetConfigIP4(String interfaceName) throws KuraException {
    NetConfigIP4 netConfigIP4 = null;
    NetInterfaceStatus netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
    StringBuilder key = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.ip4.status");
    String statusString = KuranetConfig.getProperty(key.toString());
    if (statusString != null && !statusString.isEmpty()) {
        netInterfaceStatus = NetInterfaceStatus.valueOf(statusString);
    }
    s_logger.debug("Setting NetInterfaceStatus to " + netInterfaceStatus + " for " + interfaceName);
    netConfigIP4 = new NetConfigIP4(netInterfaceStatus, true, true);
    key = new StringBuilder("net.interface.").append(interfaceName).append(".config.dnsServers");
    String dnsServersStr = KuranetConfig.getProperty(key.toString());
    List<IP4Address> dnsServersList = new ArrayList<IP4Address>();
    if (dnsServersStr != null && !dnsServersStr.isEmpty()) {
        String[] serversArr = dnsServersStr.split(PppConfigWriter.DNS_DELIM);
        for (String server : serversArr) {
            try {
                dnsServersList.add((IP4Address) IPAddress.parseHostAddress(server));
            } catch (UnknownHostException e) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
            }
        }
        netConfigIP4.setDnsServers(dnsServersList);
    }
    return netConfigIP4;
}
Also used : UnknownHostException(java.net.UnknownHostException) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) IP4Address(org.eclipse.kura.net.IP4Address) KuraException(org.eclipse.kura.KuraException) ArrayList(java.util.ArrayList) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 7 with NetInterfaceStatus

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

the class HostapdConfigWriter method writeConfig.

private void writeConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
    String interfaceName = netInterfaceConfig.getName();
    List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
    if (netInterfaceAddressConfigs != null && netInterfaceAddressConfigs.size() > 0) {
        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
            if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
                List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                NetInterfaceStatus netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
                WifiConfig apConfig = null;
                String interfaceDriver = null;
                if (netConfigs != null) {
                    for (NetConfig netConfig : netConfigs) {
                        try {
                            if (netConfig instanceof WifiConfig) {
                                if (((WifiConfig) netConfig).getMode() == WifiMode.MASTER) {
                                    s_logger.debug("Found wifiConfig with mode set to master");
                                    interfaceDriver = ((WifiConfig) netConfig).getDriver();
                                    if (interfaceDriver != null) {
                                        s_logger.debug("Writing wifiConfig: {}", netConfig);
                                        apConfig = (WifiConfig) netConfig;
                                    } else {
                                        s_logger.error("Can't generate hostapd config - no driver specified");
                                    }
                                }
                            } else if (netConfig instanceof NetConfigIP4) {
                                netInterfaceStatus = ((NetConfigIP4) netConfig).getStatus();
                            }
                        } catch (Exception e) {
                            s_logger.error("Failed to configure Hostapd");
                            throw KuraException.internalError(e);
                        }
                    }
                    if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusDisabled) {
                        s_logger.info("Network interface status for {} is disabled - not overwriting hostapd configuration file", interfaceName);
                        return;
                    }
                    if (apConfig != null) {
                        try {
                            generateHostapdConf(apConfig, interfaceName, interfaceDriver);
                        } catch (Exception e) {
                            s_logger.error("Failed to generate hostapd configuration file for {} interface", interfaceName);
                            throw KuraException.internalError(e);
                        }
                    }
                }
            }
        }
    }
}
Also used : NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) NetConfig(org.eclipse.kura.net.NetConfig) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 8 with NetInterfaceStatus

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

the class GwtNetworkServiceImpl method updateNetInterfaceConfigurations.

@Override
public void updateNetInterfaceConfigurations(GwtXSRFToken xsrfToken, GwtNetInterfaceConfig config) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
    s_logger.debug("config.getStatus(): {}", GwtSafeHtmlUtils.htmlEscape(config.getStatus()));
    String status = config.getStatus();
    boolean autoConnect = true;
    if (GwtNetIfStatus.netIPv4StatusDisabled.name().equals(status)) {
        autoConnect = false;
    }
    try {
        // Interface status
        NetInterfaceStatus netInterfaceStatus = null;
        if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusDisabled.name())) {
            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
        } else if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusEnabledLAN.name())) {
            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledLAN;
        } else if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusEnabledWAN.name())) {
            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledWAN;
        }
        // Set up configs
        List<NetConfig> netConfigs = new ArrayList<NetConfig>();
        // Initialize NetConfigIP4 object
        NetConfigIP4 netConfig4 = new NetConfigIP4(netInterfaceStatus, autoConnect);
        // build the appropriate NetConfig objects for ethernet type
        if (config.getHwTypeEnum() == GwtNetIfType.ETHERNET || config.getHwTypeEnum() == GwtNetIfType.WIFI || config.getHwTypeEnum() == GwtNetIfType.MODEM) {
            s_logger.debug("config.getConfigMode(): {}", config.getConfigMode());
            String regexp = "[\\s,;\\n\\t]+";
            if (GwtNetIfConfigMode.netIPv4ConfigModeDHCP.name().equals(config.getConfigMode())) {
                s_logger.debug("mode is DHCP");
                netConfig4.setDhcp(true);
            } else {
                s_logger.debug("mode is STATIC");
                netConfig4.setDhcp(false);
                if (config.getIpAddress() != null && !config.getIpAddress().isEmpty()) {
                    s_logger.debug("setting address: {}", config.getIpAddress());
                    netConfig4.setAddress((IP4Address) IPAddress.parseHostAddress(config.getIpAddress()));
                }
                if (config.getSubnetMask() != null && !config.getSubnetMask().isEmpty()) {
                    s_logger.debug("setting subnet mask: {}", config.getSubnetMask());
                    netConfig4.setSubnetMask((IP4Address) IPAddress.parseHostAddress(config.getSubnetMask()));
                }
                if (config.getGateway() != null && !config.getGateway().isEmpty()) {
                    s_logger.debug("setting gateway: {}", config.getGateway());
                    netConfig4.setGateway((IP4Address) IPAddress.parseHostAddress(config.getGateway()));
                }
                String[] winServersString = config.getSearchDomains().split(regexp);
                if (winServersString != null && winServersString.length > 0) {
                    IP4Address winServer;
                    List<IP4Address> dnsServers = new ArrayList<IP4Address>();
                    for (String winsEntry : winServersString) {
                        if (!winsEntry.trim().isEmpty()) {
                            s_logger.debug("setting WINs: {}", winsEntry);
                            winServer = (IP4Address) IPAddress.parseHostAddress(winsEntry);
                            dnsServers.add(winServer);
                        }
                    }
                    netConfig4.setDnsServers(dnsServers);
                }
            }
            String[] dnsServersString = config.getDnsServers().split(regexp);
            if (dnsServersString != null && dnsServersString.length > 0) {
                IP4Address dnsServer;
                List<IP4Address> dnsServers = new ArrayList<IP4Address>();
                for (String dnsEntry : dnsServersString) {
                    if (!dnsEntry.trim().isEmpty()) {
                        s_logger.debug("setting DNS: {}", dnsEntry);
                        dnsServer = (IP4Address) IPAddress.parseHostAddress(dnsEntry);
                        dnsServers.add(dnsServer);
                    }
                }
                netConfig4.setDnsServers(dnsServers);
            }
            netConfigs.add(netConfig4);
            // Set up DHCP and NAT
            if (!GwtNetIfConfigMode.netIPv4ConfigModeDHCP.name().equals(config.getConfigMode())) {
                // <--
                List<NetConfig> dhcpConfigs = getDhcpConfig(config);
                if (dhcpConfigs != null) {
                    s_logger.debug("Adding dhcp and/or nat configs to interface update config");
                    netConfigs.addAll(dhcpConfigs);
                }
            }
            if (config.getHwTypeEnum() == GwtNetIfType.ETHERNET) {
                nas.updateEthernetInterfaceConfig(GwtSafeHtmlUtils.htmlEscape(config.getName()), autoConnect, config.getHwMTU(), netConfigs);
            }
        }
        if (config.getHwTypeEnum() == GwtNetIfType.WIFI) {
            if (config instanceof GwtWifiNetInterfaceConfig) {
                GwtWifiConfig gwtWifiConfig = ((GwtWifiNetInterfaceConfig) config).getActiveWifiConfig();
                if (gwtWifiConfig != null) {
                    WifiConfig wifiConfig = getWifiConfig(gwtWifiConfig);
                    String passKey = new String(wifiConfig.getPasskey().getPassword());
                    if (passKey != null && passKey.equals(PLACEHOLDER)) {
                        List<GwtNetInterfaceConfig> result = privateFindNetInterfaceConfigurations();
                        for (GwtNetInterfaceConfig netConfig : result) {
                            if (netConfig instanceof GwtWifiNetInterfaceConfig && config.getName().equals(((GwtWifiNetInterfaceConfig) netConfig).getName())) {
                                GwtWifiNetInterfaceConfig oldWifiConfig = (GwtWifiNetInterfaceConfig) netConfig;
                                GwtWifiConfig oldGwtWifiConfig;
                                if (gwtWifiConfig.getWirelessMode().equals(GwtWifiWirelessMode.netWifiWirelessModeAccessPoint.name())) {
                                    oldGwtWifiConfig = oldWifiConfig.getAccessPointWifiConfig();
                                } else {
                                    oldGwtWifiConfig = oldWifiConfig.getStationWifiConfig();
                                }
                                if (oldGwtWifiConfig != null) {
                                    wifiConfig.setPasskey(oldGwtWifiConfig.getPassword());
                                }
                            }
                        }
                    }
                    netConfigs.add(wifiConfig);
                    nas.updateWifiInterfaceConfig(GwtSafeHtmlUtils.htmlEscape(config.getName()), autoConnect, null, netConfigs);
                }
            }
        } else if (config.getHwTypeEnum() == GwtNetIfType.MODEM) {
            if (config instanceof GwtModemInterfaceConfig) {
                GwtModemInterfaceConfig gwtModemConfig = (GwtModemInterfaceConfig) config;
                ModemConfig modemConfig = new ModemConfig();
                String serialNum = gwtModemConfig.getHwSerial();
                String modemId = gwtModemConfig.getModemId();
                int pppNum = gwtModemConfig.getPppNum();
                // modem enabled/disabled
                if (netInterfaceStatus.equals(NetInterfaceStatus.netIPv4StatusEnabledWAN)) {
                    modemConfig.setEnabled(true);
                } else {
                    modemConfig.setEnabled(false);
                }
                modemConfig.setApn(gwtModemConfig.getApn());
                modemConfig.setPppNumber(gwtModemConfig.getPppNum());
                modemConfig.setDataCompression(gwtModemConfig.getDataCompression());
                modemConfig.setDialString(gwtModemConfig.getDialString());
                modemConfig.setHeaderCompression(gwtModemConfig.getHeaderCompression());
                String passKey = new String(gwtModemConfig.getPassword());
                if (passKey != null && passKey.equals(PLACEHOLDER)) {
                    List<GwtNetInterfaceConfig> result = privateFindNetInterfaceConfigurations();
                    for (GwtNetInterfaceConfig netConfig : result) {
                        if (netConfig instanceof GwtModemInterfaceConfig) {
                            GwtModemInterfaceConfig oldModemConfig = (GwtModemInterfaceConfig) netConfig;
                            if (gwtModemConfig.getName().equals(oldModemConfig.getName())) {
                                modemConfig.setPassword(oldModemConfig.getPassword());
                            }
                        }
                    }
                } else if (passKey != null) {
                    modemConfig.setPassword(passKey);
                }
                modemConfig.setUsername(gwtModemConfig.getUsername());
                modemConfig.setResetTimeout(gwtModemConfig.getResetTimeout());
                modemConfig.setPersist(gwtModemConfig.isPersist());
                modemConfig.setMaxFail(gwtModemConfig.getMaxFail());
                modemConfig.setIdle(gwtModemConfig.getIdle());
                modemConfig.setActiveFilter(gwtModemConfig.getActiveFilter());
                modemConfig.setLcpEchoInterval(gwtModemConfig.getLcpEchoInterval());
                modemConfig.setLcpEchoFailure(gwtModemConfig.getLcpEchoFailure());
                modemConfig.setGpsEnabled(gwtModemConfig.isGpsEnabled());
                GwtModemAuthType authType = gwtModemConfig.getAuthType();
                if (authType != null) {
                    if (authType.equals(GwtModemAuthType.netModemAuthNONE)) {
                        modemConfig.setAuthType(ModemConfig.AuthType.NONE);
                    } else if (authType.equals(GwtModemAuthType.netModemAuthAUTO)) {
                        modemConfig.setAuthType(ModemConfig.AuthType.AUTO);
                    } else if (authType.equals(GwtModemAuthType.netModemAuthCHAP)) {
                        modemConfig.setAuthType(ModemConfig.AuthType.CHAP);
                    } else if (authType.equals(GwtModemAuthType.netModemAuthPAP)) {
                        modemConfig.setAuthType(ModemConfig.AuthType.PAP);
                    }
                }
                GwtModemPdpType pdpType = gwtModemConfig.getPdpType();
                if (pdpType != null) {
                    if (pdpType.equals(GwtModemPdpType.netModemPdpIP)) {
                        modemConfig.setPdpType(ModemConfig.PdpType.IP);
                    } else if (pdpType.equals(GwtModemPdpType.netModemPdpIPv6)) {
                        modemConfig.setPdpType(ModemConfig.PdpType.IPv6);
                    } else if (pdpType.equals(GwtModemPdpType.netModemPdpPPP)) {
                        modemConfig.setPdpType(ModemConfig.PdpType.PPP);
                    } else {
                        modemConfig.setPdpType(ModemConfig.PdpType.UNKNOWN);
                    }
                }
                netConfigs.add(modemConfig);
                nas.updateModemInterfaceConfig(config.getName(), serialNum, modemId, pppNum, autoConnect, -1, netConfigs);
            }
        } else {
        // TODO - more types
        }
    } catch (Exception e) {
        s_logger.warn("Failed", e);
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) GwtModemAuthType(org.eclipse.kura.web.shared.model.GwtModemAuthType) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) IP4Address(org.eclipse.kura.net.IP4Address) GwtWifiConfig(org.eclipse.kura.web.shared.model.GwtWifiConfig) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) GwtWifiConfig(org.eclipse.kura.web.shared.model.GwtWifiConfig) ArrayList(java.util.ArrayList) GwtWifiNetInterfaceConfig(org.eclipse.kura.web.shared.model.GwtWifiNetInterfaceConfig) KuraException(org.eclipse.kura.KuraException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) UnknownHostException(java.net.UnknownHostException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) GwtModemPdpType(org.eclipse.kura.web.shared.model.GwtModemPdpType) GwtNetInterfaceConfig(org.eclipse.kura.web.shared.model.GwtNetInterfaceConfig) NetConfig(org.eclipse.kura.net.NetConfig) NetworkAdminService(org.eclipse.kura.net.NetworkAdminService) List(java.util.List) ArrayList(java.util.ArrayList) GwtModemInterfaceConfig(org.eclipse.kura.web.shared.model.GwtModemInterfaceConfig)

Example 9 with NetInterfaceStatus

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

Example 10 with NetInterfaceStatus

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

the class IfcfgConfigReader method getConfig.

private void getConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig, Properties kuraExtendedProps) throws KuraException {
    String interfaceName = netInterfaceConfig.getName();
    s_logger.debug("Getting config for {}", interfaceName);
    NetInterfaceType type = netInterfaceConfig.getType();
    if (type == NetInterfaceType.ETHERNET || type == NetInterfaceType.WIFI || type == NetInterfaceType.LOOPBACK) {
        NetInterfaceStatus netInterfaceStatus = null;
        StringBuilder sb = new StringBuilder().append("net.interface.").append(netInterfaceConfig.getName()).append(".config.ip4.status");
        if (kuraExtendedProps != null && kuraExtendedProps.getProperty(sb.toString()) != null) {
            netInterfaceStatus = NetInterfaceStatus.valueOf(kuraExtendedProps.getProperty(sb.toString()));
        } else {
            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
        }
        s_logger.debug("Setting NetInterfaceStatus to " + netInterfaceStatus + " for " + netInterfaceConfig.getName());
        boolean autoConnect = false;
        // int mtu = -1; // MTU is not currently used
        boolean dhcp = false;
        IP4Address address = null;
        String ipAddress = null;
        String prefixString = null;
        String netmask = null;
        String gateway = null;
        File ifcfgFile = null;
        if (OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) || OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.BeagleBone.getImageName()) || OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName()) || OS_VERSION.equals(KuraConstants.ReliaGATE_50_21_Ubuntu.getImageName() + "_" + KuraConstants.ReliaGATE_50_21_Ubuntu.getImageVersion())) {
            ifcfgFile = new File(DEBIAN_NET_CONFIGURATION_DIRECTORY + "interfaces");
        } else {
            ifcfgFile = new File(REDHAT_NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName);
        }
        if (ifcfgFile.exists()) {
            Properties kuraProps;
            // found our match so load the properties
            if (OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion()) || OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.BeagleBone.getImageName()) || OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName()) || OS_VERSION.equals(KuraConstants.ReliaGATE_50_21_Ubuntu.getImageName() + "_" + KuraConstants.ReliaGATE_50_21_Ubuntu.getImageVersion())) {
                kuraProps = parseDebianConfigFile(ifcfgFile, interfaceName);
            } else {
                kuraProps = parseRedhatConfigFile(ifcfgFile, interfaceName);
            }
            if (kuraProps != null) {
                String onBoot = kuraProps.getProperty("ONBOOT");
                if ("yes".equals(onBoot)) {
                    s_logger.debug("Setting autoConnect to true");
                    autoConnect = true;
                } else {
                    s_logger.debug("Setting autoConnect to false");
                    autoConnect = false;
                }
                // override MTU with what is in config if it is present
                /*
                     * IAB: MTU is not currently used
                     * 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";
                }
                // actual properties
                if (netInterfaceStatus == NetInterfaceStatus.netIPv4StatusDisabled) {
                    if (autoConnect) {
                        if (defroute.equals("no")) {
                            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledLAN;
                        } else {
                            netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledWAN;
                        }
                    }
                }
                // 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) {
                    throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + ifcfgFile.toString(), e);
                }
                if (ipAddress != null && !ipAddress.isEmpty()) {
                    try {
                        address = (IP4Address) IPAddress.parseHostAddress(ipAddress);
                    } catch (UnknownHostException e) {
                        s_logger.warn("Error parsing address: " + ipAddress, e);
                    }
                }
                // make sure at least prefix or netmask is present if static
                if (autoConnect && !dhcp && prefixString == null && netmask == null) {
                    throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + ifcfgFile.toString() + " must contain NETMASK and/or PREFIX");
                }
            }
            List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
            if (netInterfaceAddressConfigs == null) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "InterfaceAddressConfig list is null");
            } else if (netInterfaceAddressConfigs.size() == 0) {
                throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "InterfaceAddressConfig list has no entries");
            }
            for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                if (netConfigs == null) {
                    netConfigs = new ArrayList<NetConfig>();
                    if (netInterfaceAddressConfig instanceof NetInterfaceAddressConfigImpl) {
                        ((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
                        if (dhcp) {
                            // Replace with DNS provided by DHCP server
                            // (displayed as read-only in Denali)
                            List<? extends IPAddress> dhcpDnsServers = getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress());
                            if (dhcpDnsServers != null) {
                                ((NetInterfaceAddressConfigImpl) netInterfaceAddressConfig).setDnsServers(dhcpDnsServers);
                            }
                        }
                    } else if (netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
                        ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setNetConfigs(netConfigs);
                        if (dhcp) {
                            // Replace with DNS provided by DHCP server
                            // (displayed as read-only in Denali)
                            List<? extends IPAddress> dhcpDnsServers = getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress());
                            if (dhcpDnsServers != null) {
                                ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).setDnsServers(dhcpDnsServers);
                            }
                        }
                    }
                }
                NetConfigIP4 netConfig = new NetConfigIP4(netInterfaceStatus, autoConnect);
                setNetConfigIP4(netConfig, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
                s_logger.debug("NetConfig: {}", netConfig);
                netConfigs.add(netConfig);
            }
        }
    }
}
Also used : NetInterfaceAddressConfigImpl(org.eclipse.kura.core.net.NetInterfaceAddressConfigImpl) UnknownHostException(java.net.UnknownHostException) NetInterfaceStatus(org.eclipse.kura.net.NetInterfaceStatus) IP4Address(org.eclipse.kura.net.IP4Address) Properties(java.util.Properties) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetInterfaceType(org.eclipse.kura.net.NetInterfaceType) KuraException(org.eclipse.kura.KuraException) NetConfig(org.eclipse.kura.net.NetConfig) ArrayList(java.util.ArrayList) List(java.util.List) IPAddress(org.eclipse.kura.net.IPAddress) File(java.io.File) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) WifiInterfaceAddressConfigImpl(org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)

Aggregations

NetInterfaceStatus (org.eclipse.kura.net.NetInterfaceStatus)14 NetConfig (org.eclipse.kura.net.NetConfig)12 KuraException (org.eclipse.kura.KuraException)11 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)11 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)7 IOException (java.io.IOException)5 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 IP4Address (org.eclipse.kura.net.IP4Address)4 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)4 WifiMode (org.eclipse.kura.net.wifi.WifiMode)4 HashMap (java.util.HashMap)3 WifiInterfaceAddressConfigImpl (org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl)3 EvdoCellularModem (org.eclipse.kura.net.admin.modem.EvdoCellularModem)3 HspaCellularModem (org.eclipse.kura.net.admin.modem.HspaCellularModem)3 CellularModem (org.eclipse.kura.net.modem.CellularModem)3 WifiInterfaceAddressConfig (org.eclipse.kura.net.wifi.WifiInterfaceAddressConfig)3 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2