- Merge 54895, 54896, 54899, 54912, 54913, 54915, and 54916 from wlan-bringup
[reactos.git] / reactos / dll / win32 / dhcpcsvc / dhcp / api.c
1 /* $Id: $
2 *
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
7 * PROGRAMMER: arty
8 */
9
10 #include "rosdhcp.h"
11
12 #define NDEBUG
13 #include <reactos/debug.h>
14
15 static CRITICAL_SECTION ApiCriticalSection;
16
17 extern HANDLE AdapterStateChangedEvent;
18
19 VOID ApiInit() {
20 InitializeCriticalSection( &ApiCriticalSection );
21 }
22
23 VOID ApiLock() {
24 EnterCriticalSection( &ApiCriticalSection );
25 }
26
27 VOID ApiUnlock() {
28 LeaveCriticalSection( &ApiCriticalSection );
29 }
30
31 VOID ApiFree() {
32 DeleteCriticalSection( &ApiCriticalSection );
33 }
34
35 /* This represents the service portion of the DHCP client API */
36
37 DWORD DSLeaseIpAddress( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
38 COMM_DHCP_REPLY Reply;
39 PDHCP_ADAPTER Adapter;
40 struct protocol* proto;
41
42 ApiLock();
43
44 Adapter = AdapterFindIndex( Req->AdapterIndex );
45
46 Reply.Reply = Adapter ? 1 : 0;
47
48 if( Adapter ) {
49 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
50 if (proto)
51 remove_protocol(proto);
52
53 add_protocol( Adapter->DhclientInfo.name,
54 Adapter->DhclientInfo.rfdesc, got_one,
55 &Adapter->DhclientInfo );
56
57 Adapter->DhclientInfo.client->state = S_INIT;
58 state_reboot(&Adapter->DhclientInfo);
59
60 if (AdapterStateChangedEvent != NULL)
61 SetEvent(AdapterStateChangedEvent);
62 }
63
64 ApiUnlock();
65
66 return Send( &Reply );
67 }
68
69 DWORD DSQueryHWInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
70 COMM_DHCP_REPLY Reply;
71 PDHCP_ADAPTER Adapter;
72
73 ApiLock();
74
75 Adapter = AdapterFindIndex( Req->AdapterIndex );
76
77 Reply.Reply = Adapter ? 1 : 0;
78
79 if (Adapter) {
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;
84 }
85
86 ApiUnlock();
87
88 return Send( &Reply );
89 }
90
91 DWORD DSReleaseIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
92 COMM_DHCP_REPLY Reply;
93 PDHCP_ADAPTER Adapter;
94 struct protocol* proto;
95
96 ApiLock();
97
98 Adapter = AdapterFindIndex( Req->AdapterIndex );
99
100 Reply.Reply = Adapter ? 1 : 0;
101
102 if( Adapter ) {
103 if (Adapter->NteContext)
104 DeleteIPAddress( Adapter->NteContext );
105 if (Adapter->RouterMib.dwForwardNextHop)
106 DeleteIpForwardEntry( &Adapter->RouterMib );
107
108 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
109 if (proto)
110 remove_protocol(proto);
111
112 Adapter->DhclientInfo.client->active = NULL;
113 Adapter->DhclientInfo.client->state = S_INIT;
114
115 if (AdapterStateChangedEvent != NULL)
116 SetEvent(AdapterStateChangedEvent);
117 }
118
119 ApiUnlock();
120
121 return Send( &Reply );
122 }
123
124 DWORD DSRenewIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
125 COMM_DHCP_REPLY Reply;
126 PDHCP_ADAPTER Adapter;
127 struct protocol* proto;
128
129 ApiLock();
130
131 Adapter = AdapterFindIndex( Req->AdapterIndex );
132
133 if( !Adapter || Adapter->DhclientState.state == S_STATIC ) {
134 Reply.Reply = 0;
135 ApiUnlock();
136 return Send( &Reply );
137 }
138
139 Reply.Reply = 1;
140
141 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
142 if (proto)
143 remove_protocol(proto);
144
145 add_protocol( Adapter->DhclientInfo.name,
146 Adapter->DhclientInfo.rfdesc, got_one,
147 &Adapter->DhclientInfo );
148
149 Adapter->DhclientInfo.client->state = S_INIT;
150 state_reboot(&Adapter->DhclientInfo);
151
152 if (AdapterStateChangedEvent != NULL)
153 SetEvent(AdapterStateChangedEvent);
154
155 ApiUnlock();
156
157 return Send( &Reply );
158 }
159
160 DWORD DSStaticRefreshParams( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
161 NTSTATUS Status;
162 COMM_DHCP_REPLY Reply;
163 PDHCP_ADAPTER Adapter;
164 struct protocol* proto;
165
166 ApiLock();
167
168 Adapter = AdapterFindIndex( Req->AdapterIndex );
169
170 Reply.Reply = Adapter ? 1 : 0;
171
172 if( Adapter ) {
173 if (Adapter->NteContext)
174 DeleteIPAddress( Adapter->NteContext );
175 if (Adapter->RouterMib.dwForwardNextHop)
176 DeleteIpForwardEntry( &Adapter->RouterMib );
177
178 Adapter->DhclientState.state = S_STATIC;
179 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
180 if (proto)
181 remove_protocol(proto);
182 Status = AddIPAddress( Req->Body.StaticRefreshParams.IPAddress,
183 Req->Body.StaticRefreshParams.Netmask,
184 Req->AdapterIndex,
185 &Adapter->NteContext,
186 &Adapter->NteInstance );
187 Reply.Reply = NT_SUCCESS(Status);
188
189 if (AdapterStateChangedEvent != NULL)
190 SetEvent(AdapterStateChangedEvent);
191 }
192
193 ApiUnlock();
194
195 return Send( &Reply );
196 }
197
198 DWORD DSGetAdapterInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
199 COMM_DHCP_REPLY Reply;
200 PDHCP_ADAPTER Adapter;
201
202 ApiLock();
203
204 Adapter = AdapterFindIndex( Req->AdapterIndex );
205
206 Reply.Reply = Adapter ? 1 : 0;
207
208 if( Adapter ) {
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);
216 } else {
217 DPRINT1("Unexpected server address len %d\n",
218 Adapter->DhclientState.active->serveraddress.len);
219 Reply.GetAdapterInfo.DhcpServer = htonl(INADDR_NONE);
220 }
221 Reply.GetAdapterInfo.LeaseObtained = Adapter->DhclientState.active->obtained;
222 Reply.GetAdapterInfo.LeaseExpires = Adapter->DhclientState.active->expiry;
223 } else {
224 Reply.GetAdapterInfo.DhcpServer = htonl(INADDR_NONE);
225 Reply.GetAdapterInfo.LeaseObtained = 0;
226 Reply.GetAdapterInfo.LeaseExpires = 0;
227 }
228 }
229
230 ApiUnlock();
231
232 return Send( &Reply );
233 }