Mumble / Google protobufs客户端读取数据函数错误

时间:2016-02-03 22:24:35

标签: c++ sockets tcp protocol-buffers

我正在努力开发一个笨蛋客户端。要连接到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(它读取的字节数)。但是,当我尝试将其注销到控制台时,缓冲区仍为空。

我以三种方式注销:

  1. printf() - 因为buf被声明为char数组,所以我在printf()中使用%s将其打印出来
  2. cout - 使用std :: cout
  3. 打印出原始缓冲区
  4. 我知道杂音可能会将二进制数据发回给我,所以我试图将前4个字节强制转换为uint32_t并尝试查看我是否甚至收回了数据。它打印0,意味着数据一无所获。
  5. 我知道我的客户正在阅读一些数据,原因有三个:

    1. SSL_read返回读取的数据的数量,在这种情况下为55。
    2. 我尝试在端口433连接到谷歌IP。程序在连接完成后继续运行。它从未完成运行,SSL_read没有返回任何数据。 (谷歌不会发送这样的数据,所以这很有意义)
    3. Mumble Wiki表示连接成功后,服务器应发送其版本信息。
    4. 我的问题是为什么我看不到正在阅读的数据。在连接SSL时是否有任何遗漏,或者我从服务器上读取的方式有问题。

      对于那些犯了笨蛋客户的人:如何从杂音服务器获取版本信息?

      这是我的代码。 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;
      }
      

2 个答案:

答案 0 :(得分:1)

假设您正在尝试读取您未说明的第一个(Version)消息,并假设'common string'表示'以null结尾的字符串',并假设您已将完整的消息读入{{1}首先:

buf

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

E&安培; OE

答案 1 :(得分:1)

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

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

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个字节,而是实现谷歌协议缓冲区来解释这6个字节和协议缓冲区发送的数据。这样,您就可以获得所需语言的数据(这就是协议缓冲区的作用)。

要获取更多信息,请访问Google Protocol Buffers

如果您使用C ++创建一个mumble客户端,您可能还想访问Mumlib Library