blob: e57fdd44ba81b061934723cbacb28c4ace57415f [file] [log] [blame] [view]
Mark Slee54b7ab92007-03-06 00:06:27 +00001Thrift C++ Software Library
2
Roger Meier6370cfd2014-05-04 22:21:58 +02003# License
Bryan Duxburydef30a62009-04-08 00:19:37 +00004
5Licensed to the Apache Software Foundation (ASF) under one
6or more contributor license agreements. See the NOTICE file
7distributed with this work for additional information
8regarding copyright ownership. The ASF licenses this file
9to you under the Apache License, Version 2.0 (the
10"License"); you may not use this file except in compliance
11with the License. You may obtain a copy of the License at
12
13 http://www.apache.org/licenses/LICENSE-2.0
14
15Unless required by applicable law or agreed to in writing,
16software distributed under the License is distributed on an
17"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18KIND, either express or implied. See the License for the
19specific language governing permissions and limitations
20under the License.
Mark Slee54b7ab92007-03-06 00:06:27 +000021
Roger Meier6370cfd2014-05-04 22:21:58 +020022
23# Using Thrift with C++
Mark Slee54b7ab92007-03-06 00:06:27 +000024
25The Thrift C++ libraries are built using the GNU tools. Follow the instructions
Roger Meier6370cfd2014-05-04 22:21:58 +020026in the top-level README.md
Mark Slee54b7ab92007-03-06 00:06:27 +000027
Roger Meier6370cfd2014-05-04 22:21:58 +020028In case you do not want to open another README.md file, do this thrift src:
29
30 ./bootstrap.sh
31 ./configure (--with-boost=/usr/local)
32 make
33 sudo make install
Mark Slee54b7ab92007-03-06 00:06:27 +000034
35Thrift is divided into two libraries.
36
Roger Meier6370cfd2014-05-04 22:21:58 +020037* libthrift - The core Thrift library contains all the core Thrift code. It requires
Mark Slee54b7ab92007-03-06 00:06:27 +000038 boost shared pointers, pthreads, and librt.
39
Roger Meier6370cfd2014-05-04 22:21:58 +020040* libthriftnb - This library contains the Thrift nonblocking server, which uses libevent.
Mark Slee54b7ab92007-03-06 00:06:27 +000041 To link this library you will also need to link libevent.
42
Roger Meier6370cfd2014-05-04 22:21:58 +020043## Linking Against Thrift
Mark Slee54b7ab92007-03-06 00:06:27 +000044
45After you build and install Thrift the libraries are installed to
46/usr/local/lib by default. Make sure this is in your LDPATH.
47
48On Linux, the best way to do this is to ensure that /usr/local/lib is in
49your /etc/ld.so.conf and then run /sbin/ldconfig.
50
51Depending upon whether you are linking dynamically or statically and how
52your build environment it set up, you may need to include additional
53libraries when linking against thrift, such as librt and/or libpthread. If
54you are using libthriftnb you will also need libevent.
55
Roger Meier6370cfd2014-05-04 22:21:58 +020056## Dependencies
Mark Slee54b7ab92007-03-06 00:06:27 +000057
58boost shared pointers
59http://www.boost.org/libs/smart_ptr/smart_ptr.htm
60
61libevent (for libthriftnb only)
62http://monkey.org/~provos/libevent/
Roger Meier6370cfd2014-05-04 22:21:58 +020063
64# Using Thrift with C++ on Windows
65
66You need to define an enviroment variable called THIRD_PARTY. The project
67assumes that you have extracted the dependancies into their default structure
68into the path defined by THIRD_PARTY.
69
70e.g. $(THIRD_PARTY)/boost/boost_1_47_0/
71
72Thrift is divided into two libraries.
73
74* libthrift - The core Thrift library contains all the core Thrift code. It requires
75 boost shared pointers, pthreads, and librt.
76
77* libthriftnb - This library contains the Thrift nonblocking server, which uses libevent.
78 To link this library you will also need to link libevent.
79
80## Linking Against Thrift
81
82You need to link your project that uses thrift against all the thrift
83dependancies; in the case of libthrift, boost and for
84libthriftnb, libevent.
85
86In the project properties you must also set HAVE_CONFIG_H as force include
87the config header: "windows/confg.h"
88
89## Dependencies
90
91boost shared pointers
92http://www.boost.org/libs/smart_ptr/smart_ptr.htm
93
94boost thread
95http://www.boost.org/doc/libs/release/doc/html/thread.html
96
97libevent (for libthriftnb only)
98http://monkey.org/~provos/libevent/
99
100## Notes on boost thread (static vs shared):
101
102By default lib/cpp/windows/force_inc.h defines:
103
104 #define BOOST_ALL_NO_LIB 1
105 #define BOOST_THREAD_NO_LIB 1
106
107This has for effect to have the host application linking against Thrift
108to have to link with boost thread as a static library.
109
110If you wanted instead to link with boost thread as a shared library,
111you'll need to uncomment those two lines, and recompile.
112
113## Windows version compatibility
114
115The Thrift library targets Windows XP for broadest compatbility. A notable
116difference is in the Windows-specific implementation of the socket poll
117function. To target Vista, Win7 or other versions, comment out the line
118
119 #define TARGET_WIN_XP.
120
121## Named Pipes
122
123Named Pipe transport has been added in the TPipe and TPipeServer classes. This
124is currently Windows-only. Named pipe transport for *NIX has not been
125implemented. Domain sockets are a better choice for local IPC under non-Windows
126OS's. *NIX named pipes only support 1:1 client-server connection.
127
128# Thrift/SSL
129
1301. Scope
131
132 This SSL only supports blocking mode socket I/O. It can only be used with
133 TSimpleServer, TThreadedServer, and TThreadPoolServer.
134
1352. Implementation
136
137 There're two main classes TSSLSocketFactory and TSSLSocket. Instances of
138 TSSLSocket are always created from TSSLSocketFactory.
139
140 PosixSSLThreadFactory creates PosixSSLThread. The only difference from the
141 PthreadThread type is that it cleanups OpenSSL error queue upon exiting
142 the thread. Ideally, OpenSSL APIs should only be called from PosixSSLThread.
143
1443. How to use SSL APIs
145
146 // This is for demo. In real code, typically only one TSSLSocketFactory
147 // instance is needed.
148 shared_ptr<TSSLSocketFactory> getSSLSocketFactory() {
149 shared_ptr<TSSLSocketFactory> factory(new TSSLSocketFactory());
150 // client: load trusted certificates
151 factory->loadTrustedCertificates("my-trusted-ca-certificates.pem");
152 // client: optionally set your own access manager, otherwise,
153 // the default client access manager will be loaded.
154
155 factory->loadCertificate("my-certificate-signed-by-ca.pem");
156 factory->loadPrivateKey("my-private-key.pem");
157 // server: optionally setup access manager
158 // shared_ptr<AccessManager> accessManager(new MyAccessManager);
159 // factory->access(accessManager);
160 ...
161 }
162
163
164 // client code sample
165 shared_ptr<TSSLSocketFactory> factory = getSSLSocketFactory();
166 shared_ptr<TSocket> socket = factory.createSocket(host, port);
167 shared_ptr<TBufferedTransport> transport(new TBufferedTransport(socket));
168 ...
169
170
171 // server code sample
172 shared_ptr<TSSLSocketFactory> factory = getSSLSocketFactory();
173 shared_ptr<TSSLServerSocket> socket(new TSSLServerSocket(port, factory));
174 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory));
175 ...
176
1774. AccessManager
178
179 AccessManager defines a callback interface. It has three callback methods:
180
181 (a) Decision verify(const sockaddr_storage& sa);
182 (b) Decision verify(const string& host, const char* name, int size);
183 (c) Decision verify(const sockaddr_storage& sa, const char* data, int size);
184
185 After SSL handshake completes, additional checks are conducted. Application
186 is given the chance to decide whether or not to continue the conversation
187 with the remote. Application is queried through the above three "verify"
188 method. They are called at different points of the verification process.
189
190 Decisions can be one of ALLOW, DENY, and SKIP. ALLOW and DENY means the
191 conversation should be continued or disconnected, respectively. ALLOW and
192 DENY decision stops the verification process. SKIP means there's no decision
193 based on the given input, continue the verification process.
194
195 First, (a) is called with the remote IP. It is called once at the beginning.
196 "sa" is the IP address of the remote peer.
197
198 Then, the certificate of remote peer is loaded. SubjectAltName extensions
199 are extracted and sent to application for verification. When a DNS
200 subjectAltName field is extracted, (b) is called. When an IP subjectAltName
201 field is extracted, (c) is called.
202
203 The "host" in (b) is the value from TSocket::getHost() if this is a client
204 side socket, or TSocket::getPeerHost() if this is a server side socket. The
205 reason is client side socket initiates the connection. TSocket::getHost()
206 is the remote host name. On server side, the remote host name is unknown
207 unless it's retrieved through TSocket::getPeerHost(). Either way, "host"
208 should be the remote host name. Keep in mind, if TSocket::getPeerHost()
209 failed, it would return the remote host name in numeric format.
210
211 If all subjectAltName extensions were "skipped", the common name field would
212 be checked. It is sent to application through (c), where "sa" is the remote
213 IP address. "data" is the IP address extracted from subjectAltName IP
214 extension, and "size" is the length of the extension data.
215
216 If any of the above "verify" methods returned a decision ALLOW or DENY, the
217 verification process would be stopped.
218
219 If any of the above "verify" methods returned SKIP, that decision would be
220 ignored and the verification process would move on till the last item is
221 examined. At that point, if there's still no decision, the connection is
222 terminated.
223
224 Thread safety, an access manager should not store state information if it's
225 to be used by many SSL sockets.
226
2275. SIGPIPE signal
228
229 Applications running OpenSSL over network connections may crash if SIGPIPE
230 is not ignored. This happens when they receive a connection reset by remote
231 peer exception, which somehow triggers a SIGPIPE signal. If not handled,
232 this signal would kill the application.
233
2346. How to run test client/server in SSL mode
235
236 The server and client expects the followings from the directory /test/
237
238 - keys/server.crt
239 - keys/server.key
240 - keys/CA.pem
241
242 The file names are hard coded in the source code. You need to create these
243 certificates before you can run the test code in SSL mode. Make sure at least
244 one of the followings is included in "keys/server.crt",
245
246 - subjectAltName, DNS localhost
247 - subjectAltName, IP 127.0.0.1
248 - common name, localhost
249
250 Run within /test/ folder,
251
252 ./cpp/TestServer --ssl &
253 ./cpp/TestClient --ssl
254
255 If "-h <host>" is used to run client, the above "localhost" in the above
256 server-certificate.pem has to be replaced with that host name.
257
2587. TSSLSocketFactory::randomize()
259
260 The default implementation of OpenSSLSocketFactory::randomize() simply calls
261 OpenSSL's RAND_poll() when OpenSSL library is first initialized.
262
263 The PRNG seed is key to the application security. This method should be
264 overridden if it's not strong enough for you.