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 @@
aux_source_directory(. DIR_LIB_SRCS)

View File

@ -0,0 +1,12 @@
#ifndef NANOMQ_ACL_HANDLER_H
#define NANOMQ_ACL_HANDLER_H
#include "nng/nng.h"
#include "nng/supplemental/nanolib/conf.h"
#include "nng/supplemental/nanolib/acl_conf.h"
#ifdef ACL_SUPP
extern bool auth_acl(
conf *config, acl_action_type type, conn_param *param, const char *topic);
#endif
#endif

View File

@ -0,0 +1,10 @@
#ifndef AWS_BRIDGE_H
#define AWS_BRIDGE_H
#include "nng/supplemental/nanolib/conf.h"
#include "broker.h"
extern int aws_bridge_client(conf_bridge_node *node);
extern void aws_bridge_forward(nano_work *work);
#endif

View File

@ -0,0 +1,42 @@
#ifndef NANOMQ_BRIDGE_H
#define NANOMQ_BRIDGE_H
#include "nng/mqtt/mqtt_client.h"
#include "nng/nng.h"
#include "nng/supplemental/nanolib/conf.h"
#include "nng/supplemental/util/platform.h"
#include <stdio.h>
#include <stdlib.h>
#include "broker.h"
#include "pub_handler.h"
typedef struct {
nng_socket *sock;
conf_bridge_node *config; // bridge conf file
nng_mqtt_client *client;
nng_msg *connmsg;
conf *conf; //parent conf file
nng_mtx *switch_mtx;
nng_cv *switch_cv;
nng_mtx *exec_mtx;
nng_cv *exec_cv;
nng_duration cancel_timeout;
} bridge_param;
extern bool topic_filter(const char *origin, const char *input);
extern int bridge_client(
nng_socket *sock, conf *config, conf_bridge_node *bridge_conf);
extern int hybrid_bridge_client(
nng_socket *sock, conf *config, conf_bridge_node *node);
extern void bridge_handle_topic_reflection(nano_work *work, conf_bridge *bridge);
extern nng_msg *bridge_publish_msg(const char *topic, uint8_t *payload,
uint32_t len, bool dup, uint8_t qos, bool retain, property *props);
extern int bridge_reload(nng_socket *sock, conf *config, conf_bridge_node *node);
extern int bridge_subscribe(nng_socket *sock, conf_bridge_node *node,
nng_mqtt_topic_qos *topic_qos, size_t sub_count, property *properties);
extern int bridge_unsubscribe(nng_socket *sock, conf_bridge_node *node,
nng_mqtt_topic *topic, size_t unsub_count, property *properties);
#endif // NANOMQ_BRIDGE_H

View File

