⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpip_properties.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * PROJECT:     ReactOS Network Control Panel
 * LICENSE:     GPL - See COPYING in the top level directory
 * FILE:        lib/cpl/system/tcpip_properties.c
 * PURPOSE:     ReactOS Network Control Panel
 * COPYRIGHT:   Copyright 2004 Gero Kuehn (reactos.filter@gkware.com)
 *              Copyright 2006 Ge van Geldorp <gvg@reactos.org>
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <tchar.h>
#include <windows.h>
#include <iptypes.h>
#include <iphlpapi.h>
#include <commctrl.h>
#include <dhcpcapi.h>
#include <prsht.h>


#ifdef _MSC_VER
#include <cpl.h>
#else

// this is missing on reactos...
#ifndef IPM_SETADDRESS
#define IPM_SETADDRESS (WM_USER+101)
#endif

#endif

#include "resource.h"
#include "ncpa.h"

#define NDEBUG
#include <debug.h>

typedef struct _TCPIP_PROPERTIES_DATA {
    DWORD AdapterIndex;
    char *AdapterName;
    BOOL DhcpEnabled;
    DWORD IpAddress;
    DWORD SubnetMask;
    DWORD Gateway;
    DWORD Dns1;
    DWORD Dns2;
    BOOL OldDhcpEnabled;
    DWORD OldIpAddress;
    DWORD OldSubnetMask;
    DWORD OldGateway;
    DWORD OldDns1;
    DWORD OldDns2;
} TCPIP_PROPERTIES_DATA, *PTCPIP_PROPERTIES_DATA;

void InitPropSheetPage(PROPSHEETPAGE *psp, WORD idDlg, DLGPROC DlgProc, LPARAM lParam);

DWORD APIENTRY DhcpNotifyConfigChange(LPWSTR ServerName, LPWSTR AdapterName,
                                      BOOL NewIpAddress, DWORD IpIndex,
                                      DWORD IpAddress, DWORD SubnetMask,
                                      int DhcpAction);


static void
ManualDNS(HWND Dlg, BOOL Enabled, UINT uCmd) {
    PTCPIP_PROPERTIES_DATA DlgData = 
        (PTCPIP_PROPERTIES_DATA) GetWindowLongPtrW(Dlg, GWL_USERDATA);

    if (! DlgData->OldDhcpEnabled && 
        (uCmd == IDC_USEDHCP || uCmd == IDC_NODHCP)) {
        if (INADDR_NONE != DlgData->OldIpAddress) {
            SendDlgItemMessage(Dlg, IDC_IPADDR, IPM_SETADDRESS, 0,
                               ntohl(DlgData->OldIpAddress));
        }
        if (INADDR_NONE != DlgData->OldSubnetMask) {
            SendDlgItemMessage(Dlg, IDC_SUBNETMASK, IPM_SETADDRESS, 0,
                               ntohl(DlgData->OldSubnetMask));
        }
        if (INADDR_NONE != DlgData->OldGateway) {
            SendDlgItemMessage(Dlg, IDC_DEFGATEWAY, IPM_SETADDRESS, 0,
                               ntohl(DlgData->OldGateway));
        }
    }

    if (INADDR_NONE != DlgData->OldDns1 && 
        (uCmd == IDC_FIXEDDNS || uCmd == IDC_AUTODNS || IDC_NODHCP)) {
        SendDlgItemMessage(Dlg, IDC_DNS1, IPM_SETADDRESS, 0,
                           ntohl(DlgData->OldDns1));
        if (INADDR_NONE != DlgData->OldDns2) {
            SendDlgItemMessage(Dlg, IDC_DNS2, IPM_SETADDRESS, 0,
                               ntohl(DlgData->OldDns2));
        }
    }

    CheckDlgButton(Dlg, IDC_FIXEDDNS,
                   Enabled ? BST_CHECKED : BST_UNCHECKED);
    CheckDlgButton(Dlg, IDC_AUTODNS,
                   Enabled ? BST_UNCHECKED : BST_CHECKED);
    EnableWindow(GetDlgItem(Dlg, IDC_DNS1), Enabled);
    EnableWindow(GetDlgItem(Dlg, IDC_DNS2), FALSE/*Enabled*/);
    if (! Enabled) {
        SendDlgItemMessage(Dlg, IDC_DNS1, IPM_CLEARADDRESS, 0, 0);
        SendDlgItemMessage(Dlg, IDC_DNS2, IPM_CLEARADDRESS, 0, 0);
	
    }
}

