DANE support structures, constructructors and accessors
[oweals/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 static int done = 0;
22
23 void interrupt()
24 {
25     done = 1;
26 }
27
28 void sigsetup(void)
29 {
30     struct sigaction sa;
31
32     /*
33      * Catch at most once, and don't restart the accept system call.
34      */
35     sa.sa_flags = SA_RESETHAND;
36     sa.sa_handler = interrupt;
37     sigemptyset(&sa.sa_mask);
38     sigaction(SIGINT, &sa, NULL);
39 }
40
41 int main(int argc, char *argv[])
42 {
43     char *port = NULL;
44     BIO *in = NULL;
45     BIO *ssl_bio, *tmp;
46     SSL_CTX *ctx;
47     char buf[512];
48     int ret = 1, i;
49
50     if (argc <= 1)
51         port = "*:4433";
52     else
53         port = argv[1];
54
55     SSL_load_error_strings();
56
57     /* Add ciphers and message digests */
58     OpenSSL_add_ssl_algorithms();
59
60     ctx = SSL_CTX_new(TLS_server_method());
61     if (!SSL_CTX_use_certificate_chain_file(ctx, CERT_FILE))
62         goto err;
63     if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
64         goto err;
65     if (!SSL_CTX_check_private_key(ctx))
66         goto err;
67
68     /* Setup server side SSL bio */
69     ssl_bio = BIO_new_ssl(ctx, 0);
70
71     if ((in = BIO_new_accept(port)) == NULL)
72         goto err;
73
74     /*
75      * This means that when a new connection is accepted on 'in', The ssl_bio
76      * will be 'duplicated' and have the new socket BIO push into it.
77      * Basically it means the SSL BIO will be automatically setup
78      */
79     BIO_set_accept_bios(in, ssl_bio);
80
81     /* Arrange to leave server loop on interrupt */
82     sigsetup();
83
84  again:
85     /*
86      * The first call will setup the accept socket, and the second will get a
87      * socket.  In this loop, the first actual accept will occur in the
88      * BIO_read() function.
89      */
90
91     if (BIO_do_accept(in) <= 0)
92         goto err;
93
94     while (!done) {
95         i = BIO_read(in, buf, 512);
96         if (i == 0) {
97             /*
98              * If we have finished, remove the underlying BIO stack so the
99              * next time we call any function for this BIO, it will attempt
100              * to do an accept
101              */
102             printf("Done\n");
103             tmp = BIO_pop(in);
104             BIO_free_all(tmp);
105             goto again;
106         }
107         if (i < 0)
108             goto err;
109         fwrite(buf, 1, i, stdout);
110         fflush(stdout);
111     }
112
113     ret = 0;
114  err:
115     if (ret) {
116         ERR_print_errors_fp(stderr);
117     }
118     BIO_free(in);
119     exit(ret);
120     return (!ret);
121 }