Merge branch 'master' of github.com:NDN-Routing/NLSR0.0
diff --git a/Makefile b/Makefile
index f26d056..893ff21 100644
--- a/Makefile
+++ b/Makefile
@@ -2,17 +2,20 @@
 CFLAGS = -g -Wall -Wpointer-arith -Wreturn-type -Wstrict-prototypes
 LIBS = -lccn -lcrypto
 
-PROGRAMS = nlsr
+PROGRAMS = nlsrc nlsr
 INSTALL_PATH=/usr/local/bin/
 
 NLSR_SRCS=nlsr.c nlsr_ndn.c nlsr_npl.c  nlsr_adl.c nlsr_lsdb.c nlsr_route.c nlsr_npt.c nlsr_fib.c utility.c
-
+NLSRC_SRCS=nlsrc.c
 
 all: $(PROGRAMS)
 
 nlsr: $(NLSR_SRCS)
 	$(CC) $(CFLAGS) $(NLSR_SRCS) -o nlsr $(LIBS) -lm
 
+nlsrc: $(NLSRC_SRCS)
+	$(CC) $(CFLAGS) $(NLSRC_SRCS) -o nlsrc $(LIBS) -lm
+
 install: $(PROGRAMS)
 	cp $(PROGRAMS) $(INSTALL_PATH)
 	cd $(INSTALL_PATH); chown root:0 $(PROGRAMS); chmod 755 $(PROGRAMS)
diff --git a/macbook.conf b/macbook.conf
index 2bfe740..19b6c30 100644
--- a/macbook.conf
+++ b/macbook.conf
@@ -2,7 +2,7 @@
 #
 router-name /ndn/memphis.edu/netlab/macbook/
 ccnneighbor /ndn/memphis.edu/dunhall/castor face9
-ccnneighbor /ndn/memphis.edu/netlab/pollux face11
+ccnneighbor /ndn/memphis.edu/pollux face38
 ccnname /ndn/memphis.edu/patterson
 ccnname /ndn/memphis.edu/houston/
 #------lsdb-synch-interval-----
@@ -12,5 +12,6 @@
 lsa-refresh-time 600
 router-dead-interval 900
 multi-path-face-num 2 
+#debug on
 
 logdir /Users/akmhoque/NLSR2.0
diff --git a/nlsr.c b/nlsr.c
index e1a49c2..7846a1d 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -1,19 +1,24 @@
-#include<stdio.h>
-#include<string.h>
-#include<stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <assert.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
-#include <sys/types.h>
-#include <signal.h>
-
-
-
 
 #include <ccn/ccn.h>
 #include <ccn/uri.h>
@@ -52,6 +57,7 @@
 {
     { "daemon",      no_argument,       NULL, 'd'},
     { "config_file", required_argument, NULL, 'f'},
+    { "api_port",    required_argument, NULL, 'p'},
     { "help",        no_argument,       NULL, 'h'},
     { 0 }
 };
@@ -64,6 +70,7 @@
 	NDN routing....\n\
 	-d, --daemon        Run in daemon mode\n\
 	-f, --config_file   Specify configuration file name\n\
+	-p, --api_port      port where api client will connect\n\
 	-h, --help          Display this help message\n", progname);
 
     exit(1);
@@ -595,6 +602,189 @@
 	return 0;
 }
 
+char *
+process_api_client_command(char *command)
+{
+	char *msg;
+	msg=(char *)malloc(100);	
+	memset(msg,100,0);
+	//strcpy(msg,"Action Carried Out for NLSR Api Client");
+
+	const char *sep=" \t\n";
+	char *rem=NULL;
+	char *cmd_type=NULL;
+	char *op_type=NULL;
+	char *name=NULL;
+	char *face=NULL;
+	int face_id;
+	int res;
+
+	op_type=strtok_r(command,sep,&rem);
+	cmd_type=strtok_r(NULL,sep,&rem);
+	name=strtok_r(NULL,sep,&rem);
+	if ( name[strlen(name)-1] == '/' )
+		name[strlen(name)-1]='\0';
+
+	struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+	np->name=(char *)malloc(strlen(name)+1);
+	memset(np->name,0,strlen(name)+1);
+	memcpy(np->name,name,strlen(name)+1);
+	np->length=strlen(name)+1;
+
+	if ( strcmp(cmd_type,"name")!= 0 )
+	{
+		face=strtok_r(NULL,sep,&rem);
+		sscanf(face,"face%d",&face_id);
+	}
+	
+	if ( strcmp(cmd_type,"name")== 0 )
+	{
+		if ( strcmp(op_type,"del") == 0 ) 
+		{
+			res=does_name_exist_in_npl(np);
+			if ( res == 0)
+			{
+				sprintf(msg,"Name %s does not exist !!",name);
+			}
+			else
+			{
+				long int ls_id=get_lsa_id_from_npl(np);
+				if ( ls_id != 0 )
+				{
+					make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
+					sprintf(msg,"Name %s has been deleted and Advertised.",name);
+				}
+				else 
+				{
+					sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
+				}
+			}			
+		}
+		else if ( strcmp(op_type,"add") == 0 )
+		{
+			res=does_name_exist_in_npl(np);
+			if ( res == 0)
+			{
+				add_name_to_npl(np);
+				build_and_install_single_name_lsa(np);
+				sprintf(msg,"Name %s has been added to advertise.",name);
+			}
+			else
+			{
+				sprintf(msg,"Name %s has already been advertised from this router !!",name);
+			}
+		} 
+	}
+	else if ( strcmp(cmd_type,"neighbor") == 0 )
+	{
+		if ( strcmp(op_type,"del") == 0 ) 
+		{
+			res=is_neighbor(np->name);
+			if ( res == 0)
+			{
+				sprintf(msg,"Neighbor %s does not exist !!",name);
+			}
+			else
+			{
+				update_adjacent_status_to_adl(np,NBR_DOWN);
+				delete_nbr_from_adl(np);
+				if(!nlsr->is_build_adj_lsa_sheduled)
+				{
+					nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
+					nlsr->is_build_adj_lsa_sheduled=1;		
+				}
+				sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);	
+			}
+		}
+		else if ( strcmp(op_type,"add") == 0 )
+		{
+			res=is_neighbor(np->name);
+			if ( res == 0 )
+			{
+				add_nbr_to_adl(np,face_id);
+				sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
+			}
+			else
+			{
+				sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
+			}
+		}
+	}
+		
+
+	return msg;
+}
+
+int
+nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
+{
+	struct timeval timeout;
+	if ( time_out_micro_sec < 0 )
+	{
+		timeout.tv_sec=1;
+		timeout.tv_usec=0;
+	}
+	else
+	{
+		time_out_micro_sec=(long int)time_out_micro_sec*0.4;
+		timeout.tv_sec=time_out_micro_sec / 1000000;
+		timeout.tv_usec=time_out_micro_sec % 1000000;
+	}
+
+	
+	int fd;
+	int nread;
+	int result;
+	fd_set testfds;
+	unsigned int client_len;
+	int client_sockfd;
+	char recv_buffer[1024];
+	bzero(recv_buffer,1024);
+	struct sockaddr_in client_address;
+
+	testfds=nlsr->readfds;
+	result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
+	
+	for(fd = 0; fd < FD_SETSIZE; fd++) 
+	{
+		if(FD_ISSET(fd,&testfds)) 
+		{
+			if ( fd == ccn_fd )
+			{
+				return 0;
+			}			
+			else if(fd == nlsr->nlsr_api_server_sock_fd)
+			{
+				client_len = sizeof(client_address);
+				client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
+				FD_SET(client_sockfd, &nlsr->readfds);
+			}
+			else
+			{   
+					
+				ioctl(fd, FIONREAD, &nread);
+				if(nread == 0) 
+				{
+					close(fd);
+					FD_CLR(fd, &nlsr->readfds);
+				}
+				else 
+				{
+					recv(fd, recv_buffer, 1024, 0);
+					printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
+					char *msg=process_api_client_command(recv_buffer);
+					send(fd, msg, strlen(msg),0);
+					free(msg);
+					close(fd);
+					FD_CLR(fd, &nlsr->readfds);
+				}
+			}
+		}
+	}
+
+	return 0;
+}
+
 void 
 nlsr_destroy( void )
 {
@@ -635,7 +825,9 @@
 	hashtb_end(e);
 	hashtb_destroy(&nlsr->npt);
 
-	
+
+	close(nlsr->nlsr_api_server_sock_fd);	
+
 	ccn_schedule_destroy(&nlsr->sched);
 	ccn_destroy(&nlsr->ccn);
 
@@ -652,6 +844,38 @@
 }
 
 
