IPACM/IPANAT: Fix using syslog causing crash

Before IPACM/IPANAT use the same syslog
circular buffer to log messages, now they
write the info to different files using
circular memory instead.

Change-Id: I8b637be2aa12d7e4999799b4b780133446422d2a
Signed-off-by: Skylar Chang <chiaweic@codeaurora.org>
Signed-off-by: Ravi Gummadidala <rgummadi@codeaurora.org>
diff --git a/ipacm/inc/IPACM_Config.h b/ipacm/inc/IPACM_Config.h
old mode 100755
new mode 100644
diff --git a/ipacm/inc/IPACM_ConntrackClient.h b/ipacm/inc/IPACM_ConntrackClient.h
old mode 100755
new mode 100644
diff --git a/ipacm/inc/IPACM_ConntrackListener.h b/ipacm/inc/IPACM_ConntrackListener.h
old mode 100755
new mode 100644
diff --git a/ipacm/inc/IPACM_Conntrack_NATApp.h b/ipacm/inc/IPACM_Conntrack_NATApp.h
old mode 100755
new mode 100644
index 530feb9..0a6d19a
--- a/ipacm/inc/IPACM_Conntrack_NATApp.h
+++ b/ipacm/inc/IPACM_Conntrack_NATApp.h
@@ -42,8 +42,6 @@
 #include <ipa_nat_drv.h>
 }
 
-#define IPACM_DEBUG
-
 typedef struct _nat_table_entry
 {
 	uint32_t private_ip;
@@ -63,12 +61,7 @@
 
 }nat_table_entry;
 
-#define CHK_TBL_HDL() if(0 == nat_table_hdl){\
-                             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, LOG_ERR);\
-				  		             }
+#define CHK_TBL_HDL()  if(nat_table_hdl == 0){ return -1; }
 
 class NatApp
 {
diff --git a/ipacm/inc/IPACM_Defs.h b/ipacm/inc/IPACM_Defs.h
old mode 100755
new mode 100644
diff --git a/ipacm/inc/IPACM_Log.h b/ipacm/inc/IPACM_Log.h
index f75e73c..54be34a 100644
--- a/ipacm/inc/IPACM_Log.h
+++ b/ipacm/inc/IPACM_Log.h
@@ -50,28 +50,19 @@
 #include <string.h>
 #include <syslog.h>
 
-#define LOG_SIZE 200
-
 #define PERROR(fmt)   printf("%s:%d %s()", __FILE__, __LINE__, __FUNCTION__);\
                       perror(fmt);
 
-#define IPACMDBG(fmt, ...) {\
-                             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, LOG_DEBUG);\
-				  		             }
+#define IPACMERR(fmt, ...) syslog(LOG_ERR, "ERROR: %s:%d %s() " fmt, __FILE__,  __LINE__, __FUNCTION__, ##__VA_ARGS__);\
+                           printf("ERR: %s:%d %s() " fmt, __FILE__,  __LINE__, __FUNCTION__, ##__VA_ARGS__);
 
+#ifdef DEBUG
+#define IPACMDBG(fmt, ...) syslog(LOG_DEBUG, "%s:%d %s() " fmt, __FILE__,  __LINE__, __FUNCTION__, ##__VA_ARGS__);\
+                           printf("%s:%d %s() " fmt, __FILE__,  __LINE__, __FUNCTION__, ##__VA_ARGS__);
+#else
+#define IPACMDBG(fmt, ...)
+#endif
 
-#define IPACMERR(fmt, ...) {\
-                             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, LOG_ERR);\
-				  		             }
-
-extern void logmessage(char *msg, int log_level);
-extern char log_buf[LOG_SIZE];
 
 #ifdef __cplusplus
 }
diff --git a/ipacm/inc/IPACM_Wlan.h b/ipacm/inc/IPACM_Wlan.h
old mode 100755
new mode 100644
diff --git a/ipacm/inc/IPACM_Xml.h b/ipacm/inc/IPACM_Xml.h
old mode 100755
new mode 100644
diff --git a/ipacm/src/IPACM_Config.cpp b/ipacm/src/IPACM_Config.cpp
old mode 100755
new mode 100644
index 4717db0..0decb50
--- a/ipacm/src/IPACM_Config.cpp
+++ b/ipacm/src/IPACM_Config.cpp
@@ -300,7 +300,7 @@
 			/* Reset the matched entry */
 			memset(pNatIfaces[i].iface_name, 0, IPA_IFACE_NAME_LEN);
 
