Additional comment changes for reformat of 1.0.1
[openssl.git] / demos / bio / saccept.c
1 /* NOCW */
2 /* demos/bio/saccept.c */
3
4 /*-
5  * A minimal program to server 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
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(argc,argv)
30 int argc;
31 char *argv[];
32         {
33         char *port=NULL;
34         BIO *ssl_bio,*tmp;
35         SSL_CTX *ctx;
36         SSL *ssl;
37         char buf[512];
38         int ret=1,i;
39
40         if (argc <= 1)
41                 port="*:4433";
42         else
43                 port=argv[1];
44
45         signal(SIGINT,close_up);
46
47         SSL_load_error_strings();
48
49 #ifdef WATT32
50         dbug_init();
51         sock_init();
52 #endif
53
54         /* Add ciphers and message digests */
55         OpenSSL_add_ssl_algorithms();
56
57         ctx=SSL_CTX_new(SSLv23_server_method());
58         if (!SSL_CTX_use_certificate_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
59                 goto err;
60         if (!SSL_CTX_use_PrivateKey_file(ctx,CERT_FILE,SSL_FILETYPE_PEM))
61                 goto err;
62         if (!SSL_CTX_check_private_key(ctx))
63                 goto err;
64
65         /* Setup server side SSL bio */
66         ssl=SSL_new(ctx);
67         ssl_bio=BIO_new_ssl(ctx,0);
68
69         if ((in=BIO_new_accept(port)) == NULL) goto err;
70
71         /* This means that when a new connection is acceptede on 'in',
72          * The ssl_bio will be 'dupilcated' and have the new socket
73          * BIO push into it.  Basically it means the SSL BIO will be
74          * automatically setup */
75         BIO_set_accept_bios(in,ssl_bio);
76
77 again:
78         /* The first call will setup the accept socket, and the second
79          * will get a socket.  In this loop, the first actual accept
80          * will occur in the BIO_read() function. */
81
82         if (BIO_do_accept(in) <= 0) goto err;
83
84         for (;;)
85                 {
86                 i=BIO_read(in,buf,512);
87                 if (i == 0)
88                         {
89                         /* If we have finished, remove the underlying
90                          * BIO stack so the next time we call any function
91                          * for this BIO, it will attempt to do an
92                          * accept */
93                         printf("Done\n");
94                         tmp=BIO_pop(in);
95                         BIO_free_all(tmp);
96                         goto again;
97                         }
98                 if (i < 0) goto err;
99                 fwrite(buf,1,i,stdout);
100                 fflush(stdout);
101                 }
102
103         ret=0;
104 err:
105         if (ret)
106                 {
107                 ERR_print_errors_fp(stderr);
108                 }
109         if (in != NULL) BIO_free(in);
110         exit(ret);
111         return(!ret);
112         }
113