static void
EnableDHCP(HWND Dlg, BOOL Enabled, UINT uCmd) {
    CheckDlgButton(Dlg, IDC_USEDHCP, Enabled ? BST_CHECKED : BST_UNCHECKED);
    CheckDlgButton(Dlg, IDC_NODHCP, Enabled ? BST_UNCHECKED : BST_CHECKED);
    EnableWindow(GetDlgItem(Dlg, IDC_IPADDR), ! Enabled);
    EnableWindow(GetDlgItem(Dlg, IDC_SUBNETMASK), ! Enabled);
    EnableWindow(GetDlgItem(Dlg, IDC_DEFGATEWAY), ! Enabled);
    EnableWindow(GetDlgItem(Dlg, IDC_AUTODNS), Enabled);
    if (Enabled) {
        SendDlgItemMessage(Dlg, IDC_IPADDR, IPM_CLEARADDRESS, 0, 0);
        SendDlgItemMessage(Dlg, IDC_SUBNETMASK, IPM_CLEARADDRESS, 0, 0);
        SendDlgItemMessage(Dlg, IDC_DEFGATEWAY, IPM_CLEARADDRESS, 0, 0);
    } else {
        ManualDNS(Dlg, TRUE, uCmd);
    }
}

static void
ShowError(HWND Parent, UINT MsgId)
{
    WCHAR Error[32], Msg[64];

    if (0 == LoadStringW((HINSTANCE) GetWindowLongPtrW(Parent, GWLP_HINSTANCE),
                         IDS_ERROR, Error, sizeof(Error) / sizeof(Error[0]))) {
        wcscpy(Error, L"Error");
    }
    if (0 == LoadStringW((HINSTANCE) GetWindowLongPtrW(Parent, GWLP_HINSTANCE),
                         MsgId, Msg, sizeof(Msg) / sizeof(Msg[0]))) {
        wcscpy(Msg, L"Unknown error");
    }
    MessageBoxW(Parent, Msg, Error, MB_OK | MB_ICONSTOP);
}

static
BOOL GetAddressFromField( HWND hwndDlg, UINT CtlId, 
                          DWORD *dwIPAddr,
                          const char **AddressString ) {
    LRESULT lResult;
    struct in_addr inIPAddr;

    *AddressString = NULL;

    lResult = SendMessage(GetDlgItem(hwndDlg, CtlId), IPM_GETADDRESS, 0, 
                          (ULONG_PTR)dwIPAddr);
    if( lResult != 4 ) return FALSE;
    
    *dwIPAddr = htonl(*dwIPAddr);
    inIPAddr.s_addr = *dwIPAddr;
    *AddressString = inet_ntoa(inIPAddr);
    if( !*AddressString ) return FALSE;

    return TRUE;
}

