Demo of use of errors in applications.
[oweals/openssl.git] / demos / bio / server-arg.c
1 /* NOCW */
2 /* demos/bio/server-arg.c */
3
4 /* A minimal program to serve an SSL connection.
5  * It uses blocking.
6  * It use the SSL_CONF API with the command line.
7  *
8  * cc -I../../include server-arg.c -L../.. -lssl -lcrypto -ldl
9  */
10
11 #include <stdio.h>
12 #include <signal.h>
13 #include <openssl/err.h>
14 #include <openssl/ssl.h>
15
16
17 int main(int argc, char *argv[])
18         {
19         char *port = "*:4433";
20         BIO *ssl_bio,*tmp;
21         SSL_CTX *ctx;
22         SSL_CONF_CTX *cctx;
23         char buf[512];
24         BIO *in=NULL;
25         int ret=1,i;
26         char **args = argv + 1;
27         int nargs = argc - 1;
28
29         SSL_load_error_strings();
30
31         /* Add ciphers and message digests */
32         OpenSSL_add_ssl_algorithms();
33
34         ctx=SSL_CTX_new(SSLv23_server_method());
35
36         cctx = SSL_CONF_CTX_new();
37         SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
38         SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);
39         SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
40         while(*args && **args == '-')
41                 {
42                 int rv;
43                 /* Parse standard arguments */
44                 rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
45                 if (rv == -3)
46                         {
47                         fprintf(stderr, "Missing argument for %s\n", *args);
48                         goto err;
49                         }
50                 if (rv < 0)
51                         {
52                         fprintf(stderr, "Error in command %s\n", *args);
53                         ERR_print_errors_fp(stderr);
54                         goto err;
55                         }
56                 /* If rv > 0 we processed something so proceed to next arg */
57                 if (rv > 0)
58                         continue;
59                 /* Otherwise application specific argument processing */
60                 if (!strcmp(*args, "-port"))
61                         {
62                         port = args[1];
63                         if (port == NULL)
64                                 {
65                                 fprintf(stderr, "Missing -port argument\n");
66                                 goto err;
67                                 }
68                         args += 2;
69                         nargs -= 2;
70                         continue;
71                         }
72                 else
73                         {
74                         fprintf(stderr, "Unknown argument %s\n", *args);
75                         goto err;
76                         }
77                 }
78
79         if (!SSL_CONF_CTX_finish(cctx))
80                 {
81                 fprintf(stderr, "Finish error\n");
82                 ERR_print_errors_fp(stderr);
83                 goto err;
84                 }
85
86         /* Setup server side SSL bio */
87         ssl_bio=BIO_new_ssl(ctx,0);
88
89         if ((in=BIO_new_accept(port)) == NULL) goto err;
90
91         /* This means that when a new connection is accepted on 'in',
92          * The ssl_bio will be 'duplicated' and have the new socket
93          * BIO push into it.  Basically it means the SSL BIO will be
94          * automatically setup */
95         BIO_set_accept_bios(in,ssl_bio);
96
97 again:
98         /* The first call will setup the accept socket, and the second
99          * will get a socket.  In this loop, the first actual accept
100          * will occur in the BIO_read() function. */
101
102         if (BIO_do_accept(in) <= 0) goto err;
103
104         for (;;)
105                 {
106                 i=BIO_read(in,buf,512);
107                 if (i == 0)
108                         {
109                         /* If we have finished, remove the underlying
110                          * BIO stack so the next time we call any function
111                          * for this BIO, it will attempt to do an
112                          * accept */
113                         printf("Done\n");
114                         tmp=BIO_pop(in);
115                         BIO_free_all(tmp);
116                         goto again;
117                         }
118                 if (i < 0) goto err;
119                 fwrite(buf,1,i,stdout);
120                 fflush(stdout);
121                 }
122
123         ret=0;
124 err:
125         if (ret)
126                 {
127                 ERR_print_errors_fp(stderr);
128                 }
129         if (in != NULL) BIO_free(in);
130         exit(ret);
131         return(!ret);
132         }
133