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