]> git.ipfire.org Git - thirdparty/openssl.git/blob - demos/bio/sconnect.c
Import of old SSLeay release: SSLeay 0.9.1b (unreleased)
[thirdparty/openssl.git] / demos / bio / sconnect.c
1 /* NOCW */
2 /* demos/bio/sconnect.c */
3
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'
7 *
8 * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
9 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "err.h"
13 #include "ssl.h"
14
15 extern int errno;
16
17 int main(argc,argv)
18 int argc;
19 char *argv[];
20 {
21 char *host;
22 BIO *out;
23 char buf[1024*10],*p;
24 SSL_CTX *ssl_ctx=NULL;
25 SSL *ssl;
26 BIO *ssl_bio;
27 int i,len,off,ret=1;
28
29 if (argc <= 1)
30 host="localhost:4433";
31 else
32 host=argv[1];
33
34 /* Lets get nice error messages */
35 SSL_load_error_strings();
36
37 /* Setup all the global SSL stuff */
38 SSLeay_add_ssl_algorithms();
39 ssl_ctx=SSL_CTX_new(SSLv23_client_method());
40
41 /* Lets make a SSL structure */
42 ssl=SSL_new(ssl_ctx);
43 SSL_set_connect_state(ssl);
44
45 /* Use it inside an SSL BIO */
46 ssl_bio=BIO_new(BIO_f_ssl());
47 BIO_set_ssl(ssl_bio,ssl,BIO_CLOSE);
48
49 /* Lets use a connect BIO under the SSL BIO */
50 out=BIO_new(BIO_s_connect());
51 BIO_set_conn_hostname(out,host);
52 BIO_set_nbio(out,1);
53 out=BIO_push(ssl_bio,out);
54
55 p="GET / HTTP/1.0\r\n\r\n";
56 len=strlen(p);
57
58 off=0;
59 for (;;)
60 {
61 i=BIO_write(out,&(p[off]),len);
62 if (i <= 0)
63 {
64 if (BIO_should_retry(out))
65 {
66 fprintf(stderr,"write DELAY\n");
67 sleep(1);
68 continue;
69 }
70 else
71 {
72 goto err;
73 }
74 }
75 off+=i;
76 len-=i;
77 if (len <= 0) break;
78 }
79
80 for (;;)
81 {
82 i=BIO_read(out,buf,sizeof(buf));
83 if (i == 0) break;
84 if (i < 0)
85 {
86 if (BIO_should_retry(out))
87 {
88 fprintf(stderr,"read DELAY\n");
89 sleep(1);
90 continue;
91 }
92 goto err;
93 }
94 fwrite(buf,1,i,stdout);
95 }
96
97 ret=1;
98
99 if (0)
100 {
101 err:
102 if (ERR_peek_error() == 0) /* system call error */
103 {
104 fprintf(stderr,"errno=%d ",errno);
105 perror("error");
106 }
107 else
108 ERR_print_errors_fp(stderr);
109 }
110 BIO_free_all(out);
111 if (ssl_ctx != NULL) SSL_CTX_free(ssl_ctx);
112 exit(!ret);
113 return(ret);
114 }
115