1 /*
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  */
19 
20 #ifndef _THRIFT_TRANSPORT_TSERVERWINPIPES_H_
21 #define _THRIFT_TRANSPORT_TSERVERWINPIPES_H_ 1
22 
23 #include <memory>
24 #include <thrift/transport/TServerTransport.h>
25 #ifndef _WIN32
26 #include <thrift/transport/TServerSocket.h>
27 #endif
28 
29 #define TPIPE_SERVER_MAX_CONNS_DEFAULT PIPE_UNLIMITED_INSTANCES
30 
31 // Windows - set security to allow non-elevated apps
32 // to access pipes created by elevated apps.
33 // Full access to everyone
34 const std::string DEFAULT_PIPE_SECURITY{"D:(A;;FA;;;WD)"};
35 
36 namespace apache {
37 namespace thrift {
38 namespace transport {
39 
40 /**
41  * Windows Pipes implementation of TServerTransport.
42  * Don't destroy a TPipeServer at global scope, as that will cause a thread join
43  * during DLLMain.  That also means that TServer's using TPipeServer shouldn't be at global
44  * scope.
45  */
46 #ifdef _WIN32
47 class TPipeServerImpl;
48 class TPipe;
49 
50 class TPipeServer : public TServerTransport {
51 public:
52   // Constructors
53   // Named Pipe -
54   TPipeServer(const std::string& pipename, uint32_t bufsize);
55   TPipeServer(const std::string& pipename, uint32_t bufsize, uint32_t maxconnections);
56   TPipeServer(const std::string& pipename,
57               uint32_t bufsize,
58               uint32_t maxconnections,
59               const std::string& securityDescriptor);
60   TPipeServer(const std::string& pipename);
61   // Anonymous pipe -
62   TPipeServer(int bufsize);
63   TPipeServer();
64 
65   // Destructor
66   virtual ~TPipeServer();
67 
68   bool isOpen() const override;
69 
70   // Standard transport callbacks
71   void interrupt() override;
72   void close() override;
73   void listen() override;
74 
75   // Accessors
76   std::string getPipename();
77   void setPipename(const std::string& pipename);
78   int getBufferSize();
79   void setBufferSize(int bufsize);
80   HANDLE getPipeHandle(); // Named Pipe R/W -or- Anonymous pipe Read handle
81   HANDLE getWrtPipeHandle();
82   HANDLE getClientRdPipeHandle();
83   HANDLE getClientWrtPipeHandle();
84   bool getAnonymous();
85   void setAnonymous(bool anon);
86   void setMaxConnections(uint32_t maxconnections);
87   void setSecurityDescriptor(const std::string& securityDescriptor);
88 
89   // this function is intended to be used in generic / template situations,
90   // so its name needs to be the same as TPipe's
91   HANDLE getNativeWaitHandle();
92 
93 protected:
94   virtual std::shared_ptr<TTransport> acceptImpl();
95 
96 private:
97   std::shared_ptr<TPipeServerImpl> impl_;
98 
99   std::string pipename_;
100   std::string securityDescriptor_;
101   uint32_t bufsize_;
102   uint32_t maxconns_;
103   bool isAnonymous_;
104 };
105 #else //_WIN32
106 //*NIX named pipe implementation uses domain socket
107 typedef TServerSocket TPipeServer;
108 #endif
109 }
110 }
111 } // apache::thrift::transport
112 
113 #endif // #ifndef _THRIFT_TRANSPORT_TSERVERWINPIPES_H_
114