Search in sources :

Example 6 with IP4Address

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

the class ConnectionInfoImpl method getDnsServers.

/**
 * Gets the DNS addresses associated with this interface
 *
 * @return A List of IP4Address objects representing the DNS of this interface. If there are none it returns an
 *         empty list.
 */
@Override
public List<IP4Address> getDnsServers() {
    List<IP4Address> lDnsServers = new ArrayList<IP4Address>(2);
    for (int i = 1; i <= 2; i++) {
        String sDns = this.m_props.getProperty("DNS" + i);
        if (sDns != null) {
            try {
                IP4Address dns = (IP4Address) IPAddress.parseHostAddress(sDns);
                lDnsServers.add(dns);
            } catch (Exception e) {
                s_logger.error("Error parsing DNS addresses!", e);
            }
        }
    }
    return lDnsServers;
}
Also used : IP4Address(org.eclipse.kura.net.IP4Address) ArrayList(java.util.ArrayList) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException)

Example 7 with IP4Address

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

the class DhcpServerTest method testSettings.

@TestTarget(targetPlatforms = { TestTarget.PLATFORM_ALL })
@Test
public void testSettings() {
    s_logger.info("Test get/set dhcp settings");
    try {
        boolean enabled = true;
        boolean passDns = true;
        int defaultLeaseTime = 13579;
        int maximumLeaseTime = 97531;
        IP4Address routerAddress = (IP4Address) IPAddress.parseHostAddress("192.168.2.1");
        IP4Address rangeFrom = (IP4Address) IPAddress.parseHostAddress("192.168.2.33");
        IP4Address rangeTo = (IP4Address) IPAddress.parseHostAddress("192.168.2.44");
        IP4Address subnetMask = (IP4Address) IPAddress.parseHostAddress("255.255.255.0");
        IP4Address subnet = (IP4Address) IPAddress.parseHostAddress(NetworkUtil.calculateNetwork(routerAddress.getHostAddress(), subnetMask.getHostAddress()));
        short prefix = NetworkUtil.getNetmaskShortForm(subnetMask.getHostAddress());
        List<IP4Address> dnsServers = new ArrayList<IP4Address>();
        dnsServers.add((IP4Address) IPAddress.parseHostAddress("8.8.8.8"));
        DhcpServerConfigIP4 dhcpServerConfig4 = new DhcpServerConfigIP4(s_testInterface, enabled, subnet, routerAddress, subnetMask, defaultLeaseTime, maximumLeaseTime, prefix, rangeFrom, rangeTo, passDns, dnsServers);
        // This assumes an existing subnet config from the previous test
        s_dhcpServer.setConfig(dhcpServerConfig4);
        s_dhcpServer.enable();
        assertEquals(dhcpServerConfig4, s_dhcpServer.getDhcpServerConfig(enabled, passDns));
    } catch (Exception e) {
        fail("testEnable failed: " + e);
    }
}
Also used : DhcpServerConfigIP4(org.eclipse.kura.net.dhcp.DhcpServerConfigIP4) IP4Address(org.eclipse.kura.net.IP4Address) ArrayList(java.util.ArrayList) TestTarget(org.eclipse.kura.test.annotation.TestTarget) Test(org.junit.Test)

Example 8 with IP4Address

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

the class DhcpServerImpl method readConfig.

