Search in sources :

Example 1 with NetInterfaceAddress

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

the class NetworkConfiguration method toString.

@Override
public String toString() {
    StringBuffer sb = new StringBuffer();
    Iterator<String> it = this.m_netInterfaceConfigs.keySet().iterator();
    while (it.hasNext()) {
        NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig = this.m_netInterfaceConfigs.get(it.next());
        sb.append("\nname: " + netInterfaceConfig.getName());
        sb.append(" :: Loopback? " + netInterfaceConfig.isLoopback());
        sb.append(" :: Point to Point? " + netInterfaceConfig.isPointToPoint());
        sb.append(" :: Up? " + netInterfaceConfig.isUp());
        sb.append(" :: Virtual? " + netInterfaceConfig.isVirtual());
        sb.append(" :: Driver: " + netInterfaceConfig.getDriver());
        sb.append(" :: Driver Version: " + netInterfaceConfig.getDriverVersion());
        sb.append(" :: Firmware Version: " + netInterfaceConfig.getFirmwareVersion());
        sb.append(" :: MTU: " + netInterfaceConfig.getMTU());
        if (netInterfaceConfig.getHardwareAddress() != null) {
            sb.append(" :: Hardware Address: " + new String(netInterfaceConfig.getHardwareAddress()));
        }
        sb.append(" :: State: " + netInterfaceConfig.getState());
        sb.append(" :: Type: " + netInterfaceConfig.getType());
        sb.append(" :: Usb Device: " + netInterfaceConfig.getUsbDevice());
        List<? extends NetInterfaceAddress> netInterfaceAddresses = netInterfaceConfig.getNetInterfaceAddresses();
        for (NetInterfaceAddress netInterfaceAddress : netInterfaceAddresses) {
            if (netInterfaceAddress.getAddress() != null) {
                sb.append(" :: Address: " + netInterfaceAddress.getAddress().getHostAddress());
            }
            sb.append(" :: Prefix: " + netInterfaceAddress.getNetworkPrefixLength());
            if (netInterfaceAddress.getNetmask() != null) {
                sb.append(" :: Netmask: " + netInterfaceAddress.getNetmask().getHostAddress());
            }
            if (netInterfaceAddress.getBroadcast() != null) {
                sb.append(" :: Broadcast: " + netInterfaceAddress.getBroadcast().getHostAddress());
            }
        }
        List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
        if (netInterfaceAddressConfigs != null) {
            for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
                List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
                if (netConfigs != null) {
                    for (NetConfig netConfig : netConfigs) {
                        if (netConfig instanceof NetConfigIP4) {
                            sb.append("\n\tIPv4 ");
                            if (((NetConfigIP4) netConfig).isDhcp()) {
                                sb.append(" :: is DHCP client");
                                Map<String, Object> dhcp4Map = ((NetConfigIP4) netConfig).getProperties();
                                for (Map.Entry<String, Object> entry : dhcp4Map.entrySet()) {
                                    String dhcpKey = entry.getKey();
                                    sb.append(" :: " + dhcpKey + ": " + entry.getValue());
                                }
                            } else if (((NetConfigIP4) netConfig).getAddress() == null) {
                                sb.append(" :: is not configured for STATIC or DHCP");
                            } else {
                                sb.append(" :: is STATIC client");
                                if (((NetConfigIP4) netConfig).getAddress() != null) {
                                    sb.append(" :: Address: " + ((NetConfigIP4) netConfig).getAddress().getHostAddress());
                                }
                                sb.append(" :: Prefix: " + ((NetConfigIP4) netConfig).getNetworkPrefixLength());
                                if (((NetConfigIP4) netConfig).getGateway() != null) {
                                    sb.append(" :: Gateway: " + ((NetConfigIP4) netConfig).getGateway().getHostAddress());
                                }
                                List<IP4Address> dnsServers = ((NetConfigIP4) netConfig).getDnsServers();
                                List<IP4Address> winsServers = ((NetConfigIP4) netConfig).getWinsServers();
                                List<String> domains = ((NetConfigIP4) netConfig).getDomains();
                                if (dnsServers != null) {
                                    for (IP4Address dnsServer : dnsServers) {
                                        sb.append(" :: DNS : " + dnsServer.getHostAddress());
                                    }
                                }
                                if (winsServers != null) {
                                    for (IP4Address winsServer : winsServers) {
                                        sb.append(" :: WINS Server : " + winsServer.getHostAddress());
                                    }
                                }
                                if (domains != null) {
                                    for (String domain : domains) {
                                        sb.append(" :: Domains : " + domain);
                                    }
                                }
                            }
                        } else if (netConfig instanceof NetConfigIP6) {
                            sb.append("\n\tIPv6 ");
                            if (((NetConfigIP6) netConfig).isDhcp()) {
                                sb.append(" :: is DHCP client");
                                Map<String, Object> dhcp6Map = ((NetConfigIP6) netConfig).getProperties();
                                Iterator<String> it2 = dhcp6Map.keySet().iterator();
                                while (it2.hasNext()) {
                                    String dhcpKey = it2.next();
                                    sb.append(" :: " + dhcpKey + ": " + dhcp6Map.get(dhcpKey));
                                }
                            } else {
                                sb.append(" :: is STATIC client");
                                if (((NetConfigIP6) netConfig).getAddress() != null) {
                                    sb.append(" :: Address: " + ((NetConfigIP6) netConfig).getAddress().getHostAddress());
                                }
                                List<IP6Address> dnsServers = ((NetConfigIP6) netConfig).getDnsServers();
                                List<String> domains = ((NetConfigIP6) netConfig).getDomains();
                                for (IP6Address dnsServer : dnsServers) {
                                    sb.append(" :: DNS : " + dnsServer.getHostAddress());
                                }
                                for (String domain : domains) {
                                    sb.append(" :: Domains : " + domain);
                                }
                            }
                        } else if (netConfig instanceof WifiConfig) {
                            sb.append("\n\tWifiConfig ");
                            sb.append(" :: SSID: " + ((WifiConfig) netConfig).getSSID());
                            sb.append(" :: BgScan: " + ((WifiConfig) netConfig).getBgscan());
                            sb.append(" :: Broadcast: " + ((WifiConfig) netConfig).getBroadcast());
                            int[] channels = ((WifiConfig) netConfig).getChannels();
                            if (channels != null && channels.length > 0) {
                                for (int i = 0; i < channels.length; i++) {
                                    sb.append(channels[i]);
                                    if (i + 1 < channels.length) {
                                        sb.append(",");
                                    }
                                }
                            }
                            sb.append(" :: Group Ciphers: " + ((WifiConfig) netConfig).getGroupCiphers());
                            sb.append(" :: Hardware Mode: " + ((WifiConfig) netConfig).getHardwareMode());
                            sb.append(" :: Mode: " + ((WifiConfig) netConfig).getMode());
                            sb.append(" :: Pairwise Ciphers: " + ((WifiConfig) netConfig).getPairwiseCiphers());
                            sb.append(" :: Passkey: " + ((WifiConfig) netConfig).getPasskey());
                            sb.append(" :: Security: " + ((WifiConfig) netConfig).getSecurity());
                        } else if (netConfig instanceof ModemConfig) {
                            sb.append("\n\tModemConfig ");
                            sb.append(" :: APN: " + ((ModemConfig) netConfig).getApn());
                            sb.append(" :: Data Compression: " + ((ModemConfig) netConfig).getDataCompression());
                            sb.append(" :: Dial String: " + ((ModemConfig) netConfig).getDialString());
                            sb.append(" :: Header Compression: " + ((ModemConfig) netConfig).getHeaderCompression());
                            sb.append(" :: Password: " + ((ModemConfig) netConfig).getPassword());
                            sb.append(" :: PPP number: " + ((ModemConfig) netConfig).getPppNumber());
                            sb.append(" :: Profile ID: " + ((ModemConfig) netConfig).getProfileID());
                            sb.append(" :: Username: " + ((ModemConfig) netConfig).getUsername());
                            sb.append(" :: Auth Type: " + ((ModemConfig) netConfig).getAuthType());
                            sb.append(" :: IP Address: " + ((ModemConfig) netConfig).getIpAddress());
                            sb.append(" :: PDP Type: " + ((ModemConfig) netConfig).getPdpType());
                        } else if (netConfig instanceof DhcpServerConfig) {
                            sb.append("\n\tDhcpServerConfig ");
                        // TODO - finish displaying
                        } else if (netConfig instanceof FirewallAutoNatConfig) {
                            sb.append("\n\tFirewallAutoNatConfig ");
                        // TODO - finish displaying
                        } else {
                            if (netConfig != null && netConfig.getClass() != null) {
                                sb.append("\n\tUNKNOWN CONFIG TYPE???: " + netConfig.getClass().getName());
                            } else {
                                sb.append("\n\tNULL NETCONFIG PRESENT?!?");
                            }
                        }
                    }
                }
            }
        }
    }
    return sb.toString();
}
Also used : FirewallAutoNatConfig(org.eclipse.kura.net.firewall.FirewallAutoNatConfig) WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) NetConfigIP4(org.eclipse.kura.net.NetConfigIP4) NetConfigIP6(org.eclipse.kura.net.NetConfigIP6) Iterator(java.util.Iterator) DhcpServerConfig(org.eclipse.kura.net.dhcp.DhcpServerConfig) List(java.util.List) ArrayList(java.util.ArrayList) NetInterfaceAddressConfig(org.eclipse.kura.net.NetInterfaceAddressConfig) IP4Address(org.eclipse.kura.net.IP4Address) IP6Address(org.eclipse.kura.net.IP6Address) 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) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with NetInterfaceAddress

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

