import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
+import java.util.TreeMap;
import java.util.regex.Pattern;
import org.eclipse.swt.SWT;
public class TapUtil {
public static String BRIDGE_NAME = "bridge1"; //$NON-NLS-1$
+ static String BRIDGE_OPTION = "--bridge"; //$NON-NLS-1$
+ static String NETSH = "netsh.exe"; //$NON-NLS-1$
+ private static int isDhcp = 0;
+ private static int WAIT_TIME = 2000;
+
+ public static String getCheckNetPath() {
+ String checkNet;
+ if (EmulatorManager.isWin()) {
+ checkNet = "check-net.exe"; //$NON-NLS-1$
+ } else {
+ checkNet = "check-net"; //$NON-NLS-1$
+ }
+ return FilePathResources.getEmulatorCheckToolPath() + File.separator + checkNet;
+ }
+
public static String getDevconPath(String platformVersion) {
return FilePathResources.getPlatformEmulatorBinPath(platformVersion)
+ File.separator + "devcon.exe"; //$NON-NLS-1$
+ "\\" + subKey + "\\" + key, //$NON-NLS-1$ //$NON-NLS-2$
value);
if (tapName != null) {
- EMLogger.getLogger().warning(
+ EMLogger.getLogger().info(
"tapname added: " + tapName); //$NON-NLS-1$
tapNameList.add(tapName);
}
regKey = Advapi32Util.registryGetKey(root, topKey, WinNT.KEY_READ);
String[] subKeys = Advapi32Util.registryGetKeys(regKey.getValue());
for (String subKey : subKeys) {
- String compId = Advapi32Util.registryGetStringValue(root,
- topKey + "\\" + subKey, value1); //$NON-NLS-1$
- String tapId = Advapi32Util.registryGetStringValue(root, topKey
- + "\\" + subKey, value2); //$NON-NLS-1$
- if (compId != null && tapId != null) {
- if (compId.startsWith("tap")) { //$NON-NLS-1$
- tapIdList.add(tapId);
+ try {
+ String compId = Advapi32Util.registryGetStringValue(root,
+ topKey + "\\" + subKey, value1); //$NON-NLS-1$
+ String tapId = Advapi32Util.registryGetStringValue(root,
+ topKey + "\\" + subKey, value2); //$NON-NLS-1$
+ if (compId != null && tapId != null) {
+ if (compId.toLowerCase().startsWith("tap")) { //$NON-NLS-1$
+ EMLogger.getLogger().info("tapId added: " + tapId);
+ tapIdList.add(tapId);
+ }
}
+ } catch (Win32Exception e) {
+ EMLogger.getLogger().info(e.getMessage());
}
}
topKey = "SYSTEM\\CurrentControlSet\\services\\Bridge\\Linkage"; //$NON-NLS-1$
}
value = "Route"; //$NON-NLS-1$
- String valueArr[] = Advapi32Util.registryGetStringArray(root,
+ String valueArr[] = registryGetStringArray(root,
topKey, value);
for (String str : valueArr) {
if (str.replace("\"", "").equals(foundTapId)) { //$NON-NLS-1$ //$NON-NLS-2$
return result;
}
+ private static String[] registryGetStringArray(HKEY hKey, String value) {
+ return registryGetStringArray(hKey, null, value);
+ }
+
+ private static String[] registryGetStringArray(HKEY hKey, String path,
+ String value) {
+ TreeMap<String, Object> values = null;
+ if (path == null) {
+ values = Advapi32Util.registryGetValues(hKey);
+ } else {
+ values = Advapi32Util.registryGetValues(hKey, path);
+ }
+ String[] resultArr = null;
+ for (String val : values.keySet()) {
+ if (val.equals(value)) {
+ Object obj = values.get(val);
+ if (obj instanceof String[]) {
+ resultArr = (String[]) obj;
+ break;
+ }
+ }
+ }
+ EMLogger.getLogger().fine("Data in Registry " + path + ", " + value);
+ if (resultArr == null) {
+ return new String[0];
+ }
+ for (String str : resultArr) {
+ EMLogger.getLogger().fine(str);
+ }
+ return resultArr;
+ }
+
public static String getNetmaskFromTap(String tapName) {
String netMask = ""; //$NON-NLS-1$
if (EmulatorManager.isLinux()) {
HKEY key = getBridgeTcpipKey();
if (key != null) {
try {
- String data[] = Advapi32Util.registryGetStringArray(key,
+ String data[] = registryGetStringArray(key,
"SubnetMask"); //$NON-NLS-1$
if (data.length > 0) {
netMask = data[0];
key = getInterfaceTcpipKey(getBridgeId());
if (key != null) {
try {
- String data[] = Advapi32Util.registryGetStringArray(
+ String data[] = registryGetStringArray(
key, "SubnetMask"); //$NON-NLS-1$
if (data.length > 0) {
netMask = data[0];
private static void setWinbridgeNetwork(String interfaceClassID)
throws VMWorkerException {
HKEY interfaceKey = getInterfaceTcpipKey(interfaceClassID);
- // HKEY bridgeKey = getInterfaceTcpipKey(getBridgeId());
- System.out.println("interfaceKey: " + interfaceClassID); //$NON-NLS-1$
- System.out.println("bridgeKey: " + getBridgeId()); //$NON-NLS-1$
+ EMLogger.getLogger().info("interfaceID: " + interfaceClassID); //$NON-NLS-1$
+ EMLogger.getLogger().info("bridgeID: " + getBridgeId()); //$NON-NLS-1$
String dns = null;
String ip = null;
String gateway = null;
String netmask = null;
if (interfaceKey != null) {
try {
- String ips[] = Advapi32Util.registryGetStringArray(
- interfaceKey, "IPAddress"); //$NON-NLS-1$
- if (ips.length > 0) {
- ip = ips[0];
- }
-
- String gateways[] = Advapi32Util.registryGetStringArray(
- interfaceKey, "DefaultGateway"); //$NON-NLS-1$
- if (gateways.length > 0) {
- gateway = gateways[0];
- }
-
- String netmasks[] = Advapi32Util.registryGetStringArray(
- interfaceKey, "SubnetMask"); //$NON-NLS-1$
- if (netmasks.length > 0) {
- netmask = netmasks[0];
+ isDhcp = Advapi32Util.registryGetIntValue(interfaceKey,
+ "EnableDHCP");
+ EMLogger.getLogger().info("DHCP: " + isDhcp); //$NON-NLS-1$
+ if (isDhcp == 0) {
+ /* get original network interface's TCP/IP configuration */
+ String ips[] = registryGetStringArray(interfaceKey,
+ "IPAddress"); //$NON-NLS-1$
+ if (ips.length > 0) {
+ ip = ips[0];
+ }
+ EMLogger.getLogger().info("ip: " + ip);
+ String gateways[] = registryGetStringArray(interfaceKey,
+ "DefaultGateway"); //$NON-NLS-1$
+ if (gateways.length > 0) {
+ gateway = gateways[0];
+ }
+ EMLogger.getLogger().info("gateway: " + gateway);
+ String netmasks[] = registryGetStringArray(interfaceKey,
+ "SubnetMask"); //$NON-NLS-1$
+ if (netmasks.length > 0) {
+ netmask = netmasks[0];
+ }
+ EMLogger.getLogger().info("netmask: " + netmask);
}
-
String val = Advapi32Util.registryGetStringValue(interfaceKey,
"NameServer"); //$NON-NLS-1$
-
String[] arr = val.split(","); //$NON-NLS-1$
if (arr.length > 0) {
dns = arr[0];
}
+ 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);
+ 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$
+ "gateway=all"); //$NON-NLS-1$
+ res = HelperClass.runProcess(cmd);
+ if (res.isSuccess() == false) {
+ EMLogger.getLogger()
+ .warning(res.getResultMessage());
+ }
- String interfaceName = getBridgeNameFromClassID(interfaceClassID);
- List<String> cmd = Arrays.asList("netsh.exe", "interface", //$NON-NLS-1$ //$NON-NLS-2$
- "ip", "delete", "address", interfaceName, "addr=" + ip, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- "gateway=all"); //$NON-NLS-1$
- ProcessResult res = HelperClass.runProcess(cmd);
- if (res.isSuccess() == false) {
- throw new VMWorkerException(res.getResultMessage());
- }
-
- String bridgeKey = "HKLM\\SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces\\" //$NON-NLS-1$
- + getBridgeId();
- // TOOD: Advapi32Util.registrySetStringValue(...) not work
- cmd = Arrays.asList("reg.exe", "add", bridgeKey, "/f", "/v", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- "EnableDHCP", "/t", "REG_DWORD", "/d", "0"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
- res = HelperClass.runProcess(cmd);
- if (res.isSuccess() == false) {
- throw new VMWorkerException(res.getResultMessage());
- }
-
- cmd = Arrays.asList("reg.exe", "add", bridgeKey, "/f", "/v", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- "IPAddress", "/t", "REG_MULTI_SZ", "/d", ip); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- res = HelperClass.runProcess(cmd);
- if (res.isSuccess() == false) {
- throw new VMWorkerException(res.getResultMessage());
- }
-
- cmd = Arrays.asList("reg.exe", "add", bridgeKey, "/f", "/v", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- "SubnetMask", "/t", "REG_MULTI_SZ", "/d", netmask); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- res = HelperClass.runProcess(cmd);
- if (res.isSuccess() == false) {
- throw new VMWorkerException(res.getResultMessage());
- }
-
- cmd = Arrays.asList("reg.exe", "add", bridgeKey, "/f", "/v", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- "DefaultGateway", "/t", "REG_MULTI_SZ", "/d", gateway); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- res = HelperClass.runProcess(cmd);
- if (res.isSuccess() == false) {
- throw new VMWorkerException(res.getResultMessage());
- }
-
- cmd = Arrays
- .asList("reg.exe", "add", bridgeKey, "/f", "/v", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- "DefaultGatewayMetric", "/t", "REG_MULTI_SZ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- "/d", "1"); //$NON-NLS-1$ //$NON-NLS-2$
- res = HelperClass.runProcess(cmd);
- if (res.isSuccess() == false) {
- throw new VMWorkerException(res.getResultMessage());
+ if ((netmask != null && !netmask.isEmpty())
+ && (gateway != null && !gateway.isEmpty())) {
+ cmd = Arrays
+ .asList(NETSH, "-c", "int", "ipv4", "set", "address", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ "\"" + "name=" + bridgeName + "\"", "source=static", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ "address=" + ip, "mask=" + netmask, "gateway=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ + gateway, "gwmetric=0");
+ res = HelperClass.runProcess(cmd);
+ if (res.isSuccess() == false) {
+ EMLogger.getLogger().warning(
+ "Failed to configure TCP/IP: "
+ + res.getResultMessage());
+ }
+ }
+ }
+ } else if (isDhcp == 1) {
+ cmd = Arrays.asList(
+ NETSH, "-c", "int", "ipv4", "set", "address", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ "\"" + "name=" + bridgeName + "\"", "dhcp"); //$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 enable DHCP: "
+ + res.getResultMessage());
+ }
+ } else {
+ EMLogger.getLogger().warning(
+ "dhcp value is wring:" + isDhcp);
}
-
- cmd = Arrays.asList("reg.exe", "add", bridgeKey, "/f", "/v", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- "NameServer", "/t", "REG_SZ", "/d", dns); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- res = HelperClass.runProcess(cmd);
- if (res.isSuccess() == false) {
- throw new VMWorkerException(res.getResultMessage());
+ if (dns != null && !dns.isEmpty()) {
+ cmd = Arrays.asList(
+ NETSH, "interface", "ipv4", "set", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ "dnsservers", "\"" + "name=" + bridgeName + "\"", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ "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());
+ }
}
} catch (Win32Exception e) {
EMLogger.getLogger().warning(e.getMessage());
} finally {
Advapi32Util.registryCloseKey(interfaceKey);
}
+ } else {
+ EMLogger.getLogger().warning("interface key is null");
}
-
}
private static String getBridgeId() {
String bridgeId = null;
if (EmulatorManager.isWin()) {
- HKEY root = WinReg.HKEY_LOCAL_MACHINE;
- String topKey = "SYSTEM\\CurrentControlSet\\services\\BridgeMP"; //$NON-NLS-1$
- String value = "Device"; //$NON-NLS-1$
- try {
- String data = Advapi32Util.registryGetStringValue(root, topKey,
- value);
- if (data != null) {
- String arr[] = data.split("\\\\"); //$NON-NLS-1$
- bridgeId = arr[arr.length - 1];
+ List<String> cmd = Arrays.asList(getCheckNetPath(), BRIDGE_OPTION); //$NON-NLS-1$
+ ProcessResult res = HelperClass.runProcess(cmd);
+ boolean isCommandSuccess = false;
+ if (res.isSuccess()) {
+ for (String str : res.getStdOutMsg()) {
+ if (str.startsWith("ClassID:")) { //$NON-NLS-1$
+ String arr[] = str.split(": "); //$NON-NLS-1$
+ bridgeId = arr[arr.length - 1];
+ break;
+ }
}
- } catch (Win32Exception e) {
- EMLogger.getLogger().warning(
- "Failed to getBridgeId(): " + e.getMessage()); //$NON-NLS-1$
+ } else if (!res.isSuccess() || !isCommandSuccess) {
+ EMLogger.getLogger().warning(res.getResultMessage());
}
+ } else {
+ EMLogger.getLogger().warning("OS is not Windows!");
}
+ EMLogger.getLogger().info("bridgeId: " + bridgeId);
return bridgeId;
}
HKEY key = getBridgeTcpipKey();
if (key != null) {
try {
- String data[] = Advapi32Util.registryGetStringArray(key,
+ String data[] = registryGetStringArray(key,
"DefaultGateway"); //$NON-NLS-1$
if (data.length > 0) {
gateway = data[0];
key = getInterfaceTcpipKey(getBridgeId());
if (key != null) {
try {
- String data[] = Advapi32Util.registryGetStringArray(
+ String data[] = registryGetStringArray(
key, "DefaultGateway"); //$NON-NLS-1$
if (data.length > 0) {
gateway = data[0];
return gateway;
}
-
private static Boolean MANUAL_BRIDGE = null;
public static void showGuideForMac() {
// check version
}
public static boolean isWinBridgeExist() {
- String proxyCommand = "check-net.exe"; //$NON-NLS-1$
boolean isExist = false;
- List<String> cmd = Arrays.asList(FilePathResources.getEmulatorCheckToolPath()
- + File.separator + proxyCommand, "--bridge"); //$NON-NLS-1$
+ List<String> cmd = Arrays.asList(getCheckNetPath(), BRIDGE_OPTION); //$NON-NLS-1$
ProcessResult res = HelperClass.runProcess(cmd);
boolean isCommandSuccess = false;
if (res.isSuccess()) {
if (!res.isSuccess()) {
throw new VMWorkerException(res.getResultMessage());
}
- EMLogger.getLogger().info("set bridge's IP"); //$NON-NLS-1$
- setWinbridgeNetwork(getClassIDFromPnpInstanceID(CompatibleIDs));
}
+ EMLogger.getLogger().info("set bridge's IP"); //$NON-NLS-1$
+ setWinbridgeNetwork(getClassIDFromPnpInstanceID(CompatibleIDs));
}
// Get tap list before create new tap.
if (!res.isSuccess()) {
throw new VMWorkerException(res.getResultMessage());
}
-
+ //FIXME: to wait for re-naming tap name(work around)
+ if (EmulatorManager.isWin8AndAbove()) {
+ EMLogger.getLogger().info("wait for renaming tap name");
+ Thread.sleep(WAIT_TIME);
+ }
// Get tap list and find new one.
EMLogger.getLogger().info("Get tap list and find new one."); //$NON-NLS-1$
List<String> after = TapUtil.getTapList();
// Rename new tap to tapX
EMLogger.getLogger().info("Rename new tap to tapX."); //$NON-NLS-1$
- cmd = Arrays.asList("netsh", "interface", "set", "interface", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ cmd = Arrays.asList(NETSH, "interface", "set", "interface", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"name=" + newOne, "newname=" + tapName); //$NON-NLS-1$ //$NON-NLS-2$
res = HelperClass.runProcess(cmd);
if (!res.isSuccess()) {
if (!res.isSuccess()) {
throw new VMWorkerException(res.getResultMessage());
}
-
- EMLogger.getLogger().info(
- "wait while finishing bridged network setting"); //$NON-NLS-1$
- String proxyCommand = "check-net.exe"; //$NON-NLS-1$
- cmd = Arrays.asList(FilePathResources.getEmulatorCheckToolPath()
- + File.separator + proxyCommand, "--bridge"); //$NON-NLS-1$
- res = HelperClass.runProcess(cmd);
- while (!res.getStdOutMsg().isEmpty()) {
- boolean isCommandSuccess = false;
- String ipAddr = null;
- if (res.isSuccess()) {
- for (String str : res.getStdOutMsg()) {
- if (str.startsWith("IP Address:")) { //$NON-NLS-1$
- isCommandSuccess = true;
- ipAddr = str.split("IP Address: ")[1] //$NON-NLS-1$
- .trim();
- EMLogger.getLogger().info(
- "bridge IP address: " + ipAddr); //$NON-NLS-1$
- break;
+ if (isDhcp == 0) {
+ EMLogger.getLogger().info(
+ "wait while finishing bridged network setting"); //$NON-NLS-1$
+ cmd = Arrays.asList(getCheckNetPath(), BRIDGE_OPTION); //$NON-NLS-1$
+ res = HelperClass.runProcess(cmd);
+ while (!res.getStdOutMsg().isEmpty()) {
+ boolean isCommandSuccess = false;
+ String ipAddr = null;
+ if (res.isSuccess()) {
+ for (String str : res.getStdOutMsg()) {
+ if (str.startsWith("IP Address:")) { //$NON-NLS-1$
+ isCommandSuccess = true;
+ ipAddr = str.split("IP Address: ")[1] //$NON-NLS-1$
+ .trim();
+ EMLogger.getLogger().info(
+ "bridge IP address: " + ipAddr); //$NON-NLS-1$
+ break;
+ }
}
+ } else if (!res.isSuccess() || !isCommandSuccess) {
+ EMLogger.getLogger().warning(
+ res.getResultMessage());
}
- } else if (!res.isSuccess() || !isCommandSuccess) {
- EMLogger.getLogger()
- .warning(res.getResultMessage());
- }
- if (ipAddr != null) {
- break;
+ if (ipAddr != null) {
+ break;
+ }
+ res = HelperClass.runProcess(cmd);
}
- res = HelperClass.runProcess(cmd);
}
-
// shows success message
final MessageDialog messageDialog = new MessageDialog();
if (isTapExist) {
@Override
public void run() {
int res = resultDialog
- .openSelectionDialog(Messages.getString("TapUtil.CreatedTapDevice.0") //$NON-NLS-1$
+ .openSelectionDialog(Messages.getString("TapUtil.CreatedTapDevice.0" + tapName) //$NON-NLS-1$
+ StringResources.NEW_LINE
- + Messages.getString("TapUtil.CreatedTapDevice.1") //$NON-NLS-1$
- + tapName + StringResources.NEW_LINE
+ Messages.getString("TapUtil.CreatedTapDevice.1")); //$NON-NLS-1$
if (res == SWT.OK) {
// Show bridge guide dialog
});
} else {
- throw new VMWorkerException("Tap is not exist : " //$NON-NLS-1$
- + tapName);
+ throw new VMWorkerException("Tap is not exist : " + tapName);//$NON-NLS-1$
}
}
} catch (VMWorkerException e) {
}
});
+ } catch (InterruptedException e) {
+ EMLogger.getLogger().warning(e.getMessage());
} finally {
Display.getDefault().asyncExec(new Runnable() {
@Override