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
13 #include <openssl/err.h>
14 #include <openssl/ssl.h>
25 SSL_CTX
*ssl_ctx
=NULL
;
31 host
="localhost:4433";
40 /* Lets get nice error messages */
41 SSL_load_error_strings();
43 /* Setup all the global SSL stuff */
44 OpenSSL_add_ssl_algorithms();
45 ssl_ctx
=SSL_CTX_new(SSLv23_client_method());
47 /* Lets make a SSL structure */
49 SSL_set_connect_state(ssl
);
51 /* Use it inside an SSL BIO */
52 ssl_bio
=BIO_new(BIO_f_ssl());
53 BIO_set_ssl(ssl_bio
,ssl
,BIO_CLOSE
);
55 /* Lets use a connect BIO under the SSL BIO */
56 out
=BIO_new(BIO_s_connect());
57 BIO_set_conn_hostname(out
,host
);
59 out
=BIO_push(ssl_bio
,out
);
61 p
="GET / HTTP/1.0\r\n\r\n";
67 i
=BIO_write(out
,&(p
[off
]),len
);
70 if (BIO_should_retry(out
))
72 fprintf(stderr
,"write DELAY\n");
88 i
=BIO_read(out
,buf
,sizeof(buf
));
92 if (BIO_should_retry(out
))
94 fprintf(stderr
,"read DELAY\n");
100 fwrite(buf
,1,i
,stdout
);
108 if (ERR_peek_error() == 0) /* system call error */
110 fprintf(stderr
,"errno=%d ",errno
);
114 ERR_print_errors_fp(stderr
);
117 if (ssl_ctx
!= NULL
) SSL_CTX_free(ssl_ctx
);