Changeset 22162 in vbox
- Timestamp:
- Aug 11, 2009 12:03:47 PM (15 years ago)
- Location:
- trunk/src/VBox/Main
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/ConsoleImpl2.cpp
r22070 r22162 3 3 * VBox Console COM Class implementation 4 4 * 5 * @remark We've split out the code that the 64-bit VC++ v8 compiler 6 * finds problematic to optimize so we can disable optimizations 7 * and later, perhaps, find a real solution for it. 5 * @remark We've split out the code that the 64-bit VC++ v8 compiler finds 6 * problematic to optimize so we can disable optimizations and later, 7 * perhaps, find a real solution for it (like rewriting the code and 8 * to stop resemble a tonne of spaghetti). 8 9 */ 9 10 … … 1091 1092 1092 1093 char szLUN[16]; 1093 RTStrPrintf (szLUN, sizeof(szLUN), "LUN#% d", iLUN);1094 RTStrPrintf (szLUN, sizeof(szLUN), "LUN#%u", iLUN); 1094 1095 1095 1096 rc = CFGMR3InsertNode (pCtlInst, szLUN, &pLunL0); RC_CHECK(); … … 2085 2086 * @param pLunL0 To store the pointer to the LUN#0. 2086 2087 * @param pInst The instance CFGM node 2087 * @param attachDetach To determine if the network attachment should be attached/detached 2088 * after/before configuration. 2088 * @param fAttachDetach To determine if the network attachment should 2089 * be attached/detached after/before 2090 * configuration. 2089 2091 * 2090 2092 * @note Locks the Console object for writing. … … 2094 2096 INetworkAdapter *aNetworkAdapter, 2095 2097 PCFGMNODE pCfg, PCFGMNODE pLunL0, 2096 PCFGMNODE pInst, bool attachDetach)2098 PCFGMNODE pInst, bool fAttachDetach) 2097 2099 { 2098 2100 int rc = VINF_SUCCESS; 2099 int rcRet = VINF_SUCCESS;2100 2101 2101 2102 AutoCaller autoCaller(pThis); 2102 AssertComRCReturn 2103 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED); 2103 2104 2104 2105 /* … … 2109 2110 AutoWriteLock alock(pThis); 2110 2111 2111 PVM pVM = pThis->mpVM; 2112 PVM pVM = pThis->mpVM; 2113 BSTR str = NULL; 2114 char *psz = NULL; 2112 2115 2113 2116 #define STR_CONV() do { rc = RTUtf16ToUtf8(str, &psz); RC_CHECK(); } while (0) 2114 2117 #define STR_FREE() do { if (str) { SysFreeString(str); str = NULL; } if (psz) { RTStrFree(psz); psz = NULL; } } while (0) 2115 #define RC_CHECK() do { if (RT_FAILURE(rc)) { AssertMsgFailed(("rc=%Rrc\n", rc)); return rc; } } while (0) 2116 #define H() do { if (FAILED(hrc)) { AssertMsgFailed(("hrc=%#x\n", hrc)); return VERR_GENERAL_FAILURE; } } while (0) 2117 do 2118 { 2119 HRESULT hrc; 2120 ComPtr<IMachine> pMachine = pThis->machine(); 2121 2122 ComPtr<IVirtualBox> virtualBox; 2123 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); 2124 H(); 2125 2126 ComPtr<IHost> host; 2127 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); 2128 H(); 2129 2130 BSTR str = NULL; 2131 char *psz = NULL; 2132 2133 /* 2134 * Detach the device train for the current network attachment. 2135 */ 2136 2137 if (attachDetach) 2138 { 2139 rc = PDMR3DeviceDetach (pVM, pszDevice, uInstance, uLun, PDMDEVATT_FLAGS_NOT_HOT_PLUG); 2140 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN) 2141 rc = VINF_SUCCESS; 2142 AssertRC (rc); 2143 2144 /* nuke anything which might have been left behind. */ 2145 CFGMR3RemoveNode (CFGMR3GetChildF (pInst, "LUN#%d", uLun)); 2146 } 2147 2148 /* 2149 * Enable the packet sniffer if requested. 2150 */ 2151 BOOL fSniffer; 2152 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer); 2153 H(); 2154 if (fSniffer) 2155 { 2156 /* insert the sniffer filter driver. */ 2157 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2158 RC_CHECK(); 2159 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); 2160 RC_CHECK(); 2161 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2162 RC_CHECK(); 2163 hrc = aNetworkAdapter->COMGETTER(TraceFile)(&str); 2118 #define RC_CHECK() do { if (RT_FAILURE(rc)) { AssertMsgFailed(("rc=%Rrc\n", rc)); STR_FREE(); return rc; } } while (0) 2119 #define H() do { if (FAILED(hrc)) { AssertMsgFailed(("hrc=%#x\n", hrc)); STR_FREE(); return VERR_GENERAL_FAILURE; } } while (0) 2120 2121 HRESULT hrc; 2122 ComPtr<IMachine> pMachine = pThis->machine(); 2123 2124 ComPtr<IVirtualBox> virtualBox; 2125 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); 2126 H(); 2127 2128 ComPtr<IHost> host; 2129 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); 2130 H(); 2131 2132 /* 2133 * Detach the device train for the current network attachment. 2134 */ 2135 if (fAttachDetach) 2136 { 2137 rc = PDMR3DeviceDetach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/); 2138 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN) 2139 rc = VINF_SUCCESS; 2140 AssertLogRelRCReturn(rc, rc); 2141 2142 /* nuke anything which might have been left behind. */ 2143 CFGMR3RemoveNode(CFGMR3GetChildF(pInst, "LUN#%u", uLun)); 2144 } 2145 2146 /* 2147 * Enable the packet sniffer if requested. 2148 */ 2149 BOOL fSniffer; 2150 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer); 2151 H(); 2152 if (fSniffer) 2153 { 2154 /* insert the sniffer filter driver. */ 2155 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 2156 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); RC_CHECK(); 2157 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 2158 hrc = aNetworkAdapter->COMGETTER(TraceFile)(&str); H(); 2159 if (str) /* check convention for indicating default file. */ 2160 { 2161 STR_CONV(); 2162 rc = CFGMR3InsertString(pCfg, "File", psz); RC_CHECK(); 2163 STR_FREE(); 2164 } 2165 } 2166 2167 Bstr networkName, trunkName, trunkType; 2168 NetworkAttachmentType_T eAttachmentType; 2169 hrc = aNetworkAdapter->COMGETTER(AttachmentType)(&eAttachmentType); 2170 H(); 2171 switch (eAttachmentType) 2172 { 2173 case NetworkAttachmentType_Null: 2174 break; 2175 2176 case NetworkAttachmentType_NAT: 2177 { 2178 if (fSniffer) 2179 { 2180 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK(); 2181 } 2182 else 2183 { 2184 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 2185 } 2186 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); RC_CHECK(); 2187 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 2188 2189 /* Configure TFTP prefix and boot filename. */ 2190 hrc = virtualBox->COMGETTER(HomeFolder)(&str); 2164 2191 H(); 2165 if (str) /* check convention for indicating default file. */ 2166 { 2167 STR_CONV(); 2168 rc = CFGMR3InsertString(pCfg, "File", psz); 2169 RC_CHECK(); 2170 STR_FREE(); 2171 } 2172 } 2173 2174 Bstr networkName, trunkName, trunkType; 2175 NetworkAttachmentType_T eAttachmentType; 2176 hrc = aNetworkAdapter->COMGETTER(AttachmentType)(&eAttachmentType); 2177 H(); 2178 switch (eAttachmentType) 2179 { 2180 case NetworkAttachmentType_Null: 2181 break; 2182 2183 case NetworkAttachmentType_NAT: 2184 { 2185 if (fSniffer) 2186 { 2187 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2188 RC_CHECK(); 2189 } 2190 else 2191 { 2192 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2193 RC_CHECK(); 2194 } 2195 2196 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); 2197 RC_CHECK(); 2198 2199 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2200 RC_CHECK(); 2201 2202 /* Configure TFTP prefix and boot filename. */ 2203 hrc = virtualBox->COMGETTER(HomeFolder)(&str); 2204 H(); 2192 STR_CONV(); 2193 if (psz && *psz) 2194 { 2195 rc = CFGMR3InsertStringF(pCfg, "TFTPPrefix", "%s%c%s", psz, RTPATH_DELIMITER, "TFTP"); RC_CHECK(); 2196 } 2197 STR_FREE(); 2198 hrc = pMachine->COMGETTER(Name)(&str); 2199 H(); 2200 STR_CONV(); 2201 rc = CFGMR3InsertStringF(pCfg, "BootFile", "%s.pxe", psz); RC_CHECK(); 2202 STR_FREE(); 2203 2204 hrc = aNetworkAdapter->COMGETTER(NATNetwork)(&str); 2205 H(); 2206 if (str) 2207 { 2205 2208 STR_CONV(); 2206 2209 if (psz && *psz) 2207 2210 { 2208 char *pszTFTPPrefix = NULL; 2209 RTStrAPrintf(&pszTFTPPrefix, "%s%c%s", psz, RTPATH_DELIMITER, "TFTP"); 2210 rc = CFGMR3InsertString(pCfg, "TFTPPrefix", pszTFTPPrefix); 2211 rc = CFGMR3InsertString(pCfg, "Network", psz); RC_CHECK(); 2212 /* NAT uses its own DHCP implementation */ 2213 //networkName = Bstr(psz); 2214 } 2215 2216 STR_FREE(); 2217 } 2218 break; 2219 } 2220 2221 case NetworkAttachmentType_Bridged: 2222 { 2223 #if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX) 2224 Assert ((int)pThis->maTapFD[uInstance] >= 0); 2225 if ((int)pThis->maTapFD[uInstance] >= 0) 2226 { 2227 if (fSniffer) 2228 { 2229 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK(); 2230 } 2231 else 2232 { 2233 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 2234 } 2235 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); RC_CHECK(); 2236 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 2237 rc = CFGMR3InsertInteger(pCfg, "FileHandle", pThis->maTapFD[uInstance]); RC_CHECK(); 2238 } 2239 #elif defined(VBOX_WITH_NETFLT) 2240 /* 2241 * This is the new VBoxNetFlt+IntNet stuff. 2242 */ 2243 if (fSniffer) 2244 { 2245 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK(); 2246 } 2247 else 2248 { 2249 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 2250 } 2251 2252 Bstr HifName; 2253 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 2254 if (FAILED(hrc)) 2255 { 2256 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 2257 H(); 2258 } 2259 2260 Utf8Str HifNameUtf8(HifName); 2261 const char *pszHifName = HifNameUtf8.raw(); 2262 2263 # if defined(RT_OS_DARWIN) 2264 /* The name is on the form 'ifX: long name', chop it off at the colon. */ 2265 char szTrunk[8]; 2266 strncpy(szTrunk, pszHifName, sizeof(szTrunk)); 2267 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 2268 if (!pszColon) 2269 { 2270 hrc = aNetworkAdapter->Detach(); H(); 2271 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2272 N_("Malformed host interface networking name '%ls'"), 2273 HifName.raw()); 2274 } 2275 *pszColon = '\0'; 2276 const char *pszTrunk = szTrunk; 2277 2278 # elif defined(RT_OS_SOLARIS) 2279 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */ 2280 char szTrunk[256]; 2281 strlcpy(szTrunk, pszHifName, sizeof(szTrunk)); 2282 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk)); 2283 2284 /* 2285 * Currently don't bother about malformed names here for the sake of people using 2286 * VBoxManage and setting only the NIC name from there. If there is a space we 2287 * chop it off and proceed, otherwise just use whatever we've got. 2288 */ 2289 if (pszSpace) 2290 *pszSpace = '\0'; 2291 2292 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */ 2293 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 2294 if (pszColon) 2295 *pszColon = '\0'; 2296 2297 const char *pszTrunk = szTrunk; 2298 2299 # elif defined(RT_OS_WINDOWS) 2300 ComPtr<IHostNetworkInterface> hostInterface; 2301 hrc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 2302 if (!SUCCEEDED(hrc)) 2303 { 2304 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: FindByName failed, rc=%Rhrc (0x%x)", hrc, hrc)); 2305 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2306 N_("Inexistent host networking interface, name '%ls'"), 2307 HifName.raw()); 2308 } 2309 2310 HostNetworkInterfaceType_T ifType; 2311 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 2312 if (FAILED(hrc)) 2313 { 2314 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc)); 2315 H(); 2316 } 2317 2318 if (ifType != HostNetworkInterfaceType_Bridged) 2319 { 2320 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2321 N_("Interface ('%ls') is not a Bridged Adapter interface"), 2322 HifName.raw()); 2323 } 2324 2325 Bstr hostIFGuid_; 2326 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 2327 if (FAILED(hrc)) 2328 { 2329 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc)); 2330 H(); 2331 } 2332 Guid hostIFGuid(hostIFGuid_); 2333 2334 INetCfg *pNc; 2335 ComPtr<INetCfgComponent> pAdaptorComponent; 2336 LPWSTR pszApp; 2337 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 2338 2339 hrc = VBoxNetCfgWinQueryINetCfg(FALSE /*fGetWriteLock*/, 2340 L"VirtualBox", 2341 &pNc, 2342 &pszApp); 2343 Assert(hrc == S_OK); 2344 if (hrc == S_OK) 2345 { 2346 /* get the adapter's INetCfgComponent*/ 2347 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 2348 if (hrc != S_OK) 2349 { 2350 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/); 2351 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 2352 H(); 2353 } 2354 } 2355 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 2356 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 2357 char *pszTrunkName = szTrunkName; 2358 wchar_t * pswzBindName; 2359 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 2360 Assert(hrc == S_OK); 2361 if (hrc == S_OK) 2362 { 2363 int cwBindName = (int)wcslen(pswzBindName) + 1; 2364 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 2365 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 2366 { 2367 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 2368 pszTrunkName += cbFullBindNamePrefix-1; 2369 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 2370 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 2371 { 2372 DWORD err = GetLastError(); 2373 hrc = HRESULT_FROM_WIN32(err); 2374 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 2375 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err)); 2376 } 2377 } 2378 else 2379 { 2380 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n")); 2381 /** @todo set appropriate error code */ 2382 hrc = E_FAIL; 2383 } 2384 2385 if (hrc != S_OK) 2386 { 2387 AssertFailed(); 2388 CoTaskMemFree(pswzBindName); 2389 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/); 2390 H(); 2391 } 2392 2393 /* we're not freeing the bind name since we'll use it later for detecting wireless*/ 2394 } 2395 else 2396 { 2397 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/); 2398 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 2399 H(); 2400 } 2401 const char *pszTrunk = szTrunkName; 2402 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */ 2403 2404 # elif defined(RT_OS_LINUX) 2405 /* @todo Check for malformed names. */ 2406 const char *pszTrunk = pszHifName; 2407 2408 # else 2409 # error "PORTME (VBOX_WITH_NETFLT)" 2410 # endif 2411 2412 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK(); 2413 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 2414 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK(); 2415 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); 2416 RC_CHECK(); 2417 char szNetwork[80]; 2418 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 2419 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK(); 2420 networkName = Bstr(szNetwork); 2421 trunkName = Bstr(pszTrunk); 2422 trunkType = Bstr(TRUNKTYPE_NETFLT); 2423 2424 # if defined(RT_OS_DARWIN) 2425 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */ 2426 if ( strstr(pszHifName, "Wireless") 2427 || strstr(pszHifName, "AirPort" )) 2428 { 2429 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK(); 2430 } 2431 # elif defined(RT_OS_LINUX) 2432 int iSock = socket(AF_INET, SOCK_DGRAM, 0); 2433 if (iSock >= 0) 2434 { 2435 struct iwreq WRq; 2436 2437 memset(&WRq, 0, sizeof(WRq)); 2438 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ); 2439 bool fSharedMacOnWire = ioctl(iSock, SIOCGIWNAME, &WRq) >= 0; 2440 close(iSock); 2441 if (fSharedMacOnWire) 2442 { 2443 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 2211 2444 RC_CHECK(); 2212 RTStrFree(pszTFTPPrefix); 2213 } 2214 STR_FREE(); 2215 hrc = pMachine->COMGETTER(Name)(&str); 2216 H(); 2445 Log(("Set SharedMacOnWire\n")); 2446 } 2447 else 2448 Log(("Failed to get wireless name\n")); 2449 } 2450 else 2451 Log(("Failed to open wireless socket\n")); 2452 # elif defined(RT_OS_WINDOWS) 2453 # define DEVNAME_PREFIX L"\\\\.\\" 2454 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control 2455 * there is a pretty long way till there though since we need to obtain the symbolic link name 2456 * for the adapter device we are going to query given the device Guid */ 2457 2458 2459 /* prepend the "\\\\.\\" to the bind name to obtain the link name */ 2460 2461 wchar_t FileName[MAX_PATH]; 2462 wcscpy(FileName, DEVNAME_PREFIX); 2463 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName); 2464 2465 /* open the device */ 2466 HANDLE hDevice = CreateFile(FileName, 2467 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 2468 NULL, 2469 OPEN_EXISTING, 2470 FILE_ATTRIBUTE_NORMAL, 2471 NULL); 2472 2473 if (hDevice != INVALID_HANDLE_VALUE) 2474 { 2475 bool fSharedMacOnWire = false; 2476 2477 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */ 2478 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM; 2479 NDIS_PHYSICAL_MEDIUM PhMedium; 2480 DWORD cbResult; 2481 if (DeviceIoControl(hDevice, 2482 IOCTL_NDIS_QUERY_GLOBAL_STATS, 2483 &Oid, 2484 sizeof(Oid), 2485 &PhMedium, 2486 sizeof(PhMedium), 2487 &cbResult, 2488 NULL)) 2489 { 2490 /* that was simple, now examine PhMedium */ 2491 if ( PhMedium == NdisPhysicalMediumWirelessWan 2492 || PhMedium == NdisPhysicalMediumWirelessLan 2493 || PhMedium == NdisPhysicalMediumNative802_11 2494 || PhMedium == NdisPhysicalMediumBluetooth) 2495 fSharedMacOnWire = true; 2496 } 2497 else 2498 { 2499 int winEr = GetLastError(); 2500 LogRel(("Console::configConstructor: DeviceIoControl failed, err (0x%x), ignoring\n", winEr)); 2501 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND); 2502 } 2503 CloseHandle(hDevice); 2504 2505 if (fSharedMacOnWire) 2506 { 2507 Log(("this is a wireless adapter")); 2508 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK(); 2509 Log(("Set SharedMacOnWire\n")); 2510 } 2511 else 2512 Log(("this is NOT a wireless adapter")); 2513 } 2514 else 2515 { 2516 int winEr = GetLastError(); 2517 AssertLogRelMsgFailed(("Console::configConstructor: CreateFile failed, err (0x%x), ignoring\n", winEr)); 2518 } 2519 2520 CoTaskMemFree(pswzBindName); 2521 2522 pAdaptorComponent.setNull(); 2523 /* release the pNc finally */ 2524 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/); 2525 # else 2526 /** @todo PORTME: wireless detection */ 2527 # endif 2528 2529 # if defined(RT_OS_SOLARIS) 2530 # if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */ 2531 /* Zone access restriction, don't allow snopping the global zone. */ 2532 zoneid_t ZoneId = getzoneid(); 2533 if (ZoneId != GLOBAL_ZONEID) 2534 { 2535 rc = CFGMR3InsertInteger(pCfg, "IgnoreAllPromisc", true); RC_CHECK(); 2536 } 2537 # endif 2538 # endif 2539 2540 #elif defined(RT_OS_WINDOWS) /* not defined NetFlt */ 2541 /* NOTHING TO DO HERE */ 2542 #elif defined(RT_OS_LINUX) 2543 /// @todo aleksey: is there anything to be done here? 2544 #elif defined(RT_OS_FREEBSD) 2545 /** @todo FreeBSD: Check out this later (HIF networking). */ 2546 #else 2547 # error "Port me" 2548 #endif 2549 break; 2550 } 2551 2552 case NetworkAttachmentType_Internal: 2553 { 2554 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(&str); H(); 2555 if (str) 2556 { 2217 2557 STR_CONV(); 2218 char *pszBootFile = NULL; 2219 RTStrAPrintf(&pszBootFile, "%s.pxe", psz); 2220 STR_FREE(); 2221 rc = CFGMR3InsertString(pCfg, "BootFile", pszBootFile); 2222 RC_CHECK(); 2223 RTStrFree(pszBootFile); 2224 2225 hrc = aNetworkAdapter->COMGETTER(NATNetwork)(&str); 2226 H(); 2227 if (str) 2228 { 2229 STR_CONV(); 2230 if (psz && *psz) 2231 { 2232 rc = CFGMR3InsertString(pCfg, "Network", psz); 2233 RC_CHECK(); 2234 /* NAT uses its own DHCP implementation */ 2235 //networkName = Bstr(psz); 2236 } 2237 2238 STR_FREE(); 2239 } 2240 break; 2241 } 2242 2243 case NetworkAttachmentType_Bridged: 2244 { 2245 #if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX) 2246 Assert ((int)pThis->maTapFD[uInstance] >= 0); 2247 if ((int)pThis->maTapFD[uInstance] >= 0) 2558 if (psz && *psz) 2248 2559 { 2249 2560 if (fSniffer) … … 2257 2568 RC_CHECK(); 2258 2569 } 2259 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); 2260 RC_CHECK(); 2261 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2262 RC_CHECK(); 2263 rc = CFGMR3InsertInteger(pCfg, "FileHandle", pThis->maTapFD[uInstance]); 2264 RC_CHECK(); 2265 } 2266 #elif defined(VBOX_WITH_NETFLT) 2267 /* 2268 * This is the new VBoxNetFlt+IntNet stuff. 2269 */ 2270 if (fSniffer) 2271 { 2272 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2273 RC_CHECK(); 2570 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK(); 2571 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 2572 rc = CFGMR3InsertString(pCfg, "Network", psz); RC_CHECK(); 2573 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); RC_CHECK(); 2574 networkName = Bstr(psz); 2575 trunkType = Bstr(TRUNKTYPE_WHATEVER); 2576 } 2577 STR_FREE(); 2578 } 2579 break; 2580 } 2581 2582 case NetworkAttachmentType_HostOnly: 2583 { 2584 if (fSniffer) 2585 { 2586 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2587 RC_CHECK(); 2588 } 2589 else 2590 { 2591 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2592 RC_CHECK(); 2593 } 2594 2595 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK(); 2596 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 2597 2598 Bstr HifName; 2599 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 2600 if (FAILED(hrc)) 2601 { 2602 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)\n", hrc)); 2603 H(); 2604 } 2605 2606 Utf8Str HifNameUtf8(HifName); 2607 const char *pszHifName = HifNameUtf8.raw(); 2608 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface): %s\n", pszHifName)); 2609 ComPtr<IHostNetworkInterface> hostInterface; 2610 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 2611 if (!SUCCEEDED(rc)) 2612 { 2613 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)\n", rc)); 2614 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2615 N_("Inexistent host networking interface, name '%ls'"), 2616 HifName.raw()); 2617 } 2618 2619 char szNetwork[80]; 2620 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 2621 2622 #if defined(RT_OS_WINDOWS) 2623 # ifndef VBOX_WITH_NETFLT 2624 hrc = E_NOTIMPL; 2625 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented\n")); 2626 H(); 2627 # else /* defined VBOX_WITH_NETFLT*/ 2628 /** @todo r=bird: Put this in a function. */ 2629 2630 HostNetworkInterfaceType_T ifType; 2631 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 2632 if (FAILED(hrc)) 2633 { 2634 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc)); 2635 H(); 2636 } 2637 2638 if (ifType != HostNetworkInterfaceType_HostOnly) 2639 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2640 N_("Interface ('%ls') is not a Host-Only Adapter interface"), 2641 HifName.raw()); 2642 2643 Bstr hostIFGuid_; 2644 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 2645 if (FAILED(hrc)) 2646 { 2647 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)\n", hrc)); 2648 H(); 2649 } 2650 Guid hostIFGuid(hostIFGuid_); 2651 2652 INetCfg *pNc; 2653 ComPtr<INetCfgComponent> pAdaptorComponent; 2654 LPWSTR pszApp; 2655 rc = VERR_INTNET_FLT_IF_NOT_FOUND; 2656 2657 hrc = VBoxNetCfgWinQueryINetCfg(FALSE, 2658 L"VirtualBox", 2659 &pNc, 2660 &pszApp); 2661 Assert(hrc == S_OK); 2662 if (hrc == S_OK) 2663 { 2664 /* get the adapter's INetCfgComponent*/ 2665 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 2666 if (hrc != S_OK) 2667 { 2668 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/); 2669 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc)); 2670 H(); 2671 } 2672 } 2673 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 2674 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 2675 char *pszTrunkName = szTrunkName; 2676 wchar_t * pswzBindName; 2677 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 2678 Assert(hrc == S_OK); 2679 if (hrc == S_OK) 2680 { 2681 int cwBindName = (int)wcslen(pswzBindName) + 1; 2682 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 2683 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 2684 { 2685 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 2686 pszTrunkName += cbFullBindNamePrefix-1; 2687 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 2688 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 2689 { 2690 DWORD err = GetLastError(); 2691 hrc = HRESULT_FROM_WIN32(err); 2692 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err)); 2693 } 2274 2694 } 2275 2695 else 2276 2696 { 2277 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2278 RC_CHECK(); 2279 } 2280 2281 Bstr HifName; 2282 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 2283 if(FAILED(hrc)) 2284 { 2285 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 2697 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n")); 2698 /** @todo set appropriate error code */ 2699 hrc = E_FAIL; 2700 } 2701 2702 if (hrc != S_OK) 2703 { 2704 AssertFailed(); 2705 CoTaskMemFree(pswzBindName); 2706 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/); 2286 2707 H(); 2287 2708 } 2288 2289 Utf8Str HifNameUtf8(HifName); 2290 const char *pszHifName = HifNameUtf8.raw(); 2291 2292 # if defined(RT_OS_DARWIN) 2293 /* The name is on the form 'ifX: long name', chop it off at the colon. */ 2294 char szTrunk[8]; 2295 strncpy(szTrunk, pszHifName, sizeof(szTrunk)); 2296 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 2297 if (!pszColon) 2298 { 2299 hrc = aNetworkAdapter->Detach(); 2300 H(); 2301 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2302 N_("Malformed host interface networking name '%ls'"), 2303 HifName.raw()); 2304 } 2305 *pszColon = '\0'; 2306 const char *pszTrunk = szTrunk; 2307 2308 # elif defined(RT_OS_SOLARIS) 2309 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */ 2310 char szTrunk[256]; 2311 strlcpy(szTrunk, pszHifName, sizeof(szTrunk)); 2312 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk)); 2313 2314 /* 2315 * Currently don't bother about malformed names here for the sake of people using 2316 * VBoxManage and setting only the NIC name from there. If there is a space we 2317 * chop it off and proceed, otherwise just use whatever we've got. 2318 */ 2319 if (pszSpace) 2320 *pszSpace = '\0'; 2321 2322 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */ 2323 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 2324 if (pszColon) 2325 *pszColon = '\0'; 2326 2327 const char *pszTrunk = szTrunk; 2328 2329 # elif defined(RT_OS_WINDOWS) 2330 ComPtr<IHostNetworkInterface> hostInterface; 2331 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 2332 if (!SUCCEEDED(rc)) 2333 { 2334 AssertBreakpoint(); 2335 LogRel(("NetworkAttachmentType_Bridged: FindByName failed, rc (0x%x)", rc)); 2336 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2337 N_("Inexistent host networking interface, name '%ls'"), 2338 HifName.raw()); 2339 } 2340 2341 HostNetworkInterfaceType_T ifType; 2342 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 2343 if(FAILED(hrc)) 2344 { 2345 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc)); 2346 H(); 2347 } 2348 2349 if(ifType != HostNetworkInterfaceType_Bridged) 2350 { 2351 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2352 N_("Interface ('%ls') is not a Bridged Adapter interface"), 2353 HifName.raw()); 2354 } 2355 2356 Bstr hostIFGuid_; 2357 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 2358 if(FAILED(hrc)) 2359 { 2360 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc)); 2361 H(); 2362 } 2363 Guid hostIFGuid(hostIFGuid_); 2364 2365 INetCfg *pNc; 2366 ComPtr<INetCfgComponent> pAdaptorComponent; 2367 LPWSTR lpszApp; 2368 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 2369 2370 hrc = VBoxNetCfgWinQueryINetCfg( FALSE, 2371 L"VirtualBox", 2372 &pNc, 2373 &lpszApp ); 2374 Assert(hrc == S_OK); 2375 if(hrc == S_OK) 2376 { 2377 /* get the adapter's INetCfgComponent*/ 2378 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 2379 if(hrc != S_OK) 2709 } 2710 else 2711 { 2712 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/); 2713 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc)); 2714 H(); 2715 } 2716 2717 2718 CoTaskMemFree(pswzBindName); 2719 2720 pAdaptorComponent.setNull(); 2721 /* release the pNc finally */ 2722 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/); 2723 2724 const char *pszTrunk = szTrunkName; 2725 2726 2727 /* TODO: set the proper Trunk and Network values, currently the driver uses the first adapter instance */ 2728 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK(); 2729 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK(); 2730 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK(); 2731 networkName = Bstr(szNetwork); 2732 trunkName = Bstr(pszTrunk); 2733 trunkType = TRUNKTYPE_NETADP; 2734 # endif /* defined VBOX_WITH_NETFLT*/ 2735 #elif defined(RT_OS_DARWIN) 2736 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); RC_CHECK(); 2737 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK(); 2738 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK(); 2739 networkName = Bstr(szNetwork); 2740 trunkName = Bstr(pszHifName); 2741 trunkType = TRUNKTYPE_NETADP; 2742 #else 2743 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); RC_CHECK(); 2744 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK(); 2745 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); RC_CHECK(); 2746 networkName = Bstr(szNetwork); 2747 trunkName = Bstr(pszHifName); 2748 trunkType = TRUNKTYPE_NETFLT; 2749 #endif 2750 #if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT) 2751 2752 Bstr tmpAddr, tmpMask; 2753 2754 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPAddress", pszHifName), tmpAddr.asOutParam()); 2755 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty()) 2756 { 2757 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPNetMask", pszHifName), tmpMask.asOutParam()); 2758 if (SUCCEEDED(hrc) && !tmpMask.isEmpty()) 2759 hrc = hostInterface->EnableStaticIpConfig(BstrFmt("HostOnly/%s/IPAddress", pszHifName), 2760 BstrFmt("HostOnly/%s/IPNetMask", pszHifName)); 2761 else 2762 hrc = hostInterface->EnableStaticIpConfig(BstrFmt("HostOnly/%s/IPAddress", pszHifName), 2763 Bstr(VBOXNET_IPV4MASK_DEFAULT)); 2764 } 2765 else 2766 hrc = hostInterface->EnableStaticIpConfig(Bstr(VBOXNET_IPV4ADDR_DEFAULT), 2767 Bstr(VBOXNET_IPV4MASK_DEFAULT)); 2768 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */ 2769 2770 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6Address", pszHifName), tmpAddr.asOutParam()); 2771 if (SUCCEEDED(hrc)) 2772 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6NetMask", pszHifName), tmpMask.asOutParam()); 2773 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty() && !tmpMask.isEmpty()) 2774 { 2775 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr, Utf8Str(tmpMask).toUInt32()); 2776 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */ 2777 } 2778 #endif 2779 break; 2780 } 2781 2782 default: 2783 AssertMsgFailed(("should not get here!\n")); 2784 break; 2785 } 2786 2787 /* 2788 * Attempt to attach the driver. 2789 */ 2790 switch (eAttachmentType) 2791 { 2792 case NetworkAttachmentType_Null: 2793 break; 2794 2795 case NetworkAttachmentType_Bridged: 2796 case NetworkAttachmentType_Internal: 2797 case NetworkAttachmentType_HostOnly: 2798 case NetworkAttachmentType_NAT: 2799 { 2800 if (SUCCEEDED(hrc) && SUCCEEDED(rc)) 2801 { 2802 if (fAttachDetach) 2803 { 2804 rc = PDMR3DeviceAttach(pVM, pszDevice, uInstance, uLun, NULL, 0 /*fFlags*/); 2805 AssertRC(rc); 2806 } 2807 2808 { 2809 /** @todo pritesh: get the dhcp server name from the 2810 * previous network configuration and then stop the server 2811 * else it may conflict with the dhcp server running with 2812 * the current attachment type 2813 */ 2814 /* Stop the hostonly DHCP Server */ 2815 } 2816 2817 if (!networkName.isNull()) 2818 { 2819 /* 2820 * Until we implement service reference counters DHCP Server will be stopped 2821 * by DHCPServerRunner destructor. 2822 */ 2823 ComPtr<IDHCPServer> dhcpServer; 2824 hrc = virtualBox->FindDHCPServerByNetworkName(networkName.mutableRaw(), dhcpServer.asOutParam()); 2825 if (SUCCEEDED(hrc)) 2380 2826 { 2381 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2382 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 2383 H(); 2384 } 2385 } 2386 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 2387 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 2388 char *pszTrunkName = szTrunkName; 2389 wchar_t * pswzBindName; 2390 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 2391 Assert(hrc == S_OK); 2392 if (hrc == S_OK) 2393 { 2394 int cwBindName = (int)wcslen(pswzBindName) + 1; 2395 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 2396 if(sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 2397 { 2398 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 2399 pszTrunkName += cbFullBindNamePrefix-1; 2400 if(!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 2401 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 2827 /* there is a DHCP server available for this network */ 2828 BOOL fEnabled; 2829 hrc = dhcpServer->COMGETTER(Enabled)(&fEnabled); 2830 if (FAILED(hrc)) 2402 2831 { 2403 Assert(0); 2404 DWORD err = GetLastError(); 2405 hrc = HRESULT_FROM_WIN32(err); 2406 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 2407 LogRel(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x)\n", hrc, hrc)); 2832 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (%Rhrc)", hrc)); 2833 H(); 2408 2834 } 2835 2836 if (fEnabled) 2837 hrc = dhcpServer->Start(networkName, trunkName, trunkType); 2409 2838 } 2410 2839 else 2411 { 2412 Assert(0); 2413 LogRel(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n")); 2414 /** @todo set appropriate error code */ 2415 hrc = E_FAIL; 2416 } 2417 2418 if(hrc != S_OK) 2419 { 2420 Assert(0); 2421 CoTaskMemFree(pswzBindName); 2422 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2423 H(); 2424 } 2425 2426 /* we're not freeing the bind name since we'll use it later for detecting wireless*/ 2427 } 2428 else 2429 { 2430 Assert(0); 2431 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2432 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 2433 H(); 2434 } 2435 const char *pszTrunk = szTrunkName; 2436 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */ 2437 2438 # elif defined(RT_OS_LINUX) 2439 /* @todo Check for malformed names. */ 2440 const char *pszTrunk = pszHifName; 2441 2442 # else 2443 # error "PORTME (VBOX_WITH_NETFLT)" 2444 # endif 2445 2446 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 2447 RC_CHECK(); 2448 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2449 RC_CHECK(); 2450 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); 2451 RC_CHECK(); 2452 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); 2453 RC_CHECK(); 2454 char szNetwork[80]; 2455 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 2456 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); 2457 RC_CHECK(); 2458 networkName = Bstr(szNetwork); 2459 trunkName = Bstr(pszTrunk); 2460 trunkType = Bstr(TRUNKTYPE_NETFLT); 2461 2462 # if defined(RT_OS_DARWIN) 2463 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */ 2464 if ( strstr(pszHifName, "Wireless") 2465 || strstr(pszHifName, "AirPort" )) 2466 { 2467 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 2468 RC_CHECK(); 2469 } 2470 # elif defined(RT_OS_LINUX) 2471 int iSock = socket(AF_INET, SOCK_DGRAM, 0); 2472 if (iSock >= 0) 2473 { 2474 struct iwreq WRq; 2475 2476 memset(&WRq, 0, sizeof(WRq)); 2477 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ); 2478 if (ioctl(iSock, SIOCGIWNAME, &WRq) >= 0) 2479 { 2480 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 2481 RC_CHECK(); 2482 Log(("Set SharedMacOnWire\n")); 2483 } 2484 else 2485 { 2486 Log(("Failed to get wireless name\n")); 2487 } 2488 close(iSock); 2489 } 2490 else 2491 { 2492 Log(("Failed to open wireless socket\n")); 2493 } 2494 # elif defined(RT_OS_WINDOWS) 2495 # define DEVNAME_PREFIX L"\\\\.\\" 2496 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control 2497 * there is a pretty long way till there though since we need to obtain the symbolic link name 2498 * for the adapter device we are going to query given the device Guid */ 2499 2500 2501 /* prepend the "\\\\.\\" to the bind name to obtain the link name */ 2502 2503 wchar_t FileName[MAX_PATH]; 2504 wcscpy(FileName, DEVNAME_PREFIX); 2505 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName); 2506 2507 /* open the device */ 2508 HANDLE hDevice = CreateFile(FileName, 2509 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 2510 NULL, 2511 OPEN_EXISTING, 2512 FILE_ATTRIBUTE_NORMAL, 2513 NULL); 2514 2515 if (hDevice != INVALID_HANDLE_VALUE) 2516 { 2517 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */ 2518 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM; 2519 NDIS_PHYSICAL_MEDIUM PhMedium; 2520 DWORD cbResult; 2521 if (DeviceIoControl(hDevice, 2522 IOCTL_NDIS_QUERY_GLOBAL_STATS, 2523 &Oid, 2524 sizeof(Oid), 2525 &PhMedium, 2526 sizeof(PhMedium), 2527 &cbResult, 2528 NULL)) 2529 { 2530 /* that was simple, now examine PhMedium */ 2531 if ( PhMedium == NdisPhysicalMediumWirelessWan 2532 || PhMedium == NdisPhysicalMediumWirelessLan 2533 || PhMedium == NdisPhysicalMediumNative802_11 2534 || PhMedium == NdisPhysicalMediumBluetooth) 2535 { 2536 Log(("this is a wireless adapter")); 2537 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 2538 RC_CHECK(); 2539 Log(("Set SharedMacOnWire\n")); 2540 } 2541 else 2542 { 2543 Log(("this is NOT a wireless adapter")); 2544 } 2545 } 2546 else 2547 { 2548 int winEr = GetLastError(); 2549 LogRel(("Console::configConstructor: DeviceIoControl failed, err (0x%x), ignoring\n", winEr)); 2550 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND); 2551 } 2552 2553 CloseHandle(hDevice); 2554 } 2555 else 2556 { 2557 int winEr = GetLastError(); 2558 LogRel(("Console::configConstructor: CreateFile failed, err (0x%x), ignoring\n", winEr)); 2559 AssertBreakpoint(); 2560 } 2561 2562 CoTaskMemFree(pswzBindName); 2563 2564 pAdaptorComponent.setNull(); 2565 /* release the pNc finally */ 2566 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2567 # else 2568 /** @todo PORTME: wireless detection */ 2569 # endif 2570 2571 # if defined(RT_OS_SOLARIS) 2572 # if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */ 2573 /* Zone access restriction, don't allow snopping the global zone. */ 2574 zoneid_t ZoneId = getzoneid(); 2575 if (ZoneId != GLOBAL_ZONEID) 2576 { 2577 rc = CFGMR3InsertInteger(pCfg, "IgnoreAllPromisc", true); RC_CHECK(); 2578 } 2579 # endif 2580 # endif 2581 2582 #elif defined(RT_OS_WINDOWS) /* not defined NetFlt */ 2583 /* NOTHING TO DO HERE */ 2584 #elif defined(RT_OS_LINUX) 2585 /// @todo aleksey: is there anything to be done here? 2586 #elif defined(RT_OS_FREEBSD) 2587 /** @todo FreeBSD: Check out this later (HIF networking). */ 2588 #else 2589 # error "Port me" 2590 #endif 2591 break; 2592 } 2593 2594 case NetworkAttachmentType_Internal: 2595 { 2596 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(&str); 2597 H(); 2598 if (str) 2599 { 2600 STR_CONV(); 2601 if (psz && *psz) 2602 { 2603 if (fSniffer) 2604 { 2605 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2606 RC_CHECK(); 2607 } 2608 else 2609 { 2610 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2611 RC_CHECK(); 2612 } 2613 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 2614 RC_CHECK(); 2615 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2616 RC_CHECK(); 2617 rc = CFGMR3InsertString(pCfg, "Network", psz); 2618 RC_CHECK(); 2619 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); 2620 RC_CHECK(); 2621 networkName = Bstr(psz); 2622 trunkType = Bstr(TRUNKTYPE_WHATEVER); 2623 } 2624 STR_FREE(); 2625 } 2626 break; 2627 } 2628 2629 case NetworkAttachmentType_HostOnly: 2630 { 2631 if (fSniffer) 2632 { 2633 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2634 RC_CHECK(); 2635 } 2636 else 2637 { 2638 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2639 RC_CHECK(); 2640 } 2641 2642 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 2643 RC_CHECK(); 2644 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2645 RC_CHECK(); 2646 2647 Bstr HifName; 2648 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 2649 if(FAILED(hrc)) 2650 { 2651 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)\n", hrc)); 2652 H(); 2653 } 2654 2655 Utf8Str HifNameUtf8(HifName); 2656 const char *pszHifName = HifNameUtf8.raw(); 2657 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface): %s\n", pszHifName)); 2658 ComPtr<IHostNetworkInterface> hostInterface; 2659 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 2660 if (!SUCCEEDED(rc)) 2661 { 2662 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)\n", rc)); 2663 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2664 N_("Inexistent host networking interface, name '%ls'"), 2665 HifName.raw()); 2666 } 2667 2668 char szNetwork[80]; 2669 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 2670 2671 #if defined(RT_OS_WINDOWS) 2672 # ifndef VBOX_WITH_NETFLT 2673 hrc = E_NOTIMPL; 2674 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented\n")); 2675 H(); 2676 # else /* defined VBOX_WITH_NETFLT*/ 2677 2678 HostNetworkInterfaceType_T ifType; 2679 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 2680 if(FAILED(hrc)) 2681 { 2682 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc)); 2683 H(); 2684 } 2685 2686 if(ifType != HostNetworkInterfaceType_HostOnly) 2687 { 2688 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2689 N_("Interface ('%ls') is not a Host-Only Adapter interface"), 2690 HifName.raw()); 2691 } 2692 2693 Bstr hostIFGuid_; 2694 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 2695 if(FAILED(hrc)) 2696 { 2697 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)\n", hrc)); 2698 H(); 2699 } 2700 Guid hostIFGuid(hostIFGuid_); 2701 2702 INetCfg *pNc; 2703 ComPtr<INetCfgComponent> pAdaptorComponent; 2704 LPWSTR lpszApp; 2705 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 2706 2707 hrc = VBoxNetCfgWinQueryINetCfg(FALSE, 2708 L"VirtualBox", 2709 &pNc, 2710 &lpszApp); 2711 Assert(hrc == S_OK); 2712 if(hrc == S_OK) 2713 { 2714 /* get the adapter's INetCfgComponent*/ 2715 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 2716 if(hrc != S_OK) 2717 { 2718 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2719 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)\n", hrc)); 2720 H(); 2721 } 2722 } 2723 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 2724 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 2725 char *pszTrunkName = szTrunkName; 2726 wchar_t * pswzBindName; 2727 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 2728 Assert(hrc == S_OK); 2729 if (hrc == S_OK) 2730 { 2731 int cwBindName = (int)wcslen(pswzBindName) + 1; 2732 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 2733 if(sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 2734 { 2735 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 2736 pszTrunkName += cbFullBindNamePrefix-1; 2737 if(!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 2738 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 2739 { 2740 Assert(0); 2741 DWORD err = GetLastError(); 2742 hrc = HRESULT_FROM_WIN32(err); 2743 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 2744 LogRel(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x)\n", hrc, hrc)); 2745 } 2746 } 2747 else 2748 { 2749 Assert(0); 2750 LogRel(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n")); 2751 /** @todo set appropriate error code */ 2752 hrc = E_FAIL; 2753 } 2754 2755 if(hrc != S_OK) 2756 { 2757 Assert(0); 2758 CoTaskMemFree(pswzBindName); 2759 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2760 H(); 2761 } 2762 } 2763 else 2764 { 2765 Assert(0); 2766 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2767 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)\n", hrc)); 2768 H(); 2769 } 2770 2771 2772 CoTaskMemFree(pswzBindName); 2773 2774 pAdaptorComponent.setNull(); 2775 /* release the pNc finally */ 2776 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2777 2778 const char *pszTrunk = szTrunkName; 2779 2780 2781 /* TODO: set the proper Trunk and Network values, currently the driver uses the first adapter instance */ 2782 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); 2783 RC_CHECK(); 2784 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); 2785 RC_CHECK(); 2786 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); 2787 RC_CHECK(); 2788 networkName = Bstr(szNetwork); 2789 trunkName = Bstr(pszTrunk); 2790 trunkType = TRUNKTYPE_NETADP; 2791 # endif /* defined VBOX_WITH_NETFLT*/ 2792 #elif defined(RT_OS_DARWIN) 2793 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); 2794 RC_CHECK(); 2795 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); 2796 RC_CHECK(); 2797 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); 2798 RC_CHECK(); 2799 networkName = Bstr(szNetwork); 2800 trunkName = Bstr(pszHifName); 2801 trunkType = TRUNKTYPE_NETADP; 2802 #else 2803 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); 2804 RC_CHECK(); 2805 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); 2806 RC_CHECK(); 2807 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); 2808 RC_CHECK(); 2809 networkName = Bstr(szNetwork); 2810 trunkName = Bstr(pszHifName); 2811 trunkType = TRUNKTYPE_NETFLT; 2812 #endif 2813 #if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT) 2814 2815 Bstr tmpAddr, tmpMask; 2816 2817 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPAddress", pszHifName), tmpAddr.asOutParam()); 2818 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty()) 2819 { 2820 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPNetMask", pszHifName), tmpMask.asOutParam()); 2821 if (SUCCEEDED(hrc) && !tmpMask.isEmpty()) 2822 hrc = hostInterface->EnableStaticIpConfig(BstrFmt("HostOnly/%s/IPAddress", pszHifName), 2823 BstrFmt("HostOnly/%s/IPNetMask", pszHifName)); 2824 else 2825 hrc = hostInterface->EnableStaticIpConfig(BstrFmt("HostOnly/%s/IPAddress", pszHifName), 2826 Bstr(VBOXNET_IPV4MASK_DEFAULT)); 2827 } 2828 else 2829 hrc = hostInterface->EnableStaticIpConfig(Bstr(VBOXNET_IPV4ADDR_DEFAULT), 2830 Bstr(VBOXNET_IPV4MASK_DEFAULT)); 2831 2832 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6Address", pszHifName), tmpAddr.asOutParam()); 2833 if (SUCCEEDED(hrc)) 2834 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6NetMask", pszHifName), tmpMask.asOutParam()); 2835 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty() && !tmpMask.isEmpty()) 2836 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr, Utf8Str(tmpMask).toUInt32()); 2837 #endif 2838 break; 2839 } 2840 2841 default: 2842 AssertMsgFailed(("should not get here!\n")); 2843 break; 2844 } 2845 2846 /* 2847 * Attempt to attach the driver. 2848 */ 2849 switch (eAttachmentType) 2850 { 2851 case NetworkAttachmentType_Null: 2852 break; 2853 2854 case NetworkAttachmentType_Bridged: 2855 case NetworkAttachmentType_Internal: 2856 case NetworkAttachmentType_HostOnly: 2857 case NetworkAttachmentType_NAT: 2858 { 2859 if (SUCCEEDED(hrc) && SUCCEEDED(rc)) 2860 { 2861 if (attachDetach) 2862 { 2863 rc = PDMR3DeviceAttach (pVM, pszDevice, uInstance, uLun, NULL, PDMDEVATT_FLAGS_NOT_HOT_PLUG); 2864 AssertRC (rc); 2865 } 2866 2867 { 2868 /** @todo r=pritesh: get the dhcp server name from the 2869 * previous network configuration and then stop the server 2870 * else it may conflict with the dhcp server running with 2871 * the current attachment type 2872 */ 2873 /* Stop the hostonly DHCP Server */ 2874 } 2875 2876 if(!networkName.isNull()) 2877 { 2878 /* 2879 * Until we implement service reference counters DHCP Server will be stopped 2880 * by DHCPServerRunner destructor. 2881 */ 2882 ComPtr<IDHCPServer> dhcpServer; 2883 hrc = virtualBox->FindDHCPServerByNetworkName(networkName.mutableRaw(), dhcpServer.asOutParam()); 2884 if(SUCCEEDED(hrc)) 2885 { 2886 /* there is a DHCP server available for this network */ 2887 BOOL bEnabled; 2888 hrc = dhcpServer->COMGETTER(Enabled)(&bEnabled); 2889 if(FAILED(hrc)) 2890 { 2891 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (0x%x)", hrc)); 2892 H(); 2893 } 2894 2895 if(bEnabled) 2896 hrc = dhcpServer->Start(networkName, trunkName, trunkType); 2897 } 2898 else 2899 { 2900 hrc = S_OK; 2901 } 2902 } 2903 } 2904 2905 break; 2906 } 2907 2908 default: 2909 AssertMsgFailed(("should not get here!\n")); 2910 break; 2911 } 2912 2913 meAttachmentType[uInstance] = eAttachmentType; 2914 } 2915 while (0); 2840 hrc = S_OK; 2841 } 2842 } 2843 2844 break; 2845 } 2846 2847 default: 2848 AssertMsgFailed(("should not get here!\n")); 2849 break; 2850 } 2851 2852 meAttachmentType[uInstance] = eAttachmentType; 2916 2853 2917 2854 #undef STR_FREE … … 2920 2857 #undef RC_CHECK 2921 2858 2922 return rcRet;2859 return VINF_SUCCESS; 2923 2860 } 2924 2861 -
trunk/src/VBox/Main/include/ConsoleImpl.h
r21961 r22162 433 433 INetworkAdapter *aNetworkAdapter, 434 434 PCFGMNODE pCfg, PCFGMNODE pLunL0, 435 PCFGMNODE pInst, bool attachDetach);435 PCFGMNODE pInst, bool fAttachDetach); 436 436 static DECLCALLBACK(void) vmstateChangeCallback(PVM aVM, VMSTATE aState, 437 437 VMSTATE aOldState, void *aUser);
Note:
See TracChangeset
for help on using the changeset viewer.