簡體   English   中英

如何進行非阻塞 OpenSSL 連接?

[英]How to make non-blocking OpenSSL connection?

我想建立一個非阻塞的 OpenSSL 連接

在此連接上 - 如果沒有可供讀取的數據,則整個程序執行流程會在 SSL_read() 上停止。 我想要這樣,如果沒有可供讀取的數據,它會給我像 WANT_READ 這樣的返回值,我知道沒有更多的數據可用。

char *sslRead (connection *c)
{
const int readSize = 1024;
char *rc = NULL;
int r;
int received, count = 0;
int ReallocSize = 0;
char buffer[1024];

if (c)
{
    while (1)
    {
        if (!rc)
        {
            rc = malloc (readSize + 1); 
            if (rc == NULL)
                printf("the major error have happen. leave program\n");
        }
        else
        {
            ReallocSize = (count + 1) * (readSize + 1);
            rc = realloc (rc, ReallocSize);
        }

        // if i have no data available for read after reading data, 
        // this call will not return anything and wait for more data

        // i want change this non blocking connections
        received = SSL_read (c->sslHandle, buffer, readSize);

        buffer[received] = '\0';


        if (received <= 0)
        {
            printf(" received equal to or less than 0\n");
            switch (SSL_get_error(c->sslHandle, r))
            {
            case SSL_ERROR_NONE:
                printf("SSL_ERROR_NONE\n");
                break;   
            case SSL_ERROR_ZERO_RETURN: 
                printf("SSL_ERROR_ZERO_RETURN\n");
                break;   
            case SSL_ERROR_WANT_READ: 
                printf("SSL_ERROR_WANT_READ\n");
                break;
            default:
                printf("error happens %i\n", r); 
            }     
            break;
        }

        count++;
    }
}
return rc;

}

這是我建立聯系的方式

connection *sslConnect (void)
{
   connection *c;

   c = malloc (sizeof (connection));
   c->sslHandle = NULL;
   c->sslContext = NULL;

   c->socket = tcpConnect ();
   if (c->socket)
   {
    // Register the error strings for libcrypto & libssl
    SSL_load_error_strings ();
    // Register the available ciphers and digests
    SSL_library_init ();

    // New context saying we are a client, and using SSL 2 or 3
    c->sslContext = SSL_CTX_new (SSLv23_client_method ());
    if (c->sslContext == NULL)
    ERR_print_errors_fp (stderr);

    // Create an SSL struct for the connection
    c->sslHandle = SSL_new (c->sslContext);
    if (c->sslHandle == NULL)
    ERR_print_errors_fp (stderr);

    // Connect the SSL struct to our connection
    if (!SSL_set_fd (c->sslHandle, c->socket))
    ERR_print_errors_fp (stderr);

    // Initiate SSL handshake
    if (SSL_connect (c->sslHandle) != 1)
    ERR_print_errors_fp (stderr);
    }
    else
    {
     perror ("Connect failed");
    }

    return c;
}

非常感謝你。

創建非阻塞套接字是非阻塞連接的先決條件...

以下步驟總結:(請參閱下面鏈接的站點中的完整說明)

1)調用 fcntl() API 將套接字描述符的當前標志設置檢索到局部變量中。

2)在該局部變量中,設置 O_NONBLOCK(非阻塞)標志。 (注意不要篡改其他標志)

3)調用 fcntl() API 將描述符的標志設置為我們本地變量中的值。

在此處閱讀有關非阻塞套接字技術的更多信息

假設一個現有的套接字,以下實現上述步驟:

BOOL SetSocketBlockingEnabled(SOCKET fd, BOOL blocking)
{
     if (fd < 0) return FALSE;  
   #ifdef WIN32
       unsigned long mode = blocking ? 0 : 1;
       return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? TRUE : FALSE;
   #else
       int flags = fcntl(fd, F_GETFL, 0);
       if (flags < 0) return false;
       flags = blocking ? (flags&~O_NONBLOCK) : (flags|O_NONBLOCK);
       return (fcntl(fd, F_SETFL, flags) == 0) ? TRUE : FALSE;
   #endif
}

一旦你有一個非阻塞套接字,然后看這篇文章解釋如何做一個非阻塞連接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM