-
Notifications
You must be signed in to change notification settings - Fork 3
/
read_handle.c
executable file
·314 lines (291 loc) · 9.86 KB
/
read_handle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright (c) 2009-2013 Roger Light <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of mosquitto nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <config.h>
#include <mosquitto_broker.h>
#include <mqtt3_protocol.h>
#include <memory_mosq.h>
#include <read_handle.h>
#include <send_mosq.h>
#include <util_mosq.h>
#ifdef WITH_SYS_TREE
extern uint64_t g_pub_bytes_received;
#endif
int mqtt3_packet_handle(struct mosquitto_db *db, struct mosquitto *context)
{
if(!context) return MOSQ_ERR_INVAL;
switch((context->in_packet.command)&0xF0){
case PINGREQ:
return _mosquitto_handle_pingreq(context);
case PINGRESP:
return _mosquitto_handle_pingresp(context);
case PUBACK:
return _mosquitto_handle_pubackcomp(context, "PUBACK");
case PUBCOMP:
return _mosquitto_handle_pubackcomp(context, "PUBCOMP");
case PUBLISH:
return mqtt3_handle_publish(db, context);
case PUBREC:
return _mosquitto_handle_pubrec(context);
case PUBREL:
return _mosquitto_handle_pubrel(db, context);
case CONNECT:
return mqtt3_handle_connect(db, context);
case DISCONNECT:
return mqtt3_handle_disconnect(db, context);
case SUBSCRIBE:
return mqtt3_handle_subscribe(db, context);
case UNSUBSCRIBE:
return mqtt3_handle_unsubscribe(db, context);
#ifdef WITH_BRIDGE
case CONNACK:
return mqtt3_handle_connack(db, context);
case SUBACK:
return _mosquitto_handle_suback(context);
case UNSUBACK:
return _mosquitto_handle_unsuback(context);
#endif
default:
/* If we don't recognise the command, return an error straight away. */
return MOSQ_ERR_PROTOCOL;
}
}
int mqtt3_handle_publish(struct mosquitto_db *db, struct mosquitto *context)
{
char *topic;
void *payload = NULL;
uint32_t payloadlen;
uint8_t dup, qos, retain;
uint16_t mid = 0;
int rc = 0;
uint8_t header = context->in_packet.command;
int res = 0;
struct mosquitto_msg_store *stored = NULL;
int len;
char *topic_mount;
bool BigFile = false;
#ifdef WITH_BRIDGE
char *topic_temp;
int i;
struct _mqtt3_bridge_topic *cur_topic;
bool match;
#endif
dup = (header & 0x08)>>3; //这里拆包、处理,可参考_mosquitto_send_real_publish函数(封包)
qos = (header & 0x06)>>1;
if(qos == 3){
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO,
"Invalid QoS in PUBLISH from %s, disconnecting.", context->id);
return 1;
}
retain = (header & 0x01);
if(_mosquitto_read_string(&context->in_packet, &topic)) return 1;
if(strlen(topic) == 0){
/* Invalid publish topic, disconnect client. */
_mosquitto_free(topic);
return 1;
}
if(!strlen(topic)){
_mosquitto_free(topic);
return 1;
}
printf("topic=%s\n", topic);
#ifdef WITH_BRIDGE
if(context->bridge && context->bridge->topics && context->bridge->topic_remapping){
for(i=0; i<context->bridge->topic_count; i++){
cur_topic = &context->bridge->topics[i];
if((cur_topic->direction == bd_both || cur_topic->direction == bd_in)
&& (cur_topic->remote_prefix || cur_topic->local_prefix)){
/* Topic mapping required on this topic if the message matches */
rc = mosquitto_topic_matches_sub(cur_topic->remote_topic, topic, &match);
if(rc){
_mosquitto_free(topic);
return rc;
}
if(match){
if(cur_topic->remote_prefix){
/* This prefix needs removing. */
if(!strncmp(cur_topic->remote_prefix, topic, strlen(cur_topic->remote_prefix))){
topic_temp = _mosquitto_strdup(topic+strlen(cur_topic->remote_prefix));
if(!topic_temp){
_mosquitto_free(topic);
return MOSQ_ERR_NOMEM;
}
_mosquitto_free(topic);
topic = topic_temp;
}
}
if(cur_topic->local_prefix){
/* This prefix needs adding. */
len = strlen(topic) + strlen(cur_topic->local_prefix)+1;
topic_temp = _mosquitto_calloc(len+1, sizeof(char));
if(!topic_temp){
_mosquitto_free(topic);
return MOSQ_ERR_NOMEM;
}
snprintf(topic_temp, len, "%s%s", cur_topic->local_prefix, topic);
_mosquitto_free(topic);
topic = topic_temp;
}
break;
}
}
}
}
#endif
if(_mosquitto_topic_wildcard_len_check(topic) != MOSQ_ERR_SUCCESS){
/* Invalid publish topic, just swallow it. */
_mosquitto_free(topic);
return 1;
}
if(qos > 0){
if(_mosquitto_read_uint16(&context->in_packet, &mid)){
_mosquitto_free(topic);
return 1;
}
}
payloadlen = context->in_packet.remaining_length - context->in_packet.pos;
#ifdef WITH_SYS_TREE
g_pub_bytes_received += payloadlen;
#endif
if(context->listener && context->listener->mount_point){
len = strlen(context->listener->mount_point) + strlen(topic) + 1;
topic_mount = _mosquitto_calloc(len, sizeof(char));
if(!topic_mount){
_mosquitto_free(topic);
return MOSQ_ERR_NOMEM;
}
snprintf(topic_mount, len, "%s%s", context->listener->mount_point, topic);
printf("topic_mount=%s,mount_point=%s\n", topic_mount, context->listener->mount_point);
_mosquitto_free(topic);
topic = topic_mount;
}
if(payloadlen){
if(db->config->message_size_limit && payloadlen > db->config->message_size_limit){
_mosquitto_log_printf(NULL, MOSQ_LOG_DEBUG, "Dropped too large PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->id, dup, qos, retain, mid, topic, (long)payloadlen);
goto process_bad_message;
}
if(db->config->large_file_size_limit && payloadlen > db->config->large_file_size_limit){
//存储到相应位置,做特殊处理
BigFile = true;
}
printf("mqtt_handle_publish recieved payload:%d\n", payloadlen);
payload = _mosquitto_calloc(payloadlen+1, sizeof(uint8_t));
if(!payload){
_mosquitto_free(topic);
return 1;
}
if(_mosquitto_read_bytes(&context->in_packet, payload, payloadlen)){
_mosquitto_free(topic);
_mosquitto_free(payload);
return 1;
}
}
/* Check for topic access */
rc = mosquitto_acl_check(db, context, topic, MOSQ_ACL_WRITE);
if(rc == MOSQ_ERR_ACL_DENIED){
_mosquitto_log_printf(NULL, MOSQ_LOG_DEBUG, "Denied PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->id, dup, qos, retain, mid, topic, (long)payloadlen);
goto process_bad_message;
}else if(rc != MOSQ_ERR_SUCCESS){
_mosquitto_free(topic);
if(payload) _mosquitto_free(payload);
return rc;
}
_mosquitto_log_printf(NULL, MOSQ_LOG_DEBUG, "Received PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->id, dup, qos, retain, mid, topic, (long)payloadlen);
if(qos > 0){
mqtt3_db_message_store_find(context, mid, &stored);
}
if(!stored){
dup = 0;
if(mqtt3_db_message_store(db, context->id, mid, topic, qos, payloadlen, payload, retain, &stored, 0)){
_mosquitto_free(topic);
if(payload) _mosquitto_free(payload);
return 1;
}
}else{
dup = 1;
}
switch(qos){
case 0:
if(BigFile){//先用普通服务试一下
retain = false; //Big File不允许被retain标记
if(mqtt3_db_messages_subhier(db, context->id, topic, qos, retain, stored))
rc = 1;
}else{
if(mqtt3_db_messages_queue(db, context->id, topic, qos, retain, stored))
rc = 1;
}
break;
case 1:
if(mqtt3_db_messages_queue(db, context->id, topic, qos, retain, stored))
rc = 1;
if(_mosquitto_send_puback(context, mid))
rc = 1;
break;
case 2:
if(!dup){
res = mqtt3_db_message_insert(db, context, mid, mosq_md_in, qos, retain, stored);
}else{
res = 0;
}
/* mqtt3_db_message_insert() returns 2 to indicate dropped message
* due to queue. This isn't an error so don't disconnect them. */
if(!res){
if(_mosquitto_send_pubrec(context, mid)) rc = 1;
}else if(res == 1){
rc = 1;
}
break;
}
_mosquitto_free(topic);
if(payload) _mosquitto_free(payload);
return rc;
process_bad_message:
_mosquitto_free(topic);
if(payload) _mosquitto_free(payload);
switch(qos){
case 0:
return MOSQ_ERR_SUCCESS;
case 1:
return _mosquitto_send_puback(context, mid);
case 2:
mqtt3_db_message_store_find(context, mid, &stored);
if(!stored){
if(mqtt3_db_message_store(db, context->id, mid, NULL, qos, 0, NULL, false, &stored, 0)){
return 1;
}
res = mqtt3_db_message_insert(db, context, mid, mosq_md_in, qos, false, stored);
}else{
res = 0;
}
if(!res){
res = _mosquitto_send_pubrec(context, mid);
}
return res;
}
return 1;
}