NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
CLIInterface.c
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* Author: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "OpenFlow.h"
16#include "CLIInterface.h"
17
18void openFlow_saveHandle(NETSIM_ID d,
19 UINT id,
20 void* handle)
21{
22 ptrOPENFLOW of = GET_OPENFLOW_VAR(d);
23 ptrHANDLEINFO h = calloc(1, sizeof* h);
24 h->handle = handle;
25 h->id = id;
26 if (of->handleInfo)
27 {
28 ptrHANDLEINFO t = of->handleInfo;
29 while (t->next)
30 t = t->next;
31 t->next = h;
32 }
33 else
34 {
35 of->handleInfo = h;
36 }
37}
38
39ptrHANDLEINFO openFlow_getHandle(NETSIM_ID d,
40 UINT id)
41{
42 ptrOPENFLOW of = GET_OPENFLOW_VAR(d);
43 ptrHANDLEINFO t = of->handleInfo;
44 ptrHANDLEINFO p = NULL;
45 while (t)
46 {
47 if (t->id == id)
48 {
49 if (p)
50 p->next = t->next;
51 else
52 of->handleInfo = t->next;
53 void* h = t->handle;
54 free(t);
55 return h;
56 }
57 p = t;
58 t = t->next;
59 }
60 return NULL;
61}
62
63
64static bool validate_input(CLIHANDLE handle, ptrCOMMANDARRAY cmd)
65{
66 if (!isSDNController(pstruEventDetails->nDeviceId))
67 {
68 CLI_SEND_MESSAGE(handle, "%d (%s) device is not a SDN controller\n",
69 DEVICE_CONFIGID(pstruEventDetails->nDeviceId),
70 DEVICE_NAME(pstruEventDetails->nDeviceId));
71 return false;
72 }
73 NETSIM_ID c = fn_NetSim_Stack_GetDeviceId_asName(cmd->commands[0]);
74 if (!c ||
75 !isOPENFLOWConfigured(c))
76 {
77 CLI_SEND_MESSAGE(handle, "%d (%s) device is not an OPEN_FLOW compatible device.\n",
78 c,
79 cmd->commands[0]);
80 return false;
81 }
82 return true;
83}
84
85typedef struct stru_multArgs
86{
87 NETSIM_ID d;
88 UINT id;
89 void* handle;
90}MULTARG, *ptrMULTARG;
91bool openflow_multiple_response_handler(ptrMULTARG arg,
92 char* msg,
93 int len,
94 bool isMore)
95{
96 if (!arg->id)
97 {
98 CLI_PRINT_MESSAGE(arg->handle, msg, len);
99 if (!isMore)
100 CLI_STOP_WAITING(arg->handle);
101 }
102 else
103 {
104 openFlow_send_response(arg->d,
105 arg->id,
106 msg,
107 len,
108 isMore);
109 }
110 if (!isMore)
111 free(arg);
112 return isMore;
113}
114
115void handle_cli_input()
116{
117 CLIHANDLE handle = pstruEventDetails->szOtherDetails;
118 ptrCOMMANDARRAY cmd = CLI_GET_CMDARRAY_FROM_HANDLE(handle);
119
120 if (validate_input(handle, cmd))
121 {
122 CLI_SEND_MESSAGE(handle, "Input is validated\n");
123 NETSIM_ID d = fn_NetSim_Stack_GetDeviceId_asName(cmd->commands[0]);
124 cmd = remove_first_word_from_commandArray(cmd);
125 if (d == pstruEventDetails->nDeviceId)
126 {
127 CLI_SEND_MESSAGE(handle, "Command is for SDN Controller\n");
128 int len = 0;
129 ptrMULTARG arg = calloc(1, sizeof* arg);
130 arg->d = d;
131 arg->handle = handle;
132 bool isMore;
133 char* ret = CLI_EXECUTE_COMMAND(cmd,
134 d,
135 &len,
136 openflow_multiple_response_handler,
137 arg,
138 &isMore);
139 CLI_PRINT_MESSAGE(handle, ret, len);
140 if (!isMore)
141 {
142 free(arg);
143 CLI_STOP_WAITING(handle);
144 }
145 }
146 else
147 {
148 CLI_SEND_MESSAGE(handle, "Sending command to client device %d\n", d);
149 if (!fn_NetSim_OPEN_FLOW_SendCommand(d, handle, cmd, cmd->length))
150 CLI_STOP_WAITING(handle);
151 }
152 }
153 else
154 {
155 CLI_STOP_WAITING(handle);
156 }
157}
158
159char* openFlow_execute_command(void* handle, NETSIM_ID d, UINT id, int* len, bool* isMore)
160{
161 ptrMULTARG arg = calloc(1, sizeof* arg);
162 arg->d = d;
163 arg->id = id;
164 return CLI_EXECUTE_COMMAND(handle, d, len, openflow_multiple_response_handler, arg, isMore);
165}
166
167void openFlow_print_response(NETSIM_ID d,
168 UINT id,
169 void* msg,
170 int len,
171 bool isMore)
172{
173 void* handle = openFlow_getHandle(d, id);
174 if (handle)
175 {
176 CLI_PRINT_MESSAGE(handle, msg, len);
177 if (!isMore)
178 CLI_STOP_WAITING(handle);
179 else
180 openFlow_saveHandle(d, id, handle);
181 }
182}