openssl3.2 - 官方dmeo学习 - server-arg.c

2024-01-11 13:28

本文主要是介绍openssl3.2 - 官方dmeo学习 - server-arg.c,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • openssl3.2 - 官方dmeo学习 - server-arg.c
    • 概述
    • 笔记
    • 备注
    • END

openssl3.2 - 官方dmeo学习 - server-arg.c

概述

TLS服务器, 等客户端来连接; 如果客户端断开了, 通过释放bio来释放客户端socket, 然后继续通过bio读来aceept.

笔记

对于开源工程, 不可能有作者那么熟悉, 变量命名需要改下有利于理解逻辑.
VS2019带的变量改名功能挺好的.
在这里插入图片描述
过了一遍的程序:

/*!
\file server-arg.c
\brief TLS服务器, 等客户端来连接; 如果客户端断开了, 通过释放bio来释放客户端socket, 然后继续通过bio读来aceept.
*//** Copyright 2013-2017 The OpenSSL Project Authors. All Rights Reserved.** Licensed under the Apache License 2.0 (the "License").  You may not use* this file except in compliance with the License.  You can obtain a copy* in the file LICENSE in the source distribution or at* https://www.openssl.org/source/license.html*//** A minimal program to serve an SSL connection. It uses blocking. It use the* SSL_CONF API with the command line. cc -I../../include server-arg.c* -L../.. -lssl -lcrypto -ldl*/#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <openssl/err.h>
#include <openssl/ssl.h>#include "my_openSSL_lib.h"int main(int argc, char *argv[])
{char *psz_port = "*:4433";BIO *bio_ssl, *bio_tmp;SSL_CTX *ctx_ssl;SSL_CONF_CTX *ctx_ssl_cfg;char buf[512];BIO *bio_in = NULL;int ret = EXIT_FAILURE, i;char **args = argv + 1;int nargs = argc - 1;ctx_ssl = SSL_CTX_new(TLS_server_method());ctx_ssl_cfg = SSL_CONF_CTX_new();SSL_CONF_CTX_set_flags(ctx_ssl_cfg, SSL_CONF_FLAG_SERVER);SSL_CONF_CTX_set_flags(ctx_ssl_cfg, SSL_CONF_FLAG_CERTIFICATE);SSL_CONF_CTX_set_ssl_ctx(ctx_ssl_cfg, ctx_ssl);while (*args && **args == '-') {int rv;/* Parse standard arguments */rv = SSL_CONF_cmd_argv(ctx_ssl_cfg, &nargs, &args);if (rv == -3) {fprintf(stderr, "Missing argument for %s\n", *args);goto err;}if (rv < 0) {fprintf(stderr, "Error in command %s\n", *args);ERR_print_errors_fp(stderr);goto err;}/* If rv > 0 we processed something so proceed to next arg */if (rv > 0)continue;/* Otherwise application specific argument processing */if (strcmp(*args, "-port") == 0) {psz_port = args[1];if (psz_port == NULL) {fprintf(stderr, "Missing -port argument\n");goto err;}args += 2;nargs -= 2;continue;} else {fprintf(stderr, "Unknown argument %s\n", *args);goto err;}}if (!SSL_CONF_CTX_finish(ctx_ssl_cfg)) {fprintf(stderr, "Finish error\n");ERR_print_errors_fp(stderr);goto err;}
#ifdef ITERATE_CERTS/** Demo of how to iterate over all certificates in an SSL_CTX structure.*/{X509 *x;int rv;rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);while (rv) {X509 *x = SSL_CTX_get0_certificate(ctx);X509_NAME_print_ex_fp(stdout, X509_get_subject_name(x), 0,XN_FLAG_ONELINE);printf("\n");rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT);}fflush(stdout);}
#endif/* Setup server side SSL bio */bio_ssl = BIO_new_ssl(ctx_ssl, 0);if ((bio_in = BIO_new_accept(psz_port)) == NULL)goto err;/** This means that when a new connection is accepted on 'in', The ssl_bio* will be 'duplicated' and have the new socket BIO push into it.* Basically it means the SSL BIO will be automatically setup*/BIO_set_accept_bios(bio_in, bio_ssl);again:/** The first call will setup the accept socket, and the second will get a* socket.  In this loop, the first actual accept will occur in the* BIO_read() function.*/if (BIO_do_accept(bio_in) <= 0)goto err;for (;;) {i = BIO_read(bio_in, buf, 512); /*! 阻塞的读客户端连上服务器发来的信息, 如果此时没有客户端连接, 阻塞在这里 */if (i == 0) {/** If we have finished, remove the underlying BIO stack so the* next time we call any function for this BIO, it will attempt* to do an accept*/printf("Done\n");bio_tmp = BIO_pop(bio_in);BIO_free_all(bio_tmp);goto again;}if (i < 0)goto err;fwrite(buf, 1, i, stdout);fflush(stdout);}ret = EXIT_SUCCESS;err:if (ret != EXIT_SUCCESS)ERR_print_errors_fp(stderr);BIO_free(bio_in);return ret;
}

备注

如果只是为了将库用起来, 库实现, 我们不用去看.
在官方给的demo中, 哪个API不知道啥意思, 去本地帮助中去查, 或者去网上去查. 知道意思就行. 防止逻辑理解不正确.

END

这篇关于openssl3.2 - 官方dmeo学习 - server-arg.c的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/594510

相关文章

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

SQL Server 查询数据库及数据文件大小的方法

《SQLServer查询数据库及数据文件大小的方法》文章介绍了查询数据库大小的SQL方法及存储过程实现,涵盖当前数据库、所有数据库的总大小及文件明细,本文结合实例代码给大家介绍的非常详细,感兴趣的... 目录1. 直接使用SQL1.1 查询当前数据库大小1.2 查询所有数据库的大小1.3 查询每个数据库的详

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.