[DHCPCSVC]
[reactos.git] / 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 {
105 DeleteIPAddress( Adapter->NteContext );
106 Adapter->NteContext = 0;
107 }
108 if (Adapter->RouterMib.dwForwardNextHop)
109 {
110 DeleteIpForwardEntry( &Adapter->RouterMib );
111 Adapter->RouterMib.dwForwardNextHop = 0;
112 }
113
114 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
115 if (proto)
116 remove_protocol(proto);
117
118 Adapter->DhclientInfo.client->active = NULL;
119 Adapter->DhclientInfo.client->state = S_INIT;
120
121 if (AdapterStateChangedEvent != NULL)
122 SetEvent(AdapterStateChangedEvent);
123 }
124
125 ApiUnlock();
126
127 return Send( &Reply );
128 }
129
130 DWORD DSRenewIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
131 COMM_DHCP_REPLY Reply;
132 PDHCP_ADAPTER Adapter;
133 struct protocol* proto;
134
135 ApiLock();
136
137 Adapter = AdapterFindIndex( Req->AdapterIndex );
138
139 if( !Adapter || Adapter->DhclientState.state == S_STATIC ) {
140 Reply.Reply = 0;
141 ApiUnlock();
142 return Send( &Reply );
143 }
144
145 Reply.Reply = 1;
146
147 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
148 if (proto)
149 remove_protocol(proto);
150
151 add_protocol( Adapter->DhclientInfo.name,
152 Adapter->DhclientInfo.rfdesc, got_one,
153 &Adapter->DhclientInfo );
154
155 Adapter->DhclientInfo.client->state = S_INIT;
156 state_reboot(&Adapter->DhclientInfo);
157
158 if (AdapterStateChangedEvent != NULL)
159 SetEvent(AdapterStateChangedEvent);
160
161 ApiUnlock();
162
163 return Send( &Reply );
164 }
165
166 DWORD DSStaticRefreshParams( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
167 NTSTATUS Status;
168 COMM_DHCP_REPLY Reply;
169 PDHCP_ADAPTER Adapter;
170 struct protocol* proto;
171
172 ApiLock();
173
174 Adapter = AdapterFindIndex( Req->AdapterIndex );
175
176 Reply.Reply = Adapter ? 1 : 0;
177
178 if( Adapter ) {
179 if (Adapter->NteContext)
180 {
181 DeleteIPAddress( Adapter->NteContext );
182 Adapter->NteContext = 0;
183 }
184 if (Adapter->RouterMib.dwForwardNextHop)
185 {
186 DeleteIpForwardEntry( &Adapter->RouterMib );
187 Adapter->RouterMib.dwForwardNextHop = 0;
188 }
189
190 Adapter->DhclientState.state = S_STATIC;
191 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
192 if (proto)
193 remove_protocol(proto);
194 Status = AddIPAddress( Req->Body.StaticRefreshParams.IPAddress,
195 Req->Body.StaticRefreshParams.Netmask,
196 Req->AdapterIndex,
197 &Adapter->NteContext,
198 &Adapter->NteInstance );
199 Reply.Reply = NT_SUCCESS(Status);
200
201 if (AdapterStateChangedEvent != NULL)
202 SetEvent(AdapterStateChangedEvent);
203 }
204
205 ApiUnlock();
206
207 return Send( &Reply );
208 }
209
210 DWORD DSGetAdapterInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
211 COMM_DHCP_REPLY Reply;
212 PDHCP_ADAPTER Adapter;
213
214 ApiLock();
215
216 Adapter = AdapterFindIndex( Req->AdapterIndex );
217
218 Reply.Reply = Adapter ? 1 : 0;
219
220 if( Adapter ) {
221 Reply.GetAdapterInfo.DhcpEnabled = (S_STATIC != Adapter->DhclientState.state);
222 if (S_BOUND == Adapter->DhclientState.state) {
223 if (sizeof(Reply.GetAdapterInfo.DhcpServer) ==
224 Adapter->DhclientState.active->serveraddress.len) {
225 memcpy(&Reply.GetAdapterInfo.DhcpServer,
226 Adapter->DhclientState.active->serveraddress.iabuf,
227 Adapter->DhclientState.active->serveraddress.len);
228 } else {
229 DPRINT1("Unexpected server address len %d\n",
230 Adapter->DhclientState.active->serveraddress.len);
231 Reply.GetAdapterInfo.DhcpServer = htonl(INADDR_NONE);
232 }
233 Reply.GetAdapterInfo.LeaseObtained = Adapter->DhclientState.active->obtained;
234 Reply.GetAdapterInfo.LeaseExpires = Adapter->DhclientState.active->expiry;
235 } else {
236 Reply.GetAdapterInfo.DhcpServer = htonl(INADDR_NONE);
237 Reply.GetAdapterInfo.LeaseObtained = 0;
238 Reply.GetAdapterInfo.LeaseExpires = 0;
239 }
240 }
241
242 ApiUnlock();
243
244 return Send( &Reply );
245 }