ca628bb56232bef891ef0b78134ad02ffebdf499
[reactos.git] / reactos / subsys / system / 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 <winsock2.h>
11 #include <iphlpapi.h>
12 #include "rosdhcp.h"
13
14 static CRITICAL_SECTION ApiCriticalSection;
15
16 VOID ApiInit() {
17 InitializeCriticalSection( &ApiCriticalSection );
18 }
19
20 VOID ApiLock() {
21 EnterCriticalSection( &ApiCriticalSection );
22 }
23
24 VOID ApiUnlock() {
25 LeaveCriticalSection( &ApiCriticalSection );
26 }
27
28 /* This represents the service portion of the DHCP client API */
29
30 DWORD DSLeaseIpAddress( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
31 COMM_DHCP_REPLY Reply;
32 PDHCP_ADAPTER Adapter;
33
34 ApiLock();
35
36 Adapter = AdapterFindIndex( Req->AdapterIndex );
37
38 Reply.Reply = Adapter ? 1 : 0;
39
40 if( Adapter ) {
41 add_protocol( Adapter->DhclientInfo.name,
42 Adapter->DhclientInfo.rfdesc, got_one,
43 &Adapter->DhclientInfo );
44 Adapter->DhclientInfo.client->state = S_INIT;
45 state_reboot(&Adapter->DhclientInfo);
46 }
47
48 ApiUnlock();
49
50 return Send( &Reply );
51 }
52
53 DWORD DSQueryHWInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
54 COMM_DHCP_REPLY Reply;
55 PDHCP_ADAPTER Adapter;
56
57 ApiLock();
58
59 Adapter = AdapterFindIndex( Req->AdapterIndex );
60
61 Reply.QueryHWInfo.AdapterIndex = Req->AdapterIndex;
62 Reply.QueryHWInfo.MediaType = Adapter->IfMib.dwType;
63 Reply.QueryHWInfo.Mtu = Adapter->IfMib.dwMtu;
64 Reply.QueryHWInfo.Speed = Adapter->IfMib.dwSpeed;
65
66 ApiUnlock();
67
68 return Send( &Reply );
69 }
70
71 DWORD DSReleaseIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
72 COMM_DHCP_REPLY Reply;
73 PDHCP_ADAPTER Adapter;
74
75 ApiLock();
76
77 Adapter = AdapterFindIndex( Req->AdapterIndex );
78
79 Reply.Reply = Adapter ? 1 : 0;
80
81 if( Adapter ) {
82 DeleteIPAddress( Adapter->NteContext );
83 remove_protocol( find_protocol_by_adapter( &Adapter->DhclientInfo ) );
84 }
85
86 ApiUnlock();
87
88 return Send( &Reply );
89 }
90
91 DWORD DSRenewIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
92 COMM_DHCP_REPLY Reply;
93 PDHCP_ADAPTER Adapter;
94
95 ApiLock();
96
97 Adapter = AdapterFindIndex( Req->AdapterIndex );
98
99 Reply.Reply = Adapter ? 1 : 0;
100
101 if( !Adapter || Adapter->DhclientState.state != S_BOUND ) {
102 Reply.Reply = 0;
103 return Send( &Reply );
104 }
105
106 Adapter->DhclientState.state = S_BOUND;
107
108 send_discover( &Adapter->DhclientInfo );
109 state_bound( &Adapter->DhclientInfo );
110
111 ApiUnlock();
112
113 return Send( &Reply );
114 }
115
116 DWORD DSStaticRefreshParams( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
117 NTSTATUS Status;
118 COMM_DHCP_REPLY Reply;
119 PDHCP_ADAPTER Adapter;
120
121 ApiLock();
122
123 Adapter = AdapterFindIndex( Req->AdapterIndex );
124
125 Reply.Reply = Adapter ? 1 : 0;
126
127 if( Adapter ) {
128 DeleteIPAddress( Adapter->NteContext );
129 Adapter->DhclientState.state = S_STATIC;
130 remove_protocol( find_protocol_by_adapter( &Adapter->DhclientInfo ) );
131 Status = AddIPAddress( Req->Body.StaticRefreshParams.IPAddress,
132 Req->Body.StaticRefreshParams.Netmask,
133 Req->AdapterIndex,
134 &Adapter->NteContext,
135 &Adapter->NteInstance );
136 Reply.Reply = NT_SUCCESS(Status);
137 }
138
139 ApiUnlock();
140
141 return Send( &Reply );
142 }