handle_pubrel.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #ifdef WITH_BROKER
  19. # include "mosquitto_broker_internal.h"
  20. #endif
  21. #include "mosquitto.h"
  22. #include "logging_mosq.h"
  23. #include "memory_mosq.h"
  24. #include "messages_mosq.h"
  25. #include "mqtt_protocol.h"
  26. #include "net_mosq.h"
  27. #include "packet_mosq.h"
  28. #include "read_handle.h"
  29. #include "send_mosq.h"
  30. #include "util_mosq.h"
  31. int handle__pubrel(struct mosquitto *mosq)
  32. {
  33. uint8_t reason_code;
  34. uint16_t mid;
  35. #ifndef WITH_BROKER
  36. struct mosquitto_message_all *message = NULL;
  37. #endif
  38. int rc;
  39. mosquitto_property *properties = NULL;
  40. assert(mosq);
  41. if(mosquitto__get_state(mosq) != mosq_cs_active){
  42. return MOSQ_ERR_PROTOCOL;
  43. }
  44. if(mosq->protocol != mosq_p_mqtt31 && mosq->in_packet.command != (CMD_PUBREL|2)){
  45. return MOSQ_ERR_MALFORMED_PACKET;
  46. }
  47. if(mosq->protocol != mosq_p_mqtt31){
  48. if((mosq->in_packet.command&0x0F) != 0x02){
  49. return MOSQ_ERR_PROTOCOL;
  50. }
  51. }
  52. rc = packet__read_uint16(&mosq->in_packet, &mid);
  53. if(rc) return rc;
  54. if(mid == 0) return MOSQ_ERR_PROTOCOL;
  55. if(mosq->protocol == mosq_p_mqtt5 && mosq->in_packet.remaining_length > 2){
  56. rc = packet__read_byte(&mosq->in_packet, &reason_code);
  57. if(rc) return rc;
  58. if(reason_code != MQTT_RC_SUCCESS && reason_code != MQTT_RC_PACKET_ID_NOT_FOUND){
  59. return MOSQ_ERR_PROTOCOL;
  60. }
  61. if(mosq->in_packet.remaining_length > 3){
  62. rc = property__read_all(CMD_PUBREL, &mosq->in_packet, &properties);
  63. if(rc) return rc;
  64. }
  65. }
  66. if(mosq->in_packet.pos < mosq->in_packet.remaining_length){
  67. #ifdef WITH_BROKER
  68. mosquitto_property_free_all(&properties);
  69. #endif
  70. return MOSQ_ERR_MALFORMED_PACKET;
  71. }
  72. #ifdef WITH_BROKER
  73. log__printf(NULL, MOSQ_LOG_DEBUG, "Received PUBREL from %s (Mid: %d)", mosq->id, mid);
  74. /* Immediately free, we don't do anything with Reason String or User Property at the moment */
  75. mosquitto_property_free_all(&properties);
  76. rc = db__message_release_incoming(mosq, mid);
  77. if(rc == MOSQ_ERR_NOT_FOUND){
  78. /* Message not found. Still send a PUBCOMP anyway because this could be
  79. * due to a repeated PUBREL after a client has reconnected. */
  80. }else if(rc != MOSQ_ERR_SUCCESS){
  81. return rc;
  82. }
  83. rc = send__pubcomp(mosq, mid, NULL);
  84. if(rc) return rc;
  85. #else
  86. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received PUBREL (Mid: %d)", mosq->id, mid);
  87. rc = send__pubcomp(mosq, mid, NULL);
  88. if(rc){
  89. message__remove(mosq, mid, mosq_md_in, &message, 2);
  90. return rc;
  91. }
  92. rc = message__remove(mosq, mid, mosq_md_in, &message, 2);
  93. if(rc == MOSQ_ERR_SUCCESS){
  94. /* Only pass the message on if we have removed it from the queue - this
  95. * prevents multiple callbacks for the same message. */
  96. pthread_mutex_lock(&mosq->callback_mutex);
  97. if(mosq->on_message){
  98. mosq->in_callback = true;
  99. mosq->on_message(mosq, mosq->userdata, &message->msg);
  100. mosq->in_callback = false;
  101. }
  102. if(mosq->on_message_v5){
  103. mosq->in_callback = true;
  104. mosq->on_message_v5(mosq, mosq->userdata, &message->msg, message->properties);
  105. mosq->in_callback = false;
  106. }
  107. pthread_mutex_unlock(&mosq->callback_mutex);
  108. mosquitto_property_free_all(&properties);
  109. message__cleanup(&message);
  110. }else if(rc == MOSQ_ERR_NOT_FOUND){
  111. return MOSQ_ERR_SUCCESS;
  112. }else{
  113. return rc;
  114. }
  115. #endif
  116. return MOSQ_ERR_SUCCESS;
  117. }