send_subscribe.c 2.6 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 "mosquitto_internal.h"
  22. #include "logging_mosq.h"
  23. #include "memory_mosq.h"
  24. #include "mqtt_protocol.h"
  25. #include "packet_mosq.h"
  26. #include "property_mosq.h"
  27. #include "send_mosq.h"
  28. #include "util_mosq.h"
  29. int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, char *const *const topic, int topic_qos, const mosquitto_property *properties)
  30. {
  31. struct mosquitto__packet *packet = NULL;
  32. uint32_t packetlen;
  33. uint16_t local_mid;
  34. int rc;
  35. int i;
  36. size_t tlen;
  37. assert(mosq);
  38. assert(topic);
  39. packetlen = 2;
  40. if(mosq->protocol == mosq_p_mqtt5){
  41. packetlen += property__get_remaining_length(properties);
  42. }
  43. for(i=0; i<topic_count; i++){
  44. tlen = strlen(topic[i]);
  45. if(tlen > UINT16_MAX){
  46. return MOSQ_ERR_INVAL;
  47. }
  48. packetlen += 2U+(uint16_t)tlen + 1U;
  49. }
  50. packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
  51. if(!packet) return MOSQ_ERR_NOMEM;
  52. packet->command = CMD_SUBSCRIBE | (1<<1);
  53. packet->remaining_length = packetlen;
  54. rc = packet__alloc(packet);
  55. if(rc){
  56. mosquitto__free(packet);
  57. return rc;
  58. }
  59. /* Variable header */
  60. local_mid = mosquitto__mid_generate(mosq);
  61. if(mid) *mid = (int)local_mid;
  62. packet__write_uint16(packet, local_mid);
  63. if(mosq->protocol == mosq_p_mqtt5){
  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. packet__write_byte(packet, (uint8_t)topic_qos);
  70. }
  71. #ifdef WITH_BROKER
  72. # ifdef WITH_BRIDGE
  73. log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending SUBSCRIBE (Mid: %d, Topic: %s, QoS: %d, Options: 0x%02x)", mosq->id, local_mid, topic[0], topic_qos&0x03, topic_qos&0xFC);
  74. # endif
  75. #else
  76. for(i=0; i<topic_count; i++){
  77. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending SUBSCRIBE (Mid: %d, Topic: %s, QoS: %d, Options: 0x%02x)", mosq->id, local_mid, topic[i], topic_qos&0x03, topic_qos&0xFC);
  78. }
  79. #endif
  80. return packet__queue(mosq, packet);
  81. }