我已经完成了使用openSSL建立客户端和服务器之间通信的任务: [1]客户 - >服务器:提示要求用户输入数字X. [2]服务器 - >客户:X + 1。 [3]客户 - >检查服务器的答案是否正确,并输出结果。客户端 重复步骤1。
我已经完成了以下步骤,但是我遇到了分段错误,你们可以告诉我哪里出错了吗
sserver.cc
#include <openssl/ssl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if (argc != 4) {
printf("./exec CertFile KeyFile port");
return -1;
}
char* cert_file = argv[1];
char* key_file = argv[2];
int port = atoi(argv[3]);
// init the ssl lib
SSL_library_init();
//SSL_METHOD* method;
//SSL_CTX *ctx;
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
const SSL_METHOD* method = SSLv3_server_method();
SSL_CTX *ctx = SSL_CTX_new(method);
// load the server's certificate
SSL_CTX_use_certificate_file(ctx, cert_file, SSL_FILETYPE_PEM);
// load the server's private key
SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM);
// check the private against the known certificate
if (!SSL_CTX_check_private_key(ctx)) {
printf("Private key does not match\n");
abort();
}
// standard tcp server setup and connection
int sd, client;
struct sockaddr_in addr;
sd = socket(PF_INET, SOCK_STREAM, 0);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
bind(sd, (struct sockaddr*)&addr, sizeof(addr));
listen(sd, 10);
client = accept(sd, 0, 0);
SSL* ssl = SSL_new(ctx);
SSL_set_fd(ssl, client);
SSL_accept(ssl);
char buf[1024];
int buf_size = 1024;
int ClientResponse,StoredValue;
int TempValue;
// real work here
while(1) {
// read message from client, plus one, then send back to client
//ClientResponse=SSL_read(ssl,(void*)StoredValue,3);
ClientResponse=SSL_read(ssl,(void*)buf,buf_size);
ClientResponse=ClientResponse+1;
SSL_write(ssl,(const void*)ClientResponse,buf_size);
}
client = SSL_get_fd(ssl);
SSL_free(ssl);
close(sd);
}
sclient.cc
#include <openssl/ssl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if (argc != 3) {
printf("./exec hostname port");
return -1;
}
char* hostname = argv[1];
int port = atoi(argv[2]);
// init the ssl lib
SSL_library_init();
printf("client...1\n");
//SSL_METHOD* method;
SSL_CTX *ctx;
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
const SSL_METHOD* method = SSLv3_client_method();
ctx = SSL_CTX_new(method);
// create a standard tcp client
int server;
struct hostent* host;
struct sockaddr_in addr;
printf("client...2\n");
host = gethostbyname(hostname);
server = socket(PF_INET, SOCK_STREAM, 0);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = *(long*)(host->h_addr);
printf("client...3\n");
connect(server, (struct sockaddr*)&addr, sizeof(addr));
printf("client...4\n");
SSL* ssl;
ssl = SSL_new(ctx);
SSL_set_fd(ssl, server);
printf("client...5\n");
int sv = SSL_connect(ssl);
printf("client...6\n");
printf("sv = %d\n", sv);
if (sv != 1) {
printf("Can't establish ssl connection with server...\n");
// send a string to
SSL_free(ssl);
return -1;
}
int UserInput,ServerOutput;
int StoredValue;
int TempValue;
// real work here
while(1) {
// 1. ask the user to input a random number, and send to server using SSL library
printf("Enter a number user:\n");
scanf("%d",&UserInput);
TempValue=UserInput;
SSL_write(ssl,(const void*)UserInput,TempValue);
// 2. wait for the response from the server
// 3. Check if the response is correct or not
ServerOutput=SSL_read(ssl,(void*)StoredValue,TempValue);
if(ServerOutput==UserInput)
printf("\nCorrect %d",UserInput);
else
printf("\nFalse");
}
SSL_free(ssl);
}
答案 0 :(得分:0)
您使用指针不正确。
在服务器部分代码中应该是这样的:
SSL_read(ssl, &TempValue, sizeof(TempValue));
TempValue++;
SSL_write(ssl, &TempValue, sizeof(TempValue));
客户端部分:
SSL_write(ssl, &UserInput, sizeof(UserInput));
SSL_read(ssl, &ServerOutput, sizeof(ServerOutput));
printf("UserInput=%d, ServerOutput=%d\n", UserInput, ServerOutput);
的内容