@ -0,0 +1,106 @@
#ifndef NANOMQ_BROKER_H
#define NANOMQ_BROKER_H
#define HTTP_CTX_NUM 4
#include "nng/supplemental/nanolib/conf.h"
#include "nng/supplemental/nanolib/nanolib.h"
#include "nng/nng.h"
#include "nng/protocol/mqtt/mqtt.h"
#include "nng/supplemental/util/platform.h"
#include "nng/mqtt/packet.h"
#include "hashmap.h"
#define PROTO_MQTT_BROKER 0x00
#define PROTO_MQTT_BRIDGE 0x01
#define PROTO_AWS_BRIDGE 0x02
#define PROTO_HTTP_SERVER 0x03
#define PROTO_ICEORYX_BRIDGE 0x04
#define STATISTICS
#if defined(ENABLE_NANOMQ_TESTS)
#undef STATISTICS
#endif
typedef struct work nano_work;
struct work {
enum {
INIT,
RECV,
WAIT,
SEND, // Actions after sending msg
HOOK, // Rule Engine
END, // Clear state and cache before disconnect
CLOSE // sending disconnect packet and err code
} state;
uint8_t proto; // logic proto
uint8_t proto_ver; // MQTT version cache
uint8_t flag; // flag for webhook & rule_engine
nng_aio * aio;
nng_msg * msg;
nng_msg ** msg_ret;
nng_ctx ctx; // ctx for mqtt broker
nng_ctx extra_ctx; // ctx for bridging/http post
nng_pipe pid;
dbtree * db;
dbtree * db_ret;
conf * config;
conf_bridge_node *node; // only works for bridge ctx
reason_code code; // MQTT reason code
nng_socket hook_sock;
struct pipe_content * pipe_ct;
conn_param * cparam;
struct pub_packet_struct *pub_packet;
packet_subscribe * sub_pkt;
packet_unsubscribe * unsub_pkt;
void *sqlite_db;
#if defined(SUPP_PLUGIN)
property *user_property;
#endif
#if defined(SUPP_ICEORYX)
void *iceoryx_suber;
void *iceoryx_puber;
nng_socket iceoryx_sock;
#endif
};
struct client_ctx {
nng_pipe pid;
#ifdef STATISTICS
nng_atomic_u64 *recv_cnt;
#endif
conn_param *cparam;
uint32_t prop_len;
property *properties;
uint8_t proto_ver;
};
static int broker_start_rc;
typedef struct client_ctx client_ctx;
extern int broker_start(int argc, char **argv);
extern int broker_stop(int argc, char **argv);
extern int broker_restart(int argc, char **argv);
extern int broker_reload(int argc, char **argv);
extern int broker_dflt(int argc, char **argv);
extern void bridge_send_cb(void *arg);
extern void *broker_start_with_conf(void *nmq_conf);
#ifdef STATISTICS
extern uint64_t nanomq_get_message_in(void);
extern uint64_t nanomq_get_message_out(void);
extern uint64_t nanomq_get_message_drop(void);
#endif
extern dbtree * get_broker_db(void);
extern struct hashmap_s *get_hashmap(void);
extern int rule_engine_insert_sql(nano_work *work);
#endif

View File

@ -0,0 +1,20 @@
#ifndef NANOMQ_CMD_PROC_H
#define NANOMQ_CMD_PROC_H
#define CMD_IPC_URL "ipc:///tmp/nanomq_cmd.ipc"
#define IPC_URL_PATH "/tmp/nanomq_cmd.ipc"
// #define CMD_IPC_URL "tcp://127.0.0.1:10000"
#define CMD_PROC_PARALLEL 1
#include "nng/nng.h"
#include "nng/supplemental/nanolib/conf.h"
#include "nng/supplemental/nanolib/log.h"
typedef struct cmd_work cmd_work;
extern void cmd_server_cb(void *arg);
extern cmd_work *alloc_cmd_work(nng_socket sock, conf *config);
extern void start_cmd_client(const char *cmd, const char *url);
extern char * encode_client_cmd(const char *conf_file, int type);
#endif // NANOMQ_CMD_PROC_H

View File

