Code Monkey home page Code Monkey logo

libcoap-minimal's People

Contributors

jkrhb avatar mrdeep1 avatar obgm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

libcoap-minimal's Issues

Failed to compile using g++ 8.1

CXXFLAGS=$(call pkgconfig,--libs,$(LIBCOAP))

LDLIBS should be used instead of CXXFLAGS, using CXXFLAGS will result in the libraries added before the object files which leads to linker unable to find the corresponding libraries.

I also needed to change libcoap-2-openssl to libcoap-2 to find the library.

Send response to client upon request under dynamic setup

Hi All,

I was able to setup dynamic resource handling at server side. Now, I want to add response which can be sent to client.

In libcoap-minimal/server.cc, this is the code sending response to client

  resource = coap_resource_init(ruri, 0);
  coap_register_handler(resource, COAP_REQUEST_GET,
                        [](auto, auto,
                           const coap_pdu_t *request,
                           auto,
                           coap_pdu_t *response) {
                          coap_show_pdu(LOG_WARNING, request);
                          coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
                          coap_add_data(response, 5,
                                        (const uint8_t *)"world");
                          coap_show_pdu(LOG_WARNING, response);
                        });
  coap_add_resource(ctx, resource);

How can I use the same in dynamic resource? Looking forward for positive response from fellow developers.

Add hex data in minimal example

I need help in adding hex data in minimal example. If I am sending it as normal payload then some bytes are missing/corrupted.

Running the client first will throw an exception

Dear Contributors

Environment

  • libcoap version : [v4.3.1-91-g78304f4]
  • Build System: [CMake]
  • Operating System: [Windows]
  • Hosted Environment: [None]

Problem Description

As the title states,when I first run client.exe, an exception 0xC0000005 is thrown.

Expected Behavior

I expect that when running the client first, the client will wait until the server starts if the server does not start. Also, how can the client implement the ability to keep waiting for the server to come back online when it is not started or when it quits unexpectedly? I looked at the code in coap-client.c in the sample include libcoap and didn't find it. Maybe it's because I'm not good enough...
Also, I've been worried that pduh will cause a memory leak. If the client can't tell that the server is down, and then the client keeps generating pdu through the coap_new_pduh function, this results in a lot of pdu not being freed. Is there any other meansl to release it? Is there a function I should use to determine this and then release it manually? Please advise!

Actual Behavior

The client runs for a few seconds before the exception is thrown.

Steps to reproduce

  1. Run client.exe first.
  2. Wait a few seconds.
  3. The exception is then thrown

Code to reproduce this issue

I only changed this line of code.

