首页 > 解决方案 > 是否可以将 SSL_set_fd() 与管道一起使用?

问题描述

我使用 ssl 服务器。我想在 SSL_set_fd 中使用管道作为文件描述符。

我的目标是通过管道将 ssl 数据包写入 ssl 服务器。代码:

// First thread
SSL * ssl = SSL_new(ctx);
int descriptors[2];
int res = pipe( descriptors );
if ( res == -1 ){ 
    throw std::runtime_error( "pipe error" );
}
res = SSL_set_rfd(ssl, descriptors[0]);
if ( res != 1 ){
    throw std::runtime_error("SSL_set_rfd");
} 
// Here the thread will stop for waiting ssl-hello
res = SSL_accept( ssl );



// In the second thread I am going to write ssl-packets to server: 
struct pollfd fds[1];
fds[0].fd = descriptors[1];    
fds[0].events = POLLOUT;
 
std::vector <unsigned char> sslPacket = waitAndPop();
            
int rc = poll( fds, 1, 10000 );
if ( rc < 0 ){
    throw std::runtime_error("poll error");
}           
if ( rc == 0 ){
    throw std::runtime_error("timeout");
}
 
if ( fds[0].revents & POLLOUT ){
    fds[0].revents = 0;
    rc = send( descriptors[1], sslPacket.data(), sslPacket.size(), 0 );
}

问题是 send() 总是返回 errno 38 ,这意味着“功能未实现”。难道我做错了什么?是否可以将管道设置为 ssl 的文件描述符?

macOS 10.15.4、Xcode 11.5、OpenSSL 1.1.1

我读了这个文档: 1.https://man7.org/linux/man-pages/man2/pipe.2.html 2.https://www.openssl.org/docs/manmaster/man3/SSL_set_fd.html 3 .https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/pipe.2.html

标签: c++openssl

解决方案


您不能send与管道一起使用。您可以使用write管道。send仅适用于套接字。


推荐阅读