]>
git.ipfire.org Git - thirdparty/openssl.git/blob - demos/bio/saccept.c
604051cda9662f113556b4b9307f6465083f917c
2 * Copyright 1998-2024 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * A minimal program to serve an SSL connection.
14 * host is the interface IP to use. If any interface, use *:port
15 * The default it *:4433
17 * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
23 #include <openssl/err.h>
24 #include <openssl/ssl.h>
26 #define CERT_FILE "server.pem"
28 static volatile int done
= 0;
30 static void interrupt(int sig
)
35 static void sigsetup(void)
37 #if defined(OPENSSL_SYS_WINDOWS)
38 signal(SIGINT
, interrupt
);
43 * Catch at most once, and don't restart the accept system call.
45 sa
.sa_flags
= SA_RESETHAND
;
46 sa
.sa_handler
= interrupt
;
47 sigemptyset(&sa
.sa_mask
);
48 sigaction(SIGINT
, &sa
, NULL
);
52 int main(int argc
, char *argv
[])
59 int ret
= EXIT_FAILURE
, i
;
66 ctx
= SSL_CTX_new(TLS_server_method());
67 if (!SSL_CTX_use_certificate_chain_file(ctx
, CERT_FILE
))
69 if (!SSL_CTX_use_PrivateKey_file(ctx
, CERT_FILE
, SSL_FILETYPE_PEM
))
71 if (!SSL_CTX_check_private_key(ctx
))
74 /* Setup server side SSL bio */
75 ssl_bio
= BIO_new_ssl(ctx
, 0);
77 if ((in
= BIO_new_accept(port
)) == NULL
)
81 * This means that when a new connection is accepted on 'in', The ssl_bio
82 * will be 'duplicated' and have the new socket BIO push into it.
83 * Basically it means the SSL BIO will be automatically setup
85 BIO_set_accept_bios(in
, ssl_bio
);
87 /* Arrange to leave server loop on interrupt */
92 * The first call will setup the accept socket, and the second will get a
93 * socket. In this loop, the first actual accept will occur in the
94 * BIO_read() function.
97 if (BIO_do_accept(in
) <= 0)
101 i
= BIO_read(in
, buf
, 512);
104 * If we have finished, remove the underlying BIO stack so the
105 * next time we call any function for this BIO, it will attempt
115 fwrite(buf
, 1, i
, stdout
);
121 if (ret
!= EXIT_SUCCESS
)
122 ERR_print_errors_fp(stderr
);