2016-03-06 22:30:17 +00:00
|
|
|
/*
|
2018-04-11 14:24:29 +00:00
|
|
|
Copyright (c) 2009-2018 Roger Light <roger@atchoo.org>
|
2016-03-06 22:30:17 +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.
|
|
|
|
*/
|
|
|
|
|
2018-08-16 10:14:51 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-03-06 22:30:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-07-08 09:10:04 +00:00
|
|
|
#include "mosquitto_broker_internal.h"
|
2016-03-06 22:30:17 +00:00
|
|
|
#include "memory_mosq.h"
|
2018-08-29 21:26:16 +00:00
|
|
|
#include "mqtt_protocol.h"
|
2016-03-06 22:30:17 +00:00
|
|
|
#include "packet_mosq.h"
|
|
|
|
#include "send_mosq.h"
|
|
|
|
|
|
|
|
int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context)
|
|
|
|
{
|
|
|
|
uint16_t mid;
|
|
|
|
char *sub;
|
2018-02-11 20:47:17 +00:00
|
|
|
int slen;
|
2018-10-25 11:03:22 +00:00
|
|
|
int rc;
|
|
|
|
struct mqtt5__property *properties = NULL;
|
2016-03-06 22:30:17 +00:00
|
|
|
|
|
|
|
if(!context) return MOSQ_ERR_INVAL;
|
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Received UNSUBSCRIBE from %s", context->id);
|
|
|
|
|
2018-08-30 20:14:38 +00:00
|
|
|
if(context->protocol != mosq_p_mqtt31){
|
2016-03-06 22:30:17 +00:00
|
|
|
if((context->in_packet.command&0x0F) != 0x02){
|
|
|
|
return MOSQ_ERR_PROTOCOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(packet__read_uint16(&context->in_packet, &mid)) return 1;
|
2018-10-25 11:12:57 +00:00
|
|
|
if(mid == 0) return MOSQ_ERR_PROTOCOL;
|
2016-03-06 22:30:17 +00:00
|
|
|
|
2018-10-25 11:03:22 +00:00
|
|
|
if(context->protocol == mosq_p_mqtt5){
|
|
|
|
rc = property__read_all(UNSUBSCRIBE, &context->in_packet, &properties);
|
|
|
|
if(rc) return rc;
|
|
|
|
/* Immediately free, we don't do anything with User Property at the moment */
|
2018-10-30 10:11:20 +00:00
|
|
|
mosquitto_property_free_all(&properties);
|
2018-10-25 11:03:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 20:14:38 +00:00
|
|
|
if(context->protocol == mosq_p_mqtt311 || context->protocol == mosq_p_mqtt5){
|
2018-01-05 16:34:28 +00:00
|
|
|
if(context->in_packet.pos == context->in_packet.remaining_length){
|
|
|
|
/* No topic specified, protocol error. */
|
|
|
|
return MOSQ_ERR_PROTOCOL;
|
|
|
|
}
|
|
|
|
}
|
2016-03-06 22:30:17 +00:00
|
|
|
while(context->in_packet.pos < context->in_packet.remaining_length){
|
|
|
|
sub = NULL;
|
2018-02-11 20:47:17 +00:00
|
|
|
if(packet__read_string(&context->in_packet, &sub, &slen)){
|
2016-03-06 22:30:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sub){
|
2018-02-11 20:47:17 +00:00
|
|
|
if(!slen){
|
2016-06-21 16:05:33 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_INFO,
|
|
|
|
"Empty unsubscription string from %s, disconnecting.",
|
|
|
|
context->id);
|
2016-03-06 22:30:17 +00:00
|
|
|
mosquitto__free(sub);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(mosquitto_sub_topic_check(sub)){
|
2016-06-21 16:05:33 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_INFO,
|
|
|
|
"Invalid unsubscription string from %s, disconnecting.",
|
|
|
|
context->id);
|
|
|
|
mosquitto__free(sub);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-03-06 22:30:17 +00:00
|
|
|
|
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "\t%s", sub);
|
2016-07-19 14:05:53 +00:00
|
|
|
sub__remove(db, context, sub, db->subs);
|
2016-03-06 22:30:17 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_UNSUBSCRIBE, "%s %s", context->id, sub);
|
|
|
|
mosquitto__free(sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
db->persistence_changes++;
|
|
|
|
#endif
|
|
|
|
|
2018-08-09 10:54:24 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Sending UNSUBACK to %s", context->id);
|
|
|
|
|
2018-10-25 12:21:42 +00:00
|
|
|
/* We don't use Reason String or User Property yet. */
|
2018-10-10 11:42:27 +00:00
|
|
|
return send__command_with_mid(context, UNSUBACK, mid, false, NULL);
|
2016-03-06 22:30:17 +00:00
|
|
|
}
|
|
|
|
|