-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlists.c
433 lines (332 loc) · 8.99 KB
/
lists.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* $Id: lists.c,v 1.3 2005/08/21 10:44:06 chris Exp $ */
/*
Squirm - A Redirector for Squid
Maintained by Chris Foote, [email protected]
Copyright (C) 1998-2005 Chris Foote
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Please see the file GPL in this directory for full copyright
information.
*/
#include"squirm.h"
#include"paths.h"
#include"log.h"
#include"lists.h"
#include"regex.h"
#include"config.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
extern int dodo_mode;
void init_lists(void)
{
subnet_head = NULL;
pattern_head = NULL;
}
/* Initialises a block which represents a group of network prefixes in the conf file */
struct subnet_block *add_subnet_block()
{
struct subnet_block *new;
struct subnet_block *curr;
new = NULL;
curr = NULL;
new = (struct subnet_block*)malloc(sizeof(struct subnet_block));
if (new == NULL) {
logprint(LOG_ERROR, "unable to allocate memory in add_subnet_block()\n");
dodo_mode = 1;
return NULL;
}
new->sub_list = NULL;
new->pattern_list = NULL;
new->abort_log_name = NULL;
new->log_name = NULL;
new->next = NULL;
/* add it to the _end_ of the global list of subnets */
if(subnet_head == NULL)
subnet_head = new;
else {
for(curr = subnet_head; curr->next != NULL; curr = curr->next)
;
curr->next = new;
}
#ifdef DEBUG
logprint(LOG_DEBUG, "created new subnet block\n");
#endif
return new;
}
/* end add_subnet_block */
int add_to_ip_list(struct cidr src_net, struct subnet_block *block)
{
struct IP_item *curr;
struct IP_item *new;
curr = NULL;
new = NULL;
new = (struct IP_item *)malloc(sizeof(struct IP_item));
if(new == NULL) {
logprint(LOG_ERROR, "unable to allocate memory in add_to_ip_list()\n");
dodo_mode = 1;
return 1;
}
new->subnet = src_net;
new->next = NULL;
if(block->sub_list == NULL)
block->sub_list = new;
else {
for(curr = block->sub_list; curr->next != NULL; curr = curr->next)
;
curr->next = new;
}
#ifdef DEBUG
logprint(LOG_DEBUG, "inserted subnet %d %d\n", src_net.address.s_addr,
src_net.netmask.s_addr);
#endif
return 0;
}
/* end add_to_ip_list() */
int add_pattern_file(char *conf_filename, struct subnet_block *block,
int methods)
{
struct pattern_file *p;
struct pattern_block **curr;
if (pattern_head != NULL) {
for(p = pattern_head; p->next != NULL; p = p->next) {
if (strcmp(conf_filename, p->filename) == 0) {
/* we have a match, no need to build this list */
#ifdef DEBUG
logprint(LOG_DEBUG, "Found already constructed pattern file\n");
#endif
break;
}
}
if ((p->next == NULL) && (strcmp(conf_filename, p->filename)!=0)) {
/* we didn't find a match, must build the list, then make p point to it */
#ifdef DEBUG
logprint(LOG_DEBUG, "Building pattern file [%s]\n", conf_filename);
#endif
p->next = create_pattern_list(conf_filename);
if (p->next == NULL) {
logprint(LOG_ERROR, "unable to create_pattern_list() in add_pattern_file()\n");
dodo_mode = 1;
return -1;
}
p = p->next;
}
} else {
pattern_head = create_pattern_list(conf_filename);
if (pattern_head == NULL) {
logprint(LOG_ERROR, "unable to create_pattern_list() in add_pattern_file()\n");
dodo_mode = 1;
return -1;
}
p = pattern_head;
}
/* now we have p pointing at the pattern file list we want, so insert */
for (curr = &(block->pattern_list); *curr != NULL; curr = &((*curr)->next))
;
/* insert the pattern_file on the end of the list */
*curr = (struct pattern_block *)malloc(sizeof(struct pattern_block));
if (*curr == NULL) {
logprint(LOG_ERROR, "unable to allocate memory in add_pattern_file()\n");
dodo_mode = 1;
return -1;
}
(*curr)->p_head = p;
(*curr)->next = NULL;
(*curr)->methods = methods;
#ifdef DEBUG
logprint(LOG_DEBUG, "inserted pattern file %s, with accept %d\n", conf_filename, methods);
#endif
return 1;
}
/* end add_pattern_file */
void add_to_plist(struct REGEX_pattern pattern, struct pattern_item **list)
{
struct pattern_item *curr;
struct pattern_item *new;
curr = NULL;
new = NULL;
/* two strings are already allocated in the "pattern" struct
argument to this function */
new = (struct pattern_item *)malloc(sizeof(struct pattern_item));
if(new == NULL) {
logprint(LOG_ERROR, "unable to allocate memory in add_to_plist()\n");
/* exit(3); */
dodo_mode = 1;
return;
}
new->patterns.pattern = pattern.pattern;
new->patterns.replacement = pattern.replacement;
new->patterns.type = pattern.type;
new->patterns.has_accel = pattern.has_accel;
new->patterns.accel = pattern.accel;
new->patterns.accel_type = pattern.accel_type;
new->patterns.case_sensitive = pattern.case_sensitive;
new->patterns.cpattern = pattern.cpattern;
new->next = NULL;
if(*list == NULL)
*list = new;
else {
for(curr = *list; curr->next != NULL; curr = curr->next)
;
curr->next = new;
}
#ifdef DEBUG
logprint(LOG_DEBUG, "inserted pattern item\n");
#endif
}
void free_pattern_blocks(struct pattern_block *head)
{
struct pattern_block *prev;
struct pattern_block *next;
prev = NULL;
next = NULL;
if(head != NULL) {
next = head->next;
free(head);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed pattern block head\n");
#endif
}
for(prev = next; next != NULL; ) {
next = prev->next;
free(prev);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed pattern block\n");
#endif
prev = next;
}
}
void free_ip_list(struct IP_item *head)
{
struct IP_item *prev;
struct IP_item *next;
prev = NULL;
next = NULL;
if(head != NULL) {
#ifdef DEBUG
logprint(LOG_DEBUG, "freed ip list head\n");
#endif
next = head->next;
free(head);
}
for(prev = next; next != NULL; ) {
next = prev->next;
free(prev);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed ip list item\n");
#endif
prev = next;
}
}
void free_subnet_blocks(void)
{
struct subnet_block *prev;
struct subnet_block *next;
prev = NULL;
next = NULL;
if(subnet_head != NULL) {
next = subnet_head->next;
if(subnet_head->abort_log_name)
free(subnet_head->abort_log_name);
if(subnet_head->log_name)
free(subnet_head->log_name);
free_ip_list(subnet_head->sub_list);
free_pattern_blocks(subnet_head->pattern_list);
free(subnet_head);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed subnet block head\n");
#endif
}
for(prev = next; next != NULL; ) {
next = prev->next;
if(prev->abort_log_name)
free(prev->abort_log_name);
if(prev->log_name)
free(prev->log_name);
free_ip_list(prev->sub_list);
free_pattern_blocks(prev->pattern_list);
free(prev);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed subnet block\n");
#endif
prev = next;
}
subnet_head = NULL;
}
void free_plist(struct pattern_item *head)
{
struct pattern_item *prev;
struct pattern_item *next;
prev = NULL;
next = NULL;
if(head != NULL) {
next = head->next;
if(head->patterns.pattern)
free(head->patterns.pattern);
if(head->patterns.replacement)
free(head->patterns.replacement);
if(head->patterns.accel)
free(head->patterns.accel);
free(head);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed plist item head\n");
#endif
}
for(prev = next; next != NULL; ) {
next = prev->next;
if(prev->patterns.pattern)
free(prev->patterns.pattern);
if(prev->patterns.replacement)
free(prev->patterns.replacement);
if(prev->patterns.accel)
free(prev->patterns.accel);
free(prev);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed plist item\n");
#endif
prev = next;
}
}
void free_pattern_files(void)
{
struct pattern_file *prev;
struct pattern_file *next;
prev = NULL;
next = NULL;
if(pattern_head != NULL) {
next = pattern_head->next;
free_plist(pattern_head->head);
if(pattern_head->filename)
free(pattern_head->filename);
free(pattern_head);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed pattern file head\n");
#endif
}
for(prev = next; next != NULL; ) {
next = prev->next;
free_plist(prev->head);
if (prev->filename)
free(prev->filename);
free(prev);
#ifdef DEBUG
logprint(LOG_DEBUG, "freed pattern file item\n");
#endif
prev = next;
}
pattern_head = NULL;
}
void free_lists(void)
{
free_subnet_blocks();
free_pattern_files();
}