Delete a Ipv6 tunnel interface that was previously created. - Android Network

Android examples for Network:IPv6

Description

Delete a Ipv6 tunnel interface that was previously created.

Demo Code

/*****************************************************************************
 *  Project: Android IPv6Config//www .  ja va  2  s .c om
 *  Description: Android application to change IPv6 kernel configuration
 *  Author: Ren? Mayrhofer
 *  Copyright: Ren? Mayrhofer, 2011-2014
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 
 * as published by the Free Software Foundation.
 *****************************************************************************/
import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main{
    /** Our logger for this class. */
    private final static Logger logger = java.util.logging.Logger
            .getLogger(Constants.LOG_TAG);
    /** Search for the "ip" and "busybox" binaries at these locations. */
    public final static String[] LINUX_BINARY_LOCATIONS = { "/sbin/",
            "/bin/", "/system/bin/", "/system/xbin/" };
    /** Preferred is to use the "ip" binary directly. 
     * @see BUSYBOX_BINARY_PREFIX
     */
    public final static String IP_BINARY = "ip";
    /** But if no (working) ip binary can be found, then try to use busybox with an "ip" applet.
     * @see IP_BINARY
     */
    public final static String BUSYBOX_BINARY = "busybox";
    /** Command to get and set network interface addresses and options under modern Linux systems. */
    private final static String ADDRESSES_COMMAND = " addr";
    /** Simply the shell command (and if necessary path) to execute a standard POSIX shell. */
    public final static String SH_COMMAND = "sh";
    /** Command to the "ip" binary to delete a tunnel interface under modern Linux systems. */
    private final static String DELETE_TUNNEL_INTERFACE = " tunnel" + DEL;
    /** Static initializer: find out where to call the "ip" binary from and remember for future use. */
    private static String ipBinaryLocation = null;
    private static String ipBinaryTriedPaths = null;
    /** Delete a tunnel interface that was previously created.
     * 
     * @param iface The interface name to delete.
     * @return true if successfully deleted, false otherwise.
     */
    public static boolean deleteTunnelInterface(String iface) {
        String cmd = getIPCommandLocation() + DELETE_TUNNEL_INTERFACE
                + iface;

        try {
            if (Command.executeCommand(SH_COMMAND, true, cmd, null, null) == 0) {
                logger.finer("Deleted tunnel interface " + iface);
                return true;
            } else {
                logger.warning("Unable to delete tunnel interface " + iface
                        + ", it probably has not been created beforehand");
                return false;
            }
        } catch (IOException e) {
            logger.severe("Unable to execute system command, tunnel interface not deleted (access privileges missing?) "
                    + e);
            return false;
        } catch (InterruptedException e) {
            return false;
        }
    }
    /** Helper to locate a usable "ip" command or null if none is found. */
    public static String getIPCommandLocation() {
        if (ipBinaryLocation == null) {
            ipBinaryTriedPaths = "";

            if (!tryIPBinaries(LINUX_BINARY_LOCATIONS, IP_BINARY, null)
                    && !tryIPBinaries(LINUX_BINARY_LOCATIONS,
                            BUSYBOX_BINARY, IP_BINARY))
                logger.severe("Could not find ip binary in"
                        + ipBinaryTriedPaths
                        + ", will be unable to read network interface details");
        }
        return ipBinaryLocation;
    }
    /** Helper function to try a list of paths with a command to verify if "ip addr" can be executed correctly.
     * 
     * @return true if a working "ip addr" call could be made, false otherwise. If true is returned,
     *          the working full binary path is stored in ipBinaryLocation.
     * @see ipBinaryLocation 
     */
    private static boolean tryIPBinaries(String[] paths, String cmd,
            String cmd2) {
        for (String path : paths) {
            String binary = path + cmd;
            // sanity check: can we actually execute our command?
            logger.finer("Checking for availibility of command '" + binary
                    + "'");
            if (new File(binary).canRead()) {
                if (cmd2 != null)
                    binary = binary + " " + cmd2;
                /* second sanity check: does this binary work?
                 * (E.g. on the Samsung Galaxy S2, there actually is a binary under 
                 * /system/bin/ip that claims to work, but doesn't).
                 */
                try {
                    logger.fine("Trying to execute cmd '" + binary
                            + ADDRESSES_COMMAND + "'");
                    Command.executeCommand(binary + ADDRESSES_COMMAND,
                            false, false, null);
                    logger.fine("Found working ip binary in " + binary);
                    ipBinaryLocation = binary;
                    return true;
                } catch (Exception e) {
                    logger.warning("Found ip binary in "
                            + binary
                            + ", but does not behave as expected. Trying next location.");
                }
            }
            ipBinaryTriedPaths = ipBinaryTriedPaths + " '" + binary + "'";
        }
        return false;
    }
}

Related Tutorials