the class LifeCyclePayloadBuilder method buildDeviceProfile.

public KuraDeviceProfile buildDeviceProfile() {
    SystemService systemService = this.m_cloudServiceImpl.getSystemService();
    SystemAdminService sysAdminService = this.m_cloudServiceImpl.getSystemAdminService();
    NetworkService networkService = this.m_cloudServiceImpl.getNetworkService();
    PositionService positionService = this.m_cloudServiceImpl.getPositionService();
    // 
    // get the network information
    StringBuilder sbConnectionIp = null;
    StringBuilder sbConnectionInterface = null;
    try {
        List<NetInterface<? extends NetInterfaceAddress>> nis = networkService.getActiveNetworkInterfaces();
        if (!nis.isEmpty()) {
            sbConnectionIp = new StringBuilder();
            sbConnectionInterface = new StringBuilder();
            for (NetInterface<? extends NetInterfaceAddress> ni : nis) {
                List<? extends NetInterfaceAddress> nias = ni.getNetInterfaceAddresses();
                if (nias != null && !nias.isEmpty()) {
                    sbConnectionInterface.append(buildConnectionInterface(ni)).append(",");
                    sbConnectionIp.append(buildConnectionIp(ni)).append(",");
                }
            }
            // Remove trailing comma
            sbConnectionIp.deleteCharAt(sbConnectionIp.length() - 1);
            sbConnectionInterface.deleteCharAt(sbConnectionInterface.length() - 1);
        }
    } catch (Exception se) {
        s_logger.warn("Error while getting ConnetionIP and ConnectionInterface", se);
    }
    String connectionIp = sbConnectionIp != null ? sbConnectionIp.toString() : "UNKNOWN";
    String connectionInterface = sbConnectionInterface != null ? sbConnectionInterface.toString() : "UNKNOWN";
    // 
    // get the network information
    // String primaryNetInterface = systemService.getPrimaryNetworkInterfaceName();
    // String connectionIp = UNKNOWN;
    // String connectionInterface = UNKNOWN;
    // try {
    // List<NetInterface<? extends NetInterfaceAddress>> nis = networkService.getActiveNetworkInterfaces();
    // if (!nis.isEmpty()) {
    // 
    // // look for the primary network interface first
    // for (NetInterface<? extends NetInterfaceAddress> ni : nis) {
    // if (ni.getName().equals(primaryNetInterface)) {
    // List<? extends NetInterfaceAddress> nias = ni.getNetInterfaceAddresses();
    // if (nias != null && !nias.isEmpty()) {
    // connectionInterface = buildConnectionInterface(ni);
    // connectionIp = buildConnectionIp(ni);
    // break;
    // }
    // }
    // }
    // 
    // // if not resolved, loop through all network interfaces until we find one with an address
    // if (UNKNOWN.equals(connectionIp) || UNKNOWN.equals(connectionInterface)) {
    // s_logger.warn("Unresolved connectionIp for primary Network Interface. Looping through all interfaces...");
    // for (NetInterface<? extends NetInterfaceAddress> ni : nis) {
    // List<? extends NetInterfaceAddress> nias = ni.getNetInterfaceAddresses();
    // if (nias != null && !nias.isEmpty()) {
    // connectionInterface = buildConnectionInterface(ni);
    // connectionIp = buildConnectionIp(ni);
    // break;
    // }
    // }
    // }
    // }
    // 
    // if (UNKNOWN.equals(connectionIp) || UNKNOWN.equals(connectionInterface)) {
    // s_logger.warn("Unresolved NetworkService reference or IP address. Defaulting to JVM Networking
    // Information.");
    // InetAddress addr = NetUtil.getCurrentInetAddress();
    // if (addr != null) {
    // connectionIp = addr.getHostAddress();
    // NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr);
    // if (netInterface != null) {
    // connectionInterface = NetUtil.hardwareAddressToString(netInterface.getHardwareAddress());
    // }
    // }
    // }
    // }
    // catch (Exception se) {
    // s_logger.warn("Error while getting ConnetionIP and ConnectionInterface", se);
    // }
    // 
    // get the position information
    double latitude = 0.0;
    double longitude = 0.0;
    double altitude = 0.0;
    if (positionService != null) {
        NmeaPosition position = positionService.getNmeaPosition();
        if (position != null) {
            latitude = position.getLatitude();
            longitude = position.getLongitude();
            altitude = position.getAltitude();
        } else {
            s_logger.warn("Unresolved PositionService reference.");
        }
    }
    // 
    // build the profile
    KuraDeviceProfile KuraDeviceProfile = new KuraDeviceProfile(sysAdminService.getUptime(), systemService.getDeviceName(), systemService.getModelName(), systemService.getModelId(), systemService.getPartNumber(), systemService.getSerialNumber(), systemService.getFirmwareVersion(), systemService.getBiosVersion(), systemService.getOsName(), systemService.getOsVersion(), systemService.getJavaVmName(), systemService.getJavaVmVersion() + " " + systemService.getJavaVmInfo(), systemService.getJavaVendor() + " " + systemService.getJavaVersion(), systemService.getKuraVersion(), connectionInterface, connectionIp, latitude, longitude, altitude, String.valueOf(systemService.getNumberOfProcessors()), String.valueOf(systemService.getTotalMemory()), systemService.getOsArch(), systemService.getOsgiFwName(), systemService.getOsgiFwVersion());
    return KuraDeviceProfile;
}
Also used : SystemAdminService(org.eclipse.kura.system.SystemAdminService) NmeaPosition(org.eclipse.kura.position.NmeaPosition) PositionService(org.eclipse.kura.position.PositionService) SystemService(org.eclipse.kura.system.SystemService) NetInterface(org.eclipse.kura.net.NetInterface) KuraDeviceProfile(org.eclipse.kura.core.message.KuraDeviceProfile) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) NetworkService(org.eclipse.kura.net.NetworkService)

