1 | #include "globals.h"
|
---|
2 | #include "cscrypt/md5.h"
|
---|
3 | #include "module-anticasc.h"
|
---|
4 | #include "module-cacheex.h"
|
---|
5 | #include "module-led.h"
|
---|
6 | #include "module-stat.h"
|
---|
7 | #include "module-webif.h"
|
---|
8 | #include "module-ird-guess.h"
|
---|
9 | #include "module-cw-cycle-check.h"
|
---|
10 | #include "module-gbox.h"
|
---|
11 | #include "oscam-cache.h"
|
---|
12 | #include "oscam-chk.h"
|
---|
13 | #include "oscam-client.h"
|
---|
14 | #include "oscam-config.h"
|
---|
15 | #include "oscam-ecm.h"
|
---|
16 | #include "oscam-garbage.h"
|
---|
17 | #include "oscam-failban.h"
|
---|
18 | #include "oscam-net.h"
|
---|
19 | #include "oscam-time.h"
|
---|
20 | #include "oscam-lock.h"
|
---|
21 | #include "oscam-string.h"
|
---|
22 | #include "oscam-work.h"
|
---|
23 | #include "reader-common.h"
|
---|
24 |
|
---|
25 | extern CS_MUTEX_LOCK ecmcache_lock;
|
---|
26 | extern struct ecm_request_t *ecmcwcache;
|
---|
27 | extern uint16_t len4caid[256];
|
---|
28 | extern uint32_t ecmcwcache_size;
|
---|
29 | extern int32_t exit_oscam;
|
---|
30 |
|
---|
31 | extern CS_MUTEX_LOCK ecm_pushed_deleted_lock;
|
---|
32 | extern struct ecm_request_t *ecm_pushed_deleted;
|
---|
33 |
|
---|
34 | static pthread_mutex_t cw_process_sleep_cond_mutex;
|
---|
35 | static pthread_cond_t cw_process_sleep_cond;
|
---|
36 | static int cw_process_wakeups;
|
---|
37 |
|
---|
38 | static uint32_t auto_timeout(ECM_REQUEST *er, uint32_t timeout)
|
---|
39 | {
|
---|
40 | (void)er; // Prevent warning about unused er, when WITH_LB is disabled
|
---|
41 | #ifdef WITH_LB
|
---|
42 | if(cfg.lb_auto_timeout)
|
---|
43 | { return lb_auto_timeout(er, timeout); }
|
---|
44 | #endif
|
---|
45 | return timeout;
|
---|
46 | }
|
---|
47 |
|
---|
48 | #ifdef CS_CACHEEX
|
---|
49 | void cacheex_timeout(ECM_REQUEST *er)
|
---|
50 | {
|
---|
51 | er->cacheex_wait_time_expired = 1;
|
---|
52 |
|
---|
53 | //if check_cw mode=0, first try to get cw from cache without check counter!
|
---|
54 | CWCHECK check_cw = get_cwcheck(er);
|
---|
55 | if(!check_cw.mode)
|
---|
56 | {
|
---|
57 | struct ecm_request_t *ecm=NULL;
|
---|
58 | ecm = check_cache(er, er->client);
|
---|
59 | if(ecm) //found in cache
|
---|
60 | {
|
---|
61 | struct s_write_from_cache *wfc=NULL;
|
---|
62 | if(!cs_malloc(&wfc, sizeof(struct s_write_from_cache)))
|
---|
63 | {
|
---|
64 | NULLFREE(ecm);
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | wfc->er_new=er;
|
---|
68 | wfc->er_cache=ecm;
|
---|
69 |
|
---|
70 | if(!add_job(er->client, ACTION_ECM_ANSWER_CACHE, wfc, sizeof(struct s_write_from_cache))) //write_ecm_answer_fromcache
|
---|
71 | { NULLFREE(ecm); }
|
---|
72 |
|
---|
73 | return;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | if(er->rc >= E_UNHANDLED && er->stage < 2 && er->cacheex_wait_time)
|
---|
79 | {
|
---|
80 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} cacheex timeout! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
|
---|
81 |
|
---|
82 | //check if "normal" readers selected, if not send NOT FOUND!
|
---|
83 | if( !cfg.wait_until_ctimeout
|
---|
84 | &&
|
---|
85 | (
|
---|
86 | (!er->from_cacheex1_client && (er->reader_count + er->fallback_reader_count - er->cacheex_reader_count) <= 0) //not-cacheex-1 client and no normal readers available (or filtered by LB)
|
---|
87 | ||
|
---|
88 | (er->from_cacheex1_client && !er->reader_nocacheex_avail) //cacheex1-client and no normal readers available for others clients
|
---|
89 | )
|
---|
90 | )
|
---|
91 | {
|
---|
92 | er->rc = E_NOTFOUND;
|
---|
93 | er->selected_reader = NULL;
|
---|
94 | er->rcEx = 0;
|
---|
95 |
|
---|
96 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} NO \"normal\" readers... not_found! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
|
---|
97 | send_dcw(er->client, er);
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | debug_ecm(D_TRACE, "request for %s %s", username(er->client), buf);
|
---|
103 | request_cw_from_readers(er, 0);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | void fallback_timeout(ECM_REQUEST *er)
|
---|
110 | {
|
---|
111 | if(er->rc >= E_UNHANDLED && er->stage < 4)
|
---|
112 | {
|
---|
113 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} fallback timeout! (stage: %d)", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid, er->stage);
|
---|
114 | debug_ecm(D_TRACE, "fallback for %s %s", username(er->client), buf);
|
---|
115 | while(er->stage < 4) //if preferlocalcards=1 and no answer from locals, initial stage will be 2! We need to reach stage=4 to call fallback's.
|
---|
116 | {
|
---|
117 | request_cw_from_readers(er, 0);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | void ecm_timeout(ECM_REQUEST *er)
|
---|
123 | {
|
---|
124 | if(!er->readers_timeout_check)
|
---|
125 | {
|
---|
126 | er->readers_timeout_check = 1;
|
---|
127 |
|
---|
128 | if(check_client(er->client) && er->rc >= E_UNHANDLED)
|
---|
129 | {
|
---|
130 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} client timeout! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
|
---|
131 | debug_ecm(D_TRACE, "timeout for %s %s", username(er->client), buf);
|
---|
132 | }
|
---|
133 |
|
---|
134 | #ifdef CS_CACHEEX
|
---|
135 | bool send_timeout = false;
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | struct s_ecm_answer *ea_list;
|
---|
139 | for(ea_list = er->matching_rdr; ea_list; ea_list = ea_list->next)
|
---|
140 | {
|
---|
141 | if((ea_list->status & (REQUEST_SENT | REQUEST_ANSWERED)) == REQUEST_SENT) //Request sent, but no answer!
|
---|
142 | {
|
---|
143 | write_ecm_answer(ea_list->reader, er, E_TIMEOUT, 0, NULL, NULL); //set timeout for readers not answered!
|
---|
144 | #ifdef CS_CACHEEX
|
---|
145 | send_timeout = true;
|
---|
146 | #endif
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | #ifdef CS_CACHEEX
|
---|
151 | if(er->from_cacheex1_client && !send_timeout && er->rc == E_UNHANDLED) //cacheex 1 clients waiting for INT cache by "normal" readers
|
---|
152 | {
|
---|
153 | er->rc = E_NOTFOUND;
|
---|
154 | er->selected_reader = NULL;
|
---|
155 | if(er->reader_requested > 0)
|
---|
156 | { er->rcEx = 0; } //not found by reader
|
---|
157 | else
|
---|
158 | { er->rcEx = E2_GROUP; } //no matching reader
|
---|
159 |
|
---|
160 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} NO cw found in cache for cacheex-1 client... send not found! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
|
---|
161 | send_dcw(er->client, er);
|
---|
162 | }
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | void increment_n_request(struct s_client *cl){
|
---|
169 | if(check_client(cl)){
|
---|
170 | cl->n_request[1]++;
|
---|
171 | first_client->n_request[1]++;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | void update_n_request(void){
|
---|
176 | struct s_client *cl;
|
---|
177 |
|
---|
178 | cs_readlock(&clientlist_lock);
|
---|
179 | for(cl = first_client->next; cl; cl = cl->next)
|
---|
180 | {
|
---|
181 | #ifdef CS_CACHEEX
|
---|
182 | if(check_client(cl) && get_module(cl)->num != R_CSP && cl->typ == 'c' && !cl->dup && cl->account && cl->account->cacheex.mode<=1) //no cacheex 2/3 client
|
---|
183 | #else
|
---|
184 | if(check_client(cl) && get_module(cl)->num != R_CSP && cl->typ == 'c' && !cl->dup)
|
---|
185 | #endif
|
---|
186 | {
|
---|
187 | cl->n_request[0]=cl->n_request[1];
|
---|
188 | cl->n_request[1]=0;
|
---|
189 | }else{
|
---|
190 | cl->n_request[0]=0;
|
---|
191 | cl->n_request[1]=0;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | first_client->n_request[0]=first_client->n_request[1];
|
---|
196 | first_client->n_request[1]=0;
|
---|
197 |
|
---|
198 | cs_readunlock(&clientlist_lock);
|
---|
199 | }
|
---|
200 |
|
---|
201 | static void *cw_process(void)
|
---|
202 | {
|
---|
203 | set_thread_name(__func__);
|
---|
204 | int64_t time_to_check_fbtimeout, time_to_check_ctimeout, next_check, ecmc_next, cache_next, n_request_next, msec_wait = 3000;
|
---|
205 | struct timeb t_now, tbc, ecmc_time, cache_time, n_request_time;
|
---|
206 | ECM_REQUEST *er = NULL;
|
---|
207 | time_t ecm_maxcachetime;
|
---|
208 |
|
---|
209 | #ifdef CS_CACHEEX
|
---|
210 | int64_t time_to_check_cacheex_wait_time;
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | cs_pthread_cond_init(&cw_process_sleep_cond_mutex, &cw_process_sleep_cond);
|
---|
214 |
|
---|
215 | #ifdef CS_ANTICASC
|
---|
216 | int32_t ac_next;
|
---|
217 | struct timeb ac_time;
|
---|
218 | cs_ftime(&ac_time);
|
---|
219 | add_ms_to_timeb(&ac_time, cfg.ac_stime * 60 * 1000);
|
---|
220 | #endif
|
---|
221 |
|
---|
222 | cs_ftime(&ecmc_time);
|
---|
223 | add_ms_to_timeb(&ecmc_time, 1000);
|
---|
224 | cs_ftime(&cache_time);
|
---|
225 | add_ms_to_timeb(&cache_time, 3000);
|
---|
226 | cs_ftime(&n_request_time);
|
---|
227 | add_ms_to_timeb(&n_request_time, 60 * 1000);
|
---|
228 |
|
---|
229 | while(!exit_oscam)
|
---|
230 | {
|
---|
231 | if(cw_process_wakeups == 0) // No waiting wakeups, proceed to sleep
|
---|
232 | {
|
---|
233 | sleepms_on_cond(&cw_process_sleep_cond_mutex, &cw_process_sleep_cond, msec_wait);
|
---|
234 | }
|
---|
235 | cw_process_wakeups = 0; // We've been woken up, reset the counter
|
---|
236 | if(exit_oscam)
|
---|
237 | { break; }
|
---|
238 |
|
---|
239 | next_check = 0;
|
---|
240 | #ifdef CS_ANTICASC
|
---|
241 | ac_next = 0;
|
---|
242 | #endif
|
---|
243 | ecmc_next = 0;
|
---|
244 | cache_next = 0;
|
---|
245 | msec_wait = 0;
|
---|
246 |
|
---|
247 | cs_ftime(&t_now);
|
---|
248 | cs_readlock(&ecmcache_lock);
|
---|
249 | for(er = ecmcwcache; er; er = er->next)
|
---|
250 | {
|
---|
251 |
|
---|
252 | if(
|
---|
253 | (er->from_cacheex || er->from_csp) //ignore ecms from cacheex/csp
|
---|
254 | ||
|
---|
255 | er->readers_timeout_check //ignore already checked
|
---|
256 | ||
|
---|
257 | !check_client(er->client) //ignore ecm of killed clients
|
---|
258 | )
|
---|
259 | { continue; }
|
---|
260 |
|
---|
261 | if(er->rc >= E_UNHANDLED)
|
---|
262 | {
|
---|
263 |
|
---|
264 | #ifdef CS_CACHEEX
|
---|
265 | //cacheex_wait_time
|
---|
266 | if(er->stage < 2 && er->cacheex_wait_time)
|
---|
267 | {
|
---|
268 | tbc = er->tps;
|
---|
269 | time_to_check_cacheex_wait_time = add_ms_to_timeb_diff(&tbc, auto_timeout(er, er->cacheex_wait_time));
|
---|
270 | if(comp_timeb(&t_now, &tbc) >= 0)
|
---|
271 | {
|
---|
272 | add_job(er->client, ACTION_CACHEEX_TIMEOUT, (void *)er, 0);
|
---|
273 | time_to_check_cacheex_wait_time = 0;
|
---|
274 | }
|
---|
275 | if(!next_check || (time_to_check_cacheex_wait_time > 0 && time_to_check_cacheex_wait_time < next_check))
|
---|
276 | { next_check = time_to_check_cacheex_wait_time; }
|
---|
277 | }
|
---|
278 | #endif
|
---|
279 | if(er->stage < 4)
|
---|
280 | {
|
---|
281 | //fbtimeout
|
---|
282 | tbc = er->tps;
|
---|
283 | time_to_check_fbtimeout = add_ms_to_timeb_diff(&tbc, auto_timeout(er, get_fallbacktimeout(er->caid)));
|
---|
284 | if(comp_timeb(&t_now, &tbc) >= 0)
|
---|
285 | {
|
---|
286 | add_job(er->client, ACTION_FALLBACK_TIMEOUT, (void *)er, 0);
|
---|
287 | time_to_check_fbtimeout = 0;
|
---|
288 | }
|
---|
289 | if(!next_check || (time_to_check_fbtimeout > 0 && time_to_check_fbtimeout < next_check))
|
---|
290 | { next_check = time_to_check_fbtimeout; }
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | //clienttimeout
|
---|
296 | if(!er->readers_timeout_check) //ecm stays in cache at least ctimeout+2seconds!
|
---|
297 | {
|
---|
298 | tbc = er->tps;
|
---|
299 | time_to_check_ctimeout = add_ms_to_timeb_diff(&tbc, auto_timeout(er, cfg.ctimeout));
|
---|
300 | if(comp_timeb(&t_now, &tbc) >= 0)
|
---|
301 | {
|
---|
302 | add_job(er->client, ACTION_CLIENT_TIMEOUT, (void *)er, 0);
|
---|
303 | time_to_check_ctimeout = 0;
|
---|
304 | }
|
---|
305 | if(!next_check || (time_to_check_ctimeout > 0 && time_to_check_ctimeout < next_check))
|
---|
306 | { next_check = time_to_check_ctimeout; }
|
---|
307 | }
|
---|
308 | }
|
---|
309 | cs_readunlock(&ecmcache_lock);
|
---|
310 | #ifdef CS_ANTICASC
|
---|
311 | if(cfg.ac_enabled && (ac_next = comp_timeb(&ac_time, &t_now)) <= 10)
|
---|
312 | {
|
---|
313 | ac_do_stat();
|
---|
314 | cs_ftime(&ac_time);
|
---|
315 | ac_next = add_ms_to_timeb_diff(&ac_time, cfg.ac_stime * 60 * 1000);
|
---|
316 | }
|
---|
317 | #endif
|
---|
318 | if((ecmc_next = comp_timeb(&ecmc_time, &t_now)) <= 10)
|
---|
319 | {
|
---|
320 | uint32_t count = 0;
|
---|
321 | struct ecm_request_t *ecm, *ecmt = NULL, *prv;
|
---|
322 |
|
---|
323 | cs_readlock(&ecmcache_lock);
|
---|
324 | for(ecm = ecmcwcache, prv = NULL; ecm; prv = ecm, ecm = ecm->next, count++)
|
---|
325 | {
|
---|
326 | ecm_maxcachetime = t_now.time - ((cfg.ctimeout+500)/1000+3); //to be sure no more access er!
|
---|
327 |
|
---|
328 | if(ecm->tps.time < ecm_maxcachetime)
|
---|
329 | {
|
---|
330 | cs_readunlock(&ecmcache_lock);
|
---|
331 | cs_writelock(&ecmcache_lock);
|
---|
332 | ecmt = ecm;
|
---|
333 | if(prv)
|
---|
334 | { prv->next = NULL; }
|
---|
335 | else
|
---|
336 | { ecmcwcache = NULL; }
|
---|
337 | cs_writeunlock(&ecmcache_lock);
|
---|
338 | break;
|
---|
339 | }
|
---|
340 | }
|
---|
341 | if(!ecmt)
|
---|
342 | { cs_readunlock(&ecmcache_lock); }
|
---|
343 | ecmcwcache_size = count;
|
---|
344 |
|
---|
345 | while(ecmt)
|
---|
346 | {
|
---|
347 | ecm = ecmt->next;
|
---|
348 | free_ecm(ecmt);
|
---|
349 | ecmt = ecm;
|
---|
350 | }
|
---|
351 |
|
---|
352 | #ifdef CS_CACHEEX
|
---|
353 | ecmt=NULL;
|
---|
354 | cs_readlock(&ecm_pushed_deleted_lock);
|
---|
355 | for(ecm = ecm_pushed_deleted, prv = NULL; ecm; prv = ecm, ecm = ecm->next)
|
---|
356 | {
|
---|
357 | ecm_maxcachetime = t_now.time - ((cfg.ctimeout+500)/1000+3);
|
---|
358 | if(ecm->tps.time < ecm_maxcachetime)
|
---|
359 | {
|
---|
360 | cs_readunlock(&ecm_pushed_deleted_lock);
|
---|
361 | cs_writelock(&ecm_pushed_deleted_lock);
|
---|
362 | ecmt = ecm;
|
---|
363 | if(prv)
|
---|
364 | { prv->next = NULL; }
|
---|
365 | else
|
---|
366 | { ecm_pushed_deleted = NULL; }
|
---|
367 | cs_writeunlock(&ecm_pushed_deleted_lock);
|
---|
368 | break;
|
---|
369 | }
|
---|
370 | }
|
---|
371 | if(!ecmt)
|
---|
372 | { cs_readunlock(&ecm_pushed_deleted_lock); }
|
---|
373 |
|
---|
374 | while(ecmt)
|
---|
375 | {
|
---|
376 | ecm = ecmt->next;
|
---|
377 | free_push_in_ecm(ecmt);
|
---|
378 | ecmt = ecm;
|
---|
379 | }
|
---|
380 | #endif
|
---|
381 |
|
---|
382 | cs_ftime(&ecmc_time);
|
---|
383 | ecmc_next = add_ms_to_timeb_diff(&ecmc_time, 1000);
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | if((cache_next = comp_timeb(&cache_time, &t_now)) <= 10)
|
---|
388 | {
|
---|
389 |
|
---|
390 |
|
---|
391 | cleanup_cache();
|
---|
392 | #ifdef CS_CACHEEX
|
---|
393 | cleanup_hitcache();
|
---|
394 | #endif
|
---|
395 |
|
---|
396 | cs_ftime(&cache_time);
|
---|
397 | cache_next = add_ms_to_timeb_diff(&cache_time, 3000);
|
---|
398 | }
|
---|
399 |
|
---|
400 | if((n_request_next = comp_timeb(&n_request_time, &t_now)) <= 10)
|
---|
401 | {
|
---|
402 | update_n_request();
|
---|
403 | cs_ftime(&n_request_time);
|
---|
404 | n_request_next = add_ms_to_timeb_diff(&n_request_time, 60 * 1000);
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | msec_wait = next_check;
|
---|
409 | #ifdef CS_ANTICASC
|
---|
410 | if(!msec_wait || (ac_next > 0 && ac_next < msec_wait))
|
---|
411 | { msec_wait = ac_next; }
|
---|
412 | #endif
|
---|
413 | if(!msec_wait || (ecmc_next > 0 && ecmc_next < msec_wait))
|
---|
414 | { msec_wait = ecmc_next; }
|
---|
415 |
|
---|
416 | if(!msec_wait || (cache_next > 0 && cache_next < msec_wait))
|
---|
417 | { msec_wait = cache_next; }
|
---|
418 |
|
---|
419 | if(!msec_wait || (n_request_next > 0 && n_request_next < msec_wait))
|
---|
420 | { msec_wait = n_request_next; }
|
---|
421 |
|
---|
422 | if(!msec_wait)
|
---|
423 | { msec_wait = 3000; }
|
---|
424 |
|
---|
425 | cleanupcwcycle();
|
---|
426 | }
|
---|
427 |
|
---|
428 | return NULL;
|
---|
429 | }
|
---|
430 |
|
---|
431 | void cw_process_thread_start(void)
|
---|
432 | {
|
---|
433 | start_thread((void *) &cw_process, "cw_process");
|
---|
434 | }
|
---|
435 |
|
---|
436 | void cw_process_thread_wakeup(void)
|
---|
437 | {
|
---|
438 | cw_process_wakeups++; // Do not sleep...
|
---|
439 | pthread_cond_signal(&cw_process_sleep_cond);
|
---|
440 | }
|
---|
441 |
|
---|
442 | #ifdef WITH_LB
|
---|
443 | /**
|
---|
444 | * search for same ecm hash with same readers
|
---|
445 | **/
|
---|
446 | struct ecm_request_t *check_same_ecm(ECM_REQUEST *er)
|
---|
447 | {
|
---|
448 | struct ecm_request_t *ecm;
|
---|
449 | time_t timeout;
|
---|
450 | struct s_ecm_answer *ea_ecm = NULL, *ea_er = NULL;
|
---|
451 | uint8_t rdrs = 0;
|
---|
452 |
|
---|
453 |
|
---|
454 | cs_readlock(&ecmcache_lock);
|
---|
455 | for(ecm = ecmcwcache; ecm; ecm = ecm->next)
|
---|
456 | {
|
---|
457 | timeout = time(NULL) - ((cfg.ctimeout + 500) / 1000);
|
---|
458 |
|
---|
459 | if(ecm->tps.time <= timeout)
|
---|
460 | { break; }
|
---|
461 |
|
---|
462 | if(ecm == er) { continue; }
|
---|
463 |
|
---|
464 | if(memcmp(ecm->ecmd5, er->ecmd5, CS_ECMSTORESIZE))
|
---|
465 | { continue; }
|
---|
466 |
|
---|
467 | if(!er->readers || !ecm->readers || er->readers != ecm->readers)
|
---|
468 | { continue; }
|
---|
469 |
|
---|
470 | ea_ecm = ecm->matching_rdr;
|
---|
471 | ea_er = er->matching_rdr;
|
---|
472 | rdrs = er->readers;
|
---|
473 |
|
---|
474 | while(rdrs && ea_ecm && ea_er)
|
---|
475 | {
|
---|
476 | if(ea_ecm->reader != ea_er->reader)
|
---|
477 | { break; }
|
---|
478 | ea_ecm = ea_ecm->next;
|
---|
479 | ea_er = ea_er->next;
|
---|
480 | rdrs--;
|
---|
481 | }
|
---|
482 |
|
---|
483 | if(!rdrs)
|
---|
484 | {
|
---|
485 | cs_readunlock(&ecmcache_lock);
|
---|
486 | return ecm;
|
---|
487 | }
|
---|
488 | }
|
---|
489 | cs_readunlock(&ecmcache_lock);
|
---|
490 | return NULL; // nothing found so return null
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | void use_same_readers(ECM_REQUEST *er_new, ECM_REQUEST *er_cache)
|
---|
495 | {
|
---|
496 | struct s_ecm_answer *ea_new = er_new->matching_rdr;
|
---|
497 | struct s_ecm_answer *ea_cache = er_cache->matching_rdr;
|
---|
498 | uint8_t rdrs = er_new->readers;
|
---|
499 | while(rdrs)
|
---|
500 | {
|
---|
501 | ea_new->status &= ~(READER_ACTIVE | READER_FALLBACK);
|
---|
502 | if((ea_cache->status & READER_ACTIVE))
|
---|
503 | {
|
---|
504 | if(!(ea_cache->status & READER_FALLBACK))
|
---|
505 | {
|
---|
506 | ea_new->status |= READER_ACTIVE;
|
---|
507 | }
|
---|
508 | else
|
---|
509 | {
|
---|
510 | ea_new->status |= (READER_ACTIVE | READER_FALLBACK);
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 | ea_new = ea_new->next;
|
---|
515 | ea_cache = ea_cache->next;
|
---|
516 | rdrs--;
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | #endif
|
---|
521 |
|
---|
522 |
|
---|
523 |
|
---|
524 | void convert_to_beta(struct s_client *cl, ECM_REQUEST *er, uint16_t caidto)
|
---|
525 | {
|
---|
526 | static uint8_t headerN3[10] = {0xc7, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x87, 0x12};
|
---|
527 | static uint8_t headerN2[10] = {0xc9, 0x00, 0x00, 0x00, 0x01, 0x10, 0x10, 0x00, 0x48, 0x12};
|
---|
528 |
|
---|
529 | er->ocaid = er->caid;
|
---|
530 | er->caid = caidto;
|
---|
531 | er->prid = 0;
|
---|
532 | er->ecmlen = er->ecm[2] + 3;
|
---|
533 |
|
---|
534 | memmove(er->ecm + 13, er->ecm + 3, er->ecmlen - 3);
|
---|
535 |
|
---|
536 | if(er->ecmlen > 0x88)
|
---|
537 | {
|
---|
538 | memcpy(er->ecm + 3, headerN3, 10);
|
---|
539 | if(er->ecm[0] == 0x81)
|
---|
540 | { er->ecm[12] += 1; }
|
---|
541 | er->ecm[1] = 0x70;
|
---|
542 | }
|
---|
543 | else
|
---|
544 | {
|
---|
545 | memcpy(er->ecm + 3, headerN2, 10);
|
---|
546 | }
|
---|
547 |
|
---|
548 | er->ecmlen += 10;
|
---|
549 | er->ecm[2] = er->ecmlen - 3;
|
---|
550 | er->btun = 1;
|
---|
551 |
|
---|
552 | cl->cwtun++;
|
---|
553 | cl->account->cwtun++;
|
---|
554 | first_client->cwtun++;
|
---|
555 |
|
---|
556 | cs_debug_mask(D_TRACE, "ECM converted ocaid from 0x%04X to BetaCrypt caid 0x%04X for service id 0x%04X",
|
---|
557 | er->ocaid, caidto, er->srvid);
|
---|
558 | }
|
---|
559 |
|
---|
560 | void convert_to_nagra(struct s_client *cl, ECM_REQUEST *er, uint16_t caidto)
|
---|
561 | {
|
---|
562 | cs_debug_mask(D_TRACE, "convert_to_nagra");
|
---|
563 | er->ocaid = er->caid;
|
---|
564 | er->caid = caidto;
|
---|
565 | er->prid = 0;
|
---|
566 | er->ecmlen = er->ecm[2] + 3;
|
---|
567 |
|
---|
568 | //not sure
|
---|
569 | if(er->ecmlen < 0x52)
|
---|
570 | { er->ecm[1] = 0x30; }
|
---|
571 |
|
---|
572 | memmove(er->ecm + 3, er->ecm + 13, er->ecmlen - 3);
|
---|
573 |
|
---|
574 | er->ecmlen -= 10;
|
---|
575 | er->ecm[2] = er->ecmlen - 3;
|
---|
576 | er->btun = 1;
|
---|
577 |
|
---|
578 | cl->cwtun++;
|
---|
579 | cl->account->cwtun++;
|
---|
580 | first_client->cwtun++;
|
---|
581 |
|
---|
582 | cs_debug_mask(D_TRACE, "ECM converted ocaid from: 0x%04X to Nagra: 0x04%X for service id:0x04%X",
|
---|
583 | er->ocaid, caidto, er->srvid);
|
---|
584 | }
|
---|
585 |
|
---|
586 | void cs_betatunnel(ECM_REQUEST *er)
|
---|
587 | {
|
---|
588 | int32_t n;
|
---|
589 | struct s_client *cl = cur_client();
|
---|
590 | uint32_t mask_all = 0xFFFF;
|
---|
591 |
|
---|
592 | TUNTAB *ttab;
|
---|
593 | ttab = &cl->ttab;
|
---|
594 |
|
---|
595 | for(n = 0; n < ttab->n; n++)
|
---|
596 | {
|
---|
597 | if((er->caid == ttab->bt_caidfrom[n]) && ((er->srvid == ttab->bt_srvid[n]) || (ttab->bt_srvid[n]) == mask_all))
|
---|
598 | {
|
---|
599 | if(chk_is_betatunnel_caid(er->caid) == 1 && er->ocaid == 0x0000)
|
---|
600 | {
|
---|
601 | convert_to_nagra(cl, er, ttab->bt_caidto[n]);
|
---|
602 | }
|
---|
603 | else if(er->ocaid == 0x0000)
|
---|
604 | {
|
---|
605 | convert_to_beta(cl, er, ttab->bt_caidto[n]);
|
---|
606 | }
|
---|
607 | return;
|
---|
608 | }
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | static void remove_ecm_from_reader(ECM_REQUEST *ecm)
|
---|
613 | {
|
---|
614 | int32_t i;
|
---|
615 | struct s_ecm_answer *ea = ecm->matching_rdr;
|
---|
616 | while(ea)
|
---|
617 | {
|
---|
618 | if((ea->status & REQUEST_SENT) && !(ea->status & REQUEST_ANSWERED))
|
---|
619 | {
|
---|
620 | //we found a outstanding reader, clean it:
|
---|
621 | struct s_reader *rdr = ea->reader;
|
---|
622 | if(rdr)
|
---|
623 | {
|
---|
624 | struct s_client *cl = rdr->client;
|
---|
625 | if(check_client(cl))
|
---|
626 | {
|
---|
627 | ECM_REQUEST *ecmtask = cl->ecmtask;
|
---|
628 | if(ecmtask)
|
---|
629 | {
|
---|
630 | for(i = 0; i < cfg.max_pending; ++i)
|
---|
631 | {
|
---|
632 | if(ecmtask[i].parent == ecm)
|
---|
633 | {
|
---|
634 | ecmtask[i].client = NULL;
|
---|
635 | cacheex_set_csp_lastnode(&ecmtask[i]);
|
---|
636 | }
|
---|
637 | }
|
---|
638 | }
|
---|
639 | }
|
---|
640 | }
|
---|
641 | }
|
---|
642 | ea = ea->next;
|
---|
643 | }
|
---|
644 | }
|
---|
645 |
|
---|
646 | void free_ecm(ECM_REQUEST *ecm)
|
---|
647 | {
|
---|
648 | struct s_ecm_answer *ea, *nxt;
|
---|
649 | cacheex_free_csp_lastnodes(ecm);
|
---|
650 | //remove this ecm from reader queue to avoid segfault on very late answers (when ecm is already disposed)
|
---|
651 | //first check for outstanding answers:
|
---|
652 | remove_ecm_from_reader(ecm);
|
---|
653 | //free matching_rdr list:
|
---|
654 | ea = ecm->matching_rdr;
|
---|
655 | ecm->matching_rdr = NULL;
|
---|
656 | while(ea)
|
---|
657 | {
|
---|
658 | nxt = ea->next;
|
---|
659 | cs_lock_destroy(&ea->ecmanswer_lock);
|
---|
660 | add_garbage(ea);
|
---|
661 | ea = nxt;
|
---|
662 | }
|
---|
663 | if(ecm->src_data)
|
---|
664 | { add_garbage(ecm->src_data); }
|
---|
665 | add_garbage(ecm);
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | void free_push_in_ecm(ECM_REQUEST *ecm)
|
---|
670 | {
|
---|
671 | cacheex_free_csp_lastnodes(ecm);
|
---|
672 | if(ecm->src_data)
|
---|
673 | { NULLFREE(ecm->src_data); }
|
---|
674 | NULLFREE(ecm);
|
---|
675 | }
|
---|
676 |
|
---|
677 |
|
---|
678 | ECM_REQUEST *get_ecmtask(void)
|
---|
679 | {
|
---|
680 | ECM_REQUEST *er = NULL;
|
---|
681 | struct s_client *cl = cur_client();
|
---|
682 | if(!cl)
|
---|
683 | { return NULL; }
|
---|
684 | if(!cs_malloc(&er, sizeof(ECM_REQUEST)))
|
---|
685 | { return NULL; }
|
---|
686 | cs_ftime(&er->tps);
|
---|
687 |
|
---|
688 | #ifdef CS_CACHEEX
|
---|
689 | er->cacheex_wait = er->tps;
|
---|
690 | er->cacheex_wait_time = 0;
|
---|
691 | #endif
|
---|
692 | #ifdef MODULE_GBOX
|
---|
693 | er->gbox_ecm_id = 0;
|
---|
694 | #endif
|
---|
695 | er->rc = E_UNHANDLED;
|
---|
696 | er->client = cl;
|
---|
697 | er->grp = 0; //no readers/cacheex-clients answers yet
|
---|
698 | //cs_log("client %s ECMTASK %d module %s", username(cl), n, get_module(cl)->desc);
|
---|
699 | return er;
|
---|
700 | }
|
---|
701 |
|
---|
702 | void cleanup_ecmtasks(struct s_client *cl)
|
---|
703 | {
|
---|
704 | if(cl && cl->account && !cl->account->usr[0]) { return; } //not for anonymous users!
|
---|
705 |
|
---|
706 | ECM_REQUEST *ecm;
|
---|
707 |
|
---|
708 | //remove this clients ecm from queue. because of cache, just null the client:
|
---|
709 | cs_readlock(&ecmcache_lock);
|
---|
710 | for(ecm = ecmcwcache; ecm; ecm = ecm->next)
|
---|
711 | {
|
---|
712 | if(ecm->client == cl)
|
---|
713 | {
|
---|
714 | ecm->client = NULL;
|
---|
715 | }
|
---|
716 | }
|
---|
717 | cs_readunlock(&ecmcache_lock);
|
---|
718 |
|
---|
719 | //remove client from rdr ecm-queue:
|
---|
720 | cs_readlock(&readerlist_lock);
|
---|
721 | struct s_reader *rdr = first_active_reader;
|
---|
722 | while(rdr)
|
---|
723 | {
|
---|
724 | if(check_client(rdr->client) && rdr->client->ecmtask)
|
---|
725 | {
|
---|
726 | int i;
|
---|
727 | for(i = 0; i < cfg.max_pending; i++)
|
---|
728 | {
|
---|
729 | ecm = &rdr->client->ecmtask[i];
|
---|
730 | if(ecm->client == cl)
|
---|
731 | {
|
---|
732 | ecm->client = NULL;
|
---|
733 | }
|
---|
734 | }
|
---|
735 | }
|
---|
736 | rdr = rdr->next;
|
---|
737 | }
|
---|
738 | cs_readunlock(&readerlist_lock);
|
---|
739 |
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 | static void add_cascade_data(struct s_client *client, ECM_REQUEST *er)
|
---|
744 | {
|
---|
745 | if(!client->cascadeusers)
|
---|
746 | { client->cascadeusers = ll_create("cascade_data"); }
|
---|
747 | LLIST *l = client->cascadeusers;
|
---|
748 | LL_ITER it = ll_iter_create(l);
|
---|
749 | time_t now = time(NULL);
|
---|
750 | struct s_cascadeuser *cu;
|
---|
751 | int8_t found = 0;
|
---|
752 | while((cu = ll_iter_next(&it)))
|
---|
753 | {
|
---|
754 | if(er->caid == cu->caid && er->prid == cu->prid && er->srvid == cu->srvid) //found it
|
---|
755 | {
|
---|
756 | if(cu->time < now)
|
---|
757 | { cu->cwrate = now - cu->time; }
|
---|
758 | cu->time = now;
|
---|
759 | found = 1;
|
---|
760 | }
|
---|
761 | else if(cu->time + 60 < now) // old
|
---|
762 | { ll_iter_remove_data(&it); }
|
---|
763 | }
|
---|
764 | if(!found) //add it if not found
|
---|
765 | {
|
---|
766 | if(!cs_malloc(&cu, sizeof(struct s_cascadeuser)))
|
---|
767 | { return; }
|
---|
768 | cu->caid = er->caid;
|
---|
769 | cu->prid = er->prid;
|
---|
770 | cu->srvid = er->srvid;
|
---|
771 | cu->time = now;
|
---|
772 | ll_append(l, cu);
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | static int32_t is_double_check_caid(ECM_REQUEST *er)
|
---|
777 | {
|
---|
778 | if(!cfg.double_check_caid.caid[0]) //no caids defined: Check all
|
---|
779 | { return 1; }
|
---|
780 | int32_t i;
|
---|
781 | for(i = 0; i < CS_MAXCAIDTAB; i++)
|
---|
782 | {
|
---|
783 | uint16_t tcaid = cfg.double_check_caid.caid[i];
|
---|
784 | if(!tcaid)
|
---|
785 | { break; }
|
---|
786 | if(tcaid == er->caid || (tcaid < 0x0100 && (er->caid >> 8) == tcaid))
|
---|
787 | {
|
---|
788 | return 1;
|
---|
789 | }
|
---|
790 | }
|
---|
791 | return 0;
|
---|
792 | }
|
---|
793 |
|
---|
794 | struct s_ecm_answer *get_ecm_answer(struct s_reader *reader, ECM_REQUEST *er)
|
---|
795 | {
|
---|
796 | if(!er || !reader) { return NULL; }
|
---|
797 |
|
---|
798 | struct s_ecm_answer *ea;
|
---|
799 |
|
---|
800 | for(ea = er->matching_rdr; ea; ea = ea->next)
|
---|
801 | {
|
---|
802 | if(ea->reader == reader)
|
---|
803 | {
|
---|
804 | return ea;
|
---|
805 | }
|
---|
806 | }
|
---|
807 | return NULL;
|
---|
808 | }
|
---|
809 |
|
---|
810 |
|
---|
811 | void distribute_ea(struct s_ecm_answer *ea)
|
---|
812 | {
|
---|
813 |
|
---|
814 | struct s_ecm_answer *ea_temp;
|
---|
815 |
|
---|
816 | for(ea_temp = ea->pending; ea_temp; ea_temp = ea_temp->pending_next)
|
---|
817 | {
|
---|
818 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [distribute_ea] send ea (%s) by reader %s answering for client %s", (check_client(ea_temp->er->client) ? ea_temp->er->client->account->usr : "-"), ea_temp->er->caid, ea_temp->er->prid, ea_temp->er->srvid, ea->rc==E_FOUND?"OK":"NOK", ea_temp->reader->label, (check_client(ea->er->client) ? ea->er->client->account->usr : "-"));
|
---|
819 |
|
---|
820 | //e.g. we cannot send timeout, because "ea_temp->er->client" could wait/ask other readers! Simply set not_found if different from E_FOUND!
|
---|
821 | write_ecm_answer(ea_temp->reader, ea_temp->er, (ea->rc==E_FOUND? E_FOUND : E_NOTFOUND), ea->rcEx, ea->cw, NULL);
|
---|
822 | }
|
---|
823 | }
|
---|
824 |
|
---|
825 |
|
---|
826 | int32_t send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
827 | {
|
---|
828 | if(!check_client(client) || client->typ != 'c')
|
---|
829 | { return 0; }
|
---|
830 |
|
---|
831 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [send_dcw] rc %d from reader %s", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid, er->rc, er->selected_reader ? er->selected_reader->label : "-");
|
---|
832 |
|
---|
833 | static const char stageTxt[] = {'0', 'C', 'L', 'P', 'F', 'X'};
|
---|
834 | static const char *stxt[] = {"found", "cache1", "cache2", "cache3",
|
---|
835 | "not found", "timeout", "sleeping",
|
---|
836 | "fake", "invalid", "corrupt", "no card", "expdate", "disabled", "stopped"
|
---|
837 | };
|
---|
838 | static const char *stxtEx[16] = {"", "group", "caid", "ident", "class", "chid", "queue", "peer", "sid", "", "", "", "", "", "", ""};
|
---|
839 | static const char *stxtWh[16] = {"", "user ", "reader ", "server ", "lserver ", "", "", "", "", "", "", "", "" , "" , "", ""};
|
---|
840 | char sby[100] = "", sreason[32] = "", scwcinfo[32] = "", schaninfo[32] = "";
|
---|
841 | char erEx[32] = "";
|
---|
842 | char uname[38] = "";
|
---|
843 | char channame[32];
|
---|
844 | struct timeb tpe;
|
---|
845 |
|
---|
846 | snprintf(uname, sizeof(uname) - 1, "%s", username(client));
|
---|
847 |
|
---|
848 | #ifdef WITH_DEBUG
|
---|
849 | if(cs_dblevel & D_CLIENTECM)
|
---|
850 | {
|
---|
851 | char buf[ECM_FMT_LEN];
|
---|
852 | char ecmd5[17 * 3];
|
---|
853 | char cwstr[17 * 3];
|
---|
854 | format_ecm(er, buf, ECM_FMT_LEN);
|
---|
855 | cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
|
---|
856 | cs_hexdump(0, er->cw, 16, cwstr, sizeof(cwstr));
|
---|
857 | #ifdef CS_CACHEEX
|
---|
858 | char csphash[5 * 3];
|
---|
859 | cs_hexdump(0, (void *)&er->csp_hash, 4, csphash, sizeof(csphash));
|
---|
860 | cs_debug_mask(D_CLIENTECM, "Client %s csphash %s cw %s rc %d %s", username(client), csphash, cwstr, er->rc, buf);
|
---|
861 | #else
|
---|
862 | cs_debug_mask(D_CLIENTECM, "Client %s cw %s rc %d %s", username(client), cwstr, er->rc, buf);
|
---|
863 | #endif
|
---|
864 | }
|
---|
865 | #endif
|
---|
866 |
|
---|
867 | struct s_reader *er_reader = er->selected_reader; //responding reader
|
---|
868 | struct s_ecm_answer *ea_orig = get_ecm_answer(er_reader, er);
|
---|
869 |
|
---|
870 | //check if ecm_answer from pending's
|
---|
871 | if(ea_orig && ea_orig->is_pending && er->rc == E_FOUND)
|
---|
872 | { er->rc = E_CACHE2; }
|
---|
873 |
|
---|
874 | //check if answer from cacheex-1 reader
|
---|
875 | if(er->rc == E_FOUND && er_reader && cacheex_reader(er_reader)) //so add hit to cacheex mode 1 readers
|
---|
876 | {
|
---|
877 | er->rc = E_CACHEEX;
|
---|
878 | }
|
---|
879 |
|
---|
880 | if(er->rc == E_TIMEOUT)
|
---|
881 | {
|
---|
882 | struct s_ecm_answer *ea_list;
|
---|
883 | int32_t ofs = 0;
|
---|
884 | for(ea_list = er->matching_rdr; ea_list; ea_list = ea_list->next)
|
---|
885 | {
|
---|
886 | if(ea_list->reader && ofs < (int32_t)sizeof(sby) && ((ea_list->status & REQUEST_SENT) && (ea_list->rc == E_TIMEOUT || ea_list->rc >= E_99))) //Request send, but no cw answered!
|
---|
887 | {
|
---|
888 | ofs += snprintf(sby + ofs, sizeof(sby) - ofs - 1, "%s%s", ofs ? "," : " by ", ea_list->reader->label);
|
---|
889 | }
|
---|
890 | }
|
---|
891 | if(er->ocaid && ofs < (int32_t)sizeof(sby))
|
---|
892 | { ofs += snprintf(sby + ofs, sizeof(sby) - ofs - 1, "(btun %04X)", er->ocaid); }
|
---|
893 | }
|
---|
894 | else if(er_reader)
|
---|
895 | {
|
---|
896 | // add marker to reader if ECM_REQUEST was betatunneled
|
---|
897 | if(er->ocaid)
|
---|
898 | { snprintf(sby, sizeof(sby) - 1, " by %s(btun %04X)", er_reader->label, er->ocaid); }
|
---|
899 | else
|
---|
900 | { snprintf(sby, sizeof(sby) - 1, " by %s", er_reader->label); }
|
---|
901 | }
|
---|
902 | #ifdef CS_CACHEEX
|
---|
903 | else if(er->cacheex_src) //only for cacheex mode-3 clients (no mode-1 or mode-2 because reader is set!) and csp
|
---|
904 | {
|
---|
905 | char *cex_name = "-";
|
---|
906 | if(check_client(er->cacheex_src) && er->cacheex_src->account){
|
---|
907 | if(er->cacheex_src->account->usr[0] != '\0')
|
---|
908 | cex_name = er->cacheex_src->account->usr;
|
---|
909 | else
|
---|
910 | cex_name = "csp";
|
---|
911 | }
|
---|
912 |
|
---|
913 | if(er->ocaid){
|
---|
914 | snprintf(sby, sizeof(sby) - 1, " by %s(btun %04X)", cex_name, er->ocaid);
|
---|
915 | }else{
|
---|
916 | snprintf(sby, sizeof(sby) - 1, " by %s", cex_name);
|
---|
917 | }
|
---|
918 | }
|
---|
919 | #endif
|
---|
920 |
|
---|
921 | if(er->rc < E_NOTFOUND)
|
---|
922 | { er->rcEx = 0; }
|
---|
923 |
|
---|
924 | if(er->rcEx)
|
---|
925 | { snprintf(erEx, sizeof(erEx) - 1, "rejected %s%s", stxtWh[er->rcEx >> 4], stxtEx[er->rcEx & 0xf]); }
|
---|
926 |
|
---|
927 | get_servicename_or_null(client, er->srvid, er->caid, channame);
|
---|
928 | if(!channame[0])
|
---|
929 | { schaninfo[0] = '\0'; }
|
---|
930 | else
|
---|
931 | { snprintf(schaninfo, sizeof(schaninfo) - 1, " - %s", channame); }
|
---|
932 |
|
---|
933 | if(er->msglog[0])
|
---|
934 | { snprintf(sreason, sizeof(sreason) - 1, " (%s)", er->msglog); }
|
---|
935 | #ifdef CW_CYCLE_CHECK
|
---|
936 | if(er->cwc_msg_log[0])
|
---|
937 | { snprintf(scwcinfo, sizeof(scwcinfo) - 1, " (%s)", er->cwc_msg_log); }
|
---|
938 | #endif
|
---|
939 |
|
---|
940 | cs_ftime(&tpe);
|
---|
941 |
|
---|
942 | #ifdef CS_CACHEEX
|
---|
943 | if(er->cacheex_wait_time && er->rc != E_TIMEOUT)
|
---|
944 | {
|
---|
945 | if(er->rc >= E_CACHEEX)
|
---|
946 | {
|
---|
947 | uint32_t ntime = comp_timeb(&tpe, &er->tps);
|
---|
948 | if(ntime >= er->cacheex_wait_time)
|
---|
949 | {
|
---|
950 | snprintf(sreason, sizeof(sreason) - 1, " (wait_time over)");
|
---|
951 | }
|
---|
952 | }
|
---|
953 | else if(er->rc == E_FOUND)
|
---|
954 | {
|
---|
955 | if(ea_orig) { snprintf(sreason, sizeof(sreason) - 1, " (real %d ms)", ea_orig->ecm_time); }
|
---|
956 | }
|
---|
957 | }
|
---|
958 |
|
---|
959 | //print counter
|
---|
960 | if(er->cw_count>1){
|
---|
961 | char *cur = sreason;
|
---|
962 | cur += snprintf(cur, sizeof(sreason) - 1, " (cw count %d)", er->cw_count);
|
---|
963 | }
|
---|
964 | #endif
|
---|
965 |
|
---|
966 | client->cwlastresptime = comp_timeb(&tpe, &er->tps);
|
---|
967 |
|
---|
968 | time_t now = time(NULL);
|
---|
969 | webif_client_add_lastresponsetime(client, client->cwlastresptime, now, er->rc); // add to ringbuffer
|
---|
970 |
|
---|
971 | if(er_reader)
|
---|
972 | {
|
---|
973 | struct s_client *er_cl = er_reader->client;
|
---|
974 | if(check_client(er_cl))
|
---|
975 | {
|
---|
976 | er_cl->cwlastresptime = client->cwlastresptime;
|
---|
977 | webif_client_add_lastresponsetime(er_cl, client->cwlastresptime, now, er->rc);
|
---|
978 | er_cl->last_srvidptr = client->last_srvidptr;
|
---|
979 | }
|
---|
980 | }
|
---|
981 |
|
---|
982 | webif_client_init_lastreader(client, er, er_reader, stxt);
|
---|
983 |
|
---|
984 | client->last = now;
|
---|
985 |
|
---|
986 | //cs_debug_mask(D_TRACE, "CHECK rc=%d er->cacheex_src=%s", er->rc, username(er->cacheex_src));
|
---|
987 | switch(er->rc)
|
---|
988 | {
|
---|
989 | case E_FOUND:
|
---|
990 | {
|
---|
991 | client->cwfound++;
|
---|
992 | client->account->cwfound++;
|
---|
993 | first_client->cwfound++;
|
---|
994 | break;
|
---|
995 | }
|
---|
996 | case E_CACHE1:
|
---|
997 | case E_CACHE2:
|
---|
998 | case E_CACHEEX:
|
---|
999 | {
|
---|
1000 | client->cwcache++;
|
---|
1001 | client->account->cwcache++;
|
---|
1002 | first_client->cwcache++;
|
---|
1003 | #ifdef CS_CACHEEX
|
---|
1004 | if(check_client(er->cacheex_src))
|
---|
1005 | {
|
---|
1006 | first_client->cwcacheexhit++;
|
---|
1007 | er->cacheex_src->cwcacheexhit++;
|
---|
1008 | if(er->cacheex_src->account)
|
---|
1009 | { er->cacheex_src->account->cwcacheexhit++; }
|
---|
1010 | }
|
---|
1011 | #endif
|
---|
1012 | break;
|
---|
1013 | }
|
---|
1014 | case E_NOTFOUND:
|
---|
1015 | case E_CORRUPT:
|
---|
1016 | case E_NOCARD:
|
---|
1017 | {
|
---|
1018 | if(er->rcEx)
|
---|
1019 | {
|
---|
1020 | client->cwignored++;
|
---|
1021 | client->account->cwignored++;
|
---|
1022 | first_client->cwignored++;
|
---|
1023 | }
|
---|
1024 | else
|
---|
1025 | {
|
---|
1026 | client->cwnot++;
|
---|
1027 | client->account->cwnot++;
|
---|
1028 | first_client->cwnot++;
|
---|
1029 | }
|
---|
1030 | break;
|
---|
1031 | }
|
---|
1032 | case E_TIMEOUT:
|
---|
1033 | {
|
---|
1034 | client->cwtout++;
|
---|
1035 | client->account->cwtout++;
|
---|
1036 | first_client->cwtout++;
|
---|
1037 | break;
|
---|
1038 | }
|
---|
1039 | default:
|
---|
1040 | {
|
---|
1041 | client->cwignored++;
|
---|
1042 | client->account->cwignored++;
|
---|
1043 | first_client->cwignored++;
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 |
|
---|
1048 | ac_chk(client, er, 1);
|
---|
1049 | int32_t is_fake = 0;
|
---|
1050 | if(er->rc == E_FAKE)
|
---|
1051 | {
|
---|
1052 | is_fake = 1;
|
---|
1053 | er->rc = E_FOUND;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | if (er->caid >> 8 == 0x09 && er->cw && er->rc < E_NOTFOUND){
|
---|
1057 | if (er->ecm[0] == 0x80 && checkCWpart(er->cw, 1) && !checkCWpart(er->cw, 0)){ // wrong: even ecm should only have even part of cw used
|
---|
1058 | cs_debug_mask(D_TRACE,"NDS videoguard controlword swapped");
|
---|
1059 | memcpy(er->cw, er->cw + 8, 8); // move card cw answer to right part!
|
---|
1060 | memset(er->cw+8,0,8); // blanc old position
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | if (er->ecm[0] == 0x81 && checkCWpart(er->cw, 0) && !checkCWpart(er->cw, 1)){ // wrong: odd ecm should only have odd part of cw used
|
---|
1064 | cs_debug_mask(D_TRACE,"NDS videoguard controlword swapped");
|
---|
1065 | memcpy(er->cw+8, er->cw, 8); // move card cw answer to right part!
|
---|
1066 | memset(er->cw,0,8); // blanc old position
|
---|
1067 | }
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | if(cfg.double_check && er->rc == E_FOUND && er->selected_reader && is_double_check_caid(er))
|
---|
1071 | {
|
---|
1072 | if(er->checked == 0) //First CW, save it and wait for next one
|
---|
1073 | {
|
---|
1074 | er->checked = 1;
|
---|
1075 | er->origin_reader = er->selected_reader;
|
---|
1076 | memcpy(er->cw_checked, er->cw, sizeof(er->cw));
|
---|
1077 | cs_log("DOUBLE CHECK FIRST CW by %s idx %d cpti %d", er->origin_reader->label, er->idx, er->msgid);
|
---|
1078 | }
|
---|
1079 | else if(er->origin_reader != er->selected_reader) //Second (or third and so on) cw. We have to compare
|
---|
1080 | {
|
---|
1081 | if(memcmp(er->cw_checked, er->cw, sizeof(er->cw)) == 0)
|
---|
1082 | {
|
---|
1083 | er->checked++;
|
---|
1084 | cs_log("DOUBLE CHECKED! %d. CW by %s idx %d cpti %d", er->checked, er->selected_reader->label, er->idx, er->msgid);
|
---|
1085 | }
|
---|
1086 | else
|
---|
1087 | {
|
---|
1088 | cs_log("DOUBLE CHECKED NONMATCHING! %d. CW by %s idx %d cpti %d", er->checked, er->selected_reader->label, er->idx, er->msgid);
|
---|
1089 | }
|
---|
1090 | }
|
---|
1091 | if(er->checked < 2) //less as two same cw? mark as pending!
|
---|
1092 | {
|
---|
1093 | er->rc = E_UNHANDLED;
|
---|
1094 | goto ESC;
|
---|
1095 | }
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | get_module(client)->send_dcw(client, er);
|
---|
1099 |
|
---|
1100 | add_cascade_data(client, er);
|
---|
1101 |
|
---|
1102 | if(is_fake)
|
---|
1103 | { er->rc = E_FAKE; }
|
---|
1104 |
|
---|
1105 | if(!(er->rc == E_SLEEPING && client->cwlastresptime == 0))
|
---|
1106 | {
|
---|
1107 | char buf[ECM_FMT_LEN];
|
---|
1108 | format_ecm(er, buf, ECM_FMT_LEN);
|
---|
1109 | if(er->reader_avail == 1 || er->stage == 0)
|
---|
1110 | {
|
---|
1111 | cs_log("%s (%s): %s (%d ms)%s%s%s%s",
|
---|
1112 | uname, buf,
|
---|
1113 | er->rcEx ? erEx : stxt[er->rc], client->cwlastresptime, sby, schaninfo, sreason, scwcinfo);
|
---|
1114 | }
|
---|
1115 | else
|
---|
1116 | {
|
---|
1117 | cs_log("%s (%s): %s (%d ms)%s (%c/%d/%d/%d)%s%s%s",
|
---|
1118 | uname, buf,
|
---|
1119 | er->rcEx ? erEx : stxt[er->rc],
|
---|
1120 | client->cwlastresptime, sby,
|
---|
1121 | stageTxt[er->stage], er->reader_requested, (er->reader_count + er->fallback_reader_count), er->reader_avail,
|
---|
1122 | schaninfo, sreason, scwcinfo);
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | cs_ddump_mask(D_ATR, er->cw, 16, "cw:");
|
---|
1127 | led_status_cw_not_found(er);
|
---|
1128 |
|
---|
1129 | ESC:
|
---|
1130 |
|
---|
1131 | return 0;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | /*
|
---|
1135 | * write_ecm_request():
|
---|
1136 | */
|
---|
1137 | static int32_t write_ecm_request(struct s_reader *rdr, ECM_REQUEST *er)
|
---|
1138 | {
|
---|
1139 | add_job(rdr->client, ACTION_READER_ECM_REQUEST, (void *)er, 0);
|
---|
1140 | return 1;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 |
|
---|
1144 | /**
|
---|
1145 | * sends the ecm request to the readers
|
---|
1146 | * ECM_REQUEST er : the ecm
|
---|
1147 | * er->stage: 0 = no reader asked yet
|
---|
1148 | * 2 = ask only local reader (skipped without preferlocalcards)
|
---|
1149 | * 3 = ask any non fallback reader
|
---|
1150 | * 4 = ask fallback reader
|
---|
1151 | **/
|
---|
1152 | void request_cw_from_readers(ECM_REQUEST *er, uint8_t stop_stage)
|
---|
1153 | {
|
---|
1154 | struct s_ecm_answer *ea;
|
---|
1155 | int8_t sent = 0;
|
---|
1156 |
|
---|
1157 | if(er->stage >= 4) { return; }
|
---|
1158 |
|
---|
1159 | while(1)
|
---|
1160 | {
|
---|
1161 | if(stop_stage && er->stage >= stop_stage) { return; }
|
---|
1162 |
|
---|
1163 | er->stage++;
|
---|
1164 |
|
---|
1165 | #ifndef CS_CACHEEX
|
---|
1166 | if(er->stage == 1)
|
---|
1167 | { er->stage++; }
|
---|
1168 | #endif
|
---|
1169 | if(er->stage == 2 && !cfg.preferlocalcards)
|
---|
1170 | { er->stage++; }
|
---|
1171 |
|
---|
1172 | for(ea = er->matching_rdr; ea; ea = ea->next)
|
---|
1173 | {
|
---|
1174 | switch(er->stage)
|
---|
1175 | {
|
---|
1176 | #ifdef CS_CACHEEX
|
---|
1177 | case 1:
|
---|
1178 | {
|
---|
1179 | // Cache-Exchange
|
---|
1180 | if((ea->status & REQUEST_SENT) ||
|
---|
1181 | (ea->status & (READER_CACHEEX | READER_ACTIVE)) != (READER_CACHEEX | READER_ACTIVE) || cfg.preferlocalcards==2)
|
---|
1182 | { continue; }
|
---|
1183 | break;
|
---|
1184 | }
|
---|
1185 | #endif
|
---|
1186 | case 2:
|
---|
1187 | {
|
---|
1188 | // only local reader
|
---|
1189 | if((ea->status & REQUEST_SENT) ||
|
---|
1190 | (ea->status & (READER_ACTIVE | READER_FALLBACK | READER_LOCAL)) != (READER_ACTIVE | READER_LOCAL))
|
---|
1191 | { continue; }
|
---|
1192 | break;
|
---|
1193 | }
|
---|
1194 | case 3:
|
---|
1195 | {
|
---|
1196 | // any non fallback reader not asked yet
|
---|
1197 | if((ea->status & REQUEST_SENT) ||
|
---|
1198 | (ea->status & (READER_ACTIVE | READER_FALLBACK)) != READER_ACTIVE)
|
---|
1199 | { continue; }
|
---|
1200 | break;
|
---|
1201 | }
|
---|
1202 | default:
|
---|
1203 | {
|
---|
1204 | // only fallbacks
|
---|
1205 | if((ea->status & REQUEST_SENT) ||
|
---|
1206 | (ea->status & (READER_ACTIVE | READER_FALLBACK)) != (READER_ACTIVE | READER_FALLBACK))
|
---|
1207 | { continue; }
|
---|
1208 | break;
|
---|
1209 | }
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | struct s_reader *rdr = ea->reader;
|
---|
1213 | char ecmd5[17 * 3];
|
---|
1214 | cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
|
---|
1215 | cs_debug_mask(D_TRACE | D_CSP, "request_cw stage=%d to reader %s ecm hash=%s", er->stage, rdr ? rdr->label : "", ecmd5);
|
---|
1216 |
|
---|
1217 | ea->status |= REQUEST_SENT;
|
---|
1218 | cs_ftime(&ea->time_request_sent);
|
---|
1219 |
|
---|
1220 | er->reader_requested++;
|
---|
1221 |
|
---|
1222 | write_ecm_request(ea->reader, er);
|
---|
1223 |
|
---|
1224 | //set sent=1 only if reader is active/connected. If not, switch to next stage!
|
---|
1225 | if(!sent && rdr)
|
---|
1226 | {
|
---|
1227 | struct s_client *rcl = rdr->client;
|
---|
1228 | if(check_client(rcl))
|
---|
1229 | {
|
---|
1230 | if(rcl->typ == 'r' && rdr->card_status == CARD_INSERTED)
|
---|
1231 | { sent = 1; }
|
---|
1232 | else if(rcl->typ == 'p' && (rdr->card_status == CARD_INSERTED || rdr->tcp_connected))
|
---|
1233 | { sent = 1; }
|
---|
1234 | }
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [write_ecm_request] reader %s --> SENT %d", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid, rdr ? ea->reader->label : "-", sent);
|
---|
1238 | }
|
---|
1239 | if(sent || er->stage >= 4)
|
---|
1240 | { break; }
|
---|
1241 | }
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 |
|
---|
1245 | void add_cache_from_reader(ECM_REQUEST *er, struct s_reader *rdr, int32_t csp_hash, uchar *ecmd5, uchar *cw, int16_t caid, int32_t prid, int16_t srvid ){
|
---|
1246 | ECM_REQUEST *ecm;
|
---|
1247 | if (cs_malloc(&ecm, sizeof(ECM_REQUEST))){
|
---|
1248 | cs_ftime(&ecm->tps);
|
---|
1249 |
|
---|
1250 | ecm->cwc_cycletime = er->cwc_cycletime;
|
---|
1251 | ecm->cwc_next_cw_cycle = er->cwc_next_cw_cycle;
|
---|
1252 | memcpy(ecm->ecm, er->ecm, sizeof(ecm->ecm)); // ecm[0] is pushed to cacheexclients so we need a copy from it
|
---|
1253 | ecm->caid = caid;
|
---|
1254 | ecm->prid = prid;
|
---|
1255 | ecm->srvid = srvid;
|
---|
1256 | memcpy(ecm->ecmd5, ecmd5, CS_ECMSTORESIZE);
|
---|
1257 | ecm->csp_hash = csp_hash;
|
---|
1258 | ecm->rc = E_FOUND;
|
---|
1259 | memcpy(ecm->cw, cw, sizeof(ecm->cw));
|
---|
1260 | ecm->grp = rdr->grp;
|
---|
1261 | ecm->selected_reader = rdr;
|
---|
1262 | #ifdef CS_CACHEEX
|
---|
1263 | if(rdr && cacheex_reader(rdr))
|
---|
1264 | { ecm->cacheex_src = rdr->client; } //so adds hits to reader
|
---|
1265 | #endif
|
---|
1266 |
|
---|
1267 | add_cache(ecm); //add cw to cache
|
---|
1268 |
|
---|
1269 | #ifdef CS_CACHEEX
|
---|
1270 | cs_writelock(&ecm_pushed_deleted_lock);
|
---|
1271 | ecm->next = ecm_pushed_deleted;
|
---|
1272 | ecm_pushed_deleted = ecm;
|
---|
1273 | cs_writeunlock(&ecm_pushed_deleted_lock);
|
---|
1274 | #else
|
---|
1275 | NULLFREE(ecm);
|
---|
1276 | #endif
|
---|
1277 | }
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 |
|
---|
1281 | void chk_dcw(struct s_ecm_answer *ea)
|
---|
1282 | {
|
---|
1283 | if(!ea || !ea->er)
|
---|
1284 | { return; }
|
---|
1285 |
|
---|
1286 | ECM_REQUEST *ert = ea->er;
|
---|
1287 | struct s_ecm_answer *ea_list;
|
---|
1288 | struct s_reader *eardr = ea->reader;
|
---|
1289 | if(!ert)
|
---|
1290 | { return; }
|
---|
1291 |
|
---|
1292 | //cache update
|
---|
1293 | if(ea && ea->rc < E_NOTFOUND)
|
---|
1294 | add_cache_from_reader(ert, eardr, ert->csp_hash, ert->ecmd5, ea->cw, ert->caid, ert->prid, ert->srvid );
|
---|
1295 |
|
---|
1296 | //ecm request already answered!
|
---|
1297 | if(ert->rc < E_99)
|
---|
1298 | {
|
---|
1299 | #ifdef CS_CACHEEX
|
---|
1300 | if(ea && ert->rc < E_NOTFOUND && ea->rc < E_NOTFOUND && memcmp(ea->cw, ert->cw, sizeof(ert->cw)) != 0)
|
---|
1301 | {
|
---|
1302 | char cw1[16 * 3 + 2], cw2[16 * 3 + 2];
|
---|
1303 | cs_hexdump(0, ea->cw, 16, cw1, sizeof(cw1));
|
---|
1304 | cs_hexdump(0, ert->cw, 16, cw2, sizeof(cw2));
|
---|
1305 |
|
---|
1306 | char ip1[20] = "", ip2[20] = "";
|
---|
1307 | if(ea->reader && check_client(ea->reader->client)) { cs_strncpy(ip1, cs_inet_ntoa(ea->reader->client->ip), sizeof(ip1)); }
|
---|
1308 | if(ert->cacheex_src) { cs_strncpy(ip2, cs_inet_ntoa(ert->cacheex_src->ip), sizeof(ip2)); }
|
---|
1309 | else if(ert->selected_reader && check_client(ert->selected_reader->client)) { cs_strncpy(ip2, cs_inet_ntoa(ert->selected_reader->client->ip), sizeof(ip2)); }
|
---|
1310 |
|
---|
1311 | ECM_REQUEST *er = ert;
|
---|
1312 | debug_ecm(D_TRACE, "WARNING2: Different CWs %s from %s(%s)<>%s(%s): %s<>%s", buf,
|
---|
1313 | username(ea->reader ? ea->reader->client : ert->client), ip1,
|
---|
1314 | er->cacheex_src ? username(er->cacheex_src) : (ea->reader ? ea->reader->label : "unknown/csp"), ip2,
|
---|
1315 | cw1, cw2);
|
---|
1316 | }
|
---|
1317 | #endif
|
---|
1318 |
|
---|
1319 | return;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 |
|
---|
1323 | #ifdef CS_CACHEEX
|
---|
1324 | //check if check_cw enabled
|
---|
1325 | if(eardr && cacheex_reader(eardr)){ //IF mode-1 reader
|
---|
1326 | CWCHECK check_cw = get_cwcheck(ert);
|
---|
1327 | if(check_cw.counter>1) //if answer from cacheex-1 reader, and we have to check cw counter, not send answer to client! thread check_cache will send answer to client!
|
---|
1328 | return;
|
---|
1329 | }
|
---|
1330 | #endif
|
---|
1331 |
|
---|
1332 |
|
---|
1333 | int32_t reader_left = 0, local_left = 0, reader_not_flb_left = 0, has_not_fallback = 0, has_local = 0;
|
---|
1334 | #ifdef CS_CACHEEX
|
---|
1335 | int8_t cacheex_left = 0;
|
---|
1336 | uint8_t has_cacheex = 0;
|
---|
1337 | #endif
|
---|
1338 |
|
---|
1339 | ert->selected_reader = eardr;
|
---|
1340 |
|
---|
1341 | switch(ea->rc)
|
---|
1342 | {
|
---|
1343 | case E_FOUND:
|
---|
1344 | memcpy(ert->cw, ea->cw, 16);
|
---|
1345 | ert->rcEx = 0;
|
---|
1346 | ert->rc = ea->rc;
|
---|
1347 | ert->grp |= eardr->grp;
|
---|
1348 | #ifdef CS_CACHEEX
|
---|
1349 | if(eardr && cacheex_reader(eardr))
|
---|
1350 | {
|
---|
1351 | ert->cacheex_src = eardr->client; //so adds hits to reader
|
---|
1352 | }
|
---|
1353 | #endif
|
---|
1354 | break;
|
---|
1355 | case E_TIMEOUT: // if timeout, no one reader answered in ctimeout, so send timeout to client!
|
---|
1356 | ert->rc = E_TIMEOUT;
|
---|
1357 | ert->rcEx = 0;
|
---|
1358 | break;
|
---|
1359 | case E_NOTFOUND:
|
---|
1360 | {
|
---|
1361 |
|
---|
1362 | #ifdef CS_CACHEEX
|
---|
1363 | /* if not wait_time expired and wait_time due to hitcache, or ecm is cacheex-1 and there are other normal readers for check INT cache, we have to wait wait_time expires before send rc to client!
|
---|
1364 | * (Before cacheex_wait_time_expired, this answered reader is obviously a cacheex mode 1 reader!)
|
---|
1365 | */
|
---|
1366 | if((!ert->cacheex_wait_time_expired && ert->cacheex_hitcache) || (ert->from_cacheex1_client && ert->reader_nocacheex_avail))
|
---|
1367 | { return; }
|
---|
1368 | #endif
|
---|
1369 |
|
---|
1370 | //check if there are other readers to ask, and if not send NOT_FOUND to client
|
---|
1371 | ert->rcEx = ea->rcEx;
|
---|
1372 | cs_strncpy(ert->msglog, ea->msglog, sizeof(ert->msglog));
|
---|
1373 |
|
---|
1374 | for(ea_list = ert->matching_rdr; ea_list; ea_list = ea_list->next)
|
---|
1375 | {
|
---|
1376 | cs_readlock(&ea_list->ecmanswer_lock);
|
---|
1377 |
|
---|
1378 | #ifdef CS_CACHEEX
|
---|
1379 | if(((ea_list->status & (READER_CACHEEX | READER_FALLBACK | READER_ACTIVE))) == (READER_CACHEEX | READER_ACTIVE))
|
---|
1380 | { has_cacheex = 1; }
|
---|
1381 | if((!(ea_list->status & READER_FALLBACK) && ((ea_list->status & (REQUEST_SENT | REQUEST_ANSWERED | READER_CACHEEX | READER_ACTIVE)) == (REQUEST_SENT | READER_CACHEEX | READER_ACTIVE))) || ea_list->rc < E_NOTFOUND)
|
---|
1382 | { cacheex_left++; }
|
---|
1383 | #endif
|
---|
1384 | if((!(ea_list->status & READER_FALLBACK) && ((ea_list->status & (REQUEST_SENT | REQUEST_ANSWERED | READER_LOCAL | READER_ACTIVE)) == (REQUEST_SENT | READER_LOCAL | READER_ACTIVE))) || ea_list->rc < E_NOTFOUND)
|
---|
1385 | { local_left++; }
|
---|
1386 |
|
---|
1387 | if((!(ea_list->status & READER_FALLBACK) && ((ea_list->status & (REQUEST_SENT | REQUEST_ANSWERED | READER_ACTIVE)) == (REQUEST_SENT | READER_ACTIVE))) || ea_list->rc < E_NOTFOUND)
|
---|
1388 | { reader_not_flb_left++; }
|
---|
1389 |
|
---|
1390 | if(((ea_list->status & (REQUEST_ANSWERED | READER_ACTIVE)) == (READER_ACTIVE)) || ea_list->rc < E_NOTFOUND)
|
---|
1391 | { reader_left++; }
|
---|
1392 |
|
---|
1393 | if(((ea_list->status & (READER_FALLBACK | READER_ACTIVE))) == (READER_ACTIVE))
|
---|
1394 | { has_not_fallback = 1; }
|
---|
1395 | if(((ea_list->status & (READER_LOCAL | READER_FALLBACK | READER_ACTIVE))) == (READER_LOCAL | READER_ACTIVE))
|
---|
1396 | { has_local = 1; }
|
---|
1397 |
|
---|
1398 | cs_readunlock(&ea_list->ecmanswer_lock);
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | switch(ert->stage)
|
---|
1402 | {
|
---|
1403 | #ifdef CS_CACHEEX
|
---|
1404 | case 1: // Cache-Exchange
|
---|
1405 | {
|
---|
1406 | if(has_cacheex && !cacheex_left) { request_cw_from_readers(ert, 0); }
|
---|
1407 | break;
|
---|
1408 | }
|
---|
1409 | #endif
|
---|
1410 | case 2: // only local reader (used only if cfg.preferlocalcards=1)
|
---|
1411 | {
|
---|
1412 | if(has_local && !local_left) { request_cw_from_readers(ert, 0); }
|
---|
1413 | break;
|
---|
1414 | }
|
---|
1415 | case 3:
|
---|
1416 | {
|
---|
1417 | // any fallback reader not asked yet
|
---|
1418 | if(has_not_fallback && !reader_not_flb_left) { request_cw_from_readers(ert, 0); }
|
---|
1419 | break;
|
---|
1420 | }
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | if(!reader_left) // no more matching reader
|
---|
1424 | { ert->rc = E_NOTFOUND; } //so we set the return code
|
---|
1425 |
|
---|
1426 | break;
|
---|
1427 | }
|
---|
1428 | default:
|
---|
1429 | cs_log("unexpected ecm answer rc=%d.", ea->rc);
|
---|
1430 | return;
|
---|
1431 | break;
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | if(ert->rc < E_99)
|
---|
1435 | send_dcw(ert->client, ert);
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 | uint32_t chk_provid(uint8_t *ecm, uint16_t caid)
|
---|
1439 | {
|
---|
1440 | int32_t i, len, descriptor_length = 0;
|
---|
1441 | uint32_t provid = 0;
|
---|
1442 |
|
---|
1443 | switch(caid >> 8)
|
---|
1444 | {
|
---|
1445 | case 0x01:
|
---|
1446 | // seca
|
---|
1447 | provid = b2i(2, ecm + 3);
|
---|
1448 | break;
|
---|
1449 | case 0x05:
|
---|
1450 | // viaccess
|
---|
1451 | i = (ecm[4] == 0xD2) ? ecm[5] + 2 : 0; // skip d2 nano
|
---|
1452 | if((ecm[5 + i] == 3) && ((ecm[4 + i] == 0x90) || (ecm[4 + i] == 0x40)))
|
---|
1453 | { provid = (b2i(3, ecm + 6 + i) & 0xFFFFF0); }
|
---|
1454 |
|
---|
1455 | i = (ecm[6] == 0xD2) ? ecm[7] + 2 : 0; // skip d2 nano long ecm
|
---|
1456 | if((ecm[7 + i] == 7) && ((ecm[6 + i] == 0x90) || (ecm[6 + i] == 0x40)))
|
---|
1457 | { provid = (b2i(3, ecm + 8 + i) & 0xFFFFF0); }
|
---|
1458 |
|
---|
1459 | break;
|
---|
1460 | case 0x0D:
|
---|
1461 | // cryptoworks
|
---|
1462 | len = (((ecm[1] & 0xf) << 8) | ecm[2]) + 3;
|
---|
1463 | for(i = 8; i < len; i += descriptor_length + 2)
|
---|
1464 | {
|
---|
1465 | descriptor_length = ecm[i + 1];
|
---|
1466 | if(ecm[i] == 0x83)
|
---|
1467 | {
|
---|
1468 | provid = (uint32_t)ecm[i + 2] & 0xFE;
|
---|
1469 | break;
|
---|
1470 | }
|
---|
1471 | }
|
---|
1472 | break;
|
---|
1473 | #ifdef WITH_LB
|
---|
1474 | default:
|
---|
1475 | for(i = 0; i < CS_MAXCAIDTAB; i++)
|
---|
1476 | {
|
---|
1477 | uint16_t tcaid = cfg.lb_noproviderforcaid.caid[i];
|
---|
1478 | if(!tcaid) { break; }
|
---|
1479 | if(tcaid == caid)
|
---|
1480 | {
|
---|
1481 | provid = 0;
|
---|
1482 | break;
|
---|
1483 | }
|
---|
1484 | if(tcaid < 0x0100 && (caid >> 8) == tcaid)
|
---|
1485 | {
|
---|
1486 | provid = 0;
|
---|
1487 | break;
|
---|
1488 | }
|
---|
1489 | }
|
---|
1490 | #endif
|
---|
1491 | }
|
---|
1492 | return provid;
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | void update_chid(ECM_REQUEST *er)
|
---|
1496 | {
|
---|
1497 | er->chid = get_subid(er);
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 |
|
---|
1501 | int32_t write_ecm_answer(struct s_reader *reader, ECM_REQUEST *er, int8_t rc, uint8_t rcEx, uint8_t *cw, char *msglog)
|
---|
1502 | {
|
---|
1503 | if(!reader || !er || !er->tps.time) { return 0; }
|
---|
1504 |
|
---|
1505 | // drop too late answers, to avoid seg fault --> only answer until tps.time+((cfg.ctimeout+500)/1000+1) is accepted
|
---|
1506 | time_t timeout = time(NULL) - ((cfg.ctimeout+500)/1000+1);
|
---|
1507 | if(er->tps.time < timeout) //< and NOT <=
|
---|
1508 | { return 0; }
|
---|
1509 |
|
---|
1510 | int32_t i;
|
---|
1511 | uint8_t c;
|
---|
1512 | struct timeb now;
|
---|
1513 | cs_ftime(&now);
|
---|
1514 |
|
---|
1515 | if(er && er->parent)
|
---|
1516 | {
|
---|
1517 | // parent is only set on reader->client->ecmtask[], but we want original er
|
---|
1518 | ECM_REQUEST *er_reader_cp = er;
|
---|
1519 | er = er->parent; //Now er is "original" ecm, before it was the reader-copy
|
---|
1520 | er_reader_cp->rc = rc;
|
---|
1521 | er_reader_cp->idx = 0;
|
---|
1522 |
|
---|
1523 | timeout = time(NULL) - ((cfg.ctimeout+500)/1000+1);
|
---|
1524 | if(er->tps.time < timeout)
|
---|
1525 | { return 0; }
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | struct s_ecm_answer *ea = get_ecm_answer(reader, er);
|
---|
1529 | if(!ea) { return 0; }
|
---|
1530 |
|
---|
1531 | cs_writelock(&ea->ecmanswer_lock);
|
---|
1532 |
|
---|
1533 | if((ea->status & REQUEST_ANSWERED))
|
---|
1534 | {
|
---|
1535 | cs_debug_mask(D_READER, "Reader %s already answer, skip this ecm answer!", reader ? reader->label : "-");
|
---|
1536 | cs_writeunlock(&ea->ecmanswer_lock);
|
---|
1537 | return 0;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 |
|
---|
1541 | //SPECIAL CHECKs for rc
|
---|
1542 | if(rc < E_NOTFOUND && cw && chk_is_null_CW(cw)) //if cw=0 by anticascading
|
---|
1543 | {
|
---|
1544 | rc = E_NOTFOUND;
|
---|
1545 | cs_debug_mask(D_TRACE | D_LB, "WARNING: reader %s send fake cw, set rc=E_NOTFOUND!", reader ? reader->label : "-");
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | if(reader && cw && rc < E_NOTFOUND)
|
---|
1549 | {
|
---|
1550 | if(reader->disablecrccws == 0)
|
---|
1551 | {
|
---|
1552 | for(i = 0; i < 16; i += 4)
|
---|
1553 | {
|
---|
1554 | c = ((cw[i] + cw[i + 1] + cw[i + 2]) & 0xff);
|
---|
1555 | if(cw[i + 3] != c)
|
---|
1556 | {
|
---|
1557 | if(reader->dropbadcws)
|
---|
1558 | {
|
---|
1559 | rc = E_NOTFOUND;
|
---|
1560 | rcEx = E2_WRONG_CHKSUM;
|
---|
1561 | break;
|
---|
1562 | }
|
---|
1563 | else
|
---|
1564 | {
|
---|
1565 | cs_debug_mask(D_TRACE, "notice: changed dcw checksum byte cw[%i] from %02x to %02x", i + 3, cw[i + 3], c);
|
---|
1566 | cw[i + 3] = c;
|
---|
1567 | }
|
---|
1568 | }
|
---|
1569 | }
|
---|
1570 | }
|
---|
1571 | else
|
---|
1572 | {
|
---|
1573 | cs_debug_mask(D_TRACE, "notice: CW checksum check disabled");
|
---|
1574 | }
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | #ifdef CW_CYCLE_CHECK
|
---|
1578 | uint8_t cwc_ct = er->cwc_cycletime > 0 ? er->cwc_cycletime : 0;
|
---|
1579 | uint8_t cwc_ncwc = er->cwc_next_cw_cycle < 2 ? er->cwc_next_cw_cycle : 2;
|
---|
1580 | if(!checkcwcycle(er->client, er, reader, cw, rc, cwc_ct, cwc_ncwc))
|
---|
1581 | {
|
---|
1582 | rc = E_NOTFOUND;
|
---|
1583 | rcEx = E2_WRONG_CHKSUM;
|
---|
1584 | cs_debug_mask(D_CACHEEX | D_CWC | D_LB, "{client %s, caid %04X, srvid %04X} [write_ecm_answer] cyclecheck failed! Reader: %s set rc: %i", (er->client ? er->client->account->usr : "-"), er->caid, er->srvid, reader ? reader->label : "-", rc);
|
---|
1585 | }
|
---|
1586 | else { cs_debug_mask(D_CACHEEX | D_CWC | D_LB, "{client %s, caid %04X, srvid %04X} [write_ecm_answer] cyclecheck passed! Reader: %s rc: %i", (er->client ? er->client->account->usr : "-"), er->caid, er->srvid, reader ? reader->label : "-", rc); }
|
---|
1587 | #endif
|
---|
1588 | //END -- SPECIAL CHECKs for rc
|
---|
1589 |
|
---|
1590 |
|
---|
1591 | ea->status |= REQUEST_ANSWERED;
|
---|
1592 | ea->rc = rc;
|
---|
1593 | ea->ecm_time = comp_timeb(&now, &ea->time_request_sent);
|
---|
1594 | if(ea->ecm_time < 1) { ea->ecm_time = 1; } //set ecm_time 1 if answer immediately
|
---|
1595 | ea->rcEx = rcEx;
|
---|
1596 | if(cw) { memcpy(ea->cw, cw, 16); }
|
---|
1597 | if(msglog) { memcpy(ea->msglog, msglog, MSGLOGSIZE); }
|
---|
1598 |
|
---|
1599 | cs_writeunlock(&ea->ecmanswer_lock);
|
---|
1600 |
|
---|
1601 | struct timeb tpe;
|
---|
1602 | cs_ftime(&tpe);
|
---|
1603 | int32_t ntime = comp_timeb(&tpe, &er->tps);
|
---|
1604 | if(ntime < 1) { ntime = 1; }
|
---|
1605 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [write_ecm_answer] reader %s rc %d, ecm time %d ms (%d ms)", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid, reader ? reader->label : "-", rc, ea->ecm_time, ntime);
|
---|
1606 |
|
---|
1607 | //send ea for ecm request
|
---|
1608 | int32_t res = 0;
|
---|
1609 | struct s_client *cl = er->client;
|
---|
1610 | if(check_client(cl))
|
---|
1611 | {
|
---|
1612 | res = 1;
|
---|
1613 | add_job(er->client, ACTION_ECM_ANSWER_READER, ea, 0); //chk_dcw
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | //distribute ea for pendings
|
---|
1617 | if(ea->pending) //has pending ea
|
---|
1618 | { distribute_ea(ea); }
|
---|
1619 |
|
---|
1620 |
|
---|
1621 | if(!ea->is_pending) //not for pending ea - only once for ea
|
---|
1622 | {
|
---|
1623 | send_reader_stat(reader, er, ea, ea->rc); //send stats for LB
|
---|
1624 |
|
---|
1625 | //reader checks
|
---|
1626 | char ecmd5[17 * 3];
|
---|
1627 | cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
|
---|
1628 | rdr_debug_mask(reader, D_TRACE, "ecm answer for ecm hash %s rc=%d", ecmd5, ea->rc);
|
---|
1629 |
|
---|
1630 | //Update reader stats:
|
---|
1631 | if(ea->rc == E_FOUND)
|
---|
1632 | {
|
---|
1633 | if(cfg.cwlogdir != NULL)
|
---|
1634 | { logCWtoFile(er, ea->cw); } /* CWL logging only if cwlogdir is set in config */
|
---|
1635 |
|
---|
1636 | reader->ecmsok++;
|
---|
1637 | #ifdef CS_CACHEEX
|
---|
1638 | struct s_client *eacl = reader->client;
|
---|
1639 | if(cacheex_reader(reader) && check_client(eacl))
|
---|
1640 | {
|
---|
1641 | eacl->cwcacheexgot++;
|
---|
1642 | cacheex_add_stats(eacl, ea->er->caid, ea->er->srvid, ea->er->prid, 1);
|
---|
1643 | first_client->cwcacheexgot++;
|
---|
1644 | }
|
---|
1645 | #endif
|
---|
1646 | }
|
---|
1647 | else if(ea->rc == E_NOTFOUND)
|
---|
1648 | {
|
---|
1649 | reader->ecmsnok++;
|
---|
1650 | if(reader->ecmnotfoundlimit && reader->ecmsnok >= reader->ecmnotfoundlimit)
|
---|
1651 | {
|
---|
1652 | rdr_log(reader, "ECM not found limit reached %u. Restarting the reader.",
|
---|
1653 | reader->ecmsnok);
|
---|
1654 | reader->ecmsnok = 0; // Reset the variable
|
---|
1655 | reader->ecmshealthnok = 0; // Reset the variable
|
---|
1656 | add_job(reader->client, ACTION_READER_RESTART, NULL, 0);
|
---|
1657 | }
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | //Reader ECMs Health Try (by Pickser)
|
---|
1661 | if(reader->ecmsok != 0 || reader->ecmsnok != 0)
|
---|
1662 | {
|
---|
1663 | reader->ecmshealthok = ((double) reader->ecmsok / (reader->ecmsok + reader->ecmsnok)) * 100;
|
---|
1664 | reader->ecmshealthnok = ((double) reader->ecmsnok / (reader->ecmsok + reader->ecmsnok)) * 100;
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | if(rc == E_FOUND && reader->resetcycle > 0)
|
---|
1668 | {
|
---|
1669 | reader->resetcounter++;
|
---|
1670 | if(reader->resetcounter > reader->resetcycle)
|
---|
1671 | {
|
---|
1672 | reader->resetcounter = 0;
|
---|
1673 | rdr_log(reader, "Resetting reader, resetcyle of %d ecms reached", reader->resetcycle);
|
---|
1674 | reader->card_status = CARD_NEED_INIT;
|
---|
1675 | cardreader_reset(cl);
|
---|
1676 | }
|
---|
1677 | }
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | return res;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | static void guess_cardsystem(ECM_REQUEST *er)
|
---|
1684 | {
|
---|
1685 | uint16_t last_hope = 0;
|
---|
1686 |
|
---|
1687 | // viaccess - check by provid-search
|
---|
1688 | if((er->prid = chk_provid(er->ecm, 0x500)))
|
---|
1689 | { er->caid = 0x500; }
|
---|
1690 |
|
---|
1691 | // nagra
|
---|
1692 | // is ecm[1] always 0x30 ?
|
---|
1693 | // is ecm[3] always 0x07 ?
|
---|
1694 | if((er->ecm[6] == 1) && (er->ecm[4] == er->ecm[2] - 2))
|
---|
1695 | { er->caid = 0x1801; }
|
---|
1696 |
|
---|
1697 | // seca2 - very poor
|
---|
1698 | if((er->ecm[8] == 0x10) && ((er->ecm[9] & 0xF1) == 1))
|
---|
1699 | { last_hope = 0x100; }
|
---|
1700 |
|
---|
1701 | // is cryptoworks, but which caid ?
|
---|
1702 | if((er->ecm[3] == 0x81) && (er->ecm[4] == 0xFF) &&
|
---|
1703 | (!er->ecm[5]) && (!er->ecm[6]) && (er->ecm[7] == er->ecm[2] - 5))
|
---|
1704 | {
|
---|
1705 | last_hope = 0xd00;
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | if(!er->caid && er->ecm[2] == 0x31 && er->ecm[0x0b] == 0x28)
|
---|
1709 | { guess_irdeto(er); }
|
---|
1710 |
|
---|
1711 | if(!er->caid) // guess by len ..
|
---|
1712 | { er->caid = len4caid[er->ecm[2] + 3]; }
|
---|
1713 |
|
---|
1714 | if(!er->caid)
|
---|
1715 | { er->caid = last_hope; }
|
---|
1716 | }
|
---|
1717 |
|
---|
1718 | //chid calculation from module stat to here
|
---|
1719 | //to improve the quickfix concerning ecm chid info and extend it to all client requests wereby the chid is known in module stat
|
---|
1720 |
|
---|
1721 | uint32_t get_subid(ECM_REQUEST *er)
|
---|
1722 | {
|
---|
1723 | if(!er->ecmlen)
|
---|
1724 | { return 0; }
|
---|
1725 |
|
---|
1726 | uint32_t id = 0;
|
---|
1727 | switch(er->caid >> 8)
|
---|
1728 | {
|
---|
1729 | case 0x01:
|
---|
1730 | id = b2i(2, er->ecm + 7);
|
---|
1731 | break; // seca
|
---|
1732 | case 0x05:
|
---|
1733 | id = b2i(2, er->ecm + 8);
|
---|
1734 | break; // viaccess
|
---|
1735 | case 0x06:
|
---|
1736 | id = b2i(2, er->ecm + 6);
|
---|
1737 | break; // irdeto
|
---|
1738 | case 0x09:
|
---|
1739 | id = b2i(2, er->ecm + 11);
|
---|
1740 | break; // videoguard
|
---|
1741 | case 0x4A: // DRE-Crypt, Bulcrypt, Tongfang and others?
|
---|
1742 | if(!(er->caid == 0x4AEE)) // Bulcrypt excluded for now
|
---|
1743 | { id = b2i(2, er->ecm + 6); }
|
---|
1744 | break;
|
---|
1745 | }
|
---|
1746 | return id;
|
---|
1747 | }
|
---|
1748 |
|
---|
1749 |
|
---|
1750 | static void set_readers_counter(ECM_REQUEST *er)
|
---|
1751 | {
|
---|
1752 | struct s_ecm_answer *ea;
|
---|
1753 |
|
---|
1754 | er->reader_count = 0;
|
---|
1755 | er->fallback_reader_count = 0;
|
---|
1756 | for(ea = er->matching_rdr; ea; ea = ea->next)
|
---|
1757 | {
|
---|
1758 | if(ea->status & READER_ACTIVE)
|
---|
1759 | {
|
---|
1760 | if(!(ea->status & READER_FALLBACK))
|
---|
1761 | { er->reader_count++; }
|
---|
1762 | else
|
---|
1763 | { er->fallback_reader_count++; }
|
---|
1764 | }
|
---|
1765 | }
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 |
|
---|
1769 | void write_ecm_answer_fromcache(struct s_write_from_cache *wfc)
|
---|
1770 | {
|
---|
1771 | ECM_REQUEST *er = NULL;
|
---|
1772 | ECM_REQUEST *ecm = NULL;
|
---|
1773 |
|
---|
1774 | er = wfc->er_new;
|
---|
1775 | ecm = wfc->er_cache;
|
---|
1776 |
|
---|
1777 | int8_t rc_orig = er->rc;
|
---|
1778 |
|
---|
1779 | er->grp |= ecm->grp; //update group
|
---|
1780 | #ifdef CS_CACHEEX
|
---|
1781 | if(ecm->from_csp) { er->csp_answered = 1; } //update er as answered by csp (csp have no group)
|
---|
1782 | #endif
|
---|
1783 |
|
---|
1784 | if(er->rc >= E_NOTFOUND)
|
---|
1785 | {
|
---|
1786 | #ifdef CS_CACHEEX
|
---|
1787 | if(ecm->cacheex_src) //from cacheex or csp
|
---|
1788 | {
|
---|
1789 | er->rc = E_CACHEEX;
|
---|
1790 | }
|
---|
1791 | else
|
---|
1792 | #endif
|
---|
1793 | { er->rc=E_CACHE1; } //from normal readers
|
---|
1794 |
|
---|
1795 | memcpy(er->cw, ecm->cw, 16);
|
---|
1796 | er->selected_reader = ecm->selected_reader;
|
---|
1797 | er->cw_count = ecm->cw_count;
|
---|
1798 |
|
---|
1799 | #ifdef CS_CACHEEX
|
---|
1800 | if(ecm->cacheex_src && is_valid_client(ecm->cacheex_src) && !ecm->cacheex_src->kill){ //here we should be sure cex client has not been freed!
|
---|
1801 | er->cacheex_src = ecm->cacheex_src;
|
---|
1802 | er->cwc_cycletime = ecm->cwc_cycletime;
|
---|
1803 | er->cwc_next_cw_cycle = ecm->cwc_next_cw_cycle;
|
---|
1804 | }else{
|
---|
1805 | er->cacheex_src = NULL;
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | int8_t cacheex = check_client(er->client) && er->client->account ? er->client->account->cacheex.mode : 0;
|
---|
1809 | if(cacheex == 1 && check_client(er->client))
|
---|
1810 | {
|
---|
1811 | cacheex_add_stats(er->client, er->caid, er->srvid, er->prid, 0);
|
---|
1812 | er->client->cwcacheexpush++;
|
---|
1813 | if(er->client->account)
|
---|
1814 | { er->client->account->cwcacheexpush++; }
|
---|
1815 | first_client->cwcacheexpush++;
|
---|
1816 | }
|
---|
1817 | #endif
|
---|
1818 |
|
---|
1819 | #ifdef CS_CACHEEX
|
---|
1820 | if(cfg.delay && cacheex!=1) //No delay on cacheexchange mode 1 client!
|
---|
1821 | { cs_sleepms(cfg.delay); }
|
---|
1822 | #else
|
---|
1823 | if(cfg.delay)
|
---|
1824 | { cs_sleepms(cfg.delay); }
|
---|
1825 | #endif
|
---|
1826 |
|
---|
1827 | if(rc_orig == E_UNHANDLED)
|
---|
1828 | {
|
---|
1829 | cs_debug_mask(D_LB,"{client %s, caid %04X, prid %06X, srvid %04X} [write_ecm_answer_fromcache] found cw in CACHE (count %d)!", (check_client(er->client)?er->client->account->usr:"-"),er->caid, er->prid, er->srvid, er->cw_count);
|
---|
1830 | send_dcw(er->client, er);
|
---|
1831 | }
|
---|
1832 | }
|
---|
1833 | }
|
---|
1834 |
|
---|
1835 |
|
---|
1836 | void get_cw(struct s_client *client, ECM_REQUEST *er)
|
---|
1837 | {
|
---|
1838 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [get_cw] NEW REQUEST!", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
|
---|
1839 | increment_n_request(client);
|
---|
1840 |
|
---|
1841 | int32_t i, j, m;
|
---|
1842 | time_t now = time((time_t *)0);
|
---|
1843 | uint32_t line = 0;
|
---|
1844 |
|
---|
1845 | er->client = client;
|
---|
1846 | er->rc = E_UNHANDLED; // set default rc status to unhandled
|
---|
1847 | er->cwc_next_cw_cycle = 2; //set it to: we dont know
|
---|
1848 | if(now - client->lastecm > cfg.hideclient_to) { client->lastswitch = 0; } // user was on freetv or didn't request for some time so we reset lastswitch to get correct stats/webif display
|
---|
1849 | client->lastecm = now;
|
---|
1850 |
|
---|
1851 | if(client == first_client || !client ->account || client->account == first_client->account)
|
---|
1852 | {
|
---|
1853 | //DVBApi+serial is allowed to request anonymous accounts:
|
---|
1854 | int16_t listenertype = get_module(client)->listenertype;
|
---|
1855 | if(listenertype != LIS_DVBAPI && listenertype != LIS_SERIAL)
|
---|
1856 | {
|
---|
1857 | er->rc = E_INVALID;
|
---|
1858 | er->rcEx = E2_GLOBAL;
|
---|
1859 | snprintf(er->msglog, sizeof(er->msglog), "invalid user account %s", username(client));
|
---|
1860 | }
|
---|
1861 | }
|
---|
1862 |
|
---|
1863 | if(er->ecmlen > MAX_ECM_SIZE)
|
---|
1864 | {
|
---|
1865 | er->rc = E_INVALID;
|
---|
1866 | er->rcEx = E2_GLOBAL;
|
---|
1867 | snprintf(er->msglog, sizeof(er->msglog), "ECM size %d > Max Ecm size %d, ignored! client %s", er->ecmlen, MAX_ECM_SIZE, username(client));
|
---|
1868 | }
|
---|
1869 |
|
---|
1870 | if(!client->grp)
|
---|
1871 | {
|
---|
1872 | er->rc = E_INVALID;
|
---|
1873 | er->rcEx = E2_GROUP;
|
---|
1874 | snprintf(er->msglog, sizeof(er->msglog), "invalid user group %s", username(client));
|
---|
1875 | }
|
---|
1876 |
|
---|
1877 |
|
---|
1878 | if(!er->caid)
|
---|
1879 | { guess_cardsystem(er); }
|
---|
1880 |
|
---|
1881 | /* Quickfix Area */
|
---|
1882 |
|
---|
1883 | // add chid for all client requests as in module stat
|
---|
1884 | update_chid(er);
|
---|
1885 |
|
---|
1886 | // quickfix for 0100:000065
|
---|
1887 | if(er->caid == 0x100 && er->prid == 0x65 && er->srvid == 0)
|
---|
1888 | { er->srvid = 0x0642; }
|
---|
1889 |
|
---|
1890 | // Quickfixes for Opticum/Globo HD9500
|
---|
1891 | // Quickfix for 0500:030300
|
---|
1892 | if(er->caid == 0x500 && er->prid == 0x030300)
|
---|
1893 | { er->prid = 0x030600; }
|
---|
1894 |
|
---|
1895 | // Quickfix for 0500:D20200
|
---|
1896 | if(er->caid == 0x500 && er->prid == 0xD20200)
|
---|
1897 | { er->prid = 0x030600; }
|
---|
1898 |
|
---|
1899 | //betacrypt ecm with nagra header
|
---|
1900 | if(chk_is_betatunnel_caid(er->caid) == 1 && (er->ecmlen == 0x89 || er->ecmlen == 0x4A) && er->ecm[3] == 0x07 && (er->ecm[4] == 0x84 || er->ecm[4] == 0x45))
|
---|
1901 | {
|
---|
1902 | if(er->caid == 0x1702)
|
---|
1903 | {
|
---|
1904 | er->caid = 0x1833;
|
---|
1905 | }
|
---|
1906 | else
|
---|
1907 | {
|
---|
1908 | check_lb_auto_betatunnel_mode(er);
|
---|
1909 | }
|
---|
1910 | cs_debug_mask(D_TRACE, "Quickfix remap beta->nagra: 0x%X, 0x%X, 0x%X, 0x%X", er->caid, er->ecmlen, er->ecm[3], er->ecm[4]);
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | //nagra ecm with betacrypt header 1801, 1833, 1834, 1835
|
---|
1914 | if(chk_is_betatunnel_caid(er->caid) == 2 && (er->ecmlen == 0x93 || er->ecmlen == 0x54) && er->ecm[13] == 0x07 && (er->ecm[14] == 0x84 || er->ecm[14] == 0x45))
|
---|
1915 | {
|
---|
1916 | if(er->caid == 0x1833)
|
---|
1917 | {
|
---|
1918 | er->caid = 0x1702;
|
---|
1919 | }
|
---|
1920 | else
|
---|
1921 | {
|
---|
1922 | er->caid = 0x1722;
|
---|
1923 | }
|
---|
1924 | cs_debug_mask(D_TRACE, "Quickfix remap nagra->beta: 0x%X, 0x%X, 0x%X, 0x%X", er->caid, er->ecmlen, er->ecm[13], er->ecm[44]);
|
---|
1925 | }
|
---|
1926 |
|
---|
1927 | //Ariva quickfix (invalid nagra provider)
|
---|
1928 | if(((er->caid & 0xFF00) == 0x1800) && er->prid > 0x00FFFF)
|
---|
1929 | { er->prid = 0; }
|
---|
1930 |
|
---|
1931 | //Check for invalid provider, extract provider out of ecm:
|
---|
1932 | uint32_t prid = chk_provid(er->ecm, er->caid);
|
---|
1933 | if(!er->prid)
|
---|
1934 | {
|
---|
1935 | er->prid = prid;
|
---|
1936 | }
|
---|
1937 | else
|
---|
1938 | {
|
---|
1939 | if(prid && prid != er->prid)
|
---|
1940 | {
|
---|
1941 | cs_debug_mask(D_TRACE, "provider fixed: %04X:%06X to %04X:%06X", er->caid, er->prid, er->caid, prid);
|
---|
1942 | er->prid = prid;
|
---|
1943 | }
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 | #ifdef MODULE_NEWCAMD
|
---|
1947 | // Set providerid for newcamd clients if none is given
|
---|
1948 | if(!er->prid && client->ncd_server)
|
---|
1949 | {
|
---|
1950 | int32_t pi = client->port_idx;
|
---|
1951 | if(pi >= 0 && cfg.ncd_ptab.nports && cfg.ncd_ptab.nports >= pi && cfg.ncd_ptab.ports[pi].ncd)
|
---|
1952 | { er->prid = cfg.ncd_ptab.ports[pi].ncd->ncd_ftab.filts[0].prids[0]; }
|
---|
1953 | }
|
---|
1954 | #endif
|
---|
1955 |
|
---|
1956 | // CAID not supported or found
|
---|
1957 | if(!er->caid)
|
---|
1958 | {
|
---|
1959 | er->rc = E_INVALID;
|
---|
1960 | er->rcEx = E2_CAID;
|
---|
1961 | snprintf(er->msglog, MSGLOGSIZE, "CAID not supported or found");
|
---|
1962 | }
|
---|
1963 |
|
---|
1964 | // user expired
|
---|
1965 | if(client->expirationdate && client->expirationdate < client->lastecm)
|
---|
1966 | { er->rc = E_EXPDATE; }
|
---|
1967 |
|
---|
1968 | // out of timeframe
|
---|
1969 | if(client->allowedtimeframe[0] && client->allowedtimeframe[1])
|
---|
1970 | {
|
---|
1971 | struct tm acttm;
|
---|
1972 | localtime_r(&now, &acttm);
|
---|
1973 | int32_t curtime = (acttm.tm_hour * 60) + acttm.tm_min;
|
---|
1974 | int32_t mintime = client->allowedtimeframe[0];
|
---|
1975 | int32_t maxtime = client->allowedtimeframe[1];
|
---|
1976 | if(!((mintime <= maxtime && curtime > mintime && curtime < maxtime) || (mintime > maxtime && (curtime > mintime || curtime < maxtime))))
|
---|
1977 | {
|
---|
1978 | er->rc = E_EXPDATE;
|
---|
1979 | }
|
---|
1980 | cs_debug_mask(D_TRACE, "Check Timeframe - result: %d, start: %d, current: %d, end: %d\n", er->rc, mintime, curtime, maxtime);
|
---|
1981 | }
|
---|
1982 |
|
---|
1983 | // user disabled
|
---|
1984 | if(client->disabled != 0)
|
---|
1985 | {
|
---|
1986 | if(client->failban & BAN_DISABLED)
|
---|
1987 | {
|
---|
1988 | cs_add_violation(client, client->account->usr);
|
---|
1989 | cs_disconnect_client(client);
|
---|
1990 | }
|
---|
1991 | er->rc = E_DISABLED;
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | if(!chk_global_whitelist(er, &line))
|
---|
1995 | {
|
---|
1996 | debug_ecm(D_TRACE, "whitelist filtered: %s (%s) line %d", username(client), buf, line);
|
---|
1997 | er->rc = E_INVALID;
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 | #ifdef CS_CACHEEX
|
---|
2001 | if(client->account && client->account->cacheex.mode==2 && !client->account->cacheex.allow_request)
|
---|
2002 | {
|
---|
2003 | er->rc = E_INVALID;
|
---|
2004 | snprintf(er->msglog, MSGLOGSIZE, "invalid request form cacheex-2 client");
|
---|
2005 | }
|
---|
2006 | #endif
|
---|
2007 |
|
---|
2008 |
|
---|
2009 | // rc<100 -> ecm error
|
---|
2010 | if(er->rc >= E_UNHANDLED)
|
---|
2011 | {
|
---|
2012 | m = er->caid;
|
---|
2013 | i = er->srvid;
|
---|
2014 |
|
---|
2015 | if(i != client->last_srvid || !client->lastswitch)
|
---|
2016 | {
|
---|
2017 | if(cfg.usrfileflag)
|
---|
2018 | { cs_statistics(client); }
|
---|
2019 | client->lastswitch = now;
|
---|
2020 | }
|
---|
2021 |
|
---|
2022 | // user sleeping
|
---|
2023 | if(client->tosleep && (now - client->lastswitch > client->tosleep))
|
---|
2024 | {
|
---|
2025 | if(client->failban & BAN_SLEEPING)
|
---|
2026 | {
|
---|
2027 | cs_add_violation(client, client->account->usr);
|
---|
2028 | cs_disconnect_client(client);
|
---|
2029 | }
|
---|
2030 | if(client->c35_sleepsend != 0)
|
---|
2031 | {
|
---|
2032 | er->rc = E_STOPPED; // send stop command CMD08 {00 xx}
|
---|
2033 | }
|
---|
2034 | else
|
---|
2035 | {
|
---|
2036 | er->rc = E_SLEEPING;
|
---|
2037 | }
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | client->last_srvid = i;
|
---|
2041 | client->last_caid = m;
|
---|
2042 |
|
---|
2043 | int32_t ecm_len = (((er->ecm[1] & 0x0F) << 8) | er->ecm[2]) + 3;
|
---|
2044 |
|
---|
2045 | for(j = 0; (j < 6) && (er->rc >= E_UNHANDLED); j++)
|
---|
2046 | {
|
---|
2047 | switch(j)
|
---|
2048 | {
|
---|
2049 | case 0:
|
---|
2050 | // fake (uniq)
|
---|
2051 | if(client->dup)
|
---|
2052 | { er->rc = E_FAKE; }
|
---|
2053 | break;
|
---|
2054 | case 1:
|
---|
2055 | // invalid (caid)
|
---|
2056 | if(!chk_bcaid(er, &client->ctab))
|
---|
2057 | {
|
---|
2058 | er->rc = E_INVALID;
|
---|
2059 | er->rcEx = E2_CAID;
|
---|
2060 | snprintf(er->msglog, MSGLOGSIZE, "invalid caid 0x%04X", er->caid);
|
---|
2061 | }
|
---|
2062 | break;
|
---|
2063 | case 2:
|
---|
2064 | // invalid (srvid)
|
---|
2065 | // matching srvids (or 0000) specified in betatunnel will bypass this filter
|
---|
2066 | if(!chk_srvid(client, er))
|
---|
2067 | {
|
---|
2068 | if(!chk_on_btun(SRVID_ZERO, client, er))
|
---|
2069 | {
|
---|
2070 | er->rc = E_INVALID;
|
---|
2071 | snprintf(er->msglog, MSGLOGSIZE, "invalid SID");
|
---|
2072 | }
|
---|
2073 | }
|
---|
2074 | break;
|
---|
2075 | case 3:
|
---|
2076 | // invalid (ufilters)
|
---|
2077 | if(!chk_ufilters(er))
|
---|
2078 | { er->rc = E_INVALID; }
|
---|
2079 | break;
|
---|
2080 | case 4:
|
---|
2081 | // invalid (sfilter)
|
---|
2082 | if(!chk_sfilter(er, &get_module(client)->ptab))
|
---|
2083 | { er->rc = E_INVALID; }
|
---|
2084 | break;
|
---|
2085 | case 5:
|
---|
2086 | // corrupt
|
---|
2087 | if((i = er->ecmlen - ecm_len))
|
---|
2088 | {
|
---|
2089 | if(i > 0)
|
---|
2090 | {
|
---|
2091 | cs_debug_mask(D_TRACE, "warning: ecm size adjusted from %d to %d", er->ecmlen, ecm_len);
|
---|
2092 | er->ecmlen = ecm_len;
|
---|
2093 | }
|
---|
2094 | else
|
---|
2095 | { er->rc = E_CORRUPT; }
|
---|
2096 | }
|
---|
2097 | break;
|
---|
2098 | }
|
---|
2099 | }
|
---|
2100 | }
|
---|
2101 |
|
---|
2102 |
|
---|
2103 | //not continue, send rc to client
|
---|
2104 | if(er->rc < E_UNHANDLED)
|
---|
2105 | {
|
---|
2106 | send_dcw(client, er);
|
---|
2107 | free_ecm(er);
|
---|
2108 | return;
|
---|
2109 | }
|
---|
2110 |
|
---|
2111 |
|
---|
2112 | #ifdef CS_CACHEEX
|
---|
2113 | int8_t cacheex = client->account ? client->account->cacheex.mode : 0;
|
---|
2114 | er->from_cacheex1_client = 0;
|
---|
2115 | if(cacheex == 1) { er->from_cacheex1_client = 1; }
|
---|
2116 | #endif
|
---|
2117 |
|
---|
2118 |
|
---|
2119 | //Schlocke: above checks could change er->rc so
|
---|
2120 | /*BetaCrypt tunneling
|
---|
2121 | *moved behind the check routines,
|
---|
2122 | *because newcamd ECM will fail
|
---|
2123 | *if ECM is converted before
|
---|
2124 | */
|
---|
2125 | if(chk_is_betatunnel_caid(er->caid) && client->ttab.n)
|
---|
2126 | {
|
---|
2127 | cs_ddump_mask(D_TRACE, er->ecm, 13, "betatunnel? ecmlen=%d", er->ecmlen);
|
---|
2128 | cs_betatunnel(er);
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 | // ignore ecm ...
|
---|
2132 | int32_t offset = 3;
|
---|
2133 | // ... and betacrypt header for cache md5 calculation
|
---|
2134 | if((er->caid >> 8) == 0x17)
|
---|
2135 | { offset = 13; }
|
---|
2136 | unsigned char md5tmp[MD5_DIGEST_LENGTH];
|
---|
2137 | // store ECM in cache
|
---|
2138 | memcpy(er->ecmd5, MD5(er->ecm + offset, er->ecmlen - offset, md5tmp), CS_ECMSTORESIZE);
|
---|
2139 | cacheex_update_hash(er);
|
---|
2140 | ac_chk(client, er, 0);
|
---|
2141 |
|
---|
2142 |
|
---|
2143 | //******** CHECK IF FOUND ECM IN CACHE
|
---|
2144 | struct ecm_request_t *ecm = NULL;
|
---|
2145 | ecm = check_cache(er, client);
|
---|
2146 | if(ecm) //found in cache
|
---|
2147 | {
|
---|
2148 | cs_debug_mask(D_LB,"{client %s, caid %04X, prid %06X, srvid %04X} [get_cw] cw found immediately in cache! ", (check_client(er->client)?er->client->account->usr:"-"),er->caid, er->prid, er->srvid);
|
---|
2149 |
|
---|
2150 | struct s_write_from_cache *wfc = NULL;
|
---|
2151 | if(!cs_malloc(&wfc, sizeof(struct s_write_from_cache)))
|
---|
2152 | {
|
---|
2153 | NULLFREE(ecm);
|
---|
2154 | free_ecm(er);
|
---|
2155 | return;
|
---|
2156 | }
|
---|
2157 |
|
---|
2158 | wfc->er_new = er;
|
---|
2159 | wfc->er_cache = ecm;
|
---|
2160 | write_ecm_answer_fromcache(wfc);
|
---|
2161 | NULLFREE(ecm);
|
---|
2162 | free_ecm(er);
|
---|
2163 |
|
---|
2164 | return;
|
---|
2165 | }
|
---|
2166 |
|
---|
2167 |
|
---|
2168 | er->reader_avail = 0;
|
---|
2169 | er->readers = 0;
|
---|
2170 | #ifdef CS_CACHEEX
|
---|
2171 | er->reader_nocacheex_avail = 0;
|
---|
2172 | #endif
|
---|
2173 |
|
---|
2174 | struct s_ecm_answer *ea, *prv = NULL;
|
---|
2175 | struct s_reader *rdr;
|
---|
2176 |
|
---|
2177 | cs_readlock(&readerlist_lock);
|
---|
2178 | cs_readlock(&clientlist_lock);
|
---|
2179 |
|
---|
2180 | for(rdr = first_active_reader; rdr; rdr = rdr->next)
|
---|
2181 | {
|
---|
2182 | uint8_t is_fallback = chk_is_fixed_fallback(rdr, er);
|
---|
2183 |
|
---|
2184 | int8_t match = matching_reader(er, rdr);
|
---|
2185 | #ifdef WITH_LB
|
---|
2186 | //if this reader does not match, check betatunnel for it
|
---|
2187 | if(!match && cfg.lb_auto_betatunnel)
|
---|
2188 | {
|
---|
2189 | uint16_t caid = lb_get_betatunnel_caid_to(er->caid);
|
---|
2190 | if(caid)
|
---|
2191 | {
|
---|
2192 | uint16_t save_caid = er->caid;
|
---|
2193 | er->caid = caid;
|
---|
2194 | match = matching_reader(er, rdr); //matching
|
---|
2195 | er->caid = save_caid;
|
---|
2196 | }
|
---|
2197 | }
|
---|
2198 | #endif
|
---|
2199 | if(match)
|
---|
2200 | {
|
---|
2201 | er->reader_avail++;
|
---|
2202 |
|
---|
2203 | #ifdef CS_CACHEEX
|
---|
2204 | if(!rdr->cacheex.mode) { er->reader_nocacheex_avail++; }
|
---|
2205 | if(cacheex == 1 && !cacheex_reader(rdr)) //ex1-cl only ask ex1-rdr
|
---|
2206 | { continue; }
|
---|
2207 | #endif
|
---|
2208 |
|
---|
2209 | if(!cs_malloc(&ea, sizeof(struct s_ecm_answer)))
|
---|
2210 | { goto OUT; }
|
---|
2211 |
|
---|
2212 | er->readers++;
|
---|
2213 |
|
---|
2214 | ea->reader = rdr;
|
---|
2215 | ea->er = er;
|
---|
2216 | ea->rc = E_UNHANDLED;
|
---|
2217 | if(prv)
|
---|
2218 | { prv->next = ea; }
|
---|
2219 | else
|
---|
2220 | { er->matching_rdr = ea; }
|
---|
2221 | prv = ea;
|
---|
2222 |
|
---|
2223 | ea->status = READER_ACTIVE;
|
---|
2224 | if(!is_network_reader(rdr))
|
---|
2225 | { ea->status |= READER_LOCAL; }
|
---|
2226 | else if(cacheex_reader(rdr))
|
---|
2227 | {
|
---|
2228 | ea->status |= READER_CACHEEX;
|
---|
2229 | er->cacheex_reader_count++;
|
---|
2230 | }
|
---|
2231 |
|
---|
2232 | if(is_fallback)
|
---|
2233 | { ea->status |= READER_FALLBACK; }
|
---|
2234 |
|
---|
2235 | ea->pending = NULL;
|
---|
2236 | ea->is_pending = false;
|
---|
2237 | cs_lock_create(&ea->ecmanswer_lock, "ecmanswer_lock", 5000);
|
---|
2238 | }
|
---|
2239 | }
|
---|
2240 |
|
---|
2241 | OUT:
|
---|
2242 | cs_readunlock(&clientlist_lock);
|
---|
2243 | cs_readunlock(&readerlist_lock);
|
---|
2244 |
|
---|
2245 |
|
---|
2246 | #ifdef WITH_LB
|
---|
2247 | //FILTER readers by loadbalancing
|
---|
2248 | stat_get_best_reader(er);
|
---|
2249 | #endif
|
---|
2250 |
|
---|
2251 | //set reader_count and fallback_reader_count
|
---|
2252 | set_readers_counter(er);
|
---|
2253 |
|
---|
2254 |
|
---|
2255 | #ifdef CS_CACHEEX
|
---|
2256 | //WAIT_TIME
|
---|
2257 | uint32_t cacheex_wait_time = 0;
|
---|
2258 | uint32_t wait_time_no_hitcache = 0;
|
---|
2259 | uint32_t wait_time_hitcache = 0;
|
---|
2260 |
|
---|
2261 | if(client->account && !client->account->no_wait_time)
|
---|
2262 | {
|
---|
2263 | wait_time_no_hitcache = get_cacheex_wait_time(er,NULL); //NO check hitcache. Wait_time is dwtime, or, if 0, awtime.
|
---|
2264 | wait_time_hitcache = get_cacheex_wait_time(er,client); //check hitcache for calculating wait_time! If hitcache wait_time is biggest value between dwtime and awtime, else it's awtime.
|
---|
2265 |
|
---|
2266 | if(
|
---|
2267 | //If "normal" client and ex1-rdr>0, we cannot use hitcache for calculating wait_time because we cannot know if cw is available or not on ex1 server!
|
---|
2268 | (cacheex != 1 && er->cacheex_reader_count)
|
---|
2269 | ||
|
---|
2270 | /* Cw for ex1-cl comes from: INT. cache by "normal" readers (normal clients that ask normal readers), ex1-rdr and ex2-rdr and ex3-rdr.
|
---|
2271 | * If readers, we have to wait cws generating by normal clients asking normal readers and answers by ex1-rdr (cannot use hitcache).
|
---|
2272 | * If no readers, use hitcache for calculating wait_time.
|
---|
2273 | */
|
---|
2274 | (cacheex == 1 && er->reader_avail)
|
---|
2275 | )
|
---|
2276 | { cacheex_wait_time = wait_time_no_hitcache; }
|
---|
2277 | else
|
---|
2278 | { cacheex_wait_time = wait_time_hitcache; }
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | cs_debug_mask(D_TRACE | D_CACHEEX, "[GET_CW] wait_time %d caid %04X prov %06X srvid %04X rc %d cacheex cl mode %d ex1rdr %d", cacheex_wait_time, er->caid, er->prid, er->srvid, er->rc, cacheex, er->cacheex_reader_count);
|
---|
2282 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [get_cw] wait_time %d - client cacheex mode %d, reader avail for ecm %d, hitcache %d", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid, cacheex_wait_time, cacheex == 1 ? 1 : 0, er->reader_avail, wait_time_hitcache ? 1 : 0);
|
---|
2283 | //END WAIT_TIME calculation
|
---|
2284 |
|
---|
2285 | if(!cacheex_wait_time && (er->reader_count + er->fallback_reader_count) == 0)
|
---|
2286 | #else
|
---|
2287 | if((er->reader_count + er->fallback_reader_count) == 0)
|
---|
2288 | #endif
|
---|
2289 | {
|
---|
2290 | er->rc = E_NOTFOUND;
|
---|
2291 | if(!er->rcEx)
|
---|
2292 | { er->rcEx = E2_GROUP; }
|
---|
2293 | snprintf(er->msglog, MSGLOGSIZE, "no matching reader");
|
---|
2294 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [get_cw] NO Readers and NO wait_time... not_found! ", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid);
|
---|
2295 | send_dcw(client, er);
|
---|
2296 | free_ecm(er);
|
---|
2297 | return;
|
---|
2298 | }
|
---|
2299 |
|
---|
2300 |
|
---|
2301 | //insert it in ecmcwcache!
|
---|
2302 | cs_writelock(&ecmcache_lock);
|
---|
2303 | er->next = ecmcwcache;
|
---|
2304 | ecmcwcache = er;
|
---|
2305 | ecmcwcache_size++;
|
---|
2306 | cs_writeunlock(&ecmcache_lock);
|
---|
2307 |
|
---|
2308 |
|
---|
2309 | #ifdef WITH_LB
|
---|
2310 | if(cfg.lb_mode){
|
---|
2311 | //cache2 is handled by readers queue, so, if a same ecm hash with same readers, use these same readers to get cache2 from them! Not ask other readers!
|
---|
2312 | struct ecm_request_t *ecm_eq = NULL;
|
---|
2313 | ecm_eq = check_same_ecm(er);
|
---|
2314 | if(ecm_eq)
|
---|
2315 | {
|
---|
2316 | //set all readers used by ecm_eq, so we get cache2 from them!
|
---|
2317 | use_same_readers(er, ecm_eq);
|
---|
2318 | cs_debug_mask(D_LB, "{client %s, caid %04X, prid %06X, srvid %04X} [get_cw] found same ecm with same readers from client %s, use them!", (check_client(er->client) ? er->client->account->usr : "-"), er->caid, er->prid, er->srvid, (check_client(ecm_eq->client) ? ecm_eq->client->account->usr : "-"));
|
---|
2319 |
|
---|
2320 | //set reader_count and fallback_reader_count
|
---|
2321 | set_readers_counter(er);
|
---|
2322 | }
|
---|
2323 | }
|
---|
2324 | #endif
|
---|
2325 |
|
---|
2326 |
|
---|
2327 | er->rcEx = 0;
|
---|
2328 | #ifdef CS_CACHEEX
|
---|
2329 | er->cacheex_wait_time_expired = 1;
|
---|
2330 | er->cacheex_hitcache = 0;
|
---|
2331 | if(cacheex_wait_time) //wait time for cacheex
|
---|
2332 | {
|
---|
2333 | add_ms_to_timeb(&er->cacheex_wait, cacheex_wait_time);
|
---|
2334 | er->cacheex_wait_time = cacheex_wait_time;
|
---|
2335 | er->cacheex_wait_time_expired = 0;
|
---|
2336 | if(er->cacheex_reader_count > 0)
|
---|
2337 | {
|
---|
2338 | er->cacheex_hitcache = wait_time_hitcache ? 1 : 0; //usefull only when cacheex mode 1 readers answers before wait_time and we have to decide if we have to wait until wait_time expires.
|
---|
2339 | request_cw_from_readers(er, 1); // setting stop_stage=1, we request only cacheex mode 1 readers. Others are requested at cacheex timeout!
|
---|
2340 | }
|
---|
2341 | }
|
---|
2342 | else
|
---|
2343 | #endif
|
---|
2344 | request_cw_from_readers(er, 0);
|
---|
2345 |
|
---|
2346 |
|
---|
2347 | #ifdef WITH_DEBUG
|
---|
2348 | if(D_CLIENTECM & cs_dblevel)
|
---|
2349 | {
|
---|
2350 | char buf[ECM_FMT_LEN];
|
---|
2351 | format_ecm(er, buf, ECM_FMT_LEN);
|
---|
2352 | cs_ddump_mask(D_CLIENTECM, er->ecm, er->ecmlen, "Client %s ECM dump %s", username(client), buf);
|
---|
2353 | }
|
---|
2354 | #endif
|
---|
2355 |
|
---|
2356 | cw_process_thread_wakeup();
|
---|
2357 | }
|
---|
2358 |
|
---|
2359 |
|
---|
2360 | int32_t ecmfmt(uint16_t caid, uint16_t onid, uint32_t prid, uint16_t chid, uint16_t pid, uint16_t srvid, uint16_t l, char *ecmd5hex, char *csphash, char *cw, char *result, size_t size, uint16_t origin_peer, uint8_t distance)
|
---|
2361 | {
|
---|
2362 | if(!cfg.ecmfmt)
|
---|
2363 | { return snprintf(result, size, "%04X&%06X/%04X/%04X/%02X:%s", caid, prid, chid, srvid, l, ecmd5hex); }
|
---|
2364 |
|
---|
2365 | uint32_t s = 0, zero = 0, flen = 0, value = 0;
|
---|
2366 | char *c = cfg.ecmfmt, fmt[5] = "%04X";
|
---|
2367 | while(*c)
|
---|
2368 | {
|
---|
2369 | switch(*c)
|
---|
2370 | {
|
---|
2371 | case '0':
|
---|
2372 | zero = 1;
|
---|
2373 | value = 0;
|
---|
2374 | break;
|
---|
2375 | case 'c':
|
---|
2376 | flen = 4;
|
---|
2377 | value = caid;
|
---|
2378 | break;
|
---|
2379 | case 'o':
|
---|
2380 | flen = 4;
|
---|
2381 | value = onid;
|
---|
2382 | break;
|
---|
2383 | case 'p':
|
---|
2384 | flen = 6;
|
---|
2385 | value = prid;
|
---|
2386 | break;
|
---|
2387 | case 'i':
|
---|
2388 | flen = 4;
|
---|
2389 | value = chid;
|
---|
2390 | break;
|
---|
2391 | case 'd':
|
---|
2392 | flen = 4;
|
---|
2393 | value = pid;
|
---|
2394 | break;
|
---|
2395 | case 's':
|
---|
2396 | flen = 4;
|
---|
2397 | value = srvid;
|
---|
2398 | break;
|
---|
2399 | case 'l':
|
---|
2400 | flen = 2;
|
---|
2401 | value = l;
|
---|
2402 | break;
|
---|
2403 | case 'h':
|
---|
2404 | flen = CS_ECMSTORESIZE;
|
---|
2405 | break;
|
---|
2406 | case 'e':
|
---|
2407 | flen = 5;
|
---|
2408 | break;
|
---|
2409 | case 'w':
|
---|
2410 | flen = 17;
|
---|
2411 | break;
|
---|
2412 | case 'j':
|
---|
2413 | flen = 2;
|
---|
2414 | value = distance;
|
---|
2415 | break;
|
---|
2416 | case 'g':
|
---|
2417 | flen = 4;
|
---|
2418 | value = origin_peer;
|
---|
2419 | break;
|
---|
2420 | case '\\':
|
---|
2421 | c++;
|
---|
2422 | flen = 0;
|
---|
2423 | value = *c;
|
---|
2424 | break;
|
---|
2425 | default:
|
---|
2426 | flen = 0;
|
---|
2427 | value = *c;
|
---|
2428 | break;
|
---|
2429 | }
|
---|
2430 | if(value)
|
---|
2431 | { zero = 0; }
|
---|
2432 |
|
---|
2433 | if(!zero)
|
---|
2434 | {
|
---|
2435 | //fmt[0] = '%';
|
---|
2436 | if(flen) //Build %04X / %06X / %02X
|
---|
2437 | {
|
---|
2438 | fmt[1] = '0';
|
---|
2439 | fmt[2] = flen + '0';
|
---|
2440 | fmt[3] = 'X';
|
---|
2441 | fmt[4] = 0;
|
---|
2442 | }
|
---|
2443 | else
|
---|
2444 | {
|
---|
2445 | fmt[1] = 'c';
|
---|
2446 | fmt[2] = 0;
|
---|
2447 | }
|
---|
2448 | if(flen == CS_ECMSTORESIZE) { s += snprintf(result + s, size - s , "%s", ecmd5hex); }
|
---|
2449 | else if(flen == 5) { s += snprintf(result + s, size - s , "%s", csphash); }
|
---|
2450 | else if(flen == 17) { s += snprintf(result + s, size - s , "%s", cw); }
|
---|
2451 | else { s += snprintf(result + s, size - s, fmt, value); }
|
---|
2452 | }
|
---|
2453 | c++;
|
---|
2454 | }
|
---|
2455 | return s;
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | uint8_t checkCWpart(uchar *cw, int8_t part)
|
---|
2459 | {
|
---|
2460 | uint8_t eo = part ? 8 : 0;
|
---|
2461 | int8_t i;
|
---|
2462 | for(i = 0; i < 8; i++)
|
---|
2463 | if(cw[i + eo]) { return 1; }
|
---|
2464 | return 0;
|
---|
2465 | }
|
---|
2466 |
|
---|
2467 | int32_t format_ecm(ECM_REQUEST *ecm, char *result, size_t size)
|
---|
2468 | {
|
---|
2469 | char ecmd5hex[17 * 3];
|
---|
2470 | char csphash[5 * 3] = { 0 };
|
---|
2471 | char cwhex[17 * 3];
|
---|
2472 | cs_hexdump(0, ecm->ecmd5, 16, ecmd5hex, sizeof(ecmd5hex));
|
---|
2473 | #ifdef CS_CACHEEX
|
---|
2474 | cs_hexdump(0, (void *)&ecm->csp_hash, 4, csphash, sizeof(csphash));
|
---|
2475 | #endif
|
---|
2476 | cs_hexdump(0, ecm->cw, 16, cwhex, sizeof(cwhex));
|
---|
2477 | #ifdef MODULE_GBOX
|
---|
2478 | struct gbox_ecm_request_ext *ere = ecm->src_data;
|
---|
2479 | if(ere && check_client(ecm->client) && get_module(ecm->client)->num == R_GBOX && ere->gbox_hops)
|
---|
2480 | { return ecmfmt(ecm->caid, ecm->onid, ecm->prid, ecm->chid, ecm->pid, ecm->srvid, ecm->ecmlen, ecmd5hex, csphash, cwhex, result, size, ere->gbox_peer, ere->gbox_hops); }
|
---|
2481 | else
|
---|
2482 | #endif
|
---|
2483 | return ecmfmt(ecm->caid, ecm->onid, ecm->prid, ecm->chid, ecm->pid, ecm->srvid, ecm->ecmlen, ecmd5hex, csphash, cwhex, result, size, 0, 0);
|
---|
2484 | }
|
---|
2485 |
|
---|