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