SSL_shutdown返回值的研究(1)

SSL_shutdown用于关闭TLS/SSL连接,发送'close notify'告警通知对端。其返回值0表示应用已启动关闭,1表示双方已完成关闭。-1通常指示错误,需调用SSL_get_error获取具体信息。在非阻塞BIO中,可能需要多次调用以完成双向关闭。若启用静默关闭,SSL_shutdown将始终返回1,不发送'close notify'消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Format

LIBS := CSSL 
#include <openssl/ssl.h>
int SSL_shutdown(SSL *ssl)

ssl
    A pointer to a token returned on the SSL_new call.

Normal return

    Return code 0 indicates that the application issued the SSL_shutdown function first. Continue issuing the SSL_shutdown function until you receive return code 1, which indicates the remote application has also shut down.
    In SSL version 3 and TLS version 1, return code 1 indicates that both the client and server applications have issued the SSL_shutdown function.
    In SSL version 2, a return code of 1 is always returned.

Error return

A return code equal to -1 indicates an error. Issue the SSL_get_error function to obtain specific information about the error.
Programming considerations

    To use this function, you must include the library specified in the prototype in your makefile.
    The SSL_shutdown function is the normal way to shut down an SSL session. It is a good idea that you shut 
将如下代码改成一个函数#include <string.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <openssl/ssl.h> #include <openssl/err.h> #define MAX_BUF_SIZE 1024 int main(int argc, char *argv[]) { int sockfd, ret; char *url = argv[1]; // 输入的 url char *cert_path = argv[2]; // 输入的证书路径 struct sockaddr_in servaddr; SSL_CTX *ctx; SSL *ssl; char buf[MAX_BUF_SIZE]; // 初始化 SSLSSL_library_init(); OpenSSL_add_all_algorithms(); SSL_load_error_strings(); // 创建 SSL 上下文 ctx = SSL_CTX_new(TLS_client_method()); if (ctx == NULL) { printf("SSL_CTX_new error.\n"); return -1; } // 加载证书 ret = SSL_CTX_load_verify_locations(ctx, cert_path, NULL); if (ret != 1) { printf("SSL_CTX_load_verify_locations error.\n"); return -1; } // 创建 socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { printf("socket error.\n"); return -1; } // 设置服务器地址 memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(443); inet_pton(AF_INET, url, &servaddr.sin_addr); // 连接服务器 ret = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); if (ret < 0) { printf("connect error.\n"); return -1; } // 创建 SSL 对象 ssl = SSL_new(ctx); if (ssl == NULL) { printf("SSL_new error.\n"); return -1; } SSL_set_fd(ssl, sockfd); // 建立 SSL 连接 ret = SSL_connect(ssl); if (ret != 1) { printf("SSL_connect error.\n"); return -1; } // 发送请求 char *request = "GET / HTTP/1.1\r\nHost: %s\r\n\r\n"; sprintf(buf, request, url); SSL_write(ssl, buf, strlen(buf)); // 接收响应 while (1) { memset(buf, 0, sizeof(buf)); ret = SSL_read(ssl, buf, sizeof(buf) - 1); if (ret < 0) { printf("SSL_read error.\n"); break; } else if (ret == 0) { break; } else { printf("%s", buf); } } // 关闭 SSL 连接 SSL_shutdown(ssl); SSL_free(ssl); // 关闭 socket close(sockfd); // 清理 SSL 上下文 SSL_CTX_free(ctx); return 0; }
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值