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