Search in sources :

Example 31 with IP4Address

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

the class DhcpConfigReader method populateDhcpdConfig.

private DhcpServerConfig4 populateDhcpdConfig(String interfaceName, File dhcpConfigFile, Properties kuraExtendedProps) throws KuraException {
    DhcpServerConfigIP4 dhcpServerConfigIP4 = null;
    BufferedReader br = null;
    try {
        boolean enabled = false;
        IP4Address subnet = null;
        IP4Address netmask = null;
        IP4Address router = null;
        int defaultLeaseTime = -1;
        int maxLeaseTime = -1;
        IP4Address rangeStart = null;
        IP4Address rangeEnd = null;
        boolean passDns = true;
        ArrayList<IP4Address> dnsList = new ArrayList<IP4Address>();
        br = new BufferedReader(new FileReader(dhcpConfigFile));
        String line = null;
        while ((line = br.readLine()) != null) {
            // TODO - really simple for now
            StringTokenizer st = new StringTokenizer(line);
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (token.equals("#")) {
                    break;
                } else if (token.equals("subnet")) {
                    subnet = (IP4Address) IPAddress.parseHostAddress(st.nextToken());
                    if (!st.nextToken().equals("netmask")) {
                        throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "invalid dhcp config file: " + dhcpConfigFile.getAbsolutePath());
                    }
                    netmask = (IP4Address) IPAddress.parseHostAddress(st.nextToken());
                } else if (token.equals("interface")) {
                    interfaceName = st.nextToken();
                    interfaceName = interfaceName.substring(0, interfaceName.indexOf(';'));
                } else if (token.equals("ddns-update-style")) {
                    if (st.nextToken().equals("none;")) {
                        passDns = false;
                    }
                } else if (token.equals("ddns-updates")) {
                    if (st.nextToken().equals("off;")) {
                        passDns = false;
                    }
                } else if (token.equals("default-lease-time")) {
                    String leaseTime = st.nextToken();
                    defaultLeaseTime = Integer.parseInt(leaseTime.substring(0, leaseTime.indexOf(';')));
                } else if (token.equals("max-lease-time")) {
                    String leaseTime = st.nextToken();
                    maxLeaseTime = Integer.parseInt(leaseTime.substring(0, leaseTime.indexOf(';')));
                } else if (token.equals("range")) {
                    rangeStart = (IP4Address) IPAddress.parseHostAddress(st.nextToken());
                    String rangeEndString = st.nextToken();
                    rangeEndString = rangeEndString.substring(0, rangeEndString.indexOf(';'));
                    rangeEnd = (IP4Address) IPAddress.parseHostAddress(rangeEndString);
                } else if (token.equals("option")) {
                    String option = st.nextToken();
                    if (option.equals("routers")) {
                        String routerString = st.nextToken();
                        routerString = routerString.substring(0, routerString.indexOf(';'));
                        router = (IP4Address) IPAddress.parseHostAddress(routerString);
                    } else if (option.equals("domain-name-servers")) {
                        String dnsString = st.nextToken();
                        dnsString = dnsString.substring(0, dnsString.indexOf(';'));
                        dnsList.add((IP4Address) IPAddress.parseHostAddress(dnsString));
                    }
                }
            }
        }
        StringBuilder sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.dhcpServer4.enabled");
        if (kuraExtendedProps != null && kuraExtendedProps.getProperty(sb.toString()) != null) {
            enabled = Boolean.parseBoolean(kuraExtendedProps.getProperty(sb.toString()));
        } else {
            // the file is present and the flag is not - so assume enabled is true
            enabled = true;
        }
        sb = new StringBuilder().append("net.interface.").append(interfaceName).append(".config.dhcpServer4.passDns");
        if (kuraExtendedProps != null && kuraExtendedProps.getProperty(sb.toString()) != null) {
            passDns = Boolean.parseBoolean(kuraExtendedProps.getProperty(sb.toString()));
        }
        short prefix = NetworkUtil.getNetmaskShortForm(netmask.getHostAddress());
        s_logger.debug("instantiating DHCP server configuration during init with " + " | interfaceName: " + interfaceName + " | enabled: " + enabled + " | subnet: " + subnet.getHostAddress() + " | router: " + router.getHostAddress() + " | netmask: " + netmask.getHostAddress() + " | prefix: " + prefix + " | defaultLeaseTime: " + defaultLeaseTime + " | maxLeaseTime: " + maxLeaseTime + " | rangeStart: " + rangeStart.getHostAddress() + " | rangeEnd: " + rangeEnd.getHostAddress() + " | passDns: " + passDns + " | dnsList: " + dnsList.toString());
        dhcpServerConfigIP4 = new DhcpServerConfigIP4(interfaceName, enabled, subnet, router, netmask, defaultLeaseTime, maxLeaseTime, prefix, rangeStart, rangeEnd, passDns, dnsList);
    } catch (FileNotFoundException e) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e);
    } catch (IOException e) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
    }
    return dhcpServerConfigIP4;
}
Also used : DhcpServerConfigIP4(org.eclipse.kura.net.dhcp.DhcpServerConfigIP4) IP4Address(org.eclipse.kura.net.IP4Address) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) KuraException(org.eclipse.kura.KuraException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 32 with IP4Address

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

the class FirewallConfigurationServiceImpl method getFirewallConfiguration.

@Override
public FirewallConfiguration getFirewallConfiguration() throws KuraException {
    s_logger.debug("getting the firewall configuration");
    FirewallConfiguration firewallConfiguration = new FirewallConfiguration();
    LinuxFirewall firewall = LinuxFirewall.getInstance();
    Iterator<LocalRule> localRules = firewall.getLocalRules().iterator();
    while (localRules.hasNext()) {
        LocalRule localRule = localRules.next();
        if (localRule.getPortRange() != null) {
            s_logger.debug("getFirewallConfiguration() :: Adding local rule for {}", localRule.getPortRange());
            firewallConfiguration.addConfig(new FirewallOpenPortConfigIP4(localRule.getPortRange(), NetProtocol.valueOf(localRule.getProtocol()), localRule.getPermittedNetwork(), localRule.getPermittedInterfaceName(), localRule.getUnpermittedInterfaceName(), localRule.getPermittedMAC(), localRule.getSourcePortRange()));
        } else {
            s_logger.debug("getFirewallConfiguration() :: Adding local rule for {}", localRule.getPort());
            firewallConfiguration.addConfig(new FirewallOpenPortConfigIP4(localRule.getPort(), NetProtocol.valueOf(localRule.getProtocol()), localRule.getPermittedNetwork(), localRule.getPermittedInterfaceName(), localRule.getUnpermittedInterfaceName(), localRule.getPermittedMAC(), localRule.getSourcePortRange()));
        }
    }
    Iterator<PortForwardRule> portForwardRules = firewall.getPortForwardRules().iterator();
    while (portForwardRules.hasNext()) {
        PortForwardRule portForwardRule = portForwardRules.next();
        try {
            s_logger.debug("getFirewallConfiguration() :: Adding port forwarding - inbound iface is {}", portForwardRule.getInboundIface());
            firewallConfiguration.addConfig(new FirewallPortForwardConfigIP4(portForwardRule.getInboundIface(), portForwardRule.getOutboundIface(), (IP4Address) IPAddress.parseHostAddress(portForwardRule.getAddress()), NetProtocol.valueOf(portForwardRule.getProtocol()), portForwardRule.getInPort(), portForwardRule.getOutPort(), portForwardRule.isMasquerade(), new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(portForwardRule.getPermittedNetwork()), (short) portForwardRule.getPermittedNetworkMask()), portForwardRule.getPermittedMAC(), portForwardRule.getSourcePortRange()));
        } catch (UnknownHostException e) {
            e.printStackTrace();
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
    Iterator<NATRule> autoNatRules = firewall.getAutoNatRules().iterator();
    while (autoNatRules.hasNext()) {
        NATRule autoNatRule = autoNatRules.next();
        s_logger.debug("getFirewallConfiguration() :: Adding auto NAT rules {}", autoNatRule.getSourceInterface());
        firewallConfiguration.addConfig(new FirewallAutoNatConfig(autoNatRule.getSourceInterface(), autoNatRule.getDestinationInterface(), autoNatRule.isMasquerade()));
    }
    Iterator<NATRule> natRules = firewall.getNatRules().iterator();
    while (natRules.hasNext()) {
        NATRule natRule = natRules.next();
        s_logger.debug("getFirewallConfiguration() :: Adding NAT rules {}", natRule.getSourceInterface());
        firewallConfiguration.addConfig(new FirewallNatConfig(natRule.getSourceInterface(), natRule.getDestinationInterface(), natRule.getProtocol(), natRule.getSource(), natRule.getDestination(), natRule.isMasquerade()));
    }
    return firewallConfiguration;
}
Also used : UnknownHostException(java.net.UnknownHostException) FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) PortForwardRule(org.eclipse.kura.linux.net.iptables.PortForwardRule) IP4Address(org.eclipse.kura.net.IP4Address) FirewallConfiguration(org.eclipse.kura.core.net.FirewallConfiguration) LinuxFirewall(org.eclipse.kura.linux.net.iptables.LinuxFirewall) NATRule(org.eclipse.kura.linux.net.iptables.NATRule) FirewallNatConfig(org.eclipse.kura.net.firewall.FirewallNatConfig) NetworkPair(org.eclipse.kura.net.NetworkPair) KuraException(org.eclipse.kura.KuraException) FirewallPortForwardConfigIP4(org.eclipse.kura.net.firewall.FirewallPortForwardConfigIP4) FirewallOpenPortConfigIP4(org.eclipse.kura.net.firewall.FirewallOpenPortConfigIP4) LocalRule(org.eclipse.kura.linux.net.iptables.LocalRule)

Example 33 with IP4Address

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

the class NetworkConfiguration method addNetConfigIP4Properties.

private static void addNetConfigIP4Properties(NetConfigIP4 nc, String netIfConfigPrefix, Map<String, Object> properties) {
    properties.put(netIfConfigPrefix + "autoconnect", nc.isAutoConnect());
    properties.put(netIfConfigPrefix + "ip4.status", nc.getStatus().toString());
    StringBuilder sbDnsAddresses = new StringBuilder();
    if (nc.getDnsServers() != null) {
        for (IP4Address ip : nc.getDnsServers()) {
            if (sbDnsAddresses.length() != 0) {
                sbDnsAddresses.append(",");
            }
            sbDnsAddresses.append(ip.getHostAddress());
        }
    }
    properties.put(netIfConfigPrefix + "ip4.dnsServers", sbDnsAddresses.toString());
    if (nc.isDhcp()) {
        properties.put(netIfConfigPrefix + "dhcpClient4.enabled", true);
    } else {
        properties.put(netIfConfigPrefix + "dhcpClient4.enabled", false);
        if (nc.getAddress() != null) {
            properties.put(netIfConfigPrefix + "ip4.address", nc.getAddress().getHostAddress());
        } else {
            properties.put(netIfConfigPrefix + "ip4.address", "");
        }
        properties.put(netIfConfigPrefix + "ip4.prefix", nc.getNetworkPrefixLength());
        if (nc.getGateway() != null) {
            properties.put(netIfConfigPrefix + "ip4.gateway", nc.getGateway().getHostAddress());
        } else {
            properties.put(netIfConfigPrefix + "ip4.gateway", "");
        }
        StringBuilder sbWinsAddresses = new StringBuilder();
        if (nc.getWinsServers() != null) {
            for (IP4Address ip : nc.getWinsServers()) {
                if (sbWinsAddresses.length() != 0) {
                    sbWinsAddresses.append(",");
                }
                sbWinsAddresses.append(ip.getHostAddress());
            }
        }
        properties.put(netIfConfigPrefix + "winsServers", sbWinsAddresses.toString());
        StringBuilder sbDomains = new StringBuilder();
        if (nc.getDomains() != null) {
            for (String domain : nc.getDomains()) {
                if (sbDomains.length() != 0) {
                    sbDomains.append(",");
                }
                sbDomains.append(domain);
            }
        }
        properties.put(netIfConfigPrefix + "domains", sbDomains.toString());
    }
}
Also used : IP4Address(org.eclipse.kura.net.IP4Address)

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