Search in sources :

Example 1 with NetInterfaceAddressImpl

use of org.eclipse.kura.core.net.NetInterfaceAddressImpl 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 2 with NetInterfaceAddressImpl

use of org.eclipse.kura.core.net.NetInterfaceAddressImpl in project kura by eclipse.

the class EmulatedNetworkServiceImpl method getActiveNetworkInterfaces.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public List<NetInterface<? extends NetInterfaceAddress>> getActiveNetworkInterfaces() throws KuraException {
    IPAddress netAddress = null;
    NetInterfaceAddressImpl addressImpl = null;
    List<NetInterfaceAddress> addresses = null;
    List<NetInterface<? extends NetInterfaceAddress>> interfaces = new ArrayList<NetInterface<? extends NetInterfaceAddress>>();
    EthernetInterfaceImpl ethInterface = null;
    java.net.NetworkInterface jnInterface = null;
    List<java.net.InterfaceAddress> jnInterfaceAddresses = null;
    Enumeration<java.net.NetworkInterface> jnInterfaces = null;
    try {
        jnInterfaces = java.net.NetworkInterface.getNetworkInterfaces();
        while (jnInterfaces.hasMoreElements()) {
            try {
                jnInterface = jnInterfaces.nextElement();
                if (jnInterface.isUp() && !jnInterface.isVirtual() && !jnInterface.isLoopback() && !jnInterface.isPointToPoint() && jnInterface.getHardwareAddress() != null && !jnInterface.getInterfaceAddresses().isEmpty()) {
                    ethInterface = new EthernetInterfaceImpl(jnInterface.getName());
                    ethInterface.setVirtual(jnInterface.isVirtual());
                    ethInterface.setState(NetInterfaceState.ACTIVATED);
                    ethInterface.setAutoConnect(true);
                    byte[] hwAddr = null;
                    boolean isUp = false;
                    boolean isLoop = false;
                    int mtu = 0;
                    boolean isP2p = false;
                    boolean multi = false;
                    try {
                        hwAddr = jnInterface.getHardwareAddress();
                        isUp = jnInterface.isUp();
                        isLoop = jnInterface.isLoopback();
                        mtu = jnInterface.getMTU();
                        isP2p = jnInterface.isPointToPoint();
                        multi = jnInterface.supportsMulticast();
                    } catch (Exception e) {
                        s_logger.warn("Exception while getting information for interface " + jnInterface.getName(), e);
                    }
                    ethInterface.setHardwareAddress(hwAddr);
                    ethInterface.setLinkUp(isUp);
                    ethInterface.setLoopback(isLoop);
                    ethInterface.setMTU(mtu);
                    ethInterface.setPointToPoint(isP2p);
                    ethInterface.setSupportsMulticast(multi);
                    ethInterface.setUp(isUp);
                    addresses = new ArrayList<NetInterfaceAddress>();
                    jnInterfaceAddresses = jnInterface.getInterfaceAddresses();
                    for (java.net.InterfaceAddress jnInterfaceAddress : jnInterfaceAddresses) {
                        netAddress = IPAddress.getByAddress(jnInterfaceAddress.getAddress().getAddress());
                        addressImpl = new NetInterfaceAddressImpl();
                        addressImpl.setAddress(netAddress);
                        if (jnInterfaceAddress.getBroadcast() != null) {
                            addressImpl.setBroadcast(IPAddress.getByAddress(jnInterfaceAddress.getBroadcast().getAddress()));
                        }
                        addressImpl.setNetworkPrefixLength(jnInterfaceAddress.getNetworkPrefixLength());
                        addresses.add(addressImpl);
                    }
                    ethInterface.setNetInterfaceAddresses(addresses);
                    interfaces.add(ethInterface);
                }
            } catch (SocketException se) {
                s_logger.warn("Exception while getting information for interface " + jnInterface.getName(), se);
            }
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    return interfaces;
}
Also used : SocketException(java.net.SocketException) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) ArrayList(java.util.ArrayList) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) SocketException(java.net.SocketException) KuraException(org.eclipse.kura.KuraException) EthernetInterfaceImpl(org.eclipse.kura.core.net.EthernetInterfaceImpl) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) NetInterfaceAddressImpl(org.eclipse.kura.core.net.NetInterfaceAddressImpl) IPAddress(org.eclipse.kura.net.IPAddress)

Example 3 with NetInterfaceAddressImpl

use of org.eclipse.kura.core.net.NetInterfaceAddressImpl in project kura by eclipse.

