OSDN Git Service

Modified mac cache to store not-found info
authorwatanaby <watanaby@users.sourceforge.net>
Wed, 19 Jun 2013 05:56:06 +0000 (14:56 +0900)
committerwatanaby <watanaby@users.sourceforge.net>
Wed, 19 Jun 2013 05:56:06 +0000 (14:56 +0900)
doc/Changes.html
mdsrc/macdbcache.c
mdsrc/managementdb.c
mdsrc/opengatemd.c
mdsrc/opengatemd.h
sqlscript/createtablescript
sqlscript/updatescript1
sqlscript/updatescript2
sqlscript/updatescript3
sqlscript/updatescript4 [new file with mode: 0644]

index 60ffd1c..f8b1d5d 100644 (file)
@@ -221,6 +221,10 @@ OpengateM History</H3>
        Modified to save all ip addresses(opengatemd).
        At updating, rerun createtablescript to add a table.
        </DD>
+       <DT>Ver.0.8.4 at 2013.6.19
+       </DT><DD>
+       Modified mac cache to store not-found info (opengatemd).
+       </DD>
 </DL>
 
 </BODY>
index 384887c..74505d1 100644 (file)
@@ -46,7 +46,7 @@ void dumpHashTable(DB* table);
 /****************************************
 add item to Mac cache 
 ****************************************/
