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