Search in sources :

Example 1 with ModemInterfaceAddress

use of org.eclipse.kura.net.modem.ModemInterfaceAddress in project kura by eclipse.

the class NetworkServiceImpl method getModemInterface.

private ModemInterface<ModemInterfaceAddress> getModemInterface(String interfaceName, boolean isUp, ModemDevice modemDevice) throws KuraException {
    ModemInterfaceImpl<ModemInterfaceAddress> modemInterface = new ModemInterfaceImpl<ModemInterfaceAddress>(interfaceName);
    modemInterface.setModemDevice(modemDevice);
    if (modemDevice instanceof UsbModemDevice) {
        UsbModemDevice usbModemDevice = (UsbModemDevice) modemDevice;
        SupportedUsbModemInfo supportedUsbModemInfo = null;
        supportedUsbModemInfo = SupportedUsbModemsInfo.getModem(usbModemDevice.getVendorId(), usbModemDevice.getProductId(), usbModemDevice.getProductName());
        modemInterface.setTechnologyTypes(supportedUsbModemInfo.getTechnologyTypes());
        modemInterface.setUsbDevice((UsbModemDevice) modemDevice);
    } else if (modemDevice instanceof SerialModemDevice) {
        SupportedSerialModemInfo supportedSerialModemInfo = null;
        supportedSerialModemInfo = SupportedSerialModemsInfo.getModem();
        modemInterface.setTechnologyTypes(supportedSerialModemInfo.getTechnologyTypes());
    }
    int pppNum = 0;
    if (interfaceName.startsWith("ppp")) {
        pppNum = Integer.parseInt(interfaceName.substring(3));
    }
    modemInterface.setPppNum(pppNum);
    modemInterface.setManufacturer(modemDevice.getManufacturerName());
    modemInterface.setModel(modemDevice.getProductName());
    modemInterface.setModemIdentifier(modemDevice.getProductName());
    // these properties required net.admin packages
    modemInterface.setDriver(getDriver());
    modemInterface.setDriverVersion(getDriverVersion());
    modemInterface.setFirmwareVersion(getFirmwareVersion());
    modemInterface.setSerialNumber("unknown");
    modemInterface.setLoopback(false);
    modemInterface.setPointToPoint(true);
    modemInterface.setState(getState(interfaceName, isUp));
    modemInterface.setHardwareAddress(new byte[] { 0, 0, 0, 0, 0, 0 });
    LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
    if (ifconfig != null) {
        modemInterface.setMTU(ifconfig.getMtu());
        modemInterface.setSupportsMulticast(ifconfig.isMulticast());
    }
    modemInterface.setUp(isUp);
    modemInterface.setVirtual(isVirtual());
    modemInterface.setNetInterfaceAddresses(getModemInterfaceAddresses(interfaceName, isUp));
    return modemInterface;
}
Also used : UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) ModemInterfaceImpl(org.eclipse.kura.core.net.modem.ModemInterfaceImpl) SerialModemDevice(org.eclipse.kura.net.modem.SerialModemDevice) SupportedSerialModemInfo(org.eclipse.kura.linux.net.modem.SupportedSerialModemInfo) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress) SupportedUsbModemInfo(org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) LinuxIfconfig(org.eclipse.kura.linux.net.util.LinuxIfconfig)

Example 2 with ModemInterfaceAddress

use of org.eclipse.kura.net.modem.ModemInterfaceAddress 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 3 with ModemInterfaceAddress

use of org.eclipse.kura.net.modem.ModemInterfaceAddress in project kura by eclipse.

the class NetworkServiceImpl method getModemInterfaceAddresses.

