OSDN Git Service

modified comments
[opengatem/opengatem.git] / mdsrc / macdbcache.c
1 /**************************************************
2 OpengateM - a MAC address authentication system
3  module to control cache for MAC address DB
4
5   the cache holds the temporary copy of DB to speedup checking.
6   Only the allowable MAC addresses are hold.
7
8   As ip address check by database is time consuming procedure,
9   the recently checked mac addresses are cached.
10   Implemented with HashTable.
11   HashTable:
12     Key= MAC Address
13     Val= userId, extraId and cache time.
14     If MAC address is found in table and time is new, skip DB access.
15     If time is old, remove the cached item.
16
17 Copyright (C) 2012 Opengate Project Team
18 Written by Yoshiaki Watanabe
19
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License
22 as published by the Free Software Foundation; either version 2
23 of the License, or (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
33
34 Email: watanaby@is.saga-u.ac.jp
35 **************************************************/
36 #include "opengatemd.h"
37
38 /* HashTable to store MacAddress->userId,extraId,Time */
39 static DB* macHashDb;
40
41 /* Cache Timeout(seconds) (read from conf file) */
42 static int macCacheTimeout;
43
44 void dumpHashTable(DB* table);
45
46 /****************************************
47 add item to Mac cache(hash table:macHashDb)
48  key=macaddress, value="<found>SPACE<unixtime>SPACE<userId>SPACE<extraId>"
49  <found>: TRUE=found in DB, FALSE=not found in DB
50 ****************************************/
51 int addMacCacheItem(char* macAddress, char* userId, char* extraId, int found) {
52
53   DBT hashKey;
54   DBT hashVal;
55   char hashValueStr[BUFFMAXLN];
56
57   /* check address format */
58   if(isNull(macAddress)) return FALSE;
59   if(!ReFormatMacAddr(macAddress)) return FALSE;
60
61   /** setup hash key **/
62   /* hash key : string of mac address  */  
63   hashKey.data = macAddress;
64   hashKey.size = strlen(macAddress) + 1;
65
66   /** setup hash value **/
67   /* hash value : string "<found>SPACE<unixtime>SPACE<userId>SPACE<extraId>" */
68   snprintf(hashValueStr,BUFFMAXLN,"%d %ld %s %s",
69            found,time(NULL),userId,extraId);
70   hashVal.data = hashValueStr;
71   hashVal.size = strlen(hashValueStr) + 1;    
72   if(macHashDb->put(macHashDb, &hashKey, &hashVal, 0) == -1) {
73     err_msg("ERR at %s#%d: fail to put into hash table",__FILE__,__LINE__);
74     return FALSE;
75   }
76
77   return TRUE;
78 }
79
80 /****************************************
81 query userid and extraid for mac address
82 if the address is in cache, return TRUE(found in DB) or FALSE(not found).
83 if not in cache, or too old, or error, return ERROR.
84 ****************************************/
85 int queryMacFromMacCache(char* macAddress, char* userId, char* extraId){
86
87   DBT hashKey;
88   DBT hashVal;
89   time_t entryTime;
90   int ret;
91   int found;
92
93   /* set default */
94   entryTime=0;
95   userId[0]=extraId[0]='\0';
96
97   /* if null or illegal form, return */
98   if(isNull(macAddress)) return ERROR;
99   if(!ReFormatMacAddr(macAddress)) return ERROR;
100
101   /***** get hashed item matched to the indicated mac */
102   hashKey.data = macAddress;
103   hashKey.size = strlen(macAddress) + 1;
104   memset(&hashVal, 0, sizeof(DBT));
105   ret=macHashDb->get(macHashDb, &hashKey, &hashVal, 0);
106
107   /* get is failed, return false */
108   if(ret!=0) return ERROR;
109
110   /* get is successed */
111   /* pick up the hash values */
112   ret=sscanf(hashVal.data,"%d %ld %s %s",&found,&entryTime,userId,extraId);
113
114   /* found, entryTime should be obtained */
115   if(ret<=1) return ERROR;
116
117   /* if entry time is older than timeout, return false */
118   if( entryTime + macCacheTimeout < time(NULL) ) return ERROR;
119
120   /* return the mac db access result (found/not found) */
121   return found;
122 }
123
124 /****************************************
125 initialize Mac Cache
126 ****************************************/
127 void initMacCache(void) {
128
129   /* prepare hash table */
130   if((macHashDb = dbopen(NULL, O_CREAT | O_RDWR, 0644, DB_HASH, NULL)) == NULL) {
131     err_msg("ERR at %s#%d: fail to open mac hash table",__FILE__,__LINE__);
132     terminateProg(0);
133   }
134
135   /* set counter and timeout parameter */
136   if(isNull(GetConfValue("MacCacheTimeout"))){
137     err_msg("ERR at %s#%d: cannot get MacCacheTimeout from conf file",__FILE__,__LINE__);
138     macCacheTimeout=0;
139   }else{
140     macCacheTimeout=atoi(GetConfValue("MacCacheTimeout"));
141   }  
142 }
143
144 /****************************************
145 Memory free for Mac Cache
146 ****************************************/
147 void freeMacCache(void) {
148
149   macHashDb->close(macHashDb);
150 }
151
152 /****************************************
153 delete item from Mac cache matched to the mac address
154 ****************************************/
155 int delMacCacheItem(char* macAddress) {
156
157   DBT hashKey;
158   
159   /* if null or illegal form, return */
160   if(isNull(macAddress)) return FALSE;
161   if(!ReFormatMacAddr(macAddress)) return FALSE;
162
163   /* delete the item from Hash Table */
164   hashKey.data = macAddress;
165   hashKey.size = strlen(macAddress) + 1;
166   macHashDb->del(macHashDb, &hashKey, 0);
167
168   return TRUE;
169 }
170
171 /************************************
172 dump print of hash table to syslog
173 (debug routine for hash table)
174 ************************************/
175 void dumpHashTable(DB* table){
176   DBT hashKey;
177   DBT hashVal;
178   int ret;
179
180   memset(&hashKey, 0, sizeof(DBT));
181   memset(&hashVal, 0, sizeof(DBT));
182   ret=table->seq(table, &hashKey, &hashVal, R_FIRST);
183   while(ret==0){
184
185     err_msg("%s:%s", (char*)hashKey.data, (char*)hashVal.data);
186
187     /* get next entry */
188     ret=table->seq(table, &hashKey, &hashVal, R_NEXT);
189   }
190 }
191
192 /****************************************************
193  routines for debugging putput
194  ***************************************************/
195
196 void InitMacCache(void) {
197   if(debug>1) err_msg("DEBUG:=>initmacCache( )");
198   initMacCache();
199   if(debug>1) err_msg("DEBUG:<=initMacCache( )");
200 }
201
202 void FreeMacCache(void) {
203   if(debug>1) err_msg("DEBUG:=>freemacCache()");
204   freeMacCache();
205   if(debug>1) err_msg("DEBUG:<=freeMacCache()");
206 }
207
208 int AddMacCacheItem(char* macAddress, char* userId, char* extraId, int found) {
209   int ret;
210   if(debug>1) err_msg("DEBUG:=>addMacCacheItem(%s,%s,%s,%d)", macAddress,userId,extraId,found);
211   ret = addMacCacheItem(macAddress,userId,extraId,found);
212   if(debug>1) err_msg("DEBUG:(%d)<=addMacCacheItem( )",ret);
213   return ret;
214 }
215
216 int QueryMacFromMacCache(char* macAddress, char* userId, char* extraId){
217   int ret;
218   if(debug>1) err_msg("DEBUG:=>queryMacFromMacCache(%s)", macAddress);
219   ret = queryMacFromMacCache(macAddress, userId, extraId);
220   if(debug>1) err_msg("DEBUG:(%d)<=queryMacFromMacCache(,%s,%s)",ret,userId,extraId);
221   return ret;
222 }
223
224 int DelMacCacheItem(char* macAddress) {
225   int ret;
226   if(debug>1) err_msg("DEBUG:=>delMacCacheItem(%s)", macAddress);
227   ret = delMacCacheItem(macAddress);
228   if(debug>1) err_msg("DEBUG:(%d)<=delMacCacheItem( )",ret);
229   return ret;
230 }
231