/src/mosquitto/lib/will_mosq.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2010-2021 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include <stdio.h> |
22 | | #include <string.h> |
23 | | |
24 | | #ifdef WITH_BROKER |
25 | | # include "mosquitto_broker_internal.h" |
26 | | #endif |
27 | | |
28 | | #include "mosquitto.h" |
29 | | #include "mosquitto_internal.h" |
30 | | #include "logging_mosq.h" |
31 | | #include "messages_mosq.h" |
32 | | #include "mosquitto/mqtt_protocol.h" |
33 | | #include "net_mosq.h" |
34 | | #include "read_handle.h" |
35 | | #include "send_mosq.h" |
36 | | #include "util_mosq.h" |
37 | | #include "will_mosq.h" |
38 | | |
39 | | int will__set(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain, mosquitto_property *properties) |
40 | 0 | { |
41 | 0 | int rc = MOSQ_ERR_SUCCESS; |
42 | 0 | mosquitto_property *p; |
43 | |
|
44 | 0 | if(!mosq || !topic) return MOSQ_ERR_INVAL; |
45 | 0 | if(payloadlen < 0 || payloadlen > (int)MQTT_MAX_PAYLOAD) return MOSQ_ERR_PAYLOAD_SIZE; |
46 | 0 | if(payloadlen > 0 && !payload) return MOSQ_ERR_INVAL; |
47 | | |
48 | 0 | if(mosquitto_pub_topic_check(topic)) return MOSQ_ERR_INVAL; |
49 | 0 | if(mosquitto_validate_utf8(topic, (uint16_t)strlen(topic))) return MOSQ_ERR_MALFORMED_UTF8; |
50 | | |
51 | 0 | if(properties){ |
52 | 0 | if(mosq->protocol != mosq_p_mqtt5){ |
53 | 0 | return MOSQ_ERR_NOT_SUPPORTED; |
54 | 0 | } |
55 | 0 | p = properties; |
56 | 0 | while(p){ |
57 | 0 | rc = mosquitto_property_check_command(CMD_WILL, mosquitto_property_identifier(p)); |
58 | 0 | if(rc) return rc; |
59 | 0 | p = mosquitto_property_next(p); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | if(mosq->will){ |
64 | 0 | mosquitto_FREE(mosq->will->msg.topic); |
65 | 0 | mosquitto_FREE(mosq->will->msg.payload); |
66 | 0 | mosquitto_property_free_all(&mosq->will->properties); |
67 | 0 | mosquitto_FREE(mosq->will); |
68 | 0 | } |
69 | |
|
70 | 0 | mosq->will = mosquitto_calloc(1, sizeof(struct mosquitto_message_all)); |
71 | 0 | if(!mosq->will) return MOSQ_ERR_NOMEM; |
72 | 0 | mosq->will->msg.topic = mosquitto_strdup(topic); |
73 | 0 | if(!mosq->will->msg.topic){ |
74 | 0 | rc = MOSQ_ERR_NOMEM; |
75 | 0 | goto cleanup; |
76 | 0 | } |
77 | 0 | mosq->will->msg.payloadlen = payloadlen; |
78 | 0 | if(mosq->will->msg.payloadlen > 0){ |
79 | 0 | if(!payload){ |
80 | 0 | rc = MOSQ_ERR_INVAL; |
81 | 0 | goto cleanup; |
82 | 0 | } |
83 | 0 | mosq->will->msg.payload = mosquitto_malloc(sizeof(char)*(unsigned int)mosq->will->msg.payloadlen); |
84 | 0 | if(!mosq->will->msg.payload){ |
85 | 0 | rc = MOSQ_ERR_NOMEM; |
86 | 0 | goto cleanup; |
87 | 0 | } |
88 | | |
89 | 0 | memcpy(mosq->will->msg.payload, payload, (unsigned int)payloadlen); |
90 | 0 | } |
91 | 0 | mosq->will->msg.qos = qos; |
92 | 0 | mosq->will->msg.retain = retain; |
93 | |
|
94 | 0 | mosq->will->properties = properties; |
95 | |
|
96 | 0 | return MOSQ_ERR_SUCCESS; |
97 | | |
98 | 0 | cleanup: |
99 | 0 | if(mosq->will){ |
100 | 0 | mosquitto_FREE(mosq->will->msg.topic); |
101 | 0 | mosquitto_FREE(mosq->will->msg.payload); |
102 | 0 | mosquitto_FREE(mosq->will); |
103 | 0 | } |
104 | |
|
105 | 0 | return rc; |
106 | 0 | } |
107 | | |
108 | | int will__clear(struct mosquitto *mosq) |
109 | 12.5k | { |
110 | 12.5k | if(!mosq->will) return MOSQ_ERR_SUCCESS; |
111 | | |
112 | 4 | mosquitto_FREE(mosq->will->msg.topic); |
113 | 4 | mosquitto_FREE(mosq->will->msg.payload); |
114 | | |
115 | 4 | mosquitto_property_free_all(&mosq->will->properties); |
116 | | |
117 | 4 | mosquitto_FREE(mosq->will); |
118 | 4 | mosq->will_delay_interval = 0; |
119 | | |
120 | 4 | return MOSQ_ERR_SUCCESS; |
121 | 12.5k | } |
122 | | |