Reactos
1/*
2 * PROJECT: ReactOS Networking
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: lib/iphlpapi/dhcp_reactos.c
5 * PURPOSE: DHCP helper functions for ReactOS
6 * COPYRIGHT: Copyright 2006 Ge van Geldorp <gvg@reactos.org>
7 */
8
9#include "iphlpapi_private.h"
10
11DWORD
12getDhcpInfoForAdapter(
13 DWORD AdapterIndex,
14 PIP_ADAPTER_INFO ptr)
15{
16 const char *ifname = NULL;
17 HKEY hKeyInterfaces = NULL, hKeyInterface = NULL;
18 DWORD dwValue, dwSize, dwType;
19 DWORD ret = ERROR_SUCCESS;
20
21 ptr->DhcpEnabled = 0;
22 ptr->LeaseObtained = 0;
23 ptr->LeaseExpires = 0;
24 strcpy(ptr->DhcpServer.IpAddress.String, "");
25
26 ifname = getInterfaceNameByIndex(AdapterIndex);
27 if (!ifname)
28 return ERROR_OUTOFMEMORY;
29
30 ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
31 L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces",
32 0,
33 KEY_READ,
34 &hKeyInterfaces);
35 if (ret != ERROR_SUCCESS)
36 goto done;
37
38 ret = RegOpenKeyExA(hKeyInterfaces,
39 ifname,
40 0,
41 KEY_READ,
42 &hKeyInterface);
43 if (ret != ERROR_SUCCESS)
44 goto done;
45
46 dwSize = sizeof(ptr->DhcpEnabled);
47 ret = RegQueryValueExW(hKeyInterface,
48 L"EnableDHCP",
49 NULL,
50 &dwType,
51 (PBYTE)&ptr->DhcpEnabled,
52 &dwSize);
53 if (ret != ERROR_SUCCESS)
54 ptr->DhcpEnabled = 0;
55
56 if (ptr->DhcpEnabled != 0)
57 {
58 dwSize = sizeof(ptr->LeaseObtained);
59 ret = RegQueryValueExW(hKeyInterface,
60 L"LeaseObtainedTime",
61 NULL,
62 &dwType,
63 (PBYTE)&dwValue,
64 &dwSize);
65 if (ret == ERROR_SUCCESS)
66 ptr->LeaseObtained = (time_t)dwValue;
67
68 dwSize = sizeof(dwValue);
69 ret = RegQueryValueExW(hKeyInterface,
70 L"LeaseTerminatesTime",
71 NULL,
72 &dwType,
73 (PBYTE)&dwValue,
74 &dwSize);
75 if (ret == ERROR_SUCCESS)
76 ptr->LeaseExpires = (time_t)dwValue;
77
78 dwSize = sizeof(ptr->DhcpServer.IpAddress.String);
79 ret = RegQueryValueExA(hKeyInterface,
80 "DhcpServer",
81 NULL,
82 &dwType,
83 (PBYTE)&ptr->DhcpServer.IpAddress.String,
84 &dwSize);
85 if (ret != ERROR_SUCCESS)
86 strcpy(ptr->DhcpServer.IpAddress.String, "");
87 }
88 ret = ERROR_SUCCESS;
89
90done:
91 if (hKeyInterface)
92 RegCloseKey(hKeyInterface);
93
94 if (hKeyInterfaces)
95 RegCloseKey(hKeyInterfaces);
96
97 if (ifname)
98 consumeInterfaceName(ifname);
99
100 return ret;
101}