1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | #include <assert.h>
|
31 | #include <errno.h>
|
32 | #include <stdio.h>
|
33 | #include <string.h>
|
34 | #ifndef WIN32
|
35 | #include <sys/select.h>
|
36 | #include <unistd.h>
|
37 | #else
|
38 | #include <winsock2.h>
|
39 | typedef int ssize_t;
|
40 | #endif
|
41 |
|
42 | #include "mosquitto.h"
|
43 | #include "mosquitto_internal.h"
|
44 | #include "logging_mosq.h"
|
45 | #include "messages_mosq.h"
|
46 | #include "memory_mosq.h"
|
47 | #include "mqtt3_protocol.h"
|
48 | #include "net_mosq.h"
|
49 | #include "read_handle.h"
|
50 | #include "send_mosq.h"
|
51 | #include "util_mosq.h"
|
52 |
|
53 | int _mosquitto_will_set(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain)
|
54 | {
|
55 | int rc = MOSQ_ERR_SUCCESS;
|
56 |
|
57 | if(!mosq || !topic) return MOSQ_ERR_INVAL;
|
58 | if(payloadlen < 0 || payloadlen > MQTT_MAX_PAYLOAD) return MOSQ_ERR_PAYLOAD_SIZE;
|
59 | if(payloadlen > 0 && !payload) return MOSQ_ERR_INVAL;
|
60 |
|
61 | if(_mosquitto_topic_wildcard_pos_check(topic)) return MOSQ_ERR_INVAL;
|
62 |
|
63 | if(mosq->will){
|
64 | if(mosq->will->topic){
|
65 | _mosquitto_free(mosq->will->topic);
|
66 | mosq->will->topic = NULL;
|
67 | }
|
68 | if(mosq->will->payload){
|
69 | _mosquitto_free(mosq->will->payload);
|
70 | mosq->will->payload = NULL;
|
71 | }
|
72 | _mosquitto_free(mosq->will);
|
73 | mosq->will = NULL;
|
74 | }
|
75 |
|
76 | mosq->will = _mosquitto_calloc(1, sizeof(struct mosquitto_message));
|
77 | if(!mosq->will) return MOSQ_ERR_NOMEM;
|
78 | mosq->will->topic = _mosquitto_strdup(topic);
|
79 | if(!mosq->will->topic){
|
80 | rc = MOSQ_ERR_NOMEM;
|
81 | goto cleanup;
|
82 | }
|
83 | mosq->will->payloadlen = payloadlen;
|
84 | if(mosq->will->payloadlen > 0){
|
85 | if(!payload){
|
86 | rc = MOSQ_ERR_INVAL;
|
87 | goto cleanup;
|
88 | }
|
89 | mosq->will->payload = _mosquitto_malloc(sizeof(char)*mosq->will->payloadlen);
|
90 | if(!mosq->will->payload){
|
91 | rc = MOSQ_ERR_NOMEM;
|
92 | goto cleanup;
|
93 | }
|
94 |
|
95 | memcpy(mosq->will->payload, payload, payloadlen);
|
96 | }
|
97 | mosq->will->qos = qos;
|
98 | mosq->will->retain = retain;
|
99 |
|
100 | return MOSQ_ERR_SUCCESS;
|
101 |
|
102 | cleanup:
|
103 | if(mosq->will){
|
104 | if(mosq->will->topic) _mosquitto_free(mosq->will->topic);
|
105 | if(mosq->will->payload) _mosquitto_free(mosq->will->payload);
|
106 | }
|
107 | _mosquitto_free(mosq->will);
|
108 | mosq->will = NULL;
|
109 |
|
110 | return rc;
|
111 | }
|
112 |
|
113 | int _mosquitto_will_clear(struct mosquitto *mosq)
|
114 | {
|
115 | if(!mosq->will) return MOSQ_ERR_SUCCESS;
|
116 |
|
117 | if(mosq->will->topic){
|
118 | _mosquitto_free(mosq->will->topic);
|
119 | mosq->will->topic = NULL;
|
120 | }
|
121 | if(mosq->will->payload){
|
122 | _mosquitto_free(mosq->will->payload);
|
123 | mosq->will->payload = NULL;
|
124 | }
|
125 | _mosquitto_free(mosq->will);
|
126 | mosq->will = NULL;
|
127 |
|
128 | return MOSQ_ERR_SUCCESS;
|
129 | }
|
130 |
|