[DHCPSVC]: It's a service, move it in the correct directory.
[reactos.git] / reactos / base / services / dhcpcsvc / dhcp / pipe.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: subsys/system/dhcp/pipe.c
5 * PURPOSE: DHCP client pipe
6 * PROGRAMMER: arty
7 */
8
9 #include <rosdhcp.h>
10
11 #define NDEBUG
12 #include <reactos/debug.h>
13
14 static HANDLE CommPipe = INVALID_HANDLE_VALUE, CommThread;
15 DWORD CommThrId;
16
17 #define COMM_PIPE_OUTPUT_BUFFER sizeof(COMM_DHCP_REQ)
18 #define COMM_PIPE_INPUT_BUFFER sizeof(COMM_DHCP_REPLY)
19 #define COMM_PIPE_DEFAULT_TIMEOUT 1000
20
21 DWORD PipeSend( COMM_DHCP_REPLY *Reply ) {
22 DWORD Written = 0;
23 BOOL Success =
24 WriteFile( CommPipe,
25 Reply,
26 sizeof(*Reply),
27 &Written,
28 NULL );
29 return Success ? Written : -1;
30 }
31
32 DWORD WINAPI PipeThreadProc( LPVOID Parameter ) {
33 DWORD BytesRead;
34 COMM_DHCP_REQ Req;
35 COMM_DHCP_REPLY Reply;
36 BOOL Result, Connected;
37
38 while( TRUE ) {
39 Connected = ConnectNamedPipe( CommPipe, NULL ) ?
40 TRUE : GetLastError() == ERROR_PIPE_CONNECTED;
41
42 if (!Connected) {
43 DbgPrint("DHCP: Could not connect named pipe\n");
44 CloseHandle( CommPipe );
45 CommPipe = INVALID_HANDLE_VALUE;
46 break;
47 }
48
49 Result = ReadFile( CommPipe, &Req, sizeof(Req), &BytesRead, NULL );
50 if( Result ) {
51 switch( Req.Type ) {
52 case DhcpReqQueryHWInfo:
53 DSQueryHWInfo( PipeSend, &Req );
54 break;
55
56 case DhcpReqLeaseIpAddress:
57 DSLeaseIpAddress( PipeSend, &Req );
58 break;
59
60 case DhcpReqReleaseIpAddress:
61 DSReleaseIpAddressLease( PipeSend, &Req );
62 break;
63
64 case DhcpReqRenewIpAddress:
65 DSRenewIpAddressLease( PipeSend, &Req );
66 break;
67
68 case DhcpReqStaticRefreshParams:
69 DSStaticRefreshParams( PipeSend, &Req );
70 break;
71
72 case DhcpReqGetAdapterInfo:
73 DSGetAdapterInfo( PipeSend, &Req );
74 break;
75
76 default:
77 DPRINT1("Unrecognized request type %d\n", Req.Type);
78 ZeroMemory( &Reply, sizeof( COMM_DHCP_REPLY ) );
79 Reply.Reply = 0;
80 PipeSend( &Reply );
81 break;
82 }
83 }
84 DisconnectNamedPipe( CommPipe );
85 }
86
87 return TRUE;
88 }
89
90 HANDLE PipeInit() {
91 CommPipe = CreateNamedPipeW
92 ( DHCP_PIPE_NAME,
93 PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
94 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
95 1,
96 COMM_PIPE_OUTPUT_BUFFER,
97 COMM_PIPE_INPUT_BUFFER,
98 COMM_PIPE_DEFAULT_TIMEOUT,
99 NULL );
100
101 if( CommPipe == INVALID_HANDLE_VALUE ) {
102 DbgPrint("DHCP: Could not create named pipe\n");
103 return CommPipe;
104 }
105
106 CommThread = CreateThread( NULL, 0, PipeThreadProc, NULL, 0, &CommThrId );
107
108 if( !CommThread ) {
109 CloseHandle( CommPipe );
110 CommPipe = INVALID_HANDLE_VALUE;
111 }
112
113 return CommPipe;
114 }
115
116 VOID PipeDestroy() {
117 CloseHandle( CommPipe );
118 CommPipe = INVALID_HANDLE_VALUE;
119 }