-			for (i; i < ipa_nat_iface_entries - 1; i++)
+			for (; i < ipa_nat_iface_entries - 1; i++)
 			{
 				memcpy(pNatIfaces[i].iface_name,
 							 pNatIfaces[i + 1].iface_name, IPA_IFACE_NAME_LEN);
diff --git a/ipacm/src/IPACM_ConntrackClient.cpp b/ipacm/src/IPACM_ConntrackClient.cpp
old mode 100755
new mode 100644
diff --git a/ipacm/src/IPACM_ConntrackListener.cpp b/ipacm/src/IPACM_ConntrackListener.cpp
old mode 100755
new mode 100644
index 56eeb4d..ace8a68
--- a/ipacm/src/IPACM_ConntrackListener.cpp
+++ b/ipacm/src/IPACM_ConntrackListener.cpp
@@ -578,9 +578,9 @@
 	 
 	 if(cnt == MAX_NAT_IFACES)
 	 {
-		IPACM_ConntrackClient::iptodot("ProcessTCPorUDPMsg(): ignoring iface with ip address", 
-				                       nat_iface_ipv4_addr[cnt]);
-	    return;
+		 IPACM_ConntrackClient::iptodot("ProcessTCPorUDPMsg(): ignoring nat rule with private ip",
+																		rule.private_ip);
+		 return;
 	 }
 	 
 	 IPACMDBG("Nat Entry with below information will be added\n");
diff --git a/ipacm/src/IPACM_Conntrack_NATApp.cpp b/ipacm/src/IPACM_Conntrack_NATApp.cpp
old mode 100755
new mode 100644
index f82d7da..3f027e3
--- a/ipacm/src/IPACM_Conntrack_NATApp.cpp
+++ b/ipacm/src/IPACM_Conntrack_NATApp.cpp
@@ -201,13 +201,11 @@
 
 	CHK_TBL_HDL();
 
-#ifdef IPACM_DEBUG
   IPACMDBG("Received below nat entry for deletion\n");
 	IPACM_ConntrackClient::iptodot("Private IP", rule->private_ip);
 	IPACM_ConntrackClient::iptodot("Target IP", rule->target_ip);
 	IPACMDBG("Private Port: %d\t Target Port: %d\t", rule->private_port, rule->target_port);
 	IPACMDBG("protocolcol: %d\n", rule->protocol);
-#endif
 
 	for(; cnt < max_entries; cnt++)
 	{
@@ -345,8 +343,6 @@
 		return -1;
 	}
 
-
-#ifdef IPACM_DEBUG
 	if(cache[cnt].enabled == true)
 	{
 		IPACMDBG("Added below rule successfully\n");
@@ -356,7 +352,6 @@
 		IPACMDBG("Public Port:%d\n", rule->public_port);
 		IPACMDBG("protocol: %d\n", rule->protocol);
 	}
-#endif
 
 	return 0;
 }
@@ -365,11 +360,9 @@
 {
 	int ret;
 
-#ifdef IPACM_DEBUG
 	IPACM_ConntrackClient::iptodot("Private IP:", rule->private_ip);
 	IPACM_ConntrackClient::iptodot("Target IP:",  rule->target_ip);
 	IPACMDBG("Private Port: %d, Target Port: %d\n", rule->private_port, rule->target_port);
-#endif
 
 	if(!ct_hdl)
 	{
@@ -603,14 +596,12 @@
 			}
 			cache[cnt].enabled = true;
 
-#ifdef IPACM_DEBUG
 			IPACMDBG("On power reset added below rule successfully\n");
 			IPACM_ConntrackClient::iptodot("Private IP", nat_rule.private_ip);
 			IPACM_ConntrackClient::iptodot("Target IP", nat_rule.target_ip);
 			IPACMDBG("Private Port:%d \t Target Port: %d\t", nat_rule.private_port, nat_rule.target_port);
 			IPACMDBG("Public Port:%d\n", nat_rule.public_port);
 			IPACMDBG("protocol: %d\n", nat_rule.protocol);
-#endif
 
 		}
 	}
diff --git a/ipacm/src/IPACM_Iface.cpp b/ipacm/src/IPACM_Iface.cpp
old mode 100755
new mode 100644
index 3328a01..154e77a
--- a/ipacm/src/IPACM_Iface.cpp
+++ b/ipacm/src/IPACM_Iface.cpp
@@ -526,10 +526,11 @@
 	int res = IPACM_SUCCESS, len = 0;
 	struct ipa_flt_rule_add flt_rule_entry;
 	ipa_ioc_add_flt_rule *m_pFilteringTable;
