1. adding sys log support
2. added dynamic enabled/disable file/sys log
3. added circular file log

Change-Id: I9f7de1779239d0617b6b171e3a4e40d439711185
diff --git a/ipacm/inc/IPACM_Conntrack_NATApp.h b/ipacm/inc/IPACM_Conntrack_NATApp.h
index 682316f..777a23f 100644
--- a/ipacm/inc/IPACM_Conntrack_NATApp.h
+++ b/ipacm/inc/IPACM_Conntrack_NATApp.h
@@ -67,7 +67,7 @@
                              int n =0; \
                              n = snprintf(log_buf, sizeof(log_buf), "%s:%d %s() %s", __FILE__,  __LINE__, __FUNCTION__, "Error:");\
                              snprintf((log_buf+n), (sizeof(log_buf)-n)-1, "Invalid table handle\n");\
-                             logmessage(log_buf);\
+                             logmessage(log_buf, LOG_ERR);\
 				  		             }
 
 class NatApp
@@ -103,6 +103,7 @@
 	static NatApp* GetInstance();
 
 	int AddTable(uint32_t);
+	uint32_t GetTableHdl(uint32_t);
 	int DeleteTable(uint32_t);
 
 	int AddEntry(const nat_table_entry *);
diff --git a/ipacm/inc/IPACM_Log.h b/ipacm/inc/IPACM_Log.h
index 785b654..f75e73c 100644
--- a/ipacm/inc/IPACM_Log.h
+++ b/ipacm/inc/IPACM_Log.h
@@ -48,6 +48,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <syslog.h>
 
 #define LOG_SIZE 200
 
@@ -58,7 +59,7 @@
                              int n =0; \
                              n = snprintf(log_buf, sizeof(log_buf), "%s:%d %s() ", __FILE__,  __LINE__, __FUNCTION__);\
                              snprintf((log_buf+n), (sizeof(log_buf)-n-1), fmt, ##__VA_ARGS__);\
-                             logmessage(log_buf);\
+                             logmessage(log_buf, LOG_DEBUG);\
 				  		             }
 
 
@@ -66,10 +67,10 @@
                              int n =0; \
                              n = snprintf(log_buf, sizeof(log_buf), "%s:%d %s() %s", __FILE__,  __LINE__, __FUNCTION__, "Error:");\
                              snprintf((log_buf+n), (sizeof(log_buf)-n-1), fmt, ##__VA_ARGS__);\
-                             logmessage(log_buf);\
+                             logmessage(log_buf, LOG_ERR);\
 				  		             }
 
-extern void logmessage(char *msg);
+extern void logmessage(char *msg, int log_level);
 extern char log_buf[LOG_SIZE];
 
 #ifdef __cplusplus
diff --git a/ipacm/src/IPACM_Conntrack_NATApp.cpp b/ipacm/src/IPACM_Conntrack_NATApp.cpp
index 9c14004..28de036 100644
--- a/ipacm/src/IPACM_Conntrack_NATApp.cpp
+++ b/ipacm/src/IPACM_Conntrack_NATApp.cpp
@@ -608,5 +608,13 @@
 	return -1;
 }
 
+uint32_t NatApp::GetTableHdl(uint32_t in_ip_addr)
+{
+	if(in_ip_addr == pub_ip_addr)
+	{
+		return nat_table_hdl;
+	}
 
+	return -1;
+}
 
diff --git a/ipacm/src/IPACM_Log.cpp b/ipacm/src/IPACM_Log.cpp
index 3333358..74ae2aa 100644
--- a/ipacm/src/IPACM_Log.cpp
+++ b/ipacm/src/IPACM_Log.cpp
@@ -39,30 +39,91 @@
 */
 #include "IPACM_Log.h"
 #include <stdlib.h>
+#include <unistd.h>
 
+/* write logging to a local file */
+#define LOG_USE_FILE "/etc/IPACM_LOG_F"
 #define FILE_NAME "/usr/ipacm_log.txt"
 
-static FILE *fp = NULL;
+/* To use syslog for logging and
+	 use logread cmd to read */
+#define LOG_USE_SYS  "/etc/IPACM_LOG_S"
+
+/* Maximum log file size. 1MB(1048576)  */
+#define MAX_LOG_FILE_SIZE (1048576ul/4)
+
 char log_buf[LOG_SIZE];
 
-void logmessage(char *msg)
+void logmessage(char *msg, int log_level)
 {
+#ifdef DEBUG
+	 static FILE *fp = NULL;
+	 static bool is_sys_log_open = false;
+	 static bool dft_syslog = false;
+	 int log_sys = 0, log_file = 0;
+
 	 printf("%s\n", msg);
-#if 0
-	 if(fp == NULL)
+
+	 if(dft_syslog == false)
 	 {
-			fp = fopen(FILE_NAME, "wb+");
-			if(fp  == NULL)
-			{
-				 printf("unable to open file\n");
-				 return;
-			}
+		 /* By Default sys log is open */
+		 fp = fopen(LOG_USE_SYS, "w+");
+		 if(fp != NULL)
+		 {
+			 dft_syslog = true;
+			 fclose(fp);
+			 fp = NULL;
+		 }
 	 }
 
-	 fprintf(fp, msg);
-	 fflush(fp);
+	 if(access(LOG_USE_SYS, F_OK) != -1 )
+	 {
+		 log_sys = 1;
+	 }
+	 else if(access(LOG_USE_FILE, F_OK) != -1)
+	 {
+		 log_file = 1;
+	 }
+	 else
+	 {
+		 log_sys = 0;
+		 log_file = 0;
+	 }
+
+	 if(log_sys)
+	 {
+		 if(is_sys_log_open == false)
+		 {
+			 setlogmask(LOG_UPTO(LOG_DEBUG));
+			 openlog("IPACM", LOG_PID, LOG_LOCAL0);
+
+			 is_sys_log_open = true;
+		 }
+		 syslog(log_level, msg);
+	 }
+	 else if(log_file)
+	 {
+		 if(fp == NULL)
+		 {
+				fp = fopen(FILE_NAME, "w+");
+				if(fp  == NULL)
+				{
+					 printf("unable to open file\n");
+					 return;
+				}
+		 }
+		 
+		 if(ftell(fp) > MAX_LOG_FILE_SIZE)
+		 {
+			 rewind(fp);
+			 fprintf(fp, "====Rewinding=====\n");
+		 }
+
+		 fprintf(fp, msg);
+		 fflush(fp);
+	}
 #endif
-	 return;
+	return;
 }
 
 
diff --git a/ipacm/src/Makefile.am b/ipacm/src/Makefile.am
index c43343a..57e3386 100644
--- a/ipacm/src/Makefile.am
+++ b/ipacm/src/Makefile.am
@@ -1,8 +1,8 @@
-EXTRA_CPPFLAGS	= -DDEBUG
 AM_CPPFLAGS = -I./../inc \
 	      -I$(top_srcdir)/ipanat/inc \
 	      ${LIBXML_CFLAGS}
 AM_CPPFLAGS += -Wall -Wundef -Wno-trigraphs
+AM_CPPFLAGS	+= -DDEBUG
 
 ipacm_SOURCES =	IPACM_Main.cpp \
 		IPACM_Conntrack_NATApp.cpp\
diff --git a/ipanat/inc/ipa_nat_drvi.h b/ipanat/inc/ipa_nat_drvi.h
index 25309e1..2a5a5ba 100644
--- a/ipanat/inc/ipa_nat_drvi.h
+++ b/ipanat/inc/ipa_nat_drvi.h
@@ -468,6 +468,7 @@
 void ipa_nati_print_rule(struct ipa_nat_rule *rule, uint32_t rule_id);
 void ipa_nat_dump_ipv4_table(uint32_t tbl_hdl);
 void ipa_nati_print_index_rule(struct ipa_nat_indx_tbl_rule *param, uint32_t rule_id);