-int addMacCacheItem(char* macAddress, char* userId, char* extraId) {
+int addMacCacheItem(char* macAddress, char* userId, char* extraId, int found) {
 
   DBT hashKey;
   DBT hashVal;
@@ -62,8 +62,9 @@ int addMacCacheItem(char* macAddress, char* userId, char* extraId) {
   hashKey.size = strlen(macAddress) + 1;
 
   /** setup hash value **/
-  /* hash value : string "<unixtime>SPACE<userId>SPACE<extraId>" */
-  snprintf(hashValueStr,BUFFMAXLN,"%d %s %s",time(NULL),userId,extraId);
+  /* hash value : string "<found>SPACE<unixtime>SPACE<userId>SPACE<extraId>" */
+  snprintf(hashValueStr,BUFFMAXLN,"%d %d %s %s",
+          found,time(NULL),userId,extraId);
   hashVal.data = hashValueStr;
   hashVal.size = strlen(hashValueStr) + 1;    
   if(macHashDb->put(macHashDb, &hashKey, &hashVal, 0) == -1) {
@@ -76,7 +77,8 @@ int addMacCacheItem(char* macAddress, char* userId, char* extraId) {
 
 /****************************************
 query userid and extraid for mac address
-if the entry time is old, return false
+if address is found in cache, return true or false(enable addr or not).
+if not found, too old or error, return error.
 ****************************************/
 int queryMacFromMacCache(char* macAddress, char* userId, char* extraId){
 
@@ -84,14 +86,15 @@ int queryMacFromMacCache(char* macAddress, char* userId, char* extraId){
   DBT hashVal;
   time_t entryTime;
   int ret;
+  int found;
 
   /* set default */
   entryTime=0;
   userId[0]=extraId[0]='\0';
 
   /* if null or illegal form, return */
-  if(isNull(macAddress)) return FALSE;
-  if(!ReFormatMacAddr(macAddress)) return FALSE;
+  if(isNull(macAddress)) return ERROR;
+  if(!ReFormatMacAddr(macAddress)) return ERROR;
 
   /***** get hashed item matched to the indicated mac */
   hashKey.data = macAddress;
@@ -100,19 +103,20 @@ int queryMacFromMacCache(char* macAddress, char* userId, char* extraId){
   ret=macHashDb->get(macHashDb, &hashKey, &hashVal, 0);
 
   /* get is failed, return false */
-  if(ret!=0) return FALSE;
+  if(ret!=0) return ERROR;
 
   /* get is successed */
   /* pick up the hash values */
-  ret=sscanf(hashVal.data,"%d %s %s",&entryTime,userId,extraId);
+  ret=sscanf(hashVal.data,"%d %d %s %s",&found,&entryTime,userId,extraId);
 
-  /* entryTime and userId should be obtained, but extraId is not */
-  if(ret<=1) return FALSE;
+  /* found, entryTime should be obtained */
+  if(ret<=1) return ERROR;
 
   /* if entry time is older than timeout, return false */
-  if( entryTime + macCacheTimeout < time(NULL) ) return FALSE;
+  if( entryTime + macCacheTimeout < time(NULL) ) return ERROR;
 
-  return TRUE;
+  /* return the mac db access result (found/not found) */
+  return found;
 }
 
 /****************************************
@@ -198,10 +202,10 @@ void FreeMacCache(void) {
   if(debug>1) err_msg("DEBUG:<=freeMacCache()");
 }
 
-int AddMacCacheItem(char* macAddress, char* userId, char* extraId) {
+int AddMacCacheItem(char* macAddress, char* userId, char* extraId, int found) {
   int ret;
-  if(debug>1) err_msg("DEBUG:=>addMacCacheItem(%s,%s,%s)", macAddress,userId,extraId);
-  ret = addMacCacheItem(macAddress,userId,extraId);
+  if(debug>1) err_msg("DEBUG:=>addMacCacheItem(%s,%s,%s,%d)", macAddress,userId,extraId,found);
+  ret = addMacCacheItem(macAddress,userId,extraId,found);
   if(debug>1) err_msg("DEBUG:(%d)<=addMacCacheItem( )",ret);
   return ret;
 }
index 87f0dc0..5432c17 100644 (file)
@@ -76,7 +76,8 @@ void closeMngDb(void){
 
 /******************************************
 query data from management db
-input=macAddress,output=others
+input=macAddress
+return value: 1=found,0=not found,-1=query error
 ******************************************/
 int queryMacFromMngDb(char* macAddress, char* userId, char* extraId){
   MYSQL_RES *res;
@@ -97,7 +98,7 @@ int queryMacFromMngDb(char* macAddress, char* userId, char* extraId){
   if (mysql_query(&mysql, queryStr)){
      err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
             mysql_error(&mysql));
-     return FALSE;
+     return ERROR;
   }
 
   res = mysql_use_result(&mysql);
index 31bff0c..c5cf6f7 100644 (file)
@@ -235,10 +235,14 @@ int  main(int argc, char **argv)
     /* search the address in cache of MAC DB */ 
     macFound = QueryMacFromMacCache(macAddress, userId, extraId);
 
-    /* if not found in MAC DB cache, access MAC DB and add result to cache */
-    if(!macFound){
+    /* if accessing cache of MAC DB is failed, access MAC DB */
+    if(macFound==ERROR){
       macFound = QueryMacFromMngDb(macAddress, userId, extraId);
-      if(macFound) AddMacCacheItem(macAddress, userId, extraId);
+      
+      /* if db access is failed, set not-found and retry next time */
+      /* if db access is successed (found or not), save it to cache */ 
+      if(macFound==ERROR) macFound=FALSE;
+      else AddMacCacheItem(macAddress, userId, extraId, macFound);
     }
 
     /*** depending the states, add/del/renew the session ***/
index 4aa7499..46be3ed 100644 (file)
@@ -230,5 +230,5 @@ void InitMacCache(void);
 void FreeMacCache(void);
 int QueryMacFromMacCache(char* macAddress, char* userId, char* extraId);
 int DelMacCacheItem(char* macAddress);
-int AddMacCacheItem(char* macAddress, char* userId, char* extraId);
+int AddMacCacheItem(char* macAddress, char* userId, char* extraId, int found);
 
index ab3eda3..a591f4e 100644 (file)
@@ -37,7 +37,7 @@ create table if not exists nicvendors(
        oui CHAR(8),
        org TINYTEXT);
 
-create view sessionview as select
+create or replace view sessionview as select
        userId,
        extraId,
        macaddrs.macAddress,
index e8a1622..6476fca 100644 (file)
@@ -2,7 +2,7 @@
 Change the mac address to deleted mode 
 for device passing limit date and belonging to specific user (extraId indicated).
 Use as
- mysql < createscript1
+ mysql < updatescript1
 */
 use opengatem;
 update macaddrs set status='D' where extraId!='' and limitDate<now() and status!='D';
index 1e5d8dc..675b0ec 100644 (file)
@@ -2,7 +2,7 @@
 Change the mac address to deleted mode 
 for device passing 6 month after limit date.
 Use as
- mysql < createscript2
+ mysql < updatescript2
 */
 use opengatem;
 update macaddrs set status='D' where limitDate<adddate(now(), interval -6 month) and status!='D';
index 3d0ec3c..7fe7186 100644 (file)
@@ -2,7 +2,7 @@
 Remove the mac address record 
 for device passing 1 year after limit date.
 Use as
- mysql < createscript3
+ mysql < updatescript3
 */
 use opengatem;
 delete from macaddrs where limitDate<adddate(now(), interval -1 year);
diff --git a/sqlscript/updatescript4 b/sqlscript/updatescript4
new file mode 100644 (file)
index 0000000..de717ff
--- /dev/null
@@ -0,0 +1,10 @@
+/*
+Remove the log record passing 1 year after recorded date.
+Use as
+ mysql < updatescript4
+*/
+use opengatem;
+delete from sessionmd where closeTime<adddate(now(), interval -1 year);
+delete from macippair where findTime<adddate(now(), interval -1 year);
+
+