1 /**************************************************
2 OpengateM - MAC address authentication system
3 module to control queue
5 As ip address check by database is time consuming procedure,
6 the recently checked addresses are cached and skiped.
7 Implemented with HashTable and Queue.
8 HashTable has recentry checked dataStress and checked time.
9 If ip is included in table and time is new, ignore checking.
10 Queue has dataStresses odrered by checked time.
11 If an old item is found in table, elder items are removed from table.
12 The queue controls the remove sequence.
15 Copyright (C) 2011 Opengate Project Team
16 Written by Yoshiaki Watanabe
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License
20 as published by the Free Software Foundation; either version 2
21 of the License, or (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 Email: watanaby@is.saga-u.ac.jp
33 **************************************************/
35 #include "opengatemmng.h"
37 /* Queue to store string-key stirng-data pair */
39 char keyStr[WORDMAXLN];
40 char dataStr[WORDMAXLN];
41 struct queueNode *next;
43 static struct queueNode* queueTail=NULL;
44 static struct queueNode* queueHead=NULL;
47 /*********************************************
48 initialize Address Queue
50 HeadNode - DataNode - DataNode - TailNode
53 *********************************************/
56 char keyStr[WORDMAXLN];
57 char dataStr[WORDMAXLN];
59 /* if not exist, prepare head and tail */
61 queueHead=(struct queueNode*)malloc(sizeof(struct queueNode));
63 err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
66 queueTail=(struct queueNode*)malloc(sizeof(struct queueNode));
68 err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
71 queueHead->keyStr[0]='\0';
72 queueHead->dataStr[0]='\0';
73 queueTail->keyStr[0]='\0';
74 queueTail->dataStr[0]='\0';
75 queueHead->next=queueTail;
79 /* if exist, reset all */
81 while(Dequeue(keyStr, dataStr))
87 /****************************************
88 Add data to the tail of Queue
90 ****************************************/
91 int enqueue(char* keyStr, char* dataStr){
92 struct queueNode *newNode;
94 /* if not prepared, error */
96 err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
100 /* add item after the tail and set it as new tail*/
101 newNode=(struct queueNode*)malloc(sizeof(struct queueNode));
103 err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
106 strncpy(queueTail->keyStr, keyStr, WORDMAXLN);
107 strncpy(queueTail->dataStr, dataStr, WORDMAXLN);
108 queueTail->next=newNode;
110 queueTail->keyStr[0]='\0';
111 queueTail->dataStr[0]='\0';
112 queueTail->next=NULL;
116 /****************************************
117 Get and remove address data from the head of Queue
119 ****************************************/
120 int dequeue(char* keyStr, char* dataStr){
122 /* set null string as default */
126 /* if not prepared, error */
128 err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
131 else if(queueHead->next==NULL){
132 err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
136 /* if no data, return false */
137 else if(queueHead->next==queueTail){
141 /* get item from the head */
143 struct queueNode *temp;
144 temp=queueHead->next;
145 queueHead->next=temp->next;
146 strncpy(keyStr, temp->keyStr, sizeof(temp->keyStr));
147 strncpy(dataStr, temp->dataStr, sizeof(temp->dataStr));
153 /****************************************
154 Listing IpAddress Queue (for debugging)
155 ****************************************/
158 struct queueNode *temp;
160 if(queueHead==NULL) return FALSE;
161 temp=queueHead->next;
162 while(temp->next!=NULL){
163 printf("[%s][%s]\n", temp->keyStr, temp->dataStr);
170 /****************************************
171 memory free for Address Queue
172 ****************************************/
173 void freequeue(void){
174 char keyStr[WORDMAXLN];
175 char dataStr[WORDMAXLN];
176 while(Dequeue(keyStr, dataStr));
179 queueHead=queueTail=NULL;
182 /****************************************************
183 routines for debugging putput
184 ***************************************************/
187 if(debug>1) err_msg("DEBUG:=>initqueue( )");
189 if(debug>1) err_msg("DEBUG:(%d)<=initqueue( )",ret);
193 int Enqueue(char* keyStr, char* dataStr){
195 if(debug>1) err_msg("DEBUG:=>enqueue(%s,%s)", keyStr, dataStr);
196 ret = enqueue(keyStr, dataStr);
197 if(debug>1) err_msg("DEBUG:(%d)<=enqueue( )",ret);
201 int Dequeue(char* keyStr, char* dataStr){
203 if(debug>1) err_msg("DEBUG:=>dequeue( )");
204 ret = dequeue(keyStr, dataStr);
205 if(debug>1) err_msg("DEBUG:(%d)<=dequque(%s,%s)",ret, keyStr, dataStr);
211 if(debug>1) err_msg("DEBUG:=>listQueue( )");
213 if(debug>1) err_msg("DEBUG:(%d)<=listQueue( )",ret);
217 void Freequeue(void){
218 if(debug>1) err_msg("DEBUG:=>freeQueue()");
220 if(debug>1) err_msg("DEBUG:<=freequeue()");