VirtualBox

Changeset 50761 in vbox


Ignore:
Timestamp:
Mar 13, 2014 8:30:17 AM (11 years ago)
Author:
vboxsync
Message:

VBoxNetFlt-darwin.cpp: style + only detect p_count offset once.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/VBoxNetFlt/darwin/VBoxNetFlt-darwin.cpp

    r50747 r50761  
    55
    66/*
    7  * Copyright (C) 2006-2012 Oracle Corporation
     7 * Copyright (C) 2006-2014 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    139139static mbuf_tag_id_t g_idTag;
    140140
    141 /** the offset of the struct ifnet::if_pcount variable (valid for Lion). */
     141/** The offset of the struct ifnet::if_pcount variable.
     142 * @remarks Initial value is valid for Lion and earlier. We adjust it on attach
     143 *          for later releases.  */
    142144static unsigned g_offIfNetPCount = sizeof(void *) * (1 /*if_softc*/ + 1 /*if_name*/ + 2 /*if_link*/ + 2 /*if_addrhead*/ + 1 /*if_check_multi*/)
    143145                                 + sizeof(u_long) /*if_refcnt*/;
    144146/** Macro for accessing ifnet::if_pcount. */
    145147#define VBOX_GET_PCOUNT(pIfNet) ( *(int *)((uintptr_t)pIfNet + g_offIfNetPCount) )
    146 
    147148/** The size of area of ifnet structure we try to locate if_pcount in. */
    148149#define VBOXNETFLT_DARWIN_IFNET_SIZE 256
    149 
    150 /*
    151  * Note that this implementation relies on if_pcount to be aligned on sizeof(int).
     150/** Indicates whether g_offIfNetPCount has been adjusted already (no point in
     151 * doing it more than once). */
     152static bool g_fNetPCountFound  = false;
     153
     154
     155/**
     156 * Change the promiscuous setting and try spot the changed in @a pIfNet.
     157 *
     158 * @returns Offset of potential p_count field.
     159 * @param   pIfNet      The interface we're attaching to.
     160 * @param   iPromisc    Whether to enable (1) or disable (0) promiscuous mode.
     161 *
     162 * @note    This implementation relies on if_pcount to be aligned on sizeof(int).
    152163 */
    153164static unsigned vboxNetFltDarwinSetAndDiff(ifnet_t pIfNet, int iPromisc)
    154165{
    155     unsigned i;
    156     int aSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE/sizeof(int)];
    157 
    158     memcpy(aSavedState, pIfNet, sizeof(aSavedState));
     166    int aiSavedState[VBOXNETFLT_DARWIN_IFNET_SIZE / sizeof(int)];
     167    memcpy(aiSavedState, pIfNet, sizeof(aiSavedState));
     168
    159169    ifnet_set_promiscuous(pIfNet, iPromisc);
    160170
    161     int offset = 0;
    162     int iDiff = iPromisc ? 1 : -1;
     171    int const iDiff = iPromisc ? 1 : -1;
     172
    163173    /*
    164174     * We assume that ifnet structure will never have less members in front of if_pcount
     
    166176     * have to start from zero offset.
    167177     */
    168     for (i = g_offIfNetPCount / sizeof(int); i < sizeof(aSavedState) / sizeof(int); i++)
    169         if (((int*)pIfNet)[i] - aSavedState[i] == iDiff)
    170         {
    171             offset = i * sizeof(int);
    172             break;
    173         }
    174 
    175     return offset;
    176 }
     178    for (unsigned i = g_offIfNetPCount / sizeof(int); i < RT_ELEMENTS(aiSavedState); i++)
     179        if (((int*)pIfNet)[i] - aiSavedState[i] == iDiff)
     180            return i * sizeof(int);
     181
     182    return 0;
     183}
     184
    177185
    178186/**
    179187 * Detect and adjust the offset of ifnet::if_pcount.
     188 *
     189 * @param   pIfNet      The interface we're attaching to.
    180190 */
    181191static void vboxNetFltDarwinDetectPCountOffset(ifnet_t pIfNet)
    182192{
    183     unsigned offTry1, offTry2, offTry3, offTry4;
     193    if (g_fNetPCountFound)
     194        return;
     195
    184196    /*
    185197     * It would be nice to use locking at this point, but it is not available via KPI.
     
    187199     * to rule out false detections.
    188200     */
    189     for (int nAttempts = 0; nAttempts < 3; nAttempts++)
     201    unsigned offTry1, offTry2, offTry3, offTry4;
     202    for (int iAttempt = 0; iAttempt < 3; iAttempt++)
    190203    {
    191204        offTry1 = vboxNetFltDarwinSetAndDiff(pIfNet, 1);
     
    193206        offTry3 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
    194207        offTry4 = vboxNetFltDarwinSetAndDiff(pIfNet, 0);
     208
    195209        /* If any attempt has failed we won't continue as our algorithm is flawed. */
    196210        if (!offTry1 || !offTry2 || !offTry3 || !offTry4)
     
    202216                Log(("VBoxNetFltDarwinDetectPCountOffset: Adjusted if_pcount offset to %x from %x.\n", offTry1, g_offIfNetPCount));
    203217                g_offIfNetPCount = offTry1;
     218                g_fNetPCountFound = true;
    204219            }
    205220            break;
     
    210225        LogRel(("VBoxNetFlt: Failed to detect promiscuous count, all traffic may reach wire (%x != %x).\n", g_offIfNetPCount, offTry1));
    211226}
     227
    212228
    213229/**
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