VirtualBox

Changeset 23045 in vbox for trunk/src


Ignore:
Timestamp:
Sep 15, 2009 8:56:37 PM (15 years ago)
Author:
vboxsync
Message:

VBoxNetAdpCtl: refined removing of addresses

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/apps/adpctl/VBoxNetAdpCtl.cpp

    r23036 r23045  
    109109#define MAX_ADDRLEN   64
    110110
    111 static bool removeAddresses(const char *pszAdapterName)
    112 {
    113     char szCmd[1024], szBuf[1024];
     111static bool removeAddresses(char *pszAdapterName)
     112{
     113    char szArgv[1024], szBuf[1024];
    114114    char aszAddresses[MAX_ADDRESSES][MAX_ADDRLEN];
     115    int rc;
     116    int fds[2];
     117    char * const argv[] = { pszAdapterName, NULL };
     118    char * const envp[] = { (char*)"LC_ALL=C", NULL };
    115119
    116120    memset(aszAddresses, 0, sizeof(aszAddresses));
    117     snprintf(szCmd, sizeof(szCmd), VBOXADPCTL_IFCONFIG_PATH " %s", pszAdapterName);
    118     FILE *fp = popen(szCmd, "r");
    119 
    120     if (!fp)
     121
     122    rc = pipe(fds);
     123    if (rc < 0)
    121124        return false;
    122125
    123     int cAddrs;
    124     for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
    125     {
    126         int cbSkipWS = strspn(szBuf, " \t");
     126    pid_t pid = fork();
     127    if (pid < 0)
     128        return false;
     129
     130    if (pid == 0)
     131    {
     132        close(fds[0]);
     133        close(STDOUT_FILENO);
     134        rc = dup2(fds[1], STDOUT_FILENO);
     135        if (rc >= 0)
     136            execve(VBOXADPCTL_IFCONFIG_PATH, argv, envp);
     137    }
     138    else
     139    {
     140        close(fds[1]);
     141        FILE *fp = fdopen(fds[0], "r");
     142        if (!fp)
     143            return false;
     144
     145        int cAddrs;
     146        for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
     147        {
     148            int cbSkipWS = strspn(szBuf, " \t");
    127149#if 0 /* Don't use this! assert() breaks the mac build. Use IPRT or be a rectangular building thing. */
    128         assert(cbSkipWS < 20);
    129 #endif
    130         char *pszWord = strtok(szBuf + cbSkipWS, " ");
    131         /* We are concerned with IPv6 address lines only. */
    132         if (!pszWord || strcmp(pszWord, "inet6"))
    133             continue;
     150            assert(cbSkipWS < 20);
     151#endif
     152            char *pszWord = strtok(szBuf + cbSkipWS, " ");
     153            /* We are concerned with IPv6 address lines only. */
     154            if (!pszWord || strcmp(pszWord, "inet6"))
     155                continue;
    134156#ifdef RT_OS_LINUX
    135         pszWord = strtok(NULL, " ");
    136         /* Skip "addr:". */
    137         if (!pszWord || strcmp(pszWord, "addr:"))
    138             continue;
    139 #endif
    140         pszWord = strtok(NULL, " ");
    141         /* Skip link-local addresses. */
    142         if (!pszWord || !strncmp(pszWord, "fe80", 4))
    143             continue;
    144         strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
    145     }
    146     pclose(fp);
    147 
    148     for (int i = 0; i < cAddrs; i++)
    149     {
    150         if (executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
    151             return false;
     157            pszWord = strtok(NULL, " ");
     158            /* Skip "addr:". */
     159            if (!pszWord || strcmp(pszWord, "addr:"))
     160                continue;
     161#endif
     162            pszWord = strtok(NULL, " ");
     163            /* Skip link-local addresses. */
     164            if (!pszWord || !strncmp(pszWord, "fe80", 4))
     165                continue;
     166            strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
     167        }
     168        fclose(fp);
     169
     170        for (int i = 0; i < cAddrs; i++)
     171        {
     172            if (executeIfconfig(pszAdapterName, "inet6",
     173                                VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
     174                return false;
     175        }
    152176    }
    153177
     
    179203
    180204{
    181     const char *pszAdapterName;
     205    char *pszAdapterName;
    182206    const char *pszAddress;
    183207    const char *pszNetworkMask = NULL;
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