示例#1
0
DWORD WINAPI CLuzj_ZTEDlg::IpconfigRenew()
{

	int i;
	DWORD ret;
	char  adaptername[MAX_ADAPTER_NAME];
	int count = 0;

	DWORD dwOutBufLen = sizeof(IP_INTERFACE_INFO);
	PIP_INTERFACE_INFO pIfTable = (PIP_INTERFACE_INFO)malloc(dwOutBufLen);
	if(pIfTable == NULL) return -1;
	
	ret = GetInterfaceInfo(pIfTable, &dwOutBufLen);
	if(ret == ERROR_INSUFFICIENT_BUFFER) {
		free(pIfTable); pIfTable = (PIP_INTERFACE_INFO)malloc(dwOutBufLen);
		if(pIfTable == NULL) return -2;
		ret = GetInterfaceInfo(pIfTable, &dwOutBufLen);
	}

	if(ret != NO_ERROR) return -3;

	for(i = 0; i < pIfTable->NumAdapters; i++) {
		wcstombs(adaptername, pIfTable->Adapter[i].Name, MAX_ADAPTER_NAME);

		if(stricmp(adaptername, ToTCPName(Config.m_csNetCard)) == 0) {

			EnableDHCP(GetGUID(adaptername), true);
			
			while(1) {
				if(count <= MAX_DHCP_TIMES) Log(I_INFO, "fetching IP address by DHCP...");	
				IpReleaseAddress(&pIfTable->Adapter[i]);
				ret = IpRenewAddress(&pIfTable->Adapter[i]);
				if(ret == NO_ERROR) break;	
				if(count <= MAX_DHCP_TIMES) {
					if(count == MAX_DHCP_TIMES) {
						count++;
						Log(I_INFO, "dhcp keep quiet.");
					} else {
						Log(I_WARN, "fetch IP address failed. (%d)", ret); count++;
					}
				} 
				Sleep(10000);
			}
			
			char *info = GetAdapterInfo((LPCSTR)Config.m_csNetCard);
			if(info == NULL) Log(I_INFO, "GetAdapterInfo: NULL");
			else Log(I_INFO, info);		

			break;
		}	
	}

	free(pIfTable);
	return 0;
}
示例#2
0
HRESULT RefreshIPCfgDialog(HWND hwndDlg, BOOL fRenew)
{
    PIP_ADAPTER_INFO    pAdapterInfo = NULL, pAdapterInfoHead = NULL;
    PIP_INTERFACE_INFO  pBuffer = NULL;
    HRESULT             hr = S_OK;
    ULONG               ulBufferSize = 0;
    DWORD               dwRetVal;
    HWND                hAddrTypeLbl;
    HWND                hIPAddrLbl;
    HWND                hGatewayLbl;
    HWND                hSubnetMskLbl;
    WCHAR               szIpAddr[MAX_IP_FIELD_WIDTH];
    WCHAR               szGateway[MAX_IP_FIELD_WIDTH];
    WCHAR               szSubnetMsk[MAX_IP_FIELD_WIDTH];
    WCHAR               szOldIpAddr[MAX_IP_FIELD_WIDTH];
    WCHAR               szOldGateway[MAX_IP_FIELD_WIDTH];
    WCHAR               szOldSubnetMsk[MAX_IP_FIELD_WIDTH];
    WCHAR               szOldDhcpEnabled[STRING_BUFFER_MAX];
    INT                 i;
    TCHAR               szStatic[STRING_BUFFER_MAX];
    TCHAR               szDhcp[STRING_BUFFER_MAX];
    WCHAR               szAdapterName[MAX_ADAPTERNAME_STR];

    // Get UI components
    hAddrTypeLbl        = GetDlgItem(hwndDlg, IDC_ADDRTYPE_LBL_INF);
    hIPAddrLbl          = GetDlgItem(hwndDlg, IDC_IPADDR_LBL_INF);
    hGatewayLbl         = GetDlgItem(hwndDlg, IDC_GATEWAY_LBL_INF);
    hSubnetMskLbl       = GetDlgItem(hwndDlg, IDC_SUBNETMSK_LBL_INF);

    // Get IP adapter info
    hr = GetIPAdapterInfo(hwndDlg, &pAdapterInfoHead, &pAdapterInfo);
    if (FAILED(hr)) {
        DEBUGMSG(ZONE_ERROR, (TEXT("NETUIQC: Could not get the adapter info")));
        goto exit;
    }

    // If renew IP Info is specified
    if (fRenew) {
        ulBufferSize = 0;

        // Initialize interface
        dwRetVal = GetInterfaceInfo(pBuffer, &ulBufferSize);
        if (dwRetVal == ERROR_INSUFFICIENT_BUFFER) {
            pBuffer = (PIP_INTERFACE_INFO)malloc(ulBufferSize);
            if (!pBuffer) {
                DEBUGMSG(ZONE_ERROR, (TEXT("Insufficient Memory for IPInterface allocation.")));
                hr = E_FAIL;
                goto exit;
            }
            dwRetVal = GetInterfaceInfo(pBuffer, &ulBufferSize);
        }

        if (dwRetVal != NO_ERROR) {
            DEBUGMSG(ZONE_ERROR, (TEXT("NetUIQC: Could not get interface for renewing IP info")));
            hr = E_FAIL;
            goto exit;
        }

        // If no adapter interface is present
        if (ulBufferSize == 0) {
            hr = E_FAIL;
            goto exit;
        }

        SetCursor(LoadCursor(NULL, IDC_WAIT));

        if (GetAdapterNameW(hwndDlg,szAdapterName,sizeof(szAdapterName)) != S_OK) {
            hr = E_FAIL;
            goto exit;
        }

        // Loop through adpaters to find the current adapter
        for (i=0;i<pBuffer->NumAdapters;i++) {
            if (!wcscmp(pBuffer->Adapter[i].Name, szAdapterName)) {
                // Release the IP address of the specified adpater
                dwRetVal = IpReleaseAddress(&(pBuffer->Adapter[i]));

                // Reset labels based on a successful Release
                if (dwRetVal == NO_ERROR) {
                    SetWindowText(hIPAddrLbl, L"0.0.0.0");
                    SetWindowText(hSubnetMskLbl, L"0.0.0.0");
                    SetWindowText(hGatewayLbl, L"");
                    Sleep(500); // Sleep for dramatic effect
                }

                // Renew the IP address of the specified adapter
                IpRenewAddress(&(pBuffer->Adapter[i]));
            }
        }

        SetCursor(LoadCursor(NULL, IDC_ARROW));
    } else {
        // Get IP data from iphelper
        mbstowcs(szIpAddr, pAdapterInfo->IpAddressList.IpAddress.String, strlen(pAdapterInfo->IpAddressList.IpAddress.String)+1);
        mbstowcs(szGateway, pAdapterInfo->GatewayList.IpAddress.String, strlen(pAdapterInfo->GatewayList.IpAddress.String)+1);
        mbstowcs(szSubnetMsk, pAdapterInfo->IpAddressList.IpMask.String, strlen(pAdapterInfo->IpAddressList.IpMask.String)+1);

        LoadString(v_hInst, IDS_QC_DHCP, szDhcp, sizeof(szDhcp)/sizeof(szDhcp[0]));
        LoadString(v_hInst, IDS_QC_STATIC_IP, szStatic, sizeof(szStatic)/sizeof(szStatic[0]));

        // Set the data in the UI components if it has changed

        GetWindowText(hAddrTypeLbl, szOldDhcpEnabled, STRING_BUFFER_MAX);
        if (wcscmp(((pAdapterInfo->DhcpEnabled) ? szDhcp : szStatic), szOldDhcpEnabled) != 0) {
            SetWindowText(hAddrTypeLbl, (pAdapterInfo->DhcpEnabled) ? szDhcp : szStatic);
        }

        GetWindowText(hIPAddrLbl, szOldIpAddr, MAX_IP_FIELD_WIDTH);
        if (wcscmp(szOldIpAddr, szIpAddr) != 0) {
            SetWindowText(hIPAddrLbl, szIpAddr);
        }

        GetWindowText(hGatewayLbl, szOldGateway, MAX_IP_FIELD_WIDTH);
        if (wcscmp(szOldGateway, szGateway) != 0) {
            SetWindowText(hGatewayLbl, szGateway);
        }

        GetWindowText(hSubnetMskLbl, szOldSubnetMsk, MAX_IP_FIELD_WIDTH);
        if (wcscmp(szOldSubnetMsk, szSubnetMsk) != 0) {
            SetWindowText(hSubnetMskLbl, szSubnetMsk);
        }
    }

    exit:

    if (pBuffer) {
        free(pBuffer);
    }
    if (pAdapterInfoHead) {
        free(pAdapterInfoHead);
    }

    return hr;

} // RefreshIPCfgDialog