/src/mosquitto/src/send_connack.c
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2009-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 "mosquitto_broker_internal.h" |
22 | | #include "mosquitto/mqtt_protocol.h" |
23 | | #include "packet_mosq.h" |
24 | | #include "property_mosq.h" |
25 | | #include "sys_tree.h" |
26 | | #include "util_mosq.h" |
27 | | |
28 | | |
29 | | int send__connack(struct mosquitto *context, uint8_t ack, uint8_t reason_code, const mosquitto_property *properties) |
30 | 1.21k | { |
31 | 1.21k | struct mosquitto__packet *packet = NULL; |
32 | 1.21k | int rc; |
33 | 1.21k | mosquitto_property *connack_props = NULL; |
34 | 1.21k | uint32_t remaining_length; |
35 | | |
36 | 1.21k | rc = mosquitto_property_copy_all(&connack_props, properties); |
37 | 1.21k | if(rc){ |
38 | 0 | return rc; |
39 | 0 | } |
40 | | |
41 | 1.21k | if(context->id){ |
42 | 80 | log__printf(NULL, MOSQ_LOG_DEBUG, "Sending CONNACK to %s (%d, %d)", context->id, ack, reason_code); |
43 | 1.13k | }else{ |
44 | 1.13k | log__printf(NULL, MOSQ_LOG_DEBUG, "Sending CONNACK to %s (%d, %d)", context->address, ack, reason_code); |
45 | 1.13k | } |
46 | | |
47 | 1.21k | remaining_length = 2; |
48 | | |
49 | 1.21k | if(context->protocol == mosq_p_mqtt5){ |
50 | 1.13k | if(reason_code < 128 && db.config->retain_available == false){ |
51 | 2 | rc = mosquitto_property_add_byte(&connack_props, MQTT_PROP_RETAIN_AVAILABLE, 0); |
52 | 2 | if(rc){ |
53 | 0 | mosquitto_property_free_all(&connack_props); |
54 | 0 | return rc; |
55 | 0 | } |
56 | 2 | } |
57 | 1.13k | if(reason_code < 128 && db.config->max_packet_size > 0){ |
58 | 0 | rc = mosquitto_property_add_int32(&connack_props, MQTT_PROP_MAXIMUM_PACKET_SIZE, db.config->max_packet_size); |
59 | 0 | if(rc){ |
60 | 0 | mosquitto_property_free_all(&connack_props); |
61 | 0 | return rc; |
62 | 0 | } |
63 | 0 | } |
64 | 1.13k | if(reason_code < 128 && db.config->max_inflight_messages > 0){ |
65 | 0 | rc = mosquitto_property_add_int16(&connack_props, MQTT_PROP_RECEIVE_MAXIMUM, db.config->max_inflight_messages); |
66 | 0 | if(rc){ |
67 | 0 | mosquitto_property_free_all(&connack_props); |
68 | 0 | return rc; |
69 | 0 | } |
70 | 0 | } |
71 | 1.13k | if(context->listener->max_qos != 2){ |
72 | 1.13k | rc = mosquitto_property_add_byte(&connack_props, MQTT_PROP_MAXIMUM_QOS, context->listener->max_qos); |
73 | 1.13k | if(rc){ |
74 | 0 | mosquitto_property_free_all(&connack_props); |
75 | 0 | return rc; |
76 | 0 | } |
77 | 1.13k | } |
78 | | |
79 | 1.13k | remaining_length += mosquitto_property_get_remaining_length(connack_props); |
80 | 1.13k | } |
81 | | |
82 | 1.21k | if(packet__check_oversize(context, remaining_length)){ |
83 | 3 | mosquitto_property_free_all(&connack_props); |
84 | 3 | return MOSQ_ERR_OVERSIZE_PACKET; |
85 | 3 | } |
86 | | |
87 | 1.21k | rc = packet__alloc(&packet, CMD_CONNACK, remaining_length); |
88 | 1.21k | if(rc){ |
89 | 0 | mosquitto_property_free_all(&connack_props); |
90 | 0 | return rc; |
91 | 0 | } |
92 | 1.21k | packet__write_byte(packet, ack); |
93 | 1.21k | packet__write_byte(packet, reason_code); |
94 | 1.21k | if(context->protocol == mosq_p_mqtt5){ |
95 | 1.13k | property__write_all(packet, connack_props, true); |
96 | 1.13k | } |
97 | 1.21k | mosquitto_property_free_all(&connack_props); |
98 | | |
99 | 1.21k | metrics__int_inc(mosq_counter_mqtt_connack_sent, 1); |
100 | 1.21k | return packet__queue(context, packet); |
101 | 1.21k | } |
102 | | |