/src/mosquitto/lib/handle_ping.c
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2009-2021 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include <assert.h> |
22 | | #include <stdio.h> |
23 | | #include <string.h> |
24 | | |
25 | | #ifdef WITH_BROKER |
26 | | # include "mosquitto_broker_internal.h" |
27 | | #endif |
28 | | |
29 | | #include "mosquitto.h" |
30 | | #include "logging_mosq.h" |
31 | | #include "messages_mosq.h" |
32 | | #include "mosquitto/mqtt_protocol.h" |
33 | | #include "net_mosq.h" |
34 | | #include "packet_mosq.h" |
35 | | #include "read_handle.h" |
36 | | #include "send_mosq.h" |
37 | | #include "util_mosq.h" |
38 | | |
39 | | |
40 | | int handle__pingreq(struct mosquitto *mosq) |
41 | 33 | { |
42 | 33 | assert(mosq); |
43 | | |
44 | 33 | if(mosquitto__get_state(mosq) != mosq_cs_active){ |
45 | 8 | #ifdef WITH_BROKER |
46 | 8 | log__printf(NULL, MOSQ_LOG_INFO, "Protocol error from %s: PINGREQ before session is active.", mosq->id); |
47 | 8 | #endif |
48 | 8 | return MOSQ_ERR_PROTOCOL; |
49 | 8 | } |
50 | 25 | if(mosq->in_packet.command != CMD_PINGREQ || mosq->in_packet.remaining_length != 0){ |
51 | 24 | return MOSQ_ERR_MALFORMED_PACKET; |
52 | 24 | } |
53 | | |
54 | 1 | #ifdef WITH_BROKER |
55 | 1 | log__printf(NULL, MOSQ_LOG_DEBUG, "Received PINGREQ from %s", SAFE_PRINT(mosq->id)); |
56 | 1 | return send__pingresp(mosq); |
57 | | #else |
58 | | return MOSQ_ERR_PROTOCOL; |
59 | | #endif |
60 | 25 | } |
61 | | |
62 | | |
63 | | int handle__pingresp(struct mosquitto *mosq) |
64 | 32 | { |
65 | 32 | assert(mosq); |
66 | | |
67 | 32 | if(mosquitto__get_state(mosq) != mosq_cs_active){ |
68 | 9 | #ifdef WITH_BROKER |
69 | 9 | log__printf(NULL, MOSQ_LOG_INFO, "Protocol error from %s: PINGRESP before session is active.", mosq->id); |
70 | 9 | #endif |
71 | 9 | return MOSQ_ERR_PROTOCOL; |
72 | 9 | } |
73 | 23 | if(mosq->in_packet.command != CMD_PINGRESP || mosq->in_packet.remaining_length != 0){ |
74 | 21 | return MOSQ_ERR_MALFORMED_PACKET; |
75 | 21 | } |
76 | | |
77 | 2 | mosq->ping_t = 0; /* No longer waiting for a PINGRESP. */ |
78 | 2 | #ifdef WITH_BROKER |
79 | 2 | if(mosq->bridge == NULL){ |
80 | 0 | log__printf(NULL, MOSQ_LOG_INFO, "Protocol error from %s: PINGRESP when not a bridge.", mosq->id); |
81 | 0 | return MOSQ_ERR_PROTOCOL; |
82 | 0 | } |
83 | 2 | log__printf(NULL, MOSQ_LOG_DEBUG, "Received PINGRESP from %s", SAFE_PRINT(mosq->id)); |
84 | | #else |
85 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received PINGRESP", SAFE_PRINT(mosq->id)); |
86 | | #endif |
87 | 2 | return MOSQ_ERR_SUCCESS; |
88 | 2 | } |
89 | | |