send_unsubscribe.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <string.h>
  17. #ifdef WITH_BROKER
  18. # include "mosquitto_broker_internal.h"
  19. #endif
  20. #include "mosquitto.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 "send_mosq.h"
  27. #include "util_mosq.h"
  28. int send__unsubscribe(struct mosquitto *mosq, int *mid, int topic_count, char *const *const topic, const mosquitto_property *properties)
  29. {
  30. struct mosquitto__packet *packet = NULL;
  31. uint32_t packetlen;
  32. uint16_t local_mid;
  33. int rc;
  34. int i;
  35. size_t tlen;
  36. assert(mosq);
  37. assert(topic);
  38. packetlen = 2;
  39. for(i=0; i<topic_count; i++){
  40. tlen = strlen(topic[i]);
  41. if(tlen > UINT16_MAX){
  42. return MOSQ_ERR_INVAL;
  43. }
  44. packetlen += 2U+(uint16_t)tlen;
  45. }
  46. packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
  47. if(!packet) return MOSQ_ERR_NOMEM;
  48. if(mosq->protocol == mosq_p_mqtt5){
  49. packetlen += property__get_remaining_length(properties);
  50. }
  51. packet->command = CMD_UNSUBSCRIBE | (1<<1);
  52. packet->remaining_length = packetlen;
  53. rc = packet__alloc(packet);
  54. if(rc){
  55. mosquitto__free(packet);
  56. return rc;
  57. }
  58. /* Variable header */
  59. local_mid = mosquitto__mid_generate(mosq);
  60. if(mid) *mid = (int)local_mid;
  61. packet__write_uint16(packet, local_mid);
  62. if(mosq->protocol == mosq_p_mqtt5){
  63. /* We don't use User Property yet. */
  64. property__write_all(packet, properties, true);
  65. }
  66. /* Payload */
  67. for(i=0; i<topic_count; i++){
  68. packet__write_string(packet, topic[i], (uint16_t)strlen(topic[i]));
  69. }
  70. #ifdef WITH_BROKER
  71. # ifdef WITH_BRIDGE
  72. for(i=0; i<topic_count; i++){
  73. log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending UNSUBSCRIBE (Mid: %d, Topic: %s)", mosq->id, local_mid, topic[i]);
  74. }
  75. # endif
  76. #else
  77. for(i=0; i<topic_count; i++){
  78. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending UNSUBSCRIBE (Mid: %d, Topic: %s)", mosq->id, local_mid, topic[i]);
  79. }
  80. #endif
  81. return packet__queue(mosq, packet);
  82. }