VirtualBox

Changeset 19433 in vbox for trunk/src/VBox


Ignore:
Timestamp:
May 6, 2009 1:26:13 PM (16 years ago)
Author:
vboxsync
Message:

#2957: Dynamic adding/removing host-only adapters via GUI/Main.

Location:
trunk/src/VBox/Main
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/darwin/NetIf-darwin.cpp

    r19233 r19433  
    340340}
    341341
    342 int NetIfGetConfigByName(IN_BSTR aName, NETIFINFO *pInfo)
     342int NetIfGetConfigByName(PNETIFINFO pInfo)
    343343{
    344     return VERR_NOT_IMPLEMENTED;
     344    int rc = VINF_SUCCESS;
     345    size_t cbNeeded;
     346    char *pBuf, *pNext;
     347    int aiMib[6];
     348   
     349    aiMib[0] = CTL_NET;
     350    aiMib[1] = PF_ROUTE;
     351    aiMib[2] = 0;
     352    aiMib[3] = 0;       /* address family */
     353    aiMib[4] = NET_RT_IFLIST;
     354    aiMib[5] = 0;
     355
     356    if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
     357    {
     358        Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
     359        return RTErrConvertFromErrno(errno);
     360    }
     361    if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
     362        return VERR_NO_MEMORY;
     363    if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
     364    {
     365        free(pBuf);
     366        Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
     367        return RTErrConvertFromErrno(errno);
     368    }
     369
     370    int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
     371    if (sock < 0)
     372    {
     373        free(pBuf);
     374        Log(("NetIfList: socket() -> %d\n", errno));
     375        return RTErrConvertFromErrno(errno);
     376    }
     377
     378    char *pEnd = pBuf + cbNeeded;
     379    for (pNext = pBuf; pNext < pEnd;)
     380    {
     381        struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
     382
     383        if (pIfMsg->ifm_type != RTM_IFINFO)
     384        {
     385            Log(("NetIfList: Got message %u while expecting %u.\n",
     386                 pIfMsg->ifm_type, RTM_IFINFO));
     387            rc = VERR_INTERNAL_ERROR;
     388            break;
     389        }
     390        struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
     391
     392        bool fSkip = !!strcmp(pInfo->szShortName, pSdl->sdl_data);
     393
     394        pNext += pIfMsg->ifm_msglen;
     395        while (pNext < pEnd)
     396        {
     397            struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
     398
     399            if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
     400                break;
     401            if (!fSkip)
     402                extractAddresses(pIfAddrMsg->ifam_addrs, (char *)(pIfAddrMsg + 1), pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg, pInfo);
     403            pNext += pIfAddrMsg->ifam_msglen;
     404        }
     405
     406        if (!fSkip && pSdl->sdl_type == IFT_ETHER)
     407        {
     408            size_t cbNameLen = pSdl->sdl_nlen + 1;
     409            memcpy(pInfo->MACAddress.au8, LLADDR(pSdl), sizeof(pInfo->MACAddress.au8));
     410            pInfo->enmMediumType = NETIF_T_ETHERNET;
     411            /* Generate UUID from name and MAC address. */
     412            RTUUID uuid;
     413            RTUuidClear(&uuid);
     414            memcpy(&uuid, pInfo->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
     415            uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
     416            uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
     417            memcpy(uuid.Gen.au8Node, pInfo->MACAddress.au8, sizeof(uuid.Gen.au8Node));
     418            pInfo->Uuid = uuid;
     419
     420            struct ifreq IfReq;
     421            strcpy(IfReq.ifr_name, pInfo->szShortName);
     422            if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
     423            {
     424                Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
     425                pInfo->enmStatus = NETIF_S_UNKNOWN;
     426            }
     427            else
     428                pInfo->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
     429
     430            return VINF_SUCCESS;
     431        }
     432    }
     433    close(sock);
     434    free(pBuf);
     435    return rc;
    345436}
    346437
  • trunk/src/VBox/Main/generic/NetIf-generic.cpp

    r19242 r19433  
    170170                        *pLast = 0;
    171171
    172                     NETIFINFO Info;
    173                     Bstr IfName(szBuf);
    174                     rc = NetIfGetConfigByName(IfName, &Info);
    175                     if (RT_FAILURE(rc))
    176                     {
    177                         progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
    178                                                  "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add').\n", szBuf);
    179                     }
     172                    size_t cbNameLen = strlen(szBuf) + 1;
     173                    PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
     174                    if (!pInfo)
     175                        rc = VERR_NO_MEMORY;
    180176                    else
    181177                    {
    182                         /* create a new uninitialized host interface object */
    183                         ComObjPtr <HostNetworkInterface> iface;
    184                         iface.createObject();
    185                         iface->init(IfName, HostNetworkInterfaceType_HostOnly, &Info);
    186                         iface.queryInterfaceTo (aHostNetworkInterface);
     178                        strcpy(pInfo->szShortName, szBuf);
     179                        strcpy(pInfo->szName, szBuf);
     180                        rc = NetIfGetConfigByName(pInfo);
     181                        if (RT_FAILURE(rc))
     182                        {
     183                            progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
     184                                                     "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add').\n", szBuf);
     185                        }
     186                        else
     187                        {
     188                            Bstr IfName(szBuf);
     189                            /* create a new uninitialized host interface object */
     190                            ComObjPtr <HostNetworkInterface> iface;
     191                            iface.createObject();
     192                            iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
     193                            iface.queryInterfaceTo (aHostNetworkInterface);
     194                        }
     195                        RTMemFree(pInfo);
    187196                    }
    188197                }
  • trunk/src/VBox/Main/include/netif.h

    r19233 r19433  
    8686int NetIfRemoveHostOnlyNetworkInterface (VirtualBox *pVbox, IN_GUID aId, IHostNetworkInterface **aHostNetworkInterface, IProgress **aProgress);
    8787int NetIfGetConfig(HostNetworkInterface * pIf, NETIFINFO *);
    88 int NetIfGetConfigByName(IN_BSTR aName, NETIFINFO *pInfo);
     88int NetIfGetConfigByName(PNETIFINFO pInfo);
    8989int NetIfDhcpRediscover(VirtualBox *pVbox, HostNetworkInterface * pIf);
    9090
  • trunk/src/VBox/Main/linux/NetIf-linux.cpp

    r19233 r19433  
    168168}
    169169
    170 int NetIfGetConfigByName(IN_BSTR aName, NETIFINFO *pInfo)
     170int NetIfGetConfigByName(PNETIFINFO pInfo)
    171171{
    172172    int rc = VINF_SUCCESS;
     
    174174    if (sock < 0)
    175175        return VERR_NOT_IMPLEMENTED;
    176     rc = getInterfaceInfo(sock, Utf8Str(aName).c_str(), pInfo);
     176    rc = getInterfaceInfo(sock, pInfo->szShortName, pInfo);
    177177    close(sock);
    178178    return rc;
  • trunk/src/VBox/Main/solaris/NetIf-solaris.cpp

    r19233 r19433  
    394394#endif
    395395
    396 int NetIfGetConfigByName(IN_BSTR aName, NETIFINFO *pInfo)
     396int NetIfGetConfigByName(PNETIFINFO pInfo)
    397397{
    398398    return VERR_NOT_IMPLEMENTED;
  • trunk/src/VBox/Main/win/NetIf-win.cpp

    r19239 r19433  
    10411041}
    10421042
    1043 int NetIfGetConfigByName(IN_BSTR /* aName */, NETIFINFO *)
     1043int NetIfGetConfigByName(PNETIFINFO)
    10441044{
    10451045    return VERR_NOT_IMPLEMENTED;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette