Compare commits

..
82 Commits
Author SHA1 Message Date
fengzeroz 283c0f6433 Merge pull request #2916 from fengzeroz/v2.13
ekuiper: filters no tags write
2026-07-22 14:16:40 +08:00
fengzero c639b8c508 ekuiper: filters no tags write 2026-07-22 06:15:29 +00:00
fengzeroz f1062761b7 Merge pull request #2914 from fengzeroz/v2.13
rm system api
2026-07-17 17:32:46 +08:00
fengzero b7f5aff32d rm system api 2026-07-17 09:31:19 +00:00
fengzeroz 1167254791 Merge pull request #2910 from fengzeroz/v2.13
bool: parse option
2026-07-17 17:27:01 +08:00
fengzero b4cea40ace bool: parse option 2026-07-17 09:26:12 +00:00
fengzeroz ae80b83084 Merge pull request #2896 from fengzeroz/v2.13
check array length
2026-07-02 17:28:15 +08:00
fengzero f7a295dc9d check array length 2026-07-02 09:11:00 +00:00
fengzeroz e804abbf11 Merge pull request #2893 from fengzeroz/v2.13
mqtt check item case
2026-07-02 17:01:04 +08:00
fengzero 21810c08bf mqtt check item case 2026-07-02 08:13:56 +00:00
fengzeroz 9b82a4e55e Merge pull request #2889 from fengzeroz/v2.13
cid: check depth
2026-07-02 16:11:22 +08:00
fengzero c4249a8967 cid: check depth 2026-07-02 07:55:09 +00:00
fengzeroz 3b071367a0 Merge pull request #2885 from fengzeroz/v2.13
fix array string check null
2026-07-02 15:50:44 +08:00
fengzero dd20275d6b fix array string check null 2026-07-02 07:03:56 +00:00
fengzeroz 145a8b4deb Merge pull request #2881 from fengzeroz/v2.13
limit path & fix memory check
2026-07-02 14:37:30 +08:00
fengzero ea2ad27f5e limit path & fix memory check 2026-07-02 06:24:00 +00:00
fengzeroz bfe3002239 Merge pull request #2875 from fengzeroz/v2.13
fix double free
2026-07-01 17:33:53 +08:00
fengzero 88ed1af382 fix double free 2026-07-01 09:06:07 +00:00
fengzeroz af81f34460 Merge pull request #2871 from fengzeroz/v2.13
fix null point
2026-07-01 16:48:10 +08:00
fengzero 95ed0c9b86 fix null point 2026-07-01 08:33:39 +00:00
fengzeroz 91684e9e39 Merge pull request #2866 from fengzeroz/v2.13
enable pie
2026-07-01 16:30:51 +08:00
fengzero 6bf6827b4d enable pie 2026-07-01 08:20:37 +00:00
fengzeroz b9e122655f Merge pull request #2857 from fengzeroz/v2.13
fix memory overflow
2026-07-01 15:57:14 +08:00
fengzeroz 14fabc34b7 Merge pull request #2861 from ZhouBox/v2.13
refactor json array decode
2026-07-01 15:42:21 +08:00
ZhouBox d4c79f0e9a refactor json array decode 2026-07-01 15:23:29 +08:00
fengzero 9edf2820ea fix memory overflow 2026-07-01 07:18:18 +00:00
fengzeroz 8b6ca3c2d5 Merge pull request #2846 from fengzeroz/v2.13
replace strcpy with snprintf
2026-07-01 13:51:27 +08:00
fengzero b3e0e4d623 replace strcpy with snprintf 2026-07-01 03:56:13 +00:00
fengzeroz 5ec90849d1 Merge pull request #2839 from fengzeroz/v2.13
compile with strong protector
2026-07-01 11:43:42 +08:00
fengzero 3f3983bebb compile with strong protector 2026-07-01 02:39:11 +00:00
fengzeroz aabf247cdc Merge pull request #2828 from hxy7yx/v2.13-1
[v2.13]fix(ft):mqtt configuration
2026-06-25 11:20:48 +08:00
hxy7yx 53fa5aac7c fix(ft):mqtt configuration 2026-06-23 23:19:43 -07:00
fengzeroz ec466cf8c1 Merge pull request #2814 from ZhouBox/v2.13
[v2.13]change array value length type from uint8_t to uint16_t
2026-06-24 10:43:20 +08:00
fengzeroz 5029882613 Merge pull request #2819 from hxy7yx/v2.13-1
[v2.13]fix(mqtt): require CA for TLS and verify broker certificates
2026-06-24 10:39:34 +08:00
hxy7yx 4a5bacf4af fix(mqtt): require CA for TLS and verify broker certificates 2026-06-22 02:28:59 -07:00
ZhouBox e4eb9b6863 change array value length type from uint8_t to uint16_t 2026-06-22 16:50:35 +08:00
fengzeroz 2bcc7a7f27 Merge pull request #2806 from fengzeroz/v2.13
rm neurn.key pem
2026-06-17 15:20:09 +08:00
fengzero 775dabfa00 rm neurn.key pem 2026-06-17 07:18:14 +00:00
fengzeroz 76abf7ba89 Merge pull request #2790 from fengzeroz/v2.13
update jwt
2026-06-11 15:20:52 +08:00
fengzero 3bfbad9f2f update jwt 2026-06-11 07:08:09 +00:00
fengzeroz cc936e5572 Merge pull request #2786 from fengzeroz/v2.13
interval limit 30ms
2026-06-11 13:59:35 +08:00
fengzero 5e80d69263 interval limit 30ms 2026-06-11 05:57:48 +00:00
fengzeroz 028764ea21 Merge pull request #2780 from fengzeroz/v2.13
version: 2.13.1
2026-06-03 14:41:26 +08:00
fengzero aafbbc467b version: 2.13.1 2026-06-03 06:40:48 +00:00
fengzeroz 3e25bd4650 Merge pull request #2777 from fengzeroz/v2.13
refactor: replace strcpy with strncpy
2026-06-02 17:05:39 +08:00
fengzero 0e2c8dbdb5 refactor: replace strcpy with strncpy 2026-06-02 08:39:50 +00:00
fengzeroz 103d0b3968 Merge pull request #2774 from fengzeroz/v2.13
api(cid,tpy,db): enable validate jwt
2026-05-29 14:51:12 +08:00
fengzero 95b116fe8d api(cid,tpy,db): enable validate jwt 2026-05-29 06:26:34 +00:00
fengzeroz f345c3fd06 Merge pull request #2769 from fengzeroz/v2.13
fix(mqtt): replace strcpy with strncpy
2026-05-29 14:24:49 +08:00
fengzero 0c6b16976d fix(mqtt): replace strcpy with strncpy 2026-05-29 06:10:27 +00:00
fengzeroz a124726331 Merge pull request #2763 from fengzeroz/v2.13
fix(mqtt): verify the length of the data received from protobuf
2026-05-25 09:38:02 +08:00
fengzero dfe881f3f0 fix(mqtt): verify the length of the data received from protobuf 2026-05-22 08:48:21 +00:00
fengzeroz 261f55a642 Merge pull request #2754 from fengzeroz/v2.13
delete api related to log file (deprecated)
2026-05-22 15:56:01 +08:00
fengzero 49d602416e delete api related to log file (deprecated) 2026-05-22 07:46:44 +00:00
fengzeroz f030f6ba13 Merge pull request #2732 from ZhouBox/v2.13
[v2.13]fix(core): correct parameter order in neu_req_del_tag_copy
2026-04-09 10:56:19 +08:00
ZhouBox cbafa6c37f fix(core): correct parameter order in neu_req_del_tag_copy
- Fix reversed parameter order in neu_req_del_tag_copy call at manager.c:1488
- Add missing return statement on strdup failure in neu_req_del_tag_copy function
2026-04-09 10:42:33 +08:00
fengzeroz 2505931755 Merge pull request #2730 from ZhouBox/v2.13
[v2.13]fix bug:json object equality check failed
2026-04-08 14:15:55 +08:00
ZhouBox 7d5670180f fix bug:json object equality check failed 2026-04-08 13:56:18 +08:00
fengzeroz f4607f5592 Merge pull request #2716 from fengzeroz/v2.13
rm mqtt db cache
2026-03-17 10:08:06 +08:00
fengzero 0e4f59d737 rm mqtt db cache 2026-03-17 01:53:47 +00:00
fengzeroz 84ccd1519d Merge pull request #2714 from fengzeroz/v2.13
version: 2.13.2
2026-03-16 14:34:32 +08:00
fengzero 9d177fca99 version: 2.13.2 2026-03-16 06:33:25 +00:00
fengzeroz dc73b5cc4c Merge pull request #2711 from ZhouBox/v2.13
[v2.13]log errno detail on message forward failure
2026-03-13 17:06:44 +08:00
ZhouBox 8e57c67533 fix monitor ready check to use abstract socket name index 2026-03-13 16:51:28 +08:00
ZhouBox cb8b793980 log errno detail on message forward failure 2026-03-13 15:35:49 +08:00
fengzeroz 22ab84bfdb Merge pull request #2708 from ZhouBox/v2.13
[v2.13]compile:support gcc 14
2026-03-11 09:18:53 +08:00
ZhouBox 81a7507f3b compile:support gcc 14 2026-03-11 08:39:59 +08:00
fengzeroz 6af24bc027 Merge pull request #2704 from fengzeroz/v2.13
filter array in metas
2026-03-10 11:26:36 +08:00
fengzero 8e8904eb50 filter array in metas 2026-03-10 03:12:49 +00:00
fengzeroz e5eda4651f Merge pull request #2699 from ZhouBox/v2.13
[v2.13]fixbug:heap use after free
2026-03-03 13:38:21 +08:00
xiang.zhou 470c6b1c0c fixbug:heap use after free 2026-03-03 11:09:51 +08:00
fengzeroz 46984ee06b Merge pull request #2683 from fengzeroz/v2.13
V2.13
2026-01-27 18:06:16 +08:00
fengzero 1509f696f2 core(cache): update_im_f_m, filter dup value 2026-01-27 09:58:56 +00:00
fengzero 772d792b33 modify default log config 2026-01-27 09:58:56 +00:00
fengzeroz e89995ad56 Merge pull request #2664 from fengzeroz/v2.13
write tag log
2026-01-19 15:04:12 +08:00
fengzero a767d2a7f7 write tag log 2026-01-19 06:56:45 +00:00
fengzeroz 0e2d6342e2 Merge pull request #2657 from fengzeroz/v2.13
cid name len
2026-01-06 14:29:31 +08:00
fengzero a39d1205ea cid name len 2026-01-06 06:25:45 +00:00
fengzeroz 90050cc222 Merge pull request #2646 from fengzeroz/v2.13
fix(group): free group context with old group
2025-12-24 16:15:03 +08:00
fengzero 0440fe59a4 fix(group): free group context with old group 2025-12-24 08:14:03 +00:00
fengzeroz bc595ab3eb Merge pull request #2641 from fengzeroz/v2.13
fix(connection): close fd if ip invalid
2025-12-19 15:04:46 +08:00
fengzero 4e8d47dbdf fix(connection): close fd if ip invalid 2025-12-19 07:01:24 +00:00
88 changed files with 2058 additions and 909 deletions
+13 -1
View File
@@ -13,7 +13,7 @@ jobs:
steps:
# checkout
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
submodules: recursive
@@ -40,6 +40,18 @@ jobs:
cppcheck --enable=all --error-exitcode=2 --inline-suppr --suppress=missingInclude --suppress=unusedFunction --suppress=*:src/otel/trace.pb-c.c --suppress=*:src/otel/common.pb-c.c --suppress=*:src/otel/resource.pb-c.c ./plugins ./src
cppcheck --enable=all --error-exitcode=2 --inline-suppr --suppress=missingInclude --suppress=variableScope ./simulator
flawfinder:
name: Flawfinder Scan
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: flawfinder_scan
uses: david-a-wheeler/flawfinder@2.0.20
with:
arguments: '--minlevel=3 .'
build:
runs-on: ubuntu-24.04
container: ghcr.io/neugates/build:x86_64-v2.13
+1 -1
View File
@@ -8,7 +8,7 @@ on:
- published
env:
VERSION: 2.13.0
VERSION: 2.13.2
UI_VERSION: 2.6.1
BRANCH: v2.13
+6 -1
View File
@@ -40,8 +40,13 @@ if(USE_GCOV)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
endif(USE_GCOV)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=format-truncation")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O0")
add_compile_options(-fstack-protector-strong)
add_link_options(-fstack-protector-strong)
add_link_options(-pie -Wl,-z,relro,-z,now)
if(NOT DISABLE_ASAN)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -fsanitize=address")
@@ -97,6 +102,7 @@ set(NEURON_BASE_SOURCES
src/utils/log.c
src/utils/cid.c
src/utils/tpy.c
src/utils/neu_path.c
${PERSIST_SOURCES})
if (SMART_LINK)
@@ -166,7 +172,6 @@ set(NEURON_SOURCES
plugins/restful/otel_handle.c
plugins/restful/cid_handle.c
plugins/restful/tpy_handle.c
plugins/restful/system_handle.c
plugins/restful/datalayers_handle.c
plugins/restful/rest.c
plugins/restful/user.c)
+9 -1
View File
@@ -72,7 +72,15 @@ $ unzip neuron-dashboard.zip
$ ./neuron --log
```
5. Open a web browser and navigate to `http://localhost:7000` to access the Neuron web interface.
On the first startup, Neuron will automatically generate `neuron.key` and `neuron.key.pub` in the config directory if they do not exist yet.
If you want to pre-generate the key pair manually, you can still run:
```bash
$ bash ./generate_rsa_keypair.sh ./build/config
```
5. Open a browser and navigate to `http://localhost:7000` to access the Neuron web interface.
### Docker
+1 -2
View File
@@ -10,6 +10,5 @@ syslog = "<P>%d(%Y-%m-%dT%T%z) %H neuron[%p]: level=%v msg="[%c] %m" file="%f:%L
[rules]
*.* "./logs/%c.log", 25MB * 1 ~ "./logs/%c.#2r.log"; file_format
*.* "./logs/%c.log", 50MB * 2 ~ "./logs/%c.#2r.log"; file_format
*.* >stdout; stdout_format
*.* $remote_syslog,"%V"; syslog
+34
View File
@@ -0,0 +1,34 @@
#!/bin/bash
set -euo pipefail
output_dir="${1:-config}"
private_key_file="${output_dir}/neuron.key"
public_key_file="${output_dir}/neuron.key.pub"
if ! command -v openssl >/dev/null 2>&1; then
echo "openssl is required but was not found in PATH."
exit 1
fi
mkdir -p "${output_dir}"
if [[ -f "${private_key_file}" || -f "${public_key_file}" ]]; then
read -r -p "Existing key files found in ${output_dir}. Overwrite them? [y/N] " answer
case "${answer}" in
[yY]|[yY][eE][sS]) ;;
*)
echo "Aborted."
exit 0
;;
esac
fi
umask 077
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "${private_key_file}"
openssl pkey -in "${private_key_file}" -pubout -out "${public_key_file}"
echo "Generated RSA key pair:"
echo " ${private_key_file}"
echo " ${public_key_file}"
echo "Run neuron with --config_dir ${output_dir} to use the generated keys."
+3
View File
@@ -86,6 +86,9 @@ typedef struct adapter_callbacks {
void (*update_im)(neu_adapter_t *adapter, const char *group,
const char *tag, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta);
void (*update_im_f_m)(neu_adapter_t *adapter, const char *group,
const char *tag, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta);
void (*scan_tags_response)(neu_adapter_t *adapter, void *r,
neu_resp_scan_tags_t *resp_scan);
void (*test_read_tag_response)(neu_adapter_t *adapter, void *r,
+8 -5
View File
@@ -24,7 +24,7 @@
#define NEU_VERSION_MAJOR 2
#define NEU_VERSION_MINOR 13
#define NEU_VERSION_FIX 0
#define NEU_VERSION_FIX 2
#define NEU_GET_VERSION_MAJOR(V) ((0xffff0000 & V) >> 16)
#define NEU_GET_VERSION_MINOR(V) ((0x0000ff00 & V) >> 8)
@@ -36,7 +36,7 @@
#define NEU_TAG_DESCRIPTION_LEN 128
#define NEU_GROUP_MAX_PER_NODE 512
#define NEU_GROUP_NAME_LEN 128
#define NEU_GROUP_INTERVAL_LIMIT 100
#define NEU_GROUP_INTERVAL_LIMIT 30
#define NEU_DEFAULT_GROUP_INTERVAL 100
#define NEU_NODE_NAME_LEN 128
#define NEU_PLUGIN_NAME_LEN 32
@@ -67,7 +67,7 @@
#define NEU_LOG_LEVEL_FATAL "fatal"
#define NEU_CID_LNCLASS_LEN 12
#define NEU_CID_LNTYPE_LEN 32
#define NEU_CID_LNTYPE_LEN 40
#define NEU_CID_LNPREFIX_LEN 10
#define NEU_CID_LNINST_LEN 4
#define NEU_CID_LDEVICE_LEN 32
@@ -85,7 +85,7 @@
#define NEU_CID_DATASET_NAME_LEN 32
#define NEU_CID_REPORT_NAME_LEN 32
#define NEU_CID_REPORT_ID_LEN 64
#define NEU_CID_ID_LEN 32
#define NEU_CID_ID_LEN 40
#define NEU_CID_REF_TYPE_LEN 32
#define NEU_CID_IED_NAME_LEN 32
#define NEU_CID_ACCESS_POINT_NAME_LEN 16
@@ -93,9 +93,12 @@
#define NEU_CID_LEN4 4
#define NEU_CID_LEN8 8
#define NEU_CID_LEN16 16
#define NEU_CID_LEN32 32
#define NEU_CID_LEN32 72
#define NEU_CID_LEN64 64
#define NEU_TRACE_ID_LEN 64
#define NEU_SPAN_ID_LEN 32
#define CHECK_NODE_NAME_LENGTH_ERR \
do { \
NEU_JSON_RESPONSE_ERROR(NEU_ERR_NODE_NAME_TOO_LONG, { \
+15 -15
View File
@@ -61,58 +61,58 @@ typedef enum neu_json_type {
} neu_json_type_e;
typedef struct {
int8_t *i8s;
uint8_t length;
int8_t * i8s;
uint16_t length;
} neu_json_value_array_int8_t;
typedef struct {
uint8_t *u8s;
uint8_t length;
uint16_t length;
} neu_json_value_array_uint8_t;
typedef struct {
int16_t *i16s;
uint8_t length;
uint16_t length;
} neu_json_value_array_int16_t;
typedef struct {
uint16_t *u16s;
uint8_t length;
uint16_t length;
} neu_json_value_array_uint16_t;
typedef struct {
int32_t *i32s;
uint8_t length;
uint16_t length;
} neu_json_value_array_int32_t;
typedef struct {
uint32_t *u32s;
uint8_t length;
uint16_t length;
} neu_json_value_array_uint32_t;
typedef struct {
int64_t *i64s;
uint8_t length;
uint16_t length;
} neu_json_value_array_int64_t;
typedef struct {
uint64_t *u64s;
uint8_t length;
uint16_t length;
} neu_json_value_array_uint64_t;
typedef struct {
float * f32s;
uint8_t length;
float * f32s;
uint16_t length;
} neu_json_value_array_float_t;
typedef struct {
double *f64s;
uint8_t length;
double * f64s;
uint16_t length;
} neu_json_value_array_double_t;
typedef struct {
bool * bools;
uint8_t length;
bool * bools;
uint16_t length;
} neu_json_value_array_bool_t;
typedef struct {
+2 -2
View File
@@ -286,8 +286,8 @@ typedef struct {
typedef struct {
char distro[32];
char kernel[32];
char machine[32];
char kernel[80];
char machine[80];
char clib[32];
char clib_version[32];
unsigned cpu_percent;
+9 -8
View File
@@ -654,8 +654,8 @@ static inline void neu_req_add_tag_fini(neu_req_add_tag_t *req)
static inline int neu_req_add_tag_copy(neu_req_add_tag_t *dst,
neu_req_add_tag_t *src)
{
strcpy(dst->driver, src->driver);
strcpy(dst->group, src->group);
strncpy(dst->driver, src->driver, NEU_NODE_NAME_LEN - 1);
strncpy(dst->group, src->group, NEU_GROUP_NAME_LEN - 1);
dst->tags = (neu_datatag_t *) calloc(src->n_tag, sizeof(src->tags[0]));
if (NULL == dst->tags) {
return -1;
@@ -690,8 +690,8 @@ static inline void neu_req_del_tag_fini(neu_req_del_tag_t *req)
static inline int neu_req_del_tag_copy(neu_req_del_tag_t *dst,
neu_req_del_tag_t *src)
{
strcpy(dst->driver, src->driver);
strcpy(dst->group, src->group);
strncpy(dst->driver, src->driver, NEU_NODE_NAME_LEN - 1);
strncpy(dst->group, src->group, NEU_GROUP_NAME_LEN - 1);
dst->tags = (char **) calloc(src->n_tag, sizeof(src->tags[0]));
if (NULL == dst->tags) {
return -1;
@@ -704,6 +704,7 @@ static inline int neu_req_del_tag_copy(neu_req_del_tag_t *dst,
free(dst->tags[i]);
}
free(dst->tags);
return -1;
}
}
return 0;
@@ -834,7 +835,7 @@ static inline void neu_req_node_setting_fini(neu_req_node_setting_t *req)
static inline int neu_req_node_setting_copy(neu_req_node_setting_t *dst,
neu_req_node_setting_t *src)
{
strcpy(dst->node, src->node);
strncpy(dst->node, src->node, NEU_NODE_NAME_LEN - 1);
dst->setting = strdup(src->setting);
return dst->setting ? 0 : -1;
}
@@ -1658,13 +1659,13 @@ inline static void neu_msg_exchange(neu_reqresp_head_t *header)
{
char tmp[NEU_NODE_NAME_LEN] = { 0 };
strcpy(tmp, header->sender);
strncpy(tmp, header->sender, sizeof(tmp) - 1);
memset(header->sender, 0, sizeof(header->sender));
strcpy(header->sender, header->receiver);
strncpy(header->sender, header->receiver, sizeof(header->sender) - 1);
memset(header->receiver, 0, sizeof(header->receiver));
strcpy(header->receiver, tmp);
strncpy(header->receiver, tmp, sizeof(header->receiver) - 1);
}
typedef struct {
+1
View File
@@ -38,6 +38,7 @@ extern "C" {
#include "utils/zlog.h"
#include "utils/neu_jwt.h"
#include "utils/neu_path.h"
#include "utils/time.h"
#include "utils/utarray.h"
#include "utils/utextend.h"
+5
View File
@@ -345,6 +345,11 @@ typedef struct {
uint8_t precision;
} neu_dvalue_t;
static inline bool neu_dvalue_is_array(const neu_dvalue_t *dvalue)
{
return dvalue->type >= NEU_TYPE_ARRAY_CHAR;
}
static inline bool neu_equal_dvalue(const neu_dvalue_t *one, neu_dvalue_t *two)
{
if (one == NULL || two == NULL) {
+50
View File
@@ -0,0 +1,50 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2024 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#ifndef NEURON_UTILS_PATH_H
#define NEURON_UTILS_PATH_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
// Reject an untrusted path component (e.g. a node/plugin/library name that is
// used to build a filesystem path). Returns true only when `name` is a single,
// safe path segment: non-empty, contains no '/' and is not "." or "..".
bool neu_path_is_valid_component(const char *name);
// Confine an untrusted `path` to the trusted base directory `base`.
//
// The result is the canonical absolute path when it resolves inside `base`
// (either a relative path joined under `base`, or an absolute path that already
// lies within `base`). Returns a newly heap-allocated string the caller must
// free(), or NULL if the path escapes `base`, is invalid, or cannot be
// resolved. A not-yet-existing final component (e.g. a file about to be
// created) is handled by resolving its parent directory.
//
// If `base` is NULL or empty, the current working directory is used.
char *neu_path_confine(const char *base, const char *path);
#ifdef __cplusplus
}
#endif
#endif
+1 -1
View File
@@ -513,7 +513,7 @@ do {
*/
#ifdef HASH_DEBUG
#include <stdio.h> /* fprintf, stderr */
#define HASH_OOPS(...) do { fprintf(stderr, __VA_ARGS__); exit(-1); } while (0)
#define HASH_OOPS(...) do { fprintf(stderr, "%s", __VA_ARGS__); exit(-1); } while (0)
#define HASH_FSCK(hh,head,where) \
do { \
struct UT_hash_handle *_thh; \
+14 -2
View File
@@ -1,8 +1,20 @@
import jwt # pip install pyjwt
import time
from pathlib import Path
with open("neuron.key", "r") as f:
private_key = f.read()
key_candidates = [
Path("config") / "neuron.key",
Path("neuron.key"),
]
private_key = None
for key_path in key_candidates:
if key_path.is_file():
private_key = key_path.read_text()
break
if private_key is None:
raise FileNotFoundError("Cannot find neuron.key in ./config or the current directory")
current_time = int(time.time())
two_years = 60 * 60 * 24 * 365 * 2
+2 -2
View File
@@ -1,8 +1,8 @@
{
"ip": "0.0.0.0",
"ip": "127.0.0.1",
"port": 7000,
"disable_auth": 0,
"syslog_host": "",
"syslog_port": 541,
"sub_filter_error": 0
}
}
+3
View File
@@ -30,6 +30,7 @@
#include "read_write.h"
#define EKUIPER_PLUGIN_URL "tcp://127.0.0.1:7081"
#define EKUIPER_PLUGIN_RECV_MAX_SIZE (4 * 1024 * 1024)
const neu_plugin_module_t neu_plugin_module;
@@ -149,6 +150,8 @@ static inline int start(neu_plugin_t *plugin, const char *url)
nng_pipe_notify(plugin->sock, NNG_PIPE_EV_REM_POST, pipe_rm_cb, plugin);
nng_socket_set_int(plugin->sock, NNG_OPT_SENDBUF, 2048);
nng_socket_set_int(plugin->sock, NNG_OPT_RECVBUF, 2048);
nng_socket_set_size(plugin->sock, NNG_OPT_RECVMAXSZ,
EKUIPER_PLUGIN_RECV_MAX_SIZE);
if ((rv = nng_listen(plugin->sock, url, NULL, 0)) != 0) {
plog_error(plugin, "nng_listen: %s", nng_strerror(rv));
+13 -12
View File
@@ -204,7 +204,8 @@ void recv_data_callback(void *arg)
body_str = nng_msg_body(msg);
body_len = nng_msg_len(msg);
if (*(uint8_t *) body_str == 0x0A && *(uint8_t *) (body_str + 1) == 0xCE) {
if (body_len >= 26 && *(uint8_t *) body_str == 0x0A &&
*(uint8_t *) (body_str + 1) == 0xCE) {
// trace
trace_id = (uint8_t *) (body_str + 2);
span_id = (uint8_t *) (body_str + 18);
@@ -239,13 +240,12 @@ void recv_data_callback(void *arg)
req->single.tag, req->single.node);
}
} else {
rv = send_write_tags_req(plugin, &req->plural, json_str, trace_flag,
trace_id, span_id);
if (rv == 0) {
if (req->plural.n_tag == 0) {
plog_warn(plugin, "no tags to write on node %s",
req->plural.node);
} else {
if (req->plural.n_tag == 0) {
plog_warn(plugin, "no tags to write on node %s", req->plural.node);
} else {
rv = send_write_tags_req(plugin, &req->plural, json_str, trace_flag,
trace_id, span_id);
if (rv == 0) {
plog_notice(plugin, "write %d tags on node %s",
req->plural.n_tag, req->plural.node);
}
@@ -273,7 +273,8 @@ static int json_value_to_tag_value(union neu_json_value *req,
break;
case NEU_JSON_STR:
value->type = NEU_TYPE_STRING;
strncpy(value->value.str, req->val_str, sizeof(value->value.str));
strncpy(value->value.str, req->val_str, sizeof(value->value.str) - 1);
value->value.str[sizeof(value->value.str) - 1] = '\0';
break;
case NEU_JSON_DOUBLE:
value->type = NEU_TYPE_DOUBLE;
@@ -327,7 +328,7 @@ static int send_write_tag_req(neu_plugin_t *plugin, neu_json_write_req_t *req,
header.ctx = calloc(1, 48 + strlen(playload) + 1);
binary_to_hex(trace_id, 16, header.ctx);
binary_to_hex(span_id, 8, header.ctx + 32);
strcpy(header.ctx + 48, playload);
strncpy(header.ctx + 48, playload, strlen(playload));
}
neu_req_write_tag_t cmd = {
@@ -374,7 +375,7 @@ static int send_write_tags_req(neu_plugin_t * plugin,
header.ctx = calloc(1, 48 + strlen(playload) + 1);
binary_to_hex(trace_id, 16, header.ctx);
binary_to_hex(span_id, 8, header.ctx + 32);
strcpy(header.ctx + 48, playload);
strncpy(header.ctx + 48, playload, strlen(playload));
}
neu_req_write_tags_t cmd = { 0 };
@@ -387,7 +388,7 @@ static int send_write_tags_req(neu_plugin_t * plugin,
}
for (int i = 0; i < cmd.n_tag; i++) {
strcpy(cmd.tags[i].tag, req->tags[i].tag);
strncpy(cmd.tags[i].tag, req->tags[i].tag, NEU_TAG_NAME_LEN - 1);
if (0 !=
json_value_to_tag_value(&req->tags[i].value, req->tags[i].t,
&cmd.tags[i].value)) {
+12 -6
View File
@@ -180,7 +180,9 @@ static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group)
{
neu_dvalue_t dvalue = { 0 };
FILE *fp = fopen(tag->address, "r");
char *safe_path = neu_path_confine(NULL, tag->address);
FILE *fp = safe_path ? fopen(safe_path, "r") : NULL;
free(safe_path);
if (fp == NULL) {
dvalue.type = NEU_TYPE_ERROR;
if (errno == ENOENT) {
@@ -189,14 +191,16 @@ static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group)
dvalue.value.i32 = NEU_ERR_FILE_OPEN_FAILURE;
}
} else {
char *buf = calloc(1, 1024);
int ret = fread(buf, 1, 1024, fp);
if (ret < 0) {
char * buf = calloc(1, 1024);
size_t ret = fread(buf, 1, 1024, fp);
if (ret == 0 && ferror(fp)) {
dvalue.type = NEU_TYPE_ERROR;
dvalue.value.i32 = NEU_ERR_FILE_READ_FAILURE;
} else {
size_t n = ret < NEU_VALUE_SIZE - 1 ? ret : NEU_VALUE_SIZE - 1;
dvalue.type = NEU_TYPE_STRING;
strncpy(dvalue.value.str, buf, NEU_VALUE_SIZE - 1);
strncpy(dvalue.value.str, buf, n);
dvalue.value.str[n] = '\0';
}
fclose(fp);
@@ -218,7 +222,9 @@ static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group)
static int driver_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
neu_value_u value)
{
FILE *fp = fopen(tag->address, "w");
char *safe_path = neu_path_confine(NULL, tag->address);
FILE *fp = safe_path ? fopen(safe_path, "w") : NULL;
free(safe_path);
if (fp == NULL) {
if (errno == ENOENT) {
plugin->common.adapter_callbacks->driver.write_response(
+1 -1
View File
@@ -111,7 +111,7 @@
"name_zh": "CA 证书",
"description": "CA certificate which signs the server certificate",
"description_zh": "签发服务器证书的 CA 证书",
"attribute": "optional",
"attribute": "required",
"type": "file",
"condition": {
"field": "ssl",
+4 -4
View File
@@ -84,11 +84,11 @@ static inline int parse_ssl_params(neu_plugin_t *plugin, const char *setting,
return 0;
}
// ca, optional
// ca, required when ssl enabled
ret = neu_parse_param(setting, NULL, 1, ca);
if (0 != ret) {
plog_notice(plugin, "setting no ca");
return 0;
plog_error(plugin, "setting no ca, required when ssl enabled");
return -1;
}
if (0 != decode_b64_param(plugin, ca)) {
@@ -106,7 +106,7 @@ static inline int parse_ssl_params(neu_plugin_t *plugin, const char *setting,
return -1;
}
// key, required if cert enable
// key, required if cert is set
ret = parse_b64_param(plugin, setting, key);
if (0 != ret) {
return -1;
+18 -4
View File
@@ -41,9 +41,11 @@ static char *generate_heartbeat_json(neu_plugin_t *plugin, UT_array *states)
char * json_str = NULL;
json.n_state = utarray_len(states);
json.states = calloc(json.n_state, sizeof(neu_json_node_state_t));
if (NULL == json.states) {
return NULL;
if (json.n_state > 0) {
json.states = calloc(json.n_state, sizeof(neu_json_node_state_t));
if (NULL == json.states) {
return NULL;
}
}
utarray_foreach(states, neu_nodes_state_t *, state)
@@ -210,6 +212,10 @@ static char *generate_event_json(neu_plugin_t *plugin, neu_reqresp_type_e event,
json_req.add_gtag.n_group = add_gtag->n_group;
json_req.add_gtag.groups =
calloc(add_gtag->n_group, sizeof(*json_req.add_gtag.groups));
if (NULL == json_req.add_gtag.groups) {
plog_error(plugin, "calloc fail");
break;
}
for (u_int16_t i = 0; i < add_gtag->n_group; i++) {
json_req.add_gtag.groups[i].group =
strdup(add_gtag->groups[i].group);
@@ -218,6 +224,10 @@ static char *generate_event_json(neu_plugin_t *plugin, neu_reqresp_type_e event,
json_req.add_gtag.groups[i].tags =
calloc(add_gtag->groups[i].n_tag,
sizeof(*json_req.add_gtag.groups->tags));
if (NULL == json_req.add_gtag.groups[i].tags) {
plog_error(plugin, "calloc fail");
break;
}
for (u_int16_t j = 0; j < add_gtag->groups[i].n_tag; j++) {
json_req.add_gtag.groups[i].tags[j].type =
add_gtag->groups[i].tags[j].type;
@@ -239,7 +249,11 @@ static char *generate_event_json(neu_plugin_t *plugin, neu_reqresp_type_e event,
neu_json_encode_by_fn(&json_req, neu_json_encode_add_gtags_req,
&json_str);
free(json_req.add_tags.tags);
for (u_int16_t i = 0; i < add_gtag->n_group; i++) {
free(json_req.add_gtag.groups[i].group);
free(json_req.add_gtag.groups[i].tags);
}
free(json_req.add_gtag.groups);
break;
}
case NEU_REQ_ADD_PLUGIN_EVENT: {
+1 -1
View File
@@ -206,7 +206,7 @@
"name_zh": "根 CA 证书",
"description": "AWS IoT data endpoint root CA certificate",
"description_zh": "AWS IoT 设备数据终端节点根 CA 证书",
"attribute": "optional",
"attribute": "required",
"type": "file",
"condition": {
"field": "ssl",
-4
View File
@@ -106,10 +106,6 @@
"description_zh": "签发服务器证书的 CA 证书",
"attribute": "required",
"type": "file",
"condition": {
"field": "auth",
"value": 1
},
"valid": {
"length": 81960
}
+13 -15
View File
@@ -29,22 +29,10 @@
#define AUTH_CERT 1
static int parse_ssl_params(neu_plugin_t *plugin, const char *setting,
neu_json_elem_t *ca, neu_json_elem_t *cert,
neu_json_elem_t *key)
neu_json_elem_t *cert, neu_json_elem_t *key)
{
// ca, required
int ret = neu_parse_param(setting, NULL, 1, ca);
if (0 != ret) {
plog_notice(plugin, "setting no ca");
return -1;
}
if (0 != decode_b64_param(plugin, ca)) {
return -1;
}
// cert, required
ret = neu_parse_param(setting, NULL, 1, cert);
int ret = neu_parse_param(setting, NULL, 1, cert);
if (0 != ret) {
plog_notice(plugin, "setting no cert");
return -1;
@@ -137,8 +125,18 @@ static int azure_parse_config(neu_plugin_t *plugin, const char *setting,
password.v.val_str = strdup("");
}
// ca, required for TLS (SSL is always enabled)
ret = neu_parse_param(setting, NULL, 1, &ca);
if (0 != ret) {
plog_error(plugin, "setting no ca");
goto error;
}
if (0 != decode_b64_param(plugin, &ca)) {
goto error;
}
if (AUTH_CERT == auth.v.val_int) {
ret = parse_ssl_params(plugin, setting, &ca, &cert, &key);
ret = parse_ssl_params(plugin, setting, &cert, &key);
if (0 != ret) {
plog_error(plugin, "setting certificates fail");
goto error;
+1 -1
View File
@@ -306,7 +306,7 @@
"name_zh": "CA 证书",
"description": "CA certificate which signs the server certificate",
"description_zh": "签发服务器证书的 CA 证书",
"attribute": "optional",
"attribute": "required",
"type": "file",
"condition": {
"field": "ssl",
+4 -4
View File
@@ -81,11 +81,11 @@ static int parse_ssl_params(neu_plugin_t *plugin, const char *setting,
return 0;
}
// ca, optional
// ca, required when ssl enabled
ret = neu_parse_param(setting, NULL, 1, ca);
if (0 != ret) {
plog_notice(plugin, "setting no ca");
return 0;
plog_error(plugin, "setting no ca, required when ssl enabled");
return -1;
}
if (0 != decode_b64_param(plugin, ca)) {
@@ -103,7 +103,7 @@ static int parse_ssl_params(neu_plugin_t *plugin, const char *setting,
return -1;
}
// key, required if cert enable
// key, required if cert is set
ret = parse_b64_param(plugin, setting, key);
if (0 != ret) {
return -1;
+99 -34
View File
@@ -39,7 +39,7 @@ static void to_traceparent(uint8_t *trace_id, char *span_id, char *out)
size += sprintf(out + size, "%02x", trace_id[i]);
}
sprintf(out + size, "-%s-01", span_id);
snprintf(out + size, strlen(span_id) + 5, "-%s-01", span_id);
}
static int tag_values_to_json(UT_array *tags, mqtt_static_vt_t *s_tags,
@@ -281,7 +281,7 @@ static inline int send_driver_action(neu_plugin_t * plugin,
header.ctx = NULL;
neu_req_driver_action_t action = { 0 };
strncpy(action.driver, req->driver, NEU_NODE_NAME_LEN);
strncpy(action.driver, req->driver, NEU_NODE_NAME_LEN - 1);
action.action = strdup(req->action);
if (0 != neu_plugin_op(plugin, header, &action)) {
@@ -379,7 +379,8 @@ static int json_value_to_tag_value(union neu_json_value *req,
value->value.strs.strs =
calloc(req->val_array_str.length, sizeof(char *));
for (size_t i = 0; i < req->val_array_str.length; i++) {
value->value.strs.strs[i] = strdup(req->val_array_str.p_strs[i]);
const char *s = req->val_array_str.p_strs[i];
value->value.strs.strs[i] = strdup(s ? s : "");
}
break;
case NEU_JSON_OBJECT:
@@ -465,7 +466,7 @@ static int send_write_tags_req(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt,
}
for (int i = 0; i < cmd.n_tag; i++) {
strcpy(cmd.tags[i].tag, req->tags[i].tag);
strncpy(cmd.tags[i].tag, req->tags[i].tag, NEU_TAG_NAME_LEN - 1);
if (0 !=
json_value_to_tag_value(&req->tags[i].value, req->tags[i].t,
&cmd.tags[i].value)) {
@@ -574,7 +575,44 @@ void handle_write_req(neu_mqtt_qos_e qos, const char *topic,
return;
}
mqtt = calloc(1, sizeof(neu_json_mqtt_t));
if (strlen(wr->node) >= NEU_NODE_NAME_LEN ||
strlen(wr->group) >= NEU_GROUP_NAME_LEN) {
plog_error(plugin, "node or group name too long");
model__write_request__free_unpacked(wr, NULL);
return;
}
for (size_t i = 0; i < wr->n_tags; i++) {
if (wr->tags[i]->name == NULL ||
wr->tags[i]->item_case != MODEL__DATA_ITEM__ITEM_VALUE ||
wr->tags[i]->value == NULL) {
plog_error(plugin, "write request tag missing name/value");
model__write_request__free_unpacked(wr, NULL);
return;
}
if (strlen(wr->tags[i]->name) >= NEU_TAG_NAME_LEN) {
plog_error(plugin, "tag name too long");
model__write_request__free_unpacked(wr, NULL);
return;
}
if (wr->tags[i]->value->value_case ==
MODEL__DATA_ITEM_VALUE__VALUE_STRING_VALUE) {
if (strlen(wr->tags[i]->value->string_value) >=
NEU_VALUE_SIZE) {
plog_error(plugin, "string tag value too long");
model__write_request__free_unpacked(wr, NULL);
return;
}
}
}
mqtt = calloc(1, sizeof(neu_json_mqtt_t));
if (NULL == mqtt) {
plog_error(plugin, "calloc mqtt fail");
model__write_request__free_unpacked(wr, NULL);
return;
}
mqtt->uuid = strdup(wr->uuid);
mqtt->payload = NULL;
mqtt->traceparent = NULL;
@@ -590,10 +628,18 @@ void handle_write_req(neu_mqtt_qos_e qos, const char *topic,
cmd.n_tag = wr->n_tags;
if (cmd.n_tag > 0) {
cmd.tags = calloc(cmd.n_tag, sizeof(neu_resp_tag_value_t));
if (NULL == cmd.tags) {
plog_error(plugin, "calloc cmd.tags fail");
free(cmd.driver);
free(cmd.group);
neu_json_decode_mqtt_req_free(mqtt);
model__write_request__free_unpacked(wr, NULL);
return;
}
}
for (int i = 0; i < cmd.n_tag; i++) {
strcpy(cmd.tags[i].tag, wr->tags[i]->name);
strncpy(cmd.tags[i].tag, wr->tags[i]->name, NEU_TAG_NAME_LEN - 1);
switch (wr->tags[i]->value->value_case) {
case MODEL__DATA_ITEM_VALUE__VALUE_INT_VALUE:
cmd.tags[i].value.type = NEU_TYPE_INT64;
@@ -610,8 +656,8 @@ void handle_write_req(neu_mqtt_qos_e qos, const char *topic,
break;
case MODEL__DATA_ITEM_VALUE__VALUE_STRING_VALUE:
cmd.tags[i].value.type = NEU_TYPE_STRING;
strcpy(cmd.tags[i].value.value.str,
wr->tags[i]->value->string_value);
strncpy(cmd.tags[i].value.value.str,
wr->tags[i]->value->string_value, NEU_VALUE_SIZE - 1);
break;
default:
break;
@@ -900,14 +946,19 @@ void handle_driver_action_req(neu_mqtt_qos_e qos, const char *topic,
return;
}
mqtt = calloc(1, sizeof(neu_json_mqtt_t));
mqtt = calloc(1, sizeof(neu_json_mqtt_t));
if (NULL == mqtt) {
plog_error(plugin, "calloc mqtt fail");
model__driver_action_request__free_unpacked(dar, NULL);
return;
}
mqtt->uuid = strdup(dar->uuid);
mqtt->payload = NULL;
mqtt->traceparent = NULL;
mqtt->tracestate = NULL;
header.ctx = mqtt;
strcpy(cmd.driver, dar->node);
strncpy(cmd.driver, dar->node, NEU_NODE_NAME_LEN - 1);
cmd.action = strdup(dar->action);
model__driver_action_request__free_unpacked(dar, NULL);
@@ -930,7 +981,7 @@ void handle_driver_action_req(neu_mqtt_qos_e qos, const char *topic,
}
header.ctx = mqtt;
strcpy(cmd.driver, req->driver);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
cmd.action = strdup(req->action);
neu_json_decode_driver_action_req_free(req);
free(json_str);
@@ -974,7 +1025,12 @@ void handle_read_req(neu_mqtt_qos_e qos, const char *topic,
neu_reqresp_head_t header = { 0 };
mqtt = calloc(1, sizeof(neu_json_mqtt_t));
mqtt = calloc(1, sizeof(neu_json_mqtt_t));
if (NULL == mqtt) {
plog_error(plugin, "calloc mqtt fail");
model__read_request__free_unpacked(read_req, NULL);
return;
}
mqtt->uuid = strdup(read_req->uuid);
mqtt->payload = NULL;
mqtt->traceparent = NULL;
@@ -991,6 +1047,14 @@ void handle_read_req(neu_mqtt_qos_e qos, const char *topic,
cmd.n_tag = read_req->n_tags;
if (cmd.n_tag > 0) {
cmd.tags = calloc(cmd.n_tag, sizeof(char *));
if (NULL == cmd.tags) {
plog_error(plugin, "calloc cmd.tags fail");
free(cmd.driver);
free(cmd.group);
neu_json_decode_mqtt_req_free(mqtt);
model__read_request__free_unpacked(read_req, NULL);
return;
}
}
for (int i = 0; i < cmd.n_tag; i++) {
cmd.tags[i] = strdup(read_req->tags[i]);
@@ -999,6 +1063,7 @@ void handle_read_req(neu_mqtt_qos_e qos, const char *topic,
if (0 != neu_plugin_op(plugin, header, &cmd)) {
neu_req_read_group_fini(&cmd);
plog_error(plugin, "neu_plugin_op(NEU_REQ_READ_GROUP) fail");
model__read_request__free_unpacked(read_req, NULL);
return;
}
@@ -1768,8 +1833,8 @@ void handle_driver_directory_req(neu_mqtt_qos_e qos, const char *topic,
mqtt->tracestate = NULL;
header.ctx = mqtt;
strcpy(cmd.driver, flr->node);
strcpy(cmd.path, flr->path);
strncpy(cmd.driver, flr->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.path, flr->path, NEU_PATH_LEN - 1);
model__file_list_request__free_unpacked(flr, NULL);
} else {
@@ -1792,8 +1857,8 @@ void handle_driver_directory_req(neu_mqtt_qos_e qos, const char *topic,
header.ctx = mqtt;
strcpy(cmd.driver, req->driver);
strcpy(cmd.path, req->path);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.path, req->path, NEU_PATH_LEN - 1);
neu_json_decode_driver_directory_req_free(req);
free(json_str);
@@ -1957,8 +2022,8 @@ void handle_driver_fup_open_req(neu_mqtt_qos_e qos, const char *topic,
mqtt->tracestate = NULL;
header.ctx = mqtt;
strcpy(cmd.driver, fur->node);
strcpy(cmd.path, fur->path);
strncpy(cmd.driver, fur->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.path, fur->path, NEU_PATH_LEN - 1);
model__file_upload_request__free_unpacked(fur, NULL);
} else {
@@ -1979,8 +2044,8 @@ void handle_driver_fup_open_req(neu_mqtt_qos_e qos, const char *topic,
return;
}
header.ctx = mqtt;
strcpy(cmd.driver, req->driver);
strcpy(cmd.path, req->path);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.path, req->path, NEU_PATH_LEN - 1);
neu_json_decode_driver_fup_open_req_free(req);
free(json_str);
@@ -2084,8 +2149,8 @@ void handle_driver_fup_data_req(neu_mqtt_qos_e qos, const char *topic,
header.ctx = mqtt;
strcpy(cmd.driver, fudr->node);
strcpy(cmd.path, fudr->path);
strncpy(cmd.driver, fudr->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.path, fudr->path, NEU_PATH_LEN - 1);
model__file_upload_data_request__free_unpacked(fudr, NULL);
} else {
@@ -2108,8 +2173,8 @@ void handle_driver_fup_data_req(neu_mqtt_qos_e qos, const char *topic,
header.ctx = mqtt;
strcpy(cmd.driver, req->driver);
strcpy(cmd.path, req->path);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.path, req->path, NEU_PATH_LEN - 1);
neu_json_decode_driver_fup_data_req_free(req);
free(json_str);
@@ -2218,9 +2283,9 @@ void handle_driver_fdown_open_req(neu_mqtt_qos_e qos, const char *topic,
mqtt->tracestate = NULL;
header.ctx = mqtt;
strcpy(cmd.driver, fdr->node);
strcpy(cmd.src_path, fdr->src_path);
strcpy(cmd.dst_path, fdr->dst_path);
strncpy(cmd.driver, fdr->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.src_path, fdr->src_path, NEU_PATH_LEN - 1);
strncpy(cmd.dst_path, fdr->dst_path, NEU_PATH_LEN - 1);
cmd.size = fdr->size;
model__file_download_request__free_unpacked(fdr, NULL);
@@ -2242,9 +2307,9 @@ void handle_driver_fdown_open_req(neu_mqtt_qos_e qos, const char *topic,
return;
}
header.ctx = mqtt;
strcpy(cmd.driver, req->driver);
strcpy(cmd.src_path, req->src_path);
strcpy(cmd.dst_path, req->dst_path);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.src_path, req->src_path, NEU_PATH_LEN - 1);
strncpy(cmd.dst_path, req->dst_path, NEU_PATH_LEN - 1);
cmd.size = req->size;
neu_json_decode_driver_fdown_open_req_free(req);
@@ -2346,8 +2411,8 @@ void handle_driver_fdown_data_req(neu_mqtt_qos_e qos, const char *topic,
mqtt->tracestate = NULL;
header.ctx = mqtt;
strcpy(cmd.driver, fddr->node);
strcpy(cmd.src_path, fddr->path);
strncpy(cmd.driver, fddr->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.src_path, fddr->path, NEU_PATH_LEN - 1);
cmd.more = fddr->more;
cmd.len = fddr->data.len;
cmd.data = calloc(fddr->data.len, sizeof(uint8_t));
@@ -2373,8 +2438,8 @@ void handle_driver_fdown_data_req(neu_mqtt_qos_e qos, const char *topic,
}
header.ctx = mqtt;
strcpy(cmd.driver, req->driver);
strcpy(cmd.src_path, req->src_path);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.src_path, req->src_path, NEU_PATH_LEN - 1);
cmd.more = req->more;
cmd.len = req->len;
cmd.data = calloc(req->len, sizeof(uint8_t));
+2
View File
@@ -167,6 +167,7 @@ int mqtt_plugin_uninit(neu_plugin_t *plugin)
mqtt_config_fini(&plugin->config);
if (plugin->client) {
neu_mqtt_client_remove_cache_db(plugin->client);
neu_mqtt_client_close(plugin->client);
neu_mqtt_client_free(plugin->client);
plugin->client = NULL;
@@ -395,6 +396,7 @@ int mqtt_plugin_config(neu_plugin_t *plugin, const char *setting)
}
if (plugin->client != NULL) {
neu_mqtt_client_remove_cache_db(plugin->client);
neu_mqtt_client_close(plugin->client);
neu_mqtt_client_free(plugin->client);
}
+4 -4
View File
@@ -24,7 +24,7 @@ static int mqtt_schema_parse(json_t *root, mqtt_schema_vt_t **vts,
mqtt_schema_vt_t *vt = &(*vts)[*vts_len - 1];
memset(vt, 0, sizeof(mqtt_schema_vt_t));
strcpy(vt->name, key);
strncpy(vt->name, key, sizeof(vt->name) - 1);
if (strcmp(str_val, "${timestamp}") == 0) {
vt->vt = MQTT_SCHEMA_TIMESTAMP;
@@ -50,7 +50,7 @@ static int mqtt_schema_parse(json_t *root, mqtt_schema_vt_t **vts,
return -1;
} else {
vt->vt = MQTT_SCHEMA_UD;
strcpy(vt->ud, str_val);
strncpy(vt->ud, str_val, sizeof(vt->ud) - 1);
}
}
} else if (json_is_object(value)) {
@@ -63,7 +63,7 @@ static int mqtt_schema_parse(json_t *root, mqtt_schema_vt_t **vts,
mqtt_schema_vt_t *vt = &(*vts)[*vts_len - 1];
memset(vt, 0, sizeof(mqtt_schema_vt_t));
strcpy(vt->name, key);
strncpy(vt->name, key, sizeof(vt->name) - 1);
vt->vt = MQTT_SCHEMA_OBJECT;
return mqtt_schema_parse(value, &vt->sub_vts, &vt->n_sub_vts,
@@ -338,7 +338,7 @@ int mqtt_static_validate(const char *static_tags, mqtt_static_vt_t **vts,
mqtt_static_vt_t *vt = &(*vts)[*vts_len - 1];
memset(vt, 0, sizeof(mqtt_static_vt_t));
strcpy(vt->name, key);
strncpy(vt->name, key, sizeof(vt->name) - 1);
if (json_is_string(value)) {
vt->jtype = NEU_JSON_STR;
+15 -14
View File
@@ -56,8 +56,8 @@ void handle_add_adapter(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_ADD_NODE;
strcpy(cmd.node, req->name);
strcpy(cmd.plugin, req->plugin);
strncpy(cmd.node, req->name, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.plugin, req->plugin, NEU_PLUGIN_NAME_LEN - 1);
cmd.setting = req->setting;
req->setting = NULL; // moved
ret = neu_plugin_op(plugin, header, &cmd);
@@ -96,8 +96,8 @@ static inline int send_node_update_req(nng_aio * aio,
}
neu_req_update_node_t cmd = { 0 };
strcpy(cmd.node, req->name);
strcpy(cmd.new_name, req->new_name);
strncpy(cmd.node, req->name, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.new_name, req->new_name, NEU_NODE_NAME_LEN - 1);
neu_reqresp_head_t header = {
.ctx = aio,
@@ -140,7 +140,7 @@ void handle_del_adapter(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_DEL_NODE;
strcpy(cmd.node, req->name);
strncpy(cmd.node, req->name, NEU_NODE_NAME_LEN - 1);
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
@@ -178,16 +178,16 @@ void handle_get_adapter(nng_aio *aio)
if (neu_http_get_param_str(aio, "plugin", plugin_name,
sizeof(plugin_name)) > 0) {
strcpy(cmd.plugin, plugin_name);
strncpy(cmd.plugin, plugin_name, NEU_PLUGIN_NAME_LEN - 1);
}
if (neu_http_get_param_str(aio, "node", node_name, sizeof(node_name)) > 0) {
strcpy(cmd.node, node_name);
strncpy(cmd.node, node_name, NEU_NODE_NAME_LEN - 1);
}
if (neu_http_get_param_str(aio, "group", group_name, sizeof(group_name)) >
0) {
strcpy(cmd.query.q_group_name, group_name);
strncpy(cmd.query.q_group_name, group_name, NEU_GROUP_NAME_LEN - 1);
}
uintmax_t query_state = 0;
@@ -265,7 +265,7 @@ void handle_set_node_setting(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_NODE_SETTING;
strcpy(cmd.node, req->node);
strncpy(cmd.node, req->node, NEU_NODE_NAME_LEN - 1);
cmd.setting = req->setting;
req->setting = NULL; // ownership moved
ret = neu_plugin_op(plugin, header, &cmd);
@@ -299,7 +299,7 @@ void handle_get_node_setting(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_GET_NODE_SETTING;
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
strcpy(cmd.node, node_name);
strncpy(cmd.node, node_name, NEU_NODE_NAME_LEN - 1);
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
@@ -348,7 +348,7 @@ void handle_node_ctl(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_NODE_CTL;
strcpy(cmd.node, req->node);
strncpy(cmd.node, req->node, NEU_NODE_NAME_LEN - 1);
cmd.ctl = req->cmd;
ret = neu_plugin_op(plugin, header, &cmd);
@@ -379,7 +379,7 @@ void handle_get_node_state(nng_aio *aio)
header.type = NEU_REQ_GET_NODES_STATE;
} else {
header.type = NEU_REQ_GET_NODE_STATE;
strcpy(cmd.node, node_name);
strncpy(cmd.node, node_name, NEU_NODE_NAME_LEN - 1);
}
ret = neu_plugin_op(plugin, header, &cmd);
@@ -453,7 +453,8 @@ static int json_to_gdatatags(neu_json_gtag_array_t *gtag_array,
}
for (int i = 0; i < gtag_array->len; i++) {
strcpy(gdatatags[i].group, gtag_array->gtags[i].group);
strncpy(gdatatags[i].group, gtag_array->gtags[i].group,
NEU_GROUP_NAME_LEN - 1);
gdatatags[i].n_tag = gtag_array->gtags[i].n_tag;
gdatatags[i].interval = gtag_array->gtags[i].interval;
gdatatags[i].tags =
@@ -622,4 +623,4 @@ void handle_put_drivers(nng_aio *aio)
ret, { neu_http_response(aio, ret, result_error); });
}
})
}
}
+3 -1
View File
@@ -872,7 +872,9 @@ void handle_get_server_security_policy_resp(
if (resp->policy_name != NULL && strlen(resp->policy_name) > 0) {
// Split policy names by comma
char *token = strtok(resp->policy_name, ",");
while (token != NULL) {
while (token != NULL &&
policy_count <
(int) (sizeof(policy_names) / sizeof(policy_names[0]))) {
policy_names[policy_count] = strdup(token);
policy_count++;
token = strtok(NULL, ",");
+1 -1
View File
@@ -17,7 +17,7 @@ void handle_cid(nng_aio *aio)
neu_plugin_t *plugin = neu_rest_get_plugin();
(void) plugin;
NEU_PROCESS_HTTP_REQUEST(
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_upload_cid_t, neu_json_decode_upload_cid_req, {
cid_t cid = { 0 };
int parse_re = neu_cid_parse(req->path, &cid);
+13 -12
View File
@@ -76,8 +76,8 @@ void handle_add_tags(nng_aio *aio)
} else {
header.ctx = aio;
header.type = NEU_REQ_ADD_TAG;
strcpy(cmd.driver, req->node);
strcpy(cmd.group, req->group);
strncpy(cmd.driver, req->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
cmd.n_tag = req->n_tag;
cmd.tags = calloc(req->n_tag, sizeof(neu_datatag_t));
@@ -184,12 +184,13 @@ void handle_add_gtags(nng_aio *aio)
if (add_resp.error > 0) {
handle_add_tags_resp(aio, &add_resp);
} else {
strcpy(cmd.driver, req->node);
strncpy(cmd.driver, req->node, NEU_NODE_NAME_LEN - 1);
cmd.n_group = req->n_group;
cmd.groups = calloc(req->n_group, sizeof(neu_gdatatag_t));
for (int i = 0; i < req->n_group; i++) {
strcpy(cmd.groups[i].group, req->groups[i].group);
strncpy(cmd.groups[i].group, req->groups[i].group,
NEU_GROUP_NAME_LEN - 1);
cmd.groups[i].n_tag = req->groups[i].n_tag;
cmd.groups[i].interval = req->groups[i].interval;
cmd.groups[i].tags =
@@ -267,7 +268,7 @@ void handle_import_tags(nng_aio *aio)
header.type = NEU_REQ_IMPORT_TAGS;
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
strcpy(cmd.node, req->node);
strncpy(cmd.node, req->node, NEU_NODE_NAME_LEN - 1);
int ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
@@ -318,8 +319,8 @@ void handle_del_tags(nng_aio *aio)
goto error;
}
strcpy(cmd.driver, req->node);
strcpy(cmd.group, req->group);
strncpy(cmd.driver, req->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
cmd.n_tag = req->n_tags;
for (int i = 0; i < req->n_tags; i++) {
@@ -364,8 +365,8 @@ void handle_update_tags(nng_aio *aio)
header.type = NEU_REQ_UPDATE_TAG;
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
strcpy(cmd.driver, req->node);
strcpy(cmd.group, req->group);
strncpy(cmd.driver, req->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
cmd.n_tag = req->n_tag;
cmd.tags = calloc(req->n_tag, sizeof(neu_datatag_t));
@@ -428,11 +429,11 @@ void handle_get_tags(nng_aio *aio)
}
if (neu_http_get_param_str(aio, "name", tag_name, sizeof(tag_name)) >= 0) {
strcpy(cmd.name, tag_name);
strncpy(cmd.name, tag_name, NEU_TAG_NAME_LEN - 1);
}
strcpy(cmd.driver, node);
strcpy(cmd.group, group);
strncpy(cmd.driver, node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, group, NEU_GROUP_NAME_LEN - 1);
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
+15 -15
View File
@@ -771,8 +771,8 @@ static int get_tags(context_t *ctx, neu_resp_driver_group_info_t *info)
};
neu_req_get_tag_t cmd = { 0 };
strcpy(cmd.driver, info->driver);
strcpy(cmd.group, info->group);
strncpy(cmd.driver, info->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, info->group, NEU_GROUP_NAME_LEN - 1);
if (0 != neu_plugin_op(plugin, header, &cmd)) {
rv = NEU_ERR_IS_BUSY;
@@ -925,7 +925,7 @@ static int get_subscriptions(context_t *ctx, neu_resp_node_info_t *info)
};
neu_req_get_subscribe_group_t cmd = { 0 };
strcpy(cmd.app, info->node);
strncpy(cmd.app, info->node, NEU_NODE_NAME_LEN - 1);
if (0 != neu_plugin_op(plugin, header, &cmd)) {
return NEU_ERR_IS_BUSY;
@@ -1009,7 +1009,7 @@ static int get_setting(context_t *ctx, neu_resp_node_info_t *info)
};
neu_req_get_node_setting_t cmd = { 0 };
strcpy(cmd.node, info->node);
strncpy(cmd.node, info->node, NEU_NODE_NAME_LEN - 1);
if (0 != neu_plugin_op(plugin, header, &cmd)) {
return NEU_ERR_IS_BUSY;
@@ -1089,7 +1089,7 @@ static int del_node(context_t *ctx, neu_resp_node_info_t *info)
};
neu_req_del_node_t cmd = { 0 };
strcpy(cmd.node, info->node);
strncpy(cmd.node, info->node, NEU_NODE_NAME_LEN - 1);
if (0 != neu_plugin_op(plugin, header, &cmd)) {
return NEU_ERR_IS_BUSY;
@@ -1109,8 +1109,8 @@ static int add_node(context_t *ctx, neu_json_get_nodes_resp_node_t *req)
};
neu_req_add_node_t cmd = { 0 };
strcpy(cmd.node, req->name);
strcpy(cmd.plugin, req->plugin);
strncpy(cmd.node, req->name, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.plugin, req->plugin, NEU_PLUGIN_NAME_LEN - 1);
if (0 != neu_plugin_op(plugin, header, &cmd)) {
return NEU_ERR_IS_BUSY;
@@ -1131,8 +1131,8 @@ static int add_group(context_t * ctx,
};
neu_req_add_group_t cmd = { 0 };
strcpy(cmd.driver, data->driver);
strcpy(cmd.group, data->group);
strncpy(cmd.driver, data->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, data->group, NEU_GROUP_NAME_LEN - 1);
cmd.interval = data->interval;
if (0 != neu_plugin_op(plugin, header, &cmd)) {
@@ -1154,8 +1154,8 @@ static int add_tag(context_t *ctx, neu_json_add_tags_req_t *data)
};
neu_req_add_tag_t cmd = { 0 };
strcpy(cmd.driver, data->node);
strcpy(cmd.group, data->group);
strncpy(cmd.driver, data->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, data->group, NEU_GROUP_NAME_LEN - 1);
cmd.n_tag = data->n_tag;
cmd.tags = calloc(data->n_tag, sizeof(neu_datatag_t));
if (NULL == cmd.tags) {
@@ -1207,9 +1207,9 @@ static int add_subscription(context_t *ctx, neu_json_subscribe_req_t *data)
};
neu_req_subscribe_t cmd = { 0 };
strcpy(cmd.app, data->app);
strcpy(cmd.driver, data->driver);
strcpy(cmd.group, data->group);
strncpy(cmd.app, data->app, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.driver, data->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, data->group, NEU_GROUP_NAME_LEN - 1);
cmd.params = data->params; // ownership moved
data->params = NULL;
@@ -1231,7 +1231,7 @@ static int add_setting(context_t *ctx, neu_json_node_setting_req_t *data)
};
neu_req_node_setting_t cmd = { 0 };
strcpy(cmd.node, data->node);
strncpy(cmd.node, data->node, NEU_NODE_NAME_LEN - 1);
cmd.setting = data->setting; // ownership moved
data->setting = NULL;
+16 -16
View File
@@ -48,8 +48,8 @@ void handle_add_group_config(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_ADD_GROUP;
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
strcpy(cmd.driver, req->node);
strcpy(cmd.group, req->group);
strncpy(cmd.driver, req->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
cmd.interval = req->interval;
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
@@ -92,14 +92,14 @@ static inline int send_update_group(nng_aio * aio,
} else if (len >= NEU_GROUP_NAME_LEN) {
return NEU_ERR_GROUP_NAME_TOO_LONG;
}
strcpy(cmd.new_name, req->new_name);
strncpy(cmd.new_name, req->new_name, NEU_GROUP_NAME_LEN - 1);
} else {
// if `new_name` is omitted, then keep the node name
strcpy(cmd.new_name, req->group);
strncpy(cmd.new_name, req->group, NEU_GROUP_NAME_LEN - 1);
}
strcpy(cmd.driver, req->node);
strcpy(cmd.group, req->group);
strncpy(cmd.driver, req->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
cmd.interval = req->set_interval ? req->interval : 0;
neu_reqresp_head_t header = {
@@ -146,8 +146,8 @@ void handle_del_group_config(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_DEL_GROUP;
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
strcpy(cmd.driver, req->node);
strcpy(cmd.group, req->group);
strncpy(cmd.driver, req->node, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
@@ -174,14 +174,14 @@ void handle_get_group_config(nng_aio *aio)
NEU_VALIDATE_JWT(aio);
neu_http_get_param_str(aio, "node", node_name, sizeof(node_name));
strcpy(cmd.driver, node_name);
strncpy(cmd.driver, node_name, NEU_NODE_NAME_LEN - 1);
if (strlen(cmd.driver) == 0) {
header.type = NEU_REQ_GET_DRIVER_GROUP;
}
if (neu_http_get_param_str(aio, "group", group_name, sizeof(group_name)) >
0) {
strcpy(cmd.q_group, group_name);
strncpy(cmd.q_group, group_name, NEU_GROUP_NAME_LEN - 1);
}
ret = neu_plugin_op(plugin, header, &cmd);
@@ -273,9 +273,9 @@ static inline int send_subscribe(nng_aio *aio, neu_reqresp_type_e type,
}
neu_req_subscribe_t cmd = { 0 };
strcpy(cmd.app, req->app);
strcpy(cmd.driver, req->driver);
strcpy(cmd.group, req->group);
strncpy(cmd.app, req->app, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
cmd.params = req->params; // ownership moved
cmd.static_tags = req->static_tags; // ownership moved
req->params = NULL;
@@ -340,9 +340,9 @@ void handle_grp_unsubscribe(nng_aio *aio)
goto error;
}
strcpy(cmd.app, req->app);
strcpy(cmd.driver, req->driver);
strcpy(cmd.group, req->group);
strncpy(cmd.app, req->app, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
-28
View File
@@ -38,7 +38,6 @@
#include "plugin_handle.h"
#include "rw_handle.h"
#include "scan_handle.h"
#include "system_handle.h"
#include "tpy_handle.h"
#include "utils/http.h"
#include "version_handle.h"
@@ -121,12 +120,6 @@ static struct neu_http_handler cors_handler[] = {
{
.url = "/api/v2/log/level",
},
{
.url = "/api/v2/log/list",
},
{
.url = "/api/v2/log/file",
},
{
.url = "/api/v2/global/config",
},
@@ -148,9 +141,6 @@ static struct neu_http_handler cors_handler[] = {
{
.url = "/api/v2/tpy",
},
{
.url = "/api/v2/system/ctl",
},
{
.url = "/api/v2/users",
},
@@ -464,18 +454,6 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/log/global",
.value.handler = handle_get_global_log_level,
},
{
.method = NEU_HTTP_METHOD_GET,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/log/list",
.value.handler = handle_log_list,
},
{
.method = NEU_HTTP_METHOD_GET,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/log/file",
.value.handler = handle_log_file,
},
{
.method = NEU_HTTP_METHOD_GET,
.type = NEU_HTTP_HANDLER_FUNCTION,
@@ -536,12 +514,6 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/otel",
.value.handler = handle_otel_get,
},
{
.method = NEU_HTTP_METHOD_POST,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/system/ctl",
.value.handler = handle_system_ctl,
},
{
.method = NEU_HTTP_METHOD_GET,
.type = NEU_HTTP_HANDLER_FUNCTION,
+2 -119
View File
@@ -90,7 +90,8 @@ void handle_log_level(nng_aio *aio)
cmd.core = req->core;
cmd.log_level = log_level;
if (req->node_name != NULL) {
strcpy(cmd.node, req->node_name);
strncpy(cmd.node, req->node_name,
NEU_NODE_NAME_LEN - 1);
}
int ret = neu_plugin_op(plugin, header, &cmd);
@@ -194,121 +195,3 @@ void handle_get_global_log_level(nng_aio *aio)
neu_http_ok(aio, json_response);
}
void handle_log_list(nng_aio *aio)
{
NEU_VALIDATE_JWT(aio);
char log_dir[PATH_MAX] = { 0 };
getcwd(log_dir, PATH_MAX - 1);
strncat(log_dir, "/", sizeof(log_dir) - strlen(log_dir) - 1);
strncat(log_dir, LOG_DIR, sizeof(log_dir) - strlen(log_dir) - 1);
DIR *dir = opendir(log_dir);
if (NULL == dir) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_FILE_NOT_EXIST, {
neu_http_response(aio, NEU_ERR_FILE_NOT_EXIST, result_error);
});
}
UT_icd icd = { sizeof(neu_resp_log_file_t), NULL, NULL, NULL };
UT_array *log_files = NULL;
utarray_new(log_files, &icd);
struct dirent *entry = NULL;
while (NULL != (entry = readdir(dir))) {
if (NULL == strstr(entry->d_name, ".log")) {
continue;
}
char full_path[PATH_MAX] = { 0 };
strncpy(full_path, log_dir, sizeof(full_path) - 1);
strncat(full_path, "/", sizeof(full_path) - strlen(full_path) - 1);
strncat(full_path, entry->d_name,
sizeof(full_path) - strlen(full_path) - 1);
struct stat st = { 0 };
if (0 != stat(full_path, &st)) {
continue;
}
neu_resp_log_file_t log_file = { 0 };
strncpy(log_file.file, entry->d_name, sizeof(log_file.file) - 1);
log_file.size = st.st_size;
utarray_push_back(log_files, &log_file);
}
closedir(dir);
char *result = NULL;
neu_json_encode_by_fn(log_files, neu_json_encode_log_list_resp, &result);
neu_http_ok(aio, result);
free(result);
utarray_free(log_files);
}
static int response(nng_aio *aio, char *content, enum nng_http_status status)
{
nng_http_res *res = NULL;
nng_http_res_alloc(&res);
nng_http_res_set_header(res, "Content-Type", "text/plain");
nng_http_res_set_header(res, "Access-Control-Allow-Origin", "*");
nng_http_res_set_header(res, "Access-Control-Allow-Methods",
"POST,GET,PUT,DELETE,OPTIONS");
nng_http_res_set_header(res, "Access-Control-Allow-Headers", "*");
if (content != NULL && strlen(content) > 0) {
nng_http_res_copy_data(res, content, strlen(content));
}
nng_http_res_set_status(res, status);
nng_http_req *nng_req = nng_aio_get_input(aio, 0);
nlog_notice("<%p> %s %s [%d]", aio, nng_http_req_get_method(nng_req),
nng_http_req_get_uri(nng_req), status);
nng_aio_set_output(aio, 0, res);
nng_aio_finish(aio, 0);
return 0;
}
void handle_log_file(nng_aio *aio)
{
NEU_VALIDATE_JWT(aio);
char file_name[NEU_NODE_NAME_LEN] = { 0 };
if (neu_http_get_param_str(aio, "name", file_name, sizeof(file_name)) <=
0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, {
neu_http_response(aio, error_code.error, result_error);
})
return;
}
char path[PATH_MAX] = { 0 };
getcwd(path, PATH_MAX - 1);
strncat(path, "/", sizeof(path) - strlen(path) - 1);
strncat(path, LOG_DIR, sizeof(path) - strlen(path) - 1);
strncat(path, "/", sizeof(path) - strlen(path) - 1);
strncat(path, file_name, sizeof(path) - strlen(path) - 1);
FILE *file = fopen(path, "r");
if (NULL == file) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_FILE_NOT_EXIST, {
neu_http_response(aio, NEU_ERR_FILE_NOT_EXIST, result_error);
});
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char *file_content = (char *) malloc(file_size + 1);
fread(file_content, 1, file_size, file);
file_content[file_size] = '\0';
fclose(file);
response(aio, file_content, NNG_HTTP_STATUS_OK);
free(file_content);
}
-3
View File
@@ -22,11 +22,8 @@
#include <nng/nng.h>
// void handle_get_log(nng_aio *aio);
void handle_log_level(nng_aio *aio);
void handle_global_log_level(nng_aio *aio);
void handle_get_global_log_level(nng_aio *aio);
void handle_log_list(nng_aio *aio);
void handle_log_file(nng_aio *aio);
#endif
+13 -2
View File
@@ -52,6 +52,16 @@ void handle_ping(nng_aio *aio)
void handle_login(nng_aio *aio)
{
if (disable_jwt) {
neu_json_login_resp_t login_resp = { .token = "" };
char * result = NULL;
neu_json_encode_by_fn(&login_resp, neu_json_encode_login_resp, &result);
neu_http_ok(aio, result);
free(result);
return;
}
NEU_PROCESS_HTTP_REQUEST(
aio, neu_json_login_req_t, neu_json_decode_login_req, {
neu_json_login_resp_t login_resp = { 0 };
@@ -114,7 +124,7 @@ void handle_get_user(nng_aio *aio)
utarray_foreach(user_infos, neu_persist_user_info_t *, p_info)
{
neu_json_user_resp_t resp = { 0 };
strcpy(resp.name, p_info->name);
strncpy(resp.name, p_info->name, NEU_USER_NAME_MAX_LEN);
utarray_push_back(user_list, &resp);
}
utarray_free(user_infos);
@@ -547,7 +557,7 @@ void handle_get_plugin_schema(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_CHECK_SCHEMA;
strcpy(cmd.schema, schema_name);
strncpy(cmd.schema, schema_name, NEU_PLUGIN_NAME_LEN - 1);
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
@@ -809,6 +819,7 @@ done:
void handle_export_db(nng_aio *aio)
{
NEU_VALIDATE_JWT(aio);
write_csv_header();
UT_array *nodes = NULL;
+3 -3
View File
@@ -41,7 +41,7 @@ void handle_add_plugin(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_ADD_PLUGIN;
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
strncpy(cmd.library, req->library, NEU_PLUGIN_LIBRARY_LEN);
strncpy(cmd.library, req->library, NEU_PLUGIN_LIBRARY_LEN - 1);
cmd.schema_file = req->schema_file;
cmd.so_file = req->so_file;
ret = neu_plugin_op(plugin, header, &cmd);
@@ -66,7 +66,7 @@ void handle_update_plugin(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_UPDATE_PLUGIN;
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
strncpy(cmd.library, req->library, NEU_PLUGIN_LIBRARY_LEN);
strncpy(cmd.library, req->library, NEU_PLUGIN_LIBRARY_LEN - 1);
cmd.schema_file = req->schema_file;
cmd.so_file = req->so_file;
ret = neu_plugin_op(plugin, header, &cmd);
@@ -91,7 +91,7 @@ void handle_del_plugin(nng_aio *aio)
header.type = NEU_REQ_DEL_PLUGIN;
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
if (strlen(req->plugin) <= sizeof(cmd.plugin)) {
strcpy(cmd.plugin, req->plugin);
strncpy(cmd.plugin, req->plugin, NEU_PLUGIN_NAME_LEN - 1);
int ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
+30 -22
View File
@@ -171,10 +171,10 @@ void handle_test_read_tag(nng_aio *aio)
goto error;
}
strcpy(cmd.driver, req->driver);
strcpy(cmd.group, req->group);
strcpy(cmd.tag, req->tag);
strcpy(cmd.address, req->address);
strncpy(cmd.driver, req->driver, NEU_NODE_NAME_LEN - 1);
strncpy(cmd.group, req->group, NEU_GROUP_NAME_LEN - 1);
strncpy(cmd.tag, req->tag, NEU_TAG_NAME_LEN - 1);
strncpy(cmd.address, req->address, NEU_TAG_ADDRESS_LEN - 1);
cmd.attribute = req->attribute;
cmd.type = req->type;
cmd.precision = req->precision;
@@ -316,7 +316,8 @@ void handle_write(nng_aio *aio)
break;
case NEU_JSON_STR:
cmd.value.type = NEU_TYPE_STRING;
strcpy(cmd.value.value.str, req->value.val_str);
strncpy(cmd.value.value.str, req->value.val_str,
NEU_VALUE_SIZE);
break;
case NEU_JSON_DOUBLE:
cmd.value.type = NEU_TYPE_DOUBLE;
@@ -364,8 +365,8 @@ void handle_write(nng_aio *aio)
cmd.value.value.strs.strs =
calloc(req->value.val_array_str.length, sizeof(char *));
for (size_t i = 0; i < req->value.val_array_str.length; i++) {
cmd.value.value.strs.strs[i] =
strdup(req->value.val_array_str.p_strs[i]);
const char *s = req->value.val_array_str.p_strs[i];
cmd.value.value.strs.strs[i] = strdup(s ? s : "");
}
break;
case NEU_JSON_OBJECT:
@@ -522,7 +523,8 @@ void handle_write_tags(nng_aio *aio)
req->group = NULL; // ownership moved
for (int i = 0; i < cmd.n_tag; i++) {
strcpy(cmd.tags[i].tag, req->tags[i].tag);
strncpy(cmd.tags[i].tag, req->tags[i].tag,
NEU_TAG_NAME_LEN - 1);
switch (req->tags[i].t) {
case NEU_JSON_INT:
cmd.tags[i].value.type = NEU_TYPE_INT64;
@@ -530,8 +532,8 @@ void handle_write_tags(nng_aio *aio)
break;
case NEU_JSON_STR:
cmd.tags[i].value.type = NEU_TYPE_STRING;
strcpy(cmd.tags[i].value.value.str,
req->tags[i].value.val_str);
strncpy(cmd.tags[i].value.value.str,
req->tags[i].value.val_str, NEU_VALUE_SIZE);
break;
case NEU_JSON_DOUBLE:
cmd.tags[i].value.type = NEU_TYPE_DOUBLE;
@@ -586,8 +588,10 @@ void handle_write_tags(nng_aio *aio)
sizeof(char *));
for (size_t k = 0;
k < req->tags[i].value.val_array_str.length; k++) {
const char *s =
req->tags[i].value.val_array_str.p_strs[k];
cmd.tags[i].value.value.strs.strs[k] =
strdup(req->tags[i].value.val_array_str.p_strs[k]);
strdup(s ? s : "");
}
break;
case NEU_JSON_OBJECT:
@@ -650,7 +654,8 @@ static void trans(neu_json_write_gtags_req_t *req, neu_req_write_gtags_t *cmd)
calloc(cmd->groups[i].n_tag, sizeof(neu_resp_tag_value_t));
for (int k = 0; k < cmd->groups[i].n_tag; k++) {
strcpy(cmd->groups[i].tags[k].tag, req->groups[i].tags[k].tag);
strncpy(cmd->groups[i].tags[k].tag, req->groups[i].tags[k].tag,
NEU_TAG_NAME_LEN - 1);
switch (req->groups[i].tags[k].t) {
case NEU_JSON_INT:
@@ -660,8 +665,8 @@ static void trans(neu_json_write_gtags_req_t *req, neu_req_write_gtags_t *cmd)
break;
case NEU_JSON_STR:
cmd->groups[i].tags[k].value.type = NEU_TYPE_STRING;
strcpy(cmd->groups[i].tags[k].value.value.str,
req->groups[i].tags[k].value.val_str);
strncpy(cmd->groups[i].tags[k].value.value.str,
req->groups[i].tags[k].value.val_str, NEU_VALUE_SIZE);
break;
case NEU_JSON_DOUBLE:
cmd->groups[i].tags[k].value.type = NEU_TYPE_DOUBLE;
@@ -679,8 +684,8 @@ static void trans(neu_json_write_gtags_req_t *req, neu_req_write_gtags_t *cmd)
req->groups[i].tags[k].value.val_array_int64.length;
cmd->groups[i].tags[k].value.value.i64s.i64s =
calloc(sizeof(int64_t),
req->groups[i].tags[k].value.val_array_int64.length);
calloc(req->groups[i].tags[k].value.val_array_int64.length,
sizeof(int64_t));
memcpy(cmd->groups[i].tags[k].value.value.i64s.i64s,
req->groups[i].tags[k].value.val_array_int64.i64s,
@@ -694,8 +699,9 @@ static void trans(neu_json_write_gtags_req_t *req, neu_req_write_gtags_t *cmd)
req->groups[i].tags[k].value.val_array_double.length;
cmd->groups[i].tags[k].value.value.f64s.f64s = calloc(
sizeof(double),
req->groups[i].tags[k].value.val_array_double.length);
req->groups[i].tags[k].value.val_array_double.length,
sizeof(double));
memcpy(
cmd->groups[i].tags[k].value.value.f64s.f64s,
req->groups[i].tags[k].value.val_array_double.f64s,
@@ -723,13 +729,15 @@ static void trans(neu_json_write_gtags_req_t *req, neu_req_write_gtags_t *cmd)
req->groups[i].tags[k].value.val_array_str.length;
cmd->groups[i].tags[k].value.value.strs.strs =
calloc(sizeof(char *),
req->groups[i].tags[k].value.val_array_str.length);
calloc(req->groups[i].tags[k].value.val_array_str.length,
sizeof(char *));
for (size_t j = 0;
j < req->groups[i].tags[k].value.val_array_str.length;
j++) {
cmd->groups[i].tags[k].value.value.strs.strs[j] = strdup(
req->groups[i].tags[k].value.val_array_str.p_strs[j]);
const char *s =
req->groups[i].tags[k].value.val_array_str.p_strs[j];
cmd->groups[i].tags[k].value.value.strs.strs[j] =
strdup(s ? s : "");
}
break;
+3 -3
View File
@@ -43,15 +43,15 @@ void handle_scan_tags(nng_aio *aio)
}
if (NULL != req->node) {
strcpy(cmd.driver, req->node);
strncpy(cmd.driver, req->node, NEU_NODE_NAME_LEN - 1);
}
if (NULL != req->id) {
strcpy(cmd.id, req->id);
strncpy(cmd.id, req->id, NEU_TAG_ADDRESS_LEN - 1);
}
if (NULL != req->ctx) {
strcpy(cmd.ctx, req->ctx);
strncpy(cmd.ctx, req->ctx, NEU_VALUE_SIZE);
}
if (0 != neu_plugin_op(plugin, header, &cmd)) {
-32
View File
@@ -1,32 +0,0 @@
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <jwt.h>
#include <libgen.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "json/neu_json_fn.h"
#include "handle.h"
#include "utils/http.h"
#include "utils/neu_jwt.h"
#include "parser/neu_json_system.h"
#include "system_handle.h"
void handle_system_ctl(nng_aio *aio)
{
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_system_ctl_req_t, neu_json_decode_system_ctl_req, {
if (req->action == 1) {
neu_http_ok(aio, "{\"error\": 0 }");
exit(-2);
}
})
}
-27
View File
@@ -1,27 +0,0 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#ifndef _NEU_SYSTEM_HANDLE_H_
#define _NEU_SYSTEM_HANDLE_H_
#include <nng/nng.h>
void handle_system_ctl(nng_aio *aio);
#endif
+1 -1
View File
@@ -17,7 +17,7 @@ void handle_tpy(nng_aio *aio)
neu_plugin_t *plugin = neu_rest_get_plugin();
(void) plugin;
NEU_PROCESS_HTTP_REQUEST(
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_upload_cid_t, neu_json_decode_upload_cid_req, {
tpy_t tpy = { 0 };
int parse_re = neu_tpy_parse(req->path, &tpy);
+109 -14
View File
@@ -71,8 +71,8 @@ inline static tkey_t to_key(const char *group, const char *tag)
{
tkey_t key = { 0 };
strcpy(key.group, group);
strcpy(key.tag, tag);
strncpy(key.group, group, sizeof(key.group) - 1);
strncpy(key.tag, tag, sizeof(key.tag) - 1);
return key;
}
@@ -160,8 +160,8 @@ void neu_driver_cache_add(neu_driver_cache_t *cache, const char *group,
if (elem == NULL) {
elem = calloc(1, sizeof(struct elem));
strcpy(elem->key.group, group);
strcpy(elem->key.tag, tag);
snprintf(elem->key.group, sizeof(elem->key.group), "%s", group);
snprintf(elem->key.tag, sizeof(elem->key.tag), "%s", tag);
HASH_ADD(hh, cache->table, key, sizeof(tkey_t), elem);
}
@@ -179,7 +179,7 @@ void neu_driver_cache_update_trace(neu_driver_cache_t *cache, const char *group,
group_trace_t *elem = NULL;
char key[NEU_GROUP_NAME_LEN] = { 0 };
strcpy(key, group);
snprintf(key, sizeof(key), "%s", group);
pthread_mutex_lock(&cache->mtx);
HASH_FIND(hh, cache->trace_table, &key, sizeof(key), elem);
@@ -187,7 +187,7 @@ void neu_driver_cache_update_trace(neu_driver_cache_t *cache, const char *group,
if (elem == NULL) {
elem = calloc(1, sizeof(group_trace_t));
strcpy(elem->key, group);
snprintf(elem->key, sizeof(elem->key), "%s", group);
elem->trace_ctx = trace_ctx;
HASH_ADD(hh, cache->trace_table, key, sizeof(key), elem);
@@ -205,7 +205,7 @@ void *neu_driver_cache_get_trace(neu_driver_cache_t *cache, const char *group)
void *trace = NULL;
strcpy(key, group);
snprintf(key, sizeof(key), "%s", group);
pthread_mutex_lock(&cache->mtx);
HASH_FIND(hh, cache->trace_table, &key, sizeof(key), elem);
@@ -219,14 +219,15 @@ void *neu_driver_cache_get_trace(neu_driver_cache_t *cache, const char *group)
return trace;
}
void neu_driver_cache_update_change(neu_driver_cache_t *cache,
bool neu_driver_cache_update_change(neu_driver_cache_t *cache,
const char *group, const char *tag,
int64_t timestamp, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta,
bool change)
{
struct elem *elem = NULL;
tkey_t key = to_key(group, tag);
struct elem *elem = NULL;
tkey_t key = to_key(group, tag);
bool tag_changed = false;
pthread_mutex_lock(&cache->mtx);
HASH_FIND(hh, cache->table, &key, sizeof(tkey_t), elem);
@@ -241,6 +242,7 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
(sub_filter_err && elem->value.type != value.type &&
elem->value.type != NEU_TYPE_ERROR)) {
elem->changed = true;
tag_changed = true;
} else if (sub_filter_err && elem->value.type != value.type &&
elem->value.type == NEU_TYPE_ERROR) {
switch (value.type) {
@@ -264,17 +266,20 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (memcmp(&elem->value_old.value, &value.value,
sizeof(value.value)) != 0) {
elem->changed = true;
tag_changed = true;
}
break;
case NEU_TYPE_BYTES:
if (elem->value_old.value.bytes.length !=
value.value.bytes.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcpy(elem->value_old.value.bytes.bytes,
if (memcmp(elem->value_old.value.bytes.bytes,
value.value.bytes.bytes,
value.value.bytes.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -282,11 +287,13 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.bools.length !=
value.value.bools.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.bools.bools,
value.value.bools.bools,
value.value.bools.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -294,11 +301,13 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.i8s.length !=
value.value.i8s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.i8s.i8s,
value.value.i8s.i8s,
value.value.i8s.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -306,11 +315,13 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.u8s.length !=
value.value.u8s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.u8s.u8s,
value.value.u8s.u8s,
value.value.u8s.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -318,12 +329,14 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.i16s.length !=
value.value.i16s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.i16s.i16s,
value.value.i16s.i16s,
value.value.i16s.length * sizeof(int16_t)) !=
0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -331,12 +344,14 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.u16s.length !=
value.value.u16s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.u16s.u16s,
value.value.u16s.u16s,
value.value.u16s.length * sizeof(uint16_t)) !=
0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -344,12 +359,14 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.i32s.length !=
value.value.i32s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.i32s.i32s,
value.value.i32s.i32s,
value.value.i32s.length * sizeof(int32_t)) !=
0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -357,12 +374,14 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.u32s.length !=
value.value.u32s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.u32s.u32s,
value.value.u32s.u32s,
value.value.u32s.length * sizeof(uint32_t)) !=
0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -370,12 +389,14 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.i64s.length !=
value.value.i64s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.i64s.i64s,
value.value.i64s.i64s,
value.value.i64s.length * sizeof(int64_t)) !=
0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -383,12 +404,14 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.u64s.length !=
value.value.u64s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.u64s.u64s,
value.value.u64s.u64s,
value.value.u64s.length * sizeof(uint64_t)) !=
0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -396,11 +419,13 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.f32s.length !=
value.value.f32s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.f32s.f32s,
value.value.f32s.f32s,
value.value.f32s.length * sizeof(float)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -408,11 +433,13 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.f64s.length !=
value.value.f64s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.f64s.f64s,
value.value.f64s.f64s,
value.value.f64s.length * sizeof(double)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -420,18 +447,21 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.strs.length !=
value.value.strs.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.strs.strs,
value.value.strs.strs,
value.value.strs.length * sizeof(char *)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_CUSTOM: {
if (json_equal(elem->value_old.value.json, value.value.json) !=
0) {
1) {
elem->changed = true;
tag_changed = true;
}
break;
}
@@ -439,11 +469,13 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.value.ptr.length !=
value.value.ptr.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value_old.value.ptr.ptr,
value.value.ptr.ptr,
value.value.ptr.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
@@ -453,10 +485,12 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.precision == 0) {
elem->changed =
elem->value_old.value.f32 != value.value.f32;
tag_changed = elem->changed;
} else {
if (fabs(elem->value_old.value.f32 - value.value.f32) >
pow(0.1, elem->value_old.precision)) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -464,10 +498,12 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value_old.precision == 0) {
elem->changed =
elem->value_old.value.d64 != value.value.d64;
tag_changed = elem->changed;
} else {
if (fabs(elem->value_old.value.d64 - value.value.d64) >
pow(0.1, elem->value_old.precision)) {
elem->changed = true;
tag_changed = true;
}
}
@@ -497,17 +533,20 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (memcmp(&elem->value.value, &value.value,
sizeof(value.value)) != 0) {
elem->changed = true;
tag_changed = true;
}
break;
case NEU_TYPE_BYTES:
if (elem->value.value.bytes.length !=
value.value.bytes.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value.value.bytes.bytes,
value.value.bytes.bytes,
value.value.bytes.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
@@ -515,174 +554,206 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
if (elem->value.value.bools.length !=
value.value.bools.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value.value.bools.bools,
value.value.bools.bools,
value.value.bools.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_INT8:
if (elem->value.value.i8s.length != value.value.i8s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value.value.i8s.i8s, value.value.i8s.i8s,
value.value.i8s.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_UINT8:
if (elem->value.value.u8s.length != value.value.u8s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value.value.u8s.u8s, value.value.u8s.u8s,
value.value.u8s.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_INT16:
if (elem->value.value.i16s.length != value.value.i16s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(
elem->value.value.i16s.i16s, value.value.i16s.i16s,
value.value.i16s.length * sizeof(int16_t)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_UINT16:
if (elem->value.value.u16s.length != value.value.u16s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(
elem->value.value.u16s.u16s, value.value.u16s.u16s,
value.value.u16s.length * sizeof(uint16_t)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_INT32:
if (elem->value.value.i32s.length != value.value.i32s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(
elem->value.value.i32s.i32s, value.value.i32s.i32s,
value.value.i32s.length * sizeof(int32_t)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_UINT32:
if (elem->value.value.u32s.length != value.value.u32s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(
elem->value.value.u32s.u32s, value.value.u32s.u32s,
value.value.u32s.length * sizeof(uint32_t)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_INT64:
if (elem->value.value.i64s.length != value.value.i64s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(
elem->value.value.i64s.i64s, value.value.i64s.i64s,
value.value.i64s.length * sizeof(int64_t)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_UINT64:
if (elem->value.value.u64s.length != value.value.u64s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(
elem->value.value.u64s.u64s, value.value.u64s.u64s,
value.value.u64s.length * sizeof(uint64_t)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_FLOAT:
if (elem->value.value.f32s.length != value.value.f32s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value.value.f32s.f32s,
value.value.f32s.f32s,
value.value.f32s.length * sizeof(float)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_DOUBLE:
if (elem->value.value.f64s.length != value.value.f64s.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value.value.f64s.f64s,
value.value.f64s.f64s,
value.value.f64s.length * sizeof(double)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ARRAY_STRING:
if (elem->value.value.strs.length != value.value.strs.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value.value.strs.strs,
value.value.strs.strs,
value.value.strs.length * sizeof(char *)) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_PTR: {
if (elem->value.value.ptr.length != value.value.ptr.length) {
elem->changed = true;
tag_changed = true;
} else {
if (memcmp(elem->value.value.ptr.ptr, value.value.ptr.ptr,
value.value.ptr.length) != 0) {
elem->changed = true;
tag_changed = true;
}
}
break;
}
case NEU_TYPE_CUSTOM: {
if (json_equal(elem->value.value.json, value.value.json) != 0) {
if (json_equal(elem->value.value.json, value.value.json) != 1) {
elem->changed = true;
tag_changed = true;
}
break;
}
case NEU_TYPE_FLOAT:
if (elem->value.precision == 0) {
elem->changed = elem->value.value.f32 != value.value.f32;
tag_changed = elem->changed;
} else {
if (fabs(elem->value.value.f32 - value.value.f32) >
pow(0.1, elem->value.precision)) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_DOUBLE:
if (elem->value.precision == 0) {
elem->changed = elem->value.value.d64 != value.value.d64;
tag_changed = elem->changed;
} else {
if (fabs(elem->value.value.d64 - value.value.d64) >
pow(0.1, elem->value.precision)) {
elem->changed = true;
tag_changed = true;
}
}
break;
case NEU_TYPE_ERROR:
elem->changed = true;
tag_changed = true;
break;
}
}
@@ -778,7 +849,12 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
}
elem->metas = calloc(n_meta, sizeof(neu_tag_meta_t));
for (int i = 0; i < n_meta; i++) {
memcpy(&elem->metas[i], &metas[i], sizeof(neu_tag_meta_t));
if (!neu_dvalue_is_array(&metas[i].value)) {
memcpy(&elem->metas[i], &metas[i], sizeof(neu_tag_meta_t));
} else {
neu_free_dvalue(&metas[i].value);
elem->metas[i].value.type = NEU_TYPE_INT8;
}
}
elem->n_meta = n_meta;
@@ -786,6 +862,7 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
}
pthread_mutex_unlock(&cache->mtx);
return tag_changed;
}
void neu_driver_cache_update(neu_driver_cache_t *cache, const char *group,
@@ -1189,3 +1266,21 @@ void neu_driver_cache_del(neu_driver_cache_t *cache, const char *group,
pthread_mutex_unlock(&cache->mtx);
}
void neu_driver_cache_rename(neu_driver_cache_t *cache, const char *group,
const char *old_tag, const char *new_tag)
{
struct elem *elem = NULL;
tkey_t key = to_key(group, old_tag);
pthread_mutex_lock(&cache->mtx);
HASH_FIND(hh, cache->table, &key, sizeof(tkey_t), elem);
if (elem != NULL) {
HASH_DEL(cache->table, elem);
elem->key = to_key(group, new_tag);
HASH_ADD(hh, cache->table, key, sizeof(tkey_t), elem);
}
pthread_mutex_unlock(&cache->mtx);
}
+1 -1
View File
@@ -35,7 +35,7 @@ void neu_driver_cache_update(neu_driver_cache_t *cache, const char *group,
const char *tag, int64_t timestamp,
neu_dvalue_t value, neu_tag_meta_t *metas,
int n_meta);
void neu_driver_cache_update_change(neu_driver_cache_t *cache,
bool neu_driver_cache_update_change(neu_driver_cache_t *cache,
const char *group, const char *tag,
int64_t timestamp, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta,
+129
View File
@@ -113,6 +113,9 @@ static void update(neu_adapter_t *adapter, const char *group, const char *tag,
static void update_im(neu_adapter_t *adapter, const char *group,
const char *tag, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta);
static void update_im_f_m(neu_adapter_t *adapter, const char *group,
const char *tag, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta);
static void update_with_meta(neu_adapter_t *adapter, const char *group,
const char *tag, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta);
@@ -439,6 +442,107 @@ static void update_with_trace(neu_adapter_t *adapter, const char *group,
}
}
static void update_im_f_m(neu_adapter_t *adapter, const char *group,
const char *tag, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta)
{
neu_adapter_driver_t *driver = (neu_adapter_driver_t *) adapter;
if (tag == NULL) {
nlog_warn("update_im_f_m tag is null");
return;
}
group_t *find = NULL;
HASH_FIND_STR(driver->groups, group, find);
if (find == NULL) {
nlog_error(
"update_im_f_m group not found, driver: %s, group: %s, tag: %s",
driver->adapter.name, group, tag);
return;
}
bool changed = neu_driver_cache_update_change(driver->cache, group, tag,
global_timestamp, value,
metas, n_meta, false);
driver->adapter.cb_funs.update_metric(&driver->adapter,
NEU_METRIC_TAG_READS_TOTAL, 1, NULL);
if (value.type == NEU_TYPE_ERROR) {
return;
}
if (!changed) {
return;
}
UT_array *tags = neu_adapter_driver_get_ptag(driver, group, tag);
if (tags == NULL) {
return;
}
neu_datatag_t *first = utarray_front(tags);
if (first == NULL) {
utarray_free(tags);
nlog_debug("update immediately, driver: %s, "
"group: %s, tag: %s, type: %s, "
"timestamp: %" PRId64,
driver->adapter.name, group, tag,
neu_type_string(value.type), global_timestamp);
return;
}
nlog_debug("update and report immediately, driver: %s, "
"group: %s, tag: %s, type: %s, "
"timestamp: %" PRId64,
driver->adapter.name, group, tag, neu_type_string(value.type),
global_timestamp);
neu_reqresp_head_t header = {
.type = NEU_REQRESP_TRANS_DATA,
};
neu_reqresp_trans_data_t *data =
calloc(1, sizeof(neu_reqresp_trans_data_t));
data->driver = strdup(driver->adapter.name);
data->group = strdup(group);
utarray_new(data->tags, neu_resp_tag_value_meta_icd());
read_report_group(global_timestamp, 0,
neu_adapter_get_tag_cache_type(&driver->adapter),
driver->cache, group, tags, data->tags);
if (utarray_len(data->tags) > 0) {
data->ctx = calloc(1, sizeof(neu_reqresp_trans_data_ctx_t));
data->ctx->index = utarray_len(find->apps);
pthread_mutex_init(&data->ctx->mtx, NULL);
if (utarray_len(find->apps) > 0) {
pthread_mutex_lock(&find->apps_mtx);
utarray_foreach(find->apps, sub_app_t *, app)
{
if (driver->adapter.cb_funs.responseto(
&driver->adapter, &header, data, app->addr) != 0) {
neu_trans_data_free(data);
}
}
pthread_mutex_unlock(&find->apps_mtx);
} else {
neu_trans_data_free(data);
}
} else {
utarray_free(data->tags);
free(data->group);
free(data->driver);
}
utarray_free(tags);
free(data);
}
static void update_im(neu_adapter_t *adapter, const char *group,
const char *tag, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta)
@@ -580,6 +684,7 @@ neu_adapter_driver_t *neu_adapter_driver_create()
driver->adapter.cb_funs.driver.fdown_open_response = fdown_open_response;
driver->adapter.cb_funs.driver.fup_data_response = fup_data_response;
driver->adapter.cb_funs.driver.update_im = update_im;
driver->adapter.cb_funs.driver.update_im_f_m = update_im_f_m;
driver->adapter.cb_funs.driver.update_with_trace = update_with_trace;
driver->adapter.cb_funs.driver.update_with_meta = update_with_meta;
driver->adapter.cb_funs.driver.scan_tags_response = scan_tags_response;
@@ -1397,6 +1502,8 @@ int neu_adapter_driver_write_tags(neu_adapter_driver_t *driver,
group_t *g = find_group(driver, cmd->group);
if (g == NULL) {
nlog_warn("driver write tags group not exist, driver: %s, group: %s",
driver->adapter.name, cmd->group);
driver->adapter.cb_funs.driver.write_response(&driver->adapter, req,
NEU_ERR_GROUP_NOT_EXIST);
return NEU_ERR_GROUP_NOT_EXIST;
@@ -1417,6 +1524,9 @@ int neu_adapter_driver_write_tags(neu_adapter_driver_t *driver,
cmd->tags[i].value.value.d64,
cmd->tags[i].value.type, tag->decimal);
} else {
nlog_warn("driver write tags not exist, driver: %s, group: %s, "
"tag: %s",
driver->adapter.name, cmd->group, cmd->tags[i].tag);
value_check = NEU_ERR_TAG_NOT_EXIST;
}
@@ -1450,6 +1560,8 @@ int neu_adapter_driver_write_tags(neu_adapter_driver_t *driver,
}
if (value_err != NEU_ERR_SUCCESS) {
nlog_warn("driver write tags value error(%d), driver: %s, group: %s",
value_err, driver->adapter.name, cmd->group);
driver->adapter.cb_funs.driver.write_response(&driver->adapter, req,
value_err);
utarray_foreach(tags, neu_plugin_tag_value_t *, tv)
@@ -1463,6 +1575,9 @@ int neu_adapter_driver_write_tags(neu_adapter_driver_t *driver,
}
if (utarray_len(tags) != (unsigned int) cmd->n_tag) {
nlog_warn("driver write tags some tag not exist, driver: %s, group: "
"%s",
driver->adapter.name, cmd->group);
driver->adapter.cb_funs.driver.write_response(&driver->adapter, req,
NEU_ERR_TAG_NOT_EXIST);
utarray_foreach(tags, neu_plugin_tag_value_t *, tv)
@@ -1662,6 +1777,8 @@ int neu_adapter_driver_write_tag(neu_adapter_driver_t *driver,
group_t * g = find_group(driver, cmd->group);
if (g == NULL) {
nlog_warn("driver write tag group not exist, driver: %s, group: %s",
driver->adapter.name, cmd->group);
driver->adapter.cb_funs.driver.write_response(&driver->adapter, req,
NEU_ERR_GROUP_NOT_EXIST);
return NEU_ERR_GROUP_NOT_EXIST;
@@ -1669,11 +1786,16 @@ int neu_adapter_driver_write_tag(neu_adapter_driver_t *driver,
neu_datatag_t *tag = neu_group_find_tag(g->group, cmd->tag);
if (tag == NULL) {
nlog_warn("driver write tag not exist, driver: %s, group: %s, tag: %s",
driver->adapter.name, cmd->group, cmd->tag);
driver->adapter.cb_funs.driver.write_response(&driver->adapter, req,
NEU_ERR_TAG_NOT_EXIST);
return NEU_ERR_TAG_NOT_EXIST;
} else {
if ((tag->attribute & NEU_ATTRIBUTE_WRITE) != NEU_ATTRIBUTE_WRITE) {
nlog_warn("driver write tag not allow write, driver: %s, group: "
"%s, tag: %s",
driver->adapter.name, cmd->group, cmd->tag);
driver->adapter.cb_funs.driver.write_response(
&driver->adapter, req, NEU_ERR_PLUGIN_TAG_NOT_ALLOW_WRITE);
neu_tag_free(tag);
@@ -1685,6 +1807,9 @@ int neu_adapter_driver_write_tag(neu_adapter_driver_t *driver,
cmd->value.type, tag->decimal);
if (value_check != NEU_ERR_SUCCESS) {
nlog_warn("driver write tag value error(%d), driver: %s, group: "
"%s, tag: %s",
value_check, driver->adapter.name, cmd->group, cmd->tag);
driver->adapter.cb_funs.driver.write_response(&driver->adapter, req,
value_check);
neu_tag_free(tag);
@@ -1822,6 +1947,10 @@ int neu_adapter_driver_update_group(neu_adapter_driver_t *driver,
if (NULL != new_name && 0 != strcmp(name, new_name)) {
char *new_name_cp1 = strdup(new_name);
char *new_name_cp2 = strdup(new_name);
if (find->grp.group_free != NULL) {
find->grp.group_free(&find->grp);
find->grp.group_free = NULL;
}
if (new_name_cp1 && new_name_cp2 &&
0 == neu_group_set_name(find->group, new_name)) {
HASH_DEL(driver->groups, find);
+24 -2
View File
@@ -49,8 +49,15 @@ static void update_timestamp(neu_group_t *group);
neu_group_t *neu_group_new(const char *name, uint32_t interval)
{
neu_group_t *group = calloc(1, sizeof(neu_group_t));
if (NULL == group) {
return NULL;
}
group->name = strdup(name);
group->name = strdup(name);
if (NULL == group->name) {
free(group);
return NULL;
}
group->interval = interval;
pthread_mutex_init(&group->mtx, NULL);
@@ -128,9 +135,22 @@ int neu_group_add_tag(neu_group_t *group, const neu_datatag_t *tag)
return NEU_ERR_TAG_NAME_CONFLICT;
}
el = calloc(1, sizeof(tag_elem_t));
el = calloc(1, sizeof(tag_elem_t));
if (NULL == el) {
pthread_mutex_unlock(&group->mtx);
return NEU_ERR_EINTERNAL;
}
el->name = strdup(tag->name);
el->tag = neu_tag_dup(tag);
if (NULL == el->name || NULL == el->tag) {
free(el->name);
if (el->tag) {
neu_tag_free(el->tag);
}
free(el);
pthread_mutex_unlock(&group->mtx);
return NEU_ERR_EINTERNAL;
}
HASH_ADD_STR(group->tags, name, el);
update_timestamp(group);
@@ -372,8 +392,10 @@ void neu_group_change_test(neu_group_t *group, int64_t timestamp, void *arg,
neu_group_change_fn fn)
{
if (group->timestamp != timestamp) {
pthread_mutex_lock(&group->mtx);
UT_array *tags = to_array(group->tags);
fn(arg, group->timestamp, tags, group->interval);
pthread_mutex_unlock(&group->mtx);
}
}
+4 -3
View File
@@ -76,7 +76,7 @@ static void find_os_info()
}
buf[strcspn(buf, "\n")] = 0;
strncpy(g_metrics_.machine, buf, sizeof(g_metrics_.machine));
g_metrics_.kernel[sizeof(g_metrics_.machine) - 1] = 0;
g_metrics_.machine[sizeof(g_metrics_.machine) - 1] = 0;
pclose(f);
@@ -87,6 +87,7 @@ static void find_os_info()
strncpy(g_metrics_.clib, "glibc", sizeof(g_metrics_.clib));
strncpy(g_metrics_.clib_version, gnu_get_libc_version(),
sizeof(g_metrics_.clib_version));
g_metrics_.clib_version[sizeof(g_metrics_.clib_version) - 1] = 0;
#endif
}
@@ -96,7 +97,7 @@ static size_t parse_memory_fields(int col)
char buf[64] = {};
size_t val = 0;
sprintf(buf, "free -b | awk 'NR==2 {print $%i}'", col);
snprintf(buf, sizeof(buf), "free -b | awk 'NR==2 {print $%i}'", col);
f = popen(buf, "r");
if (NULL == f) {
@@ -131,7 +132,7 @@ static inline size_t neuron_memory_used()
size_t val = 0;
pid_t pid = getpid();
sprintf(buf, "ps -o rss= %ld", (long) pid);
snprintf(buf, sizeof(buf), "ps -o rss= %ld", (long) pid);
f = popen(buf, "r");
if (NULL == f) {
+4
View File
@@ -99,6 +99,9 @@ UT_icd *neu_tag_get_icd()
neu_datatag_t *neu_tag_dup(const neu_datatag_t *tag)
{
neu_datatag_t *new = calloc(1, sizeof(*new));
if (NULL == new) {
return NULL;
}
tag_array_copy(new, tag);
return new;
}
@@ -297,6 +300,7 @@ int neu_datatag_parse_addr_option(const neu_datatag_t * datatag,
break;
}
case NEU_TYPE_BOOL:
case NEU_TYPE_BIT: {
char *op = find_last_character(datatag->address, '.');
+5
View File
@@ -871,6 +871,7 @@ static void conn_connect(neu_conn_t *conn)
} else {
zlog_error(conn->param.log, "invalid ip: %s",
conn->param.params.tcp_server.ip);
close(fd);
return;
}
@@ -941,6 +942,7 @@ static void conn_connect(neu_conn_t *conn)
} else {
zlog_error(conn->param.log, "invalid ip: %s",
conn->param.params.tcp_server.ip);
close(fd);
return;
}
@@ -1400,6 +1402,9 @@ int neu_conn_stream_tcp_server_consume(neu_conn_t *conn, int fd, void *context,
int neu_conn_wait_msg(neu_conn_t *conn, void *context, uint16_t n_byte,
neu_conn_process_msg fn)
{
if (n_byte > conn->buf_size) {
n_byte = conn->buf_size;
}
ssize_t ret = neu_conn_recv(conn, conn->buf, n_byte);
neu_protocol_unpack_buf_t pbuf = { 0 };
conn->offset = 0;
+3 -3
View File
@@ -210,7 +210,7 @@ int neu_conn_eth_check_interface(const char *interface)
return -1;
}
strcpy(ifr.ifr_name, interface);
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
ret = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (ret != 0) {
return -1;
@@ -302,7 +302,7 @@ static int get_mac(neu_conn_eth_t *conn, const char *interface)
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
int ret = -1;
strcpy(ifr.ifr_name, interface);
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
memcpy(conn->ic->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
ret = 0;
@@ -328,7 +328,7 @@ static int init_socket(const char *interface, uint16_t protocol, uint8_t mac[6])
(void) mac;
(void) pn_mcast_addr;
strcpy(ifr.ifr_name, interface);
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", interface);
if (ioctl(fd, SIOCGIFINDEX, &ifr) != 0) {
close(fd);
return -1;
+13 -14
View File
@@ -1263,20 +1263,19 @@ int neu_mqtt_client_set_tls(neu_mqtt_client_t *client, bool enabled,
goto end;
}
if (ca && (rv = nng_tls_config_ca_chain(cfg, ca, NULL)) != 0) {
if (NULL == ca || '\0' == ca[0]) {
log(error, "tls enabled but no ca");
rv = -1;
goto end;
}
if ((rv = nng_tls_config_ca_chain(cfg, ca, NULL)) != 0) {
log(error, "nng_tls_config_ca_chain fail: %s", nng_strerror(rv));
rv = -1;
goto end;
}
if (cert != NULL && key != NULL) {
if ((rv = nng_tls_config_auth_mode(cfg, NNG_TLS_AUTH_MODE_REQUIRED)) !=
0) {
log(error, "nng_tls_config_auth_mode fail: %s", nng_strerror(rv));
rv = -1;
goto end;
}
if (keypass) {
pass[0] = write_string_to_random_file(((char **) keypass)[0]);
pass[1] = write_string_to_random_file(((char **) keypass)[1]);
@@ -1288,12 +1287,12 @@ int neu_mqtt_client_set_tls(neu_mqtt_client_t *client, bool enabled,
rv = -1;
goto end;
}
} else {
if ((rv = nng_tls_config_auth_mode(cfg, NNG_TLS_AUTH_MODE_NONE)) != 0) {
log(error, "nng_tls_config_auth_mode fail: %s", nng_strerror(rv));
rv = -1;
goto end;
}
}
if ((rv = nng_tls_config_auth_mode(cfg, NNG_TLS_AUTH_MODE_REQUIRED)) != 0) {
log(error, "nng_tls_config_auth_mode fail: %s", nng_strerror(rv));
rv = -1;
goto end;
}
end:
+27 -15
View File
@@ -31,6 +31,7 @@
#include "utils/base64.h"
#include "utils/http.h"
#include "utils/log.h"
#include "utils/neu_path.h"
#include "utils/time.h"
#include "adapter.h"
@@ -283,6 +284,21 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
char schema[64] = { 0 };
char buffer[65] = { 0 };
// The library name is used to build a path under the plugin dir; reject
// any name with a path separator or '..' so it cannot escape that dir.
if (!neu_path_is_valid_component(cmd->library)) {
nlog_warn("reject library name with path separator: %s",
cmd->library);
free(cmd->so_file);
free(cmd->schema_file);
header->type = NEU_RESP_ERROR;
e.error = NEU_ERR_LIBRARY_NAME_NOT_CONFORM;
snprintf(header->receiver, sizeof(header->receiver), "%s",
header->sender);
reply(manager, header, &e);
break;
}
if (sscanf(cmd->library, "libplugin-%64s.so", buffer) != 1) {
nlog_warn("library %s no conform", cmd->library);
free(cmd->so_file);
@@ -1419,21 +1435,18 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
UT_array *apps = neu_subscribe_manager_find(
manager->subscribe_manager, cmd->driver, cmd->group);
forward_msg(manager, header, header->receiver);
neu_subscribe_manager_remove(manager->subscribe_manager,
cmd->driver, cmd->group);
if (NULL == apps) {
break;
if (NULL != apps) {
utarray_foreach(apps, neu_app_subscribe_t *, app)
{
forward_msg_copy(manager, header, app->app_name);
}
utarray_free(apps);
}
// notify app node about group deletion
utarray_foreach(apps, neu_app_subscribe_t *, app)
{
forward_msg_copy(manager, header, app->app_name);
}
utarray_free(apps);
forward_msg(manager, header, header->receiver);
}
break;
}
@@ -1451,13 +1464,10 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
neu_msg_exchange(header);
reply(manager, header, &e);
} else {
forward_msg(manager, header, header->receiver);
UT_array *apps = neu_subscribe_manager_find(
manager->subscribe_manager, cmd->driver, cmd->group);
if (NULL != apps) {
// notify app nodes about tag deletion
utarray_foreach(apps, neu_app_subscribe_t *, app)
{
neu_msg_t *msg_copy = neu_msg_copy((neu_msg_t *) header);
@@ -1465,12 +1475,14 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
neu_msg_get_header(msg_copy);
neu_req_del_tag_t *cmd_copy =
(neu_req_del_tag_t *) &header_copy[1];
neu_req_del_tag_copy(cmd, cmd_copy);
neu_req_del_tag_copy(cmd_copy, cmd);
forward_msg(manager, header_copy, app->app_name);
}
utarray_free(apps);
}
forward_msg(manager, header, header->receiver);
}
break;
@@ -1987,7 +1999,7 @@ inline static void notify_monitor(neu_manager_t * manager,
// monitor not ready, ignore
neu_node_manager_t *mgr = manager->node_manager;
struct sockaddr_un addr = neu_node_manager_get_addr(mgr, "monitor");
if (0 == addr.sun_path) {
if (0 == addr.sun_path[1]) {
return;
}
+4 -2
View File
@@ -20,6 +20,8 @@
#ifndef _NEU_MANAGER_INTERNAL_H_
#define _NEU_MANAGER_INTERNAL_H_
#include <errno.h>
#include "event/event.h"
#include "persist/persist.h"
@@ -109,9 +111,9 @@ inline static void forward_msg(neu_manager_t * manager,
if (0 == ret) {
nlog_info("forward msg %s to %s", neu_reqresp_type_string(t), receiver);
} else {
nlog_warn("forward msg %s to node (%s)%s %s %s fail",
nlog_warn("forward msg %s to node (%s)%s %s %s fail, errno: %s(%d)",
neu_reqresp_type_string(t), receiver, &addr.sun_path[1], node,
header->sender);
header->sender, strerror(errno), errno);
neu_msg_free(msg);
}
}
+2 -1
View File
@@ -357,7 +357,8 @@ int neu_plugin_manager_find(neu_plugin_manager_t *mgr, const char *plugin_name,
info->kind = plugin->kind;
if (plugin->single_name != NULL) {
strcpy(info->single_name, plugin->single_name);
snprintf(info->single_name, sizeof(info->single_name), "%s",
plugin->single_name);
}
strncpy(info->name, plugin->name, sizeof(info->name));
strncpy(info->library, plugin->lib_name, sizeof(info->library));
-1
View File
@@ -38,7 +38,6 @@
static bool exit_flag = false;
neu_manager_t * g_manager = NULL;
zlog_category_t *neuron = NULL;
bool disable_jwt = false;
bool sub_filter_err = false;
int default_log_level = ZLOG_LEVEL_NOTICE;
char host_port[32] = { 0 };
+29 -9
View File
@@ -785,7 +785,15 @@ void neu_otel_scope_set_parent_span_id2(neu_otel_scope_ctx ctx,
{
trace_scope_t *scope = (trace_scope_t *) ctx;
if (len < 0) {
len = 0;
} else if (len > 8) {
len = 8; // parent_span_id buffer is 8 bytes
}
uint8_t *p_sp_id = calloc(1, 8);
if (NULL == p_sp_id) {
return;
}
memcpy(p_sp_id, parent_span_id, len);
scope->span->parent_span_id.len = len;
scope->span->parent_span_id.data = p_sp_id;
@@ -1033,29 +1041,39 @@ void neu_otel_split_traceparent(const char *in, char *trace_id, char *span_id,
char * copy = strdup(in);
char t_flags[32] = { 0 };
const size_t trace_id_cap = 64;
const size_t span_id_cap = 32;
if (NULL == copy) {
trace_id[0] = '\0';
span_id[0] = '\0';
*flags = 0;
return;
}
token = strtok_r(copy, delimiter, &saveptr);
token = strtok_r(NULL, delimiter, &saveptr);
if (token != NULL) {
strcpy(trace_id, token);
snprintf(trace_id, trace_id_cap, "%s", token);
} else {
strcpy(trace_id, "");
trace_id[0] = '\0';
}
token = strtok_r(NULL, delimiter, &saveptr);
if (token != NULL) {
strcpy(span_id, token);
snprintf(span_id, span_id_cap, "%s", token);
} else {
strcpy(span_id, "");
span_id[0] = '\0';
}
token = strtok_r(NULL, delimiter, &saveptr);
if (token != NULL) {
strcpy(t_flags, token);
snprintf(t_flags, sizeof(t_flags), "%s", token);
sscanf(t_flags, "%x", flags);
} else {
strcpy(t_flags, "");
*flags = 0;
t_flags[0] = '\0';
*flags = 0;
}
free(copy);
@@ -1183,10 +1201,12 @@ void neu_otel_set_config(void *config)
otel_control_flag = req->control_flag;
otel_data_flag = req->data_flag;
if (req->collector_url) {
strcpy(otel_collector_url, req->collector_url);
snprintf(otel_collector_url, sizeof(otel_collector_url), "%s",
req->collector_url);
}
if (req->service_name) {
strcpy(otel_service_name, req->service_name);
snprintf(otel_service_name, sizeof(otel_service_name), "%s",
req->service_name);
}
otel_data_sample_rate = req->data_sample_rate;
-1
View File
@@ -110,7 +110,6 @@ int neu_parse_param(const char *buf, char **err_param, int n,
int ret = 0;
if (neu_json_decode_value(json, &params_ele) != 0) {
neu_json_decode_free(json);
if (err_param) {
*err_param = strdup("params");
}
+12 -2
View File
@@ -239,7 +239,12 @@ int neu_json_decode_global_config_req_subscriptions(
json_t *grp_array = json_object_get(sub_obj, "groups");
for (size_t j = 0; j < json_array_size(grp_array); ++j) {
p->app = strdup(json_string_value(app_obj));
const char *app_str = json_string_value(app_obj);
if (NULL == app_str) {
nlog_error("`app` field is not a string");
goto error;
}
p->app = strdup(app_str);
if (NULL == p->app) {
nlog_error("strdup fail");
goto error;
@@ -318,7 +323,12 @@ int neu_json_decode_global_config_req_settings(
goto error;
}
p->node = strdup(json_string_value(node_obj));
const char *node_str = json_string_value(node_obj);
if (NULL == node_str) {
nlog_error("`node` field is not a string");
goto error;
}
p->node = strdup(node_str);
if (NULL == p->node) {
nlog_error("strdup fail");
goto error;
+26
View File
@@ -591,6 +591,12 @@ static int decode_write_tags_req_json(void * json_obj,
}
req->tags = calloc(req->n_tag, sizeof(neu_json_write_tags_elem_t));
if (NULL == req->tags) {
req->n_tag = 0;
free(req->node);
free(req->group);
return -1;
}
for (int i = 0; i < req->n_tag; i++) {
neu_json_elem_t v_elems[] = {
{
@@ -1299,6 +1305,11 @@ static int decode_write_gtags_req_json(void * json_obj,
}
req->groups = calloc(req->n_group, sizeof(neu_json_write_gtags_elem_t));
if (NULL == req->groups) {
req->n_group = 0;
free(req->node);
return -1;
}
for (int i = 0; i < req->n_group; i++) {
neu_json_elem_t g_elems[] = {
{
@@ -1313,6 +1324,13 @@ static int decode_write_gtags_req_json(void * json_obj,
ret = neu_json_decode_array_by_json(
json_obj, "groups", i, NEU_JSON_ELEM_SIZE(g_elems), g_elems);
if (ret != 0) {
for (int x = i - 1; x >= 0; x--) {
free(req->groups[x].tags);
}
free(req->groups);
return -1;
}
req->groups[i].group = g_elems[0].v.val_str;
req->groups[i].n_tag = json_array_size(g_elems[1].v.val_object);
@@ -1322,6 +1340,14 @@ static int decode_write_gtags_req_json(void * json_obj,
req->groups[i].tags =
calloc(req->groups[i].n_tag, sizeof(neu_json_write_tags_elem_t));
if (NULL == req->groups[i].tags) {
req->groups[i].n_tag = 0;
for (int x = i - 1; x >= 0; x--) {
free(req->groups[x].tags);
}
free(req->groups);
return -1;
}
for (int k = 0; k < req->groups[i].n_tag; k++) {
neu_json_elem_t v_elems[] = {
-75
View File
@@ -1,75 +0,0 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/*
* DO NOT EDIT THIS FILE MANUALLY!
* It was automatically generated by `json-autotype`.
*/
#include <stdlib.h>
#include <string.h>
#include "json/json.h"
#include "neu_json_system.h"
void neu_json_decode_system_ctl_req_free(neu_json_system_ctl_req_t *req)
{
if (req != NULL) {
free(req);
}
}
int neu_json_decode_system_ctl_req(char * buf,
neu_json_system_ctl_req_t **result)
{
int ret = 0;
void *json_obj = neu_json_decode_new(buf);
neu_json_system_ctl_req_t *req =
calloc(1, sizeof(neu_json_system_ctl_req_t));
if (req == NULL) {
return -1;
}
neu_json_elem_t req_elems[] = {
{
.name = "action",
.t = NEU_JSON_INT,
},
};
ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems),
req_elems);
if (ret != 0) {
free(req);
if (json_obj != NULL) {
neu_json_decode_free(json_obj);
}
return -1;
}
req->action = req_elems[0].v.val_int;
*result = req;
if (json_obj != NULL) {
neu_json_decode_free(json_obj);
}
return ret;
}
-43
View File
@@ -1,43 +0,0 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/*
* DO NOT EDIT THIS FILE MANUALLY!
* It was automatically generated by `json-autotype`.
*/
#ifndef _NEU_JSON_API_NEU_JSON_SYSTEM_H_
#define _NEU_JSON_API_NEU_JSON_SYSTEM_H_
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int action;
} neu_json_system_ctl_req_t;
void neu_json_decode_system_ctl_req_free(neu_json_system_ctl_req_t *req);
int neu_json_decode_system_ctl_req(char * buf,
neu_json_system_ctl_req_t **result);
#ifdef __cplusplus
}
#endif
#endif
+6 -2
View File
@@ -247,7 +247,7 @@ int neu_json_decode_tag_array_json(void *json_obj, neu_json_tag_array_t *arr)
return 0;
decode_fail:
while (--i > 0) {
while (i-- > 0) {
neu_json_decode_tag_fini(&tags[i]);
}
free(tags);
@@ -556,7 +556,7 @@ int neu_json_decode_gtag_array_json(void *json_obj, neu_json_gtag_array_t *arr)
return 0;
decode_fail:
while (--i > 0) {
while (i-- > 0) {
neu_json_decode_gtag_fini(&gtags[i]);
}
free(gtags);
@@ -775,6 +775,10 @@ int neu_json_decode_del_tags_req(char *buf, neu_json_del_tags_req_t **result)
}
req->tags = calloc(req->n_tags, sizeof(neu_json_del_tags_req_name_t));
if (NULL == req->tags) {
req->n_tags = 0;
goto decode_fail;
}
neu_json_del_tags_req_name_t *p_tag = req->tags;
for (int i = 0; i < req->n_tags; i++) {
neu_json_elem_t id_elems[] = { {
+12 -8
View File
@@ -117,19 +117,23 @@ int read_file_string(const char *fn, char **out)
}
// read all file content
ssize_t n = 0;
while ((n = read(fd, buf + n, fsize))) {
if (fsize == n) {
break;
} else if (n < fsize && EINTR == errno) {
continue;
} else {
ssize_t total = 0;
while (total < fsize) {
ssize_t n = read(fd, buf + total, fsize - total);
if (n < 0) {
if (EINTR == errno) {
continue;
}
rv = -1;
goto error_read;
}
if (0 == n) {
break; // EOF before the expected size
}
total += n;
}
buf[fsize] = 0;
buf[total] = 0;
*out = buf;
close(fd);
return rv;
+59 -25
View File
@@ -532,12 +532,15 @@ int neu_sqlite_persister_load_nodes(neu_persister_t *self,
int step = sqlite3_step(stmt);
while (SQLITE_ROW == step) {
neu_persist_node_info_t info = {};
char *name = strdup((char *) sqlite3_column_text(stmt, 0));
const char *name_txt = (const char *) sqlite3_column_text(stmt, 0);
char * name = name_txt ? strdup(name_txt) : NULL;
if (NULL == name) {
break;
}
char *plugin_name = strdup((char *) sqlite3_column_text(stmt, 3));
const char *plugin_name_txt =
(const char *) sqlite3_column_text(stmt, 3);
char *plugin_name = plugin_name_txt ? strdup(plugin_name_txt) : NULL;
if (NULL == plugin_name) {
free(name);
break;
@@ -925,12 +928,16 @@ int neu_sqlite_persister_load_subscriptions(neu_persister_t *self,
int step = sqlite3_step(stmt);
while (SQLITE_ROW == step) {
char *driver_name = strdup((char *) sqlite3_column_text(stmt, 0));
const char *driver_name_txt =
(const char *) sqlite3_column_text(stmt, 0);
char *driver_name = driver_name_txt ? strdup(driver_name_txt) : NULL;
if (NULL == driver_name) {
break;
}
char *group_name = strdup((char *) sqlite3_column_text(stmt, 1));
const char *group_name_txt =
(const char *) sqlite3_column_text(stmt, 1);
char *group_name = group_name_txt ? strdup(group_name_txt) : NULL;
if (NULL == group_name) {
free(driver_name);
break;
@@ -1045,7 +1052,8 @@ static int collect_group_info(sqlite3_stmt *stmt, UT_array **group_infos)
int step = sqlite3_step(stmt);
while (SQLITE_ROW == step) {
neu_persist_group_info_t info = {};
char *name = strdup((char *) sqlite3_column_text(stmt, 0));
const char *name_txt = (const char *) sqlite3_column_text(stmt, 0);
char * name = name_txt ? strdup(name_txt) : NULL;
if (NULL == name) {
break;
}
@@ -1161,7 +1169,8 @@ int neu_sqlite_persister_load_node_setting(neu_persister_t * self,
goto end;
}
char *s = strdup((char *) sqlite3_column_text(stmt, 0));
const char *setting_txt = (const char *) sqlite3_column_text(stmt, 0);
char * s = setting_txt ? strdup(setting_txt) : NULL;
if (NULL == s) {
nlog_error("strdup fail");
rv = NEU_ERR_EINTERNAL;
@@ -1194,14 +1203,17 @@ static int collect_user_info(sqlite3_stmt *stmt, UT_array **user_infos)
int step = sqlite3_step(stmt);
while (SQLITE_ROW == step) {
neu_persist_user_info_t info = {};
char *name = strdup((char *) sqlite3_column_text(stmt, 0));
const char *name_txt = (const char *) sqlite3_column_text(stmt, 0);
char * name = name_txt ? strdup(name_txt) : NULL;
if (NULL == name) {
break;
}
info.name = name;
info.hash = strdup((char *) sqlite3_column_text(stmt, 1));
info.name = name;
const char *hash_txt = (const char *) sqlite3_column_text(stmt, 1);
info.hash = hash_txt ? strdup(hash_txt) : NULL;
if (NULL == info.hash) {
free(name);
break;
}
utarray_push_back(*user_infos, &info);
@@ -1296,7 +1308,8 @@ int neu_sqlite_persister_load_user(neu_persister_t *self, const char *user_name,
goto error;
}
user->hash = strdup((char *) sqlite3_column_text(stmt, 0));
const char *hash_txt = (const char *) sqlite3_column_text(stmt, 0);
user->hash = hash_txt ? strdup(hash_txt) : NULL;
if (NULL == user->hash) {
nlog_error("strdup fail");
goto error;
@@ -1435,8 +1448,10 @@ int neu_sqlite_persister_load_server_cert(
// Read all fields
cert_info->id = sqlite3_column_int(stmt, 0);
cert_info->app_name = strdup((char *) sqlite3_column_text(stmt, 1));
cert_info->common_name = strdup((char *) sqlite3_column_text(stmt, 2));
char *app_name_db = (char *) sqlite3_column_text(stmt, 1);
cert_info->app_name = app_name_db ? strdup(app_name_db) : NULL;
char *common_name = (char *) sqlite3_column_text(stmt, 2);
cert_info->common_name = common_name ? strdup(common_name) : NULL;
char *subject = (char *) sqlite3_column_text(stmt, 3);
cert_info->subject = subject ? strdup(subject) : NULL;
@@ -1497,13 +1512,16 @@ static int collect_client_cert_info(sqlite3_stmt *stmt, UT_array **cert_infos)
info.id = sqlite3_column_int(stmt, 0);
char *app_name = strdup((char *) sqlite3_column_text(stmt, 1));
const char *app_name_col = (const char *) sqlite3_column_text(stmt, 1);
char * app_name = app_name_col ? strdup(app_name_col) : NULL;
if (NULL == app_name) {
break;
}
info.app_name = app_name;
char *common_name = strdup((char *) sqlite3_column_text(stmt, 2));
const char *common_name_col =
(const char *) sqlite3_column_text(stmt, 2);
char *common_name = common_name_col ? strdup(common_name_col) : NULL;
if (NULL == common_name) {
free(app_name);
break;
@@ -1792,7 +1810,8 @@ int neu_sqlite_persister_load_security_policy(
info->id = sqlite3_column_int(stmt, 0);
char *app_name_col = strdup((char *) sqlite3_column_text(stmt, 1));
const char *app_name_txt = (const char *) sqlite3_column_text(stmt, 1);
char * app_name_col = app_name_txt ? strdup(app_name_txt) : NULL;
if (NULL == app_name_col) {
free(info);
sqlite3_finalize(stmt);
@@ -1800,7 +1819,9 @@ int neu_sqlite_persister_load_security_policy(
}
info->app_name = app_name_col;
char *policy_name = strdup((char *) sqlite3_column_text(stmt, 2));
const char *policy_name_txt =
(const char *) sqlite3_column_text(stmt, 2);
char *policy_name = policy_name_txt ? strdup(policy_name_txt) : NULL;
if (NULL == policy_name) {
free(info->app_name);
free(info);
@@ -1853,13 +1874,16 @@ static int collect_security_policy_info(sqlite3_stmt *stmt,
info.id = sqlite3_column_int(stmt, 0);
char *app_name = strdup((char *) sqlite3_column_text(stmt, 1));
const char *app_name_txt = (const char *) sqlite3_column_text(stmt, 1);
char * app_name = app_name_txt ? strdup(app_name_txt) : NULL;
if (NULL == app_name) {
break;
}
info.app_name = app_name;
char *policy_name = strdup((char *) sqlite3_column_text(stmt, 2));
const char *policy_name_txt =
(const char *) sqlite3_column_text(stmt, 2);
char *policy_name = policy_name_txt ? strdup(policy_name_txt) : NULL;
if (NULL == policy_name) {
free(app_name);
break;
@@ -1981,7 +2005,8 @@ int neu_sqlite_persister_load_auth_setting(
auth_info->id = sqlite3_column_int(stmt, 0);
auth_info->enabled = sqlite3_column_int(stmt, 2);
auth_info->app_name = strdup((char *) sqlite3_column_text(stmt, 1));
const char *auth_app_name = (const char *) sqlite3_column_text(stmt, 1);
auth_info->app_name = auth_app_name ? strdup(auth_app_name) : NULL;
if (NULL == auth_info->app_name) {
nlog_error("strdup fail");
goto error;
@@ -2018,20 +2043,25 @@ static int collect_auth_user_info(sqlite3_stmt *stmt, UT_array **user_infos)
info.id = sqlite3_column_int(stmt, 0);
char *app_name = strdup((char *) sqlite3_column_text(stmt, 1));
const char *app_name_txt = (const char *) sqlite3_column_text(stmt, 1);
char * app_name = app_name_txt ? strdup(app_name_txt) : NULL;
if (NULL == app_name) {
break;
}
info.app_name = app_name;
char *username = strdup((char *) sqlite3_column_text(stmt, 2));
const char *username_txt = (const char *) sqlite3_column_text(stmt, 2);
char * username = username_txt ? strdup(username_txt) : NULL;
if (NULL == username) {
free(app_name);
break;
}
info.username = username;
char *password_hash = strdup((char *) sqlite3_column_text(stmt, 3));
const char *password_hash_txt =
(const char *) sqlite3_column_text(stmt, 3);
char *password_hash =
password_hash_txt ? strdup(password_hash_txt) : NULL;
if (NULL == password_hash) {
free(app_name);
free(username);
@@ -2124,19 +2154,23 @@ int neu_sqlite_persister_load_auth_user(
user_info->id = sqlite3_column_int(stmt, 0);
user_info->app_name = strdup((char *) sqlite3_column_text(stmt, 1));
const char *ui_app_name = (const char *) sqlite3_column_text(stmt, 1);
user_info->app_name = ui_app_name ? strdup(ui_app_name) : NULL;
if (NULL == user_info->app_name) {
nlog_error("strdup fail");
goto error;
}
user_info->username = strdup((char *) sqlite3_column_text(stmt, 2));
const char *ui_username = (const char *) sqlite3_column_text(stmt, 2);
user_info->username = ui_username ? strdup(ui_username) : NULL;
if (NULL == user_info->username) {
nlog_error("strdup fail");
goto error;
}
user_info->password_hash = strdup((char *) sqlite3_column_text(stmt, 3));
const char *ui_password_hash = (const char *) sqlite3_column_text(stmt, 3);
user_info->password_hash =
ui_password_hash ? strdup(ui_password_hash) : NULL;
if (NULL == user_info->password_hash) {
nlog_error("strdup fail");
goto error;
+12 -3
View File
@@ -18,6 +18,7 @@
**/
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -40,7 +41,7 @@ size_t calcDecodeLength(const char *b64input)
{ // Calculates the length of a decoded string
size_t len = strlen(b64input), padding = 0;
if (0 == len) {
if (len < 4) {
return 0;
}
@@ -57,8 +58,16 @@ int Base64Decode(const char *b64message, unsigned char **buffer, int *length)
{ // Decodes a base64 encoded string
BIO *bio, *b64;
int decodeLen = calcDecodeLength(b64message);
*buffer = (unsigned char *) malloc(decodeLen + 1);
// BIO_read() takes an int length; reject oversized input so neither the
// read length nor the derived decodeLen can overflow/truncate to int.
if (strlen(b64message) > INT_MAX) {
return -1;
}
int decodeLen = calcDecodeLength(b64message);
*buffer = (unsigned char *) malloc(decodeLen + 1);
if (NULL == *buffer) {
return -1;
}
(*buffer)[decodeLen] = '\0';
bio = BIO_new_mem_buf(b64message, -1);
+100 -43
View File
@@ -24,6 +24,7 @@
#include "define.h"
#include "utils/cid.h"
#include "utils/log.h"
#include "utils/neu_path.h"
#include "json/json.h"
static int parse_ied(xmlNode *xml_ied, cid_ied_t *ied);
@@ -40,7 +41,14 @@ static void fill_doi_ctls(cid_t *cid);
int neu_cid_parse(const char *path, cid_t *cid)
{
memset(cid, 0, sizeof(cid_t));
xmlDoc *doc = xmlReadFile(path, NULL, 0);
char *safe_path = neu_path_confine(NULL, path);
if (safe_path == NULL) {
nlog_warn("reject icd file path outside working dir: %s", path);
return -1;
}
// XML_PARSE_NONET blocks network access for external entities/DTDs (XXE).
xmlDoc *doc = xmlReadFile(safe_path, NULL, XML_PARSE_NONET);
free(safe_path);
if (doc == NULL) {
nlog_warn("Failed to read icd file %s", path);
return -1;
@@ -274,13 +282,20 @@ typedef struct {
} da_basic_type_t;
static int find_da_basic_type(cid_template_t *template, const char *datype_id,
const char *seg_name, da_basic_type_t *da_types)
const char *seg_name, da_basic_type_t *da_types,
int max, int depth)
{
char name[NEU_CID_LEN64] = { 0 };
int index = 0;
if (depth > 32) {
return 0;
}
for (int i = 0; i < template->n_datypes; i++) {
if (strcmp(template->datypes[i].id, datype_id) == 0) {
for (int j = 0; j < template->datypes[i].n_bdas; j++) {
if (index >= max) {
break;
}
if (strlen(seg_name) > 0) {
snprintf(name, sizeof(name), "%s.%s", seg_name,
template->datypes[i].bdas[j].name);
@@ -291,9 +306,10 @@ static int find_da_basic_type(cid_template_t *template, const char *datype_id,
if (template->datypes[i].bdas[j].btype == Struct) {
index += find_da_basic_type(
template, template->datypes[i].bdas[j].ref_type, name,
da_types + index);
da_types + index, max - index, depth + 1);
} else {
strcpy(da_types[index].all_name, name);
snprintf(da_types[index].all_name,
sizeof(da_types[index].all_name), "%s", name);
da_types[index].btype = template->datypes[i].bdas[j].btype;
index += 1;
}
@@ -335,8 +351,9 @@ static int find_basic_type(cid_template_t *template, const char *type_id,
*array_size = dotype->das[i].array_size;
da_basic_type_t da_types[32] = { 0 };
int n_da_types = find_da_basic_type(template, dotype->das[i].ref_type,
dotype->das[i].name, da_types);
int n_da_types = find_da_basic_type(
template, dotype->das[i].ref_type, dotype->das[i].name, da_types,
(int) (sizeof(da_types) / sizeof(da_types[0])), 0);
for (int j = 0; j < n_da_types; j++) {
if (strlen(da_name) > 0) {
if (strcmp(da_types[j].all_name, da_name) == 0) {
@@ -457,13 +474,15 @@ static void update_doi(const char *ref_type, cid_doi_t *dois, int n_dois,
ctl->btype = da->btype;
ctl->fc = da->fc;
strcpy(ctl->da_name, da->name);
snprintf(ctl->da_name, sizeof(ctl->da_name), "%s",
da->name);
}
}
da_basic_type_t da_types[32] = { 0 };
int n_da_types = find_da_basic_type(
template, tm_do->das[k].ref_type, tm_do->das[k].name, da_types);
template, tm_do->das[k].ref_type, tm_do->das[k].name, da_types,
(int) (sizeof(da_types) / sizeof(da_types[0])), 0);
if (da->fc == SP || da->fc == SG) {
for (int j = 0; j < n_da_types; j++) {
@@ -475,7 +494,8 @@ static void update_doi(const char *ref_type, cid_doi_t *dois, int n_dois,
ctl->btype = da_types[j].btype;
ctl->fc = da->fc;
strcpy(ctl->da_name, da_types[j].all_name);
snprintf(ctl->da_name, sizeof(ctl->da_name), "%s",
da_types[j].all_name);
}
}
@@ -488,9 +508,14 @@ static void update_doi(const char *ref_type, cid_doi_t *dois, int n_dois,
ctl->btype = da_types[0].btype;
ctl->fc = da->fc;
strcpy(ctl->da_name, da->name);
ctl->n_co_types = n_da_types;
for (int j = 0; j < n_da_types; j++) {
snprintf(ctl->da_name, sizeof(ctl->da_name), "%s", da->name);
int n_co = n_da_types;
if (n_co >
(int) (sizeof(ctl->co_types) / sizeof(ctl->co_types[0]))) {
n_co = sizeof(ctl->co_types) / sizeof(ctl->co_types[0]);
}
ctl->n_co_types = n_co;
for (int j = 0; j < n_co; j++) {
ctl->co_types[j] = da_types[j].btype;
}
}
@@ -517,12 +542,16 @@ static int parse_ied(xmlNode *xml_ied, cid_ied_t *ied)
{
int ret = -1;
char *ied_name = (char *) xmlGetProp(xml_ied, (const xmlChar *) "name");
if (ied_name == NULL) {
nlog_warn("IED has no name attribute");
return -1;
}
if (strlen(ied_name) >= NEU_CID_IED_NAME_LEN) {
xmlFree(ied_name);
nlog_warn("IED name is too long");
return -1;
} else {
strcpy(ied->name, ied_name);
snprintf(ied->name, sizeof(ied->name), "%s", ied_name);
xmlFree(ied_name);
}
xmlNode *access_point = xml_ied->children;
@@ -540,7 +569,7 @@ static int parse_ied(xmlNode *xml_ied, cid_ied_t *ied)
memset(ap, 0, sizeof(cid_access_point_t));
if (name != NULL) {
strcpy(ap->name, name);
snprintf(ap->name, sizeof(ap->name), "%s", name);
xmlFree(name);
}
@@ -581,7 +610,7 @@ static int parse_ldevice(xmlNode *xml_server, cid_access_point_t *ap)
memset(ldev, 0, sizeof(cid_ldevice_t));
char *inst = (char *) xmlGetProp(ldevice, (const xmlChar *) "inst");
if (inst != NULL) {
strcpy(ldev->inst, inst);
snprintf(ldev->inst, sizeof(ldev->inst), "%s", inst);
xmlFree(inst);
}
int ret = parse_lnode(ldevice, ldev);
@@ -903,8 +932,9 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&template->lnotypes[template->n_lnotypes - 1];
memset(tm_lno, 0, sizeof(cid_tm_lno_type_t));
strcpy(tm_lno->id, id);
strcpy(tm_lno->ln_class, ln_class);
snprintf(tm_lno->id, sizeof(tm_lno->id), "%s", id);
snprintf(tm_lno->ln_class, sizeof(tm_lno->ln_class), "%s",
ln_class);
xmlNode *xdo = child->children;
while (xdo != NULL) {
@@ -926,8 +956,9 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&tm_lno->dos[tm_lno->n_dos - 1];
memset(tm_do, 0, sizeof(cid_tm_lno_do_t));
strcpy(tm_do->name, name);
strcpy(tm_do->ref_type, ref_type);
snprintf(tm_do->name, sizeof(tm_do->name), "%s", name);
snprintf(tm_do->ref_type, sizeof(tm_do->ref_type), "%s",
ref_type);
}
if (name != NULL) {
@@ -960,7 +991,7 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&template->dotypes[template->n_dotypes - 1];
memset(tm_do, 0, sizeof(cid_tm_do_type_t));
strcpy(tm_do->id, id);
snprintf(tm_do->id, sizeof(tm_do->id), "%s", id);
xmlNode *da = child->children;
while (da != NULL) {
if (da->type != XML_ELEMENT_NODE) {
@@ -982,8 +1013,10 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&tm_do->sdos[tm_do->n_sdos - 1];
memset(tm_sdo, 0, sizeof(cid_tm_sdo_t));
strcpy(tm_sdo->name, sdo_name);
strcpy(tm_sdo->ref_type, sdo_ref_type);
snprintf(tm_sdo->name, sizeof(tm_sdo->name), "%s",
sdo_name);
snprintf(tm_sdo->ref_type, sizeof(tm_sdo->ref_type),
"%s", sdo_ref_type);
}
if (sdo_name != NULL) {
@@ -1015,9 +1048,12 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&tm_do->das[tm_do->n_das - 1];
memset(tm_da, 0, sizeof(cid_tm_do_da_t));
strcpy(tm_da->name, name);
snprintf(tm_da->name, sizeof(tm_da->name), "%s",
name);
if (ref_type != NULL) {
strcpy(tm_da->ref_type, ref_type);
snprintf(tm_da->ref_type,
sizeof(tm_da->ref_type), "%s",
ref_type);
}
tm_da->btype = T_UNKNOWN;
tm_da->btype =
@@ -1034,8 +1070,14 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
}
if (count != NULL) {
int cnt = atoi(count);
if (cnt < 0) {
cnt = 0;
} else if (cnt > 255) {
cnt = 255; // array_size is uint8_t
}
tm_da->is_array = true;
tm_da->array_size = atoi(count);
tm_da->array_size = (uint8_t) cnt;
} else {
tm_da->is_array = false;
tm_da->array_size = 0;
@@ -1079,7 +1121,7 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
&template->datypes[template->n_datypes - 1];
memset(tm_dat, 0, sizeof(cid_tm_da_type_t));
strcpy(tm_dat->id, id);
snprintf(tm_dat->id, sizeof(tm_dat->id), "%s", id);
xmlNode *bda = child->children;
while (bda != NULL) {
if (bda->type != XML_ELEMENT_NODE) {
@@ -1101,9 +1143,11 @@ static int parse_template(xmlNode *xml_template, cid_template_t *template)
cid_tm_bda_type_t *tm_bda =
&tm_dat->bdas[tm_dat->n_bdas - 1];
memset(tm_bda, 0, sizeof(cid_tm_bda_type_t));
strcpy(tm_bda->name, name);
snprintf(tm_bda->name, sizeof(tm_bda->name), "%s",
name);
if (ref_type != NULL) {
strcpy(tm_bda->ref_type, ref_type);
snprintf(tm_bda->ref_type, sizeof(tm_bda->ref_type),
"%s", ref_type);
}
tm_bda->btype = T_UNKNOWN;
tm_bda->btype = decode_basictype((const char *) btype);
@@ -1239,12 +1283,18 @@ cid_dataset_info_t *neu_cid_info_from_string(const char *str)
cid_dataset_info_t *info = calloc(1, sizeof(cid_dataset_info_t));
info->control = elems[0].v.val_bool;
info->buffered = elems[1].v.val_bool;
strcpy(info->ied_name, elems[2].v.val_str);
strcpy(info->ldevice_inst, elems[3].v.val_str);
strcpy(info->ln_class, elems[4].v.val_str);
strcpy(info->report_name, elems[5].v.val_str);
strcpy(info->report_id, elems[6].v.val_str);
strcpy(info->dataset_name, elems[7].v.val_str);
snprintf(info->ied_name, sizeof(info->ied_name), "%s",
elems[2].v.val_str);
snprintf(info->ldevice_inst, sizeof(info->ldevice_inst), "%s",
elems[3].v.val_str);
snprintf(info->ln_class, sizeof(info->ln_class), "%s",
elems[4].v.val_str);
snprintf(info->report_name, sizeof(info->report_name), "%s",
elems[5].v.val_str);
snprintf(info->report_id, sizeof(info->report_id), "%s",
elems[6].v.val_str);
snprintf(info->dataset_name, sizeof(info->dataset_name), "%s",
elems[7].v.val_str);
free(elems[2].v.val_str);
free(elems[3].v.val_str);
@@ -1283,8 +1333,9 @@ static void ld_ctrl_do_to_group(const char *ied_name, const char *ap_name,
cid_dataset_info_t *g_info = calloc(1, sizeof(cid_dataset_info_t));
g_info->control = true;
strcpy(g_info->ied_name, ied_name);
strcpy(g_info->ldevice_inst, ld->inst);
snprintf(g_info->ied_name, sizeof(g_info->ied_name), "%s", ied_name);
snprintf(g_info->ldevice_inst, sizeof(g_info->ldevice_inst), "%s",
ld->inst);
group->context = g_info;
snprintf(group->group, NEU_GROUP_NAME_LEN, "%s/%s$Control", ap_name,
@@ -1447,13 +1498,19 @@ static void ln_dataset_to_group(const char *ied_name, const char *ap_name,
calloc(1, sizeof(cid_dataset_info_t));
g_info->control = false;
strcpy(g_info->ied_name, ied_name);
strcpy(g_info->ldevice_inst, ld_inst);
strcpy(g_info->ln_class, ln->lnclass);
snprintf(g_info->ied_name, sizeof(g_info->ied_name), "%s",
ied_name);
snprintf(g_info->ldevice_inst, sizeof(g_info->ldevice_inst),
"%s", ld_inst);
snprintf(g_info->ln_class, sizeof(g_info->ln_class), "%s",
ln->lnclass);
g_info->buffered = ln->reports[i].buffered;
strcpy(g_info->report_name, ln->reports[i].name);
strcpy(g_info->report_id, ln->reports[i].id);
strcpy(g_info->dataset_name, ln->datasets[j].name);
snprintf(g_info->report_name, sizeof(g_info->report_name), "%s",
ln->reports[i].name);
snprintf(g_info->report_id, sizeof(g_info->report_id), "%s",
ln->reports[i].id);
snprintf(g_info->dataset_name, sizeof(g_info->dataset_name),
"%s", ln->datasets[j].name);
group->context = g_info;
@@ -1606,7 +1663,7 @@ static void ln_dataset_to_group(const char *ied_name, const char *ap_name,
void neu_cid_to_msg(char *driver, cid_t *cid, neu_req_add_gtag_t *cmd)
{
strcpy(cmd->driver, driver);
snprintf(cmd->driver, sizeof(cmd->driver), "%s", driver);
cmd->n_group = 0;
cmd->groups = NULL;
+531
View File
@@ -0,0 +1,531 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils/ede.h"
#include "utils/neu_path.h"
typedef struct {
int object_type;
const char *area;
neu_type_e present_value_type;
} object_map_t;
typedef struct {
const char *property_id;
neu_type_e type;
} property_map_t;
static const object_map_t g_object_map[] = {
{ 0, "AI", NEU_TYPE_FLOAT }, { 1, "AO", NEU_TYPE_FLOAT },
{ 2, "AV", NEU_TYPE_FLOAT }, { 3, "BI", NEU_TYPE_BIT },
{ 4, "BO", NEU_TYPE_BIT }, { 5, "BV", NEU_TYPE_BIT },
{ 8, "DEV", NEU_TYPE_ERROR }, { 13, "MSI", NEU_TYPE_UINT8 },
{ 14, "MSO", NEU_TYPE_UINT8 }, { 19, "MSV", NEU_TYPE_UINT8 },
{ 23, "ACC", NEU_TYPE_UINT8 },
};
static const property_map_t g_property_map[] = {
{ "Object_Name", NEU_TYPE_STRING },
{ "Object_Tyep", NEU_TYPE_UINT8 },
{ "Object_Type", NEU_TYPE_UINT8 },
{ "Description", NEU_TYPE_STRING },
{ "Device_Type", NEU_TYPE_STRING },
{ "Status_Flags", NEU_TYPE_STRING },
{ "Event_State", NEU_TYPE_UINT8 },
{ "Out_Of_Service", NEU_TYPE_BOOL },
{ "Update_Interval", NEU_TYPE_UINT8 },
{ "Min_Pres_Value", NEU_TYPE_FLOAT },
{ "Max_Pres_Value", NEU_TYPE_FLOAT },
{ "Resolution", NEU_TYPE_FLOAT },
{ "COV_Increment", NEU_TYPE_FLOAT },
{ "Time_Delay", NEU_TYPE_UINT8 },
{ "Notification_Class", NEU_TYPE_UINT8 },
{ "Notify_Type", NEU_TYPE_UINT8 },
{ "Units", NEU_TYPE_UINT8 },
{ "High_Limit", NEU_TYPE_FLOAT },
{ "Low_Limit", NEU_TYPE_FLOAT },
{ "Deadband", NEU_TYPE_FLOAT },
{ "Reliability", NEU_TYPE_UINT8 },
{ "Polarity", NEU_TYPE_UINT8 },
{ "System_Status", NEU_TYPE_UINT8 },
{ "Vendor_Name", NEU_TYPE_STRING },
{ "Vendor_Identifier", NEU_TYPE_UINT8 },
{ "Model_Name", NEU_TYPE_STRING },
{ "Firmware_Revision", NEU_TYPE_STRING },
{ "Application_Software_Version", NEU_TYPE_STRING },
{ "Location", NEU_TYPE_STRING },
{ "Protocol_Version", NEU_TYPE_UINT16 },
{ "Protocol_Conformance_Class", NEU_TYPE_UINT8 },
{ "Protocol_Service_Supported", NEU_TYPE_STRING },
{ "Protocol_Object_Types_Supported", NEU_TYPE_STRING },
{ "Serial_Number", NEU_TYPE_STRING },
{ "Max_APDU_Length_Accepted", NEU_TYPE_UINT16 },
{ "Segmentation_Supported", NEU_TYPE_UINT8 },
{ "LOCAL_TIME", NEU_TYPE_STRING },
{ "LOCAL_DATE", NEU_TYPE_STRING },
{ "UTC_Offset", NEU_TYPE_INT8 },
{ "Daylight_Savings_Status", NEU_TYPE_BOOL },
{ "APUD_Segment_Timeout", NEU_TYPE_UINT8 },
{ "APUD_Timeout", NEU_TYPE_UINT16 },
{ "Number_Of_APDU_Retries", NEU_TYPE_UINT8 },
{ "Max_Master", NEU_TYPE_UINT8 },
{ "Max_Info_Frame", NEU_TYPE_UINT8 },
{ "Profile_Name", NEU_TYPE_STRING },
{ "Pulse_Rate", NEU_TYPE_UINT8 },
{ "Scale", NEU_TYPE_FLOAT },
{ "Prescale", NEU_TYPE_FLOAT },
{ "Value_Before_Change", NEU_TYPE_UINT8 },
{ "Value_Change_Time", NEU_TYPE_STRING },
};
static neu_attribute_e commandable_to_attribute(const char *commandable)
{
if (commandable == NULL || commandable[0] == '\0') {
return NEU_ATTRIBUTE_READ;
}
if (strcmp(commandable, "N") == 0 || strcmp(commandable, "n") == 0 ||
strcmp(commandable, "0") == 0 || strcmp(commandable, "false") == 0 ||
strcmp(commandable, "FALSE") == 0) {
return NEU_ATTRIBUTE_READ;
}
return (neu_attribute_e)(NEU_ATTRIBUTE_READ | NEU_ATTRIBUTE_WRITE);
}
static void trim_right(char *s)
{
size_t len = strlen(s);
while (len > 0) {
const char ch = s[len - 1];
if (ch == '\n' || ch == '\r' || isspace((unsigned char) ch)) {
s[len - 1] = '\0';
--len;
continue;
}
break;
}
}
static int read_field(const char *line, int field_index, char *out,
size_t out_size)
{
const char *start = line;
const char *end = line;
int idx = 0;
if (out_size == 0) {
return -1;
}
while (*end != '\0' && idx < field_index) {
if (*end == ';') {
++idx;
start = end + 1;
}
++end;
}
if (idx != field_index) {
return -1;
}
end = start;
while (*end != '\0' && *end != ';') {
++end;
}
size_t copy_len = (size_t)(end - start);
if (copy_len >= out_size) {
copy_len = out_size - 1;
}
memcpy(out, start, copy_len);
out[copy_len] = '\0';
return 0;
}
static const object_map_t *find_object_map(int object_type)
{
size_t i = 0;
for (i = 0; i < sizeof(g_object_map) / sizeof(g_object_map[0]); ++i) {
if (g_object_map[i].object_type == object_type) {
return &g_object_map[i];
}
}
return NULL;
}
static const char *get_file_name(const char *path)
{
const char *name = NULL;
if (path == NULL) {
return NULL;
}
name = strrchr(path, '/');
if (name == NULL) {
return path;
}
return name + 1;
}
static int push_entry(neu_ede_result_t *result, const char *address,
const char *name, neu_type_e data_type,
const char *description, neu_attribute_e attribute)
{
neu_ede_entry_t *new_entries =
realloc(result->entries, (result->count + 1) * sizeof(neu_ede_entry_t));
if (new_entries == NULL) {
return -1;
}
result->entries = new_entries;
if (name == NULL) {
result->entries[result->count].name[0] = '\0';
} else {
snprintf(result->entries[result->count].name,
sizeof(result->entries[result->count].name), "%s", name);
}
snprintf(result->entries[result->count].address,
sizeof(result->entries[result->count].address), "%s", address);
result->entries[result->count].data_type = data_type;
if (description == NULL) {
result->entries[result->count].description[0] = '\0';
} else {
snprintf(result->entries[result->count].description,
sizeof(result->entries[result->count].description), "%s",
description);
}
result->entries[result->count].attribute = attribute;
++result->count;
return 0;
}
int neu_ede_parse_file(const char *file_path, neu_ede_result_t *result)
{
FILE *fp = NULL;
char line[4096] = { 0 };
bool object_header_seen = false;
if (file_path == NULL || result == NULL) {
return -1;
}
result->entries = NULL;
result->count = 0;
char *safe_path = neu_path_confine(NULL, file_path);
if (safe_path == NULL) {
return -1;
}
fp = fopen(safe_path, "r");
free(safe_path);
if (fp == NULL) {
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL) {
char object_name_buf[256] = { 0 };
char object_type_buf[32] = { 0 };
char object_instance_buf[32] = { 0 };
char description_buf[512] = { 0 };
char commandable_buf[32] = { 0 };
char address[128] = { 0 };
char *end_ptr = NULL;
long object_type = 0;
long object_instance = 0;
trim_right(line);
if (line[0] == '\0') {
continue;
}
if (line[0] == '#') {
if (strstr(line, "object-type") != NULL &&
strstr(line, "object-instance") != NULL) {
object_header_seen = true;
}
continue;
}
if (!object_header_seen) {
continue;
}
if (read_field(line, 2, object_name_buf, sizeof(object_name_buf)) !=
0) {
object_name_buf[0] = '\0';
}
if (read_field(line, 3, object_type_buf, sizeof(object_type_buf)) !=
0) {
continue;
}
if (read_field(line, 4, object_instance_buf,
sizeof(object_instance_buf)) != 0) {
continue;
}
if (read_field(line, 5, description_buf, sizeof(description_buf)) !=
0) {
description_buf[0] = '\0';
}
if (read_field(line, 9, commandable_buf, sizeof(commandable_buf)) !=
0) {
commandable_buf[0] = '\0';
}
errno = 0;
object_type = strtol(object_type_buf, &end_ptr, 10);
if (errno != 0 || end_ptr == object_type_buf || *end_ptr != '\0') {
continue;
}
errno = 0;
object_instance = strtol(object_instance_buf, &end_ptr, 10);
if (errno != 0 || end_ptr == object_instance_buf || *end_ptr != '\0' ||
object_instance < 0) {
continue;
}
const object_map_t *map = find_object_map((int) object_type);
if (map == NULL) {
continue;
}
snprintf(address, sizeof(address), "%s%ld", map->area, object_instance);
if (push_entry(result, address, object_name_buf,
map->present_value_type, description_buf,
commandable_to_attribute(commandable_buf)) != 0) {
neu_ede_result_uninit(result);
fclose(fp);
return -1;
}
}
fclose(fp);
return 0;
}
void neu_ede_result_uninit(neu_ede_result_t *result)
{
if (result == NULL) {
return;
}
free(result->entries);
result->entries = NULL;
result->count = 0;
}
int neu_ede_parse_file_to_tags(const char *file_path, neu_datatag_t **tags,
size_t *count)
{
neu_ede_result_t result = { 0 };
neu_datatag_t * out = NULL;
size_t parsed_count = 0;
size_t i = 0;
if (tags == NULL || count == NULL) {
return -1;
}
*tags = NULL;
*count = 0;
if (neu_ede_parse_file(file_path, &result) != 0) {
return -1;
}
if (result.count == 0) {
neu_ede_result_uninit(&result);
*tags = NULL;
*count = 0;
return 0;
}
out = calloc(result.count, sizeof(neu_datatag_t));
if (out == NULL) {
neu_ede_result_uninit(&result);
return -1;
}
parsed_count = result.count;
for (i = 0; i < parsed_count; ++i) {
out[i].name = strdup(result.entries[i].name);
out[i].address = strdup(result.entries[i].address);
out[i].attribute = result.entries[i].attribute;
out[i].type = result.entries[i].data_type;
out[i].description = strdup(result.entries[i].description);
out[i].unit = strdup("");
if (out[i].name == NULL || out[i].address == NULL ||
out[i].description == NULL || out[i].unit == NULL) {
neu_ede_tags_uninit(out, parsed_count);
neu_ede_result_uninit(&result);
return -1;
}
}
neu_ede_result_uninit(&result);
*tags = out;
*count = parsed_count;
return 0;
}
void neu_ede_tags_uninit(neu_datatag_t *tags, size_t count)
{
size_t i = 0;
if (tags == NULL) {
return;
}
for (i = 0; i < count; ++i) {
neu_tag_fini(&tags[i]);
}
free(tags);
}
void neu_ede_to_msg(char *driver, const char *path, neu_req_add_gtag_t *cmd)
{
neu_datatag_t *tags = NULL;
size_t count = 0;
const char * base_name = NULL;
char * ext = NULL;
if (driver == NULL || path == NULL || cmd == NULL) {
return;
}
if (neu_ede_parse_file_to_tags(path, &tags, &count) != 0) {
return;
}
strncpy(cmd->driver, driver, NEU_NODE_NAME_LEN - 1);
cmd->n_group = 1;
cmd->groups = calloc(1, sizeof(neu_gdatatag_t));
if (cmd->groups == NULL) {
neu_ede_tags_uninit(tags, count);
return;
}
base_name = get_file_name(path);
if (base_name == NULL || base_name[0] == '\0') {
strncpy(cmd->groups[0].group, "ede_import", NEU_GROUP_NAME_LEN - 1);
} else {
strncpy(cmd->groups[0].group, base_name, NEU_GROUP_NAME_LEN - 1);
ext = strrchr(cmd->groups[0].group, '.');
if (ext != NULL) {
*ext = '\0';
}
}
cmd->groups[0].interval = 1000;
cmd->groups[0].n_tag = (int) count;
cmd->groups[0].tags = tags;
}
int neu_ede_format_address(const char *area, uint32_t address,
const char *property_id, bool custom,
uint32_t custom_id, char *out, size_t out_size)
{
if (area == NULL || out == NULL || out_size == 0) {
return -1;
}
if (custom) {
if (snprintf(out, out_size, "%s%u.custom.%u", area, address,
custom_id) >= (int) out_size) {
return -1;
}
return 0;
}
if (property_id == NULL || property_id[0] == '\0') {
if (snprintf(out, out_size, "%s%u", area, address) >= (int) out_size) {
return -1;
}
return 0;
}
if (strcmp(property_id, "NULL") == 0) {
return -1;
}
if (snprintf(out, out_size, "%s%u.%s", area, address, property_id) >=
(int) out_size) {
return -1;
}
return 0;
}
neu_type_e neu_ede_get_data_type(const char *area, const char *property_id,
bool custom)
{
size_t i = 0;
if (custom) {
return NEU_TYPE_CUSTOM;
}
if (property_id != NULL && property_id[0] != '\0') {
if (strcmp(property_id, "NULL") == 0) {
return NEU_TYPE_ERROR;
}
for (i = 0; i < sizeof(g_property_map) / sizeof(g_property_map[0]);
++i) {
if (strcmp(property_id, g_property_map[i].property_id) == 0) {
return g_property_map[i].type;
}
}
return NEU_TYPE_ERROR;
}
if (area == NULL) {
return NEU_TYPE_ERROR;
}
for (i = 0; i < sizeof(g_object_map) / sizeof(g_object_map[0]); ++i) {
if (strcmp(area, g_object_map[i].area) == 0) {
if (strcmp(area, "DEV") == 0) {
return NEU_TYPE_ERROR;
}
return g_object_map[i].present_value_type;
}
}
return NEU_TYPE_ERROR;
}
+5 -1
View File
@@ -67,6 +67,9 @@ ssize_t neu_url_decode(const char *s, size_t len, char *buf, size_t size)
size_t i = 0, j = 0;
int n = 0;
unsigned int c;
if (size == 0) {
return -1;
}
while (i < len && j < size) {
c = s[i++];
if ('+' == c) {
@@ -440,7 +443,8 @@ int neu_http_post_otel_trace(uint8_t *data, int len)
char url_buf[128] = { 0 };
sprintf(url_buf, "http://%s/v1/traces", neu_otel_collector_url());
snprintf(url_buf, sizeof(url_buf), "http://%s/v1/traces",
neu_otel_collector_url());
if (nng_url_parse(&url, url_buf) != 0) {
nng_aio_free(aio);
+115 -117
View File
@@ -18,6 +18,7 @@
**/
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -544,6 +545,13 @@ static int decode_object(json_t *root, neu_json_elem_t *ele)
}
}
if (json_is_array(ob) && json_array_size(ob) > UINT16_MAX) {
ele->ok = false;
zlog_error(neuron, "json decode: array too long (%zu elements)",
json_array_size(ob));
return -1;
}
ele->ok = true;
switch (ele->t) {
@@ -586,255 +594,245 @@ static int decode_object(json_t *root, neu_json_elem_t *ele)
ele->v.val_bool = json_boolean_value(ob);
break;
case NEU_JSON_ARRAY_STR: {
json_t *value = NULL;
ele->v.val_array_str.length = json_array_size(ob);
ele->v.val_array_str.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_str.length > 0) {
int index = 0;
ele->v.val_array_str.p_strs =
calloc(ele->v.val_array_str.length, sizeof(char *));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_str.p_strs) {
ele->v.val_array_str.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_str.length; i++) {
json_t * value = json_array_get(ob, i);
const char *str_val = json_string_value(value);
if (str_val != NULL) {
ele->v.val_array_str.p_strs[index] = strdup(str_val);
}
ele->v.val_array_str.p_strs[i] =
strdup(str_val != NULL ? str_val : "");
}
}
break;
}
case NEU_JSON_ARRAY_BOOL: {
json_t *value = NULL;
ele->v.val_array_bool.length = json_array_size(ob);
ele->v.val_array_bool.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_bool.length > 0) {
int index = 0;
ele->v.val_array_bool.bools =
calloc(ele->v.val_array_bool.length, sizeof(bool));
json_array_foreach(ob, index, value)
{
ele->v.val_array_bool.bools[index] = json_boolean_value(value);
if (NULL == ele->v.val_array_bool.bools) {
ele->v.val_array_bool.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_bool.length; i++) {
json_t *value = json_array_get(ob, i);
ele->v.val_array_bool.bools[i] = json_boolean_value(value);
}
}
break;
}
case NEU_JSON_ARRAY_UINT8: {
json_t *value = NULL;
ele->v.val_array_uint8.length = json_array_size(ob);
ele->v.val_array_uint8.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_uint8.length > 0) {
int index = 0;
ele->v.val_array_uint8.u8s =
calloc(ele->v.val_array_uint8.length, sizeof(int8_t));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_uint8.u8s) {
ele->v.val_array_uint8.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_uint8.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_real(value)) {
ele->v.val_array_uint8.u8s[index] =
ele->v.val_array_uint8.u8s[i] =
(uint8_t) json_real_value(value);
} else {
ele->v.val_array_uint8.u8s[index] =
json_integer_value(value);
ele->v.val_array_uint8.u8s[i] = json_integer_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_INT8: {
json_t *value = NULL;
ele->v.val_array_int8.length = json_array_size(ob);
ele->v.val_array_int8.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_int8.length > 0) {
int index = 0;
ele->v.val_array_int8.i8s =
calloc(ele->v.val_array_int8.length, sizeof(int8_t));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_int8.i8s) {
ele->v.val_array_int8.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_int8.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_real(value)) {
ele->v.val_array_int8.i8s[index] =
ele->v.val_array_int8.i8s[i] =
(int8_t) json_real_value(value);
} else {
ele->v.val_array_int8.i8s[index] =
json_integer_value(value);
ele->v.val_array_int8.i8s[i] = json_integer_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_UINT16: {
json_t *value = NULL;
ele->v.val_array_uint16.length = json_array_size(ob);
ele->v.val_array_uint16.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_uint16.length > 0) {
int index = 0;
ele->v.val_array_uint16.u16s =
calloc(ele->v.val_array_uint16.length, sizeof(int16_t));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_uint16.u16s) {
ele->v.val_array_uint16.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_uint16.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_real(value)) {
ele->v.val_array_uint16.u16s[index] =
ele->v.val_array_uint16.u16s[i] =
(uint16_t) json_real_value(value);
} else {
ele->v.val_array_uint16.u16s[index] =
json_integer_value(value);
ele->v.val_array_uint16.u16s[i] = json_integer_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_INT16: {
json_t *value = NULL;
ele->v.val_array_int16.length = json_array_size(ob);
ele->v.val_array_int16.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_int16.length > 0) {
int index = 0;
ele->v.val_array_int16.i16s =
calloc(ele->v.val_array_int16.length, sizeof(int16_t));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_int16.i16s) {
ele->v.val_array_int16.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_int16.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_real(value)) {
ele->v.val_array_int16.i16s[index] =
ele->v.val_array_int16.i16s[i] =
(int16_t) json_real_value(value);
} else {
ele->v.val_array_int16.i16s[index] =
json_integer_value(value);
ele->v.val_array_int16.i16s[i] = json_integer_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_UINT32: {
json_t *value = NULL;
ele->v.val_array_uint32.length = json_array_size(ob);
ele->v.val_array_uint32.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_uint32.length > 0) {
int index = 0;
ele->v.val_array_uint32.u32s =
calloc(ele->v.val_array_uint32.length, sizeof(int32_t));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_uint32.u32s) {
ele->v.val_array_uint32.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_uint32.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_real(value)) {
ele->v.val_array_uint32.u32s[index] =
ele->v.val_array_uint32.u32s[i] =
(uint32_t) json_real_value(value);
} else {
ele->v.val_array_uint32.u32s[index] =
json_integer_value(value);
ele->v.val_array_uint32.u32s[i] = json_integer_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_INT32: {
json_t *value = NULL;
ele->v.val_array_int32.length = json_array_size(ob);
ele->v.val_array_int32.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_int32.length > 0) {
int index = 0;
ele->v.val_array_int32.i32s =
calloc(ele->v.val_array_int32.length, sizeof(int32_t));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_int32.i32s) {
ele->v.val_array_int32.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_int32.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_real(value)) {
ele->v.val_array_int32.i32s[index] =
ele->v.val_array_int32.i32s[i] =
(int32_t) json_real_value(value);
} else {
ele->v.val_array_int32.i32s[index] =
json_integer_value(value);
ele->v.val_array_int32.i32s[i] = json_integer_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_UINT64: {
json_t *value = NULL;
ele->v.val_array_uint64.length = json_array_size(ob);
ele->v.val_array_uint64.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_uint64.length > 0) {
int index = 0;
ele->v.val_array_uint64.u64s =
calloc(ele->v.val_array_uint64.length, sizeof(int64_t));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_uint64.u64s) {
ele->v.val_array_uint64.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_uint64.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_real(value)) {
ele->v.val_array_uint64.u64s[index] =
ele->v.val_array_uint64.u64s[i] =
(uint64_t) json_real_value(value);
} else {
ele->v.val_array_uint64.u64s[index] =
json_integer_value(value);
ele->v.val_array_uint64.u64s[i] = json_integer_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_INT64: {
json_t *value = NULL;
ele->v.val_array_int64.length = json_array_size(ob);
ele->v.val_array_int64.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_int64.length > 0) {
int index = 0;
ele->v.val_array_int64.i64s =
calloc(ele->v.val_array_int64.length, sizeof(int64_t));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_int64.i64s) {
ele->v.val_array_int64.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_int64.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_real(value)) {
ele->v.val_array_int64.i64s[index] =
ele->v.val_array_int64.i64s[i] =
(int64_t) json_real_value(value);
} else {
ele->v.val_array_int64.i64s[index] =
json_integer_value(value);
ele->v.val_array_int64.i64s[i] = json_integer_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_FLOAT: {
json_t *value = NULL;
ele->v.val_array_float.length = json_array_size(ob);
ele->v.val_array_float.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_float.length > 0) {
int index = 0;
ele->v.val_array_float.f32s =
calloc(ele->v.val_array_float.length, sizeof(float));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_float.f32s) {
ele->v.val_array_float.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_float.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_integer(value)) {
ele->v.val_array_float.f32s[index] =
ele->v.val_array_float.f32s[i] =
(float) json_integer_value(value);
} else {
ele->v.val_array_float.f32s[index] = json_real_value(value);
ele->v.val_array_float.f32s[i] = json_real_value(value);
}
}
}
break;
}
case NEU_JSON_ARRAY_DOUBLE: {
json_t *value = NULL;
ele->v.val_array_double.length = json_array_size(ob);
ele->v.val_array_double.length = (uint16_t) json_array_size(ob);
if (ele->v.val_array_double.length > 0) {
int index = 0;
ele->v.val_array_double.f64s =
calloc(ele->v.val_array_double.length, sizeof(double));
json_array_foreach(ob, index, value)
{
if (NULL == ele->v.val_array_double.f64s) {
ele->v.val_array_double.length = 0;
break;
}
for (int i = 0; i < ele->v.val_array_double.length; i++) {
json_t *value = json_array_get(ob, i);
if (json_is_integer(value)) {
ele->v.val_array_double.f64s[index] =
ele->v.val_array_double.f64s[i] =
(double) json_integer_value(value);
} else {
ele->v.val_array_double.f64s[index] =
json_real_value(value);
ele->v.val_array_double.f64s[i] = json_real_value(value);
}
}
}
+147 -57
View File
@@ -20,10 +20,16 @@
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <jwt.h>
@@ -39,50 +45,12 @@ struct public_key_store {
int size;
};
bool disable_jwt = false;
static struct public_key_store key_store;
static char neuron_private_key[2048] = { 0 };
static char neuron_public_key[2048] = { 0 };
static const char *private_key_content =
"-----BEGIN RSA PRIVATE KEY-----\n"
"MIIEpAIBAAKCAQEAzrO7FUz4GGKl4nP5lwKMh5uageJAAHnLZpVxTR1wXA9E870s\n"
"U03gHnPWsh2bXCCx85ymYaiu/5VlptgnU0rQB1I3xGRG8eHmHPrhHNWDRKpY6oY8\n"
"AnTPqXvfa/Z4u+njJzDSqi4qM8Gwfqlgpjlg8DUF+sheLx8hL+x9XFzovTxafrmy\n"
"JRRAVQTkhB7esKKqRF8BMjdOdYZxJEv9jxa0BwXcjprPDlj5TV6k3wJqbq4UtnEE\n"
"VbjEQKqFVkfY4FuJ5NSPHpGEix94PGQGRsiWkGX0vx5udHJ0SlXM6qpMLny18FhW\n"
"bwKNX3urc3mImn/j3zcJ5x1d+FlXAXs1NBSeTQIDAQABAoIBAQDJOT8PZXbAhohn\n"
"A/AeimS0P08S0mbsD6VroGBEajxP4q2FeswD7PQZsTt4+kmcTlfuiLmQqN50AcSL\n"
"wDHIbDRIbEnN7rECGKAj5jfwEgtQdWVKKpOQ8JaYr/a466BtjyuLo4PyGC8NY6mm\n"
"JM3qBEHSlkvT7+uAhBWSye7gU7JfRPoCeFRpaMMC3Ad0DaIt/Pdt9CddNwj56+9j\n"
"gl//blNnBImjvA7/kh9gL69SYVrQoG9vm/UgFbdIwd+w2hhhpx6kuHjRJ21xIV/y\n"
"Yh9k4QhPr7zIkhQ8yn11ZhyZkMC8XAMy7NKCmAFIPNJ8jwvgBkunPEk2aYBeDUaq\n"
"a30VwlIhAoGBAOnm8DW5DROcfSj4Q8zsvgmvhGM0zzF5zmA+4E+tEbf4t6sDUjSs\n"
"ADo2EaiJZz75vLsD7UZHKzFEV/wnuT7SiNCbrJafx8Tp2UgijTkW8gEhAWREJDn5\n"
"FS375RKFdEcpmVHWgsuz4z/0L3BGxmyYWQxx5pD2NL49ZpTIHAHm/XETAoGBAOI6\n"
"8Y08TmM2bnXbL/MggYMJlqOyv27ueVYz9of3FYFAMIe7ebSgrlSCOwsoDZYmegBM\n"
"OrowR3AAnoP4fjGNbIPjNYbiv1qLOdtnvPxYotEz+ZaUBSn9V/gUEVFlgrKxgTKr\n"
"sqi/QMyyJplBTUkUuqYRDuv6TZ3OWKiDUoXTdv8fAoGBAIbbyf+PlESMY3Vtvtm2\n"
"XdODyRbR6ewiyKShW/9UT/T8iBknrwwDZ5YoeoHrxwV+RBynpPRyMCsVto0B8kKQ\n"
"bKWqPBYURb/4/Hgkw7v4yMtx1jWTPDfYryd2JptJKsOk7mtK/Nqp+wpypa9cfyc5\n"
"p44PVdqauco96Jk7zzohjlrVAoGAfjQDrJaH5DDpTjYIeckYdtFSh9+fi3LdnYk/\n"
"bnoYNRJqAE5Fhs5ccih0Z7TgX3L3fFMKL/Pe5kxyIYzuWRZcAvctVSIJPamNjShB\n"
"9UQ9EBe+lJHej54VBP+s6YuHbcg4GtxNvnVy4L5Bah1T0AEQXrQFbv8jbXU/YEJi\n"
"NuXQ7GECgYA0UNvuZxVSDn8fBxARcX3ub5aok5owdLL3SOCPMpNrmOBQj8iJGT+5\n"
"nv7fH1TWVh9yUdRwhd81s5H/PO3/myLmgq0TcPvewtEEwY+++QpsYZwHTDXgxbbw\n"
"++lmJali4fzmd/72JqocGABYhZFt0jbcQ1QEcqUYcD380+cNVZPO+w==\n"
"-----END RSA PRIVATE KEY-----\n";
const char *public_key_content =
"-----BEGIN PUBLIC KEY-----\n"
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzrO7FUz4GGKl4nP5lwKM\n"
"h5uageJAAHnLZpVxTR1wXA9E870sU03gHnPWsh2bXCCx85ymYaiu/5VlptgnU0rQ\n"
"B1I3xGRG8eHmHPrhHNWDRKpY6oY8AnTPqXvfa/Z4u+njJzDSqi4qM8Gwfqlgpjlg\n"
"8DUF+sheLx8hL+x9XFzovTxafrmyJRRAVQTkhB7esKKqRF8BMjdOdYZxJEv9jxa0\n"
"BwXcjprPDlj5TV6k3wJqbq4UtnEEVbjEQKqFVkfY4FuJ5NSPHpGEix94PGQGRsiW\n"
"kGX0vx5udHJ0SlXM6qpMLny18FhWbwKNX3urc3mImn/j3zcJ5x1d+FlXAXs1NBSe\n"
"TQIDAQAB\n"
"-----END PUBLIC KEY-----\n";
static int find_key(const char *name)
{
for (int i = 0; i < key_store.size; i++) {
@@ -105,7 +73,8 @@ static void add_key(char *name, char *value)
sizeof(key_store.key[key_store.size - 1].key) - 1);
}
static char *load_key(const char *dir, char *name)
static bool load_key_file(const char *dir, const char *name, char *dst,
size_t dst_len)
{
FILE * f = NULL;
int len = 0;
@@ -117,31 +86,146 @@ static char *load_key(const char *dir, char *name)
f = fopen(path, "r");
if (f == NULL) {
zlog_error(neuron, "Failed to open file: %s, errno: %d", name, errno);
return NULL;
return false;
}
len = fread(content, 1, sizeof(content), f);
if (len <= 0) {
zlog_error(neuron, "Failed to read file: %s, errno: %d", name, errno);
fclose(f);
return NULL;
return false;
}
fclose(f);
return content;
strncpy(dst, content, dst_len - 1);
dst[dst_len - 1] = '\0';
return true;
}
static bool write_private_key(FILE *file, EVP_PKEY *pkey)
{
if (fchmod(fileno(file), S_IRUSR | S_IWUSR) != 0) {
return false;
}
return PEM_write_PrivateKey(file, pkey, NULL, NULL, 0, NULL, NULL) == 1;
}
static bool write_public_key(FILE *file, EVP_PKEY *pkey)
{
if (fchmod(fileno(file), S_IRUSR | S_IWUSR) != 0) {
return false;
}
return PEM_write_PUBKEY(file, pkey) == 1;
}
static bool generate_key_pair(const char *dir_path)
{
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY * pkey = NULL;
FILE * private_file = NULL;
FILE * public_file = NULL;
char private_path[256] = { 0 };
char public_path[256] = { 0 };
bool ok = false;
snprintf(private_path, sizeof(private_path), "%s/%s", dir_path,
"neuron.key");
snprintf(public_path, sizeof(public_path), "%s/%s", dir_path,
"neuron.key.pub");
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (ctx == NULL) {
return false;
}
if (EVP_PKEY_keygen_init(ctx) <= 0 ||
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0 ||
EVP_PKEY_keygen(ctx, &pkey) <= 0) {
goto done;
}
private_file = fopen(private_path, "w");
if (private_file == NULL) {
goto done;
}
public_file = fopen(public_path, "w");
if (public_file == NULL) {
goto done;
}
ok = write_private_key(private_file, pkey) &&
write_public_key(public_file, pkey);
done:
if (private_file != NULL) {
fclose(private_file);
}
if (public_file != NULL) {
fclose(public_file);
}
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(ctx);
if (!ok) {
unlink(private_path);
unlink(public_path);
}
return ok;
}
static void load_neuron_key(const char *dir_path)
{
char private_path[256] = { 0 };
char public_path[256] = { 0 };
(void) dir_path;
strncpy(neuron_private_key, private_key_content,
sizeof(neuron_private_key) - 1);
if (dir_path == NULL || *dir_path == '\0') {
zlog_error(neuron, "JWT config directory is empty");
return;
}
strncpy(neuron_public_key, public_key_content,
sizeof(neuron_public_key) - 1);
snprintf(private_path, sizeof(private_path), "%s/%s", dir_path,
"neuron.key");
snprintf(public_path, sizeof(public_path), "%s/%s", dir_path,
"neuron.key.pub");
if (!load_key_file(dir_path, "neuron.key", neuron_private_key,
sizeof(neuron_private_key)) ||
!load_key_file(dir_path, "neuron.key.pub", neuron_public_key,
sizeof(neuron_public_key))) {
zlog_warn(neuron, "Missing JWT RSA key pair in %s, generating new keys",
dir_path);
if (!generate_key_pair(dir_path)) {
zlog_error(neuron, "Failed to generate JWT RSA key pair in %s",
dir_path);
neuron_private_key[0] = '\0';
neuron_public_key[0] = '\0';
return;
}
zlog_notice(neuron,
"Generated JWT RSA key pair in config directory: %s",
dir_path);
if (!load_key_file(dir_path, "neuron.key", neuron_private_key,
sizeof(neuron_private_key)) ||
!load_key_file(dir_path, "neuron.key.pub", neuron_public_key,
sizeof(neuron_public_key))) {
zlog_error(neuron,
"Failed to load generated JWT RSA key pair from %s",
dir_path);
neuron_private_key[0] = '\0';
neuron_public_key[0] = '\0';
return;
}
} else {
zlog_notice(neuron,
"Load external RSA key pair from config directory: %s",
dir_path);
}
}
static void scanf_key(const char *dir_path)
@@ -158,8 +242,6 @@ static void scanf_key(const char *dir_path)
}
while (NULL != (ptr = readdir(dir))) {
char *content = NULL;
if (!strcmp((char *) ptr->d_name, ".") ||
!strcmp((char *) ptr->d_name, "..")) {
continue;
@@ -167,8 +249,9 @@ static void scanf_key(const char *dir_path)
if (strstr((char *) ptr->d_name, ".pem") != NULL ||
strstr((char *) ptr->d_name, ".pub") != NULL) {
content = load_key(dir_path, (char *) ptr->d_name);
assert(content != NULL);
char content[2047] = { 0 };
assert(load_key_file(dir_path, (char *) ptr->d_name, content,
sizeof(content)));
add_key((char *) ptr->d_name, content);
}
}
@@ -178,9 +261,16 @@ static void scanf_key(const char *dir_path)
int neu_jwt_init(const char *dir_path)
{
if (disable_jwt) {
zlog_notice(neuron,
"HTTP auth is disabled, skip JWT RSA key generation");
return 0;
}
load_neuron_key(dir_path);
scanf_key("certs");
return 0;
return neuron_private_key[0] == '\0' || neuron_public_key[0] == '\0' ? -1
: 0;
}
int neu_jwt_new(char **token, const char *user)
+122
View File
@@ -0,0 +1,122 @@
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2024 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils/neu_path.h"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
bool neu_path_is_valid_component(const char *name)
{
if (name == NULL || name[0] == '\0') {
return false;
}
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
return false;
}
// A safe component must not contain a path separator.
if (strchr(name, '/') != NULL) {
return false;
}
return true;
}
// Return true if `resolved` is `base` or lies below `base/`.
static bool is_within(const char *base, const char *resolved)
{
size_t bl = strlen(base);
if (strncmp(resolved, base, bl) != 0) {
return false;
}
// Exact match, or the next char is a separator (a real sub-path).
return resolved[bl] == '\0' || resolved[bl] == '/';
}
char *neu_path_confine(const char *base, const char *path)
{
if (path == NULL || path[0] == '\0') {
return NULL;
}
char base_real[PATH_MAX];
if (base == NULL || base[0] == '\0') {
base = ".";
}
if (realpath(base, base_real) == NULL) {
return NULL;
}
// Build an absolute candidate: absolute paths are taken as-is (and must
// still resolve inside base), relative paths are joined under base.
char candidate[PATH_MAX];
if (path[0] == '/') {
if (strlen(path) >= sizeof(candidate)) {
return NULL;
}
strcpy(candidate, path);
} else {
if ((size_t) snprintf(candidate, sizeof(candidate), "%s/%s", base_real,
path) >= sizeof(candidate)) {
return NULL;
}
}
char resolved[PATH_MAX];
if (realpath(candidate, resolved) == NULL) {
// The final component may not exist yet (e.g. a file about to be
// created). Resolve the parent directory and re-append the basename.
char tmp[PATH_MAX];
strncpy(tmp, candidate, sizeof(tmp));
tmp[sizeof(tmp) - 1] = '\0';
char *slash = strrchr(tmp, '/');
if (slash == NULL) {
return NULL;
}
*slash = '\0';
const char *bname = slash + 1;
if (!neu_path_is_valid_component(bname)) {
return NULL;
}
char parent_real[PATH_MAX];
if (realpath(tmp[0] != '\0' ? tmp : "/", parent_real) == NULL) {
return NULL;
}
if (!is_within(base_real, parent_real)) {
return NULL;
}
if ((size_t) snprintf(resolved, sizeof(resolved), "%s/%s", parent_real,
bname) >= sizeof(resolved)) {
return NULL;
}
return strdup(resolved);
}
if (!is_within(base_real, resolved)) {
return NULL;
}
return strdup(resolved);
}
+11 -2
View File
@@ -20,6 +20,7 @@
#include <libxml/tree.h>
#include "utils/log.h"
#include "utils/neu_path.h"
#include "utils/tpy.h"
static int parse_vars(xmlNode *xml_ied, tpy_t *tpy);
@@ -27,7 +28,14 @@ static int parse_vars(xmlNode *xml_ied, tpy_t *tpy);
int neu_tpy_parse(const char *path, tpy_t *tpy)
{
memset(tpy, 0, sizeof(tpy_t));
xmlDoc *doc = xmlReadFile(path, NULL, 0);
char *safe_path = neu_path_confine(NULL, path);
if (safe_path == NULL) {
nlog_warn("reject tpy file path outside working dir: %s", path);
return -1;
}
// XML_PARSE_NONET blocks network access for external entities/DTDs (XXE).
xmlDoc *doc = xmlReadFile(safe_path, NULL, XML_PARSE_NONET);
free(safe_path);
if (doc == NULL) {
nlog_warn("Failed to read tpy file %s", path);
return -1;
@@ -101,7 +109,8 @@ static int parse_vars(xmlNode *xml_ied, tpy_t *tpy)
attr, (const xmlChar *) "TaskPrio");
snprintf(static_name, sizeof(static_name), "%s_%s",
name_static, name_task);
name_static != NULL ? name_static : "",
name_task != NULL ? name_task : "");
strncpy(tpy_var.name, content,
sizeof(tpy_var.name) - 1);
xmlFree(name_static);
+9
View File
@@ -123,6 +123,7 @@ def conf_base(mocker):
"host": HUB,
"auth": 0,
"sas": SAS,
"ca": mocker.ca_base64,
}
@@ -174,6 +175,13 @@ def conf_bad_fmt(conf_base):
}
@pytest.fixture(scope="function")
def conf_no_ca(conf_base):
conf = {**conf_base}
del conf["ca"]
return conf
@pytest.fixture(scope="function")
def conf_ssl_bad_ca(conf_ssl):
return {
@@ -230,6 +238,7 @@ class TestAzure:
"conf_bad_id",
"conf_bad_qos",
"conf_bad_fmt",
"conf_no_ca",
"conf_ssl_bad_ca",
"conf_ssl_no_cert",
"conf_ssl_bad_cert",
+11
View File
@@ -184,6 +184,7 @@ def conf_emqx_ssl(conf_base, mocker):
"host": "broker.emqx.io",
"port": 8883,
"ssl": True,
"ca": mocker.ca_base64,
}
@@ -262,6 +263,15 @@ def conf_bad_fmt(conf_base):
}
@pytest.fixture(scope="function")
def conf_ssl_no_ca(conf_base, mocker):
return {
**conf_base,
"port": mocker.ssl_port,
"ssl": True,
}
@pytest.fixture(scope="function")
def conf_ssl_bad_ca(conf_ssl1):
return {
@@ -374,6 +384,7 @@ class TestMQTT:
"conf_bad_id",
"conf_bad_qos",
"conf_bad_fmt",
"conf_ssl_no_ca",
"conf_ssl_bad_ca",
"conf_ssl_bad_cert",
"conf_ssl_no_key",
+40
View File
@@ -1,5 +1,11 @@
import pytest
import inspect
import neuron.api as api
import neuron.config as config
import neuron.process as process
def pytest_addoption(parser):
parser.addoption(
@@ -10,3 +16,37 @@ def pytest_addoption(parser):
@pytest.fixture(autouse=True, scope="session")
def get_envx(request):
return request.config.getoption("--env")
def _refresh_default_jwt(token):
old_jwt = config.default_jwt
new_jwt = f"Bearer {token}"
config.default_jwt = new_jwt
for _, func in inspect.getmembers(api, inspect.isfunction):
defaults = func.__defaults__
if defaults:
func.__defaults__ = tuple(
new_jwt if value == old_jwt else value for value in defaults
)
kwdefaults = func.__kwdefaults__
if kwdefaults:
func.__kwdefaults__ = {
key: new_jwt if value == old_jwt else value
for key, value in kwdefaults.items()
}
@pytest.fixture(autouse=True, scope="session")
def init_default_jwt():
process.remove_persistence()
neuron = process.NeuronProcess()
neuron.start()
response = api.login()
assert 200 == response.status_code
_refresh_default_jwt(response.json()["token"])
neuron.stop()
+2 -2
View File
@@ -46,9 +46,9 @@ class TestGroup:
assert 200 == response.status_code
assert NEU_ERR_SUCCESS == response.json()['error']
@description(given="existent node", when="add group with interval < 100 under existent node", then="add failed")
@description(given="existent node", when="add group with interval < 30 under existent node", then="add failed")
def test_add_group_wrong_interval(self):
response = api.add_group(node="modbus-tcp", group='group', interval=99)
response = api.add_group(node="modbus-tcp", group='group-bad-interval', interval=29)
assert 400 == response.status_code
assert NEU_ERR_GROUP_PARAMETER_INVALID == response.json()['error']
+1 -12
View File
@@ -126,15 +126,4 @@ class TestLog:
response = api.get_nodes_state(node='')
assert 200 == response.status_code
assert "notice" == response.json()['neuron_core']
@description(given="neuron started", when="get log list", then="success")
def test_get_log_list(self):
response = api.get_log_list()
assert 200 == response.status_code
print(response.json())
assert "neuron.log" in [item['file'] for item in response.json()["files"]]
@description(given="neuron started", when="get log file", then="success")
def test_get_log_file(self):
response = api.get_log_file(file_name='neuron.log')
assert 200 == response.status_code
+13
View File
@@ -215,6 +215,19 @@ class TestLaunch:
os.remove(dst_file)
os.rename(dst_backup, dst_file)
@description(given="disable_auth enabled", when="call login", then="login should still succeed")
def test_login_with_disable_auth(self):
process.remove_persistence()
p = process.NeuronProcess()
p.start_disable_auth()
response = api.login()
assert 200 == response.status_code
assert "" == response.json()["token"]
p.stop()
@description(given="encironment variables 1, command line 0",
when="start neuron",
then="log level set right")
+1 -1
View File
@@ -1,5 +1,5 @@
BASE_URL = "http://127.0.0.1:7000"
default_jwt = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJuZXVyb24iLCJib2R5RW5jb2RlIjowLCJleHAiOjE3ODExNTc4MzcsImlhdCI6MTcxODA4NTgzOCwiaXNzIjoibmV1cm9uIn0.OmMIxGiaJYcuetslmliVGAWtjIZxNgvOCsZGdljHUSnFBOpTV1BqFSBqR55tZx51z09RPEdm8tIeGZN-jUUQgXgS22K7LUqhAYXgGY8bhh37pN6GXPzaHCdjan3VzS_5ywuiGHgNIjr5RrHNTCjLGb_dYlUGL-Hv0z0cOC6ObbqS0snlEOJsxNcjOaRMqbpwtxuTKc4_QA0yWtYTyGXjhXVA2ZAg1HsF9M_9XJ2qk-_6qEadAR2MzjphJvsk-um1oHOGtJhGGxlRNONL6yxFav2BTHs2dSz2P1YanVsUtFF0HO44HYPU4vs511RuOvcHyTM1TV3BudKgDpCeWKpq1Q"
default_jwt = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJuZXVyb24iLCJib2R5RW5jb2RlIjowLCJleHAiOjIwOTY1MjE2MDgsImlhdCI6MTc4MTE2MTYwOCwiaXNzIjoibmV1cm9uIiwidXNlciI6Im5ldXJvbiJ9.JHStM1lxjL3S5cUoqk8RM4LmiuEZTB25UlLmduEbyUr-R72xmCcYqeImnNUcZnPE-ZSMCR2QzwOUpZ10-UPsDo60mzR3fzsockI-SvT1Pf-c6v8YL_1F5VNsfQvxuTq3ZtWxBbEcT13pCc2v9dzr4T15kZBkc6_OKx1SRzxvN5NlmzCAajRXCeA-TDB--4kKBsqt6wb-MwJNsFbKcGhPHwMzO9V0cuVKkDU-mA11L0CxtU5bweDbyOc2Oh8DO5-Uxc9Mfl4kkQmi5BuX0rcTwzFnw8dtjuDdoYhN0VpzFW3I8Ugij45Nn_AaJQuBsArM-A0WPl4tcjGK5XS7KaJPiw"
NEU_NODE_DRIVER = 1
NEU_NODE_APP = 2
+2 -2
View File
@@ -24,14 +24,14 @@ target_include_directories(jwt_test PRIVATE
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src/utils)
target_link_libraries(jwt_test neuron-base gtest_main gtest jansson ssl crypto jwt libzlog.so)
target_link_libraries(jwt_test neuron-base gtest_main gtest jansson OpenSSL::SSL OpenSSL::Crypto jwt libzlog.so)
add_executable(base64_test base64_test.cc ${CMAKE_SOURCE_DIR}/src/utils/base64.c)
target_include_directories(base64_test PRIVATE
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/include
)
target_link_libraries(base64_test neuron-base gtest_main gtest crypto)
target_link_libraries(base64_test neuron-base gtest_main gtest OpenSSL::Crypto)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/base/tag_sort.c SRC_SORT)
add_executable(tag_sort_test tag_sort_test.cc ${SRC_SORT})
+1 -15
View File
@@ -1,10 +1,7 @@
#include <gtest/gtest.h>
#include "json/json.h"
#include "utils/log.h"
#include "parser/neu_json_system.h"
#include "json/json.h"
zlog_category_t *neuron = NULL;
TEST(JsonTest, DecodeField)
@@ -331,17 +328,6 @@ TEST(JsonTest, EncodeArray)
neu_json_decode_free(ob);
}
TEST(JsonTest, System_ctl)
{
char * buf = (char *) "{\"action\": 1}";
char * buf1 = (char *) "{\"action1\": 1}";
neu_json_system_ctl_req_t *req = NULL;
EXPECT_EQ(-1, neu_json_decode_system_ctl_req(buf1, &req));
EXPECT_EQ(0, neu_json_decode_system_ctl_req(buf, &req));
EXPECT_EQ(1, req->action);
free(req);
}
int main(int argc, char **argv)
{
zlog_init("./config/dev.conf");
+1 -1
View File
@@ -1 +1 @@
2.13.0
2.13.1
+1 -2
View File
@@ -9,5 +9,4 @@ syslog = "<P>%d(%Y-%m-%dT%T%z) %H neuron[%p]: level=%v msg="[%c] %m" file="%f:%L
[rules]
*.* "./logs/%c.log", 25MB * 1 ~ "./logs/%c.#2r.log"; simple
*.* $remote_syslog,"%V"; syslog
*.* "./logs/%c.log", 50MB * 2 ~ "./logs/%c.#2r.log"; simple