Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: UDP heartbeat(ping/pong) not available #723

Closed
wants to merge 1 commit into from
Closed

fix: UDP heartbeat(ping/pong) not available #723

wants to merge 1 commit into from

Conversation

wfeii1980
Copy link

解决的两个问题:

  1. UDP协议下ping/pong不能有效工作的问题。
  2. 服务端断开客户端ping包超时情况下,重建连接后再触发心跳会直接超时。

@obgm
Copy link
Owner

obgm commented Jul 13, 2021

May I kindly ask you to provide the contents of this pull request in English. Also, a descriptive commit message is missing, and you might want to pick a committer user name that simplifies attribution to you.

@obgm
Copy link
Owner

obgm commented Jul 13, 2021

Good practice also is to include a recipe in the PR description how to reproduce the issue to be solved.

@@ -384,7 +384,7 @@ coap_mid_t coap_session_send_ping(coap_session_t *session) {
return COAP_INVALID_MID;
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
uint16_t mid = coap_new_message_id (session);
ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
ping = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PING, mid, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COAP_SIGNALING_CODE_PING is only valid for TCP, not UDP, so existing code is correct.

As per https://datatracker.ietf.org/doc/html/rfc7252#section-4.3

   Summarizing Sections 4.2 and 4.3, the four message types can be used
   as in Table 1.  "*" means that the combination is not used in normal
   operation but only to elicit a Reset message ("CoAP ping").

For UDP, an empty request is sent (Ping), and the RST response acknowledges the Ping (Pong).

@@ -2956,7 +2956,7 @@ handle_signaling(coap_context_t *context, coap_session_t *session,
if (session->state == COAP_SESSION_STATE_CSM)
coap_session_connected(session);
} else if (pdu->code == COAP_SIGNALING_CODE_PING) {
coap_pdu_t *pong = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PONG, 0, 1);
coap_pdu_t *pong = coap_pdu_init(COAP_MESSAGE_ACK, COAP_SIGNALING_CODE_PONG, pdu->mid, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As COAP_SIGNALING_CODE_PING is only for TCP it actually does not matter wheth the message type and message id are defined as these ar enot sent over a TCP connection because it is reliable.

@wfeii1980
Copy link
Author

Thank you, mrdeep1

I didn't read the RFC document carefully. After retesting, I determined that the Ping / Pong packet format of UDP was correct and didn't need to be modified.

My problems are:

  1. When a session has been established between the client and the server, Shut down the server.
  2. Client Ping timeout, disconnect session.
  3. Restart the server.
  4. Rebuild session between client and server.
  5. Problem: heartbeat will not be triggered again!

The reason, I think, is the function coap_session_disconnected in no set last_ping=0.

@mrdeep1
Copy link
Collaborator

mrdeep1 commented Jul 18, 2021

If the client application does not have a nack handler, or does not destroy the existing session on receipt of COAP_NACK_NOT_DELIVERABLE, then your change to coap_session_disconnected() causes the ping to continue to be sent at ping_timeout intervals. If the server then starts to respond everything continues as expected.

@wfeii1980
Copy link
Author

$ ./coap-client -v 7 -m put coap://127.0.0.1/example_data -K 30
Jul 19 16:55:21.956 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : new outgoing session
Jul 19 16:55:21.956 DEBG sending CoAP request:
Jul 19 16:55:21.956 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 17 bytes
v:1 t:CON c:PUT i:1414 {} [ Uri-Path:example_data ]
Jul 19 16:55:21.956 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1414: added to retransmit queue (2000ms)
Jul 19 16:55:21.956 DEBG timeout is set to 90 seconds
Jul 19 16:55:21.956 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : received 4 bytes
v:1 t:ACK c:2.01 i:1414 {} [ ]
Jul 19 16:55:21.956 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1414: removed
Jul 19 16:55:21.956 DEBG ** process incoming 2.01 response:
Jul 19 16:55:51.957 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 4 bytes
v:1 t:CON c:0.00 i:1415 {} [ ]
Jul 19 16:55:51.958 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1415: added to retransmit queue (2250ms)
Jul 19 16:55:51.958 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : received 4 bytes
v:1 t:RST c:0.00 i:1415 {} [ ]
Jul 19 16:55:51.958 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1415: removed

[coap-server close] =========================================================================================

Jul 19 16:56:21.959 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 4 bytes
v:1 t:CON c:0.00 i:1416 {} [ ]
Jul 19 16:56:21.959 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1416: added to retransmit queue (2156ms)
Jul 19 16:56:21.959 WARN coap_network_read: unreachable
Jul 19 16:56:21.959 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:56:24.115 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1416: retransmission #1
Jul 19 16:56:24.115 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 4 bytes
v:1 t:CON c:0.00 i:1416 {} [ ]
Jul 19 16:56:24.115 WARN coap_network_read: unreachable
Jul 19 16:56:24.115 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:56:28.427 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1416: retransmission #2
Jul 19 16:56:28.427 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 4 bytes
v:1 t:CON c:0.00 i:1416 {} [ ]
Jul 19 16:56:28.427 WARN coap_network_read: unreachable
Jul 19 16:56:28.427 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:56:37.052 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1416: retransmission #3
Jul 19 16:56:37.052 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 4 bytes
v:1 t:CON c:0.00 i:1416 {} [ ]
Jul 19 16:56:37.052 WARN coap_network_read: unreachable
Jul 19 16:56:37.052 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:56:54.300 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1416: retransmission #4
Jul 19 16:56:54.300 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 4 bytes
v:1 t:CON c:0.00 i:1416 {} [ ]
Jul 19 16:56:54.300 WARN coap_network_read: unreachable
Jul 19 16:56:54.300 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:57:13.322 DEBG sending CoAP request:
Jul 19 16:57:13.322 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 17 bytes
v:1 t:CON c:PUT i:1417 {} [ Uri-Path:example_data ]
Jul 19 16:57:13.322 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1417: added to retransmit queue (21022ms)
Jul 19 16:57:13.322 DEBG timeout is set to 90 seconds
Jul 19 16:57:13.322 WARN coap_network_read: unreachable
Jul 19 16:57:13.322 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:57:15.323 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1417: retransmission #1
Jul 19 16:57:15.323 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 17 bytes
v:1 t:CON c:PUT i:1417 {} [ Uri-Path:example_data ]
Jul 19 16:57:15.323 WARN coap_network_read: unreachable
Jul 19 16:57:15.323 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:57:19.324 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1417: retransmission #2
Jul 19 16:57:19.324 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 17 bytes
v:1 t:CON c:PUT i:1417 {} [ Uri-Path:example_data ]
Jul 19 16:57:19.324 WARN coap_network_read: unreachable
Jul 19 16:57:19.324 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:57:27.324 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1417: retransmission #3
Jul 19 16:57:27.324 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 17 bytes
v:1 t:CON c:PUT i:1417 {} [ Uri-Path:example_data ]
Jul 19 16:57:27.324 WARN coap_network_read: unreachable
Jul 19 16:57:27.324 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:57:28.796 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1416: give up after 4 attempts
Jul 19 16:57:43.324 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1417: retransmission #4
Jul 19 16:57:43.325 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 17 bytes
v:1 t:CON c:PUT i:1417 {} [ Uri-Path:example_data ]
Jul 19 16:57:43.325 WARN coap_network_read: unreachable
Jul 19 16:57:43.325 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 4)
Jul 19 16:58:13.326 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 1)
Jul 19 16:58:13.326 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1417: removed
Jul 19 16:58:14.327 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 1)
...

