1 | #include "globals.h"
|
---|
2 |
|
---|
3 | #ifdef CS_CACHEEX
|
---|
4 |
|
---|
5 | #include "cscrypt/md5.h"
|
---|
6 | #include "module-cacheex.h"
|
---|
7 | #include "module-cw-cycle-check.h"
|
---|
8 | #include "oscam-cache.h"
|
---|
9 | #include "oscam-chk.h"
|
---|
10 | #include "oscam-client.h"
|
---|
11 | #include "oscam-conf.h"
|
---|
12 | #include "oscam-ecm.h"
|
---|
13 | #include "oscam-lock.h"
|
---|
14 | #include "oscam-net.h"
|
---|
15 | #include "oscam-string.h"
|
---|
16 | #include "oscam-time.h"
|
---|
17 | #include "oscam-work.h"
|
---|
18 |
|
---|
19 | #define cs_cacheex_matcher "oscam.cacheex"
|
---|
20 |
|
---|
21 | extern uint8_t cc_node_id[8];
|
---|
22 | extern uint8_t camd35_node_id[8];
|
---|
23 |
|
---|
24 | uint8_t cacheex_peer_id[8];
|
---|
25 |
|
---|
26 | extern CS_MUTEX_LOCK ecm_pushed_deleted_lock;
|
---|
27 | extern struct ecm_request_t *ecm_pushed_deleted;
|
---|
28 | extern CS_MUTEX_LOCK ecmcache_lock;
|
---|
29 | extern struct ecm_request_t *ecmcwcache;
|
---|
30 |
|
---|
31 | static void *chkcache_process(void)
|
---|
32 | {
|
---|
33 | set_thread_name(__func__);
|
---|
34 |
|
---|
35 | time_t timeout;
|
---|
36 | struct ecm_request_t *er, *ecm;
|
---|
37 | #ifdef CS_CACHEEX
|
---|
38 | uint8_t add_hitcache_er;
|
---|
39 | struct s_reader *cl_rdr;
|
---|
40 | struct s_reader *rdr;
|
---|
41 | struct s_ecm_answer *ea;
|
---|
42 | struct s_client *cex_src=NULL;
|
---|
43 | #endif
|
---|
44 | struct s_write_from_cache *wfc=NULL;
|
---|
45 |
|
---|
46 | while(1)
|
---|
47 | {
|
---|
48 | cs_readlock(&ecmcache_lock);
|
---|
49 | for(er = ecmcwcache; er; er = er->next)
|
---|
50 | {
|
---|
51 | timeout = time(NULL)-((cfg.ctimeout+500)/1000+1);
|
---|
52 | if(er->tps.time < timeout)
|
---|
53 | { break; }
|
---|
54 |
|
---|
55 | if(er->rc<E_UNHANDLED || er->readers_timeout_check) //already answered
|
---|
56 | { continue; }
|
---|
57 |
|
---|
58 | //******** CHECK IF FOUND ECM IN CACHE
|
---|
59 | ecm = check_cache(er, er->client);
|
---|
60 | if(ecm) //found in cache
|
---|
61 | {
|
---|
62 |
|
---|
63 | #ifdef CS_CACHEEX
|
---|
64 | //check for add_hitcache
|
---|
65 | if(ecm->cacheex_src) //cw from cacheex
|
---|
66 | {
|
---|
67 | if((er->cacheex_wait_time && !er->cacheex_wait_time_expired) || !er->cacheex_wait_time) //only when no wait_time expires (or not wait_time)
|
---|
68 | {
|
---|
69 |
|
---|
70 | //add_hitcache already called, but we check if we have to call it for these (er) caid|prid|srvid
|
---|
71 | if(ecm->prid!=er->prid || ecm->srvid!=er->srvid)
|
---|
72 | {
|
---|
73 | cex_src = ecm->cacheex_src && is_valid_client(ecm->cacheex_src) && !ecm->cacheex_src->kill ? ecm->cacheex_src : NULL; //here we should be sure cex client has not been freed!
|
---|
74 | if(cex_src){ //add_hitcache only if client is really active
|
---|
75 | add_hitcache_er=1;
|
---|
76 | cl_rdr = cex_src->reader;
|
---|
77 | if(cl_rdr && cl_rdr->cacheex.mode == 2)
|
---|
78 | {
|
---|
79 | for(ea = er->matching_rdr; ea; ea = ea->next)
|
---|
80 | {
|
---|
81 | rdr = ea->reader;
|
---|
82 | if(cl_rdr == rdr && ((ea->status & REQUEST_ANSWERED) == REQUEST_ANSWERED))
|
---|
83 | {
|
---|
84 | cs_debug_mask(D_CACHEEX|D_CSP|D_LB,"{client %s, caid %04X, prid %06X, srvid %04X} [CACHEEX] skip ADD self request!", (check_client(er->client)?er->client->account->usr:"-"),er->caid, er->prid, er->srvid);
|
---|
85 | add_hitcache_er=0; //don't add hit cache, reader requested self
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | if(add_hitcache_er)
|
---|
91 | { add_hitcache(cex_src, er); } //USE cacheex client (to get correct group) and ecm from requesting client (to get correct caid|prid|srvid)!!!
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | //add_hitcache already called, but we have to remove it because cacheex not coming before wait_time
|
---|
99 | if(ecm->prid==er->prid && ecm->srvid==er->srvid)
|
---|
100 | { del_hitcache(ecm); }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | //END check for add_hitcache
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | if(check_client(er->client))
|
---|
107 | {
|
---|
108 |
|
---|
109 | wfc=NULL;
|
---|
110 | if(!cs_malloc(&wfc, sizeof(struct s_write_from_cache)))
|
---|
111 | {
|
---|
112 | NULLFREE(ecm);
|
---|
113 | continue;
|
---|
114 | }
|
---|
115 |
|
---|
116 | wfc->er_new=er;
|
---|
117 | wfc->er_cache=ecm;
|
---|
118 |
|
---|
119 | if(!add_job(er->client, ACTION_ECM_ANSWER_CACHE, wfc, sizeof(struct s_write_from_cache))) //write_ecm_answer_fromcache
|
---|
120 | {
|
---|
121 | NULLFREE(ecm);
|
---|
122 | continue;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | else
|
---|
126 | { NULLFREE(ecm); }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | cs_readunlock(&ecmcache_lock);
|
---|
130 |
|
---|
131 | cs_sleepms(10);
|
---|
132 | }
|
---|
133 |
|
---|
134 | return NULL;
|
---|
135 | }
|
---|
136 |
|
---|
137 | void checkcache_process_thread_start(void)
|
---|
138 | {
|
---|
139 | start_thread((void *) &chkcache_process, "chkcache_process");
|
---|
140 | }
|
---|
141 |
|
---|
142 | void cacheex_init(void)
|
---|
143 | {
|
---|
144 | // Init random node id
|
---|
145 | get_random_bytes(cacheex_peer_id, 8);
|
---|
146 | #ifdef MODULE_CCCAM
|
---|
147 | memcpy(cacheex_peer_id, cc_node_id, 8);
|
---|
148 | #endif
|
---|
149 | #ifdef MODULE_CAMD35_TCP
|
---|
150 | memcpy(camd35_node_id, cacheex_peer_id, 8);
|
---|
151 | #endif
|
---|
152 | }
|
---|
153 |
|
---|
154 | void cacheex_clear_account_stats(struct s_auth *account)
|
---|
155 | {
|
---|
156 | account->cwcacheexgot = 0;
|
---|
157 | account->cwcacheexpush = 0;
|
---|
158 | account->cwcacheexhit = 0;
|
---|
159 | }
|
---|
160 |
|
---|
161 | void cacheex_clear_client_stats(struct s_client *client)
|
---|
162 | {
|
---|
163 | client->cwcacheexgot = 0;
|
---|
164 | client->cwcacheexpush = 0;
|
---|
165 | client->cwcacheexhit = 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | int32_t cacheex_add_stats(struct s_client *cl, uint16_t caid, uint16_t srvid, uint32_t prid, uint8_t direction)
|
---|
169 | {
|
---|
170 | if(!cfg.cacheex_enable_stats)
|
---|
171 | { return -1; }
|
---|
172 |
|
---|
173 | // create list if doesn't exist
|
---|
174 | if(!cl->ll_cacheex_stats)
|
---|
175 | { cl->ll_cacheex_stats = ll_create("ll_cacheex_stats"); }
|
---|
176 |
|
---|
177 | time_t now = time((time_t *)0);
|
---|
178 | LL_ITER itr = ll_iter_create(cl->ll_cacheex_stats);
|
---|
179 | S_CACHEEX_STAT_ENTRY *cacheex_stats_entry;
|
---|
180 |
|
---|
181 | // check for existing entry
|
---|
182 | while((cacheex_stats_entry = ll_iter_next(&itr)))
|
---|
183 | {
|
---|
184 | if(cacheex_stats_entry->cache_srvid == srvid &&
|
---|
185 | cacheex_stats_entry->cache_caid == caid &&
|
---|
186 | cacheex_stats_entry->cache_prid == prid &&
|
---|
187 | cacheex_stats_entry->cache_direction == direction)
|
---|
188 | {
|
---|
189 | // we already have this entry - just add count and time
|
---|
190 | cacheex_stats_entry->cache_count++;
|
---|
191 | cacheex_stats_entry->cache_last = now;
|
---|
192 | return cacheex_stats_entry->cache_count;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | // if we land here we have to add a new entry
|
---|
197 | if(cs_malloc(&cacheex_stats_entry, sizeof(S_CACHEEX_STAT_ENTRY)))
|
---|
198 | {
|
---|
199 | cacheex_stats_entry->cache_caid = caid;
|
---|
200 | cacheex_stats_entry->cache_srvid = srvid;
|
---|
201 | cacheex_stats_entry->cache_prid = prid;
|
---|
202 | cacheex_stats_entry->cache_count = 1;
|
---|
203 | cacheex_stats_entry->cache_last = now;
|
---|
204 | cacheex_stats_entry->cache_direction = direction;
|
---|
205 | ll_iter_insert(&itr, cacheex_stats_entry);
|
---|
206 | return 1;
|
---|
207 | }
|
---|
208 | return 0;
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | int8_t cacheex_maxhop(struct s_client *cl)
|
---|
213 | {
|
---|
214 | int maxhop = 10;
|
---|
215 | if(cl->reader && cl->reader->cacheex.maxhop)
|
---|
216 | { maxhop = cl->reader->cacheex.maxhop; }
|
---|
217 | else if(cl->account && cl->account->cacheex.maxhop)
|
---|
218 | { maxhop = cl->account->cacheex.maxhop; }
|
---|
219 | return maxhop;
|
---|
220 | }
|
---|
221 |
|
---|
222 | static void cacheex_cache_push_to_client(struct s_client *cl, ECM_REQUEST *er)
|
---|
223 | {
|
---|
224 | add_job(cl, ACTION_CACHE_PUSH_OUT, er, 0);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * cacheex modes:
|
---|
229 | *
|
---|
230 | * cacheex=1 CACHE PULL:
|
---|
231 | * Situation: oscam A reader1 has cacheex=1, oscam B account1 has cacheex=1
|
---|
232 | * oscam A gets a ECM request, reader1 send this request to oscam B, oscam B checks his cache
|
---|
233 | * a. not found in cache: return NOK
|
---|
234 | * a. found in cache: return OK+CW
|
---|
235 | * b. not found in cache, but found pending request: wait max cacheexwaittime and check again
|
---|
236 | * oscam B never requests new ECMs
|
---|
237 | *
|
---|
238 | * CW-flow: B->A
|
---|
239 | *
|
---|
240 | * cacheex=2 CACHE PUSH:
|
---|
241 | * Situation: oscam A reader1 has cacheex=2, oscam B account1 has cacheex=2
|
---|
242 | * if oscam B gets a CW, its pushed to oscam A
|
---|
243 | * reader has normal functionality and can request ECMs
|
---|
244 | *
|
---|
245 | * Problem: oscam B can only push if oscam A is connected
|
---|
246 | * Problem or feature?: oscam A reader can request ecms from oscam B
|
---|
247 | *
|
---|
248 | * CW-flow: B->A
|
---|
249 | *
|
---|
250 | */
|
---|
251 | void cacheex_cache_push(ECM_REQUEST *er)
|
---|
252 | {
|
---|
253 | if(er->rc >= E_NOTFOUND) { return; }
|
---|
254 |
|
---|
255 | //cacheex=2 mode: push (server->remote)
|
---|
256 | struct s_client *cl;
|
---|
257 | cs_readlock(&clientlist_lock);
|
---|
258 | for(cl = first_client->next; cl; cl = cl->next)
|
---|
259 | {
|
---|
260 | if(check_client(cl) && er->cacheex_src != cl)
|
---|
261 | {
|
---|
262 | if(get_module(cl)->num == R_CSP) // always send to csp cl
|
---|
263 | {
|
---|
264 | if(!er->cacheex_src || cfg.csp.allow_reforward) { cacheex_cache_push_to_client(cl, er); } // but not if the origin was cacheex (might loop)
|
---|
265 | }
|
---|
266 | else if(cl->typ == 'c' && !cl->dup && cl->account && cl->account->cacheex.mode == 2) //send cache over user
|
---|
267 | {
|
---|
268 | if(get_module(cl)->c_cache_push // cache-push able
|
---|
269 | && (!er->grp || (cl->grp & er->grp)) //Group-check
|
---|
270 | /**** OUTGOING FILTER CHECK ***/
|
---|
271 | && (!er->selected_reader || !cacheex_reader(er->selected_reader) || !cfg.block_same_name || strcmp(username(cl), er->selected_reader->label)) //check reader mode-1 loopback by same name
|
---|
272 | && (!er->selected_reader || !cacheex_reader(er->selected_reader) || !cfg.block_same_ip || (check_client(er->selected_reader->client) && !IP_EQUAL(cl->ip, er->selected_reader->client->ip))) //check reader mode-1 loopback by same ip
|
---|
273 | && (!cl->account->cacheex.drop_csp || checkECMD5(er)) //cacheex_drop_csp-check
|
---|
274 | && chk_ctab(er->caid, &cl->ctab) //Caid-check
|
---|
275 | && (!checkECMD5(er) || chk_ident_filter(er->caid, er->prid, &cl->ftab)) //Ident-check (not for csp: prid=0 always!)
|
---|
276 | && chk_srvid(cl, er) //Service-check
|
---|
277 | )
|
---|
278 | {
|
---|
279 | cacheex_cache_push_to_client(cl, er);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 | cs_readunlock(&clientlist_lock);
|
---|
285 |
|
---|
286 |
|
---|
287 | //cacheex=3 mode: reverse push (reader->server)
|
---|
288 | cs_readlock(&readerlist_lock);
|
---|
289 | cs_readlock(&clientlist_lock);
|
---|
290 | struct s_reader *rdr;
|
---|
291 | for(rdr = first_active_reader; rdr; rdr = rdr->next)
|
---|
292 | {
|
---|
293 | cl = rdr->client;
|
---|
294 | if(check_client(cl) && er->cacheex_src != cl && rdr->cacheex.mode == 3) //send cache over reader
|
---|
295 | {
|
---|
296 | if(rdr->ph.c_cache_push // cache-push able
|
---|
297 | && (!er->grp || (rdr->grp & er->grp)) //Group-check
|
---|
298 | /**** OUTGOING FILTER CHECK ***/
|
---|
299 | && (!er->selected_reader || !cacheex_reader(er->selected_reader) || !cfg.block_same_name || strcmp(username(cl), er->selected_reader->label)) //check reader mode-1 loopback by same name
|
---|
300 | && (!er->selected_reader || !cacheex_reader(er->selected_reader) || !cfg.block_same_ip || (check_client(er->selected_reader->client) && !IP_EQUAL(cl->ip, er->selected_reader->client->ip))) //check reader mode-1 loopback by same ip
|
---|
301 | && (!rdr->cacheex.drop_csp || checkECMD5(er)) //cacheex_drop_csp-check
|
---|
302 | && chk_ctab(er->caid, &rdr->ctab) //Caid-check
|
---|
303 | && (!checkECMD5(er) || chk_ident_filter(er->caid, er->prid, &rdr->ftab)) //Ident-check (not for csp: prid=0 always!)
|
---|
304 | && chk_srvid(cl, er) //Service-check
|
---|
305 | )
|
---|
306 | {
|
---|
307 | cacheex_cache_push_to_client(cl, er);
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 | cs_readunlock(&clientlist_lock);
|
---|
312 | cs_readunlock(&readerlist_lock);
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | /**** INCOMING FILTER CHECK ***/
|
---|
317 | uint8_t check_cacheex_filter(struct s_client *cl, ECM_REQUEST *er)
|
---|
318 | {
|
---|
319 |
|
---|
320 | if(check_client(cl) && cl->typ == 'p' && cl->reader && cl->reader->cacheex.mode==2
|
---|
321 | && (!cl->reader->cacheex.drop_csp || checkECMD5(er)) //cacheex_drop_csp-check
|
---|
322 | && chk_ctab(er->caid, &cl->reader->ctab) //Caid-check
|
---|
323 | && (!checkECMD5(er) || chk_ident_filter(er->caid, er->prid, &cl->reader->ftab)) //Ident-check (not for csp: prid=0 always!)
|
---|
324 | && chk_srvid(cl, er) //Service-check
|
---|
325 | && chk_csp_ctab(er, &cl->reader->cacheex.filter_caidtab) //cacheex_ecm_filter -> for compatibility with old oscam
|
---|
326 | )
|
---|
327 | { return 1; }
|
---|
328 |
|
---|
329 | if(check_client(cl) && cl->typ == 'c' && cl->account && cl->account->cacheex.mode==3
|
---|
330 | && (!cl->account->cacheex.drop_csp || checkECMD5(er)) //cacheex_drop_csp-check
|
---|
331 | && chk_ctab(er->caid, &cl->ctab) //Caid-check
|
---|
332 | && (!checkECMD5(er) || chk_ident_filter(er->caid, er->prid, &cl->ftab)) //Ident-check (not for csp: prid=0 always!)
|
---|
333 | && chk_srvid(cl, er) //Service-check
|
---|
334 | && chk_csp_ctab(er, &cl->account->cacheex.filter_caidtab) //cacheex_ecm_filter -> for compatibility with old oscam
|
---|
335 | )
|
---|
336 | { return 1; }
|
---|
337 |
|
---|
338 | NULLFREE(er);
|
---|
339 | return 0;
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 |
|
---|
344 | static inline struct s_cacheex_matcher *is_cacheex_matcher_matching(ECM_REQUEST *from_er, ECM_REQUEST *to_er)
|
---|
345 | {
|
---|
346 | struct s_cacheex_matcher *entry = cfg.cacheex_matcher;
|
---|
347 | int8_t v_ok = (from_er && to_er) ? 2 : 1;
|
---|
348 | while(entry)
|
---|
349 | {
|
---|
350 | int8_t ok = 0;
|
---|
351 | if(from_er
|
---|
352 | && (!entry->caid || entry->caid == from_er->caid)
|
---|
353 | && (!entry->provid || entry->provid == from_er->prid)
|
---|
354 | && (!entry->srvid || entry->srvid == from_er->srvid)
|
---|
355 | && (!entry->chid || entry->chid == from_er->chid)
|
---|
356 | && (!entry->pid || entry->pid == from_er->pid)
|
---|
357 | && (!entry->ecmlen || entry->ecmlen == from_er->ecmlen))
|
---|
358 | { ok++; }
|
---|
359 |
|
---|
360 | if(to_er
|
---|
361 | && (!entry->to_caid || entry->to_caid == to_er->caid)
|
---|
362 | && (!entry->to_provid || entry->to_provid == to_er->prid)
|
---|
363 | && (!entry->to_srvid || entry->to_srvid == to_er->srvid)
|
---|
364 | && (!entry->to_chid || entry->to_chid == to_er->chid)
|
---|
365 | && (!entry->to_pid || entry->to_pid == to_er->pid)
|
---|
366 | && (!entry->to_ecmlen || entry->to_ecmlen == to_er->ecmlen))
|
---|
367 | { ok++; }
|
---|
368 |
|
---|
369 | if(ok == v_ok)
|
---|
370 | {
|
---|
371 | if(!from_er || !to_er || from_er->srvid == to_er->srvid)
|
---|
372 | { return entry; }
|
---|
373 | }
|
---|
374 | entry = entry->next;
|
---|
375 | }
|
---|
376 | return NULL;
|
---|
377 | }
|
---|
378 |
|
---|
379 | bool cacheex_is_match_alias(struct s_client *cl, ECM_REQUEST *er)
|
---|
380 | {
|
---|
381 | return check_client(cl) && cl->account && cl->account->cacheex.mode == 1 && is_cacheex_matcher_matching(NULL, er);
|
---|
382 | }
|
---|
383 |
|
---|
384 | inline int8_t cacheex_match_alias(struct s_client *cl, ECM_REQUEST *er, ECM_REQUEST *ecm)
|
---|
385 | {
|
---|
386 | if(check_client(cl) && cl->account && cl->account->cacheex.mode == 1)
|
---|
387 | {
|
---|
388 | struct s_cacheex_matcher *entry = is_cacheex_matcher_matching(ecm, er);
|
---|
389 | if(entry)
|
---|
390 | {
|
---|
391 | int32_t diff = comp_timeb(&er->tps, &ecm->tps);
|
---|
392 | if(diff > entry->valid_from && diff < entry->valid_to)
|
---|
393 | {
|
---|
394 | #ifdef WITH_DEBUG
|
---|
395 | if(D_CACHEEX & cs_dblevel)
|
---|
396 | {
|
---|
397 | char result[CXM_FMT_LEN] = { 0 };
|
---|
398 | int32_t s, size = CXM_FMT_LEN;
|
---|
399 | s = ecmfmt(entry->caid, 0, entry->provid, entry->chid, entry->pid, entry->srvid, entry->ecmlen, 0, 0, 0, result, size, 0, 0);
|
---|
400 | s += snprintf(result + s, size - s, " = ");
|
---|
401 | s += ecmfmt(entry->to_caid, 0, entry->to_provid, entry->to_chid, entry->to_pid, entry->to_srvid, entry->to_ecmlen, 0, 0, 0, result + s, size - s, 0, 0);
|
---|
402 | s += snprintf(result + s, size - s, " valid %d/%d", entry->valid_from, entry->valid_to);
|
---|
403 | cs_debug_mask(D_CACHEEX, "cacheex-matching for: %s", result);
|
---|
404 | }
|
---|
405 | #endif
|
---|
406 | return 1;
|
---|
407 | }
|
---|
408 | }
|
---|
409 | }
|
---|
410 | return 0;
|
---|
411 | }
|
---|
412 |
|
---|
413 | static int32_t cacheex_add_to_cache_int(struct s_client *cl, ECM_REQUEST *er, int8_t csp)
|
---|
414 | {
|
---|
415 | if(er->rc >= E_NOTFOUND) { return 0; }
|
---|
416 |
|
---|
417 | if(!cl)
|
---|
418 | { return 0; }
|
---|
419 | if(!csp && cl->reader && cl->reader->cacheex.mode != 2) //from reader
|
---|
420 | {
|
---|
421 | cs_debug_mask(D_CACHEEX, "CACHEX received, but disabled for %s", username(cl));
|
---|
422 | return 0;
|
---|
423 | }
|
---|
424 | if(!csp && !cl->reader && cl->account && cl->account->cacheex.mode != 3) //from user
|
---|
425 | {
|
---|
426 | cs_debug_mask(D_CACHEEX, "CACHEX received, but disabled for %s", username(cl));
|
---|
427 | return 0;
|
---|
428 | }
|
---|
429 | if(!csp && !cl->reader && !cl->account) //not active!
|
---|
430 | {
|
---|
431 | cs_debug_mask(D_CACHEEX, "CACHEX received, but invalid client state %s", username(cl));
|
---|
432 | return 0;
|
---|
433 | }
|
---|
434 |
|
---|
435 | uint8_t i, c;
|
---|
436 | uint8_t null = 0;
|
---|
437 | for(i = 0; i < 16; i += 4)
|
---|
438 | {
|
---|
439 | c = ((er->cw[i] + er->cw[i + 1] + er->cw[i + 2]) & 0xff);
|
---|
440 | null |= (er->cw[i] | er->cw[i + 1] | er->cw[i + 2]);
|
---|
441 | if(er->cw[i + 3] != c)
|
---|
442 | {
|
---|
443 | cs_ddump_mask(D_CACHEEX, er->cw, 16, "push received cw with chksum error from %s", csp ? "csp" : username(cl));
|
---|
444 | cl->cwcacheexerr++;
|
---|
445 | if(cl->account)
|
---|
446 | { cl->account->cwcacheexerr++; }
|
---|
447 | return 0;
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | if(null == 0 || chk_is_null_CW(er->cw))
|
---|
452 | {
|
---|
453 | cs_ddump_mask(D_CACHEEX, er->cw, 16, "push received null cw from %s", csp ? "csp" : username(cl));
|
---|
454 | cl->cwcacheexerr++;
|
---|
455 | if(cl->account)
|
---|
456 | { cl->account->cwcacheexerr++; }
|
---|
457 | return 0;
|
---|
458 | }
|
---|
459 |
|
---|
460 | er->grp |= cl->grp; //ok for mode2 reader too: cl->reader->grp
|
---|
461 | er->rc = E_CACHEEX;
|
---|
462 | er->cacheex_src = cl;
|
---|
463 | er->selected_reader = cl->reader;
|
---|
464 | er->client = NULL; //No Owner! So no fallback!
|
---|
465 |
|
---|
466 | if(check_client(cl))
|
---|
467 | {
|
---|
468 | cl->cwcacheexgot++;
|
---|
469 | if(cl->account)
|
---|
470 | { cl->account->cwcacheexgot++; }
|
---|
471 | first_client->cwcacheexgot++;
|
---|
472 | }
|
---|
473 |
|
---|
474 | add_hitcache(cl, er); //we have to call it before add_cache, because in chk_process we could remove it!
|
---|
475 | add_cache(er);
|
---|
476 | cacheex_add_stats(cl, er->caid, er->srvid, er->prid, 1);
|
---|
477 |
|
---|
478 | cs_writelock(&ecm_pushed_deleted_lock);
|
---|
479 | er->next = ecm_pushed_deleted;
|
---|
480 | ecm_pushed_deleted = er;
|
---|
481 | cs_writeunlock(&ecm_pushed_deleted_lock);
|
---|
482 |
|
---|
483 | return 1; //NO free, we have to wait cache push out stuff ends.
|
---|
484 | }
|
---|
485 |
|
---|
486 |
|
---|
487 | void cacheex_add_to_cache(struct s_client *cl, ECM_REQUEST *er)
|
---|
488 | {
|
---|
489 | er->from_cacheex = 1;
|
---|
490 | if(!cacheex_add_to_cache_int(cl, er, 0))
|
---|
491 | { free_push_in_ecm(er); }
|
---|
492 | }
|
---|
493 |
|
---|
494 | void cacheex_add_to_cache_from_csp(struct s_client *cl, ECM_REQUEST *er)
|
---|
495 | {
|
---|
496 | if(!cacheex_add_to_cache_int(cl, er, 1))
|
---|
497 | { free_push_in_ecm(er); }
|
---|
498 | }
|
---|
499 |
|
---|
500 | //Format:
|
---|
501 | //caid:prov:srvid:pid:chid:ecmlen=caid:prov:srvid:pid:chid:ecmlen[,validfrom,validto]
|
---|
502 | //validfrom: default=-2000
|
---|
503 | //validto: default=4000
|
---|
504 | //valid time if found in cache
|
---|
505 | static struct s_cacheex_matcher *cacheex_matcher_read_int(void)
|
---|
506 | {
|
---|
507 | FILE *fp = open_config_file(cs_cacheex_matcher);
|
---|
508 | if(!fp)
|
---|
509 | { return NULL; }
|
---|
510 |
|
---|
511 | char token[1024];
|
---|
512 | unsigned char type;
|
---|
513 | int32_t i, ret, count = 0;
|
---|
514 | struct s_cacheex_matcher *new_cacheex_matcher = NULL, *entry, *last = NULL;
|
---|
515 | uint32_t line = 0;
|
---|
516 |
|
---|
517 | while(fgets(token, sizeof(token), fp))
|
---|
518 | {
|
---|
519 | line++;
|
---|
520 | if(strlen(token) <= 1) { continue; }
|
---|
521 | if(token[0] == '#' || token[0] == '/') { continue; }
|
---|
522 | if(strlen(token) > 100) { continue; }
|
---|
523 |
|
---|
524 | for(i = 0; i < (int)strlen(token); i++)
|
---|
525 | {
|
---|
526 | if((token[i] == ':' || token[i] == ' ') && token[i + 1] == ':')
|
---|
527 | {
|
---|
528 | memmove(token + i + 2, token + i + 1, strlen(token) - i + 1);
|
---|
529 | token[i + 1] = '0';
|
---|
530 | }
|
---|
531 | if(token[i] == '#' || token[i] == '/')
|
---|
532 | {
|
---|
533 | token[i] = '\0';
|
---|
534 | break;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | type = 'm';
|
---|
539 | uint32_t caid = 0, provid = 0, srvid = 0, pid = 0, chid = 0, ecmlen = 0;
|
---|
540 | uint32_t to_caid = 0, to_provid = 0, to_srvid = 0, to_pid = 0, to_chid = 0, to_ecmlen = 0;
|
---|
541 | int32_t valid_from = -2000, valid_to = 4000;
|
---|
542 |
|
---|
543 | ret = sscanf(token, "%c:%4x:%6x:%4x:%4x:%4x:%4X=%4x:%6x:%4x:%4x:%4x:%4X,%4d,%4d",
|
---|
544 | &type,
|
---|
545 | &caid, &provid, &srvid, &pid, &chid, &ecmlen,
|
---|
546 | &to_caid, &to_provid, &to_srvid, &to_pid, &to_chid, &to_ecmlen,
|
---|
547 | &valid_from, &valid_to);
|
---|
548 |
|
---|
549 | type = tolower(type);
|
---|
550 |
|
---|
551 | if(ret < 7 || type != 'm')
|
---|
552 | { continue; }
|
---|
553 |
|
---|
554 | if(!cs_malloc(&entry, sizeof(struct s_cacheex_matcher)))
|
---|
555 | {
|
---|
556 | fclose(fp);
|
---|
557 | return new_cacheex_matcher;
|
---|
558 | }
|
---|
559 | count++;
|
---|
560 | entry->line = line;
|
---|
561 | entry->type = type;
|
---|
562 | entry->caid = caid;
|
---|
563 | entry->provid = provid;
|
---|
564 | entry->srvid = srvid;
|
---|
565 | entry->pid = pid;
|
---|
566 | entry->chid = chid;
|
---|
567 | entry->ecmlen = ecmlen;
|
---|
568 | entry->to_caid = to_caid;
|
---|
569 | entry->to_provid = to_provid;
|
---|
570 | entry->to_srvid = to_srvid;
|
---|
571 | entry->to_pid = to_pid;
|
---|
572 | entry->to_chid = to_chid;
|
---|
573 | entry->to_ecmlen = to_ecmlen;
|
---|
574 | entry->valid_from = valid_from;
|
---|
575 | entry->valid_to = valid_to;
|
---|
576 |
|
---|
577 | cs_debug_mask(D_TRACE, "cacheex-matcher: %c: %04X:%06X:%04X:%04X:%04X:%02X = %04X:%06X:%04X:%04X:%04X:%02X valid %d/%d",
|
---|
578 | entry->type, entry->caid, entry->provid, entry->srvid, entry->pid, entry->chid, entry->ecmlen,
|
---|
579 | entry->to_caid, entry->to_provid, entry->to_srvid, entry->to_pid, entry->to_chid, entry->to_ecmlen,
|
---|
580 | entry->valid_from, entry->valid_to);
|
---|
581 |
|
---|
582 | if(!new_cacheex_matcher)
|
---|
583 | {
|
---|
584 | new_cacheex_matcher = entry;
|
---|
585 | last = new_cacheex_matcher;
|
---|
586 | }
|
---|
587 | else
|
---|
588 | {
|
---|
589 | last->next = entry;
|
---|
590 | last = entry;
|
---|
591 | }
|
---|
592 | }
|
---|
593 |
|
---|
594 | if(count)
|
---|
595 | { cs_log("%d entries read from %s", count, cs_cacheex_matcher); }
|
---|
596 |
|
---|
597 | fclose(fp);
|
---|
598 |
|
---|
599 | return new_cacheex_matcher;
|
---|
600 | }
|
---|
601 |
|
---|
602 | void cacheex_load_config_file(void)
|
---|
603 | {
|
---|
604 | struct s_cacheex_matcher *entry, *old_list;
|
---|
605 |
|
---|
606 | old_list = cfg.cacheex_matcher;
|
---|
607 | cfg.cacheex_matcher = cacheex_matcher_read_int();
|
---|
608 |
|
---|
609 | while(old_list)
|
---|
610 | {
|
---|
611 | entry = old_list->next;
|
---|
612 | NULLFREE(old_list);
|
---|
613 | old_list = entry;
|
---|
614 | }
|
---|
615 | }
|
---|
616 |
|
---|
617 |
|
---|
618 |
|
---|
619 | CWCHECK get_cwcheck(ECM_REQUEST *er){
|
---|
620 | int32_t i;
|
---|
621 | int8_t mode = 0;
|
---|
622 | int16_t counter = 1;
|
---|
623 |
|
---|
624 | for(i = 0; i < cfg.cacheex_cwcheck_tab.n; i++)
|
---|
625 | {
|
---|
626 | if(i == 0 && cfg.cacheex_cwcheck_tab.caid[i] <= 0)
|
---|
627 | {
|
---|
628 | mode = cfg.cacheex_cwcheck_tab.mode[i];
|
---|
629 | counter = cfg.cacheex_cwcheck_tab.counter[i];
|
---|
630 | continue; //check other, only valid for unset
|
---|
631 | }
|
---|
632 |
|
---|
633 | if(cfg.cacheex_cwcheck_tab.caid[i] == er->caid || cfg.cacheex_cwcheck_tab.caid[i] == er->caid >> 8 || ((cfg.cacheex_cwcheck_tab.cmask[i] >= 0 && (er->caid & cfg.cacheex_cwcheck_tab.cmask[i]) == cfg.cacheex_cwcheck_tab.caid[i]) || cfg.cacheex_cwcheck_tab.caid[i] == -1))
|
---|
634 | {
|
---|
635 | if((cfg.cacheex_cwcheck_tab.prid[i] >= 0 && cfg.cacheex_cwcheck_tab.prid[i] == (int32_t)er->prid) || cfg.cacheex_cwcheck_tab.prid[i] == -1)
|
---|
636 | {
|
---|
637 | if((cfg.cacheex_cwcheck_tab.srvid[i] >= 0 && cfg.cacheex_cwcheck_tab.srvid[i] == er->srvid) || cfg.cacheex_cwcheck_tab.srvid[i] == -1)
|
---|
638 | {
|
---|
639 | mode = cfg.cacheex_cwcheck_tab.mode[i];
|
---|
640 | counter = cfg.cacheex_cwcheck_tab.counter[i];
|
---|
641 | break;
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | }
|
---|
646 | }
|
---|
647 |
|
---|
648 | //check for correct values
|
---|
649 | if(mode>2 || mode<0) mode=0;
|
---|
650 | if(counter<1) counter=1;
|
---|
651 |
|
---|
652 | CWCHECK check_cw;
|
---|
653 | memset(&check_cw, 0, sizeof(CWCHECK));
|
---|
654 | check_cw.mode = mode;
|
---|
655 | check_cw.counter = counter;
|
---|
656 |
|
---|
657 | return check_cw;
|
---|
658 | }
|
---|
659 |
|
---|
660 |
|
---|
661 | uint32_t get_cacheex_wait_time(ECM_REQUEST *er, struct s_client *cl)
|
---|
662 | {
|
---|
663 | int32_t i, dwtime = -1, awtime = -1;
|
---|
664 |
|
---|
665 | for(i = 0; i < cfg.cacheex_wait_timetab.n; i++)
|
---|
666 | {
|
---|
667 | if(i == 0 && cfg.cacheex_wait_timetab.caid[i] <= 0)
|
---|
668 | {
|
---|
669 | dwtime = cfg.cacheex_wait_timetab.dwtime[i];
|
---|
670 | awtime = cfg.cacheex_wait_timetab.awtime[i];
|
---|
671 | continue; //check other, only valid for unset
|
---|
672 | }
|
---|
673 |
|
---|
674 | if(cfg.cacheex_wait_timetab.caid[i] == er->caid || cfg.cacheex_wait_timetab.caid[i] == er->caid >> 8 || ((cfg.cacheex_wait_timetab.cmask[i] >= 0 && (er->caid & cfg.cacheex_wait_timetab.cmask[i]) == cfg.cacheex_wait_timetab.caid[i]) || cfg.cacheex_wait_timetab.caid[i] == -1))
|
---|
675 | {
|
---|
676 | if((cfg.cacheex_wait_timetab.prid[i] >= 0 && cfg.cacheex_wait_timetab.prid[i] == (int32_t)er->prid) || cfg.cacheex_wait_timetab.prid[i] == -1)
|
---|
677 | {
|
---|
678 | if((cfg.cacheex_wait_timetab.srvid[i] >= 0 && cfg.cacheex_wait_timetab.srvid[i] == er->srvid) || cfg.cacheex_wait_timetab.srvid[i] == -1)
|
---|
679 | {
|
---|
680 | dwtime = cfg.cacheex_wait_timetab.dwtime[i];
|
---|
681 | awtime = cfg.cacheex_wait_timetab.awtime[i];
|
---|
682 | break;
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | }
|
---|
687 |
|
---|
688 | }
|
---|
689 | if(awtime > 0 && (dwtime <= 0 || awtime==dwtime) ) //if awtime==dwtime useless check hitcache
|
---|
690 | {
|
---|
691 | return awtime;
|
---|
692 | }
|
---|
693 | if(cl == NULL)
|
---|
694 | {
|
---|
695 | if(dwtime < 0)
|
---|
696 | { dwtime = 0; }
|
---|
697 | return dwtime;
|
---|
698 | }
|
---|
699 | if(awtime > 0 || dwtime > 0)
|
---|
700 | {
|
---|
701 | //if found last in cache return dynwaittime else alwayswaittime
|
---|
702 | if(check_hitcache(er,cl))
|
---|
703 | { return dwtime >= awtime ? dwtime : awtime; }
|
---|
704 | else
|
---|
705 | { return awtime > 0 ? awtime : 0; }
|
---|
706 | }
|
---|
707 | return 0;
|
---|
708 | }
|
---|
709 |
|
---|
710 | int32_t chk_csp_ctab(ECM_REQUEST *er, CECSPVALUETAB *tab)
|
---|
711 | {
|
---|
712 | if(!er->caid || !tab->n)
|
---|
713 | { return 1; } // nothing setup we add all
|
---|
714 | int32_t i;
|
---|
715 | for(i = 0; i < tab->n; i++)
|
---|
716 | {
|
---|
717 |
|
---|
718 | if(tab->caid[i] > 0)
|
---|
719 | {
|
---|
720 | if(tab->caid[i] == er->caid || tab->caid[i] == er->caid >> 8 || ((tab->cmask[i] >= 0 && (er->caid & tab->cmask[i]) == tab->caid[i]) || tab->caid[i] == -1))
|
---|
721 | {
|
---|
722 | if((tab->prid[i] >= 0 && tab->prid[i] == (int32_t)er->prid) || tab->prid[i] == -1)
|
---|
723 | {
|
---|
724 | if((tab->srvid[i] >= 0 && tab->srvid[i] == er->srvid) || tab->srvid[i] == -1)
|
---|
725 | {
|
---|
726 | return 1;
|
---|
727 | }
|
---|
728 | }
|
---|
729 | }
|
---|
730 | }
|
---|
731 | }
|
---|
732 | return 0;
|
---|
733 | }
|
---|
734 |
|
---|
735 | #endif
|
---|