NetSim Source Code Help
Loading...
Searching...
No Matches
CLIInterpretor.c
Go to the documentation of this file.
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();
16
19void* add_new_socket_client(SOCKET s, char* name)
20{
21 ptrCLIENTINFO n = calloc(1, sizeof* n);
24 n->deviceName = _strdup(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
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);
49 n->CLIENT.fileClient.fpInputFile = fopen(inputFile, "r");
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",
60 "CLIOutput.txt");
63 {
64 perror(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
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
98{
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 {
112 }
113 return iswait;
114}
115
116#include "CLI.h"
118{
119 return handle->cmdArray;
120}
121
122
124 ptrCLIENTINFO info)
125{
126 CLIHANDLE c = calloc(1, sizeof* c);
127 c->clientInfo = info;
128 c->cmdArray = cmd;
129 return c;
130}
131
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
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
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);
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
204{
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)
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}
unsigned int NETSIM_ID
Definition: Animation.h:45
static ANIM_HANDLE handle
Definition: BatteryModel.c:197
void read_file_and_execute(FILE *fp)
Definition: FileInput.c:31
void set_fileClientInfo(ptrCLIENTINFO info)
Definition: FileInput.c:21
#define CMD_CHANGEPROMPT
Definition: CLI.h:12
void execute_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, NETSIM_ID d)
@ CLIENTTYPE_SOCKET
Definition: CLI.h:26
@ CLIENTTYPE_FILE
Definition: CLI.h:25
@ CLIENTTYPE_STRING
Definition: CLI.h:27
void send_message(ptrCLIENTINFO info, char *msg,...)
void * CLIHANDLE
Definition: CLIInterface.h:64
bool init_cliInterpretor(ptrCLIINFO cliInfo)
static ptrCLIENTINFO lastClient
void init_socket()
Definition: Socket.c:116
static bool iswait
int fn_NetSim_CLI_HandleTimerEvent()
void CLI_SEND_MESSAGE(CLIHANDLE handle, char *msg,...)
void * add_new_file_client(char *inputFile)
CLIHANDLE FORM_CLI_HANDLE(ptrCOMMANDARRAY cmd, ptrCLIENTINFO info)
DWORD WINAPI command_recv_process(LPVOID lpParam)
Definition: Socket.c:130
void CLI_STOP_WAITING(CLIHANDLE handle)
int fnStopConnection(int sig)
char * CLI_EXECUTE_COMMAND(ptrCOMMANDARRAY cmd, NETSIM_ID d, int *len, bool(*multResp)(void *, char *msg, int len, bool isMore), void *arg, bool *isMore)
void * add_new_socket_client(SOCKET s, char *name)
void CLI_PRINT_MESSAGE(CLIHANDLE info, char *msg, int len)
static ptrCLIENTINFO firstClient
ptrCOMMANDARRAY CLI_GET_CMDARRAY_FROM_HANDLE(CLIHANDLE handle)
void add_to_string(ptrCLIENTINFO info, char *sendMsg, int len)
#define c
#define LPVOID
Definition: Linux.h:81
#define _strdup(x)
Definition: Linux.h:126
#define _declspec(dllexport)
This function is used to trigger the update.
Definition: Linux.h:41
#define WINAPI
Definition: Linux.h:80
#define DWORD
Definition: Linux.h:79
#define Sleep(x)
Definition: Linux.h:104
#define realloc(p, s)
Definition: Memory.h:32
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
NETSIM_ID fn_NetSim_Stack_GetDeviceId_asName(const char *name)
EXPORTED char * pszIOPath
Definition: Stack.h:842
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
NETSIM_ID nDeviceId
Definition: Stack.h:750
int(* fnHandleTimerEvent)()
Definition: CLIInterface.h:52
char * inputFileName
Definition: CLIInterface.h:51
CLIENTTYPE clientType
Definition: CLI.h:71
union stru_clientInfo::client CLIENT
char * deviceName
Definition: CLI.h:80
NETSIM_ID deviceId
Definition: CLI.h:79
bool(* multResp)(void *, char *, int, bool)
Definition: CLI.h:85
struct stru_clientInfo * next
Definition: CLI.h:87
void * multRespArg
Definition: CLI.h:86
bool isMultResp
Definition: CLI.h:84
char * inputFile
Definition: CLI.h:44
char * outputFile
Definition: CLI.h:46
FILE * fpInputFile
Definition: CLI.h:45
FILE * fpOutputFile
Definition: CLI.h:47
bool dontUseMe
Definition: CLI.h:60
HANDLE clientRecvHandle
Definition: CLI.h:55
DWORD clientRecvId
Definition: CLI.h:56
SOCKET clientSocket
Definition: CLI.h:52
char * buffer
Definition: CLI.h:65
SOCKCLIENTINFO sockClient
Definition: CLI.h:74
STRINGCLIENTINFO stringClient
Definition: CLI.h:76
FILECLIENTINFO fileClient
Definition: CLI.h:75