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