Skip to content

Commit

Permalink
Merge branch 'Coding'
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitizsaini113 committed Oct 1, 2020
2 parents 69379f9 + 8aa937c commit 7a20485
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 18 deletions.
26 changes: 15 additions & 11 deletions Project_Implementation/Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
CC=gcc
CFLAGS=-g
COMPILER=gcc
COMPILER_FLAGS=-g
TARGET:main.sh

OBJS=linkedlist/linkedlist.o \
networkgraph.o \
network_topology.o
OBJECT_FILES=linkedlist/linkedlist.o \
networkgraph.o \
network_topology.o \
network.o

main.sh:main.o ${OBJS}
${CC} ${CFLAGS} main.o ${OBJS} -o main.sh
main.sh:main.o ${OBJECT_FILES}
${COMPILER} ${COMPILER_FLAGS} main.o ${OBJECT_FILES} -o main.sh

main.o:main.c
${CC} ${CFLAGS} -c main.c -o main.o
${COMPILER} ${COMPILER_FLAGS} -c main.c -o main.o

linkedlist/linkedlist.o:linkedlist/linkedlist.c
${CC} ${CFLAGS} -c -I linkedlist linkedlist/linkedlist.c -o linkedlist/linkedlist.o
${COMPILER} ${COMPILER_FLAGS} -c -I linkedlist linkedlist/linkedlist.c -o linkedlist/linkedlist.o

networkgraph.o:networkgraph.c
${CC} ${CFLAGS} -c -I . networkgraph.c -o networkgraph.o
${COMPILER} ${COMPILER_FLAGS} -c -I . networkgraph.c -o networkgraph.o

network_topology.o:network_topology.c
${CC} ${CFLAGS} -c -I . network_topology.c -o network_topology.o
${COMPILER} ${COMPILER_FLAGS} -c -I . network_topology.c -o network_topology.o

network.o:network.c
${COMPILER} ${COMPILER_FLAGS} -c -I . network.c -o network.o


clean:
Expand Down
10 changes: 7 additions & 3 deletions Project_Implementation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ Here, we are focusing on implementation of network graph and its functions in th

2) The file [networkgraph.c](networkgraph.c) contains the functions definations for the functions defined in [networkgraph.h](networkgraph.h) to be used for Network Graph implementation.

3) The file [network_topology.c](network_topology.c) is used for creating network topologies.
3) The file [network.h](network.h) contains the structures, macros and function declarations used for the Networking part. *i.e. IP, MAC and Loopback*

4) The file [main.c](main.c) is used to call the functions.
4) The file [network.c](network.c) contains the functional definations for the functions defined in [network.h](network.h) to assign network properties to our network. *Network constrains like IP, MAC and Loopback.*

5) Makefile is created to make the project compilation easy.
5) The file [network_topology.c](network_topology.c) is used for creating network topologies.

6) The file [main.c](main.c) is used to call the functions.

7) Makefile is created to make the project compilation easy.

## Running the project

Expand Down
2 changes: 1 addition & 1 deletion Project_Implementation/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ extern network_graph_t *build_first_network_topology();
int main(int argc, char **argv)
{
network_graph_t *network_topology = build_first_network_topology();
dump_network_graph(network_topology);
dump_network_graph_net(network_topology);
return 0;
}
152 changes: 152 additions & 0 deletions Project_Implementation/network.c
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]);
}
145 changes: 145 additions & 0 deletions Project_Implementation/network.h
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
16 changes: 16 additions & 0 deletions Project_Implementation/network_topology.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ network_graph_t * build_first_network_topology()
insert_link_between_two_network_nodes(router_1, router_2, "eth0/2", "eth0/3", 1);
insert_link_between_two_network_nodes(router_0, router_2, "eth0/4", "eth0/5", 1);

network_node_device_type(router_0, L3_ROUTER);
network_node_set_loopback_address(router_0, "122.1.1.0");
network_node_set_interface_ip_address(router_0, "eth0/4", "40.1.1.1", 24);
network_node_set_interface_ip_address(router_0, "eth0/0", "20.1.1.1", 24);

network_node_device_type(router_1, L3_ROUTER);
network_node_set_loopback_address(router_1, "122.1.1.1");
network_node_set_interface_ip_address(router_1, "eth0/1", "20.1.1.1", 24);
network_node_set_interface_ip_address(router_1, "eth0/2", "30.1.1.1", 24);

network_node_device_type(router_2, L3_ROUTER);
network_node_set_loopback_address(router_2, "122.1.1.2");
network_node_set_interface_ip_address(router_2, "eth0/3", "30.1.1.1", 24);
network_node_set_interface_ip_address(router_2, "eth0/5", "40.1.1.1", 24);


return network_topology_1;
}

Expand Down
Loading

0 comments on commit 7a20485

Please sign in to comment.