forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkip.c
executable file
·52 lines (43 loc) · 1.27 KB
/
checkip.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
#include <string.h>
#include "checkip.h"
#include "debug.h"
static int CheckIP_Add(CheckIP *c, const char *Domain, int Port, int Timeout, int Strategy)
{
CheckingMeta cm = {Port, Timeout, Strategy};
return StringChunk_Add_Domain(&(c -> Chunk), Domain, (const char *)&cm, sizeof(CheckingMeta));
}
/* Domain Port Timeout [keep|discard] */
static int CheckIP_Add_From_String(CheckIP *c, const char *Rule)
{
char Domain[128] = {0}, StrategyS[16] = {0};
int Port, Timeout, Strategy;
sscanf(Rule, "%127s%d%d%15s", Domain, &Port, &Timeout, StrategyS);
#define IS_STATE(s) (strncmp(StrategyS, (s), strlen(s)) == 0)
if( IS_STATE("discard") )
{
Strategy = STRATEGY_DISCARD;
} else if( IS_STATE("keep") ){
Strategy = STRATEGY_KEEP;
} else {
ERRORMSG("Invalid `CheckIP' option : %s\n", Rule);
return -1;
}
return CheckIP_Add(c, Domain, Port, Timeout, Strategy);
}
static const CheckingMeta *CheckIP_Find(CheckIP *c, const char *Domain)
{
const CheckingMeta *cm;
if( StringChunk_Domain_Match(&(c -> Chunk), Domain, NULL, (char **)&cm) == TRUE )
{
return cm;
} else {
return NULL;
}
}
int CheckIP_Init(CheckIP *c)
{
c -> Add = CheckIP_Add;
c -> AddFromString = CheckIP_Add_From_String;
c -> Find = CheckIP_Find;
return StringChunk_Init(&(c -> Chunk), NULL);
}