APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright (c) 2009-2019 Roger Light <roger@atchoo.org>
 
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.
*/
 
#include "config.h"
 
#include <stdio.h>
#include <string.h>
 
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "packet_mosq.h"
#include "property_mosq.h"
 
 
 
int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context)
{
    int rc = 0;
    int rc2;
    uint16_t mid;
    char *sub;
    uint8_t subscription_options;
    uint32_t subscription_identifier = 0;
    uint8_t qos;
    uint8_t retain_handling = 0;
    uint8_t *payload = NULL, *tmp_payload;
    uint32_t payloadlen = 0;
    int len;
    int slen;
    char *sub_mount;
    mosquitto_property *properties = NULL;
 
    if(!context) return MOSQ_ERR_INVAL;
 
    if(context->state != mosq_cs_connected){
        return MOSQ_ERR_PROTOCOL;
    }
 
    log__printf(NULL, MOSQ_LOG_DEBUG, "Received SUBSCRIBE from %s", context->id);
 
    if(context->protocol != mosq_p_mqtt31){
        if((context->in_packet.command&0x0F) != 0x02){
            return MOSQ_ERR_PROTOCOL;
        }
    }
    if(packet__read_uint16(&context->in_packet, &mid)) return 1;
    if(mid == 0) return MOSQ_ERR_PROTOCOL;
 
    if(context->protocol == mosq_p_mqtt5){
        rc = property__read_all(CMD_SUBSCRIBE, &context->in_packet, &properties);
        if(rc) return rc;
 
        if(mosquitto_property_read_varint(properties, MQTT_PROP_SUBSCRIPTION_IDENTIFIER,
                    &subscription_identifier, false)){
 
            /* If the identifier was force set to 0, this is an error */
            if(subscription_identifier == 0){
                mosquitto_property_free_all(&properties);
                return MOSQ_ERR_PROTOCOL;
            }
        }
 
        mosquitto_property_free_all(&properties);
        /* Note - User Property not handled */
    }
 
    while(context->in_packet.pos < context->in_packet.remaining_length){
        sub = NULL;
        if(packet__read_string(&context->in_packet, &sub, &slen)){
            mosquitto__free(payload);
            return 1;
        }
 
        if(sub){
            if(!slen){
                log__printf(NULL, MOSQ_LOG_INFO,
                        "Empty subscription string from %s, disconnecting.",
                        context->address);
                mosquitto__free(sub);
                mosquitto__free(payload);
                return 1;
            }
            if(mosquitto_sub_topic_check(sub)){
                log__printf(NULL, MOSQ_LOG_INFO,
                        "Invalid subscription string from %s, disconnecting.",
                        context->address);
                mosquitto__free(sub);
                mosquitto__free(payload);
                return 1;
            }
 
            if(packet__read_byte(&context->in_packet, &subscription_options)){
                mosquitto__free(sub);
                mosquitto__free(payload);
                return 1;
            }
            if(context->protocol == mosq_p_mqtt31 || context->protocol == mosq_p_mqtt311){
                qos = subscription_options;
                if(context->is_bridge){
                    subscription_options = MQTT_SUB_OPT_RETAIN_AS_PUBLISHED | MQTT_SUB_OPT_NO_LOCAL;
                }
            }else{
                qos = subscription_options & 0x03;
                subscription_options &= 0xFC;
 
                retain_handling = (subscription_options & 0x30);
                if(retain_handling == 0x30 || (subscription_options & 0xC0) != 0){
                    return MOSQ_ERR_PROTOCOL;
                }
            }
            if(qos > 2){
                log__printf(NULL, MOSQ_LOG_INFO,
                        "Invalid QoS in subscription command from %s, disconnecting.",
                        context->address);
                mosquitto__free(sub);
                mosquitto__free(payload);
                return 1;
            }
 
 
            if(context->listener && context->listener->mount_point){
                len = strlen(context->listener->mount_point) + slen + 1;
                sub_mount = mosquitto__malloc(len+1);
                if(!sub_mount){
                    mosquitto__free(sub);
                    mosquitto__free(payload);
                    return MOSQ_ERR_NOMEM;
                }
                snprintf(sub_mount, len, "%s%s", context->listener->mount_point, sub);
                sub_mount[len] = '\0';
 
                mosquitto__free(sub);
                sub = sub_mount;
 
            }
            log__printf(NULL, MOSQ_LOG_DEBUG, "\t%s (QoS %d)", sub, qos);
 
            if(context->protocol != mosq_p_mqtt31){
                rc2 = mosquitto_acl_check(db, context, sub, 0, NULL, qos, false, MOSQ_ACL_SUBSCRIBE);
                switch(rc2){
                    case MOSQ_ERR_SUCCESS:
                        break;
                    case MOSQ_ERR_ACL_DENIED:
                        qos = 0x80;
                        break;
                    default:
                        mosquitto__free(sub);
                        return rc2;
                }
            }
 
            if(qos != 0x80){
                rc2 = sub__add(db, context, sub, qos, subscription_identifier, subscription_options, &db->subs);
                if(rc2 > 0){
                    mosquitto__free(sub);
                    return rc2;
                }
                if(context->protocol == mosq_p_mqtt311 || context->protocol == mosq_p_mqtt31){
                    if(rc2 == MOSQ_ERR_SUCCESS || rc2 == MOSQ_ERR_SUB_EXISTS){
                        if(sub__retain_queue(db, context, sub, qos, 0)) rc = 1;
                    }
                }else{
                    if((retain_handling == MQTT_SUB_OPT_SEND_RETAIN_ALWAYS)
                            || (rc2 == MOSQ_ERR_SUCCESS && retain_handling == MQTT_SUB_OPT_SEND_RETAIN_NEW)){
 
                        if(sub__retain_queue(db, context, sub, qos, subscription_identifier)) rc = 1;
                    }
                }
 
                log__printf(NULL, MOSQ_LOG_SUBSCRIBE, "%s %d %s", context->id, qos, sub);
            }
            mosquitto__free(sub);
 
            tmp_payload = mosquitto__realloc(payload, payloadlen + 1);
            if(tmp_payload){
                payload = tmp_payload;
                payload[payloadlen] = qos;
                payloadlen++;
            }else{
                mosquitto__free(payload);
 
                return MOSQ_ERR_NOMEM;
            }
        }
    }
 
    if(context->protocol != mosq_p_mqtt31){
        if(payloadlen == 0){
            /* No subscriptions specified, protocol error. */
            return MOSQ_ERR_PROTOCOL;
        }
    }
    if(send__suback(context, mid, payloadlen, payload)) rc = 1;
    mosquitto__free(payload);
 
#ifdef WITH_PERSISTENCE
    db->persistence_changes++;
#endif
 
    return rc;
}