blob: 76ed93bc0b08da1de4733826c1a4ed81919e9f1a [file] [log] [blame]
Roger Meier3bef8c22012-10-06 06:58:00 +00001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19unit Thrift.Transport.Pipes;
20
21{$WARN SYMBOL_PLATFORM OFF}
22
23interface
24
25uses
26 Windows, SysUtils, Math, AccCtrl, AclAPI,
27 Thrift.Transport,
28 Thrift.Console,
29 Thrift.Stream;
30
31const
32 DEFAULT_THRIFT_PIPE_TIMEOUT = 5 * 1000; // ms
33
34
35type
Roger Meier79655fb2012-10-20 20:59:41 +000036 //--- Pipe Streams ---
Roger Meier3bef8c22012-10-06 06:58:00 +000037
38
Roger Meier79655fb2012-10-20 20:59:41 +000039 TPipeStreamBaseImpl = class( TThriftStreamImpl)
40 strict protected
41 FPipe : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +000042 FTimeout : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +000043
Roger Meier3bef8c22012-10-06 06:58:00 +000044 procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
45 function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
Roger Meier79655fb2012-10-20 20:59:41 +000046 //procedure Open; override; - see derived classes
Roger Meier3bef8c22012-10-06 06:58:00 +000047 procedure Close; override;
48 procedure Flush; override;
49
50 function IsOpen: Boolean; override;
51 function ToArray: TBytes; override;
52 public
Roger Meier79655fb2012-10-20 20:59:41 +000053 constructor Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT);
Roger Meier3bef8c22012-10-06 06:58:00 +000054 destructor Destroy; override;
55 end;
56
57
Roger Meier79655fb2012-10-20 20:59:41 +000058 TNamedPipeStreamImpl = class sealed( TPipeStreamBaseImpl)
59 private
60 FPipeName : string;
61 FShareMode : DWORD;
62 FSecurityAttribs : PSecurityAttributes;
Roger Meier3bef8c22012-10-06 06:58:00 +000063
Roger Meier79655fb2012-10-20 20:59:41 +000064 protected
65 procedure Open; override;
66
67 public
68 constructor Create( const aPipeName : string;
69 const aShareMode: DWORD = 0;
70 const aSecurityAttributes: PSecurityAttributes = nil;
71 const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); overload;
72 end;
73
74
75 THandlePipeStreamImpl = class sealed( TPipeStreamBaseImpl)
76 private
77 FSrcHandle : THandle;
78
79 protected
80 procedure Open; override;
81
82 public
83 constructor Create( const aPipeHandle : THandle; aOwnsHandle : Boolean); overload;
84 destructor Destroy; override;
85 end;
86
87
88 //--- Pipe Transports ---
89
90
91 IPipe = interface( IStreamTransport)
92 ['{5E05CC85-434F-428F-BFB2-856A168B5558}']
93 end;
94
95
96 TPipeTransportBaseImpl = class( TStreamTransportImpl, IPipe)
97 public
98 // ITransport
99 function GetIsOpen: Boolean; override;
100 procedure Open; override;
101 procedure Close; override;
102 end;
103
104
105 TNamedPipeImpl = class( TPipeTransportBaseImpl)
106 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000107 // Named pipe constructors
108 constructor Create( aPipe : THandle; aOwnsHandle : Boolean); overload;
109 constructor Create( const aPipeName : string;
110 const aShareMode: DWORD = 0;
111 const aSecurityAttributes: PSecurityAttributes = nil;
112 const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000113 end;
114
115
Roger Meier79655fb2012-10-20 20:59:41 +0000116 TNamedPipeServerImpl = class( TNamedPipeImpl)
117 strict private
118 FHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000119 public
Roger Meier79655fb2012-10-20 20:59:41 +0000120 // ITransport
121 procedure Close; override;
122 constructor Create( aPipe : THandle; aOwnsHandle : Boolean); reintroduce;
123 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000124
Roger Meier79655fb2012-10-20 20:59:41 +0000125
126 TAnonymousPipeImpl = class( TPipeTransportBaseImpl)
127 public
Roger Meier3bef8c22012-10-06 06:58:00 +0000128 // Anonymous pipe constructor
129 constructor Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean); overload;
Roger Meier3bef8c22012-10-06 06:58:00 +0000130 end;
131
132
Roger Meier79655fb2012-10-20 20:59:41 +0000133 //--- Server Transports ---
134
135
136 IAnonymousServerPipe = interface( IServerTransport)
Roger Meier3bef8c22012-10-06 06:58:00 +0000137 ['{7AEE6793-47B9-4E49-981A-C39E9108E9AD}']
138 // Server side anonymous pipe ends
Roger Meier79655fb2012-10-20 20:59:41 +0000139 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000140 function WriteHandle : THandle;
141 // Client side anonymous pipe ends
142 function ClientAnonRead : THandle;
143 function ClientAnonWrite : THandle;
144 end;
145
146
Roger Meier79655fb2012-10-20 20:59:41 +0000147 INamedServerPipe = interface( IServerTransport)
148 ['{9DF9EE48-D065-40AF-8F67-D33037D3D960}']
149 function Handle : THandle;
150 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000151
Roger Meier79655fb2012-10-20 20:59:41 +0000152
153 TServerPipeBaseImpl = class( TServerTransportImpl)
154 public
155 procedure Listen; override;
156 end;
157
158
159 TAnonymousServerPipeImpl = class( TServerPipeBaseImpl, IAnonymousServerPipe)
160 private
161 FBufSize : DWORD;
162
163 // Server side anonymous pipe handles
164 FReadHandle,
Roger Meier3bef8c22012-10-06 06:58:00 +0000165 FWriteHandle : THandle;
166
167 //Client side anonymous pipe handles
168 FClientAnonRead,
169 FClientAnonWrite : THandle;
170
171 protected
172 function AcceptImpl: ITransport; override;
173
Roger Meier3bef8c22012-10-06 06:58:00 +0000174 function CreateAnonPipe : Boolean;
175
Roger Meier79655fb2012-10-20 20:59:41 +0000176 // IAnonymousServerPipe
177 function ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000178 function WriteHandle : THandle;
179 function ClientAnonRead : THandle;
180 function ClientAnonWrite : THandle;
181
182 public
Roger Meier79655fb2012-10-20 20:59:41 +0000183 constructor Create( aBufsize : Cardinal = 4096);
Roger Meier3bef8c22012-10-06 06:58:00 +0000184
Roger Meier3bef8c22012-10-06 06:58:00 +0000185 procedure Close; override;
186 end;
187
188
Roger Meier79655fb2012-10-20 20:59:41 +0000189 TNamedServerPipeImpl = class( TServerPipeBaseImpl, INamedServerPipe)
190 private
191 FPipeName : string;
192 FMaxConns : DWORD;
193 FBufSize : DWORD;
194
195 FHandle : THandle;
196
197 protected
198 function AcceptImpl: ITransport; override;
199
200 function CreateNamedPipe : Boolean;
201
202 // INamedServerPipe
203 function Handle : THandle;
204
205 public
206 constructor Create( aPipename : string; aBufsize : Cardinal = 4096;
207 aMaxConns : Cardinal = PIPE_UNLIMITED_INSTANCES);
208
209 procedure Close; override;
210 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000211
212
213implementation
214
215
Roger Meier79655fb2012-10-20 20:59:41 +0000216procedure ClosePipeHandle( var hPipe : THandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000217begin
Roger Meier79655fb2012-10-20 20:59:41 +0000218 if hPipe <> INVALID_HANDLE_VALUE
219 then try
220 CloseHandle( hPipe);
221 finally
222 hPipe := INVALID_HANDLE_VALUE;
223 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000224end;
225
226
Roger Meier79655fb2012-10-20 20:59:41 +0000227function DuplicatePipeHandle( const hSource : THandle) : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000228begin
Roger Meier79655fb2012-10-20 20:59:41 +0000229 if not DuplicateHandle( GetCurrentProcess, hSource,
230 GetCurrentProcess, @result,
231 0, FALSE, DUPLICATE_SAME_ACCESS)
232 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
233 'DuplicateHandle: '+SysErrorMessage(GetLastError));
Roger Meier3bef8c22012-10-06 06:58:00 +0000234end;
235
236
Roger Meier79655fb2012-10-20 20:59:41 +0000237
238{ TPipeStreamBaseImpl }
239
240
241constructor TPipeStreamBaseImpl.Create( const aTimeOut : DWORD = DEFAULT_THRIFT_PIPE_TIMEOUT);
242begin
243 inherited Create;
244 FPipe := INVALID_HANDLE_VALUE;
245 FTimeout := aTimeOut;
246end;
247
248
249destructor TPipeStreamBaseImpl.Destroy;
Roger Meier3bef8c22012-10-06 06:58:00 +0000250begin
251 try
252 Close;
253 finally
254 inherited Destroy;
255 end;
256end;
257
258
Roger Meier79655fb2012-10-20 20:59:41 +0000259procedure TPipeStreamBaseImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000260begin
Roger Meier79655fb2012-10-20 20:59:41 +0000261 ClosePipeHandle( FPipe);
Roger Meier3bef8c22012-10-06 06:58:00 +0000262end;
263
264
Roger Meier79655fb2012-10-20 20:59:41 +0000265procedure TPipeStreamBaseImpl.Flush;
Roger Meier3bef8c22012-10-06 06:58:00 +0000266begin
267 // nothing to do
268end;
269
270
Roger Meier79655fb2012-10-20 20:59:41 +0000271function TPipeStreamBaseImpl.IsOpen: Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000272begin
273 result := (FPipe <> INVALID_HANDLE_VALUE);
274end;
275
276
Roger Meier79655fb2012-10-20 20:59:41 +0000277procedure TPipeStreamBaseImpl.Write(const buffer: TBytes; offset, count: Integer);
Roger Meier3bef8c22012-10-06 06:58:00 +0000278var cbWritten : DWORD;
279begin
280 if not IsOpen
281 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
282 'Called write on non-open pipe');
283
284 if not WriteFile( FPipe, buffer[offset], count, cbWritten, nil)
285 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
286 'Write to pipe failed');
287end;
288
289
Roger Meier79655fb2012-10-20 20:59:41 +0000290function TPipeStreamBaseImpl.Read( var buffer: TBytes; offset, count: Integer): Integer;
291var cbRead, dwErr : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000292 bytes, retries : LongInt;
293 bOk : Boolean;
294const INTERVAL = 10; // ms
295begin
296 if not IsOpen
297 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
298 'Called read on non-open pipe');
299
300 // MSDN: Handle can be a handle to a named pipe instance,
301 // or it can be a handle to the read end of an anonymous pipe,
302 // The handle must have GENERIC_READ access to the pipe.
303 if FTimeOut <> INFINITE then begin
304 retries := Max( 1, Round( 1.0 * FTimeOut / INTERVAL));
305 while TRUE do begin
306 if IsOpen
307 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
308 and (bytes > 0)
309 then Break; // there are data
310
Roger Meier79655fb2012-10-20 20:59:41 +0000311 dwErr := GetLastError;
312 if (dwErr = ERROR_BROKEN_PIPE)
313 or (dwErr = ERROR_PIPE_NOT_CONNECTED)
314 then begin
315 result := 0; // other side closed the pipe
316 Exit;
317 end;
318
Roger Meier3bef8c22012-10-06 06:58:00 +0000319 Dec( retries);
320 if retries > 0
321 then Sleep( INTERVAL)
322 else raise TTransportException.Create( TTransportException.TExceptionType.TimedOut,
323 'Pipe read timed out');
324 end;
325 end;
326
327 // read the data (or block INFINITE-ly)
328 bOk := ReadFile( FPipe, buffer[offset], count, cbRead, nil);
329 if (not bOk) and (GetLastError() <> ERROR_MORE_DATA)
330 then result := 0 // No more data, possibly because client disconnected.
331 else result := cbRead;
332end;
333
334
Roger Meier79655fb2012-10-20 20:59:41 +0000335function TPipeStreamBaseImpl.ToArray: TBytes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000336var bytes : LongInt;
337begin
338 SetLength( result, 0);
339 bytes := 0;
340
341 if IsOpen
342 and PeekNamedPipe( FPipe, nil, 0, nil, @bytes, nil)
343 and (bytes > 0)
344 then begin
345 SetLength( result, bytes);
346 Read( result, 0, bytes);
347 end;
348end;
349
350
Roger Meier79655fb2012-10-20 20:59:41 +0000351{ TNamedPipeStreamImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000352
353
Roger Meier79655fb2012-10-20 20:59:41 +0000354constructor TNamedPipeStreamImpl.Create( const aPipeName : string; const aShareMode: DWORD;
355 const aSecurityAttributes: PSecurityAttributes;
356 const aTimeOut : DWORD);
Roger Meier3bef8c22012-10-06 06:58:00 +0000357begin
Roger Meier79655fb2012-10-20 20:59:41 +0000358 inherited Create( aTimeout);
359
360 FPipeName := aPipeName;
361 FShareMode := aShareMode;
362 FSecurityAttribs := aSecurityAttributes;
363
364 if Copy(FPipeName,1,2) <> '\\'
365 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
Roger Meier3bef8c22012-10-06 06:58:00 +0000366end;
367
368
Roger Meier79655fb2012-10-20 20:59:41 +0000369procedure TNamedPipeStreamImpl.Open;
370var hPipe : THandle;
371 dwMode : DWORD;
372begin
373 if IsOpen then Exit;
374
375 // open that thingy
376
377 if not WaitNamedPipe( PChar(FPipeName), FTimeout)
378 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
379 'Unable to open pipe, '+SysErrorMessage(GetLastError));
380
381 hPipe := CreateFile( PChar( FPipeName),
382 GENERIC_READ or GENERIC_WRITE,
383 FShareMode, // sharing
384 FSecurityAttribs, // security attributes
385 OPEN_EXISTING, // opens existing pipe
386 0, // default attributes
387 0); // no template file
388
389 if hPipe = INVALID_HANDLE_VALUE
390 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
391 'Unable to open pipe, '+SysErrorMessage(GetLastError));
392
393 // pipe connected; change to message-read mode.
394 dwMode := PIPE_READMODE_MESSAGE;
395 if not SetNamedPipeHandleState( hPipe, dwMode, nil, nil) then begin
396 Close;
397 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
398 'SetNamedPipeHandleState failed');
399 end;
400
401 // everything fine
402 FPipe := hPipe;
403end;
404
405
406{ THandlePipeStreamImpl }
407
408
409constructor THandlePipeStreamImpl.Create( const aPipeHandle : THandle; aOwnsHandle : Boolean);
410begin
411 inherited Create( DEFAULT_THRIFT_PIPE_TIMEOUT);
412
413 if aOwnsHandle
414 then FSrcHandle := aPipeHandle
415 else FSrcHandle := DuplicatePipeHandle( aPipeHandle);
416
417 Open;
418end;
419
420
421destructor THandlePipeStreamImpl.Destroy;
422begin
423 try
424 ClosePipeHandle( FSrcHandle);
425 finally
426 inherited Destroy;
427 end;
428end;
429
430
431procedure THandlePipeStreamImpl.Open;
432begin
433 if not IsOpen
434 then FPipe := DuplicatePipeHandle( FSrcHandle);
435end;
436
437
438{ TPipeTransportBaseImpl }
439
440
441function TPipeTransportBaseImpl.GetIsOpen: Boolean;
442begin
443 result := (FInputStream <> nil);
444end;
445
446
447procedure TPipeTransportBaseImpl.Open;
448begin
449 FInputStream.Open;
450 FOutputStream.Open;
451end;
452
453
454procedure TPipeTransportBaseImpl.Close;
455begin
456 FInputStream.Close;
457 FOutputStream.Close;
458end;
459
460
461{ TNamedPipeImpl }
462
463
Roger Meier3bef8c22012-10-06 06:58:00 +0000464constructor TNamedPipeImpl.Create( const aPipeName : string; const aShareMode: DWORD;
465 const aSecurityAttributes: PSecurityAttributes;
466 const aTimeOut : DWORD);
467// Named pipe constructor
468begin
Roger Meier79655fb2012-10-20 20:59:41 +0000469 inherited Create( nil, nil);
470 FInputStream := TNamedPipeStreamImpl.Create( aPipeName, aShareMode, aSecurityAttributes, aTimeOut);
Roger Meier3bef8c22012-10-06 06:58:00 +0000471 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000472end;
473
474
475constructor TNamedPipeImpl.Create( aPipe : THandle; aOwnsHandle : Boolean);
476// Named pipe constructor
477begin
Roger Meier79655fb2012-10-20 20:59:41 +0000478 inherited Create( nil, nil);
479 FInputStream := THandlePipeStreamImpl.Create( aPipe, aOwnsHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000480 FOutputStream := FInputStream; // true for named pipes
Roger Meier3bef8c22012-10-06 06:58:00 +0000481end;
482
483
Roger Meier79655fb2012-10-20 20:59:41 +0000484{ TNamedPipeServerImpl }
485
486
487constructor TNamedPipeServerImpl.Create( aPipe : THandle; aOwnsHandle : Boolean);
488// Named pipe constructor
Roger Meier3bef8c22012-10-06 06:58:00 +0000489begin
Roger Meier79655fb2012-10-20 20:59:41 +0000490 FHandle := DuplicatePipeHandle( aPipe);
491 inherited Create( aPipe, aOwnsHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000492end;
493
494
Roger Meier79655fb2012-10-20 20:59:41 +0000495procedure TNamedPipeServerImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000496begin
Roger Meier79655fb2012-10-20 20:59:41 +0000497 FlushFileBuffers( FHandle);
498 DisconnectNamedPipe( FHandle); // force client off the pipe
499 ClosePipeHandle( FHandle);
Roger Meier3bef8c22012-10-06 06:58:00 +0000500
Roger Meier79655fb2012-10-20 20:59:41 +0000501 inherited Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000502end;
503
504
505{ TAnonymousPipeImpl }
506
507
Roger Meier3bef8c22012-10-06 06:58:00 +0000508constructor TAnonymousPipeImpl.Create( const aPipeRead, aPipeWrite : THandle; aOwnsHandles : Boolean);
509// Anonymous pipe constructor
510begin
Roger Meier79655fb2012-10-20 20:59:41 +0000511 inherited Create( nil, nil);
512 FInputStream := THandlePipeStreamImpl.Create( aPipeRead, aOwnsHandles);
513 FOutputStream := THandlePipeStreamImpl.Create( aPipeWrite, aOwnsHandles);
Roger Meier3bef8c22012-10-06 06:58:00 +0000514end;
515
516
Roger Meier79655fb2012-10-20 20:59:41 +0000517{ TServerPipeBaseImpl }
518
519
520procedure TServerPipeBaseImpl.Listen;
Roger Meier3bef8c22012-10-06 06:58:00 +0000521begin
Roger Meier79655fb2012-10-20 20:59:41 +0000522 // not much to do here
Roger Meier3bef8c22012-10-06 06:58:00 +0000523end;
524
525
Roger Meier79655fb2012-10-20 20:59:41 +0000526{ TAnonymousServerPipeImpl }
Roger Meier3bef8c22012-10-06 06:58:00 +0000527
528
Roger Meier79655fb2012-10-20 20:59:41 +0000529constructor TAnonymousServerPipeImpl.Create( aBufsize : Cardinal);
Roger Meier3bef8c22012-10-06 06:58:00 +0000530// Anonymous pipe CTOR
531begin
532 inherited Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000533 FBufsize := aBufSize;
Roger Meier79655fb2012-10-20 20:59:41 +0000534 FReadHandle := INVALID_HANDLE_VALUE;
Roger Meier3bef8c22012-10-06 06:58:00 +0000535 FWriteHandle := INVALID_HANDLE_VALUE;
536 FClientAnonRead := INVALID_HANDLE_VALUE;
537 FClientAnonWrite := INVALID_HANDLE_VALUE;
538
539 // The anonymous pipe needs to be created first so that the server can
540 // pass the handles on to the client before the serve (acceptImpl)
541 // blocking call.
542 if not CreateAnonPipe
543 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
544 ClassName+'.Create() failed');
545end;
546
547
Roger Meier79655fb2012-10-20 20:59:41 +0000548function TAnonymousServerPipeImpl.AcceptImpl: ITransport;
Roger Meier3bef8c22012-10-06 06:58:00 +0000549var buf : Byte;
550 br : DWORD;
Roger Meier3bef8c22012-10-06 06:58:00 +0000551begin
Roger Meier79655fb2012-10-20 20:59:41 +0000552 // This 0-byte read serves merely as a blocking call.
553 if not ReadFile( FReadHandle, buf, 0, br, nil)
554 and (GetLastError() <> ERROR_MORE_DATA)
555 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
556 'TServerPipe unable to initiate pipe communication');
557 result := TAnonymousPipeImpl.Create( FReadHandle, FWriteHandle, FALSE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000558end;
559
560
Roger Meier79655fb2012-10-20 20:59:41 +0000561procedure TAnonymousServerPipeImpl.Close;
Roger Meier3bef8c22012-10-06 06:58:00 +0000562begin
Roger Meier79655fb2012-10-20 20:59:41 +0000563 ClosePipeHandle( FReadHandle);
564 ClosePipeHandle( FWriteHandle);
565 ClosePipeHandle( FClientAnonRead);
566 ClosePipeHandle( FClientAnonWrite);
Roger Meier3bef8c22012-10-06 06:58:00 +0000567end;
568
569
Roger Meier79655fb2012-10-20 20:59:41 +0000570function TAnonymousServerPipeImpl.ReadHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000571begin
Roger Meier79655fb2012-10-20 20:59:41 +0000572 result := FReadHandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000573end;
574
575
Roger Meier79655fb2012-10-20 20:59:41 +0000576function TAnonymousServerPipeImpl.WriteHandle : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000577begin
578 result := FWriteHandle;
579end;
580
581
Roger Meier79655fb2012-10-20 20:59:41 +0000582function TAnonymousServerPipeImpl.ClientAnonRead : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000583begin
584 result := FClientAnonRead;
585end;
586
587
Roger Meier79655fb2012-10-20 20:59:41 +0000588function TAnonymousServerPipeImpl.ClientAnonWrite : THandle;
Roger Meier3bef8c22012-10-06 06:58:00 +0000589begin
590 result := FClientAnonWrite;
591end;
592
593
Roger Meier79655fb2012-10-20 20:59:41 +0000594function TAnonymousServerPipeImpl.CreateAnonPipe : Boolean;
595var sd : PSECURITY_DESCRIPTOR;
596 sa : SECURITY_ATTRIBUTES; //TSecurityAttributes;
597 hCAR, hPipeW, hCAW, hPipe : THandle;
598begin
599 result := FALSE;
600
601 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
602 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
603 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, nil, FALSE));
604
605 sa.nLength := sizeof( sa);
606 sa.lpSecurityDescriptor := sd;
607 sa.bInheritHandle := TRUE; //allow passing handle to child
608
609 if not CreatePipe( hCAR, hPipeW, @sa, FBufSize) then begin //create stdin pipe
610 Console.WriteLine( 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
611 Exit;
612 end;
613
614 if not CreatePipe( hPipe, hCAW, @sa, FBufSize) then begin //create stdout pipe
615 Console.WriteLine( 'TServerPipe CreatePipe (anon) failed, '+SysErrorMessage(GetLastError));
616 CloseHandle( hCAR);
617 CloseHandle( hPipeW);
618 Exit;
619 end;
620
621 FClientAnonRead := hCAR;
622 FClientAnonWrite := hCAW;
623 FReadHandle := hPipe;
624 FWriteHandle := hPipeW;
625
626 result := TRUE;
627end;
628
629
630{ TNamedServerPipeImpl }
631
632
633constructor TNamedServerPipeImpl.Create( aPipename : string; aBufsize, aMaxConns : Cardinal);
634// Named Pipe CTOR
635begin
636 inherited Create;
637 FPipeName := aPipename;
638 FBufsize := aBufSize;
639 FMaxConns := Max( 1, Min( PIPE_UNLIMITED_INSTANCES, aMaxConns));
640 FHandle := INVALID_HANDLE_VALUE;
641
642 if Copy(FPipeName,1,2) <> '\\'
643 then FPipeName := '\\.\pipe\' + FPipeName; // assume localhost
644end;
645
646
647function TNamedServerPipeImpl.AcceptImpl: ITransport;
648var connectRet : Boolean;
649begin
650 if not CreateNamedPipe()
651 then raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
652 'TServerPipe CreateNamedPipe failed');
653
654 // Wait for the client to connect; if it succeeds, the
655 // function returns a nonzero value. If the function returns
656 // zero, GetLastError should return ERROR_PIPE_CONNECTED.
657 if ConnectNamedPipe( FHandle,nil)
658 then connectRet := TRUE
659 else connectRet := (GetLastError() = ERROR_PIPE_CONNECTED);
660
661 if not connectRet then begin
662 Close;
663 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
664 'TServerPipe: client connection failed');
665 end;
666
667 result := TNamedPipeServerImpl.Create( FHandle, TRUE);
668end;
669
670
671procedure TNamedServerPipeImpl.Close;
672begin
673 if FHandle <> INVALID_HANDLE_VALUE
674 then try
675 FlushFileBuffers( FHandle);
676 DisconnectNamedPipe( FHandle);
677 finally
678 ClosePipeHandle( FHandle);
679 end;
680end;
681
682
683function TNamedServerPipeImpl.Handle : THandle;
684begin
685 result := FHandle;
686end;
687
688
689function TNamedServerPipeImpl.CreateNamedPipe : Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000690var SIDAuthWorld : SID_IDENTIFIER_AUTHORITY ;
691 everyone_sid : PSID;
692 ea : EXPLICIT_ACCESS;
693 acl : PACL;
694 sd : PSECURITY_DESCRIPTOR;
695 sa : SECURITY_ATTRIBUTES;
696 hPipe : THandle;
697const
698 SECURITY_WORLD_SID_AUTHORITY : TSIDIdentifierAuthority = (Value : (0,0,0,0,0,1));
699 SECURITY_WORLD_RID = $00000000;
700begin
701 // Windows - set security to allow non-elevated apps
702 // to access pipes created by elevated apps.
703 SIDAuthWorld := SECURITY_WORLD_SID_AUTHORITY;
704 everyone_sid := nil;
705 AllocateAndInitializeSid( SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, everyone_sid);
706
707 ZeroMemory( @ea, SizeOf(ea));
708 ea.grfAccessPermissions := SPECIFIC_RIGHTS_ALL or STANDARD_RIGHTS_ALL;
709 ea.grfAccessMode := SET_ACCESS;
710 ea.grfInheritance := NO_INHERITANCE;
711 ea.Trustee.TrusteeForm := TRUSTEE_IS_SID;
712 ea.Trustee.TrusteeType := TRUSTEE_IS_WELL_KNOWN_GROUP;
713 ea.Trustee.ptstrName := PChar(everyone_sid);
714
715 acl := nil;
716 SetEntriesInAcl( 1, @ea, nil, acl);
717
718 sd := PSECURITY_DESCRIPTOR( LocalAlloc( LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH));
719 Win32Check( InitializeSecurityDescriptor( sd, SECURITY_DESCRIPTOR_REVISION));
720 Win32Check( SetSecurityDescriptorDacl( sd, TRUE, acl, FALSE));
721
722 sa.nLength := SizeOf(sa);
723 sa.lpSecurityDescriptor := sd;
724 sa.bInheritHandle := FALSE;
725
726 // Create an instance of the named pipe
727 hPipe := Windows.CreateNamedPipe( PChar( FPipeName), // pipe name
728 PIPE_ACCESS_DUPLEX, // read/write access
729 PIPE_TYPE_MESSAGE or // message type pipe
730 PIPE_READMODE_MESSAGE, // message-read mode
731 FMaxConns, // max. instances
732 FBufSize, // output buffer size
733 FBufSize, // input buffer size
734 0, // client time-out
735 @sa); // default security attribute
736
737 if( hPipe = INVALID_HANDLE_VALUE) then begin
738 FHandle := INVALID_HANDLE_VALUE;
739 raise TTransportException.Create( TTransportException.TExceptionType.NotOpen,
740 'CreateNamedPipe() failed ' + IntToStr(GetLastError));
741 end;
742
743 FHandle := hPipe;
744 result := TRUE;
745end;
746
747
Roger Meier3bef8c22012-10-06 06:58:00 +0000748
749end.
750
751
752