-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsrv-newcamd.c
602 lines (548 loc) · 19 KB
/
srv-newcamd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#if 0
#
# Copyright (c) 2014 - 2015 Javier Sayago <admin@lonasdigital.com>
# Contact: javilonas@esp-desarrolladores.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#endif
///////////////////////////////////////////////////////////////////////////////
// PROTO
///////////////////////////////////////////////////////////////////////////////
void cs_disconnect_cli(struct cs_client_data *cli);
void *cs_connect_cli_thread(void *param);
void cs_cli_recvmsg(struct cs_client_data *cli, struct cardserver_data *cs);
uint cs_check_sendcw();
struct cs_client_data *getnewcamdclientbyid(struct cardserver_data *cs, uint32_t id)
{
struct cs_client_data *cli = cs->client;
while (cli) {
if (cli->id==id) return cli;
cli = cli->next;
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////
void cs_disconnect_cli(struct cs_client_data *cli)
{
if (cli->handle!=INVALID_SOCKET) {
debugf(" newcamd: client '%s' disconnected\n", cli->user);
close(cli->handle);
cli->handle = INVALID_SOCKET;
cli->uptime += GetTickCount()-cli->connected;
cli->connected = 0;
}
}
struct cs_clicon {
struct cardserver_data *cs;
int32_t sock;
uint32_t ip;
};
void *th_cs_connect_cli(struct cs_clicon *param)
{
char passwdcrypt[120];
unsigned char keymod[14];
int32_t i,index;
unsigned char sessionkey[16];
struct cs_custom_data clicd;
unsigned char buf[CWS_NETMSGSIZE];
struct cardserver_data *cs = param->cs;
int32_t sock = param->sock;
uint32_t ip = param->ip;
free(param);
// Create random deskey
for (i=0; i<14; i++) keymod[i] = 0xff & rand();
// Create NewBox ID
keymod[3] = (keymod[0]^'N') + keymod[1] + keymod[2];
keymod[7] = keymod[4] + (keymod[5]^'B') + keymod[6];
keymod[11] = keymod[8] + keymod[9] + (keymod[10]^'x');
//STAT: SEND RND KEY
// send random des key
if (send_nonb(sock, keymod, 14, 500)<=0) {
debugf(" ERR SEND\n");
close(sock);
pthread_exit(NULL);
}
uint32_t ticks = GetTickCount();
// Calc SessionKey
//debugf(" DES Key: "); debughex(keymod,14);
des_login_key_get(keymod, cs->key, 14, sessionkey);
//debugf(" Login Key: "); debughex(sessionkey,16);
//md5_crypt("pass", "$1$abcdefgh$",passwdcrypt);
//debugf(" passwdcrypt = %s\n",passwdcrypt);
//STAT: LOGIN INFO
// 3. login info
i = cs_message_receive(sock, &clicd, buf, sessionkey,3000);
if (i<=0) {
if (i==-2) debugf(" %s: (%s) new connection closed, wrong des key\n", cs->name, ip2string(ip));
else debugf(" %s: (%s) new connection closed, receive timeout\n", cs->name, ip2string(ip));
close(sock);
pthread_exit(NULL);
}
ticks = GetTickCount()-ticks;
if (buf[0]!=MSG_CLIENT_2_SERVER_LOGIN) {
close(sock);
pthread_exit(NULL);
}
// Check username length
if ( strlen( (char*)buf+3 )>63 ) {
/*
buf[0] = MSG_CLIENT_2_SERVER_LOGIN_NAK;
buf[1] = 0;
buf[2] = 0;
cs_message_send(sock, NULL, buf, 3, sessionkey);
*/
debugf(" %s: (%s) new connection closed, wrong username length\n", cs->name, ip2string(ip));
close(sock);
pthread_exit(NULL);
}
pthread_mutex_lock(&prg.lockcli);
index = 3;
struct cs_client_data *usr = cs->client;
int32_t found = 0;
while (usr) {
if (!strcmp(usr->user,(char*)&buf[index])) {
found=1;
break;
}
usr = usr->next;
}
if (!found) {
pthread_mutex_unlock(&prg.lockcli);
buf[0] = MSG_CLIENT_2_SERVER_LOGIN_NAK;
buf[1] = 0;
buf[2] = 0;
//cs_message_send(sock, NULL, buf, 3, sessionkey);
debugf(" %s: (%s) new connection closed, unknown user '%s'\n", cs->name, ip2string(ip), &buf[3]);
close(sock);
pthread_exit(NULL);
}
// Check password
index += strlen(usr->user) +1;
__md5_crypt(usr->pass, "$1$abcdefgh$",passwdcrypt);
if (!strcmp(passwdcrypt,(char*)&buf[index])) {
//Check Reconnection
if (usr->handle!=INVALID_SOCKET) {
debugf(" %s: user already connected '%s' (%s)\n", cs->name, usr->user, ip2string(ip));
cs_disconnect_cli(usr);
}
// Store program id
usr->progid = clicd.sid;
//
buf[0] = MSG_CLIENT_2_SERVER_LOGIN_ACK;
buf[1] = 0;
buf[2] = 0;
// Send NewBox Id&Version
if (clicd.provid==0x0057484F) { // WHO
clicd.provid = 0x004E4278; // MBx
clicd.sid = REVISION; // Revision
cs_message_send(sock, &clicd, buf, 3, sessionkey);
} else cs_message_send(sock, NULL, buf, 3, sessionkey);
des_login_key_get( cs->key, (unsigned char*)passwdcrypt, strlen(passwdcrypt),sessionkey);
memcpy( &usr->sessionkey, &sessionkey, 16);
// Setup User data
usr->handle = sock;
usr->ip = ip;
usr->ping = ticks;
usr->lastecmtime = GetTickCount();
usr->ecm.busy=0;
usr->ecm.recvtime=0;
usr->connected = GetTickCount();
usr->chkrecvtime = 0;
debugf(" %s: client '%s' connected (%s)\n", cs->name, usr->user, ip2string(ip));
pipe_wakeup( srvsocks[1] );
}
else {
// send NAK
buf[0] = MSG_CLIENT_2_SERVER_LOGIN_NAK;
buf[1] = 0;
buf[2] = 0;
//cs_message_send(sock, NULL, buf, 3, sessionkey);
debugf(" newcamd: client '%s' wrong password (%s)\n", usr->user, ip2string(ip));
close(sock);
}
pthread_mutex_unlock(&prg.lockcli);
pthread_exit(NULL);
}
void *cs_connect_cli_thread(void *param)
{
int32_t clientsock;
struct sockaddr_in clientaddr;
socklen_t socklen = sizeof(struct sockaddr);
// Connect Clients
pthread_t srv_tid;
while (1) {
pthread_mutex_lock(&prg.locksrvcs);
struct pollfd pfd[MAX_CSPORTS];
int32_t pfdcount = 0;
struct cardserver_data *cs = cfg.cardserver;
while(cs) {
if (cs->handle>0) {
cs->ipoll = pfdcount;
pfd[pfdcount].fd = cs->handle;
pfd[pfdcount++].events = POLLIN | POLLPRI;
} else cs->ipoll = -1;
cs = cs->next;
}
int32_t retval = poll(pfd, pfdcount, 3000);
if (retval>0) {
struct cardserver_data *cs = cfg.cardserver;
while(cs) {
if ( (cs->handle>0) && (cs->ipoll>=0) && (cs->handle==pfd[cs->ipoll].fd) ) {
if ( pfd[cs->ipoll].revents & (POLLIN|POLLPRI) ) {
clientsock = accept(cs->handle, (struct sockaddr*)&clientaddr, &socklen );
if (clientsock<=0) {
//if (errno == EAGAIN || errno == EINTR) continue;
//else {
debugf(" newcamd: Accept failed (errno=%d)\n",errno);
//}
}
else {
//debugf(" New Client Connection...%s\n", ip2string(clientaddr.sin_addr.s_addr) );
SetSocketKeepalive(clientsock);
struct cs_clicon *clicondata = malloc( sizeof(struct cs_clicon) );
clicondata->cs = cs;
clicondata->sock = clientsock;
clicondata->ip = clientaddr.sin_addr.s_addr;
//while(EAGAIN==pthread_create(&srv_tid, NULL, th_cs_connect_cli,clicondata)) usleep(1000); pthread_detach(&srv_tid);
create_prio_thread(&srv_tid, (threadfn)th_cs_connect_cli,clicondata, 50);
}
}
}
cs = cs->next;
}
}
pthread_mutex_unlock(&prg.locksrvcs);
usleep(3000);
}
END_PROCESS = 1;
}
void cs_store_ecmclient(struct cardserver_data *cs, int32_t ecmid, struct cs_client_data *cli, int32_t climsgid)
{
//check for last ecm recv time
uint32_t ticks = GetTickCount();
cli->ecm.dcwtime = cli->dcwtime;
cli->ecm.recvtime = ticks;
cli->ecm.id = ecmid;
cli->ecm.climsgid = climsgid;
cli->ecm.status = STAT_ECM_SENT;
}
void cs_cli_recvmsg(struct cs_client_data *cli, struct cardserver_data *cs)
{
struct cs_custom_data clicd; // Custom data
int32_t len;
unsigned char buf[CWS_NETMSGSIZE];
unsigned char data[CWS_NETMSGSIZE]; // for other use
if (cli->handle>0) {
len = cs_msg_chkrecv(cli->handle);
if (len==0) {
debugf(" newcamd: client '%s' read failed %d\n", cli->user,len);
cs_disconnect_cli(cli);
}
else if (len==-1) {
if (!cli->chkrecvtime) cli->chkrecvtime = GetTickCount();
else if ( (cli->chkrecvtime+300)<GetTickCount() ) {
debugf(" newcamd: client '%s' read timeout %d\n", cli->user,len);
cs_disconnect_cli(cli);
}
}
else if (len>0) {
cli->chkrecvtime = 0;
len = cs_message_receive(cli->handle, &clicd, buf, cli->sessionkey,3);
if (len==0) {
debugf(" newcamd: client '%s' read failed %d\n", cli->user,len);
cs_disconnect_cli(cli);
}
else if (len<0) {
debugf(" newcamd: client '%s' read failed %d(%d)\n", cli->user,len,errno);
cs_disconnect_cli(cli);
}
else if (len>0) switch ( buf[0] ) {
case MSG_CARD_DATA_REQ:
memset( buf, 0, sizeof(buf) );
buf[0] = MSG_CARD_DATA;
if (!cli->card.caid) {
buf[4] = cs->card.caid>>8;
buf[5] = cs->card.caid&0xff;
buf[14] = cs->card.nbprov;
for(len=0;len<cs->card.nbprov;len++) {
buf[15+11*len] = cs->card.prov[len]>>16;
buf[16+11*len] = cs->card.prov[len]>>8;
buf[17+11*len] = cs->card.prov[len]&0xff;
}
cs_message_send(cli->handle, &clicd, buf, 15+11*cs->card.nbprov, cli->sessionkey);
}
else {
buf[4] = cli->card.caid>>8;
buf[5] = cli->card.caid&0xff;
buf[14] = cli->card.nbprov;
for(len=0;len<cli->card.nbprov;len++) {
buf[15+11*len] = cli->card.prov[len]>>16;
buf[16+11*len] = cli->card.prov[len]>>8;
buf[17+11*len] = cli->card.prov[len]&0xff;
}
cs_message_send(cli->handle, &clicd, buf, 15+11*cs->card.nbprov, cli->sessionkey);
}
//debugf(" newcamd: send card data to client '%s'\n", cli->user);
break;
case 0x80:
case 0x81:
//debugdump(buf, len, "ECM: ");
cli->lastecmtime = GetTickCount();
cli->ecmnb++;
if (cli->ecm.busy) {
cli->ecmdenied++;
// send decode failed
buf[1] = 0; buf[2] = 0;
cs_message_send( cli->handle, &clicd, buf, 3, cli->sessionkey);
debugf(" <|> decode failed to client '%s' ch %04x:%06x:%04x, too many ecm requests\n", cli->user,clicd.caid,clicd.provid,clicd.sid);
break;
}
// Check for caid, accept caid=0
if (!clicd.caid) clicd.caid = cs->card.caid;
if (clicd.caid!=cs->card.caid) {
cli->ecmdenied++;
// send decode failed
buf[1] = 0; buf[2] = 0;
cs_message_send( cli->handle, &clicd, buf, 3, cli->sessionkey);
debugf(" <|> decode failed to client '%s' ch %04x:%06x:%04x Wrong CAID\n", cli->user,clicd.caid,clicd.provid,clicd.sid);
break;
}
// Check for provid, accept provid==0
if ( !accept_prov(cs,clicd.provid) ) {
cli->ecmdenied++;
cs->ecmdenied++;
// send decode failed
buf[1] = 0; buf[2] = 0;
cs_message_send( cli->handle, &clicd, buf, 3, cli->sessionkey);
debugf(" <|> decode failed to client '%s' ch %04x:%06x:%04x Wrong PROVIDER\n", cli->user,clicd.caid,clicd.provid,clicd.sid);
break;
}
// Check for Accepted sids
if ( !accept_sid(cs,clicd.sid) ) {
cli->ecmdenied++;
cs->ecmdenied++;
// send decode failed
buf[1] = 0; buf[2] = 0;
cs_message_send( cli->handle, &clicd, buf, 3, cli->sessionkey);
debugf(" <|> decode failed to client '%s' ch %04x:%06x:%04x SID not accepted\n", cli->user,clicd.caid,clicd.provid,clicd.sid);
break;
}
// Check ECM length
if ( !accept_ecmlen(len) ) {
cli->ecmdenied++;
cs->ecmdenied++;
buf[1] = 0; buf[2] = 0;
cs_message_send( cli->handle, &clicd, buf, 3, cli->sessionkey);
debugf(" <- ecm length error from client '%s'\n",cli->user);
break;
}
// ACCEPTED
memcpy( &data[0], &buf[0], len);
pthread_mutex_lock(&prg.lockecm); //###
// Search for ECM
int32_t ecmid = search_ecmdata_dcw( data, len, clicd.sid); // dont get failed ecm request from cache
if ( ecmid!=-1 ) {
ECM_DATA *ecm=getecmbyid(ecmid);
ecm->lastrecvtime = GetTickCount();
//TODO: Add another card for sending ecm
cs_store_ecmclient(cs, ecmid, cli, clicd.msgid);
debugf(" <- ecm from client '%s' ch %04x:%06x:%04x*\n",cli->user,clicd.caid,clicd.provid,clicd.sid);
cli->ecm.busy=1;
cli->ecm.hash = ecm->hash;
}
else {
cs->ecmaccepted++;
// Setup ECM Request for Server(s)
ecmid = store_ecmdata(cs->id, &data[0], len, clicd.sid,clicd.caid,clicd.provid);
ECM_DATA *ecm=getecmbyid(ecmid);
cs_store_ecmclient(cs, ecmid, cli, clicd.msgid);
debugf(" <- ecm from client '%s' ch %04x:%06x:%04x (>%dms)\n",cli->user,clicd.caid,clicd.provid,clicd.sid, cli->ecm.dcwtime);
cli->ecm.busy=1;
cli->ecm.hash = ecm->hash;
if (cs->usecache && cfg.cachepeer) {
pipe_send_cache_find(ecm, cs);
ecm->waitcache = 1;
ecm->dcwstatus = STAT_DCW_WAITCACHE;
} else ecm->dcwstatus = STAT_DCW_WAIT;
}
ecm_check_time = cs_dcw_check_time = 0;
pthread_mutex_unlock(&prg.lockecm); //###
break;
// Incoming DCW from client
case 0xC0:
case 0xC1:
cli->lastecmtime = GetTickCount();
if (!cli->ecm.busy) break;
pthread_mutex_lock(&prg.lockecm); //###
ECM_DATA *ecm = getecmbyid( cli->ecm.id );
if (!ecm) {
debugf(" <!= wrong cw from Client '%s' ch %04x:%06x:%04x\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->lastecmtime);
pthread_mutex_unlock(&prg.lockecm);
break;
}
if (ecm->hash!=cli->ecm.hash) {
debugf(" <!= wrong cw from Client '%s' ch %04x:%06x:%04x\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->lastecmtime);
pthread_mutex_unlock(&prg.lockecm);
break;
}
if ( (ecm->caid!=clicd.caid) || (ecm->sid!=clicd.sid) || (ecm->provid!=clicd.provid) || (cli->ecm.climsgid!=clicd.msgid) ) {
debugf(" <!= wrong cw from Client '%s' ch %04x:%06x:%04x\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->lastecmtime);
pthread_mutex_unlock(&prg.lockecm);
break;
}
if ( (buf[0]&0x81)!=ecm->ecm[0] ) {
debugf(" <!= wrong cw from Client '%s' ch %04x:%06x:%04x\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->lastecmtime);
pthread_mutex_unlock(&prg.lockecm);
break;
}
if (buf[2]!=0x10) {
debugf(" <!= wrong cw from Client '%s' ch %04x:%06x:%04x\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->lastecmtime);
pthread_mutex_unlock(&prg.lockecm);
break;
}
// Check for DCW
if (!acceptDCW(&buf[3])) {
debugf(" <!= wrong cw from Client '%s' ch %04x:%06x:%04x\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->lastecmtime);
pthread_mutex_unlock(&prg.lockecm);
break;
}
debugf(" <= cw from Client '%s' ch %04x:%06x:%04x (%dms)\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->lastecmtime);
if (ecm->dcwstatus!=STAT_DCW_SUCCESS) {
static char msg[] = "Good dcw from Newcamd Client";
ecm->statusmsg = msg;
// Store ECM Answer
ecm_setdcw( getcsbyid(ecm->csid), ecm, &buf[3], DCW_SOURCE_CSCLIENT, (ecm->csid<<16)|cli->id );
}
else { //TODO: check same dcw between cards
if ( memcmp(&ecm->cw, &buf[3],16) ) debugf(" !!! different dcw from newcamd client '%s'\n", cli->user);
}
pthread_mutex_unlock(&prg.lockecm); //###
break;
default:
if (buf[0]==MSG_KEEPALIVE) {
// Check for Cache client??
if (clicd.sid==(('C'<<8)|'H')) clicd.caid = ('O'<<8)|'K';
//debugf(" <-> keepalive from/to client '%s'\n",cli->user);
cs_message_send(cli->handle, &clicd, buf, 3, cli->sessionkey);
}
else {
debugf(" newcamd: unknown message type '%02x' from client '%s'\n",buf[0],cli->user);
buf[1]=0; buf[2]=0;
cs_message_send( cli->handle, &clicd, buf, 3, cli->sessionkey);
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// DCW
///////////////////////////////////////////////////////////////////////////////
void cs_senddcw_cli(struct cs_client_data *cli)
{
unsigned char buf[CWS_NETMSGSIZE];
struct cs_custom_data clicd; // Custom data
if (cli->ecm.status==STAT_DCW_SENT) {
debugf(" +> cw send failed to client '%s', cw already sent\n", cli->user);
return;
}
if (cli->handle==INVALID_SOCKET) {
debugf(" +> cw send failed to client '%s', client disconnected\n", cli->user);
return;
}
if (!cli->ecm.busy) {
debugf(" +> cw send failed to client '%s', no ecm request\n", cli->user);
return;
}
ECM_DATA *ecm = getecmbyid(cli->ecm.id);
cli->ecm.lastcaid = ecm->caid;
cli->ecm.lastprov = ecm->provid;
cli->ecm.lastsid = ecm->sid;
cli->ecm.lastdecodetime = GetTickCount()-cli->ecm.recvtime;
cli->ecm.lastid = cli->ecm.id;
clicd.msgid = cli->ecm.climsgid;
clicd.sid = ecm->sid;
clicd.caid = ecm->caid;
clicd.provid = ecm->provid;
if ( (ecm->hash==cli->ecm.hash)&&(ecm->dcwstatus==STAT_DCW_SUCCESS) ) {
cli->ecm.lastdcwsrctype = ecm->dcwsrctype;
cli->ecm.lastdcwsrcid = ecm->dcwsrcid;
cli->ecm.laststatus=1;
cli->ecmok++;
cli->ecmoktime += GetTickCount()-cli->ecm.recvtime;
buf[0] = ecm->ecm[0];
buf[1] = 0;
buf[2] = 0x10;
memcpy( &buf[3], &ecm->cw, 16 );
cs_message_send( cli->handle, &clicd, buf, 19, cli->sessionkey);
debugf(" => cw to client '%s' ch %04x:%06x:%04x (%dms)\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->ecm.recvtime);
cli->lastdcwtime = GetTickCount();
}
else { //if (ecm->data->dcwstatus==STAT_DCW_FAILED)
cli->ecm.laststatus=0;
cli->ecm.lastdcwsrctype = DCW_SOURCE_NONE;
cli->ecm.lastdcwsrcid = 0;
buf[0] = ecm->ecm[0];
buf[1] = 0;
buf[2] = 0;
cs_message_send( cli->handle, &clicd, buf, 3, cli->sessionkey);
debugf(" |> decode failed to client '%s' ch %04x:%06x:%04x (%dms)\n", cli->user, ecm->caid,ecm->provid,ecm->sid, GetTickCount()-cli->ecm.recvtime);
}
cli->ecm.busy=0;
cli->ecm.status = STAT_DCW_SENT;
}
// Check sending cw to clients
uint cs_check_sendcw()
{
struct cardserver_data *cs = cfg.cardserver;
uint restime = GetTickCount() + 10000;
uint clitime = 0;
while(cs) {
if (cs->handle>0) {
struct cs_client_data *cli = cs->client;
uint ticks = GetTickCount();
while (cli) {
if ( (cli->handle!=INVALID_SOCKET)&&(cli->ecm.busy)&&(cli->ecm.status==STAT_ECM_SENT) ) {
clitime = ticks+11000;
pthread_mutex_lock(&prg.lockecm); //###
// Check for DCW ANSWER
ECM_DATA *ecm = getecmbyid(cli->ecm.id);
if (ecm->hash!=cli->ecm.hash) cs_senddcw_cli( cli );
//debugf(" cs_check_sendcw: %s:%s ch %04x:%06x:%04x\n", cs->name, cli->user, ecm->caid, ecm->provid, ecm->sid);
// Check for FAILED
if (ecm->dcwstatus==STAT_DCW_FAILED) {
cs_senddcw_cli( cli );
}
// Check for SUCCESS
else if (ecm->dcwstatus==STAT_DCW_SUCCESS) {
// check for client allowed cw time
if ( (cli->ecm.recvtime+cli->ecm.dcwtime)<=ticks )
cs_senddcw_cli( cli );
else
clitime = cli->ecm.recvtime+cli->ecm.dcwtime;
}
// check for timeout
else if ( (cli->ecm.recvtime+cs->dcwtimeout) <= ticks ) {
cs_senddcw_cli( cli );
}
else clitime = cli->ecm.recvtime+cs->dcwtimeout;
if (restime>clitime) restime = clitime;
pthread_mutex_unlock(&prg.lockecm); //###
}
cli = cli->next;
}
}
cs = cs->next;
}
return (restime+1);
}