我想通過SSL連接到安全的服務,但是,SSL_read
決不返回,這當然是正常的行爲,如果服務器不返回任何消息,服務器我想連接到但應該返回某種消息。有什麼不允許在下面閱讀?SSL_read不是在下面的代碼接收
//Initialize SSL library
OpenSSL_add_ssl_algorithms();
//Initialize Crypto algorithms
OpenSSL_add_all_algorithms();
//Create new SSL context accepting SSL V2, V3 or TLS V1.0, V1.1 and V1.2
const SSL_METHOD *method = SSLv23_client_method();
SSL_CTX *ctx = SSL_CTX_new(method);
if (ctx == NULL)
{
printf("Error initializing SSL context.\n");
return 0;
}
SSL *ssl = SSL_new(ctx);
//Create socket descriptor
int sd = 0;
//Create hints for connection
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;//Can be both IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo * result;
//Get address info, this could potentially return multiple
int err = getaddrinfo("api.okcoin.com", "9880", &hints, &result);
if (err != 0)
{
printf("Could not get addr info.\n");
return 0;
}
//Try connecting to any of the returned addresses
struct addrinfo * res;
for (res = result; res != NULL; res = res->ai_next)
{
sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sd == -1)
{
printf("Could not connect to host.\n");
return 0;
}
if (connect(sd, res->ai_addr, res->ai_addrlen) == 0)
{
//Socket is now connected, free addrinfo results
freeaddrinfo(result);
//Assign socket descriptor to SSL
if (SSL_set_fd(ssl, sd) == 0)
{
printf("Could not assign socket descriptor.\n");
return 0;
}
//Begin SSL-handshake
if(SSL_connect(ssl) == 0)
{
printf("Could not perform handshake.\n");
return 0;
}
break;
}
}
//Could not connect socket, free addrinfo results and return error
if (res == NULL)
{
printf("Could no connect to to any host.\n");
freeaddrinfo(result);
return 0;
}
printf("Connected.\n");
SSL_write(ssl, "HELLO\x01", 6);
char * m = malloc(8192);
SSL_read(ssl, m, 8192);
請告訴我們有關服務器的信息。服務器在什麼條件下發送數據?它是否只是發送東西,不管是什麼?還是預計會迴應「你好」? –
它預計會響應任何消息(在這種情況下帶有錯誤消息)。由於涉及安全代碼,我無法提供有效的輸入消息。 – sigvardsen
@sigvardsen:我建議你做一個數據包捕獲(即Wireshark的或類似的),看看是否有來自服務器由客戶端發送數據幀進行應答的數據幀。這兩個數據幀應該在成功的SSL握手之後。 –