1 |
/* Copyright (C) 2003 Stephane Thiell |
2 |
* |
3 |
* This file is part of pxyservd (from pxys) |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
* |
19 |
*/ |
20 |
#define RCSID "$Id: glineq.c,v 1.2 2004/01/15 01:46:50 mbuna Exp $" |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include "config.h" |
24 |
#endif |
25 |
|
26 |
#include "glineq.h" |
27 |
|
28 |
#include <assert.h> |
29 |
#include <errno.h> |
30 |
#include <stdio.h> |
31 |
#include <stdlib.h> |
32 |
#include <string.h> |
33 |
#include <unistd.h> |
34 |
#include <netinet/in.h> |
35 |
|
36 |
#if 0 |
37 |
#include <string.h> |
38 |
#include <stdio.h> |
39 |
#endif |
40 |
|
41 |
#define Q_SPACE_INIT_SIZE 32 |
42 |
#define Q_SPACE_GROW_SIZE 32 |
43 |
|
44 |
struct g_data |
45 |
{ |
46 |
struct in_addr addr; |
47 |
int hitcnt; |
48 |
char reason[248]; |
49 |
}; |
50 |
|
51 |
struct g_elem |
52 |
{ |
53 |
int next_idx; |
54 |
struct g_data data; |
55 |
}; |
56 |
|
57 |
struct g_space |
58 |
{ |
59 |
int sblk, sblk_end; /* index */ |
60 |
int nelem; |
61 |
int size; |
62 |
int maxelems; |
63 |
struct g_elem *space; |
64 |
}; |
65 |
|
66 |
static struct g_space qspace; |
67 |
static int qhead_idx = -1, qtail_idx; |
68 |
static int qsize; |
69 |
|
70 |
static void |
71 |
qspace_grow() |
72 |
{ |
73 |
qspace.sblk_end = qspace.size; |
74 |
qspace.size += Q_SPACE_GROW_SIZE; |
75 |
qspace.space = (struct g_elem *)realloc(qspace.space, qspace.size |
76 |
* sizeof(struct g_elem)); |
77 |
} |
78 |
|
79 |
void |
80 |
glineq_init(int max_queue_elems) |
81 |
{ |
82 |
qspace.nelem = 0; |
83 |
qspace.size = Q_SPACE_INIT_SIZE; |
84 |
qspace.maxelems = max_queue_elems; |
85 |
qspace.space = (struct g_elem *)malloc(qspace.size * sizeof(struct g_elem)); |
86 |
qspace.sblk = qspace.sblk_end = 0; |
87 |
} |
88 |
|
89 |
void |
90 |
glineq_finalize() |
91 |
{ |
92 |
free(qspace.space); |
93 |
} |
94 |
|
95 |
int |
96 |
glineq_empty() |
97 |
{ |
98 |
return qsize == 0; |
99 |
} |
100 |
|
101 |
int |
102 |
glineq_push(const struct g_args *args) |
103 |
{ |
104 |
struct g_elem *e; |
105 |
int idx; |
106 |
|
107 |
if (qspace.maxelems && (qspace.nelem >= qspace.maxelems)) |
108 |
return -1; /* overflow */ |
109 |
|
110 |
if (qspace.nelem >= qspace.size) |
111 |
qspace_grow(); |
112 |
assert(qspace.nelem < qspace.size); |
113 |
|
114 |
if (qspace.sblk_end >= qspace.size) |
115 |
qspace.sblk_end = 0; |
116 |
|
117 |
idx = qspace.sblk_end++; |
118 |
e = &qspace.space[idx]; |
119 |
qspace.nelem++; |
120 |
e->next_idx = -1; |
121 |
|
122 |
e->data.addr = args->addr; |
123 |
e->data.hitcnt = args->hitcnt; |
124 |
snprintf(e->data.reason, sizeof(e->data.reason), "%s", args->reason); |
125 |
|
126 |
if (qhead_idx == -1) |
127 |
qhead_idx = idx; |
128 |
else |
129 |
qspace.space[qtail_idx].next_idx = idx; |
130 |
|
131 |
qtail_idx = idx; |
132 |
qsize++; |
133 |
|
134 |
return 0; |
135 |
} |
136 |
|
137 |
const struct g_args * |
138 |
glineq_first() |
139 |
{ |
140 |
static struct g_args args; |
141 |
struct g_args *argsp = NULL; |
142 |
struct g_data *d; |
143 |
|
144 |
assert(qhead_idx >= 0); |
145 |
|
146 |
if (qsize > 0) |
147 |
{ |
148 |
d = &qspace.space[qhead_idx].data; |
149 |
|
150 |
args.addr = d->addr; |
151 |
args.hitcnt = d->hitcnt; |
152 |
args.reason = d->reason; |
153 |
argsp = &args; |
154 |
} |
155 |
return argsp; |
156 |
} |
157 |
|
158 |
void |
159 |
glineq_pop() |
160 |
{ |
161 |
assert(qsize > 0); |
162 |
assert(qhead_idx >= 0); |
163 |
qhead_idx = qspace.space[qhead_idx].next_idx; |
164 |
qsize--; |
165 |
|
166 |
assert(qspace.nelem > 0); |
167 |
|
168 |
if (qspace.sblk >= qspace.size) |
169 |
qspace.sblk = 0; |
170 |
qspace.sblk++; |
171 |
qspace.nelem--; |
172 |
} |
173 |
|
174 |
#if 0 |
175 |
int |
176 |
main() |
177 |
{ |
178 |
char reasonbuf[256]; |
179 |
int i; |
180 |
struct in_addr ip; |
181 |
struct g_args arg; |
182 |
|
183 |
glineq_init(10); |
184 |
|
185 |
for (i = 0; i < 900; i++) |
186 |
{ |
187 |
arg.addr.s_addr = i; |
188 |
arg.reason = reasonbuf; |
189 |
snprintf(reasonbuf, sizeof(reasonbuf), "Reason %d", i); |
190 |
glineq_push(&arg); |
191 |
} |
192 |
|
193 |
while (qhead_idx >= 0) |
194 |
{ |
195 |
printf("gline '%s' '%s'\n", inet_ntoa(qspace.space[qhead_idx].data.addr), |
196 |
qspace.space[qhead_idx].data.reason); |
197 |
glineq_pop(); |
198 |
} |
199 |
|
200 |
return 0; |
201 |
} |
202 |
#endif |