+void
+init_api_server(int ccn_fd)
+{
+	int server_sockfd;
+	int server_len;
+	struct sockaddr_in server_address;
+	unsigned int yes=1;	
+
+	server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
+
+	int flags = fcntl(server_sockfd, F_GETFL, 0);
+	fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
+
+	if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) 
+	{
+       		ON_ERROR_DESTROY(-1);
+       	}
+
+	server_address.sin_family = AF_INET;
+	server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
+	server_address.sin_port = nlsr->api_port;
+
+	server_len = sizeof(server_address);
+	bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
+	listen(server_sockfd, 100);
+	FD_ZERO(&nlsr->readfds);
+	FD_SET(server_sockfd, &nlsr->readfds);
+	FD_SET(ccn_fd, &nlsr->readfds);
+	nlsr->nlsr_api_server_sock_fd=server_sockfd;
+
+}
+
 int 
 init_nlsr(void)
 {
@@ -676,7 +900,7 @@
 	struct hashtb_param param_adl = {0};
 	nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
 	struct hashtb_param param_npl = {0};
-	nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
+	nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
 	struct hashtb_param param_pit_alsa = {0};	
 	nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
 	struct hashtb_param param_npt = {0};	
@@ -721,10 +945,10 @@
 	nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
 	nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
 	nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
-
-
 	nlsr->semaphor=NLSR_UNLOCKED;
 
+	nlsr->api_port=API_PORT;
+
 	return 0;
 }
 
@@ -735,10 +959,11 @@
     	int res, ret;
     	char *config_file;
 	int daemon_mode=0;
+	int port=0;
 
 	
 
-	while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1) 
+	while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1) 
 	{
         	switch (res) 
 		{
@@ -748,6 +973,9 @@
 			case 'f':
 				config_file = optarg;
 				break;
+			case 'p':
+				port = atoi(optarg);
+				break;
 			case 'h':
 			default:
 				usage(argv[0]);
@@ -756,8 +984,11 @@
 
 	ret=init_nlsr();	
     	ON_ERROR_EXIT(ret);
+
+	if ( port !=0 )
+		nlsr->api_port=port;
+
 	readConfigFile(config_file);
-	
 	if ( daemon_mode == 1 )
 	{
 		daemonize_nlsr();
@@ -766,12 +997,16 @@
 	startLogging(nlsr->logDir);
 	
 	nlsr->ccn=ccn_create();
-	if(ccn_connect(nlsr->ccn, NULL) == -1)
+	int ccn_fd=ccn_connect(nlsr->ccn, NULL);
+	if(ccn_fd == -1)
 	{
 		fprintf(stderr,"Could not connect to ccnd\n");
 		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
 		ON_ERROR_DESTROY(-1);
 	}
+
+	init_api_server(ccn_fd);
+	
 	struct ccn_charbuf *router_prefix;	
 	router_prefix=ccn_charbuf_create(); 
 	res=ccn_name_from_uri(router_prefix,nlsr->router_name);		
@@ -809,17 +1044,20 @@
 	nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
 	nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
 
+	
 	while(1)
 	{	
 		if ( nlsr->semaphor == NLSR_UNLOCKED  )
 		{
 			if( nlsr->sched != NULL )
 			{
-				ccn_schedule_run(nlsr->sched);
+				long int micro_sec=ccn_schedule_run(nlsr->sched);
+				res=nlsr_api_server_poll(micro_sec,ccn_fd);
+				ON_ERROR_DESTROY(res);
 			}
 			if(nlsr->ccn != NULL)
 			{
-        			res = ccn_run(nlsr->ccn, 500);
+        			res = ccn_run(nlsr->ccn, 0);
 			}
 			if (!(nlsr->sched && nlsr->ccn))
 			{	      
@@ -828,6 +1066,7 @@
 		}
 
 	}
+	
 
 	return 0;
 }
diff --git a/nlsr.h b/nlsr.h
index f113d52..a53cc2b 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -16,6 +16,8 @@
 #define NAME_LSA_VALID 1
 #define NAME_LSA_INVALID 0
 
+#define API_PORT 9696
+
 
 struct name_prefix
 {
@@ -83,6 +85,10 @@
 	int debugging;
 
 	int semaphor;
+
+	int nlsr_api_server_sock_fd;
+	fd_set readfds;
+	int api_port;
 	
 };
 
diff --git a/nlsr_adl.c b/nlsr_adl.c
index 1c30be8..54d8954 100644
--- a/nlsr_adl.c
+++ b/nlsr_adl.c
@@ -378,6 +378,35 @@
 	hashtb_end(e);
 }
 
+
+void 
+delete_nbr_from_adl(struct name_prefix *nbr)
+{
+	if ( nlsr->debugging )
+		printf("delete_nbr_from_adl called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_nbr_from_adl called \n");
+
+	int res;
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+
+	hashtb_start(nlsr->adl, e);
+   	res = hashtb_seek(e, nbr->name, nbr->length, 0);
+
+
+	if (res == HT_OLD_ENTRY)
+	{
+		hashtb_delete(e);	
+	}
+	else if(res == HT_NEW_ENTRY)
+	{
+		hashtb_delete(e);
+	}
+
+	hashtb_end(e);
+}
+
 void 
 update_lsdb_synch_interval_to_adl(struct name_prefix *nbr, long int interval)
 {
diff --git a/nlsr_adl.h b/nlsr_adl.h
index a1f48b2..5e9fa5a 100644
--- a/nlsr_adl.h
+++ b/nlsr_adl.h
@@ -21,6 +21,7 @@
 };
 
 void add_nbr_to_adl(struct name_prefix *new_nbr,int face);
+void delete_nbr_from_adl(struct name_prefix *nbr);
 void print_adjacent(struct ndn_neighbor *nbr);
 void print_adjacent_from_adl(void);
 int get_adjacent_status(struct name_prefix *nbr);
diff --git a/nlsr_lsdb.c b/nlsr_lsdb.c
index 5b30e70..bec9869 100644
--- a/nlsr_lsdb.c
+++ b/nlsr_lsdb.c
@@ -64,7 +64,7 @@
 	memcpy(key+strlen(key),"/",1);
 	memcpy(key+strlen(key),lsid,strlen(lsid));
 
-	printf("Key: %s\n",key);
+	//printf("Key: %s\n",key);
 	
 }
 
@@ -77,7 +77,8 @@
 		writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
 
 	int i, npl_element;
-	struct name_prefix *np;
+	//struct name_prefix *np;
+	struct name_prefix_list_entry *npe;
 
 	struct hashtb_enumerator ee;
     	struct hashtb_enumerator *e = &ee;
@@ -87,11 +88,12 @@
 
 	for(i=0;i<npl_element;i++)
 	{
-		np=e->data;
+		npe=e->data;
 		struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
-		build_name_lsa(name_lsa,np);
+		build_name_lsa(name_lsa,npe->np);
 		
 		install_name_lsa(name_lsa);
+		update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
 		free(name_lsa->header->orig_router->name);
 		free(name_lsa->header->orig_router);
 		free(name_lsa->header);
@@ -102,6 +104,33 @@
 	}
 
 	hashtb_end(e);	
+	
+	print_name_prefix_from_npl();
+
+}
+
+void 
+build_and_install_single_name_lsa(struct name_prefix *np)
+{
+	if ( nlsr->debugging )
+		printf("build_and_install_single_name_lsa called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
+	
+	struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
+	build_name_lsa(name_lsa,np);
+		
+	install_name_lsa(name_lsa);
+	update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
+
+	free(name_lsa->header->orig_router->name);
+	free(name_lsa->header->orig_router);
+	free(name_lsa->header);
+	free(name_lsa->name_prefix->name);
+	free(name_lsa->name_prefix);
+	free(name_lsa);
+	
+	print_name_prefix_from_npl();
 
 }
 
@@ -162,8 +191,12 @@
 		memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
 
 
-		make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);	
-		printf("Key:%s Length:%d\n",key,(int)strlen(key));
+		make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
+		if ( nlsr->debugging )
+			printf("Key:%s Length:%d\n",key,(int)strlen(key));
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));	
+		
 
 		struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free
 
