Import of old SSLeay release: SSLeay 0.9.1b (unreleased)
[openssl.git] / demos / eay / conn.c
1 /* NOCW */
2 /* demos/eay/conn.c */
3
4 /* A minimal program to connect to a port using the sock4a protocol.
5  *
6  * cc -I../../include conn.c -L../.. -lcrypto
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "err.h"
11 #include "bio.h"
12 #include "proxy.h"
13
14 extern int errno;
15
16 int main(argc,argv)
17 int argc;
18 char *argv[];
19         {
20         PROXY *pxy;
21         char *host;
22         char buf[1024*10],*p;
23         BIO *bio;
24         int i,len,off,ret=1;
25
26         if (argc <= 1)
27                 host="localhost:4433";
28         else
29                 host=argv[1];
30
31         /* Lets get nice error messages */
32         ERR_load_crypto_strings();
33
34         /* First, configure proxy settings */
35         pxy=PROXY_new();
36         PROXY_add_server(pxy,PROXY_PROTOCOL_SOCKS,"gromit:1080");
37
38         bio=BIO_new(BIO_s_socks4a_connect());
39
40         BIO_set_conn_hostname(bio,host);
41         BIO_set_proxies(bio,pxy);
42         BIO_set_socks_userid(bio,"eay");
43         BIO_set_nbio(bio,1);
44
45         p="GET / HTTP/1.0\r\n\r\n";
46         len=strlen(p);
47
48         off=0;
49         for (;;)
50                 {
51                 i=BIO_write(bio,&(p[off]),len);
52                 if (i <= 0)
53                         {
54                         if (BIO_should_retry(bio))
55                                 {
56                                 fprintf(stderr,"write DELAY\n");
57                                 sleep(1);
58                                 continue;
59                                 }
60                         else
61                                 {
62                                 goto err;
63                                 }
64                         }
65                 off+=i;
66                 len-=i;
67                 if (len <= 0) break;
68                 }
69
70         for (;;)
71                 {
72                 i=BIO_read(bio,buf,sizeof(buf));
73                 if (i == 0) break;
74                 if (i < 0)
75                         {
76                         if (BIO_should_retry(bio))
77                                 {
78                                 fprintf(stderr,"read DELAY\n");
79                                 sleep(1);
80                                 continue;
81                                 }
82                         goto err;
83                         }
84                 fwrite(buf,1,i,stdout);
85                 }
86
87         ret=1;
88
89         if (0)
90                 {
91 err:
92                 if (ERR_peek_error() == 0) /* system call error */
93                         {
94                         fprintf(stderr,"errno=%d ",errno);
95                         perror("error");
96                         }
97                 else
98                         ERR_print_errors_fp(stderr);
99                 }
100         BIO_free_all(bio);
101         if (pxy != NULL) PROXY_free(pxy);
102         exit(!ret);
103         return(ret);
104         }
105