From fc62f7f284387a180e42402e8706965a666efba7 Mon Sep 17 00:00:00 2001 From: lloyd Date: Fri, 8 Apr 2011 14:57:49 +0000 Subject: More pubkey doc updates --- doc/examples/tls_client.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 doc/examples/tls_client.cpp (limited to 'doc/examples/tls_client.cpp') diff --git a/doc/examples/tls_client.cpp b/doc/examples/tls_client.cpp new file mode 100644 index 000000000..9f6f6229a --- /dev/null +++ b/doc/examples/tls_client.cpp @@ -0,0 +1,96 @@ +/* +* (C) 2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include "socket.h" + +using namespace Botan; + +#include +#include +#include +#include + +class Client_TLS_Policy : public TLS_Policy + { + public: + bool check_cert(const std::vector& certs) const + { + for(size_t i = 0; i != certs.size(); ++i) + { + std::cout << certs[i].to_string(); + } + + std::cout << "Warning: not checking cert signatures\n"; + + return true; + } + }; + +int main(int argc, char* argv[]) + { + if(argc != 2 && argc != 3) + { + printf("Usage: %s host [port]\n", argv[0]); + return 1; + } + + try + { + LibraryInitializer init; + + std::string host = argv[1]; + u32bit port = argc == 3 ? Botan::to_u32bit(argv[2]) : 443; + + printf("Connecting to %s:%d...\n", host.c_str(), port); + + Socket sock(argv[1], port); + + std::auto_ptr rng( + Botan::RandomNumberGenerator::make_rng()); + + Client_TLS_Policy policy; + + TLS_Client tls(std::tr1::bind(&Socket::read, std::tr1::ref(sock), _1, _2), + std::tr1::bind(&Socket::write, std::tr1::ref(sock), _1, _2), + policy, *rng); + + printf("Handshake extablished...\n"); + +#if 0 + std::string http_command = "GET / HTTP/1.1\r\n" + "Server: " + host + ':' + to_string(port) + "\r\n\r\n"; +#else + std::string http_command = "GET / HTTP/1.0\r\n\r\n"; +#endif + + tls.write((const byte*)http_command.c_str(), http_command.length()); + + u32bit total_got = 0; + + while(true) + { + if(tls.is_closed()) + break; + + byte buf[16+1] = { 0 }; + u32bit got = tls.read(buf, sizeof(buf)-1); + printf("%s", buf); + fflush(0); + + total_got += got; + } + + printf("\nRetrieved %d bytes total\n", total_got); + } + catch(std::exception& e) + { + printf("%s\n", e.what()); + return 1; + } + return 0; + } -- cgit v1.2.3