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;
}
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;
}
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;
}
Aggregations