2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/dhcpcapi/dhcpcapi.c
5 * PURPOSE: Client API for DHCP
6 * COPYRIGHT: Copyright 2005 Art Yerkes <ayerkes@speakeasy.net>
14 static HANDLE PipeHandle
= INVALID_HANDLE_VALUE
;
16 DWORD APIENTRY
DhcpCApiInitialize(LPDWORD Version
) {
19 /* Wait for the pipe to be available */
20 if (WaitNamedPipeW(DHCP_PIPE_NAME
, NMPWAIT_USE_DEFAULT_WAIT
))
22 /* It's available, let's try to open it */
23 PipeHandle
= CreateFileW(DHCP_PIPE_NAME
,
24 GENERIC_READ
| GENERIC_WRITE
,
25 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
31 /* Check if we succeeded in opening the pipe */
32 if (PipeHandle
== INVALID_HANDLE_VALUE
)
35 return GetLastError();
39 /* Change the pipe into message mode */
40 PipeMode
= PIPE_READMODE_MESSAGE
;
41 if (!SetNamedPipeHandleState(PipeHandle
, &PipeMode
, NULL
, NULL
))
43 /* Mode change failed */
44 CloseHandle(PipeHandle
);
45 PipeHandle
= INVALID_HANDLE_VALUE
;
46 return GetLastError();
50 /* We're good to go */
58 /* No good, we failed */
59 return GetLastError();
63 VOID APIENTRY
DhcpCApiCleanup() {
64 CloseHandle(PipeHandle
);
65 PipeHandle
= INVALID_HANDLE_VALUE
;
68 DWORD APIENTRY
DhcpQueryHWInfo( DWORD AdapterIndex
,
73 COMM_DHCP_REPLY Reply
;
77 ASSERT(PipeHandle
!= INVALID_HANDLE_VALUE
);
79 Req
.Type
= DhcpReqQueryHWInfo
;
80 Req
.AdapterIndex
= AdapterIndex
;
82 Result
= TransactNamedPipe(PipeHandle
,
84 &Reply
, sizeof(Reply
),
88 /* Pipe transaction failed */
92 if( !Reply
.Reply
) return 0;
94 *MediaType
= Reply
.QueryHWInfo
.MediaType
;
95 *Mtu
= Reply
.QueryHWInfo
.Mtu
;
96 *Speed
= Reply
.QueryHWInfo
.Speed
;
101 DWORD APIENTRY
DhcpLeaseIpAddress( DWORD AdapterIndex
) {
103 COMM_DHCP_REPLY Reply
;
107 ASSERT(PipeHandle
!= INVALID_HANDLE_VALUE
);
109 Req
.Type
= DhcpReqLeaseIpAddress
;
110 Req
.AdapterIndex
= AdapterIndex
;
112 Result
= TransactNamedPipe(PipeHandle
,
114 &Reply
, sizeof(Reply
),
118 /* Pipe transaction failed */
125 DWORD APIENTRY
DhcpReleaseIpAddressLease( DWORD AdapterIndex
) {
127 COMM_DHCP_REPLY Reply
;
131 ASSERT(PipeHandle
!= INVALID_HANDLE_VALUE
);
133 Req
.Type
= DhcpReqReleaseIpAddress
;
134 Req
.AdapterIndex
= AdapterIndex
;
136 Result
= TransactNamedPipe(PipeHandle
,
138 &Reply
, sizeof(Reply
),
142 /* Pipe transaction failed */
149 DWORD APIENTRY
DhcpRenewIpAddressLease( DWORD AdapterIndex
) {
151 COMM_DHCP_REPLY Reply
;
155 ASSERT(PipeHandle
!= INVALID_HANDLE_VALUE
);
157 Req
.Type
= DhcpReqRenewIpAddress
;
158 Req
.AdapterIndex
= AdapterIndex
;
160 Result
= TransactNamedPipe(PipeHandle
,
162 &Reply
, sizeof(Reply
),
166 /* Pipe transaction failed */
173 DWORD APIENTRY
DhcpStaticRefreshParams( DWORD AdapterIndex
,
177 COMM_DHCP_REPLY Reply
;
181 ASSERT(PipeHandle
!= INVALID_HANDLE_VALUE
);
183 Req
.Type
= DhcpReqStaticRefreshParams
;
184 Req
.AdapterIndex
= AdapterIndex
;
185 Req
.Body
.StaticRefreshParams
.IPAddress
= Address
;
186 Req
.Body
.StaticRefreshParams
.Netmask
= Netmask
;
188 Result
= TransactNamedPipe(PipeHandle
,
190 &Reply
, sizeof(Reply
),
194 /* Pipe transaction failed */
202 * Set new TCP/IP parameters and notify DHCP client service of this
204 * \param[in] ServerName
205 * NULL for local machine
207 * \param[in] AdapterName
208 * IPHLPAPI name of adapter to change
210 * \param[in] NewIpAddress
211 * TRUE if IP address changes
213 * \param[in] IpAddress
214 * New IP address (network byte order)
216 * \param[in] SubnetMask
217 * New subnet mask (network byte order)
219 * \param[in] DhcpAction
224 * \return non-zero on success
226 * \remarks Undocumented by Microsoft
229 DhcpNotifyConfigChange(LPWSTR ServerName
,
237 DbgPrint("DHCPCSVC: DhcpNotifyConfigChange not implemented yet\n");
242 * Get DHCP info for an adapter
244 * \param[in] AdapterIndex
245 * Index of the adapter (iphlpapi-style) for which info is
248 * \param[out] DhcpEnabled
249 * Returns whether DHCP is enabled for the adapter
251 * \param[out] DhcpServer
252 * Returns DHCP server IP address (255.255.255.255 if no
253 * server reached yet), in network byte order
255 * \param[out] LeaseObtained
256 * Returns time at which the lease was obtained
258 * \param[out] LeaseExpires
259 * Returns time at which the lease will expire
261 * \return non-zero on success
263 * \remarks This is a ReactOS-only routine
265 DWORD APIENTRY
DhcpRosGetAdapterInfo( DWORD AdapterIndex
,
268 time_t *LeaseObtained
,
269 time_t *LeaseExpires
)
272 COMM_DHCP_REPLY Reply
;
276 ASSERT(PipeHandle
!= INVALID_HANDLE_VALUE
);
278 Req
.Type
= DhcpReqGetAdapterInfo
;
279 Req
.AdapterIndex
= AdapterIndex
;
281 Result
= TransactNamedPipe(PipeHandle
,
283 &Reply
, sizeof(Reply
),
286 if ( 0 != Result
&& 0 != Reply
.Reply
) {
287 *DhcpEnabled
= Reply
.GetAdapterInfo
.DhcpEnabled
;
289 *DhcpEnabled
= FALSE
;
291 if ( *DhcpEnabled
) {
292 *DhcpServer
= Reply
.GetAdapterInfo
.DhcpServer
;
293 *LeaseObtained
= Reply
.GetAdapterInfo
.LeaseObtained
;
294 *LeaseExpires
= Reply
.GetAdapterInfo
.LeaseExpires
;
296 *DhcpServer
= INADDR_NONE
;
305 DllMain(PVOID hinstDll
,
311 case DLL_PROCESS_ATTACH
:
312 DisableThreadLibraryCalls(hinstDll
);
315 case DLL_PROCESS_DETACH
: