Update examples
Update per stdlib and language changes
Fix compilation with latest harec and stdlib
OpenSSL bindings for Hare.
First, OpenSSL needs to be initialized with openssl::init()
.
Next, an SSL context has to be initialized. The default way to to it is by using
openssl::ctx_new_tls()
and then freeing it with openssl::ctx_free(ctx)
.
An encrypted connection can be established by using
openssl::bio_new_ssl_connect(ctx)
and then specifying the destination using
openssl::bio_set_conn_hostname(bio, host, port)
. This creates a BIO object
that will be used later.
At this point, the connection is inactive. To activate it either read or write
to it, or use openssl::bio_do_handshake
. This is important when using
ssl_get_fd
, which is going to fail on an inactive connection.
A BIO can be directly read or written to using openssl::bio_read
and
openssl::bio_write
respectively, but it's much nicer to use an io::stream
abstraction over it:
const bstream = openssl::biostream_init(bio);
fmt::fprintln(&bstream, "Hello world!")?;
// Read the output
io::copy(os::stdout, &bstream)?;
To close a BIO, use openssl::bio_free_all(bio)
. Note that closing a BIO
stream will not close it.
Full documentation for all functions including ones not listed here is available
via haredoc openssl
.
The examples/
directory contains a few example programs using this library:
get.ha
- shows initialization, connection and blocking read and write
operations.poll.ha
- a bit more complex example that implements an interactive tool
for communicating lines of text with a server. It uses poll to multiplex
between user input and the server connection.All examples are built by using make
into the examples
, but without the
.ha
extension.
$ make install
$ git subtree -P vendor/hare-openssl/ add https://git.sr.ht/~yerinalexey/hare-openssl master