【问题标题】:is it possible to use SSL_set_fd() with pipe?是否可以将 SSL_set_fd() 与管道一起使用?
【发布时间】:2021-01-05 21:52:02
【问题描述】:

我使用 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

【问题讨论】:

  • 什么版本的 OpenSSL?
  • @AsteroidsWithWings,openssl 1.1.1
  • “问题是 send() 总是返回 errno 38,这意味着“功能未实现” 这是否表明问题出在 send(),而不是您使用的其他功能?
  • 你真的是说它“返回”errno 38? send() 不返回错误号。 It returns the number of bytes sent, or -1 for error。如果出现错误,请检查 errno 全局变量以找出原因。不幸的是,你没有在你的问题中展示你的工作,所以我只能猜测......
  • @AsteroidsWithWings,send() 与套接字完美配合。只有当我将其更改为管道时,我才有错误。你是对的 - send() return -1, errno = 38.

标签: c++ openssl


【解决方案1】:

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

【讨论】:

  • 耻辱 ENOTSOCK 真的没有被给予
【解决方案2】:

虽然 BIO_new_bio_pair 似乎是一种更好的方法 - 您仍然可以使用套接字伪造管道。 以下是如何创建套接字对的示例:https://github.com/KxSystems/kafka/blob/master/socketpair.c

它适用于 Windows,但可以轻松移植到任何具有 POSIX 套接字的系统

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 2021-08-01
    • 2011-05-03
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多