diff options
author | Nikos Mavrogiannopoulos <nmav@crystal.(none)> | 2008-05-17 09:28:22 +0300 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@crystal.(none)> | 2008-05-17 09:28:22 +0300 |
commit | 8aa3130a523e4b8c4b68d5b0e49db8d9957e6c27 (patch) | |
tree | 6db5efd669c3edddd0a33a111f2f8931ba579f41 /doc | |
parent | edfaa96915e79ed8cfb3e6f7c28c691bb903c267 (diff) | |
download | gnutls-8aa3130a523e4b8c4b68d5b0e49db8d9957e6c27.tar.gz |
Updated the C++ API with patch from Eduardo Villanueva Che. Suggested by Benjamin Herr.
Diffstat (limited to 'doc')
-rw-r--r-- | doc/examples/Makefile.am | 14 | ||||
-rw-r--r-- | doc/examples/ex-cxx.cpp | 100 | ||||
-rw-r--r-- | doc/gnutls.texi | 9 |
3 files changed, 122 insertions, 1 deletions
diff --git a/doc/examples/Makefile.am b/doc/examples/Makefile.am index f5db096241..18ca3538e9 100644 --- a/doc/examples/Makefile.am +++ b/doc/examples/Makefile.am @@ -28,8 +28,20 @@ LDADD = libexamples.la \ ../../libextra/libgnutls-extra.la \ ../../gl/libgnu.la +CXX_LDADD = libexamples.la \ + ../../gl/libgnu.la \ + ../../lib/libgnutls.la \ + ../../lib/libgnutlsxx.la \ + ../../libextra/libgnutls-extra.la + noinst_PROGRAMS = ex-cert-select ex-client2 ex-client-resume \ - ex-crq ex-serv1 ex-serv-export + ex-crq ex-serv1 ex-serv-export + +if ENABLE_CXX +excxx_SOURCES = ex-cxx.cpp +excxx_LDADD = $(CXX_LDADD) +noinst_PROGRAMS += excxx +endif if ENABLE_ANON noinst_PROGRAMS += ex-client1 ex-serv-anon diff --git a/doc/examples/ex-cxx.cpp b/doc/examples/ex-cxx.cpp new file mode 100644 index 0000000000..b43aba658a --- /dev/null +++ b/doc/examples/ex-cxx.cpp @@ -0,0 +1,100 @@ +#if HAVE_CONFIG_H +# include <config.h> +#else +#endif +#include <iostream> +#include <stdexcept> +#include <gnutls/gnutls.h> +#include <gnutls/gnutlsxx.h> + +/* A very basic TLS client, with anonymous authentication. + * written by Eduardo Villanueva Che. + */ + +#define MAX_BUF 1024 +#define SA struct sockaddr + +#define CAFILE "ca.pem" +#define MSG "GET / HTTP/1.0\r\n\r\n" + +extern "C" +{ + int tcp_connect(void); + void tcp_close(int sd); +} + + +int main(void) +{ + int sd = -1; + gnutls_global_init(); + + try + { + + /* Allow connections to servers that have OpenPGP keys as well. + */ + gnutls::client_session session; + + /* X509 stuff */ + gnutls::certificate_credentials credentials; + + + /* sets the trusted cas file + */ + credentials.set_x509_trust_file(CAFILE, GNUTLS_X509_FMT_PEM); + /* put the x509 credentials to the current session + */ + session.set_credentials(credentials); + + /* Use default priorities */ + session.set_priority ("NORMAL", NULL); + + /* connect to the peer + */ + sd = tcp_connect(); + session.set_transport_ptr((gnutls_transport_ptr_t) sd); + + /* Perform the TLS handshake + */ + int ret = session.handshake(); + if (ret < 0) + { +// gnutls_perror(ret); + throw std::runtime_error("Handshake failed"); + } + else + { + std::cout << "- Handshake was completed" << std::endl; + } + + session.send(MSG, strlen(MSG)); + char buffer[MAX_BUF + 1]; + ret = session.recv(buffer, MAX_BUF); + if (ret == 0) + { + throw std::runtime_error("Peer has closed the TLS connection"); + } + else if (ret < 0) + { + throw std::runtime_error(gnutls_strerror(ret)); + } + + std::cout << "- Received " << ret << " bytes:" << std::endl; + std::cout.write(buffer, ret); + std::cout << std::endl; + + session.bye(GNUTLS_SHUT_RDWR); + } + catch (std::exception &ex) + { + std::cerr << "Exception caught: " << ex.what() << std::endl; + } + + if (sd != -1) + tcp_close(sd); + + gnutls_global_deinit(); + + return 0; +} diff --git a/doc/gnutls.texi b/doc/gnutls.texi index b14ab2cd27..7e79f31004 100644 --- a/doc/gnutls.texi +++ b/doc/gnutls.texi @@ -2248,6 +2248,7 @@ implemented by another example. * Client with Resume capability example:: * Simple client example with SRP authentication:: * Simple client example with TLS/IA support:: +* Simple client example in @acronym{C++}:: * Helper function for TCP connections:: @end menu @@ -2343,6 +2344,14 @@ The following client is a simple client which uses the @verbatiminclude examples/ex-client-tlsia.c +@node Simple client example in @acronym{C++} +@subsection Simple Client Example using the @acronym{C++} API + +The following client is a simple example of a client +client utilizing the GnuTLS @acronym{C++} API. + +@verbatiminclude examples/ex-cxx.cpp + @node Helper function for TCP connections @subsection Helper Function for TCP Connections |