obgm / libcoap

A CoAP (RFC 7252) implementation in C
Other
777 stars 421 forks source link

Enable application control over .well-known/core response #1428

Closed anyc closed 1 month ago

anyc commented 1 month ago

As described in #1425, this PR makes it possible for the application to control what is returned for .well-known/core requests.

anyc commented 1 month ago

I do not understand what the last documentation issue is about.

mrdeep1 commented 1 month ago

I do not understand what the last documentation issue is about.

You need to update the man page checker with

diff --git a/man/examples-code-check.c b/man/examples-code-check.c
index f242671b..9cf997d2 100644
--- a/man/examples-code-check.c
+++ b/man/examples-code-check.c
@@ -125,6 +125,7 @@ const char *number_list[] = {
   "coap_pdu_type_t ",
   "coap_mid_t ",
   "coap_pdu_code_t ",
+  "coap_print_status_t ",
   "coap_proto_t ",
   "coap_session_state_t ",
   "coap_session_type_t ",

Then run man/examples-code-check man to check that all is now working..

mrdeep1 commented 1 month ago

@anyc it would be good to get the code updated, squashed into 1 commit ready to be merged so it can be added into 4.3.5rc1 which will be happening shortly.

This is needed for any reverse proxy to pass on .well-known/core.

anyc commented 1 month ago

Ah, I thought it would be easier to review if I split it into smaller commits. I pushed a new commit where only the coap_handler issue remains unsolved.

anyc commented 1 month ago

But it seems it does not work anymore with the new locking changes:

../git/src/coap_net.c:2727: get_wkc_len: Assertion `(context) && coap_thread_pid == ((context)->lock.being_freed ? (context)->lock.freeing_pid : (context)->lock.pid)' failed.

(gdb) back
#0  __pthread_kill_implementation (threadid=<optimized out>, signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
#1  0x76c7a390 in __pthread_kill_internal (signo=6, threadid=<optimized out>) at pthread_kill.c:78
#2  0x76c36e70 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#3  0x76c215f8 in __GI_abort () at abort.c:79
#4  0x76c30308 in __assert_fail_base (fmt=0x76d4e220 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", 
    assertion=0x76f78d10 "(context) && coap_thread_pid == ((context)->lock.being_freed ? (context)->lock.freeing_pid : (context)->lock.pid)", 
    assertion@entry=0x76ff2020 "", file=0x76f7b154 "../git/src/coap_net.c", file@entry=0x76d4e220 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", 
    line=line@entry=2727, function=0x76f7c6bc <__PRETTY_FUNCTION__.3> "get_wkc_len", function@entry=0x76f7b154 "../git/src/coap_net.c") at assert.c:92
#5  0x76c303ac in __GI___assert_fail (assertion=0x76ff2020 "", file=0x76d4e220 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", line=line@entry=2727, 
    function=0x76f7b154 "../git/src/coap_net.c") at assert.c:101
#6  0x76f4af58 in get_wkc_len (query_filter=0x0, session=0x76d4e220, context=0x76ff2020) at ../git/src/coap_net.c:2727
#7  hnd_get_wellknown (resource=0x0, session=0x76d4e220, request=0x80, query=0x0, response=0x4baec0) at ../git/src/coap_net.c:2763
#8  0x76f4fea8 in handle_request (context=0x768d9b9c, context@entry=0x443c90, session=session@entry=0x4abc38, pdu=0xffffffe8, pdu@entry=0x4b0398)
    at ../git/src/coap_net.c:3446
#9  0x76f518c4 in coap_dispatch (context=context@entry=0x443c90, session=session@entry=0x4abc38, pdu=pdu@entry=0x4b0398) at ../git/src/coap_net.c:4148
#10 0x76f528d4 in coap_handle_dgram (ctx=0x443c90, session=session@entry=0x4abc38, 
    msg=msg@entry=0x7effebf4 "B\001y;QW\273.well-known\004core~@\354\377~e3c:de0", msg_len=23) at ../git/src/coap_net.c:2471
#11 0x76f59bec in coap_dtls_receive (session=session@entry=0x4abc38, data=<optimized out>, data_len=<optimized out>) at ../git/src/coap_openssl.c:3621
#12 0x76f52a44 in coap_handle_dgram_for_proto (ctx=ctx@entry=0x443c90, session=session@entry=0x4abc38, packet=packet@entry=0x7efff1f8)
    at ../git/src/coap_net.c:1947
#13 0x76f52b5c in coap_read_endpoint (ctx=0x443c90, endpoint=endpoint@entry=0x467170, now=<optimized out>) at ../git/src/coap_net.c:2232
#14 0x76f533cc in coap_io_do_epoll_lkd (ctx=ctx@entry=0x443c90, events=events@entry=0x7efff898, nevents=nevents@entry=1) at ../git/src/coap_net.c:2359
#15 0x76f4a808 in coap_io_process_with_fds_lkd (ctx=ctx@entry=0x443c90, timeout_ms=<optimized out>, timeout_ms@entry=4294967295, enfds=enfds@entry=0, 
    ereadfds=ereadfds@entry=0x0, ewritefds=ewritefds@entry=0x0, eexceptfds=eexceptfds@entry=0x0) at ../git/src/coap_io.c:1731
#16 0x76f4aa40 in coap_io_process_lkd (ctx=ctx@entry=0x443c90, timeout_ms=timeout_ms@entry=4294967295) at ../git/src/coap_io.c:1527
#17 0x76f4aa8c in coap_io_process (ctx=0x443c90, timeout_ms=timeout_ms@entry=4294967295) at ../git/src/coap_io.c:1520
anyc commented 1 month ago

Maybe this helps:

(gdb) print(context)
$1 = (coap_context_t *) 0x76ff2020
(gdb) print(coap_thread_pid) 
No symbol "coap_thread_pid" in current context.
(gdb) print((context)->lock.freeing_pid)
$2 = 0
(gdb) print((context)->lock.pid)
$3 = 0
mrdeep1 commented 1 month ago

Looking at why locking is currently failing with your code.

mrdeep1 commented 1 month ago

I can see the issue. hnd_get_wellknown() is being treated as an application handler, not as an internal libcoap handler. Thinking through how best to handle this as context does need to be locked (for multi-thread safety) as context->resources is getting iterated through in coap_print_wellknown_lkd().

mrdeep1 commented 1 month ago

Try this fix well_known.patch.zip

anyc commented 1 month ago

Seems to work, thank you. I also added the request object as an additional parameter to the callback signature as I need this if I am going to use async functions.

mrdeep1 commented 1 month ago

Looks good - thanks for getting this together.