mosquitto/lib/read_handle.c

157 lines
4.2 KiB
C
Raw Normal View History

2014-05-07 22:27:00 +00:00
/*
Copyright (c) 2009-2015 Roger Light <roger@atchoo.org>
2014-05-07 22:27:00 +00:00
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
2015-04-29 20:37:47 +00:00
#include "mosquitto.h"
#include "logging_mosq.h"
#include "memory_mosq.h"
#include "messages_mosq.h"
#include "mqtt3_protocol.h"
#include "net_mosq.h"
#include "packet_mosq.h"
#include "read_handle.h"
#include "send_mosq.h"
#include "time_mosq.h"
#include "util_mosq.h"
2014-05-07 22:27:00 +00:00
int mosquitto__packet_handle(struct mosquitto *mosq)
2014-05-07 22:27:00 +00:00
{
assert(mosq);
switch((mosq->in_packet.command)&0xF0){
case PINGREQ:
2015-05-16 17:43:06 +00:00
return handle__pingreq(mosq);
2014-05-07 22:27:00 +00:00
case PINGRESP:
2015-05-16 17:43:06 +00:00
return handle__pingresp(mosq);
2014-05-07 22:27:00 +00:00
case PUBACK:
2015-05-16 17:43:06 +00:00
return handle__pubackcomp(mosq, "PUBACK");
2014-05-07 22:27:00 +00:00
case PUBCOMP:
2015-05-16 17:43:06 +00:00
return handle__pubackcomp(mosq, "PUBCOMP");
2014-05-07 22:27:00 +00:00
case PUBLISH:
2015-05-16 17:43:06 +00:00
return handle__publish(mosq);
2014-05-07 22:27:00 +00:00
case PUBREC:
2015-05-16 17:43:06 +00:00
return handle__pubrec(mosq);
2014-05-07 22:27:00 +00:00
case PUBREL:
2015-05-16 17:43:06 +00:00
return handle__pubrel(NULL, mosq);
2014-05-07 22:27:00 +00:00
case CONNACK:
2015-05-16 17:43:06 +00:00
return handle__connack(mosq);
2014-05-07 22:27:00 +00:00
case SUBACK:
2015-05-16 17:43:06 +00:00
return handle__suback(mosq);
2014-05-07 22:27:00 +00:00
case UNSUBACK:
2015-05-16 17:43:06 +00:00
return handle__unsuback(mosq);
2014-05-07 22:27:00 +00:00
default:
/* If we don't recognise the command, return an error straight away. */
2015-05-18 07:53:21 +00:00
log__printf(mosq, MOSQ_LOG_ERR, "Error: Unrecognised command %d\n", (mosq->in_packet.command)&0xF0);
2014-05-07 22:27:00 +00:00
return MOSQ_ERR_PROTOCOL;
}
}
2015-05-16 17:43:06 +00:00
int handle__publish(struct mosquitto *mosq)
2014-05-07 22:27:00 +00:00
{
uint8_t header;
struct mosquitto_message_all *message;
int rc = 0;
uint16_t mid;
assert(mosq);
message = mosquitto__calloc(1, sizeof(struct mosquitto_message_all));
2014-05-07 22:27:00 +00:00
if(!message) return MOSQ_ERR_NOMEM;
header = mosq->in_packet.command;
message->dup = (header & 0x08)>>3;
message->msg.qos = (header & 0x06)>>1;
message->msg.retain = (header & 0x01);
2015-05-16 13:16:40 +00:00
rc = packet__read_string(&mosq->in_packet, &message->msg.topic);
2014-05-07 22:27:00 +00:00
if(rc){
2015-05-16 13:29:54 +00:00
message__cleanup(&message);
2014-05-07 22:27:00 +00:00
return rc;
}
if(!strlen(message->msg.topic)){
2015-05-16 13:29:54 +00:00
message__cleanup(&message);
2014-05-07 22:27:00 +00:00
return MOSQ_ERR_PROTOCOL;
}
if(message->msg.qos > 0){
2015-05-16 13:16:40 +00:00
rc = packet__read_uint16(&mosq->in_packet, &mid);
2014-05-07 22:27:00 +00:00
if(rc){
2015-05-16 13:29:54 +00:00
message__cleanup(&message);
2014-05-07 22:27:00 +00:00
return rc;
}
message->msg.mid = (int)mid;
}
message->msg.payloadlen = mosq->in_packet.remaining_length - mosq->in_packet.pos;
if(message->msg.payloadlen){
message->msg.payload = mosquitto__calloc(message->msg.payloadlen+1, sizeof(uint8_t));
2014-05-07 22:27:00 +00:00
if(!message->msg.payload){
2015-05-16 13:29:54 +00:00
message__cleanup(&message);
2014-05-07 22:27:00 +00:00
return MOSQ_ERR_NOMEM;
}
2015-05-16 13:16:40 +00:00
rc = packet__read_bytes(&mosq->in_packet, message->msg.payload, message->msg.payloadlen);
2014-05-07 22:27:00 +00:00
if(rc){
2015-05-16 13:29:54 +00:00
message__cleanup(&message);
2014-05-07 22:27:00 +00:00
return rc;
}
}
2015-05-18 07:53:21 +00:00
log__printf(mosq, MOSQ_LOG_DEBUG,
2014-05-07 22:27:00 +00:00
"Client %s received PUBLISH (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))",
mosq->id, message->dup, message->msg.qos, message->msg.retain,
message->msg.mid, message->msg.topic,
(long)message->msg.payloadlen);
message->timestamp = mosquitto_time();
switch(message->msg.qos){
case 0:
pthread_mutex_lock(&mosq->callback_mutex);
if(mosq->on_message){
mosq->in_callback = true;
mosq->on_message(mosq, mosq->userdata, &message->msg);
mosq->in_callback = false;
}
pthread_mutex_unlock(&mosq->callback_mutex);
2015-05-16 13:29:54 +00:00
message__cleanup(&message);
2014-05-07 22:27:00 +00:00
return MOSQ_ERR_SUCCESS;
case 1:
2015-05-16 14:24:24 +00:00
rc = send__puback(mosq, message->msg.mid);
2014-05-07 22:27:00 +00:00
pthread_mutex_lock(&mosq->callback_mutex);
if(mosq->on_message){
mosq->in_callback = true;
mosq->on_message(mosq, mosq->userdata, &message->msg);
mosq->in_callback = false;
}
pthread_mutex_unlock(&mosq->callback_mutex);
2015-05-16 13:29:54 +00:00
message__cleanup(&message);
2014-05-07 22:27:00 +00:00
return rc;
case 2:
2015-05-16 14:24:24 +00:00
rc = send__pubrec(mosq, message->msg.mid);
2014-05-07 22:27:00 +00:00
pthread_mutex_lock(&mosq->in_message_mutex);
message->state = mosq_ms_wait_for_pubrel;
2015-05-16 13:29:54 +00:00
message__queue(mosq, message, mosq_md_in);
2014-05-07 22:27:00 +00:00
pthread_mutex_unlock(&mosq->in_message_mutex);
return rc;
default:
2015-05-16 13:29:54 +00:00
message__cleanup(&message);
2014-05-07 22:27:00 +00:00
return MOSQ_ERR_PROTOCOL;
}
}