Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序在SSL_connect上退出_C++_Ssl - Fatal编程技术网

C++ 程序在SSL_connect上退出

C++ 程序在SSL_connect上退出,c++,ssl,C++,Ssl,我遇到一个问题,我正在使用的程序在调用SSL_connect后会立即关闭。我无法从它那里得到任何错误代码,因为调用SSL\u get\u error之后将被忽略,因为它退出了程序。如果我只是用connect执行一个普通的http请求,它就可以正常工作。有人对此有什么想法吗 我和Raspbian一起在Raspberry Pi 3上运行这个。 我对SSL非常陌生,因此任何提示都将不胜感激 编辑:下面是我要做的一段代码 #include <openssl/ssl.h> #include &

我遇到一个问题,我正在使用的程序在调用SSL_connect后会立即关闭。我无法从它那里得到任何错误代码,因为调用SSL\u get\u error之后将被忽略,因为它退出了程序。如果我只是用connect执行一个普通的http请求,它就可以正常工作。有人对此有什么想法吗

我和Raspbian一起在Raspberry Pi 3上运行这个。 我对SSL非常陌生,因此任何提示都将不胜感激

编辑:下面是我要做的一段代码

#include <openssl/ssl.h>
#include <iostream>
#include <resolv.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string>
#include <unistd.h>

int main()
{
        int sockfd;
        struct addrinfo hints, *servinfo, *p;
        int rv;

        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if ((rv = getaddrinfo("www.example.com", "https", &hints, &servinfo)) != 0)
        {
            fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(rv));
            exit(1);
        }

        SSL_load_error_strings();
        SSL_library_init();
        ssl_ctx = SSL_CTX_new(SSLv23_client_method());

        SSL * connection = SSL_new(int sockfd;
        struct addrinfo hints, *servinfo, *p;
        int rv;

        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if ((rv = getaddrinfo("www.example.com", "https", &hints, &servinfo)) != 0)
        {
            fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(rv));
            exit(1);
        }

        SSL_load_error_strings();
        SSL_library_init();
        ssl_ctx = SSL_CTX_new(SSLv23_client_method());

        SSL * connection = SSL_new(ssl_ctx);

        for (p = servinfo; p != NULL; p = p->ai_next)
        {
            if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) 
                {
                    perror("socket");
                    continue;
                }
            int fd = SSL_set_fd(connection, sockfd);

                break;
        }

        if (p == NULL) 
        {
            // looped off the end of the list with no connection
            fprintf(stderr, "failed to connect\n");
            exit(2);
        }
        int connRet = 0;
        while(connRet != 1 )
        {
            connRet = SSL_connect(connection);
            cout << "Error : " << SSL_get_error(connection, connRet) << endl;

        }



        freeaddrinfo(servinfo);
return 0;
}
编辑2:最终通过调试器运行它,现在我得到:

程序已退出,代码:141 按return继续


根据这个问题:这是一个信号管信号。

不确定它为什么会导致崩溃,但SSL\u connect并不等同于connect:它在已经建立的连接上启动TLS/SSL握手。由于代码中没有连接sockfd,它会失败并将connRet设置为-1,从而导致无休止的循环

您应该在创建套接字之后和启动SSL握手之前立即连接套接字:

    if ((sockfd = socket(p->ai_family, p->ai_socktype,
        p->ai_protocol)) == -1) 
    {
        perror("socket");
        continue;
    }
    /* connect at the TCP level */
    if (connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen)) {
        perror("connect");
        continue;
    }
    int fd = SSL_set_fd(connection, sockfd);
    ...

但是无论如何,你应该控制你的程序来消除可能的循环,并且在错误消息中保持一致:要么使用C++流CUT要么C文件*STDRR。如果可以打开插座,最好将其正确关闭。

听起来您可能会崩溃。使用调试信息构建一个程序版本,添加-g标志,然后在调试器中运行,查看它的内容。没有代码,我们无法知道崩溃的原因。构建一个[mvce],并在此处显示它以及错误消息。目前您的问题没有主题,因为它缺少代码。@SergeBallesta使用代码样本编辑,可能不相关,但在调用SSL\U connect之前,您应该进行if p==NULL检查。@Someprogrammerdude谢谢,edited您是个天才!非常感谢你!