use of org.eclipse.kura.net.wifi.WifiConfig in project kura by eclipse.
the class GwtNetworkServiceImpl method updateNetInterfaceConfigurations.
@Override
public void updateNetInterfaceConfigurations(GwtXSRFToken xsrfToken, GwtNetInterfaceConfig config) throws GwtKuraException {
checkXSRFToken(xsrfToken);
NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
s_logger.debug("config.getStatus(): {}", GwtSafeHtmlUtils.htmlEscape(config.getStatus()));
String status = config.getStatus();
boolean autoConnect = true;
if (GwtNetIfStatus.netIPv4StatusDisabled.name().equals(status)) {
autoConnect = false;
}
try {
// Interface status
NetInterfaceStatus netInterfaceStatus = null;
if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusDisabled.name())) {
netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
} else if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusEnabledLAN.name())) {
netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledLAN;
} else if (config.getStatus().equals(GwtNetIfStatus.netIPv4StatusEnabledWAN.name())) {
netInterfaceStatus = NetInterfaceStatus.netIPv4StatusEnabledWAN;
}
// Set up configs
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
// Initialize NetConfigIP4 object
NetConfigIP4 netConfig4 = new NetConfigIP4(netInterfaceStatus, autoConnect);
// build the appropriate NetConfig objects for ethernet type
if (config.getHwTypeEnum() == GwtNetIfType.ETHERNET || config.getHwTypeEnum() == GwtNetIfType.WIFI || config.getHwTypeEnum() == GwtNetIfType.MODEM) {
s_logger.debug("config.getConfigMode(): {}", config.getConfigMode());
String regexp = "[\\s,;\\n\\t]+";
if (GwtNetIfConfigMode.netIPv4ConfigModeDHCP.name().equals(config.getConfigMode())) {
s_logger.debug("mode is DHCP");
netConfig4.setDhcp(true);
} else {
s_logger.debug("mode is STATIC");
netConfig4.setDhcp(false);
if (config.getIpAddress() != null && !config.getIpAddress().isEmpty()) {
s_logger.debug("setting address: {}", config.getIpAddress());
netConfig4.setAddress((IP4Address) IPAddress.parseHostAddress(config.getIpAddress()));
}
if (config.getSubnetMask() != null && !config.getSubnetMask().isEmpty()) {
s_logger.debug("setting subnet mask: {}", config.getSubnetMask());
netConfig4.setSubnetMask((IP4Address) IPAddress.parseHostAddress(config.getSubnetMask()));
}
if (config.getGateway() != null && !config.getGateway().isEmpty()) {
s_logger.debug("setting gateway: {}", config.getGateway());
netConfig4.setGateway((IP4Address) IPAddress.parseHostAddress(config.getGateway()));
}
String[] winServersString = config.getSearchDomains().split(regexp);
if (winServersString != null && winServersString.length > 0) {
IP4Address winServer;
List<IP4Address> dnsServers = new ArrayList<IP4Address>();
for (String winsEntry : winServersString) {
if (!winsEntry.trim().isEmpty()) {
s_logger.debug("setting WINs: {}", winsEntry);
winServer = (IP4Address) IPAddress.parseHostAddress(winsEntry);
dnsServers.add(winServer);
}
}
netConfig4.setDnsServers(dnsServers);
}
}
String[] dnsServersString = config.getDnsServers().split(regexp);
if (dnsServersString != null && dnsServersString.length > 0) {
IP4Address dnsServer;
List<IP4Address> dnsServers = new ArrayList<IP4Address>();
for (String dnsEntry : dnsServersString) {
if (!dnsEntry.trim().isEmpty()) {
s_logger.debug("setting DNS: {}", dnsEntry);
dnsServer = (IP4Address) IPAddress.parseHostAddress(dnsEntry);
dnsServers.add(dnsServer);
}
}
netConfig4.setDnsServers(dnsServers);
}
netConfigs.add(netConfig4);
// Set up DHCP and NAT
if (!GwtNetIfConfigMode.netIPv4ConfigModeDHCP.name().equals(config.getConfigMode())) {
// <--
List<NetConfig> dhcpConfigs = getDhcpConfig(config);
if (dhcpConfigs != null) {
s_logger.debug("Adding dhcp and/or nat configs to interface update config");
netConfigs.addAll(dhcpConfigs);
}
}
if (config.getHwTypeEnum() == GwtNetIfType.ETHERNET) {
nas.updateEthernetInterfaceConfig(GwtSafeHtmlUtils.htmlEscape(config.getName()), autoConnect, config.getHwMTU(), netConfigs);
}
}
if (config.getHwTypeEnum() == GwtNetIfType.WIFI) {
if (config instanceof GwtWifiNetInterfaceConfig) {
GwtWifiConfig gwtWifiConfig = ((GwtWifiNetInterfaceConfig) config).getActiveWifiConfig();
if (gwtWifiConfig != null) {
WifiConfig wifiConfig = getWifiConfig(gwtWifiConfig);
String passKey = new String(wifiConfig.getPasskey().getPassword());
if (passKey != null && passKey.equals(PLACEHOLDER)) {
List<GwtNetInterfaceConfig> result = privateFindNetInterfaceConfigurations();
for (GwtNetInterfaceConfig netConfig : result) {
if (netConfig instanceof GwtWifiNetInterfaceConfig && config.getName().equals(((GwtWifiNetInterfaceConfig) netConfig).getName())) {
GwtWifiNetInterfaceConfig oldWifiConfig = (GwtWifiNetInterfaceConfig) netConfig;
GwtWifiConfig oldGwtWifiConfig;
if (gwtWifiConfig.getWirelessMode().equals(GwtWifiWirelessMode.netWifiWirelessModeAccessPoint.name())) {
oldGwtWifiConfig = oldWifiConfig.getAccessPointWifiConfig();
} else {
oldGwtWifiConfig = oldWifiConfig.getStationWifiConfig();
}
if (oldGwtWifiConfig != null) {
wifiConfig.setPasskey(oldGwtWifiConfig.getPassword());
}
}
}
}
netConfigs.add(wifiConfig);
nas.updateWifiInterfaceConfig(GwtSafeHtmlUtils.htmlEscape(config.getName()), autoConnect, null, netConfigs);
}
}
} else if (config.getHwTypeEnum() == GwtNetIfType.MODEM) {
if (config instanceof GwtModemInterfaceConfig) {
GwtModemInterfaceConfig gwtModemConfig = (GwtModemInterfaceConfig) config;
ModemConfig modemConfig = new ModemConfig();
String serialNum = gwtModemConfig.getHwSerial();
String modemId = gwtModemConfig.getModemId();
int pppNum = gwtModemConfig.getPppNum();
// modem enabled/disabled
if (netInterfaceStatus.equals(NetInterfaceStatus.netIPv4StatusEnabledWAN)) {
modemConfig.setEnabled(true);
} else {
modemConfig.setEnabled(false);
}
modemConfig.setApn(gwtModemConfig.getApn());
modemConfig.setPppNumber(gwtModemConfig.getPppNum());
modemConfig.setDataCompression(gwtModemConfig.getDataCompression());
modemConfig.setDialString(gwtModemConfig.getDialString());
modemConfig.setHeaderCompression(gwtModemConfig.getHeaderCompression());
String passKey = new String(gwtModemConfig.getPassword());
if (passKey != null && passKey.equals(PLACEHOLDER)) {
List<GwtNetInterfaceConfig> result = privateFindNetInterfaceConfigurations();
for (GwtNetInterfaceConfig netConfig : result) {
if (netConfig instanceof GwtModemInterfaceConfig) {
GwtModemInterfaceConfig oldModemConfig = (GwtModemInterfaceConfig) netConfig;
if (gwtModemConfig.getName().equals(oldModemConfig.getName())) {
modemConfig.setPassword(oldModemConfig.getPassword());
}
}
}
} else if (passKey != null) {
modemConfig.setPassword(passKey);
}
modemConfig.setUsername(gwtModemConfig.getUsername());
modemConfig.setResetTimeout(gwtModemConfig.getResetTimeout());
modemConfig.setPersist(gwtModemConfig.isPersist());
modemConfig.setMaxFail(gwtModemConfig.getMaxFail());
modemConfig.setIdle(gwtModemConfig.getIdle());
modemConfig.setActiveFilter(gwtModemConfig.getActiveFilter());
modemConfig.setLcpEchoInterval(gwtModemConfig.getLcpEchoInterval());
modemConfig.setLcpEchoFailure(gwtModemConfig.getLcpEchoFailure());
modemConfig.setGpsEnabled(gwtModemConfig.isGpsEnabled());
GwtModemAuthType authType = gwtModemConfig.getAuthType();
if (authType != null) {
if (authType.equals(GwtModemAuthType.netModemAuthNONE)) {
modemConfig.setAuthType(ModemConfig.AuthType.NONE);
} else if (authType.equals(GwtModemAuthType.netModemAuthAUTO)) {
modemConfig.setAuthType(ModemConfig.AuthType.AUTO);
} else if (authType.equals(GwtModemAuthType.netModemAuthCHAP)) {
modemConfig.setAuthType(ModemConfig.AuthType.CHAP);
} else if (authType.equals(GwtModemAuthType.netModemAuthPAP)) {
modemConfig.setAuthType(ModemConfig.AuthType.PAP);
}
}
GwtModemPdpType pdpType = gwtModemConfig.getPdpType();
if (pdpType != null) {
if (pdpType.equals(GwtModemPdpType.netModemPdpIP)) {
modemConfig.setPdpType(ModemConfig.PdpType.IP);
} else if (pdpType.equals(GwtModemPdpType.netModemPdpIPv6)) {
modemConfig.setPdpType(ModemConfig.PdpType.IPv6);
} else if (pdpType.equals(GwtModemPdpType.netModemPdpPPP)) {
modemConfig.setPdpType(ModemConfig.PdpType.PPP);
} else {
modemConfig.setPdpType(ModemConfig.PdpType.UNKNOWN);
}
}
netConfigs.add(modemConfig);
nas.updateModemInterfaceConfig(config.getName(), serialNum, modemId, pppNum, autoConnect, -1, netConfigs);
}
} else {
// TODO - more types
}
} catch (Exception e) {
s_logger.warn("Failed", e);
throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
}
}
use of org.eclipse.kura.net.wifi.WifiConfig 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);
}
}
use of org.eclipse.kura.net.wifi.WifiConfig in project kura by eclipse.
the class HostapdConfigReader method getWifiHostConfig.
private static WifiConfig getWifiHostConfig(String ifaceName) throws KuraException {
try {
WifiConfig wifiConfig = new WifiConfig();
wifiConfig.setMode(WifiMode.MASTER);
File configFile = new File(HostapdManager.getHostapdConfigFileName(ifaceName));
Properties hostapdProps = new Properties();
s_logger.debug("parsing hostapd config file: " + configFile.getAbsolutePath());
if (configFile.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(configFile);
hostapdProps.load(fis);
} finally {
if (null != fis) {
fis.close();
}
}
// remove any quotes around the values
Enumeration<Object> keys = hostapdProps.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement().toString();
String val = hostapdProps.getProperty(key);
if (val.startsWith("\"") && val.endsWith("\"") && val.length() > 1) {
hostapdProps.setProperty(key, val.substring(1, val.length() - 1));
}
}
String iface = hostapdProps.getProperty("interface");
if (ifaceName != null && ifaceName.equals(iface)) {
String driver = hostapdProps.getProperty("driver");
String essid = hostapdProps.getProperty("ssid");
int channel = Integer.parseInt(hostapdProps.getProperty("channel"));
int ignoreSSID = Integer.parseInt(hostapdProps.getProperty("ignore_broadcast_ssid"));
// Determine radio mode
WifiRadioMode wifiRadioMode = null;
String hwModeStr = hostapdProps.getProperty("hw_mode");
if ("a".equals(hwModeStr)) {
wifiRadioMode = WifiRadioMode.RADIO_MODE_80211a;
} else if ("b".equals(hwModeStr)) {
wifiRadioMode = WifiRadioMode.RADIO_MODE_80211b;
} else if ("g".equals(hwModeStr)) {
wifiRadioMode = WifiRadioMode.RADIO_MODE_80211g;
if ("1".equals(hostapdProps.getProperty("ieee80211n"))) {
wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT20;
String ht_capab = hostapdProps.getProperty("ht_capab");
if (ht_capab != null) {
if (ht_capab.contains("HT40+")) {
wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40above;
} else if (ht_capab.contains("HT40-")) {
wifiRadioMode = WifiRadioMode.RADIO_MODE_80211nHT40below;
}
}
}
} else {
throw KuraException.internalError("malformatted config file, unexpected hw_mode: " + configFile.getAbsolutePath());
}
// Determine security and pass
WifiSecurity security = WifiSecurity.SECURITY_NONE;
String password = "";
if (hostapdProps.containsKey("wpa")) {
if ("1".equals(hostapdProps.getProperty("wpa"))) {
security = WifiSecurity.SECURITY_WPA;
} else if ("2".equals(hostapdProps.getProperty("wpa"))) {
security = WifiSecurity.SECURITY_WPA2;
} else if ("3".equals(hostapdProps.getProperty("wpa"))) {
security = WifiSecurity.SECURITY_WPA_WPA2;
} else {
throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
}
if (hostapdProps.containsKey("wpa_passphrase")) {
password = hostapdProps.getProperty("wpa_passphrase");
} else if (hostapdProps.containsKey("wpa_psk")) {
password = hostapdProps.getProperty("wpa_psk");
} else {
throw KuraException.internalError("malformatted config file, no wpa passphrase: " + configFile.getAbsolutePath());
}
} else if (hostapdProps.containsKey("wep_key0")) {
security = WifiSecurity.SECURITY_WEP;
password = hostapdProps.getProperty("wep_key0");
}
WifiCiphers pairwise = null;
if (hostapdProps.containsKey("wpa_pairwise")) {
if ("TKIP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
pairwise = WifiCiphers.TKIP;
} else if ("CCMP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
pairwise = WifiCiphers.CCMP;
} else if ("CCMP TKIP".equals(hostapdProps.getProperty("wpa_pairwise"))) {
pairwise = WifiCiphers.CCMP_TKIP;
} else {
throw KuraException.internalError("malformatted config file: " + configFile.getAbsolutePath());
}
}
// Populate the config
wifiConfig.setSSID(essid);
wifiConfig.setDriver(driver);
wifiConfig.setChannels(new int[] { channel });
wifiConfig.setPasskey(password);
wifiConfig.setSecurity(security);
wifiConfig.setPairwiseCiphers(pairwise);
wifiConfig.setRadioMode(wifiRadioMode);
if (ignoreSSID == 0) {
wifiConfig.setIgnoreSSID(false);
wifiConfig.setBroadcast(true);
} else {
wifiConfig.setIgnoreSSID(true);
wifiConfig.setBroadcast(false);
}
// hw mode
if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211b) {
wifiConfig.setHardwareMode("b");
} else if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211g) {
wifiConfig.setHardwareMode("g");
} else if (wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT20 || wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT40above || wifiRadioMode == WifiRadioMode.RADIO_MODE_80211nHT40below) {
// TODO: specify these 'n' modes separately?
wifiConfig.setHardwareMode("n");
}
}
} else {
s_logger.warn("getWifiHostConfig() :: {} file doesn't exist, will generate default wifiConfig", configFile.getName());
wifiConfig.setSSID("kura_gateway");
wifiConfig.setDriver("nl80211");
wifiConfig.setChannels(new int[] { 11 });
wifiConfig.setPasskey("");
wifiConfig.setSecurity(WifiSecurity.SECURITY_NONE);
wifiConfig.setPairwiseCiphers(WifiCiphers.CCMP);
wifiConfig.setRadioMode(WifiRadioMode.RADIO_MODE_80211b);
wifiConfig.setIgnoreSSID(false);
wifiConfig.setBroadcast(true);
wifiConfig.setHardwareMode("b");
}
return wifiConfig;
} catch (Exception e) {
s_logger.error("Exception getting WiFi configuration", e);
throw KuraException.internalError(e);
}
}
use of org.eclipse.kura.net.wifi.WifiConfig in project kura by eclipse.
the class WifiMonitorServiceImpl method internalWifiConfigCompare.
private void internalWifiConfigCompare(Set<String> reconfiguredInterfaces, String interfaceName, List<NetConfig> currentNetConfigs, List<NetConfig> newNetConfigs) {
for (int i = 0; i < currentNetConfigs.size(); i++) {
NetConfig currentNetConfig = currentNetConfigs.get(i);
if (currentNetConfig instanceof FirewallAutoNatConfig) {
// we don't compare FirewallAutoNatConfig instances
continue;
}
for (int j = 0; j < newNetConfigs.size(); j++) {
NetConfig newNetConfig = newNetConfigs.get(j);
if (newNetConfig instanceof FirewallAutoNatConfig) {
// we don't compare FirewallAutoNatConfig instances
continue;
}
if (newNetConfig.getClass() == currentNetConfig.getClass() && !newNetConfig.equals(currentNetConfig) && !(currentNetConfig instanceof WifiConfig && ((WifiConfig) currentNetConfig).getMode() != ((WifiConfig) newNetConfig).getMode())) {
// ((WifiConfig)
// currentNetConfig).getMode()
// != newWifiMode
// we are not entering here if we are comparing WifiConfig instances and the mode differs. Two
// instances
// of WifiConfig exist: one with mode= MASTER and one with mode= INFRA.
// we try to compare only objects with the same mode, in order to have a correct comparison.
s_logger.debug("\tConfig changed - Old config: {}", currentNetConfig);
s_logger.debug("\tConfig changed - New config: {}", newNetConfig);
reconfiguredInterfaces.add(interfaceName);
return;
}
}
}
}
use of org.eclipse.kura.net.wifi.WifiConfig in project kura by eclipse.
the class WifiMonitorServiceImpl method monitor.
private void monitor() {
synchronized (s_lock) {
NetworkConfiguration newNetConfiguration = this.m_newNetConfiguration;
try {
// Track the interfaces being reconfigured
List<String> interfacesToReconfigure = new ArrayList<String>();
// Check to see if the configuration has changed
// s_logger.debug("m_newNetConfiguration: " + m_newNetConfiguration);
// s_logger.debug("m_currentNetworkConfiguration: " + m_currentNetworkConfiguration);
s_logger.debug("monitor() :: wifi has started another run ...");
// Find and disable interfaces affected by the configuration change
if (newNetConfiguration != null && !newNetConfiguration.equals(this.m_currentNetworkConfiguration)) {
s_logger.info("monitor() :: Found a new network configuration, will check if wifi has been reconfigured ...");
// Note that the call to getReconfiguredWifiInterfaces() may also update
// m_enabledInterfaces or m_disabledInterfaces
interfacesToReconfigure.addAll(getReconfiguredWifiInterfaces());
this.m_currentNetworkConfiguration = newNetConfiguration;
// interface statuses, a call to WifiState.isUp() should return false.
for (String interfaceName : interfacesToReconfigure) {
s_logger.debug("monitor() :: configuration has changed for {} , disabling...", interfaceName);
disableInterface(interfaceName);
}
}
// Check all interfaces configured to be enabled.
// This includes the interfaces that might have been enabled by the above configuration change.
// Get fresh interface statuses and post status change events.
Map<String, InterfaceState> newStatuses = getInterfaceStatuses(this.m_enabledInterfaces);
checkStatusChange(this.m_interfaceStatuses, newStatuses);
this.m_interfaceStatuses = newStatuses;
for (String interfaceName : this.m_enabledInterfaces) {
// Get current configuration
WifiInterfaceConfigImpl wifiInterfaceConfig = (WifiInterfaceConfigImpl) this.m_currentNetworkConfiguration.getNetInterfaceConfig(interfaceName);
WifiConfig wifiConfig = getWifiConfig(wifiInterfaceConfig);
// Make sure we have enough information
if (wifiInterfaceConfig == null) {
s_logger.warn("monitor() :: missing WifiInterfaceConfigImpl for {}", interfaceName);
continue;
}
if (wifiConfig == null) {
s_logger.warn("monitor() :: missing WifiConfig for {}", interfaceName);
continue;
}
// If not we treat the interface as if needing to be reconfigured.
if (this.m_first && !LinuxNetworkUtil.isKernelModuleLoadedForMode(interfaceName, wifiConfig.getMode())) {
s_logger.info("monitor() :: {} kernel module not suitable for WiFi mode {}", interfaceName, wifiConfig.getMode());
this.m_first = false;
interfacesToReconfigure.add(interfaceName);
disableInterface(interfaceName);
// Update the current wifi state
m_interfaceStatuses.remove(interfaceName);
m_interfaceStatuses.put(interfaceName, new InterfaceState(NetInterfaceType.WIFI, interfaceName));
}
// Get current state
InterfaceState wifiState = this.m_interfaceStatuses.get(interfaceName);
if (wifiState == null) {
s_logger.warn("monitor() :: missing InterfaceState for {}", interfaceName);
continue;
}
// s_logger.debug("Evaluating: " + interfaceName + " and is currently up? " + wifiState.isUp());
// s_logger.debug("Evaluating: " + interfaceName + " and is currently link up? " +
// wifiState.isLinkUp());
// This flag is changed if the interface is disabled intentionally by the code below
boolean up = wifiState.isUp();
if (up) {
// FIXME should we just disable it like in the Infrastructure case above?
if (WifiMode.INFRA.equals(wifiConfig.getMode())) {
// get signal strength only if somebody needs it
if (this.m_listeners != null && this.m_listeners.size() > 0) {
int rssi = 0;
try {
s_logger.debug("monitor() :: Getting Signal Level for {} -> {}", interfaceName, wifiConfig.getSSID());
rssi = getSignalLevel(interfaceName, wifiConfig.getSSID());
s_logger.debug("monitor() :: Wifi RSSI is {}", rssi);
} catch (KuraException e) {
s_logger.error("monitor() :: Failed to get Signal Level for {} -> {}", interfaceName, wifiConfig.getSSID());
s_logger.error("monitor() :: Failed to get Signal Level - {}", e);
rssi = 0;
}
for (WifiClientMonitorListener listener : this.m_listeners) {
listener.setWifiSignalLevel(rssi);
}
}
if (!wifiState.isLinkUp()) {
s_logger.debug("monitor() :: link is down - disabling {}", interfaceName);
disableInterface(interfaceName);
up = false;
}
s_logger.debug("monitor() :: pingAccessPoint()? {}", wifiConfig.pingAccessPoint());
if (wifiConfig.pingAccessPoint()) {
NetConfigIP4 netConfigIP4 = getIP4config(wifiInterfaceConfig);
if (netConfigIP4 != null && netConfigIP4.isDhcp()) {
boolean isApReachable = false;
for (int i = 0; i < 3; i++) {
isApReachable = isAccessPointReachable(interfaceName, 1000);
if (isApReachable) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
if (!isApReachable) {
this.m_netAdminService.renewDhcpLease(interfaceName);
}
}
}
NetConfigIP4 netConfigIP4 = getIP4config(wifiInterfaceConfig);
if (netConfigIP4.getStatus().equals(NetInterfaceStatus.netIPv4StatusEnabledLAN)) {
if (netConfigIP4.isDhcp()) {
RouteService rs = RouteServiceImpl.getInstance();
RouteConfig rconf = rs.getDefaultRoute(interfaceName);
if (rconf != null) {
s_logger.debug("monitor() :: {} is configured for LAN/DHCP - removing GATEWAY route ...", rconf.getInterfaceName());
rs.removeStaticRoute(rconf.getDestination(), rconf.getGateway(), rconf.getNetmask(), rconf.getInterfaceName());
}
}
}
} else if (WifiMode.MASTER.equals(wifiConfig.getMode())) {
if (!wifiState.isLinkUp()) {
// disabling interface is probably needed to handle potential driver issues.
s_logger.warn("monitor() :: !! Link is down for the {} in AP mode, while IP address is assigned. Will disable and reenable interface ...", interfaceName);
disableInterface(interfaceName);
enableInterface(wifiInterfaceConfig);
}
}
}
// * just enable interface
if (!up) {
// FIXME if reloading fails it won't be retried.
if (interfacesToReconfigure.contains(interfaceName)) {
try {
s_logger.info("monitor() :: reload {} kernel module for WiFi mode {}", interfaceName, wifiConfig.getMode());
reloadKernelModule(interfaceName, wifiConfig.getMode());
} catch (KuraException e) {
s_logger.warn("monitor() :: failed to reload {} kernel module." + " FIXME: THIS WON'T BE RETRIED", interfaceName, e);
continue;
}
}
try {
if (WifiMode.MASTER.equals(wifiConfig.getMode())) {
s_logger.debug("monitor() :: enable {} in master mode", interfaceName);
enableInterface(wifiInterfaceConfig);
} else if (WifiMode.INFRA.equals(wifiConfig.getMode())) {
if (wifiConfig.ignoreSSID()) {
s_logger.info("monitor() :: enable {} in infra mode", interfaceName);
enableInterface(wifiInterfaceConfig);
} else {
if (isAccessPointAvailable(interfaceName, wifiConfig.getSSID())) {
s_logger.info("monitor() :: found access point - enable {} in infra mode", interfaceName);
enableInterface(wifiInterfaceConfig);
} else {
s_logger.warn("monitor() :: {} - access point is not available", wifiConfig.getSSID());
}
}
}
} catch (KuraException e) {
s_logger.error("monitor() :: Error enabling {} interface, will try to reset wifi", interfaceName, e);
resetWifiDevice(interfaceName);
}
}
}
// Check all interfaces configured to be disabled
for (String interfaceName : this.m_disabledInterfaces) {
InterfaceState wifiState = this.m_interfaceStatuses.get(interfaceName);
if (wifiState != null && wifiState.isUp()) {
s_logger.debug("monitor() :: {} is currently up - disable interface", interfaceName);
disableInterface(interfaceName);
}
}
// Shut down the monitor if no interface is configured to be enabled
if (this.m_enabledInterfaces.isEmpty() && monitorTask != null) {
s_logger.info("monitor() :: No enabled wifi interfaces - shutting down monitor thread");
stopThread.set(true);
monitorTask.cancel(true);
monitorTask = null;
}
} catch (Exception e) {
s_logger.warn("Error during WiFi Monitor handle event", e);
}
}
}
Aggregations