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