STACK_OF(SSL_COMP) is a public type
[openssl.git] / demos / bio / server-cmod.c
1 /* NOCW */
2 /* demos/bio/server-cmod.c */
3
4 /*
5  * A minimal TLS server it ses SSL_CTX_config and a configuration file to
6  * set most server parameters.
7  */
8
9 #include <stdio.h>
10 #include <signal.h>
11 #include <openssl/err.h>
12 #include <openssl/ssl.h>
13 #include <openssl/conf.h>
14
15 int main(int argc, char *argv[])
16 {
17     unsigned char buf[512];
18     char *port = "*:4433";
19     BIO *in = NULL;
20     BIO *ssl_bio, *tmp;
21     SSL_CTX *ctx;
22     int ret = 1, i;
23
24     SSL_load_error_strings();
25
26     /* Add ciphers and message digests */
27     OpenSSL_add_ssl_algorithms();
28
29     if (CONF_modules_load_file("cmod.cnf", "testapp", 0) <= 0) {
30         fprintf(stderr, "Error processing config file\n");
31         goto err;
32     }
33
34     ctx = SSL_CTX_new(TLS_server_method());
35
36     if (SSL_CTX_config(ctx, "server") == 0) {
37         fprintf(stderr, "Error configuring server.\n");
38         goto err;
39     }
40
41     /* Setup server side SSL bio */
42     ssl_bio = BIO_new_ssl(ctx, 0);
43
44     if ((in = BIO_new_accept(port)) == NULL)
45         goto err;
46
47     /*
48      * This means that when a new connection is accepted on 'in', The ssl_bio
49      * will be 'duplicated' and have the new socket BIO push into it.
50      * Basically it means the SSL BIO will be automatically setup
51      */
52     BIO_set_accept_bios(in, ssl_bio);
53
54  again:
55     /*
56      * The first call will setup the accept socket, and the second will get a
57      * socket.  In this loop, the first actual accept will occur in the
58      * BIO_read() function.
59      */
60
61     if (BIO_do_accept(in) <= 0)
62         goto err;
63
64     for (;;) {
65         i = BIO_read(in, buf, sizeof(buf));
66         if (i == 0) {
67             /*
68              * If we have finished, remove the underlying BIO stack so the
69              * next time we call any function for this BIO, it will attempt
70              * to do an accept
71              */
72             printf("Done\n");
73             tmp = BIO_pop(in);
74             BIO_free_all(tmp);
75             goto again;
76         }
77         if (i < 0) {
78             if (BIO_should_retry(in))
79                 continue;
80             goto err;
81         }
82         fwrite(buf, 1, i, stdout);
83         fflush(stdout);
84     }
85
86     ret = 0;
87  err:
88     if (ret) {
89         ERR_print_errors_fp(stderr);
90     }
91     BIO_free(in);
92     exit(ret);
93     return (!ret);
94 }