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