@ -0,0 +1,43 @@
#ifndef NANOMQ_CONF_API_H
#define NANOMQ_CONF_API_H
#include "nng/supplemental/nanolib/conf.h"
#include "nng/supplemental/nanolib/cJSON.h"
#include "nng/supplemental/nanolib/file.h"
#include "nng/supplemental/nanolib/utils.h"
#include "nng/supplemental/nanolib/cvector.h"
#include "rest_api.h"
extern cJSON *get_reload_config(conf *config);
extern cJSON *get_basic_config(conf *config);
extern cJSON *get_tls_config(conf_tls *tls, bool is_server);
extern cJSON *get_auth_config(conf_auth *auth);
extern cJSON *get_auth_http_config(conf_auth_http *auth_http);
extern cJSON *get_websocket_config(conf_websocket *ws);
extern cJSON *get_http_config(conf_http_server *http);
extern cJSON *get_sqlite_config(conf_sqlite *sqlite);
extern cJSON *get_bridge_config(conf_bridge *bridge, const char *node_name);
extern void set_bridge_node_conf(
conf_bridge *config, cJSON *node_obj, const char *name);
extern void set_reload_config(cJSON *json, conf *config);
extern void set_basic_config(cJSON *json, conf *config);
extern void set_tls_config(
cJSON *json, const char *conf_path, conf_tls *tls, const char *key_prefix);
extern void set_auth_config(
cJSON *json, const char *conf_path, conf_auth *auth);
extern void set_auth_http_config(
cJSON *json, const char *conf_path, conf_auth_http *auth);
extern void set_http_config(
cJSON *json, const char *conf_path, conf_http_server *http);
extern void set_websocket_config(
cJSON *json, const char *conf_path, conf_websocket *ws);
extern void set_sqlite_config(cJSON *json, const char *conf_path,
conf_sqlite *sqlite, const char *key_prefix);
extern void reload_basic_config(conf *cur_conf, conf *new_conf);
extern void reload_sqlite_config(conf_sqlite *cur_conf, conf_sqlite *new_conf);
extern void reload_auth_config(conf_auth *cur_conf, conf_auth *new_conf);
extern void reload_log_config(conf *cur_conf, conf *new_conf);
#endif

View File

@ -0,0 +1,2 @@
extern const char tmp_example[];

View File

@ -0,0 +1,79 @@
/*
* AWS IoT Device SDK for Embedded C 202108.00
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef CORE_MQTT_CONFIG_H_
#define CORE_MQTT_CONFIG_H_
/**************************************************/
/******* DO NOT CHANGE the following order ********/
/**************************************************/
/* Include logging header files and define logging macros in the following order:
* 1. Include the header file "logging_levels.h".
* 2. Define the LIBRARY_LOG_NAME and LIBRARY_LOG_LEVEL macros depending on
* the logging configuration for MQTT.
* 3. Include the header file "logging_stack.h", if logging is enabled for MQTT.
*/
#include <aws/logging_levels.h>
/* Logging configuration for the MQTT library. */
#ifndef LIBRARY_LOG_NAME
#define LIBRARY_LOG_NAME "MQTT"
#endif
#ifndef LIBRARY_LOG_LEVEL
#define LIBRARY_LOG_LEVEL LOG_NONE
#endif
#include <aws/logging_stack.h>
/************ End of logging configuration ****************/
/**
* @brief Determines the maximum number of MQTT PUBLISH messages, pending
* acknowledgement at a time, that are supported for incoming and outgoing
* direction of messages, separately.
*
* QoS 1 and 2 MQTT PUBLISHes require acknowledgement from the server before
* they can be completed. While they are awaiting the acknowledgement, the
* client must maintain information about their state. The value of this
* macro sets the limit on how many simultaneous PUBLISH states an MQTT
* context maintains, separately, for both incoming and outgoing direction of
* PUBLISHes.
*
* @note The MQTT context maintains separate state records for outgoing
* and incoming PUBLISHes, and thus, 2 * MQTT_STATE_ARRAY_MAX_COUNT amount
* of memory is statically allocated for the state records.
*/
#define MQTT_STATE_ARRAY_MAX_COUNT ( 10U )
/**
* @brief Number of milliseconds to wait for a ping response to a ping
* request as part of the keep-alive mechanism.
*
* If a ping response is not received before this timeout, then
* #MQTT_ProcessLoop will return #MQTTKeepAliveTimeout.
*/
#define MQTT_PINGRESP_TIMEOUT_MS ( 5000U )
#endif /* ifndef CORE_MQTT_CONFIG_H_ */

View File

@ -0,0 +1,12 @@
#ifndef DB_CLI_H
#define DB_CLI_H
#include "fdb_version.h"
#include <pthread.h>
#include <stdlib.h>
#include <foundationdb/fdb_c.h>
#include <foundationdb/fdb_c_options.g.h>
FDBDatabase* openDatabase(pthread_t* netThread);
#endif

View File

