NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
CLIInterpretor.c
1#define _CRT_SECURE_NO_WARNINGS
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <Ws2tcpip.h>
6#include <windows.h>
7#include <stdbool.h>
8#include <signal.h>
9#include "main.h"
10#include "CLI.h"
11#include "CLIInterface.h"
12#pragma comment(lib,"ws2_32.lib")
13
14void init_socket();
15DWORD WINAPI command_recv_process(LPVOID lpParam);
16
17static ptrCLIENTINFO firstClient = NULL;
18static ptrCLIENTINFO lastClient = NULL;
19void* add_new_socket_client(SOCKET s, char* name)
20{
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;
28
29 if (firstClient)
30 {
31 lastClient->next = n;
32 lastClient = n;
33 }
34 else
35 {
36 firstClient = n;
37 lastClient = n;
38 }
39
40 n->CLIENT.sockClient.clientRecvHandle =
41 CreateThread(NULL, 1024, command_recv_process, n, false, &n->CLIENT.sockClient.clientRecvId);
42 return n;
43}
44
45void* add_new_file_client(char* inputFile)
46{
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)
51 {
52 perror(inputFile);
53 free(n);
54 return NULL;
55 }
56 n->CLIENT.fileClient.outputFile = calloc(strlen(pszIOPath) + 50, sizeof(char));
57 sprintf(n->CLIENT.fileClient.outputFile,
58 "%s/%s",
59 pszIOPath,
60 "CLIOutput.txt");
61 n->CLIENT.fileClient.fpOutputFile = fopen(n->CLIENT.fileClient.outputFile, "w");
62 if (!n->CLIENT.fileClient.fpOutputFile)
63 {
64 perror(n->CLIENT.fileClient.outputFile);
65 free(n->CLIENT.fileClient.outputFile);
66 free(n);
67 return NULL;
68 }
69 n->CLIENT.fileClient.inputFile = _strdup(inputFile);
70
71 if (firstClient)
72 {
73 lastClient->next = n;
74 lastClient = n;
75 }
76 else
77 {
78 firstClient = n;
79 lastClient = n;
80 }
81
82 set_fileClientInfo(n);
83 read_file_and_execute(n->CLIENT.fileClient.fpInputFile);
84 return n;
85}
86
87static bool iswait = true;
88int fnStopConnection(int sig)
89{
90 if (sig == SIGINT)
91 {
92 iswait = false;
93 }
94 return 0;
95}
96
97_declspec(dllexport) bool init_cliInterpretor(ptrCLIINFO cliInfo)
98{
99 cliInfo->fnHandleTimerEvent = fn_NetSim_CLI_HandleTimerEvent;
100 init_socket();
101 fprintf(stderr, "Waiting for first client to connect. Press ctrl+c to stop connection.\n");
102 if (!cliInfo->inputFileName)
103 {
104 signal(SIGINT, fnStopConnection);
105 while (iswait && !firstClient)
106 Sleep(1000);
107 signal(SIGINT, SIG_DFL);
108 }
109 else
110 {
111 add_new_file_client(cliInfo->inputFileName);
112 }
113 return iswait;
114}
115
116#include "CLI.h"
117_declspec(dllexport) ptrCOMMANDARRAY CLI_GET_CMDARRAY_FROM_HANDLE(CLIHANDLE handle)
118{
119 return handle->cmdArray;
120}
121
122
123CLIHANDLE FORM_CLI_HANDLE(ptrCOMMANDARRAY cmd,
124 ptrCLIENTINFO info)
125{
126 CLIHANDLE c = calloc(1, sizeof* c);
127 c->clientInfo = info;
128 c->cmdArray = cmd;
129 return c;
130}
131
132_declspec(dllexport) void CLI_SEND_MESSAGE(CLIHANDLE handle,
133 char* msg,
134 ...)
135{
136 char sendMSG[2 * BUFSIZ];
137 va_list l;
138 va_start(l, msg);
139 vsprintf(sendMSG, msg, l);
140 send_message(handle->clientInfo, sendMSG);
141}
142
143_declspec(dllexport) void CLI_STOP_WAITING(CLIHANDLE handle)
144{
145 ptrCLIENTINFO info = handle->clientInfo;
146 if (info->clientType == CLIENTTYPE_SOCKET)
147 {
148 info->CLIENT.sockClient.iswait = false;
149 WakeByAddressSingle(&info->CLIENT.sockClient.iswait);
150 }
151}
152
153_declspec(dllexport) char* CLI_EXECUTE_COMMAND(ptrCOMMANDARRAY cmd,
154 NETSIM_ID d,
155 int* len,
156 bool(*multResp)(void*,char* msg,int len,bool isMore),
157 void* arg,
158 bool* isMore)
159{
160 char* ret;
161 ptrCLIENTINFO info = calloc(1, sizeof* info);
162 info->clientType = CLIENTTYPE_STRING;
163 info->deviceId = d;
164 info->multResp = multResp;
165 info->multRespArg = arg;
166 execute_command(info, cmd, d);
167
168 *len = info->CLIENT.stringClient.len;
169 ret = info->CLIENT.stringClient.buffer;
170 if (!info->isMultResp)
171 {
172 *isMore = false;
173 free(info);
174 }
175 else
176 {
177 *isMore = true;
178 }
179 return ret;
180}
181
182_declspec(dllexport) void CLI_PRINT_MESSAGE(CLIHANDLE info, char* msg, int len)
183{
184 int i = 0;
185 char* s = msg;
186 char* t;
187 while (i<len)
188 {
189 char* q = strstr(s, CMD_CHANGEPROMPT);
190 if (q)
191 {
192 info->clientInfo->promptString = _strdup(q + 1 + strlen(CMD_CHANGEPROMPT));
193 }
194
195 send_message(info->clientInfo, s);
196 i += (int)strlen(s) + 1;
197 t = strchr(s, 0);
198 if (t)
199 s = t + 1;
200 }
201}
202
203int fn_NetSim_CLI_HandleTimerEvent()
204{
205 ptrCLIHANDLE handle = pstruEventDetails->szOtherDetails;
206 execute_command(handle->clientInfo, handle->cmdArray, pstruEventDetails->nDeviceId);
207 return 0;
208}
209
210void add_to_string(ptrCLIENTINFO info, char* sendMsg, int len)
211{
212 if (info->CLIENT.stringClient.len)
213 info->CLIENT.stringClient.buffer = realloc(info->CLIENT.stringClient.buffer,
214 sizeof(char)*
215 (info->CLIENT.stringClient.len + len));
216 else
217 info->CLIENT.stringClient.buffer = calloc(len, sizeof(char));
218 memcpy(info->CLIENT.stringClient.buffer + info->CLIENT.stringClient.len,
219 sendMsg, len);
220 info->CLIENT.stringClient.len += len;
221}