use of org.eclipse.kura.net.IPAddress 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.net.IPAddress 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;
}
use of org.eclipse.kura.net.IPAddress 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;
}
Aggregations