2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: subsys/system/dhcp/api.c
5 * PURPOSE: DHCP client api handlers
12 #include <reactos/debug.h>
14 static CRITICAL_SECTION ApiCriticalSection
;
16 extern HANDLE AdapterStateChangedEvent
;
19 InitializeCriticalSection( &ApiCriticalSection
);
23 EnterCriticalSection( &ApiCriticalSection
);
27 LeaveCriticalSection( &ApiCriticalSection
);
31 DeleteCriticalSection( &ApiCriticalSection
);
34 /* This represents the service portion of the DHCP client API */
36 DWORD
DSLeaseIpAddress( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
37 COMM_DHCP_REPLY Reply
;
38 PDHCP_ADAPTER Adapter
;
39 struct protocol
* proto
;
43 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
45 Reply
.Reply
= Adapter
? 1 : 0;
48 proto
= find_protocol_by_adapter( &Adapter
->DhclientInfo
);
50 remove_protocol(proto
);
52 add_protocol( Adapter
->DhclientInfo
.name
,
53 Adapter
->DhclientInfo
.rfdesc
, got_one
,
54 &Adapter
->DhclientInfo
);
56 Adapter
->DhclientInfo
.client
->state
= S_INIT
;
57 state_reboot(&Adapter
->DhclientInfo
);
59 if (AdapterStateChangedEvent
!= NULL
)
60 SetEvent(AdapterStateChangedEvent
);
65 return Send( &Reply
);
68 DWORD
DSQueryHWInfo( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
69 COMM_DHCP_REPLY Reply
;
70 PDHCP_ADAPTER Adapter
;
74 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
76 Reply
.Reply
= Adapter
? 1 : 0;
79 Reply
.QueryHWInfo
.AdapterIndex
= Req
->AdapterIndex
;
80 Reply
.QueryHWInfo
.MediaType
= Adapter
->IfMib
.dwType
;
81 Reply
.QueryHWInfo
.Mtu
= Adapter
->IfMib
.dwMtu
;
82 Reply
.QueryHWInfo
.Speed
= Adapter
->IfMib
.dwSpeed
;
87 return Send( &Reply
);
90 DWORD
DSReleaseIpAddressLease( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
91 COMM_DHCP_REPLY Reply
;
92 PDHCP_ADAPTER Adapter
;
93 struct protocol
* proto
;
97 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
99 Reply
.Reply
= Adapter
? 1 : 0;
102 if (Adapter
->NteContext
)
104 DeleteIPAddress( Adapter
->NteContext
);
105 Adapter
->NteContext
= 0;
107 if (Adapter
->RouterMib
.dwForwardNextHop
)
109 DeleteIpForwardEntry( &Adapter
->RouterMib
);
110 Adapter
->RouterMib
.dwForwardNextHop
= 0;
113 proto
= find_protocol_by_adapter( &Adapter
->DhclientInfo
);
115 remove_protocol(proto
);
117 Adapter
->DhclientInfo
.client
->active
= NULL
;
118 Adapter
->DhclientInfo
.client
->state
= S_INIT
;
120 if (AdapterStateChangedEvent
!= NULL
)
121 SetEvent(AdapterStateChangedEvent
);
126 return Send( &Reply
);
129 DWORD
DSRenewIpAddressLease( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
130 COMM_DHCP_REPLY Reply
;
131 PDHCP_ADAPTER Adapter
;
132 struct protocol
* proto
;
136 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
138 if( !Adapter
|| Adapter
->DhclientState
.state
== S_STATIC
) {
141 return Send( &Reply
);
146 proto
= find_protocol_by_adapter( &Adapter
->DhclientInfo
);
148 remove_protocol(proto
);
150 add_protocol( Adapter
->DhclientInfo
.name
,
151 Adapter
->DhclientInfo
.rfdesc
, got_one
,
152 &Adapter
->DhclientInfo
);
154 Adapter
->DhclientInfo
.client
->state
= S_INIT
;
155 state_reboot(&Adapter
->DhclientInfo
);
157 if (AdapterStateChangedEvent
!= NULL
)
158 SetEvent(AdapterStateChangedEvent
);
162 return Send( &Reply
);
165 DWORD
DSStaticRefreshParams( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
167 COMM_DHCP_REPLY Reply
;
168 PDHCP_ADAPTER Adapter
;
169 struct protocol
* proto
;
173 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
175 Reply
.Reply
= Adapter
? 1 : 0;
178 if (Adapter
->NteContext
)
180 DeleteIPAddress( Adapter
->NteContext
);
181 Adapter
->NteContext
= 0;
183 if (Adapter
->RouterMib
.dwForwardNextHop
)
185 DeleteIpForwardEntry( &Adapter
->RouterMib
);
186 Adapter
->RouterMib
.dwForwardNextHop
= 0;
189 Adapter
->DhclientState
.state
= S_STATIC
;
190 proto
= find_protocol_by_adapter( &Adapter
->DhclientInfo
);
192 remove_protocol(proto
);
193 Status
= AddIPAddress( Req
->Body
.StaticRefreshParams
.IPAddress
,
194 Req
->Body
.StaticRefreshParams
.Netmask
,
196 &Adapter
->NteContext
,
197 &Adapter
->NteInstance
);
198 Reply
.Reply
= NT_SUCCESS(Status
);
200 if (AdapterStateChangedEvent
!= NULL
)
201 SetEvent(AdapterStateChangedEvent
);
206 return Send( &Reply
);
209 DWORD
DSGetAdapterInfo( PipeSendFunc Send
, COMM_DHCP_REQ
*Req
) {
210 COMM_DHCP_REPLY Reply
;
211 PDHCP_ADAPTER Adapter
;
215 Adapter
= AdapterFindIndex( Req
->AdapterIndex
);
217 Reply
.Reply
= Adapter
? 1 : 0;
220 Reply
.GetAdapterInfo
.DhcpEnabled
= (S_STATIC
!= Adapter
->DhclientState
.state
);
221 if (S_BOUND
== Adapter
->DhclientState
.state
) {
222 if (sizeof(Reply
.GetAdapterInfo
.DhcpServer
) ==
223 Adapter
->DhclientState
.active
->serveraddress
.len
) {
224 memcpy(&Reply
.GetAdapterInfo
.DhcpServer
,
225 Adapter
->DhclientState
.active
->serveraddress
.iabuf
,
226 Adapter
->DhclientState
.active
->serveraddress
.len
);
228 DPRINT1("Unexpected server address len %d\n",
229 Adapter
->DhclientState
.active
->serveraddress
.len
);
230 Reply
.GetAdapterInfo
.DhcpServer
= htonl(INADDR_NONE
);
232 Reply
.GetAdapterInfo
.LeaseObtained
= Adapter
->DhclientState
.active
->obtained
;
233 Reply
.GetAdapterInfo
.LeaseExpires
= Adapter
->DhclientState
.active
->expiry
;
235 Reply
.GetAdapterInfo
.DhcpServer
= htonl(INADDR_NONE
);
236 Reply
.GetAdapterInfo
.LeaseObtained
= 0;
237 Reply
.GetAdapterInfo
.LeaseExpires
= 0;
243 return Send( &Reply
);