Path: blob/main/crypto/openssl/demos/bio/server-cmod.c
34868 views
/*1* Copyright 2015-2017 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, *tmp;27SSL_CTX *ctx;28int ret = EXIT_FAILURE, i;2930ctx = SSL_CTX_new(TLS_server_method());3132if (CONF_modules_load_file("cmod.cnf", "testapp", 0) <= 0) {33fprintf(stderr, "Error processing config file\n");34goto err;35}3637if (SSL_CTX_config(ctx, "server") == 0) {38fprintf(stderr, "Error configuring server.\n");39goto err;40}4142/* Setup server side SSL bio */43ssl_bio = BIO_new_ssl(ctx, 0);4445if ((in = BIO_new_accept(port)) == NULL)46goto err;4748/*49* This means that when a new connection is accepted on 'in', The ssl_bio50* will be 'duplicated' and have the new socket BIO push into it.51* Basically it means the SSL BIO will be automatically setup52*/53BIO_set_accept_bios(in, ssl_bio);5455again:56/*57* The first call will setup the accept socket, and the second will get a58* socket. In this loop, the first actual accept will occur in the59* BIO_read() function.60*/6162if (BIO_do_accept(in) <= 0)63goto err;6465for (;;) {66i = BIO_read(in, buf, sizeof(buf));67if (i == 0) {68/*69* If we have finished, remove the underlying BIO stack so the70* next time we call any function for this BIO, it will attempt71* to do an accept72*/73printf("Done\n");74tmp = BIO_pop(in);75BIO_free_all(tmp);76goto again;77}78if (i < 0) {79if (BIO_should_retry(in))80continue;81goto err;82}83fwrite(buf, 1, i, stdout);84fflush(stdout);85}8687ret = EXIT_SUCCESS;88err:89if (ret != EXIT_SUCCESS)90ERR_print_errors_fp(stderr);91BIO_free(in);92return ret;93}949596