RTM Project

This commit is contained in:
2024-05-07 22:02:08 +05:30
commit f7d7a53ade
20 changed files with 2033 additions and 0 deletions

15
Sync/CMakeLists.txt Normal file
View File

@@ -0,0 +1,15 @@
set(SYNC_SRC
"sync.c")
set(SYNC_HEADER
"sync.h")
add_library( ${SM_NAME} STATIC
${SYNC_SRC}
${SYNC_HEADER})
target_include_directories( ${SM_NAME} PUBLIC
"./")
target_link_libraries( ${SM_NAME} PUBLIC
${ML_NAME}
${RT_NAME})

141
Sync/sync.c Normal file
View File

@@ -0,0 +1,141 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/unistd.h>
#include <sys/mman.h> // for unlink
#include <errno.h>
#include "dll.h"
#include "sync.h"
/*
* Makes changes to a routing table or mac list based on the
* instructions encoded in sync_msg.
*/
void process_sync_mesg(dll_t* dll, sync_msg_t* sync_msg) {
dll_node_t* node;
if (sync_msg->l_code == L3) {
node = find_routing_table_entry(dll,
sync_msg->msg_body.routing_table_entry.dest,
sync_msg->msg_body.routing_table_entry.mask);
switch (sync_msg->op_code) {
case CREATE:
if (node == dll->head) {
append(dll, &sync_msg->msg_body.routing_table_entry);
node = find_routing_table_entry(dll,
sync_msg->msg_body.routing_table_entry.dest,
sync_msg->msg_body.routing_table_entry.mask);
if (node != dll->head) {
routing_table_entry_t entry = *((routing_table_entry_t*)node->data);
printf("Added Destination IP: %s mask: %u Gateway IP: %s OIF: %s\n",
entry.dest,
entry.mask,
entry.gw,
entry.oif);
}
}
break;
case UPDATE:
if (node != dll->head) {
update(node,
sync_msg->msg_body.routing_table_entry.gw,
sync_msg->msg_body.routing_table_entry.oif);
node = find_routing_table_entry(dll,
sync_msg->msg_body.routing_table_entry.dest,
sync_msg->msg_body.routing_table_entry.mask);
if (node != dll->head) {
routing_table_entry_t entry = *((routing_table_entry_t*)node->data);
printf("Updated Destination IP: %s mask: %u Gateway IP: %s OIF: %s\n",
entry.dest,
entry.mask,
entry.gw,
entry.oif);
}
}
break;
case DELETE:
if (node != dll->head) {
del(dll, node);
node = find_routing_table_entry(dll,
sync_msg->msg_body.routing_table_entry.dest,
sync_msg->msg_body.routing_table_entry.mask);
if (node == dll->head) {
printf("Deleted Destination IP: %s mask: %u\n",
sync_msg->msg_body.routing_table_entry.dest,
sync_msg->msg_body.routing_table_entry.mask);
}
}
break;
default:
break;
}
}
else {
node = find_mac(dll,
sync_msg->msg_body.mac_list_entry.mac);
switch (sync_msg->op_code) {
case CREATE:
if (node == dll->head) {
append(dll, &sync_msg->msg_body.mac_list_entry);
node = find_mac(dll,
sync_msg->msg_body.mac_list_entry.mac);
if (node != dll->head) {
mac_list_entry_t entry = *((mac_list_entry_t*)node->data);
printf("Added MAC: %s", entry.mac);
char ip[IP_ADDR_LEN];
if (get_IP(entry.mac, ip) != -1) {
printf("IP: %s", ip);
}
putchar('\n');
}
}
break;
case DELETE:
if (node == dll->head) {
del(dll, node);
node = find_mac(dll,
sync_msg->msg_body.mac_list_entry.mac);
if (node == dll->head) {
printf("Deleted: MAC: %s\n",
sync_msg->msg_body.mac_list_entry.mac);
unlink(sync_msg->msg_body.mac_list_entry.mac);
//deallocate shared memory region corresponding to this MAC key.
}
}
break;
default:
break;
}
}
}

52
Sync/sync.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef SYNC_H
#define SYNC_H
#include "routing-table.h"
#include "mac-list.h"
/*
* Synchronization protocol constants, structure,
* and API definitions.
*/
#define SOCKET_NAME "AdminNetworkSocket"
#define WAIT 0
#define RT 1
#define ML 2
typedef struct dll_ dll_t;
typedef enum {
CREATE,
UPDATE,
DELETE,
NONE // indicate that all current updates from server have been processed
}OPCODE;
/*
* Specifies whether we're dealing with L3 (IP routing table) or L2 (MAC address list)
*/
typedef enum {
L3,
L2
} LCODE;
typedef struct sync_msg_ {
OPCODE op_code;
LCODE l_code;
union {
routing_table_entry_t routing_table_entry;
mac_list_entry_t mac_list_entry;
}msg_body;
}sync_msg_t;
void process_sync_mesg(dll_t* dll, sync_msg_t* sync_msg);
extern int get_IP(const char* mac, char* ip);
#endif