send_mosq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. # include "sys_tree.h"
  21. #else
  22. # define G_PUB_BYTES_SENT_INC(A)
  23. #endif
  24. #include "mosquitto.h"
  25. #include "mosquitto_internal.h"
  26. #include "logging_mosq.h"
  27. #include "mqtt_protocol.h"
  28. #include "memory_mosq.h"
  29. #include "net_mosq.h"
  30. #include "packet_mosq.h"
  31. #include "property_mosq.h"
  32. #include "send_mosq.h"
  33. #include "time_mosq.h"
  34. #include "util_mosq.h"
  35. int send__pingreq(struct mosquitto *mosq)
  36. {
  37. int rc;
  38. assert(mosq);
  39. #ifdef WITH_BROKER
  40. log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGREQ to %s", mosq->id);
  41. #else
  42. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGREQ", mosq->id);
  43. #endif
  44. rc = send__simple_command(mosq, CMD_PINGREQ);
  45. if(rc == MOSQ_ERR_SUCCESS){
  46. mosq->ping_t = mosquitto_time();
  47. }
  48. return rc;
  49. }
  50. int send__pingresp(struct mosquitto *mosq)
  51. {
  52. #ifdef WITH_BROKER
  53. log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGRESP to %s", mosq->id);
  54. #else
  55. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGRESP", mosq->id);
  56. #endif
  57. return send__simple_command(mosq, CMD_PINGRESP);
  58. }
  59. int send__puback(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties)
  60. {
  61. #ifdef WITH_BROKER
  62. log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBACK to %s (m%d, rc%d)", mosq->id, mid, reason_code);
  63. #else
  64. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBACK (m%d, rc%d)", mosq->id, mid, reason_code);
  65. #endif
  66. util__increment_receive_quota(mosq);
  67. /* We don't use Reason String or User Property yet. */
  68. return send__command_with_mid(mosq, CMD_PUBACK, mid, false, reason_code, properties);
  69. }
  70. int send__pubcomp(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties)
  71. {
  72. #ifdef WITH_BROKER
  73. log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBCOMP to %s (m%d)", mosq->id, mid);
  74. #else
  75. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBCOMP (m%d)", mosq->id, mid);
  76. #endif
  77. util__increment_receive_quota(mosq);
  78. /* We don't use Reason String or User Property yet. */
  79. return send__command_with_mid(mosq, CMD_PUBCOMP, mid, false, 0, properties);
  80. }
  81. int send__pubrec(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties)
  82. {
  83. #ifdef WITH_BROKER
  84. log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREC to %s (m%d, rc%d)", mosq->id, mid, reason_code);
  85. #else
  86. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREC (m%d, rc%d)", mosq->id, mid, reason_code);
  87. #endif
  88. if(reason_code >= 0x80 && mosq->protocol == mosq_p_mqtt5){
  89. util__increment_receive_quota(mosq);
  90. }
  91. /* We don't use Reason String or User Property yet. */
  92. return send__command_with_mid(mosq, CMD_PUBREC, mid, false, reason_code, properties);
  93. }
  94. int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties)
  95. {
  96. #ifdef WITH_BROKER
  97. log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREL to %s (m%d)", mosq->id, mid);
  98. #else
  99. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREL (m%d)", mosq->id, mid);
  100. #endif
  101. /* We don't use Reason String or User Property yet. */
  102. return send__command_with_mid(mosq, CMD_PUBREL|2, mid, false, 0, properties);
  103. }
  104. /* For PUBACK, PUBCOMP, PUBREC, and PUBREL */
  105. int send__command_with_mid(struct mosquitto *mosq, uint8_t command, uint16_t mid, bool dup, uint8_t reason_code, const mosquitto_property *properties)
  106. {
  107. struct mosquitto__packet *packet = NULL;
  108. int rc;
  109. assert(mosq);
  110. packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
  111. if(!packet) return MOSQ_ERR_NOMEM;
  112. packet->command = command;
  113. if(dup){
  114. packet->command |= 8;
  115. }
  116. packet->remaining_length = 2;
  117. if(mosq->protocol == mosq_p_mqtt5){
  118. if(reason_code != 0 || properties){
  119. packet->remaining_length += 1;
  120. }
  121. if(properties){
  122. packet->remaining_length += property__get_remaining_length(properties);
  123. }
  124. }
  125. rc = packet__alloc(packet);
  126. if(rc){
  127. mosquitto__free(packet);
  128. return rc;
  129. }
  130. packet__write_uint16(packet, mid);
  131. if(mosq->protocol == mosq_p_mqtt5){
  132. if(reason_code != 0 || properties){
  133. packet__write_byte(packet, reason_code);
  134. }
  135. if(properties){
  136. property__write_all(packet, properties, true);
  137. }
  138. }
  139. return packet__queue(mosq, packet);
  140. }
  141. /* For DISCONNECT, PINGREQ and PINGRESP */
  142. int send__simple_command(struct mosquitto *mosq, uint8_t command)
  143. {
  144. struct mosquitto__packet *packet = NULL;
  145. int rc;
  146. assert(mosq);
  147. packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
  148. if(!packet) return MOSQ_ERR_NOMEM;
  149. packet->command = command;
  150. packet->remaining_length = 0;
  151. rc = packet__alloc(packet);
  152. if(rc){
  153. mosquitto__free(packet);
  154. return rc;
  155. }
  156. return packet__queue(mosq, packet);
  157. }