VirtualBox

Changeset 48180 in vbox for trunk/src


Ignore:
Timestamp:
Aug 30, 2013 7:37:03 AM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
88563
Message:

Devices/Network/tstIntNet-1: Use the default route interface for testing on Linux + minor updates in route table parsing in NetIf-linux.cpp

Location:
trunk/src/VBox
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Network/testcase/tstIntNet-1.cpp

    r46904 r48180  
    596596}
    597597
     598#ifdef RT_OS_LINUX
     599#include <stdio.h>
     600#include <net/if.h>
     601#include <net/route.h>
     602/**
     603 * Obtain the name of the interface used for default routing.
     604 *
     605 * NOTE: Copied from Main/src-server/linux/NetIf-linux.cpp
     606 *
     607 * @returns VBox status code.
     608 *
     609 * @param   pszName     The buffer of IFNAMSIZ+1 length where to put the name.
     610 */
     611static int getDefaultIfaceName(char *pszName)
     612{
     613    FILE *fp = fopen("/proc/net/route", "r");
     614    char szBuf[1024];
     615    char szIfName[17];
     616    uint32_t uAddr;
     617    uint32_t uGateway;
     618    uint32_t uMask;
     619    int  iTmp;
     620    unsigned uFlags;
     621
     622    if (fp)
     623    {
     624        while (fgets(szBuf, sizeof(szBuf)-1, fp))
     625        {
     626            int n = sscanf(szBuf, "%16s %x %x %x %d %d %d %x %d %d %d\n",
     627                           szIfName, &uAddr, &uGateway, &uFlags, &iTmp, &iTmp, &iTmp,
     628                           &uMask, &iTmp, &iTmp, &iTmp);
     629            if (n < 10 || !(uFlags & RTF_UP))
     630                continue;
     631
     632            if (uAddr == 0 && uMask == 0)
     633            {
     634                fclose(fp);
     635                strncpy(pszName, szIfName, 16);
     636                pszName[16] = 0;
     637                return VINF_SUCCESS;
     638            }
     639        }
     640        fclose(fp);
     641    }
     642    return VERR_INTERNAL_ERROR;
     643}
     644#endif /* RT_OS_LINUX */
     645
    598646
    599647int main(int argc, char **argv)
     
    625673    const char *pszIf = "en0";
    626674#elif defined(RT_OS_LINUX)
    627     const char *pszIf = "eth0";
     675    char        szIf[IFNAMSIZ+1] = "eth0"; /* Reasonable default */
     676    /*
     677     * Try to update the default interface by consulting the routing table.
     678     * If we fail we still have our reasonable default.
     679     */
     680    getDefaultIfaceName(szIf);
     681    const char *pszIf = szIf;
    628682#elif defined(RT_OS_SOLARIS)
    629683    const char* pszIf = "rge0";
  • trunk/src/VBox/Main/src-server/linux/NetIf-linux.cpp

    r48016 r48180  
    3838#include "Logging.h"
    3939
     40/**
     41 * Obtain the name of the interface used for default routing.
     42 *
     43 * NOTE: There is a copy in Devices/Network/testcase/tstIntNet-1.cpp.
     44 *
     45 * @returns VBox status code.
     46 *
     47 * @param   pszName     The buffer of IFNAMSIZ+1 length where to put the name.
     48 */
    4049static int getDefaultIfaceName(char *pszName)
    4150{
     
    4352    char szBuf[1024];
    4453    char szIfName[17];
    45     char szAddr[129];
    46     char szGateway[129];
    47     char szMask[129];
     54    uint32_t uAddr;
     55    uint32_t uGateway;
     56    uint32_t uMask;
    4857    int  iTmp;
    4958    unsigned uFlags;
     
    5362        while (fgets(szBuf, sizeof(szBuf)-1, fp))
    5463        {
    55             int n = sscanf(szBuf, "%16s %128s %128s %X %d %d %d %128s %d %d %d\n",
    56                            szIfName, szAddr, szGateway, &uFlags, &iTmp, &iTmp, &iTmp,
    57                            szMask, &iTmp, &iTmp, &iTmp);
     64            int n = sscanf(szBuf, "%16s %x %x %x %d %d %d %x %d %d %d\n",
     65                           szIfName, &uAddr, &uGateway, &uFlags, &iTmp, &iTmp, &iTmp,
     66                           &uMask, &iTmp, &iTmp, &iTmp);
    5867            if (n < 10 || !(uFlags & RTF_UP))
    5968                continue;
    6069
    61             if (strcmp(szAddr, "00000000") == 0 && strcmp(szMask, "00000000") == 0)
     70            if (uAddr == 0 && uMask == 0)
    6271            {
    6372                fclose(fp);
Note: See TracChangeset for help on using the changeset viewer.

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