warmcat / libwebsockets

canonical libwebsockets.org networking library
https://libwebsockets.org
Other
4.78k stars 1.49k forks source link

CLIENT_CONNECTION_ERROR: tls: error:00000001:lib(0):func(0):reason(1) #2878

Open sci-ripper opened 1 year ago

sci-ripper commented 1 year ago

Hi, I'm trying to connect to IBM Watson Text to Speech API's Websocket using libwebsockets. I based my code on a simple websocket client found on iamscottmoyers / simple-libwebsockets-example / client.c. I added support for SSL but I'm getting the following TLS error:

[2023/05/19 17:25:09:1697] N: lws_create_context: LWS: 4.3.2-unknown, NET CLI SRV H1 H2 WS ConMon IPv6-absent [2023/05/19 17:25:09:1767] N: lws_lc_tag: ++ [wsi|0|pipe] (1) [2023/05/19 17:25:09:1867] N: lws_lc_tag: ++ [vh|0|default||-1] (1) [2023/05/19 17:25:09:2017] N: lws_plat_vhost_tls_client_ctx_init: Imported 46 certs from plat store [2023/05/19 17:25:09:2067] N: lws_lc_tag: ++ [wsicli|0|WS/h1/default/api.eu-gb.text-to-speech.watson.cloud.] (1) [2023/05/19 17:25:09:2957] W: lws_plat_set_socket_options_ip: not implemented on windows platform [2023/05/19 17:25:09:3377] N: lws_gate_accepts: on = 0 [2023/05/19 17:25:09:3507] N: lws_gate_accepts: on = 0 [2023/05/19 17:25:09:3547] E: CLIENT_CONNECTION_ERROR: tls: error:00000001:lib(0):func(0):reason(1) [2023/05/19 17:25:09:3607] N: lws_gate_accepts: on = 0 [2023/05/19 17:25:09:3637] N: lws_lc_untag: -- [wsicli|0|WS/h1/default/api.eu-gb.text-to-speech.watson.cloud.] (0) 157.014ms [2023/05/19 17:25:14:1987] N: lws_lc_tag: ++ [wsicli|1|WS/h1/default/api.eu-gb.text-to-speech.watson.cloud.] (1) [2023/05/19 17:25:14:2907] W: lws_plat_set_socket_options_ip: not implemented on windows platform [2023/05/19 17:25:14:3237] N: lws_gate_accepts: on = 0 [2023/05/19 17:25:14:3367] N: lws_gate_accepts: on = 0 [2023/05/19 17:25:14:3397] E: CLIENT_CONNECTION_ERROR: tls: error:00000001:lib(0):func(0):reason(1) [2023/05/19 17:25:14:3447] N: lws_gate_accepts: on = 0 [2023/05/19 17:25:14:3477] N: lws_lc_untag: -- [wsicli|1|WS/h1/default/api.eu-gb.text-to-speech.watson.cloud.] (0) 149.018ms

My code is as follows:


static struct lws_protocols protocols[] =
{
    {
        "wss",
        callback_example,
        0,
        EXAMPLE_RX_BUFFER_BYTES,
    },
    { NULL, NULL, 0, 0 } /* terminator */
};

int main( int argc, char *argv[] )
{
    struct lws_context_creation_info info;
    memset( &info, 0, sizeof(info) );

    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = protocols;
    info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
    info.gid = -1;
    info.uid = -1;

    struct lws_context *context = lws_create_context( &info );

    time_t old = 0;
    while( 1 )
    {
        struct timeval tv;
        gettimeofday( &tv, NULL );

        /* Connect if we are not connected to the server. */
        if( !web_socket && tv.tv_sec != old )
        {
            struct lws_client_connect_info ccinfo = {0};
            ccinfo.context = context;
            ccinfo.port = 443;
            ccinfo.address = "api.eu-gb.text-to-speech.watson.cloud.ibm.com";
            ccinfo.path = "/instances/{instance_id}/v1/synthesize?access_token={access_token}&voice=en-US_AllisonV3Voice";
            ccinfo.host = lws_canonical_hostname( context );
            ccinfo.origin = "origin";
            ccinfo.protocol = protocols[0].name;
            ccinfo.ssl_connection   = LCCSCF_USE_SSL | LCCSCF_PIPELINE;
            web_socket = lws_client_connect_via_info(&ccinfo);
        }

        if( tv.tv_sec != old )
        {
            /* Send a random number to the server every second. */
            lws_callback_on_writable( web_socket );
            old = tv.tv_sec;
        }

        lws_service( context, /* timeout_ms = */ 250 );
    }

    lws_context_destroy( context );

    return 0;
}

Thanks in advance!

ljluestc commented 5 months ago

#include <libwebsockets.h>
#include <string.h>
#include <sys/time.h>

#define EXAMPLE_RX_BUFFER_BYTES (100)

static struct lws *web_socket = NULL;

static int callback_example(struct lws *wsi, enum lws_callback_reasons reason,
                            void *user, void *in, size_t len)
{
    // Add your callback handling code here
    return 0;
}

static struct lws_protocols protocols[] =
{
    {
        "wss",
        callback_example,
        0,
        EXAMPLE_RX_BUFFER_BYTES,
    },
    { NULL, NULL, 0, 0 } /* terminator */
};

int main( int argc, char *argv[] )
{
    struct lws_context_creation_info info;
    memset( &info, 0, sizeof(info) );

    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = protocols;
    info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
    info.gid = -1;
    info.uid = -1;

    struct lws_context *context = lws_create_context( &info );

    time_t old = 0;
    while( 1 )
    {
        struct timeval tv;
        gettimeofday( &tv, NULL );

        /* Connect if we are not connected to the server. */
        if( !web_socket && tv.tv_sec != old )
        {
            struct lws_client_connect_info ccinfo = {0};
            ccinfo.context = context;
            ccinfo.port = 443;
            ccinfo.address = "api.eu-gb.text-to-speech.watson.cloud.ibm.com";
            ccinfo.path = "/instances/{instance_id}/v1/synthesize?access_token={access_token}&voice=en-US_AllisonV3Voice";
            ccinfo.host = lws_canonical_hostname( context );
            ccinfo.origin = "origin";
            ccinfo.protocol = protocols[0].name;
            ccinfo.ssl_connection  = LCCSCF_USE_SSL | LCCSCF_PIPELINE;
            // Additional SSL options or certificate handling can be added here if needed
            web_socket = lws_client_connect_via_info(&ccinfo);
        }

        if( tv.tv_sec != old )
        {
            /* Send a random number to the server every second. */
            lws_callback_on_writable( web_socket );
            old = tv.tv_sec;
        }

        lws_service( context, /* timeout_ms = */ 250 );
    }

    lws_context_destroy( context );

    return 0;
}