@ -0,0 +1,113 @@
#ifndef SHEREDOM_HASHMAP_H_INCLUDED
#define SHEREDOM_HASHMAP_H_INCLUDED
#if defined(_MSC_VER)
// Workaround a bug in the MSVC runtime where it uses __cplusplus when not
// defined.
#pragma warning(push, 0)
#pragma warning(disable : 4668)
#endif
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "nng/nng.h"
#include "nng/supplemental/util/platform.h"
#if (defined(_MSC_VER) && defined(__AVX__)) || \
(!defined(_MSC_VER) && defined(__SSE4_2__))
#define HASHMAP_SSE42
#endif
#if defined(HASHMAP_SSE42)
#include <nmmintrin.h>
#endif
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(_MSC_VER)
#pragma warning(push)
/* Stop MSVC complaining about unreferenced functions */
#pragma warning(disable : 4505)
/* Stop MSVC complaining about not inlining functions */
#pragma warning(disable : 4710)
/* Stop MSVC complaining about inlining functions! */
#pragma warning(disable : 4711)
#elif defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
#endif
#if defined(_MSC_VER)
#define HASHMAP_USED
#elif defined(__GNUC__)
#define HASHMAP_USED __attribute__((used))
#else
#define HASHMAP_USED
#endif
typedef struct hashmap_s hashmap_s;
/* We need to keep keys and values. */
struct hashmap_element_s {
const char *key;
unsigned key_len;
int in_use;
uint32_t data;
};
/* A hashmap has some maximum size and current size, as well as the data to
* hold. */
struct hashmap_s {
unsigned table_size;
unsigned size;
struct hashmap_element_s *data;
};
#define HASHMAP_MAX_CHAIN_LENGTH (8)
#if defined(__cplusplus)
extern "C" {
#endif
int nano_hashmap_create(const unsigned initial_size,
struct hashmap_s * out_hashmap) HASHMAP_USED;
int nano_hashmap_put(struct hashmap_s * hashmap, const char *const key,
const unsigned len, uint32_t value) HASHMAP_USED;
uint32_t nano_hashmap_get(const struct hashmap_s *const hashmap,
const char *const key,
const unsigned len) HASHMAP_USED;
int nano_hashmap_remove(struct hashmap_s * m, const char *const key,
const unsigned len);
void nano_hashmap_destroy(struct hashmap_s * m);
#if defined(__cplusplus)
}
#endif
#if defined(__cplusplus)
#define HASHMAP_CAST(type, x) static_cast<type>(x)
#define HASHMAP_PTR_CAST(type, x) reinterpret_cast<type>(x)
#define HASHMAP_NULL NULL
#else
#define HASHMAP_CAST(type, x) ((type)x)
#define HASHMAP_PTR_CAST(type, x) ((type)x)
#define HASHMAP_NULL 0
#endif
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif

View File

@ -0,0 +1,38 @@
#ifndef MQTT_API_H
#define MQTT_API_H
#include "nng/mqtt/mqtt_client.h"
#include "nng/protocol/mqtt/mqtt.h"
#include "nng/supplemental/nanolib/conf.h"
#include "nng/supplemental/tls/tls.h"
#include "nng/supplemental/util/options.h"
#include "nng/supplemental/util/platform.h"
#define INPROC_SERVER_URL "inproc://inproc_server"
int nano_listen(
nng_socket sid, const char *addr, nng_listener *lp, int flags, conf *conf);
int init_listener_tls(nng_listener l, conf_tls *tls);
extern int decode_common_mqtt_msg(nng_msg **dest, nng_msg *src);
extern int encode_common_mqtt_msg(
nng_msg **dest, nng_msg *src, const char *clientid, uint8_t proto_ver);
extern int log_init(conf_log *log);
extern int log_fini(conf_log *log);
extern char *nano_pipe_get_local_address(nng_pipe p);
extern uint8_t *nano_pipe_get_local_address6(nng_pipe p);
extern uint16_t nano_pipe_get_local_port(nng_pipe p);
extern uint16_t nano_pipe_get_local_port6(nng_pipe p);
#if defined(SUPP_ICEORYX)
#include "nng/iceoryx_shm/iceoryx_shm.h"
extern int nano_iceoryx_send_nng_msg(
nng_iceoryx_puber *puber, nng_msg *msg, nng_socket *sock);
extern int nano_iceoryx_recv_nng_msg(
nng_iceoryx_suber *suber, nng_msg *icemsg, nng_msg **msg);
extern bool nano_iceoryx_topic_filter(char *icetopic, char *topic, uint32_t topicsz);
#endif
#endif // MQTT_API_H

