2 Copyright (c) 2009-2013 Roger Light <roger@atchoo.org>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14 contributors may be used to endorse or promote products derived from
15 this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
34 #include "mosquitto.h"
35 #include "logging_mosq.h"
36 #include "memory_mosq.h"
37 #include "messages_mosq.h"
38 #include "mqtt3_protocol.h"
40 #include "read_handle.h"
41 #include "send_mosq.h"
42 #include "time_mosq.h"
43 #include "util_mosq.h"
45 int _mosquitto_packet_handle(struct mosquitto *mosq)
49 switch((mosq->in_packet.command)&0xF0){
51 return _mosquitto_handle_pingreq(mosq);
53 return _mosquitto_handle_pingresp(mosq);
55 return _mosquitto_handle_pubackcomp(mosq, "PUBACK");
57 return _mosquitto_handle_pubackcomp(mosq, "PUBCOMP");
59 return _mosquitto_handle_publish(mosq);
61 return _mosquitto_handle_pubrec(mosq);
63 return _mosquitto_handle_pubrel(NULL, mosq);
65 return _mosquitto_handle_connack(mosq);
67 return _mosquitto_handle_suback(mosq);
69 return _mosquitto_handle_unsuback(mosq);
71 /* If we don't recognise the command, return an error straight away. */
72 _mosquitto_log_printf(mosq, MOSQ_LOG_ERR, "Error: Unrecognised command %d\n", (mosq->in_packet.command)&0xF0);
73 return MOSQ_ERR_PROTOCOL;
77 int _mosquitto_handle_publish(struct mosquitto *mosq)
80 struct mosquitto_message_all *message;
86 message = _mosquitto_calloc(1, sizeof(struct mosquitto_message_all));
87 if(!message) return MOSQ_ERR_NOMEM;
89 header = mosq->in_packet.command;
91 message->dup = (header & 0x08)>>3;
92 message->msg.qos = (header & 0x06)>>1;
93 message->msg.retain = (header & 0x01);
95 rc = _mosquitto_read_string(&mosq->in_packet, &message->msg.topic);
97 _mosquitto_message_cleanup(&message);
100 if(!strlen(message->msg.topic)){
101 _mosquitto_message_cleanup(&message);
102 return MOSQ_ERR_PROTOCOL;
105 if(message->msg.qos > 0){
106 rc = _mosquitto_read_uint16(&mosq->in_packet, &mid);
108 _mosquitto_message_cleanup(&message);
111 message->msg.mid = (int)mid;
114 message->msg.payloadlen = mosq->in_packet.remaining_length - mosq->in_packet.pos;
115 if(message->msg.payloadlen){
116 message->msg.payload = _mosquitto_calloc(message->msg.payloadlen+1, sizeof(uint8_t));
117 if(!message->msg.payload){
118 _mosquitto_message_cleanup(&message);
119 return MOSQ_ERR_NOMEM;
121 rc = _mosquitto_read_bytes(&mosq->in_packet, message->msg.payload, message->msg.payloadlen);
123 _mosquitto_message_cleanup(&message);
127 _mosquitto_log_printf(mosq, MOSQ_LOG_DEBUG,
128 "Client %s received PUBLISH (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))",
129 mosq->id, message->dup, message->msg.qos, message->msg.retain,
130 message->msg.mid, message->msg.topic,
131 (long)message->msg.payloadlen);
133 message->timestamp = mosquitto_time();
134 switch(message->msg.qos){
136 pthread_mutex_lock(&mosq->callback_mutex);
137 if(mosq->on_message){
138 mosq->in_callback = true;
139 mosq->on_message(mosq, mosq->userdata, &message->msg);
140 mosq->in_callback = false;
142 pthread_mutex_unlock(&mosq->callback_mutex);
143 _mosquitto_message_cleanup(&message);
144 return MOSQ_ERR_SUCCESS;
146 rc = _mosquitto_send_puback(mosq, message->msg.mid);
147 pthread_mutex_lock(&mosq->callback_mutex);
148 if(mosq->on_message){
149 mosq->in_callback = true;
150 mosq->on_message(mosq, mosq->userdata, &message->msg);
151 mosq->in_callback = false;
153 pthread_mutex_unlock(&mosq->callback_mutex);
154 _mosquitto_message_cleanup(&message);
157 rc = _mosquitto_send_pubrec(mosq, message->msg.mid);
158 pthread_mutex_lock(&mosq->in_message_mutex);
159 message->state = mosq_ms_wait_for_pubrel;
160 _mosquitto_message_queue(mosq, message, mosq_md_in);
161 pthread_mutex_unlock(&mosq->in_message_mutex);
164 _mosquitto_message_cleanup(&message);
165 return MOSQ_ERR_PROTOCOL;