From b69ddcdb962b8de6b3fb38af59bdd7ef2f9de19a Mon Sep 17 00:00:00 2001 From: Abhishek Sagar Date: Sat, 31 Oct 2020 13:24:58 -0700 Subject: [PATCH] Soln Ready --- WheelTimer/Assignment/soln/compile.sh | 8 + WheelTimer/Assignment/soln/rt.c | 163 ++++++++++++++++++ WheelTimer/Assignment/soln/rt.h | 132 ++++++++++++++ .../Assignment/soln/rt_entry_expiration.c | 79 +++++++++ 4 files changed, 382 insertions(+) create mode 100644 WheelTimer/Assignment/soln/compile.sh create mode 100644 WheelTimer/Assignment/soln/rt.c create mode 100644 WheelTimer/Assignment/soln/rt.h create mode 100644 WheelTimer/Assignment/soln/rt_entry_expiration.c diff --git a/WheelTimer/Assignment/soln/compile.sh b/WheelTimer/Assignment/soln/compile.sh new file mode 100644 index 0000000..848a3a5 --- /dev/null +++ b/WheelTimer/Assignment/soln/compile.sh @@ -0,0 +1,8 @@ +rm *o +rm *exe +gcc -g -c rt.c -o rt.o +gcc -g -c rt_entry_expiration.c -o rt_entry_expiration.o +# below two lines compiles the WheelTimer Library +gcc -g -c ../../WheelTimer.c -o ../../WheelTimer.o +gcc -g -c ../../gluethread/glthread.c -o ../../gluethread/glthread.o +gcc -g rt_entry_expiration.o rt.o ../../WheelTimer.o ../../gluethread/glthread.o -o rt_entry_expiration.exe -lpthread diff --git a/WheelTimer/Assignment/soln/rt.c b/WheelTimer/Assignment/soln/rt.c new file mode 100644 index 0000000..5504269 --- /dev/null +++ b/WheelTimer/Assignment/soln/rt.c @@ -0,0 +1,163 @@ +/* + * ===================================================================================== + * + * Filename: rt.h + * + * Description: + * + * Version: 1.0 + * Created: 02/22/2020 06:14:33 AM + * Revision: none + * Compiler: gcc + * + * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com + * Company: Juniper Networks + * + * This file is part of the Netlink Sockets distribution (https://github.com/sachinites) + * Copyright (c) 2019 Abhishek Sagar. + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects + * + * ===================================================================================== + */ + +#include +#include +#include +#include +#include "rt.h" +#include "../../WheelTimer.h" + +void +rt_init_rt_table(rt_table_t *rt_table){ + + rt_table->head = NULL; + rt_table->wt = init_wheel_timer(60, 1); + start_wheel_timer(rt_table->wt); +} + +static void +timer_delete_rt_entry_cbk(void *arg, int arg_size){ + + rt_entry_t *rt_entry = (rt_entry_t *)arg; + de_register_app_event(rt_entry->rt_table->wt, rt_entry->exp_wt_elem); + rt_entry->exp_wt_elem = NULL; + rt_entry_remove(rt_entry->rt_table, rt_entry); + free(rt_entry); +} + +bool +rt_add_new_rt_entry(rt_table_t *rt_table, + char *dest, + char mask, + char *gw_ip, + char *oif){ + + rt_entry_t *head = NULL; + rt_entry_t *rt_entry = NULL; + + rt_entry = calloc(1, sizeof(rt_entry_t)); + + if(!rt_entry) + return false; + + strncpy(rt_entry->rt_entry_keys.dest, dest, sizeof(rt_entry->rt_entry_keys.dest)); + rt_entry->rt_entry_keys.mask = mask; + + if(gw_ip) + strncpy(rt_entry->gw_ip, gw_ip, sizeof(rt_entry->gw_ip)); + if(oif) + strncpy(rt_entry->oif, oif, sizeof(rt_entry->oif)); + + rt_entry->time_to_expire = RT_TABLE_EXP_TIME; + rt_entry->rt_table = rt_table; + + head = rt_table->head; + rt_table->head = rt_entry; + rt_entry->prev = 0; + rt_entry->next = head; + if(head) + head->prev = rt_entry; + + rt_entry->exp_wt_elem = register_app_event( + rt_table->wt, + timer_delete_rt_entry_cbk, + (void *)rt_entry, + sizeof(rt_entry_t), + rt_entry->time_to_expire, + 0); + + return true; +} + +bool +rt_delete_rt_entry(rt_table_t *rt_table, + char *dest, char mask){ + + rt_entry_t *rt_entry = NULL; + + ITERTAE_RT_TABLE_BEGIN(rt_table, rt_entry){ + + if(strncmp(rt_entry->rt_entry_keys.dest, + dest, sizeof(rt_entry->rt_entry_keys.dest)) == 0 && + rt_entry->rt_entry_keys.mask == mask){ + + rt_entry_remove(rt_table, rt_entry); + + if(rt_entry->exp_wt_elem) { + de_register_app_event(rt_table->wt, rt_entry->exp_wt_elem); + rt_entry->exp_wt_elem = NULL; + } + free(rt_entry); + return true; + } + } ITERTAE_RT_TABLE_END(rt_table, curr); + + return false; +} + +bool +rt_update_rt_entry(rt_table_t *rt_table, + char *dest, + char mask, + char *new_gw_ip, + char *new_oif){ + + return true; +} + +void +rt_clear_rt_table(rt_table_t *rt_table){ + + +} + +void +rt_free_rt_table(rt_table_t *rt_table){ + + +} + +void +rt_dump_rt_table(rt_table_t *rt_table){ + + rt_entry_t *rt_entry = NULL; + + ITERTAE_RT_TABLE_BEGIN(rt_table, rt_entry){ + + printf("%-20s %-4d %-20s %-12s %usec\n", + rt_entry->rt_entry_keys.dest, + rt_entry->rt_entry_keys.mask, + rt_entry->gw_ip, + rt_entry->oif, + wt_get_remaining_time(rt_table->wt, + rt_entry->exp_wt_elem)); + } ITERTAE_RT_TABLE_END(rt_tabl, rt_entry); +} diff --git a/WheelTimer/Assignment/soln/rt.h b/WheelTimer/Assignment/soln/rt.h new file mode 100644 index 0000000..ca7dcb1 --- /dev/null +++ b/WheelTimer/Assignment/soln/rt.h @@ -0,0 +1,132 @@ +/* + * ===================================================================================== + * + * Filename: rt.h + * + * Description: + * + * Version: 1.0 + * Created: 02/22/2020 06:14:33 AM + * Revision: none + * Compiler: gcc + * + * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com + * Company: Juniper Networks + * + * This file is part of the Netlink Sockets distribution (https://github.com/sachinites) + * Copyright (c) 2019 Abhishek Sagar. + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects + * + * ===================================================================================== + */ + +#ifndef __RT__ +#define __RT__ + +#include +#include + +#define RT_TABLE_EXP_TIME 30 /* 30 sec */ + +/* import the definitions of the external Structures */ +typedef struct _wheel_timer_t wheel_timer_t; +typedef struct _wheel_timer_elem_t wheel_timer_elem_t; + +typedef struct rt_entry_keys_{ + + char dest[16]; + char mask; +} rt_entry_keys_t; + +typedef struct rt_table_ rt_table_t; +typedef struct rt_entry_{ + + /* A Structure which represents only the keys of the + * Routing Table.*/ + rt_entry_keys_t rt_entry_keys; + + char gw_ip[16]; + char oif[32]; + uint32_t time_to_expire; /* time left to delete the entry */ + struct rt_entry_ *prev; + struct rt_entry_ *next; + rt_table_t *rt_table; + wheel_timer_elem_t *exp_wt_elem; +} rt_entry_t; + +typedef struct rt_table_{ + + rt_entry_t *head; + wheel_timer_t *wt; +} rt_table_t; + +void +rt_init_rt_table(rt_table_t *rt_table); + +bool +rt_add_new_rt_entry(rt_table_t *rt_table, + char *dest_ip, char mask, char *gw_ip, char *oif); + +bool +rt_delete_rt_entry(rt_table_t *rt_table, + char *dest_ip, char mask); + +bool +rt_update_rt_entry(rt_table_t *rt_table, + char *dest_ip, char mask, + char *new_gw_ip, char *new_oif); + +void +rt_clear_rt_table(rt_table_t *rt_table); + +void +rt_free_rt_table(rt_table_t *rt_table); + +void +rt_dump_rt_table(rt_table_t *rt_table); + +static inline void +rt_entry_remove(rt_table_t *rt_table, + rt_entry_t *rt_entry){ + + if(!rt_entry->prev){ + if(rt_entry->next){ + rt_entry->next->prev = 0; + rt_table->head = rt_entry->next; + rt_entry->next = 0; + return; + } + rt_table->head = NULL; + return; + } + if(!rt_entry->next){ + rt_entry->prev->next = 0; + rt_entry->prev = 0; + return; + } + + rt_entry->prev->next = rt_entry->next; + rt_entry->next->prev = rt_entry->prev; + rt_entry->prev = 0; + rt_entry->next = 0; +} + +#define ITERTAE_RT_TABLE_BEGIN(rt_table_ptr, rt_entry_ptr) \ +{ \ + rt_entry_t *_next_rt_entry; \ + for((rt_entry_ptr) = (rt_table_ptr)->head; \ + (rt_entry_ptr); \ + (rt_entry_ptr) = _next_rt_entry) { \ + _next_rt_entry = (rt_entry_ptr)->next; + +#define ITERTAE_RT_TABLE_END(rt_table_ptr, rt_entry_ptr) }} + +#endif /* __RT__ */ diff --git a/WheelTimer/Assignment/soln/rt_entry_expiration.c b/WheelTimer/Assignment/soln/rt_entry_expiration.c new file mode 100644 index 0000000..ed09a0c --- /dev/null +++ b/WheelTimer/Assignment/soln/rt_entry_expiration.c @@ -0,0 +1,79 @@ +/* + * ===================================================================================== + * + * Filename: rt_entry_expiration.c + * + * Description: This file illustrates how to age out rt table entry + * + * Version: 1.0 + * Created: 10/27/2020 04:59:45 AM + * Revision: none + * Compiler: gcc + * + * Author: ABHISHEK SAGAR (), sachinites@gmail.com + * Organization: Juniper Networks + * + * ===================================================================================== + */ + +#include +#include +#include +#include "rt.h" + +static rt_table_t rt; + +int +main(int argc, char **argv){ + + rt_init_rt_table(&rt); + + rt_add_new_rt_entry(&rt, "122.1.1.1", 32, "10.1.1.1", "eth0"); + rt_add_new_rt_entry(&rt, "122.1.1.2", 32, "10.1.1.2", "eth1"); + rt_add_new_rt_entry(&rt, "122.1.1.3", 32, "10.1.1.3", "eth2"); + rt_add_new_rt_entry(&rt, "122.1.1.4", 32, "10.1.1.4", "eth3"); + rt_add_new_rt_entry(&rt, "122.1.1.5", 32, "10.1.1.5", "eth4"); + + while(1){ + printf("1. Add rt table entry\n"); + printf("2. Update rt table entry\n"); + printf("3. Delete rt table entry\n"); + printf("4. Dump rt table\n"); + + int choice; + printf("Enter Choice :"); + scanf("%d", &choice); + fflush(stdin); + switch(choice){ + case 1: + { + char dest[16]; + uint8_t mask; + char oif[32]; + char gw[16]; + printf("Enter Destination :"); + scanf("%s", dest); + printf("Mask : "); + scanf("%hhd", &mask); + printf("Enter oif name :"); + scanf("%s", oif); + printf("Enter Gateway IP :"); + scanf("%s", gw); + if(!rt_add_new_rt_entry(&rt, dest, mask, + gw, oif)){ + printf("Error : Could not add an entry\n"); + } + } + break; + case 2: + break; + case 3: + break; + case 4: + rt_dump_rt_table(&rt); + default: + ; + } + } + return 0; +}