Adding options to disable/enable firewall policy and choose MAC/IP translation methods
[iot2.git] / iotjava / iotruntime / master / RouterConfig.java
index 48bb8f691fd000026ab6a696e6d4b24ef4835da4..ace0ea053abb822271575491aef354ca9574dc3b 100644 (file)
@@ -39,9 +39,11 @@ public final class RouterConfig {
         * RouterConfig constants
         */
        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
@@ -273,7 +275,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,7 +283,7 @@ 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 {
@@ -305,6 +307,42 @@ 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) {
+
+                //HashMap<String,String> hmMACToIPAdd = new HashMap<String,String>();
+                try {
+                        // We replace with "cat /usr/sbin/arp"
+                        String cmd = "ssh " + STR_SSH_USERNAME_RASPBERRYPI + "@" + strRouterAddress +
+                         " /usr/sbin/arp";
+                        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
         *