API Addition 2nd Version
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..189e11f 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -1,19 +1,21 @@
-#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>
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
-#include <sys/types.h>
-#include <signal.h>
-
-
-
 
 #include <ccn/ccn.h>
 #include <ccn/uri.h>
@@ -595,6 +597,75 @@
 	return 0;
 }
 
+
+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_un 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);
+					close(fd);
+					FD_CLR(fd, &nlsr->readfds);
+					free(recv_buffer);
+				}
+			}
+		}
+	}
+
+	return 0;
+}
+
 void 
 nlsr_destroy( void )
 {
@@ -652,6 +723,31 @@
 }
 
 
+void
+init_api_server(int ccn_fd)
+{
+	int server_sockfd;
+	int server_len;
+	struct sockaddr_un server_address;
+	
+	unlink("/tmp/nlsr_api_server_socket");
+	server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+
+	int flags = fcntl(server_sockfd, F_GETFL, 0);
+	fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
+
+	server_address.sun_family = AF_UNIX;
+	strcpy(server_address.sun_path, "/tmp/nlsr_api_server_socket");
+	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)
 {
@@ -721,8 +817,6 @@
 	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;
 
 	return 0;
@@ -766,12 +860,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 +907,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 +929,7 @@
 		}
 
 	}
+	
 
 	return 0;
 }
diff --git a/nlsr.h b/nlsr.h
index f113d52..69e82dd 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -83,6 +83,9 @@
 	int debugging;
 
 	int semaphor;
+
+	int nlsr_api_server_sock_fd;
+	fd_set readfds;
 	
 };
 
diff --git a/nlsr_npl.c b/nlsr_npl.c
index 62101d4..c384133 100644
--- a/nlsr_npl.c
+++ b/nlsr_npl.c
@@ -19,6 +19,7 @@
 
 #include "nlsr.h"
 #include "nlsr_npl.h"
+#include "utility.h"
 
 
 void 
@@ -50,7 +51,10 @@
 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;
 
@@ -63,7 +67,10 @@
 	for(i=0;i<npl_element;i++)
 	{
 		np=e->data;
-		printf("Name Prefix: %s and Length: %d \n",np->name,np->length);	
+		if ( nlsr->debugging )
+			printf("Name Prefix: %s and Length: %d \n",np->name,np->length);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name Prefix: %s and Length: %d \n",np->name,np->length);	
 		hashtb_next(e);		
 	}
 
diff --git a/nlsr_npt.c b/nlsr_npt.c
index a83e2fe..9f690ff 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]);
 					}
 				}
@@ -385,8 +400,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 +423,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 +451,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 +468,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);	
 			}
 		}
@@ -510,9 +555,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 +578,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 +593,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 +604,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 +628,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 +655,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 +682,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 +734,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 +758,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 +776,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 +805,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 +831,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 +1036,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]);
 					}
 				}
diff --git a/nlsr_route.c b/nlsr_route.c
index a5956d8..0fbe8fd 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 )
 	{
@@ -225,8 +229,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);		
 	}
@@ -316,7 +322,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 +559,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 +923,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 +939,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 +963,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);	
 			}
 		}
@@ -964,14 +994,22 @@
 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 +1019,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 +1034,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 +1065,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 +1104,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 +1131,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 +1143,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..e980687 100755
--- a/nlsrc.c
+++ b/nlsrc.c
@@ -7,6 +7,20 @@
 #include <stdlib.h>
 #include <string.h>
 
+static int 
+usage(char *progname)
+{
+    printf("Usage: %s [OPTIONS...]\n\
+	NLSR Api client....\n\
+	nlsrc 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;
@@ -17,6 +31,12 @@
 
 	int command_len=0;
 	int i;
+
+	if (argc < 4 )
+		usage(argv[0]);
+	if ( strcmp(argv[2],"neighbor") == 0 && argc <5 )
+		usage(argv[0]);
+
 	for(i=1;i<argc;i++)
 		command_len+=(strlen(argv[i])+1);
 	char *command=malloc(command_len);
@@ -33,12 +53,14 @@
 	strcpy(address.sun_path, "/tmp/nlsr_api_server_socket");
 	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);
 	byteSend=send(sockfd, command, strlen(command),0);
+	free(command);
 	close(sockfd);
 	exit(0);
 }