| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/mqtt/mqtt.h> |
| 7 |
|
| 8 |
#include <aws/common/encoding.h> |
| 9 |
#include <aws/http/http.h> |
| 10 |
#include <aws/io/logging.h> |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
static bool s_is_valid_topic(const struct aws_byte_cursor *topic, bool is_filter) { |
| 17 |
if (topic == NULL) { |
| 18 |
return false; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
if (!topic->ptr || !topic->len) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
if (aws_mqtt_validate_utf8_text(*topic) == AWS_OP_ERR) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
if (memchr(topic->ptr, 0, topic->len)) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
if (topic->len > 65535) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
bool saw_hash = false; |
| 41 |
|
| 42 |
struct aws_byte_cursor topic_part; |
| 43 |
AWS_ZERO_STRUCT(topic_part); |
| 44 |
while (aws_byte_cursor_next_split(topic, '/', &topic_part)) { |
| 45 |
|
| 46 |
if (saw_hash) { |
| 47 |
|
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
if (topic_part.len == 0) { |
| 52 |
|
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
if (memchr(topic_part.ptr, '+', topic_part.len)) { |
| 58 |
if (!is_filter) { |
| 59 |
|
| 60 |
return false; |
| 61 |
} |
| 62 |
if (topic_part.len > 1) { |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
if (memchr(topic_part.ptr, '#', topic_part.len)) { |
| 70 |
if (!is_filter) { |
| 71 |
|
| 72 |
return false; |
| 73 |
} |
| 74 |
if (topic_part.len > 1) { |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
saw_hash = true; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
bool aws_mqtt_is_valid_topic(const struct aws_byte_cursor *topic) { |
| 86 |
|
| 87 |
return s_is_valid_topic(topic, false); |
| 88 |
} |
| 89 |
bool aws_mqtt_is_valid_topic_filter(const struct aws_byte_cursor *topic_filter) { |
| 90 |
|
| 91 |
return s_is_valid_topic(topic_filter, true); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
#define AWS_DEFINE_ERROR_INFO_MQTT(C, ES) AWS_DEFINE_ERROR_INFO(C, ES, "libaws-c-mqtt") |
| 99 |
|
| 100 |
static struct aws_error_info s_errors[] = { |
| 101 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 102 |
AWS_ERROR_MQTT_INVALID_RESERVED_BITS, |
| 103 |
"Bits marked as reserved in the MQTT spec were incorrectly set."), |
| 104 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 105 |
AWS_ERROR_MQTT_BUFFER_TOO_BIG, |
| 106 |
"[MQTT-1.5.3] Encoded UTF-8 buffers may be no bigger than 65535 bytes."), |
| 107 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 108 |
AWS_ERROR_MQTT_INVALID_REMAINING_LENGTH, |
| 109 |
"[MQTT-2.2.3] Encoded remaining length field is malformed."), |
| 110 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 111 |
AWS_ERROR_MQTT_UNSUPPORTED_PROTOCOL_NAME, |
| 112 |
"[MQTT-3.1.2-1] Protocol name specified is unsupported."), |
| 113 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 114 |
AWS_ERROR_MQTT_UNSUPPORTED_PROTOCOL_LEVEL, |
| 115 |
"[MQTT-3.1.2-2] Protocol level specified is unsupported."), |
| 116 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 117 |
AWS_ERROR_MQTT_INVALID_CREDENTIALS, |
| 118 |
"[MQTT-3.1.2-21] Connect packet may not include password when no username is present."), |
| 119 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 120 |
AWS_ERROR_MQTT_INVALID_QOS, |
| 121 |
"Both bits in a QoS field must not be set."), |
| 122 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 123 |
AWS_ERROR_MQTT_INVALID_PACKET_TYPE, |
| 124 |
"Packet type in packet fixed header is invalid."), |
| 125 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 126 |
AWS_ERROR_MQTT_INVALID_TOPIC, |
| 127 |
"Topic or filter is invalid."), |
| 128 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 129 |
AWS_ERROR_MQTT_TIMEOUT, |
| 130 |
"Time limit between request and response has been exceeded."), |
| 131 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 132 |
AWS_ERROR_MQTT_PROTOCOL_ERROR, |
| 133 |
"Protocol error occurred."), |
| 134 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 135 |
AWS_ERROR_MQTT_NOT_CONNECTED, |
| 136 |
"The requested operation is invalid as the connection is not open."), |
| 137 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 138 |
AWS_ERROR_MQTT_ALREADY_CONNECTED, |
| 139 |
"The requested operation is invalid as the connection is already open."), |
| 140 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 141 |
AWS_ERROR_MQTT_BUILT_WITHOUT_WEBSOCKETS, |
| 142 |
"Library built without MQTT_WITH_WEBSOCKETS option."), |
| 143 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 144 |
AWS_ERROR_MQTT_UNEXPECTED_HANGUP, |
| 145 |
"The connection was closed unexpectedly."), |
| 146 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 147 |
AWS_ERROR_MQTT_CONNECTION_SHUTDOWN, |
| 148 |
"MQTT operation interrupted by connection shutdown."), |
| 149 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 150 |
AWS_ERROR_MQTT_CONNECTION_DESTROYED, |
| 151 |
"Connection has started destroying process, all uncompleted requests will fail."), |
| 152 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 153 |
AWS_ERROR_MQTT_CONNECTION_DISCONNECTING, |
| 154 |
"Connection is disconnecting, it's not safe to do this operation until the connection finishes shutdown."), |
| 155 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 156 |
AWS_ERROR_MQTT_CANCELLED_FOR_CLEAN_SESSION, |
| 157 |
"Old requests from the previous session are cancelled, and offline request will not be accept."), |
| 158 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 159 |
AWS_ERROR_MQTT_QUEUE_FULL, |
| 160 |
"MQTT request queue is full."), |
| 161 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 162 |
AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION, |
| 163 |
"Invalid mqtt5 client options value."), |
| 164 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 165 |
AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION, |
| 166 |
"Invalid mqtt5 connect packet options value."), |
| 167 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 168 |
AWS_ERROR_MQTT5_DISCONNECT_OPTIONS_VALIDATION, |
| 169 |
"Invalid mqtt5 disconnect packet options value."), |
| 170 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 171 |
AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION, |
| 172 |
"Invalid mqtt5 publish packet options value."), |
| 173 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 174 |
AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION, |
| 175 |
"Invalid mqtt5 subscribe packet options value."), |
| 176 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 177 |
AWS_ERROR_MQTT5_UNSUBSCRIBE_OPTIONS_VALIDATION, |
| 178 |
"Invalid mqtt5 unsubscribe packet options value."), |
| 179 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 180 |
AWS_ERROR_MQTT5_USER_PROPERTY_VALIDATION, |
| 181 |
"Invalid mqtt5 user property value."), |
| 182 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 183 |
AWS_ERROR_MQTT5_PACKET_VALIDATION, |
| 184 |
"General mqtt5 packet validation error"), |
| 185 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 186 |
AWS_ERROR_MQTT5_ENCODE_FAILURE, |
| 187 |
"Error occurred while encoding an outgoing mqtt5 packet"), |
| 188 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 189 |
AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR, |
| 190 |
"Mqtt5 decoder received an invalid packet that broke mqtt5 protocol rules"), |
| 191 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 192 |
AWS_ERROR_MQTT5_CONNACK_CONNECTION_REFUSED, |
| 193 |
"Remote endpoint rejected the CONNECT attempt by returning an unsuccessful CONNACK"), |
| 194 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 195 |
AWS_ERROR_MQTT5_CONNACK_TIMEOUT, |
| 196 |
"Remote endpoint did not respond to a CONNECT request before timeout exceeded"), |
| 197 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 198 |
AWS_ERROR_MQTT5_PING_RESPONSE_TIMEOUT, |
| 199 |
"Remote endpoint did not respond to a PINGREQ before timeout exceeded"), |
| 200 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 201 |
AWS_ERROR_MQTT5_USER_REQUESTED_STOP, |
| 202 |
"Mqtt5 client connection interrupted by user request."), |
| 203 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 204 |
AWS_ERROR_MQTT5_DISCONNECT_RECEIVED, |
| 205 |
"Mqtt5 client connection interrupted by server DISCONNECT."), |
| 206 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 207 |
AWS_ERROR_MQTT5_CLIENT_TERMINATED, |
| 208 |
"Mqtt5 client terminated by user request."), |
| 209 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 210 |
AWS_ERROR_MQTT5_OPERATION_FAILED_DUE_TO_OFFLINE_QUEUE_POLICY, |
| 211 |
"Mqtt5 operation failed due to a disconnection event in conjunction with the client's offline queue retention policy."), |
| 212 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 213 |
AWS_ERROR_MQTT5_ENCODE_SIZE_UNSUPPORTED_PACKET_TYPE, |
| 214 |
"Unsupported packet type for encode size calculation"), |
| 215 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 216 |
AWS_ERROR_MQTT5_OPERATION_PROCESSING_FAILURE, |
| 217 |
"Error while processing mqtt5 operational state"), |
| 218 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 219 |
AWS_ERROR_MQTT5_INVALID_INBOUND_TOPIC_ALIAS, |
| 220 |
"Incoming publish contained an invalid (too large or unknown) topic alias"), |
| 221 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 222 |
AWS_ERROR_MQTT5_INVALID_OUTBOUND_TOPIC_ALIAS, |
| 223 |
"Outgoing publish contained an invalid (too large or unknown) topic alias"), |
| 224 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 225 |
AWS_ERROR_MQTT5_INVALID_UTF8_STRING, |
| 226 |
"Outbound packet contains invalid utf-8 data in a field that must be utf-8"), |
| 227 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 228 |
AWS_ERROR_MQTT_CONNECTION_RESET_FOR_ADAPTER_CONNECT, |
| 229 |
"Mqtt5 connection was reset by the Mqtt3 adapter in order to guarantee correct connection configuration"), |
| 230 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 231 |
AWS_ERROR_MQTT_CONNECTION_RESUBSCRIBE_NO_TOPICS, |
| 232 |
"Resubscribe was called when there were no subscriptions"), |
| 233 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 234 |
AWS_ERROR_MQTT_CONNECTION_SUBSCRIBE_FAILURE, |
| 235 |
"MQTT subscribe operation failed"), |
| 236 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 237 |
AWS_ERROR_MQTT_ACK_REASON_CODE_FAILURE, |
| 238 |
"MQTT ack packet received with a failing reason code"), |
| 239 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 240 |
AWS_ERROR_MQTT_PROTOCOL_ADAPTER_FAILING_REASON_CODE, |
| 241 |
"MQTT operation returned a failing reason code"), |
| 242 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 243 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_CLIENT_SHUT_DOWN, |
| 244 |
"Request operation failed due to client shut down"), |
| 245 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 246 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_TIMEOUT, |
| 247 |
"Request operation failed due to timeout"), |
| 248 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 249 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_NO_SUBSCRIPTION_CAPACITY, |
| 250 |
"Streaming request operation failed because there was no space for the subscription"), |
| 251 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 252 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_SUBSCRIBE_FAILURE, |
| 253 |
"Request operation failed because the associated subscribe failed synchronously"), |
| 254 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 255 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_INTERNAL_ERROR, |
| 256 |
"Request operation failed due to a non-specific internal error within the client."), |
| 257 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 258 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_PUBLISH_FAILURE, |
| 259 |
"Request-response operation failed because the associated publish failed synchronously."), |
| 260 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 261 |
AWS_ERROR_MQTT_REUQEST_RESPONSE_STREAM_ALREADY_ACTIVATED, |
| 262 |
"Streaming operation activation failed because the operation had already been activated."), |
| 263 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 264 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_MODELED_SERVICE_ERROR, |
| 265 |
"Request-response operation failed with a modeled service error."), |
| 266 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 267 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_PAYLOAD_PARSE_ERROR, |
| 268 |
"Request-response operation failed due to an inability to parse the payload."), |
| 269 |
AWS_DEFINE_ERROR_INFO_MQTT( |
| 270 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_INVALID_RESPONSE_PATH, |
| 271 |
"Request-response operation failed due to arrival on an unknown response path"), |
| 272 |
}; |
| 273 |
|
| 274 |
#undef AWS_DEFINE_ERROR_INFO_MQTT |
| 275 |
|
| 276 |
static struct aws_error_info_list s_error_list = { |
| 277 |
.error_list = s_errors, |
| 278 |
.count = AWS_ARRAY_SIZE(s_errors), |
| 279 |
}; |
| 280 |
|
| 281 |
|
| 282 |
static struct aws_log_subject_info s_logging_subjects[] = { |
| 283 |
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT_GENERAL, "mqtt", "Misc MQTT logging"), |
| 284 |
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT_CLIENT, "mqtt-client", "MQTT client and connections"), |
| 285 |
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT_TOPIC_TREE, "mqtt-topic-tree", "MQTT subscription tree"), |
| 286 |
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT5_GENERAL, "mqtt5-general", "Misc MQTT5 logging"), |
| 287 |
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT5_CLIENT, "mqtt5-client", "MQTT5 client and connections"), |
| 288 |
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT5_CANARY, "mqtt5-canary", "MQTT5 canary logging"), |
| 289 |
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT5_TO_MQTT3_ADAPTER, "mqtt5-to-mqtt3-adapter", "MQTT5-To-MQTT3 adapter logging"), |
| 290 |
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT_REQUEST_RESPONSE, "mqtt-request-response-systems", "MQTT request-response systems logging"), |
| 291 |
}; |
| 292 |
|
| 293 |
|
| 294 |
static struct aws_log_subject_info_list s_logging_subjects_list = { |
| 295 |
.subject_list = s_logging_subjects, |
| 296 |
.count = AWS_ARRAY_SIZE(s_logging_subjects), |
| 297 |
}; |
| 298 |
|
| 299 |
static bool s_mqtt_library_initialized = false; |
| 300 |
|
| 301 |
void aws_mqtt_library_init(struct aws_allocator *allocator) { |
| 302 |
|
| 303 |
(void)allocator; |
| 304 |
|
| 305 |
if (!s_mqtt_library_initialized) { |
| 306 |
s_mqtt_library_initialized = true; |
| 307 |
aws_io_library_init(allocator); |
| 308 |
aws_http_library_init(allocator); |
| 309 |
|
| 310 |
aws_register_error_info(&s_error_list); |
| 311 |
aws_register_log_subject_info_list(&s_logging_subjects_list); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
void aws_mqtt_library_clean_up(void) { |
| 316 |
if (s_mqtt_library_initialized) { |
| 317 |
s_mqtt_library_initialized = false; |
| 318 |
aws_thread_join_all_managed(); |
| 319 |
aws_unregister_error_info(&s_error_list); |
| 320 |
aws_unregister_log_subject_info_list(&s_logging_subjects_list); |
| 321 |
|
| 322 |
aws_http_library_clean_up(); |
| 323 |
aws_io_library_clean_up(); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void aws_mqtt_fatal_assert_library_initialized(void) { |
| 328 |
if (!s_mqtt_library_initialized) { |
| 329 |
AWS_LOGF_FATAL( |
| 330 |
AWS_LS_MQTT_GENERAL, |
| 331 |
"aws_mqtt_library_init() must be called before using any functionality in aws-c-mqtt."); |
| 332 |
|
| 333 |
AWS_FATAL_ASSERT(s_mqtt_library_initialized); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
static int aws_mqtt_utf8_decoder(uint32_t codepoint, void *user_data) { |
| 339 |
(void)user_data; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
if (AWS_UNLIKELY(codepoint <= 0x001F)) { |
| 344 |
return aws_raise_error(AWS_ERROR_MQTT5_INVALID_UTF8_STRING); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
if (AWS_UNLIKELY((codepoint >= 0x007F) && (codepoint <= 0x009F))) { |
| 349 |
return aws_raise_error(AWS_ERROR_MQTT5_INVALID_UTF8_STRING); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
if (AWS_UNLIKELY((codepoint & 0x00FFFF) >= 0x00FFFE)) { |
| 354 |
return aws_raise_error(AWS_ERROR_MQTT5_INVALID_UTF8_STRING); |
| 355 |
} |
| 356 |
if (AWS_UNLIKELY(codepoint >= 0xFDD0 && codepoint <= 0xFDEF)) { |
| 357 |
return aws_raise_error(AWS_ERROR_MQTT5_INVALID_UTF8_STRING); |
| 358 |
} |
| 359 |
|
| 360 |
return AWS_OP_SUCCESS; |
| 361 |
} |
| 362 |
|
| 363 |
static struct aws_utf8_decoder_options s_aws_mqtt_utf8_decoder_options = { |
| 364 |
.on_codepoint = aws_mqtt_utf8_decoder, |
| 365 |
}; |
| 366 |
|
| 367 |
int aws_mqtt_validate_utf8_text(struct aws_byte_cursor text) { |
| 368 |
return aws_decode_utf8(text, &s_aws_mqtt_utf8_decoder_options); |
| 369 |
} |