Example 3 with NetInterfaceAddress

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

the class NetworkServiceImpl method getNetInterfaceAddresses.

private List<NetInterfaceAddress> getNetInterfaceAddresses(String interfaceName, NetInterfaceType type, boolean isUp) throws KuraException {
    List<NetInterfaceAddress> netInterfaceAddresses = new ArrayList<NetInterfaceAddress>();
    if (isUp) {
        ConnectionInfo conInfo = new ConnectionInfoImpl(interfaceName);
        NetInterfaceAddressImpl netInterfaceAddress = new NetInterfaceAddressImpl();
        try {
            LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
            if (ifconfig != null) {
                String currentNetmask = ifconfig.getInetMask();
                if (currentNetmask != null) {
                    netInterfaceAddress.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
                    netInterfaceAddress.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
                    netInterfaceAddress.setNetmask(IPAddress.parseHostAddress(currentNetmask));
                    netInterfaceAddress.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(currentNetmask));
                    netInterfaceAddress.setGateway(conInfo.getGateway());
                    if (type == NetInterfaceType.MODEM) {
                        if (isUp) {
                            netInterfaceAddress.setDnsServers(LinuxDns.getInstance().getPppDnServers());
                        }
                    } else {
                        netInterfaceAddress.setDnsServers(conInfo.getDnsServers());
                    }
                    netInterfaceAddresses.add(netInterfaceAddress);
                }
            }
        } catch (UnknownHostException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
        }
    }
    return netInterfaceAddresses;
}
Also used : UnknownHostException(java.net.UnknownHostException) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) ArrayList(java.util.ArrayList) ConnectionInfo(org.eclipse.kura.net.ConnectionInfo) NetInterfaceAddressImpl(org.eclipse.kura.core.net.NetInterfaceAddressImpl) LinuxIfconfig(org.eclipse.kura.linux.net.util.LinuxIfconfig)