@@ -504,7 +537,10 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
 
 
@@ -732,7 +768,10 @@
 		{
 			if ( adj_lsa->no_link > 0)
 			{
-				printf("New ADJ LSA... Adding to LSDB\n");
+				if ( nlsr->debugging )
+					printf("New ADJ LSA... Adding to LSDB\n");
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
 				new_adj_lsa = e->data;
 
 				new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
@@ -1008,7 +1047,10 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
 
 void 
@@ -1456,6 +1498,75 @@
 	hashtb_end(e);
 }
 
+void
+make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
+{
+
+	if ( nlsr->debugging )
+		printf("make_name_lsa_invalid called  \n");	
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called  \n");
+
+
+	char lst[2];
+	memset(lst,0,2);
+	sprintf(lst,"%d",ls_type);	
+
+	char lsid[10];
+	memset(lsid,0,10);
+	sprintf(lsid,"%ld",ls_id);
+	
+	
+	char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
+	memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
+
+
+	make_name_lsa_key(key, np->name,ls_type,ls_id);	
+	printf("Key:%s Length:%d\n",key,(int)strlen(key));
+
+	struct nlsa *nlsa;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+	 	
+    	int res;
+
+   	hashtb_start(nlsr->lsdb->name_lsdb, e);
+    	res = hashtb_seek(e, key,strlen(key) , 0);
+
+	if( res == HT_OLD_ENTRY )
+	{
+		nlsa=e->data;
+	
+		nlsa->header->isValid=0;
+
+		writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
+		writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
+		write_log_for_name_lsa(nlsa);
+		writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
+
+		hashtb_delete(e);
+	}
+	else if( res == HT_NEW_ENTRY )
+	{
+		hashtb_delete(e);
+	}
+	hashtb_end(e);
+	
+	if ( nlsr->debugging )
+		printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+
+	set_new_lsdb_version();	
+
+	if ( nlsr->debugging )
+		printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+
+}
+
 int 
 delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
 {
diff --git a/nlsr_lsdb.h b/nlsr_lsdb.h
index 9ba33b1..0383954 100644
--- a/nlsr_lsdb.h
+++ b/nlsr_lsdb.h
@@ -47,6 +47,7 @@
 
 void set_new_lsdb_version(void);
 void build_and_install_name_lsas(void);
+void build_and_install_single_name_lsa(struct name_prefix *np);
 void build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np);
 void install_name_lsa(struct nlsa *name_lsa);
 void print_name_lsa(struct nlsa *name_lsa);
@@ -81,4 +82,6 @@
 void get_name_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId);
 void get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId);
 
+void make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id);
+
 #endif
diff --git a/nlsr_ndn.c b/nlsr_ndn.c
index 82e8df8..9af0a05 100644
--- a/nlsr_ndn.c
+++ b/nlsr_ndn.c
@@ -839,38 +839,69 @@
 			orig_router=strtok_r(NULL,sep,&rem);
 			lst=strtok_r(NULL,sep,&rem);
 			ls_type=atoi(lst);
-			printf("Orig Router: %s ls Type: %d",orig_router,ls_type);			
+
+			if ( nlsr->debugging )
+				printf("Orig Router: %s ls Type: %d",orig_router,ls_type);
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s ls Type: %d",orig_router,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);
-				printf(" LS Id: %ld  Orig Time: %s\n",ls_id ,orig_time);
+
+				if ( nlsr->debugging )
+					printf(" LS Id: %ld  Orig Time: %s\n",ls_id ,orig_time);
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld  Orig Time: %s\n",ls_id ,orig_time);
+
+				
 				int is_new_name_lsa=check_is_new_name_lsa(orig_router,lst,lsid,orig_time);
 				if ( is_new_name_lsa == 1 )
 				{
-					printf("New NAME LSA.....\n");
+					if ( nlsr->debugging )
+						printf("New NAME LSA.....\n");
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"New NAME LSA.....\n");
+					
 					send_interest_for_name_lsa(nbr,orig_router,lst,lsid);	
 				}
 				else 
 				{
-					printf("Name LSA already exists in LSDB\n");
+					if ( nlsr->debugging )
+						printf("Name LSA already exists in LSDB\n");
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA already exists in LSDB\n");
+					
 				}
 			}
 			else
 			{
 				orig_time=strtok_r(NULL,sep,&rem);
-				printf(" Orig Time: %s\n",orig_time);
+
+				if ( nlsr->debugging )
+					printf(" Orig Time: %s\n",orig_time);
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__," Orig Time: %s\n",orig_time);
+				
+
 				int is_new_adj_lsa=check_is_new_adj_lsa(orig_router,lst,orig_time);
 				if ( is_new_adj_lsa == 1 )
 				{
-					printf("New ADJ LSA.....\n");
+					if ( nlsr->debugging )
+						printf("New Adj LSA.....\n");
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Adj LSA.....\n");
 					send_interest_for_adj_lsa(nbr,orig_router,lst);
 				}
 				else
 				{
-					printf("ADJ LSA already exists in LSDB\n");
+					if ( nlsr->debugging )
+						printf("Adj LSA already exists in LSDB\n");
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA already exists in LSDB\n");
 				}
 			}
 
