handle_pubrec.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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__pubrec(struct mosquitto *mosq)
  32. {
  33. uint8_t reason_code = 0;
  34. uint16_t mid;
  35. int rc;
  36. mosquitto_property *properties = NULL;
  37. assert(mosq);
  38. if(mosquitto__get_state(mosq) != mosq_cs_active){
  39. return MOSQ_ERR_PROTOCOL;
  40. }
  41. if(mosq->in_packet.command != CMD_PUBREC){
  42. return MOSQ_ERR_MALFORMED_PACKET;
  43. }
  44. rc = packet__read_uint16(&mosq->in_packet, &mid);
  45. if(rc) return rc;
  46. if(mid == 0) return MOSQ_ERR_PROTOCOL;
  47. if(mosq->protocol == mosq_p_mqtt5 && mosq->in_packet.remaining_length > 2){
  48. rc = packet__read_byte(&mosq->in_packet, &reason_code);
  49. if(rc) return rc;
  50. if(reason_code != MQTT_RC_SUCCESS
  51. && reason_code != MQTT_RC_NO_MATCHING_SUBSCRIBERS
  52. && reason_code != MQTT_RC_UNSPECIFIED
  53. && reason_code != MQTT_RC_IMPLEMENTATION_SPECIFIC
  54. && reason_code != MQTT_RC_NOT_AUTHORIZED
  55. && reason_code != MQTT_RC_TOPIC_NAME_INVALID
  56. && reason_code != MQTT_RC_PACKET_ID_IN_USE
  57. && reason_code != MQTT_RC_QUOTA_EXCEEDED){
  58. return MOSQ_ERR_PROTOCOL;
  59. }
  60. if(mosq->in_packet.remaining_length > 3){
  61. rc = property__read_all(CMD_PUBREC, &mosq->in_packet, &properties);
  62. if(rc) return rc;
  63. /* Immediately free, we don't do anything with Reason String or User Property at the moment */
  64. mosquitto_property_free_all(&properties);
  65. }
  66. }
  67. if(mosq->in_packet.pos < mosq->in_packet.remaining_length){
  68. #ifdef WITH_BROKER
  69. mosquitto_property_free_all(&properties);
  70. #endif
  71. return MOSQ_ERR_MALFORMED_PACKET;
  72. }
  73. #ifdef WITH_BROKER
  74. log__printf(NULL, MOSQ_LOG_DEBUG, "Received PUBREC from %s (Mid: %d)", mosq->id, mid);
  75. if(reason_code < 0x80){
  76. rc = db__message_update_outgoing(mosq, mid, mosq_ms_wait_for_pubcomp, 2);
  77. }else{
  78. return db__message_delete_outgoing(mosq, mid, mosq_ms_wait_for_pubrec, 2);
  79. }
  80. #else
  81. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received PUBREC (Mid: %d)", mosq->id, mid);
  82. if(reason_code < 0x80 || mosq->protocol != mosq_p_mqtt5){
  83. rc = message__out_update(mosq, mid, mosq_ms_wait_for_pubcomp, 2);
  84. }else{
  85. if(!message__delete(mosq, mid, mosq_md_out, 2)){
  86. /* Only inform the client the message has been sent once. */
  87. pthread_mutex_lock(&mosq->callback_mutex);
  88. if(mosq->on_publish_v5){
  89. mosq->in_callback = true;
  90. mosq->on_publish_v5(mosq, mosq->userdata, mid, reason_code, properties);
  91. mosq->in_callback = false;
  92. }
  93. pthread_mutex_unlock(&mosq->callback_mutex);
  94. }
  95. util__increment_send_quota(mosq);
  96. pthread_mutex_lock(&mosq->msgs_out.mutex);
  97. message__release_to_inflight(mosq, mosq_md_out);
  98. pthread_mutex_unlock(&mosq->msgs_out.mutex);
  99. return MOSQ_ERR_SUCCESS;
  100. }
  101. #endif
  102. if(rc == MOSQ_ERR_NOT_FOUND){
  103. log__printf(mosq, MOSQ_LOG_WARNING, "Warning: Received PUBREC from %s for an unknown packet identifier %d.", mosq->id, mid);
  104. }else if(rc != MOSQ_ERR_SUCCESS){
  105. return rc;
  106. }
  107. rc = send__pubrel(mosq, mid, NULL);
  108. if(rc) return rc;
  109. return MOSQ_ERR_SUCCESS;
  110. }