forked from SuryankDixit/Dynamic-Routing-Protocols
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.c
More file actions
206 lines (145 loc) · 5.29 KB
/
graph.c
File metadata and controls
206 lines (145 loc) · 5.29 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "graph.h"
#include "css.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
int index_for_storing_edges =0;
void
init_udp_socket(node *node);
graph *createGraph(int vertex,int edges){
graph* topology = (graph*) malloc(sizeof(graph));
topology->numVertex = vertex;
topology->numEdges = edges;
topology->routersArray = (node*) malloc(vertex* sizeof(node)); // array of nodes
topology->edgesArray = (edge*) malloc(edges* sizeof(edge)); // array holding edges
for(int i=0;i<vertex;i++){
// Giving Name to each Router.
char name[NAME_SIZE] = "Router";
int index = 6;
int num = i;
char str[NAME_SIZE];
if(my_itoa(num, str) != NULL){
for(int j = 0; str[j] != '\0'; j++){
char ch = str[j];
name[index++] = ch;
}
}
// if(i<10)
// name[6] = i+'0';
createGraphNodes(&topology->routersArray[i], name);
createRoutingTable(&topology->routersArray[i].rt, topology->numVertex);
}
return topology;
}
void createGraphNodes(node* router, char *name){
strncpy(router->routerName,name,32);
router->routerName[NAME_SIZE-1] = '\0';
init_udp_socket(router);
char ip[16] = "127.0.0.1";
strncpy(router->loopbackIPAddress.ip,ip,16);
router->loopbackIPAddress.ip[15] = '\0';
}
edge* addEdge(graph *topology ,node* node1 , node* node2, char* from_, char* to_, int cost){
edge *link = (edge*) malloc(sizeof(edge));
// Setting name of each interface in the edge structure.
strncpy(link->intf1.interfaceName, from_ ,NAME_SIZE);
link->intf1.interfaceName[NAME_SIZE-1] = '\0';
strncpy(link->intf2.interfaceName, to_ ,NAME_SIZE);
link->intf2.interfaceName[NAME_SIZE-1] = '\0';
link->intf1.attachedEdge = link; // Setting back pointer to the edge structure.
link->intf2.attachedEdge = link; // this will help in determining the neighbour of the each interface;
link->intf1.attachedNode = node1;
link->intf2.attachedNode = node2;
link->cost = cost;
int emptyInterfaceSlot;
emptyInterfaceSlot = getEmptyInterfaceSlot(node1);
if(emptyInterfaceSlot == -1){
printf("Cannot add more edges to this Node.");
exit(0);
}
node1->intf[emptyInterfaceSlot] = &link->intf1;
emptyInterfaceSlot = getEmptyInterfaceSlot(node2);
if(emptyInterfaceSlot == -1){
printf("Cannot add more edges to this Node.");
exit(0);
}
node2->intf[emptyInterfaceSlot] = &link->intf2;
topology->edgesArray[index_for_storing_edges++] = *link;
return link;
}
int getEmptyInterfaceSlot(node *router){
for(int i=0 ; i<MAXIMUM_INTERFACE_PER_NODE;i++){
if(router->intf[i] != NULL)
continue;
return i;
}
return -1;
}
void printGraph(graph* topology){
int v = topology->numVertex;
int e = MAXIMUM_INTERFACE_PER_NODE;
interface *intf;
printf("\n\n Traversing Network Topology:\n");
for(int i=0;i<v;i++){
printf("%s ->\n",topology->routersArray[i].routerName); // printing router name;
for(int j=0;j<e;j++){
intf = topology->routersArray[i].intf[j];
if(!intf)
break;
printInterface(intf); // function to print details of connected Interface.
}
}
}
void printInterface(interface *intf){
edge *link = intf->attachedEdge;
node *neighbourNode = getNeighbourNode(intf);
// printf("%s",intf->interfaceProperties.ip.ip);
// printf("%d",intf->interfaceProperties.subnetMask);
printf(ANSI_COLOR_YELLOW"\tInterface Name : %s \n\t IP Address: %s/%d,\n\t Neighbour Node: %s,\n\t cost = %u\n\n"ANSI_COLOR_RESET,
intf->interfaceName,intf->interfaceProperties.ip.ip,intf->interfaceProperties.subnetMask,neighbourNode->routerName, link->cost);
}
void printEdges(graph* topology,int numEdges){
printf("\n\nPrinting All the Edges:\n");
for(int i=0;i<numEdges;i++){
edge link = topology->edgesArray[i];
printf("\n\tNode Name : %s ::\n\t Neighbour Node: %s,\n\t cost = %u\n",
link.intf1.attachedNode->routerName,link.intf2.attachedNode->routerName, link.cost);
}
}
node *getNeighbourNode(interface *intf) { // Each edge has two ends which means 2 nodes, so this function returns the node that is on other end of the edge.
edge *link = intf->attachedEdge;
if(&link->intf1 == intf)
return link->intf2.attachedNode;
else
return link->intf1.attachedNode;
}
int getIndexOfNode(node* router,graph* topology){
int v = topology->numVertex;
for(int i=0;i<v;i++){
if(&(topology->routersArray[i]) == router){
return i;
}
}
return -1;
}
interface * get_node_if_by_name(node *router, char *if_name){
int i ;
interface *intf;
for( i = 0 ; i < MAXIMUM_INTERFACE_PER_NODE; i++){
intf = router->intf[i];
if(!intf) return NULL;
if(strncmp(intf->interfaceName, if_name, NAME_SIZE) == 0){
return intf;
}
}
return NULL;
}
char *my_itoa(int num, char *str)
{
if(str == NULL){
return NULL;
}
sprintf(str, "%d", num);
return str;
}