forked from sschweiz/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_cmd.c
263 lines (232 loc) · 6.82 KB
/
cli_cmd.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* cli_cmd.c - functions for creating and implementing cli commands
* (C) 2010-2011 Stephen Schweizer <schweizer@alumni.cmu.edu>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curses.h>
#include <arpa/inet.h>
#include "clibase.h"
#include "cli.h"
#include "cli_cmd.h"
void cli_cmd_if_set_type(cli_ctx *ctx, const char *value)
{
cli_if *iface = ctx->ifs[ctx->ifsel];
int tmp;
if (iface != NULL) {
if (strncmp(value, "tcp", 3) == 0) {
memset(&iface->sock, 0, sizeof(struct sockaddr_in));
iface->sock.sin_family = AF_INET;
iface->sock.sin_addr.s_addr = 0x0100007fUL;
iface->sock.sin_port = htons((short)80);
tmp = socket(AF_INET, SOCK_STREAM, 0);
if (tmp < 0) {
cli_print_error("if set type: ");
iface->active = 0;
} else {
iface->rxdev.fd = tmp;
iface->rxopen = 1;
}
iface->type = CLI_TYPE_TCP;
} else if (strncmp(value, "udp", 3) == 0) {
memset(&iface->sock, 0, sizeof(struct sockaddr_in));
iface->sock.sin_family = AF_INET;
iface->sock.sin_addr.s_addr = 0x0100007fUL;
iface->sock.sin_port = htons((short)80);
tmp = socket(AF_INET, SOCK_DGRAM, 0);
if (tmp < 0) {
cli_print_error("if set type: ");
iface->active = 0;
} else {
iface->rxdev.fd = tmp;
iface->rxopen = 1;
}
iface->type = CLI_TYPE_UDP;
} else if ((strncmp(value, "mem", 3) == 0) ||
(strncmp(value, "memory", 6) == 0)) {
iface->type = CLI_TYPE_MEMORY;
iface->rxdev.ptr = NULL;
iface->active = 0;
} else if (strncmp(value, "file", 3) == 0) {
iface->type = CLI_TYPE_FILE;
memset(iface->devname, 0, CLI_DEFAULT_BUFFER);
sprintf(iface->devname, "stdout");
iface->rxdev.fp = stdout;
} else if ((strncmp(value, "bin", 3) == 0) ||
(strncmp(value, "exec", 4) == 0)) {
iface->type = CLI_TYPE_EXEC;
memset(iface->devname, 0, CLI_DEFAULT_BUFFER);
sprintf(iface->devname, "/usr/bin/cat");
iface->active = 0;
} else if (strncmp(value, "serial", 6) == 0) {
memset(iface->devname, 0, CLI_DEFAULT_BUFFER);
sprintf(iface->devname, "/dev/ttyS0");
iface->active = 0;
iface->type = CLI_TYPE_SERIAL;
} else {
printw("Error: `if set' type `%s' unrecognized.\n", value);
}
}
}
void cli_cmd_if_set_buffersize(cli_ctx *ctx, const char *value)
{
int i;
cli_if *iface = ctx->ifs[ctx->ifsel];
if (iface != NULL) {
i = atoi(value);
if (i < CLI_MIN_BUFFER) i = CLI_DEFAULT_BUFFER;
iface->buffer_size = i;
}
}
void cli_cmd_if_set_ipaddr(cli_ctx *ctx, const char *value)
{
cli_if *iface = ctx->ifs[ctx->ifsel];
if (iface != NULL) {
if (inet_pton(AF_INET, value, &iface->sock.sin_addr) != 1) {
printw("Error: `if set' could not parse ip address `%s'.\n", value);
}
}
}
void cli_cmd_if_set_ipport(cli_ctx *ctx, const char *value)
{
int i;
cli_if *iface = ctx->ifs[ctx->ifsel];
if (iface != NULL) {
i = atoi(value);
iface->sock.sin_port = htons((short)i);
}
}
void cli_cmd_if_set_devname(cli_ctx *ctx, const char *value)
{
cli_if *iface = ctx->ifs[ctx->ifsel];
if (iface != NULL) {
switch (iface->type) {
case CLI_TYPE_FILE:
if (strncmp(iface->devname, "stdout", 6) != 0) {
fflush(iface->rxdev.fp);
fclose(iface->rxdev.fp);
}
if (strncmp(value, "stdout", 6) == 0) {
iface->rxdev.fp = stdout;
} else {
iface->rxdev.fp = fopen(value, "wb");
}
break;
default:
break;
}
memcpy(iface->devname, value, CLI_DEFAULT_BUFFER);
}
}
void cli_cmd_if_set_xmode(cli_if_mode *mode, const char *value)
{
if ((strncmp(value, "zlib", 4) == 0) ||
(value[0] == 'z')) {
*mode = CLI_MODE_Z;
} else if ((strncmp(value, "pt", 2) == 0) ||
(strncmp(value, "plaintext", 9) == 0) ||
(strncmp(value, "ascii", 5) == 0) ||
(value[0] == 'a')) {
*mode = CLI_MODE_PLAINTEXT;
} else if ((strncmp(value, "hex", 3) == 0) ||
(value[0] == 'h') ||
(value[0] == 'x')) {
*mode = CLI_MODE_HEX;
} else if ((strncmp(value, "binary", 6) == 0) ||
(value[0] == 'b')) {
*mode = CLI_MODE_BINARY;
}
}
void cli_cmd_if_set(cli_ctx *ctx)
{
int pos = 0, lpos = 0, rpos = 0;
int seeneq = 0;
char c;
char var[CLI_DEFAULT_BUFFER];
char val[CLI_DEFAULT_BUFFER];
memset(val, 0, 256);
memset(var, 0, 256);
while ((ctx->buffer[pos]) && (ctx->buffer[pos] != 't')) { pos++; }
pos++;
while ((pos < CLI_MAX_BUFFER) && (ctx->buffer[pos])) {
c = ctx->buffer[pos];
if (c == '=') {
pos++;
seeneq = 1;
continue;
}
if (c != ' ') {
if (seeneq) {
if (lpos > CLI_DEFAULT_BUFFER) break;
val[lpos] = c;
lpos++;
} else {
if (rpos < CLI_DEFAULT_BUFFER) {
var[rpos] = c;
rpos++;
}
}
}
pos++;
}
if (var[0] == 0) { printw("Error: `if set' must specify variable name.\n"); }
else if (val[0] == 0) { printw("Error: `if set' must specify value.\n"); }
else {
if ((strncmp(var, "devname", 7) == 0) &&
(ctx->ifs[ctx->ifsel]->type & CLI_DEVNAME_TYPES)) {
cli_cmd_if_set_devname(ctx, val);
} else if ((strncmp(var, "ipaddr", 6) == 0) &&
(ctx->ifs[ctx->ifsel]->type & CLI_IP_TYPES)) {
cli_cmd_if_set_ipaddr(ctx, val);
} else if ((strncmp(var, "ipport", 6) == 0) &&
(ctx->ifs[ctx->ifsel]->type & CLI_IP_TYPES)) {
cli_cmd_if_set_ipport(ctx, val);
} else if ((strncmp(var, "addr", 4) == 0) &&
(ctx->ifs[ctx->ifsel]->type == CLI_TYPE_MEMORY)) {
//cli_cmd_if_set_addr(ctx, val);
} else if (strncmp(var, "type", 4) == 0) {
cli_cmd_if_set_type(ctx, val);
} else if (strncmp(var, "buffer", 6) == 0) {
cli_cmd_if_set_buffersize(ctx, val);
} else if (strncmp(var, "rxmode", 6) == 0) {
cli_cmd_if_set_xmode(&ctx->ifs[ctx->ifsel]->rxmode, val);
} else if (strncmp(var, "txmode", 6) == 0) {
cli_cmd_if_set_xmode(&ctx->ifs[ctx->ifsel]->txmode, val);
} else {
printw("Error: `if set' variable `%s' unknown for target interface.\n", var);
}
}
fseek(ctx->ifs[ctx->ifsel]->offset, 0, SEEK_SET);
fwrite(ctx->ifs[ctx->ifsel], 1,
sizeof(cli_if), ctx->ifs[ctx->ifsel]->offset);
fseek(ctx->ifs[ctx->ifsel]->offset, 0, SEEK_END);
}
/**
static struct cli_options opts[] = {
{"add", cli_cmd_add, 0, "add"},
{"a", cli_cmd_add, 1, "add"},
...
{0, 0, 0, 0}
}
*/
int cli_command(cli_ctx *ctx, const struct cli_option *opts)
{
int i = 0, ret = 0;
int len;
while (opts[i].name != 0) {
len = strlen(opts[i].name);
if (strncmp(ctx->buffer, opts[i].name, len) == 0) {
opts[i].func(ctx);
ret = 1;
break;
}
i++;
}
return ret;
}