1#define _CRT_SECURE_NO_WARNINGS
11#include "CLIInterface.h"
12#pragma comment(lib,"ws2_32.lib")
15DWORD WINAPI command_recv_process(LPVOID lpParam);
17static ptrCLIENTINFO firstClient = NULL;
18static ptrCLIENTINFO lastClient = NULL;
19void* add_new_socket_client(SOCKET s,
char* name)
21 ptrCLIENTINFO n = calloc(1,
sizeof* n);
22 n->clientType = CLIENTTYPE_SOCKET;
23 n->CLIENT.sockClient.clientSocket = s;
24 n->deviceName = _strdup(name);
25 n->deviceId = fn_NetSim_Stack_GetDeviceId_asName(name);
26 n->CLIENT.sockClient.dontUseMe =
true;
27 n->CLIENT.sockClient.iswait =
false;
40 n->CLIENT.sockClient.clientRecvHandle =
41 CreateThread(NULL, 1024, command_recv_process, n,
false, &n->CLIENT.sockClient.clientRecvId);
45void* add_new_file_client(
char* inputFile)
47 ptrCLIENTINFO n = calloc(1,
sizeof* n);
48 n->clientType = CLIENTTYPE_FILE;
49 n->CLIENT.fileClient.fpInputFile = fopen(inputFile,
"r");
50 if (!n->CLIENT.fileClient.fpInputFile)
56 n->CLIENT.fileClient.outputFile = calloc(strlen(pszIOPath) + 50,
sizeof(
char));
57 sprintf(n->CLIENT.fileClient.outputFile,
61 n->CLIENT.fileClient.fpOutputFile = fopen(n->CLIENT.fileClient.outputFile,
"w");
62 if (!n->CLIENT.fileClient.fpOutputFile)
64 perror(n->CLIENT.fileClient.outputFile);
65 free(n->CLIENT.fileClient.outputFile);
69 n->CLIENT.fileClient.inputFile = _strdup(inputFile);
82 set_fileClientInfo(n);
83 read_file_and_execute(n->CLIENT.fileClient.fpInputFile);
87static bool iswait =
true;
88int fnStopConnection(
int sig)
97_declspec(dllexport)
bool init_cliInterpretor(ptrCLIINFO cliInfo)
99 cliInfo->fnHandleTimerEvent = fn_NetSim_CLI_HandleTimerEvent;
101 fprintf(stderr,
"Waiting for first client to connect. Press ctrl+c to stop connection.\n");
102 if (!cliInfo->inputFileName)
104 signal(SIGINT, fnStopConnection);
105 while (iswait && !firstClient)
107 signal(SIGINT, SIG_DFL);
111 add_new_file_client(cliInfo->inputFileName);
117_declspec(dllexport) ptrCOMMANDARRAY CLI_GET_CMDARRAY_FROM_HANDLE(CLIHANDLE handle)
119 return handle->cmdArray;
123CLIHANDLE FORM_CLI_HANDLE(ptrCOMMANDARRAY cmd,
126 CLIHANDLE c = calloc(1,
sizeof* c);
127 c->clientInfo = info;
132_declspec(dllexport)
void CLI_SEND_MESSAGE(CLIHANDLE handle,
136 char sendMSG[2 * BUFSIZ];
139 vsprintf(sendMSG, msg, l);
140 send_message(handle->clientInfo, sendMSG);
143_declspec(dllexport)
void CLI_STOP_WAITING(CLIHANDLE handle)
145 ptrCLIENTINFO info = handle->clientInfo;
146 if (info->clientType == CLIENTTYPE_SOCKET)
148 info->CLIENT.sockClient.iswait =
false;
149 WakeByAddressSingle(&info->CLIENT.sockClient.iswait);
153_declspec(dllexport)
char* CLI_EXECUTE_COMMAND(ptrCOMMANDARRAY cmd,
156 bool(*multResp)(
void*,
char* msg,
int len,
bool isMore),
161 ptrCLIENTINFO info = calloc(1,
sizeof* info);
162 info->clientType = CLIENTTYPE_STRING;
164 info->multResp = multResp;
165 info->multRespArg = arg;
166 execute_command(info, cmd, d);
168 *len = info->CLIENT.stringClient.len;
169 ret = info->CLIENT.stringClient.buffer;
170 if (!info->isMultResp)
182_declspec(dllexport)
void CLI_PRINT_MESSAGE(CLIHANDLE info,
char* msg,
int len)
189 char* q = strstr(s, CMD_CHANGEPROMPT);
192 info->clientInfo->promptString = _strdup(q + 1 + strlen(CMD_CHANGEPROMPT));
195 send_message(info->clientInfo, s);
196 i += (int)strlen(s) + 1;
203int fn_NetSim_CLI_HandleTimerEvent()
205 ptrCLIHANDLE handle = pstruEventDetails->szOtherDetails;
206 execute_command(handle->clientInfo, handle->cmdArray, pstruEventDetails->nDeviceId);
210void add_to_string(ptrCLIENTINFO info,
char* sendMsg,
int len)
212 if (info->CLIENT.stringClient.len)
213 info->CLIENT.stringClient.buffer = realloc(info->CLIENT.stringClient.buffer,
215 (info->CLIENT.stringClient.len + len));
217 info->CLIENT.stringClient.buffer = calloc(len,
sizeof(
char));
218 memcpy(info->CLIENT.stringClient.buffer + info->CLIENT.stringClient.len,
220 info->CLIENT.stringClient.len += len;