private void readConfig(boolean enabled, boolean passDns) throws KuraException {
    // TODO
    File configFile = new File(this.persistentConfigFileName);
    if (configFile.exists()) {
        s_logger.debug("initing DHCP Server configuration for {}", this.m_interfaceName);
        try {
            IP4Address subnet = null;
            IP4Address netmask = null;
            IP4Address router = null;
            String interfaceName = null;
            int defaultLeaseTime = -1;
            int maxLeaseTime = -1;
            IP4Address rangeStart = null;
            IP4Address rangeEnd = null;
            ArrayList<IP4Address> dnsList = new ArrayList<IP4Address>();
            BufferedReader br = new BufferedReader(new FileReader(configFile));
            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")) {
                            br.close();
                            br = null;
                            throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "invalid dhcp config file: " + this.persistentConfigFileName);
                        }
                        netmask = (IP4Address) IPAddress.parseHostAddress(st.nextToken());
                    } else if (token.equals("interface")) {
                        interfaceName = st.nextToken();
                        interfaceName = interfaceName.substring(0, interfaceName.indexOf(';'));
                    } 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));
                        }
                    }
                }
            }
            boolean running = isRunning();
            /*
                 * LinuxNamed linuxNamed = LinuxNamed.getInstance();
                 * DnsServerConfigIP4 dnsServerConfig = linuxNamed.getDnsServerConfig();
                 * List<IP4Address> forwarders = dnsServerConfig.getForwarders();
                 * List<NetworkPair<IP4Address>> allowedNetworks = dnsServerConfig.getAllowedNetworks();
                 * boolean passDns = (forwarders != null && forwarders.size() > 0
                 * && allowedNetworks != null && allowedNetworks.size() > 0);
                 */
            // FIXME - prefix still hardcoded
            s_logger.debug("instantiating DHCP server configuration during init with " + "\n\t\tinterfaceName: " + interfaceName + "\n\t\trunning: " + running + "\n\t\tsubnet: " + subnet.getHostAddress() + "\n\t\trouter: " + router.getHostAddress() + "\n\t\tnetmask: " + netmask.getHostAddress() + "\n\t\tdefaultLeaseTime: " + defaultLeaseTime + "\n\t\tmaxLeaseTime: " + maxLeaseTime + "\n\t\trangeStart: " + rangeStart.getHostAddress() + "\n\t\trangeEnd: " + rangeEnd.getHostAddress() + "\n\t\tpassDns: " + passDns + "\n\t\tdnsList: " + dnsList.toString());
            this.m_dhcpServerConfig4 = new DhcpServerConfigIP4(interfaceName, enabled, subnet, router, netmask, defaultLeaseTime, maxLeaseTime, (short) 24, rangeStart, rangeEnd, passDns, dnsList);
            br.close();
            br = null;
        } catch (FileNotFoundException e) {
            throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e);
        } catch (IOException e) {
            throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e);
        }
    } else {
        s_logger.debug("There is no current DHCP server configuration for {}", this.m_interfaceName);
    }
}
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) File(java.io.File)

Example 9 with IP4Address

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

the class DhcpConfigReader method populateUdhcpdConfig.

private DhcpServerConfig4 populateUdhcpdConfig(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) {
            line.trim();
            if (line.startsWith("start")) {
                rangeStart = (IP4Address) IPAddress.parseHostAddress(line.substring("start".length()).trim());
            } else if (line.startsWith("end")) {
                rangeEnd = (IP4Address) IPAddress.parseHostAddress(line.substring("end".length()).trim());
            } else if (line.startsWith("opt router")) {
                router = (IP4Address) IPAddress.parseHostAddress(line.substring("opt router".length()).trim());
            } else if (line.startsWith("opt subnet")) {
                netmask = (IP4Address) IPAddress.parseHostAddress(line.substring("opt subnet".length()).trim());
            } else if (line.startsWith("opt lease")) {
                defaultLeaseTime = Integer.parseInt(line.substring("opt lease".length()).trim());
                maxLeaseTime = defaultLeaseTime;
            } else if (line.startsWith("opt dns")) {
                line = line.substring("opt dns".length()).trim();
                StringTokenizer st = new StringTokenizer(line);
                while (st.hasMoreTokens()) {
                    dnsList.add((IP4Address) IPAddress.parseHostAddress(st.nextToken()));
                }
            }
        }
        subnet = (IP4Address) IPAddress.parseHostAddress(NetworkUtil.calculateNetwork(router.getHostAddress(), netmask.getHostAddress()));
        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.info("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 (Exception 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) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) StringTokenizer(java.util.StringTokenizer) KuraException(org.eclipse.kura.KuraException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 10 with IP4Address

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

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