blob: c8c416075ae2c24464bb63b48c666efd95c5f3a2 [file] [log] [blame]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/time.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ccn/ccn.h>
#include <ccn/uri.h>
#include <ccn/keystore.h>
#include <ccn/signing.h>
#include <ccn/schedule.h>
#include <ccn/hashtb.h>
#include "nlsr.h"
#include "nlsr_lsdb.h"
#include "utility.h"
#include "nlsr_ndn.h"
void
make_name_lsa_key(struct ccn_charbuf *key, struct ccn_charbuf *orig_router, unsigned int ls_type, long int nlsa_id, long int orig_time)
{
printf("make_name_lsa_key called \n");
ccn_charbuf_append_string(key,ccn_charbuf_as_string(orig_router));
struct ccn_charbuf *c=ccn_charbuf_create();
ccn_charbuf_reset(c);
ccn_charbuf_putf(c, "%d", ls_type);
ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
ccn_charbuf_reset(c);
ccn_charbuf_putf(c, "%ld", nlsa_id);
ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
ccn_charbuf_reset(c);
ccn_charbuf_putf(c, "%ld", orig_time);
ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
//ccn_charbuf_append_string(key,orig_time);
ccn_charbuf_destroy(&c);
printf("Key: %s length: %d\n",ccn_charbuf_as_string(key),(int)key->length);
}
void
install_name_lsa(struct nlsa *new_name_lsa)
{
printf("install_name_lsa called \n");
struct ccn_charbuf *key=ccn_charbuf_create();
make_name_lsa_key(key,new_name_lsa->header->orig_router,new_name_lsa->header->ls_type,new_name_lsa->header->ls_id,new_name_lsa->header->orig_time);
struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *));
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
int res;
hashtb_start(nlsr->lsdb->name_lsdb, e);
res = hashtb_seek(e, key->buf , key->length, 0);
if(res == HT_NEW_ENTRY )
{
printf("New Name LSA. Added...\n");
name_lsa = e->data;
name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *));
name_lsa->header->ls_type=new_name_lsa->header->ls_type;
name_lsa->header->orig_time=new_name_lsa->header->orig_time;
name_lsa->header->ls_id=new_name_lsa->header->ls_id;
name_lsa->header->orig_router=ccn_charbuf_create();
ccn_charbuf_append_string(name_lsa->header->orig_router,ccn_charbuf_as_string(new_name_lsa->header->orig_router));
name_lsa->header->isValid=new_name_lsa->header->isValid;
name_lsa->name_prefix=ccn_charbuf_create();
ccn_charbuf_append_string(name_lsa->name_prefix,ccn_charbuf_as_string(new_name_lsa->name_prefix));
//ccn_charbuf_append_charbuf(name_lsa->name_prefix,new_name_lsa->name_prefix);
}
else if(res == HT_OLD_ENTRY)
{
printf("Duplicate Name LSA. Discarded...\n");
}
hashtb_end(e);
ccn_charbuf_destroy(&key);
}
void
build_name_lsa(struct nlsa *name_lsa, struct ccn_charbuf *name_prefix)
{
printf("build_name_lsa called \n");
//struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *));
//name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *));
//name_lsa->header->orig_router=ccn_charbuf_create();
name_lsa->header->ls_type=LS_TYPE_NAME;
name_lsa->header->orig_time=get_current_time_microsec();
//name_lsa->header->orig_time=(char *)malloc(strlen(get_current_time_microsec())+1);
//memcpy(name_lsa->header->orig_time,get_current_time_microsec(),strlen(get_current_time_microsec())+1);
name_lsa->header->ls_id=++nlsr->nlsa_id;
ccn_charbuf_append_string(name_lsa->header->orig_router,nlsr->router_name);
name_lsa->header->isValid=1;
//name_lsa->name_prefix=ccn_charbuf_create();
ccn_charbuf_append_string(name_lsa->name_prefix,ccn_charbuf_as_string(name_prefix));
//ccn_charbuf_append_charbuf(name_lsa->name_prefix,name_prefix);
//return name_lsa;
}
int
initial_build_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
{
printf("initial_build_name_lsa called \n");
int i, npl_element;
struct name_prefix *np;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npl, e);
npl_element=hashtb_n(nlsr->npl);
for(i=0;i<npl_element;i++)
{
np=e->data;
struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *));
name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *));
name_lsa->header->orig_router=ccn_charbuf_create();
name_lsa->name_prefix=ccn_charbuf_create();
struct ccn_charbuf *name;
name=ccn_charbuf_create();
ccn_charbuf_append_string(name,np->name);
build_name_lsa(name_lsa,name);
install_name_lsa(name_lsa);
//ccn_charbuf_destroy(&name_lsa->header->orig_router);
//free(name_lsa->header);
//ccn_charbuf_destroy(&name_lsa->name_prefix);
free(name_lsa);
ccn_charbuf_destroy(&name);
hashtb_next(e);
}
hashtb_end(e);
print_name_lsdb();
//nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
return 0;
}
void
print_name_lsa(struct nlsa *name_lsa)
{
printf("print_name_lsa called \n");
printf("-----------Name LSA---------------\n");
printf(" Origination Router : %s\n",ccn_charbuf_as_string(name_lsa->header->orig_router));
printf(" Origination Router Length: %d\n",(int)name_lsa->header->orig_router->length);
printf(" LS Type : %d\n",name_lsa->header->ls_type);
printf(" LS Id : %ld\n",name_lsa->header->ls_id);
printf(" Origination Time : %ld\n",name_lsa->header->orig_time);
printf(" Is Valid : %d\n",name_lsa->header->isValid);
printf(" LSA Data \n");
printf(" Name Prefix: : %s\n",ccn_charbuf_as_string(name_lsa->name_prefix));
printf(" Name Prefix Length : %d\n",(int)name_lsa->name_prefix->length);
printf("\n");
}
void
print_name_lsdb(void)
{
printf("print_name_lsdb called \n");
int i, lsdb_element;
struct nlsa *name_lsa;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->lsdb->name_lsdb, e);
lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
for(i=0;i<lsdb_element;i++)
{
name_lsa=e->data;
print_name_lsa(name_lsa);
hashtb_next(e);
}
hashtb_end(e);
}
int
install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
{
printf("install_adj_lsa called \n");
ev = ccn_schedule_event(nlsr->sched, 1000000, &install_adj_lsa, NULL, 0);
return 0;
}