02479edc032eea6108b0bd87c4c9863395b7d2a0
[openssl.git] / demos / bio / saccept.c
1 /* NOCW */
2 /* demos/bio/saccept.c */
3
4 /* A minimal program to serve an SSL connection.
5  * It uses blocking.
6  * saccept host:port
7  * host is the interface IP to use.  If any interface, use *:port
8  * The default it *:4433
9  *
10  * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
11  */
12
13 #include <stdio.h>
14 #include <signal.h>
15 #include <openssl/err.h>
16 #include <openssl/ssl.h>
17
18 #define CERT_FILE       "server.pem"
19
20 BIO *in=NULL;
21
22 void close_up()
23         {
24         if (in != NULL)
25                 BIO_free(in);
26         }
27
28 int main(int argc, char *argv[])
29         {
30         char *port=NULL;
31         BIO *ssl_bio,*tmp;
32         SSL_CTX *ctx;
33         char buf[512];
34         int ret=1,i;
35
36         if (argc <= 1)
37                 port="*:4433";
38         else
39                 port=argv[1];
40
41         signal(SIGINT,close_up);
42
43         SSL_load_error_strings();
44
45         /* Add ciphers and message digests */
46         OpenSSL_add_ssl_algorithms();
47
48         ctx=SSL_CTX_new(SSLv23_server_method());
49         if (!SSL_CTX_use_certificate_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
50                 goto err;
51         if (!SSL_CTX_use_PrivateKey_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
52                 goto err;
53         if (!SSL_CTX_check_private_key(ctx))
54                 goto err;
55
56         /* Setup server side SSL bio */
57         ssl_bio=BIO_new_ssl(ctx,0);
58
59         if ((in=BIO_new_accept(port)) == NULL) goto err;
60
61         /* This means that when a new connection is accepted on 'in',
62          * The ssl_bio will be 'duplicated' and have the new socket
63          * BIO push into it.  Basically it means the SSL BIO will be
64          * automatically setup */
65         BIO_set_accept_bios(in,ssl_bio);
66
67 again:
68         /* The first call will setup the accept socket, and the second
69          * will get a socket.  In this loop, the first actual accept
70          * will occur in the BIO_read() function. */
71
72         if (BIO_do_accept(in) <= 0) goto err;
73
74         for (;;)
75                 {
76                 i=BIO_read(in,buf,512);
77                 if (i == 0)
78                         {
79                         /* If we have finished, remove the underlying
80                          * BIO stack so the next time we call any function
81                          * for this BIO, it will attempt to do an
82                          * accept */
83                         printf("Done\n");
84                         tmp=BIO_pop(in);
85                         BIO_free_all(tmp);
86                         goto again;
87                         }
88                 if (i < 0) goto err;
89                 fwrite(buf,1,i,stdout);
90                 fflush(stdout);
91                 }
92
93         ret=0;
94 err:
95         if (ret)
96                 {
97                 ERR_print_errors_fp(stderr);
98                 }
99         if (in != NULL) BIO_free(in);
100         exit(ret);
101         return(!ret);
102         }
103