send_disconnect.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "send_mosq.h"
  27. int send__disconnect(struct mosquitto *mosq, uint8_t reason_code, const mosquitto_property *properties)
  28. {
  29. struct mosquitto__packet *packet = NULL;
  30. int rc;
  31. assert(mosq);
  32. #ifdef WITH_BROKER
  33. # ifdef WITH_BRIDGE
  34. if(mosq->bridge){
  35. log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending DISCONNECT", mosq->id);
  36. }else
  37. # else
  38. {
  39. log__printf(mosq, MOSQ_LOG_DEBUG, "Sending DISCONNECT to %s (rc%d)", mosq->id, reason_code);
  40. }
  41. # endif
  42. #else
  43. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending DISCONNECT", mosq->id);
  44. #endif
  45. assert(mosq);
  46. packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
  47. if(!packet) return MOSQ_ERR_NOMEM;
  48. packet->command = CMD_DISCONNECT;
  49. if(mosq->protocol == mosq_p_mqtt5 && (reason_code != 0 || properties)){
  50. packet->remaining_length = 1;
  51. if(properties){
  52. packet->remaining_length += property__get_remaining_length(properties);
  53. }
  54. }else{
  55. packet->remaining_length = 0;
  56. }
  57. rc = packet__alloc(packet);
  58. if(rc){
  59. mosquitto__free(packet);
  60. return rc;
  61. }
  62. if(mosq->protocol == mosq_p_mqtt5 && (reason_code != 0 || properties)){
  63. packet__write_byte(packet, reason_code);
  64. if(properties){
  65. property__write_all(packet, properties, true);
  66. }
  67. }
  68. return packet__queue(mosq, packet);
  69. }