send_publish.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. # include "sys_tree.h"
  20. #else
  21. # define G_PUB_BYTES_SENT_INC(A)
  22. #endif
  23. #include "mosquitto.h"
  24. #include "mosquitto_internal.h"
  25. #include "logging_mosq.h"
  26. #include "mqtt_protocol.h"
  27. #include "memory_mosq.h"
  28. #include "net_mosq.h"
  29. #include "packet_mosq.h"
  30. #include "property_mosq.h"
  31. #include "send_mosq.h"
  32. int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval)
  33. {
  34. #ifdef WITH_BROKER
  35. size_t len;
  36. #ifdef WITH_BRIDGE
  37. int i;
  38. struct mosquitto__bridge_topic *cur_topic;
  39. bool match;
  40. int rc;
  41. char *mapped_topic = NULL;
  42. char *topic_temp = NULL;
  43. #endif
  44. #endif
  45. assert(mosq);
  46. #if defined(WITH_BROKER) && defined(WITH_WEBSOCKETS)
  47. if(mosq->sock == INVALID_SOCKET && !mosq->wsi) return MOSQ_ERR_NO_CONN;
  48. #else
  49. if(mosq->sock == INVALID_SOCKET) return MOSQ_ERR_NO_CONN;
  50. #endif
  51. if(!mosq->retain_available){
  52. retain = false;
  53. }
  54. #ifdef WITH_BROKER
  55. if(mosq->listener && mosq->listener->mount_point){
  56. len = strlen(mosq->listener->mount_point);
  57. if(len < strlen(topic)){
  58. topic += len;
  59. }else{
  60. /* Invalid topic string. Should never happen, but silently swallow the message anyway. */
  61. return MOSQ_ERR_SUCCESS;
  62. }
  63. }
  64. #ifdef WITH_BRIDGE
  65. if(mosq->bridge && mosq->bridge->topics && mosq->bridge->topic_remapping){
  66. for(i=0; i<mosq->bridge->topic_count; i++){
  67. cur_topic = &mosq->bridge->topics[i];
  68. if((cur_topic->direction == bd_both || cur_topic->direction == bd_out)
  69. && (cur_topic->remote_prefix || cur_topic->local_prefix)){
  70. /* Topic mapping required on this topic if the message matches */
  71. rc = mosquitto_topic_matches_sub(cur_topic->local_topic, topic, &match);
  72. if(rc){
  73. return rc;
  74. }
  75. if(match){
  76. mapped_topic = mosquitto__strdup(topic);
  77. if(!mapped_topic) return MOSQ_ERR_NOMEM;
  78. if(cur_topic->local_prefix){
  79. /* This prefix needs removing. */
  80. if(!strncmp(cur_topic->local_prefix, mapped_topic, strlen(cur_topic->local_prefix))){
  81. topic_temp = mosquitto__strdup(mapped_topic+strlen(cur_topic->local_prefix));
  82. mosquitto__free(mapped_topic);
  83. if(!topic_temp){
  84. return MOSQ_ERR_NOMEM;
  85. }
  86. mapped_topic = topic_temp;
  87. }
  88. }
  89. if(cur_topic->remote_prefix){
  90. /* This prefix needs adding. */
  91. len = strlen(mapped_topic) + strlen(cur_topic->remote_prefix)+1;
  92. topic_temp = mosquitto__malloc(len+1);
  93. if(!topic_temp){
  94. mosquitto__free(mapped_topic);
  95. return MOSQ_ERR_NOMEM;
  96. }
  97. snprintf(topic_temp, len, "%s%s", cur_topic->remote_prefix, mapped_topic);
  98. topic_temp[len] = '\0';
  99. mosquitto__free(mapped_topic);
  100. mapped_topic = topic_temp;
  101. }
  102. log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, mapped_topic, (long)payloadlen);
  103. G_PUB_BYTES_SENT_INC(payloadlen);
  104. rc = send__real_publish(mosq, mid, mapped_topic, payloadlen, payload, qos, retain, dup, cmsg_props, store_props, expiry_interval);
  105. mosquitto__free(mapped_topic);
  106. return rc;
  107. }
  108. }
  109. }
  110. }
  111. #endif
  112. log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, topic, (long)payloadlen);
  113. G_PUB_BYTES_SENT_INC(payloadlen);
  114. #else
  115. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBLISH (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, topic, (long)payloadlen);
  116. #endif
  117. return send__real_publish(mosq, mid, topic, payloadlen, payload, qos, retain, dup, cmsg_props, store_props, expiry_interval);
  118. }
  119. int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval)
  120. {
  121. struct mosquitto__packet *packet = NULL;
  122. unsigned int packetlen;
  123. unsigned int proplen = 0, varbytes;
  124. int rc;
  125. mosquitto_property expiry_prop;
  126. assert(mosq);
  127. if(topic){
  128. packetlen = 2+(unsigned int)strlen(topic) + payloadlen;
  129. }else{
  130. packetlen = 2 + payloadlen;
  131. }
  132. if(qos > 0) packetlen += 2; /* For message id */
  133. if(mosq->protocol == mosq_p_mqtt5){
  134. proplen = 0;
  135. proplen += property__get_length_all(cmsg_props);
  136. proplen += property__get_length_all(store_props);
  137. if(expiry_interval > 0){
  138. expiry_prop.next = NULL;
  139. expiry_prop.value.i32 = expiry_interval;
  140. expiry_prop.identifier = MQTT_PROP_MESSAGE_EXPIRY_INTERVAL;
  141. expiry_prop.client_generated = false;
  142. proplen += property__get_length_all(&expiry_prop);
  143. }
  144. varbytes = packet__varint_bytes(proplen);
  145. if(varbytes > 4){
  146. /* FIXME - Properties too big, don't publish any - should remove some first really */
  147. cmsg_props = NULL;
  148. store_props = NULL;
  149. expiry_interval = 0;
  150. }else{
  151. packetlen += proplen + varbytes;
  152. }
  153. }
  154. if(packet__check_oversize(mosq, packetlen)){
  155. #ifdef WITH_BROKER
  156. log__printf(NULL, MOSQ_LOG_NOTICE, "Dropping too large outgoing PUBLISH for %s (%d bytes)", mosq->id, packetlen);
  157. #else
  158. log__printf(NULL, MOSQ_LOG_NOTICE, "Dropping too large outgoing PUBLISH (%d bytes)", packetlen);
  159. #endif
  160. return MOSQ_ERR_OVERSIZE_PACKET;
  161. }
  162. packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
  163. if(!packet) return MOSQ_ERR_NOMEM;
  164. packet->mid = mid;
  165. packet->command = (uint8_t)(CMD_PUBLISH | (uint8_t)((dup&0x1)<<3) | (uint8_t)(qos<<1) | retain);
  166. packet->remaining_length = packetlen;
  167. rc = packet__alloc(packet);
  168. if(rc){
  169. mosquitto__free(packet);
  170. return rc;
  171. }
  172. /* Variable header (topic string) */
  173. if(topic){
  174. packet__write_string(packet, topic, (uint16_t)strlen(topic));
  175. }else{
  176. packet__write_uint16(packet, 0);
  177. }
  178. if(qos > 0){
  179. packet__write_uint16(packet, mid);
  180. }
  181. if(mosq->protocol == mosq_p_mqtt5){
  182. packet__write_varint(packet, proplen);
  183. property__write_all(packet, cmsg_props, false);
  184. property__write_all(packet, store_props, false);
  185. if(expiry_interval > 0){
  186. property__write_all(packet, &expiry_prop, false);
  187. }
  188. }
  189. /* Payload */
  190. if(payloadlen){
  191. packet__write_bytes(packet, payload, payloadlen);
  192. }
  193. return packet__queue(mosq, packet);
  194. }