1 /**************************************************
2 OpengateM - MAC address authentication system
3 module to control queue
5 As address check by database is time consuming procedure,
6 the recently checked addresses are cached.
7 The cache is implemented with HashTable and Queue.
8 Queue has data odrered by checked time, and
9 controls the remove sequence.
11 Copyright (C) 2011 Opengate Project Team
12 Written by Yoshiaki Watanabe
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 Email: watanaby@is.saga-u.ac.jp
29 **************************************************/
31 #include "opengatemmng.h"
33 /* Queue (linked-list) for storing string-key stirng-data pair */
35 char keyStr[WORDMAXLN];
36 char dataStr[WORDMAXLN];
37 struct queueNode *next;
39 static struct queueNode* queueTail=NULL;
40 static struct queueNode* queueHead=NULL;
42 /***************************************************/
43 /* initialize Queue (linked-list) */
45 /* HeadNode -> DataNode -> DataNode -> TailNode */
48 /* queueHead queueTail */
49 /***************************************************/
52 char keyStr[WORDMAXLN];
53 char dataStr[WORDMAXLN];
55 /* if not exist, prepare head and tail nodes */
57 queueHead=(struct queueNode*)malloc(sizeof(struct queueNode));
59 err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
62 queueTail=(struct queueNode*)malloc(sizeof(struct queueNode));
64 err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
67 queueHead->keyStr[0]='\0';
68 queueHead->dataStr[0]='\0';
69 queueTail->keyStr[0]='\0';
70 queueTail->dataStr[0]='\0';
71 queueHead->next=queueTail;
75 /* if exist, reset all */
77 while(Dequeue(keyStr, dataStr))
83 /****************************************/
84 /* Add data to the tail of Queue */
85 /* keyStr,dataStr:(input) */
86 /****************************************/
87 int enqueue(char* keyStr, char* dataStr){
88 struct queueNode *newNode;
90 /* if not prepared, error */
92 err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
96 /* add item after the tail and set it as new tail*/
97 newNode=(struct queueNode*)malloc(sizeof(struct queueNode));
99 err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
102 strlcpy(queueTail->keyStr, keyStr, WORDMAXLN);
103 strlcpy(queueTail->dataStr, dataStr, WORDMAXLN);
104 queueTail->next=newNode;
106 queueTail->keyStr[0]='\0';
107 queueTail->dataStr[0]='\0';
108 queueTail->next=NULL;
112 /**********************************************/
113 /* Get and remove data from the head of Queue */
114 /* keyStr, dataStr:(output) */
115 /**********************************************/
116 int dequeue(char* keyStr, char* dataStr){
118 /* set null string as default */
122 /* if not prepared, error */
124 err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
127 else if(queueHead->next==NULL){
128 err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
132 /* if no data, return false */
133 else if(queueHead->next==queueTail){
137 /* get item from the head */
139 struct queueNode *temp;
140 temp=queueHead->next;
141 queueHead->next=temp->next;
142 strlcpy(keyStr, temp->keyStr, sizeof(temp->keyStr));
143 strlcpy(dataStr, temp->dataStr, sizeof(temp->dataStr));
149 /******************************************/
150 /* Listing Queue items (for debugging) */
151 /******************************************/
154 struct queueNode *temp;
156 if(queueHead==NULL) return FALSE;
157 temp=queueHead->next;
158 while(temp->next!=NULL){
159 printf("[%s][%s]\n", temp->keyStr, temp->dataStr);
166 /*****************************/
167 /* free all memory for Queue */
168 /*****************************/
169 void freequeue(void){
170 char keyStr[WORDMAXLN];
171 char dataStr[WORDMAXLN];
172 while(Dequeue(keyStr, dataStr));
175 queueHead=queueTail=NULL;
178 /**********************************/
179 /**********************************/
182 if(debug>1) err_msg("DEBUG:=>initqueue( )");
184 if(debug>1) err_msg("DEBUG:(%d)<=initqueue( )",ret);
188 int Enqueue(char* keyStr, char* dataStr){
190 if(debug>1) err_msg("DEBUG:=>enqueue(%s,%s)", keyStr, dataStr);
191 ret = enqueue(keyStr, dataStr);
192 if(debug>1) err_msg("DEBUG:(%d)<=enqueue( )",ret);
196 int Dequeue(char* keyStr, char* dataStr){
198 if(debug>1) err_msg("DEBUG:=>dequeue( )");
199 ret = dequeue(keyStr, dataStr);
200 if(debug>1) err_msg("DEBUG:(%d)<=dequque(%s,%s)",ret, keyStr, dataStr);
206 if(debug>1) err_msg("DEBUG:=>listQueue( )");
208 if(debug>1) err_msg("DEBUG:(%d)<=listQueue( )",ret);
212 void Freequeue(void){
213 if(debug>1) err_msg("DEBUG:=>freeQueue()");
215 if(debug>1) err_msg("DEBUG:<=freequeue()");