c - 为什么 OpenSSL 导致 sigpipe @ SSL_connect?

标签 c linux openssl signals sigpipe

    int sfd = socket(AF_INET6, SOCK_STREAM, 0);
    if (sfd < 0) continue;
    struct timeval timeout;
    timeout.tv_sec = 60;
    timeout.tv_usec = 0;
    setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof(timeout));
    setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, sizeof(timeout));
    if (connect(sfd, (struct sockaddr*) &ig, sizeof(struct sockaddr_in6))) {
        close(sfd);
        continue;
    }
    SSL* ssl = SSL_new(ctx);
    if (ssl == NULL) {
        close(sfd);
        continue;
    }
    SSL_set_connect_state(ssl);
    SSL_set_fd(ssl, sfd);
    printf("%i\n", SSL_get_fd(ssl));
    int con = SSL_connect(ssl);

OpenSSL 导致在我调用 SSL_connect 的线路上触发 SIGPIPE。我用 GDB 运行并确保套接字没有关闭。在/proc/fd/# 中,socket 之前或之后都没有出现 closed。我尝试切换 set_fd 和 connect_state 调用的顺序。我想我搞砸了 OpenSSL,但我似乎无法弄清楚。

最佳答案

SIGPIPE 在应用程序写入远程连接已关闭的套接字时由内核触发。请注意,在这种情况下,本地文件描述符仍然有效,但写入它会导致 SIGPIPE 或 EPIPE 发出信号,表明数据无法传送到远程端。

因为 SSL_connect 正在执行由多个数据交换组成的 SSL 握手。我的猜测是服务器或两者之间的某个中间件(如防火墙)在握手完成之前关闭了连接。可以通过数据包捕获(即 wireshark 或类似工具)找出此行为的确切细节。

关于c - 为什么 OpenSSL 导致 sigpipe @ SSL_connect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36408396/

相关文章:

需要 Linux 重定向帮助

c - 如何在 C 中的队列中存储 (x,y) 坐标

c++ - 混合 C 和 C++ 的线程同步

c - 打印输出时出现段错误

linux - 使用 IDA sigmake 生成 Libc 签名

linux - 不能 pull 分支

c - 错误 : ‘str’ undeclared (first use in this function) b = bst_inorder(b->left, str);

c - 通过SSL绑定(bind)客户端本地IP

c++ - 简单的 HTTPS 服务器和 Keep-Alive 连接

ruby - 为什么在尝试连接到 Apple 推送通知服务时出现 OpenSSL 错误?