-        /* Adding this hack because WLAN may not registered for Rx-endpoint, other ifaces will always have*/
-	char *dev_wlan0="wlan0";
-	char *dev_wlan1="wlan1";
-	char *dev_ecm0="ecm0";
+
+  /* Adding this hack because WLAN may not registered for Rx-endpoint, other ifaces will always have*/
+	const char *dev_wlan0="wlan0";
+	const char *dev_wlan1="wlan1";
+	const char *dev_ecm0="ecm0";
 
 	/* update the iface ip-type to be IPA_IP_v4, IPA_IP_v6 or both*/
 	if (iptype == IPA_IP_v4)
diff --git a/ipacm/src/IPACM_Lan.cpp b/ipacm/src/IPACM_Lan.cpp
old mode 100755
new mode 100644
diff --git a/ipacm/src/IPACM_Log.cpp b/ipacm/src/IPACM_Log.cpp
index b777540..72f71c5 100644
--- a/ipacm/src/IPACM_Log.cpp
+++ b/ipacm/src/IPACM_Log.cpp
@@ -41,75 +41,8 @@
 #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"
-
-/* 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, int log_level)
+void logmessage(int log_level)
 {
-#ifdef DEBUG
-	 static FILE *fp = NULL;
-	 static bool is_sys_log_open = false;
-	 int log_sys = 0, log_file = 0;
-
-	 printf("%s\n", msg);
-
-	 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;
 }
 
diff --git a/ipacm/src/IPACM_Wan.cpp b/ipacm/src/IPACM_Wan.cpp
old mode 100755
new mode 100644
diff --git a/ipacm/src/IPACM_Wlan.cpp b/ipacm/src/IPACM_Wlan.cpp
old mode 100755
new mode 100644
diff --git a/ipacm/src/IPACM_Xml.cpp b/ipacm/src/IPACM_Xml.cpp
old mode 100755
new mode 100644
diff --git a/ipacm/src/IPACM_cfg.xml b/ipacm/src/IPACM_cfg.xml
old mode 100755
new mode 100644
diff --git a/ipacm/src/Makefile.am b/ipacm/src/Makefile.am
index 1eac45a..3811c40 100644
--- a/ipacm/src/Makefile.am
+++ b/ipacm/src/Makefile.am
@@ -2,7 +2,7 @@
 	      -I$(top_srcdir)/ipanat/inc \
 	      ${LIBXML_CFLAGS}
 AM_CPPFLAGS += -Wall -Wundef -Wno-trigraphs
-AM_CPPFLAGS	+= -DDEBUG
+#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 2a5a5ba..6fce315 100644
--- a/ipanat/inc/ipa_nat_drvi.h
+++ b/ipanat/inc/ipa_nat_drvi.h
@@ -407,8 +407,9 @@
                             uint32_t  rule_hdl,
                             uint32_t  *time_stamp);
 
-uint32_t ipa_nati_add_ipv4_rule(uint32_t tbl_hdl, 
-                           const ipa_nat_ipv4_rule *clnt_rule);
+int ipa_nati_add_ipv4_rule(uint32_t tbl_hdl, 
+                           const ipa_nat_ipv4_rule *clnt_rule,
+													 uint32_t *rule_hdl);
 
 int ipa_nati_generate_rule(uint32_t tbl_hdl,
                            const ipa_nat_ipv4_rule *clnt_rule,
diff --git a/ipanat/inc/ipa_nat_logi.h b/ipanat/inc/ipa_nat_logi.h
index b4eee0f..0e1a5d1 100644
--- a/ipanat/inc/ipa_nat_logi.h
+++ b/ipanat/inc/ipa_nat_logi.h
@@ -50,32 +50,24 @@
 #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_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_ERR);\
-				  		             }
+#define IPAERR(fmt, ...) syslog(LOG_ERR, "ERROR: %s:%d %s() " fmt, __FILE__,  __LINE__, __FUNCTION__, ##__VA_ARGS__);\
+										     printf("ERR: %s:%d %s() " fmt, __FILE__,  __LINE__, __FUNCTION__, ##__VA_ARGS__);
 
-#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_INFO);\
-				  		             }
+#ifdef DEBUG
+#define IPADBG(fmt, ...) syslog(LOG_DEBUG, "%s:%d %s() " fmt, __FILE__,  __LINE__, __FUNCTION__, ##__VA_ARGS__);\
+	                       printf("%s:%d %s() " fmt, __FILE__,  __LINE__, __FUNCTION__, ##__VA_ARGS__);
 
-extern void log_nat_message(char *msg, int log_level);
-extern char nat_log_buf[NAT_LOG_SIZE];
+#define IPADUMP(fmt, ...) syslog(LOG_DEBUG, fmt, ##__VA_ARGS__);\
+                          printf(fmt, ##__VA_ARGS__);
+
+#else
+#define IPADBG(fmt, ...)
+#define IPADUMP(fmt, ...)
+#endif
+
 
 #ifdef __cplusplus
 }
diff --git a/ipanat/src/Makefile.am b/ipanat/src/Makefile.am
index a688893..ef6969e 100644
--- a/ipanat/src/Makefile.am
+++ b/ipanat/src/Makefile.am
@@ -1,6 +1,6 @@
 AM_CFLAGS = -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs
 AM_CFLAGS += -I./../inc
-AM_CFLAGS += -DDEBUG
+#AM_CFLAGS += -DDEBUG
 
 c_sources   = ipa_nat_drv.c \
               ipa_nat_drvi.c \
diff --git a/ipanat/src/ipa_nat_drv.c b/ipanat/src/ipa_nat_drv.c
index 68e2de8..2b62a3c 100644
--- a/ipanat/src/ipa_nat_drv.c
+++ b/ipanat/src/ipa_nat_drv.c
@@ -110,9 +110,12 @@
   }
   IPADBG("Passed Table handle: 0x%x\n", tbl_hdl);
 
-  *rule_hdl = ipa_nati_add_ipv4_rule(tbl_hdl, clnt_rule);
-  IPADBG("returning rule handle 0x%x\n", *rule_hdl);
+  if(ipa_nati_add_ipv4_rule(tbl_hdl, clnt_rule, rule_hdl) != 0)
+	{
+		return -1;
+	}
 
+  IPADBG("returning rule handle 0x%x\n", *rule_hdl);
   return 0;
 }
 
diff --git a/ipanat/src/ipa_nat_drvi.c b/ipanat/src/ipa_nat_drvi.c
old mode 100755
new mode 100644
index 692509f..5678306
--- a/ipanat/src/ipa_nat_drvi.c
+++ b/ipanat/src/ipa_nat_drvi.c
@@ -1072,14 +1072,14 @@
   return 0;
 }
 
-uint32_t ipa_nati_add_ipv4_rule(uint32_t tbl_hdl,
-                                const ipa_nat_ipv4_rule *clnt_rule)
+int ipa_nati_add_ipv4_rule(uint32_t tbl_hdl,
+                           const ipa_nat_ipv4_rule *clnt_rule,
+													 uint32_t *rule_hdl)
 {
   struct ipa_nat_ip4_table_cache *tbl_ptr;
   struct ipa_nat_sw_rule sw_rule;
   struct ipa_nat_indx_tbl_sw_rule index_sw_rule;
   uint16_t new_entry, new_index_tbl_entry;
-  uint32_t rule_hdl = 0;
 
   memset(&sw_rule, 0, sizeof(sw_rule));
   memset(&index_sw_rule, 0, sizeof(index_sw_rule));
@@ -1108,8 +1108,8 @@
   }
  
   /* Generate rule handle */
-  rule_hdl  = ipa_nati_make_rule_hdl((uint16_t)tbl_hdl, new_entry);
-  if (!rule_hdl)
+  *rule_hdl  = ipa_nati_make_rule_hdl((uint16_t)tbl_hdl, new_entry);
+  if (!(*rule_hdl))
   {
     IPAERR("unable to generate rule handle\n");
     return -1;
@@ -1119,7 +1119,7 @@
   ipa_nat_dump_ipv4_table(tbl_hdl);
 #endif
 
-  return rule_hdl;
+  return 0;
 }
 int ipa_nati_generate_rule(uint32_t tbl_hdl,
                            const ipa_nat_ipv4_rule *clnt_rule,
diff --git a/ipanat/src/ipa_nat_logi.c b/ipanat/src/ipa_nat_logi.c
index ead8154..b829b78 100644
--- a/ipanat/src/ipa_nat_logi.c
+++ b/ipanat/src/ipa_nat_logi.c
@@ -41,74 +41,8 @@
 #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"
-
-/* 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, int log_level)
+void log_nat_message(char *msg)
 {
-#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(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_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;
 }