+int ipa_nati_query_nat_rules(uint32_t tbl_hdl, nat_table_type tbl_type);
 #endif
 
 #endif /* #ifndef IPA_NAT_DRVI_H */
diff --git a/ipanat/inc/ipa_nat_logi.h b/ipanat/inc/ipa_nat_logi.h
index e98445a..b4eee0f 100644
--- a/ipanat/inc/ipa_nat_logi.h
+++ b/ipanat/inc/ipa_nat_logi.h
@@ -48,34 +48,33 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <syslog.h>
 
 #define NAT_LOG_SIZE 200
 
 #define PERROR(fmt)   printf("%s:%d %s()", __FILE__, __LINE__, __FUNCTION__);\
                       perror(fmt);
-
 #define IPADBG(fmt, ...) {\
                              int n =0; \
                              n = snprintf(nat_log_buf, sizeof(nat_log_buf), "%s:%d %s() ", __FILE__,  __LINE__, __FUNCTION__);\
                              snprintf((nat_log_buf+n), (sizeof(nat_log_buf)-n-1), fmt, ##__VA_ARGS__);\
-                             log_nat_message(nat_log_buf);\
+                             log_nat_message(nat_log_buf, LOG_DEBUG);\
 				  		             }
 
-
 #define IPAERR(fmt, ...) {\
                              int n =0; \
                              n = snprintf(nat_log_buf, sizeof(nat_log_buf), "%s:%d %s() %s", __FILE__,  __LINE__, __FUNCTION__, "Error:");\
                              snprintf((nat_log_buf+n), (sizeof(nat_log_buf)-n-1), fmt, ##__VA_ARGS__);\
-                             log_nat_message(nat_log_buf);\
+                             log_nat_message(nat_log_buf, LOG_ERR);\
 				  		             }
 
 #define IPADUMP(fmt, ...) {\
                              int n =0; \
                              snprintf((nat_log_buf+n), (sizeof(nat_log_buf)-1), fmt, ##__VA_ARGS__);\
-                             log_nat_message(nat_log_buf);\
+                             log_nat_message(nat_log_buf, LOG_INFO);\
 				  		             }
 
-extern void log_nat_message(char *msg);
+extern void log_nat_message(char *msg, int log_level);
 extern char nat_log_buf[NAT_LOG_SIZE];
 
 #ifdef __cplusplus
diff --git a/ipanat/src/Makefile.am b/ipanat/src/Makefile.am
index f8b775c..a688893 100644
--- a/ipanat/src/Makefile.am
+++ b/ipanat/src/Makefile.am
@@ -1,5 +1,6 @@
 AM_CFLAGS = -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs
 AM_CFLAGS += -I./../inc
+AM_CFLAGS += -DDEBUG
 
 c_sources   = ipa_nat_drv.c \
               ipa_nat_drvi.c \
diff --git a/ipanat/src/ipa_nat_drvi.c b/ipanat/src/ipa_nat_drvi.c
index 106abac..dbe9102 100644
--- a/ipanat/src/ipa_nat_drvi.c
+++ b/ipanat/src/ipa_nat_drvi.c
@@ -2262,7 +2262,6 @@
 														INDX_TBL_TBL_ENTRY_FIELD))
     {
 			ipa_nati_print_index_rule(&indx_tbl_ptr[cnt], cnt);
-      //ipa_nati_dump_index_rule_buf(&indx_tbl_ptr[cnt], sizeof(struct ipa_nat_indx_tbl_rule), cnt);
     }
   }
   if(!atl_one)
@@ -2284,7 +2283,6 @@
 														INDX_TBL_TBL_ENTRY_FIELD))
     {
 			ipa_nati_print_index_rule(&indx_tbl_ptr[cnt], cnt);
-      //ipa_nati_dump_index_rule_buf(&indx_tbl_ptr[cnt], sizeof(struct ipa_nat_indx_tbl_rule), cnt);
     }
   }
   if(!atl_one)
