tap: change ip setting if use DHCP ip on Windows
authorMunkyu Im <munkyu.im@samsung.com>
Tue, 22 Dec 2015 09:16:54 +0000 (18:16 +0900)
committerMunkyu Im <munkyu.im@samsung.com>
Tue, 22 Dec 2015 10:33:54 +0000 (19:33 +0900)
If physical network gets ip address from DHCP server,
remove it.
If bridged network ues DHCP server,
change registry key to get host ip address.

Change-Id: I6ea0a7b3cb1bedff2b3f661db9a97de3ee905883
Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
src/org/tizen/emulator/manager/tool/TapUtil.java

index c1691ec..780b178 100644 (file)
@@ -613,10 +613,13 @@ public class TapUtil {
                HKEY interfaceKey = getInterfaceTcpipKey(interfaceClassID);
                EMLogger.getLogger().info("interfaceID: " + interfaceClassID); //$NON-NLS-1$
                EMLogger.getLogger().info("bridgeID: " + getBridgeId()); //$NON-NLS-1$
+               String interfaceName = getBridgeNameFromClassID(interfaceClassID);
                String dns = null;
                String ip = null;
                String gateway = null;
                String netmask = null;
+               List<String> cmd;
+               ProcessResult res;
                if (interfaceKey != null) {
                        try {
                                isDhcp = Advapi32Util.registryGetIntValue(interfaceKey,
@@ -642,6 +645,45 @@ public class TapUtil {
                                                netmask = netmasks[0];
                                        }
                                        EMLogger.getLogger().info("netmask: " + netmask);
+                               } else if (isDhcp == 1) {
+                                       String dhcpIp = null;
+                                       String dhcpGateway = null;
+                                       String dhcpNetmask = null;
+                                       dhcpIp = Advapi32Util.registryGetStringValue(interfaceKey, "DhcpIPAddress"); //$NON-NLS-1$
+                                       EMLogger.getLogger().info("DHCP ip: " + dhcpIp);
+
+                                       String gateways[] = registryGetStringArray(interfaceKey,
+                                                       "DhcpDefaultGateway"); //$NON-NLS-1$
+                                       if (gateways.length > 0) {
+                                               dhcpGateway = gateways[0];
+                                       }
+                                       EMLogger.getLogger().info("DHCP gateway: " + dhcpGateway);
+                                       dhcpNetmask = Advapi32Util.registryGetStringValue(interfaceKey, "DhcpSubnetMask"); //$NON-NLS-1$
+                                       EMLogger.getLogger().info("DHCP netmask: " + dhcpNetmask);
+
+                                       if ((dhcpIp != null && !dhcpIp.isEmpty())
+                                                       && (dhcpNetmask != null && !dhcpNetmask.isEmpty())
+                                                       && (dhcpGateway != null && !dhcpGateway.isEmpty())) {
+                                               EMLogger.getLogger().info("disable using DHCP");
+                                               cmd = Arrays.asList(NETSH, "-c", "int", "ipv4", "set", "address", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+                                                                               "\"" + "name=" + interfaceName + "\"", "source=static", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+                                                                               "address=" + dhcpIp, "mask=" + dhcpNetmask, "gateway=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+                                                                                               + dhcpGateway, "gwmetric=0"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+                                               res = HelperClass.runProcess(cmd);
+                                               if (res.isSuccess() == false) {
+                                                       EMLogger.getLogger().warning(
+                                                                       "Failed to configure TCP/IP: "+ res.getResultMessage());
+                                               }
+                                               EMLogger.getLogger().info("delete DHCP ip address");
+                                               cmd = Arrays
+                                                               .asList(NETSH, "interface", //$NON-NLS-1$ //$NON-NLS-2$
+                                                                               "ipv4", "delete", "address", interfaceName, "addr=" + dhcpIp, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+                                                                               "gateway=all"); //$NON-NLS-1$
+                                               res = HelperClass.runProcess(cmd);
+                                               if (res.isSuccess() == false) {
+                                                       EMLogger.getLogger().warning(res.getResultMessage());
+                                               }
+                                       }
                                }
                                String val = Advapi32Util.registryGetStringValue(interfaceKey,
                                                "NameServer"); //$NON-NLS-1$
@@ -651,14 +693,13 @@ public class TapUtil {
                                }
                                EMLogger.getLogger().info("NameServer: " + dns);
 
-                               List<String> cmd;
-                               ProcessResult res;
                                String bridgeName = getBridgeNameFromClassID(getBridgeId());
                                EMLogger.getLogger().info("Bridge Name: " + bridgeName);
                                /* set bridge network interface's TCP/IP configuration */
                                if (isDhcp == 0) {
                                        if (ip != null && !ip.isEmpty()) {
-                                               String interfaceName = getBridgeNameFromClassID(interfaceClassID);
+                                               interfaceName = getBridgeNameFromClassID(interfaceClassID);
+                                               EMLogger.getLogger().info("delete ip address");
                                                cmd = Arrays
                                                                .asList(NETSH, "interface", //$NON-NLS-1$ //$NON-NLS-2$
                                                                                "ipv4", "delete", "address", interfaceName, "addr=" + ip, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
@@ -668,7 +709,7 @@ public class TapUtil {
                                                        EMLogger.getLogger()
                                                                        .warning(res.getResultMessage());
                                                }
-
+                                               EMLogger.getLogger().info("set ip to bridge network");
                                                if ((netmask != null && !netmask.isEmpty())
                                                                && (gateway != null && !gateway.isEmpty())) {
                                                        cmd = Arrays
@@ -678,9 +719,7 @@ public class TapUtil {
                                                                                                        + gateway, "gwmetric=0");
                                                        res = HelperClass.runProcess(cmd);
                                                        if (res.isSuccess() == false) {
-                                                               EMLogger.getLogger().warning(
-                                                                               "Failed to configure TCP/IP: "
-                                                                                               + res.getResultMessage());
+                                                               EMLogger.getLogger().warning("Failed to configure TCP/IP: " + res.getResultMessage());
                                                        }
                                                }
                                        }
@@ -691,13 +730,12 @@ public class TapUtil {
                                        res = HelperClass.runProcess(cmd);
                                        if (res.isSuccess() == false) {
                                                EMLogger.getLogger().warning(
-                                                               "Failed to enable DHCP: "
-                                                                               + res.getResultMessage());
+                                                               "Failed to enable DHCP: " + res.getResultMessage());
                                        }
                                } else {
-                                       EMLogger.getLogger().warning(
-                                                       "dhcp value is wring:" + isDhcp);
+                                       EMLogger.getLogger().warning("isDhcp is wrong value: " + isDhcp);
                                }
+
                                if (dns != null && !dns.isEmpty()) {
                                        cmd = Arrays.asList(
                                                        NETSH, "interface", "ipv4", "set", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
@@ -705,9 +743,7 @@ public class TapUtil {
                                                        "static", dns, "primary"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                                        res = HelperClass.runProcess(cmd);
                                        if (res.isSuccess() == false) {
-                                               EMLogger.getLogger().warning(
-                                                               "Failed to set DNS server: "
-                                                                               + res.getResultMessage());
+                                               EMLogger.getLogger().warning("Failed to set DNS server: " + res.getResultMessage());
                                        }
                                }
                        } catch (Win32Exception e) {
@@ -1202,10 +1238,15 @@ public class TapUtil {
                        HKEY key = getInterfaceTcpipKey(getBridgeId());
                        if (key != null) {
                                try {
-                                       String data[] = Advapi32Util.registryGetStringArray(key,
-                                                       "IPAddress"); //$NON-NLS-1$
-                                       if (data.length > 0) {
-                                               ipAddr = data[0];
+                                       if (isDhcp == 0) {
+                                               String data[] = Advapi32Util.registryGetStringArray(key, "IPAddress"); //$NON-NLS-1$
+                                               if (data.length > 0) {
+                                                       ipAddr = data[0];
+                                               }
+                                       } else if (isDhcp == 1) {
+                                               ipAddr = Advapi32Util.registryGetStringValue(key, "DhcpIPAddress"); //$NON-NLS-1$
+                                       } else {
+                                               EMLogger.getLogger().warning("isDhcp is wrong value: " + isDhcp);
                                        }
 
                                } catch (Win32Exception e) {
@@ -1554,6 +1595,9 @@ public class TapUtil {
                                        if (!res.isSuccess()) {
                                                throw new VMWorkerException(res.getResultMessage());
                                        }
+                                       HKEY bridgeKey = getBridgeTcpipKey();
+                                       isDhcp = Advapi32Util.registryGetIntValue(bridgeKey, "EnableDHCP");
+                                       EMLogger.getLogger().info("bridge DHCP: " + isDhcp);
                                        if (isDhcp == 0) {
                                                EMLogger.getLogger().info(
                                                                "wait while finishing bridged network setting"); //$NON-NLS-1$