handle_unsuback.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "property_mosq.h"
  29. #include "read_handle.h"
  30. #include "send_mosq.h"
  31. #include "util_mosq.h"
  32. int handle__unsuback(struct mosquitto *mosq)
  33. {
  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_UNSUBACK){
  42. return MOSQ_ERR_MALFORMED_PACKET;
  43. }
  44. #ifdef WITH_BROKER
  45. if(mosq->bridge == NULL){
  46. /* Client is not a bridge, so shouldn't be sending SUBACK */
  47. return MOSQ_ERR_PROTOCOL;
  48. }
  49. log__printf(NULL, MOSQ_LOG_DEBUG, "Received UNSUBACK from %s", mosq->id);
  50. #else
  51. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received UNSUBACK", mosq->id);
  52. #endif
  53. rc = packet__read_uint16(&mosq->in_packet, &mid);
  54. if(rc) return rc;
  55. if(mid == 0) return MOSQ_ERR_PROTOCOL;
  56. if(mosq->protocol == mosq_p_mqtt5){
  57. rc = property__read_all(CMD_UNSUBACK, &mosq->in_packet, &properties);
  58. if(rc) return rc;
  59. }
  60. #ifdef WITH_BROKER
  61. /* Immediately free, we don't do anything with Reason String or User Property at the moment */
  62. mosquitto_property_free_all(&properties);
  63. #else
  64. pthread_mutex_lock(&mosq->callback_mutex);
  65. if(mosq->on_unsubscribe){
  66. mosq->in_callback = true;
  67. mosq->on_unsubscribe(mosq, mosq->userdata, mid);
  68. mosq->in_callback = false;
  69. }
  70. if(mosq->on_unsubscribe_v5){
  71. mosq->in_callback = true;
  72. mosq->on_unsubscribe_v5(mosq, mosq->userdata, mid, properties);
  73. mosq->in_callback = false;
  74. }
  75. pthread_mutex_unlock(&mosq->callback_mutex);
  76. mosquitto_property_free_all(&properties);
  77. #endif
  78. return MOSQ_ERR_SUCCESS;
  79. }