ipconfig.c

来自「一个类似windows」· C语言 代码 · 共 518 行 · 第 1/2 页

C
518
字号
        }
    }
    else
    {
        ;
        /* we need to be able to release connections by name with support for globbing
         * i.e. ipconfig /release Eth* will release all cards starting with Eth...
         *      ipconfig /release *con* will release all cards with 'con' in their name
         */
    }


    /* Call IpReleaseAddress to release the IP address on the specified adapter. */
    if ((dwRetVal = IpReleaseAddress(&AdapterInfo)) != NO_ERROR)
    {
        _tprintf(_T("\nAn error occured while releasing interface %s : "), _T("*name*"));
        DoFormatMessage(dwRetVal);
    }
    return 0;
}




INT Renew(TCHAR Index)
{
    IP_ADAPTER_INDEX_MAP AdapterInfo;
    DWORD dwRetVal = 0;

    /* if interface is not given, query GetInterfaceInfo */
    if (Index == (TCHAR)NULL)
    {
        PIP_INTERFACE_INFO pInfo;
        ULONG ulOutBufLen;
        pInfo = (IP_INTERFACE_INFO *) malloc(sizeof(IP_INTERFACE_INFO));
        ulOutBufLen = 0;

        /* Make an initial call to GetInterfaceInfo to get
         * the necessary size into the ulOutBufLen variable */
        if ( GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
        {
            GlobalFree(pInfo);
            pInfo = (IP_INTERFACE_INFO *) malloc (ulOutBufLen);
        }

        /* Make a second call to GetInterfaceInfo to get the actual data we want */
        if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR )
        {
            AdapterInfo = pInfo->Adapter[0];
            _tprintf(_T("name - %S\n"), pInfo->Adapter[0].Name);
        } else {
            _tprintf(_T("\nGetInterfaceInfo failed : "));
            DoFormatMessage(dwRetVal);
        }
    }
    else
    {
        ;
        /* we need to be able to renew connections by name with support for globbing
         * i.e. ipconfig /renew Eth* will renew all cards starting with Eth...
         *      ipconfig /renew *con* will renew all cards with 'con' in their name
         */
    }


    /* Call IpRenewAddress to renew the IP address on the specified adapter. */
    if ((dwRetVal = IpRenewAddress(&AdapterInfo)) != NO_ERROR)
    {
        _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
        DoFormatMessage(dwRetVal);
    }
    return 0;
}

/* temp func for testing purposes */
VOID Info()
{
     // Declare and initialize variables
    PIP_INTERFACE_INFO pInfo;
    ULONG ulOutBufLen;
    DWORD dwRetVal;

    pInfo = (IP_INTERFACE_INFO *) malloc( sizeof(IP_INTERFACE_INFO) );
    ulOutBufLen = sizeof(IP_INTERFACE_INFO);
    dwRetVal = 0;


    // Make an initial call to GetInterfaceInfo to get
    // the necessary size in the ulOutBufLen variable
    if ( GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
    {
        free(pInfo);
        pInfo = (IP_INTERFACE_INFO *) malloc (ulOutBufLen);
    }

    // Make a second call to GetInterfaceInfo to get
    // the actual data we need
    if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR )
    {
        int i;
        for (i=0; i<pInfo->NumAdapters; i++)
        {
            printf("\tAdapter Name: %S\n", pInfo->Adapter[i].Name);
            printf("\tAdapter Index: %ld\n", pInfo->Adapter[i].Index);
            printf("\tNum Adapters: %ld\n", pInfo->NumAdapters);
        }
    }
    else
    {
        printf("GetInterfaceInfo failed.\n");
        DoFormatMessage(dwRetVal);
    }
}


VOID Usage(VOID)
{
    _tprintf(_T("\nUSAGE:\n"
    "    ipconfig [/? | /all | /renew [adapter] | /release [adapter] |\n"
    "              /flushdns | /displaydns | /registerdns |\n"
    "              /showclassid adapter |\n"
    "              /setclassid adapter [classid] ]\n"
    "\n"
    "where\n"
    "    adapter         Connection name\n"
    "                   (wildcard characters * and ? allowed, see examples)\n"
    "\n"
    "    Options:\n"
    "       /?           Display this help message\n"
    "       /all         Display full configuration information.\n"
    "       /release     Release the IP address for the specified adapter.\n"
    "       /renew       Renew the IP address for the specified adapter.\n"
    "       /flushdns    Purges the DNS Resolver cache.\n"
    "       /registerdns Refreshes all DHCP leases and re-registers DNS names.\n"
    "       /displaydns  Display the contents of the DNS Resolver Cache.\n"
    "       /showclassid Displays all the dhcp class IDs allowed for adapter.\n"
    "       /setclassid  Modifies the dhcp class id.\n"
    "\n"
    "The default is to display only the IP address, subnet mask and\n"
    "default gateway for each adapter bound to TCP/IP.\n"
    "\n"
    "For Release and Renew, if no adapter name is specified, then the IP address\n"
    "leases for all adapters bound to TCP/IP will be released or renewed.\n"
    "\n"
    "For Setclassid, if no ClassId is specified, then the ClassId is removed.\n"
    "\n"
    "Examples:\n"
    "    > ipconfig                   ... Show information.\n"
    "    > ipconfig /all              ... Show detailed information\n"
    "    > ipconfig /renew            ... renew all adapters\n"
    "    > ipconfig /renew EL*        ... renew any connection that has its\n"
    "                                     name starting with EL\n"
    "    > ipconfig /release *Con*    ... release all matching connections,\n"
    "                                     eg. \"Local Area Connection 1\" or\n"
    "                                         \"Local Area Connection 2\"\n"));
}