View File

@ -0,0 +1,67 @@
#ifndef NANOMQ_NANOMQ_H
#define NANOMQ_NANOMQ_H
#include "nng/supplemental/nanolib/log.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#define NNI_PUT16(ptr, u) \
do { \
(ptr)[0] = (uint8_t)(((uint16_t)(u)) >> 8u); \
(ptr)[1] = (uint8_t)((uint16_t)(u)); \
} while (0)
#define NNI_PUT32(ptr, u) \
do { \
(ptr)[0] = (uint8_t)(((uint32_t)(u)) >> 24u); \
(ptr)[1] = (uint8_t)(((uint32_t)(u)) >> 16u); \
(ptr)[2] = (uint8_t)(((uint32_t)(u)) >> 8u); \
(ptr)[3] = (uint8_t)((uint32_t)(u)); \
} while (0)
#define NNI_PUT64(ptr, u) \
do { \
(ptr)[0] = (uint8_t)(((uint64_t)(u)) >> 56u); \
(ptr)[1] = (uint8_t)(((uint64_t)(u)) >> 48u); \
(ptr)[2] = (uint8_t)(((uint64_t)(u)) >> 40u); \
(ptr)[3] = (uint8_t)(((uint64_t)(u)) >> 32u); \
(ptr)[4] = (uint8_t)(((uint64_t)(u)) >> 24u); \
(ptr)[5] = (uint8_t)(((uint64_t)(u)) >> 16u); \
(ptr)[6] = (uint8_t)(((uint64_t)(u)) >> 8u); \
(ptr)[7] = (uint8_t)((uint64_t)(u)); \
} while (0)
#define NNI_GET16(ptr, v) \
v = (((uint16_t)((uint8_t)(ptr)[0])) << 8u) + \
(((uint16_t)(uint8_t)(ptr)[1]))
#define NNI_GET32(ptr, v) \
v = (((uint32_t)((uint8_t)(ptr)[0])) << 24u) + \
(((uint32_t)((uint8_t)(ptr)[1])) << 16u) + \
(((uint32_t)((uint8_t)(ptr)[2])) << 8u) + \
(((uint32_t)(uint8_t)(ptr)[3]))
#define NNI_GET64(ptr, v) \
v = (((uint64_t)((uint8_t)(ptr)[0])) << 56u) + \
(((uint64_t)((uint8_t)(ptr)[1])) << 48u) + \
(((uint64_t)((uint8_t)(ptr)[2])) << 40u) + \
(((uint64_t)((uint8_t)(ptr)[3])) << 32u) + \
(((uint64_t)((uint8_t)(ptr)[4])) << 24u) + \
(((uint64_t)((uint8_t)(ptr)[5])) << 16u) + \
(((uint64_t)((uint8_t)(ptr)[6])) << 8u) + \
(((uint64_t)(uint8_t)(ptr)[7]))
#define NANO_UNUSED(x) (x) __attribute__((unused))
#define NANO_NNG_FATAL(s, rv) \
do { \
log_fatal(s); \
nng_fatal((s), (rv)); \
} while(0)
extern int get_cache_argc();
extern char **get_cache_argv();
#endif

View File

