-
Notifications
You must be signed in to change notification settings - Fork 0
/
RADRunner.cpp
558 lines (485 loc) · 15.1 KB
/
RADRunner.cpp
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#include <StdFuncs.h>
#include <Args.h>
#include <Lex.h>
#include <StdTextFile.h>
#include <memory>
#include <signal.h>
#include <string.h>
#include <vector>
#include <Yggdrasil/Commands.h>
#ifndef WIN32
#include <sys/select.h>
#endif /* ! WIN32 */
#define ARGS_DIR 0
#define ARGS_EXECUTE 1
#define ARGS_GET 2
#define ARGS_PORT 3
#define ARGS_SCRIPT 4
#define ARGS_SEND 5
#define ARGS_SERVER 6
#define ARGS_SHUTDOWN 7
#define ARGS_REMOTE 8
#define ARGS_NUM_ARGS 9
#ifdef __amigaos4__
/* Lovely version structure. Only Amiga makes it possible! */
static const struct Resident g_ROMTag __attribute__((used)) =
{
RTC_MATCHWORD,
(struct Resident *) &g_ROMTag,
(struct Resident *) (&g_ROMTag + 1),
RTF_AUTOINIT,
0,
NT_LIBRARY,
0,
"RADRunner",
"\0$VER: RADRunner 0.01 (17.04.2021)\r\n",
NULL
};
#elif defined(__amigaos__)
static const char g_accVersion[] = "$VER: RADRunner 0.01 (17.04.2021)";
#endif /* __amigaos__ */
/* Template for use in obtaining command line parameters. Remember to change the indexes */
/* in Commands.h if the ordering or number of these change */
static const char g_template[] = "DIR/K,EXECUTE/K,GET/K,PORT/K,SCRIPT/K,SEND/K,SERVER/S,SHUTDOWN/S,REMOTE";
static volatile bool g_break; /* Set to true if when ctrl-c is hit by the user */
static RArgs g_args; /* Contains the parsed command line arguments */
/* Written: Friday 02-Jan-2009 10:30 am */
static void SignalHandler(int /*a_signal*/)
{
/* Signal that ctrl-c has been pressed so that we break out of the scanning routine */
g_break = true;
}
/**
* Processes the given script file and executes its contents.
* Reads the script file passed in one line at a time and executes each command found. Lines starting
* with a # character are considered to be comments and are skipped.
*
* @date Sunday 10-Jan-2021 8:11 am, Code HQ Bergmannstrasse
* @param a_socket The socket to which to write client commands
* @param a_scriptName Fully qualified path to the script to be parsed
* @return A TResult containing both results from RADRunner and from any client command launched
*/
static TResult ProcessScript(RSocket &a_socket, const char *a_scriptName)
{
int scriptArgLength;
RTextFile scriptFile;
TLex scriptArgTokens(a_scriptName, static_cast<int>(strlen(a_scriptName)));
TResult retVal{ KErrNone, 0 };
/* The first argument is the script name so extract that separately */
const char *scriptArgToken = scriptArgTokens.NextToken(&scriptArgLength);
std::string script(scriptArgToken, scriptArgLength);
/* Now iterate through the remaining arguments and save them for easy reference later */
std::vector<std::string> arguments;
while ((scriptArgToken = scriptArgTokens.NextToken(&scriptArgLength)) != nullptr)
{
arguments.push_back(std::string(scriptArgToken, scriptArgLength));
}
printf("Parsing script \"%s\"...\n", script.c_str());
if (scriptFile.open(script.c_str()) == KErrNone)
{
const char *line;
/* Iterate through the script, reading each line and parsing it */
while ((line = scriptFile.GetLine()) != nullptr)
{
const char *commandToken;
int commandLength;
TLex tokens(line, static_cast<int>(strlen(line)));
/* Extract the first token on the line */
if ((commandToken = tokens.NextToken(&commandLength)) != nullptr)
{
CHandler *handler = nullptr;
std::string command(commandToken, commandLength);
/* If it is a comment character then continue to the next line. We check for the character being */
/* '#' rather than the entire string, to enable comments with no space after the '#' character */
if (command[0] == '#')
{
continue;
}
/* Extract the argument token and put it in a string of the precise length returned, to ensure */
/* that any trailing " is stripped */
const char *argumentToken = tokens.NextToken(&commandLength);
std::string argument(argumentToken, commandLength);
char variable[] = "$1";
size_t offset, variableIndex;
/* Variables in the range $1 to $9 are accepted, so iterate check whether any of these are present */
/* in the argument list and replace them with the matching argument that was passed in on the */
/* command line */
for (variableIndex = 0; variableIndex < 9; ++variableIndex)
{
variable[1] = '1' + static_cast<char>(variableIndex);
/* There may be more than one instance of the variable so search for it in a loop */
while ((offset = argument.find(variable)) != std::string::npos)
{
if (variableIndex < arguments.size())
{
argument.replace(offset, 2, arguments[variableIndex]);
}
/* If no argument was passed in for this variable, just treat it was a warning and continue */
else
{
printf("Warning: No argument passed in for variable %s\n", variable);
break;
}
}
}
if (command == "dir")
{
if (!argument.empty())
{
handler = new CDir(&a_socket, argument.c_str());
}
else
{
Utils::Error("No value was specified for the argument \"dir\"");
}
}
else if (command == "execute")
{
if (!argument.empty())
{
handler = new CExecute(&a_socket, argument.c_str());
}
else
{
Utils::Error("No value was specified for the argument \"execute\"");
}
}
else if (command == "get")
{
if (!argument.empty())
{
handler = new CGet(&a_socket, argument.c_str());
}
else
{
Utils::Error("No value was specified for the argument \"get\"");
}
}
else if (command == "send")
{
if (!argument.empty())
{
handler = new CSend(&a_socket, argument.c_str());
}
else
{
Utils::Error("No value was specified for the argument \"send\"");
}
}
else if (command == "shutdown")
{
handler = new CShutdown(&a_socket);
}
else
{
printf("Warning: Unknown command \"%s\" found\n", command.c_str());
}
if (handler != nullptr)
{
printf("Sending request \"%s\"\n", command.c_str());
retVal = handler->sendRequest();
delete handler;
if ((retVal.m_result != KErrNone) || (retVal.m_subResult != 0))
{
break;
}
}
}
}
scriptFile.close();
}
else
{
Utils::Error("Unable to open script file \"%s\" for parsing", a_scriptName);
}
return retVal;
}
/**
* Launches RADRunner in client mode.
* This method must be executed when RADRunner is launched in client mode. It will connect to the remote server
* specified in the program arguments and then send the commands specified in the program arguments to it. At
* the end of this, it will close the connection to the server.
*
* @date Tuesday 27-Apr-2021 6:33 am, Code HQ Bergmannstrasse
* @param a_port The port to which to connect
*/
static TResult StartClient(unsigned short a_port)
{
RSocket socket;
TResult retVal{ KErrNone, 0 };
if (g_args[ARGS_REMOTE] != nullptr)
{
std::string server;
Utils::splitHost(g_args[ARGS_REMOTE], server, a_port, a_port);
if (socket.open(server.c_str(), a_port) == KErrNone)
{
try
{
/* Start by sending a signature, to identify us as a RADRunner client */
socket.write(g_signature, 4);
/* Handlers are stored in a shared_ptr so that they are freed if an exception */
/* occurs, but we allocate them with new (rather than std::make_shared()) as we */
/* need them to be zero initialised by MungWall */
std::shared_ptr<CHandler> handler(new CVersion(&socket));
/* Check whether the server's protocol version is supported. This handler will */
/* display an error and exit if it is not */
handler->sendRequest();
handler = nullptr;
if (g_args[ARGS_SCRIPT] != nullptr)
{
retVal = ProcessScript(socket, g_args[ARGS_SCRIPT]);
}
else
{
if (g_args[ARGS_DIR] != nullptr)
{
handler = std::shared_ptr<CHandler>(new CDir(&socket, g_args[ARGS_DIR]));
}
else if (g_args[ARGS_EXECUTE] != nullptr)
{
handler = std::shared_ptr<CHandler>(new CExecute(&socket, g_args[ARGS_EXECUTE]));
}
else if (g_args[ARGS_GET] != nullptr)
{
handler = std::shared_ptr<CHandler>(new CGet(&socket, g_args[ARGS_GET]));
}
else if (g_args[ARGS_SEND] != nullptr)
{
handler = std::shared_ptr<CHandler>(new CSend(&socket, g_args[ARGS_SEND]));
}
else if (g_args[ARGS_SHUTDOWN] != nullptr)
{
handler = std::shared_ptr<CHandler>(new CShutdown(&socket));
}
else
{
Utils::Error("Unknown command");
}
if (handler != nullptr)
{
retVal = handler->sendRequest();
handler = nullptr;
}
}
}
catch (std::runtime_error &a_exception)
{
Utils::Error(a_exception.what());
}
socket.close();
}
else
{
Utils::Error("Cannot connect to %s:%d", server.c_str(), a_port);
}
}
else
{
Utils::Error("REMOTE argument must be specified");
}
return retVal;
}
/**
* Launches RADRunner in server mode.
* This method must be executed when RADRunner is launched in server mode. It will listen on a socket for
* incoming connections and, when received, will process whatever commands are received from the client. At
* the end of this, it will close the connection to the client and wait for a new connection to come in.
*
* @date Sunday 17-Nov-2019 3:29 pm, Sankt Oberholz
* @param a_port The port on which to listen for connections
*/
static void StartServer(unsigned short a_port)
{
bool shutdown = false;
int result, selectResult;
RSocket socket;
SCommand command;
fd_set socketSet;
printf("Starting RADRunner server\n");
if ((result = socket.open(nullptr, a_port)) == KErrNone)
{
if ((result = socket.listen(a_port)) == KErrNone)
{
do
{
printf("Listening for a client connection... ");
fflush(stdout);
if ((result = socket.accept()) == KErrNone)
{
printf("connected\n");
try
{
FD_ZERO(&socketSet);
FD_SET(socket.m_socket, &socketSet);
/* Ensure that the client that just connected is a RADRunner client, which will always send a */
/* signature as soon as it connects */
char clientSignature[4];
socket.read(clientSignature, sizeof(clientSignature));
if (memcmp(clientSignature, g_signature, 4) != 0)
{
Utils::Error("Connected client is not an instance of RADRunner, closing connection");
shutdown = true;
}
while (!g_break && !shutdown)
{
selectResult = select(FD_SETSIZE, &socketSet, nullptr, nullptr, nullptr);
if (selectResult > 0)
{
socket.read(&command, sizeof(command));
SWAP(&command.m_command);
SWAP(&command.m_size);
printf("Received request \"%s\"\n", g_commandNames[command.m_command]);
/* Handlers are stored in a shared_ptr so that they are freed if an exception */
/* occurs, but we allocate them with new (rather than std::make_shared()) as we */
/* need them to be zero initialised by MungWall */
std::shared_ptr<CHandler> handler(nullptr);
if (command.m_command == EVersion)
{
handler = std::shared_ptr<CHandler>(new CVersion(&socket, command));
}
else if (command.m_command == EDelete)
{
handler = std::shared_ptr<CHandler>(new CDelete(&socket, command));
}
else if (command.m_command == EDir)
{
handler = std::shared_ptr<CHandler>(new CDir(&socket, command));
}
else if (command.m_command == EExecute)
{
handler = std::shared_ptr<CHandler>(new CExecute(&socket, command));
}
else if (command.m_command == EFileInfo)
{
handler = std::shared_ptr<CHandler>(new CFileInfo(&socket, command));
}
else if (command.m_command == EGet)
{
handler = std::shared_ptr<CHandler>(new CGet(&socket, command));
}
else if (command.m_command == ERename)
{
handler = std::shared_ptr<CHandler>(new CRename(&socket, command));
}
else if (command.m_command == ESend)
{
handler = std::shared_ptr<CHandler>(new CSend(&socket, command));
}
else if (command.m_command == EShutdown)
{
printf("shutdown: Exiting\n");
shutdown = true;
}
else
{
printf("Invalid command received: %d\n", command.m_command);
socket.write("invalid");
}
if (handler != nullptr)
{
handler->execute();
handler = nullptr;
}
}
else if (selectResult == -1)
{
shutdown = true;
}
}
}
catch (RSocket::Error &a_exception)
{
/* If the result was 0 then the remote socket has been closed and we want to go back to listening. */
/* Otherwise it is a "real" socket error, so display an error and shut down */
if (a_exception.m_result != 0)
{
printf("Unable to perform I/O on socket (Error = %d)\n", a_exception.m_result);
shutdown = true;
}
}
}
else
{
printf("failed (Error = %d)\n", result);
shutdown = true;
}
} while (!g_break && !shutdown);
}
else
{
printf("Unable to listen on socket (Error = %d)\n", result);
shutdown = true;
}
socket.close();
}
else
{
Utils::Error("Unable to open socket (Error = %d)", result);
shutdown = true;
}
if (g_break)
{
printf("Received ctrl-c, exiting\n");
}
}
/**
* Entry point of the program.
* Reads the arguments passed into the program and determines whether to launch in client or server mode
* as appropriate.
*
* @date Sunday 17-Nov-2019 3:30 pm, Sankt Oberholz
* @param a_argc Number of arguments passed in
* @param a_arcv Array of string arguments
* @return RETURN_OK if successful, else RETURN_ERROR
*/
int main(int a_argc, const char *a_argv[])
{
int port = 80, result;
TResult clientResult{KErrNone, 0};
/* Install a ctrl-c handler so we can handle ctrl-c being pressed and shut down the scan */
/* properly */
signal(SIGINT, SignalHandler);
/* Parse the command line parameters passed in and make sure they are formatted correctly */
if ((result = g_args.open(g_template, ARGS_NUM_ARGS, a_argv, a_argc)) == KErrNone)
{
if (g_args[ARGS_PORT] != nullptr)
{
if (Utils::StringToInt(g_args[ARGS_PORT], &port) != KErrNone)
{
Utils::Error("Invalid port specified, using default port %d", port);
}
}
if (g_args[ARGS_SERVER] != nullptr)
{
StartServer(static_cast<unsigned short>(port));
}
else
{
clientResult = StartClient(static_cast<unsigned short>(port));
}
g_args.close();
}
else
{
if (result == KErrNotFound)
{
Utils::Error("Required argument missing");
}
else
{
Utils::Error("Unable to read command line arguments");
}
}
/* In order to enable the user to differentiate between failure due to RADRunner itself having an error, and */
/* failure due to the software launched by the execute command returning an error, we will return the standard */
/* AmigaOS RETURN_ERROR error for RADRunner errors and the negative error returned by the execute command for */
/* execution errors */
if (clientResult.m_result == KErrNone)
{
return (clientResult.m_subResult == 0) ? RETURN_OK : -clientResult.m_subResult;
}
else
{
return RETURN_ERROR;
}
}