handle_disconnect.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <stdio.h>
  16. #include <string.h>
  17. #include "logging_mosq.h"
  18. #include "mqtt_protocol.h"
  19. #include "memory_mosq.h"
  20. #include "net_mosq.h"
  21. #include "packet_mosq.h"
  22. #include "property_mosq.h"
  23. #include "read_handle.h"
  24. #include "send_mosq.h"
  25. #include "util_mosq.h"
  26. int handle__disconnect(struct mosquitto *mosq)
  27. {
  28. int rc;
  29. uint8_t reason_code;
  30. mosquitto_property *properties = NULL;
  31. if(!mosq){
  32. return MOSQ_ERR_INVAL;
  33. }
  34. if(mosq->protocol != mosq_p_mqtt5){
  35. return MOSQ_ERR_PROTOCOL;
  36. }
  37. if(mosq->in_packet.command != CMD_DISCONNECT){
  38. return MOSQ_ERR_MALFORMED_PACKET;
  39. }
  40. rc = packet__read_byte(&mosq->in_packet, &reason_code);
  41. if(rc) return rc;
  42. if(mosq->in_packet.remaining_length > 2){
  43. rc = property__read_all(CMD_DISCONNECT, &mosq->in_packet, &properties);
  44. if(rc) return rc;
  45. mosquitto_property_free_all(&properties);
  46. }
  47. log__printf(mosq, MOSQ_LOG_DEBUG, "Received DISCONNECT (%d)", reason_code);
  48. do_client_disconnect(mosq, reason_code, properties);
  49. mosquitto_property_free_all(&properties);
  50. return MOSQ_ERR_SUCCESS;
  51. }