2 Copyright (c) 2009-2013 Roger Light <roger@atchoo.org>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14 contributors may be used to endorse or promote products derived from
15 this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
38 #include "mosquitto.h"
39 #include "memory_mosq.h"
41 #include "send_mosq.h"
42 #include "time_mosq.h"
44 #include "util_mosq.h"
47 #include "mosquitto_broker.h"
50 int _mosquitto_packet_alloc(struct _mosquitto_packet *packet)
52 uint8_t remaining_bytes[5], byte;
53 uint32_t remaining_length;
58 remaining_length = packet->remaining_length;
59 packet->payload = NULL;
60 packet->remaining_count = 0;
62 byte = remaining_length % 128;
63 remaining_length = remaining_length / 128;
64 /* If there are more digits to encode, set the top bit of this digit */
65 if(remaining_length > 0){
68 remaining_bytes[packet->remaining_count] = byte;
69 packet->remaining_count++;
70 }while(remaining_length > 0 && packet->remaining_count < 5);
71 if(packet->remaining_count == 5) return MOSQ_ERR_PAYLOAD_SIZE;
72 packet->packet_length = packet->remaining_length + 1 + packet->remaining_count;
73 packet->payload = _mosquitto_malloc(sizeof(uint8_t)*packet->packet_length);
74 if(!packet->payload) return MOSQ_ERR_NOMEM;
76 packet->payload[0] = packet->command;
77 for(i=0; i<packet->remaining_count; i++){
78 packet->payload[i+1] = remaining_bytes[i];
80 packet->pos = 1 + packet->remaining_count;
82 return MOSQ_ERR_SUCCESS;
85 void _mosquitto_check_keepalive(struct mosquitto *mosq)
89 time_t now = mosquitto_time();
95 #if defined(WITH_BROKER) && defined(WITH_BRIDGE)
96 /* Check if a lazy bridge should be timed out due to idle. */
97 if(mosq->bridge && mosq->bridge->start_type == bst_lazy
98 && mosq->sock != INVALID_SOCKET
99 && now - mosq->last_msg_out >= mosq->bridge->idle_timeout){
101 _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Bridge connection %s has exceeded idle timeout, disconnecting.", mosq->id);
102 _mosquitto_socket_close(mosq);
106 pthread_mutex_lock(&mosq->msgtime_mutex);
107 last_msg_out = mosq->last_msg_out;
108 last_msg_in = mosq->last_msg_in;
109 pthread_mutex_unlock(&mosq->msgtime_mutex);
110 if(mosq->keepalive && mosq->sock != INVALID_SOCKET &&
111 (now - last_msg_out >= mosq->keepalive || now - last_msg_in >= mosq->keepalive)){
113 if(mosq->state == mosq_cs_connected && mosq->ping_t == 0){
114 _mosquitto_send_pingreq(mosq);
115 /* Reset last msg times to give the server time to send a pingresp */
116 pthread_mutex_lock(&mosq->msgtime_mutex);
117 mosq->last_msg_in = now;
118 mosq->last_msg_out = now;
119 pthread_mutex_unlock(&mosq->msgtime_mutex);
123 mosq->listener->client_count--;
124 assert(mosq->listener->client_count >= 0);
126 mosq->listener = NULL;
128 _mosquitto_socket_close(mosq);
130 pthread_mutex_lock(&mosq->state_mutex);
131 if(mosq->state == mosq_cs_disconnecting){
132 rc = MOSQ_ERR_SUCCESS;
136 pthread_mutex_unlock(&mosq->state_mutex);
137 pthread_mutex_lock(&mosq->callback_mutex);
138 if(mosq->on_disconnect){
139 mosq->in_callback = true;
140 mosq->on_disconnect(mosq, mosq->userdata, rc);
141 mosq->in_callback = false;
143 pthread_mutex_unlock(&mosq->callback_mutex);
149 uint16_t _mosquitto_mid_generate(struct mosquitto *mosq)
154 if(mosq->last_mid == 0) mosq->last_mid++;
156 return mosq->last_mid;
159 /* Search for + or # in a topic. Return MOSQ_ERR_INVAL if found.
160 * Also returns MOSQ_ERR_INVAL if the topic string is too long.
161 * Returns MOSQ_ERR_SUCCESS if everything is fine.
163 int _mosquitto_topic_wildcard_len_check(const char *str)
166 while(str && str[0]){
167 if(str[0] == '+' || str[0] == '#'){
168 return MOSQ_ERR_INVAL;
173 if(len > 65535) return MOSQ_ERR_INVAL;
175 return MOSQ_ERR_SUCCESS;
178 /* Search for + or # in a topic, check they aren't in invalid positions such as foo/#/bar, foo/+bar or foo/bar#.
179 * Return MOSQ_ERR_INVAL if invalid position found.
180 * Also returns MOSQ_ERR_INVAL if the topic string is too long.
181 * Returns MOSQ_ERR_SUCCESS if everything is fine.
183 int _mosquitto_topic_wildcard_pos_check(const char *str)
187 while(str && str[0]){
189 if((c != '\0' && c != '/') || (str[1] != '\0' && str[1] != '/')){
190 return MOSQ_ERR_INVAL;
192 }else if(str[0] == '#'){
193 if((c != '\0' && c != '/') || str[1] != '\0'){
194 return MOSQ_ERR_INVAL;
201 if(len > 65535) return MOSQ_ERR_INVAL;
203 return MOSQ_ERR_SUCCESS;
206 /* Does a topic match a subscription? */
207 int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result)
211 bool multilevel_wildcard = false;
213 if(!sub || !topic || !result) return MOSQ_ERR_INVAL;
216 tlen = strlen(topic);
219 if((sub[0] == '$' && topic[0] != '$')
220 || (topic[0] == '$' && sub[0] != '$')){
223 return MOSQ_ERR_SUCCESS;
230 while(spos < slen && tpos < tlen){
231 if(sub[spos] == topic[tpos]){
234 if(spos == slen && tpos == tlen){
236 return MOSQ_ERR_SUCCESS;
237 }else if(tpos == tlen && spos == slen-1 && sub[spos] == '+'){
240 return MOSQ_ERR_SUCCESS;
243 if(sub[spos] == '+'){
245 while(tpos < tlen && topic[tpos] != '/'){
248 if(tpos == tlen && spos == slen){
250 return MOSQ_ERR_SUCCESS;
252 }else if(sub[spos] == '#'){
253 multilevel_wildcard = true;
256 return MOSQ_ERR_SUCCESS;
259 return MOSQ_ERR_SUCCESS;
263 return MOSQ_ERR_SUCCESS;
267 /* Check for e.g. foo matching foo/# */
269 && sub[spos+1] == '/'
270 && sub[spos+2] == '#'){
272 multilevel_wildcard = true;
273 return MOSQ_ERR_SUCCESS;
277 if(multilevel_wildcard == false && (tpos < tlen || spos < slen)){
281 return MOSQ_ERR_SUCCESS;
284 #ifdef REAL_WITH_TLS_PSK
285 int _mosquitto_hex2bin(const char *hex, unsigned char *bin, int bin_max_len)
290 if(BN_hex2bn(&bn, hex) == 0){
294 if(BN_num_bytes(bn) > bin_max_len){
299 len = BN_bn2bin(bn, bin);
305 FILE *_mosquitto_fopen(const char *path, const char *mode)
310 rc = ExpandEnvironmentStrings(path, buf, MAX_PATH);
311 if(rc == 0 || rc == MAX_PATH){
314 return fopen(buf, mode);
317 return fopen(path, mode);