blob: c578223fd1be697482e82e8d162ae6085b372137 [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
Roger Meier4fba9d22014-05-04 22:34:44 +0200130## Scope
Roger Meier6370cfd2014-05-04 22:21:58 +0200131
Roger Meier4fba9d22014-05-04 22:34:44 +0200132This SSL only supports blocking mode socket I/O. It can only be used with
133TSimpleServer, TThreadedServer, and TThreadPoolServer.
Roger Meier6370cfd2014-05-04 22:21:58 +0200134
Roger Meier4fba9d22014-05-04 22:34:44 +0200135## Implementation
Roger Meier6370cfd2014-05-04 22:21:58 +0200136
Roger Meier4fba9d22014-05-04 22:34:44 +0200137There're two main classes TSSLSocketFactory and TSSLSocket. Instances of
138TSSLSocket are always created from TSSLSocketFactory.
Roger Meier6370cfd2014-05-04 22:21:58 +0200139
Roger Meier4fba9d22014-05-04 22:34:44 +0200140PosixSSLThreadFactory creates PosixSSLThread. The only difference from the
141PthreadThread type is that it cleanups OpenSSL error queue upon exiting
142the thread. Ideally, OpenSSL APIs should only be called from PosixSSLThread.
Roger Meier6370cfd2014-05-04 22:21:58 +0200143
Roger Meier4fba9d22014-05-04 22:34:44 +0200144## How to use SSL APIs
Roger Meier6370cfd2014-05-04 22:21:58 +0200145
Roger Meier4fba9d22014-05-04 22:34:44 +0200146This is for demo. In real code, typically only one TSSLSocketFactory
147instance is needed.
Roger Meier6370cfd2014-05-04 22:21:58 +0200148
Roger Meier4fba9d22014-05-04 22:34:44 +0200149 shared_ptr<TSSLSocketFactory> getSSLSocketFactory() {
150 shared_ptr<TSSLSocketFactory> factory(new TSSLSocketFactory());
151 // client: load trusted certificates
152 factory->loadTrustedCertificates("my-trusted-ca-certificates.pem");
153 // client: optionally set your own access manager, otherwise,
154 // the default client access manager will be loaded.
155
156 factory->loadCertificate("my-certificate-signed-by-ca.pem");
157 factory->loadPrivateKey("my-private-key.pem");
158 // server: optionally setup access manager
159 // shared_ptr<AccessManager> accessManager(new MyAccessManager);
160 // factory->access(accessManager);
161 ...
162 }
Roger Meier6370cfd2014-05-04 22:21:58 +0200163
164
Roger Meier4fba9d22014-05-04 22:34:44 +0200165client code sample
166
167 shared_ptr<TSSLSocketFactory> factory = getSSLSocketFactory();
168 shared_ptr<TSocket> socket = factory.createSocket(host, port);
169 shared_ptr<TBufferedTransport> transport(new TBufferedTransport(socket));
170 ...
Roger Meier6370cfd2014-05-04 22:21:58 +0200171
172
Roger Meier4fba9d22014-05-04 22:34:44 +0200173server code sample
Roger Meier6370cfd2014-05-04 22:21:58 +0200174
Roger Meier4fba9d22014-05-04 22:34:44 +0200175 shared_ptr<TSSLSocketFactory> factory = getSSLSocketFactory();
176 shared_ptr<TSSLServerSocket> socket(new TSSLServerSocket(port, factory));
177 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory));
178 ...
Roger Meier6370cfd2014-05-04 22:21:58 +0200179
Roger Meier4fba9d22014-05-04 22:34:44 +0200180## AccessManager
Roger Meier6370cfd2014-05-04 22:21:58 +0200181
Roger Meier4fba9d22014-05-04 22:34:44 +0200182AccessManager defines a callback interface. It has three callback methods:
Roger Meier6370cfd2014-05-04 22:21:58 +0200183
Roger Meier4fba9d22014-05-04 22:34:44 +0200184(a) Decision verify(const sockaddr_storage& sa);
Roger Meier6370cfd2014-05-04 22:21:58 +0200185
Roger Meier4fba9d22014-05-04 22:34:44 +0200186(b) Decision verify(const string& host, const char* name, int size);
Roger Meier6370cfd2014-05-04 22:21:58 +0200187
Roger Meier4fba9d22014-05-04 22:34:44 +0200188(c) Decision verify(const sockaddr_storage& sa, const char* data, int size);
Roger Meier6370cfd2014-05-04 22:21:58 +0200189
Roger Meier4fba9d22014-05-04 22:34:44 +0200190After SSL handshake completes, additional checks are conducted. Application
191is given the chance to decide whether or not to continue the conversation
192with the remote. Application is queried through the above three "verify"
193method. They are called at different points of the verification process.
Roger Meier6370cfd2014-05-04 22:21:58 +0200194
Roger Meier4fba9d22014-05-04 22:34:44 +0200195Decisions can be one of ALLOW, DENY, and SKIP. ALLOW and DENY means the
196conversation should be continued or disconnected, respectively. ALLOW and
197DENY decision stops the verification process. SKIP means there's no decision
198based on the given input, continue the verification process.
Roger Meier6370cfd2014-05-04 22:21:58 +0200199
Roger Meier4fba9d22014-05-04 22:34:44 +0200200First, (a) is called with the remote IP. It is called once at the beginning.
201"sa" is the IP address of the remote peer.
Roger Meier6370cfd2014-05-04 22:21:58 +0200202
Roger Meier4fba9d22014-05-04 22:34:44 +0200203Then, the certificate of remote peer is loaded. SubjectAltName extensions
204are extracted and sent to application for verification. When a DNS
205subjectAltName field is extracted, (b) is called. When an IP subjectAltName
206field is extracted, (c) is called.
Roger Meier6370cfd2014-05-04 22:21:58 +0200207
Roger Meier4fba9d22014-05-04 22:34:44 +0200208The "host" in (b) is the value from TSocket::getHost() if this is a client
209side socket, or TSocket::getPeerHost() if this is a server side socket. The
210reason is client side socket initiates the connection. TSocket::getHost()
211is the remote host name. On server side, the remote host name is unknown
212unless it's retrieved through TSocket::getPeerHost(). Either way, "host"
213should be the remote host name. Keep in mind, if TSocket::getPeerHost()
214failed, it would return the remote host name in numeric format.
Roger Meier6370cfd2014-05-04 22:21:58 +0200215
Roger Meier4fba9d22014-05-04 22:34:44 +0200216If all subjectAltName extensions were "skipped", the common name field would
217be checked. It is sent to application through (c), where "sa" is the remote
218IP address. "data" is the IP address extracted from subjectAltName IP
219extension, and "size" is the length of the extension data.
Roger Meier6370cfd2014-05-04 22:21:58 +0200220
Roger Meier4fba9d22014-05-04 22:34:44 +0200221If any of the above "verify" methods returned a decision ALLOW or DENY, the
222verification process would be stopped.
Roger Meier6370cfd2014-05-04 22:21:58 +0200223
Roger Meier4fba9d22014-05-04 22:34:44 +0200224If any of the above "verify" methods returned SKIP, that decision would be
225ignored and the verification process would move on till the last item is
226examined. At that point, if there's still no decision, the connection is
227terminated.
Roger Meier6370cfd2014-05-04 22:21:58 +0200228
Roger Meier4fba9d22014-05-04 22:34:44 +0200229Thread safety, an access manager should not store state information if it's
230to be used by many SSL sockets.
Roger Meier6370cfd2014-05-04 22:21:58 +0200231
Roger Meier4fba9d22014-05-04 22:34:44 +0200232## SIGPIPE signal
Roger Meier6370cfd2014-05-04 22:21:58 +0200233
Roger Meier4fba9d22014-05-04 22:34:44 +0200234Applications running OpenSSL over network connections may crash if SIGPIPE
235is not ignored. This happens when they receive a connection reset by remote
236peer exception, which somehow triggers a SIGPIPE signal. If not handled,
237this signal would kill the application.
Roger Meier6370cfd2014-05-04 22:21:58 +0200238
Roger Meier4fba9d22014-05-04 22:34:44 +0200239## How to run test client/server in SSL mode
Roger Meier6370cfd2014-05-04 22:21:58 +0200240
Roger Meier4fba9d22014-05-04 22:34:44 +0200241The server and client expects the followings from the directory /test/
Roger Meier6370cfd2014-05-04 22:21:58 +0200242
Roger Meier4fba9d22014-05-04 22:34:44 +0200243- keys/server.crt
244- keys/server.key
245- keys/CA.pem
Roger Meier6370cfd2014-05-04 22:21:58 +0200246
Roger Meier4fba9d22014-05-04 22:34:44 +0200247The file names are hard coded in the source code. You need to create these
248certificates before you can run the test code in SSL mode. Make sure at least
249one of the followings is included in "keys/server.crt",
Roger Meier6370cfd2014-05-04 22:21:58 +0200250
Roger Meier4fba9d22014-05-04 22:34:44 +0200251- subjectAltName, DNS localhost
252- subjectAltName, IP 127.0.0.1
253- common name, localhost
Roger Meier6370cfd2014-05-04 22:21:58 +0200254
Roger Meier4fba9d22014-05-04 22:34:44 +0200255Run within /test/ folder,
Roger Meier6370cfd2014-05-04 22:21:58 +0200256
Roger Meier4fba9d22014-05-04 22:34:44 +0200257 ./cpp/TestServer --ssl &
258 ./cpp/TestClient --ssl
Roger Meier6370cfd2014-05-04 22:21:58 +0200259
Roger Meier4fba9d22014-05-04 22:34:44 +0200260If "-h <host>" is used to run client, the above "localhost" in the above
261keys/server.crt has to be replaced with that host name.
262
263## TSSLSocketFactory::randomize()
264
265The default implementation of OpenSSLSocketFactory::randomize() simply calls
266OpenSSL's RAND_poll() when OpenSSL library is first initialized.
267
268The PRNG seed is key to the application security. This method should be
269overridden if it's not strong enough for you.