libcoap icon indicating copy to clipboard operation
libcoap copied to clipboard

Server is unreachable when started inside a class

Open sixdenip opened this issue 5 years ago • 5 comments

Environment

  • libcoap version (run git describe --tags to find it): 4.3.0rc2

    // v4.3.0-rc3-41-g25fe796

  • Build System: CMake

  • Operating System: Linux

  • Operating System Version: Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-140-generic x86_64)

  • Hosted Environment: None

Problem Description

I'm writing a customable CoAP API for embedded targets. I used the minimal coap example as a test and it work well. Now for development I basically wrapped up the minimal coap example and added some logic. When I start the server and enter my application enter its loop, it is unreachable by the client from the minimal coap example. My client and server are executed on the same host

Expected Behavior

I expect the server to be reachable by a client after launch.

Actual Behavior

The server is not reachable by the client

Steps to reproduce

  1. Initialize a server inside the constructor of a class
  2. Try to reach it with a client

Code to reproduce this issue

Please find my trimmed minimal example to reproduce this issue main.cpp

Debug Logs

From the client, with DEBG level :

v:1 t:CON c:GET i:0d28 {} [ Observe:0, Uri-Path:/hello ]
Jun 29 23:02:17.216 DEBG *  [::1]:38258 <-> [::1]:5683 UDP : sent 12 bytes
v:1 t:CON c:GET i:0d28 {} [ Observe:0, Uri-Path:/hello ]
Jun 29 23:02:17.216 DEBG ** [::1]:38258 <-> [::1]:5683 UDP : mid=0xd28: added to retransmit queue (2594ms)
Jun 29 23:02:17.216 WARN coap_network_read: unreachable
Jun 29 23:02:17.216 DEBG ***[::1]:38258 <-> [::1]:5683 UDP : session disconnected (reason 4)
Jun 29 23:02:19.810 DEBG ** [::1]:38258 <-> [::1]:5683 UDP : mid=0xd28: retransmission #1
Jun 29 23:02:19.810 DEBG *  [::1]:38258 <-> [::1]:5683 UDP : sent 12 bytes
v:1 t:CON c:GET i:0d28 {} [ Observe:0, Uri-Path:/hello ]
Jun 29 23:02:19.810 WARN coap_network_read: unreachable
Jun 29 23:02:19.810 DEBG ***[::1]:38258 <-> [::1]:5683 UDP : session disconnected (reason 4)
Jun 29 23:02:24.998 DEBG ** [::1]:38258 <-> [::1]:5683 UDP : mid=0xd28: retransmission #2
Jun 29 23:02:24.998 DEBG *  [::1]:38258 <-> [::1]:5683 UDP : sent 12 bytes
v:1 t:CON c:GET i:0d28 {} [ Observe:0, Uri-Path:/hello ]
Jun 29 23:02:24.998 WARN coap_network_read: unreachable
Jun 29 23:02:24.998 DEBG ***[::1]:38258 <-> [::1]:5683 UDP : session disconnected (reason 4)
Jun 29 23:02:35.374 DEBG ** [::1]:38258 <-> [::1]:5683 UDP : mid=0xd28: retransmission #3
Jun 29 23:02:35.374 DEBG *  [::1]:38258 <-> [::1]:5683 UDP : sent 12 bytes

From the server, nothing is displayed in the log with DEBG level

Thanks for your help

sixdenip avatar Jun 29 '21 21:06 sixdenip

The primary change you need to make (remove the leading /) is

-    api.AddResource("/hello",0);
+    api.AddResource("hello",0);

COAP_RESPONSE_CODE_OK actually is not a valid CoAP response code - it will be removed in the future. Typically COAP_RESPONSE_CODE_CONTENT is used in a response to a GET.

Otherwise, this works for me for 4.3.0 (no data is returned in the response because you have not defined any). I would recommend that you upgrade to the latest release - version 4.3.0. This does change the parameters for coap_method_handler_t (See Wiki)).

mrdeep1 avatar Jun 30 '21 10:06 mrdeep1

Thanks for your help and hints. In the client I changed "localhost" in the resolve_address function call by "127.0.0.1" and it worked. However another strange thing happened : When used with the test server I had derived from the minimal example, if "127.0.0.1" is used as the target on the client it doesn't work, but when I use "localhost" it works. Could this be caused by the address solver function ? Or is this issue specific to my network configuration ?

sixdenip avatar Jun 30 '21 12:06 sixdenip

"localhost" resolving to IP address is usually based on what DNS resolves to (could be /etc/hosts). DNS is entitled to return the results to a lookup to be in a different order to the next time a lookup is done against the same name.

If you want the server to listen for traffic coming from any IP, then I would use "::" and libcoap will then accept traffic coming to any IP address that is configured for that host. If you want to to restrict the server to listen for either the IPv4 and IPv6 localhost addresses, then you need to set up 2 endpoints - one for "127.0.0.1" and "::1" associated with the same coap_context.

To see what the server is listening on, you can do netstat -an | grep 568 .

mrdeep1 avatar Jun 30 '21 20:06 mrdeep1

@sixdenip Are things working OK for you now? If so, please close off this issue.

mrdeep1 avatar Jul 05 '21 10:07 mrdeep1

Sorry I wasn't working on this last week. So far I haven't been able to test it extensively but my issue seems to be specific to my host network configuration and I managed to bypass the issue by using the loopback IP directly. Thanks a lot for those tips on libcoap networking. I'll close this issue. I'll reopen one if I find something that could be of interest.

sixdenip avatar Jul 05 '21 13:07 sixdenip