-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.cpp
More file actions
341 lines (293 loc) · 10.7 KB
/
Shell.cpp
File metadata and controls
341 lines (293 loc) · 10.7 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*-------------------------------------------------------------------------
This source file is a part of mil
For the latest info, see http://www.marrin.org/
Copyright (c) 2025, Chris Marrin
All rights reserved.
-------------------------------------------------------------------------*/
#include "Shell.h"
#include "Application.h"
#include "HTTPParser.h"
#include "System.h"
#include "WebFileSystem.h"
#include <sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
using namespace mil;
static const char* TAG = "Shell";
static constexpr int TelnetPort = 23;
Shell::~Shell()
{
_terminating = true;
close(_serverSocket);
_serverThread.join();
}
bool
Shell::begin(Application* app)
{
_app = app;
// Set cwd
_dirs.clear();
std::string cwd;
if (!_app->getNVSParam("cwd", cwd)) {
// Not found, set cwd to '/'
cwd = "/";
_app->setNVSParam("cwd", cwd);
}
_dirs.push_back(cwd);
_serverThread = std::thread([this]() { tcpServerTask(); });
return true;
}
std::string
Shell::makeAbsolutePath(const std::string& path) const
{
if (path.empty()) {
return cwd();
}
if (path[0] == '/') {
return path;
}
if (path.length() >= 2 && path[0] == '.' && path[1] == '/') {
// Relative path
return cwd() + path.substr(1);
}
return WebFileSystem::lexicallyNormal(cwd() + "/" + path);
}
// This is hackery. When trying to make a path in handleHTTPRequest the suffix would always get dropped
// on espidf. moving the concat to a separate method fixes it. Until I get to the bottom of it, this
// solution will do.
static std::string makeCmdPath(const char* root, const char* cmd, const char* suffix)
{
std::string s = std::string(root) + cmd + suffix;
return s;
}
// There are several built-in commands (adapted from bash) but since we
// don't current support most of bash's features, this is a small
// subset:
//
// cd - Change to passed dir
// date - Shows the current date
// dirs - Shows the dirs stack
// history - Show history stack (implemented in shell.html)
// popd - Pop the top of the dirs stack and go to the next entry in the stack
// pushd - Go to the passed dire and push it onto the dirs stack
// pwd - Show the current dir
//
// cd, dirs, popd, pushd and pwd all have to do with maintaining the current working
// directory (cwd). When cd is executed the cwd is set to that and saved as an NVS
// param. There is also a stack of directories. When 'pushd' is executed the cwd
// is changed and saved like in 'cd' but it is also pushed onto the top of the
// stack. When popd is called the top of the dirs stack is popped and the next
// entry is made the cwd. The dirs stack is not preserved as an NVS param so at each
// reboot it is reset to the cwd.
void
Shell::showDirs(PrintCB printCB) const
{
std::string dirs;
for (int i = int(_dirs.size() - 1); i >= 0; --i) {
dirs += _dirs[i] + " ";
};
LuaManager::print(printCB, (dirs + "\n").c_str(), dirs.length() + 1);
}
int8_t
Shell::handleShellCommand(const std::string& incomingCmd, PrintCB printCB)
{
std::string cmd = incomingCmd;
if (cmd.back() == '\n') {
cmd.pop_back();
}
if (cmd.back() == '\r') {
cmd.pop_back();
}
if (cmd.empty()) {
std::string s = "no command supplied";
LuaManager::print(printCB, s.c_str(), s.length());
return -1;
}
// Make sure the WebFileSystem has the right cwd
WebFileSystem::setCWD(_dirs.back().c_str());
std::vector<std::string>args = HTTPParser::split(cmd, ' ');
cmd = args.front();
args.erase(args.begin());
// See if it's a built-in command
// FIXME: For now just do an if cascade. Maybe we use a map at some time?
if (cmd == "date") {
std::string date(" ");
date += _app->clock() ? _app->clock()->prettyTime().c_str() : "no clock";
date += "\n";
LuaManager::print(printCB, date.c_str(), date.length());
return -1;
}
if (cmd == "pwd") {
LuaManager::print(printCB, (_dirs.back() + "\n").c_str(), _dirs.back().length() + 1);
return -1;
}
if (cmd == "cd" || cmd == "pushd") {
// cd pops the top of the dirs stack and replaces it
// pushd just pushes the dir onto the top of the stack
std::string cwd = makeAbsolutePath(args.empty() ? "/" : args[0]);
if (!WebFileSystem::exists(cwd.c_str())) {
std::string response = cmd + ": " + cwd + ": No such file or directory\n";
LuaManager::print(printCB, response.c_str(), response.length());
} else {
if (cmd == "cd") {
_dirs.pop_back();
}
_dirs.push_back(cwd);
WebFileSystem::setCWD(cwd.c_str());
if (cmd != "cd") {
showDirs(printCB);
}
}
return -1;
}
if (cmd == "popd") {
if (_dirs.size() == 1) {
std::string s = "popd: directory stack empty\n";
LuaManager::print(printCB, s.c_str(), s.length());
} else {
_dirs.pop_back();
WebFileSystem::setCWD(_dirs.back().c_str());
showDirs(printCB);
}
return -1;
}
if (cmd == "dirs") {
showDirs(printCB);
return -1;
}
// FIXME: for now all commands are .lua in the sys folder
std::string path = makeCmdPath("/sys/", cmd.c_str(), ".lua");
if (!WebFileSystem::exists(path.c_str())) {
std::string s = path + ": command not found\n";
LuaManager::print(printCB, s.c_str(), s.length());
return -1;
}
// Execute command
return LuaManager::execute(path.c_str(), _termChars, args, printCB);
}
void
Shell::tcpServerTask()
{
_serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (_serverSocket < 0) {
System::logE(TAG, "Unable to create listen socket %i: errno %d", _serverSocket, errno);
return;
}
int opt = 1;
setsockopt(_serverSocket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
System::logI(TAG, "Listen socket %i created", _serverSocket);
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(TelnetPort);
int err = bind(_serverSocket, (struct sockaddr *)&address, sizeof(address));
if (err != 0) {
System::logE(TAG, "Listen socket %i unable to bind: errno %d", _serverSocket, errno);
close(_serverSocket);
_serverSocket = -1;
return;
}
System::logI(TAG, "Listen socket %i bound, port %d", _serverSocket, TelnetPort);
err = listen(_serverSocket, 1);
if (err != 0) {
System::logE(TAG, "Error occurred during listen socket %i: errno %d", _serverSocket, errno);
close(_serverSocket);
_serverSocket = -1;
return;
}
while (!_terminating) {
System::logI(TAG, "Socket listening for new connection");
struct sockaddr_storage sourceAddr;
socklen_t addrLen = sizeof(sourceAddr);
int sock = accept(_serverSocket, (struct sockaddr *)&sourceAddr, &addrLen);
if (sock < 0) {
System::logE(TAG, "Unable to accept connection: errno %d", errno);
break;
}
struct sockaddr_in *sin = (struct sockaddr_in *)&sourceAddr;
unsigned char* ip = (unsigned char *)&sin->sin_addr.s_addr;
System::logI(TAG, "Accepted connection:ip=%d.%d.%d.%d, socket=%d", ip[0], ip[1], ip[2], ip[3], sock);
// Set tcp keepalive option
int keepAlive = 1;
int keepIdle = 5;
int keepInterval = 5;
int keepCount = 3;
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int));
setsockopt(sock, IPPROTO_TCP, 5, &keepIdle, sizeof(int));
setsockopt(sock, IPPROTO_TCP, 5, &keepInterval, sizeof(int));
setsockopt(sock, IPPROTO_TCP, 3, &keepCount, sizeof(int));
// Start a thread with a shell for the client
std::thread clientThread = std::thread([this, sock]() { tcpClientTask(sock); });
clientThread.detach();
// FIXME: ultimately we need to keep all the client threads in a list
// along with their socket so we can kill them and stuff like that
System::delay(20);
}
}
static const telnet_telopt_t telopts[] = {
{ TELNET_TELOPT_NAWS, TELNET_WONT, TELNET_DO},
{ TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO},
{ TELNET_TELOPT_NEW_ENVIRON, TELNET_WILL, TELNET_DONT },
{ TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
{ -1, 0, 0 }
};
void
Shell::telnetHandler(int sock, telnet_t *telnet, telnet_event_t *event)
{
if (event->type == TELNET_EV_WILL && event->iac.cmd == TELNET_TELOPT_NAWS) {
telnet_iac(telnet, TELNET_TELOPT_NAWS);
return;
}
if (event->type == TELNET_EV_SUBNEGOTIATION && event->sub.telopt == TELNET_TELOPT_NAWS) {
// Should have a buffer with 4 bytes. First two are width (msb first), second is height
// Ignore if not 2 bytes and set to a default
if (event->sub.size != 4) {
_termChars = 80;
_termLines = 24;
} else {
_termChars = (int(uint8_t(event->sub.buffer[0])) << 8) | uint8_t(event->sub.buffer[1]);
_termLines = (int(uint8_t(event->sub.buffer[2])) << 8) | uint8_t(event->sub.buffer[3]);
}
return;
}
if (event->type == TELNET_EV_DATA) {
handleShellCommand(std::string(event->data.buffer, event->data.size), [telnet](const char* buf, size_t size)
{
telnet_send(telnet, buf, size);
});
return;
}
if (event->type == TELNET_EV_SEND) {
ssize_t result = send(sock, event->data.buffer, event->data.size, 0);
if (result < 0) {
System::logE(TAG, "Error sending data:%s", strerror(errno));
}
return;
}
}
void
Shell::tcpClientTask(int sock)
{
// Create the telnet handler
TelnetClient client(sock, this);
telnet_t* telnet = telnet_init(telopts, _telnetHandler, 0, &client);
if (!telnet) {
System::logE(TAG, "Failed to initialize libtelnet=%s", strerror(errno));
return;
}
// FIXME: Allocate the buffer (and probably bigger) to avoid blowing the stack
char buf[128];
while (true) {
ssize_t result = recv(sock, buf, 127, 0);
if (result == 0) {
System::logI(TAG, "Connection closed");
break;
}
if (result < 0) {
System::logE(TAG, "Error receiving data, result=%d", int(result));
break;
}
telnet_recv(telnet, buf, result);
}
telnet_free(telnet);
}