@@ -2320,4 +2318,117 @@
   IPADUMP("\n");
   return;
 }
+
+int ipa_nati_query_nat_rules(uint32_t tbl_hdl, nat_table_type tbl_type)
+{
+
+ struct ipa_nat_rule *tbl_ptr;
+ struct ipa_nat_indx_tbl_rule *indx_tbl_ptr;
+ int cnt =0, ret = 0;
+
+ if (IPA_NAT_INVALID_NAT_ENTRY == tbl_hdl || 
+		 tbl_hdl > IPA_NAT_MAX_IP4_TBLS)
+ {
+	 IPAERR("invalid table handle passed \n");
+	 return ret;
+ }
+
+ /* Print ipv4 rules */
+ if (tbl_type == IPA_NAT_BASE_TBL)
+ {
+	 IPADBG("Counting ipv4 active rules:\n");
+	 tbl_ptr = (struct ipa_nat_rule *)
+								ipv4_nat_cache.ip4_tbl[tbl_hdl-1].ipv4_rules_addr;
+	 for( cnt=0; 
+				cnt<ipv4_nat_cache.ip4_tbl[tbl_hdl-1].table_entries;
+				cnt++)
+	 {
+		 if (Read16BitFieldValue( tbl_ptr[cnt].ip_cksm_enbl,
+															ENABLE_FIELD))
+		 {
+			 ret++;
+		 }
+	 }
+	 if(!ret)
+	 {
+		 IPADBG("No active base rules\n");
+	 }
+
+	 IPADBG("Number of active base rules: %d\n", ret);
+ }
+
+ /* Print ipv4 expansion rules */
+ if (tbl_type == IPA_NAT_EXPN_TBL)
+ {
+	 IPADBG("Counting ipv4 active expansion rules:\n");
+	 tbl_ptr = (struct ipa_nat_rule *)
+								ipv4_nat_cache.ip4_tbl[tbl_hdl-1].ipv4_expn_rules_addr;
+	 for( cnt=0; 
+				cnt<ipv4_nat_cache.ip4_tbl[tbl_hdl-1].expn_table_entries;
+				cnt++)
+	 {
+		 if (Read16BitFieldValue(tbl_ptr[cnt].ip_cksm_enbl,
+														 ENABLE_FIELD))
+		 {
+			 ret++;
+		 }
+	 }
+	 if(!ret)
+	 {
+		 IPADBG("No active base expansion rules\n");
+	 }
+
+	 IPADBG("Number of active base expansion rules: %d\n", ret);
+ }
+
+	/* Print ipv4 index rules */
+ if (tbl_type == IPA_NAT_INDX_TBL)
+ {
+	 IPADBG("Counting ipv4 index active rules: \n");
+	 indx_tbl_ptr = (struct ipa_nat_indx_tbl_rule *)
+									ipv4_nat_cache.ip4_tbl[tbl_hdl-1].index_table_addr;
+		for( cnt=0; 
+				 cnt<ipv4_nat_cache.ip4_tbl[tbl_hdl-1].table_entries;
+				 cnt++)
+	 {
+		 if (Read16BitFieldValue(indx_tbl_ptr[cnt].tbl_entry_nxt_indx,
+														 INDX_TBL_TBL_ENTRY_FIELD))
+		 {
+			 ret++;
+		 }
+	 }
+	 if(!ret)
+	 {
+		 IPADBG("No active index table rules\n");
+	 }
+
+	 IPADBG("Number of active index table rules: %d\n", ret);
+ }
+
+ 	/* Print ipv4 index expansion rules */
+ if (tbl_type == IPA_NAT_INDEX_EXPN_TBL)
+ {
+	 IPADBG("Counting ipv4 index expansion active rules: \n");
+	 indx_tbl_ptr = (struct ipa_nat_indx_tbl_rule *)
+									ipv4_nat_cache.ip4_tbl[tbl_hdl-1].index_table_expn_addr;
+	 for ( cnt=0; 
+				 cnt<ipv4_nat_cache.ip4_tbl[tbl_hdl-1].expn_table_entries;
+				 cnt++)
+	 {
+		 if (Read16BitFieldValue(indx_tbl_ptr[cnt].tbl_entry_nxt_indx,
+														 INDX_TBL_TBL_ENTRY_FIELD))
+		 {
+			 ret++;
+		 }
+	 }
+	 if(!ret)
+	 {
+		 IPADBG("No active index expansion rules\n");
+	 }
+
+   IPADBG("Number of active index expansion rules: %d\n", ret);
+ }
+
+ return ret;
+}
 #endif
