3 * Copyright (C) 2004 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * PROJECT: ReactOS Network Control Panel
22 * FILE: lib/cpl/system/tcpip_properties.c
23 * PURPOSE: ReactOS Network Control Panel
24 * PROGRAMMER: Gero Kuehn (reactos.filter@gkware.com)
45 // this is missing on reactos...
46 #ifndef IPM_SETADDRESS
47 #define IPM_SETADDRESS (WM_USER+101)
56 void InitPropSheetPage(PROPSHEETPAGE
*psp
, WORD idDlg
, DLGPROC DlgProc
);
59 EnableDHCP( HWND hwndDlg
, BOOL Enabled
) {
60 CheckDlgButton(hwndDlg
,IDC_USEDHCP
,Enabled
? BST_CHECKED
: BST_UNCHECKED
);
61 CheckDlgButton(hwndDlg
,IDC_NODHCP
,Enabled
? BST_UNCHECKED
: BST_CHECKED
);
65 ManualDNS( HWND hwndDlg
, BOOL Enabled
) {
66 CheckDlgButton(hwndDlg
,IDC_FIXEDDNS
,Enabled
? BST_CHECKED
: BST_UNCHECKED
);
67 CheckDlgButton(hwndDlg
,IDC_AUTODNS
,Enabled
? BST_UNCHECKED
: BST_CHECKED
);
68 EnableWindow(GetDlgItem(hwndDlg
,IDC_DNS1
),Enabled
);
69 EnableWindow(GetDlgItem(hwndDlg
,IDC_DNS2
),Enabled
);
73 GetAddressFromField( HWND hwndDlg
, UINT CtlId
,
75 const char **AddressString
) {
77 struct in_addr inIPAddr
;
79 *AddressString
= NULL
;
81 lResult
= SendMessage(GetDlgItem(hwndDlg
, IDC_IPADDR
), IPM_GETADDRESS
, 0,
83 if( lResult
!= 4 ) return FALSE
;
85 inIPAddr
.s_addr
= *dwIPAddr
;
86 *AddressString
= inet_ntoa(inIPAddr
);
87 if( !*AddressString
) return FALSE
;
93 InternTCPIPSettings( HWND hwndDlg
) {
94 PROPSHEETPAGE
*pPage
= (PROPSHEETPAGE
*)GetWindowLongPtr(hwndDlg
,GWL_USERDATA
);
95 IP_ADAPTER_INFO
*pInfo
= NULL
;
96 TCHAR pszRegKey
[MAX_PATH
];
98 DWORD IpAddress
, NetMask
, Gateway
;
99 const char *AddressString
;
101 BOOL DhcpEnabled
= FALSE
;
102 MIB_IPFORWARDROW RowToAdd
= { 0 };
105 pInfo
= (IP_ADAPTER_INFO
*)pPage
->lParam
;
107 if( !pPage
|| !pInfo
) goto cleanup
;
109 _stprintf(pszRegKey
,_T("SYSTEM\\CurrentControlSet\\Services\\TCPIP\\Parameters\\Interfaces\\%S"),pInfo
->AdapterName
);
110 if(RegOpenKey(HKEY_LOCAL_MACHINE
,pszRegKey
,&hKey
)!=ERROR_SUCCESS
) goto cleanup
;
112 if( !GetAddressFromField
113 ( hwndDlg
, IDC_IPADDR
, &IpAddress
, &AddressString
) ||
115 ( hKey
, _T("IPAddress"), 0, REG_SZ
, (const BYTE
*)AddressString
,
116 strlen(AddressString
) ) != ERROR_SUCCESS
)
119 if( !GetAddressFromField
120 ( hwndDlg
, IDC_SUBNETMASK
, &NetMask
, &AddressString
) ||
122 ( hKey
, _T("SubnetMask"), 0, REG_SZ
, (const BYTE
*)AddressString
,
123 strlen(AddressString
) ) != ERROR_SUCCESS
)
126 if( DhcpEnabled
) /* FIXME - DhcpEnabled is never initialized at this point! */
127 DhcpLeaseIpAddress( pInfo
->Index
);
129 DhcpReleaseIpAddressLease( pInfo
->Index
);
130 DhcpStaticRefreshParams( pInfo
->Index
, IpAddress
, NetMask
);
133 if( !GetAddressFromField
134 ( hwndDlg
, IDC_DEFGATEWAY
, &Gateway
, &AddressString
) ||
136 ( hKey
, _T("DefaultGateway"), 0, REG_SZ
, (const BYTE
*)AddressString
,
137 strlen(AddressString
) ) != ERROR_SUCCESS
)
140 /* If not on DHCP then add a default gateway, assuming one was specified */
142 RowToAdd
.dwForwardMask
= 0;
143 RowToAdd
.dwForwardMetric1
= 1;
144 RowToAdd
.dwForwardNextHop
= Gateway
;
145 CreateIpForwardEntry( &RowToAdd
);
149 if( hKey
) RegCloseKey( hKey
);
154 static INT_PTR CALLBACK
155 TCPIPPropertyPageProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
157 PROPSHEETPAGE
*pPage
= (PROPSHEETPAGE
*)GetWindowLongPtr(hwndDlg
,GWL_USERDATA
);
158 IP_ADAPTER_INFO
*pInfo
= NULL
;
163 pInfo
= (IP_ADAPTER_INFO
*)pPage
->lParam
;
168 pPage
= (PROPSHEETPAGE
*)lParam
;
169 pInfo
= (IP_ADAPTER_INFO
*)pPage
->lParam
;
170 EnableWindow(GetDlgItem(hwndDlg
,IDC_ADVANCED
),FALSE
);
171 SetWindowLongPtr(hwndDlg
,GWL_USERDATA
,(DWORD_PTR
)pPage
->lParam
);
173 EnableDHCP( hwndDlg
, pInfo
->DhcpEnabled
);
177 IP_ADDR_STRING
*pString
;
178 pString
= &pInfo
->IpAddressList
;
180 pString
= pString
->Next
;
181 dwIPAddr
= inet_addr(pString
->IpAddress
.String
);
182 SendDlgItemMessage(hwndDlg
,IDC_IPADDR
,IPM_SETADDRESS
,0,
184 dwIPAddr
= inet_addr(pString
->IpMask
.String
);
185 SendDlgItemMessage(hwndDlg
,IDC_SUBNETMASK
,IPM_SETADDRESS
,0,
188 pString
= &pInfo
->GatewayList
;
190 pString
= pString
->Next
;
191 dwIPAddr
= inet_addr(pString
->IpAddress
.String
);
192 SendDlgItemMessage(hwndDlg
,IDC_DEFGATEWAY
,IPM_SETADDRESS
,0,
197 TCHAR pszRegKey
[MAX_PATH
];
199 _stprintf(pszRegKey
,_T("SYSTEM\\CurrentControlSet\\Services\\TCPIP\\Parameters\\Interfaces\\%S"),pInfo
->AdapterName
);
200 if(RegOpenKey(HKEY_LOCAL_MACHINE
,pszRegKey
,&hKey
)==ERROR_SUCCESS
)
202 char pszDNS
[MAX_PATH
];
203 DWORD dwSize
= sizeof(pszDNS
);
204 DWORD dwType
= REG_SZ
;
206 RegQueryValueExA(hKey
,"NameServer",NULL
,&dwType
,(BYTE
*)pszDNS
,&dwSize
);
209 NextDNSServer
= pszDNS
;
211 while( NextDNSServer
&& StaticDNS
< 2 ) {
212 dwIPAddr
= inet_addr(NextDNSServer
);
213 if( dwIPAddr
!= INADDR_NONE
) {
214 SendDlgItemMessage(hwndDlg
,IDC_DNS1
+ StaticDNS
,
215 IPM_SETADDRESS
,0,ntohl(dwIPAddr
));
218 NextDNSServer
= strchr( pszDNS
, ',' );
223 ManualDNS( hwndDlg
, StaticDNS
);
229 switch(LOWORD(wParam
))
232 ManualDNS( hwndDlg
, TRUE
);
236 ManualDNS( hwndDlg
, FALSE
);
240 EnableDHCP( hwndDlg
, TRUE
);
244 EnableDHCP( hwndDlg
, FALSE
);
248 /* Set the IP Address and DNS Information so we won't
249 * be doing all this for nothing */
250 InternTCPIPSettings( hwndDlg
);
258 void DisplayTCPIPProperties(HWND hParent
,IP_ADAPTER_INFO
*pInfo
)
260 PROPSHEETPAGE psp
[1];
262 INITCOMMONCONTROLSEX cce
;
264 cce
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
265 cce
.dwICC
= ICC_INTERNET_CLASSES
;
266 InitCommonControlsEx(&cce
);
268 ZeroMemory(&psh
, sizeof(PROPSHEETHEADER
));
269 psh
.dwSize
= sizeof(PROPSHEETHEADER
);
270 psh
.dwFlags
= PSH_PROPSHEETPAGE
| PSH_NOAPPLYNOW
;
271 psh
.hwndParent
= hParent
;
272 psh
.hInstance
= hApplet
;
273 psh
.hIcon
= LoadIcon(hApplet
, MAKEINTRESOURCE(IDI_CPLSYSTEM
));
274 psh
.pszCaption
= NULL
;//Caption;
275 psh
.nPages
= sizeof(psp
) / sizeof(PROPSHEETPAGE
);
278 psh
.pfnCallback
= NULL
;
281 InitPropSheetPage(&psp
[0], IDD_TCPIPPROPERTIES
, TCPIPPropertyPageProc
);
282 psp
[0].lParam
= (LPARAM
)pInfo
;
284 if (PropertySheet(&psh
) == -1)
286 MessageBox(hParent
,_T("Unable to create property sheet"),_T("Error"),MB_ICONSTOP
);