diff --git a/client.cc b/client.cc
index a37d80f..946356a 100644
--- a/client.cc
+++ b/client.cc
@@ -25,7 +25,7 @@ main(void) {
   coap_set_log_level(LOG_WARNING);

   /* resolve destination address where server should be sent */
-  if (resolve_address("coap.me", "5683", &dst) < 0) {
+  if (resolve_address("localhost", "5683", &dst) < 0) {
     coap_log(LOG_CRIT, "failed to resolve address\n");
     goto finish;
   }

Debug Logs

I located the code that gave the error to the coap_io_process function,Under Debug, the call stack information is:
client.exe!_coap_socket_send�() (Unknown source:0)
client.exe!_coap_netif_dgrm_write�() (Unknown source:0)
client.exe!_coap_send_message_type�() (Unknown source:0)
kernel32.dll!77ccf989() (Unknown source:0)
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll] (Unknown source:0)
ntdll.dll!77e174b4() (Unknown source:0)
ntdll.dll!77e17484() (Unknown source:0)
client.exe!_coap_session_connected�() (Unknown source:0)

v:1 t:CON c:GET i:fce5 {} [ Uri-Path:hello ]
Apr 27 16:04:24.056 WARN ** [::1]:52327 <-> [::1]:5683 UDP : coap_socket_recv: ICMP: 远程主机强迫关闭了一个现有的连接。

Exception thrown at 0x00367F3F in client.exe: 0xC0000005: Access violation reading location 0x00000014.

Other items if possible

When I run the server first, then the client, everything works fine.
I tried the following modifications, but nothing worked:

diff --git a/client.cc b/client.cc
index a37d80f..823abb2 100644
--- a/client.cc
+++ b/client.cc
@@ -6,18 +6,43 @@
 #include <cstring>
 #include <cstdlib>
 #include <cstdio>
+#include <iostream>
+#include <thread>
+#include <atomic>
+#include <ctime>
+#include <iomanip>

 #include "common.hh"

 static int have_response = 0;
+constexpr int MAX_RETRIES = 1;
+int retries = 0;
+coap_pdu_t *pdu = nullptr;
+
+static int event_handler(coap_session_t *session, coap_event_t event) {
+  if (event == COAP_EVENT_MSG_RETRANSMITTED) {
+    retries++;
+    if (retries >= MAX_RETRIES) {
+      std::cout << "Reached max retries, waiting for server to start..." << std::endl;
+      coap_session_set_max_retransmit(session, -1);  // Disable further retransmissions
+      std::this_thread::sleep_for(std::chrono::seconds(5));  // Wait for 5 seconds
+      retries = 0;  // Reset the retries counter
+      coap_session_set_max_retransmit(session, COAP_DEFAULT_MAX_RETRANSMIT);  // Enable retransmissions
+      coap_send(session, pdu);  // Resend the PDU
+    }
+  }
+  std::cout << "Event: " << event << std::endl;
+  return 0;
+}

 int
 main(void) {
   coap_context_t  *ctx = nullptr;
-  coap_session_t *session = nullptr;
   coap_address_t dst;
+  coap_session_t *session = nullptr;
   coap_pdu_t *pdu = nullptr;
-  int result = EXIT_FAILURE;;
+  int result = EXIT_FAILURE;;
+  std::string textUrl = "hello";

   coap_startup();

@@ -25,7 +50,7 @@ main(void) {
   coap_set_log_level(LOG_WARNING);

   /* resolve destination address where server should be sent */
-  if (resolve_address("coap.me", "5683", &dst) < 0) {
+  if (resolve_address("localhost", "5683", &dst) < 0) {
     coap_log(LOG_CRIT, "failed to resolve address\n");
     goto finish;
   }
@@ -44,15 +69,6 @@ main(void) {
     coap_log(LOG_EMERG, "cannot create client session\n");
     goto finish;
   }
-
-  /* coap_register_response_handler(ctx, response_handler); */
-  coap_register_response_handler(ctx, [](auto, auto,
-                                         const coap_pdu_t *received,
-                                         auto) {
-                                        have_response = 1;
-                                        coap_show_pdu(LOG_WARNING, received);
-                                        return COAP_RESPONSE_OK;
-                                      });
   /* construct CoAP message */
   pdu = coap_pdu_init(COAP_MESSAGE_CON,
                       COAP_REQUEST_CODE_GET,
@@ -63,16 +79,52 @@ main(void) {
     goto finish;
   }

+  /* coap_register_response_handler(ctx, response_handler); */
+  coap_register_response_handler(ctx, [](auto, auto,
+                                         const coap_pdu_t *received,
+                                         auto) {
+                                        have_response = 1;
+                                        coap_show_pdu(LOG_WARNING, received);
+                                        return COAP_RESPONSE_OK;
+                                      });
+  coap_register_event_handler(ctx, event_handler);
+  coap_register_nack_handler(ctx, [](coap_session_t *session, const coap_pdu_t *sent,
+              const coap_nack_reason_t reason, const coap_mid_t mid) {
+                std::cout << "NACK received:"<< mid << std::endl;
+              });
+
+
   /* add a Uri-Path option */
-  coap_add_option(pdu, COAP_OPTION_URI_PATH, 5,
-                  reinterpret_cast<const uint8_t *>("hello"));
+  coap_add_option(pdu, COAP_OPTION_URI_PATH, 6,
+                  reinterpret_cast<const uint8_t *>("helloo"));

   coap_show_pdu(LOG_WARNING, pdu);
   /* and send the PDU */
   coap_send(session, pdu);

-  while (have_response == 0)
-    coap_io_process(ctx, COAP_IO_WAIT);
+  int attempts = 0;
+  int timestamp = 0;
+  while (attempts < 5)
+  {
+    coap_io_process(ctx, 1000);
+    std::this_thread::sleep_for(std::chrono::seconds(1));
+
+    auto time = std::time(nullptr);
+    std::cout  << "["<< std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S") << "] " << std::endl;
+    if(result < 0)
+      attempts++;
+  }

   result = EXIT_SUCCESS;
  finish:

[NULL] Version value updates

Note: Issue has been closed due to discovery of version mismatch of linux package from source repository

-[NULLIFIED]-

Values and Functions of Error:

COAP_LOG_WARN (Has become LOG_WARNING)
coap_log_warn() (Has become coap_log(LOG_WARNING, ...)
coap_log_err() (Has become coap_log(LOG_ERR, ...)
coap_log_emerg() (Has become coap_log(LOG_EMERG, ...)
coap_uri_into_options()

Terminal Error Output (on make all):

g++ -std=c++14 -Wall -Wextra   -c -o client.o client.cc
client.cc: In function ‘int main(int, char**)’:
client.cc:53:22: error: ‘COAP_LOG_WARN’ was not declared in this scope; did you mean ‘COAP_IO_WAIT’?
   53 |   coap_set_log_level(COAP_LOG_WARN); //COAP_LOG_WARN
      |                      ^~~~~~~~~~~~~
      |                      COAP_IO_WAIT
client.cc:58:5: error: ‘coap_log_warn’ was not declared in this scope; did you mean ‘coap_log_t’?
   58 |     coap_log_warn("Failed to parse uri %s\n", coap_uri);
      |     ^~~~~~~~~~~~~
      |     coap_log_t
client.cc:65:5: error: ‘coap_log_warn’ was not declared in this scope; did you mean ‘coap_log_t’?
   65 |     coap_log_warn("Failed to resolve address %*.*s\n", (int)uri.host.length,
      |     ^~~~~~~~~~~~~
      |     coap_log_t
client.cc:73:5: error: ‘coap_log_emerg’ was not declared in this scope; did you mean ‘coap_log_impl’?
   73 |     coap_log_emerg("cannot create libcoap context\n");
      |     ^~~~~~~~~~~~~~
      |     coap_log_impl
client.cc:88:5: error: ‘coap_log_emerg’ was not declared in this scope; did you mean ‘coap_log_impl’?
   88 |     coap_log_emerg("cannot create client session\n");
      |     ^~~~~~~~~~~~~~
      |     coap_log_impl
client.cc: In lambda function:
client.cc:101:51: error: ‘COAP_LOG_WARN’ is not captured
  101 |                                     coap_show_pdu(COAP_LOG_WARN, received);
      |                                                   ^~~~~~~~~~~~~
client.cc:94:35: note: the lambda has no capture-default
   94 |                                  [](auto, auto, const coap_pdu_t *received, auto) {
      |                                   ^
client.cc:53:22: note: ‘<typeprefixerror>COAP_LOG_WARN’ declared here
   53 |   coap_set_log_level(COAP_LOG_WARN); //COAP_LOG_WARN
      |                      ^~~~~~~~~~~~~
client.cc: In function ‘int main(int, char**)’:
client.cc:114:5: error: ‘coap_log_emerg’ was not declared in this scope; did you mean ‘coap_log_impl’?
  114 |     coap_log_emerg("cannot create PDU\n");
      |     ^~~~~~~~~~~~~~
      |     coap_log_impl
client.cc:119:9: error: ‘coap_uri_into_options’ was not declared in this scope; did you mean ‘coap_register_option’?
  119 |   len = coap_uri_into_options(&uri, &dst, &optlist, 1, scratch, sizeof(scratch));
      |         ^~~~~~~~~~~~~~~~~~~~~
      |         coap_register_option
client.cc:121:5: error: ‘coap_log_warn’ was not declared in this scope; did you mean ‘coap_log_t’?
  121 |     coap_log_warn("Failed to create options\n");
      |     ^~~~~~~~~~~~~
      |     coap_log_t
client.cc:128:7: error: ‘coap_log_warn’ was not declared in this scope; did you mean ‘coap_log_t’?
  128 |       coap_log_warn("Failed to add options to PDU\n");
      |       ^~~~~~~~~~~~~
      |       coap_log_t
client.cc:137:5: error: ‘coap_log_err’ was not declared in this scope; did you mean ‘coap_log_t’?
  137 |     coap_log_err("cannot send CoAP pdu\n");
      |     ^~~~~~~~~~~~
      |     coap_log_t
make: *** [<builtin>: client.o] Error 1

Run in Docker

I'm trying to run this in a Docker container, first locally and then deployed to a toy server in the Cloud. You can see my Dockerfile and tracking issue on my fork.

I originally thought it was a Docker for Mac networking issue but ruled it out because netcat over udp works just fine.

I'm a novice when it comes to C/C++ and but more importantly I've never done any UNIX socket programming, so the solution may be obvious. Any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.