我正在尝试开发一个 mumble 客户端。要连接到 mumble 服务器(也称为 murmur),我需要按照 wiki“https://mumble-protocol.readthedocs.org/en/latest/establishing_connection.html#connect”中列出的步骤进行操作。
我在 Windows Visual Studios 中使用 C++ 编写代码。
第 1 步是与服务器建立 TCP 连接并进行 TSLv1 握手。
我尝试建立 TCP 连接并成功进行了 TSL 握手。然后我尝试使用 SSL_read(ssl, buf, sizeof(buf)) 读取数据,该函数返回 55(这是它读取的字节数)。但是,当我尝试将其注销到控制台时,缓冲区仍然是空的。
我通过 3 种方式注销:
- printf() - 因为 buf 被声明为 char 数组,所以我在 printf() 中使用 %s 打印它
- cout - 使用 std::cout 打印出原始缓冲区
- 我知道杂音可能会将二进制数据发回给我,所以我尝试将前 4 个字节强行转换为 uint32_t 并尝试查看我是否能取回数据。它打印了 0,这意味着数据什么也没有。
我知道我的客户正在读取一些数据,原因有 3 个:
- SSL_read 返回读取的数据数,在本例中为 55。
- 我尝试在端口 433 连接到 google IP。连接完成后程序继续运行。它从未完成运行,并且 SSL_read 没有返回任何数据。 (谷歌不会发送那样的数据,所以这是有道理的)
- 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;
}
最佳答案
假设您正在尝试读取您未声明的第一条(版本)消息,并假设“通用字符串”表示“空终止字符串”,并假设您将完整的消息读入 buf
首先:
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
);
... 或类似的。请注意,版本信息是十六进制而不是十进制。
环境与环境
关于c++ - Mumble/Google protobufs 客户端读取数据函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35188947/