forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
mcontext.c
executable file
·156 lines (124 loc) · 3.27 KB
/
mcontext.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
#include <string.h>
#include <time.h>
#include "mcontext.h"
#include "common.h"
static int ModuleContext_Sweep_Collect(Bst *t,
const MsgContext *Context,
Array *Pending
)
{
if( time(NULL) - ((IHeader *)Context)->Timestamp > 2 )
{
Array_PushBack(Pending, &Context, NULL);
}
return 0;
}
static void ModuleContext_Sweep(ModuleContext *c, SweepCallback cb, void *Arg)
{
Array Pending;
int i;
if( Array_Init(&Pending,
sizeof(const MsgContext *),
4,
FALSE,
NULL
)
!= 0
)
{
return;
}
c->d.Enum(&(c->d),
(Bst_Enum_Callback)ModuleContext_Sweep_Collect,
&Pending
);
for( i = 0; i < Array_GetUsed(&Pending); ++i )
{
const MsgContext **Context;
Context = Array_GetBySubscript(&Pending, i);
if( cb != NULL )
{
cb(*Context, i + 1, Arg);
}
c->d.Delete(&(c->d), *Context);
}
Array_Free(&Pending);
}
static MsgContext *ModuleContext_Add(ModuleContext *c, MsgContext *MsgCtx)
{
IHeader *h;
if( MsgCtx == NULL )
{
return NULL;
}
h = (IHeader *)MsgCtx;
h->Timestamp = time(NULL);
return (MsgContext *)(c->d.Add(&(c->d), MsgCtx));
}
static const MsgContext *ModuleContext_Find(ModuleContext *c, MsgContext *Input)
{
return c->d.Search(&(c->d), Input, NULL);
}
static void ModuleContext_Del(ModuleContext *c, MsgContext *Input)
{
c->d.Delete(&(c->d), Input);
}
static int ModuleContext_GenAnswerHeaderAndRemove(ModuleContext *c,
MsgContext *Input,
MsgContext *Output
)
{
IHeader *h1, *h2;
const MsgContext *ri;
int EntityLength;
BOOL EDNSEnabled;
h1 = (IHeader *)Input;
h2 = (IHeader *)Output;
ri = ModuleContext_Find(c, Input);
if( ri == NULL )
{
return -60;
}
EntityLength = h1->EntityLength;
EDNSEnabled = h1->EDNSEnabled;
memcpy(Output, ri, sizeof(IHeader));
h2->EntityLength = EntityLength;
h2->EDNSEnabled = EDNSEnabled;
IHeader_Reset((IHeader *)ri);
c->d.Delete(&(c->d), ri);
return 0;
}
static int ModuleContextCompare(const void *_1, const void *_2)
{
const IHeader *One = (IHeader *)_1;
const IHeader *Two = (IHeader *)_2;
int Id_1 = DNSGetQueryIdentifier(One + 1);
int Id_2 = DNSGetQueryIdentifier(Two + 1);
if( Id_1 != Id_2 )
{
return Id_1 - Id_2;
} else {
return One->HashValue - Two->HashValue;
}
}
void ModuleContext_Free(ModuleContext *c)
{
c->d.Free(&(c->d));
}
int ModuleContext_Init(ModuleContext *c, int ItemLength)
{
if( c == NULL )
{
return -86;
}
if( Bst_Init(&(c->d), ItemLength, ModuleContextCompare) != 0 )
{
return -106;
}
c->Add = ModuleContext_Add;
c->Del = ModuleContext_Del;
c->Find = ModuleContext_Find;
c->GenAnswerHeaderAndRemove = ModuleContext_GenAnswerHeaderAndRemove;
c->Sweep = ModuleContext_Sweep;
return 0;
}