Example 4 with NetInterfaceAddress

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

the class SystemServiceImpl method getPrimaryMacAddress.

@Override
public String getPrimaryMacAddress() {
    String primaryNetworkInterfaceName = getPrimaryNetworkInterfaceName();
    String macAddress = null;
    InetAddress ip;
    if (OS_MAC_OSX.equals(getOsName())) {
        SafeProcess proc = null;
        try {
            s_logger.info("executing: ifconfig and looking for " + primaryNetworkInterfaceName);
            proc = ProcessUtil.exec("ifconfig");
            BufferedReader br = null;
            try {
                proc.waitFor();
                br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = br.readLine()) != null) {
                    if (line.startsWith(primaryNetworkInterfaceName)) {
                        // get the next line and save the MAC
                        line = br.readLine();
                        if (line == null) {
                            throw new IOException("Null imput!");
                        }
                        if (!line.trim().startsWith("ether")) {
                            line = br.readLine();
                        }
                        String[] splitLine = line.split(" ");
                        if (splitLine.length > 0) {
                            return splitLine[1].toUpperCase();
                        }
                    }
                }
            } catch (InterruptedException e) {
                s_logger.error("Exception while executing ifconfig!", e);
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException ex) {
                        s_logger.error("I/O Exception while closing BufferedReader!");
                    }
                }
            }
        } catch (Exception e) {
            s_logger.error("Failed to get network interfaces", e);
        } finally {
            if (proc != null) {
                ProcessUtil.destroy(proc);
            }
        }
    } else if (getOsName().contains("Windows")) {
        try {
            s_logger.info("executing: InetAddress.getLocalHost " + primaryNetworkInterfaceName);
            ip = InetAddress.getLocalHost();
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            while (networks.hasMoreElements()) {
                NetworkInterface network = networks.nextElement();
                if ("eth0".equals(network.getName())) {
                    ip = network.getInetAddresses().nextElement();
                    break;
                }
            }
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            byte[] mac = network.getHardwareAddress();
            macAddress = NetUtil.hardwareAddressToString(mac);
            s_logger.info("macAddress " + macAddress);
        } catch (UnknownHostException e) {
            s_logger.error(e.getLocalizedMessage());
        } catch (SocketException e) {
            s_logger.error(e.getLocalizedMessage());
        }
    } else {
        try {
            List<NetInterface<? extends NetInterfaceAddress>> interfaces = this.m_networkService.getNetworkInterfaces();
            if (interfaces != null) {
                for (NetInterface<? extends NetInterfaceAddress> iface : interfaces) {
                    if (iface.getName() != null && getPrimaryNetworkInterfaceName().equals(iface.getName())) {
                        macAddress = NetUtil.hardwareAddressToString(iface.getHardwareAddress());
                        break;
                    }
                }
            }
        } catch (KuraException e) {
            s_logger.error("Failed to get network interfaces", e);
        }
    }
    return macAddress;
}
Also used : SocketException(java.net.SocketException) Enumeration(java.util.Enumeration) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) ComponentException(org.osgi.service.component.ComponentException) SocketException(java.net.SocketException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) KuraException(org.eclipse.kura.KuraException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) InetAddress(java.net.InetAddress)

Example 5 with NetInterfaceAddress

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

Aggregations

NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)14 NetInterface (org.eclipse.kura.net.NetInterface)9 ArrayList (java.util.ArrayList)7 KuraException (org.eclipse.kura.KuraException)7 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)5 SocketException (java.net.SocketException)3 UnknownHostException (java.net.UnknownHostException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 EthernetInterfaceImpl (org.eclipse.kura.core.net.EthernetInterfaceImpl)3 NetInterfaceAddressImpl (org.eclipse.kura.core.net.NetInterfaceAddressImpl)3 IPAddress (org.eclipse.kura.net.IPAddress)3 NetConfig (org.eclipse.kura.net.NetConfig)3 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)3 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)3 ModemInterfaceAddress (org.eclipse.kura.net.modem.ModemInterfaceAddress)3 WifiConfig (org.eclipse.kura.net.wifi.WifiConfig)3 WifiInterfaceAddress (org.eclipse.kura.net.wifi.WifiInterfaceAddress)3 EthernetInterfaceConfigImpl (org.eclipse.kura.core.net.EthernetInterfaceConfigImpl)2 LoopbackInterfaceConfigImpl (org.eclipse.kura.core.net.LoopbackInterfaceConfigImpl)2