diff --git a/ipanat/src/ipa_nat_logi.c b/ipanat/src/ipa_nat_logi.c
index 11991c9..ead8154 100644
--- a/ipanat/src/ipa_nat_logi.c
+++ b/ipanat/src/ipa_nat_logi.c
@@ -39,28 +39,75 @@
 */
 #include "ipa_nat_logi.h"
 #include <stdlib.h>
+#include <unistd.h>
 
+/* write logging to a local file */
+#define LOG_USE_FILE "/etc/IPACM_LOG_F"
 #define NAT_FILE_NAME "/usr/ipanat_log.txt"
 
-static FILE *nat_fp = NULL;
+/* To use syslog for logging and
+	 use logread cmd to read */
+#define LOG_USE_SYS  "/etc/IPACM_LOG_S"
+
+/* Maximum log file size. 1MB(1048576)  */
+#define MAX_LOG_FILE_SIZE (1048576ul/4)
+
 char nat_log_buf[NAT_LOG_SIZE];
 
-void log_nat_message(char *msg)
+void log_nat_message(char *msg, int log_level)
 {
+#ifdef DEBUG
+	 static FILE *nat_fp = NULL;
+	 static int is_natsys_log_open = 0;
+	 int log_sys = 0, log_file = 0;
+
 	 printf("%s", msg);
-#if 0
-	 if(nat_fp == NULL)
+	 if(access(LOG_USE_SYS, F_OK) != -1 )
 	 {
-			nat_fp = fopen(NAT_FILE_NAME, "wb+");
-			if(nat_fp  == NULL)
-			{
-				 printf("unable to open file\n");
-				 return;
-			}
+		 log_sys = 1;
+	 }
+	 else if(access(LOG_USE_FILE, F_OK) != -1)
+	 {
+		 log_file = 1;
+	 }
+	 else
+	 {
+		 log_sys = 0;
+		 log_file = 0;
 	 }
 
-	 fprintf(nat_fp, msg);
-	 fflush(nat_fp);
+	 if(log_sys)
+	 {
+		 if(is_natsys_log_open == 0)
+		 {
+			 setlogmask(LOG_UPTO(LOG_DEBUG));
+			 openlog("IPANAT", LOG_PID, LOG_LOCAL0);
+
+			 is_natsys_log_open = 1;
+		 }
+		 syslog(log_level, msg);
+	 }
+	 else if(log_file)
+	 {
+		 if(nat_fp == NULL)
+		 {
+			 nat_fp = fopen(NAT_FILE_NAME, "wb+");
+			 if(nat_fp  == NULL)
+			 {
+				 printf("unable to open file\n");
+				 return;
+			 }
+		 }
+		 
+		 if(ftell(nat_fp) > MAX_LOG_FILE_SIZE)
+		 {
+			 rewind(nat_fp);
+			 fprintf(nat_fp, "====Rewinding=====\n");
+		 }
+
+		 fprintf(nat_fp, msg);
+		 fflush(nat_fp);
+	 }
 #endif
 	 return;
 }