[Libwebsockets] new context_info structs

Jack Mitchell ml at communistcode.co.uk
Mon Feb 11 16:55:34 CET 2013


On 11/02/13 15:47, "Andy Green (林安廸)" wrote:
> On 11/02/13 23:38, the mail apparently from Jack Mitchell included:
>> On 11/02/13 15:14, "Andy Green (林安廸)" wrote:
>>> On 11/02/13 23:09, the mail apparently from Jack Mitchell included:
>>>> Hi,
>>>>
>>>> I am having issues trying to create my contexts properly (I think) 
>>>> with
>>>> the new struct layout.
>>>>
>>>> I have the following:
>>>>
>>>> static struct libwebsocket_protocols generic_protocol[] =
>>>> {
>>>>     {"http-only", callback_http, 0, 0},
>>>>     {"send-receive-value", webSock_genericSendRecieve, sizeof(struct
>>>> per_session_data_genericSendRecieve), RX_BUFFER_SIZE},
>>>>     {NULL, NULL, 0, 0},
>>>> };
>>>>
>>>> card {
>>>>        .context_info =
>>>>        {
>>>>           .port = PORT_BASE_ADDRESS,
>>>>           .protocols = generic_protocol,
>>>>           .extensions = libwebsocket_internal_extensions,
>>>>           .gid = -1,
>>>>           .uid = -1,
>>>>        },
>>>> }
>>>>
>>>> libwebsocket_create_context(&card->context_info);
>>>>
>>>> However, when I run my application I get a segfault, under GDB it 
>>>> points
>>>> to this:
>>>>
>>>> [946686144:9408] NOTICE: Initial logging level 7
>>>> [946686144:9426] NOTICE: Library version: 1.1 224149a
>>>> [946686144:9445] NOTICE:  static allocation: 5460 + (12 x 1024 fds) =
>>>> 17748 bytes
>>>> [946686144:9500] NOTICE:  canonical_hostname = xx
>>>> [946686144:9524] NOTICE:  Compiled without SSL support
>>>> [946686144:9543] NOTICE:  per-conn mem: 172 + 1328 headers + 
>>>> protocol rx
>>>> buf
>>>> [946686144:9557] NOTICE:  Listening on port 8000
>>>>
>>>>
>>>> Program received signal SIGSEGV, Segmentation fault.
>>>> libwebsocket_callback_on_writable_all_protocol
>>>> (protocol=protocol at entry=0x1d370 <systemConf+96>)
>>>>      at libwebsockets.c:1342
>>>> 1342        for (n = 0; n < context->fds_count; n++) {
>>>>
>>>> It looks like I am initialising something wrong but I'm not sure 
>>>> what...
>>>>
>>>> Does this set off any alarm bells. Am I initialising everything 
>>>> fully in
>>>> the .context_info struct, do I need to put all the SSL related
>>>> parameters and such in there?
>>>
>>> The test server is updated to match, you can use that as a reference.
>>>
>>> If you're creating the info on the stack, make sure you memset it to 0
>>> and then fill in the nonzero / non-NULL members.  Even the things you
>>> don't care about will need to be NULL or 0 and not random stack 
>>> contents.
>>>
>>> -Andy
>>>
>>>
>>>
>>
>> Hi Andy,
>>
>> I ensured that all the context_info is created beforehand, it's not on
>> the stack, it allocated at compile:
>>
>> static struct libwebsocket_protocols generic_protocol[] =
>> {
>>     {"http-only", callback_http, 0, 0},
>>     {"send-receive-value", webSock_genericSendRecieve, sizeof(struct
>> per_session_data_genericSendRecieve), RX_BUFFER_SIZE},
>>     {NULL, NULL, 0, 0},
>> };
>>
>> static t_systemConfig systemConf =
>> {
>>
>>     .card[0] =
>>     {
>>        .context_info =
>>        {
>>           .port = PORT_BASE_ADDRESS + 0,
>>           .interface = NULL,
>>           .protocols = generic_protocol,
>>           .extensions = libwebsocket_internal_extensions,
>>           .ssl_cert_filepath = NULL,
>>           .ssl_private_key_filepath = NULL,
>>           .ssl_ca_filepath = NULL,
>>           .gid = -1,
>>           .uid = -1,
>>           .options = 0,
>>           .user = NULL,
>>           .ka_time = 5,
>>           .ka_probes = 5,
>>           .ka_interval = 1,
>>        },
>>     },
>> }
>>
>> GDB:
>>
>> [946687684:2292] NOTICE: Initial logging level 15
>> [946687684:2293] NOTICE: Library version: 1.1 224149a
>> [946687684:2294] INFO:  LWS_MAX_HEADER_NAME_LENGTH: 64
>> [946687684:2295] INFO:  LWS_MAX_HEADER_LEN: 1024
>> [946687684:2296] INFO:  LWS_MAX_PROTOCOLS: 5
>> [946687684:2297] INFO:  LWS_MAX_EXTENSIONS_ACTIVE: 3
>> [946687684:2297] INFO:  SPEC_LATEST_SUPPORTED: 13
>> [946687684:2298] INFO:  AWAITING_TIMEOUT: 5
>> [946687684:2299] INFO:  CIPHERS_LIST_STRING: 'DEFAULT'
>> [946687684:2300] INFO:  SYSTEM_RANDOM_FILEPATH: '/dev/urandom'
>> [946687684:2301] INFO:  LWS_MAX_ZLIB_CONN_BUFFER: 65536
>> [946687684:2303] NOTICE:  static allocation: 5460 + (12 x 1024 fds) =
>> 17748 bytes
>> [946687684:2361] NOTICE:  canonical_hostname = xx
>> [946687684:2385] NOTICE:  Compiled without SSL support
>> [946687684:2403] NOTICE:  per-conn mem: 172 + 1328 headers + protocol rx
>> buf
>> [946687684:2422] INFO: insert_wsi_socket_into_fds: wsi=0x1b91c8,
>> sock=14, fds pos=0
>> [946687684:2441] NOTICE:  Listening on port 8000
>>
>>
>> Program received signal SIGSEGV, Segmentation fault.
>> libwebsocket_callback_on_writable_all_protocol
>> (protocol=protocol at entry=0x1d374 <systemConf+96>)
>>      at libwebsockets.c:1342
>> 1342        for (n = 0; n < context->fds_count; n++) {
>> (gdb) bt
>> #0  libwebsocket_callback_on_writable_all_protocol
>> (protocol=protocol at entry=0x1d374 <systemConf+96>)
>>      at libwebsockets.c:1342
>> #1  0x0000e0ac in webSock_broadcastJsonObject (jsonObj=0x1b9b48,
>> card=card at entry=0x1d314 <systemConf>)
>>      at webInterfaces/webInterface_webSockets.c:250
>> #2  0x0000f408 in controlWeb_setCardStatus (card=card at entry=0x1d314
>> <systemConf>, status=status at entry=CARD_AVAILABLE)
>>      at controlWeb/controlWeb.c:90
>> #3  0x0000a548 in r0005_initCard (card=0x1d314 <systemConf>) at 
>> R0005.c:197
>> #4  0x0000a1c0 in main () at R0005.c:65
>>
>>
>> Valgrind:
>>
>> [946688211:2171] NOTICE: Initial logging level 15
>> [946688211:2196] NOTICE: Library version: 1.1 224149a
>> [946688211:2226] INFO:  LWS_MAX_HEADER_NAME_LENGTH: 64
>> [946688211:2244] INFO:  LWS_MAX_HEADER_LEN: 1024
>> [946688211:2262] INFO:  LWS_MAX_PROTOCOLS: 5
>> [946688211:2279] INFO:  LWS_MAX_EXTENSIONS_ACTIVE: 3
>> [946688211:2296] INFO:  SPEC_LATEST_SUPPORTED: 13
>> [946688211:2312] INFO:  AWAITING_TIMEOUT: 5
>> [946688211:2327] INFO:  CIPHERS_LIST_STRING: 'DEFAULT'
>> [946688211:2345] INFO:  SYSTEM_RANDOM_FILEPATH: '/dev/urandom'
>> [946688211:2362] INFO:  LWS_MAX_ZLIB_CONN_BUFFER: 65536
>> [946688211:2383] NOTICE:  static allocation: 5460 + (12 x 1024 fds) =
>> 17748 bytes
>> [946688211:2608] NOTICE:  canonical_hostname = 
>> 97-103-108-101.res.bhn.net
>> [946688211:2631] NOTICE:  Compiled without SSL support
>> [946688211:2651] NOTICE:  per-conn mem: 172 + 1328 headers + protocol rx
>> buf
>> [946688211:2677] INFO: insert_wsi_socket_into_fds: wsi=0x4b08de0,
>> sock=14, fds pos=0
>> [946688211:2717] NOTICE:  Listening on port 8000
>>
>> ==470== Invalid read of size 4
>> ==470==    at 0x498BD34: libwebsocket_callback_on_writable_all_protocol
>> (libwebsockets.c:1342)
>> ==470==    by 0xE0AB: webSock_broadcastJsonObject
>> (webInterface_webSockets.c:250)
>> ==470==    by 0xF407: controlWeb_setCardStatus (controlWeb.c:90)
>> ==470==    by 0xA547: r0005_initCard (R0005.c:197)
>> ==470==    by 0xA1BF: main (R0005.c:65)
>> ==470==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
>> ==470==
>> ==470==
>> ==470== Process terminating with default action of signal 11 (SIGSEGV)
>> ==470==  Access not within mapped region at address 0x8
>> ==470==    at 0x498BD34: libwebsocket_callback_on_writable_all_protocol
>> (libwebsockets.c:1342)
>> ==470==    by 0xE0AB: webSock_broadcastJsonObject
>> (webInterface_webSockets.c:250)
>> ==470==    by 0xF407: controlWeb_setCardStatus (controlWeb.c:90)
>> ==470==    by 0xA547: r0005_initCard (R0005.c:197)
>> ==470==    by 0xA1BF: main (R0005.c:65)
>
> I guess the "owning server" of the protocol is NULL
>
> Can you change the log here (libwebsockets.c:2112) to lwsl_notice and 
> see if this is actually getting executed
>
>
>     /* initialize supported protocols */
>
>     for (context->count_protocols = 0;
>         info->protocols[context->count_protocols].callback;
>                            context->count_protocols++) {
>
>         lwsl_parser("  Protocol: %s\n",
> info->protocols[context->count_protocols].name);
>
> info->protocols[context->count_protocols].owning_server =
>                                     context;
> info->protocols[context->count_protocols].protocol_index =
>                                context->count_protocols;
>
>         /*
>          * inform all the protocols that they are doing their one-time
>          * initialization if they want to
>          */
> info->protocols[context->count_protocols].callback(context,
>                    NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
>     }
>
> Are we checking if the context that was created is NULL, ie, it failed?
>
> -Andy
>

Yes, check for NULL is in place.

With modification:

[946689226:8809] NOTICE:   Protocol: http-only
[946689226:8811] NOTICE:   Protocol: send-receive-value

Regards,

-- 

   Jack Mitchell (jack at embed.me.uk)
   Embedded Systems Engineer
   http://www.embed.me.uk

--




More information about the Libwebsockets mailing list