4

我正在阅读这本书Network Security with OpenSSLPravir 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

4

2 回答 2

4

你会更接近一点gcc -o server sslserver.c -pthread -lssl

本书的勘误表也告诉我们SSL_init_library应该是SSL_library_init.

剩下的唯一未定义的参考是“THREAD_setup”。

于 2013-01-26T21:57:22.357 回答
1

您似乎没有与 openssl 库链接。尝试添加

[..] -lssl [..]

除了链接时的其他库。

您的 gcc 行似乎也不完整:

 gcc -o -lpthread -lm server.c 

-o参数后面应该跟一个输出文件名(在这种情况下,它将使用“-lpthread”作为文件名。

于 2013-01-26T21:53:09.277 回答