forked from lifenjoiner/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachettlcrtl.c
executable file
·106 lines (90 loc) · 2.24 KB
/
cachettlcrtl.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
#include <stdio.h>
#include <string.h>
#include "cachettlcrtl.h"
#include "logs.h"
int CacheTtlCrtl_Init(CacheTtlCtrl *c)
{
return StringChunk_Init((StringChunk *)c, NULL);
}
int CacheTtlCrtl_Add(CacheTtlCtrl *c, const char *Domain, int State, uint32_t Coefficient, uint32_t Increment, int Infection)
{
CtrlContent cc = {State, Coefficient, Increment, Infection};
if( State == TTL_STATE_FIXED )
{
cc.Coefficient = 0;
}
return StringChunk_Add_Domain((StringChunk *)c, Domain, (const char *)&cc, sizeof(CtrlContent));
}
int CacheTtlCrtl_Add_From_String(CacheTtlCtrl *c, const char *Rule)
{
char Domain[128], Cmd[16], Arg[64];
int State;
uint32_t Coefficient = 0;
uint32_t Increment = 0;
int Infection = TTL_CTRL_INFECTION_AGGRESSIVLY;
sscanf(Rule, "%127s%15s%64s", Domain, Cmd, Arg);
if( Cmd[0] == '$' )
{
if( Cmd[1] == '$' )
{
Infection = TTL_CTRL_INFECTION_NONE;
memmove(Cmd, Cmd + 2, sizeof(Cmd) - 2);
} else {
Infection = TTL_CTRL_INFECTION_PASSIVLY;
memmove(Cmd, Cmd + 1, sizeof(Cmd) - 1);
}
} else {
Infection = TTL_CTRL_INFECTION_AGGRESSIVLY;
}
#define IS_STATE(s) (strncmp(Cmd, (s), strlen(s)) == 0)
if( IS_STATE("orig") )
{
State = TTL_STATE_ORIGINAL;
} else if( IS_STATE("nocache") )
{
State = TTL_STATE_NO_CACHE;
} else if( IS_STATE("fixed") )
{
State = TTL_STATE_FIXED;
Coefficient = 0;
sscanf(Arg, "%u", &Increment);
} else if( IS_STATE("vari") )
{
State = TTL_STATE_VARIABLE;
sscanf(Arg, "%ux+%u", &Coefficient, &Increment);
} else {
ERRORMSG("Invalid `CacheControl' option : %s\n", Rule);
return -1;
}
return CacheTtlCrtl_Add(c, Domain, State, Coefficient, Increment, Infection);
}
int CacheTtlCrtl_Add_From_StringList(CacheTtlCtrl *c, StringList *sl)
{
const char *Itr = NULL;
StringListIterator sli;
if( sl == NULL )
{
return 0;
}
if( StringListIterator_Init(&sli, sl) != 0 )
{
return -1;
}
Itr = sli.Next(&sli);
while( Itr != NULL )
{
CacheTtlCrtl_Add_From_String(c, Itr);
Itr = sli.Next(&sli);
}
return 0;
}
const CtrlContent *CacheTtlCrtl_Get(CacheTtlCtrl *c, const char *Domain)
{
CtrlContent *ret = NULL;
if( StringChunk_Domain_Match((StringChunk *)c, Domain, NULL, (void **)&(ret)) == TRUE )
{
return ret;
} else {
return NULL;
}
}