2 /* demos/bio/sconnect.c */
4 /* A minimal program to do SSL to a passed host and port.
5 * It is actually using non-blocking IO but in a very simple manner
6 * sconnect host:port - it does a 'GET / HTTP/1.0'
8 * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
12 #include <openssl/err.h>
13 #include <openssl/ssl.h>
24 SSL_CTX *ssl_ctx=NULL;
30 host="localhost:4433";
34 /* Lets get nice error messages */
35 SSL_load_error_strings();
37 /* Setup all the global SSL stuff */
38 SSLeay_add_ssl_algorithms();
39 ssl_ctx=SSL_CTX_new(SSLv23_client_method());
41 /* Lets make a SSL structure */
43 SSL_set_connect_state(ssl);
45 /* Use it inside an SSL BIO */
46 ssl_bio=BIO_new(BIO_f_ssl());
47 BIO_set_ssl(ssl_bio,ssl,BIO_CLOSE);
49 /* Lets use a connect BIO under the SSL BIO */
50 out=BIO_new(BIO_s_connect());
51 BIO_set_conn_hostname(out,host);
53 out=BIO_push(ssl_bio,out);
55 p="GET / HTTP/1.0\r\n\r\n";
61 i=BIO_write(out,&(p[off]),len);
64 if (BIO_should_retry(out))
66 fprintf(stderr,"write DELAY\n");
82 i=BIO_read(out,buf,sizeof(buf));
86 if (BIO_should_retry(out))
88 fprintf(stderr,"read DELAY\n");
94 fwrite(buf,1,i,stdout);
102 if (ERR_peek_error() == 0) /* system call error */
104 fprintf(stderr,"errno=%d ",errno);
108 ERR_print_errors_fp(stderr);
111 if (ssl_ctx != NULL) SSL_CTX_free(ssl_ctx);