-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
421 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
#include "networkgraph.h" | ||
#include <memory.h> | ||
#include "utils.h" | ||
#include <stdio.h> | ||
|
||
|
||
void interface_assign_mac_address(network_interface_t *network_interface) | ||
{ | ||
// Function used to assigns the mac address to existing interface. | ||
memset(INTERFACE_MAC(network_interface), 0, 48); | ||
// Clears the memory buffer using memset and assigning 0. | ||
strcpy(INTERFACE_MAC(network_interface), network_interface->attached_node->network_node_name); | ||
// Copies the attached node name. | ||
strcat(INTERFACE_MAC(network_interface), network_interface->interface_name); | ||
// Concatenates the current node name. | ||
// Converts to MAC address to generate a unique MAC address at random | ||
} | ||
|
||
|
||
boolean_t network_node_device_type(network_node_t *network_node, | ||
unsigned int F) | ||
{ | ||
// Sets the type of device (hub/switch/router) | ||
SET_BIT(network_node->node_network_properties.flags, F); | ||
// Sets the device bit according to the flag provided to define it's type. | ||
return TRUE; | ||
} | ||
|
||
|
||
boolean_t network_node_set_loopback_address(network_node_t *network_node, | ||
char *ip_address) | ||
{ | ||
// Sets the loopback address for a machine if not already set. | ||
assert(ip_address); | ||
|
||
if(IS_BIT_SET(network_node->node_network_properties.flags, HUB)) | ||
{ | ||
assert(0); | ||
// Stops the execution as a hub dont habve any IP Address. | ||
} | ||
|
||
if(!IS_BIT_SET(network_node->node_network_properties.flags, L3_ROUTER)) | ||
{ | ||
assert(0); | ||
// Stops the execution as L3 routing must be enabled. | ||
} | ||
|
||
network_node->node_network_properties.is_loopback_address_configured = TRUE; | ||
// Change the loopback flag to true as loopback adds is assigned. | ||
strncpy(NODE_LOOPBACK_ADDRESS(network_node), ip_address, 16); | ||
// Copies the string in empty ip buffer to be used as loopback ip. | ||
NODE_LOOPBACK_ADDRESS(network_node)[16] = '\0'; | ||
|
||
return TRUE; | ||
} | ||
|
||
|
||
boolean_t network_node_set_interface_ip_address(network_node_t *network_node, | ||
char *local_interface, | ||
char *ip_address, | ||
char mask) | ||
{ | ||
// Sets the IP Address to a device. | ||
network_interface_t *network_interface = get_network_node_interface_by_name(network_node, local_interface); | ||
if(!network_interface) | ||
{ | ||
assert(0); | ||
// If interface if already configured, then stop the execution. | ||
} | ||
|
||
strncpy(INTERFACE_IP(network_interface), ip_address, 16); | ||
INTERFACE_IP(network_interface)[16] = '\0'; | ||
// Copies the ip address to the cleared ip buffer. | ||
network_interface->interface_network_properties.mask = mask; | ||
network_interface->interface_network_properties.is_ip_address_configured = TRUE; | ||
// Sets the mask for the IP and changes it's flag to true. | ||
|
||
return TRUE; | ||
} | ||
|
||
|
||
boolean_t network_node_unset_interface_ip_address(network_node_t *network_node, | ||
char *local_interface) | ||
{ | ||
// Unsets the IP Address | ||
return TRUE; | ||
} | ||
|
||
|
||
void dump_network_graph_net(network_graph_t *network_graph) | ||
{ | ||
network_node_t *network_node; | ||
doublylinkedlist_t *current; | ||
network_interface_t *network_interface; | ||
unsigned int i; | ||
// Variable declarations for future use. | ||
|
||
printf("Topology Name : %s\n", network_graph->network_topology_name); | ||
// Prints the topology name | ||
|
||
ITERATE_DOUBLY_LINKED_LIST_BEGINING(&network_graph->network_node_list, current) | ||
{ | ||
network_node = graph_glue_to_node(current); | ||
dump_network_node_properties(network_node); | ||
for(i=0; i<MAXIMUM_INTERFACE_PER_NODE; i++) | ||
{ | ||
network_interface = network_node->interface[i]; | ||
if(!network_interface) | ||
{ | ||
break; | ||
} | ||
dump_network_interface_properties(network_interface); | ||
} | ||
} ITERATE_DOUBLY_LINKED_LIST_END(&network_graph->network_node_list, current) | ||
// Iterates the doubly linked list from begining to end using the macro defined loop. | ||
// During each iteration, it is printing the node properties and dump network Interface | ||
} | ||
|
||
|
||
void dump_network_node_properties(network_node_t *network_node) | ||
{ | ||
printf("\nNode Name : %s\n", network_node->network_node_name); | ||
printf("\nNode Flags : %u", network_node->node_network_properties.flags); | ||
if(network_node->node_network_properties.is_loopback_address_configured) | ||
{ | ||
printf("\tLoopback Address : %s/32\n", NODE_LOOPBACK_ADDRESS(network_node)); | ||
} | ||
// Dumps the information about a particular Node. | ||
} | ||
|
||
|
||
void dump_network_interface_properties(network_interface_t *network_interface) | ||
{ | ||
dump_network_interface(network_interface); | ||
|
||
// Prints the IP which means that %s/%u show linux interface properties. | ||
if(network_interface->interface_network_properties.is_ip_address_configured) | ||
{ | ||
printf("\tIP Address : %s/%u", INTERFACE_IP(network_interface), network_interface->interface_network_properties.mask); | ||
} | ||
else | ||
{ | ||
printf("\tIP Address : %s/%u", "Nil", 0); | ||
} | ||
|
||
printf("\tMAC Address : %u:%u:%u:%u:%u:%u\n", INTERFACE_MAC(network_interface)[0], | ||
INTERFACE_MAC(network_interface)[1], | ||
INTERFACE_MAC(network_interface)[2], | ||
INTERFACE_MAC(network_interface)[3], | ||
INTERFACE_MAC(network_interface)[4], | ||
INTERFACE_MAC(network_interface)[5]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#ifndef __NET__ | ||
#define __NET__ | ||
// Performing header file management to avoid duplicate inclusion of header file. | ||
|
||
|
||
#include "utils.h" | ||
#include <memory.h> | ||
// Including the required header files | ||
|
||
|
||
#define L3_ROUTER ( 1 << 0 ) | ||
#define L2_SWITCH ( 1 << 1 ) | ||
#define HUB ( 1 << 2 ) | ||
// We use 1<<0, 1<<1 and 1<<2 instead of 1, 2 and 4 because | ||
// we want our values to be bit masks instead of just normal integers | ||
// And for a macro we cant define a data-type, using the shift operator makes this possible. | ||
// | ||
// A mask defines which bits we want to keep, and which bits we want to clear. | ||
// Masking is the act of applying a mask to a value. This is accomplished by doing: | ||
// Bitwise ANDing in order to extract a subset of the bits in the value | ||
// Bitwise ORing in order to set a subset of the bits in the value | ||
// Bitwise XORing in order to toggle a subset of the bits in the value | ||
// | ||
// Common use-case: Extracting individual bytes from a larger word. We define the high-order | ||
// bits in the word as the first byte. We use two operators for this, &, and >> (shift right). | ||
|
||
|
||
// Forward Declarations Begin | ||
typedef struct network_graph_ network_graph_t; | ||
|
||
typedef struct network_interface_ network_interface_t; | ||
|
||
typedef struct network_node_ network_node_t; | ||
// Forward Declarations End | ||
|
||
|
||
typedef struct ip_address_ | ||
{ | ||
char ip_address[16]; | ||
} ip_address_t; | ||
// Defining a structure to represent an IP Address | ||
|
||
|
||
typedef struct mac_address_ | ||
{ | ||
char mac_address[48]; | ||
} mac_address_t; | ||
// Defining a structure to represent a Mac Address | ||
|
||
|
||
typedef struct node_network_properties_ | ||
{ | ||
unsigned int flags; | ||
// Flag to represent the number of connected devices. | ||
|
||
boolean_t is_loopback_address_configured; | ||
ip_address_t loopback_address; | ||
// Defining two variables for our usage to make our program efficient. | ||
// The boolean value will check if the loopback address is configured or not, | ||
// If the flag is true, then only we will try to access the loopback address. | ||
} node_network_properties_t; | ||
|
||
|
||
static inline void initialize_node_network_properties(node_network_properties_t *node_network_properties) | ||
{ | ||
// Initializes the network node. | ||
node_network_properties->flags = 0; | ||
|
||
node_network_properties->is_loopback_address_configured = FALSE; | ||
memset(node_network_properties->loopback_address.ip_address, 0, 16); | ||
// memset() is used to fill a block of memory with a particular value. | ||
// The syntax of memset() function is as follows : | ||
// void *memset(void *ptr, int x, size_t n); | ||
// ptr ==> Starting address of memory to be filled | ||
// x ==> Value to be filled | ||
// n ==> Number of bytes to be filled starting from ptr to be filled | ||
// memset is used instead of any string assignment because memset clears the budder for assigning | ||
// a value while string only assigns the value in the specified set and keeps the else value as a | ||
// garbage making us access a completely different thing. | ||
} | ||
|
||
|
||
typedef struct interface_network_properties_ | ||
{ | ||
mac_address_t mac_address; | ||
|
||
boolean_t is_ip_address_configured; | ||
ip_address_t ip_address; | ||
char mask; | ||
} interface_network_properties_t; | ||
|
||
|
||
static inline void initialize_interface_network_properties(interface_network_properties_t *interface_network_properties) | ||
{ | ||
memset(interface_network_properties->mac_address.mac_address, 0, 48); | ||
|
||
interface_network_properties->is_ip_address_configured = FALSE; | ||
memset(interface_network_properties->ip_address.ip_address, 0, 16); | ||
interface_network_properties->mask = 0; | ||
} | ||
|
||
|
||
void interface_assign_mac_address(network_interface_t *network_interface); | ||
|
||
|
||
// Defining shorthand macros to avoid code repetition | ||
#define INTERFACE_MAC(interface_ptr) \ | ||
((interface_ptr)->interface_network_properties.mac_address.mac_address) | ||
|
||
#define INTERFACE_IP(interface_ptr) \ | ||
((interface_ptr)->interface_network_properties.ip_address.ip_address) | ||
|
||
#define NODE_LOOPBACK_ADDRESS(network_node_ptr) \ | ||
(network_node_ptr->node_network_properties.loopback_address.ip_address) | ||
// Finishing the shorthand macro declaration | ||
|
||
|
||
// Defining functions to be defined as API in network.c | ||
boolean_t network_node_device_type(network_node_t *network_node, | ||
unsigned int F); | ||
|
||
boolean_t network_node_set_loopback_address(network_node_t *network_node, | ||
char *ip_address); | ||
|
||
boolean_t network_node_set_interface_ip_address(network_node_t *network_node, | ||
char *local_interface, | ||
char *ip_address, | ||
char mask); | ||
|
||
boolean_t network_node_unset_interface_ip_address(network_node_t *network_node, | ||
char *local_interface); | ||
// Function declarations | ||
|
||
|
||
// Functions to dump information on the screen | ||
void dump_network_graph_net(network_graph_t *network_graph); | ||
|
||
void dump_network_node_properties(network_node_t *network_node); | ||
|
||
void dump_network_interface_properties(network_interface_t *network_interfacef); | ||
// Function declaration ends | ||
|
||
|
||
#endif | ||
// Ending Header File Management |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.