Problem Statement on Timer Expiration

This commit is contained in:
Abhishek Sagar
2020-10-27 07:04:02 -07:00
parent 3af6b3c3e2
commit 8eab25d619
4 changed files with 343 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
rm *o
rm *exe
gcc -g -c rt.c -o rt.o
gcc -g -c rt_entry_expiration.c -o rt_entry_expiration.o
gcc -g rt_entry_expiration.o rt.o -o rt_entry_expiration.exe

View File

@@ -0,0 +1,134 @@
/*
* =====================================================================================
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include "rt.h"
void
rt_init_rt_table(rt_table_t *rt_table){
rt_table->head = NULL;
}
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;
head = rt_table->head;
rt_table->head = rt_entry;
rt_entry->prev = 0;
rt_entry->next = head;
if(head)
head->prev = rt_entry;
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);
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,
rt_entry->time_to_expire);
} ITERTAE_RT_TABLE_END(rt_tabl, rt_entry);
}

View File

@@ -0,0 +1,125 @@
/*
* =====================================================================================
*
* 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 <stdint.h>
#include <stdbool.h>
#include "../timerlib.h"
#define RT_TABLE_EXP_TIME 30 /* 30 sec */
typedef struct rt_entry_keys_{
char dest[16];
char mask;
} rt_entry_keys_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_entry_t;
typedef struct rt_table_{
rt_entry_t *head;
} 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;
}
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__ */

View File

@@ -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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#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;
}