NLSR with CCN Sync
diff --git a/nlsr-sync-0.0/nlsr_sync.c b/nlsr-sync-0.0/nlsr_sync.c
index 3839f67..e89fcb8 100644
--- a/nlsr-sync-0.0/nlsr_sync.c
+++ b/nlsr-sync-0.0/nlsr_sync.c
@@ -28,6 +28,7 @@
 #include "nlsr.h"
 #include "nlsr_sync.h"
 #include "nlsr_lsdb.h"
+#include "utility.h"
 
 int 
 get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
@@ -173,6 +174,101 @@
 	return (char *)ptr;
 }
 
+void 
+process_incoming_sync_content_lsa(char *content_data)
+{
+
+
+	if ( nlsr->debugging )
+		printf("process_incoming_sync_content_lsa called \n");
+	//if ( nlsr->detailed_logging )
+		//writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");	
+
+	char *sep="|";
+	char *rem;
+	char *orig_router;
+	char *orl;
+	int orig_router_length;
+	char *lst;
+	int ls_type;
+	char *lsid;
+	long int ls_id;
+	char *isvld;
+	int isValid;
+	char *num_link;
+	int no_link;
+	char *np;
+	char *np_length;
+	int name_length;
+	char *data;
+	char *orig_time;
+
+	
+	if ( nlsr->debugging )
+		printf("LSA Data \n");
+	//if ( nlsr->detailed_logging )
+	//	writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");	
+
+	if( strlen(content_data ) > 0 )
+	{
+
+		orig_router=strtok_r((char *)content_data,sep,&rem);
+		orl=strtok_r(NULL,sep,&rem);
+		orig_router_length=atoi(orl);
+
+		if ( nlsr->debugging )
+		{
+			printf("	Orig Router Name  : %s\n",orig_router);
+			printf("	Orig Router Length: %d\n",orig_router_length);
+		}
+
+		lst=strtok_r(NULL,sep,&rem);		
+		ls_type=atoi(lst);
+
+		if ( nlsr->debugging )
+			printf("	LS Type  : %d\n",ls_type);
+
+		if ( ls_type == LS_TYPE_NAME )
+		{
+			lsid=strtok_r(NULL,sep,&rem);
+			ls_id=atoi(lsid);
+			orig_time=strtok_r(NULL,sep,&rem);
+			isvld=strtok_r(NULL,sep,&rem);
+			isValid=atoi(isvld);
+			np=strtok_r(NULL,sep,&rem);
+			np_length=strtok_r(NULL,sep,&rem);
+			name_length=atoi(np_length);
+			if ( nlsr->debugging )
+			{
+				printf("	LS ID  : %ld\n",ls_id);
+				printf("	isValid  : %d\n",isValid);
+				printf("	Name Prefix : %s\n",np);
+				printf("	Orig Time   : %s\n",orig_time);
+				printf("	Name Prefix length: %d\n",name_length);
+			}
+
+			build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
+
+			print_name_lsdb();
+
+		}
+		else if ( ls_type == LS_TYPE_ADJ )
+		{
+			orig_time=strtok_r(NULL,sep,&rem);
+			num_link=strtok_r(NULL,sep,&rem);
+			no_link=atoi(num_link);
+			data=rem;
+
+			if ( nlsr->debugging )
+			{
+				printf("	No Link  : %d\n",no_link);
+				printf("	Data  : %s\n",data);
+			}
+			build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
+		}
+	}
+}
+
 void
 process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
 {
@@ -186,6 +282,10 @@
 	int ls_type;
 	long int ls_id=0;
 
+	char *time_stamp=(char *)malloc(20);
+	memset(time_stamp,0,20);
+	get_current_timestamp_micro(time_stamp);
+
 	struct ccn_charbuf *uri = ccn_charbuf_create();
 	ccn_uri_append(uri, content_name->buf, content_name->length, 0);	
 
@@ -195,28 +295,40 @@
 
 	res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
 	
-	printf("Ls Type: %s\n",lst);
+	//printf("Ls Type: %s\n",lst);
 	ls_type=atoi((char *)lst);
 	if(ls_type == LS_TYPE_NAME)
 	{
+		
 		res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
 		ls_id=atoi((char *)lsid);
 		res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
 		get_name_part(orig_router,content_name,components,3);
-		printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
 
-		int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
-		if ( is_new_name_lsa == 1 )
+		int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
+
+		//printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
+
+		if ( (strcmp((char *)orig_router,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp((char *)orig_router,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
 		{
-			printf("New NAME LSA.....\n");	
-			char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
-			printf("Content Data: %s \n",content_data);
+			int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
+			if ( is_new_name_lsa == 1 )
+			{
+				printf("New NAME LSA.....\n");	
+				char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
+				printf("Content Data: %s \n",content_data);
+				process_incoming_sync_content_lsa(content_data);
+			}
+			else 
+			{
+				printf("Name LSA / Newer Name LSA already xists in LSDB\n");
+				char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
+				printf("Content Data: %s \n",content_data);
+			}
 		}
 		else 
 		{
-			printf("Name LSA / Newer Name LSA already xists in LSDB\n");
-			char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
-			printf("Content Data: %s \n",content_data);
+			printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
 		}
 	}
 	else if(ls_type == LS_TYPE_ADJ)
@@ -224,22 +336,33 @@
 		res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
 		get_name_part(orig_router,content_name,components,2);
 		printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
-	
-		int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
-		if ( is_new_adj_lsa == 1 )
-		{
-			printf("New Adj LSA.....\n");	
-			char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
-			printf("Content Data: %s \n",content_data);			
-		}
-		else
-		{
 
-			printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
-			char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
-			printf("Content Data: %s \n",content_data);
-		}
+		int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
 
+		//printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
+
+		if ( (strcmp((char *)orig_router,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp((char *)orig_router,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )	
+		{
+			int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
+			if ( is_new_adj_lsa == 1 )
+			{
+				printf("New Adj LSA.....\n");	
+				char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
+				printf("Content Data: %s \n",content_data);
+				process_incoming_sync_content_lsa(content_data);			
+			}
+			else
+			{
+
+				printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
+				char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
+				printf("Content Data: %s \n",content_data);
+			}
+		}
+		else 
+		{
+			printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
+		}
 	}
 
 	ccn_charbuf_destroy(&uri);