static BOOL
ValidateAndStore(HWND Dlg, PTCPIP_PROPERTIES_DATA DlgData)
{
    DWORD IpAddress;
    MIB_IPFORWARDROW RowToAdd = { 0 }, RowToRem = { 0 };

    DlgData->DhcpEnabled = (BST_CHECKED ==
                            IsDlgButtonChecked(Dlg, IDC_USEDHCP));
    if (! DlgData->DhcpEnabled) {
	DhcpReleaseIpAddressLease( DlgData->AdapterIndex );

        if (4 != SendMessageW(GetDlgItem(Dlg, IDC_IPADDR), IPM_GETADDRESS,
                              0, (LPARAM) &IpAddress)) {
            ShowError(Dlg, IDS_ENTER_VALID_IPADDRESS);
            SetFocus(GetDlgItem(Dlg, IDC_IPADDR));
            return FALSE;
        }
        DlgData->IpAddress = htonl(IpAddress);
        if (4 != SendMessageW(GetDlgItem(Dlg, IDC_SUBNETMASK), IPM_GETADDRESS,
                              0, (LPARAM) &IpAddress)) {
            ShowError(Dlg, IDS_ENTER_VALID_SUBNET);
            SetFocus(GetDlgItem(Dlg, IDC_SUBNETMASK));
            return FALSE;
        }
        DlgData->SubnetMask = htonl(IpAddress);
        if (4 != SendMessageW(GetDlgItem(Dlg, IDC_DEFGATEWAY), IPM_GETADDRESS,
                              0, (LPARAM) &IpAddress)) {
            DlgData->Gateway = INADDR_NONE;
        } else {
            DlgData->Gateway = htonl(IpAddress);
        }
	DhcpStaticRefreshParams
	    ( DlgData->AdapterIndex, DlgData->IpAddress, DlgData->SubnetMask );

	RowToRem.dwForwardMask = 0;
	RowToRem.dwForwardMetric1 = 1;
	RowToRem.dwForwardNextHop = DlgData->OldGateway;

	DeleteIpForwardEntry( &RowToRem );

	RowToAdd.dwForwardMask = 0;
	RowToAdd.dwForwardMetric1 = 1;
	RowToAdd.dwForwardNextHop = DlgData->Gateway;

	CreateIpForwardEntry( &RowToAdd );
        ASSERT(BST_CHECKED == IsDlgButtonChecked(Dlg, IDC_FIXEDDNS));
    } else {
        DlgData->IpAddress = INADDR_NONE;
        DlgData->SubnetMask = INADDR_NONE;
        DlgData->Gateway = INADDR_NONE;
	DhcpLeaseIpAddress( DlgData->AdapterIndex );
    }

    if (BST_CHECKED == IsDlgButtonChecked(Dlg, IDC_FIXEDDNS)) {
        if (4 != SendMessageW(GetDlgItem(Dlg, IDC_DNS1), IPM_GETADDRESS,
                              0, (LPARAM) &IpAddress)) {
            DlgData->Dns1 = INADDR_NONE;
        } else {
            DlgData->Dns1 = htonl(IpAddress);
        }
        if (4 != SendMessageW(GetDlgItem(Dlg, IDC_DNS2), IPM_GETADDRESS,
                              0, (LPARAM) &IpAddress)) {
            DlgData->Dns2 = INADDR_NONE;
        } else {
            DlgData->Dns2 = htonl(IpAddress);
        }
    } else {
        DlgData->Dns1 = INADDR_NONE;
        DlgData->Dns2 = INADDR_NONE;
    }

    return TRUE;
}

static BOOL
InternTCPIPSettings(HWND Dlg, PTCPIP_PROPERTIES_DATA DlgData) {
    /*BOOL Changed;
    BOOL IpChanged;
    int DhcpAction;
    LPWSTR AdapterName;*/
    BOOL SetIpAddressByDhcp;
    BOOL SetDnsByDhcp;
    TCHAR pszRegKey[MAX_PATH];
    const char *AddressString;
    DWORD Address = 0;
    LONG rc;
    HKEY hKey = NULL;
    BOOL ret = FALSE;

    if (! ValidateAndStore(Dlg, DlgData)) {
        /* Should never happen, we should have validated at PSN_KILLACTIVE */
        ASSERT(FALSE);
        return FALSE;
    }

    SetIpAddressByDhcp = IsDlgButtonChecked(Dlg, IDC_USEDHCP);
    SetDnsByDhcp = IsDlgButtonChecked(Dlg, IDC_AUTODNS);

    /* Save parameters in HKLM\SYSTEM\CurrentControlSet\Services\{GUID}\Parameters\Tcpip */
    _stprintf(pszRegKey,_T("SYSTEM\\CurrentControlSet\\Services\\%S\\Parameters\\Tcpip"), DlgData->AdapterName);
    rc = RegOpenKey(HKEY_LOCAL_MACHINE, pszRegKey, &hKey);
    if (rc != ERROR_SUCCESS)
        goto cleanup;
    if (SetIpAddressByDhcp)
        AddressString = "0.0.0.0";
    else if (!GetAddressFromField(Dlg, IDC_IPADDR, &Address, &AddressString))
        goto cleanup;
    rc = RegSetValueExA(hKey, "IPAddress", 0, REG_SZ, (const BYTE*)AddressString, strlen(AddressString) + 1);
    if (rc != ERROR_SUCCESS)
        goto cleanup;
    if (!SetIpAddressByDhcp && !GetAddressFromField(Dlg, IDC_SUBNETMASK, &Address, &AddressString))
        goto cleanup;
    rc = RegSetValueExA(hKey, "SubnetMask", 0, REG_SZ, (const BYTE*)AddressString, strlen(AddressString) + 1);
    if (rc != ERROR_SUCCESS)
        goto cleanup;
    if (!SetIpAddressByDhcp && !GetAddressFromField(Dlg, IDC_DEFGATEWAY, &Address, &AddressString))
        goto cleanup;
    rc = RegSetValueExA(hKey, "DefaultGateway", 0, REG_SZ, (const BYTE*)AddressString, strlen(AddressString) + 1);

⌨️ 快捷键说明

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