1b31f99017b1b424cf85202539f5115ba28b9161
[reactos.git] / reactos / 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 static HANDLE CommPipe = INVALID_HANDLE_VALUE, CommThread;
13 DWORD CommThrId;
14
15 #define COMM_PIPE_OUTPUT_BUFFER sizeof(COMM_DHCP_REQ)
16 #define COMM_PIPE_INPUT_BUFFER sizeof(COMM_DHCP_REPLY)
17 #define COMM_PIPE_DEFAULT_TIMEOUT 1000
18
19 DWORD PipeSend( COMM_DHCP_REPLY *Reply ) {
20 DWORD Written = 0;
21 BOOL Success =
22 WriteFile( CommPipe,
23 Reply,
24 sizeof(*Reply),
25 &Written,
26 NULL );
27 return Success ? Written : -1;
28 }
29
30 DWORD WINAPI PipeThreadProc( LPVOID Parameter ) {
31 DWORD BytesRead, BytesWritten;
32 COMM_DHCP_REQ Req;
33 BOOL Result;
34 BOOLEAN Connection;
35
36 while( (Connection = ConnectNamedPipe( CommPipe, NULL )) ) {
37 Result = ReadFile( CommPipe, &Req, sizeof(Req), &BytesRead, NULL );
38 if( Result ) {
39 switch( Req.Type ) {
40 case DhcpReqQueryHWInfo:
41 BytesWritten = DSQueryHWInfo( PipeSend, &Req );
42 break;
43
44 case DhcpReqLeaseIpAddress:
45 BytesWritten = DSLeaseIpAddress( PipeSend, &Req );
46 break;
47
48 case DhcpReqReleaseIpAddress:
49 BytesWritten = DSReleaseIpAddressLease( PipeSend, &Req );
50 break;
51
52 case DhcpReqRenewIpAddress:
53 BytesWritten = DSRenewIpAddressLease( PipeSend, &Req );
54 break;
55 }
56 }
57 CloseHandle( CommPipe );
58 }
59
60 return TRUE;
61 }
62
63 HANDLE PipeInit() {
64 CommPipe = CreateNamedPipe
65 ( DHCP_PIPE_NAME,
66 PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
67 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
68 1,
69 COMM_PIPE_OUTPUT_BUFFER,
70 COMM_PIPE_INPUT_BUFFER,
71 COMM_PIPE_DEFAULT_TIMEOUT,
72 NULL );
73
74 if( CommPipe == INVALID_HANDLE_VALUE ) {
75 DbgPrint("DHCP: Could not create named pipe\n");
76 return CommPipe;
77 }
78
79 CommThread = CreateThread( NULL, 0, PipeThreadProc, NULL, 0, &CommThrId );
80
81 if( !CommThread ) {
82 CloseHandle( CommPipe );
83 CommPipe = INVALID_HANDLE_VALUE;
84 }
85
86 return CommPipe;
87 }
88
89 VOID PipeDestroy() {
90 CloseHandle( CommPipe );
91 CommPipe = INVALID_HANDLE_VALUE;
92 }