【问题标题】:Mumble/Google protobufs client side read data function errorMumble/Google protobufs 客户端读取数据功能错误
【发布时间】:2016-05-13 08:27:18
【问题描述】:

我正在尝试开发一个喃喃自语的客户端。要连接到 mumble 服务器(也称为 murmur),我需要按照 wiki "https://mumble-protocol.readthedocs.org/en/latest/establishing_connection.html#connect" 中列出的步骤进行操作。

我正在使用 C++ 在 Windows Visual Studios 中进行编码。

第 1 步是建立与服务器的 TCP 连接并进行 TSLv1 握手。

我尝试建立 TCP 连接并成功进行了 TSL 握手。然后我尝试使用 SSL_read(ssl, buf, sizeof(buf)) 读取数据,该函数返回 55(这是它读取的字节数)。但是,当我尝试将其注销到控制台时,缓冲区仍然是空的。

我通过 3 种方式注销:

  1. printf() - 因为 buf 被声明为 char 数组,所以我在 printf() 中使用 %s 打印出来
  2. cout - 使用 std::cout 打印出原始缓冲区
  3. 我知道杂音可能会将二进制数据发回给我,因此我尝试将前 4 个字节强制转换为 uint32_t 并尝试查看我是否能恢复数据。它打印了 0,这意味着数据什么都没有。

我知道我的客户正在读取一些数据,原因有 3 个:

  1. SSL_read 返回读取的数据数,在这种情况下为 55。
  2. 我尝试通过 433 端口连接到 google IP。连接完成后程序继续运行。它从未完成运行,并且 SSL_read 没有返回任何数据。 (谷歌不会发送这样的数据,所以这是有道理的)
  3. Mumble Wiki 说连接成功后,服务器应该发送它的版本信息。

我的问题是为什么我看不到正在读取的数据。我在连接 SSL 时是否遗漏了什么,或者我从服务器读取的方式有什么问题。

制作过mumble客户端的朋友:如何从murmur服务器获取版本信息?

这是我的代码。 Check my output

#include "stdafx.h"

#define _WINSOCK_DEPRECATED_NO_WARNINGS

#pragma comment(lib,"Ws2_32.lib")
/*****************************************************************************/
/*** ssl_client.c                                                          ***/
/***                                                                       ***/
/*** Demonstrate an SSL client.                                            ***/
/*****************************************************************************/

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <memory.h>
#include <errno.h>
#include <sys/types.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>

//#include <sys/socket.h>
//#include <resolv.h>
//#include <netdb.h>
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <stdint.h>
#define FAIL    -1

/*---------------------------------------------------------------------*/
/*--- OpenConnection - create socket and connect to server.         ---*/
/*---------------------------------------------------------------------*/
SOCKET OpenConnection(const char *hostname, int port)
{
    SOCKET sd;
    struct hostent *host;
    struct sockaddr_in addr;

    WSADATA wsadata;

    int error = WSAStartup(0x0202, &wsadata);

    //Did something happen?
    if (error)
        return false;

    //Did we get the right Winsock version?
    if (wsadata.wVersion != 0x0202)
    {
        WSACleanup(); //Clean up Winsock
        return false;
    }


    printf("Starting connection\n");
    /*
    if ((host = gethostbyname(hostname)) == NULL)
    {
        printf("No Host\n");
        perror(hostname);
        abort();
    }*/

    //memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(hostname);

    sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    int err = connect(sd, (SOCKADDR *)&addr, sizeof(addr));
    printf("Socket: %d\n", sd);
    if (err!= 0)
    {
        closesocket(sd);
        perror(hostname);
    }
    else 
        printf("Connected to %s:%d\n", hostname, port);
        return sd;
}

/*---------------------------------------------------------------------*/
/*--- InitCTX - initialize the SSL engine.                          ---*/
/*---------------------------------------------------------------------*/
SSL_CTX* InitCTX(void)
{
    const SSL_METHOD *method;
    SSL_CTX *ctx;
    SSL_library_init(); /* load encryption & hash algorithms for SSL */
    SSL_load_error_strings();
    OpenSSL_add_all_algorithms();       /* Load cryptos, et.al. */      /* Bring in and register error messages */
    method = TLSv1_client_method();     /* Create new client-method instance */
    ctx = SSL_CTX_new(method);          /* Create new context */
    if (ctx == NULL)
    {
        ERR_print_errors_fp(stderr);
    }
    return ctx;
}

