diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/helpers/Engine6p0.java')
-rw-r--r-- | app/src/fil/libre/repwifiapp/helpers/Engine6p0.java | 249 |
1 files changed, 62 insertions, 187 deletions
diff --git a/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java b/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java index d4ff0f7..3eff6c8 100644 --- a/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java +++ b/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java @@ -26,16 +26,9 @@ import fil.libre.repwifiapp.Commons; public class Engine6p0 extends Engine { @Override - protected String getCmdWpaStart() { - return "wpa_supplicant -B -dd -i" + Commons.INTERFACE_NAME + " -C" + Commons.SOCKET_DIR - + " -P" + Commons.PID_FILE + " -I" + Commons.OVERLAY_FILE + " -e" - + Commons.ENTROPY_FILE; - } - - @Override public boolean connect(AccessPointInfo info) { - killBackEndProcesses(); + WpaSupplicant.kill(); if (info == null) { Utils.logDebug("Engine's connect() received a null AccessPointInfo"); @@ -62,81 +55,81 @@ public class Engine6p0 extends Engine { // launch wpa_supplicant specifying our custom configuration and the // socket file - if (!executeRootCmd(getCmdWpaStart())) { + if (!WpaSupplicant.start()) { Utils.logDebug("Unable to run wpa start"); return false; } // create new network and get network id - String netID = createNetworkGetId(); + String netID = WpaCli.createNetworkGetId(); if (netID == null) { Utils.logDebug("Unable to fetch network id"); return false; } // set network SSID - if (!setNetworkSSID(info.getSsid(), netID)) { + if (!WpaCli.setNetworkSSID(info.getSsid(), netID)) { Utils.logDebug("Failed to set network ssid"); return false; } - if (info.isHidden() && !setNetworkScanSSID(netID)) { + if (info.isHidden() && !WpaCli.setNetworkScanSSID(netID)) { Utils.logDebug("Failed to set scan_ssid 1 for hidden network."); return false; } // set password (if any) - if (!setNetworkPSK(info, netID)) { + if (!WpaCli.setNetworkPSK(info, netID)) { Utils.logDebug("Failed to set network psk"); return false; } // select the network we just created - if (!selectNetwork(netID)) { + if (!WpaCli.selectNetwork(netID)) { Utils.logDebug("Unable to wpa_cli select network"); return false; } // enable the newtork - if (!enableNetwork(netID)) { + if (!WpaCli.enableNetwork(netID)) { Utils.logDebug("Unable to wpa_cli enable_newtork"); return false; } - - // try to reassociate to Access Point - /* - * if (! reassociate()){ - * Utils.logDebug("Unable to wpa_cli reassociate"); return false; } - */ + + // kill previous dhchcd instances + if (!RootCommand.executeRootCmd("killall -SIGINT dhcpcd")){ + Utils.logError("Unable to kill previous instances of dhcpcd"); + } // get DHCP Utils.logDebug("Attempt to run dhcpcd.."); - if (!runDhcpcd()) { + if (!runDhcpcd(info.getDhcpConfiguration())) { Utils.logDebug("Failed to run dhcpcd"); return false; } // try to fetch gateway - String gw = getGateway(); - if (gw == null || gw.trim().length() < 7) { + String gw = getGateWayTimeout(Commons.WAIT_FOR_GATEWAY); + if (gw == null || !InetAddressUtils.isIPv4Address(gw)) { // failed to get gateway Utils.logDebug("Failed to get gateway"); return false; } - if (!executeRootCmd("ndc network create 1")) { + if (!RootCommand.executeRootCmd("ndc network create 1")) { Utils.logDebug("Failed to wpa_cli network create 1 "); return false; } - if (!executeRootCmd("ndc network interface add 1 " + Commons.INTERFACE_NAME)) { + if (!RootCommand.executeRootCmd("ndc network interface add 1 " + + WpaSupplicant.INTERFACE_NAME)) { Utils.logDebug("Failed to add interface."); return false; } // set route to gateway for all traffic - if (!executeRootCmd("ndc network route add 1 " + Commons.INTERFACE_NAME + " 0.0.0.0/0 " - + gw)) { + if (!RootCommand.executeRootCmd("ndc network route add 1 " + WpaSupplicant.INTERFACE_NAME + + " 0.0.0.0/0 " + gw)) { Utils.logDebug("Failed to add route to gateway"); return false; } @@ -147,7 +140,7 @@ public class Engine6p0 extends Engine { } // use network - if (!executeRootCmd("ndc network default set 1")) { + if (!RootCommand.executeRootCmd("ndc network default set 1")) { Utils.logDebug("Failed to set network as default"); return false; } @@ -156,163 +149,9 @@ public class Engine6p0 extends Engine { } - private String createNetworkGetId() { - - try { - - RootCommand su = new RootCommand(getCmdWpaCli() + " add_network"); - if (su.execute() == 0) { - String out = su.getOutput(); - if (out == null || out.trim().equals("")) { - return null; - } else { - return out.replace("\n", ""); - } - } else { - return null; - } - - } catch (Exception e) { - Utils.logError("Error while creating network", e); - return null; - } - - } - private boolean destroyNetwork() { // needs root (tested) - return executeRootCmd("ndc network destroy 1"); - } - - private boolean setNetworkSSID(String ssid, String networkID) { - - try { - - // needs root (wpa_cli) - RootCommand su = new RootCommand(getCmdWpaCli() + " set_network " + networkID - + " ssid '\"" + ssid + "\"'"); - if (su.execute() == 0) { - String out = su.getOutput(); - if (out != null && out.trim().replace("\n", "").equals("OK")) { - return true; - } else { - return false; - } - } else { - return false; - } - - } catch (Exception e) { - Utils.logError("Error while setting network SSID", e); - return false; - } - - } - - private boolean setNetworkPSK(AccessPointInfo info, String networkID) { - - try { - - // needs root (wpa_cli) - - String cmdSetPass = null; - if (info.needsPassword()) { - cmdSetPass = getCmdWpaCli() + " set_network " + networkID + " psk '\"" - + info.getPassword() + "\"'"; - } else { - cmdSetPass = getCmdWpaCli() + " set_network " + networkID + " key_mgmt NONE"; - } - - RootCommand su = new RootCommand(cmdSetPass); - if (su.execute() == 0) { - String out = su.getOutput(); - if (out != null && out.trim().replace("\n", "").equals("OK")) { - return true; - } else { - return false; - } - } else { - return false; - } - - } catch (Exception e) { - Utils.logError("Error while setting network PSK", e); - return false; - } - - } - - private boolean setNetworkScanSSID(String networkID) { - - try { - - // needs root (wpa_cli) - RootCommand su = new RootCommand(getCmdWpaCli() + " set_network " + networkID - + " scan_ssid 1"); - if (su.execute() == 0) { - String out = su.getOutput(); - if (out != null && out.trim().replace("\n", "").equals("OK")) { - return true; - } else { - return false; - } - } else { - return false; - } - - } catch (Exception e) { - Utils.logError("Error while setting network SSID", e); - return false; - } - } - - private boolean selectNetwork(String networkID) { - - try { - - // needs root (wpa_cli) - RootCommand su = new RootCommand(getCmdWpaCli() + " select_network " + networkID); - if (su.execute() == 0) { - String out = su.getOutput(); - if (out != null && out.trim().replace("\n", "").equals("OK")) { - return true; - } else { - return false; - } - } else { - return false; - } - - } catch (Exception e) { - Utils.logError("Error while selecting network", e); - return false; - } - - } - - private boolean enableNetwork(String networkID) { - - try { - - // needs root (wpa_cli) - - RootCommand su = new RootCommand(getCmdWpaCli() + " enable_network " + networkID); - if (su.execute() == 0) { - String out = su.getOutput(); - if (out != null && out.trim().replace("\n", "").equals("OK")) { - return true; - } else { - return false; - } - } else { - return false; - } - - } catch (Exception e) { - Utils.logError("Error while enabling network", e); - return false; - } - + return RootCommand.executeRootCmd("ndc network destroy 1"); } private boolean setDns(String[] dnss, String gateway) { @@ -343,7 +182,42 @@ public class Engine6p0 extends Engine { cmd += " " + dnss[0]; } - return executeRootCmd(cmd); + return RootCommand.executeRootCmd(cmd); + } + + private String getGateWayTimeout(int timeoutMillis) { + + String gw = getGateway(); + if (gw != null && !gw.trim().isEmpty()) { + return gw; + } + + Utils.logDebug("Gateway not available.. going into wait loop.."); + + // gateway not (yet) available + // waits for a maximum of timeoutMillis milliseconds + // to let the interface being registered. + int msWaited = 0; + while (msWaited < timeoutMillis) { + + try { + Thread.sleep(100); + } catch (Exception e) { + return null; + } + msWaited += 100; + + gw = getGateway(); + if (gw != null && !gw.trim().isEmpty()) { + Utils.logDebug("Gateway found after wait loop!"); + return gw; + } + } + + // unable to get gateway + Utils.logError("Gateway not found after wait loop."); + return null; + } private String getGateway() { @@ -351,7 +225,7 @@ public class Engine6p0 extends Engine { try { // doesn't need root (tested) - RootCommand cmd = new RootCommand("ip route show dev " + Commons.INTERFACE_NAME); + ShellCommand cmd = new ShellCommand("ip route show dev " + WpaSupplicant.INTERFACE_NAME); if (cmd.execute() != 0) { Utils.logDebug("command failed show route"); return null; @@ -390,7 +264,8 @@ public class Engine6p0 extends Engine { private boolean clearAddrs() { // needs root (tested) - return executeRootCmd("ndc interface clearaddrs " + Commons.INTERFACE_NAME); + return RootCommand.executeRootCmd("ndc interface clearaddrs " + + WpaSupplicant.INTERFACE_NAME); } }
\ No newline at end of file |