2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2020-02-27 23:26:58 +00:00
|
|
|
Copyright (c) 2010-2020 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.
|
|
|
|
*/
|
|
|
|
|
2018-08-16 10:14:51 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-04-16 10:48:42 +00:00
|
|
|
#ifdef WITH_BROKER
|
|
|
|
# include "mosquitto_broker_internal.h"
|
|
|
|
#endif
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "mosquitto.h"
|
|
|
|
#include "mosquitto_internal.h"
|
|
|
|
#include "logging_mosq.h"
|
|
|
|
#include "messages_mosq.h"
|
|
|
|
#include "memory_mosq.h"
|
2018-08-29 21:26:16 +00:00
|
|
|
#include "mqtt_protocol.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "net_mosq.h"
|
|
|
|
#include "read_handle.h"
|
|
|
|
#include "send_mosq.h"
|
|
|
|
#include "util_mosq.h"
|
2018-11-01 11:37:57 +00:00
|
|
|
#include "will_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2018-11-01 11:37:57 +00:00
|
|
|
int will__set(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain, mosquitto_property *properties)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc = MOSQ_ERR_SUCCESS;
|
2018-11-01 11:37:57 +00:00
|
|
|
mosquitto_property *p;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(!mosq || !topic) return MOSQ_ERR_INVAL;
|
|
|
|
if(payloadlen < 0 || payloadlen > MQTT_MAX_PAYLOAD) return MOSQ_ERR_PAYLOAD_SIZE;
|
|
|
|
if(payloadlen > 0 && !payload) return MOSQ_ERR_INVAL;
|
|
|
|
|
2014-09-10 14:57:20 +00:00
|
|
|
if(mosquitto_pub_topic_check(topic)) return MOSQ_ERR_INVAL;
|
2016-06-20 21:56:01 +00:00
|
|
|
if(mosquitto_validate_utf8(topic, strlen(topic))) return MOSQ_ERR_MALFORMED_UTF8;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2018-11-01 11:37:57 +00:00
|
|
|
if(properties){
|
|
|
|
if(mosq->protocol != mosq_p_mqtt5){
|
|
|
|
return MOSQ_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
p = properties;
|
|
|
|
while(p){
|
2018-11-01 18:53:06 +00:00
|
|
|
rc = mosquitto_property_check_command(CMD_WILL, p->identifier);
|
2018-11-01 11:37:57 +00:00
|
|
|
if(rc) return rc;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
if(mosq->will){
|
2018-10-24 13:07:09 +00:00
|
|
|
mosquitto__free(mosq->will->msg.topic);
|
|
|
|
mosquitto__free(mosq->will->msg.payload);
|
2018-11-01 11:37:57 +00:00
|
|
|
mosquitto_property_free_all(&mosq->will->properties);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 13:07:09 +00:00
|
|
|
mosq->will = mosquitto__calloc(1, sizeof(struct mosquitto_message_all));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!mosq->will) return MOSQ_ERR_NOMEM;
|
2018-10-24 13:07:09 +00:00
|
|
|
mosq->will->msg.topic = mosquitto__strdup(topic);
|
|
|
|
if(!mosq->will->msg.topic){
|
2014-05-07 22:27:00 +00:00
|
|
|
rc = MOSQ_ERR_NOMEM;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-10-24 13:07:09 +00:00
|
|
|
mosq->will->msg.payloadlen = payloadlen;
|
|
|
|
if(mosq->will->msg.payloadlen > 0){
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!payload){
|
|
|
|
rc = MOSQ_ERR_INVAL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-10-24 13:07:09 +00:00
|
|
|
mosq->will->msg.payload = mosquitto__malloc(sizeof(char)*mosq->will->msg.payloadlen);
|
|
|
|
if(!mosq->will->msg.payload){
|
2014-05-07 22:27:00 +00:00
|
|
|
rc = MOSQ_ERR_NOMEM;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:07:09 +00:00
|
|
|
memcpy(mosq->will->msg.payload, payload, payloadlen);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2018-10-24 13:07:09 +00:00
|
|
|
mosq->will->msg.qos = qos;
|
|
|
|
mosq->will->msg.retain = retain;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2018-11-01 11:37:57 +00:00
|
|
|
mosq->will->properties = properties;
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if(mosq->will){
|
2018-10-24 13:07:09 +00:00
|
|
|
mosquitto__free(mosq->will->msg.topic);
|
|
|
|
mosquitto__free(mosq->will->msg.payload);
|
2015-09-22 13:42:56 +00:00
|
|
|
|
|
|
|
mosquitto__free(mosq->will);
|
|
|
|
mosq->will = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int will__clear(struct mosquitto *mosq)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
if(!mosq->will) return MOSQ_ERR_SUCCESS;
|
|
|
|
|
2018-10-24 13:07:09 +00:00
|
|
|
mosquitto__free(mosq->will->msg.topic);
|
|
|
|
mosq->will->msg.topic = NULL;
|
2015-09-22 13:42:56 +00:00
|
|
|
|
2018-10-24 13:07:09 +00:00
|
|
|
mosquitto__free(mosq->will->msg.payload);
|
|
|
|
mosq->will->msg.payload = NULL;
|
2015-09-22 13:42:56 +00:00
|
|
|
|
2018-11-01 11:37:57 +00:00
|
|
|
mosquitto_property_free_all(&mosq->will->properties);
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosq->will = NULL;
|
2020-08-11 09:56:35 +00:00
|
|
|
mosq->will_delay_interval = 0;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|