Search in sources :

Example 16 with IP4Address

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

the class DnsMonitorServiceImpl method activate.

protected void activate(ComponentContext componentContext) {
    s_logger.debug("Activating DnsProxyMonitor Service...");
    Dictionary<String, String[]> d = new Hashtable<String, String[]>();
    d.put(EventConstants.EVENT_TOPIC, EVENT_TOPICS);
    componentContext.getBundleContext().registerService(EventHandler.class.getName(), this, d);
    try {
        this.m_networkConfiguration = this.m_netConfigService.getNetworkConfiguration();
    } catch (KuraException e) {
        s_logger.error("Could not get initial network configuration", e);
    }
    stopThread = new AtomicBoolean();
    // FIXME - brute force handler for DNS updates
    // m_executorUtil = ExecutorUtil.getInstance();
    this.m_executor = Executors.newSingleThreadExecutor();
    stopThread.set(false);
    s_monitorTask = this.m_executor.submit(new Runnable() {

        @Override
        public void run() {
            while (!stopThread.get()) {
                Thread.currentThread().setName("DnsMonitorServiceImpl");
                Set<IPAddress> dnsServers = LinuxDns.getInstance().getDnServers();
                // Check that resolv.conf matches what is configured
                Set<IPAddress> configuredServers = getConfiguredDnsServers();
                if (!configuredServers.equals(dnsServers)) {
                    setDnsServers(configuredServers);
                    dnsServers = configuredServers;
                }
                Set<IP4Address> forwarders = new HashSet<IP4Address>();
                if (dnsServers != null && !dnsServers.isEmpty()) {
                    for (IPAddress dnsServer : dnsServers) {
                        s_logger.debug("Found DNS Server: {}", dnsServer.getHostAddress());
                        forwarders.add((IP4Address) dnsServer);
                    }
                }
                if (forwarders != null && !forwarders.isEmpty()) {
                    if (!forwarders.equals(DnsMonitorServiceImpl.this.m_forwarders)) {
                        // there was a change - deal with it
                        s_logger.info("Detected DNS resolv.conf change - restarting DNS proxy");
                        DnsMonitorServiceImpl.this.m_forwarders = forwarders;
                        try {
                            LinuxNamed linuxNamed = LinuxNamed.getInstance();
                            DnsServerConfigIP4 currentDnsServerConfig = linuxNamed.getDnsServerConfig();
                            DnsServerConfigIP4 newDnsServerConfig = new DnsServerConfigIP4(DnsMonitorServiceImpl.this.m_forwarders, DnsMonitorServiceImpl.this.m_allowedNetworks);
                            if (currentDnsServerConfig.equals(newDnsServerConfig)) {
                                s_logger.debug("DNS server config has changed - updating from " + currentDnsServerConfig + " to " + newDnsServerConfig);
                                s_logger.debug("Disabling DNS proxy");
                                linuxNamed.disable();
                                s_logger.debug("Writing config");
                                linuxNamed.setConfig(newDnsServerConfig);
                                if (DnsMonitorServiceImpl.this.m_enabled) {
                                    sleep(500);
                                    s_logger.debug("Starting DNS proxy");
                                    linuxNamed.enable();
                                } else {
                                    s_logger.debug("DNS proxy not enabled");
                                }
                            }
                        } catch (KuraException e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    monitorWait();
                } catch (InterruptedException e) {
                    s_logger.debug("DNS monitor interrupted", e);
                }
            }
        }
    });
}
Also used : Hashtable(java.util.Hashtable) IP4Address(org.eclipse.kura.net.IP4Address) EventHandler(org.osgi.service.event.EventHandler) DnsServerConfigIP4(org.eclipse.kura.net.dns.DnsServerConfigIP4) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LinuxNamed(org.eclipse.kura.linux.net.dns.LinuxNamed) KuraException(org.eclipse.kura.KuraException) IPAddress(org.eclipse.kura.net.IPAddress) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 17 with IP4Address

use of org.eclipse.kura.net.IP4Address 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 18 with IP4Address

use of org.eclipse.kura.net.IP4Address 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 19 with IP4Address

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

the class GenericNetworkInterface method setNetConfigIP4.

/**
 * Populate a NetConfigIP4 object using the given values
 *
 * @param netConfig
 * @param status
 * @param autoConnect
 * @param dhcp
 * @param address
 * @param gateway
 * @param prefixString
 * @param netmask
 * @param kuraProps
 * @throws UnknownHostException
 * @throws KuraException
 */
private static void setNetConfigIP4(NetConfigIP4 netConfig, NetInterfaceStatus status, boolean autoConnect, boolean dhcp, IP4Address address, String gateway, String prefixString, String netmask, Properties kuraProps) throws UnknownHostException, KuraException {
    if (status != null) {
        netConfig.setStatus(status);
    } else {
        // FIXME - make a best guess??
        if (autoConnect) {
            if (dhcp) {
                netConfig.setStatus(NetInterfaceStatus.netIPv4StatusEnabledWAN);
            } else {
                if (gateway != null) {
                    netConfig.setStatus(NetInterfaceStatus.netIPv4StatusEnabledWAN);
                } else {
                    netConfig.setStatus(NetInterfaceStatus.netIPv4StatusEnabledLAN);
                }
            }
        } else {
            netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
        }
    }
    netConfig.setDhcp(dhcp);
    if (kuraProps != null) {
        // get the DNS
        List<IP4Address> dnsServers = new ArrayList<IP4Address>();
        int count = 1;
        while (true) {
            String dns = null;
            if ((dns = kuraProps.getProperty("DNS" + count)) != null) {
                dnsServers.add((IP4Address) IPAddress.parseHostAddress(dns));
                count++;
            } else {
                break;
            }
        }
        netConfig.setDnsServers(dnsServers);
        if (!dhcp) {
            netConfig.setAddress(address);
            // TODO ((NetConfigIP4)netConfig).setDomains(domains);
            if (gateway != null && !gateway.isEmpty()) {
                netConfig.setGateway((IP4Address) IPAddress.parseHostAddress(gateway));
            }
            if (prefixString != null) {
                short prefix = Short.parseShort(prefixString);
                netConfig.setNetworkPrefixLength(prefix);
            }
            if (netmask != null) {
                netConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(netmask));
            }
        // TODO netConfig.setWinsServers(winsServers);
        }
    }
}
Also used : IP4Address(org.eclipse.kura.net.IP4Address) ArrayList(java.util.ArrayList) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4)

