Skip to content

Commit 3baf3fc

Browse files
authored
Merge pull request #598 from Komzpa/darafei/cmdpipe-bounded-append
fix(cmdpipe): bound probe command appends
2 parents 146fce7 + 0cd99d5 commit 3baf3fc

3 files changed

Lines changed: 158 additions & 117 deletions

File tree

packet/cmdparse.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ enum {
2424
MAX_COMMAND_TOKENS = MAX_COMMAND_ARGUMENTS * 2 + 2
2525
};
2626

27+
#define COMMAND_NAME_SEND_PROBE "send-probe"
28+
29+
#define COMMAND_ARG_BIT_PATTERN "bit-pattern"
30+
#define COMMAND_ARG_IP4 "ip-4"
31+
#define COMMAND_ARG_IP6 "ip-6"
32+
#define COMMAND_ARG_LOCAL_DEVICE "local-device"
33+
#define COMMAND_ARG_LOCAL_IP4 "local-ip-4"
34+
#define COMMAND_ARG_LOCAL_IP6 "local-ip-6"
35+
#define COMMAND_ARG_LOCAL_PORT "local-port"
36+
#define COMMAND_ARG_MARK "mark"
37+
#define COMMAND_ARG_PORT "port"
38+
#define COMMAND_ARG_PROTOCOL "protocol"
39+
#define COMMAND_ARG_SIZE "size"
40+
#define COMMAND_ARG_TIMEOUT "timeout"
41+
#define COMMAND_ARG_TOS "tos"
42+
#define COMMAND_ARG_TTL "ttl"
43+
44+
#define COMMAND_PROTOCOL_ICMP "icmp"
45+
#define COMMAND_PROTOCOL_SCTP "sctp"
46+
#define COMMAND_PROTOCOL_TCP "tcp"
47+
#define COMMAND_PROTOCOL_UDP "udp"
48+
2749
/* Parsed commands, or command replies, ready for semantic interpretation */
2850
struct command_t {
2951
/* A unique value for matching command requests with replies */

packet/command.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,37 +98,37 @@ const char *check_support(
9898
return PACKAGE_VERSION;
9999
}
100100

101-
if (!strcmp(feature, "ip-4")) {
101+
if (!strcmp(feature, COMMAND_ARG_IP4)) {
102102
return check_ip_version_support(net_state, 4);
103103
}
104104

105-
if (!strcmp(feature, "ip-6")) {
105+
if (!strcmp(feature, COMMAND_ARG_IP6)) {
106106
return check_ip_version_support(net_state, 6);
107107
}
108108

109-
if (!strcmp(feature, "send-probe")) {
109+
if (!strcmp(feature, COMMAND_NAME_SEND_PROBE)) {
110110
return "ok";
111111
}
112112

113-
if (!strcmp(feature, "icmp")) {
113+
if (!strcmp(feature, COMMAND_PROTOCOL_ICMP)) {
114114
return check_protocol_support(net_state, IPPROTO_ICMP);
115115
}
116116

117-
if (!strcmp(feature, "udp")) {
117+
if (!strcmp(feature, COMMAND_PROTOCOL_UDP)) {
118118
return check_protocol_support(net_state, IPPROTO_UDP);
119119
}
120120

121-
if (!strcmp(feature, "tcp")) {
121+
if (!strcmp(feature, COMMAND_PROTOCOL_TCP)) {
122122
return check_protocol_support(net_state, IPPROTO_TCP);
123123
}
124124
#ifdef IPPROTO_SCTP
125-
if (!strcmp(feature, "sctp")) {
125+
if (!strcmp(feature, COMMAND_PROTOCOL_SCTP)) {
126126
return check_protocol_support(net_state, IPPROTO_SCTP);
127127
}
128128
#endif
129129

130130
#ifdef SO_MARK
131-
if (!strcmp(feature, "mark")) {
131+
if (!strcmp(feature, COMMAND_ARG_MARK)) {
132132
return "ok";
133133
}
134134
#endif
@@ -168,42 +168,42 @@ bool decode_probe_argument(
168168
char *endstr = NULL;
169169

170170
/* Pass IPv4 addresses as string values */
171-
if (!strcmp(name, "ip-4")) {
171+
if (!strcmp(name, COMMAND_ARG_IP4)) {
172172
param->ip_version = 4;
173173
param->remote_address = value;
174174
}
175175

176176
/* IPv6 address */
177-
if (!strcmp(name, "ip-6")) {
177+
if (!strcmp(name, COMMAND_ARG_IP6)) {
178178
param->ip_version = 6;
179179
param->remote_address = value;
180180
}
181181

182182
/* IPv4 address to send from */
183-
if (!strcmp(name, "local-ip-4")) {
183+
if (!strcmp(name, COMMAND_ARG_LOCAL_IP4)) {
184184
param->local_address = value;
185185
}
186186

187187
/* IPv6 address to send from */
188-
if (!strcmp(name, "local-ip-6")) {
188+
if (!strcmp(name, COMMAND_ARG_LOCAL_IP6)) {
189189
param->local_address = value;
190190
}
191191

192192
/* Device name to send from */
193-
if (!strcmp(name, "local-device")) {
193+
if (!strcmp(name, COMMAND_ARG_LOCAL_DEVICE)) {
194194
param->local_device = value;
195195
}
196196

197197
/* Protocol for the probe */
198-
if (!strcmp(name, "protocol")) {
199-
if (!strcmp(value, "icmp")) {
198+
if (!strcmp(name, COMMAND_ARG_PROTOCOL)) {
199+
if (!strcmp(value, COMMAND_PROTOCOL_ICMP)) {
200200
param->protocol = IPPROTO_ICMP;
201-
} else if (!strcmp(value, "udp")) {
201+
} else if (!strcmp(value, COMMAND_PROTOCOL_UDP)) {
202202
param->protocol = IPPROTO_UDP;
203-
} else if (!strcmp(value, "tcp")) {
203+
} else if (!strcmp(value, COMMAND_PROTOCOL_TCP)) {
204204
param->protocol = IPPROTO_TCP;
205205
#ifdef IPPROTO_SCTP
206-
} else if (!strcmp(value, "sctp")) {
206+
} else if (!strcmp(value, COMMAND_PROTOCOL_SCTP)) {
207207
param->protocol = IPPROTO_SCTP;
208208
#endif
209209
} else {
@@ -212,15 +212,15 @@ bool decode_probe_argument(
212212
}
213213

214214
/* Destination port for the probe */
215-
if (!strcmp(name, "port")) {
215+
if (!strcmp(name, COMMAND_ARG_PORT)) {
216216
param->dest_port = strtol(value, &endstr, 10);
217217
if (*endstr != 0) {
218218
return false;
219219
}
220220
}
221221

222222
/* The local port to send UDP probes from */
223-
if (!strcmp(name, "local-port")) {
223+
if (!strcmp(name, COMMAND_ARG_LOCAL_PORT)) {
224224
param->local_port = strtol(value, &endstr, 10);
225225
if (*endstr != 0) {
226226
return false;
@@ -237,47 +237,47 @@ bool decode_probe_argument(
237237
}
238238

239239
/* The "type of service" field for the IP header */
240-
if (!strcmp(name, "tos")) {
240+
if (!strcmp(name, COMMAND_ARG_TOS)) {
241241
param->type_of_service = strtol(value, &endstr, 10);
242242
if (*endstr != 0) {
243243
return false;
244244
}
245245
}
246246

247247
/* The Linux packet mark for mark-based routing */
248-
if (!strcmp(name, "mark")) {
248+
if (!strcmp(name, COMMAND_ARG_MARK)) {
249249
param->routing_mark = strtol(value, &endstr, 10);
250250
if (*endstr != 0) {
251251
return false;
252252
}
253253
}
254254

255255
/* The size of the packet (including headers) */
256-
if (!strcmp(name, "size")) {
256+
if (!strcmp(name, COMMAND_ARG_SIZE)) {
257257
param->packet_size = strtol(value, &endstr, 10);
258258
if (*endstr != 0) {
259259
return false;
260260
}
261261
}
262262

263263
/* The packet's bytes will be filled with this value */
264-
if (!strcmp(name, "bit-pattern")) {
264+
if (!strcmp(name, COMMAND_ARG_BIT_PATTERN)) {
265265
param->bit_pattern = strtol(value, &endstr, 10);
266266
if (*endstr != 0) {
267267
return false;
268268
}
269269
}
270270

271271
/* Time-to-live values */
272-
if (!strcmp(name, "ttl")) {
272+
if (!strcmp(name, COMMAND_ARG_TTL)) {
273273
param->ttl = strtol(value, &endstr, 10);
274274
if (*endstr != 0) {
275275
return false;
276276
}
277277
}
278278

279279
/* Number of seconds to wait for a reply */
280-
if (!strcmp(name, "timeout")) {
280+
if (!strcmp(name, COMMAND_ARG_TIMEOUT)) {
281281
param->timeout = strtol(value, &endstr, 10);
282282
if (*endstr != 0) {
283283
return false;
@@ -364,7 +364,7 @@ void dispatch_command(
364364
{
365365
if (!strcmp(command->command_name, "check-support")) {
366366
check_support_command(command, net_state);
367-
} else if (!strcmp(command->command_name, "send-probe")) {
367+
} else if (!strcmp(command->command_name, COMMAND_NAME_SEND_PROBE)) {
368368
send_probe_command(command, net_state);
369369
} else {
370370
/* For unrecognized commands, respond with an error */

0 commit comments

Comments
 (0)