VirtualBox

Changeset 46994 in vbox for trunk


Ignore:
Timestamp:
Jul 5, 2013 9:31:09 AM (12 years ago)
Author:
vboxsync
Message:

Main/solaris/NetIf: range check

File:
1 edited

Legend:

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

    r44742 r46994  
    2525#include <iprt/err.h>
    2626#include <iprt/ctype.h>
     27#include <iprt/mem.h>
    2728#include <iprt/path.h>
    2829#include <list>
     
    199200     */
    200201    NETIFINFO Info;
    201     memset(&Info, 0, sizeof(Info));
     202    RT_ZERO(Info);
    202203    int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    203204    if (Sock > 0)
     
    413414    {
    414415        struct lifnum IfNum;
    415         memset(&IfNum, 0, sizeof(IfNum));
     416        RT_ZERO(IfNum);
    416417        IfNum.lifn_family = AF_INET;
    417418        int rc = ioctl(Sock, SIOCGLIFNUM, &IfNum);
    418419        if (!rc)
    419420        {
    420             struct lifreq Ifaces[24];
    421             struct lifconf IfConfig;
    422             memset(&IfConfig, 0, sizeof(IfConfig));
    423             IfConfig.lifc_family = AF_INET;
    424             IfConfig.lifc_len = sizeof(Ifaces);
    425             IfConfig.lifc_buf = (caddr_t)&(Ifaces[0]);
    426             rc = ioctl(Sock, SIOCGLIFCONF, &IfConfig);
    427             if (!rc)
     421            int cbIfaces = IfNum.lifn_count * sizeof(struct lifreq);
     422            struct lifreq *Ifaces = (struct lifreq *Ifaces)RTMemTmpAlloc(cbIfaces);
     423            if (Ifaces)
    428424            {
    429                 for (int i = 0; i < IfNum.lifn_count; i++)
     425                struct lifconf IfConfig;
     426                RT_ZERO(IfConfig);
     427                IfConfig.lifc_family = AF_INET;
     428                IfConfig.lifc_len = cbIfaces;
     429                IfConfig.lifc_buf = (caddr_t)Ifaces;
     430                rc = ioctl(Sock, SIOCGLIFCONF, &IfConfig);
     431                if (!rc)
    430432                {
    431                     /*
    432                      * Skip loopback interfaces.
    433                      */
    434                     if (!strncmp(Ifaces[i].lifr_name, "lo", 2))
    435                         continue;
     433                    for (int i = 0; i < IfNum.lifn_count; i++)
     434                    {
     435                        /*
     436                         * Skip loopback interfaces.
     437                         */
     438                        if (!strncmp(Ifaces[i].lifr_name, "lo", 2))
     439                            continue;
    436440
    437441#if 0
    438                     rc = ioctl(Sock, SIOCGLIFADDR, &(Ifaces[i]));
    439                     if (rc >= 0)
    440                     {
    441                         memcpy(Info.IPAddress.au8, ((struct sockaddr *)&Ifaces[i].lifr_addr)->sa_data,
    442                                sizeof(Info.IPAddress.au8));
    443                         // SIOCGLIFNETMASK
    444                         struct arpreq ArpReq;
    445                         memcpy(&ArpReq.arp_pa, &Ifaces[i].lifr_addr, sizeof(struct sockaddr_in));
    446 
    447                         /*
    448                          * We might fail if the interface has not been assigned an IP address.
    449                          * That doesn't matter; as long as it's plumbed we can pick it up.
    450                          * But, if it has not acquired an IP address we cannot obtain it's MAC
    451                          * address this way, so we just use all zeros there.
    452                          */
    453                         rc = ioctl(Sock, SIOCGARP, &ArpReq);
     442                        rc = ioctl(Sock, SIOCGLIFADDR, &(Ifaces[i]));
    454443                        if (rc >= 0)
    455                             memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
    456 
    457                         char szNICDesc[LIFNAMSIZ + 256];
     444                        {
     445                            memcpy(Info.IPAddress.au8, ((struct sockaddr *)&Ifaces[i].lifr_addr)->sa_data,
     446                                   sizeof(Info.IPAddress.au8));
     447                            // SIOCGLIFNETMASK
     448                            struct arpreq ArpReq;
     449                            memcpy(&ArpReq.arp_pa, &Ifaces[i].lifr_addr, sizeof(struct sockaddr_in));
     450
     451                            /*
     452                             * We might fail if the interface has not been assigned an IP address.
     453                             * That doesn't matter; as long as it's plumbed we can pick it up.
     454                             * But, if it has not acquired an IP address we cannot obtain it's MAC
     455                             * address this way, so we just use all zeros there.
     456                             */
     457                            rc = ioctl(Sock, SIOCGARP, &ArpReq);
     458                            if (rc >= 0)
     459                                memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
     460
     461                            char szNICDesc[LIFNAMSIZ + 256];
     462                            char *pszIface = Ifaces[i].lifr_name;
     463                            strcpy(szNICDesc, pszIface);
     464
     465                            vboxSolarisAddLinkHostIface(pszIface, &list);
     466                        }
     467#endif
     468
    458469                        char *pszIface = Ifaces[i].lifr_name;
    459                         strcpy(szNICDesc, pszIface);
    460 
    461470                        vboxSolarisAddLinkHostIface(pszIface, &list);
    462471                    }
    463 #endif
    464 
    465                     char *pszIface = Ifaces[i].lifr_name;
    466                     vboxSolarisAddLinkHostIface(pszIface, &list);
    467472                }
     473                RTMemTempFree(Ifaces);
    468474            }
    469475        }
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