2 /* demos/bio/sconnect.c */
5 * A minimal program to do SSL to a passed host and port.
6 * It is actually using non-blocking IO but in a very simple manner
7 * sconnect host:port - it does a 'GET / HTTP/1.0'
9 * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
14 #include <openssl/err.h>
15 #include <openssl/ssl.h>
25 char buf[1024 * 10], *p;
26 SSL_CTX *ssl_ctx = NULL;
29 int i, len, off, ret = 1;
32 host = "localhost:4433";
41 /* Lets get nice error messages */
42 SSL_load_error_strings();
44 /* Setup all the global SSL stuff */
45 OpenSSL_add_ssl_algorithms();
46 ssl_ctx = SSL_CTX_new(SSLv23_client_method());
48 /* Lets make a SSL structure */
49 ssl = SSL_new(ssl_ctx);
50 SSL_set_connect_state(ssl);
52 /* Use it inside an SSL BIO */
53 ssl_bio = BIO_new(BIO_f_ssl());
54 BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
56 /* Lets use a connect BIO under the SSL BIO */
57 out = BIO_new(BIO_s_connect());
58 BIO_set_conn_hostname(out, host);
60 out = BIO_push(ssl_bio, out);
62 p = "GET / HTTP/1.0\r\n\r\n";
67 i = BIO_write(out, &(p[off]), len);
69 if (BIO_should_retry(out)) {
70 fprintf(stderr, "write DELAY\n");
84 i = BIO_read(out, buf, sizeof(buf));
88 if (BIO_should_retry(out)) {
89 fprintf(stderr, "read DELAY\n");
95 fwrite(buf, 1, i, stdout);
102 if (ERR_peek_error() == 0) { /* system call error */
103 fprintf(stderr, "errno=%d ", errno);
106 ERR_print_errors_fp(stderr);
110 SSL_CTX_free(ssl_ctx);