• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

OpengateM Source Repository


Commit MetaInfo

Révisionde6d2eaa2b7b25272be4e11d58f2d1cd65b74bef (tree)
l'heure2013-06-18 15:18:47
Auteurwatanaby <watanaby@user...>
Commiterwatanaby

Message de Log

modified to save all ip addresses

Change Summary

Modification

--- a/doc/Changes.html
+++ b/doc/Changes.html
@@ -214,7 +214,12 @@ OpengateM History</H3>
214214 </DD>
215215 <DT>Ver.0.8.2 at 2013.6.13
216216 </DT><DD>
217- Fixed bug in ip-address conversion(opengatemd)
217+ Fixed bug in ip address conversion(opengatemd)
218+ </DD>
219+ <DT>Ver.0.8.3 at 2013.6.18
220+ </DT><DD>
221+ Modified to save all ip addresses(opengatemd).
222+ At updating, rerun createtablescript to add a table.
218223 </DD>
219224 </DL>
220225
--- a/mdsrc/managementdb.c
+++ b/mdsrc/managementdb.c
@@ -122,7 +122,7 @@ int queryMacFromMngDb(char* macAddress, char* userId, char* extraId){
122122 /******************************************
123123 put open log to management db
124124 ******************************************/
125-int putOpenToMngDb(char* macAddress, char* ipAddress){
125+int putOpenToMngDb(char* macAddress){
126126
127127 char queryStr[BUFFMAXLN];
128128 struct utsname uts;
@@ -133,9 +133,9 @@ int putOpenToMngDb(char* macAddress, char* ipAddress){
133133 /* prepare query string */
134134 snprintf(queryStr, BUFFMAXLN,
135135 "insert into sessionmd "
136- "(macAddress, ipAddress, gatewayName, openTime, closeTime) "
137- "values ('%s','%s','%s', now(), 0)",
138- macAddress, ipAddress, uts.nodename);
136+ "(macAddress, gatewayName, openTime, closeTime) "
137+ "values ('%s','%s', now(), 0)",
138+ macAddress, uts.nodename);
139139
140140 /* send SQL query */
141141 if (mysql_query(&mysql, queryStr)){
@@ -170,6 +170,34 @@ int putCloseToMngDb(char* macAddress){
170170
171171 }
172172
173+/******************************************
174+put MAC-IP pair to management db
175+******************************************/
176+int putMacIpPairToMngDb(char* macAddress, char* ipAddress){
177+
178+ char queryStr[BUFFMAXLN];
179+ struct utsname uts;
180+
181+ /* get domain name */
182+ uname(&uts);
183+
184+ /* prepare query string */
185+ snprintf(queryStr, BUFFMAXLN,
186+ "insert into macippair "
187+ "(macAddress, ipAddress, findTime) "
188+ "values ('%s','%s', now())",
189+ macAddress, ipAddress);
190+
191+ /* send SQL query */
192+ if (mysql_query(&mysql, queryStr)){
193+ err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
194+ mysql_error(&mysql));
195+ return FALSE;
196+ }
197+
198+ return TRUE;
199+}
200+
173201 /********************************************
174202 routines for debugging output
175203 ********************************************/
@@ -200,11 +228,20 @@ int PutCloseToMngDb(char* macAddress){
200228 if(debug>1) err_msg("DEBUG:(%d)<=putCloseToMngDb()",ret);
201229 return ret;
202230 }
203-int PutOpenToMngDb(char* macAddress, char* ipAddress){
231+
232+int PutOpenToMngDb(char* macAddress){
204233 int ret;
205- if(debug>1) err_msg("DEBUG:=>putOpenToMngDb(%s,%s)", macAddress, ipAddress);
206- ret = putOpenToMngDb(macAddress, ipAddress);
234+ if(debug>1) err_msg("DEBUG:=>putOpenToMngDb(%s)", macAddress);
235+ ret = putOpenToMngDb(macAddress);
207236 if(debug>1) err_msg("DEBUG:(%d)<=putOpenToMngDb()",ret);
208237 return ret;
209238 }
210239
240+int PutMacIpPairToMngDb(char* macAddress, char* ipAddress){
241+ int ret;
242+ if(debug>1) err_msg("DEBUG:=>putMacIpPairToMngDb(%s,%s)",
243+ macAddress, ipAddress);
244+ ret = putMacIpPairToMngDb(macAddress, ipAddress);
245+ if(debug>1) err_msg("DEBUG:(%d)<=putMacIpPairToMngDb()",ret);
246+ return ret;
247+}
--- a/mdsrc/opengatemd.c
+++ b/mdsrc/opengatemd.c
@@ -29,7 +29,7 @@ to maintain the state of each terminal.
2929 1. Packet Check Cache
3030 To speed up the packet check process, the address checked once
3131 is ignored for a while.
32- The cache (key: MAC address) is used to decide
32+ The cache (key: MAC&IP address pair) is used to decide
3333 the necessity of checking.
3434 The cache is maintained the information of arrived packets
3535 by using hash-table and queue in the memory of local machine.
@@ -47,7 +47,8 @@ to maintain the state of each terminal.
4747 (key: MAC address) is maintained in the memory of local machine.
4848
4949 MAC address is used as main key for the user terminal.
50-IP address is kept only for information (firstly detected address only)
50+IP address is kept only for information.
51+The packet cache uses the MAC-IP pair, as to record all IP.
5152 *************************************/
5253
5354 #include "opengatemd.h"
@@ -66,10 +67,10 @@ int main(int argc, char **argv)
6667 {
6768 char ipAddress[ADDRMAXLN]; /* packet source ip address */
6869 char macAddress[ADDRMAXLN]; /* packet source mac address */
69- unsigned char macAddressRaw[ADDRMAXLN];/* mac addr in raw form */
70- unsigned char ipAddressRaw[ADDRMAXLN];/* ip addr in raw form */
71- /* above is network raw binary, MAC(6bytes) and IP(4or16Bytes) */
72- int ipAddrLen; /* ip address byte length 4 or 16 */
70+ unsigned char macAndIpAddressRaw[ADDRMAXLN];/* mac&ip addr in raw form */
71+ /* above is network raw binary concatenating MAC(6bytes) and IP(4or16Bytes) */
72+ int addrLen; /* ip address byte length 6+4 or 6+16 */
73+
7374 char userId[USERMAXLN]; /* user id related to the mac address */
7475 char extraId[USERMAXLN]; /* optional id for the user */
7576 int macFound; /* flag: mac address is resistered in db */
@@ -80,6 +81,7 @@ int main(int argc, char **argv)
8081 int stopServiceMode=FALSE; /* flag: start with stop service option */
8182 int showVersionMode=FALSE; /* flag: show version */
8283 int helpMode=FALSE; /* flag: start with help mode */
84+
8385 int ttl; /* packet ttl(time to live) or hlim(hop limit) */
8486 int i; /* for loop control */
8587 int uselessCheckTime=0; /* the last time for useless check */
@@ -181,14 +183,14 @@ int main(int argc, char **argv)
181183 sigIoArrived=FALSE;
182184 while(GetDataFromUdpPort(macAddrInUdp, ADDRMAXLN, clientIpAddress)>0){
183185 if(IsUdpClientTrusted(clientIpAddress)){
184- DelCacheItem(macAddrInUdp);
186+ DelCacheItem(macAddrInUdp,"");
185187 DelMacCacheItem(macAddrInUdp);
186188 }
187189 }
188190 }
189191
190192 /* get one packet from pcap */
191- ret=GetNextPacketFromPcap(macAddressRaw, ipAddressRaw, &ipAddrLen, &ttl);
193+ ret=GetNextPacketFromPcap(macAndIpAddressRaw, &addrLen, &ttl);
192194
193195 /* if no packet */
194196 if(ret==0){
@@ -211,13 +213,14 @@ int main(int argc, char **argv)
211213 if(ttl<=1) continue;
212214
213215 /* ignore the packet checked recently */
214- if( IsRecentlyCheckedAddress(macAddressRaw) ) continue;
216+ if( IsRecentlyCheckedAddress(macAndIpAddressRaw, addrLen) ) continue;
215217
216218 /**** only cache timeout packets proceeds to below ****/
217219
218220 /* convert address from network-raw form to presentation form */
219- ConvertMacFromRawToDisplay(macAddressRaw,macAddress);
220- ConvertIpFromRawToDisplay(ipAddressRaw,ipAddrLen,ipAddress);
221+ ConvertMacFromRawToDisplay(macAndIpAddressRaw, macAddress);
222+ ConvertIpFromRawToDisplay(macAndIpAddressRaw+MACADDRLN,
223+ addrLen-MACADDRLN, ipAddress);
221224
222225 /* check nat/router and save info to db */
223226 isNatOrRouter=IsSentViaNatOrRouter(ipAddress, macAddress, ttl);
@@ -225,25 +228,31 @@ int main(int argc, char **argv)
225228 PutMacInfoToWorkDb(macAddress, ttl, isNatOrRouter);
226229
227230 /*** get the status of the terminal from session table and DB ***/
231+
228232 /* search the address in session table */
229233 sessionFound = IsMatchedSessionFound(macAddress);
230234
231- /* search the address in MAC DB cache */
235+ /* search the address in cache of MAC DB */
232236 macFound = QueryMacFromMacCache(macAddress, userId, extraId);
233237
234- /* if not found in MAC DB cache, search DB and add it to cache */
238+ /* if not found in MAC DB cache, access MAC DB and add result to cache */
235239 if(!macFound){
236240 macFound = QueryMacFromMngDb(macAddress, userId, extraId);
237241 if(macFound) AddMacCacheItem(macAddress, userId, extraId);
238242 }
239243
240244 /*** depending the states, add/del/renew the session ***/
245+
241246 /* if valid mac and no session, start session */
242247 if(macFound && !sessionFound){
243- AddSession(macAddress, ipAddress, userId, extraId);
248+ AddSession(macAddress, userId, extraId);
249+
250+ /* save MAC and IP address pair */
251+ SetMacIpPair(macAddress, ipAddress, userId, extraId);
244252 }
245253
246254 /* if no mac and started session, stop session */
255+ /* (MAC and IP pairs are removed in stop session) */
247256 if(!macFound && sessionFound){
248257 DelSession(macAddress);
249258 }
@@ -257,11 +266,16 @@ int main(int argc, char **argv)
257266 /* when no ipfw rule exists, reset the session */
258267 else{
259268 DelSession(macAddress);
260- AddSession(macAddress, ipAddress, userId, extraId);
269+ AddSession(macAddress, userId, extraId);
261270 }
271+
272+ /* save MAC and IP address pair */
273+ /* only when new pair is found. */
274+ SetMacIpPair(macAddress, ipAddress, userId, extraId);
262275 }
263276
264277 /* check useless sessions at some interval */
278+ /* (MAC and IP pairs are removed in stop session) */
265279 if( time(NULL) - uselessCheckTime > checkInterval ){
266280 uselessCheckTime = time(NULL);
267281 DelUselessSessions();
--- a/mdsrc/opengatemd.h
+++ b/mdsrc/opengatemd.h
@@ -154,7 +154,7 @@ char *GetNextConfValue(void);
154154
155155 /* pcap.c */
156156 int InitPcap(void);
157-int GetNextPacketFromPcap(unsigned char* macAddressRaw, unsigned char* ipAddressRaw, int* pIpAddrLen, int* pTtl);
157+int GetNextPacketFromPcap(unsigned char* macAndIpAddressRaw, int* pAddrLen, int* pTtl);
158158 void ClosePcap(void);
159159 int GetMyMacAddress(char* macAddress);
160160 void ConvertIpFromRawToDisplay(unsigned char* ipAddressRaw, int ipAddrLen, char* ipAddress);
@@ -162,9 +162,9 @@ void ConvertMacFromRawToDisplay(unsigned char* macAddressRaw, char* macAddress);
162162
163163 /* packetcache.c */
164164 void InitCache(void);
165-int IsRecentlyCheckedAddress(unsigned char* macAddressRaw);
165+int IsRecentlyCheckedAddress(unsigned char* macAndIpAddressRaw, int addrLen);
166166 void FreeCache(void);
167-int DelCacheItem(char* macAddress);
167+int DelCacheItem(char* macAddress, char* ipAddress);
168168 int DelOldestCacheItem(void);
169169 int ReFormatMacAddr(char* macAddr);
170170
@@ -173,27 +173,30 @@ int InitMngDb(void);
173173 int QueryMacFromMngDb(char* macAddress, char* userid, char* extraid);
174174 void CloseMngDb(void);
175175 int PutCloseToMngDb(char* macAddress);
176-int PutOpenToMngDb(char* macAddress, char* ipAddress);
176+int PutOpenToMngDb(char* macAddress);
177+int PutMacIpPairToMngDb(char* macAddress, char* ipAddress);
177178
178179 /* workdb.c */
179180 int SetupSqliteBusyTimeoutValue(void);
180181 int InitWorkDb(void);
181182 int FinalizeWorkDb(void);
182183 int InsertSessionToWorkDb(char* macAddress, char* userId, char* extraId,
183- char* ipAddress, int ruleNumber);
184+ int ruleNumber);
184185 int DelSessionFromWorkDb(char* macAddress);
185186 int GetSessionFromWorkDb(char* macAddress, char* userId, char* extraId,
186- int* openTime, int* checkTime, char *ipAddress,
187- int* ruleNumber);
187+ int* openTime, int* checkTime, int* ruleNumber);
188188 int UpdateCheckTimeInWorkDb(char* macAddress);
189189 int DelUselessSessionsInWorkDb(int delayed);
190190 int GetSessionTableFromWorkDb(DB* sessionTable);
191191 int PutMacInfoToWorkDb(char* macAddress, int ttl, int isNat);
192192 int GetMacInfoFromWorkDb(char* macAddress, char* detectTimeStr, int* pTtl);
193193 int IsActiveRuleInWorkDb(int ruleNumber);
194+int IsFoundMacIpPairInWorkDb(char* macAddress, char* ipAddress);
195+int PutMacIpPairToWorkDb(char* macAddress, char* ipAddress);
196+int DelMacIpPairsInWorkDb(char* macAddress);
194197
195198 /* session.c */
196-int AddSession(char* macAddress, char* ipAddress, char* userId, char* extraId);
199+int AddSession(char* macAddress, char* userId, char* extraId);
197200 void DelSession(char* macAddress);
198201 void RenewSession(char* macAddress);
199202 void DelUselessSessions(void);
@@ -201,12 +204,14 @@ void DelAllSessions(void);
201204 int CloseSession(void* pParam, int argc, char *argv[], char* colName[]);
202205 int IsMatchedSessionFound(char* macAddress);
203206 void CloseUnmatchSessions(void);
204-void WriteOpenToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress);
205-void WriteCloseToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress, int openTime);
206-void WriteSessionInfoToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress, int ruleNumber);
207+void WriteOpenToSyslog(char* userId, char* extraId, char* macAddress);
208+void WriteCloseToSyslog(char* userId, char* extraId, char* macAddress, int openTime);
209+void WriteSessionInfoToSyslog(char* userId, char* extraId, char* macAddress, int ruleNumber);
207210 void RemoveSessionUnmatchedToIpfwRule(DB* ruleTable, DB* sessionTable);
208211 void RemoveIpfwRuleUnmatchedToSession(DB* ruleTable, DB* sessionTable);
209212 int IsProcessFoundForTheRule(int ruleNumber);
213+void SetMacIpPair(char* macAddress, char* ipAddress, char* userId, char* extraId);
214+void ResetMacIpPairs(char* macAddress);
210215
211216 /* ttlcheck.c */
212217 int InitTtlCheck(void);
--- a/mdsrc/packetcache.c
+++ b/mdsrc/packetcache.c
@@ -1,19 +1,19 @@
11 /**************************************************
2-OpengateM - a MAC address authentication system
3- module to control packet check cache
2+opengate Mac addr auth program
43
4+ module to control cache of mac and ip address pair
55 to skip checking every packets.
6- All detected mac addresses are cached.
6+ All detected address pair (allowable or not) are cached.
77
88 As checking packet is time consuming procedure,
99 the recently checked addresses are cached and skiped.
1010 Implemented with HashTable and Queue.
1111 HashTable:
12- Key= MAC Addresses
12+ Key= comcatenation of MAC and IP Addresses
1313 Val= checked time
14- If address is included in table and time is new, skip checking.
14+ If address pair is included in table and time is new, skip checking.
1515 Queue:
16- Address odrered by checked time.
16+ Address pair odrered by checked time.
1717 If an old item is found in table, elder items are removed from table.
1818 The queue controls the remove sequence.
1919
@@ -46,40 +46,39 @@ int DeQueueForCache(unsigned char* addrRaw, int* pAddrLen);
4646 int ListQueueForCache(void);
4747 void FreeQueueForCache(void);
4848
49-/* Queue to store MacAddress in time order */
49+/* Queue to store MacAndIpAddress */
5050 struct queueNode{
5151 int addrLen;
52- unsigned char addrRaw[MACADDRLN];
52+ unsigned char addrRaw[MACADDRLN+IPV6ADDRLN];
5353 struct queueNode *next;
5454 };
5555 static struct queueNode* queueTail=NULL;
5656 static struct queueNode* queueHead=NULL;
5757
58-/* HashTable key=MacAddress and value=DetectTime */
58+/* HashTable to store MacAndIpAddress and Time */
5959 static DB* hashDb;
6060
6161 /* Cache Timeout(seconds) : packet checking interval */
6262 static int cacheTimeout;
6363
64-/* number of items in the cache */
6564 static int cacheItemCount=0;
6665
6766 /**********************************
6867 This cache is made from HashTable and Queue.
69-HashTable for quick access, and Queue for ordering.
68+HashTabel for quick access, and Queue for ordering.
7069 Same data are stored in the two data structures.
7170 If you add/delete items in cache, treat both structures.
7271 Don't add/delete items from one structure only.
7372 ***********************************/
7473
7574 /****************************************
76-Is the macAddress checked recently or not
77- input=macAddress return TRUE if checked recently
75+Is the IpAddress checked recently or not
76+ input=ipAddress return TRUE if checked recently
7877 ****************************************/
79-int isRecentlyCheckedAddress(unsigned char* macAddressRaw){
78+int isRecentlyCheckedAddress(unsigned char* macAndIpAddressRaw, int addrLen){
8079
8180 int timeNow;
82- unsigned char storedAddrRaw[ADDRMAXLN];
81+ unsigned char storedAddrRaw[MACADDRLN+IPV6ADDRLN];
8382 int storedAddrLen;
8483 int ret;
8584 int* pTime;
@@ -89,9 +88,9 @@ int isRecentlyCheckedAddress(unsigned char* macAddressRaw){
8988 /* get present time */
9089 timeNow=time(NULL);
9190
92- /***** get item matched to the mac from hash table */
93- hashKey.data = macAddressRaw;
94- hashKey.size = MACADDRLN;
91+ /***** get item matched to the mac&ip from hash table */
92+ hashKey.data = macAndIpAddressRaw;
93+ hashKey.size = addrLen;
9594 memset(&hashVal, 0, sizeof(DBT));
9695 ret=hashDb->get(hashDb, &hashKey, &hashVal, 0);
9796
@@ -117,10 +116,10 @@ int isRecentlyCheckedAddress(unsigned char* macAddressRaw){
117116 hashKey.data=storedAddrRaw;
118117 hashKey.size = storedAddrLen;
119118 hashDb->del(hashDb, &hashKey, 0);
120- if(memcmp(macAddressRaw,storedAddrRaw,storedAddrLen)==0)break;
119+ if(memcmp(macAndIpAddressRaw,storedAddrRaw,storedAddrLen)==0)break;
121120 }
122121
123- /* insert last item after renewing the time */
122+ /* insert update item to queue and hashTable */
124123 EnQueueForCache(storedAddrRaw, storedAddrLen);
125124 hashVal.data = &timeNow;
126125 hashVal.size = sizeof(int);
@@ -145,7 +144,7 @@ int isRecentlyCheckedAddress(unsigned char* macAddressRaw){
145144 terminateProg(0);
146145 }
147146 /* insert to queue */
148- EnQueueForCache(macAddressRaw, MACADDRLN);
147+ EnQueueForCache(macAndIpAddressRaw, addrLen);
149148 /*************** end adding item to Cache ***/
150149
151150 /* if cache size is over, remove oldest one */
@@ -159,8 +158,8 @@ int isRecentlyCheckedAddress(unsigned char* macAddressRaw){
159158 }
160159
161160 /****************************************
162-initialize packet check Cache
163- The Cache is formed with in HashTable and Queue
161+initialize Mac&IpAddress Cache
162+ Mac&Ip Cache is formed with in HashTable and Queue
164163 HashTable=to search an item quickly
165164 Queue =to list items in FIFO order
166165 ****************************************/
@@ -180,7 +179,7 @@ void initCache(void) {
180179 }
181180
182181 /****************************************
183-memory free for packet check Cache
182+memory free for Mac&IpAddress Cache
184183 ****************************************/
185184 void freeCache(void) {
186185
@@ -190,24 +189,67 @@ void freeCache(void) {
190189
191190
192191 /****************************************
193-delete item from packet check cache matched to the mac address
192+delete item from MacAndIp cache matched to the mac or/and ip address
193+set arguments for mac and/or ip to detele
194+set argument as "", if the address is not known
195+ eg: to delete item for an ipv4 and unknown mac, ("", "192.168.0.100")
194196 ****************************************/
195-int delCacheItem(char* macAddress) {
197+int delCacheItem(char* macAddress, char* ipAddress) {
196198 int found=FALSE;
197199 DBT hashKey;
198- unsigned char macAddressRaw[MACADDRLN];
200+ unsigned char addrRaw[MACADDRLN+IPV6ADDRLN];
199201 unsigned char* pRawMac;
200202 struct queueNode *temp;
201203 struct queueNode *prev;
204+ int inMac=FALSE;
205+ int inIpv4=FALSE;
206+ int inIpv6=FALSE;
207+ int shift=0;
208+ int length=0;
202209
203210 /*** set the raw values */
204- /* if mac is null, return fail */
205- if(isNull(macAddress)) return FALSE;
211+ /* if mac is not null, set the mac raw value */
212+ if(!isNull(macAddress)) {
213+ if((pRawMac=(unsigned char*)ether_aton(macAddress)) != NULL){
214+ memcpy(addrRaw, pRawMac, MACADDRLN);
215+ inMac=TRUE;
216+ }
217+ }
206218
207- /* convert mac to raw form */
208- if((pRawMac=(unsigned char*)ether_aton(macAddress)) != NULL){
209- memcpy(macAddressRaw, pRawMac, MACADDRLN);
210- }else return FALSE;
219+ /* if ip is not null, set the ipv6/ipv4 raw value */
220+ if(!isNull(ipAddress)) {
221+ if(strchr(ipAddress, ':') != NULL) {
222+ if(inet_pton(AF_INET6, ipAddress, addrRaw+MACADDRLN) > 0) inIpv6=TRUE;
223+ }
224+ else{
225+ if(inet_pton(AF_INET, ipAddress, addrRaw+MACADDRLN) > 0) inIpv4=TRUE;
226+ }
227+ }
228+
229+ /*** set start point and length for comparing string */
230+ if(inMac && !inIpv6 && !inIpv4){ /* only mac is indivcated */
231+ shift=0;
232+ length=MACADDRLN;
233+ }
234+ else if(!inMac && inIpv6){ /* only ipv6 is indicated */
235+ shift=MACADDRLN;
236+ length=IPV6ADDRLN;
237+ }
238+ else if(!inMac && inIpv4){ /* only ipv4 is indicated */
239+ shift=MACADDRLN;
240+ length=IPV4ADDRLN;
241+ }
242+ else if(inMac && inIpv6){ /* mac and ipv6 is indicated */
243+ shift=0;
244+ length=MACADDRLN+IPV6ADDRLN;
245+ }
246+ else if(inMac && inIpv4){ /* mac and ipv4 is indicated */
247+ shift=0;
248+ length=MACADDRLN+IPV4ADDRLN;
249+ }
250+ else{ /* no one is indicated */
251+ return FALSE;
252+ }
211253
212254 /*** scan queue to find matched address */
213255 /* set search point to the head of mac-ip cache */
@@ -219,7 +261,7 @@ int delCacheItem(char* macAddress) {
219261 while(temp->next!=NULL){
220262
221263 /* compare indicated value and queue value */
222- if(memcmp(macAddressRaw, (temp->addrRaw), MACADDRLN)==0){
264+ if(memcmp(addrRaw+shift, (temp->addrRaw)+shift, length)==0){
223265
224266 /* set found flag */
225267 found=TRUE;
@@ -243,11 +285,11 @@ int delCacheItem(char* macAddress) {
243285 }
244286
245287 /****************************************
246-delete oldest item from packet cache
288+delete oldest item from MacAndIp cache
247289 ****************************************/
248290 int delOldestCacheItem(void) {
249291 DBT hashKey;
250- unsigned char addrRaw[ADDRMAXLN];
292+ unsigned char addrRaw[MACADDRLN+IPV6ADDRLN];
251293 int addrLen=0;
252294
253295 /* delete oldest item(=head) from queue */
@@ -265,16 +307,15 @@ int delOldestCacheItem(void) {
265307 }
266308
267309 /*********************************************
268-initialize Queue
310+initialize MacAndIpAddress Queue
269311 Queue
270312 HeadNode - DataNode - DataNode - TailNode
271313 (dummy) (dummy)
272314 ^queueHead ^queueTail
273-* this queue is internal struct of packet cache
274315 *********************************************/
275316 int initQueueForCache(void){
276317
277- unsigned char addrRaw[MACADDRLN];
318+ unsigned char addrRaw[MACADDRLN+IPV6ADDRLN];
278319 int addrLen;
279320
280321 /* if not exist, prepare head and tail */
@@ -289,9 +330,9 @@ int initQueueForCache(void){
289330 err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
290331 terminateProg(0);
291332 }
292- bzero(queueHead->addrRaw, MACADDRLN);
333+ bzero(queueHead->addrRaw, MACADDRLN+IPV6ADDRLN);
293334 queueHead->addrLen=0;
294- bzero(queueTail->addrRaw, MACADDRLN);
335+ bzero(queueTail->addrRaw, MACADDRLN+IPV6ADDRLN);
295336 queueTail->addrLen=0;
296337 queueHead->next=queueTail;
297338 queueTail->next=NULL;
@@ -310,7 +351,7 @@ int initQueueForCache(void){
310351 }
311352
312353 /****************************************
313-Add data to the tail of Queue
354+Add data to the tail of MacAndIP Queue
314355 input=addr
315356 ****************************************/
316357 int enQueueForCache(unsigned char* addrRaw, int addrLen){
@@ -322,12 +363,6 @@ int enQueueForCache(unsigned char* addrRaw, int addrLen){
322363 return FALSE;
323364 }
324365
325- /* check length */
326- if(addrLen>MACADDRLN){
327- err_msg("ERR at %s#%d: queue data is too long",__FILE__,__LINE__);
328- return FALSE;
329- }
330-
331366 /* add item after the tail and set it as new tail*/
332367 newNode=(struct queueNode*)malloc(sizeof(struct queueNode));
333368 if(newNode==NULL){
@@ -338,7 +373,7 @@ int enQueueForCache(unsigned char* addrRaw, int addrLen){
338373 queueTail->addrLen=addrLen;
339374 queueTail->next=newNode;
340375 queueTail=newNode;
341- bzero(queueTail->addrRaw,MACADDRLN);
376+ bzero(queueTail->addrRaw,MACADDRLN+IPV6ADDRLN);
342377 queueTail->addrLen=0;
343378 queueTail->next=NULL;
344379
@@ -349,15 +384,15 @@ int enQueueForCache(unsigned char* addrRaw, int addrLen){
349384 }
350385
351386 /****************************************
352-Get and remove address data from the head of Queue
387+Get and remove address data from the head of MacAndIP Queue
353388 output
354- addrRaw:binary string of Mac (length=MACADDRLN)
389+ addrRaw:binary string of Mac&Ip (length=MACADDRLN+IPV6ADDRLN)
355390 pAddrLen: pointer to the aquired string length
356391 ****************************************/
357392 int deQueueForCache(unsigned char* addrRaw, int* pAddrLen){
358393
359394 /* set null string as default */
360- bzero(addrRaw, MACADDRLN);
395+ bzero(addrRaw, MACADDRLN+IPV6ADDRLN);
361396
362397 /* if not prepared, error */
363398 if(queueHead==NULL){
@@ -391,7 +426,7 @@ int deQueueForCache(unsigned char* addrRaw, int* pAddrLen){
391426 }
392427
393428 /****************************************
394-Listing Queue (for debugging)
429+Listing MacAndIpAddress Queue (for debugging)
395430 ****************************************/
396431 int listQueueForCache(void){
397432
@@ -413,10 +448,10 @@ int listQueueForCache(void){
413448 }
414449
415450 /****************************************
416-memory free for Queue
451+memory free for MacAndIpAddress Queue
417452 ****************************************/
418453 void freeQueueForCache(void){
419- unsigned char addrRaw[MACADDRLN];
454+ unsigned char addrRaw[MACADDRLN+IPV6ADDRLN];
420455 int addrLen;
421456 while(DeQueueForCache(addrRaw,&addrLen));
422457 free(queueHead);
@@ -456,18 +491,18 @@ void FreeCache(void) {
456491 if(debug>1) err_msg("DEBUG:<=freeCache()");
457492 }
458493
459-int IsRecentlyCheckedAddress(unsigned char* macAddressRaw){
494+int IsRecentlyCheckedAddress(unsigned char* macAndIpAddressRaw, int addrLen){
460495 int ret;
461- if(debug>1) err_msg("DEBUG:=>isRecentlyCheckedAddress(%x)", macAddressRaw[0]);
462- ret = isRecentlyCheckedAddress(macAddressRaw);
496+ if(debug>1) err_msg("DEBUG:=>isRecentlyCheckedAddress(%x,%d)", macAndIpAddressRaw[0],addrLen);
497+ ret = isRecentlyCheckedAddress(macAndIpAddressRaw, addrLen);
463498 if(debug>1) err_msg("DEBUG:(%d)<=isRecentlyCheckedAddress( )",ret);
464499 return ret;
465500 }
466501
467-int DelCacheItem(char* macAddress) {
502+int DelCacheItem(char* macAddress, char* ipAddress) {
468503 int ret;
469- if(debug>1) err_msg("DEBUG:=>delCacheItem(%s)", macAddress);
470- ret = delCacheItem(macAddress);
504+ if(debug>1) err_msg("DEBUG:=>delCacheItem(%s,5s)", macAddress,ipAddress);
505+ ret = delCacheItem(macAddress,ipAddress);
471506 if(debug>1) err_msg("DEBUG:(%d)<=delCacheItem( )",ret);
472507 return ret;
473508 }
--- a/mdsrc/pcap.c
+++ b/mdsrc/pcap.c
@@ -85,7 +85,7 @@ int initPcap(void){
8585 get next packet from pcap
8686 ret=0:no packet,1:ip packet,-1:other packet
8787 *******************************/
88-int getNextPacketFromPcap(unsigned char* macAddressRaw, unsigned char* ipAddressRaw, int* pIpAddrLen, int* pTtl){
88+int getNextPacketFromPcap(unsigned char* macAndIpAddressRaw, int* pAddrLen, int* pTtl){
8989 struct pcap_pkthdr header; /* The header captured by pcap */
9090 const u_char *packet; /* The actual packet */
9191 struct ip *ipv4h;
@@ -94,10 +94,9 @@ int getNextPacketFromPcap(unsigned char* macAddressRaw, unsigned char* ipAddress
9494 int ret=0;
9595
9696 /* initialize to null string */
97- bzero(macAddressRaw,MACADDRLN);
98- bzero(ipAddressRaw,IPV6ADDRLN);
97+ bzero(macAndIpAddressRaw,MACADDRLN+IPV6ADDRLN);
9998 *pTtl=0;
100- *pIpAddrLen=0;
99+ *pAddrLen=0;
101100
102101 /* Grab a packet */
103102 if((packet = pcap_next(handle, &header)) == NULL)return 0; /* 0=no packet*/
@@ -109,22 +108,22 @@ int getNextPacketFromPcap(unsigned char* macAddressRaw, unsigned char* ipAddress
109108
110109 /* get mac address from ether header */
111110 ethhdr = (struct ether_header *)packet;
112- memcpy(macAddressRaw, ethhdr->ether_shost, MACADDRLN);
111+ memcpy(macAndIpAddressRaw, ethhdr->ether_shost, MACADDRLN);
113112
114113 /* get ip address from ip header */
115114 switch (ntohs(ethhdr->ether_type)) {
116115 case ETHERTYPE_IP:
117- *pIpAddrLen=IPV4ADDRLN;
116+ *pAddrLen=IPV4ADDRLN+MACADDRLN;
118117 ipv4h = (struct ip *)(packet + sizeof(struct ether_header));
119- memcpy( ipAddressRaw, &ipv4h->ip_src, IPV4ADDRLN);
118+ memcpy( macAndIpAddressRaw+MACADDRLN, &ipv4h->ip_src, IPV4ADDRLN);
120119 *pTtl = ipv4h->ip_ttl;
121120 ret=1; /* 1=success */
122121 break;
123122
124123 case ETHERTYPE_IPV6:
125- *pIpAddrLen=IPV6ADDRLN;
124+ *pAddrLen=IPV6ADDRLN+MACADDRLN;
126125 ipv6h = (struct ip6_hdr *)(packet + sizeof(struct ether_header));
127- memcpy(ipAddressRaw, &ipv6h->ip6_src, IPV6ADDRLN);
126+ memcpy(macAndIpAddressRaw+MACADDRLN, &ipv6h->ip6_src, IPV6ADDRLN);
128127 *pTtl = ipv6h->ip6_ctlun.ip6_un1.ip6_un1_hlim;
129128 ret=1; /* 1=success */
130129 break;
@@ -234,11 +233,12 @@ int InitPcap(void){
234233 if(debug>1) err_msg("DEBUG:(%d)<=initPcap( )",ret);
235234 return ret;
236235 }
237-int GetNextPacketFromPcap(unsigned char* macAddressRaw, unsigned char* ipAddressRaw, int* pIpAddrLen, int* pTtl){
236+
237+int GetNextPacketFromPcap(unsigned char* macAndIpAddressRaw, int* pAddrLen, int* pTtl){
238238 int ret;
239239 if(debug>2) err_msg("DEBUG:=>getNextPacketFromPcap( )");
240- ret = getNextPacketFromPcap(macAddressRaw, ipAddressRaw, pIpAddrLen, pTtl);
241- if(debug>2) err_msg("DEBUG:(%d)<=getNextPacketFromPcap(%x,%x,%d,%d)",ret,macAddressRaw[0],ipAddressRaw,*pIpAddrLen,*pTtl);
240+ ret = getNextPacketFromPcap(macAndIpAddressRaw, pAddrLen, pTtl);
241+ if(debug>2) err_msg("DEBUG:(%d)<=getNextPacketFromPcap(%x,%d,%d)",ret,macAndIpAddressRaw[0],*pAddrLen,*pTtl);
242242 return ret;
243243 }
244244
--- a/mdsrc/session.c
+++ b/mdsrc/session.c
@@ -27,11 +27,10 @@ Email: watanaby@is.saga-u.ac.jp
2727 add session for the mac address
2828 add ipfw-rule and table-entry, write db and log
2929 *******************************/
30-int addSession(char* macAddress, char* ipAddress, char* userId, char* extraId){
30+int addSession(char* macAddress, char* userId, char* extraId){
3131 int ruleNumber;
3232 static int nSerialError=0; /* number of serial errors for addition */
3333 int failToAdd=FALSE;
34- char ipAddressInDb[ADDRMAXLN];
3534 int openTime;
3635 int checkTime;
3736 int ruleNumberInDb;
@@ -47,7 +46,7 @@ int addSession(char* macAddress, char* ipAddress, char* userId, char* extraId){
4746
4847 /* if mac is active session in work db, do nothing */
4948 if(GetSessionFromWorkDb(macAddress, userId, extraId, &openTime,
50- &checkTime, ipAddressInDb, &ruleNumberInDb)){
49+ &checkTime, &ruleNumberInDb)){
5150 return FALSE;
5251 }
5352 /* if not active session in work db, invert sign and goto write db */
@@ -60,7 +59,7 @@ int addSession(char* macAddress, char* ipAddress, char* userId, char* extraId){
6059 /* as to prevent error loop, retry count is limitted */
6160 if( failToAdd || !IsMacAddressFoundInIpfw(macAddress) ){
6261 nSerialError++;
63- if(nSerialError<1) DelCacheItem(macAddress);
62+ if(nSerialError<1) DelCacheItem(macAddress,"");
6463 err_msg("ERR at %s#%d: ipfw rule addition is failed(%d-%d)",
6564 __FILE__,__LINE__, failToAdd, nSerialError);
6665 return FALSE;
@@ -68,13 +67,13 @@ int addSession(char* macAddress, char* ipAddress, char* userId, char* extraId){
6867 nSerialError=0;
6968
7069 /* write to session db */
71- InsertSessionToWorkDb(macAddress, userId, extraId, ipAddress, ruleNumber);
70+ InsertSessionToWorkDb(macAddress, userId, extraId, ruleNumber);
7271
7372 /* write open log to syslog */
74- WriteOpenToSyslog(userId, extraId, ipAddress, macAddress);
73+ WriteOpenToSyslog(userId, extraId, macAddress);
7574
7675 /* write log to management db */
77- PutOpenToMngDb(macAddress, ipAddress);
76+ PutOpenToMngDb(macAddress);
7877 return TRUE;
7978 }
8079
@@ -85,7 +84,6 @@ delete session for the mac address
8584 void delSession(char* macAddress){
8685 char userId[USERMAXLN]="";
8786 char extraId[USERMAXLN]="";
88- char ipAddress[ADDRMAXLN]="";
8987 int openTime=0;
9088 int ruleNumber=0;
9189 int checkTime=0;
@@ -93,24 +91,27 @@ void delSession(char* macAddress){
9391
9492 /* get information from session table in work db */
9593 success=GetSessionFromWorkDb(macAddress, userId, extraId, &openTime,
96- &checkTime, ipAddress, &ruleNumber);
94+ &checkTime, &ruleNumber);
9795
9896 /* close firewall and refresh the cache */
9997 if(success) CloseClientGate(ruleNumber);
10098
10199 /* del from cache */
102- DelCacheItem(macAddress);
100+ DelCacheItem(macAddress,"");
103101
104102 /* del from session from session table in work db */
105103 DelSessionFromWorkDb(macAddress);
106104
107105 /* write close log to syslog */
108- WriteCloseToSyslog(userId, extraId, ipAddress, macAddress, openTime);
109- if(debug>0) WriteSessionInfoToSyslog(userId, extraId, ipAddress,
106+ WriteCloseToSyslog(userId, extraId, macAddress, openTime);
107+ if(debug>0) WriteSessionInfoToSyslog(userId, extraId,
110108 macAddress, ruleNumber);
111109
112110 /* write close log to management db */
113111 PutCloseToMngDb(macAddress);
112+
113+ /* reset the memory of MAC IP pair */
114+ ResetMacIpPairs(macAddress);
114115 }
115116
116117 /*******************************
@@ -146,7 +147,6 @@ int closeSession(void* pParam, int argc, char *argv[], char* colName[]){
146147 int ruleNumber;
147148 char* userId;
148149 char* extraId;
149- char* ipAddress;
150150 char* macAddress;
151151 int openTime;
152152
@@ -155,22 +155,23 @@ int closeSession(void* pParam, int argc, char *argv[], char* colName[]){
155155 ruleNumber=atoi(argv[0]);
156156 userId=argv[1];
157157 extraId=argv[2];
158- ipAddress=argv[3];
159- macAddress=argv[4];
160- openTime=atoi(argv[5]);
158+ macAddress=argv[3];
159+ openTime=atoi(argv[4]);
161160
162161 /* close firewall */
163162 CloseClientGate(ruleNumber);
164- DelCacheItem(macAddress);
163+ DelCacheItem(macAddress,"");
165164
166165 /* write close log to syslog */
167- WriteCloseToSyslog(userId, extraId, ipAddress, macAddress, openTime);
168- if(debug>0) WriteSessionInfoToSyslog(userId, extraId, ipAddress,
169- macAddress, ruleNumber);
166+ WriteCloseToSyslog(userId, extraId, macAddress, openTime);
167+ if(debug>0) WriteSessionInfoToSyslog(userId, extraId, macAddress, ruleNumber);
170168
171169 /* write close log to management db */
172170 PutCloseToMngDb(macAddress);
173171
172+ /* reset the memory of MAC IP pair */
173+ ResetMacIpPairs(macAddress);
174+
174175 return 0; /* SQLITE_OK */
175176 }
176177
@@ -195,13 +196,11 @@ int isMatchedSessionFound(char* macAddress){
195196 char extraId[USERMAXLN];
196197 int openTime;
197198 int checkTime;
198- char ipAddress[ADDRMAXLN];
199199 int ruleNumber;
200200
201201 /* get info for the macAddress */
202202 return GetSessionFromWorkDb(macAddress, userId, extraId,
203- &openTime, &checkTime, ipAddress,
204- &ruleNumber);
203+ &openTime, &checkTime, &ruleNumber);
205204 }
206205
207206 /************************************
@@ -289,7 +288,6 @@ void removeSessionUnmatchedToIpfwRule(DB* ruleTable, DB* sessionTable){
289288 int retRuleTbl;
290289 int retSesTbl;
291290 int ruleNumber;
292- char ipAddress[ADDRMAXLN];
293291 char userId[USERMAXLN];
294292 char extraId[USERMAXLN];
295293 char macAddress[ADDRMAXLN];
@@ -323,16 +321,16 @@ void removeSessionUnmatchedToIpfwRule(DB* ruleTable, DB* sessionTable){
323321 else{
324322 /* write log and close session */
325323 if(!GetSessionFromWorkDb(macAddress, userId, extraId,
326- &openTime, &checkTime,ipAddress, &ruleNumber)){
324+ &openTime, &checkTime, &ruleNumber)){
327325 err_msg("ERR at %s#%d: fail to get session info",__FILE__,__LINE__);
328326 }else{
329- WriteCloseToSyslog(userId, extraId, ipAddress, macAddress, openTime);
327+ WriteCloseToSyslog(userId, extraId, macAddress, openTime);
330328 PutCloseToMngDb(macAddress);
331329 DelSessionFromWorkDb(macAddress);
332330 }
333331
334332 /* write session info to syslog */
335- if(debug>0) WriteSessionInfoToSyslog(userId, extraId, ipAddress,
333+ if(debug>0) WriteSessionInfoToSyslog(userId, extraId,
336334 macAddress, ruleNumber);
337335 }
338336
@@ -381,14 +379,14 @@ void removeIpfwRuleUnmatchedToSession(DB* ruleTable, DB* sessionTable){
381379
382380 /* remove entry in ipfw rule */
383381 CloseClientGate(ruleNumber);
384- DelCacheItem(macAddress);
382+ DelCacheItem(macAddress,"");
385383
386384 /* write log */
387- WriteCloseToSyslog("?", "", "?", macAddress, time(NULL));
385+ WriteCloseToSyslog("?", "", macAddress, time(NULL));
388386 PutCloseToMngDb(macAddress);
389387
390388 /* write session info to syslog */
391- if(debug>0) WriteSessionInfoToSyslog("?","","?",macAddress, ruleNumber);
389+ if(debug>0) WriteSessionInfoToSyslog("?","", macAddress, ruleNumber);
392390 }
393391
394392 /* get next rule entry */
@@ -400,22 +398,21 @@ void removeIpfwRuleUnmatchedToSession(DB* ruleTable, DB* sessionTable){
400398 /******************************************
401399 write open message to syslog
402400 ******************************************/
403-void writeOpenToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress){
401+void writeOpenToSyslog(char* userId, char* extraId, char* macAddress){
404402
405403 if(extraId[0]=='\0'){
406- err_msg("OPEN: user %s from %s at %s",
407- userId, ipAddress, macAddress);
404+ err_msg("OPEN: user %s at %s",
405+ userId, macAddress);
408406 }else{
409- err_msg("OPEN: user %s%s%s from %s at %s",
410- userId, GetConfValue("UserIdSeparator"), extraId,
411- ipAddress, macAddress);
407+ err_msg("OPEN: user %s%s%s at %s",
408+ userId, GetConfValue("UserIdSeparator"), extraId, macAddress);
412409 }
413410 }
414411
415412 /******************************************
416413 write close message to syslog
417414 ******************************************/
418-void writeCloseToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress, int openTime){
415+void writeCloseToSyslog(char* userId, char* extraId, char* macAddress, int openTime){
419416
420417 double time_l;
421418 int hour, min, sec;
@@ -426,19 +423,19 @@ void writeCloseToSyslog(char* userId, char* extraId, char* ipAddress, char* macA
426423 sec=(time_l-hour*60*60-min*60);
427424
428425 if(extraId[0]=='\0'){
429- err_msg("CLOS: user %s from %s at %s ( %02d:%02d:%02d )",
430- userId, ipAddress, macAddress, hour,min,sec);
426+ err_msg("CLOS: user %s at %s ( %02d:%02d:%02d )",
427+ userId, macAddress, hour,min,sec);
431428 }else{
432- err_msg("CLOS: user %s%s%s from %s at %s ( %02d:%02d:%02d )",
429+ err_msg("CLOS: user %s%s%s at %s ( %02d:%02d:%02d )",
433430 userId, GetConfValue("UserIdSeparator"), extraId,
434- ipAddress, macAddress, hour,min,sec);
431+ macAddress, hour,min,sec);
435432 }
436433 }
437434
438435 /******************************************
439436 write session info message to syslog
440437 ******************************************/
441-void writeSessionInfoToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress, int ruleNumber){
438+void writeSessionInfoToSyslog(char* userId, char* extraId, char* macAddress, int ruleNumber){
442439
443440 char detectTimeStr[WORDMAXLN];
444441 int ttl;
@@ -447,27 +444,59 @@ void writeSessionInfoToSyslog(char* userId, char* extraId, char* ipAddress, char
447444 GetMacInfoFromWorkDb(macAddress, detectTimeStr, &ttl);
448445
449446 if(extraId[0]=='\0'){
450- err_msg("INFO: user=%s ipaddr=%s macaddr=%s "
447+ err_msg("INFO: user=%s macaddr=%s "
451448 "ipfwrule=%d ttl=%d lastcheck=%s",
452- userId, ipAddress, macAddress, ruleNumber, ttl, detectTimeStr);
449+ userId, macAddress, ruleNumber, ttl, detectTimeStr);
453450 }else{
454- err_msg("INFO: user=%s%s%s ipaddr=%s macaddr=%s "
451+ err_msg("INFO: user=%s%s%s macaddr=%s "
455452 "ipfwrule=%d ttl=%d lastcheck=%s",
456453 userId, GetConfValue("UserIdSeparator"), extraId,
457- ipAddress, macAddress, ruleNumber, ttl, detectTimeStr);
454+ macAddress, ruleNumber, ttl, detectTimeStr);
458455 }
459456 }
460457
458+/**********************************************
459+Memorize the MAC&IP address pair to DB and log
460+**********************************************/
461+void setMacIpPair(char* macAddress, char* ipAddress, char* userId, char* extraId){
462+
463+ /* if the pair is not found in work db, record it */
464+ if(!IsFoundMacIpPairInWorkDb(macAddress, ipAddress)){
465+ PutMacIpPairToWorkDb(macAddress, ipAddress);
466+
467+ /* record the pair to management db */
468+ PutMacIpPairToMngDb(macAddress, ipAddress);
469+
470+ /* write to syslog */
471+ if(isNull(extraId)){
472+ err_msg("FIND: user %s from %s at %s", userId, ipAddress, macAddress);
473+ }else{
474+ err_msg("FIND: user %s%s%s from %s at %s",
475+ userId, GetConfValue("UserIdSeparator"), extraId,
476+ ipAddress, macAddress);
477+ }
478+ }
479+}
480+
481+/**********************************
482+Reset the MAC&IP address pair in DB
483+***********************************/
484+void resetMacIpPairs(char* macAddress){
485+
486+ /* remove the all-pairs having the MAC from work db */
487+ DelMacIpPairsInWorkDb(macAddress);
488+}
489+
461490
462491 /**************************************************
463492 routines for debugging output
464493 *************************************************/
465494
466-int AddSession(char* macAddress, char* ipAddress, char* userId, char* extraId){
495+int AddSession(char* macAddress, char* userId, char* extraId){
467496 int ret;
468- if(debug>1) err_msg("DEBUG:=>addSession(%s,%s,%s,%s)",
469- macAddress, ipAddress, userId, extraId);
470- ret = addSession(macAddress, ipAddress, userId, extraId);
497+ if(debug>1) err_msg("DEBUG:=>addSession(%s,%s,%s)",
498+ macAddress, userId, extraId);
499+ ret = addSession(macAddress, userId, extraId);
471500 if(debug>1) err_msg("DEBUG:(%d)<=addSession( )",ret);
472501 return ret;
473502 }
@@ -513,21 +542,21 @@ void CloseUnmatchSessions(void){
513542 if(debug>1) err_msg("DEBUG:<=closeUnmatchSessions( )");
514543 }
515544
516-void WriteOpenToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress){
517- if(debug>1) err_msg("DEBUG:=>writeOpenToSyslog(%s,%s,%s,%s)", userId, extraId, ipAddress, macAddress);
518- writeOpenToSyslog(userId, extraId, ipAddress, macAddress);
545+void WriteOpenToSyslog(char* userId, char* extraId, char* macAddress){
546+ if(debug>1) err_msg("DEBUG:=>writeOpenToSyslog(%s,%s,%s)", userId, extraId, macAddress);
547+ writeOpenToSyslog(userId, extraId, macAddress);
519548 if(debug>1) err_msg("DEBUG:<=writeOpenToSyslog( )");
520549 }
521550
522-void WriteCloseToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress, int openTime){
523- if(debug>1) err_msg("DEBUG:=>writeCloseToSyslog(%s,%s,%s,%s,%d)", userId, extraId, ipAddress, macAddress, openTime);
524- writeCloseToSyslog(userId, extraId, ipAddress, macAddress, openTime);
551+void WriteCloseToSyslog(char* userId, char* extraId, char* macAddress, int openTime){
552+ if(debug>1) err_msg("DEBUG:=>writeCloseToSyslog(%s,%s,%s,%d)", userId, extraId, macAddress, openTime);
553+ writeCloseToSyslog(userId, extraId, macAddress, openTime);
525554 if(debug>1) err_msg("DEBUG:<=writeCloseToSyslog( )");
526555 }
527556
528-void WriteSessionInfoToSyslog(char* userId, char* extraId, char* ipAddress, char* macAddress, int ruleNumber){
529- if(debug>1) err_msg("DEBUG:=>writeSessionInfoToSyslog(%s,%s,%s,%s,%d)", userId, extraId, ipAddress, macAddress, ruleNumber);
530- writeSessionInfoToSyslog(userId, extraId, ipAddress, macAddress, ruleNumber);
557+void WriteSessionInfoToSyslog(char* userId, char* extraId, char* macAddress, int ruleNumber){
558+ if(debug>1) err_msg("DEBUG:=>writeSessionInfoToSyslog(%s,%s,%s,%d)", userId, extraId, macAddress, ruleNumber);
559+ writeSessionInfoToSyslog(userId, extraId, macAddress, ruleNumber);
531560 if(debug>1) err_msg("DEBUG:<=writeSessionInfoToSyslog( )");
532561 }
533562
@@ -545,6 +574,18 @@ void RemoveIpfwRuleUnmatchedToSession(DB* ruleTable, DB* sessionTable){
545574
546575 }
547576
577+void SetMacIpPair(char* macAddress, char* ipAddress, char* userId, char* extraId){
578+ if(debug>1) err_msg("DEBUG:=>setMacIpPair(%s,%s,%s,%s)",
579+ macAddress, ipAddress, userId, extraId);
580+ setMacIpPair(macAddress, ipAddress, userId, extraId);
581+ if(debug>1) err_msg("DEBUG:<=setMacIpPair( )");
582+}
583+
584+void ResetMacIpPairs(char* macAddress){
585+ if(debug>1) err_msg("DEBUG:=>resetMacIpPairs(%s)", macAddress);
586+ resetMacIpPairs(macAddress);
587+ if(debug>1) err_msg("DEBUG:<=resetMacIpPairs( )");
588+}
548589
549590 /********************testmain********************
550591 void testmain(){
--- a/mdsrc/workdb.c
+++ b/mdsrc/workdb.c
@@ -52,10 +52,13 @@ int initWorkDb(void){
5252 char *createCmd1="CREATE TABLE IF NOT EXISTS sessionmd "
5353 "(macAddress TEXT PRIMARY KEY, "
5454 "userId TEXT, extraId TEXT, openTime INTEGER, checkTime INTEGER, "
55- "ipAddress TEXT, ruleNumber INTEGER)";
55+ "ruleNumber INTEGER)";
5656 char *createCmd2="CREATE TABLE IF NOT EXISTS macinfo "
5757 "(macAddress TEXT PRIMARY KEY ON CONFLICT REPLACE, "
5858 "detectTime INTEGER, ttl INTEGER, isNat INTEGER)";
59+ char *createCmd3="CREATE TABLE IF NOT EXISTS macippair "
60+ "(macAddress TEXT, "
61+ "ipAddress TEXT, findTime INTEGER)";
5962
6063 /* setup static variable value for SqLite3_busy_timeout from conf */
6164 SetupSqliteBusyTimeoutValue();
@@ -79,6 +82,12 @@ int initWorkDb(void){
7982 terminateProg(0);
8083 }
8184
85+ /* create table3 */
86+ if(sqlite3_exec(dbMd, createCmd3, NULL, NULL, &pErrMsg)!=SQLITE_OK){
87+ err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
88+ terminateProg(0);
89+ }
90+
8291 return TRUE;
8392 }
8493
@@ -97,21 +106,21 @@ int finalizeWorkDb(void){
97106 insert session info to db at start
98107 ************************************************************/
99108 int insertSessionToWorkDb(char* macAddress, char* userId, char* extraId,
100- char* ipAddress, int ruleNumber){
109+ int ruleNumber){
101110
102111 int rc;
103112 char *pErrMsg=NULL;
104113
105114 /* SQL INSERT COMMAND, where %x is replaced in snprintf */
106115 char *insertFormat="INSERT INTO sessionmd "
107- "(macAddress, userId, extraId, openTime, checkTime, ipAddress, ruleNumber) "
108- "values ('%s','%s','%s', %d, %d,'%s', %d)";
116+ "(macAddress, userId, extraId, openTime, checkTime, ruleNumber) "
117+ "values ('%s','%s','%s', %d, %d, %d)";
109118 char *insertCmd;
110119 int resultFlag=TRUE;
111120
112121 /* Prepare insert command */
113122 insertCmd=sqlite3_mprintf(insertFormat, macAddress, userId,extraId,
114- time(NULL), time(NULL), ipAddress, ruleNumber);
123+ time(NULL), time(NULL), ruleNumber);
115124
116125 /* Execute insert to sqlite */
117126 if((rc=sqlite3_exec(dbMd, insertCmd, NULL, NULL, &pErrMsg))!=SQLITE_OK){
@@ -154,7 +163,7 @@ int updateCheckTimeInWorkDb(char* macAddress){
154163 }
155164
156165 /*************************************************************
157- delete session info to db at stop
166+ delete session info in db at stop
158167 *************************************************************/
159168 int delSessionFromWorkDb(char* macAddress){
160169
@@ -177,22 +186,22 @@ int delSessionFromWorkDb(char* macAddress){
177186 /*memory free for sqlite3 string */
178187 sqlite3_free(deleteCmd);
179188
180- return TRUE;
189+ return resultFlag;
181190 }
182191
183192 /************************************************
184- get info for active session about ip addr from db
193+ get info for active session about mac addr from db
185194 input = macAddress, output = others
186195 *************************************************/
187196 int getSessionFromWorkDb(char* macAddress, char* userId, char* extraId,
188- int* openTime, int* checkTime, char *ipAddress,
197+ int* openTime, int* checkTime,
189198 int* ruleNumber){
190199
191200 sqlite3_stmt *stmt;
192201
193202 /* SQL SELECT COMMAND, where %x is replaced in snprintf */
194203 char *selectFormat="SELECT userId, extraId, openTime, checkTime, "
195- "ipAddress, ruleNumber FROM sessionmd WHERE macAddress='%s'";
204+ "ruleNumber FROM sessionmd WHERE macAddress='%s'";
196205 char *selectCmd;
197206 int resultFlag=TRUE;
198207
@@ -217,15 +226,13 @@ int getSessionFromWorkDb(char* macAddress, char* userId, char* extraId,
217226 strncpy(extraId, (char*)sqlite3_column_text(stmt, 1), USERMAXLN);
218227 *openTime=(int)sqlite3_column_int(stmt, 2);
219228 *checkTime=(int)sqlite3_column_int(stmt, 3);
220- strncpy(ipAddress, (char*)sqlite3_column_text(stmt, 4), ADDRMAXLN);
221- *ruleNumber=(int)sqlite3_column_int(stmt, 5);
229+ *ruleNumber=(int)sqlite3_column_int(stmt, 4);
222230 resultFlag=TRUE;
223231 }else{
224232 userId[0]='\0';
225233 extraId[0]='\0';
226234 *openTime=0;
227235 *checkTime=0;
228- ipAddress[0]='\0';
229236 *ruleNumber=0;
230237 resultFlag=FALSE;
231238 }
@@ -256,7 +263,7 @@ int delUselessSessionsInWorkDb(int delayed){
256263 if(!delayed) uselessLimitTime = time(NULL);
257264
258265 /* SQL SELECT COMMAND, where %x is replaced in snprintf */
259- char *selectFormat="SELECT ruleNumber, userId, extraId, ipAddress, "
266+ char *selectFormat="SELECT ruleNumber, userId, extraId, "
260267 "macAddress, openTime FROM sessionmd WHERE checkTime<%d";
261268 char *deleteFormat="DELETE FROM sessionmd WHERE checkTime<%d";
262269 char *selectCmd;
@@ -451,7 +458,98 @@ int isActiveRuleInWorkDb(int ruleNumber){
451458 return resultFlag;
452459 }
453460
461+/********************************************
462+Is the MAC-IP pair found in work db
463+********************************************/
464+int isFoundMacIpPairInWorkDb(char* macAddress, char* ipAddress){
465+ sqlite3_stmt *stmt;
466+
467+ /* SQL SELECT COMMAND, where %x is replaced in snprintf */
468+ char *selectFormat="SELECT * FROM macippair "
469+ " WHERE macAddress='%s' AND ipAddress='%s'";
470+ char *selectCmd;
471+ int resultFlag=TRUE;
454472
473+ /* prepare command string */
474+ selectCmd=sqlite3_mprintf(selectFormat, macAddress, ipAddress);
475+
476+ /* compile to internal statement */
477+ if(sqlite3_prepare(dbMd, selectCmd, BUFFMAXLN, &stmt, NULL)!=SQLITE_OK){
478+ resultFlag=FALSE;
479+ err_msg("ERR at %s#%d: sqlite3_prepare",__FILE__,__LINE__);
480+
481+ /* finalize */
482+ sqlite3_free(selectCmd);
483+ sqlite3_finalize(stmt);
484+ return FALSE;
485+ }
486+
487+ /* get first record */
488+ if(sqlite3_step(stmt)==SQLITE_ROW) resultFlag=TRUE;
489+ else resultFlag=FALSE;
490+
491+ /* finalize */
492+ sqlite3_free(selectCmd);
493+ sqlite3_finalize(stmt);
494+ return resultFlag;
495+}
496+
497+
498+/********************************************
499+Insert MAC-IP pair to work db
500+********************************************/
501+int putMacIpPairToWorkDb(char* macAddress, char* ipAddress){
502+ int rc;
503+ char *pErrMsg=NULL;
504+
505+ /* SQL INSERT COMMAND, where %x is replaced in snprintf */
506+ char *insertFormat="INSERT INTO macippair "
507+ "(macAddress, ipAddress, findTime) "
508+ "values ('%s','%s', %d)";
509+ char *insertCmd;
510+ int resultFlag=TRUE;
511+
512+ /* Prepare insert command */
513+ insertCmd=sqlite3_mprintf(insertFormat, macAddress, ipAddress, time(NULL));
514+
515+ /* Execute insert to sqlite */
516+ if((rc=sqlite3_exec(dbMd, insertCmd, NULL, NULL, &pErrMsg))!=SQLITE_OK){
517+ resultFlag=FALSE;
518+ err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
519+ }
520+
521+ /*Memory free for sqlite3 string */
522+ sqlite3_free(insertCmd);
523+
524+ return resultFlag;
525+}
526+
527+/********************************************
528+Delete the mac-ip pairs in work db for the mac
529+********************************************/
530+int delMacIpPairsInWorkDb(char* macAddress){
531+ char *pErrMsg=NULL;
532+
533+ /* SQL DELETE COMMAND, where %x is replaced in mprintf */
534+ char *deleteFormat="DELETE FROM macippair WHERE macAddress='%s'";
535+ char *deleteCmd;
536+ int resultFlag=TRUE;
537+
538+ /* prepare command */
539+ deleteCmd=sqlite3_mprintf(deleteFormat, macAddress);
540+
541+ /* execute delete */
542+ if(sqlite3_exec(dbMd, deleteCmd, NULL, NULL, &pErrMsg)!=SQLITE_OK){
543+ resultFlag=FALSE;
544+ err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
545+ }
546+
547+ /*memory free for sqlite3 string */
548+ sqlite3_free(deleteCmd);
549+
550+ return resultFlag;
551+
552+}
455553
456554 /*********************************************************
457555 routines for debugging output
@@ -481,11 +579,11 @@ int FinalizeWorkDb(void){
481579 }
482580
483581 int InsertSessionToWorkDb(char* macAddress, char* userId, char* extraId,
484- char* ipAddress, int ruleNumber){
582+ int ruleNumber){
485583 int ret;
486- if(debug>1) err_msg("DEBUG:=>insertSessionToWorkDb(%s,%s,%s,%s,%d)",
487- macAddress, userId, extraId, ipAddress, ruleNumber);
488- ret = insertSessionToWorkDb(macAddress, userId, extraId, ipAddress, ruleNumber);
584+ if(debug>1) err_msg("DEBUG:=>insertSessionToWorkDb(%s,%s,%s,%d)",
585+ macAddress, userId, extraId, ruleNumber);
586+ ret = insertSessionToWorkDb(macAddress, userId, extraId, ruleNumber);
489587 if(debug>1) err_msg("DEBUG:(%d)<=insertSessionToWorkDb( )",ret);
490588 return ret;
491589 }
@@ -507,15 +605,13 @@ int DelSessionFromWorkDb(char* macAddress){
507605 }
508606
509607 int GetSessionFromWorkDb(char* macAddress, char* userId, char* extraId,
510- int* openTime, int* checkTime, char *ipAddress,
511- int* ruleNumber){
608+ int* openTime, int* checkTime, int* ruleNumber){
512609 int ret;
513610 if(debug>1) err_msg("DEBUG:=>getSessionFromWorkDb(%s)", macAddress);
514611 ret = getSessionFromWorkDb(macAddress, userId, extraId, openTime,
515- checkTime, ipAddress, ruleNumber);
516- if(debug>1) err_msg("DEBUG:(%d)<=getSessionFromWorkDb(,%s,%s,%d,%d,%s,%d)",
517- ret,userId,extraId,*openTime,*checkTime,ipAddress,
518- *ruleNumber);
612+ checkTime, ruleNumber);
613+ if(debug>1) err_msg("DEBUG:(%d)<=getSessionFromWorkDb(,%s,%s,%d,%d,%d)",
614+ ret,userId,extraId,*openTime,*checkTime, *ruleNumber);
519615 return ret;
520616 }
521617
@@ -559,3 +655,31 @@ int GetMacInfoFromWorkDb(char* macAddress, char* detectTimeStr, int* pTtl){
559655 ret, detectTimeStr, *pTtl);
560656 return ret;
561657 }
658+
659+int IsFoundMacIpPairInWorkDb(char* macAddress, char* ipAddress){
660+ int ret;
661+ if(debug>1) err_msg("DEBUG:=>isFoundMacIpPairInWorkDb(%s,%s)",
662+ macAddress,ipAddress);
663+ ret=isFoundMacIpPairInWorkDb(macAddress, ipAddress);
664+ if(debug>1) err_msg("DEBUG:(%d)<=isfoundMacIpPairInWorkDb( )", ret);
665+ return ret;
666+}
667+
668+int PutMacIpPairToWorkDb(char* macAddress, char* ipAddress){
669+ int ret;
670+ if(debug>1) err_msg("DEBUG:=>putMacIpPairToWorkDb(%s,%s)",
671+ macAddress,ipAddress);
672+ ret=putMacIpPairToWorkDb(macAddress, ipAddress);
673+ if(debug>1) err_msg("DEBUG:(%d)<=putMacIpPairtoWorkDb( )", ret);
674+ return ret;
675+}
676+
677+
678+int DelMacIpPairsInWorkDb(char* macAddress){
679+ int ret;
680+ if(debug>1) err_msg("DEBUG:=>delMacIpPairsInWorkDb(%s)",
681+ macAddress);
682+ ret=delMacIpPairsInWorkDb(macAddress);
683+ if(debug>1) err_msg("DEBUG:(%d)<=delMacIpPairsInWorkDb( )", ret);
684+ return ret;
685+}
--- a/sqlscript/createtablescript
+++ b/sqlscript/createtablescript
@@ -17,11 +17,15 @@ create table if not exists macaddrs(
1717
1818 create table if not exists sessionmd(
1919 macAddress CHAR(18),
20- ipAddress TINYTEXT,
2120 gatewayName TINYTEXT,
2221 openTime DATETIME,
2322 closeTime DATETIME);
2423
24+create table if not exists macippair(
25+ macAddress CHAR(18),
26+ ipAddress TINYTEXT,
27+ findTime DATETIME);
28+
2529 create table if not exists macmodify(
2630 userId TINYTEXT,
2731 extraId TINYTEXT,
@@ -40,8 +44,8 @@ create view sessionview as select
4044 device,
4145 openTime,
4246 closeTime,
43- gatewayName,
44- ipAddress
47+ gatewayName
4548 from macaddrs,sessionmd
4649 where macaddrs.macAddress=sessionmd.macAddress
4750 and entryDate<openTime and openTime<limitDate;
51+