gnutls在握手时说“没有这样的文件或目录”

时间:2014-02-27 22:38:14

标签: c ssl gnutls

我正在尝试连接到mumble服务器并且需要TLS。我不太了解TLS或它是如何工作的,但我一直在关注文档,我似乎很清楚在很多时候会发生什么。但是,我有点难以接受一个错误,我正在握手说它无法找到文件。我假设它在套接字的文件描述符上出错了,但我似乎无法弄清楚原因。这就是我现在所拥有的,有人能指导我做错的事吗?

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <gnutls/gnutls.h>

void error(char* msg) {
    perror(msg);
    exit(1);
}

int main()
{
    int sockfd; 
    int portno = 64738;
    struct sockaddr_in serv_addr;
    struct hostent* server;

    //set up socket
    server = gethostbyname("localhost");
    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy(server->h_addr,
          &serv_addr.sin_addr.s_addr,
          server->h_length);
    serv_addr.sin_port = htons(portno);
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        error("Couldn't create socket");
    if (connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0 ) 
        error("Connection error");

    //global init
    if (gnutls_global_init() < 0)
        error("Could not globally initialize TLS");

    //session init
    gnutls_session_t* session = malloc(sizeof(gnutls_session_t));
    if (gnutls_init(session, GNUTLS_CLIENT) != GNUTLS_E_SUCCESS)
        error("Could not initialize TLS session");

    //Should eventually use certs, but I don't know how right now
    //gnutls_certificate_credentials_t cert_cred = malloc(sizeof(gnutls_certificate_credentials_t));
    //if (gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS)
    //  
    //For now do it anonymously
    gnutls_anon_client_credentials_t* anon_cred = malloc(sizeof(gnutls_anon_client_credentials_t));
    if (gnutls_credentials_set(*session, GNUTLS_CRD_ANON, anon_cred) != GNUTLS_E_SUCCESS)
        error("Could not set credentials");
    if (gnutls_anon_allocate_client_credentials(anon_cred) != GNUTLS_E_SUCCESS)
        error("Could not allocate credentials");

    //set up socket for tls
    gnutls_transport_set_int(*session, sockfd);

    //do the handshake
    if (gnutls_handshake(*session) != GNUTLS_E_SUCCESS)
        error("TLS handshake failed");

    //deinit
    gnutls_global_deinit();
    printf("Success\n");
    return 0;
}

非常感谢任何其他提示,尤其是关于此类事物的资源,因为我似乎无法找到其他人谈论它。

0 个答案:

没有答案