@ -0,0 +1,19 @@
#ifndef NANOMQ_RULE_H
#define NANOMQ_RULE_H
#include "nng/mqtt/mqtt_client.h"
#include "nng/supplemental/sqlite/sqlite3.h"
#include "nng/supplemental/nanolib/conf.h"
#include "nng/nng.h"
#include <stdio.h>
#include <stdlib.h>
#if defined(SUPP_RULE_ENGINE)
extern int nano_client(nng_socket *sock, repub_t *repub);
extern int nano_client_publish(nng_socket *sock, const char *topic,
uint8_t *payload, uint32_t len, uint8_t qos, property *props);
extern int nanomq_client_sqlite(conf_rule *cr, bool init_last);
extern int nanomq_client_mysql(conf_rule *cr, bool init_last);
#endif
#endif // NANOMQ_RULE_H

View File

@ -0,0 +1,36 @@
//
// Copyright 2024 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 NANOMQ_PLUGIN_H
#define NANOMQ_PLUGIN_H
#include "nng/nng.h"
#include "nng/supplemental/nanolib/conf.h"
#include "nng/supplemental/nanolib/log.h"
struct nano_plugin {
char *path;
int (*init)(void);
};
enum hook_point {
HOOK_USER_PROPERTY,
};
struct plugin_hook {
unsigned int point;
int (*cb)(void *data);
};
extern int plugin_hook_register(unsigned int point, int (*cb)(void *data));
extern int plugin_hook_call(unsigned int point, void *data);
extern int plugin_register(char *path);
extern void plugins_clear();
extern int plugin_init(struct nano_plugin *plugin);
#endif // NANOMQ_PLUGIN_H

View File

@ -0,0 +1,14 @@
#ifndef PROCESS_H
#define PROCESS_H
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
extern int process_is_alive(int pid);
extern int process_send_signal(int pid, int signal);
extern int pidgrp_send_signal(int pid, int signal);
extern int process_daemonize(void);
extern int process_create_child(int (*child_run)(void *), void *data);
#endif

View File

@ -0,0 +1,71 @@
/**
* Created by Alvin on 2020/7/25.
*/
#ifndef NANOMQ_PUB_HANDLER_H
#define NANOMQ_PUB_HANDLER_H
#include "broker.h"
#include <nng/mqtt/packet.h>
#include <nng/nng.h>
#include <nng/protocol/mqtt/mqtt.h>
typedef uint32_t variable_integer;
// MQTT Fixed header
struct fixed_header {
// flag_bits
uint8_t retain : 1;
uint8_t qos : 2;
uint8_t dup : 1;
// packet_types
uint8_t packet_type : 4;
// remaining length
uint32_t remain_len;
};
// MQTT Variable header
union variable_header {
struct {
uint16_t packet_id;
struct mqtt_string topic_name;
property *properties;
uint32_t prop_len;
} publish;
struct {
uint16_t packet_id;
reason_code reason_code;
property *properties;
uint32_t prop_len;
} pub_arrc, puback, pubrec, pubrel, pubcomp;
};
struct mqtt_payload {
uint8_t *data;
uint32_t len;
};
struct pub_packet_struct {
struct fixed_header fixed_header;
union variable_header var_header;
struct mqtt_payload payload;
};
struct pipe_content {
mqtt_msg_info *msg_infos;
};
bool encode_pub_message(
nng_msg *dest_msg, nano_work *work, mqtt_control_packet_types cmd);
reason_code decode_pub_message(nano_work *work, uint8_t proto);
void free_pub_packet(struct pub_packet_struct *pub_packet);
void free_msg_infos(mqtt_msg_info *msg_infos);
void init_pipe_content(struct pipe_content *pipe_ct);
void init_pub_packet_property(struct pub_packet_struct *pub_packet);
bool check_msg_exp(nng_msg *msg, property *prop);
reason_code handle_pub(nano_work *work, struct pipe_content *pipe_ct,
uint8_t proto, bool is_event);
#endif // NNG_PUB_HANDLER_H