int main(int argc, char *argv[])
{
    BOOL DoUsage=FALSE;
    BOOL DoAll=FALSE;
    BOOL DoRelease=FALSE;
    BOOL DoRenew=FALSE;
    BOOL DoFlushdns=FALSE;
    BOOL DoRegisterdns=FALSE;
    BOOL DoDisplaydns=FALSE;
    BOOL DoShowclassid=FALSE;
    BOOL DoSetclassid=FALSE;

    /* Parse command line for options we have been given. */
    if ( (argc > 1)&&(argv[1][0]=='/') )
    {
        if( !_tcsicmp( &argv[1][1], _T("?") ))
        {
            DoUsage = TRUE;
        }
        else if( !_tcsnicmp( &argv[1][1], _T("ALL"), _tcslen(&argv[1][1]) ))
        {
           DoAll = TRUE;
        }
        else if( !_tcsnicmp( &argv[1][1], _T("RELEASE"), _tcslen(&argv[1][1]) ))
        {
            DoRelease = TRUE;
        }
        else if( ! _tcsnicmp( &argv[1][1], _T("RENEW"), _tcslen(&argv[1][1]) ))
        {
            DoRenew = TRUE;
        }
        else if( ! _tcsnicmp( &argv[1][1], _T("FLUSHDNS"), _tcslen(&argv[1][1]) ))
        {
            DoFlushdns = TRUE;
        }
        else if( ! _tcsnicmp( &argv[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv[1][1]) ))
        {
            DoRegisterdns = TRUE;
        }
        else if( ! _tcsnicmp( &argv[1][1], _T("DISPLAYDNS"), _tcslen(&argv[1][1]) ))
        {
            DoDisplaydns = TRUE;
        }
        else if( ! _tcsnicmp( &argv[1][1], _T("SHOWCLASSID"), _tcslen(&argv[1][1]) ))
        {
            DoShowclassid = TRUE;
        }
        else if( ! _tcsnicmp( &argv[1][1], _T("SETCLASSID"), _tcslen(&argv[1][1]) ))
        {
            DoSetclassid = TRUE;
        }
    }

    switch (argc)
    {
        case 1:  /* Default behaviour if no options are given*/
            ShowInfo(FALSE);
            break;
        case 2:  /* Process all the options that take no paramiters */
            if (DoUsage)
                Usage();
            else if (DoAll)
                ShowInfo(TRUE);
            else if (DoRelease)
                Release((TCHAR)NULL);
            else if (DoRenew)
                Renew((TCHAR)NULL);
            else if (DoFlushdns)
                _tprintf(_T("\nSorry /flushdns is not implemented yet\n"));
            else if (DoRegisterdns)
                _tprintf(_T("\nSorry /registerdns is not implemented yet\n"));
            else if (DoDisplaydns)
                _tprintf(_T("\nSorry /displaydns is not implemented yet\n"));
            else
                Usage();
            break;
        case 3: /* Process all the options that can have 1 paramiters */
            if (DoRelease)
                _tprintf(_T("\nSorry /release [adapter] is not implemented yet\n"));
                //Release(argv[2]);
            else if (DoRenew)
                _tprintf(_T("\nSorry /renew [adapter] is not implemented yet\n"));
            else if (DoShowclassid)
                _tprintf(_T("\nSorry /showclassid adapter is not implemented yet\n"));
            else if (DoSetclassid)
                _tprintf(_T("\nSorry /setclassid adapter is not implemented yet\n"));
            else
                Usage();
            break;
        case 4:  /* Process all the options that can have 2 paramiters */
            if (DoSetclassid)
                _tprintf(_T("\nSorry /setclassid adapter [classid]is not implemented yet\n"));
            else
                Usage();
            break;
        default:
            Usage();
    }

    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?