- Timestamp:
- Jun 16, 2009 2:03:45 PM (16 years ago)
- Location:
- trunk/src/VBox
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/Network/DevPCNet.cpp
r20578 r20631 4741 4741 pThis->aCSR[0] |= RT_BIT(15) | RT_BIT(13); /* ERR | CERR (this is probably wrong) */ 4742 4742 pThis->Led.Asserted.s.fError = pThis->Led.Actual.s.fError = 1; 4743 TMTimerSetMillies(pThis->pTimerRestore, 20000);4743 TMTimerSetMillies(pThis->pTimerRestore, 5000); 4744 4744 } 4745 4745 -
trunk/src/VBox/Main/ConsoleImpl.cpp
r20560 r20631 30 30 # include <sys/types.h> 31 31 # include <sys/wait.h> 32 #ifndef VBOX_DYNAMIC_NET_ATTACH33 32 # include <net/if.h> 34 #endif /* !VBOX_DYNAMIC_NET_ATTACH */35 33 # include <linux/if_tun.h> 36 34 # include <stdio.h> … … 91 89 # include <VBox/com/array.h> 92 90 #endif 93 94 #ifdef VBOX_DYNAMIC_NET_ATTACH95 #include <VBox/intnet.h>96 97 #if defined(RT_OS_LINUX) && defined(VBOX_WITH_NETFLT)98 # include <unistd.h>99 # include <sys/ioctl.h>100 # include <sys/socket.h>101 # include <linux/types.h>102 # include <linux/if.h>103 # include <linux/wireless.h>104 #endif105 106 #if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)107 # include <VBox/WinNetConfig.h>108 # include <Ntddndis.h>109 # include <devguid.h>110 #endif111 112 #if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)113 # include <HostNetworkInterfaceImpl.h>114 # include <netif.h>115 #endif116 117 #include "DHCPServerRunner.h"118 #endif /* VBOX_DYNAMIC_NET_ATTACH */119 91 120 92 #include <set> … … 277 249 memset(&mapUSBLed, 0, sizeof(mapUSBLed)); 278 250 memset(&mapSharedFolderLed, 0, sizeof(mapSharedFolderLed)); 279 #ifdef VBOX_DYNAMIC_NET_ATTACH280 memset(&meAttachmentType, 0, sizeof(meAttachmentType));281 #endif /* VBOX_DYNAMIC_NET_ATTACH */282 251 283 252 return S_OK; … … 3312 3281 * @returns COM status code. 3313 3282 * 3314 * @param pszDevice The PDM device name. 3315 * @param uInstance The PDM device instance. 3316 * @param uLun The PDM LUN number of the drive. 3317 * @param aAttachmentType The attachment type. 3283 * @param pszDevice The PDM device name. 3284 * @param uInstance The PDM device instance. 3285 * @param uLun The PDM LUN number of the drive. 3286 * @param eAttachmentType The new attachment type. 3287 * @param meAttachmentType The current attachment type. 3288 * @param aNetworkAdapter The network adapter whose attachment needs to be changed 3318 3289 * 3319 3290 * @note Locks this object for writing. … … 3382 3353 * @returns VBox status code. 3383 3354 * 3384 * @param pThis Pointer to the Console object. 3385 * @param pszDevice The PDM device name. 3386 * @param uInstance The PDM device instance. 3387 * @param uLun The PDM LUN number of the drive. 3388 * @param aAttachmentType The attachment type. 3355 * @param pThis Pointer to the Console object. 3356 * @param pszDevice The PDM device name. 3357 * @param uInstance The PDM device instance. 3358 * @param uLun The PDM LUN number of the drive. 3359 * @param eAttachmentType The new attachment type. 3360 * @param meAttachmentType The current attachment type. 3361 * @param aNetworkAdapter The network adapter whose attachment needs to be changed 3389 3362 * 3390 3363 * @thread EMT … … 3411 3384 AutoCaller autoCaller (pThis); 3412 3385 AssertComRCReturn (autoCaller.rc(), VERR_ACCESS_DENIED); 3413 3414 /*3415 * Locking the object before doing VMR3* calls is quite safe here, since3416 * we're on EMT. Write lock is necessary because we indirectly modify the3417 * meAttachmentType member.3418 */3419 AutoWriteLock alock (pThis);3420 3386 3421 3387 /* protect mpVM */ … … 3458 3424 } 3459 3425 3460 /** @todo r=bird: This should be code common with the config constructor in3461 * ConsoleImpl2.cpp, so please move it into into a seperate method3462 * that lives in ConsoleImpl2.cpp. I believe I mentioned this already.3463 * (The header stuff on the top of this file might also be reduced3464 * after that has been done.) */3465 3426 int rc = VINF_SUCCESS; 3466 3427 int rcRet = VINF_SUCCESS; 3467 3428 3468 #define STR_CONV() do { rc = RTUtf16ToUtf8(str, &psz); RC_CHECK(); } while (0) 3469 #define STR_FREE() do { if (str) { SysFreeString(str); str = NULL; } if (psz) { RTStrFree(psz); psz = NULL; } } while (0) 3470 #define RC_CHECK() do { if (RT_FAILURE(rc)) { AssertMsgFailed(("rc=%Rrc\n", rc)); rcRet = rc; goto rcfailed; } } while (0) 3471 #define H() do { if (FAILED(hrc)) { AssertMsgFailed(("hrc=%#x\n", hrc)); rcRet = VERR_GENERAL_FAILURE; goto rcfailed; } } while (0) 3472 do 3473 { 3474 HRESULT hrc; 3475 ComPtr <IMachine> pMachine = pThis->machine(); 3476 3477 ComPtr<IVirtualBox> virtualBox; 3478 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); 3479 H(); 3480 3481 ComPtr<IHost> host; 3482 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); 3483 H(); 3484 3485 BSTR str = NULL; 3486 char *psz = NULL; 3487 3488 /* 3489 * Detach the device train for the current network attachment. 3490 */ 3491 rc = PDMR3DeviceDetach (pVM, pszDevice, uInstance, uLun); 3492 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN) 3493 rc = VINF_SUCCESS; 3494 AssertRC (rc); 3495 3496 PCFGMNODE pCfg = NULL; /* /Devices/Dev/.../Config/ */ 3497 PCFGMNODE pLunL0 = NULL; /* /Devices/Dev/0/LUN#0/ */ 3498 PCFGMNODE pInst = CFGMR3GetChildF (CFGMR3GetRoot (pVM), "Devices/%s/%d/", pszDevice, uInstance); 3499 AssertRelease (pInst); 3500 3501 /* nuke anything which might have been left behind. */ 3502 CFGMR3RemoveNode (CFGMR3GetChildF (pInst, "LUN#%d", uLun)); 3503 3504 /* 3505 * Enable the packet sniffer if requested. 3506 */ 3507 BOOL fSniffer; 3508 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer); 3509 H(); 3510 if (fSniffer) 3511 { 3512 /* insert the sniffer filter driver. */ 3513 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 3514 RC_CHECK(); 3515 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); 3516 RC_CHECK(); 3517 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 3518 RC_CHECK(); 3519 hrc = aNetworkAdapter->COMGETTER(TraceFile)(&str); 3520 H(); 3521 if (str) /* check convention for indicating default file. */ 3522 { 3523 STR_CONV(); 3524 rc = CFGMR3InsertString(pCfg, "File", psz); 3525 RC_CHECK(); 3526 STR_FREE(); 3527 } 3528 } 3529 3530 Bstr networkName, trunkName, trunkType; 3531 switch (eAttachmentType) 3532 { 3533 case NetworkAttachmentType_Null: 3534 break; 3535 3536 case NetworkAttachmentType_NAT: 3537 { 3538 if (fSniffer) 3539 { 3540 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 3541 RC_CHECK(); 3542 } 3543 else 3544 { 3545 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 3546 RC_CHECK(); 3547 } 3548 3549 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); 3550 RC_CHECK(); 3551 3552 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 3553 RC_CHECK(); 3554 3555 /* Configure TFTP prefix and boot filename. */ 3556 hrc = virtualBox->COMGETTER(HomeFolder)(&str); 3557 H(); 3558 STR_CONV(); 3559 if (psz && *psz) 3560 { 3561 char *pszTFTPPrefix = NULL; 3562 RTStrAPrintf(&pszTFTPPrefix, "%s%c%s", psz, RTPATH_DELIMITER, "TFTP"); 3563 rc = CFGMR3InsertString(pCfg, "TFTPPrefix", pszTFTPPrefix); 3564 RC_CHECK(); 3565 RTStrFree(pszTFTPPrefix); 3566 } 3567 STR_FREE(); 3568 hrc = pMachine->COMGETTER(Name)(&str); 3569 H(); 3570 STR_CONV(); 3571 char *pszBootFile = NULL; 3572 RTStrAPrintf(&pszBootFile, "%s.pxe", psz); 3573 STR_FREE(); 3574 rc = CFGMR3InsertString(pCfg, "BootFile", pszBootFile); 3575 RC_CHECK(); 3576 RTStrFree(pszBootFile); 3577 3578 hrc = aNetworkAdapter->COMGETTER(NATNetwork)(&str); 3579 H(); 3580 if (str) 3581 { 3582 STR_CONV(); 3583 if (psz && *psz) 3584 { 3585 rc = CFGMR3InsertString(pCfg, "Network", psz); 3586 RC_CHECK(); 3587 /* NAT uses its own DHCP implementation */ 3588 //networkName = Bstr(psz); 3589 } 3590 3591 STR_FREE(); 3592 } 3593 break; 3594 } 3595 3596 case NetworkAttachmentType_Bridged: 3597 { 3598 #if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX) 3599 Assert ((int)pConsole->maTapFD[ulInstance] >= 0); 3600 if ((int)pConsole->maTapFD[ulInstance] >= 0) 3601 { 3602 if (fSniffer) 3603 { 3604 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 3605 RC_CHECK(); 3606 } 3607 else 3608 { 3609 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 3610 RC_CHECK(); 3611 } 3612 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); 3613 RC_CHECK(); 3614 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 3615 RC_CHECK(); 3616 rc = CFGMR3InsertInteger(pCfg, "FileHandle", pConsole->maTapFD[ulInstance]); 3617 RC_CHECK(); 3618 } 3619 #elif defined(VBOX_WITH_NETFLT) 3620 /* 3621 * This is the new VBoxNetFlt+IntNet stuff. 3622 */ 3623 if (fSniffer) 3624 { 3625 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 3626 RC_CHECK(); 3627 } 3628 else 3629 { 3630 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 3631 RC_CHECK(); 3632 } 3633 3634 Bstr HifName; 3635 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 3636 if(FAILED(hrc)) 3637 { 3638 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 3639 H(); 3640 } 3641 3642 Utf8Str HifNameUtf8(HifName); 3643 const char *pszHifName = HifNameUtf8.raw(); 3644 3645 # if defined(RT_OS_DARWIN) 3646 /* The name is on the form 'ifX: long name', chop it off at the colon. */ 3647 char szTrunk[8]; 3648 strncpy(szTrunk, pszHifName, sizeof(szTrunk)); 3649 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 3650 if (!pszColon) 3651 { 3652 hrc = networkAdapter->Detach(); 3653 H(); 3654 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 3655 N_("Malformed host interface networking name '%ls'"), 3656 HifName.raw()); 3657 } 3658 *pszColon = '\0'; 3659 const char *pszTrunk = szTrunk; 3660 3661 # elif defined(RT_OS_SOLARIS) 3662 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */ 3663 char szTrunk[256]; 3664 strlcpy(szTrunk, pszHifName, sizeof(szTrunk)); 3665 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk)); 3666 3667 /* 3668 * Currently don't bother about malformed names here for the sake of people using 3669 * VBoxManage and setting only the NIC name from there. If there is a space we 3670 * chop it off and proceed, otherwise just use whatever we've got. 3671 */ 3672 if (pszSpace) 3673 *pszSpace = '\0'; 3674 3675 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */ 3676 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 3677 if (pszColon) 3678 *pszColon = '\0'; 3679 3680 const char *pszTrunk = szTrunk; 3681 3682 # elif defined(RT_OS_WINDOWS) 3683 ComPtr<IHostNetworkInterface> hostInterface; 3684 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 3685 if (!SUCCEEDED(rc)) 3686 { 3687 AssertBreakpoint(); 3688 LogRel(("NetworkAttachmentType_Bridged: FindByName failed, rc (0x%x)", rc)); 3689 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 3690 N_("Inexistent host networking interface, name '%ls'"), 3691 HifName.raw()); 3692 } 3693 3694 HostNetworkInterfaceType_T ifType; 3695 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 3696 if(FAILED(hrc)) 3697 { 3698 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc)); 3699 H(); 3700 } 3701 3702 if(ifType != HostNetworkInterfaceType_Bridged) 3703 { 3704 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 3705 N_("Interface ('%ls') is not a Bridged Adapter interface"), 3706 HifName.raw()); 3707 } 3708 3709 Bstr hostIFGuid_; 3710 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 3711 if(FAILED(hrc)) 3712 { 3713 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc)); 3714 H(); 3715 } 3716 Guid hostIFGuid(hostIFGuid_); 3717 3718 INetCfg *pNc; 3719 ComPtr<INetCfgComponent> pAdaptorComponent; 3720 LPWSTR lpszApp; 3721 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 3722 3723 hrc = VBoxNetCfgWinQueryINetCfg( FALSE, 3724 L"VirtualBox", 3725 &pNc, 3726 &lpszApp ); 3727 Assert(hrc == S_OK); 3728 if(hrc == S_OK) 3729 { 3730 /* get the adapter's INetCfgComponent*/ 3731 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 3732 if(hrc != S_OK) 3733 { 3734 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 3735 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 3736 H(); 3737 } 3738 } 3739 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 3740 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 3741 char *pszTrunkName = szTrunkName; 3742 wchar_t * pswzBindName; 3743 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 3744 Assert(hrc == S_OK); 3745 if (hrc == S_OK) 3746 { 3747 int cwBindName = (int)wcslen(pswzBindName) + 1; 3748 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 3749 if(sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 3750 { 3751 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 3752 pszTrunkName += cbFullBindNamePrefix-1; 3753 if(!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 3754 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 3755 { 3756 Assert(0); 3757 DWORD err = GetLastError(); 3758 hrc = HRESULT_FROM_WIN32(err); 3759 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 3760 LogRel(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x)\n", hrc, hrc)); 3761 } 3762 } 3763 else 3764 { 3765 Assert(0); 3766 LogRel(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n")); 3767 /** @todo set appropriate error code */ 3768 hrc = E_FAIL; 3769 } 3770 3771 if(hrc != S_OK) 3772 { 3773 Assert(0); 3774 CoTaskMemFree(pswzBindName); 3775 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 3776 H(); 3777 } 3778 3779 /* we're not freeing the bind name since we'll use it later for detecting wireless*/ 3780 } 3781 else 3782 { 3783 Assert(0); 3784 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 3785 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 3786 H(); 3787 } 3788 const char *pszTrunk = szTrunkName; 3789 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */ 3790 3791 # elif defined(RT_OS_LINUX) 3792 /* @todo Check for malformed names. */ 3793 const char *pszTrunk = pszHifName; 3794 3795 # else 3796 # error "PORTME (VBOX_WITH_NETFLT)" 3797 # endif 3798 3799 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 3800 RC_CHECK(); 3801 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 3802 RC_CHECK(); 3803 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); 3804 RC_CHECK(); 3805 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); 3806 RC_CHECK(); 3807 char szNetwork[80]; 3808 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 3809 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); 3810 RC_CHECK(); 3811 networkName = Bstr(szNetwork); 3812 trunkName = Bstr(pszTrunk); 3813 trunkType = Bstr(TRUNKTYPE_NETFLT); 3814 3815 # if defined(RT_OS_DARWIN) 3816 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */ 3817 if ( strstr(pszHifName, "Wireless") 3818 || strstr(pszHifName, "AirPort" )) 3819 { 3820 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 3821 RC_CHECK(); 3822 } 3823 # elif defined(RT_OS_LINUX) 3824 int iSock = socket(AF_INET, SOCK_DGRAM, 0); 3825 if (iSock >= 0) 3826 { 3827 struct iwreq WRq; 3828 3829 memset(&WRq, 0, sizeof(WRq)); 3830 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ); 3831 if (ioctl(iSock, SIOCGIWNAME, &WRq) >= 0) 3832 { 3833 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 3834 RC_CHECK(); 3835 Log(("Set SharedMacOnWire\n")); 3836 } 3837 else 3838 { 3839 Log(("Failed to get wireless name\n")); 3840 } 3841 close(iSock); 3842 } 3843 else 3844 { 3845 Log(("Failed to open wireless socket\n")); 3846 } 3847 # elif defined(RT_OS_WINDOWS) 3848 # define DEVNAME_PREFIX L"\\\\.\\" 3849 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control 3850 * there is a pretty long way till there though since we need to obtain the symbolic link name 3851 * for the adapter device we are going to query given the device Guid */ 3852 3853 3854 /* prepend the "\\\\.\\" to the bind name to obtain the link name */ 3855 3856 wchar_t FileName[MAX_PATH]; 3857 wcscpy(FileName, DEVNAME_PREFIX); 3858 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName); 3859 3860 /* open the device */ 3861 HANDLE hDevice = CreateFile(FileName, 3862 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 3863 NULL, 3864 OPEN_EXISTING, 3865 FILE_ATTRIBUTE_NORMAL, 3866 NULL); 3867 3868 if (hDevice != INVALID_HANDLE_VALUE) 3869 { 3870 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */ 3871 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM; 3872 NDIS_PHYSICAL_MEDIUM PhMedium; 3873 DWORD cbResult; 3874 if (DeviceIoControl(hDevice, 3875 IOCTL_NDIS_QUERY_GLOBAL_STATS, 3876 &Oid, 3877 sizeof(Oid), 3878 &PhMedium, 3879 sizeof(PhMedium), 3880 &cbResult, 3881 NULL)) 3882 { 3883 /* that was simple, now examine PhMedium */ 3884 if ( PhMedium == NdisPhysicalMediumWirelessWan 3885 || PhMedium == NdisPhysicalMediumWirelessLan 3886 || PhMedium == NdisPhysicalMediumNative802_11 3887 || PhMedium == NdisPhysicalMediumBluetooth) 3888 { 3889 Log(("this is a wireless adapter")); 3890 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 3891 RC_CHECK(); 3892 Log(("Set SharedMacOnWire\n")); 3893 } 3894 else 3895 { 3896 Log(("this is NOT a wireless adapter")); 3897 } 3898 } 3899 else 3900 { 3901 int winEr = GetLastError(); 3902 LogRel(("Console::configConstructor: DeviceIoControl failed, err (0x%x), ignoring\n", winEr)); 3903 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND); 3904 } 3905 3906 CloseHandle(hDevice); 3907 } 3908 else 3909 { 3910 int winEr = GetLastError(); 3911 LogRel(("Console::configConstructor: CreateFile failed, err (0x%x), ignoring\n", winEr)); 3912 AssertBreakpoint(); 3913 } 3914 3915 CoTaskMemFree(pswzBindName); 3916 3917 pAdaptorComponent.setNull(); 3918 /* release the pNc finally */ 3919 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 3920 # else 3921 /** @todo PORTME: wireless detection */ 3922 # endif 3923 3924 # if defined(RT_OS_SOLARIS) 3925 # if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */ 3926 /* Zone access restriction, don't allow snopping the global zone. */ 3927 zoneid_t ZoneId = getzoneid(); 3928 if (ZoneId != GLOBAL_ZONEID) 3929 { 3930 rc = CFGMR3InsertInteger(pCfg, "IgnoreAllPromisc", true); RC_CHECK(); 3931 } 3932 # endif 3933 # endif 3934 3935 #elif defined(RT_OS_WINDOWS) /* not defined NetFlt */ 3936 /* NOTHING TO DO HERE */ 3937 #elif defined(RT_OS_LINUX) 3938 /// @todo aleksey: is there anything to be done here? 3939 #elif defined(RT_OS_FREEBSD) 3940 /** @todo FreeBSD: Check out this later (HIF networking). */ 3941 #else 3942 # error "Port me" 3943 #endif 3944 break; 3945 } 3946 3947 case NetworkAttachmentType_Internal: 3948 { 3949 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(&str); 3950 H(); 3951 if (str) 3952 { 3953 STR_CONV(); 3954 if (psz && *psz) 3955 { 3956 if (fSniffer) 3957 { 3958 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 3959 RC_CHECK(); 3960 } 3961 else 3962 { 3963 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 3964 RC_CHECK(); 3965 } 3966 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 3967 RC_CHECK(); 3968 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 3969 RC_CHECK(); 3970 rc = CFGMR3InsertString(pCfg, "Network", psz); 3971 RC_CHECK(); 3972 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); 3973 RC_CHECK(); 3974 networkName = Bstr(psz); 3975 trunkType = Bstr(TRUNKTYPE_WHATEVER); 3976 } 3977 STR_FREE(); 3978 } 3979 break; 3980 } 3981 3982 case NetworkAttachmentType_HostOnly: 3983 { 3984 if (fSniffer) 3985 { 3986 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 3987 RC_CHECK(); 3988 } 3989 else 3990 { 3991 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 3992 RC_CHECK(); 3993 } 3994 3995 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 3996 RC_CHECK(); 3997 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 3998 RC_CHECK(); 3999 #if defined(RT_OS_WINDOWS) 4000 # ifndef VBOX_WITH_NETFLT 4001 hrc = E_NOTIMPL; 4002 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented")); 4003 H(); 4004 # else 4005 Bstr HifName; 4006 hrc = networkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 4007 if(FAILED(hrc)) 4008 { 4009 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 4010 H(); 4011 } 4012 4013 Utf8Str HifNameUtf8(HifName); 4014 const char *pszHifName = HifNameUtf8.raw(); 4015 ComPtr<IHostNetworkInterface> hostInterface; 4016 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 4017 if (!SUCCEEDED(rc)) 4018 { 4019 AssertBreakpoint(); 4020 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)", rc)); 4021 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 4022 N_("Inexistent host networking interface, name '%ls'"), 4023 HifName.raw()); 4024 } 4025 4026 HostNetworkInterfaceType_T ifType; 4027 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 4028 if(FAILED(hrc)) 4029 { 4030 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc)); 4031 H(); 4032 } 4033 4034 if(ifType != HostNetworkInterfaceType_HostOnly) 4035 { 4036 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 4037 N_("Interface ('%ls') is not a Host-Only Adapter interface"), 4038 HifName.raw()); 4039 } 4040 4041 4042 Bstr hostIFGuid_; 4043 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 4044 if(FAILED(hrc)) 4045 { 4046 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)", hrc)); 4047 H(); 4048 } 4049 Guid hostIFGuid(hostIFGuid_); 4050 4051 INetCfg *pNc; 4052 ComPtr<INetCfgComponent> pAdaptorComponent; 4053 LPWSTR lpszApp; 4054 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 4055 4056 hrc = VBoxNetCfgWinQueryINetCfg(FALSE, 4057 L"VirtualBox", 4058 &pNc, 4059 &lpszApp); 4060 Assert(hrc == S_OK); 4061 if(hrc == S_OK) 4062 { 4063 /* get the adapter's INetCfgComponent*/ 4064 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 4065 if(hrc != S_OK) 4066 { 4067 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 4068 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 4069 H(); 4070 } 4071 } 4072 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 4073 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 4074 char *pszTrunkName = szTrunkName; 4075 wchar_t * pswzBindName; 4076 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 4077 Assert(hrc == S_OK); 4078 if (hrc == S_OK) 4079 { 4080 int cwBindName = (int)wcslen(pswzBindName) + 1; 4081 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 4082 if(sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 4083 { 4084 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 4085 pszTrunkName += cbFullBindNamePrefix-1; 4086 if(!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 4087 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 4088 { 4089 Assert(0); 4090 DWORD err = GetLastError(); 4091 hrc = HRESULT_FROM_WIN32(err); 4092 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 4093 LogRel(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x)\n", hrc, hrc)); 4094 } 4095 } 4096 else 4097 { 4098 Assert(0); 4099 LogRel(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n")); 4100 /** @todo set appropriate error code */ 4101 hrc = E_FAIL; 4102 } 4103 4104 if(hrc != S_OK) 4105 { 4106 Assert(0); 4107 CoTaskMemFree(pswzBindName); 4108 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 4109 H(); 4110 } 4111 } 4112 else 4113 { 4114 Assert(0); 4115 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 4116 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 4117 H(); 4118 } 4119 4120 4121 CoTaskMemFree(pswzBindName); 4122 4123 pAdaptorComponent.setNull(); 4124 /* release the pNc finally */ 4125 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 4126 4127 const char *pszTrunk = szTrunkName; 4128 4129 4130 /* TODO: set the proper Trunk and Network values, currently the driver uses the first adapter instance */ 4131 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); 4132 RC_CHECK(); 4133 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); 4134 RC_CHECK(); 4135 char szNetwork[80]; 4136 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 4137 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); 4138 RC_CHECK(); 4139 networkName = Bstr(szNetwork); 4140 trunkName = Bstr(pszTrunk); 4141 trunkType = TRUNKTYPE_NETADP; 4142 # endif /* defined VBOX_WITH_NETFLT*/ 4143 #elif defined(RT_OS_DARWIN) 4144 rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0"); 4145 RC_CHECK(); 4146 rc = CFGMR3InsertString(pCfg, "Network", "HostInterfaceNetworking-vboxnet0"); 4147 RC_CHECK(); 4148 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); 4149 RC_CHECK(); 4150 networkName = Bstr("HostInterfaceNetworking-vboxnet0"); 4151 trunkName = Bstr("vboxnet0"); 4152 trunkType = TRUNKTYPE_NETADP; 4153 #else 4154 rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0"); 4155 RC_CHECK(); 4156 rc = CFGMR3InsertString(pCfg, "Network", "HostInterfaceNetworking-vboxnet0"); 4157 RC_CHECK(); 4158 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); 4159 RC_CHECK(); 4160 networkName = Bstr("HostInterfaceNetworking-vboxnet0"); 4161 trunkName = Bstr("vboxnet0"); 4162 trunkType = TRUNKTYPE_NETFLT; 4163 #endif 4164 #if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT) 4165 Bstr HifName; 4166 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 4167 if(FAILED(hrc)) 4168 { 4169 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 4170 H(); 4171 } 4172 4173 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface): ", hrc)); 4174 Utf8Str HifNameUtf8(HifName); 4175 const char *pszHifName = HifNameUtf8.raw(); 4176 ComPtr<IHostNetworkInterface> hostInterface; 4177 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 4178 if (!SUCCEEDED(rc)) 4179 { 4180 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)", rc)); 4181 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 4182 N_("Inexistent host networking interface, name '%ls'"), 4183 HifName.raw()); 4184 } 4185 Bstr tmpAddr, tmpMask; 4186 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPAddress"), tmpAddr.asOutParam()); 4187 if (SUCCEEDED(hrc) && !tmpAddr.isNull()) 4188 { 4189 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPNetMask"), tmpMask.asOutParam()); 4190 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty()) 4191 hrc = hostInterface->EnableStaticIpConfig(tmpAddr, tmpMask); 4192 } 4193 else 4194 hrc = hostInterface->EnableStaticIpConfig(Bstr(VBOXNET_IPV4ADDR_DEFAULT), 4195 Bstr(VBOXNET_IPV4MASK_DEFAULT)); 4196 4197 4198 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPV6Address"), tmpAddr.asOutParam()); 4199 if (SUCCEEDED(hrc)) 4200 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPV6NetMask"), tmpMask.asOutParam()); 4201 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty()) 4202 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr, Utf8Str(tmpMask).toUInt32()); 4203 #endif 4204 break; 4205 } 4206 4207 default: 4208 AssertMsgFailed(("should not get here!\n")); 4209 break; 4210 } 4211 4212 /* 4213 * Attempt to attach the driver. 4214 */ 4215 switch (eAttachmentType) 4216 { 4217 case NetworkAttachmentType_Null: 4218 break; 4219 4220 case NetworkAttachmentType_Bridged: 4221 case NetworkAttachmentType_Internal: 4222 case NetworkAttachmentType_HostOnly: 4223 case NetworkAttachmentType_NAT: 4224 { 4225 if (SUCCEEDED(hrc) && SUCCEEDED(rc)) 4226 { 4227 rc = PDMR3DeviceAttach (pVM, pszDevice, uInstance, uLun, NULL); 4228 AssertRC (rc); 4229 4230 if(!networkName.isNull()) 4231 { 4232 /* 4233 * Until we implement service reference counters DHCP Server will be stopped 4234 * by DHCPServerRunner destructor. 4235 */ 4236 ComPtr<IDHCPServer> dhcpServer; 4237 hrc = virtualBox->FindDHCPServerByNetworkName(networkName.mutableRaw(), dhcpServer.asOutParam()); 4238 if(SUCCEEDED(hrc)) 4239 { 4240 /* there is a DHCP server available for this network */ 4241 BOOL bEnabled; 4242 hrc = dhcpServer->COMGETTER(Enabled)(&bEnabled); 4243 if(FAILED(hrc)) 4244 { 4245 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (0x%x)", hrc)); 4246 H(); 4247 } 4248 4249 if(bEnabled) 4250 hrc = dhcpServer->Start(networkName, trunkName, trunkType); 4251 } 4252 else 4253 { 4254 hrc = S_OK; 4255 } 4256 } 4257 } 4258 4259 break; 4260 } 4261 4262 default: 4263 AssertMsgFailed(("should not get here!\n")); 4264 break; 4265 } 4266 4267 *meAttachmentType = eAttachmentType; 4268 } 4269 while (0); 4270 4271 #undef STR_FREE 4272 #undef STR_CONV 4273 #undef H 4274 #undef RC_CHECK 4275 4276 rcfailed: 3429 PCFGMNODE pCfg = NULL; /* /Devices/Dev/.../Config/ */ 3430 PCFGMNODE pLunL0 = NULL; /* /Devices/Dev/0/LUN#0/ */ 3431 PCFGMNODE pInst = CFGMR3GetChildF (CFGMR3GetRoot (pVM), "Devices/%s/%d/", pszDevice, uInstance); 3432 AssertRelease (pInst); 3433 3434 /* @todo r=pritesh Need to store the previous network configuration 3435 * and restore it if configNetwork fails, currently not sure if the 3436 * previous atachment will also cleanly reattach with the later one 3437 * failing to attach. 3438 */ 3439 rcRet = configNetwork(pThis, pszDevice, uInstance, uLun, eAttachmentType, meAttachmentType, 3440 aNetworkAdapter, pCfg, pLunL0, pInst, true); 3441 4277 3442 /* 4278 3443 * Resume the VM if necessary. … … 8374 7539 NULL 8375 7540 }; 7541 7542 /** 7543 * Initializing the attachment type for the network adapters 7544 */ 7545 NetworkAttachmentType_T Console::meAttachmentType[] = {}; 7546 8376 7547 /* vi: set tabstop=4 shiftwidth=4 expandtab: */ -
trunk/src/VBox/Main/ConsoleImpl2.cpp
r20521 r20631 1207 1207 * The virtual hardware type. Create appropriate device first. 1208 1208 */ 1209 const char *cszAdapterName = "pcnet"; 1209 1210 NetworkAdapterType_T adapterType; 1210 1211 hrc = networkAdapter->COMGETTER(AdapterType)(&adapterType); H(); … … 1220 1221 case NetworkAdapterType_I82545EM: 1221 1222 pDev = pDevE1000; 1223 cszAdapterName = "e1000"; 1222 1224 break; 1223 1225 #endif … … 1357 1359 } 1358 1360 1359 1361 /* 1362 * Configure the network card now 1363 */ 1360 1364 NetworkAttachmentType_T networkAttachment; 1361 1365 hrc = networkAdapter->COMGETTER(AttachmentType)(&networkAttachment); H(); 1362 Bstr networkName, trunkName, trunkType; 1363 switch (networkAttachment) 1364 { 1365 case NetworkAttachmentType_Null: 1366 #ifdef VBOX_DYNAMIC_NET_ATTACH 1367 pConsole->meAttachmentType[ulInstance] = NetworkAttachmentType_Null; 1368 #endif /* VBOX_DYNAMIC_NET_ATTACH */ 1369 break; 1370 1371 case NetworkAttachmentType_NAT: 1372 { 1373 if (fSniffer) 1374 { 1375 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK(); 1376 } 1377 else 1378 { 1379 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 1380 } 1381 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); RC_CHECK(); 1382 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 1383 /* (Port forwarding goes here.) */ 1384 1385 /* Configure TFTP prefix and boot filename. */ 1386 hrc = virtualBox->COMGETTER(HomeFolder)(&str); H(); 1387 STR_CONV(); 1388 if (psz && *psz) 1389 { 1390 char *pszTFTPPrefix = NULL; 1391 RTStrAPrintf(&pszTFTPPrefix, "%s%c%s", psz, RTPATH_DELIMITER, "TFTP"); 1392 rc = CFGMR3InsertString(pCfg, "TFTPPrefix", pszTFTPPrefix); RC_CHECK(); 1393 RTStrFree(pszTFTPPrefix); 1394 } 1395 STR_FREE(); 1396 hrc = pMachine->COMGETTER(Name)(&str); H(); 1397 STR_CONV(); 1398 char *pszBootFile = NULL; 1399 RTStrAPrintf(&pszBootFile, "%s.pxe", psz); 1400 STR_FREE(); 1401 rc = CFGMR3InsertString(pCfg, "BootFile", pszBootFile); RC_CHECK(); 1402 RTStrFree(pszBootFile); 1403 1404 hrc = networkAdapter->COMGETTER(NATNetwork)(&str); H(); 1405 if (str) 1406 { 1407 STR_CONV(); 1408 if (psz && *psz) 1409 { 1410 rc = CFGMR3InsertString(pCfg, "Network", psz); RC_CHECK(); 1411 /* NAT uses its own DHCP implementation */ 1412 //networkName = Bstr(psz); 1413 } 1414 1415 STR_FREE(); 1416 } 1417 #ifdef VBOX_DYNAMIC_NET_ATTACH 1418 pConsole->meAttachmentType[ulInstance] = NetworkAttachmentType_NAT; 1419 #endif /* VBOX_DYNAMIC_NET_ATTACH */ 1420 break; 1421 } 1422 1423 case NetworkAttachmentType_Bridged: 1424 { 1425 /* 1426 * Perform the attachment if required (don't return on error!) 1427 */ 1428 hrc = pConsole->attachToBridgedInterface(networkAdapter); 1429 if (SUCCEEDED(hrc)) 1430 { 1431 #if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX) 1432 Assert ((int)pConsole->maTapFD[ulInstance] >= 0); 1433 if ((int)pConsole->maTapFD[ulInstance] >= 0) 1434 { 1435 if (fSniffer) 1436 { 1437 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK(); 1438 } 1439 else 1440 { 1441 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 1442 } 1443 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); RC_CHECK(); 1444 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 1445 rc = CFGMR3InsertInteger(pCfg, "FileHandle", pConsole->maTapFD[ulInstance]); RC_CHECK(); 1446 } 1447 #elif defined(VBOX_WITH_NETFLT) 1448 /* 1449 * This is the new VBoxNetFlt+IntNet stuff. 1450 */ 1451 if (fSniffer) 1452 { 1453 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK(); 1454 } 1455 else 1456 { 1457 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 1458 } 1459 1460 Bstr HifName; 1461 hrc = networkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 1462 if(FAILED(hrc)) 1463 { 1464 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 1465 H(); 1466 } 1467 1468 Utf8Str HifNameUtf8(HifName); 1469 const char *pszHifName = HifNameUtf8.raw(); 1470 1471 # if defined(RT_OS_DARWIN) 1472 /* The name is on the form 'ifX: long name', chop it off at the colon. */ 1473 char szTrunk[8]; 1474 strncpy(szTrunk, pszHifName, sizeof(szTrunk)); 1475 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 1476 if (!pszColon) 1477 { 1478 hrc = networkAdapter->Detach(); H(); 1479 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 1480 N_("Malformed host interface networking name '%ls'"), 1481 HifName.raw()); 1482 } 1483 *pszColon = '\0'; 1484 const char *pszTrunk = szTrunk; 1485 1486 # elif defined(RT_OS_SOLARIS) 1487 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */ 1488 char szTrunk[256]; 1489 strlcpy(szTrunk, pszHifName, sizeof(szTrunk)); 1490 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk)); 1491 1492 /* 1493 * Currently don't bother about malformed names here for the sake of people using 1494 * VBoxManage and setting only the NIC name from there. If there is a space we 1495 * chop it off and proceed, otherwise just use whatever we've got. 1496 */ 1497 if (pszSpace) 1498 *pszSpace = '\0'; 1499 1500 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */ 1501 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 1502 if (pszColon) 1503 *pszColon = '\0'; 1504 1505 const char *pszTrunk = szTrunk; 1506 1507 # elif defined(RT_OS_WINDOWS) 1508 ComPtr<IHostNetworkInterface> hostInterface; 1509 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 1510 if (!SUCCEEDED(rc)) 1511 { 1512 AssertBreakpoint(); 1513 LogRel(("NetworkAttachmentType_Bridged: FindByName failed, rc (0x%x)", rc)); 1514 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 1515 N_("Inexistent host networking interface, name '%ls'"), 1516 HifName.raw()); 1517 } 1518 1519 HostNetworkInterfaceType_T ifType; 1520 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 1521 if(FAILED(hrc)) 1522 { 1523 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc)); 1524 H(); 1525 } 1526 1527 if(ifType != HostNetworkInterfaceType_Bridged) 1528 { 1529 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 1530 N_("Interface ('%ls') is not a Bridged Adapter interface"), 1531 HifName.raw()); 1532 } 1533 1534 Bstr hostIFGuid_; 1535 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 1536 if(FAILED(hrc)) 1537 { 1538 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc)); 1539 H(); 1540 } 1541 Guid hostIFGuid(hostIFGuid_); 1542 1543 INetCfg *pNc; 1544 ComPtr<INetCfgComponent> pAdaptorComponent; 1545 LPWSTR lpszApp; 1546 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 1547 1548 hrc = VBoxNetCfgWinQueryINetCfg( FALSE, 1549 L"VirtualBox", 1550 &pNc, 1551 &lpszApp ); 1552 Assert(hrc == S_OK); 1553 if(hrc == S_OK) 1554 { 1555 /* get the adapter's INetCfgComponent*/ 1556 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 1557 if(hrc != S_OK) 1558 { 1559 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 1560 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 1561 H(); 1562 } 1563 } 1564 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 1565 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 1566 char *pszTrunkName = szTrunkName; 1567 wchar_t * pswzBindName; 1568 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 1569 Assert(hrc == S_OK); 1570 if (hrc == S_OK) 1571 { 1572 int cwBindName = (int)wcslen(pswzBindName) + 1; 1573 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 1574 if(sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 1575 { 1576 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 1577 pszTrunkName += cbFullBindNamePrefix-1; 1578 if(!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 1579 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 1580 { 1581 Assert(0); 1582 DWORD err = GetLastError(); 1583 hrc = HRESULT_FROM_WIN32(err); 1584 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 1585 LogRel(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x)\n", hrc, hrc)); 1586 } 1587 } 1588 else 1589 { 1590 Assert(0); 1591 LogRel(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n")); 1592 /** @todo set appropriate error code */ 1593 hrc = E_FAIL; 1594 } 1595 1596 if(hrc != S_OK) 1597 { 1598 Assert(0); 1599 CoTaskMemFree(pswzBindName); 1600 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 1601 H(); 1602 } 1603 1604 /* we're not freeing the bind name since we'll use it later for detecting wireless*/ 1605 } 1606 else 1607 { 1608 Assert(0); 1609 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 1610 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 1611 H(); 1612 } 1613 const char *pszTrunk = szTrunkName; 1614 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */ 1615 1616 # elif defined(RT_OS_LINUX) 1617 /* @todo Check for malformed names. */ 1618 const char *pszTrunk = pszHifName; 1619 1620 # else 1621 # error "PORTME (VBOX_WITH_NETFLT)" 1622 # endif 1623 1624 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK(); 1625 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 1626 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK(); 1627 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); RC_CHECK(); 1628 char szNetwork[80]; 1629 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 1630 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK(); 1631 networkName = Bstr(szNetwork); 1632 trunkName = Bstr(pszTrunk); 1633 trunkType = Bstr(TRUNKTYPE_NETFLT); 1634 1635 # if defined(RT_OS_DARWIN) 1636 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */ 1637 if ( strstr(pszHifName, "Wireless") 1638 || strstr(pszHifName, "AirPort" )) 1639 { 1640 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK(); 1641 } 1642 # elif defined(RT_OS_LINUX) 1643 int iSock = socket(AF_INET, SOCK_DGRAM, 0); 1644 if (iSock >= 0) 1645 { 1646 struct iwreq WRq; 1647 1648 memset(&WRq, 0, sizeof(WRq)); 1649 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ); 1650 if (ioctl(iSock, SIOCGIWNAME, &WRq) >= 0) 1651 { 1652 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK(); 1653 Log(("Set SharedMacOnWire\n")); 1654 } 1655 else 1656 { 1657 Log(("Failed to get wireless name\n")); 1658 } 1659 close(iSock); 1660 } 1661 else 1662 { 1663 Log(("Failed to open wireless socket\n")); 1664 } 1665 # elif defined(RT_OS_WINDOWS) 1666 # define DEVNAME_PREFIX L"\\\\.\\" 1667 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control 1668 * there is a pretty long way till there though since we need to obtain the symbolic link name 1669 * for the adapter device we are going to query given the device Guid */ 1670 1671 /* prepend the "\\\\.\\" to the bind name to obtain the link name */ 1672 wchar_t FileName[MAX_PATH]; 1673 wcscpy(FileName, DEVNAME_PREFIX); 1674 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName); 1675 1676 /* open the device */ 1677 HANDLE hDevice = CreateFile(FileName, 1678 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 1679 NULL, 1680 OPEN_EXISTING, 1681 FILE_ATTRIBUTE_NORMAL, 1682 NULL); 1683 if (hDevice != INVALID_HANDLE_VALUE) 1684 { 1685 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */ 1686 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM; 1687 NDIS_PHYSICAL_MEDIUM PhMedium; 1688 DWORD cbResult; 1689 if (DeviceIoControl(hDevice, IOCTL_NDIS_QUERY_GLOBAL_STATS, &Oid, sizeof(Oid), &PhMedium, sizeof(PhMedium), &cbResult, NULL)) 1690 { 1691 /* that was simple, now examine PhMedium */ 1692 if(PhMedium == NdisPhysicalMediumWirelessWan 1693 || PhMedium == NdisPhysicalMediumWirelessLan 1694 || PhMedium == NdisPhysicalMediumNative802_11 1695 || PhMedium == NdisPhysicalMediumBluetooth 1696 /*|| PhMedium == NdisPhysicalMediumWiMax*/ 1697 ) 1698 { 1699 Log(("this is a wireless adapter")); 1700 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK(); 1701 Log(("Set SharedMacOnWire\n")); 1702 } 1703 else 1704 { 1705 Log(("this is NOT a wireless adapter")); 1706 } 1707 } 1708 else 1709 { 1710 int winEr = GetLastError(); 1711 LogRel(("Console::configConstructor: DeviceIoControl failed, err (0x%x), ignoring\n", winEr)); 1712 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND); 1713 } 1714 1715 CloseHandle(hDevice); 1716 } 1717 else 1718 { 1719 int winEr = GetLastError(); 1720 LogRel(("Console::configConstructor: CreateFile failed, err (0x%x), ignoring\n", winEr)); 1721 AssertBreakpoint(); 1722 } 1723 1724 CoTaskMemFree(pswzBindName); 1725 1726 pAdaptorComponent.setNull(); 1727 /* release the pNc finally */ 1728 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 1729 # else 1730 /** @todo PORTME: wireless detection */ 1731 # endif 1732 1733 # if defined(RT_OS_SOLARIS) 1734 # if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */ 1735 /* Zone access restriction, don't allow snopping the global zone. */ 1736 zoneid_t ZoneId = getzoneid(); 1737 if (ZoneId != GLOBAL_ZONEID) 1738 { 1739 rc = CFGMR3InsertInteger(pCfg, "IgnoreAllPromisc", true); RC_CHECK(); 1740 } 1741 # endif 1742 # endif 1743 1744 #elif defined(RT_OS_WINDOWS) /* not defined NetFlt */ 1745 /* NOTHING TO DO HERE */ 1746 #elif defined(RT_OS_LINUX) 1747 /// @todo aleksey: is there anything to be done here? 1748 #elif defined(RT_OS_FREEBSD) 1749 /** @todo FreeBSD: Check out this later (HIF networking). */ 1750 #else 1751 # error "Port me" 1752 #endif 1753 #ifdef VBOX_DYNAMIC_NET_ATTACH 1754 pConsole->meAttachmentType[ulInstance] = NetworkAttachmentType_Bridged; 1755 #endif /* VBOX_DYNAMIC_NET_ATTACH */ 1756 } 1757 else 1758 { 1759 switch (hrc) 1760 { 1761 #ifdef RT_OS_LINUX 1762 case VERR_ACCESS_DENIED: 1763 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_( 1764 "Failed to open '/dev/net/tun' for read/write access. Please check the " 1765 "permissions of that node. Either run 'chmod 0666 /dev/net/tun' or " 1766 "change the group of that node and make yourself a member of that group. Make " 1767 "sure that these changes are permanent, especially if you are " 1768 "using udev")); 1769 #endif /* RT_OS_LINUX */ 1770 default: 1771 AssertMsgFailed(("Could not attach to host interface! Bad!\n")); 1772 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_( 1773 "Failed to initialize Host Interface Networking")); 1774 } 1775 } 1776 break; 1777 } 1778 1779 case NetworkAttachmentType_Internal: 1780 { 1781 hrc = networkAdapter->COMGETTER(InternalNetwork)(&str); H(); 1782 if (str) 1783 { 1784 STR_CONV(); 1785 if (psz && *psz) 1786 { 1787 if (fSniffer) 1788 { 1789 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK(); 1790 } 1791 else 1792 { 1793 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 1794 } 1795 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK(); 1796 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 1797 rc = CFGMR3InsertString(pCfg, "Network", psz); RC_CHECK(); 1798 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); RC_CHECK(); 1799 networkName = Bstr(psz); 1800 trunkType = Bstr(TRUNKTYPE_WHATEVER); 1801 } 1802 STR_FREE(); 1803 } 1804 #ifdef VBOX_DYNAMIC_NET_ATTACH 1805 pConsole->meAttachmentType[ulInstance] = NetworkAttachmentType_Internal; 1806 #endif /* VBOX_DYNAMIC_NET_ATTACH */ 1807 break; 1808 } 1809 1810 case NetworkAttachmentType_HostOnly: 1811 { 1812 if (fSniffer) 1813 { 1814 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK(); 1815 } 1816 else 1817 { 1818 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK(); 1819 } 1820 1821 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK(); 1822 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK(); 1823 #if defined(RT_OS_WINDOWS) 1824 # ifndef VBOX_WITH_NETFLT 1825 hrc = E_NOTIMPL; 1826 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented")); 1827 H(); 1828 # else 1829 Bstr HifName; 1830 hrc = networkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 1831 if(FAILED(hrc)) 1832 { 1833 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 1834 H(); 1835 } 1836 1837 Utf8Str HifNameUtf8(HifName); 1838 const char *pszHifName = HifNameUtf8.raw(); 1839 ComPtr<IHostNetworkInterface> hostInterface; 1840 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 1841 if (!SUCCEEDED(rc)) 1842 { 1843 AssertBreakpoint(); 1844 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)", rc)); 1845 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 1846 N_("Inexistent host networking interface, name '%ls'"), 1847 HifName.raw()); 1848 } 1849 1850 HostNetworkInterfaceType_T ifType; 1851 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 1852 if(FAILED(hrc)) 1853 { 1854 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc)); 1855 H(); 1856 } 1857 1858 if(ifType != HostNetworkInterfaceType_HostOnly) 1859 { 1860 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 1861 N_("Interface ('%ls') is not a Host-Only Adapter interface"), 1862 HifName.raw()); 1863 } 1864 1865 1866 Bstr hostIFGuid_; 1867 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 1868 if(FAILED(hrc)) 1869 { 1870 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)", hrc)); 1871 H(); 1872 } 1873 Guid hostIFGuid(hostIFGuid_); 1874 1875 INetCfg *pNc; 1876 ComPtr<INetCfgComponent> pAdaptorComponent; 1877 LPWSTR lpszApp; 1878 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 1879 1880 hrc = VBoxNetCfgWinQueryINetCfg( FALSE, 1881 L"VirtualBox", 1882 &pNc, 1883 &lpszApp ); 1884 Assert(hrc == S_OK); 1885 if(hrc == S_OK) 1886 { 1887 /* get the adapter's INetCfgComponent*/ 1888 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 1889 if(hrc != S_OK) 1890 { 1891 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 1892 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 1893 H(); 1894 } 1895 } 1896 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 1897 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 1898 char *pszTrunkName = szTrunkName; 1899 wchar_t * pswzBindName; 1900 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 1901 Assert(hrc == S_OK); 1902 if (hrc == S_OK) 1903 { 1904 int cwBindName = (int)wcslen(pswzBindName) + 1; 1905 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 1906 if(sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 1907 { 1908 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 1909 pszTrunkName += cbFullBindNamePrefix-1; 1910 if(!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 1911 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 1912 { 1913 Assert(0); 1914 DWORD err = GetLastError(); 1915 hrc = HRESULT_FROM_WIN32(err); 1916 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 1917 LogRel(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x)\n", hrc, hrc)); 1918 } 1919 } 1920 else 1921 { 1922 Assert(0); 1923 LogRel(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n")); 1924 /** @todo set appropriate error code */ 1925 hrc = E_FAIL; 1926 } 1927 1928 if(hrc != S_OK) 1929 { 1930 Assert(0); 1931 CoTaskMemFree(pswzBindName); 1932 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 1933 H(); 1934 } 1935 } 1936 else 1937 { 1938 Assert(0); 1939 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 1940 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 1941 H(); 1942 } 1943 1944 1945 CoTaskMemFree(pswzBindName); 1946 1947 pAdaptorComponent.setNull(); 1948 /* release the pNc finally */ 1949 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 1950 1951 const char *pszTrunk = szTrunkName; 1952 1953 1954 /* TODO: set the proper Trunk and Network values, currently the driver uses the first adapter instance */ 1955 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK(); 1956 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK(); 1957 char szNetwork[80]; 1958 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 1959 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK(); 1960 networkName = Bstr(szNetwork); 1961 trunkName = Bstr(pszTrunk); 1962 trunkType = TRUNKTYPE_NETADP; 1963 # endif /* defined VBOX_WITH_NETFLT*/ 1964 #elif defined(RT_OS_DARWIN) 1965 rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0"); RC_CHECK(); 1966 rc = CFGMR3InsertString(pCfg, "Network", "HostInterfaceNetworking-vboxnet0"); RC_CHECK(); 1967 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK(); 1968 networkName = Bstr("HostInterfaceNetworking-vboxnet0"); 1969 trunkName = Bstr("vboxnet0"); 1970 trunkType = TRUNKTYPE_NETADP; 1971 #else 1972 rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0"); RC_CHECK(); 1973 rc = CFGMR3InsertString(pCfg, "Network", "HostInterfaceNetworking-vboxnet0"); RC_CHECK(); 1974 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); RC_CHECK(); 1975 networkName = Bstr("HostInterfaceNetworking-vboxnet0"); 1976 trunkName = Bstr("vboxnet0"); 1977 trunkType = TRUNKTYPE_NETFLT; 1978 #endif 1979 #if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT) 1980 Bstr HifName; 1981 hrc = networkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 1982 if(FAILED(hrc)) 1983 { 1984 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 1985 H(); 1986 } 1987 1988 Utf8Str HifNameUtf8(HifName); 1989 const char *pszHifName = HifNameUtf8.raw(); 1990 ComPtr<IHostNetworkInterface> hostInterface; 1991 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 1992 if (!SUCCEEDED(rc)) 1993 { 1994 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)", rc)); 1995 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 1996 N_("Inexistent host networking interface, name '%ls'"), 1997 HifName.raw()); 1998 } 1999 Bstr tmpAddr, tmpMask; 2000 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPAddress"), tmpAddr.asOutParam()); 2001 if (SUCCEEDED(hrc) && !tmpAddr.isNull()) 2002 { 2003 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPNetMask"), tmpMask.asOutParam()); 2004 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty()) 2005 hrc = hostInterface->EnableStaticIpConfig(tmpAddr, tmpMask); 2006 } 2007 else 2008 hrc = hostInterface->EnableStaticIpConfig(Bstr(VBOXNET_IPV4ADDR_DEFAULT), 2009 Bstr(VBOXNET_IPV4MASK_DEFAULT)); 2010 2011 2012 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPV6Address"), tmpAddr.asOutParam()); 2013 if (SUCCEEDED(hrc)) 2014 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPV6NetMask"), tmpMask.asOutParam()); 2015 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty()) 2016 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr, Utf8Str(tmpMask).toUInt32()); 2017 #endif 2018 #ifdef VBOX_DYNAMIC_NET_ATTACH 2019 pConsole->meAttachmentType[ulInstance] = NetworkAttachmentType_HostOnly; 2020 #endif /* VBOX_DYNAMIC_NET_ATTACH */ 2021 break; 2022 } 2023 2024 default: 2025 AssertMsgFailed(("should not get here!\n")); 2026 break; 2027 } 2028 2029 if(!networkName.isNull()) 2030 { 2031 /* 2032 * Until we implement service reference counters DHCP Server will be stopped 2033 * by DHCPServerRunner destructor. 2034 */ 2035 ComPtr<IDHCPServer> dhcpServer; 2036 hrc = virtualBox->FindDHCPServerByNetworkName(networkName.mutableRaw(), dhcpServer.asOutParam()); 2037 if(SUCCEEDED(hrc)) 2038 { 2039 /* there is a DHCP server available for this network */ 2040 BOOL bEnabled; 2041 hrc = dhcpServer->COMGETTER(Enabled)(&bEnabled); 2042 if(FAILED(hrc)) 2043 { 2044 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (0x%x)", hrc)); 2045 H(); 2046 } 2047 2048 if(bEnabled) 2049 hrc = dhcpServer->Start(networkName, trunkName, trunkType); 2050 } 2051 else 2052 { 2053 hrc = S_OK; 2054 } 2055 } 2056 1366 1367 rc = configNetwork((Console*) pvConsole, cszAdapterName, ulInstance, 0, networkAttachment, 1368 &meAttachmentType[ulInstance], networkAdapter, pCfg, pLunL0, pInst, false); 1369 RC_CHECK(); 2057 1370 } 2058 1371 … … 2742 2055 } 2743 2056 2057 2058 /** 2059 * Construct the Network configuration tree 2060 * 2061 * @returns VBox status code. 2062 * 2063 * @param pThis Pointer to the Console object. 2064 * @param pszDevice The PDM device name. 2065 * @param uInstance The PDM device instance. 2066 * @param uLun The PDM LUN number of the drive. 2067 * @param eAttachmentType The new attachment type. 2068 * @param meAttachmentType The current attachment type. 2069 * @param aNetworkAdapter The network adapter whose attachment needs to be changed 2070 * 2071 * @note Locks the Console object for writing. 2072 */ 2073 DECLCALLBACK(int) Console::configNetwork(Console *pThis, const char *pszDevice, 2074 unsigned uInstance, unsigned uLun, 2075 NetworkAttachmentType_T eAttachmentType, 2076 NetworkAttachmentType_T *meAttachmentType, 2077 INetworkAdapter *aNetworkAdapter, 2078 PCFGMNODE pCfg, PCFGMNODE pLunL0, 2079 PCFGMNODE pInst, bool attachDetach) 2080 { 2081 int rc = VINF_SUCCESS; 2082 int rcRet = VINF_SUCCESS; 2083 2084 AutoCaller autoCaller (pThis); 2085 AssertComRCReturn (autoCaller.rc(), VERR_ACCESS_DENIED); 2086 2087 /* 2088 * Locking the object before doing VMR3* calls is quite safe here, since 2089 * we're on EMT. Write lock is necessary because we indirectly modify the 2090 * meAttachmentType member. 2091 */ 2092 AutoWriteLock alock (pThis); 2093 2094 PVM pVM = pThis->mpVM; 2095 2096 #define STR_CONV() do { rc = RTUtf16ToUtf8(str, &psz); RC_CHECK(); } while (0) 2097 #define STR_FREE() do { if (str) { SysFreeString(str); str = NULL; } if (psz) { RTStrFree(psz); psz = NULL; } } while (0) 2098 #define RC_CHECK() do { if (RT_FAILURE(rc)) { AssertMsgFailed(("rc=%Rrc\n", rc)); return rc; } } while (0) 2099 #define H() do { if (FAILED(hrc)) { AssertMsgFailed(("hrc=%#x\n", hrc)); return VERR_GENERAL_FAILURE; } } while (0) 2100 do 2101 { 2102 HRESULT hrc; 2103 ComPtr <IMachine> pMachine = pThis->machine(); 2104 2105 ComPtr<IVirtualBox> virtualBox; 2106 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); 2107 H(); 2108 2109 ComPtr<IHost> host; 2110 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); 2111 H(); 2112 2113 BSTR str = NULL; 2114 char *psz = NULL; 2115 2116 /* 2117 * Detach the device train for the current network attachment. 2118 */ 2119 2120 if (attachDetach) 2121 { 2122 rc = PDMR3DeviceDetach (pVM, pszDevice, uInstance, uLun); 2123 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN) 2124 rc = VINF_SUCCESS; 2125 AssertRC (rc); 2126 2127 /* nuke anything which might have been left behind. */ 2128 CFGMR3RemoveNode (CFGMR3GetChildF (pInst, "LUN#%d", uLun)); 2129 } 2130 2131 /* 2132 * Enable the packet sniffer if requested. 2133 */ 2134 BOOL fSniffer; 2135 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer); 2136 H(); 2137 if (fSniffer) 2138 { 2139 /* insert the sniffer filter driver. */ 2140 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2141 RC_CHECK(); 2142 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); 2143 RC_CHECK(); 2144 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2145 RC_CHECK(); 2146 hrc = aNetworkAdapter->COMGETTER(TraceFile)(&str); 2147 H(); 2148 if (str) /* check convention for indicating default file. */ 2149 { 2150 STR_CONV(); 2151 rc = CFGMR3InsertString(pCfg, "File", psz); 2152 RC_CHECK(); 2153 STR_FREE(); 2154 } 2155 } 2156 2157 Bstr networkName, trunkName, trunkType; 2158 switch (eAttachmentType) 2159 { 2160 case NetworkAttachmentType_Null: 2161 break; 2162 2163 case NetworkAttachmentType_NAT: 2164 { 2165 if (fSniffer) 2166 { 2167 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2168 RC_CHECK(); 2169 } 2170 else 2171 { 2172 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2173 RC_CHECK(); 2174 } 2175 2176 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); 2177 RC_CHECK(); 2178 2179 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2180 RC_CHECK(); 2181 2182 /* Configure TFTP prefix and boot filename. */ 2183 hrc = virtualBox->COMGETTER(HomeFolder)(&str); 2184 H(); 2185 STR_CONV(); 2186 if (psz && *psz) 2187 { 2188 char *pszTFTPPrefix = NULL; 2189 RTStrAPrintf(&pszTFTPPrefix, "%s%c%s", psz, RTPATH_DELIMITER, "TFTP"); 2190 rc = CFGMR3InsertString(pCfg, "TFTPPrefix", pszTFTPPrefix); 2191 RC_CHECK(); 2192 RTStrFree(pszTFTPPrefix); 2193 } 2194 STR_FREE(); 2195 hrc = pMachine->COMGETTER(Name)(&str); 2196 H(); 2197 STR_CONV(); 2198 char *pszBootFile = NULL; 2199 RTStrAPrintf(&pszBootFile, "%s.pxe", psz); 2200 STR_FREE(); 2201 rc = CFGMR3InsertString(pCfg, "BootFile", pszBootFile); 2202 RC_CHECK(); 2203 RTStrFree(pszBootFile); 2204 2205 hrc = aNetworkAdapter->COMGETTER(NATNetwork)(&str); 2206 H(); 2207 if (str) 2208 { 2209 STR_CONV(); 2210 if (psz && *psz) 2211 { 2212 rc = CFGMR3InsertString(pCfg, "Network", psz); 2213 RC_CHECK(); 2214 /* NAT uses its own DHCP implementation */ 2215 //networkName = Bstr(psz); 2216 } 2217 2218 STR_FREE(); 2219 } 2220 break; 2221 } 2222 2223 case NetworkAttachmentType_Bridged: 2224 { 2225 #if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX) 2226 Assert ((int)pConsole->maTapFD[ulInstance] >= 0); 2227 if ((int)pConsole->maTapFD[ulInstance] >= 0) 2228 { 2229 if (fSniffer) 2230 { 2231 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2232 RC_CHECK(); 2233 } 2234 else 2235 { 2236 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2237 RC_CHECK(); 2238 } 2239 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); 2240 RC_CHECK(); 2241 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2242 RC_CHECK(); 2243 rc = CFGMR3InsertInteger(pCfg, "FileHandle", pConsole->maTapFD[ulInstance]); 2244 RC_CHECK(); 2245 } 2246 #elif defined(VBOX_WITH_NETFLT) 2247 /* 2248 * This is the new VBoxNetFlt+IntNet stuff. 2249 */ 2250 if (fSniffer) 2251 { 2252 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2253 RC_CHECK(); 2254 } 2255 else 2256 { 2257 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2258 RC_CHECK(); 2259 } 2260 2261 Bstr HifName; 2262 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 2263 if(FAILED(hrc)) 2264 { 2265 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 2266 H(); 2267 } 2268 2269 Utf8Str HifNameUtf8(HifName); 2270 const char *pszHifName = HifNameUtf8.raw(); 2271 2272 # if defined(RT_OS_DARWIN) 2273 /* The name is on the form 'ifX: long name', chop it off at the colon. */ 2274 char szTrunk[8]; 2275 strncpy(szTrunk, pszHifName, sizeof(szTrunk)); 2276 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 2277 if (!pszColon) 2278 { 2279 hrc = networkAdapter->Detach(); 2280 H(); 2281 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2282 N_("Malformed host interface networking name '%ls'"), 2283 HifName.raw()); 2284 } 2285 *pszColon = '\0'; 2286 const char *pszTrunk = szTrunk; 2287 2288 # elif defined(RT_OS_SOLARIS) 2289 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */ 2290 char szTrunk[256]; 2291 strlcpy(szTrunk, pszHifName, sizeof(szTrunk)); 2292 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk)); 2293 2294 /* 2295 * Currently don't bother about malformed names here for the sake of people using 2296 * VBoxManage and setting only the NIC name from there. If there is a space we 2297 * chop it off and proceed, otherwise just use whatever we've got. 2298 */ 2299 if (pszSpace) 2300 *pszSpace = '\0'; 2301 2302 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */ 2303 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk)); 2304 if (pszColon) 2305 *pszColon = '\0'; 2306 2307 const char *pszTrunk = szTrunk; 2308 2309 # elif defined(RT_OS_WINDOWS) 2310 ComPtr<IHostNetworkInterface> hostInterface; 2311 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 2312 if (!SUCCEEDED(rc)) 2313 { 2314 AssertBreakpoint(); 2315 LogRel(("NetworkAttachmentType_Bridged: FindByName failed, rc (0x%x)", rc)); 2316 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2317 N_("Inexistent host networking interface, name '%ls'"), 2318 HifName.raw()); 2319 } 2320 2321 HostNetworkInterfaceType_T ifType; 2322 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 2323 if(FAILED(hrc)) 2324 { 2325 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc)); 2326 H(); 2327 } 2328 2329 if(ifType != HostNetworkInterfaceType_Bridged) 2330 { 2331 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2332 N_("Interface ('%ls') is not a Bridged Adapter interface"), 2333 HifName.raw()); 2334 } 2335 2336 Bstr hostIFGuid_; 2337 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 2338 if(FAILED(hrc)) 2339 { 2340 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc)); 2341 H(); 2342 } 2343 Guid hostIFGuid(hostIFGuid_); 2344 2345 INetCfg *pNc; 2346 ComPtr<INetCfgComponent> pAdaptorComponent; 2347 LPWSTR lpszApp; 2348 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 2349 2350 hrc = VBoxNetCfgWinQueryINetCfg( FALSE, 2351 L"VirtualBox", 2352 &pNc, 2353 &lpszApp ); 2354 Assert(hrc == S_OK); 2355 if(hrc == S_OK) 2356 { 2357 /* get the adapter's INetCfgComponent*/ 2358 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 2359 if(hrc != S_OK) 2360 { 2361 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2362 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 2363 H(); 2364 } 2365 } 2366 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 2367 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 2368 char *pszTrunkName = szTrunkName; 2369 wchar_t * pswzBindName; 2370 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 2371 Assert(hrc == S_OK); 2372 if (hrc == S_OK) 2373 { 2374 int cwBindName = (int)wcslen(pswzBindName) + 1; 2375 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 2376 if(sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 2377 { 2378 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 2379 pszTrunkName += cbFullBindNamePrefix-1; 2380 if(!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 2381 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 2382 { 2383 Assert(0); 2384 DWORD err = GetLastError(); 2385 hrc = HRESULT_FROM_WIN32(err); 2386 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 2387 LogRel(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x)\n", hrc, hrc)); 2388 } 2389 } 2390 else 2391 { 2392 Assert(0); 2393 LogRel(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n")); 2394 /** @todo set appropriate error code */ 2395 hrc = E_FAIL; 2396 } 2397 2398 if(hrc != S_OK) 2399 { 2400 Assert(0); 2401 CoTaskMemFree(pswzBindName); 2402 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2403 H(); 2404 } 2405 2406 /* we're not freeing the bind name since we'll use it later for detecting wireless*/ 2407 } 2408 else 2409 { 2410 Assert(0); 2411 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2412 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 2413 H(); 2414 } 2415 const char *pszTrunk = szTrunkName; 2416 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */ 2417 2418 # elif defined(RT_OS_LINUX) 2419 /* @todo Check for malformed names. */ 2420 const char *pszTrunk = pszHifName; 2421 2422 # else 2423 # error "PORTME (VBOX_WITH_NETFLT)" 2424 # endif 2425 2426 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 2427 RC_CHECK(); 2428 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2429 RC_CHECK(); 2430 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); 2431 RC_CHECK(); 2432 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); 2433 RC_CHECK(); 2434 char szNetwork[80]; 2435 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 2436 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); 2437 RC_CHECK(); 2438 networkName = Bstr(szNetwork); 2439 trunkName = Bstr(pszTrunk); 2440 trunkType = Bstr(TRUNKTYPE_NETFLT); 2441 2442 # if defined(RT_OS_DARWIN) 2443 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */ 2444 if ( strstr(pszHifName, "Wireless") 2445 || strstr(pszHifName, "AirPort" )) 2446 { 2447 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 2448 RC_CHECK(); 2449 } 2450 # elif defined(RT_OS_LINUX) 2451 int iSock = socket(AF_INET, SOCK_DGRAM, 0); 2452 if (iSock >= 0) 2453 { 2454 struct iwreq WRq; 2455 2456 memset(&WRq, 0, sizeof(WRq)); 2457 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ); 2458 if (ioctl(iSock, SIOCGIWNAME, &WRq) >= 0) 2459 { 2460 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 2461 RC_CHECK(); 2462 Log(("Set SharedMacOnWire\n")); 2463 } 2464 else 2465 { 2466 Log(("Failed to get wireless name\n")); 2467 } 2468 close(iSock); 2469 } 2470 else 2471 { 2472 Log(("Failed to open wireless socket\n")); 2473 } 2474 # elif defined(RT_OS_WINDOWS) 2475 # define DEVNAME_PREFIX L"\\\\.\\" 2476 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control 2477 * there is a pretty long way till there though since we need to obtain the symbolic link name 2478 * for the adapter device we are going to query given the device Guid */ 2479 2480 2481 /* prepend the "\\\\.\\" to the bind name to obtain the link name */ 2482 2483 wchar_t FileName[MAX_PATH]; 2484 wcscpy(FileName, DEVNAME_PREFIX); 2485 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName); 2486 2487 /* open the device */ 2488 HANDLE hDevice = CreateFile(FileName, 2489 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 2490 NULL, 2491 OPEN_EXISTING, 2492 FILE_ATTRIBUTE_NORMAL, 2493 NULL); 2494 2495 if (hDevice != INVALID_HANDLE_VALUE) 2496 { 2497 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */ 2498 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM; 2499 NDIS_PHYSICAL_MEDIUM PhMedium; 2500 DWORD cbResult; 2501 if (DeviceIoControl(hDevice, 2502 IOCTL_NDIS_QUERY_GLOBAL_STATS, 2503 &Oid, 2504 sizeof(Oid), 2505 &PhMedium, 2506 sizeof(PhMedium), 2507 &cbResult, 2508 NULL)) 2509 { 2510 /* that was simple, now examine PhMedium */ 2511 if ( PhMedium == NdisPhysicalMediumWirelessWan 2512 || PhMedium == NdisPhysicalMediumWirelessLan 2513 || PhMedium == NdisPhysicalMediumNative802_11 2514 || PhMedium == NdisPhysicalMediumBluetooth) 2515 { 2516 Log(("this is a wireless adapter")); 2517 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); 2518 RC_CHECK(); 2519 Log(("Set SharedMacOnWire\n")); 2520 } 2521 else 2522 { 2523 Log(("this is NOT a wireless adapter")); 2524 } 2525 } 2526 else 2527 { 2528 int winEr = GetLastError(); 2529 LogRel(("Console::configConstructor: DeviceIoControl failed, err (0x%x), ignoring\n", winEr)); 2530 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND); 2531 } 2532 2533 CloseHandle(hDevice); 2534 } 2535 else 2536 { 2537 int winEr = GetLastError(); 2538 LogRel(("Console::configConstructor: CreateFile failed, err (0x%x), ignoring\n", winEr)); 2539 AssertBreakpoint(); 2540 } 2541 2542 CoTaskMemFree(pswzBindName); 2543 2544 pAdaptorComponent.setNull(); 2545 /* release the pNc finally */ 2546 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2547 # else 2548 /** @todo PORTME: wireless detection */ 2549 # endif 2550 2551 # if defined(RT_OS_SOLARIS) 2552 # if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */ 2553 /* Zone access restriction, don't allow snopping the global zone. */ 2554 zoneid_t ZoneId = getzoneid(); 2555 if (ZoneId != GLOBAL_ZONEID) 2556 { 2557 rc = CFGMR3InsertInteger(pCfg, "IgnoreAllPromisc", true); RC_CHECK(); 2558 } 2559 # endif 2560 # endif 2561 2562 #elif defined(RT_OS_WINDOWS) /* not defined NetFlt */ 2563 /* NOTHING TO DO HERE */ 2564 #elif defined(RT_OS_LINUX) 2565 /// @todo aleksey: is there anything to be done here? 2566 #elif defined(RT_OS_FREEBSD) 2567 /** @todo FreeBSD: Check out this later (HIF networking). */ 2568 #else 2569 # error "Port me" 2570 #endif 2571 break; 2572 } 2573 2574 case NetworkAttachmentType_Internal: 2575 { 2576 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(&str); 2577 H(); 2578 if (str) 2579 { 2580 STR_CONV(); 2581 if (psz && *psz) 2582 { 2583 if (fSniffer) 2584 { 2585 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2586 RC_CHECK(); 2587 } 2588 else 2589 { 2590 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2591 RC_CHECK(); 2592 } 2593 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 2594 RC_CHECK(); 2595 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2596 RC_CHECK(); 2597 rc = CFGMR3InsertString(pCfg, "Network", psz); 2598 RC_CHECK(); 2599 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); 2600 RC_CHECK(); 2601 networkName = Bstr(psz); 2602 trunkType = Bstr(TRUNKTYPE_WHATEVER); 2603 } 2604 STR_FREE(); 2605 } 2606 break; 2607 } 2608 2609 case NetworkAttachmentType_HostOnly: 2610 { 2611 if (fSniffer) 2612 { 2613 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); 2614 RC_CHECK(); 2615 } 2616 else 2617 { 2618 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); 2619 RC_CHECK(); 2620 } 2621 2622 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); 2623 RC_CHECK(); 2624 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); 2625 RC_CHECK(); 2626 #if defined(RT_OS_WINDOWS) 2627 # ifndef VBOX_WITH_NETFLT 2628 hrc = E_NOTIMPL; 2629 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented")); 2630 H(); 2631 # else 2632 Bstr HifName; 2633 hrc = networkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 2634 if(FAILED(hrc)) 2635 { 2636 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 2637 H(); 2638 } 2639 2640 Utf8Str HifNameUtf8(HifName); 2641 const char *pszHifName = HifNameUtf8.raw(); 2642 ComPtr<IHostNetworkInterface> hostInterface; 2643 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 2644 if (!SUCCEEDED(rc)) 2645 { 2646 AssertBreakpoint(); 2647 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)", rc)); 2648 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2649 N_("Inexistent host networking interface, name '%ls'"), 2650 HifName.raw()); 2651 } 2652 2653 HostNetworkInterfaceType_T ifType; 2654 hrc = hostInterface->COMGETTER(InterfaceType)(&ifType); 2655 if(FAILED(hrc)) 2656 { 2657 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc)); 2658 H(); 2659 } 2660 2661 if(ifType != HostNetworkInterfaceType_HostOnly) 2662 { 2663 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2664 N_("Interface ('%ls') is not a Host-Only Adapter interface"), 2665 HifName.raw()); 2666 } 2667 2668 2669 Bstr hostIFGuid_; 2670 hrc = hostInterface->COMGETTER(Id)(hostIFGuid_.asOutParam()); 2671 if(FAILED(hrc)) 2672 { 2673 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)", hrc)); 2674 H(); 2675 } 2676 Guid hostIFGuid(hostIFGuid_); 2677 2678 INetCfg *pNc; 2679 ComPtr<INetCfgComponent> pAdaptorComponent; 2680 LPWSTR lpszApp; 2681 int rc = VERR_INTNET_FLT_IF_NOT_FOUND; 2682 2683 hrc = VBoxNetCfgWinQueryINetCfg(FALSE, 2684 L"VirtualBox", 2685 &pNc, 2686 &lpszApp); 2687 Assert(hrc == S_OK); 2688 if(hrc == S_OK) 2689 { 2690 /* get the adapter's INetCfgComponent*/ 2691 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam()); 2692 if(hrc != S_OK) 2693 { 2694 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2695 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 2696 H(); 2697 } 2698 } 2699 #define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\" 2700 char szTrunkName[INTNET_MAX_TRUNK_NAME]; 2701 char *pszTrunkName = szTrunkName; 2702 wchar_t * pswzBindName; 2703 hrc = pAdaptorComponent->GetBindName(&pswzBindName); 2704 Assert(hrc == S_OK); 2705 if (hrc == S_OK) 2706 { 2707 int cwBindName = (int)wcslen(pswzBindName) + 1; 2708 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX); 2709 if(sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName) 2710 { 2711 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX); 2712 pszTrunkName += cbFullBindNamePrefix-1; 2713 if(!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName, 2714 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL)) 2715 { 2716 Assert(0); 2717 DWORD err = GetLastError(); 2718 hrc = HRESULT_FROM_WIN32(err); 2719 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc)); 2720 LogRel(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x)\n", hrc, hrc)); 2721 } 2722 } 2723 else 2724 { 2725 Assert(0); 2726 LogRel(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n")); 2727 /** @todo set appropriate error code */ 2728 hrc = E_FAIL; 2729 } 2730 2731 if(hrc != S_OK) 2732 { 2733 Assert(0); 2734 CoTaskMemFree(pswzBindName); 2735 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2736 H(); 2737 } 2738 } 2739 else 2740 { 2741 Assert(0); 2742 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2743 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc)); 2744 H(); 2745 } 2746 2747 2748 CoTaskMemFree(pswzBindName); 2749 2750 pAdaptorComponent.setNull(); 2751 /* release the pNc finally */ 2752 VBoxNetCfgWinReleaseINetCfg( pNc, FALSE ); 2753 2754 const char *pszTrunk = szTrunkName; 2755 2756 2757 /* TODO: set the proper Trunk and Network values, currently the driver uses the first adapter instance */ 2758 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); 2759 RC_CHECK(); 2760 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); 2761 RC_CHECK(); 2762 char szNetwork[80]; 2763 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName); 2764 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); 2765 RC_CHECK(); 2766 networkName = Bstr(szNetwork); 2767 trunkName = Bstr(pszTrunk); 2768 trunkType = TRUNKTYPE_NETADP; 2769 # endif /* defined VBOX_WITH_NETFLT*/ 2770 #elif defined(RT_OS_DARWIN) 2771 rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0"); 2772 RC_CHECK(); 2773 rc = CFGMR3InsertString(pCfg, "Network", "HostInterfaceNetworking-vboxnet0"); 2774 RC_CHECK(); 2775 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); 2776 RC_CHECK(); 2777 networkName = Bstr("HostInterfaceNetworking-vboxnet0"); 2778 trunkName = Bstr("vboxnet0"); 2779 trunkType = TRUNKTYPE_NETADP; 2780 #else 2781 rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0"); 2782 RC_CHECK(); 2783 rc = CFGMR3InsertString(pCfg, "Network", "HostInterfaceNetworking-vboxnet0"); 2784 RC_CHECK(); 2785 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); 2786 RC_CHECK(); 2787 networkName = Bstr("HostInterfaceNetworking-vboxnet0"); 2788 trunkName = Bstr("vboxnet0"); 2789 trunkType = TRUNKTYPE_NETFLT; 2790 #endif 2791 #if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT) 2792 Bstr HifName; 2793 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam()); 2794 if(FAILED(hrc)) 2795 { 2796 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc)); 2797 H(); 2798 } 2799 2800 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface): ", hrc)); 2801 Utf8Str HifNameUtf8(HifName); 2802 const char *pszHifName = HifNameUtf8.raw(); 2803 ComPtr<IHostNetworkInterface> hostInterface; 2804 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam()); 2805 if (!SUCCEEDED(rc)) 2806 { 2807 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)", rc)); 2808 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS, 2809 N_("Inexistent host networking interface, name '%ls'"), 2810 HifName.raw()); 2811 } 2812 Bstr tmpAddr, tmpMask; 2813 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPAddress"), tmpAddr.asOutParam()); 2814 if (SUCCEEDED(hrc) && !tmpAddr.isNull()) 2815 { 2816 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPNetMask"), tmpMask.asOutParam()); 2817 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty()) 2818 hrc = hostInterface->EnableStaticIpConfig(tmpAddr, tmpMask); 2819 } 2820 else 2821 hrc = hostInterface->EnableStaticIpConfig(Bstr(VBOXNET_IPV4ADDR_DEFAULT), 2822 Bstr(VBOXNET_IPV4MASK_DEFAULT)); 2823 2824 2825 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPV6Address"), tmpAddr.asOutParam()); 2826 if (SUCCEEDED(hrc)) 2827 hrc = virtualBox->GetExtraData(Bstr("HostOnly/vboxnet0/IPV6NetMask"), tmpMask.asOutParam()); 2828 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty()) 2829 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr, Utf8Str(tmpMask).toUInt32()); 2830 #endif 2831 break; 2832 } 2833 2834 default: 2835 AssertMsgFailed(("should not get here!\n")); 2836 break; 2837 } 2838 2839 /* 2840 * Attempt to attach the driver. 2841 */ 2842 switch (eAttachmentType) 2843 { 2844 case NetworkAttachmentType_Null: 2845 break; 2846 2847 case NetworkAttachmentType_Bridged: 2848 case NetworkAttachmentType_Internal: 2849 case NetworkAttachmentType_HostOnly: 2850 case NetworkAttachmentType_NAT: 2851 { 2852 if (SUCCEEDED(hrc) && SUCCEEDED(rc)) 2853 { 2854 if (attachDetach) 2855 { 2856 rc = PDMR3DeviceAttach (pVM, pszDevice, uInstance, uLun, NULL); 2857 AssertRC (rc); 2858 } 2859 2860 if(!networkName.isNull()) 2861 { 2862 /* 2863 * Until we implement service reference counters DHCP Server will be stopped 2864 * by DHCPServerRunner destructor. 2865 */ 2866 ComPtr<IDHCPServer> dhcpServer; 2867 hrc = virtualBox->FindDHCPServerByNetworkName(networkName.mutableRaw(), dhcpServer.asOutParam()); 2868 if(SUCCEEDED(hrc)) 2869 { 2870 /* there is a DHCP server available for this network */ 2871 BOOL bEnabled; 2872 hrc = dhcpServer->COMGETTER(Enabled)(&bEnabled); 2873 if(FAILED(hrc)) 2874 { 2875 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (0x%x)", hrc)); 2876 H(); 2877 } 2878 2879 if(bEnabled) 2880 hrc = dhcpServer->Start(networkName, trunkName, trunkType); 2881 } 2882 else 2883 { 2884 hrc = S_OK; 2885 } 2886 } 2887 } 2888 2889 break; 2890 } 2891 2892 default: 2893 AssertMsgFailed(("should not get here!\n")); 2894 break; 2895 } 2896 2897 *meAttachmentType = eAttachmentType; 2898 } 2899 while (0); 2900 2901 #undef STR_FREE 2902 #undef STR_CONV 2903 #undef H 2904 #undef RC_CHECK 2905 2906 return rcRet; 2907 } 2908 2744 2909 /* vi: set tabstop=4 shiftwidth=4 expandtab: */ -
trunk/src/VBox/Main/include/ConsoleImpl.h
r20523 r20631 430 430 431 431 static DECLCALLBACK(int) configConstructor(PVM pVM, void *pvConsole); 432 static DECLCALLBACK(int) configNetwork(Console *pThis, const char *pszDevice, 433 unsigned uInstance, unsigned uLun, 434 NetworkAttachmentType_T eAttachmentType, 435 NetworkAttachmentType_T *meAttachmentType, 436 INetworkAdapter *aNetworkAdapter, 437 PCFGMNODE pCfg, PCFGMNODE pLunL0, 438 PCFGMNODE pInst, bool attachDetach); 432 439 static DECLCALLBACK(void) vmstateChangeCallback(PVM aVM, VMSTATE aState, 433 440 VMSTATE aOldState, void *aUser); … … 551 558 * This does not have to match the state maintained in the Floppy. */ 552 559 DriveState_T meFloppyState; 553 #ifdef VBOX_DYNAMIC_NET_ATTACH554 560 /** The current network attachment type in the VM. 555 561 * This does not have to match the state maintained in the NetworkAdapter. */ 556 NetworkAttachmentType_T meAttachmentType[SchemaDefs::NetworkAdapterCount]; 557 #endif /* VBOX_DYNAMIC_NET_ATTACH */ 562 static NetworkAttachmentType_T meAttachmentType[SchemaDefs::NetworkAdapterCount]; 558 563 559 564 VMMDev * const mVMMDev;
Note:
See TracChangeset
for help on using the changeset viewer.