/src/mosquitto/lib/send_mosq.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 | | # include "sys_tree.h" |
28 | | #endif |
29 | | |
30 | | #include "mosquitto.h" |
31 | | #include "mosquitto_internal.h" |
32 | | #include "logging_mosq.h" |
33 | | #include "mosquitto/mqtt_protocol.h" |
34 | | #include "net_mosq.h" |
35 | | #include "packet_mosq.h" |
36 | | #include "property_mosq.h" |
37 | | #include "send_mosq.h" |
38 | | #include "util_mosq.h" |
39 | | |
40 | | |
41 | | int send__pingreq(struct mosquitto *mosq) |
42 | 0 | { |
43 | 0 | int rc; |
44 | 0 | assert(mosq); |
45 | 0 | #ifdef WITH_BROKER |
46 | 0 | log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGREQ to %s", SAFE_PRINT(mosq->id)); |
47 | | #else |
48 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGREQ", SAFE_PRINT(mosq->id)); |
49 | | #endif |
50 | 0 | rc = send__simple_command(mosq, CMD_PINGREQ); |
51 | 0 | if(rc == MOSQ_ERR_SUCCESS){ |
52 | 0 | mosq->ping_t = mosquitto_time(); |
53 | 0 | #ifdef WITH_BROKER |
54 | 0 | metrics__int_inc(mosq_counter_mqtt_pingreq_sent, 1); |
55 | 0 | #endif |
56 | 0 | } |
57 | 0 | return rc; |
58 | 0 | } |
59 | | |
60 | | |
61 | | int send__pingresp(struct mosquitto *mosq) |
62 | 0 | { |
63 | 0 | #ifdef WITH_BROKER |
64 | 0 | log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGRESP to %s", SAFE_PRINT(mosq->id)); |
65 | 0 | metrics__int_inc(mosq_counter_mqtt_pingresp_sent, 1); |
66 | | #else |
67 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGRESP", SAFE_PRINT(mosq->id)); |
68 | | #endif |
69 | 0 | return send__simple_command(mosq, CMD_PINGRESP); |
70 | 0 | } |
71 | | |
72 | | |
73 | | int send__puback(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties) |
74 | 15 | { |
75 | 15 | #ifdef WITH_BROKER |
76 | 15 | log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBACK to %s (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code); |
77 | 15 | metrics__int_inc(mosq_counter_mqtt_puback_sent, 1); |
78 | | #else |
79 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBACK (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code); |
80 | | #endif |
81 | 15 | util__increment_receive_quota(mosq); |
82 | | /* We don't use Reason String or User Property yet. */ |
83 | 15 | return send__command_with_mid(mosq, CMD_PUBACK, mid, false, reason_code, properties); |
84 | 15 | } |
85 | | |
86 | | |
87 | | int send__pubcomp(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties) |
88 | 1 | { |
89 | 1 | #ifdef WITH_BROKER |
90 | 1 | log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBCOMP to %s (m%d)", SAFE_PRINT(mosq->id), mid); |
91 | 1 | metrics__int_inc(mosq_counter_mqtt_pubcomp_sent, 1); |
92 | | #else |
93 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBCOMP (m%d)", SAFE_PRINT(mosq->id), mid); |
94 | | #endif |
95 | 1 | util__increment_receive_quota(mosq); |
96 | | /* We don't use Reason String or User Property yet. */ |
97 | 1 | return send__command_with_mid(mosq, CMD_PUBCOMP, mid, false, 0, properties); |
98 | 1 | } |
99 | | |
100 | | |
101 | | int send__pubrec(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties) |
102 | 18 | { |
103 | 18 | #ifdef WITH_BROKER |
104 | 18 | log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREC to %s (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code); |
105 | 18 | metrics__int_inc(mosq_counter_mqtt_pubrec_sent, 1); |
106 | | #else |
107 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREC (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code); |
108 | | #endif |
109 | 18 | if(reason_code >= 0x80 && mosq->protocol == mosq_p_mqtt5){ |
110 | 2 | util__increment_receive_quota(mosq); |
111 | 2 | } |
112 | | /* We don't use Reason String or User Property yet. */ |
113 | 18 | return send__command_with_mid(mosq, CMD_PUBREC, mid, false, reason_code, properties); |
114 | 18 | } |
115 | | |
116 | | |
117 | | int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties) |
118 | 0 | { |
119 | 0 | #ifdef WITH_BROKER |
120 | 0 | log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREL to %s (m%d)", SAFE_PRINT(mosq->id), mid); |
121 | 0 | metrics__int_inc(mosq_counter_mqtt_pubrel_sent, 1); |
122 | | #else |
123 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREL (m%d)", SAFE_PRINT(mosq->id), mid); |
124 | | #endif |
125 | | /* We don't use Reason String or User Property yet. */ |
126 | 0 | return send__command_with_mid(mosq, CMD_PUBREL|2, mid, false, 0, properties); |
127 | 0 | } |
128 | | |
129 | | |
130 | | /* For PUBACK, PUBCOMP, PUBREC, and PUBREL */ |
131 | | int send__command_with_mid(struct mosquitto *mosq, uint8_t command, uint16_t mid, bool dup, uint8_t reason_code, const mosquitto_property *properties) |
132 | 34 | { |
133 | 34 | struct mosquitto__packet *packet = NULL; |
134 | 34 | int rc; |
135 | 34 | uint32_t remaining_length; |
136 | | |
137 | 34 | assert(mosq); |
138 | | |
139 | 34 | if(dup){ |
140 | 0 | command |= 8; |
141 | 0 | } |
142 | 34 | remaining_length = 2; |
143 | | |
144 | 34 | if(mosq->protocol == mosq_p_mqtt5){ |
145 | 4 | if(reason_code != 0 || properties){ |
146 | 3 | remaining_length += 1; |
147 | 3 | } |
148 | | |
149 | 4 | if(properties){ |
150 | 0 | remaining_length += mosquitto_property_get_remaining_length(properties); |
151 | 0 | } |
152 | 4 | } |
153 | | |
154 | 34 | rc = packet__alloc(&packet, command, remaining_length); |
155 | 34 | if(rc){ |
156 | 0 | return rc; |
157 | 0 | } |
158 | | |
159 | 34 | packet__write_uint16(packet, mid); |
160 | | |
161 | 34 | if(mosq->protocol == mosq_p_mqtt5){ |
162 | 4 | if(reason_code != 0 || properties){ |
163 | 3 | packet__write_byte(packet, reason_code); |
164 | 3 | } |
165 | 4 | if(properties){ |
166 | 0 | property__write_all(packet, properties, true); |
167 | 0 | } |
168 | 4 | } |
169 | | |
170 | 34 | return packet__queue(mosq, packet); |
171 | 34 | } |
172 | | |
173 | | |
174 | | /* For DISCONNECT, PINGREQ and PINGRESP */ |
175 | | int send__simple_command(struct mosquitto *mosq, uint8_t command) |
176 | 0 | { |
177 | 0 | struct mosquitto__packet *packet = NULL; |
178 | 0 | int rc; |
179 | |
|
180 | 0 | assert(mosq); |
181 | |
|
182 | 0 | rc = packet__alloc(&packet, command, 0); |
183 | 0 | if(rc){ |
184 | 0 | return rc; |
185 | 0 | } |
186 | | |
187 | 0 | return packet__queue(mosq, packet); |
188 | 0 | } |