View File

@ -0,0 +1,130 @@
//
// 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 REST_API_H
#define REST_API_H
#include "web_server.h"
#include "mqtt_api.h"
#include <ctype.h>
#include <nng/nng.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REST_URI_ROOT "/api/v4"
#define REST_HOST "http://0.0.0.0:%u"
#define REST_URL REST_HOST REST_URI_ROOT
#define getNumberValue(obj, item, key, value, rv) \
{ \
item = cJSON_GetObjectItem(obj, key); \
if (cJSON_IsNumber(item)) { \
value = cJSON_GetNumberValue(item); \
rv = (0); \
} else { \
rv = (-1); \
} \
}
#define getBoolValue(obj, item, key, value, rv) \
{ \
item = cJSON_GetObjectItem(obj, key); \
if (cJSON_IsBool(item)) { \
value = cJSON_IsTrue(item); \
rv = (0); \
} else { \
rv = (-1); \
} \
}
#define getStringValue(obj, item, key, value, rv) \
{ \
item = cJSON_GetObjectItem(obj, key); \
if (cJSON_IsString(item)) { \
value = cJSON_GetStringValue(item); \
rv = (0); \
} else { \
rv = (-1); \
} \
}
enum result_code {
SUCCEED = 0,
RPC_ERROR = 101,
UNKNOWN_MISTAKE = 102,
WRONG_USERNAME_OR_PASSWORD = 103,
EMPTY_USERNAME_OR_PASSWORD = 104,
USER_DOES_NOT_EXIST = 105,
ADMIN_CANNOT_BE_DELETED = 106,
MISSING_KEY_REQUEST_PARAMES = 107,
REQ_PARAM_ERROR = 108,
REQ_PARAMS_JSON_FORMAT_ILLEGAL = 109,
PLUGIN_IS_ENABLED = 110,
PLUGIN_IS_CLOSED = 111,
CLIENT_IS_OFFLINE = 112,
USER_ALREADY_EXISTS = 113,
OLD_PASSWORD_IS_WRONG = 114,
ILLEGAL_SUBJECT = 115,
TOKEN_EXPIRED = 116,
PARAMS_HOCON_FORMAT_ILLEGAL = 117,
WRITE_CONFIG_FAILED = 118,
};
typedef struct http_msg {
uint16_t status;
int request;
size_t content_type_len;
char * content_type;
size_t method_len;
char * method;
size_t uri_len;
char * uri;
size_t token_len;
char * token;
size_t data_len;
char * data;
bool encrypt_data;
} http_msg;
extern void put_http_msg(http_msg *msg, const char *content_type,
const char *method, const char *uri, const char *token, const char *data,
size_t data_sz);
extern void destory_http_msg(http_msg *msg);
extern http_msg process_request(
http_msg *msg, conf_http_server *config, nng_socket *sock);
#define GET_METHOD "GET"
#define POST_METHOD "POST"
#define PUT_METHOD "PUT"
#define DELETE_METHOD "DELETE"
#define REQ_ENDPOINTS 1
#define REQ_BROKERS 2
#define REQ_NODES 3
#define REQ_SUBSCRIPTIONS 4
#define REQ_CLIENTS 5
#define REQ_LOGIN 6
#define REQ_LOGOUT 7
#define REQ_CTRL 10
#define REQ_GET_CONFIG 11
#define REQ_SET_CONFIG 12
#define REQ_TREE 13
#define cJSON_AddStringOrNullToObject(obj, name, value) \
{ \
if (value) { \
cJSON_AddStringToObject(obj, name, value); \
} else { \
cJSON_AddNullToObject(obj, name); \
} \
}
#endif

View File

