Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Stephen Gentle <stephen.13 <at> gmail.com>
Subject: Example of TSSLSocket usage
Newsgroups: gmane.comp.lib.thrift.user
Date: Friday 1st April 2011 08:09:29 UTC (over 5 years ago)
Hi,

I was wondering if anyone can help me with learning how to use thrift 
with SSL. At the moment, there is absolutely no documentation about how 
to use it, and I have had little success trying to work it out from the 
code.

Ultimately I'd like to have it working with both a server and a client 
certificate, but first it would be good to be able to do it just with a 
server certificate.

I am testing with the sample service in the Objective-C tutorial, but I 
am doing the client and server in C++. Here's what I have now:

Server:
#include "gen-cpp/UserStorage.h"
#include 
#include 
#include 
#include 
#include 
#include 

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

class UserStorageHandler : virtual public UserStorageIf {
  public:
   UserStorageHandler() {
     // Your initialization goes here
   }

   void store(const UserProfile& user) {
     // Your implementation goes here
     printf("store\n");
   }

   void retrieve(UserProfile& _return, const int32_t uid) {
     // Your implementation goes here
     printf("retrieve\n");
   }

};

int main(int argc, char **argv) {
     int port = 9090;
     shared_ptr handler(new UserStorageHandler());
     shared_ptr processor(new UserStorageProcessor(handler));

     shared_ptr factory(new TSSLSocketFactory());
     factory->server(true);
     factory->authenticate(false);
     factory->loadCertificate("certificate/server.crt");
     factory->loadPrivateKey("certificate/server.key");


     shared_ptr serverTransport(new 
TSSLServerSocket(port, factory));
     shared_ptr transportFactory(new 
TBufferedTransportFactory());
     shared_ptr protocolFactory(new 
TBinaryProtocolFactory());

     TSimpleServer server(processor, serverTransport, transportFactory, 
protocolFactory);
     server.serve();
     return 0;
}

And the client:
#include "gen-cpp/UserStorage.h"
#include "gen-cpp/test_types.h"

#include 
#include 
#include 

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

int main(int argc, char **argv) {
     boost::shared_ptr factory(new TSSLSocketFactory());
     factory->authenticate(true);

     boost::shared_ptr 
socket(factory->createSocket("localhost", 9090));
     boost::shared_ptr transport(new 
TBufferedTransport(socket));
     boost::shared_ptr protocol(new TBinaryProtocol(transport));

     UserStorageClient client(protocol);
     transport->open();

     UserProfile test;

     client.retrieve(test, 12);
     transport->close();

     return 0;
}

Sorry for the code dump, and thanks a lot,

Stephen Gentle
 
CD: 3ms