3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: subsys/system/dhcp/api.c
6 * PURPOSE: DHCP client api handlers
13 #include <reactos/debug.h>
15 static CRITICAL_SECTION ApiCriticalSection
;
17 extern HANDLE AdapterStateChangedEvent
;
20 InitializeCriticalSection( &ApiCriticalSection
);
24 EnterCriticalSection( &ApiCriticalSection
);
28 LeaveCriticalSection( &ApiCriticalSection
);
32 DeleteCriticalSection( &ApiCriticalSection
);
35 /* This represents the service portion of the DHCP client API */
37 DWORD
DSLeaseIpAddress( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
38 COMM_DHCP_REPLY Reply
;
39 PDHCP_ADAPTER Adapter
;
40 struct protocol
* proto
;
44 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
46 Reply
.Reply
= Adapter
? 1 : 0;
49 proto
= find_protocol_by_adapter( &Adapter
->DhclientInfo
);
51 remove_protocol(proto
);
53 add_protocol( Adapter
->DhclientInfo
.name
,
54 Adapter
->DhclientInfo
.rfdesc
, got_one
,
55 &Adapter
->DhclientInfo
);
57 Adapter
->DhclientInfo
.client
->state
= S_INIT
;
58 state_reboot(&Adapter
->DhclientInfo
);
60 if (AdapterStateChangedEvent
!= NULL
)
61 SetEvent(AdapterStateChangedEvent
);
66 return Send( &Reply
);
69 DWORD
DSQueryHWInfo( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
70 COMM_DHCP_REPLY Reply
;
71 PDHCP_ADAPTER Adapter
;
75 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
77 Reply
.Reply
= Adapter
? 1 : 0;
80 Reply
.QueryHWInfo
.AdapterIndex
= Req
->AdapterIndex
;
81 Reply
.QueryHWInfo
.MediaType
= Adapter
->IfMib
.dwType
;
82 Reply
.QueryHWInfo
.Mtu
= Adapter
->IfMib
.dwMtu
;
83 Reply
.QueryHWInfo
.Speed
= Adapter
->IfMib
.dwSpeed
;
88 return Send( &Reply
);
91 DWORD
DSReleaseIpAddressLease( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
92 COMM_DHCP_REPLY Reply
;
93 PDHCP_ADAPTER Adapter
;
94 struct protocol
* proto
;
98 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
100 Reply
.Reply
= Adapter
? 1 : 0;
103 if (Adapter
->NteContext
)
104 DeleteIPAddress( Adapter
->NteContext
);
105 if (Adapter
->RouterMib
.dwForwardNextHop
)
106 DeleteIpForwardEntry( &Adapter
->RouterMib
);
108 proto
= find_protocol_by_adapter( &Adapter
->DhclientInfo
);
110 remove_protocol(proto
);
112 Adapter
->DhclientInfo
.client
->active
= NULL
;
113 Adapter
->DhclientInfo
.client
->state
= S_INIT
;
115 if (AdapterStateChangedEvent
!= NULL
)
116 SetEvent(AdapterStateChangedEvent
);
121 return Send( &Reply
);
124 DWORD
DSRenewIpAddressLease( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
125 COMM_DHCP_REPLY Reply
;
126 PDHCP_ADAPTER Adapter
;
127 struct protocol
* proto
;
131 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
133 if( !Adapter
|| Adapter
->DhclientState
.state
== S_STATIC
) {
136 return Send( &Reply
);
141 proto
= find_protocol_by_adapter( &Adapter
->DhclientInfo
);
143 remove_protocol(proto
);
145 add_protocol( Adapter
->DhclientInfo
.name
,
146 Adapter
->DhclientInfo
.rfdesc
, got_one
,
147 &Adapter
->DhclientInfo
);
149 Adapter
->DhclientInfo
.client
->state
= S_INIT
;
150 state_reboot(&Adapter
->DhclientInfo
);
152 if (AdapterStateChangedEvent
!= NULL
)
153 SetEvent(AdapterStateChangedEvent
);
157 return Send( &Reply
);
160 DWORD
DSStaticRefreshParams( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
162 COMM_DHCP_REPLY Reply
;
163 PDHCP_ADAPTER Adapter
;
164 struct protocol
* proto
;
168 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
170 Reply
.Reply
= Adapter
? 1 : 0;
173 if (Adapter
->NteContext
)
174 DeleteIPAddress( Adapter
->NteContext
);
175 if (Adapter
->RouterMib
.dwForwardNextHop
)
176 DeleteIpForwardEntry( &Adapter
->RouterMib
);
178 Adapter
->DhclientState
.state
= S_STATIC
;
179 proto
= find_protocol_by_adapter( &Adapter
->DhclientInfo
);
181 remove_protocol(proto
);
182 Status
= AddIPAddress( Req
->Body
.StaticRefreshParams
.IPAddress
,
183 Req
->Body
.StaticRefreshParams
.Netmask
,
185 &Adapter
->NteContext
,
186 &Adapter
->NteInstance
);
187 Reply
.Reply
= NT_SUCCESS(Status
);
189 if (AdapterStateChangedEvent
!= NULL
)
190 SetEvent(AdapterStateChangedEvent
);
195 return Send( &Reply
);
198 DWORD
DSGetAdapterInfo( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
199 COMM_DHCP_REPLY Reply
;
200 PDHCP_ADAPTER Adapter
;
204 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
206 Reply
.Reply
= Adapter
? 1 : 0;
209 Reply
.GetAdapterInfo
.DhcpEnabled
= (S_STATIC
!= Adapter
->DhclientState
.state
);
210 if (S_BOUND
== Adapter
->DhclientState
.state
) {
211 if (sizeof(Reply
.GetAdapterInfo
.DhcpServer
) ==
212 Adapter
->DhclientState
.active
->serveraddress
.len
) {
213 memcpy(&Reply
.GetAdapterInfo
.DhcpServer
,
214 Adapter
->DhclientState
.active
->serveraddress
.iabuf
,
215 Adapter
->DhclientState
.active
->serveraddress
.len
);
217 DPRINT1("Unexpected server address len %d\n",
218 Adapter
->DhclientState
.active
->serveraddress
.len
);
219 Reply
.GetAdapterInfo
.DhcpServer
= htonl(INADDR_NONE
);
221 Reply
.GetAdapterInfo
.LeaseObtained
= Adapter
->DhclientState
.active
->obtained
;
222 Reply
.GetAdapterInfo
.LeaseExpires
= Adapter
->DhclientState
.active
->expiry
;
224 Reply
.GetAdapterInfo
.DhcpServer
= htonl(INADDR_NONE
);
225 Reply
.GetAdapterInfo
.LeaseObtained
= 0;
226 Reply
.GetAdapterInfo
.LeaseExpires
= 0;
232 return Send( &Reply
);