the class EmulatedNetworkServiceImpl method getNetworkInterfaces.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public List<NetInterface<? extends NetInterfaceAddress>> getNetworkInterfaces() throws KuraException {
    IPAddress netAddress = null;
    NetInterfaceAddressImpl addressImpl = null;
    List<NetInterfaceAddress> addresses = null;
    List<NetInterface<? extends NetInterfaceAddress>> interfaces = new ArrayList<NetInterface<? extends NetInterfaceAddress>>();
    EthernetInterfaceImpl ethInterface = null;
    java.net.NetworkInterface jnInterface = null;
    List<java.net.InterfaceAddress> jnInterfaceAddresses = null;
    Enumeration<java.net.NetworkInterface> jnInterfaces = null;
    try {
        jnInterfaces = java.net.NetworkInterface.getNetworkInterfaces();
        while (jnInterfaces.hasMoreElements()) {
            jnInterface = jnInterfaces.nextElement();
            ethInterface = new EthernetInterfaceImpl(jnInterface.getName());
            ethInterface.setVirtual(jnInterface.isVirtual());
            ethInterface.setState(NetInterfaceState.ACTIVATED);
            ethInterface.setAutoConnect(true);
            byte[] hwAddr = null;
            boolean isUp = false;
            boolean isLoop = false;
            int mtu = 0;
            boolean isP2p = false;
            boolean multi = false;
            try {
                hwAddr = jnInterface.getHardwareAddress();
                isUp = jnInterface.isUp();
                isLoop = jnInterface.isLoopback();
                mtu = jnInterface.getMTU();
                isP2p = jnInterface.isPointToPoint();
                multi = jnInterface.supportsMulticast();
            } catch (Exception e) {
                s_logger.warn("Exception while getting information for interface " + jnInterface.getName() + ": " + e.getMessage());
            }
            ethInterface.setHardwareAddress(hwAddr);
            ethInterface.setLinkUp(isUp);
            ethInterface.setLoopback(isLoop);
            ethInterface.setMTU(mtu);
            ethInterface.setPointToPoint(isP2p);
            ethInterface.setSupportsMulticast(multi);
            ethInterface.setUp(isUp);
            addresses = new ArrayList<NetInterfaceAddress>();
            jnInterfaceAddresses = jnInterface.getInterfaceAddresses();
            for (java.net.InterfaceAddress jnInterfaceAddress : jnInterfaceAddresses) {
                netAddress = IPAddress.getByAddress(jnInterfaceAddress.getAddress().getAddress());
                addressImpl = new NetInterfaceAddressImpl();
                addressImpl.setAddress(netAddress);
                if (jnInterfaceAddress.getBroadcast() != null) {
                    addressImpl.setBroadcast(IPAddress.getByAddress(jnInterfaceAddress.getBroadcast().getAddress()));
                }
                addressImpl.setNetworkPrefixLength(jnInterfaceAddress.getNetworkPrefixLength());
                addresses.add(addressImpl);
            }
            ethInterface.setNetInterfaceAddresses(addresses);
            interfaces.add(ethInterface);
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    return interfaces;
}
Also used : NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) ArrayList(java.util.ArrayList) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) SocketException(java.net.SocketException) KuraException(org.eclipse.kura.KuraException) EthernetInterfaceImpl(org.eclipse.kura.core.net.EthernetInterfaceImpl) NetInterface(org.eclipse.kura.net.NetInterface) NetInterfaceAddress(org.eclipse.kura.net.NetInterfaceAddress) KuraException(org.eclipse.kura.KuraException) NetInterfaceAddressImpl(org.eclipse.kura.core.net.NetInterfaceAddressImpl) IPAddress(org.eclipse.kura.net.IPAddress)

Aggregations

ArrayList (java.util.ArrayList)3 KuraException (org.eclipse.kura.KuraException)3 NetInterfaceAddressImpl (org.eclipse.kura.core.net.NetInterfaceAddressImpl)3 NetInterfaceAddress (org.eclipse.kura.net.NetInterfaceAddress)3 SocketException (java.net.SocketException)2 EthernetInterfaceImpl (org.eclipse.kura.core.net.EthernetInterfaceImpl)2 IPAddress (org.eclipse.kura.net.IPAddress)2 NetInterface (org.eclipse.kura.net.NetInterface)2 WifiAccessPoint (org.eclipse.kura.net.wifi.WifiAccessPoint)2 UnknownHostException (java.net.UnknownHostException)1 LinuxIfconfig (org.eclipse.kura.linux.net.util.LinuxIfconfig)1 ConnectionInfo (org.eclipse.kura.net.ConnectionInfo)1