[coap-server open] =========================================================================================

...
Jul 19 16:59:27.412 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 1)
Jul 19 16:59:28.413 DEBG sending CoAP request:
Jul 19 16:59:28.413 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 17 bytes
v:1 t:CON c:PUT i:1418 {} [ Uri-Path:example_data ]
Jul 19 16:59:28.413 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1418: added to retransmit queue (2531ms)
Jul 19 16:59:28.413 DEBG timeout is set to 90 seconds
Jul 19 16:59:28.414 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : received 4 bytes
v:1 t:ACK c:2.01 i:1418 {} [ ]
Jul 19 16:59:28.414 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1418: removed
Jul 19 16:59:28.414 DEBG ** process incoming 2.01 response:
Jul 19 16:59:58.415 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 1)
...
Jul 19 17:01:26.520 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 1)
Jul 19 17:01:27.522 DEBG sending CoAP request:
Jul 19 17:01:27.522 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : sent 17 bytes
v:1 t:CON c:PUT i:1419 {} [ Uri-Path:example_data ]
Jul 19 17:01:27.522 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1419: added to retransmit queue (2156ms)
Jul 19 17:01:27.522 DEBG timeout is set to 90 seconds
Jul 19 17:01:27.522 DEBG * 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : received 4 bytes
v:1 t:ACK c:2.04 i:1419 {} [ ]
Jul 19 17:01:27.522 DEBG ** 127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : mid=0x1419: removed
Jul 19 17:01:27.522 DEBG ** process incoming 2.04 response:
Jul 19 17:01:57.523 DEBG ***127.0.0.1:58904 <-> 127.0.0.1:5683 UDP : session disconnected (reason 1)
...

No more heartbeats

@mrdeep1
Copy link
Collaborator

mrdeep1 commented Jul 21, 2021

Your coap-client is non-standard - it repeats the PUT request somehow after the CoAP ping failure. It would be good to know what your actual changes are to coap-client to demonstrate the issue as well as separately update this PR with your corrected code changes (squashing everything into a single commit) and english description.

@obgm
Copy link
Owner

obgm commented Sep 28, 2021

@wfeii1980 I wonder if this PR is still pursued. As the PR has not been updated according to the feedback provided, I am inclined to close it and encourage you to open a new PR once it is clear how to resolve the issue you have encountered. Would this work for you?

@obgm
Copy link
Owner

obgm commented Jun 17, 2022

Closing as this PR seems to be obsolete. Please feel free to open a new PR at any time if you feel that your issue has not been addressed.

@obgm obgm closed this Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants