-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathedge_mgmt.c
179 lines (141 loc) · 5.28 KB
/
edge_mgmt.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* edge_mgmt.c
*
* Created on: Aug 22, 2013
* Author: wolf
*/
#include "edge_mgmt.h"
#include "n2n_log.h"
#include <string.h>
#define CMD_STOP "stop"
#define CMD_HELP "help"
#define CMD_VERB_INC "+verb"
#define CMD_VERB_DEC "-verb"
#define CMD_RELOAD "reload"
static size_t build_stats_response(n2n_edge_t *eee, uint8_t rsp_buf[])
{
size_t msg_len = 0;
time_t now = time(NULL);
traceDebug("mgmt status rq");
msg_len += snprintf((char *) (rsp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
"Statistics for edge\n");
msg_len += snprintf((char *) (rsp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
"uptime %lu\n",
time(NULL) - eee->start_time);
msg_len += snprintf((char *) (rsp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
"paths super:%u,%u p2p:%u,%u\n",
(unsigned int) eee->tx_sup,
(unsigned int) eee->rx_sup,
(unsigned int) eee->tx_p2p,
(unsigned int) eee->rx_p2p);
msg_len += snprintf((char *) (rsp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
"trans:null |%6u|%6u|\n"
"trans:tf |%6u|%6u|\n"
"trans:aes |%6u|%6u|\n",
(unsigned int) eee->transop[N2N_TRANSOP_NULL_IDX].tx_cnt,
(unsigned int) eee->transop[N2N_TRANSOP_NULL_IDX].rx_cnt,
(unsigned int) eee->transop[N2N_TRANSOP_TF_IDX].tx_cnt,
(unsigned int) eee->transop[N2N_TRANSOP_TF_IDX].rx_cnt,
(unsigned int) eee->transop[N2N_TRANSOP_AESCBC_IDX].tx_cnt,
(unsigned int) eee->transop[N2N_TRANSOP_AESCBC_IDX].rx_cnt);
msg_len += snprintf((char *) (rsp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
"peers pend:%u full:%u\n",
(unsigned int) list_size(&eee->pending_peers),
(unsigned int) list_size(&eee->known_peers));
msg_len += snprintf((char *) (rsp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
"last super:%lu(%ld sec ago) p2p:%lu(%ld sec ago)\n",
eee->last_sup, (now - eee->last_sup), eee->last_p2p, (now - eee->last_p2p));
return msg_len;
}
static size_t build_help_response(uint8_t rsp_buf[])
{
size_t msg_len = 0;
msg_len = snprintf((char *) rsp_buf, N2N_PKT_BUF_SIZE,
"Help for edge management console:\n"
" %7s Gracefully exit edge\n"
" %7s This help message\n"
" %7s Increase verbosity of logging\n"
" %7s Decrease verbosity of logging\n"
" %7s Re-read the keyschedule\n"
" <enter> Display statistics\n\n",
CMD_STOP, CMD_HELP, CMD_VERB_INC, CMD_VERB_DEC, CMD_RELOAD);
return msg_len;
}
static size_t process_verb_inc_cmd(uint8_t rsp_buf[])
{
size_t msg_len = 0;
++traceLevel;
traceError("+verb traceLevel=%u", (unsigned int) traceLevel);
msg_len += snprintf((char *) rsp_buf, N2N_PKT_BUF_SIZE,
"> +OK traceLevel=%u\n", (unsigned int) traceLevel);
return msg_len;
}
static size_t process_verb_dec_cmd(uint8_t rsp_buf[])
{
size_t msg_len = 0;
if (traceLevel > 0)
{
--traceLevel;
msg_len += snprintf((char *) rsp_buf, N2N_PKT_BUF_SIZE,
"> -OK traceLevel=%u\n", traceLevel);
}
else
{
msg_len += snprintf((char *) rsp_buf, N2N_PKT_BUF_SIZE,
"> -NOK traceLevel=%u\n", traceLevel);
}
traceError("-verb traceLevel=%u", (unsigned int) traceLevel);
return msg_len;
}
static size_t process_reload_cmd(n2n_edge_t *eee, uint8_t buf[])
{
size_t msg_len = 0;
if (strlen(eee->keyschedule) > 0)
{
if (edge_init_keyschedule(eee) == 0)
{
msg_len += snprintf((char *) buf, N2N_PKT_BUF_SIZE, "> OK\n");
}
}
return msg_len;
}
edge_cmd_t process_edge_mgmt(n2n_edge_t *eee,
uint8_t req_buf[], ssize_t req_len,
uint8_t rsp_buf[], size_t *rsp_len)
{
if (req_len >= 4)
{
if (0 == memcmp(req_buf, CMD_STOP, strlen(CMD_STOP)))
{
return EDGE_CMD_STOP;
}
if (0 == memcmp(req_buf, "help", 4))
{
*rsp_len = build_help_response(rsp_buf);
return EDGE_CMD_HELP;
}
}
if (req_len >= 5)
{
if (0 == memcmp(req_buf, CMD_VERB_INC, strlen(CMD_VERB_INC)))
{
*rsp_len = process_verb_inc_cmd(rsp_buf);
return EDGE_CMD_VERB_INC;
}
if (0 == memcmp(req_buf, CMD_VERB_DEC, strlen(CMD_VERB_DEC)))
{
*rsp_len = process_verb_dec_cmd(rsp_buf);
return EDGE_CMD_VERB_DEC;
}
}
if (req_len >= 6)
{
if (0 == memcmp(req_buf, CMD_RELOAD, strlen(CMD_RELOAD)))
{
*rsp_len = process_reload_cmd(eee, rsp_buf);
return EDGE_CMD_RELOAD;
}
}
*rsp_len = build_stats_response(eee, rsp_buf);
return EDGE_CMD_STATS;
}