Search in sources :

Example 11 with IPAddress

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

the class GwtNetworkServiceImpl method updateDeviceFirewallOpenPorts.

@Override
public void updateDeviceFirewallOpenPorts(GwtXSRFToken xsrfToken, List<GwtFirewallOpenPortEntry> entries) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
    List<FirewallOpenPortConfigIP<? extends IPAddress>> firewallOpenPortConfigIPs = new ArrayList<FirewallOpenPortConfigIP<? extends IPAddress>>();
    s_logger.debug("updating open ports");
    try {
        for (GwtFirewallOpenPortEntry entry : entries) {
            String network = null;
            String prefix = null;
            if (entry.getPermittedNetwork() != null) {
                String[] parts = entry.getPermittedNetwork().split("/");
                network = parts[0];
                prefix = parts[1];
            }
            FirewallOpenPortConfigIP<IP4Address> firewallOpenPortConfigIP = new FirewallOpenPortConfigIP4();
            if (entry.getPortRange() != null) {
                if (entry.getPortRange().indexOf(':') > 0) {
                    firewallOpenPortConfigIP.setPortRange(entry.getPortRange());
                } else {
                    firewallOpenPortConfigIP.setPort(Integer.parseInt(entry.getPortRange()));
                }
            }
            firewallOpenPortConfigIP.setProtocol(NetProtocol.valueOf(GwtSafeHtmlUtils.htmlEscape(entry.getProtocol())));
            if (network != null && prefix != null) {
                firewallOpenPortConfigIP.setPermittedNetwork(new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(network), Short.parseShort(prefix)));
            }
            firewallOpenPortConfigIP.setPermittedInterfaceName(GwtSafeHtmlUtils.htmlEscape(entry.getPermittedInterfaceName()));
            firewallOpenPortConfigIP.setUnpermittedInterfaceName(GwtSafeHtmlUtils.htmlEscape(entry.getUnpermittedInterfaceName()));
            firewallOpenPortConfigIP.setPermittedMac(GwtSafeHtmlUtils.htmlEscape(entry.getPermittedMAC()));
            firewallOpenPortConfigIP.setSourcePortRange(GwtSafeHtmlUtils.htmlEscape(entry.getSourcePortRange()));
            s_logger.debug("adding open port entry for {}", entry.getPortRange());
            firewallOpenPortConfigIPs.add(firewallOpenPortConfigIP);
        }
        nas.setFirewallOpenPortConfiguration(firewallOpenPortConfigIPs);
    } catch (KuraException e) {
        s_logger.warn("Exception while updating firewall open ports", e);
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    } catch (NumberFormatException e) {
        s_logger.warn("Exception while updating firewall open ports", e);
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    } catch (UnknownHostException e) {
        s_logger.warn("Exception while updating firewall open ports", e);
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : GwtFirewallOpenPortEntry(org.eclipse.kura.web.shared.model.GwtFirewallOpenPortEntry) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) UnknownHostException(java.net.UnknownHostException) IP4Address(org.eclipse.kura.net.IP4Address) ArrayList(java.util.ArrayList) FirewallOpenPortConfigIP(org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP) KuraException(org.eclipse.kura.KuraException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) NetworkAdminService(org.eclipse.kura.net.NetworkAdminService) FirewallOpenPortConfigIP4(org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4) IPAddress(org.eclipse.kura.net.IPAddress)

Example 12 with IPAddress

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

the class DnsMonitorServiceImpl method updateDnsProxyConfig.

private void updateDnsProxyConfig() {
    this.m_enabled = false;
    this.m_allowedNetworks = new HashSet<NetworkPair<IP4Address>>();
    this.m_forwarders = new HashSet<IP4Address>();
    if (this.m_networkConfiguration != null) {
        if (this.m_networkConfiguration.getNetInterfaceConfigs() != null) {
            List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = this.m_networkConfiguration.getNetInterfaceConfigs();
            for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
                if (netInterfaceConfig.getType() == NetInterfaceType.ETHERNET || netInterfaceConfig.getType() == NetInterfaceType.WIFI || netInterfaceConfig.getType() == NetInterfaceType.MODEM) {
                    try {
                        getAllowedNetworks(netInterfaceConfig);
                    } catch (KuraException e) {
                        s_logger.error("Error updating dns proxy", e);
                    }
                }
            }
        }
    }
    Set<IPAddress> dnsServers = LinuxDns.getInstance().getDnServers();
    if (dnsServers != null && !dnsServers.isEmpty()) {
        for (IPAddress dnsServer : dnsServers) {
            s_logger.debug("Found DNS Server: {}", dnsServer.getHostAddress());
            this.m_forwarders.add((IP4Address) dnsServer);
        }
    }
    try {
        LinuxNamed linuxNamed = LinuxNamed.getInstance();
        s_logger.debug("Disabling DNS proxy");
        linuxNamed.disable();
        s_logger.debug("Writing config");
        DnsServerConfigIP4 dnsServerConfigIP4 = new DnsServerConfigIP4(this.m_forwarders, this.m_allowedNetworks);
        linuxNamed.setConfig(dnsServerConfigIP4);
        if (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();
    }
}
Also used : NetInterfaceConfig(org.eclipse.kura.net.NetInterfaceConfig) IP4Address(org.eclipse.kura.net.IP4Address) DnsServerConfigIP4(org.eclipse.kura.net.dns.DnsServerConfigIP4) LinuxNamed(org.eclipse.kura.linux.net.dns.LinuxNamed) NetworkPair(org.eclipse.kura.net.NetworkPair) KuraException(org.eclipse.kura.KuraException) IPAddress(org.eclipse.kura.net.IPAddress) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig)

