mirror of
https://github.com/Hizenberg469/Timer-Library.git
synced 2026-04-20 02:02:25 +03:00
Timer Library Complete
This commit is contained in:
13
Assignment/CMakeLists.txt
Normal file
13
Assignment/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
set(RT_SRC "rt.c")
|
||||
set(RT_MAIN "rt_entry_expiration.c")
|
||||
|
||||
set(RT_EXE rtManager)
|
||||
|
||||
add_executable(${RT_EXE} ${RT_SRC}
|
||||
${RT_MAIN})
|
||||
|
||||
target_include_directories(${RT_EXE} PUBLIC
|
||||
${HEADER_DIR})
|
||||
|
||||
target_link_libraries(${RT_EXE} PUBLIC
|
||||
${TIMER_LIB})
|
||||
117
Assignment/rt.c
Normal file
117
Assignment/rt.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#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,
|
||||
void (*timer_cb)(Timer_t* , void* )) {
|
||||
|
||||
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->timer = setup_timer((*timer_cb), RT_TABLE_EXP_TIME*1000, 0, 0, (void*)rt_entry, false);
|
||||
|
||||
head = rt_table->head;
|
||||
rt_table->head = rt_entry;
|
||||
rt_entry->prev = 0;
|
||||
rt_entry->next = head;
|
||||
if (head)
|
||||
head->prev = rt_entry;
|
||||
|
||||
start_timer(rt_entry->timer);
|
||||
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);
|
||||
|
||||
printf("deleting rt entry %p [%s:%d]\n",
|
||||
rt_entry, rt_entry->rt_entry_keys.dest,
|
||||
rt_entry->rt_entry_keys.mask);
|
||||
|
||||
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) {
|
||||
|
||||
unsigned long time_in_milli_sec = timer_get_time_remaining_in_milli_sec(rt_entry->timer);
|
||||
|
||||
printf("%-20s %-4d %-20s %-12s %usec (%lu)\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,
|
||||
time_in_milli_sec);
|
||||
} ITERTAE_RT_TABLE_END(rt_tabl, rt_entry);
|
||||
}
|
||||
97
Assignment/rt.h
Normal file
97
Assignment/rt.h
Normal file
@@ -0,0 +1,97 @@
|
||||
#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 */
|
||||
Timer_t* timer;
|
||||
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, void (*timer_cb)(Timer_t*, void*));
|
||||
|
||||
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__ */
|
||||
80
Assignment/rt_entry_expiration.c
Normal file
80
Assignment/rt_entry_expiration.c
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <timerlib.h>
|
||||
#include <assert.h>
|
||||
#include "rt.h"
|
||||
|
||||
static rt_table_t rt;
|
||||
|
||||
void rt_entry_delete_on_timer_expiry(Timer_t* timer, void* user_data) {
|
||||
|
||||
assert(timer != NULL);
|
||||
assert(user_data != NULL);
|
||||
|
||||
rt_entry_t* rt_entry = (rt_entry_t*)user_data;
|
||||
|
||||
char dest_ip[16];
|
||||
strncpy(dest_ip,rt_entry->rt_entry_keys.dest, sizeof(rt_entry->rt_entry_keys.dest));
|
||||
char mask = rt_entry->rt_entry_keys.mask;
|
||||
delete_timer(rt_entry->timer);
|
||||
|
||||
assert(rt_delete_rt_entry(&rt, dest_ip, mask));
|
||||
|
||||
|
||||
}
|
||||
|
||||
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_entry_delete_on_timer_expiry);
|
||||
rt_add_new_rt_entry(&rt, "122.1.1.2", 32, "10.1.1.2", "eth1",rt_entry_delete_on_timer_expiry);
|
||||
rt_add_new_rt_entry(&rt, "122.1.1.3", 32, "10.1.1.3", "eth2",rt_entry_delete_on_timer_expiry);
|
||||
rt_add_new_rt_entry(&rt, "122.1.1.4", 32, "10.1.1.4", "eth3",rt_entry_delete_on_timer_expiry);
|
||||
rt_add_new_rt_entry(&rt, "122.1.1.5", 32, "10.1.1.5", "eth4",rt_entry_delete_on_timer_expiry);
|
||||
|
||||
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, rt_entry_delete_on_timer_expiry)) {
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user