我正在阅读这本书Network Security with OpenSSL
,Pravir Chandra, Matt Messier and John Viega
并尝试在书中使用 OpenSSL 的示例之后创建 SSL 客户端/服务器连接,但我收到此错误,我不知道为什么。
这是我的服务器:
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#include <pthread.h>
#define PORT "6001"
#define SERVER "splat.zork.org"
#define CLIENT "shell.zork.org"
void handle_error(const char *file, int lineno, const char *msg)
{
fprintf(stderr, "** %s:%i %s\n", file, lineno, msg);
ERR_print_errors_fp(stderr);
exit(-1);
}
void init_OpenSSL(void)
{
if (!THREAD_setup() || !SSL_library_init())
{
fprintf(stderr, "** OpenSSL initialization failed!\n");
exit(-1);
}
SSL_load_error_strings();
}
void do_server_loop(BIO *conn)
{
int done, err, nread;
char buf[80];
do
{
for (nread = 0; nread < sizeof(buf); nread += err)
{
err = BIO_read(conn, buf + nread, sizeof(buf) - nread);
if (err <= 0)
break;
}
fwrite(buf, 1, nread, stdout);
}
while (err > 0);
}
void *server_thread(void *arg)
{
BIO *client = (BIO *)arg;
pthread_detach(pthread_self( ));
fprintf(stderr, "Connection opened.\n");
do_server_loop(client);
fprintf(stderr, "Connection closed.\n");
BIO_free(client);
ERR_remove_state(0);
}
int main(int argc, char *argv[])
{
BIO *acc, *client;
pthread_t tid;
init_OpenSSL();
acc = BIO_new_accept(PORT);
if (!acc)
handle_error(__FILE__, __LINE__, "Error creating server socket");
if (BIO_do_accept(acc) <= 0)
handle_error(__FILE__, __LINE__, "Error binding server socket");
for (;;)
{
if (BIO_do_accept(acc) <= 0)
handle_error(__FILE__, __LINE__, "Error accepting connection");
client = BIO_pop(acc);
pthread_create(&tid, NULL, server_thread, client);
}
BIO_free(acc);
return 0;
}
我正在使用 gcc 编译源代码:
$ gcc -o server server.c -lssl -crypto -lpthread -lm
Undefined symbols for architecture i386:
"_ERR_print_errors_fp", referenced from:
_handle_error in ccgAll3d.o
"_THREAD_setup", referenced from:
_init_OpenSSL in ccgAll3d.o
"_BIO_read", referenced from:
_do_server_loop in ccgAll3d.o
"_BIO_free", referenced from:
_server_thread in ccgAll3d.o
"_ERR_remove_state", referenced from:
_server_thread in ccgAll3d.o
"_BIO_new_accept", referenced from:
_main in ccgAll3d.o
"_BIO_ctrl", referenced from:
_main in ccgAll3d.o
"_BIO_pop", referenced from:
_main in ccgAll3d.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
谁能告诉我为什么会这样?
前段时间我得到了类似的东西,并通过定义有问题的函数来解决它,但这不再起作用了。
我正在 Mac OS X 10.6.8 上编译源代码。是因为我使用的是 32 位系统吗?
这是我正在编译的命令gcc -Wall -lssl -crypto -lpthread -lm -o server server.c