OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / queue.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module to control queue 
4
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.
13
14
15 Copyright (C) 2011 Opengate Project Team
16 Written by Yoshiaki Watanabe
17
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.
22
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.
27
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.
31
32 Email: watanaby@is.saga-u.ac.jp
33 **************************************************/
34
35 #include "opengatemmng.h"
36
37 /* Queue to store string-key stirng-data pair */
38 struct queueNode{
39   char keyStr[WORDMAXLN];
40   char dataStr[WORDMAXLN];
41   struct queueNode *next;
42 };
43 static struct queueNode* queueTail=NULL;
44 static struct queueNode* queueHead=NULL;
45
46
47 /*********************************************
48 initialize Address Queue
49 Queue
50  HeadNode - DataNode - DataNode - TailNode
51  (dummy)                           (dummy)
52   ^queueHead                        ^queueTail
53 *********************************************/
54 int initqueue(void){
55
56   char keyStr[WORDMAXLN];
57   char dataStr[WORDMAXLN];
58
59   /* if not exist, prepare head and tail */
60   if(queueHead==NULL){
61     queueHead=(struct queueNode*)malloc(sizeof(struct queueNode));
62     if(queueHead==NULL){
63       err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
64       terminateProg(0);
65     }
66     queueTail=(struct queueNode*)malloc(sizeof(struct queueNode));
67     if(queueTail==NULL){
68       err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
69       terminateProg(0);
70     }
71     queueHead->keyStr[0]='\0';
72     queueHead->dataStr[0]='\0';
73     queueTail->keyStr[0]='\0';
74     queueTail->dataStr[0]='\0';
75     queueHead->next=queueTail;
76     queueTail->next=NULL;
77   }
78   
79   /* if exist, reset all */
80   else{
81     while(Dequeue(keyStr, dataStr))
82       ;
83   }
84   return TRUE;
85 }
86
87 /****************************************
88 Add data to the tail of Queue
89  input=addr
90 ****************************************/
91 int enqueue(char* keyStr, char* dataStr){
92   struct queueNode *newNode;
93
94   /* if not prepared, error */
95   if(queueHead==NULL){
96     err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
97     return FALSE;
98   }
99
100   /* add item after the tail and set it as new tail*/
101   newNode=(struct queueNode*)malloc(sizeof(struct queueNode));
102   if(newNode==NULL){
103     err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
104     terminateProg(0);
105   }
106   strncpy(queueTail->keyStr, keyStr, WORDMAXLN);
107   strncpy(queueTail->dataStr, dataStr, WORDMAXLN);
108   queueTail->next=newNode;
109   queueTail=newNode;
110   queueTail->keyStr[0]='\0';
111   queueTail->dataStr[0]='\0';
112   queueTail->next=NULL;
113   return TRUE;
114 }
115
116 /****************************************
117 Get and remove address data from the head of Queue
118  output=addr
119 ****************************************/
120 int dequeue(char* keyStr, char* dataStr){
121
122   /* set null string as default */
123   keyStr[0]='\0';
124   dataStr[0]='\0';
125
126   /* if not prepared, error */
127   if(queueHead==NULL){
128     err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
129     return FALSE;
130   }
131   else if(queueHead->next==NULL){
132     err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
133     return FALSE;  
134   }
135
136   /* if no data, return false */
137   else if(queueHead->next==queueTail){
138     return FALSE;
139   }
140
141   /* get item from the head */
142   else {
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));
148     free(temp);
149   }
150   return TRUE;
151 }
152
153 /****************************************
154 Listing IpAddress Queue (for debugging)
155 ****************************************/
156 int listqueue(void){
157
158   struct queueNode *temp;
159
160   if(queueHead==NULL) return FALSE;
161   temp=queueHead->next;
162   while(temp->next!=NULL){
163     printf("[%s][%s]\n", temp->keyStr, temp->dataStr);
164     temp=temp->next;
165   }
166   return TRUE;
167 }
168
169
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));
177   free(queueHead);
178   free(queueTail);
179   queueHead=queueTail=NULL;
180 }
181
182 /****************************************************
183  routines for debugging putput
184  ***************************************************/
185 int Initqueue(void){
186   int ret;
187   if(debug>1) err_msg("DEBUG:=>initqueue( )");
188   ret = initqueue();
189   if(debug>1) err_msg("DEBUG:(%d)<=initqueue( )",ret);
190   return ret;
191
192 }
193 int Enqueue(char* keyStr, char* dataStr){
194   int ret;
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);
198   return ret;
199
200 }
201 int Dequeue(char* keyStr, char* dataStr){
202   int ret;
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);
206   return ret;
207
208 }
209 int Listqueue(void){
210   int ret;
211   if(debug>1) err_msg("DEBUG:=>listQueue( )");
212   ret = listqueue();
213   if(debug>1) err_msg("DEBUG:(%d)<=listQueue( )",ret);
214   return ret;
215 }
216
217 void Freequeue(void){
218   if(debug>1) err_msg("DEBUG:=>freeQueue()");
219   freequeue();
220   if(debug>1) err_msg("DEBUG:<=freequeue()");
221 }
222
223