2012-04-02 52 views
0

我對OpenSSL和Cpp有一些源代碼存在問題。出於某種原因,它運行良好,但不打開套接字!當我嘗試連接使用的s_client.First它我得到「連接:沒有錯誤」當我運行netstat我沒有得到任何打開的端口應該打開12120.端口我甚至禁止即暫時我的防火牆,並沒有幫助BTW!我使用Windows 7感謝您考慮我的程序只是說萬物罰款,並開始在第二個* BIO_do_accept(ABIO)阻塞;!*Cpp OpenSSL服務器出錯,出現錯誤

#include "stdio.h" 
#include "string.h" 

#include "openssl/bio.h" 
#include "openssl/ssl.h" 
#include "openssl/err.h" 

int password_callback(char *buf, int size, int rwflag, void *userdata) 
{ 
    /* For the purposes of this demonstration, the password is "dw" */ 

    printf("*** Callback function called\n"); 
    strcpy(buf, "dw"); 
    return 1; 
} 

int main() 
{ 
    SSL_CTX *ctx; 
    SSL *ssl; 
    BIO *bio, *abio, *out, *sbio; 

    int (*callback)(char *, int, int, void *) = &password_callback; 

    printf("Secure Programming with the OpenSSL API, Part 4:\n"); 
    printf("Serving it up in a secure manner\n\n"); 

    SSL_load_error_strings(); 
    ERR_load_BIO_strings(); 
    SSL_library_init(); 
    ERR_load_SSL_strings(); 
    OpenSSL_add_all_algorithms(); 

    printf("Attempting to create SSL context... "); 
    ctx = SSL_CTX_new(SSLv23_server_method()); 
    if(ctx == NULL) 
    { 
     printf("Failed. Aborting.\n"); 
     return 0; 
    } 

    printf("\nLoading certificates...\n"); 
    SSL_CTX_set_default_passwd_cb(ctx, callback); 
    if(!SSL_CTX_use_certificate_file(ctx, "certificate.pem", SSL_FILETYPE_PEM)) 
    { 
     ERR_print_errors_fp(stdout); 
     SSL_CTX_free(ctx); 
     return 0; 
    } 
    if(!SSL_CTX_use_PrivateKey_file(ctx, "private.key", SSL_FILETYPE_PEM)) 
    { 
     ERR_print_errors_fp(stdout); 
     SSL_CTX_free(ctx); 
     return 0; 
    } 

    printf("Attempting to create BIO object... "); 
    bio = BIO_new_ssl(ctx, 0); 
    if(bio == NULL) 
    { 
     printf("Failed. Aborting.\n"); 
     ERR_print_errors_fp(stdout); 
     SSL_CTX_free(ctx); 
     return 0; 
    } 

    printf("\nAttempting to set up BIO for SSL...\n"); 
    BIO_get_ssl(bio, &ssl); 
    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); 

    abio = BIO_new_accept("12120"); 
    BIO_set_accept_bios(abio, bio); 

    printf("Waiting for incoming connection...\n"); 

    if(BIO_do_accept(abio) <= 0) 
    { 
     ERR_print_errors_fp(stdout); 
     SSL_CTX_free(ctx); SSL_library_init(); 
     BIO_free_all(bio); 
     BIO_free_all(abio); 
     return 0; 
    } 

    if(BIO_do_accept(abio) <= 0) 
    { 
     ERR_print_errors_fp(stdout); 
     SSL_CTX_free(ctx); 
     BIO_free_all(bio); 
     BIO_free_all(abio); 
     return 0; 
    } 

    out = BIO_pop(abio); 

    if(BIO_do_handshake(out) <= 0) 
    { 
     printf("Handshake failed.\n"); 
     ERR_print_errors_fp(stdout); 
     SSL_CTX_free(ctx); 
     BIO_free_all(bio); 
     BIO_free_all(abio); 
     return 0; 
    } 

    BIO_puts(out, "Hello\n"); 
    BIO_flush(out); 

    BIO_free_all(out); 
    BIO_free_all(bio); 
    BIO_free_all(abio); 

    SSL_CTX_free(ctx); 
} 
+0

你的程序有一些printf的。你有多遠?請更新您的文章與您的程序的輸出,也許客戶端輸出。 – ixe013 2012-04-02 03:13:21

+0

我的程序只是說萬物罰款,並開始在第二個* BIO_do_accept(ABIO)阻塞; *我真的被困在這裏! – Confident 2012-04-02 03:27:34

回答

0

究竟是什麼,你希望在這裏你應該看到端口?在12120偵聽狀態。然後,您的客戶端應該能夠連接。然後,您的服務器不接受另一個,這將阻止它讀取接受端口上的任何I/O,並最終阻止你的客戶了。我不知道爲什麼你正在做兩接受連續的,但它是你的代碼。

+0

我調用accept兩次,因爲它不會阻止,直至碰到第二個接受的。感謝您的評論,但是當我運行netstat時,我根本沒有看到任何端口12120!你可以再次看看我的源代碼,也許給我更多的見解,然後我將所有的SSL從我的插座中刪除 – Confident 2012-04-02 04:16:25

+2

@ user1220811然後非阻塞的第一個「接受」是你應該解決的問題。做某事兩次,因爲第一次沒有工作,然後嘗試調試所產生的無效程序是不是一個合理的策略。 – EJP 2012-04-02 04:28:37

0

從用於BIO_do_accept()的OpenSSL文檔:

BIO_do_accept()提供兩種功能。當第一次調用它時, 接受BIO已經建立,它將嘗試創建accept 套接字並將地址綁定到它。第二次和後續調用 BIO_do_accept()將等待傳入連接,或以非阻塞模式請求重試 。

相關問題