NetSim Source Code Help
Loading...
Searching...
No Matches
RouteCommand.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2020 *
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 <signal.h>
16#include "CLI.h"
17#include "../IP/IP.h"
18
19static bool validate_route_add_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index)
20{
21 if (command->length - index >= 9)
22 return true;
23 send_message(info, "Too few argument for route add command\n");
24 return false;
25}
26
27static bool validate_route_delete_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index)
28{
29 if (command->length - index >= 1)
30 return true;
31 send_message(info, "Too few argument for route delete command\n");
32 return false;
33}
34
36{
37 if (command->length - index < 2)
38 {
39 send_message(info, "Too less argument for route command\n");
40 return false;
41 }
42
43 if (!_stricmp(command->commands[index + 1], "add"))
44 return validate_route_add_command(info, command, index + 1);
45
46 else if (!_stricmp(command->commands[index + 1], "print"))
47 return true;
48
49 else if (!_stricmp(command->commands[index + 1], "delete"))
50 return validate_route_delete_command(info, command, index + 1);
51
52 send_message(info, "%s is not valid argument for route command\n",
53 command->commands[index + 1]);
54 return false;
55}
56
58 ptrCOMMANDARRAY command,
59 int index,
60 NETSIM_ID d)
61{
62 NETSIM_IPAddress dest = NULL;
63 NETSIM_IPAddress mask = NULL;
64 NETSIM_IPAddress gateway = NULL;
65 UINT prefix = 0;
66 NETSIM_ID in = 0;
67 UINT metric = 0;
68 int ipType = STR_GET_IP_TYPE(command->commands[index + 1]);
69
70 if (!ipType)
71 fnNetSimError("%s ip address is not valid.\n", command->commands[index + 1]);
72
73 dest = STR_TO_IP(command->commands[index + 1], ipType);
74
75 if (ipType == 4)
76 mask = STR_TO_IP4(command->commands[index + 3]);
77 else
78 prefix = atoi(command->commands[index + 3]);
79
80 gateway = STR_TO_IP(command->commands[index + 4], ipType);
81
82 metric = atoi(command->commands[index + 6]);
83
84 in = atoi(command->commands[index + 8]);
85
86 iptable_add(IP_WRAPPER_GET(d), dest, mask, prefix, gateway, 1, &DEVICE_NWADDRESS(d, in), &in, metric, "STATIC");
87 send_message(info, "OK!\n");
88}
89
91 ptrCOMMANDARRAY command,
92 int index,
93 NETSIM_ID d)
94{
95 (void)command;
96 (void)index;
97
100 send_message(info, "===========================================\n\n");
101 send_message(info, " IP Route Table\n");
102 send_message(info, "===========================================\n\n");
103
104 send_message(info, "%30s\t%15s\t%30s\t%30s\t%10s\t%10s\n",
105 "Network Destination",
106 "Netmask//Prefix",
107 "Gateway",
108 "Interface",
109 "Metric",
110 "Type");
111
112 while (table)
113 {
114 send_message(info, "%30s\t",
115 table->networkDestination->str_ip);
116
117 if (table->networkDestination->type == 4)
118 send_message(info, "%15s\t", table->netMask->str_ip);
119 else
120 send_message(info, "%15d\t", table->prefix_len);
121
122 if (table->gateway)
123 send_message(info, "%30s\t", table->gateway->str_ip);
124 else
125 send_message(info, "%30s\t", "on-link");
126
127 send_message(info, "%30s\t%10d\t%10s\n",
128 table->Interface[0]->str_ip,
129 table->Metric,
130 table->szType);
131
132
133 table = LIST_NEXT(table);
134 }
135 send_message(info, "================================================\n");
136 send_message(info, "\n\n");
137}
138
140 ptrCOMMANDARRAY command,
141 int index,
142 NETSIM_ID d)
143{
144 NETSIM_IPAddress dest = STR_TO_IP(command->commands[index + 1],
145 STR_GET_IP_TYPE(command->commands[index + 1]));
146 iptable_delete(IP_WRAPPER_GET(d), dest, NULL, NULL);
147 send_message(info, "OK!\n");
148}
149
151{
152 if (!_stricmp(command->commands[index + 1], "add"))
153 execute_route_add_command(info, command, index + 1, d);
154
155 else if (!_stricmp(command->commands[index + 1], "print"))
156 execute_route_print_command(info, command, index + 1, d);
157
158 else if (!_stricmp(command->commands[index + 1], "delete"))
159 execute_route_delete_command(info, command, index + 1, d);
160}
unsigned int NETSIM_ID
Definition: Animation.h:45
void send_message(ptrCLIENTINFO info, char *msg,...)
int iptable_delete(ptrIP_WRAPPER wrapper, NETSIM_IPAddress dest, NETSIM_IPAddress in, char *type)
ptrIP_ROUTINGTABLE iptable_add(ptrIP_WRAPPER wrapper, NETSIM_IPAddress dest, NETSIM_IPAddress subnet, unsigned int prefix_len, NETSIM_IPAddress gateway, UINT interfaceCount, NETSIM_IPAddress *interfaceIp, NETSIM_ID *interfaceId, unsigned int metric, char *type)
NETSIM_IPAddress STR_TO_IP(char *ipStr, int type)
int STR_GET_IP_TYPE(char *ip)
#define STR_TO_IP4(ipstr)
Definition: IP_Addressing.h:94
#define _stricmp
Definition: Linux.h:127
#define UINT
Definition: Linux.h:38
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define LIST_NEXT(ls)
Definition: List.h:29
#define ptrIP_ROUTINGTABLE
static bool validate_route_delete_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index)
Definition: RouteCommand.c:27
static void execute_route_delete_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index, NETSIM_ID d)
Definition: RouteCommand.c:139
static void execute_route_print_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index, NETSIM_ID d)
Definition: RouteCommand.c:90
bool validate_route_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index)
Definition: RouteCommand.c:35
void execute_route_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index, NETSIM_ID d)
Definition: RouteCommand.c:150
static void execute_route_add_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index, NETSIM_ID d)
Definition: RouteCommand.c:57
static bool validate_route_add_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index)
Definition: RouteCommand.c:19
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
#define IP_WRAPPER_GET(DeviceId)
Definition: Stack.h:801
Structure to store ip routing table.
Definition: IP.h:157
unsigned int prefix_len
Definition: IP.h:163
NETSIM_IPAddress * Interface
Definition: IP.h:162
NETSIM_IPAddress networkDestination
Definition: IP.h:158
NETSIM_IPAddress gateway
Definition: IP.h:160
unsigned int Metric
Definition: IP.h:164
NETSIM_IPAddress netMask
Definition: IP.h:159
void * table
Definition: Stack.h:516
char str_ip[_NETSIM_IP_LEN]
Definition: IP_Addressing.h:54