-
Notifications
You must be signed in to change notification settings - Fork 24
/
cl_stackline_helpers.c
143 lines (129 loc) · 4.23 KB
/
cl_stackline_helpers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (C) 2017 Rahul Jadhav <[email protected]>
*
* This file is subject to the terms and conditions of the GNU
* General Public License v2. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup commline
* @{
*
* @file
* @brief Helper functions for stacklines
*
* @author Rahul Jadhav <[email protected]>
*
* @}
*/
#define _CL_STACKLINE_HELPERS_C_
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <dlfcn.h>
#include <commline.h>
static uint8_t g_serial_id[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
int cl_get_id2longaddr(const uint16_t id, uint8_t *addr, const int addrlen)
{
if (addrlen != 8) {
ERROR("Invalid addrlen:%d\n", addrlen);
return FAILURE;
}
if (id == 0xffff) {
memset(addr, 0, addrlen);
return SUCCESS;
}
char *ptr = (char *)&id;
memcpy(addr, g_serial_id, 8);
addr[6] = ptr[1];
addr[7] = ptr[0];
return SUCCESS;
}
uint16_t cl_get_longaddr2id(const uint8_t *addr)
{
if (!addr || !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5] | addr[6] | addr[7])) {
return 0xffff;
}
uint16_t nodeid;
uint8_t *ptr = (uint8_t *)&nodeid;
ptr[0] = addr[7];
ptr[1] = addr[6];
return nodeid;
}
#if USE_DL //------------------[USE_DL-if]-----------------
void *g_dl_lib_handle = NULL;
typedef int (*cmd_handler_func_t)(uint16_t src_id, char *buf, int len);
#define LOAD_DYN_LIB \
cmd_handler_func_t cmd_func; \
if (!g_dl_lib_handle) { \
g_dl_lib_handle = dlopen(NULL, RTLD_LAZY); \
if (!g_dl_lib_handle) { \
ERROR("Could not load library!!\n"); \
return; \
} \
}
#else //------------------[USE_DL-else]---------------
#define PLAY_CMD(MBUF, CMD) \
else if (!strcmp(cmd, #CMD)) \
{ \
extern int CMD(uint16_t, char *, int); \
(MBUF)->len = CMD(mbuf->src_id, (char *)(MBUF)->buf, (MBUF)->max_len); \
}
#endif //-----------------[USE_DL-endif]---------------
void sl_handle_cmd(msg_buf_t *mbuf)
{
DEFINE_MBUF_SZ(cbuf, MAX_CMD_RSP_SZ);
int aux_len = 0;
char *colon_ptr, cmd[256];
memcpy(cbuf->buf, mbuf->buf, mbuf->len);
cbuf->len = mbuf->len;
mbuf = cbuf;
#if USE_DL
LOAD_DYN_LIB;
#endif
colon_ptr = strchr((char *)mbuf->buf, ':');
if (colon_ptr) {
*colon_ptr++ = 0;
aux_len = strlen(colon_ptr);
strncpy(cmd, (char *)mbuf->buf, sizeof(cmd) - 1);
memmove(mbuf->buf, colon_ptr, aux_len);
} else {
strncpy(cmd, (char *)mbuf->buf, sizeof(cmd) - 1);
}
mbuf->buf[aux_len] = 0;
#if USE_DL
cmd_func = (cmd_handler_func_t)dlsym(g_dl_lib_handle, cmd);
if (!cmd_func) {
ERROR("Could not load cmd: <%s> %s\n", cmd, dlerror());
mbuf->len = snprintf((char *)mbuf->buf, mbuf->max_len, "SL_INVALID_CMD(%s)", mbuf->buf);
cl_sendto_q(MTYPE(MONITOR, CL_MGR_ID), cbuf, cbuf->len + sizeof(msg_buf_t));
}
mbuf->len = cmd_func(mbuf->src_id, (char *)mbuf->buf, mbuf->max_len);
#else
if (0) {
}
PLAY_CMD(mbuf, cmd_rpl_stats)
PLAY_CMD(mbuf, cmd_def_route)
PLAY_CMD(mbuf, cmd_route_table)
PLAY_CMD(mbuf, cmd_rtsize)
PLAY_CMD(mbuf, cmd_node_osname)
PLAY_CMD(mbuf, cmd_ipv6_stats)
PLAY_CMD(mbuf, cmd_nd6_stats)
PLAY_CMD(mbuf, cmd_icmp_stats)
PLAY_CMD(mbuf, cmd_udp_stats)
PLAY_CMD(mbuf, cmd_tcp_stats)
PLAY_CMD(mbuf, cmd_config_info)
PLAY_CMD(mbuf, cmd_start_udp)
PLAY_CMD(mbuf, cmd_get_udpapp_stat)
else
{
char tmpbuf[256];
snprintf(tmpbuf, sizeof(tmpbuf), "%s", mbuf->buf);
mbuf->len = snprintf((char *)mbuf->buf, mbuf->max_len, "SL_INVALID_CMD(%s)", tmpbuf);
}
#endif
INFO("responding with:<%s>\n", cbuf->buf);
cl_sendto_q(MTYPE(MONITOR, CL_MGR_ID), cbuf, cbuf->len + sizeof(msg_buf_t));
}