-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmcli.c
More file actions
170 lines (155 loc) · 5.12 KB
/
Copy pathnmcli.c
File metadata and controls
170 lines (155 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include "CommandParser/libcliid.h"
#include "graph.h"
#include "CommandParser/cmdtlv.h"
#include "CommandParser/libcli.h"
#include "CommandParser/clistd.h"
#include "net.h"
#include "nmcli.h" ///< Parameter codes for diff CLI commands.
#include "utils.h"
#include "layer2.h"
extern graph_t *topo;
/**
* Callback functions
*
*/
// A number of input parameters in callback function is not
// used. Ignore these warnings.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
// show topo
static int
show_topo_callback(param_t *param,
ser_buff_t *tlv_buf,
op_mode enable_or_disable){
int CMDCODE = -1;
CMDCODE = EXTRACT_CMD_CODE(tlv_buf);
switch(CMDCODE){
case CMDCODE_SHOW_TOPOLOGY:
dump_graph(topo);
break;
default:
;
}
return 0;
}
// run node <node-name> resolve-arp <ip-address>
static int
run_node_arp_resolve_callback(param_t *param,
ser_buff_t *tlv_buf,
op_mode enable_or_disable){
int CMDCODE = -1;
tlv_struct_t *tlv = NULL;
char *node_name = NULL;
char *ip_address = NULL;
node_t* req_node;
TLV_LOOP_BEGIN(tlv_buf, tlv){
if(strncmp(tlv->leaf_id, "node_name", strlen("node_name")) == 0){
node_name = tlv->value;
}
if(strncmp(tlv->leaf_id, "ip_address", strlen("ip_address")) == 0){
ip_address = tlv->value;
}
} TLV_LOOP_END;
CMDCODE = EXTRACT_CMD_CODE(tlv_buf);
switch(CMDCODE){
case CMDCODE_RUN_NODE_RESOLVE_ARP:
// Identify the interface to which the ARP broadcast has to be sent
// of all interfaces identify the interface with longest prefix lenth
// and use it
printf("Here, nodename %s\n", node_name);
printf("Here, IP address %s\n", ip_address);
req_node = get_node_by_node_name(topo, node_name);
if(req_node == NULL){
printf("Unable to find node with name %s\n", node_name);
}
interface_t *out_if = node_get_matching_subnet_interface(req_node, ip_address);
send_arp_broadcast_req(req_node, out_if, ip_address);
break;
default:
;
}
return 0;
}
#pragma GCC diagnostic pop
/**
* Validation functions
*
*/
static int
validate_node_name_callback(char *node_name){
if(get_node_by_node_name(topo, node_name) == NULL){
// Canont find node given node name
printf("Node name %s not found in topology\n", node_name);
printf("Use \"show topo\" to list all node names\n");
return VALIDATION_FAILED;
}
return VALIDATION_SUCCESS; // VALIDATION_FAILED
}
static int
validate_ip_callback(char *ip_address){
if(is_valid_ipv4(ip_address) < 0){
printf("%s is not a valid IP address\n", ip_address);
return VALIDATION_FAILED;
}
return VALIDATION_SUCCESS;
}
/**
* @brief Initialie the Command line interface.
*
* Register different commands with the CLI library
*
* @param void
* @return void
*/
void nw_init_cli(){
init_libcli();
param_t *show = libcli_get_show_hook();
param_t *debug = libcli_get_debug_hook();
param_t *config = libcli_get_config_hook();
param_t *clear = libcli_get_clear_hook();
param_t *run = libcli_get_run_hook();
/**
* All the CLI commands go here
*
*/
//CMD1: show topology
{
static param_t topology;
init_param(&topology, CMD, "topology", show_topo_callback, 0, INVALID, 0, "Show the topology of the net network");
set_param_cmd_code(&topology, CMDCODE_SHOW_TOPOLOGY);
libcli_register_param(show, &topology);
}
//CMD: run node <node-name> resolve-arp <ip-address>
{
// Add node param as suboption of run param
static param_t node;
init_param(&node, CMD, "node", 0, 0, INVALID, 0, "Help: node");
libcli_register_param(run, &node);
{
// run node <node-name> (node-name as sub option of node)
static param_t node_name;
init_param(&node_name, LEAF, 0, 0, validate_node_name_callback, STRING, "node_name", "Help: node name");
libcli_register_param(&node, &node_name);
// run node <node-name> resolve-arp
{
static param_t resolve_arp;
init_param(&resolve_arp, CMD, "resolve-arp", 0, 0, INVALID, 0, "resolve-arp <IP-address>");
libcli_register_param(&node_name, &resolve_arp);
// run node <node-name> resolve-arp <ip-address> (and done)
{
static param_t ip_address;
// validate_ip_callback
init_param(&ip_address, LEAF, 0, run_node_arp_resolve_callback, validate_ip_callback , IPV4, "ip_address", "Help: IP-address");
libcli_register_param(&resolve_arp, &ip_address);
set_param_cmd_code(&ip_address, CMDCODE_RUN_NODE_RESOLVE_ARP); // Completed constructing the entire command
}
}
}
}
/**
* Do not add any param in command config tree after here
*
*/
support_cmd_negation(config);
}