Example 13 with IPAddress

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

the class DnsMonitorServiceImpl method setDnsServers.

private void setDnsServers(Set<IPAddress> newServers) {
    LinuxDns linuxDns = LinuxDns.getInstance();
    Set<IPAddress> currentServers = linuxDns.getDnServers();
    if (newServers == null || newServers.isEmpty()) {
        s_logger.warn("Not Setting DNS servers to empty");
        return;
    }
    if (currentServers != null && !currentServers.isEmpty()) {
        if (!currentServers.equals(newServers)) {
            s_logger.info("Change to DNS - setting dns servers: " + newServers);
            linuxDns.setDnServers(newServers);
        } else {
            s_logger.debug("No change to DNS servers - not updating");
        }
    } else {
        s_logger.info("Current DNS servers are null - setting dns servers: " + newServers);
        linuxDns.setDnServers(newServers);
    }
}
Also used : LinuxDns(org.eclipse.kura.linux.net.dns.LinuxDns) IPAddress(org.eclipse.kura.net.IPAddress)

Example 14 with IPAddress

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

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

the class RouteFile method findRoute.

private RouteConfig findRoute(Properties props, int index) {
    RouteConfig route = null;
    IPAddress dest = null;
    IPAddress gw = null;
    IPAddress mask = null;
    if (!props.containsKey("ADDRESS" + index)) {
        return null;
    }
    try {
        dest = IPAddress.parseHostAddress((String) props.get("ADDRESS" + index));
        gw = IPAddress.parseHostAddress((String) props.get("GATEWAY" + index));
        mask = IPAddress.parseHostAddress((String) props.get("NETMASK" + index));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    if (dest != null && gw != null && mask != null) {
        if (dest instanceof IP4Address) {
            route = new RouteConfigIP4((IP4Address) dest, (IP4Address) gw, (IP4Address) mask, this.interfaceName, -1);
        } else if (dest instanceof IP6Address) {
            route = new RouteConfigIP6((IP6Address) dest, (IP6Address) gw, (IP6Address) mask, this.interfaceName, -1);
        }
    }
    return route;
}
Also used : RouteConfigIP6(org.eclipse.kura.net.route.RouteConfigIP6) UnknownHostException(java.net.UnknownHostException) IP4Address(org.eclipse.kura.net.IP4Address) RouteConfigIP4(org.eclipse.kura.net.route.RouteConfigIP4) IP6Address(org.eclipse.kura.net.IP6Address) RouteConfig(org.eclipse.kura.net.route.RouteConfig) IPAddress(org.eclipse.kura.net.IPAddress)

Aggregations

IPAddress (org.eclipse.kura.net.IPAddress)23 KuraException (org.eclipse.kura.KuraException)18 UnknownHostException (java.net.UnknownHostException)12 IOException (java.io.IOException)10 IP4Address (org.eclipse.kura.net.IP4Address)10 ArrayList (java.util.ArrayList)9 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)8 NetConfig (org.eclipse.kura.net.NetConfig)7 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)7 File (java.io.File)5 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)5 StringTokenizer (java.util.StringTokenizer)4 WifiMode (org.eclipse.kura.net.wifi.WifiMode)4 BufferedReader (java.io.BufferedReader)3 FileOutputStream (java.io.FileOutputStream)3 FileReader (java.io.FileReader)3 PrintWriter (java.io.PrintWriter)3 HashSet (java.util.HashSet)3 NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)3 NetInterfaceType (org.eclipse.kura.net.NetInterfaceType)3