net: fix listing network interface
authorMunkyu Im <munkyu.im@samsung.com>
Wed, 13 May 2015 10:01:40 +0000 (19:01 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Thu, 14 May 2015 02:10:22 +0000 (11:10 +0900)
If a network interface is bound to bridged network interface,
cannot list it with NetworkInterface.getNetworkInterfaces();

Change-Id: I49585bda2750c8c13220a5c050da5b88b93a20dd
Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
common-project/src/org/tizen/emulator/manager/tool/TapUtil.java
common-project/src/org/tizen/emulator/manager/ui/dialog/TapDeviceDialogForLinux.java
common-project/src/org/tizen/emulator/manager/vms/option/NetworkOption.java

index c5d228a..56f4c41 100644 (file)
@@ -64,6 +64,7 @@ import com.sun.jna.platform.win32.WinReg.HKEYByReference;
 
 public class TapUtil {
 
+       private static String BRIDGE_NAME = "bridge1";
        public static String getDevconPath(String platformVersion) {
                return FilePathResources.getEmulatorBinPath(platformVersion)
                                + File.separator + "devcon.exe";
@@ -937,18 +938,19 @@ public class TapUtil {
 
        public static String getBridgeIpAddr(String tapName) {
                String ipAddr = null;
-               if (EmulatorManager.isLinux())
-                       try {
-                               InterfaceAddress ifAddr = getInterfaceIpInfo(getBridgeFromDevice(tapName));
-                               if (ifAddr != null)
-                                       ipAddr = ifAddr.getAddress().getHostAddress();
-                       } catch (VMWorkerException e) {
-                               EMLogger.getLogger().warning(
-                                               (new StringBuilder())
-                                                               .append("Failed to get bridge ip address. ")
-                                                               .append(e.getMessage()).toString());
+               if (EmulatorManager.isLinux()) {
+                       InterfaceAddress ifAddr = getInterfaceIpInfo(tapName);
+                       if (ifAddr != null) {
+                               EMLogger.getLogger().info("get " + tapName + "'s IP");
+                               ipAddr = ifAddr.getAddress().getHostAddress();
+                       } else {
+                               EMLogger.getLogger().info("get bridge's IP");
+                               InterfaceAddress ifAddrBridge = getInterfaceIpInfo(BRIDGE_NAME);
+                               if (ifAddrBridge != null) {
+                                       ipAddr = ifAddrBridge.getAddress().getHostAddress();
+                               }
                        }
-               else if (EmulatorManager.isWin() && isTapInBridge(tapName)) {
+               else if (EmulatorManager.isWin() && isTapInBridge(tapName)) {
                        HKEY key = getInterfaceTcpipKey(getBridgeId());
                        if (key != null) {
                                try {
index 098e15e..1e948a0 100644 (file)
 
 package org.tizen.emulator.manager.ui.dialog;
 
-import java.net.NetworkInterface;
-import java.net.SocketException;
 import java.util.ArrayList;
-import java.util.Enumeration;
+import java.util.Arrays;
 import java.util.List;
 
 import org.eclipse.swt.SWT;
@@ -61,40 +59,26 @@ import org.tizen.emulator.manager.resources.PatchImageResources;
 import org.tizen.emulator.manager.tool.TapUtil;
 import org.tizen.emulator.manager.ui.MainDialog;
 import org.tizen.emulator.manager.ui.widgets.ImageLabel;
-import org.tizen.emulator.manager.vms.helper.VMWorkerException;
+import org.tizen.emulator.manager.vms.helper.HelperClass;
+import org.tizen.emulator.manager.vms.helper.ProcessResult;
 
 public class TapDeviceDialogForLinux {
-       public static ArrayList<String> getInterfaceList() {
-               ArrayList<String> result = new ArrayList<String>();
-
-               // Get intereface list
-               ArrayList<String> ifList = new ArrayList<String>();
-               Enumeration<NetworkInterface> interfaces;
-               try {
-                       interfaces = NetworkInterface.getNetworkInterfaces();
-                       if (interfaces != null) {
-                               while (interfaces.hasMoreElements()) {
-                                       NetworkInterface current = interfaces.nextElement();
-
-                                       if (!current.isUp() || current.isLoopback()
-                                                       || current.isVirtual())
-                                               continue;
-                                       ifList.add(current.getName());
-                               }
+       public static List<String> getInterfaceList() {
+               List<String> result = new ArrayList<String>();
+               //FIXME: support only ethX for base network
+               List<String> cmd = Arrays.asList("/bin/sh", "-c", "/bin/ls /sys/class/net | /bin/grep eth");
+               ProcessResult res = HelperClass.runProcess(cmd);
+               if (res.isSuccess() == false) {
+                       EMLogger.getLogger().warning(
+                                       "Get tap list fail. Command returns fail:" + "cmd : "
+                                                       + cmd.toString() + ", return : "
+                                                       + res.getExitValue());
+                       return null;
+               } else {
+                       for (String line : res.getStdOutMsg()) {
+                               result.add(line.trim());
                        }
-                       // Except bridges in list
-                       List<String> brList = TapUtil.getBridgeList();
-                       for (String str : ifList) {
-                               if (!brList.contains(str)) {
-                                       result.add(str);
-                               }
-                       }
-               } catch (SocketException e) {
-                       EMLogger.getLogger().warning(e.getMessage());
-               } catch (VMWorkerException e) {
-                       EMLogger.getLogger().warning(e.getMessage());
                }
-
                return result;
        }
 }
index 8ba5b53..dd66658 100644 (file)
@@ -50,8 +50,7 @@ public class NetworkOption extends Option {
                        throws VMWorkerException {
                boolean isBridge = property.getPropertyValue()
                                .getAdvancedOptionValue(ItemName.NET_CONNECT_TYPE)
-                               .equals("Bridge")
-                               && (EmulatorManager.isLinux() || EmulatorManager.isWin());
+                               .equals("Bridge");
 
                // Qemu MAC argument
                config.addVariable(VAR_MAC, getMacAddr(isBridge, property));
@@ -67,9 +66,15 @@ public class NetworkOption extends Option {
 
                // Qemu net argument
                if (isBridge) {
-                       String tapName = property.getPropertyValue()
-                                       .getAdvancedOptionValue(ItemName.NET_TAP_DEVICE);
-                       config.addQemuOption("-net", "tap,ifname=" + tapName + ",script=no,downscript=no");
+                       if (EmulatorManager.isWin()) {
+                               String tapName = property.getPropertyValue()
+                                               .getAdvancedOptionValue(ItemName.NET_TAP_DEVICE);
+                               config.addQemuOption("-net", "tap,ifname=" + tapName
+                                               + ",script=no,downscript=no");
+                       } else {
+                               config.addQemuOption("-net",
+                                               "tap,script=/etc/emulator-ifup.sh,downscript=no");
+                       }
                }
 
        }
@@ -167,10 +172,9 @@ public class NetworkOption extends Option {
        public void checkArgument(VMProperty property) throws VMLauncherException {
                boolean isBridge = property.getPropertyValue()
                                .getAdvancedOptionValue(ItemName.NET_CONNECT_TYPE)
-                               .equals("Bridge")
-                               && (EmulatorManager.isLinux() || EmulatorManager.isWin());
+                               .equals("Bridge");
 
-               if (isBridge) {
+               if (isBridge && EmulatorManager.isWin()) {
                        String tapName = property.getPropertyValue()
                                        .getAdvancedOptionValue(ItemName.NET_TAP_DEVICE);
                        if (TapUtil.nameNotExist(tapName))