-revert janderwalds change until because it breaks the gcc 4.x build
[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, Connection;
38
39 while( (Connection = ConnectNamedPipe( CommPipe, NULL )) ) {
40 Result = ReadFile( CommPipe, &Req, sizeof(Req), &BytesRead, NULL );
41 if( Result ) {
42 switch( Req.Type ) {
43 case DhcpReqQueryHWInfo:
44 BytesWritten = DSQueryHWInfo( PipeSend, &Req );
45 break;
46
47 case DhcpReqLeaseIpAddress:
48 BytesWritten = DSLeaseIpAddress( PipeSend, &Req );
49 break;
50
51 case DhcpReqReleaseIpAddress:
52 BytesWritten = DSReleaseIpAddressLease( PipeSend, &Req );
53 break;
54
55 case DhcpReqRenewIpAddress:
56 BytesWritten = DSRenewIpAddressLease( PipeSend, &Req );
57 break;
58
59 case DhcpReqStaticRefreshParams:
60 BytesWritten = DSStaticRefreshParams( PipeSend, &Req );
61 break;
62
63 case DhcpReqGetAdapterInfo:
64 BytesWritten = DSGetAdapterInfo( PipeSend, &Req );
65 break;
66
67 default:
68 DPRINT1("Unrecognized request type %d\n", Req.Type);
69 ZeroMemory( &Reply, sizeof( COMM_DHCP_REPLY ) );
70 Reply.Reply = 0;
71 BytesWritten = PipeSend( &Reply );
72 break;
73 }
74 }
75 DisconnectNamedPipe( CommPipe );
76 }
77
78 return TRUE;
79 }
80
81 HANDLE PipeInit() {
82 CommPipe = CreateNamedPipe
83 ( DHCP_PIPE_NAME,
84 PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
85 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
86 1,
87 COMM_PIPE_OUTPUT_BUFFER,
88 COMM_PIPE_INPUT_BUFFER,
89 COMM_PIPE_DEFAULT_TIMEOUT,
90 NULL );
91
92 if( CommPipe == INVALID_HANDLE_VALUE ) {
93 DbgPrint("DHCP: Could not create named pipe\n");
94 return CommPipe;
95 }
96
97 CommThread = CreateThread( NULL, 0, PipeThreadProc, NULL, 0, &CommThrId );
98
99 if( !CommThread ) {
100 CloseHandle( CommPipe );
101 CommPipe = INVALID_HANDLE_VALUE;
102 }
103
104 return CommPipe;
105 }
106
107 VOID PipeDestroy() {
108 CloseHandle( CommPipe );
109 CommPipe = INVALID_HANDLE_VALUE;
110 }