intégration des logiciels tier comme NanoMQ ainsi que les fichiers json de score

This commit is contained in:
2024-11-18 22:44:28 +01:00
parent 8f2ed89e05
commit a04c4c1f59
138 changed files with 27816 additions and 0 deletions

View File

@ -0,0 +1,112 @@
//
// Copyright 2022 NanoMQ Team, Inc. <jaylin@emqx.io>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#ifndef MQTT_PROTOCOL_H
#define MQTT_PROTOCOL_H
#define MQTT_PROTOCOL_NAME_v31 "MQIsdp"
#define MQTT_PROTOCOL_VERSION_v31 3
#define MQTT_PROTOCOL_NAME "MQTT"
#define MQTT_PROTOCOL_VERSION_v311 4
#define MQTT_PROTOCOL_VERSION_v5 5
/* NNG OPTs */
#define NANO_CONF "nano:conf"
/* Length defination */
// Maximum Packet Size of broker
#define NANO_MAX_RECV_PACKET_SIZE (2*1024*1024)
#define NANO_MIN_PACKET_LEN sizeof(uint8_t) * 8
#define NANO_CONNECT_PACKET_LEN sizeof(uint8_t) * 12
#define NANO_MIN_FIXED_HEADER_LEN sizeof(uint8_t) * 2
//flow control:how many QoS packet broker willing to process at same time.
#define NANO_MAX_QOS_PACKET 1024
#ifdef NANO_PACKET_SIZE
#define NNI_NANO_MAX_PACKET_SIZE sizeof(uint8_t) * NANO_PACKET_SIZE
#else
#define NNI_NANO_MAX_PACKET_SIZE sizeof(uint8_t) * 16
#endif
/* Error values */
enum err_t {
ERR_AUTH_CONTINUE = -4,
ERR_NO_SUBSCRIBERS = -3,
ERR_SUB_EXISTS = -2,
ERR_CONN_PENDING = -1,
ERR_SUCCESS = 0,
ERR_NOMEM = 1,
ERR_PROTOCOL = 2,
ERR_INVAL = 3,
ERR_NO_CONN = 4,
ERR_CONN_REFUSED = 5,
ERR_NOT_FOUND = 6,
ERR_CONN_LOST = 7,
ERR_TLS = 8,
ERR_PAYLOAD_SIZE = 9,
ERR_NOT_SUPPORTED = 10,
ERR_AUTH = 11,
ERR_ACL_DENIED = 12,
ERR_UNKNOWN = 13,
ERR_ERRNO = 14,
ERR_EAI = 15,
ERR_PROXY = 16,
ERR_PLUGIN_DEFER = 17,
ERR_MALFORMED_UTF8 = 18,
ERR_KEEPALIVE = 19,
ERR_LOOKUP = 20,
ERR_MALFORMED_PACKET = 21,
ERR_DUPLICATE_PROPERTY = 22,
ERR_TLS_HANDSHAKE = 23,
ERR_QOS_NOT_SUPPORTED = 24,
ERR_OVERSIZE_PACKET = 25,
ERR_OCSP = 26,
};
// mqtt5 macro
#define NMQ_RECEIVE_MAXIMUM_EXCEEDED 0X93
#define NMQ_PACKET_TOO_LARGE 0x95
#define NMQ_UNSEPECIFY_ERROR 0X80
#define NMQ_SERVER_UNAVAILABLE 0x88
#define NMQ_SERVER_BUSY 0x89
#define NMQ_SERVER_SHUTTING_DOWN 0x8B
#define NMQ_KEEP_ALIVE_TIMEOUT 0x8D
#define NMQ_AUTH_SUB_ERROR 0X87
// MQTT Control Packet types
typedef enum {
RESERVED = 0,
CONNECT = 1,
CONNACK = 2,
PUBLISH = 3,
PUBACK = 4,
PUBREC = 5,
PUBREL = 6,
PUBCOMP = 7,
SUBSCRIBE = 8,
SUBACK = 9,
UNSUBSCRIBE = 10,
UNSUBACK = 11,
PINGREQ = 12,
PINGRESP = 13,
DISCONNECT = 14,
AUTH = 15
} mqtt_control_packet_types;
//MQTTV5
typedef enum {
// 0 : V4 1: V5 5: V4 to V5 4: V5 to V4
MQTTV4 = 0,
MQTTV5 = 1,
MQTTV4_V5 = 2,
MQTTV5_V4 = 3,
} target_prover;
#endif

View File

