diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/helpers/Engine6p0.java')
-rw-r--r-- | app/src/fil/libre/repwifiapp/helpers/Engine6p0.java | 271 |
1 files changed, 0 insertions, 271 deletions
diff --git a/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java b/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java deleted file mode 100644 index 3eff6c8..0000000 --- a/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java +++ /dev/null @@ -1,271 +0,0 @@ -// -// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net> -// -// This file is part of RepWifiApp. -// -// RepWifiApp is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// RepWifiApp is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with RepWifiApp. If not, see <http://www.gnu.org/licenses/>. -// -// ******************************************************************** - -package fil.libre.repwifiapp.helpers; - -import org.apache.http.conn.util.InetAddressUtils; -import fil.libre.repwifiapp.Commons; - -public class Engine6p0 extends Engine { - - @Override - public boolean connect(AccessPointInfo info) { - - WpaSupplicant.kill(); - - if (info == null) { - Utils.logDebug("Engine's connect() received a null AccessPointInfo"); - return false; - } - - // clear any previously set network - if (!destroyNetwork()) { - Utils.logDebug("Unable to ndc destroy network"); - return false; - } - - // clear interface's ip - if (!clearAddrs()) { - Utils.logDebug("Unable to ndc clearaddrs"); - return false; - } - - // bring up interface - if (!interfaceUp()) { - Utils.logDebug("Unable to bring up interface."); - return false; - } - - // launch wpa_supplicant specifying our custom configuration and the - // socket file - if (!WpaSupplicant.start()) { - Utils.logDebug("Unable to run wpa start"); - return false; - } - - // create new network and get network id - String netID = WpaCli.createNetworkGetId(); - if (netID == null) { - Utils.logDebug("Unable to fetch network id"); - return false; - } - - // set network SSID - if (!WpaCli.setNetworkSSID(info.getSsid(), netID)) { - Utils.logDebug("Failed to set network ssid"); - return false; - } - - if (info.isHidden() && !WpaCli.setNetworkScanSSID(netID)) { - Utils.logDebug("Failed to set scan_ssid 1 for hidden network."); - return false; - } - - // set password (if any) - if (!WpaCli.setNetworkPSK(info, netID)) { - Utils.logDebug("Failed to set network psk"); - return false; - } - - // select the network we just created - if (!WpaCli.selectNetwork(netID)) { - Utils.logDebug("Unable to wpa_cli select network"); - return false; - } - - // enable the newtork - if (!WpaCli.enableNetwork(netID)) { - Utils.logDebug("Unable to wpa_cli enable_newtork"); - 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(info.getDhcpConfiguration())) { - Utils.logDebug("Failed to run dhcpcd"); - return false; - } - - // try to fetch gateway - 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 (!RootCommand.executeRootCmd("ndc network create 1")) { - Utils.logDebug("Failed to wpa_cli network create 1 "); - return false; - } - - 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 (!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; - } - - if (!setDns(Commons.getDnss(), gw)) { - Utils.logDebug("Failed to set DNS"); - return false; - } - - // use network - if (!RootCommand.executeRootCmd("ndc network default set 1")) { - Utils.logDebug("Failed to set network as default"); - return false; - } - - return true; - - } - - private boolean destroyNetwork() { - // needs root (tested) - return RootCommand.executeRootCmd("ndc network destroy 1"); - } - - private boolean setDns(String[] dnss, String gateway) { - - if (dnss == null || dnss.length == 0) { - // the DNS setting has been left blank - // try to use the gateway as dns - - if (gateway == null || gateway.length() == 0) { - // no possible DNS. - return false; - } - - dnss = new String[] { gateway, gateway }; - - } - - if (!InetAddressUtils.isIPv4Address(dnss[0])) { - // invalid ip can't proceed. - return false; - } - - String cmd = "ndc resolver setnetdns 1 " + dnss[0]; - - if (dnss.length > 1 && InetAddressUtils.isIPv4Address(dnss[1])) { - cmd += " " + dnss[1]; - } else { - cmd += " " + dnss[0]; - } - - 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() { - - try { - - // doesn't need root (tested) - ShellCommand cmd = new ShellCommand("ip route show dev " + WpaSupplicant.INTERFACE_NAME); - if (cmd.execute() != 0) { - Utils.logDebug("command failed show route"); - return null; - } - - // read command output - String out = cmd.getOutput(); - if (out == null) { - return null; - } - - String[] lines = out.split("\n"); - - for (String l : lines) { - - if (l.contains("default via")) { - - String[] f = l.split(" "); - if (f.length > 2) { - - // found route's address: - return f[2]; - - } - } - } - - return null; - - } catch (Exception e) { - Utils.logError("Error while trying to fetch route", e); - return null; - } - - } - - private boolean clearAddrs() { - // needs root (tested) - return RootCommand.executeRootCmd("ndc interface clearaddrs " - + WpaSupplicant.INTERFACE_NAME); - } - -}
\ No newline at end of file |