Path: blob/main/crypto/openssl/demos/bio/server-cmod.c
105862 views
/*1* Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* A minimal TLS server it ses SSL_CTX_config and a configuration file to11* set most server parameters.12*/1314#include <stdio.h>15#include <signal.h>16#include <stdlib.h>17#include <openssl/err.h>18#include <openssl/ssl.h>19#include <openssl/conf.h>2021int main(int argc, char *argv[])22{23unsigned char buf[512];24char *port = "*:4433";25BIO *in = NULL;26BIO *ssl_bio = NULL;27BIO *tmp;28SSL_CTX *ctx;29int ret = EXIT_FAILURE, i;3031ctx = SSL_CTX_new(TLS_server_method());3233if (CONF_modules_load_file("cmod.cnf", "testapp", 0) <= 0) {34fprintf(stderr, "Error processing config file\n");35goto err;36}3738if (SSL_CTX_config(ctx, "server") == 0) {39fprintf(stderr, "Error configuring server.\n");40goto err;41}4243/* Setup server side SSL bio */44ssl_bio = BIO_new_ssl(ctx, 0);4546if ((in = BIO_new_accept(port)) == NULL)47goto err;4849/*50* This means that when a new connection is accepted on 'in', The ssl_bio51* will be 'duplicated' and have the new socket BIO push into it.52* Basically it means the SSL BIO will be automatically setup53*/54BIO_set_accept_bios(in, ssl_bio);55ssl_bio = NULL;5657again:58/*59* The first call will setup the accept socket, and the second will get a60* socket. In this loop, the first actual accept will occur in the61* BIO_read() function.62*/6364if (BIO_do_accept(in) <= 0)65goto err;6667for (;;) {68i = BIO_read(in, buf, sizeof(buf));69if (i == 0) {70/*71* If we have finished, remove the underlying BIO stack so the72* next time we call any function for this BIO, it will attempt73* to do an accept74*/75printf("Done\n");76tmp = BIO_pop(in);77BIO_free_all(tmp);78goto again;79}80if (i < 0) {81if (BIO_should_retry(in))82continue;83goto err;84}85fwrite(buf, 1, i, stdout);86fflush(stdout);87}8889ret = EXIT_SUCCESS;90err:91if (ret != EXIT_SUCCESS)92ERR_print_errors_fp(stderr);93BIO_free(in);94BIO_free_all(ssl_bio);95return ret;96}979899