@@ -880,9 +911,17 @@
 		memset(lsdb_version,0,20);
 		get_lsdb_version(lsdb_version,selfp,info);
 
-		printf("Old LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));		
+		if ( nlsr->debugging )
+			printf("Old LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
+
 		update_adjacent_lsdb_version_to_adl(nbr,lsdb_version);
-		printf("New LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
+		
+		if ( nlsr->debugging )
+			printf("New LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"New LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
 
 		update_lsdb_interest_timed_out_zero_to_adl(nbr);
 
@@ -901,7 +940,11 @@
 	}
 	else 
 	{
-		printf("NACK Content Received\n");
+		
+		if ( nlsr->debugging )
+			printf("NACK Content Received\n");
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"NACK Content Received\n");
 		struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
 		get_nbr(nbr,selfp,info);
 		update_lsdb_interest_timed_out_zero_to_adl(nbr);
@@ -913,7 +956,12 @@
 void 
 process_incoming_content_lsa(struct ccn_closure *selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_content_lsa called \n");
+	
+
+	if ( nlsr->debugging )
+		printf("process_incoming_content_lsa called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");	
 
 	char *sep="|";
 	char *rem;
@@ -942,9 +990,10 @@
 	
 
 	
-	
-
-	printf("LSA Data\n");
+	if ( nlsr->debugging )
+		printf("LSA Data \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");	
 
 	if( strlen((char *) ptr ) > 0 )
 	{
@@ -953,13 +1002,17 @@
 		orl=strtok_r(NULL,sep,&rem);
 		orig_router_length=atoi(orl);
 
-		printf("	Orig Router Name  : %s\n",orig_router);
-		printf("	Orig Router Length: %d\n",orig_router_length);
+		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);
 
-		printf("	LS Type  : %d\n",ls_type);
+		if ( nlsr->debugging )
+			printf("	LS Type  : %d\n",ls_type);
 
 		if ( ls_type == LS_TYPE_NAME )
 		{
@@ -971,11 +1024,14 @@
 			np=strtok_r(NULL,sep,&rem);
 			np_length=strtok_r(NULL,sep,&rem);
 			name_length=atoi(np_length);
-			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);
+			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);
 
@@ -987,9 +1043,11 @@
 			no_link=atoi(num_link);
 			data=rem;
 
-			printf("	No Link  : %d\n",no_link);
-			printf("	Data  : %s\n",data);
-
+			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);
 		}
 	}
@@ -999,7 +1057,13 @@
 void
 process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_timed_out_interest called \n");
+	
+
+	if ( nlsr->debugging )
+		printf("process_incoming_timed_out_interest called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest called \n");
+
 	int res,i;
 	int nlsr_position=0;
 	int name_comps=(int)info->interest_comps->n;
@@ -1031,27 +1095,38 @@
 void
 process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_timed_out_interest_info called \n");
+	
+	if ( nlsr->debugging )
+		printf("process_incoming_timed_out_interest_info called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_info called \n");
 
 	struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
 	get_nbr(nbr,selfp,info);
 
-	printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	if ( nlsr->debugging )
+		printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	
 
 
 	update_adjacent_timed_out_to_adl(nbr,1);
 	print_adjacent_from_adl();	
 	int timed_out=get_timed_out_number(nbr);
 
+	if ( nlsr->debugging )
+		printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
+
 
 	if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables 
 	{
-		printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
 		send_info_interest_to_neighbor(nbr);
 	}
 	else
