handle_ping.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #endif
  21. #include "mosquitto.h"
  22. #include "logging_mosq.h"
  23. #include "memory_mosq.h"
  24. #include "messages_mosq.h"
  25. #include "mqtt_protocol.h"
  26. #include "net_mosq.h"
  27. #include "packet_mosq.h"
  28. #include "read_handle.h"
  29. #include "send_mosq.h"
  30. #include "util_mosq.h"
  31. int handle__pingreq(struct mosquitto *mosq)
  32. {
  33. assert(mosq);
  34. if(mosquitto__get_state(mosq) != mosq_cs_active){
  35. return MOSQ_ERR_PROTOCOL;
  36. }
  37. if(mosq->in_packet.command != CMD_PINGREQ){
  38. return MOSQ_ERR_MALFORMED_PACKET;
  39. }
  40. #ifdef WITH_BROKER
  41. log__printf(NULL, MOSQ_LOG_DEBUG, "Received PINGREQ from %s", mosq->id);
  42. #else
  43. return MOSQ_ERR_PROTOCOL;
  44. #endif
  45. return send__pingresp(mosq);
  46. }
  47. int handle__pingresp(struct mosquitto *mosq)
  48. {
  49. assert(mosq);
  50. if(mosquitto__get_state(mosq) != mosq_cs_active){
  51. return MOSQ_ERR_PROTOCOL;
  52. }
  53. mosq->ping_t = 0; /* No longer waiting for a PINGRESP. */
  54. #ifdef WITH_BROKER
  55. if(mosq->bridge == NULL){
  56. return MOSQ_ERR_PROTOCOL;
  57. }
  58. log__printf(NULL, MOSQ_LOG_DEBUG, "Received PINGRESP from %s", mosq->id);
  59. #else
  60. log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received PINGRESP", mosq->id);
  61. #endif
  62. return MOSQ_ERR_SUCCESS;
  63. }