7623be382ac6dc38e158c6e822748816027c1426
[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 VOID ApiInit() {
18 InitializeCriticalSection( &ApiCriticalSection );
19 }
20
21 VOID ApiLock() {
22 EnterCriticalSection( &ApiCriticalSection );
23 }
24
25 VOID ApiUnlock() {
26 LeaveCriticalSection( &ApiCriticalSection );
27 }
28
29 VOID ApiFree() {
30 DeleteCriticalSection( &ApiCriticalSection );
31 }
32
33 /* This represents the service portion of the DHCP client API */
34
35 DWORD DSLeaseIpAddress( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
36 COMM_DHCP_REPLY Reply;
37 PDHCP_ADAPTER Adapter;
38
39 ApiLock();
40
41 Adapter = AdapterFindIndex( Req->AdapterIndex );
42
43 Reply.Reply = Adapter ? 1 : 0;
44
45 if( Adapter ) {
46 add_protocol( Adapter->DhclientInfo.name,
47 Adapter->DhclientInfo.rfdesc, got_one,
48 &Adapter->DhclientInfo );
49 Adapter->DhclientInfo.client->state = S_INIT;
50 state_reboot(&Adapter->DhclientInfo);
51 }
52
53 ApiUnlock();
54
55 return Send( &Reply );
56 }
57
58 DWORD DSQueryHWInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
59 COMM_DHCP_REPLY Reply;
60 PDHCP_ADAPTER Adapter;
61
62 ApiLock();
63
64 Adapter = AdapterFindIndex( Req->AdapterIndex );
65
66 Reply.Reply = Adapter ? 1 : 0;
67
68 if (Adapter) {
69 Reply.QueryHWInfo.AdapterIndex = Req->AdapterIndex;
70 Reply.QueryHWInfo.MediaType = Adapter->IfMib.dwType;
71 Reply.QueryHWInfo.Mtu = Adapter->IfMib.dwMtu;
72 Reply.QueryHWInfo.Speed = Adapter->IfMib.dwSpeed;
73 }
74
75 ApiUnlock();
76
77 return Send( &Reply );
78 }
79
80 DWORD DSReleaseIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
81 COMM_DHCP_REPLY Reply;
82 PDHCP_ADAPTER Adapter;
83 struct protocol* proto;
84
85 ApiLock();
86
87 Adapter = AdapterFindIndex( Req->AdapterIndex );
88
89 Reply.Reply = Adapter ? 1 : 0;
90
91 if( Adapter ) {
92 if (Adapter->NteContext)
93 DeleteIPAddress( Adapter->NteContext );
94
95 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
96 if (proto)
97 remove_protocol(proto);
98 }
99
100 ApiUnlock();
101
102 return Send( &Reply );
103 }
104
105 DWORD DSRenewIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
106 COMM_DHCP_REPLY Reply;
107 PDHCP_ADAPTER Adapter;
108
109 ApiLock();
110
111 Adapter = AdapterFindIndex( Req->AdapterIndex );
112
113 if( !Adapter || Adapter->DhclientState.state == S_STATIC ) {
114 Reply.Reply = 0;
115 ApiUnlock();
116 return Send( &Reply );
117 }
118
119 Reply.Reply = 1;
120
121 add_protocol( Adapter->DhclientInfo.name,
122 Adapter->DhclientInfo.rfdesc, got_one,
123 &Adapter->DhclientInfo );
124 Adapter->DhclientInfo.client->state = S_INIT;
125 state_reboot(&Adapter->DhclientInfo);
126
127 ApiUnlock();
128
129 return Send( &Reply );
130 }
131
132 DWORD DSStaticRefreshParams( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
133 NTSTATUS Status;
134 COMM_DHCP_REPLY Reply;
135 PDHCP_ADAPTER Adapter;
136 struct protocol* proto;
137
138 ApiLock();
139
140 Adapter = AdapterFindIndex( Req->AdapterIndex );
141
142 Reply.Reply = Adapter ? 1 : 0;
143
144 if( Adapter ) {
145 if (Adapter->NteContext)
146 DeleteIPAddress( Adapter->NteContext );
147 Adapter->DhclientState.state = S_STATIC;
148 proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
149 if (proto)
150 remove_protocol(proto);
151 Status = AddIPAddress( Req->Body.StaticRefreshParams.IPAddress,
152 Req->Body.StaticRefreshParams.Netmask,
153 Req->AdapterIndex,
154 &Adapter->NteContext,
155 &Adapter->NteInstance );
156 Reply.Reply = NT_SUCCESS(Status);
157 }
158
159 ApiUnlock();
160
161 return Send( &Reply );
162 }
163
164 DWORD DSGetAdapterInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
165 COMM_DHCP_REPLY Reply;
166 PDHCP_ADAPTER Adapter;
167
168 ApiLock();
169
170 Adapter = AdapterFindIndex( Req->AdapterIndex );
171
172 Reply.Reply = Adapter ? 1 : 0;
173
174 if( Adapter ) {
175 Reply.GetAdapterInfo.DhcpEnabled = (S_STATIC != Adapter->DhclientState.state);
176 if (S_BOUND == Adapter->DhclientState.state) {
177 if (sizeof(Reply.GetAdapterInfo.DhcpServer) ==
178 Adapter->DhclientState.active->serveraddress.len) {
179 memcpy(&Reply.GetAdapterInfo.DhcpServer,
180 Adapter->DhclientState.active->serveraddress.iabuf,
181 Adapter->DhclientState.active->serveraddress.len);
182 } else {
183 DPRINT1("Unexpected server address len %d\n",
184 Adapter->DhclientState.active->serveraddress.len);
185 Reply.GetAdapterInfo.DhcpServer = htonl(INADDR_NONE);
186 }
187 Reply.GetAdapterInfo.LeaseObtained = Adapter->DhclientState.active->obtained;
188 Reply.GetAdapterInfo.LeaseExpires = Adapter->DhclientState.active->expiry;
189 } else {
190 Reply.GetAdapterInfo.DhcpServer = htonl(INADDR_NONE);
191 Reply.GetAdapterInfo.LeaseObtained = 0;
192 Reply.GetAdapterInfo.LeaseExpires = 0;
193 }
194 }
195
196 ApiUnlock();
197
198 return Send( &Reply );
199 }