/* For PUBACK, PUBCOMP, PUBREC, and PUBREL */ int send__command_with_mid(struct mosquitto *mosq, uint8_t command, uint16_t mid, bool dup) { struct mosquitto__packet *packet = NULL; int rc; assert(mosq); packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; packet->command = command; if(dup){ packet->command |= 8; } packet->remaining_length = 2; rc = packet__alloc(packet); if(rc){ mosquitto__free(packet); return rc; } packet->payload[packet->pos+0] = MOSQ_MSB(mid); packet->payload[packet->pos+1] = MOSQ_LSB(mid); return packet__queue(mosq, packet); }
int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup) { struct mosquitto__packet *packet = NULL; int packetlen; int rc; assert(mosq); assert(topic); packetlen = 2+strlen(topic) + payloadlen; if(qos > 0) packetlen += 2; /* For message id */ packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; packet->mid = mid; packet->command = PUBLISH | ((dup&0x1)<<3) | (qos<<1) | retain; packet->remaining_length = packetlen; rc = packet__alloc(packet); if(rc){ mosquitto__free(packet); return rc; } /* Variable header (topic string) */ packet__write_string(packet, topic, strlen(topic)); if(qos > 0){ packet__write_uint16(packet, mid); } /* Payload */ if(payloadlen){ packet__write_bytes(packet, payload, payloadlen); } return packet__queue(mosq, packet); }
/* For DISCONNECT, PINGREQ and PINGRESP */ int send__simple_command(struct mosquitto *mosq, uint8_t command) { struct mosquitto__packet *packet = NULL; int rc; assert(mosq); packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; packet->command = command; packet->remaining_length = 0; rc = packet__alloc(packet); if(rc){ mosquitto__free(packet); return rc; } return packet__queue(mosq, packet); }
int send__suback(struct mosquitto *context, uint16_t mid, uint32_t payloadlen, const void *payload) { struct mosquitto__packet *packet = NULL; int rc; log__printf(NULL, MOSQ_LOG_DEBUG, "Sending SUBACK to %s", context->id); packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; packet->command = SUBACK; packet->remaining_length = 2+payloadlen; rc = packet__alloc(packet); if(rc){ mosquitto__free(packet); return rc; } packet__write_uint16(packet, mid); if(payloadlen){ packet__write_bytes(packet, payload, payloadlen); } return packet__queue(context, packet); }
struct libwebsocket_context *mosq_websockets_init(struct mosquitto__listener *listener, int log_level) { struct lws_context_creation_info info; struct libwebsocket_protocols *p; int protocol_count; int i; struct libws_mqtt_hack *user; /* Count valid protocols */ for(protocol_count=0; protocols[protocol_count].name; protocol_count++); p = mosquitto__calloc(protocol_count+1, sizeof(struct libwebsocket_protocols)); if(!p){ log__printf(NULL, MOSQ_LOG_ERR, "Out of memory."); return NULL; } for(i=0; protocols[i].name; i++){ p[i].name = protocols[i].name; p[i].callback = protocols[i].callback; p[i].per_session_data_size = protocols[i].per_session_data_size; p[i].rx_buffer_size = protocols[i].rx_buffer_size; } memset(&info, 0, sizeof(info)); info.iface = listener->host; info.port = listener->port; info.protocols = p; info.gid = -1; info.uid = -1; #ifdef WITH_TLS info.ssl_ca_filepath = listener->cafile; info.ssl_cert_filepath = listener->certfile; info.ssl_private_key_filepath = listener->keyfile; info.ssl_cipher_list = listener->ciphers; if(listener->require_certificate){ info.options |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT; } #endif #if LWS_LIBRARY_VERSION_MAJOR>1 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; #endif if(listener->socket_domain == AF_INET){ info.options |= LWS_SERVER_OPTION_DISABLE_IPV6; } user = mosquitto__calloc(1, sizeof(struct libws_mqtt_hack)); if(!user){ mosquitto__free(p); log__printf(NULL, MOSQ_LOG_ERR, "Out of memory."); return NULL; } if(listener->http_dir){ #ifdef WIN32 user->http_dir = _fullpath(NULL, listener->http_dir, 0); #else user->http_dir = realpath(listener->http_dir, NULL); #endif if(!user->http_dir){ mosquitto__free(user); mosquitto__free(p); log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to open http dir \"%s\".", listener->http_dir); return NULL; } } info.user = user; listener->ws_protocol = p; lws_set_log_level(log_level, log_wrap); log__printf(NULL, MOSQ_LOG_INFO, "Opening websockets listen socket on port %d.", listener->port); return libwebsocket_create_context(&info); }
int send__connack(struct mosquitto_db *db, struct mosquitto *context, int ack, int reason_code, const mosquitto_property *properties) { struct mosquitto__packet *packet = NULL; int rc; mosquitto_property *connack_props = NULL; int proplen, varbytes; uint32_t remaining_length; rc = mosquitto_property_copy_all(&connack_props, properties); if(rc){ return rc; } if(context->id){ log__printf(NULL, MOSQ_LOG_DEBUG, "Sending CONNACK to %s (%d, %d)", context->id, ack, reason_code); }else{ log__printf(NULL, MOSQ_LOG_DEBUG, "Sending CONNACK to %s (%d, %d)", context->address, ack, reason_code); } remaining_length = 2; if(context->protocol == mosq_p_mqtt5){ if(reason_code < 128 && db->config->retain_available == false){ rc = mosquitto_property_add_byte(&connack_props, MQTT_PROP_RETAIN_AVAILABLE, 0); if(rc){ mosquitto_property_free_all(&connack_props); return rc; } } if(db->config->max_packet_size > 0){ rc = mosquitto_property_add_int32(&connack_props, MQTT_PROP_MAXIMUM_PACKET_SIZE, db->config->max_packet_size); if(rc){ mosquitto_property_free_all(&connack_props); return rc; } } proplen = property__get_length_all(connack_props); varbytes = packet__varint_bytes(proplen); remaining_length += proplen + varbytes; } if(packet__check_oversize(context, remaining_length)){ mosquitto_property_free_all(&connack_props); mosquitto__free(packet); return MOSQ_ERR_OVERSIZE_PACKET; } packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; packet->command = CMD_CONNACK; packet->remaining_length = remaining_length; rc = packet__alloc(packet); if(rc){ mosquitto_property_free_all(&connack_props); mosquitto__free(packet); return rc; } packet__write_byte(packet, ack); packet__write_byte(packet, reason_code); if(context->protocol == mosq_p_mqtt5){ property__write_all(packet, connack_props, true); } mosquitto_property_free_all(&connack_props); return packet__queue(context, packet); }
int send__unsubscribe(struct mosquitto *mosq, int *mid, int topic_count, char *const *const topic, const mosquitto_property *properties) { /* FIXME - only deals with a single topic */ struct mosquitto__packet *packet = NULL; uint32_t packetlen; uint16_t local_mid; int rc; int proplen, varbytes; int i; assert(mosq); assert(topic); packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; packetlen = 2; for(i=0; i<topic_count; i++){ packetlen += 2+strlen(topic[i]); } if(mosq->protocol == mosq_p_mqtt5){ proplen = property__get_length_all(properties); varbytes = packet__varint_bytes(proplen); packetlen += proplen + varbytes; } packet->command = CMD_UNSUBSCRIBE | (1<<1); packet->remaining_length = packetlen; rc = packet__alloc(packet); if(rc){ mosquitto__free(packet); return rc; } /* Variable header */ local_mid = mosquitto__mid_generate(mosq); if(mid) *mid = (int)local_mid; packet__write_uint16(packet, local_mid); if(mosq->protocol == mosq_p_mqtt5){ /* We don't use User Property yet. */ property__write_all(packet, properties, true); } /* Payload */ for(i=0; i<topic_count; i++){ packet__write_string(packet, topic[i], strlen(topic[i])); } #ifdef WITH_BROKER # ifdef WITH_BRIDGE for(i=0; i<topic_count; i++){ log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending UNSUBSCRIBE (Mid: %d, Topic: %s)", mosq->id, local_mid, topic[i]); } # endif #else for(i=0; i<topic_count; i++){ log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending UNSUBSCRIBE (Mid: %d, Topic: %s)", mosq->id, local_mid, topic[i]); } #endif return packet__queue(mosq, packet); }