/*---------------------------------------------------------------------*/
/*--- ShowCerts - print out the certificates.                       ---*/
/*---------------------------------------------------------------------*/
void ShowCerts(SSL* ssl)
{
    X509 *cert;
    char *line;

    cert = SSL_get_peer_certificate(ssl);   /* get the server's certificate */
    if (cert != NULL)
    {
        printf("Server certificates:\n");
        line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
        printf("\t Subject: %s\n", line);
        OPENSSL_free(line);                         /* free the malloc'ed string */
        line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
        printf("\t Issuer: %s\n", line);
        OPENSSL_free(line);                         /* free the malloc'ed string */
        X509_free(cert);                    /* free the malloc'ed certificate copy */
    }
    else
        printf("No certificates.\n");
}

/*---------------------------------------------------------------------*/
/*--- main - create SSL context and connect                         ---*/
/*---------------------------------------------------------------------*/
int main()
{
    SSL_CTX *ctx;
    SOCKET server;
    SSL *ssl;
    char buf[1024];
    int bytes;
    char *hostname;
    hostname = <put host name here>;
    int portnum = <put port here>;

    ctx = InitCTX();
    printf("Initialised SSL\n");
    server = OpenConnection(hostname, portnum);
    ssl = SSL_new(ctx);                     /* create new SSL connection state */
    SSL_set_fd(ssl, server);                /* attach the socket descriptor */
    if (SSL_connect(ssl) == FAIL)           /* perform the connection */
        ERR_print_errors_fp(stderr);
    else
    {
        int handshake = SSL_do_handshake(ssl);
        printf("Handshake Status: %i\n", handshake);

        char *msg = "Hello???";
        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
        ShowCerts(ssl);/* get any certs */

        const char* state = SSL_state_string(ssl);
        printf("State: %s\n", state);

        //SSL_write(ssl, msg, strlen(msg));         /* encrypt & send message */
        bytes = SSL_read(ssl, buf, sizeof(buf));    /* get reply & decrypt */
        buf[bytes] = '\0';
        printf("Received %d bytes: \"%s\"\n", bytes,buf);

        std::cout <<"Raw Buffer: " << buf[2] << "\n";

        uint32_t *test = (uint32_t *)buf;
        printf("Buffer force casted to 32 bit int: %u\n", *test);

        SSL_free(ssl);                              /* release connection state */
    }
    closesocket(server);                                    /* close socket */
    SSL_CTX_free(ctx);                              /* release context */
    return 0;
}

【问题讨论】:

    标签: c++ sockets tcp protocol-buffers


    【解决方案1】:

    假设您正在尝试阅读您未说明的第一条(版本)消息,并假设“通用字符串”表示“空终止字符串”,并假设您将完整的消息读入buf in第一名:

    printf("Version %04x release %s OS %s OS version %s\n",
        *(int*)buf,
        buf+4,
        buf+4+strlen(buf+4)+1,
        buf+4+strlen(buf+4+strlen(buf+4)+1)+1
        );
    

    ... 或类似的。请注意,版本信息是十六进制而不是十进制。

    电子与工程

    【讨论】:

    • 成功了。谢谢 :) 它并不能完全解决我读取数据的问题。但是通过您的代码,我能够找出问题所在。
    【解决方案2】:

    通过EJP 的回答,我找到了解决问题的方法。基本上,Mumble 使用 google 协议缓冲区来发送和接收数据。 Google 协议缓冲区在实际发送数据本身之前发送 6 个字节的有关数据的信息。出于某种原因,在我的情况下,这 6 个字节是“/ 0”。所以我的强制转换不起作用的原因是这 6 个字节中没有读取任何内容。

    但是,当我使用EJP's 打印缓冲区时,代码

    printf("Version %04x release %s OS %s OS version %s\n",
    *(int*)buf,
    buf+4,
    buf+4+strlen(buf+4)+1,
    buf+4+strlen(buf+4+strlen(buf+4)+1)+1
    );
    

    我能够看到这 6 个字节之后的数据。处理数据的正确方法不是忽略前 6 个字节,而是实现 google 协议缓冲区来解释这 6 个字节和协议缓冲区发送的数据。这样你就可以用你想要的任何语言获取数据(这就是协议缓冲区所做的)。

    如需了解更多信息,请访问Google Protocol Buffers。

    如果你正在用 C++ 创建一个 mumble 客户端,你可能还想访问Mumlib Library。

    【讨论】:

      猜你喜欢
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      相关资源
      最近更新 更多