1 | #include "globals.h"
|
---|
2 |
|
---|
3 | #if defined(MODULE_CCCAM) && defined(MODULE_CCCSHARE)
|
---|
4 |
|
---|
5 | #include "module-cccam.h"
|
---|
6 | #include "module-cccam-data.h"
|
---|
7 | #include "module-cccshare.h"
|
---|
8 | #include "oscam-chk.h"
|
---|
9 | #include "oscam-client.h"
|
---|
10 | #include "oscam-lock.h"
|
---|
11 | #include "oscam-string.h"
|
---|
12 | #include "oscam-time.h"
|
---|
13 | #include "oscam-work.h"
|
---|
14 |
|
---|
15 | extern uint32_t cfg_sidtab_generation;
|
---|
16 |
|
---|
17 | static uint32_t cc_share_id = 0x64;
|
---|
18 | static LLIST *reported_carddatas_list[CAID_KEY];
|
---|
19 | static CS_MUTEX_LOCK cc_shares_lock;
|
---|
20 |
|
---|
21 | static int32_t card_added_count = 0;
|
---|
22 | static int32_t card_removed_count = 0;
|
---|
23 | static int32_t card_dup_count = 0;
|
---|
24 | static pthread_t share_updater_thread = 0;
|
---|
25 | static bool share_updater_thread_active;
|
---|
26 |
|
---|
27 | int32_t card_valid_for_client(struct s_client *cl, struct cc_card *card);
|
---|
28 |
|
---|
29 | LLIST *get_cardlist(uint16_t caid, LLIST **list)
|
---|
30 | {
|
---|
31 | caid = (caid >> 8) % CAID_KEY;
|
---|
32 | if (!list[caid])
|
---|
33 | list[caid] = ll_create("card_list");
|
---|
34 | return list[caid];
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | LLIST **get_and_lock_sharelist(void)
|
---|
39 | {
|
---|
40 | cs_readlock(&cc_shares_lock);
|
---|
41 | return reported_carddatas_list;
|
---|
42 | }
|
---|
43 |
|
---|
44 | void unlock_sharelist(void)
|
---|
45 | {
|
---|
46 | cs_readunlock(&cc_shares_lock);
|
---|
47 | }
|
---|
48 |
|
---|
49 | void add_good_bad_sids(struct s_sidtab *ptr, SIDTABBITS sidtabno, struct cc_card *card) {
|
---|
50 | //good sids:
|
---|
51 | int32_t l;
|
---|
52 | for (l=0;l<ptr->num_srvid;l++) {
|
---|
53 | struct cc_srvid *srvid;
|
---|
54 | if (!cs_malloc(&srvid, sizeof(struct cc_srvid)))
|
---|
55 | return;
|
---|
56 | srvid->sid = ptr->srvid[l];
|
---|
57 | srvid->chid = 0;
|
---|
58 | srvid->ecmlen = 0; //0=undefined, also not used with "O" CCcam
|
---|
59 | if (!ll_contains_data(card->goodsids, srvid, sizeof(struct cc_srvid)))
|
---|
60 | ll_append(card->goodsids, srvid);
|
---|
61 | }
|
---|
62 |
|
---|
63 | //bad sids:
|
---|
64 | if (!sidtabno) return;
|
---|
65 |
|
---|
66 | struct s_sidtab *ptr_no;
|
---|
67 | int32_t n;
|
---|
68 |
|
---|
69 | for (n=0,ptr_no=cfg.sidtab; ptr_no; ptr_no=ptr_no->next,n++) {
|
---|
70 | if (sidtabno&((SIDTABBITS)1<<n)) {
|
---|
71 | int32_t m;
|
---|
72 | int32_t ok_caid = 0;
|
---|
73 | for (m=0;m<ptr_no->num_caid;m++) { //search bad sids for this caid:
|
---|
74 | if (ptr_no->caid[m] == card->caid) {
|
---|
75 | ok_caid = 1;
|
---|
76 | break;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | if (ok_caid) {
|
---|
80 | for (l=0;l<ptr_no->num_srvid;l++) {
|
---|
81 | struct cc_srvid *srvid;
|
---|
82 | if (!cs_malloc(&srvid,sizeof(struct cc_srvid)))
|
---|
83 | return;
|
---|
84 | srvid->sid = ptr_no->srvid[l];
|
---|
85 | srvid->chid = 0;
|
---|
86 | srvid->ecmlen = 0; //0=undefined, also not used with "O" CCcam
|
---|
87 | if (!ll_contains_data(card->badsids, srvid, sizeof(struct cc_srvid)))
|
---|
88 | ll_append(card->badsids, srvid);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | void add_good_bad_sids_by_rdr(struct s_reader *rdr, struct cc_card *card) {
|
---|
96 |
|
---|
97 | if (!rdr->sidtabs.ok) return;
|
---|
98 |
|
---|
99 | struct s_sidtab *ptr;
|
---|
100 | int32_t n,i;
|
---|
101 | for (n=0,ptr=cfg.sidtab; ptr; ptr=ptr->next,n++) {
|
---|
102 | if (rdr->sidtabs.ok&((SIDTABBITS)1<<n)) {
|
---|
103 | for (i=0; i<ptr->num_caid;i++) {
|
---|
104 | if (ptr->caid[i] == card->caid)
|
---|
105 | add_good_bad_sids(ptr, rdr->sidtabs.no, card);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | int32_t can_use_ext(struct cc_card *card) {
|
---|
113 | if (card->card_type == CT_REMOTECARD)
|
---|
114 | return card->is_ext;
|
---|
115 |
|
---|
116 | if (card->sidtab)
|
---|
117 | return (card->sidtab->num_srvid>0);
|
---|
118 | else
|
---|
119 | return ll_count(card->goodsids) && ll_count(card->badsids);
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | int32_t write_card(struct cc_data *cc, uint8_t *buf, struct cc_card *card, int32_t add_own, int32_t ext, int32_t au_allowed, struct s_client *cl) {
|
---|
124 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
125 | buf[0] = card->id >> 24;
|
---|
126 | buf[1] = card->id >> 16;
|
---|
127 | buf[2] = card->id >> 8;
|
---|
128 | buf[3] = card->id & 0xff;
|
---|
129 | buf[4] = card->remote_id >> 24;
|
---|
130 | buf[5] = card->remote_id >> 16;
|
---|
131 | buf[6] = card->remote_id >> 8;
|
---|
132 | buf[7] = card->remote_id & 0xFF;
|
---|
133 | buf[8] = card->caid >> 8;
|
---|
134 | buf[9] = card->caid & 0xff;
|
---|
135 | buf[10] = card->hop;
|
---|
136 | buf[11] = card->reshare;
|
---|
137 | if (au_allowed)
|
---|
138 | memcpy(buf + 12, card->hexserial, 8);
|
---|
139 |
|
---|
140 | //with cccam 2.2.0 we have assigned and rejected sids:
|
---|
141 | int32_t ofs = ext?23:21;
|
---|
142 |
|
---|
143 | //write providers:
|
---|
144 | LL_ITER it = ll_iter_create(card->providers);
|
---|
145 | struct cc_provider *prov;
|
---|
146 | while ((prov = ll_iter_next(&it))) {
|
---|
147 | uint32_t prid = prov->prov;
|
---|
148 | buf[ofs+0] = prid >> 16;
|
---|
149 | buf[ofs+1] = prid >> 8;
|
---|
150 | buf[ofs+2] = prid & 0xFF;
|
---|
151 | if (au_allowed)
|
---|
152 | memcpy(buf + ofs + 3, prov->sa, 4);
|
---|
153 | buf[20]++;
|
---|
154 | ofs+=7;
|
---|
155 | }
|
---|
156 |
|
---|
157 | //write sids only if cccam 2.2.x:
|
---|
158 | if (ext) {
|
---|
159 | if (card->sidtab) {
|
---|
160 | //good sids:
|
---|
161 | struct s_sidtab *ptr = card->sidtab;
|
---|
162 | int32_t l;
|
---|
163 | for (l=0;l<ptr->num_srvid;l++) {
|
---|
164 | buf[ofs+0] = ptr->srvid[l] >> 8;
|
---|
165 | buf[ofs+1] = ptr->srvid[l] & 0xFF;
|
---|
166 | ofs+=2;
|
---|
167 | buf[21]++; //nassign
|
---|
168 | if (buf[21] >= 240)
|
---|
169 | break;
|
---|
170 | }
|
---|
171 |
|
---|
172 | //bad sids:
|
---|
173 | int32_t n;
|
---|
174 | for (n=0,ptr=cfg.sidtab; ptr; ptr=ptr->next,n++) {
|
---|
175 | if (cl->sidtabs.no&((SIDTABBITS)1<<n) || card->sidtabno&((SIDTABBITS)1<<n)) {
|
---|
176 | int32_t m;
|
---|
177 | int32_t ok_caid = 0;
|
---|
178 | for (m=0;m<ptr->num_caid;m++) { //search bad sids for this caid:
|
---|
179 | if (ptr->caid[m] == card->caid) {
|
---|
180 | ok_caid = 1;
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | if (ok_caid) {
|
---|
185 | for (l=0;l<ptr->num_srvid;l++) {
|
---|
186 | buf[ofs+0] = ptr->srvid[l] >> 8;
|
---|
187 | buf[ofs+1] = ptr->srvid[l] & 0xFF;
|
---|
188 | ofs+=2;
|
---|
189 | buf[22]++; //nreject
|
---|
190 | if (buf[22] >= 240)
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | if (buf[22] >= 240)
|
---|
196 | break;
|
---|
197 | }
|
---|
198 |
|
---|
199 | } else {
|
---|
200 | //assigned sids:
|
---|
201 | it = ll_iter_create(card->goodsids);
|
---|
202 | struct cc_srvid *srvid;
|
---|
203 | while ((srvid = ll_iter_next(&it))) {
|
---|
204 | buf[ofs+0] = srvid->sid >> 8;
|
---|
205 | buf[ofs+1] = srvid->sid & 0xFF;
|
---|
206 | ofs+=2;
|
---|
207 | buf[21]++; //nassign
|
---|
208 | if (buf[21] >= 200)
|
---|
209 | break;
|
---|
210 | }
|
---|
211 |
|
---|
212 | //reject sids:
|
---|
213 | it = ll_iter_create(card->badsids);
|
---|
214 | while ((srvid = ll_iter_next(&it))) {
|
---|
215 | buf[ofs+0] = srvid->sid >> 8;
|
---|
216 | buf[ofs+1] = srvid->sid & 0xFF;
|
---|
217 | ofs+=2;
|
---|
218 | buf[22]++; //nreject
|
---|
219 | if (buf[22] >= 200)
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | //write remote nodes
|
---|
226 | int32_t nremote_ofs = ofs;
|
---|
227 | ofs++;
|
---|
228 | it = ll_iter_create(card->remote_nodes);
|
---|
229 | uint8_t *remote_node;
|
---|
230 | while ((remote_node = ll_iter_next(&it))) {
|
---|
231 | memcpy(buf+ofs, remote_node, 8);
|
---|
232 | ofs+=8;
|
---|
233 | buf[nremote_ofs]++;
|
---|
234 | }
|
---|
235 | if (add_own) {
|
---|
236 | memcpy(buf+ofs, cc->node_id, 8);
|
---|
237 | ofs+=8;
|
---|
238 | buf[nremote_ofs]++;
|
---|
239 | }
|
---|
240 | return ofs;
|
---|
241 | }
|
---|
242 |
|
---|
243 | static int32_t send_card_to_client(struct cc_card *card, struct s_client *cl) {
|
---|
244 | uint8_t buf[CC_MAXMSGSIZE];
|
---|
245 |
|
---|
246 | if (!card_valid_for_client(cl, card))
|
---|
247 | return 0;
|
---|
248 |
|
---|
249 | int8_t usr_reshare = cl->account->cccreshare;
|
---|
250 | if (usr_reshare == -1)
|
---|
251 | usr_reshare = cfg.cc_reshare;
|
---|
252 |
|
---|
253 | int8_t ignorereshare = cl->account->cccignorereshare;
|
---|
254 | if (ignorereshare == -1)
|
---|
255 | ignorereshare = cfg.cc_ignore_reshare;
|
---|
256 |
|
---|
257 | int8_t reader_reshare = card->origin_reader ? card->rdr_reshare : usr_reshare;
|
---|
258 | if (reader_reshare == -1)
|
---|
259 | reader_reshare = cfg.cc_reshare;
|
---|
260 |
|
---|
261 | int8_t reshare = (reader_reshare < usr_reshare) ? reader_reshare : usr_reshare;
|
---|
262 | int8_t new_reshare;
|
---|
263 | if (card->card_type == CT_CARD_BY_SERVICE_USER) {
|
---|
264 | new_reshare = usr_reshare;
|
---|
265 | } else if (ignorereshare) {
|
---|
266 | new_reshare = reshare;
|
---|
267 | } else {
|
---|
268 | new_reshare = card->reshare;
|
---|
269 | if (card->card_type == CT_REMOTECARD)
|
---|
270 | new_reshare--;
|
---|
271 | if (new_reshare > reshare)
|
---|
272 | new_reshare = reshare;
|
---|
273 | }
|
---|
274 | if (new_reshare < 0)
|
---|
275 | return 0;
|
---|
276 |
|
---|
277 | if (!card->id)
|
---|
278 | card->id = cc_share_id++;
|
---|
279 |
|
---|
280 | struct cc_data *cc = cl->cc;
|
---|
281 | int32_t is_ext = cc->cccam220 && can_use_ext(card);
|
---|
282 | int32_t len = write_card(cc, buf, card, 1, is_ext, ll_count(cl->aureader_list), cl);
|
---|
283 | //buf[10] = card->hop-1;
|
---|
284 | buf[11] = new_reshare;
|
---|
285 |
|
---|
286 | struct s_clientmsg *clientmsg;
|
---|
287 | if (cs_malloc(&clientmsg, sizeof(struct s_clientmsg))){
|
---|
288 | memcpy(clientmsg->msg, buf, len);
|
---|
289 | clientmsg->len = len;
|
---|
290 | clientmsg->cmd = is_ext?MSG_NEW_CARD_SIDINFO:MSG_NEW_CARD;
|
---|
291 | add_job(cl, ACTION_CLIENT_SEND_MSG, clientmsg, sizeof(struct s_clientmsg));
|
---|
292 | }
|
---|
293 | return 1;
|
---|
294 | }
|
---|
295 |
|
---|
296 | int32_t send_card_to_all_clients(struct cc_card *card) {
|
---|
297 | int32_t count = 0;
|
---|
298 | struct s_client *cl;
|
---|
299 | cs_readlock(&clientlist_lock);
|
---|
300 | for (cl = first_client; cl; cl = cl->next) {
|
---|
301 | if (cl->cc && cl->typ=='c' && !cl->kill && get_module(cl)->num == R_CCCAM) { //CCCam-Client!
|
---|
302 | count += send_card_to_client(card, cl);
|
---|
303 | }
|
---|
304 | }
|
---|
305 | cs_readunlock(&clientlist_lock);
|
---|
306 | return count;
|
---|
307 | }
|
---|
308 |
|
---|
309 | void send_remove_card_to_clients(struct cc_card *card) {
|
---|
310 | if (!card || !card->id)
|
---|
311 | return;
|
---|
312 |
|
---|
313 | uint8_t buf[4];
|
---|
314 | buf[0] = card->id >> 24;
|
---|
315 | buf[1] = card->id >> 16;
|
---|
316 | buf[2] = card->id >> 8;
|
---|
317 | buf[3] = card->id & 0xFF;
|
---|
318 |
|
---|
319 | struct s_client *cl;
|
---|
320 | struct s_clientmsg *clientmsg;
|
---|
321 | cs_readlock(&clientlist_lock);
|
---|
322 | for (cl = first_client; cl; cl=cl->next) {
|
---|
323 | struct cc_data *cc = cl->cc;
|
---|
324 | if (cc && cl->typ=='c' && !cl->kill && get_module(cl)->num == R_CCCAM) { //CCCam-Client!
|
---|
325 | if (card_valid_for_client(cl, card)) {
|
---|
326 | if(cs_malloc(&clientmsg, sizeof(struct s_clientmsg))){
|
---|
327 | memcpy(clientmsg->msg, buf, sizeof(buf));
|
---|
328 | clientmsg->len = sizeof(buf);
|
---|
329 | clientmsg->cmd = MSG_CARD_REMOVED;
|
---|
330 | add_job(cl, ACTION_CLIENT_SEND_MSG, clientmsg, sizeof(struct s_clientmsg));
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 | cs_readunlock(&clientlist_lock);
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * if idents defined on an cccam reader, the cards caid+provider are checked.
|
---|
341 | * return 1 a) if no ident defined b) card is in identlist
|
---|
342 | * 0 if card is not in identlist
|
---|
343 | *
|
---|
344 | * a card is in the identlist, if the cards caid is matching and mininum a provider is matching
|
---|
345 | **/
|
---|
346 | int32_t chk_ident(FTAB *ftab, struct cc_card *card) {
|
---|
347 |
|
---|
348 | int32_t j, k;
|
---|
349 | int32_t res = 1;
|
---|
350 |
|
---|
351 | if (ftab && ftab->filts) {
|
---|
352 | for (j = 0; j < ftab->nfilts; j++) {
|
---|
353 | if (ftab->filts[j].caid) {
|
---|
354 | res = 0;
|
---|
355 | if (ftab->filts[j].caid==card->caid) { //caid matches!
|
---|
356 |
|
---|
357 | int32_t nprids = ftab->filts[j].nprids;
|
---|
358 | if (!nprids) // No Provider ->Ok
|
---|
359 | return 1;
|
---|
360 |
|
---|
361 |
|
---|
362 | LL_ITER it = ll_iter_create(card->providers);
|
---|
363 | struct cc_provider *prov;
|
---|
364 |
|
---|
365 | while ((prov = ll_iter_next(&it))) {
|
---|
366 | for (k = 0; k < nprids; k++) {
|
---|
367 | uint32_t prid = ftab->filts[j].prids[k];
|
---|
368 | if (prid == prov->prov) { //Provider matches
|
---|
369 | return 1;
|
---|
370 | }
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|
377 | return res;
|
---|
378 | }
|
---|
379 |
|
---|
380 | int32_t cc_clear_reported_carddata(LLIST *reported_carddatas, LLIST *except,
|
---|
381 | int32_t send_removed) {
|
---|
382 | int32_t i=0;
|
---|
383 | LL_ITER it = ll_iter_create(reported_carddatas);
|
---|
384 | struct cc_card *card;
|
---|
385 | while ((card = ll_iter_next(&it))) {
|
---|
386 | struct cc_card *card2 = NULL;
|
---|
387 | if (except) {
|
---|
388 | LL_ITER it2 = ll_iter_create(except);
|
---|
389 | while ((card2 = ll_iter_next(&it2))) {
|
---|
390 | if (card == card2)
|
---|
391 | break;
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | if (!card2 && ll_iter_remove(&it)) { //check result of ll_iter_remove, because another thread could removed it
|
---|
396 | if (send_removed) {
|
---|
397 | cs_debug_mask(D_TRACE, "s-card removed: id %8X remoteid %8X caid %4X hop %d reshare %d originid %8X cardtype %d",
|
---|
398 | card->id, card->remote_id, card->caid, card->hop, card->reshare, card->origin_id, card->card_type);
|
---|
399 |
|
---|
400 | send_remove_card_to_clients(card);
|
---|
401 | }
|
---|
402 | cc_free_card(card);
|
---|
403 | i++;
|
---|
404 | }
|
---|
405 | }
|
---|
406 | return i;
|
---|
407 | }
|
---|
408 |
|
---|
409 | int32_t cc_free_reported_carddata(LLIST *reported_carddatas, LLIST *except,
|
---|
410 | int32_t send_removed) {
|
---|
411 | int32_t i=0;
|
---|
412 | if (reported_carddatas) {
|
---|
413 | i = cc_clear_reported_carddata(reported_carddatas, except, send_removed);
|
---|
414 | ll_destroy_NULL(reported_carddatas);
|
---|
415 | }
|
---|
416 | return i;
|
---|
417 | }
|
---|
418 |
|
---|
419 | int32_t card_valid_for_client(struct s_client *cl, struct cc_card *card) {
|
---|
420 |
|
---|
421 | //Check group:
|
---|
422 | if (card->grp && !(card->grp & cl->grp))
|
---|
423 | return 0;
|
---|
424 |
|
---|
425 | if (card->aufilter) { //aufilter (0=any, 1=au clients only, 2=nonau clients only)
|
---|
426 | int8_t cl_au = ll_count(cl->aureader_list) > 0; //client has au allowed
|
---|
427 | int8_t card_au = card->aufilter == 1;
|
---|
428 | if (card_au != cl_au)
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 |
|
---|
432 | //Check idents:
|
---|
433 | if (!chk_ident(&cl->ftab, card))
|
---|
434 | return 0;
|
---|
435 |
|
---|
436 | //Check caids:
|
---|
437 | if (!chk_ctab(card->caid, &cl->ctab))
|
---|
438 | return 0;
|
---|
439 |
|
---|
440 | //Check reshare
|
---|
441 | if (card->card_type == CT_REMOTECARD) {
|
---|
442 | int8_t ignorereshare = cl->account->cccignorereshare;
|
---|
443 | if (ignorereshare == -1) ignorereshare = cfg.cc_ignore_reshare;
|
---|
444 | if (!ignorereshare && !card->reshare)
|
---|
445 | return 0;
|
---|
446 | }
|
---|
447 |
|
---|
448 | //Check account maxhops:
|
---|
449 | if (cl->account->cccmaxhops < card->hop)
|
---|
450 | return 0;
|
---|
451 |
|
---|
452 | //Check remote node id, if card is from there, ignore it!
|
---|
453 | LL_ITER it = ll_iter_create(card->remote_nodes);
|
---|
454 | uint8_t * node;
|
---|
455 | struct cc_data *cc = cl->cc;
|
---|
456 | while ((node=ll_iter_next(&it))) {
|
---|
457 | if (!memcmp(node, cc->peer_node_id, 8)) {
|
---|
458 | return 0;
|
---|
459 | }
|
---|
460 | }
|
---|
461 |
|
---|
462 | //Check Services:
|
---|
463 | if (ll_count(card->providers)) {
|
---|
464 | it = ll_iter_create(card->providers);
|
---|
465 | struct cc_provider *prov;
|
---|
466 | int8_t found=0;
|
---|
467 | while ((prov = ll_iter_next(&it))) {
|
---|
468 | uint32_t prid = prov->prov;
|
---|
469 | if (chk_srvid_by_caid_prov(cl, card->caid, prid)) {
|
---|
470 | found = 1;
|
---|
471 | break;
|
---|
472 | }
|
---|
473 | }
|
---|
474 | if (!found) return 0;
|
---|
475 | }
|
---|
476 | else {
|
---|
477 | if (!chk_srvid_by_caid_prov(cl, card->caid, 0))
|
---|
478 | return 0;
|
---|
479 | }
|
---|
480 |
|
---|
481 | //Check Card created by Service:
|
---|
482 | if (card->sidtab) {
|
---|
483 | struct s_sidtab *ptr;
|
---|
484 | int32_t j;
|
---|
485 | int32_t ok = !cl->sidtabs.ok && !cl->sidtabs.no; //default valid if no positive services and no negative services
|
---|
486 | if (!ok) {
|
---|
487 | if (!cl->sidtabs.ok) // no positive services, so ok by default if no negative found
|
---|
488 | ok=1;
|
---|
489 |
|
---|
490 | for (j=0,ptr=cfg.sidtab; ptr; ptr=ptr->next,j++) {
|
---|
491 | if (ptr == card->sidtab) {
|
---|
492 | if (cl->sidtabs.no&((SIDTABBITS)1<<j))
|
---|
493 | return 0;
|
---|
494 | if (cl->sidtabs.ok&((SIDTABBITS)1<<j))
|
---|
495 | ok = 1;
|
---|
496 | break;
|
---|
497 | }
|
---|
498 | }
|
---|
499 | }
|
---|
500 | if (!ok)
|
---|
501 | return 0;
|
---|
502 | }
|
---|
503 |
|
---|
504 | return 1;
|
---|
505 | }
|
---|
506 |
|
---|
507 | uint32_t get_reader_prid(struct s_reader *rdr, int32_t j) {
|
---|
508 | return b2i(3, &rdr->prid[j][1]);
|
---|
509 | }
|
---|
510 | //uint32_t get_reader_prid(struct s_reader *rdr, int32_t j) {
|
---|
511 | // uint32_t prid;
|
---|
512 | // if (!is_cascading_reader(rdr)) { // Real cardreaders have 4-byte Providers
|
---|
513 | // prid = b2i(4, &rdr->prid[j][0]);
|
---|
514 | // //prid = (rdr->prid[j][0] << 24) | (rdr->prid[j][1] << 16)
|
---|
515 | // // | (rdr->prid[j][2] << 8) | (rdr->prid[j][3] & 0xFF);
|
---|
516 | // } else { // Cascading/Network-reader 3-bytes Providers
|
---|
517 | // prid = b2i(3, &rdr->prid[j][0]);
|
---|
518 | // //prid = (rdr->prid[j][0] << 16) | (rdr->prid[j][1] << 8)
|
---|
519 | // // | (rdr->prid[j][2] & 0xFF);
|
---|
520 | //
|
---|
521 | // }
|
---|
522 | // return prid;
|
---|
523 | //}
|
---|
524 |
|
---|
525 | void copy_sids(LLIST *dst, LLIST *src) {
|
---|
526 | LL_ITER it_src = ll_iter_create(src);
|
---|
527 | LL_ITER it_dst = ll_iter_create(dst);
|
---|
528 | struct cc_srvid *srvid_src;
|
---|
529 | struct cc_srvid *srvid_dst;
|
---|
530 | while ((srvid_src=ll_iter_next(&it_src))) {
|
---|
531 | ll_iter_reset(&it_dst);
|
---|
532 | while ((srvid_dst=ll_iter_next(&it_dst))) {
|
---|
533 | if (sid_eq(srvid_src, srvid_dst))
|
---|
534 | break;
|
---|
535 | }
|
---|
536 | if (!srvid_dst) {
|
---|
537 | if (!cs_malloc(&srvid_dst, sizeof(struct cc_srvid)))
|
---|
538 | break;
|
---|
539 | memcpy(srvid_dst, srvid_src, sizeof(struct cc_srvid));
|
---|
540 | ll_iter_insert(&it_dst, srvid_dst);
|
---|
541 | }
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | int32_t add_card_providers(struct cc_card *dest_card, struct cc_card *card,
|
---|
547 | int32_t copy_remote_nodes) {
|
---|
548 | int32_t modified = 0;
|
---|
549 |
|
---|
550 | //1. Copy nonexisting providers, ignore double:
|
---|
551 | struct cc_provider *prov_info;
|
---|
552 | LL_ITER it_src = ll_iter_create(card->providers);
|
---|
553 | LL_ITER it_dst = ll_iter_create(dest_card->providers);
|
---|
554 |
|
---|
555 | struct cc_provider *provider;
|
---|
556 | while ((provider = ll_iter_next(&it_src))) {
|
---|
557 | ll_iter_reset(&it_dst);
|
---|
558 | while ((prov_info = ll_iter_next(&it_dst))) {
|
---|
559 | if (prov_info->prov == provider->prov)
|
---|
560 | break;
|
---|
561 | }
|
---|
562 | if (!prov_info) {
|
---|
563 | struct cc_provider *prov_new;
|
---|
564 | if (!cs_malloc(&prov_new, sizeof(struct cc_provider)))
|
---|
565 | break;
|
---|
566 | memcpy(prov_new, provider, sizeof(struct cc_provider));
|
---|
567 | ll_iter_insert(&it_dst, prov_new);
|
---|
568 | modified = 1;
|
---|
569 | }
|
---|
570 | }
|
---|
571 |
|
---|
572 | if (copy_remote_nodes) {
|
---|
573 | //2. Copy nonexisting remote_nodes, ignoring existing:
|
---|
574 | it_src = ll_iter_create(card->remote_nodes);
|
---|
575 | it_dst = ll_iter_create(dest_card->remote_nodes);
|
---|
576 | uint8_t *remote_node;
|
---|
577 | uint8_t *remote_node2;
|
---|
578 | while ((remote_node = ll_iter_next(&it_src))) {
|
---|
579 | ll_iter_reset(&it_dst);
|
---|
580 | while ((remote_node2 = ll_iter_next(&it_dst))) {
|
---|
581 | if (memcmp(remote_node, remote_node2, 8) == 0)
|
---|
582 | break;
|
---|
583 | }
|
---|
584 | if (!remote_node2) {
|
---|
585 | uint8_t* remote_node_new;
|
---|
586 | if (!cs_malloc(&remote_node_new, 8))
|
---|
587 | break;
|
---|
588 | memcpy(remote_node_new, remote_node, 8);
|
---|
589 | ll_iter_insert(&it_dst, remote_node_new);
|
---|
590 | modified = 1;
|
---|
591 | }
|
---|
592 | }
|
---|
593 | }
|
---|
594 | return modified;
|
---|
595 | }
|
---|
596 |
|
---|
597 | #define TIMEOUT_SECONDS 3600
|
---|
598 |
|
---|
599 | void set_card_timeout(struct cc_card *card)
|
---|
600 | {
|
---|
601 | card->timeout = time(NULL)+TIMEOUT_SECONDS+((rand() & 0xff)-128)*2;
|
---|
602 | }
|
---|
603 |
|
---|
604 | struct cc_card *create_card(struct cc_card *card) {
|
---|
605 | struct cc_card *card2;
|
---|
606 | if (!cs_malloc(&card2, sizeof(struct cc_card)))
|
---|
607 | return NULL;
|
---|
608 | if (card)
|
---|
609 | memcpy(card2, card, sizeof(struct cc_card));
|
---|
610 | else
|
---|
611 | memset(card2, 0, sizeof(struct cc_card));
|
---|
612 | card2->providers = ll_create("providers");
|
---|
613 | card2->badsids = ll_create("badsids");
|
---|
614 | card2->goodsids = ll_create("goodsids");
|
---|
615 | card2->remote_nodes = ll_create("remote_nodes");
|
---|
616 |
|
---|
617 | if (card) {
|
---|
618 | copy_sids(card2->goodsids, card->goodsids);
|
---|
619 | copy_sids(card2->badsids, card->badsids);
|
---|
620 | card2->id = 0;
|
---|
621 | }
|
---|
622 | else
|
---|
623 | set_card_timeout(card2);
|
---|
624 |
|
---|
625 | return card2;
|
---|
626 | }
|
---|
627 |
|
---|
628 | struct cc_card *create_card2(struct s_reader *rdr, int32_t j, uint16_t caid, uint8_t reshare) {
|
---|
629 |
|
---|
630 | struct cc_card *card = create_card(NULL);
|
---|
631 | if (!card)
|
---|
632 | return NULL;
|
---|
633 | card->remote_id = (rdr?(rdr->cc_id << 16):0x7F7F8000)|j;
|
---|
634 | card->caid = caid;
|
---|
635 | card->reshare = reshare;
|
---|
636 | card->origin_reader = rdr;
|
---|
637 | if (rdr) {
|
---|
638 | card->grp = rdr->grp;
|
---|
639 | card->rdr_reshare = rdr->cc_reshare > -1 ? rdr->cc_reshare : cfg.cc_reshare; //copy reshare because reader could go offline
|
---|
640 | card->sidtabno = rdr->sidtabs.no;
|
---|
641 | card->hop = rdr->cc_hop;
|
---|
642 | }
|
---|
643 | else card->rdr_reshare = reshare;
|
---|
644 | return card;
|
---|
645 | }
|
---|
646 |
|
---|
647 | /**
|
---|
648 | * num_same_providers checks if card1 has exactly the same providers as card2
|
---|
649 | * returns same provider count
|
---|
650 | **/
|
---|
651 | int32_t num_same_providers(struct cc_card *card1, struct cc_card *card2) {
|
---|
652 |
|
---|
653 | int32_t found=0;
|
---|
654 |
|
---|
655 | LL_ITER it1 = ll_iter_create(card1->providers);
|
---|
656 | LL_ITER it2 = ll_iter_create(card2->providers);
|
---|
657 |
|
---|
658 | struct cc_provider *prov1, *prov2;
|
---|
659 |
|
---|
660 | while ((prov1=ll_iter_next(&it1))) {
|
---|
661 |
|
---|
662 | ll_iter_reset(&it2);
|
---|
663 | while ((prov2=ll_iter_next(&it2))) {
|
---|
664 | if (prov1->prov==prov2->prov) {
|
---|
665 | found++;
|
---|
666 | break;
|
---|
667 | }
|
---|
668 |
|
---|
669 | }
|
---|
670 | }
|
---|
671 | return found;
|
---|
672 | }
|
---|
673 |
|
---|
674 | /**
|
---|
675 | * equal_providers checks if card1 has exactly the same providers as card2
|
---|
676 | * returns 1=equal 0=different
|
---|
677 | **/
|
---|
678 | int32_t equal_providers(struct cc_card *card1, struct cc_card *card2) {
|
---|
679 |
|
---|
680 | if (ll_count(card1->providers) != ll_count(card2->providers))
|
---|
681 | return 0;
|
---|
682 | if (ll_count(card1->providers) == 0)
|
---|
683 | return 1;
|
---|
684 |
|
---|
685 | LL_ITER it1 = ll_iter_create(card1->providers);
|
---|
686 | LL_ITER it2 = ll_iter_create(card2->providers);
|
---|
687 |
|
---|
688 | struct cc_provider *prov1, *prov2;
|
---|
689 |
|
---|
690 | while ((prov1=ll_iter_next(&it1))) {
|
---|
691 |
|
---|
692 | ll_iter_reset(&it2);
|
---|
693 | while ((prov2=ll_iter_next(&it2))) {
|
---|
694 | if (prov1->prov==prov2->prov) {
|
---|
695 | break;
|
---|
696 | }
|
---|
697 |
|
---|
698 | }
|
---|
699 | if (!prov2) break;
|
---|
700 | }
|
---|
701 | return (prov1 == NULL);
|
---|
702 | }
|
---|
703 |
|
---|
704 | int32_t is_au_card(struct cc_card *card) {
|
---|
705 | if (card && card->origin_reader)
|
---|
706 | return !card->origin_reader->audisabled && cc_UA_valid(card->hexserial);
|
---|
707 | return 0;
|
---|
708 | }
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * Adds a new card to a cardlist.
|
---|
712 | */
|
---|
713 | int32_t add_card_to_serverlist(LLIST *cardlist, struct cc_card *card, int8_t free_card) {
|
---|
714 |
|
---|
715 | int32_t modified = 0;
|
---|
716 | if (!card)
|
---|
717 | return modified;
|
---|
718 |
|
---|
719 | if (card) {
|
---|
720 | if (!card->aufilter && is_au_card(card)) {
|
---|
721 |
|
---|
722 | //card keeps their hexserial, set aufilter (0=any, 1=au clients only, 2=nonau clients only)
|
---|
723 | card->aufilter = 1;
|
---|
724 | struct cc_card *card3 = create_card(card);
|
---|
725 |
|
---|
726 | add_card_providers(card3, card, 1); //copy providers to new card. Copy remote nodes to new card
|
---|
727 |
|
---|
728 | //create a copy of the card, set aufilter to 2 and remove hexserial:
|
---|
729 | card3->aufilter = 2;
|
---|
730 | memset(card3->hexserial, 0, sizeof(card3->hexserial));
|
---|
731 | modified = add_card_to_serverlist(cardlist, card3, 1);
|
---|
732 | }
|
---|
733 | }
|
---|
734 | LL_ITER it = ll_iter_create(cardlist);
|
---|
735 | struct cc_card *card2;
|
---|
736 |
|
---|
737 | //Minimize all, transmit just CAID, merge providers:
|
---|
738 | if (cfg.cc_minimize_cards == MINIMIZE_CAID && !cfg.cc_forward_origin_card) {
|
---|
739 | while ((card2 = ll_iter_next(&it))) {
|
---|
740 | //compare caid, hexserial, cardtype and sidtab (if any):
|
---|
741 | if (same_card2(card, card2, 0)) {
|
---|
742 | //Merge cards only if resulting providercount is smaller than CS_MAXPROV
|
---|
743 | int32_t nsame, ndiff, nnew;
|
---|
744 |
|
---|
745 | nsame = num_same_providers(card, card2); //count same cards
|
---|
746 | ndiff = ll_count(card->providers)-nsame; //cound different cards, this cound will be added
|
---|
747 | nnew = ndiff + ll_count(card2->providers); //new card count after add. because its limited to CS_MAXPROV, dont add it
|
---|
748 |
|
---|
749 | if (nnew <= CS_MAXPROV)
|
---|
750 | break;
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | if (!card2) { //Not found->add it:
|
---|
755 | if (free_card) { //Use this card
|
---|
756 | free_card = 0;
|
---|
757 | ll_iter_insert(&it, card);
|
---|
758 | } else {
|
---|
759 | card2 = create_card(card); //Copy card
|
---|
760 | if (!card2)
|
---|
761 | return modified;
|
---|
762 | card2->hop = 0;
|
---|
763 | ll_iter_insert(&it, card2);
|
---|
764 | add_card_providers(card2, card, 1); //copy providers to new card. Copy remote nodes to new card
|
---|
765 | }
|
---|
766 | modified = 1;
|
---|
767 |
|
---|
768 | } else { //found, merge providers:
|
---|
769 | card_dup_count++;
|
---|
770 | card2->grp |=card->grp; //add group to the card
|
---|
771 | add_card_providers(card2, card, 0); //merge all providers
|
---|
772 | ll_clear_data(card2->remote_nodes); //clear remote nodes
|
---|
773 | if (!card2->sidtab)
|
---|
774 | ll_clear_data(card2->badsids);
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | //Removed duplicate cards, keeping card with lower hop:
|
---|
779 | else if (cfg.cc_minimize_cards == MINIMIZE_HOPS && !cfg.cc_forward_origin_card) {
|
---|
780 | while ((card2 = ll_iter_next(&it))) {
|
---|
781 | //compare caid, hexserial, cardtype, sidtab (if any), providers:
|
---|
782 | if (same_card2(card, card2, 0) && equal_providers(card, card2)) {
|
---|
783 | break;
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | if (card2 && card2->hop > card->hop) { //hop is smaller, drop old card
|
---|
788 | ll_iter_remove(&it);
|
---|
789 | cc_free_card(card2);
|
---|
790 | card2 = NULL;
|
---|
791 | card_dup_count++;
|
---|
792 | }
|
---|
793 |
|
---|
794 | if (!card2) { //Not found->add it:
|
---|
795 | if (free_card) { //use this card
|
---|
796 | free_card = 0;
|
---|
797 | ll_iter_insert(&it, card);
|
---|
798 | } else {
|
---|
799 | card2 = create_card(card); //copy card
|
---|
800 | if (!card2)
|
---|
801 | return modified;
|
---|
802 | ll_iter_insert(&it, card2);
|
---|
803 | add_card_providers(card2, card, 1); //copy providers to new card. Copy remote nodes to new card
|
---|
804 | }
|
---|
805 | modified = 1;
|
---|
806 | } else { //found, merge cards (providers are same!)
|
---|
807 | card_dup_count++;
|
---|
808 | card2->grp |=card->grp; //add group to the card
|
---|
809 | add_card_providers(card2, card, 0);
|
---|
810 | if (!card2->sidtab)
|
---|
811 | ll_clear_data(card2->badsids);
|
---|
812 | }
|
---|
813 |
|
---|
814 | }
|
---|
815 | //like cccam:
|
---|
816 | else { //just remove duplicate cards (same ids)
|
---|
817 | while ((card2 = ll_iter_next(&it))) {
|
---|
818 | //compare remote_id, first_node, caid, hexserial, cardtype, sidtab (if any), providers:
|
---|
819 | if (same_card(card, card2))
|
---|
820 | break;
|
---|
821 | }
|
---|
822 |
|
---|
823 | if (card2 && card2->hop > card->hop) { //same card, if hop greater drop card
|
---|
824 | ll_iter_remove(&it);
|
---|
825 | cc_free_card(card2);
|
---|
826 | card2 = NULL;
|
---|
827 | card_dup_count++;
|
---|
828 | }
|
---|
829 | if (!card2) { //Not found, add it:
|
---|
830 | if (free_card) {
|
---|
831 | free_card = 0;
|
---|
832 | ll_iter_insert(&it, card);
|
---|
833 | } else {
|
---|
834 | card2 = create_card(card);
|
---|
835 | if (!card2)
|
---|
836 | return modified;
|
---|
837 | ll_iter_insert(&it, card2);
|
---|
838 | add_card_providers(card2, card, 1);
|
---|
839 | }
|
---|
840 | modified = 1;
|
---|
841 | } else { //Found, everything is same (including providers)
|
---|
842 | card_dup_count++;
|
---|
843 | }
|
---|
844 | }
|
---|
845 |
|
---|
846 | if (free_card)
|
---|
847 | cc_free_card(card);
|
---|
848 |
|
---|
849 | return modified;
|
---|
850 | }
|
---|
851 |
|
---|
852 | /**
|
---|
853 | * returns true if timeout-time is reached
|
---|
854 | * only local cards needs to be renewed after 1h. "O" CCCam throws away cards older than 1,2h
|
---|
855 | **/
|
---|
856 | int32_t card_timed_out(struct cc_card *card)
|
---|
857 | {
|
---|
858 | //!=CT_REMOTECARD = LOCALCARD (or virtual cards by caid/ident/service)
|
---|
859 | //timeout is set in future, so if current time is bigger, timeout is reached
|
---|
860 | int32_t res = (card->card_type != CT_REMOTECARD) && (card->timeout < time(NULL)); //local card is older than 1h?
|
---|
861 | if (res)
|
---|
862 | cs_debug_mask(D_TRACE, "card %08X timed out! refresh forced", card->id?card->id:card->origin_id);
|
---|
863 | return res;
|
---|
864 | }
|
---|
865 |
|
---|
866 | /**
|
---|
867 | * returns true if card1 is already reported.
|
---|
868 | * "reported" means, we already have this card1 in our sharelist.
|
---|
869 | * if the card1 is already reported, we throw it away, because we build a new sharelist
|
---|
870 | * so after finding all reported cards, we have a list of reported cards, which aren't used anymore
|
---|
871 | **/
|
---|
872 | int32_t find_reported_card(struct cc_card *card1)
|
---|
873 | {
|
---|
874 | LL_ITER it = ll_iter_create(get_cardlist(card1->caid, reported_carddatas_list));
|
---|
875 | struct cc_card *card2;
|
---|
876 | while ((card2 = ll_iter_next(&it))) {
|
---|
877 | if (same_card(card1, card2) && !card_timed_out(card2)) {
|
---|
878 | card1->id = card2->id; //Set old id !!
|
---|
879 | card1->timeout = card2->timeout;
|
---|
880 | cc_free_card(card2);
|
---|
881 | ll_iter_remove(&it);
|
---|
882 | return 1; //Old card and new card are equal!
|
---|
883 | }
|
---|
884 | }
|
---|
885 | return 0; //Card not found
|
---|
886 | }
|
---|
887 |
|
---|
888 | /**
|
---|
889 | * Server:
|
---|
890 | * Adds a cccam-carddata buffer to the list of reported carddatas
|
---|
891 | */
|
---|
892 | void cc_add_reported_carddata(LLIST *reported_carddatas, struct cc_card *card) {
|
---|
893 | ll_append(reported_carddatas, card);
|
---|
894 | }
|
---|
895 |
|
---|
896 | /**
|
---|
897 | * adds the card to the list of the new reported carddatas - this is the new sharelist
|
---|
898 | * if this card is not already reported, we send them to the clients
|
---|
899 | * if this card is already reported, find_reported_card throws the "origin" card away
|
---|
900 | * so the "old" sharelist is reduced
|
---|
901 | **/
|
---|
902 | void report_card(struct cc_card *card, LLIST *new_reported_carddatas, LLIST *new_cards)
|
---|
903 | {
|
---|
904 | if (!find_reported_card(card)) { //Add new card:
|
---|
905 |
|
---|
906 | cs_debug_mask(D_TRACE, "s-card added: id %8X remoteid %8X caid %4X hop %d reshare %d originid %8X cardtype %d",
|
---|
907 | card->id, card->remote_id, card->caid, card->hop, card->reshare, card->origin_id, card->card_type);
|
---|
908 |
|
---|
909 | ll_append(new_cards, card);
|
---|
910 |
|
---|
911 | card_added_count++;
|
---|
912 | }
|
---|
913 | cc_add_reported_carddata(new_reported_carddatas, card);
|
---|
914 | }
|
---|
915 |
|
---|
916 |
|
---|
917 | /**
|
---|
918 | * Server:
|
---|
919 | * Reports all caid/providers to the connected clients
|
---|
920 | * returns 1=ok, 0=error
|
---|
921 | * cfg.cc_reshare_services =0 CCCAM reader reshares only received cards + defined reader services
|
---|
922 | * =1 CCCAM reader reshares received cards + defined services
|
---|
923 | * =2 CCCAM reader reshares only defined reader-services as virtual cards
|
---|
924 | * =3 CCCAM reader reshares only defined user-services as virtual cards
|
---|
925 | * =4 CCCAM reader reshares only received cards
|
---|
926 | */
|
---|
927 | void update_card_list(void) {
|
---|
928 | int32_t i, j, k, l, flt, card_count = 0;
|
---|
929 |
|
---|
930 | LLIST *server_cards[CAID_KEY];
|
---|
931 | LLIST *new_reported_carddatas[CAID_KEY];
|
---|
932 |
|
---|
933 | LL_ITER it, it2;
|
---|
934 | struct cc_card *card;
|
---|
935 |
|
---|
936 | memset(server_cards, 0, sizeof(server_cards));
|
---|
937 | memset(new_reported_carddatas, 0, sizeof(new_reported_carddatas));
|
---|
938 |
|
---|
939 | card_added_count = 0;
|
---|
940 | card_removed_count = 0;
|
---|
941 | card_dup_count = 0;
|
---|
942 |
|
---|
943 | //User-Services:
|
---|
944 | if (cfg.cc_reshare_services==3 && cfg.sidtab) {
|
---|
945 | struct s_sidtab *ptr;
|
---|
946 | for (j=0,ptr=cfg.sidtab; ptr; ptr=ptr->next,j++) {
|
---|
947 | for (k=0;k<ptr->num_caid;k++) {
|
---|
948 | card = create_card2(NULL, (j<<8)|k, ptr->caid[k], cfg.cc_reshare);
|
---|
949 | if (!card)
|
---|
950 | return;
|
---|
951 | card->card_type = CT_CARD_BY_SERVICE_USER;
|
---|
952 | card->sidtab = ptr;
|
---|
953 | for (l=0;l<ptr->num_provid;l++) {
|
---|
954 | struct cc_provider *prov;
|
---|
955 | if (!cs_malloc(&prov, sizeof(struct cc_provider)))
|
---|
956 | return;
|
---|
957 | prov->prov = ptr->provid[l];
|
---|
958 | ll_append(card->providers, prov);
|
---|
959 | }
|
---|
960 |
|
---|
961 | add_card_to_serverlist(get_cardlist(card->caid, server_cards), card, 1);
|
---|
962 | }
|
---|
963 | flt=1;
|
---|
964 | }
|
---|
965 | }
|
---|
966 | else
|
---|
967 | {
|
---|
968 | struct s_reader *rdr;
|
---|
969 | int32_t r = 0;
|
---|
970 |
|
---|
971 | cs_readlock(&readerlist_lock);
|
---|
972 |
|
---|
973 | for (rdr = first_active_reader; rdr; rdr = rdr->next) {
|
---|
974 | //Generate a uniq reader id:
|
---|
975 | if (!rdr->cc_id) {
|
---|
976 | rdr->cc_id = ++r;
|
---|
977 | struct s_reader *rdr2;
|
---|
978 | for (rdr2 = first_active_reader; rdr2; rdr2 = rdr2->next) {
|
---|
979 | if (rdr2 != rdr && rdr2->cc_id == rdr->cc_id) {
|
---|
980 | rdr2 = first_active_reader;
|
---|
981 | rdr->cc_id=++r;
|
---|
982 | }
|
---|
983 | }
|
---|
984 | }
|
---|
985 |
|
---|
986 | flt = 0;
|
---|
987 |
|
---|
988 | int8_t reshare = rdr->cc_reshare > -1 ? rdr->cc_reshare : cfg.cc_reshare;
|
---|
989 |
|
---|
990 | //Reader-Services:
|
---|
991 | if ((cfg.cc_reshare_services==1||cfg.cc_reshare_services==2||(!rdr->caid && rdr->typ != R_CCCAM && cfg.cc_reshare_services!=4 )) &&
|
---|
992 | cfg.sidtab && (rdr->sidtabs.no || rdr->sidtabs.ok)) {
|
---|
993 | struct s_sidtab *ptr;
|
---|
994 | for (j=0,ptr=cfg.sidtab; ptr; ptr=ptr->next,j++) {
|
---|
995 | if (!(rdr->sidtabs.no&((SIDTABBITS)1<<j)) && (rdr->sidtabs.ok&((SIDTABBITS)1<<j))) {
|
---|
996 | for (k=0;k<ptr->num_caid;k++) {
|
---|
997 | card = create_card2(rdr, (j<<8)|k, ptr->caid[k], reshare);
|
---|
998 | if (!card)
|
---|
999 | return;
|
---|
1000 | card->card_type = CT_CARD_BY_SERVICE_READER;
|
---|
1001 | card->sidtab = ptr;
|
---|
1002 | for (l=0;l<ptr->num_provid;l++) {
|
---|
1003 | struct cc_provider *prov;
|
---|
1004 | if (!cs_malloc(&prov, sizeof(struct cc_provider)))
|
---|
1005 | return;
|
---|
1006 | prov->prov = ptr->provid[l];
|
---|
1007 | ll_append(card->providers, prov);
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | if (chk_ident(&rdr->ftab, card) && chk_ctab(card->caid, &rdr->ctab)) {
|
---|
1011 | if (!rdr->audisabled)
|
---|
1012 | cc_UA_oscam2cccam(rdr->hexserial, card->hexserial, card->caid);
|
---|
1013 |
|
---|
1014 | add_card_to_serverlist(get_cardlist(card->caid, server_cards), card, 1);
|
---|
1015 | flt=1;
|
---|
1016 | }
|
---|
1017 | else
|
---|
1018 | cc_free_card(card);
|
---|
1019 | }
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | //Filts by Hardware readers:
|
---|
1025 | if ((rdr->typ != R_CCCAM) && rdr->ftab.filts && !flt) {
|
---|
1026 | for (j = 0; j < CS_MAXFILTERS; j++) {
|
---|
1027 | uint16_t caid = rdr->ftab.filts[j].caid;
|
---|
1028 | if (caid) {
|
---|
1029 | card = create_card2(rdr, j, caid, reshare);
|
---|
1030 | if (!card)
|
---|
1031 | return;
|
---|
1032 | card->card_type = CT_LOCALCARD;
|
---|
1033 |
|
---|
1034 | //Setting UA: (Unique Address):
|
---|
1035 | if (!rdr->audisabled)
|
---|
1036 | cc_UA_oscam2cccam(rdr->hexserial, card->hexserial, caid);
|
---|
1037 | //cs_log("Ident CCcam card report caid: %04X readr %s subid: %06X", rdr->ftab.filts[j].caid, rdr->label, rdr->cc_id);
|
---|
1038 | for (k = 0; k < rdr->ftab.filts[j].nprids; k++) {
|
---|
1039 | struct cc_provider *prov;
|
---|
1040 | if (!cs_malloc(&prov, sizeof(struct cc_provider)))
|
---|
1041 | return;
|
---|
1042 | prov->prov = rdr->ftab.filts[j].prids[k];
|
---|
1043 |
|
---|
1044 | //cs_log("Ident CCcam card report provider: %02X%02X%02X", buf[21 + (k*7)]<<16, buf[22 + (k*7)], buf[23 + (k*7)]);
|
---|
1045 | if (!rdr->audisabled) {
|
---|
1046 | for (l = 0; l < rdr->nprov; l++) {
|
---|
1047 | uint32_t rprid = get_reader_prid(rdr, l);
|
---|
1048 | if (rprid == prov->prov)
|
---|
1049 | cc_SA_oscam2cccam(&rdr->sa[l][0], prov->sa);
|
---|
1050 | }
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | ll_append(card->providers, prov);
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | add_good_bad_sids_by_rdr(rdr, card);
|
---|
1057 | add_card_to_serverlist(get_cardlist(caid, server_cards), card, 1);
|
---|
1058 | flt = 1;
|
---|
1059 | }
|
---|
1060 | }
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | if ((rdr->typ != R_CCCAM) && !rdr->caid && !flt) {
|
---|
1064 | for (j = 0; j < CS_MAXCAIDTAB; j++) {
|
---|
1065 | //cs_log("CAID map CCcam card report caid: %04X cmap: %04X", rdr->ctab.caid[j], rdr->ctab.cmap[j]);
|
---|
1066 | uint16_t lcaid = rdr->ctab.caid[j];
|
---|
1067 |
|
---|
1068 | if (!lcaid || (lcaid == 0xFFFF))
|
---|
1069 | lcaid = rdr->ctab.cmap[j];
|
---|
1070 |
|
---|
1071 | if (lcaid && (lcaid != 0xFFFF)) {
|
---|
1072 | card = create_card2(rdr, j, lcaid, reshare);
|
---|
1073 | if (!card)
|
---|
1074 | return;
|
---|
1075 | card->card_type = CT_CARD_BY_CAID1;
|
---|
1076 | if (!rdr->audisabled)
|
---|
1077 | cc_UA_oscam2cccam(rdr->hexserial, card->hexserial, lcaid);
|
---|
1078 |
|
---|
1079 | add_good_bad_sids_by_rdr(rdr, card);
|
---|
1080 | add_card_to_serverlist(get_cardlist(lcaid, server_cards), card, 1);
|
---|
1081 | flt = 1;
|
---|
1082 | }
|
---|
1083 | }
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | if ((rdr->typ != R_CCCAM) && rdr->ctab.caid[0] && !flt) {
|
---|
1087 | //cs_log("tcp_connected: %d card_status: %d ", rdr->tcp_connected, rdr->card_status);
|
---|
1088 | int32_t c;
|
---|
1089 | if (rdr->tcp_connected || rdr->card_status == CARD_INSERTED) {
|
---|
1090 | for (c=0;c<CS_MAXCAIDTAB;c++)
|
---|
1091 | {
|
---|
1092 | uint16_t caid = rdr->ctab.caid[c];
|
---|
1093 | if (!caid) break;
|
---|
1094 |
|
---|
1095 | card = create_card2(rdr, c, caid, reshare);
|
---|
1096 | if (!card)
|
---|
1097 | break;
|
---|
1098 | card->card_type = CT_CARD_BY_CAID2;
|
---|
1099 |
|
---|
1100 | if (!rdr->audisabled)
|
---|
1101 | cc_UA_oscam2cccam(rdr->hexserial, card->hexserial, caid);
|
---|
1102 | for (j = 0; j < rdr->nprov; j++) {
|
---|
1103 | uint32_t prid = get_reader_prid(rdr, j);
|
---|
1104 | struct cc_provider *prov;
|
---|
1105 | if (!cs_malloc(&prov, sizeof(struct cc_provider)))
|
---|
1106 | return;
|
---|
1107 | prov->prov = prid;
|
---|
1108 | //cs_log("Ident CCcam card report provider: %02X%02X%02X", buf[21 + (k*7)]<<16, buf[22 + (k*7)], buf[23 + (k*7)]);
|
---|
1109 | if (!rdr->audisabled) {
|
---|
1110 | //Setting SA (Shared Addresses):
|
---|
1111 | cc_SA_oscam2cccam(rdr->sa[j], prov->sa);
|
---|
1112 | }
|
---|
1113 | ll_append(card->providers, prov);
|
---|
1114 | //cs_log("Main CCcam card report provider: %02X%02X%02X%02X", buf[21+(j*7)], buf[22+(j*7)], buf[23+(j*7)], buf[24+(j*7)]);
|
---|
1115 | }
|
---|
1116 | add_good_bad_sids_by_rdr(rdr, card);
|
---|
1117 | add_card_to_serverlist(get_cardlist(caid, server_cards), card, 1);
|
---|
1118 | flt = 1;
|
---|
1119 | }
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 |
|
---|
1124 | if ((rdr->typ != R_CCCAM) && rdr->caid && !flt) {
|
---|
1125 | //cs_log("tcp_connected: %d card_status: %d ", rdr->tcp_connected, rdr->card_status);
|
---|
1126 | if (rdr->tcp_connected || rdr->card_status == CARD_INSERTED) {
|
---|
1127 | uint16_t caid = rdr->caid;
|
---|
1128 | card = create_card2(rdr, 1, caid, reshare);
|
---|
1129 | if (!card)
|
---|
1130 | return;
|
---|
1131 | card->card_type = CT_CARD_BY_CAID3;
|
---|
1132 |
|
---|
1133 | if (!rdr->audisabled)
|
---|
1134 | cc_UA_oscam2cccam(rdr->hexserial, card->hexserial, caid);
|
---|
1135 | for (j = 0; j < rdr->nprov; j++) {
|
---|
1136 | uint32_t prid = get_reader_prid(rdr, j);
|
---|
1137 | struct cc_provider *prov;
|
---|
1138 | if (!cs_malloc(&prov, sizeof(struct cc_provider)))
|
---|
1139 | return;
|
---|
1140 | prov->prov = prid;
|
---|
1141 | //cs_log("Ident CCcam card report provider: %02X%02X%02X", buf[21 + (k*7)]<<16, buf[22 + (k*7)], buf[23 + (k*7)]);
|
---|
1142 | if (!rdr->audisabled) {
|
---|
1143 | //Setting SA (Shared Addresses):
|
---|
1144 | cc_SA_oscam2cccam(rdr->sa[j], prov->sa);
|
---|
1145 | }
|
---|
1146 | ll_append(card->providers, prov);
|
---|
1147 | //cs_log("Main CCcam card report provider: %02X%02X%02X%02X", buf[21+(j*7)], buf[22+(j*7)], buf[23+(j*7)], buf[24+(j*7)]);
|
---|
1148 | }
|
---|
1149 | add_good_bad_sids_by_rdr(rdr, card);
|
---|
1150 | add_card_to_serverlist(get_cardlist(caid, server_cards), card, 1);
|
---|
1151 | }
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | if (rdr->typ == R_CCCAM && rdr->tcp_connected &&
|
---|
1155 | (cfg.cc_reshare_services<2 || cfg.cc_reshare_services==4) && rdr->card_status != CARD_FAILURE) {
|
---|
1156 |
|
---|
1157 | cs_debug_mask(D_TRACE, "asking reader %s for cards...", rdr->label);
|
---|
1158 |
|
---|
1159 | struct s_client *rc = rdr->client;
|
---|
1160 | struct cc_data *rcc = rc?rc->cc:NULL;
|
---|
1161 |
|
---|
1162 | int32_t count = 0;
|
---|
1163 | if (rcc && rcc->cards && !rc->kill) {
|
---|
1164 | cs_readlock(&rcc->cards_busy);
|
---|
1165 |
|
---|
1166 | it = ll_iter_create(rcc->cards);
|
---|
1167 | while ((card = ll_iter_next(&it))) {
|
---|
1168 | if (chk_ctab(card->caid, &rdr->ctab)) {
|
---|
1169 | int32_t ignore = 0;
|
---|
1170 |
|
---|
1171 | it2 = ll_iter_create(card->providers);
|
---|
1172 | struct cc_provider *prov;
|
---|
1173 | while ((prov = ll_iter_next(&it2))) {
|
---|
1174 | uint32_t prid = prov->prov;
|
---|
1175 | if (!chk_srvid_by_caid_prov(rc, card->caid, prid)) {
|
---|
1176 | ignore = 1;
|
---|
1177 | break;
|
---|
1178 | }
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | if (!ignore) { //Filtered by service
|
---|
1182 | add_card_to_serverlist(get_cardlist(card->caid, server_cards), card, 0);
|
---|
1183 | count++;
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 | }
|
---|
1187 | cs_readunlock(&rcc->cards_busy);
|
---|
1188 | }
|
---|
1189 | else
|
---|
1190 | cs_debug_mask(D_TRACE, "reader %s not active!", rdr->label);
|
---|
1191 | cs_debug_mask(D_TRACE, "got %d cards from %s", count, rdr->label);
|
---|
1192 | }
|
---|
1193 | }
|
---|
1194 | cs_readunlock(&readerlist_lock);
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | LLIST *new_cards = ll_create("new_cards"); //List of new (added) cards
|
---|
1198 |
|
---|
1199 | cs_writelock(&cc_shares_lock);
|
---|
1200 |
|
---|
1201 | //report reshare cards:
|
---|
1202 | //cs_debug_mask(D_TRACE, "%s reporting %d cards", getprefix(), ll_count(server_cards));
|
---|
1203 | for (i = 0; i < CAID_KEY; i++) {
|
---|
1204 | if (server_cards[i]) {
|
---|
1205 | it = ll_iter_create(server_cards[i]);
|
---|
1206 |
|
---|
1207 | //we compare every card of our new list (server_cards) with the last list.
|
---|
1208 | while ((card = ll_iter_next(&it))) {
|
---|
1209 | //cs_debug_mask(D_TRACE, "%s card %d caid %04X hop %d", getprefix(), card->id, card->caid, card->hop);
|
---|
1210 |
|
---|
1211 | if (!new_reported_carddatas[i])
|
---|
1212 | new_reported_carddatas[i] = ll_create("new_cardlist");
|
---|
1213 | report_card(card, new_reported_carddatas[i], new_cards);
|
---|
1214 | ll_iter_remove(&it);
|
---|
1215 | }
|
---|
1216 | cc_free_cardlist(server_cards[i], 1);
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | //remove unsed, remaining cards:
|
---|
1220 | card_removed_count += cc_free_reported_carddata(reported_carddatas_list[i], new_reported_carddatas[i], 1);
|
---|
1221 | reported_carddatas_list[i] = new_reported_carddatas[i];
|
---|
1222 | card_count += ll_count(reported_carddatas_list[i]);
|
---|
1223 | //cs_debug_mask(D_TRACE, "CARDS FOR INDEX %d=%d", i, ll_count(reported_carddatas[i]));
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | //now send new cards. Always remove first, then add new:
|
---|
1227 | it = ll_iter_create(new_cards);
|
---|
1228 | while ((card = ll_iter_next(&it))) {
|
---|
1229 | send_card_to_all_clients(card);
|
---|
1230 | }
|
---|
1231 | ll_destroy_NULL(new_cards);
|
---|
1232 |
|
---|
1233 |
|
---|
1234 | cs_writeunlock(&cc_shares_lock);
|
---|
1235 |
|
---|
1236 | cs_debug_mask(D_TRACE, "reported/updated +%d/-%d/dup %d of %d cards to sharelist",
|
---|
1237 | card_added_count, card_removed_count, card_dup_count, card_count);
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | int32_t cc_srv_report_cards(struct s_client *cl) {
|
---|
1241 |
|
---|
1242 | struct cc_card *card;
|
---|
1243 | int32_t i, count = 0;
|
---|
1244 | LL_ITER it;
|
---|
1245 | cs_readlock(&cc_shares_lock);
|
---|
1246 | for (i=0; i < CAID_KEY; i++) {
|
---|
1247 | if (reported_carddatas_list[i]) {
|
---|
1248 | it = ll_iter_create(reported_carddatas_list[i]);
|
---|
1249 | while (cl->cc && !cl->kill && (card = ll_iter_next(&it))) {
|
---|
1250 | count += send_card_to_client(card, cl);
|
---|
1251 | }
|
---|
1252 | }
|
---|
1253 | }
|
---|
1254 | cs_readunlock(&cc_shares_lock);
|
---|
1255 | cs_debug_mask(D_TRACE, "reported %d cards for %s", count, username(cl));
|
---|
1256 |
|
---|
1257 | return cl->cc && !cl->kill;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | void refresh_shares(void)
|
---|
1261 | {
|
---|
1262 | update_card_list();
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | #define DEFAULT_INTERVAL 30
|
---|
1266 |
|
---|
1267 | void share_updater(void)
|
---|
1268 | {
|
---|
1269 | int32_t i = DEFAULT_INTERVAL + cfg.waitforcards_extra_delay/1000;
|
---|
1270 | uint32_t last_check = 0;
|
---|
1271 | uint32_t last_check_rdroptions = 0;
|
---|
1272 | uint32_t cur_check_rdroptions = 0;
|
---|
1273 | uint32_t last_card_check = 0;
|
---|
1274 | uint32_t last_sidtab_generation = 0;
|
---|
1275 | uint32_t card_count = 0;
|
---|
1276 | while (share_updater_thread_active) {
|
---|
1277 | if (i > 0 && card_count < 100) { //fast refresh only if we have less cards
|
---|
1278 | cs_debug_mask(D_TRACE, "share-updater mode=initfast t=1s i=%d", i);
|
---|
1279 | cs_sleepms(1000);
|
---|
1280 | i--;
|
---|
1281 | }
|
---|
1282 | else if (i > 0) {
|
---|
1283 | cs_debug_mask(D_TRACE, "share-updater mode=initslow t=6s i=%d", i);
|
---|
1284 | cs_sleepms(6000); //1s later than garbage collector because this list uses much space
|
---|
1285 | i-=6;
|
---|
1286 | }
|
---|
1287 | else
|
---|
1288 | {
|
---|
1289 | if (cfg.cc_update_interval <= 10)
|
---|
1290 | cfg.cc_update_interval = DEFAULT_UPDATEINTERVAL;
|
---|
1291 | cs_debug_mask(D_TRACE, "share-updater mode=interval t=%ds", cfg.cc_update_interval);
|
---|
1292 | cs_sleepms(cfg.cc_update_interval*1000);
|
---|
1293 | }
|
---|
1294 | if (!share_updater_thread_active)
|
---|
1295 | break;
|
---|
1296 |
|
---|
1297 | uint32_t cur_check = 0;
|
---|
1298 | uint32_t cur_card_check = 0;
|
---|
1299 | int8_t rdroptionchange = 0;
|
---|
1300 | card_count = 0;
|
---|
1301 | struct s_reader *rdr;
|
---|
1302 | struct cc_data *cc;
|
---|
1303 |
|
---|
1304 | cs_readlock(&readerlist_lock);
|
---|
1305 |
|
---|
1306 | for (rdr=first_active_reader; rdr; rdr=rdr->next) {
|
---|
1307 | struct s_client *cl = rdr->client;
|
---|
1308 | if (cl && (cc=cl->cc)) { //check cccam-cardlist:
|
---|
1309 | cur_card_check += cc->card_added_count;
|
---|
1310 | cur_card_check += cc->card_removed_count;
|
---|
1311 | card_count += ll_count(cc->cards);
|
---|
1312 | }
|
---|
1313 | cur_check = crc32(cur_check, (uint8_t*)&rdr->tcp_connected, sizeof(rdr->tcp_connected));
|
---|
1314 | cur_check = crc32(cur_check, (uint8_t*)&rdr->card_status, sizeof(rdr->card_status));
|
---|
1315 |
|
---|
1316 | //Check hexserial/UA changes only on lokal readers:
|
---|
1317 | if (!is_network_reader(rdr)) {
|
---|
1318 | cur_check = crc32(cur_check, (uint8_t*)&rdr->hexserial, 8); //check hexserial
|
---|
1319 | cur_check = crc32(cur_check, (uint8_t*)&rdr->prid, rdr->nprov * sizeof(rdr->prid[0])); //check providers
|
---|
1320 | cur_check = crc32(cur_check, (uint8_t*)&rdr->sa, rdr->nprov * sizeof(rdr->sa[0])); //check provider-SA
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | if(rdr->changes_since_shareupdate){
|
---|
1324 | rdr->changes_since_shareupdate = 0;
|
---|
1325 | rdroptionchange = 1;
|
---|
1326 | }
|
---|
1327 | }
|
---|
1328 | if(rdroptionchange){
|
---|
1329 | cur_check_rdroptions = 0;
|
---|
1330 | for (rdr=first_active_reader; rdr; rdr=rdr->next) {
|
---|
1331 | cur_check_rdroptions = crc32(cur_check_rdroptions, (uint8_t*)&rdr->ftab, sizeof(FTAB)); //check reader
|
---|
1332 | cur_check_rdroptions = crc32(cur_check_rdroptions, (uint8_t*)&rdr->ctab, sizeof(CAIDTAB)); //check caidtab
|
---|
1333 | cur_check_rdroptions = crc32(cur_check_rdroptions, (uint8_t*)&rdr->fchid, sizeof(FTAB)); //check chids
|
---|
1334 | cur_check_rdroptions = crc32(cur_check_rdroptions, (uint8_t*)&rdr->sidtabs.ok, sizeof(rdr->sidtabs.ok)); //check assigned ok services
|
---|
1335 | cur_check_rdroptions = crc32(cur_check_rdroptions, (uint8_t*)&rdr->sidtabs.no, sizeof(rdr->sidtabs.no)); //check assigned no services
|
---|
1336 | }
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | cs_readunlock(&readerlist_lock);
|
---|
1340 |
|
---|
1341 | //update cardlist if reader config has changed, also set interval to 1s / 30times
|
---|
1342 | if (cur_check != last_check || last_sidtab_generation != cfg_sidtab_generation || last_check_rdroptions != cur_check_rdroptions) {
|
---|
1343 | last_sidtab_generation = cfg_sidtab_generation;
|
---|
1344 | i = DEFAULT_INTERVAL;
|
---|
1345 | cs_debug_mask(D_TRACE, "share-update [1] %u %u", cur_check, last_check);
|
---|
1346 | refresh_shares();
|
---|
1347 | last_check = cur_check;
|
---|
1348 | last_card_check = cur_card_check;
|
---|
1349 | }
|
---|
1350 | //update cardlist if cccam cards has changed:
|
---|
1351 | else if (cur_card_check != last_card_check) {
|
---|
1352 | cs_debug_mask(D_TRACE, "share-update [2] %u %u", cur_card_check, last_card_check);
|
---|
1353 | refresh_shares();
|
---|
1354 | last_card_check = cur_card_check;
|
---|
1355 | }
|
---|
1356 | last_check_rdroptions = cur_check_rdroptions;
|
---|
1357 | }
|
---|
1358 | for (i = 0; i < CAID_KEY; i++)
|
---|
1359 | cc_free_reported_carddata(reported_carddatas_list[i], NULL, 0);
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | int32_t compare_cards_by_hop(struct cc_card **pcard1, struct cc_card **pcard2)
|
---|
1363 | {
|
---|
1364 | struct cc_card *card1 = (*pcard1), *card2 = (*pcard2);
|
---|
1365 |
|
---|
1366 | int32_t res = card1->hop - card2->hop;
|
---|
1367 | if (res) return res;
|
---|
1368 | res = card1->caid - card2->caid;
|
---|
1369 | if (res) return res;
|
---|
1370 | res = card1->reshare - card2->reshare;
|
---|
1371 | if (res) return res;
|
---|
1372 | res = card1->id - card2->id;
|
---|
1373 | return res;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | int32_t compare_cards_by_hop_r(struct cc_card **pcard1, struct cc_card **pcard2)
|
---|
1377 | {
|
---|
1378 | return -compare_cards_by_hop(pcard1, pcard2);
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | struct cc_card **get_sorted_card_copy(LLIST *cards, int32_t reverse, int32_t *size)
|
---|
1382 | {
|
---|
1383 | if (reverse)
|
---|
1384 | return (struct cc_card **)ll_sort(cards, compare_cards_by_hop_r, size);
|
---|
1385 | else
|
---|
1386 | return (struct cc_card **)ll_sort(cards, compare_cards_by_hop, size);
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | void cccam_init_share(void) {
|
---|
1390 |
|
---|
1391 | memset(reported_carddatas_list, 0, sizeof(reported_carddatas_list));
|
---|
1392 | cs_lock_create(&cc_shares_lock, 200, "cc_shares_lock");
|
---|
1393 |
|
---|
1394 | share_updater_thread = 0;
|
---|
1395 | share_updater_thread_active = 1;
|
---|
1396 | pthread_t temp;
|
---|
1397 | pthread_attr_t attr;
|
---|
1398 | pthread_attr_init(&attr);
|
---|
1399 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
1400 | int32_t ret = pthread_create(&temp, &attr, (void*)&share_updater, NULL);
|
---|
1401 | if (ret)
|
---|
1402 | cs_log("ERROR: can't create share updater thread (errno=%d %s)", ret, strerror(ret));
|
---|
1403 | else {
|
---|
1404 | cs_debug_mask(D_TRACE, "share updater thread started");
|
---|
1405 | pthread_detach(temp);
|
---|
1406 | share_updater_thread = temp;
|
---|
1407 | }
|
---|
1408 | pthread_attr_destroy(&attr);
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | void cccam_done_share(void) {
|
---|
1412 | if (share_updater_thread) {
|
---|
1413 | share_updater_thread_active = 0;
|
---|
1414 | share_updater_thread = 0;
|
---|
1415 | }
|
---|
1416 | }
|
---|
1417 | #endif
|
---|