handle_suback.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifdef WITH_BROKER
  17. # include "mosquitto_broker_internal.h"
  18. #endif
  19. #include "mosquitto.h"
  20. #include "mosquitto_internal.h"
  21. #include "logging_mosq.h"
  22. #include "memory_mosq.h"
  23. #include "mqtt_protocol.h"
  24. #include "packet_mosq.h"
  25. #include "property_mosq.h"
  26. #include "read_handle.h"
  27. #include "util_mosq.h"
  28. int handle__suback(struct mosquitto *mosq)
  29. {
  30. uint16_t mid;
  31. uint8_t qos;
  32. int *granted_qos;
  33. int qos_count;
  34. int i = 0;
  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_SUBACK){
  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 SUBACK from %s", mosq->id);
  50. #else
  51. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received SUBACK", 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_SUBACK, &mosq->in_packet, &properties);
  58. if(rc) return rc;
  59. }
  60. qos_count = (int)(mosq->in_packet.remaining_length - mosq->in_packet.pos);
  61. granted_qos = mosquitto__malloc((size_t)qos_count*sizeof(int));
  62. if(!granted_qos){
  63. #ifdef WITH_BROKER
  64. mosquitto_property_free_all(&properties);
  65. #endif
  66. return MOSQ_ERR_NOMEM;
  67. }
  68. while(mosq->in_packet.pos < mosq->in_packet.remaining_length){
  69. rc = packet__read_byte(&mosq->in_packet, &qos);
  70. if(rc){
  71. mosquitto__free(granted_qos);
  72. #ifdef WITH_BROKER
  73. mosquitto_property_free_all(&properties);
  74. #endif
  75. return rc;
  76. }
  77. granted_qos[i] = (int)qos;
  78. i++;
  79. }
  80. #ifdef WITH_BROKER
  81. /* Immediately free, we don't do anything with Reason String or User Property at the moment */
  82. mosquitto_property_free_all(&properties);
  83. #else
  84. pthread_mutex_lock(&mosq->callback_mutex);
  85. if(mosq->on_subscribe){
  86. mosq->in_callback = true;
  87. mosq->on_subscribe(mosq, mosq->userdata, mid, qos_count, granted_qos);
  88. mosq->in_callback = false;
  89. }
  90. if(mosq->on_subscribe_v5){
  91. mosq->in_callback = true;
  92. mosq->on_subscribe_v5(mosq, mosq->userdata, mid, qos_count, granted_qos, properties);
  93. mosq->in_callback = false;
  94. }
  95. pthread_mutex_unlock(&mosq->callback_mutex);
  96. mosquitto_property_free_all(&properties);
  97. #endif
  98. mosquitto__free(granted_qos);
  99. return MOSQ_ERR_SUCCESS;
  100. }