@ -0,0 +1,152 @@
#ifndef NNG_MQTT_H
#define NNG_MQTT_H
#include "nng/supplemental/nanolib/conf.h"
#include "nng/supplemental/nanolib/hash_table.h"
#include "nng/mqtt/packet.h"
#include "mqtt.h"
#include "nng/supplemental/util/platform.h"
#include "nng/nng.h"
#include <stdlib.h>
#ifdef _WIN32
#define PRIu64 "I64u"
#define PRIu64_FORMAT "%I64u"
#else
#include <inttypes.h>
#define PRIu64_FORMAT "%" PRIu64
#endif
// Do not change to %lu! just suppress the warning of the compiler!
#define DISCONNECT_MSG \
"{\"username\":\"%s\"," \
"\"ts\":" PRIu64_FORMAT ",\"reason_code\":\"%x\",\"client_id\":\"%s\",\"IPv4\":\"%s\"}"
#define CONNECT_MSG \
"{\"username\":\"%s\", " \
"\"ts\":" PRIu64_FORMAT ",\"proto_name\":\"%s\",\"keepalive\":%d,\"return_code\":" \
"\"%x\",\"proto_ver\":%d,\"client_id\":\"%s\",\"clean_start\":%d, \"IPv4\":\"%s\"}"
#define DISCONNECT_TOPIC "$SYS/brokers/disconnected"
#define CONNECT_TOPIC "$SYS/brokers/connected"
//Strip off and return the QoS bits
#define NANO_NNI_LMQ_GET_QOS_BITS(msg) ((size_t) (msg) &0x03)
// strip off and return the msg pointer
#define NANO_NNI_LMQ_GET_MSG_POINTER(msg) \
((nng_msg *) ((size_t) (msg) & (~0x03)))
// packed QoS bits to the least two significant bits of msg pointer
#define NANO_NNI_LMQ_PACKED_MSG_QOS(msg, qos) \
((nng_msg *) ((size_t) (msg) | ((qos) &0x03)))
// Variables & Structs
typedef struct pub_extra pub_extra;
// int hex_to_oct(char *str);
// uint32_t htoi(char *str);
NNG_DECL pub_extra *pub_extra_alloc(pub_extra *);
NNG_DECL void pub_extra_free(pub_extra *);
NNG_DECL uint8_t pub_extra_get_qos(pub_extra *);
NNG_DECL uint16_t pub_extra_get_packet_id(pub_extra *);
NNG_DECL void pub_extra_set_qos(pub_extra *, uint8_t);
NNG_DECL void *pub_extra_get_msg(pub_extra *);
NNG_DECL void pub_extra_set_msg(pub_extra *, void *);
NNG_DECL void pub_extra_set_packet_id(pub_extra *, uint16_t);
// MQTT CONNECT
NNG_DECL int32_t conn_handler(uint8_t *packet, conn_param *conn_param, size_t max);
NNG_DECL int conn_param_alloc(conn_param **cparam);
NNG_DECL void conn_param_free(conn_param *cparam);
NNG_DECL void conn_param_clone(conn_param *cparam);
NNG_DECL int ws_msg_adaptor(uint8_t *packet, nng_msg *dst);
// parser
NNG_DECL uint8_t put_var_integer(uint8_t *dest, uint32_t value);
NNG_DECL uint32_t get_var_integer(const uint8_t *buf, uint8_t *pos);
NNG_DECL int32_t get_utf8_str(
char **dest, const uint8_t *src, uint32_t *pos, size_t max);
NNG_DECL uint8_t *copy_utf8_str(
const uint8_t *src, uint32_t *pos, int *str_len);
NNG_DECL uint8_t *copyn_utf8_str(
const uint8_t *src, uint32_t *pos, int *str_len, int limit);
// NNG_DECL char *convert_to_utf8(char *src, char *format, size_t *len);
NNG_DECL uint8_t *copyn_str(
const uint8_t *src, uint32_t *pos, int *str_len, int limit);
NNG_DECL int utf8_check(const char *str, size_t length);
NNG_DECL uint16_t get_variable_binary(uint8_t **dest, const uint8_t *src);
NNG_DECL uint32_t DJBHash(char *str);
NNG_DECL uint32_t DJBHashn(char *str, uint16_t len);
NNG_DECL uint64_t DJBHash64(char *str);
NNG_DECL uint64_t DJBHash64n(uint8_t* str, uint32_t len);
NNG_DECL uint32_t fnv1a_hashn(char *str, size_t n);
NNG_DECL uint8_t crc_hashn(char *str, size_t n);
NNG_DECL uint32_t crc32_hashn(char *str, size_t n);
NNG_DECL uint32_t crc32c_hashn(char *str, size_t n);
NNG_DECL uint8_t verify_connect(conn_param *cparam, conf *conf);
// repack
NNG_DECL void nano_msg_set_dup(nng_msg *msg);
NNG_DECL nng_msg *nano_pubmsg_composer(nng_msg **, uint8_t retain, uint8_t qos,
mqtt_string *payload, mqtt_string *topic, uint8_t proto_ver,
nng_time time);
NNG_DECL nng_msg *nano_dismsg_composer(
reason_code code, char *rstr, uint8_t *ref, property *prop);
NNG_DECL nng_msg *nano_msg_notify_disconnect(conn_param *cparam, uint8_t code);
NNG_DECL nng_msg *nano_msg_notify_connect(conn_param *cparam, uint8_t code);
NNG_DECL nano_pipe_db *nano_msg_get_subtopic(
nng_msg *msg, nano_pipe_db *root, conn_param *cparam);
NNG_DECL void nano_msg_free_pipedb(nano_pipe_db *db);
NNG_DECL void nano_msg_ubsub_free(nano_pipe_db *db);
NNG_DECL void nmq_connack_encode(
nng_msg *msg, conn_param *cparam, uint8_t reason);
NNG_DECL void nmq_connack_session(nng_msg *msg, bool session);
// TODO : check duplicated declaration
NNG_DECL reason_code check_properties(property *prop, nng_msg *msg);
NNG_DECL property *decode_buf_properties(uint8_t *packet, uint32_t packet_len,
uint32_t *pos, uint32_t *len, bool copy_value);
NNG_DECL property *decode_properties(
nng_msg *msg, uint32_t *pos, uint32_t *len, bool copy_value);
NNG_DECL int encode_properties(nng_msg *msg, property *prop, uint8_t cmd);
NNG_DECL int property_free(property *prop);
NNG_DECL property_data *property_get_value(property *prop, uint8_t prop_id);
NNG_DECL void property_foreach(property *prop, void (*cb)(property *));
NNG_DECL int property_dup(property **dup, const property *src);
NNG_DECL property *property_pub_by_will(property *will_prop);
NNG_DECL property *property_alloc(void);
NNG_DECL property_type_enum property_get_value_type(uint8_t prop_id);
NNG_DECL property *property_set_value_u8(uint8_t prop_id, uint8_t value);
NNG_DECL property *property_set_value_u16(uint8_t prop_id, uint16_t value);
NNG_DECL property *property_set_value_u32(uint8_t prop_id, uint32_t value);
NNG_DECL property *property_set_value_varint(uint8_t prop_id, uint32_t value);
NNG_DECL property *property_set_value_binary(
uint8_t prop_id, uint8_t *value, uint32_t len, bool copy_value);
NNG_DECL property *property_set_value_str(
uint8_t prop_id, const char *value, uint32_t len, bool copy_value);
NNG_DECL property *property_set_value_strpair(uint8_t prop_id, const char *key,
uint32_t key_len, const char *value, uint32_t value_len, bool copy_value);
NNG_DECL void property_append(property *prop_list, property *last);
NNG_DECL int nmq_subtopic_decode(nng_msg *msg, uint8_t ver, topic_queue **ptq);
NNG_DECL int nmq_subinfo_decode(nng_msg *msg, void *l, uint8_t ver);
NNG_DECL int nmq_unsubinfo_decode(nng_msg *msg, void *l, uint8_t ver);
NNG_DECL bool topic_filter(const char *origin, const char *input);
NNG_DECL bool topic_filtern(const char *origin, const char *input, size_t n);
NNG_DECL int nmq_auth_http_connect(conn_param *cparam, conf_auth_http *conf);
NNG_DECL int nmq_auth_http_sub_pub(
conn_param *cparam, bool is_sub, topic_queue *topics, conf_auth_http *conf);
#endif // NNG_MQTT_H

View File

@ -0,0 +1,35 @@
//
// Copyright 2020 NanoMQ Team, Inc. <jaylin@emqx.io>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#ifndef NNG_PROTOCOL_MQTT_BROKER_H
#define NNG_PROTOCOL_MQTT_BROKER_H
#ifdef __cplusplus
extern "C" {
#endif
NNG_DECL int nng_nmq_tcp0_open(nng_socket *);
#ifndef nng_nmq_tcp_open
#define nng_nmq_tcp_open nng_nmq_tcp0_open
#endif
#define NNG_NMQ_TCP_SELF 0x31
#define NNG_NMQ_TCP_PEER 0x30
#define NNG_NMQ_TCP_SELF_NAME "nmq_broker"
#define NNG_NMQ_TCP_PEER_NAME "nmq_client"
#define NMQ_OPT_MQTT_PIPES "mqtt-clients-pipes"
#define NMQ_OPT_MQTT_QOS_DB "mqtt-clients-qos-db"
#ifdef __cplusplus
}
#endif
#endif // NNG_PROTOCOL_MQTT_BROKER_H