Cleaning up code for runtime, installer, RMI, compiler for the Java side
[iot2.git] / iotjava / iotruntime / master / RouterConfig.java
index 48bb8f691fd000026ab6a696e6d4b24ef4835da4..2c7309b54ef13d7e824e0c58bc077580e4295e6c 100644 (file)
@@ -38,10 +38,12 @@ public final class RouterConfig {
        /**
         * RouterConfig constants
         */
-       private static final String STR_SSH_USERNAME_ROUTER = "root";
-       private static final String STR_SSH_USERNAME_HOST   = "iotuser";
-       private static final String STR_POLICY_FILE_ALL         = "_all";
-       private static final String STR_POLICY_FILE_EXT         = ".policy";
+       private static final String STR_SSH_USERNAME_ROUTER         = "root";
+       private static final String STR_SSH_USERNAME_RASPBERRYPI    = "pi";
+       private static final String STR_SSH_USERNAME_HOST           = "iotuser";
+       private static final String STR_POLICY_FILE_ALL                 = "_all";
+       private static final String STR_POLICY_FILE_EXT                 = ".policy";
+       private static final String STR_INCOMPLETE                          = "(incomplete)";
 
        /**
         * RouterConfig properties
@@ -213,10 +215,7 @@ public final class RouterConfig {
                deployPolicies(strCmdSend);
                String strCmdDeploy = "ssh " + STR_SSH_USERNAME_ROUTER + "@" + strConfigHost +
                        " iptables-restore < ~/" + strConfigHost + STR_POLICY_FILE_ALL + STR_POLICY_FILE_EXT + "; rm ~/" + strConfigHost + 
-                       STR_POLICY_FILE_ALL + STR_POLICY_FILE_EXT + "; ";// + 
-                       // TODO: delete these later when we apply tight initial conditions (reject everything but SSH commands)
-                       //"iptables -F startup_filter_tcp; iptables -F startup_filter_udp; " +
-                       //"iptables -t filter -D FORWARD -j startup_filter_tcp; iptables -t filter -D FORWARD -j startup_filter_udp;";
+                       STR_POLICY_FILE_ALL + STR_POLICY_FILE_EXT + "; ";
                //System.out.println(strCmdDeploy);
                deployPolicies(strCmdDeploy);
        }
@@ -231,12 +230,12 @@ public final class RouterConfig {
 
                String strCmdSend = "scp " + strConfigHost + STR_POLICY_FILE_ALL + STR_POLICY_FILE_EXT + " " + 
                        STR_SSH_USERNAME_HOST + "@" + strConfigHost + ":~;";
-               System.out.println(strCmdSend);
+               //System.out.println(strCmdSend);
                deployPolicies(strCmdSend);
                String strCmdDeploy = "ssh " + STR_SSH_USERNAME_HOST + "@" + strConfigHost +
                        " sudo iptables-restore < ~/" + strConfigHost + STR_POLICY_FILE_ALL + STR_POLICY_FILE_EXT + "; rm ~/" + strConfigHost + 
                        STR_POLICY_FILE_ALL + STR_POLICY_FILE_EXT + ";";
-               System.out.println(strCmdDeploy);
+               //System.out.println(strCmdDeploy);
                deployPolicies(strCmdDeploy);
        }
 
@@ -273,7 +272,7 @@ public final class RouterConfig {
        }
 
        /**
-        * getAddressList() method gets list of IP addresses
+        * getAddressListTmp() method gets list of IP addresses from /tmp/dhcp.leases
         * <p>
         * This method sends an inquiry to the router to look for
         * the list of DHCP leased addresses and their mapping to MAC
@@ -281,9 +280,8 @@ public final class RouterConfig {
         *
         * @param  strRouterAddress  String that contains address of router
         */
-       public void getAddressList(String strRouterAddress) {
+       public void getAddressListTmp(String strRouterAddress) {
 
-               //HashMap<String,String> hmMACToIPAdd = new HashMap<String,String>();
                try {
                        // We can replace "cat /tmp/dhcp.leases" with "cat /proc/net/arp"
                        String cmd = "ssh " + STR_SSH_USERNAME_ROUTER + "@" + strRouterAddress +
@@ -305,6 +303,41 @@ public final class RouterConfig {
                }
        }
 
+    /**
+     * getAddressListArp() method gets list of IP addresses from arp command
+     * <p>
+     * This method sends an inquiry to the router to look for
+     * the list of DHCP leased addresses and their mapping to MAC
+     * addresses
+     *
+     * @param  strRouterAddress  String that contains address of router
+     */
+    public void getAddressListArp(String strRouterAddress) {
+
+        try {
+            // We replace with "cat /usr/sbin/arp"
+            String cmd = "ssh " + STR_SSH_USERNAME_RASPBERRYPI + "@" + strRouterAddress +
+             " /usr/sbin/arp -n";
+            Runtime runtime = Runtime.getRuntime();
+            Process process = runtime.exec(cmd);
+
+            InputStream inStream = process.getInputStream();
+            InputStreamReader isReader = new InputStreamReader(inStream);
+            BufferedReader bReader = new BufferedReader(isReader);
+            String strRead = null;
+            while((strRead = bReader.readLine()) != null) {
+                String[] str = strRead.split("\\s+");
+                // Skip if "(incomplete)" is seen!
+                if (str[1].equals(STR_INCOMPLETE))
+                       continue;
+                mapMACtoIPAdd.put(str[2], str[0]);
+            }
+        } catch (IOException ex) {
+            System.out.println("RouterConfig: IOException: " + ex.getMessage());
+            ex.printStackTrace();
+        }
+    }
+
        /**
         * getIPFromMACAddress() method gets IP from MAC address
         *
@@ -648,8 +681,6 @@ public final class RouterConfig {
                        strConfigHost + " -d " + strMonitorHost + " -p tcp --dport ssh");
                pwConfig.println("-A OUTPUT -j ACCEPT -s " + 
                        strConfigHost + " -d " + strMonitorHost + " -p tcp --sport ssh");
-               //pwConfig.println("-A FORWARD -j ACCEPT -p tcp --dport ssh");
-               //pwConfig.println("-A FORWARD -j ACCEPT -p tcp --sport ssh");
 
        }
 
@@ -935,6 +966,5 @@ public final class RouterConfig {
                pwConfig.println("-A INPUT -j ACCEPT -p udp --sport domain");
                pwConfig.println("-A OUTPUT -j ACCEPT -p udp --dport domain");
                pwConfig.println("-A OUTPUT -j ACCEPT -p udp --sport domain");
-
        }
 }