-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipdb.c
190 lines (182 loc) · 3.72 KB
/
ipdb.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
#include "uint16.h"
#include "logger.h"
#include "alloc.h"
#include "str.h"
#include <string.h>
#include <stdlib.h>
#include "ip4.h"
#include "open.h"
#include "tai.h"
#include "cdb.h"
#include "byte.h"
#include "case.h"
#include "dns.h"
#include "seek.h"
#include "response.h"
#include "ipdb.h"
#include "env.h"
#include "scan.h"
#include "prot.h"
#include "strerr.h"
#include "stralloc.h"
#include "qlog.h"
#include "uint64.h"
#define FATAL "ipdbops: fatal: "
int ipdb_key4build(key,ipuint,mask,mid,uid)
unsigned char key[15];
unsigned int ipuint;
unsigned char mask;
uint32 mid;
uint32 uid;
{
int i;
unsigned int netmask = 0;
key[0] = '\0';
key[1] = '%';
uint32_pack(key+2,mid);
uint32_pack(key+6,uid);
key[10] = (ipuint >> 24) & 0xff;
key[11] = (ipuint >> 16) & 0xff;
key[12] = (ipuint >> 8) & 0xff;
key[13] = ipuint & 0xff;
key[14] = mask;
return 1;
}
int ipdb_key4lookup(key,ipuint,mask,mid,uid)
unsigned char key[15];
unsigned int ipuint;
unsigned char mask;
uint32 mid;
uint32 uid;
{
int i;
unsigned int netmask = 0;
key[0] = '\0';
key[1] = '%';
uint32_pack(key+2,mid);
uint32_pack(key+6,uid);
for (i = 0; i < mask; i++) netmask |= 1 << (31 - i);
ipuint = ipuint & netmask;
key[10] = (ipuint >> 24) & 0xff;
key[11] = (ipuint >> 16) & 0xff;
key[12] = (ipuint >> 8) & 0xff;
key[13] = ipuint & 0xff;
key[14] = mask;
return 1;
}
/*
int ipdb_key_from_uint(key,ipuint,mask,applymask,mid,uid)
unsigned char key[15];
unsigned int ipuint;
unsigned char mask;
int applymask;
uint32 mid;
uint32 uid;
{
int i;
unsigned int netmask = 0;
key[0] = '\0';
key[1] = '%';
uint32_pack(key+2,mid);
uint32_pack(key+6,uid);
//key[2] = mapid;
if (applymask) {
for (i = 0; i < mask; i++) netmask |= 1 << (31 - i);
ipuint = ipuint & netmask;
}
key[10] = (ipuint >> 24) & 0xff;
key[11] = (ipuint >> 16) & 0xff;
key[12] = (ipuint >> 8) & 0xff;
key[13] = ipuint & 0xff;
key[14] = mask;
return 1;
}
int ipdb_key_from_str(key,ipstr,mask,applymask,uid,mapid)
unsigned char key[15];
const char *ipstr;
unsigned char mask;
int applymask;
uint32 uid;
uint32 mapid;
{
unsigned char ip[4];
unsigned int j;
ip4_scan(ipstr,ip);
ip4_num(&j,ip);
return ipdb_key_from_uint(key,j,mask,applymask,uid,mapid);
}
int ipdb_key_from_bytes(key,ip,mask,applymask,uid,mapid)
unsigned char key[15];
unsigned char ip[4];
unsigned char mask;
int applymask;
uint32 uid;
uint32 mapid;
{
unsigned int j;
ip4_num(&j,ip);
return ipdb_key_from_uint(key, j,mask,applymask,uid,mapid);
}
*/
#define IPDB_TRYLOOKUP(m) do {\
ipdb_key4lookup(key,ipint,m,mid,uid);\
r = cdb_find(c,key,15); \
if (r == -1) return 0; \
if (r == 1) {\
if (cdb_read(c,cc,4,cdb_datapos(c)) == -1) { \
return 0; \
} else { \
uint32_unpack(cc,clientloc); \
return 1; \
} \
} \
} while (0);
int ipdb_get(c,ip,clientloc,mid,uid)
struct cdb *c;
unsigned char ip[4];
uint32 *clientloc;
uint32 mid;
uint32 uid;
{
unsigned char key[15];
//unsigned char intmask;
unsigned int ipint = 0;
int r = -1;
char cc[4];
ip4_num(&ipint,ip);
IPDB_TRYLOOKUP(24);
IPDB_TRYLOOKUP(28);
IPDB_TRYLOOKUP(29);
IPDB_TRYLOOKUP(27);
IPDB_TRYLOOKUP(32);
IPDB_TRYLOOKUP(23);
IPDB_TRYLOOKUP(26);
IPDB_TRYLOOKUP(30);
IPDB_TRYLOOKUP(25);
IPDB_TRYLOOKUP(22);
IPDB_TRYLOOKUP(19);
IPDB_TRYLOOKUP(21);
IPDB_TRYLOOKUP(20);
IPDB_TRYLOOKUP(16);
IPDB_TRYLOOKUP(18);
IPDB_TRYLOOKUP(31);
IPDB_TRYLOOKUP(17);
IPDB_TRYLOOKUP(15);
IPDB_TRYLOOKUP(14);
IPDB_TRYLOOKUP(13);
IPDB_TRYLOOKUP(12);
IPDB_TRYLOOKUP(11);
IPDB_TRYLOOKUP(10);
IPDB_TRYLOOKUP(9);
IPDB_TRYLOOKUP(8);
IPDB_TRYLOOKUP(7);
IPDB_TRYLOOKUP(6);
IPDB_TRYLOOKUP(5);
IPDB_TRYLOOKUP(4);
IPDB_TRYLOOKUP(3);
IPDB_TRYLOOKUP(2);
IPDB_TRYLOOKUP(1);
/* nothing found, then go with defloc XX */
//dbger("LOC not found for (mid=%u,uid=%u)\n",mid,uid);
return 1;
}