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