-	{
-		printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);		
+	{		
 		update_adjacent_status_to_adl(nbr,NBR_DOWN);
 		if(!nlsr->is_build_adj_lsa_sheduled)
 		{
@@ -1068,18 +1143,30 @@
 void
 process_incoming_timed_out_interest_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_timed_out_interest_lsdb called \n");
+	if ( nlsr->debugging )
+		printf("process_incoming_timed_out_interest_lsdb called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_lsdb called \n");
 
 	struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
 	get_nbr(nbr,selfp,info);
 
-	printf("LSDB Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	if ( nlsr->debugging )
+		printf("LSDB Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSDB Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	
 
 	update_lsdb_interest_timed_out_to_adl(nbr,1);
 
 	int interst_timed_out_num=get_lsdb_interest_timed_out_number(nbr);
 
-	printf("Interest Timed out number : %d Interest Retry: %d \n",interst_timed_out_num,nlsr->interest_retry);
+	if ( nlsr->debugging )
+		printf("Interest Timed out number : %d Interest Retry: %d \n",interst_timed_out_num,nlsr->interest_retry);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed out number : %d Interest Retry: %d \n",interst_timed_out_num,nlsr->interest_retry);
+
+	
 
 	if( interst_timed_out_num >= nlsr->interest_retry )
 	{
@@ -1097,7 +1184,10 @@
 void
 process_incoming_timed_out_interest_lsa(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_timed_out_interest_lsa called \n");
+	if ( nlsr->debugging )
+		printf("process_incoming_timed_out_interest_lsa called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_lsa called \n");
 	
 }
 
@@ -1111,8 +1201,15 @@
 
          nlsr_lock();
 
-	printf("send_info_interest called \n");
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("send_info_interest called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
+
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 
 	int adl_element,i;
 	struct ndn_neighbor *nbr;
@@ -1141,7 +1238,12 @@
 void 
 send_info_interest_to_neighbor(struct name_prefix *nbr)
 {
-	printf("send_info_interest_to_neighbor called \n");
+
+	if ( nlsr->debugging )
+		printf("send_info_interest_to_neighbor called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor called \n");
+
 
 	int res;
 	char info_str[5];
@@ -1186,12 +1288,20 @@
 		ccn_charbuf_append_closer(templ); /* </Interest> */
 		/* Adding InterestLifeTime and InterestScope filter done */
 	
-		printf("Sending info interest on name prefix : %s \n",int_name);
+		if ( nlsr->debugging )
+			printf("Sending info interest on name prefix : %s \n",int_name);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on name prefix : %s \n",int_name);
 
 		res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
 
 		if ( res >= 0 )
-			printf("Info interest sending Successfull .... \n");	
+		{
+			if ( nlsr->debugging )
+				printf("Info interest sending Successfull .... \n");
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info interest sending Successfull .... \n");
+		}	
 		ccn_charbuf_destroy(&templ);
 	}
 	ccn_charbuf_destroy(&name);
@@ -1203,7 +1313,10 @@
 int 
 send_lsdb_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
 {
-	printf("send_lsdb_interest called \n");	
+	if ( nlsr->debugging )
+		printf("send_lsdb_interest called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_lsdb_interest called \n");	
 
 	if(flags == CCN_SCHEDULE_CANCEL)
 	{
@@ -1229,8 +1342,12 @@
 		{	
 			if(nbr->is_lsdb_send_interest_scheduled == 0)
 			{
-				long int time_diff=get_nbr_time_diff_lsdb_req(nbr->neighbor->name);	
-				printf("Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);		
+				long int time_diff=get_nbr_time_diff_lsdb_req(nbr->neighbor->name);
+				if ( nlsr->debugging )
+					printf("Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__,"Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);	
+						
 
 				if( time_diff >= ( get_lsdb_synch_interval(nbr->neighbor->name) + get_nbr_random_time_component(nbr->neighbor->name) ) )
 				{
@@ -1253,14 +1370,21 @@
 void 
 send_lsdb_interest_to_nbr(struct name_prefix *nbr)
 {
-	printf("send_lsdb_interest_to_nbr called \n");	
+	if ( nlsr->debugging )
+		printf("send_lsdb_interest_to_nbr called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_lsdb_interest_to_nbr called \n");
 
 	char *last_lsdb_version=get_nbr_lsdb_version(nbr->name);
 
 	if(last_lsdb_version !=NULL)
 	{
-		printf("Last LSDB Version: %s \n",last_lsdb_version);
+		
 
+		if ( nlsr->debugging )
+			printf("Last LSDB Version: %s \n",last_lsdb_version);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Last LSDB Version: %s \n",last_lsdb_version);
 
 		struct ccn_charbuf *name;
 		int res;
@@ -1272,7 +1396,11 @@
 		memset(&lsdb_str,0,5);
 		sprintf(lsdb_str,"lsdb");		
 		//make and send interest with exclusion filter as last_lsdb_version
-		printf("Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);	
+		if ( nlsr->debugging )
+			printf("Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);
+			
 		name=ccn_charbuf_create();
 		res=ccn_name_from_uri(name,nbr->name);
 
@@ -1315,7 +1443,10 @@
 
 			if ( res >= 0 )
 			{
-				printf("Interest sending Successfull .... \n");	
+				if ( nlsr->debugging )
+					printf("Interest sending Successfull .... \n");
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest sending Successfull .... \n");	
 				update_adjacent_last_lsdb_requested_to_adl(nbr->name,get_current_time_sec());
 
 			}
@@ -1330,7 +1461,10 @@
 void 
 send_interest_for_name_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type, char *ls_id)
 {
-	printf("send_interest_for_name_lsa called\n");
+	if ( nlsr->debugging )
+		printf("send_interest_for_name_lsa called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_interest_for_name_lsa called\n");
 
 	int res;
 	char lsa_str[5];
@@ -1381,13 +1515,22 @@
 	ccn_charbuf_append_closer(templ); /* </Interest> */
 	/* Adding InterestLifeTime and InterestScope filter done */
 
-	printf("Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
+	if ( nlsr->debugging )
+		printf("Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
+
 
 	res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
 
 	if ( res >= 0 )
-		printf("NAME LSA interest sending Successfull .... \n");	
+	{
+		if ( nlsr->debugging )
+			printf("NAME LSA interest sending Successfull .... \n");
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"NAME LSA interest sending Successfull .... \n");
 	
+	}	
 	ccn_charbuf_destroy(&templ);
 	ccn_charbuf_destroy(&name);
 	free(int_name);
@@ -1398,7 +1541,10 @@
 void 
 send_interest_for_adj_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type)
 {
-	printf("send_interest_for_adj_lsa called\n");
+	if ( nlsr->debugging )
+		printf("send_interest_for_name_lsa called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_interest_for_name_lsa called\n");
 
 	int res;
 	char lsa_str[5];
@@ -1446,13 +1592,19 @@
 	ccn_charbuf_append_closer(templ); /* </Interest> */
 	/* Adding InterestLifeTime and InterestScope filter done */
 
-	printf("Sending ADJ LSA interest on name prefix : %s\n",int_name);
+	if ( nlsr->debugging )
+		printf("Sending ADJ LSA interest on name prefix : %s\n",int_name);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending ADJ LSA interest on name prefix : %s\n",int_name);
 
 	res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
 
 	if ( res >= 0 )
 	{
-		printf("ADJ LSA interest sending Successfull .... \n");	
+		if ( nlsr->debugging )
+			printf("ADJ LSA interest sending Successfull .... \n");	
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"ADJ LSA interest sending Successfull .... \n");	
 	}
 	
 	ccn_charbuf_destroy(&templ);
diff --git a/nlsr_npl.c b/nlsr_npl.c
index 62101d4..a2e2271 100644
--- a/nlsr_npl.c
+++ b/nlsr_npl.c
@@ -19,12 +19,14 @@
 
 #include "nlsr.h"
 #include "nlsr_npl.h"
+#include "utility.h"
 
 
 void 
 add_name_to_npl(struct name_prefix *np)
 {
-	struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
+	struct name_prefix_list_entry *npe=(struct name_prefix_list_entry *)malloc(sizeof(struct name_prefix_list_entry));
+	//struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
 
 	struct hashtb_enumerator ee;
     	struct hashtb_enumerator *e = &ee; 	
@@ -35,24 +37,92 @@
 
 	if(res == HT_NEW_ENTRY)
 	{   
-
-		hnp = e->data;
-		hnp->length=np->length;
-		hnp->name=(char *)malloc(np->length); //free
-		memcpy(hnp->name,np->name,np->length);
+		npe=e->data;
+		npe->np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+		npe->np->length=np->length;
+		npe->np->name=(char *)malloc(np->length);
+		memcpy(npe->np->name,np->name,np->length);
+		npe->name_lsa_id=0;
+		//hnp = e->data;
+		//hnp->length=np->length;
+		//hnp->name=(char *)malloc(np->length); //free
+		//memcpy(hnp->name,np->name,np->length);
 	}
     	
 	hashtb_end(e);
 
 }
 
+int  
+does_name_exist_in_npl(struct name_prefix *np)
+{
+	int ret=0;
+
+	//struct name_prefix_entry *npe;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->npl, e);
+    	res = hashtb_seek(e, np->name, np->length, 0);
+
+	if(res == HT_NEW_ENTRY)
+	{   
+		hashtb_delete(e);
+		ret=0;
+	}
+	else
+	{
+		ret=1;
+    	}
+	hashtb_end(e);
+
+	return ret;
+
+}
+
+
+long int  
+get_lsa_id_from_npl(struct name_prefix *np)
+{
+	int ret=0;
+
+	struct name_prefix_list_entry *npe;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->npl, e);
+    	res = hashtb_seek(e, np->name, np->length, 0);
+
+	if(res == HT_NEW_ENTRY)
+	{   
+		hashtb_delete(e);
+		ret=0;
+	}
+	else
+	{
+		npe=e->data;
+		ret=npe->name_lsa_id;
+    	}
+	hashtb_end(e);
+
+	return ret;
+
+}
 
 void
 print_name_prefix_from_npl(void)
 {
-	printf("print_name_prefix_from_npl called \n");	
+	if ( nlsr->debugging )
+		printf("print_name_prefix_from_npl called \n");	
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_name_prefix_from_npl called\n");
 	int i, npl_element;
-	struct name_prefix *np;
+	//struct name_prefix *np;
+	struct name_prefix_list_entry *npe;
 
 	struct hashtb_enumerator ee;
     	struct hashtb_enumerator *e = &ee;
@@ -62,13 +132,43 @@
 
 	for(i=0;i<npl_element;i++)
 	{
-		np=e->data;
-		printf("Name Prefix: %s and Length: %d \n",np->name,np->length);	
+		npe=e->data;
+		if ( nlsr->debugging )
+			printf("Name Prefix: %s and Length: %d and LSA Id: %ld\n",npe->np->name,npe->np->length,npe->name_lsa_id);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name Prefix: %s and Length: %d \n",npe->np->name,npe->np->length);	
 		hashtb_next(e);		
 	}
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
+}
+
+void 
+update_nlsa_id_for_name_in_npl(struct name_prefix *np, long int nlsa_id)
+{
+	struct name_prefix_list_entry *npe;
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->npl, e);
+    	res = hashtb_seek(e, np->name, np->length, 0);
+
+	if(res == HT_OLD_ENTRY)
+	{   
+		npe=e->data;
+		npe->name_lsa_id=nlsa_id;
+	}
+	else
+	{
+		hashtb_delete(e);	
+	}
+    	
+	hashtb_end(e);
 }
 
diff --git a/nlsr_npl.h b/nlsr_npl.h
index 3c22f8e..4fce119 100644
--- a/nlsr_npl.h
+++ b/nlsr_npl.h
@@ -1,7 +1,15 @@
 #ifndef _NLSR_NPL_H_
 #define _NLSR_NPL_H_
 
+struct name_prefix_list_entry
+{
+	struct name_prefix *np;
+	long int name_lsa_id;
+};
+
 void add_name_to_npl(struct name_prefix *np);
 void print_name_prefix_from_npl(void);
-
+int does_name_exist_in_npl(struct name_prefix *np);
+void update_nlsa_id_for_name_in_npl(struct name_prefix *np, long int nlsa_id);
+long int get_lsa_id_from_npl(struct name_prefix *np);
 #endif
diff --git a/nlsr_npt.c b/nlsr_npt.c
index a83e2fe..e3c8483 100644
--- a/nlsr_npt.c
+++ b/nlsr_npt.c
@@ -25,6 +25,7 @@
 #include "nlsr_fib.h"
 #include "nlsr_route.h"
 #include "nlsr_adl.h"
+#include "utility.h"
 
 int
 add_npt_entry(char *orig_router, char *name_prefix, int num_face, int *faces, int *route_costs)
@@ -242,14 +243,22 @@
 
 				if ( is_neighbor(orig_router) == 0 )
 				{
-					printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
+					if ( nlsr->debugging )
+						printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);	
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
+					//printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
 					add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);	
 				}
 				else 
 				{
 					if ( j == last_face && is_neighbor(nle->name)==0)
 					{
-						printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
+						if ( nlsr->debugging )
+							printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);	
+						if ( nlsr->detailed_logging )
+							writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
+						//printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
 						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
 					}
 				}
@@ -347,14 +356,20 @@
 
 				if ( is_neighbor(orig_router) == 0 )
 				{
-					printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
+					if ( nlsr->debugging )
+						printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);	
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
 					add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);	
 				}
 				else 
 				{
 					if ( j == last_face && is_neighbor(nle->name)==0)
 					{
-						printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
+						if ( nlsr->debugging )
+							printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);	
+						if ( nlsr->detailed_logging )
+							writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
 						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
 					}
 				}
@@ -364,17 +379,10 @@
 			
 			
 
-			hashtb_delete(enle);
+			//hashtb_delete(enle); has to delete later
 		}
 
 		hashtb_end(enle);
-
-		/*
-		if ( hashtb_n(ne->name_list) == 0 )
-		{
-			hashtb_delete(e);
-		}
-		*/
 	}
 	
 	hashtb_end(e);
@@ -385,8 +393,17 @@
 void 
 print_npt(void)
 {
-	printf("\n");
-	printf("print_npt called\n\n");
+
+	if ( nlsr->debugging )
+	{
+		printf("\n");
+		printf("print_npt called\n");
+	}
+	if ( nlsr->detailed_logging )
+	{
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n");
+	}
 	int i, npt_element;
 	
 	struct npt_entry *ne;
@@ -399,10 +416,21 @@
 
 	for(i=0;i<npt_element;i++)
 	{
-		printf("\n");
-		printf("----------NPT ENTRY %d------------------\n",i+1);
+		if ( nlsr->debugging )
+		{
+			printf("\n");
+			printf("----------NPT ENTRY %d------------------\n",i+1);
+		}
+		if ( nlsr->detailed_logging )
+		{
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1);
+		}
 		ne=e->data;
-		printf(" Origination Router: %s \n",ne->orig_router);
+		if ( nlsr->debugging )
+			printf(" Origination Router: %s \n",ne->orig_router);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router);
 		//ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face);
 		
 		int j, nl_element,face_list_element;
@@ -416,7 +444,10 @@
 		for (j=0;j<nl_element;j++)
 		{
 			nle=enle->data;
-			printf(" Name Prefix: %s \n",nle->name);
+			if ( nlsr->debugging )
+				printf(" Name Prefix: %s \n",nle->name);
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
 			hashtb_next(enle);
 		}
 		hashtb_end(enle);
@@ -430,14 +461,21 @@
 		face_list_element=hashtb_n(ne->face_list);
 		if ( face_list_element <= 0 )
 		{
-			printf(" 	Face: No Face \n");
+			if ( nlsr->debugging )
+				printf(" 	Face: No Face \n");
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__," 	Face: No Face \n");
+			
 		}
 		else
 		{
 			for(j=0;j<face_list_element;j++)
 			{
 				fle=ef->data;
-				printf(" 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
+				if ( nlsr->debugging )
+					printf(" 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__," 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
 				hashtb_next(ef);	
 			}
 		}
@@ -449,7 +487,10 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
 
 void
@@ -485,9 +526,7 @@
 				for (j=0;j<nl_element;j++)
 				{
 					nle=enle->data;
-					
 					delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);		
-	
 					hashtb_next(enle);
 				}
 				hashtb_end(enle);				
@@ -510,9 +549,18 @@
 void
 add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
 {
-	printf("add_face_to_npt_by_face_id called\n");
+	if ( nlsr->debugging )
+	{
+		printf("add_face_to_npt_by_face_id called\n");
+		printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
+	}
+	if ( nlsr->detailed_logging )
+	{
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
+	}
 
-	printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
+	
 	int res,res1;
 	struct npt_entry *ne;
 
@@ -524,7 +572,11 @@
 
 	if ( res == HT_OLD_ENTRY )
 	{
-		printf("Dest Router Found \n");
+		if ( nlsr->debugging )
+			printf("Dest Router Found \n");
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
+	
 		ne=e->data;
 		
 		struct hashtb_enumerator eef;
@@ -535,7 +587,10 @@
 
 		if ( res1 == HT_OLD_ENTRY )
 		{
-			printf("Face Found \n");
+			if ( nlsr->debugging )
+				printf("Face Found \n");
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
 			struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
 			fle=ef->data;
 			fle->next_hop_face=face_id;
@@ -543,7 +598,10 @@
 		}
 		else if ( res1 == HT_NEW_ENTRY )
 		{
-			printf("Face Not Found \n");
+			if ( nlsr->debugging )
+				printf("Face Not Found \n");
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n");
 			struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
 			fle=ef->data;
 			fle->next_hop_face=face_id;
@@ -564,7 +622,10 @@
 void
 add_new_fib_entries_to_npt(void)
 {
-	printf("add_new_fib_entries_to_npt called\n");
+	if ( nlsr->debugging )
+		printf("add_new_fib_entries_to_npt called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
 	int i,j, rt_element,face_list_element;
 	
 	struct routing_table_entry *rte;
@@ -588,7 +649,10 @@
 		face_list_element=hashtb_n(rte->face_list);
 		if ( face_list_element <= 0 )
 		{
-			printf(" 	Face: No Face \n");
+			if ( nlsr->debugging )
+				printf(" 	Face: No Face \n");
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__," 	Face: No Face \n");
 		}
 		else
 		{
@@ -612,7 +676,10 @@
 void
 delete_face_from_npt_by_face_id(char *dest_router, int face_id)
 {
-	printf("delete_face_from_npt_by_face_id called\n");
+	if ( nlsr->debugging )
+		printf("delete_face_from_npt_by_face_id\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
 
 	int res,res1;
 	struct npt_entry *ne;
@@ -661,10 +728,17 @@
 	
 	nlsr_lock();
 
-	printf("delete_old_face_from_npt called\n");
+	if ( nlsr->debugging )
+		printf("delete_old_face_from_npt\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
+	
 	if ( ev->evdata != NULL )
-	{	
-		printf("Event Data: %s \n",(char *)ev->evdata);	
+	{		
+		if ( nlsr->debugging )
+			printf("Event Data: %s \n",(char *)ev->evdata);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
 		char *sep="|";
 		char *rem;
 		char *orig_router;
@@ -678,7 +752,11 @@
 		orig_router=strtok_r(face_data,sep,&rem);
 		faceid=strtok_r(NULL,sep,&rem);
 		face_id=atoi(faceid);
-		printf("Orig Router: %s Face: %d \n",orig_router,face_id);
+
+		if ( nlsr->debugging )
+			printf("Orig Router: %s Face: %d \n",orig_router,face_id);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
 
 		delete_face_from_npt_by_face_id(orig_router,face_id);		
 	}
@@ -692,7 +770,11 @@
 clean_old_fib_entries_from_npt(void)
 {
 	
-	printf("clean_old_fib_entries_from_npt called\n\n");
+	
+	if ( nlsr->debugging )
+		printf("clean_old_fib_entries_from_npt called\n\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
 	int i, npt_element;
 	
 	struct npt_entry *ne;
@@ -717,7 +799,11 @@
 		face_list_element=hashtb_n(ne->face_list);
 		if ( face_list_element <= 0 )
 		{
-			printf(" 	Face: No Face \n");
+			if ( nlsr->debugging )
+				printf(" 	Face: No Face \n");
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__," 	Face: No Face \n");
+			
 		}
 		else
 		{
@@ -739,12 +825,15 @@
 						nle=enle->data;
 
 						//delete all the fib entries here
-						printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
 
-						
+						//printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
+
 						if( is_neighbor(nle->name) == 0 )
 						{
-							printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
+							if ( nlsr->debugging )
+								printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
+							if ( nlsr->detailed_logging )
+								writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
 							add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
 						}						
 		
@@ -941,14 +1030,20 @@
 			{
 				if ( is_neighbor(orig_router) == 0 )
 				{
-					printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
+					if ( nlsr->debugging )
+						printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
 					add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);	
 				}
 				else 
 				{
 					if ( j == last_face && is_neighbor(nle->name)==0)
 					{
-						printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
+						if ( nlsr->debugging )
+							printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
+						if ( nlsr->detailed_logging )
+							writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
 						add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
 					}
 				}
@@ -990,5 +1085,8 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
diff --git a/nlsr_route.c b/nlsr_route.c
index a5956d8..8537b2e 100644
--- a/nlsr_route.c
+++ b/nlsr_route.c
@@ -25,6 +25,7 @@
 #include "nlsr_npt.h"
 #include "nlsr_adl.h"
 #include "nlsr_fib.h"
+#include "utility.h"
 
 int
 route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
@@ -37,7 +38,10 @@
 
 	nlsr_lock();
 
-	printf("route_calculate called\n");
+	if ( nlsr->debugging )
+		printf("route_calculate called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"route_calculate called\n");
 
 	if( ! nlsr->is_build_adj_lsa_sheduled )
 	{
@@ -67,7 +71,8 @@
 			adj_matrix[i] = malloc(map_element * sizeof(int));
 		}
 		make_adj_matrix(adj_matrix,map_element);
-		print_adj_matrix(adj_matrix,map_element);
+		if ( nlsr->debugging )
+			print_adj_matrix(adj_matrix,map_element);
 
 		long int source=get_mapping_no(nlsr->router_name);
 		long int *parent=(long int *)malloc(map_element * sizeof(long int));
@@ -198,8 +203,18 @@
 			me=e->data;
 			if(me->mapping != source)
 			{
-				print_path(parent,(long int)me->mapping);
-				printf("\n");
+				
+				if ( nlsr->debugging )
+				{
+					print_path(parent,(long int)me->mapping);
+					printf("\n");
+				}
+				if ( nlsr->detailed_logging )
+				{
+					print_path(parent,(long int)me->mapping);
+					writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
+				}
+				
 			}
 			hashtb_next(e);		
 		}
@@ -225,8 +240,10 @@
 		me=e->data;
 		if(me->mapping != source)
 		{
-			
-			printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
+			if ( nlsr->debugging )
+				printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
 		}
 		hashtb_next(e);		
 	}
@@ -260,7 +277,6 @@
 {
 	if (parent[dest] != EMPTY_PARENT )
 		print_path(parent,parent[dest]);
-
 	printf(" %ld",dest);
 }
 
@@ -316,7 +332,10 @@
 	for(i=0;i<map_element;i++)
 	{
 		me=e->data;
-		printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
+		if ( nlsr->debugging )
+			printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
 		hashtb_next(e);		
 	}
 
@@ -550,7 +569,10 @@
 	for(i=0;i<map_element;i++)
 	{
 		me=e->data;
-		printf("Mapping Number: %d Router: %s  \n",me->mapping,me->router);
+		if ( nlsr->debugging )
+			printf("Mapping Number: %d Router: %s  \n",me->mapping,me->router);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s  \n",me->mapping,me->router);
 		hashtb_next(e);		
 	}
 
@@ -911,8 +933,10 @@
 void 
 print_routing_table(void)
 {
-	printf("\n");
-	printf("print_routing_table called\n");
+	if ( nlsr->debugging )
+		printf("print_routing_table called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
 	int i,j, rt_element,face_list_element;
 	
 	struct routing_table_entry *rte;
@@ -925,9 +949,19 @@
 
 	for(i=0;i<rt_element;i++)
 	{
-		printf("----------Routing Table Entry %d------------------\n",i+1);
+		if ( nlsr->debugging )
+			printf("----------Routing Table Entry %d------------------\n",i+1);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
+		
 		rte=e->data;
-		printf(" Destination Router: %s \n",rte->dest_router);
+
+		if ( nlsr->debugging )
+			printf(" Destination Router: %s \n",rte->dest_router);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
+
+		
 		//rte->next_hop_face == NO_NEXT_HOP ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", rte->next_hop_face);
 
 		struct face_list_entry *fle;
@@ -939,14 +973,20 @@
 		face_list_element=hashtb_n(rte->face_list);
 		if ( face_list_element <= 0 )
 		{
-			printf(" 	Face: No Face \n");
+			if ( nlsr->debugging )
+				printf(" 	Face: No Face \n");
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__," 	Face: No Face \n");
 		}
 		else
 		{
 			for(j=0;j<face_list_element;j++)
 			{
 				fle=ef->data;
-				printf(" 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
+				if ( nlsr->debugging )
+					printf(" 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__," 	Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
 				hashtb_next(ef);	
 			}
 		}
@@ -957,21 +997,32 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
 
 
 int
 delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
 {
-	printf("delete_empty_rte called\n");
-	printf("Router: %s \n",(char *)ev->evdata);
+	if ( nlsr->debugging )
+	{
+		printf("delete_empty_rte called\n");
+		printf("Router: %s \n",(char *)ev->evdata);
+	}
+	if ( nlsr->detailed_logging )
+	{
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
+		//writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
+	}
+	
 	if(flags == CCN_SCHEDULE_CANCEL)
 	{
  	 	return -1;
 	}
-
-	//struct routing_table_entry *rte;
 	int res;
 	struct hashtb_enumerator ee;
     	struct hashtb_enumerator *e = &ee;
@@ -981,16 +1032,6 @@
 	
 	if ( res == HT_OLD_ENTRY )
 	{
-		//rte=e->data;
-
-		/*		
-	
-		if ( (rte->next_hop_face != NO_FACE  || rte->next_hop_face != NO_NEXT_HOP) && is_neighbor(ev->evdata)==0 )
-		{
-			add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)ev->evdata, OP_UNREG, rte->next_hop_face);
-		}
-
-		*/
 		hashtb_delete(e);
 	}
 	else if ( res == HT_NEW_ENTRY )
@@ -1006,7 +1047,10 @@
 void 
 clear_old_routing_table(void)
 {
-	printf("clear_old_routing_table called\n");
+	if ( nlsr->debugging )
+		printf("clear_old_routing_table called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
 	int i,rt_element;
 	
 	struct routing_table_entry *rte;
@@ -1034,7 +1078,11 @@
 void 
 do_old_routing_table_updates(void)
 {
-	printf("do_old_routing_table_updates called\n");
+	if ( nlsr->debugging )
+		printf("do_old_routing_table_updates called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
+	
 	int i, rt_element;
 	int mapping_no;	
 
@@ -1069,7 +1117,10 @@
 void 
 update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
 {
-	printf("update_routing_table_with_new_route called\n");
+	if ( nlsr->debugging )
+		printf("update_routing_table_with_new_route called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
 	int i, map_element;
 	struct map_entry *me;
 
@@ -1093,7 +1144,10 @@
 				if ( next_hop_router_num == NO_NEXT_HOP )
 				{
 					//update_npt_with_new_route(orig_router,NO_FACE);
-					printf ("\nOrig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
+					if ( nlsr->debugging )
+						printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
 				}
 				else 
 				{
@@ -1102,7 +1156,11 @@
 					int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
 					//update_npt_with_new_route(orig_router,next_hop_face);
 					update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
-					printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
+					if ( nlsr->debugging )
+						printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
+					
 
 				}
 			}
diff --git a/nlsrc.c b/nlsrc.c
index cd3fc3d..44ce2ad 100755
--- a/nlsrc.c
+++ b/nlsrc.c
@@ -6,39 +6,96 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <getopt.h>
+
+struct option longopts[] =
+{
+    { "server_ip",    required_argument, NULL, 's'},
+    { "server_port",    required_argument, NULL, 'p'},
+    { 0 }
+};
+
+static int 
+usage(char *progname)
+{
+    printf("Usage: %s [OPTIONS...]\n\
+	NLSR Api client....\n\
+	nlsrc -s server_ip -p server_port add|del name|neighbor name_prefix [faceX] \n\
+	add/del, -- adding/deleting operation\n\
+	name/neighbor, -- Operation for name/neighbor\n\
+	name_name, -- Name prefix for name/neighbor\n\
+	faceX, -- Face Id for neighbor if third argument is neighbor\n", progname);
+
+    exit(1);
+}
 
 int main(int argc, char *argv[])
 {
 	int sockfd;
 	int len;
-	struct sockaddr_un address;
+	struct sockaddr_in address;
 	int result;
 	int byteSend;
+	char *server_address, *server_port;	
 
 	int command_len=0;
 	int i;
-	for(i=1;i<argc;i++)
+
+	if (argc < 8 )
+		usage(argv[0]);
+	if ( strcmp(argv[6],"neighbor") == 0 && argc <9 )
+		usage(argv[0]);
+
+	while ((result = getopt_long(argc, argv, "s:p:", longopts, 0)) != -1) 
+	{
+        	switch (result) 
+		{
+			case 's':
+				server_address = optarg;
+				break;
+			case 'p':
+				server_port = optarg;
+				break;
+		}
+    	}	
+
+
+	char recv_buffer[1024];
+	bzero(recv_buffer,1024);
+
+	for(i=5;i<argc;i++)
 		command_len+=(strlen(argv[i])+1);
 	char *command=malloc(command_len);
 	memset(command,command_len+1,0);
-	for(i=1;i<argc;i++)
+	for(i=5;i<argc;i++)
 	{
 		memcpy(command+strlen(command),argv[i],strlen(argv[i]));
 		if ( i < argc-1 )
 			memcpy(command+strlen(command)," ",1);	
 	}
 
-	sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
-	address.sun_family = AF_UNIX;
-	strcpy(address.sun_path, "/tmp/nlsr_api_server_socket");
+	sockfd = socket(AF_INET, SOCK_STREAM, 0);
+	//address.sun_family = AF_UNIX;
+	//strcpy(address.sun_path, "/tmp/nlsr_api_server_socket");
+	address.sin_family = AF_INET;
+	address.sin_addr.s_addr = inet_addr(server_address);
+	address.sin_port = atoi(server_port);
+
 	len = sizeof(address);
 	result = connect(sockfd, (struct sockaddr *)&address, len);
-	if(result == -1) {
+	if(result == -1) 
+	{
 		perror("oops nlsrc ");
 		exit(1);
 	}
-	printf("Data to send: %s \n",command);
+	printf("Command to send: %s \n",command);
 	byteSend=send(sockfd, command, strlen(command),0);
+	recv(sockfd, recv_buffer, 1024, 0);
+	printf("%s\n",recv_buffer);
+	free(command);
 	close(sockfd);
 	exit(0);
 }