private List<ModemInterfaceAddress> getModemInterfaceAddresses(String interfaceName, boolean isUp) throws KuraException {
    List<ModemInterfaceAddress> modemInterfaceAddresses = new ArrayList<ModemInterfaceAddress>();
    if (isUp) {
        ConnectionInfo conInfo = new ConnectionInfoImpl(interfaceName);
        ModemInterfaceAddressImpl modemInterfaceAddress = new ModemInterfaceAddressImpl();
        modemInterfaceAddresses.add(modemInterfaceAddress);
        try {
            LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
            if (ifconfig != null) {
                String currentNetmask = ifconfig.getInetMask();
                if (currentNetmask != null) {
                    modemInterfaceAddress.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
                    modemInterfaceAddress.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
                    modemInterfaceAddress.setNetmask(IPAddress.parseHostAddress(currentNetmask));
                    modemInterfaceAddress.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(currentNetmask));
                    modemInterfaceAddress.setGateway(conInfo.getGateway());
                    modemInterfaceAddress.setDnsServers(conInfo.getDnsServers());
                    ModemConnectionStatus connectionStatus = isUp ? ModemConnectionStatus.CONNECTED : ModemConnectionStatus.DISCONNECTED;
                    modemInterfaceAddress.setConnectionStatus(connectionStatus);
                // TODO - other attributes
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (UnknownHostException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
    return modemInterfaceAddresses;
}
Also used : ModemConnectionStatus(org.eclipse.kura.net.modem.ModemConnectionStatus) UnknownHostException(java.net.UnknownHostException) KuraException(org.eclipse.kura.KuraException) ArrayList(java.util.ArrayList) ConnectionInfo(org.eclipse.kura.net.ConnectionInfo) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress) ModemInterfaceAddressImpl(org.eclipse.kura.core.net.modem.ModemInterfaceAddressImpl) LinuxIfconfig(org.eclipse.kura.linux.net.util.LinuxIfconfig)

Example 4 with ModemInterfaceAddress

use of org.eclipse.kura.net.modem.ModemInterfaceAddress in project kura by eclipse.

the class NetworkServiceImpl method getNetworkInterfaces.

@Override
public List<NetInterface<? extends NetInterfaceAddress>> getNetworkInterfaces() throws KuraException {
    s_logger.trace("getNetworkInterfaces()");
    List<NetInterface<? extends NetInterfaceAddress>> netInterfaces = new ArrayList<NetInterface<? extends NetInterfaceAddress>>();
    List<String> interfaceNames = getAllNetworkInterfaceNames();
    for (String interfaceName : interfaceNames) {
        try {
            NetInterface<? extends NetInterfaceAddress> netInterface = getNetworkInterface(interfaceName);
            if (netInterface != null) {
                netInterfaces.add(netInterface);
            }
        } catch (KuraException e) {
            s_logger.error("Can't get network interface info for {} :: exception - {}", interfaceName, e.toString());
        }
    }
    // Return an entry for non-connected modems (those w/o a ppp interface)
    Iterator<String> it = this.m_addedModems.iterator();
    while (it.hasNext()) {
        String modemId = it.next();
        UsbModemDevice usbModem = this.m_usbModems.get(modemId);
        if (usbModem != null) {
            // only add if there is not already a ppp interface for this modem
            boolean addModem = true;
            for (NetInterface<?> netInterface : netInterfaces) {
                UsbDevice usbDevice = netInterface.getUsbDevice();
                if (usbDevice != null) {
                    if (usbDevice.getUsbPort().equals(usbModem.getUsbPort())) {
                        addModem = false;
                        break;
                    }
                }
            }
            if (addModem) {
                netInterfaces.add(getModemInterface(usbModem.getUsbPort(), false, usbModem));
            }
        } else {
            // for Serial modem
            if (this.m_serialModem != null) {
                // only add if there is not already a ppp interface for this modem
                boolean addModem = true;
                for (NetInterface<?> netInterface : netInterfaces) {
                    String iface = netInterface.getName();
                    if (iface != null && iface.startsWith("ppp")) {
                        ModemInterface<ModemInterfaceAddress> pppModemInterface = getModemInterface(iface, false, this.m_serialModem);
                        ModemInterface<ModemInterfaceAddress> serialModemInterface = getModemInterface(this.m_serialModem.getProductName(), false, this.m_serialModem);
                        if (pppModemInterface != null && serialModemInterface != null) {
                            String pppModel = pppModemInterface.getModel();
                            String serialModel = serialModemInterface.getModel();
                            if (pppModel != null && pppModel.equals(serialModel)) {
                                addModem = false;
                                break;
                            }
                        }
                    }
                }
                if (addModem) {
                    netInterfaces.add(getModemInterface(this.m_serialModem.getProductName(), false, this.m_serialModem));
                }
            }
        }
    }
    return netInterfaces;
}
Also used : ArrayList(java.util.ArrayList) UsbDevice(org.eclipse.kura.usb.UsbDevice) UsbModemDevice(org.eclipse.kura.usb.UsbModemDevice) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress)

Example 5 with ModemInterfaceAddress

use of org.eclipse.kura.net.modem.ModemInterfaceAddress in project kura by eclipse.

the class NetworkConfiguration method recomputeNetworkProperties.

// ---------------------------------------------------------------
// 
// Private Methods
// 
// ---------------------------------------------------------------
private void recomputeNetworkProperties() {
    Map<String, Object> properties = new HashMap<String, Object>();
    String netIfPrefix = null;
    String netIfReadOnlyPrefix = null;
    String netIfConfigPrefix = null;
    StringBuilder sbPrefix = null;
    StringBuilder sbInterfaces = new StringBuilder();
    if (this.m_modifiedInterfaceNames != null && !this.m_modifiedInterfaceNames.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        String prefix = "";
        for (String interfaceName : this.m_modifiedInterfaceNames) {
            sb.append(prefix);
            prefix = ",";
            sb.append(interfaceName);
        }
        String result = sb.toString();
        s_logger.debug("Set modified interface names: {}", result);
        properties.put("modified.interface.names", result);
    }
    Iterator<String> it = this.m_netInterfaceConfigs.keySet().iterator();
    while (it.hasNext()) {
        NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_netInterfaceConfigs.get(it.next());
        // add the interface to the list of interface found in the platform
        if (sbInterfaces.length() != 0) {
            sbInterfaces.append(",");
        }
        sbInterfaces.append(netInterfaceConfig.getName());
        // build the prefixes for all the properties associated with this interface
        sbPrefix = new StringBuilder("net.interface.").append(netInterfaceConfig.getName()).append(".");
        netIfReadOnlyPrefix = sbPrefix.toString();
        netIfPrefix = sbPrefix.append("config.").toString();
        netIfConfigPrefix = sbPrefix.toString();
        // add the properties of the interface
        properties.put(netIfReadOnlyPrefix + "type", netInterfaceConfig.getType().toString());
        properties.put(netIfPrefix + "name", netInterfaceConfig.getName());
        if (netInterfaceConfig.getState() != null) {
            properties.put(netIfPrefix + "state", netInterfaceConfig.getState().toString());
        }
        properties.put(netIfPrefix + "autoconnect", netInterfaceConfig.isAutoConnect());
        properties.put(netIfPrefix + "mtu", netInterfaceConfig.getMTU());
        properties.put(netIfReadOnlyPrefix + "driver", netInterfaceConfig.getDriver());
        properties.put(netIfReadOnlyPrefix + "driver.version", netInterfaceConfig.getDriverVersion());
        properties.put(netIfReadOnlyPrefix + "firmware.version", netInterfaceConfig.getFirmwareVersion());
        properties.put(netIfReadOnlyPrefix + "mac", NetUtil.hardwareAddressToString(netInterfaceConfig.getHardwareAddress()));
        properties.put(netIfReadOnlyPrefix + "loopback", netInterfaceConfig.isLoopback());
        properties.put(netIfReadOnlyPrefix + "ptp", netInterfaceConfig.isPointToPoint());
        properties.put(netIfReadOnlyPrefix + "up", netInterfaceConfig.isUp());
        properties.put(netIfReadOnlyPrefix + "virtual", netInterfaceConfig.isVirtual());
        // usb
        if (netInterfaceConfig.getUsbDevice() != null) {
            UsbDevice usbDev = netInterfaceConfig.getUsbDevice();
            properties.put(netIfReadOnlyPrefix + "usb.vendor.id", usbDev.getVendorId());
            properties.put(netIfReadOnlyPrefix + "usb.vendor.name", usbDev.getManufacturerName());
            properties.put(netIfReadOnlyPrefix + "usb.product.id", usbDev.getProductId());
            properties.put(netIfReadOnlyPrefix + "usb.product.name", usbDev.getProductName());
            properties.put(netIfReadOnlyPrefix + "usb.busNumber", usbDev.getUsbBusNumber());
            properties.put(netIfReadOnlyPrefix + "usb.devicePath", usbDev.getUsbDevicePath());
        }
        // custom readonly props for Ethernet and Wifi
        if (netInterfaceConfig instanceof EthernetInterfaceConfigImpl) {
            properties.put(netIfReadOnlyPrefix + "eth.link.up", ((EthernetInterfaceConfigImpl) netInterfaceConfig).isLinkUp());
        } else if (netInterfaceConfig instanceof WifiInterfaceConfigImpl) {
            EnumSet<Capability> capabilities = ((WifiInterfaceConfigImpl) netInterfaceConfig).getCapabilities();
            if (capabilities != null && !capabilities.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                for (Capability capability : capabilities) {
                    sb.append(capability.toString());
                    sb.append(",");
                }
                String capabilitiesString = sb.toString();
                capabilitiesString = capabilitiesString.substring(0, capabilitiesString.length() - 1);
                properties.put(netIfReadOnlyPrefix + "wifi.capabilities", capabilitiesString);
            }
        }
        // add wifi properties
        if (netInterfaceConfig.getType() == NetInterfaceType.WIFI) {
            // capabilities
            StringBuilder sbCapabilities = new StringBuilder();
            EnumSet<Capability> capabilities = ((WifiInterface) netInterfaceConfig).getCapabilities();
            if (capabilities != null) {
                Iterator<Capability> it2 = ((WifiInterface) netInterfaceConfig).getCapabilities().iterator();
                while (it2.hasNext()) {
                    sbCapabilities.append(it2.next().name()).append(" ");
                }
                properties.put(netIfReadOnlyPrefix + "wifi.capabilities", sbCapabilities.toString());
            }
        }
        // add modem properties
        if (netInterfaceConfig.getType() == NetInterfaceType.MODEM) {
            String delim;
            // revision
            StringBuffer revisionIdBuf = new StringBuffer();
            String[] revisionId = ((ModemInterface<?>) netInterfaceConfig).getRevisionId();
            if (revisionId != null) {
                delim = null;
                for (String rev : revisionId) {
                    if (delim != null) {
                        revisionIdBuf.append(delim);
                    }
                    revisionIdBuf.append(rev);
                    delim = ",";
                }
            }
            // technology types
            StringBuffer techTypesBuf = new StringBuffer();
            List<ModemTechnologyType> techTypes = ((ModemInterface<?>) netInterfaceConfig).getTechnologyTypes();
            if (techTypes != null) {
                delim = null;
                for (ModemTechnologyType techType : techTypes) {
                    if (delim != null) {
                        techTypesBuf.append(delim);
                    }
                    techTypesBuf.append(techType.toString());
                    delim = ",";
                }
            }
            ModemPowerMode powerMode = ModemPowerMode.UNKNOWN;
            if (((ModemInterface<?>) netInterfaceConfig).getPowerMode() != null) {
                powerMode = ((ModemInterface<?>) netInterfaceConfig).getPowerMode();
            }
            properties.put(netIfReadOnlyPrefix + "manufacturer", ((ModemInterface<?>) netInterfaceConfig).getManufacturer());
            properties.put(netIfReadOnlyPrefix + "model", ((ModemInterface<?>) netInterfaceConfig).getModel());
            properties.put(netIfReadOnlyPrefix + "revisionId", revisionIdBuf.toString());
            properties.put(netIfReadOnlyPrefix + "serialNum", ((ModemInterface<?>) netInterfaceConfig).getSerialNumber());
            properties.put(netIfReadOnlyPrefix + "technologyTypes", techTypesBuf.toString());
            properties.put(netIfConfigPrefix + "identifier", ((ModemInterface<?>) netInterfaceConfig).getModemIdentifier());
            properties.put(netIfConfigPrefix + "powerMode", powerMode.toString());
            properties.put(netIfConfigPrefix + "pppNum", ((ModemInterface<?>) netInterfaceConfig).getPppNum());
            properties.put(netIfConfigPrefix + "poweredOn", ((ModemInterface<?>) netInterfaceConfig).isPoweredOn());
        }
        for (NetInterfaceAddress nia : netInterfaceConfig.getNetInterfaceAddresses()) {
            String typePrefix = "ip4.";
            if (nia != null) {
                if (nia.getAddress() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "address", nia.getAddress().getHostAddress());
                }
                if (nia.getBroadcast() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "broadcast", nia.getBroadcast().getHostAddress());
                }
                if (nia.getGateway() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "gateway", nia.getGateway().getHostAddress());
                }
                if (nia.getNetmask() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "netmask", nia.getNetmask().getHostAddress());
                }
                if (nia.getNetmask() != null) {
                    properties.put(netIfReadOnlyPrefix + typePrefix + "prefix", Short.valueOf(nia.getNetworkPrefixLength()));
                }
                if (nia.getDnsServers() != null) {
                    StringBuilder dnsServers = new StringBuilder();
                    for (IPAddress dnsServer : nia.getDnsServers()) {
                        if (dnsServers.length() != 0) {
                            dnsServers.append(",");
                        }
                        dnsServers.append(dnsServer);
                    }
                    properties.put(netIfReadOnlyPrefix + typePrefix + "dnsServers", dnsServers.toString());
                }
                // Wifi interface address
                if (nia instanceof WifiInterfaceAddress) {
                    // access point
                    WifiAccessPoint wap = ((WifiInterfaceAddress) nia).getWifiAccessPoint();
                    if (wap != null) {
                    /*
                             * TODO: need fields to reflect current state?
                             * properties.put(sbNetIfPrefix+"wifi.ssid", wap.getSSID());
                             * properties.put(sbNetIfPrefix+"wifi.mode", wap.getMode());
                             */
                    }
                    long bitrate = ((WifiInterfaceAddress) nia).getBitrate();
                    properties.put(netIfReadOnlyPrefix + "wifi.bitrate", Long.valueOf(bitrate));
                    WifiMode wifiMode;
                    if (((WifiInterfaceAddress) nia).getMode() != null) {
                        wifiMode = ((WifiInterfaceAddress) nia).getMode();
                    } else {
                        wifiMode = WifiMode.UNKNOWN;
                    }
                    properties.put(netIfPrefix + "wifi.mode", wifiMode.toString());
                }
                // Modem interface address
                if (nia instanceof ModemInterfaceAddress) {
                    if (((ModemInterfaceAddress) nia).getConnectionType() != null) {
                        properties.put(netIfConfigPrefix + "connection.type", ((ModemInterfaceAddress) nia).getConnectionType().toString());
                    }
                    if (((ModemInterfaceAddress) nia).getConnectionStatus() != null) {
                        properties.put(netIfConfigPrefix + "connection.status", ((ModemInterfaceAddress) nia).getConnectionStatus().toString());
                    }
                }
            }
        }
        // add the properties of the network configurations associated to the interface
        List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
        s_logger.trace("netInterfaceAddressConfigs.size() for {}: {}", netInterfaceConfig.getName(), netInterfaceAddressConfigs.size());
        for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
            List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
            if (netConfigs != null) {
                s_logger.trace("netConfigs.size(): {}", netConfigs.size());
                for (NetConfig netConfig : netConfigs) {
                    if (netConfig instanceof WifiConfig) {
                        s_logger.trace("adding netconfig WifiConfigIP4 for {}", netInterfaceConfig.getName());
                        addWifiConfigIP4Properties((WifiConfig) netConfig, netIfConfigPrefix, properties);
                    } else if (netConfig instanceof ModemConfig) {
                        s_logger.trace("adding netconfig ModemConfig for {}", netInterfaceConfig.getName());
                        addModemConfigProperties((ModemConfig) netConfig, netIfConfigPrefix, properties);
                    } else if (netConfig instanceof NetConfigIP4) {
                        s_logger.trace("adding netconfig NetConfigIP4 for {}", netInterfaceConfig.getName());
                        addNetConfigIP4Properties((NetConfigIP4) netConfig, netIfConfigPrefix, properties);
                    /*
                             * Iterator<String> it2 = properties.keySet().iterator();
                             * while(it2.hasNext()) {
                             * String key = it2.next();
                             * System.out.println("\t\t\t"+key+"="+properties.get(key));
                             * }
                             */
                    } else if (netConfig instanceof NetConfigIP6) {
                        s_logger.trace("adding netconfig NetConfigIP6 for {}", netInterfaceConfig.getName());
                        addNetConfigIP6Properties((NetConfigIP6) netConfig, netIfConfigPrefix, properties);
                    /*
                             * Iterator<String> it = properties.keySet().iterator();
                             * while(it.hasNext()) {
                             * String key = it.next();
                             * System.out.println("\t\t\t"+key+"="+properties.get(key));
                             * }
                             */
                    } else if (netConfig instanceof DhcpServerConfig4) {
                        s_logger.trace("adding netconfig DhcpServerConfig4 for {}", netInterfaceConfig.getName());
                        addDhcpServerConfig4((DhcpServerConfig4) netConfig, netIfConfigPrefix, properties);
                    } else if (netConfig instanceof FirewallAutoNatConfig) {
                        s_logger.trace("adding netconfig FirewallNatConfig for {}", netInterfaceConfig.getName());
                        addFirewallNatConfig((FirewallAutoNatConfig) netConfig, netIfConfigPrefix, properties);
                    }
                }
            }
        }
    }
    properties.put("net.interfaces", sbInterfaces.toString());
    this.m_properties = properties;
}
Also used : FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) HashMap(java.util.HashMap) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) UsbDevice(org.eclipse.kura.usb.UsbDevice) WifiInterface(org.eclipse.kura.net.wifi.WifiInterface) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) WifiMode(org.eclipse.kura.net.wifi.WifiMode) ModemTechnologyType(org.eclipse.kura.net.modem.ModemTechnologyType) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) ModemInterface(org.eclipse.kura.net.modem.ModemInterface) Capability(org.eclipse.kura.net.wifi.WifiInterface.Capability) EnumSet(java.util.EnumSet) ModemPowerMode(org.eclipse.kura.net.modem.ModemPowerMode) DhcpServerConfig4(org.eclipse.kura.net.dhcp.DhcpServerConfig4) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) ModemConfig(org.eclipse.kura.net.modem.ModemConfig) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetConfig(org.eclipse.kura.net.NetConfig) WifiInterfaceAddress(org.eclipse.kura.net.wifi.WifiInterfaceAddress) IPAddress(org.eclipse.kura.net.IPAddress) ModemInterfaceAddress(org.eclipse.kura.net.modem.ModemInterfaceAddress)

Aggregations

ModemInterfaceAddress (org.eclipse.kura.net.modem.ModemInterfaceAddress)5 ArrayList (java.util.ArrayList)3 KuraException (org.eclipse.kura.KuraException)3 NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)3 UnknownHostException (java.net.UnknownHostException)2 LinuxIfconfig (org.eclipse.kura.linux.net.util.LinuxIfconfig)2 ConnectionInfo (org.eclipse.kura.net.ConnectionInfo)2 NetConfig (org.eclipse.kura.net.NetConfig)2 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)2 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)2 DhcpServerConfig4 (org.eclipse.kura.net.dhcp.DhcpServerConfig4)2 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)2 Capability (org.eclipse.kura.net.wifi.WifiInterface.Capability)2 WifiInterfaceAddress (org.eclipse.kura.net.wifi.WifiInterfaceAddress)2 WifiMode (org.eclipse.kura.net.wifi.WifiMode)2 UsbDevice (org.eclipse.kura.usb.UsbDevice)2 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 List (java.util.List)1 EthernetInterfaceConfigImpl (org.eclipse.kura.core.net.EthernetInterfaceConfigImpl)1