Example 20 with IP4Address

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

the class ConnectionInfoImpl method getGateway.

/**
 * Gets the gateway address associated with this interface
 *
 * @return A IP4Address representing the gateway if it is not null
 */
@Override
public IP4Address getGateway() {
    IP4Address gateway = null;
    String sGateway = this.m_props.getProperty("GATEWAY");
    if (sGateway != null) {
        try {
            gateway = (IP4Address) IPAddress.parseHostAddress(sGateway);
        } catch (Exception e) {
            s_logger.error("Error parsing gateway address!", e);
        }
    }
    return gateway;
}
Also used : IP4Address(org.eclipse.kura.net.IP4Address) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException)

Aggregations

IP4Address (org.eclipse.kura.net.IP4Address)33 KuraException (org.eclipse.kura.KuraException)20 ArrayList (java.util.ArrayList)15 IOException (java.io.IOException)12 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)11 IPAddress (org.eclipse.kura.net.IPAddress)10 NetConfig (org.eclipse.kura.net.NetConfig)10 UnknownHostException (java.net.UnknownHostException)9 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)9 IP6Address (org.eclipse.kura.net.IP6Address)8 RouteConfig (org.eclipse.kura.net.route.RouteConfig)7 RouteConfigIP4 (org.eclipse.kura.net.route.RouteConfigIP4)6 RouteConfigIP6 (org.eclipse.kura.net.route.RouteConfigIP6)6 FileNotFoundException (java.io.FileNotFoundException)5 List (java.util.List)5 StringTokenizer (java.util.StringTokenizer)5 DhcpServerConfigIP4 (org.eclipse.kura.net.dhcp.DhcpServerConfigIP4)5 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)5 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4