@ -0,0 +1,51 @@
#ifndef MQTT_SUBSCRIBE_HANDLE_H
#define MQTT_SUBSCRIBE_HANDLE_H
#include <nng/nng.h>
#include <nng/mqtt/packet.h>
#include "broker.h"
typedef struct {
uint32_t pid;
dbtree *db;
} sub_destroy_info;
/*
* Use to decode sub msg.
*/
int decode_sub_msg(nano_work *);
/*
* Use to encode an ack for sub msg
*/
int encode_suback_msg(nng_msg *, nano_work *);
int sub_ctx_handle(nano_work *);
/*
* Delete a client ctx from topic node in dbtree
*/
int sub_ctx_del(void *db, char *topic, uint32_t pid);
/*
* Free the client ctx
*/
void sub_ctx_free(client_ctx *);
/*
* A wrap for sub ctx free
*/
void * wrap_sub_ctx_free_cb(void *arg);
/*
* Free a packet_subscribe.
*/
void sub_pkt_free(packet_subscribe *);
/*
* Delete all refs in dbtree about client ctx
*/
void destroy_sub_client(uint32_t pid, dbtree * db);
#endif

View File

@ -0,0 +1,14 @@
#ifndef MQTT_UNSUBSCRIBE_HANDLE_H
#define MQTT_UNSUBSCRIBE_HANDLE_H
#include <nng/nng.h>
#include "broker.h"
#include <nng/mqtt/packet.h>
int decode_unsub_msg(nano_work *);
int encode_unsuback_msg(nng_msg *, nano_work *);
int unsub_ctx_handle(nano_work *);
void unsub_pkt_free(packet_unsubscribe *);
#endif // MQTT_UNSUBSCRIBE_HANDLE_H

View File

@ -0,0 +1,5 @@
#define NANO_VER_MAJOR 0
#define NANO_VER_MINOR 22
#define NANO_VER_PATCH 10
#define NANO_VER_ID_SHORT "15"

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 WEB_SERVER_H
#define WEB_SERVER_H
#include "nng/nng.h"
#include "nng/supplemental/util/platform.h"
#include "nng/supplemental/nanolib/conf.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HTTP_DEFAULT_USER "admin"
#define HTTP_DEFAULT_PASSWORD "public"
#define HTTP_DEFAULT_PORT 8081
extern int start_rest_server(conf *conf);
extern void stop_rest_server(void);
extern void set_http_server_conf(conf_http_server *conf);
extern conf_http_server *get_http_server_conf(void);
extern void set_global_conf(conf *config);
extern conf * get_global_conf(void);
extern char * get_jwt_key(void);
extern nng_time get_boot_time(void);
#endif

View File

@ -0,0 +1,13 @@
#ifndef WEBHOOK_INPROC_H
#define WEBHOOK_INPROC_H
#include "nng/supplemental/nanolib/conf.h"
#include "nng/nng.h"
#define HOOK_IPC_URL "ipc:///tmp/nanomq_hook.ipc"
#define EXTERNAL2NANO_IPC "EX2NANO"
extern int start_hook_service(conf *conf);
extern int stop_hook_service(void);
#endif

View File

@ -0,0 +1,20 @@
#ifndef WEBHOOK_POST_H
#define WEBHOOK_POST_H
#include "webhook_inproc.h"
#include "broker.h"
extern int webhook_msg_publish(nng_socket *sock, conf_web_hook *hook_conf,
pub_packet_struct *pub_packet, const char *username,
const char *client_id);
extern int webhook_client_connack(nng_socket *sock, conf_web_hook *hook_conf,
uint8_t proto_ver, uint16_t keepalive, uint8_t reason,
const char *username, const char *client_id);
extern int webhook_client_disconnect(nng_socket *sock,
conf_web_hook *hook_conf, uint8_t proto_ver, uint16_t keepalive,
uint8_t reason, const char *username, const char *client_id);
extern int hook_entry(nano_work *work, uint8_t reason);
extern int hook_exchange_init(conf *nanomq_conf, uint64_t num_ctx);
extern int hook_exchange_sender_init(conf *nanomq_conf, struct work **works, uint64_t num_ctx);
#endif