1 | #include "globals.h"
|
---|
2 | #include "reader-common.h"
|
---|
3 |
|
---|
4 | int32_t logfd=0;
|
---|
5 |
|
---|
6 | void reader_do_idle(struct s_reader * reader);
|
---|
7 |
|
---|
8 | void cs_ri_brk(struct s_reader * reader, int32_t flag)
|
---|
9 | {
|
---|
10 | if (flag)
|
---|
11 | reader->brk_pos=reader->init_history_pos;
|
---|
12 | else
|
---|
13 | reader->init_history_pos=reader->brk_pos;
|
---|
14 | }
|
---|
15 |
|
---|
16 | void cs_ri_log(struct s_reader * reader, char *fmt,...)
|
---|
17 | {
|
---|
18 | char txt[256];
|
---|
19 |
|
---|
20 | va_list params;
|
---|
21 | va_start(params, fmt);
|
---|
22 | vsnprintf(txt, sizeof(txt), fmt, params);
|
---|
23 | va_end(params);
|
---|
24 | cs_log("%s", txt);
|
---|
25 |
|
---|
26 | if (cfg.saveinithistory) {
|
---|
27 | int32_t size = reader->init_history_pos+strlen(txt)+2;
|
---|
28 |
|
---|
29 | cs_realloc(&reader->init_history, size, -1);
|
---|
30 |
|
---|
31 | if (!reader->init_history)
|
---|
32 | return;
|
---|
33 |
|
---|
34 | snprintf(reader->init_history+reader->init_history_pos, strlen(txt)+2, "%s\n", txt);
|
---|
35 | reader->init_history_pos+=strlen(txt)+1;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | static void casc_check_dcw(struct s_reader * reader, int32_t idx, int32_t rc, uchar *cw)
|
---|
40 | {
|
---|
41 | int32_t i, pending=0;
|
---|
42 | time_t t = time(NULL);
|
---|
43 | ECM_REQUEST *ecm;
|
---|
44 | struct s_client *cl = reader->client;
|
---|
45 |
|
---|
46 | if(!cl) return;
|
---|
47 |
|
---|
48 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
49 | {
|
---|
50 | ecm = &cl->ecmtask[i];
|
---|
51 | if ((ecm->rc>=10) &&
|
---|
52 | ecm->caid == cl->ecmtask[idx].caid &&
|
---|
53 | (!memcmp(ecm->ecmd5, cl->ecmtask[idx].ecmd5, CS_ECMSTORESIZE)))
|
---|
54 | {
|
---|
55 | if (rc)
|
---|
56 | {
|
---|
57 | ecm->rc=(i==idx) ? 1 : 2;
|
---|
58 | memcpy(ecm->cw, cw, 16);
|
---|
59 | }
|
---|
60 | else
|
---|
61 | ecm->rc=0;
|
---|
62 | write_ecm_answer(reader, ecm);
|
---|
63 | ecm->idx=0;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (ecm->rc>=10 && (t-(uint32_t)ecm->tps.time > ((cfg.ctimeout + 500) / 1000) + 1)) // drop timeouts
|
---|
67 | {
|
---|
68 | ecm->rc=0;
|
---|
69 | #ifdef WITH_LB
|
---|
70 | send_reader_stat(reader, ecm, E_TIMEOUT);
|
---|
71 | #endif
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (ecm->rc >= 10)
|
---|
75 | pending++;
|
---|
76 | }
|
---|
77 | cl->pending=pending;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int32_t casc_recv_timer(struct s_reader * reader, uchar *buf, int32_t l, int32_t msec)
|
---|
81 | {
|
---|
82 | int32_t rc;
|
---|
83 | struct s_client *cl = reader->client;
|
---|
84 |
|
---|
85 | if (!cl || !cl->pfd) return(-1);
|
---|
86 |
|
---|
87 | if (!reader->ph.recv) {
|
---|
88 | cs_log("reader %s: unsupported protocol!", reader->label);
|
---|
89 | return(-1);
|
---|
90 | }
|
---|
91 |
|
---|
92 | struct pollfd pfd;
|
---|
93 | pfd.fd = cl->pfd;
|
---|
94 | pfd.events = POLLIN | POLLPRI;
|
---|
95 |
|
---|
96 | int32_t p_rc = poll(&pfd, 1, msec);
|
---|
97 |
|
---|
98 | rc=0;
|
---|
99 | if (p_rc == 1)
|
---|
100 | if (!(rc=reader->ph.recv(cl, buf, l)))
|
---|
101 | rc=-1;
|
---|
102 |
|
---|
103 | return(rc);
|
---|
104 | }
|
---|
105 |
|
---|
106 | int32_t hostResolve(struct s_reader *rdr){
|
---|
107 | struct s_client *cl = rdr->client;
|
---|
108 |
|
---|
109 | if(!cl) return 0;
|
---|
110 |
|
---|
111 | in_addr_t last_ip = cl->ip;
|
---|
112 | cl->ip = cs_getIPfromHost(rdr->device);
|
---|
113 | cl->udp_sa.sin_addr.s_addr = cl->ip;
|
---|
114 |
|
---|
115 | if (cl->ip != last_ip) {
|
---|
116 | cs_log("%s: resolved ip=%s", rdr->device, cs_inet_ntoa(cl->ip));
|
---|
117 | }
|
---|
118 |
|
---|
119 | return cl->ip?1:0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | void clear_block_delay(struct s_reader *rdr) {
|
---|
123 | rdr->tcp_block_delay = 0;
|
---|
124 | cs_ftime(&rdr->tcp_block_connect_till);
|
---|
125 | }
|
---|
126 |
|
---|
127 | void block_connect(struct s_reader *rdr) {
|
---|
128 | if (!rdr->tcp_block_delay)
|
---|
129 | rdr->tcp_block_delay = 100; //starting blocking time, 100ms
|
---|
130 | rdr->tcp_block_connect_till.time += rdr->tcp_block_delay / 1000;
|
---|
131 | rdr->tcp_block_connect_till.millitm += rdr->tcp_block_delay % 1000;
|
---|
132 | rdr->tcp_block_delay *= 2; //increment timeouts
|
---|
133 | if (rdr->tcp_block_delay >= 60*1000)
|
---|
134 | rdr->tcp_block_delay = 60*1000; //max 1min, todo config
|
---|
135 | cs_debug_mask(D_TRACE, "tcp connect blocking delay for %s set to %d", rdr->label, rdr->tcp_block_delay);
|
---|
136 | }
|
---|
137 |
|
---|
138 | int32_t is_connect_blocked(struct s_reader *rdr) {
|
---|
139 | struct timeb cur_time;
|
---|
140 | cs_ftime(&cur_time);
|
---|
141 | return (rdr->tcp_block_delay && comp_timeb(&cur_time, &rdr->tcp_block_connect_till) < 0);
|
---|
142 | }
|
---|
143 |
|
---|
144 | int32_t network_tcp_connection_open()
|
---|
145 | {
|
---|
146 | struct s_client *cl = cur_client();
|
---|
147 | struct s_reader *rdr = cl->reader;
|
---|
148 | cs_log("connecting to %s", rdr->device);
|
---|
149 |
|
---|
150 | in_addr_t last_ip = cl->ip;
|
---|
151 | if (!hostResolve(rdr))
|
---|
152 | return -1;
|
---|
153 |
|
---|
154 | if (last_ip != cl->ip) //clean blocking delay on ip change:
|
---|
155 | clear_block_delay(rdr);
|
---|
156 |
|
---|
157 | if (is_connect_blocked(rdr)) { //inside of blocking delay, do not connect!
|
---|
158 | cs_log("tcp connect blocking delay asserted for %s", rdr->label);
|
---|
159 | return -1;
|
---|
160 | }
|
---|
161 |
|
---|
162 | //int32_t flag = 1;
|
---|
163 | //setsockopt(cl->udp_fd, IPPROTO_TCP, SO_DEBUG, (char *) &flag, sizeof(int));
|
---|
164 |
|
---|
165 | int32_t sd = cl->udp_fd;
|
---|
166 | int32_t fl = fcntl(sd, F_GETFL);
|
---|
167 | fcntl(sd, F_SETFL, O_NONBLOCK); //set to nonblocking mode to avoid "endless" connecting loops and pipe-overflows:
|
---|
168 |
|
---|
169 | int32_t res = connect(sd, (struct sockaddr *)&cl->udp_sa, sizeof(cl->udp_sa));
|
---|
170 | if (res == 0) {
|
---|
171 | fcntl(sd, F_SETFL, fl); //connect sucessfull, restore blocking mode
|
---|
172 | clear_block_delay(rdr);
|
---|
173 | return sd;
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (errno == EINPROGRESS || errno == EALREADY) {
|
---|
177 | struct pollfd pfd;
|
---|
178 | pfd.fd = cl->udp_fd;
|
---|
179 | pfd.events = POLLOUT;
|
---|
180 | int32_t rc = poll(&pfd, 1, 500);
|
---|
181 | if (rc>0) { //if connect is in progress, wait apr. 500ms
|
---|
182 | int32_t r = -1;
|
---|
183 | uint32_t l = sizeof(r);
|
---|
184 | if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &r, (socklen_t*)&l) == 0) {
|
---|
185 | if (r == 0) {
|
---|
186 | fcntl(sd, F_SETFL, fl);
|
---|
187 | clear_block_delay(rdr);
|
---|
188 | return sd; //now we are connected
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | errno = ETIMEDOUT;
|
---|
193 | }
|
---|
194 | //else we are not connected - or already connected:
|
---|
195 | else if (errno == EISCONN) {
|
---|
196 | cs_log("already connected!");
|
---|
197 | fcntl(sd, F_SETFL, fl);
|
---|
198 | clear_block_delay(rdr);
|
---|
199 | return sd;
|
---|
200 | }
|
---|
201 |
|
---|
202 | cs_log("connect(fd=%d) failed: (errno=%d %s)", sd, errno, strerror(errno));
|
---|
203 |
|
---|
204 | fcntl(sd, F_SETFL, fl); //restore blocking mode
|
---|
205 |
|
---|
206 | //connect has failed. Block connect for a while:
|
---|
207 | block_connect(rdr);
|
---|
208 |
|
---|
209 | return -1;
|
---|
210 | }
|
---|
211 |
|
---|
212 | void network_tcp_connection_close(struct s_client *cl, int32_t fd)
|
---|
213 | {
|
---|
214 | if(!cl) return;
|
---|
215 | struct s_reader *reader = cl->reader;
|
---|
216 | cs_debug_mask(D_READER, "tcp_conn_close(): fd=%d, cl->typ == 'c'=%d", fd, cl->typ == 'c');
|
---|
217 |
|
---|
218 | if (fd) {
|
---|
219 | close(fd);
|
---|
220 | if (fd == cl->udp_fd)
|
---|
221 | cl->udp_fd = 0;
|
---|
222 | if (fd == cl->pfd)
|
---|
223 | cl->pfd = 0;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | if (cl->typ != 'c')
|
---|
228 | {
|
---|
229 | int32_t i;
|
---|
230 | //cl->pfd = 0;
|
---|
231 | if(reader)
|
---|
232 | reader->tcp_connected = 0;
|
---|
233 |
|
---|
234 | if (cl->ecmtask) {
|
---|
235 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
236 | cl->ecmtask[i].idx = 0;
|
---|
237 | cl->ecmtask[i].rc = 0;
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | if(reader) {
|
---|
242 | reader->ncd_msgid=0;
|
---|
243 | reader->last_s=reader->last_g=0;
|
---|
244 |
|
---|
245 | if (reader->ph.c_init(cl)) {
|
---|
246 | cs_debug_mask(D_READER, "network_tcp_connection_close() exit(1);");
|
---|
247 | cs_exit(1);
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | static void casc_do_sock_log(struct s_reader * reader)
|
---|
254 | {
|
---|
255 | int32_t i, idx;
|
---|
256 | uint16_t caid, srvid;
|
---|
257 | uint32_t provid;
|
---|
258 | struct s_client *cl = reader->client;
|
---|
259 |
|
---|
260 | if(!cl) return;
|
---|
261 |
|
---|
262 | idx=reader->ph.c_recv_log(&caid, &provid, &srvid);
|
---|
263 | cl->last=time((time_t)0);
|
---|
264 | if (idx<0) return; // no dcw-msg received
|
---|
265 |
|
---|
266 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
267 | {
|
---|
268 | if ( (cl->ecmtask[i].rc>=10)
|
---|
269 | && (cl->ecmtask[i].idx==idx)
|
---|
270 | && (cl->ecmtask[i].caid==caid)
|
---|
271 | && (cl->ecmtask[i].prid==provid)
|
---|
272 | && (cl->ecmtask[i].srvid==srvid))
|
---|
273 | {
|
---|
274 | casc_check_dcw(reader, i, 0, cl->ecmtask[i].cw); // send "not found"
|
---|
275 | break;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | static void casc_do_sock(struct s_reader * reader, int32_t w)
|
---|
281 | {
|
---|
282 | int32_t i, n, idx, rc, j;
|
---|
283 | uchar buf[1024];
|
---|
284 | uchar dcw[16];
|
---|
285 | struct s_client *cl = reader->client;
|
---|
286 |
|
---|
287 | if(!cl) return;
|
---|
288 |
|
---|
289 | if ((n=casc_recv_timer(reader, buf, sizeof(buf), w))<=0)
|
---|
290 | {
|
---|
291 | if (reader->ph.type==MOD_CONN_TCP && reader->typ != R_RADEGAST)
|
---|
292 | {
|
---|
293 | if (reader->ph.c_idle)
|
---|
294 | reader_do_idle(reader);
|
---|
295 | else {
|
---|
296 | cs_debug_mask(D_READER, "casc_do_sock: closed connection by remote");
|
---|
297 | network_tcp_connection_close(reader->client, cl->udp_fd);
|
---|
298 | }
|
---|
299 | return;
|
---|
300 | }
|
---|
301 | }
|
---|
302 | cl->last=time((time_t)0);
|
---|
303 | idx=reader->ph.c_recv_chk(cl, dcw, &rc, buf, n);
|
---|
304 |
|
---|
305 | if (idx<0) return; // no dcw received
|
---|
306 | reader->last_g=time((time_t*)0); // for reconnect timeout
|
---|
307 | //cs_log("casc_do_sock: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
308 | if (!idx) idx=cl->last_idx;
|
---|
309 | j=0;
|
---|
310 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
311 | {
|
---|
312 | if (cl->ecmtask[i].idx==idx)
|
---|
313 | {
|
---|
314 | cl->pending--;
|
---|
315 | casc_check_dcw(reader, i, rc, dcw);
|
---|
316 | j=1;
|
---|
317 | break;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | static void casc_get_dcw(struct s_reader * reader, int32_t n)
|
---|
323 | {
|
---|
324 | int32_t w;
|
---|
325 | struct timeb tps, tpe;
|
---|
326 | struct s_client *cl = reader->client;
|
---|
327 | if(!cl) return;
|
---|
328 | tpe=cl->ecmtask[n].tps;
|
---|
329 | //tpe.millitm+=1500; // TODO: timeout of 1500 should be config
|
---|
330 |
|
---|
331 | tpe.time += cfg.srtimeout/1000;
|
---|
332 | tpe.millitm += cfg.srtimeout%1000;
|
---|
333 |
|
---|
334 | cs_ftime(&tps);
|
---|
335 | while (((w=1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm)>0)
|
---|
336 | && (cl->ecmtask[n].rc>=10))
|
---|
337 | {
|
---|
338 | casc_do_sock(reader, w);
|
---|
339 | cs_ftime(&tps);
|
---|
340 | }
|
---|
341 | if (cl->ecmtask[n].rc>=10)
|
---|
342 | casc_check_dcw(reader, n, 0, cl->ecmtask[n].cw); // simulate "not found"
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 |
|
---|
347 | int32_t casc_process_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
348 | {
|
---|
349 | int32_t rc, n, i, sflag, pending=0;
|
---|
350 | time_t t;//, tls;
|
---|
351 | struct s_client *cl = reader->client;
|
---|
352 |
|
---|
353 | if(!cl) return -1;
|
---|
354 |
|
---|
355 | uchar buf[512];
|
---|
356 |
|
---|
357 | t=time((time_t *)0);
|
---|
358 | ECM_REQUEST *ecm;
|
---|
359 | for (n=-1, i=0, sflag=1; i<CS_MAXPENDING; i++)
|
---|
360 | {
|
---|
361 | ecm = &cl->ecmtask[i];
|
---|
362 | if ((ecm->rc>=10) && (t-(uint32_t)ecm->tps.time > ((cfg.ctimeout + 500) / 1000) + 1)) // drop timeouts
|
---|
363 | {
|
---|
364 | ecm->rc=0;
|
---|
365 | #ifdef WITH_LB
|
---|
366 | send_reader_stat(reader, ecm, E_TIMEOUT);
|
---|
367 | #endif
|
---|
368 | }
|
---|
369 | if (n<0 && (ecm->rc<10)) // free slot found
|
---|
370 | n=i;
|
---|
371 | if ((ecm->rc>=10) && // ecm already pending
|
---|
372 | er->caid == ecm->caid &&
|
---|
373 | (!memcmp(er->ecmd5, ecm->ecmd5, CS_ECMSTORESIZE)) &&
|
---|
374 | (er->level<=ecm->level)) // ... this level at least
|
---|
375 | sflag=0;
|
---|
376 |
|
---|
377 | if (ecm->rc >=10)
|
---|
378 | pending++;
|
---|
379 | }
|
---|
380 | cl->pending=pending;
|
---|
381 | if (n<0)
|
---|
382 | {
|
---|
383 | cs_log("WARNING: ecm pending table overflow !!");
|
---|
384 | return(-2);
|
---|
385 | }
|
---|
386 | memcpy(&cl->ecmtask[n], er, sizeof(ECM_REQUEST));
|
---|
387 | cl->ecmtask[n].matching_rdr = NULL; //This avoids double free of matching_rdr!
|
---|
388 | if( reader->typ == R_NEWCAMD )
|
---|
389 | cl->ecmtask[n].idx=(reader->ncd_msgid==0)?2:reader->ncd_msgid+1;
|
---|
390 | else {
|
---|
391 | if (!cl->idx)
|
---|
392 | cl->idx = 1;
|
---|
393 | cl->ecmtask[n].idx=cl->idx++;
|
---|
394 | }
|
---|
395 | cl->ecmtask[n].rc=10;
|
---|
396 | cs_debug_mask(D_READER, "---- ecm_task %d, idx %d, sflag=%d, level=%d",
|
---|
397 | n, cl->ecmtask[n].idx, sflag, er->level);
|
---|
398 |
|
---|
399 | if( reader->ph.type==MOD_CONN_TCP && reader->tcp_rto )
|
---|
400 | {
|
---|
401 | int32_t rto = abs(reader->last_s - reader->last_g);
|
---|
402 | if (rto >= (reader->tcp_rto*60))
|
---|
403 | {
|
---|
404 | if (reader->ph.c_idle)
|
---|
405 | reader_do_idle(reader);
|
---|
406 | else {
|
---|
407 | cs_debug_mask(D_READER, "rto=%d", rto);
|
---|
408 | network_tcp_connection_close(reader->client, cl->udp_fd);
|
---|
409 | }
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | cs_ddump_mask(D_ATR, er->ecm, er->l, "casc ecm:");
|
---|
414 | rc=0;
|
---|
415 | if (sflag)
|
---|
416 | {
|
---|
417 | if ((rc=reader->ph.c_send_ecm(cl, &cl->ecmtask[n], buf)))
|
---|
418 | casc_check_dcw(reader, n, 0, cl->ecmtask[n].cw); // simulate "not found"
|
---|
419 | else
|
---|
420 | cl->last_idx = cl->ecmtask[n].idx;
|
---|
421 | reader->last_s = t; // used for inactive_timeout and reconnect_timeout in TCP reader
|
---|
422 |
|
---|
423 | if (!reader->ph.c_multi)
|
---|
424 | casc_get_dcw(reader, n);
|
---|
425 | }
|
---|
426 |
|
---|
427 | //cs_log("casc_process_ecm 1: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
428 |
|
---|
429 | if (cl->idx>0x1ffe) cl->idx=1;
|
---|
430 | return(rc);
|
---|
431 | }
|
---|
432 |
|
---|
433 | static int32_t reader_store_emm(uchar *emm, uchar type)
|
---|
434 | {
|
---|
435 | int32_t rc;
|
---|
436 | unsigned char md5tmp[MD5_DIGEST_LENGTH];
|
---|
437 | struct s_client *cl = cur_client();
|
---|
438 | memcpy(cl->emmcache[cl->rotate].emmd5, MD5(emm, emm[2], md5tmp), CS_EMMSTORESIZE);
|
---|
439 | cl->emmcache[cl->rotate].type=type;
|
---|
440 | cl->emmcache[cl->rotate].count=1;
|
---|
441 | // cs_debug_mask(D_READER, "EMM stored (index %d)", rotate);
|
---|
442 | rc=cl->rotate;
|
---|
443 | cl->rotate=(++cl->rotate < CS_EMMCACHESIZE)?cl->rotate:0;
|
---|
444 | return(rc);
|
---|
445 | }
|
---|
446 |
|
---|
447 | static void reader_get_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
448 | {
|
---|
449 | //cs_log("hallo idx:%d rc:%d caid:%04X",er->idx,er->rc,er->caid);
|
---|
450 | if (er->rc<=E_STOPPED)
|
---|
451 | {
|
---|
452 | send_dcw(reader->client, er);
|
---|
453 | return;
|
---|
454 | }
|
---|
455 |
|
---|
456 | er->ocaid=er->caid;
|
---|
457 | if (!chk_bcaid(er, &reader->ctab))
|
---|
458 | {
|
---|
459 | cs_debug_mask(D_READER, "caid %04X filtered", er->caid);
|
---|
460 | er->rcEx=E2_CAID;
|
---|
461 | er->rc = E_RDR_NOTFOUND;
|
---|
462 | write_ecm_answer(reader, er);
|
---|
463 | return;
|
---|
464 | }
|
---|
465 | // cache2
|
---|
466 | if (check_cwcache2(er, reader->grp))
|
---|
467 | {
|
---|
468 | er->rc = E_CACHE2;
|
---|
469 | write_ecm_answer(reader, er);
|
---|
470 | return;
|
---|
471 | }
|
---|
472 | if (reader->typ & R_IS_CASCADING)
|
---|
473 | {
|
---|
474 | struct s_client *cl = reader->client;
|
---|
475 | if(!cl) return;
|
---|
476 | cl->last_srvid=er->srvid;
|
---|
477 | cl->last_caid=er->caid;
|
---|
478 | casc_process_ecm(reader, er);
|
---|
479 | return;
|
---|
480 | }
|
---|
481 | #ifdef WITH_CARDREADER
|
---|
482 | if (reader->ratelimitecm) {
|
---|
483 | cs_debug_mask(D_READER, "ratelimit idx:%d rc:%d caid:%04X srvid:%04X",er->idx,er->rc,er->caid,er->srvid);
|
---|
484 | int32_t foundspace=-1;
|
---|
485 | int32_t h;
|
---|
486 | for (h=0;h<reader->ratelimitecm;h++) {
|
---|
487 | if (reader->rlecmh[h].srvid == er->srvid) {
|
---|
488 | foundspace=h;
|
---|
489 | cs_debug_mask(D_READER, "ratelimit found srvid in use at pos: %d",h);
|
---|
490 | break;
|
---|
491 | }
|
---|
492 | }
|
---|
493 | if (foundspace<0) {
|
---|
494 | for (h=0;h<reader->ratelimitecm;h++) {
|
---|
495 | if ((reader->rlecmh[h].last ==- 1) || ((time(NULL)-reader->rlecmh[h].last) > reader->ratelimitseconds)) {
|
---|
496 | foundspace=h;
|
---|
497 | cs_debug_mask(D_READER, "ratelimit found space at pos: %d old seconds %d",h,reader->rlecmh[h].last);
|
---|
498 | break;
|
---|
499 | }
|
---|
500 | }
|
---|
501 | }
|
---|
502 | if (foundspace<0) {
|
---|
503 | //drop
|
---|
504 | cs_debug_mask(D_READER, "ratelimit could not find space for srvid %04X. Dropping.",er->srvid);
|
---|
505 | er->rcEx=32;
|
---|
506 | er->rc = E_RDR_NOTFOUND;
|
---|
507 | int32_t clcw;
|
---|
508 | for (clcw=0;clcw<16;clcw++) er->cw[clcw]=(uchar)0;
|
---|
509 | snprintf( er->msglog, MSGLOGSIZE, "ECMratelimit no space for srvid" );
|
---|
510 | write_ecm_answer(reader, er);
|
---|
511 | return;
|
---|
512 | } else {
|
---|
513 | reader->rlecmh[foundspace].last=time(NULL);
|
---|
514 | reader->rlecmh[foundspace].srvid=er->srvid;
|
---|
515 | }
|
---|
516 |
|
---|
517 | }
|
---|
518 | cs_ddump_mask(D_ATR, er->ecm, er->l, "ecm:");
|
---|
519 | er->msglog[0] = 0;
|
---|
520 | struct timeb tps, tpe;
|
---|
521 | cs_ftime(&tps);
|
---|
522 | er->rc=reader_ecm(reader, er);
|
---|
523 | if(er->rc == ERROR){
|
---|
524 | char buf[32];
|
---|
525 | cs_log("Error processing ecm for caid %04X, srvid %04X (servicename: %s) on reader %s.", er->caid, er->srvid, get_servicename(reader->client, er->srvid, er->caid, buf), reader->label);
|
---|
526 | }
|
---|
527 | cs_ftime(&tpe);
|
---|
528 | if (cs_dblevel) {
|
---|
529 | uint16_t lc, *lp;
|
---|
530 | for (lp=(uint16_t *)er->ecm+(er->l>>2), lc=0; lp>=(uint16_t *)er->ecm; lp--)
|
---|
531 | lc^=*lp;
|
---|
532 | cs_debug_mask(D_TRACE, "reader: %s ecm: %04X real time: %d ms", reader->label, lc, 1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm);
|
---|
533 | }
|
---|
534 | write_ecm_answer(reader, er);
|
---|
535 | reader_post_process(reader);
|
---|
536 | #endif
|
---|
537 | }
|
---|
538 |
|
---|
539 | static int32_t reader_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
540 | {
|
---|
541 | int32_t i, no, rc, ecs;
|
---|
542 | unsigned char md5tmp[MD5_DIGEST_LENGTH];
|
---|
543 | char *rtxt[] = { "error", (reader->typ & R_IS_CASCADING) ? "sent" : "written", "skipped", "blocked" };
|
---|
544 | char *typedesc[]= { "unknown", "unique", "shared", "global" };
|
---|
545 | struct timeb tps, tpe;
|
---|
546 | struct s_client *cl = reader->client;
|
---|
547 |
|
---|
548 | if(!cl) return 0;
|
---|
549 |
|
---|
550 | cs_ftime(&tps);
|
---|
551 |
|
---|
552 | MD5(ep->emm, ep->emm[2], md5tmp);
|
---|
553 |
|
---|
554 | no=0;
|
---|
555 | for (i=ecs=0; (i<CS_EMMCACHESIZE) && (!ecs); i++) {
|
---|
556 | if (!memcmp(cl->emmcache[i].emmd5, md5tmp, CS_EMMSTORESIZE)) {
|
---|
557 | if (reader->cachemm)
|
---|
558 | ecs=(reader->rewritemm > cl->emmcache[i].count) ? 1 : 2;
|
---|
559 | else
|
---|
560 | ecs=1;
|
---|
561 | no=++cl->emmcache[i].count;
|
---|
562 | i--;
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | if ((rc=ecs)<2)
|
---|
567 | {
|
---|
568 | if (reader->typ & R_IS_CASCADING) {
|
---|
569 | cs_debug_mask(D_READER, "network emm reader: %s" ,reader->label);
|
---|
570 |
|
---|
571 | if (reader->ph.c_send_emm) {
|
---|
572 | rc=reader->ph.c_send_emm(ep);
|
---|
573 | } else {
|
---|
574 | cs_debug_mask(D_READER, "send_emm() support missing");
|
---|
575 | rc=0;
|
---|
576 | }
|
---|
577 | } else {
|
---|
578 | cs_debug_mask(D_READER, "local emm reader: %s" ,reader->label);
|
---|
579 | #ifdef WITH_CARDREADER
|
---|
580 | rc=reader_emm(reader, ep);
|
---|
581 | #else
|
---|
582 | rc=0;
|
---|
583 | #endif
|
---|
584 | }
|
---|
585 |
|
---|
586 | if (!ecs)
|
---|
587 | {
|
---|
588 | i=reader_store_emm(ep->emm, ep->type);
|
---|
589 | no=1;
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | if (rc) cl->lastemm=time((time_t)0);
|
---|
594 |
|
---|
595 | #ifdef CS_LED
|
---|
596 | if (rc) cs_switch_led(LED3, LED_BLINK_ON);
|
---|
597 | #endif
|
---|
598 |
|
---|
599 | if (reader->logemm & (1 << rc))
|
---|
600 | {
|
---|
601 | cs_ftime(&tpe);
|
---|
602 |
|
---|
603 | cs_log("%s emmtype=%s, len=%d, idx=%d, cnt=%d: %s (%d ms) by %s",
|
---|
604 | username(ep->client), typedesc[cl->emmcache[i].type], ep->emm[2],
|
---|
605 | i, no, rtxt[rc], 1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm, reader->label); //FIXME not sure why emmtyp must come from ep->client and typedesc can be of cur_client
|
---|
606 | }
|
---|
607 |
|
---|
608 | #ifdef WEBIF
|
---|
609 | //counting results
|
---|
610 | switch(rc){
|
---|
611 | case 0:
|
---|
612 | reader->emmerror[ep->type]++;
|
---|
613 | break;
|
---|
614 | case 1:
|
---|
615 | reader->emmwritten[ep->type]++;
|
---|
616 | break;
|
---|
617 | case 2:
|
---|
618 | reader->emmskipped[ep->type]++;
|
---|
619 | break;
|
---|
620 | case 3:
|
---|
621 | reader->emmblocked[ep->type]++;
|
---|
622 | break;
|
---|
623 | }
|
---|
624 | #endif
|
---|
625 |
|
---|
626 | #ifdef QBOXHD_LED
|
---|
627 | if (rc) qboxhd_led_blink(QBOXHD_LED_COLOR_BLUE,QBOXHD_LED_BLINK_MEDIUM);
|
---|
628 | #endif
|
---|
629 |
|
---|
630 |
|
---|
631 | return(rc);
|
---|
632 | }
|
---|
633 |
|
---|
634 | static int32_t reader_listen(struct s_reader * reader, int32_t fd1, int32_t fd2)
|
---|
635 | {
|
---|
636 | int32_t i, tcp_toflag;
|
---|
637 | int32_t is_tcp=(reader->ph.type==MOD_CONN_TCP);
|
---|
638 |
|
---|
639 | tcp_toflag=(fd2 && is_tcp && reader->tcp_ito && reader->tcp_connected);
|
---|
640 |
|
---|
641 | int32_t timeout;
|
---|
642 | if (tcp_toflag)
|
---|
643 | timeout = reader->tcp_ito*60*1000;
|
---|
644 | else
|
---|
645 | timeout = (is_tcp&&!reader->tcp_connected)?cfg.reader_restart_seconds*1000:500;
|
---|
646 |
|
---|
647 |
|
---|
648 | struct pollfd pfd[3];
|
---|
649 |
|
---|
650 | int32_t pfdcount = 0;
|
---|
651 | if (fd1) {
|
---|
652 | pfd[pfdcount].fd = fd1;
|
---|
653 | pfd[pfdcount++].events = POLLIN | POLLPRI;
|
---|
654 | }
|
---|
655 | if (fd2) {
|
---|
656 | pfd[pfdcount].fd = fd2;
|
---|
657 | pfd[pfdcount++].events = POLLIN | POLLPRI;
|
---|
658 | }
|
---|
659 | if (logfd) {
|
---|
660 | pfd[pfdcount].fd = logfd;
|
---|
661 | pfd[pfdcount++].events = POLLIN | POLLPRI;
|
---|
662 | }
|
---|
663 |
|
---|
664 | int32_t rc = poll(pfd, pfdcount, timeout);
|
---|
665 |
|
---|
666 | if (rc>0) {
|
---|
667 | for (i=0; i<pfdcount; i++) {
|
---|
668 | if (!(pfd[i].revents & (POLLIN | POLLPRI)))
|
---|
669 | continue;
|
---|
670 |
|
---|
671 | if (pfd[i].fd == logfd) {
|
---|
672 | cs_debug_mask(D_READER, "select: log-socket ist set");
|
---|
673 | return(3);
|
---|
674 | }
|
---|
675 |
|
---|
676 | if (pfd[i].fd == fd2) {
|
---|
677 | cs_debug_mask(D_READER, "select: socket is set");
|
---|
678 | return(2);
|
---|
679 | }
|
---|
680 |
|
---|
681 | if (pfd[i].fd == fd1) {
|
---|
682 | if (tcp_toflag) {
|
---|
683 | time_t now;
|
---|
684 | int32_t time_diff;
|
---|
685 | time(&now);
|
---|
686 | time_diff = abs(now-reader->last_s);
|
---|
687 | if (time_diff>(reader->tcp_ito*60)) {
|
---|
688 | if (reader->ph.c_idle)
|
---|
689 | reader_do_idle(reader);
|
---|
690 | else {
|
---|
691 | cs_debug_mask(D_READER, "%s inactive_timeout (%d), close connection (fd=%d)", reader->ph.desc, time_diff, fd2);
|
---|
692 | network_tcp_connection_close(reader->client, fd2);
|
---|
693 | }
|
---|
694 | }
|
---|
695 | }
|
---|
696 | cs_debug_mask(D_READER, "select: pipe is set");
|
---|
697 | return(1);
|
---|
698 | }
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | if (tcp_toflag) {
|
---|
703 | if (reader->ph.c_idle)
|
---|
704 | reader_do_idle(reader);
|
---|
705 | else {
|
---|
706 | cs_debug_mask(D_READER, "%s inactive_timeout (%d), close connection (fd=%d)", reader->ph.desc, timeout/1000, fd2);
|
---|
707 | network_tcp_connection_close(reader->client, fd2);
|
---|
708 | }
|
---|
709 | return(0);
|
---|
710 | }
|
---|
711 |
|
---|
712 | #ifdef WITH_CARDREADER
|
---|
713 | if (!(reader->typ & R_IS_CASCADING)) reader_checkhealth(reader);
|
---|
714 | #endif
|
---|
715 | return(0);
|
---|
716 | }
|
---|
717 |
|
---|
718 | void reader_do_card_info(struct s_reader * reader)
|
---|
719 | {
|
---|
720 | #ifdef WITH_CARDREADER
|
---|
721 | reader_card_info(reader);
|
---|
722 | #endif
|
---|
723 | if (reader->ph.c_card_info)
|
---|
724 | reader->ph.c_card_info();
|
---|
725 | }
|
---|
726 |
|
---|
727 | static void reader_do_pipe(struct s_reader * reader)
|
---|
728 | {
|
---|
729 | uchar *ptr;
|
---|
730 | struct s_client *cl = reader->client;
|
---|
731 | if(cl){
|
---|
732 | int32_t fd_m2c_c = cl->fd_m2c_c;
|
---|
733 | if(fd_m2c_c){
|
---|
734 | int32_t pipeCmd = read_from_pipe(fd_m2c_c, &ptr);
|
---|
735 |
|
---|
736 | switch(pipeCmd)
|
---|
737 | {
|
---|
738 | case PIP_ID_ECM:
|
---|
739 | reader_get_ecm(reader, (ECM_REQUEST *)ptr);
|
---|
740 | break;
|
---|
741 | case PIP_ID_EMM:
|
---|
742 | reader_do_emm(reader, (EMM_PACKET *)ptr);
|
---|
743 | break;
|
---|
744 | case PIP_ID_CIN:
|
---|
745 | reader_do_card_info(reader);
|
---|
746 | break;
|
---|
747 | case PIP_ID_ERR:
|
---|
748 | cs_exit(1);
|
---|
749 | break;
|
---|
750 | default:
|
---|
751 | cs_log("unhandled pipe message %d (reader %s)", pipeCmd, reader->label);
|
---|
752 | break;
|
---|
753 | }
|
---|
754 | if (ptr) free(ptr);
|
---|
755 | }
|
---|
756 | }
|
---|
757 | }
|
---|
758 |
|
---|
759 | void reader_do_idle(struct s_reader * reader)
|
---|
760 | {
|
---|
761 | if (reader->ph.c_idle)
|
---|
762 | reader->ph.c_idle();
|
---|
763 | }
|
---|
764 |
|
---|
765 | static void reader_main(struct s_reader * reader)
|
---|
766 | {
|
---|
767 | while (1)
|
---|
768 | {
|
---|
769 | struct s_client *cl = reader->client;
|
---|
770 | switch(reader_listen(reader, cl->fd_m2c_c, cl->pfd))
|
---|
771 | {
|
---|
772 | case 0: reader_do_idle(reader); break;
|
---|
773 | case 1: reader_do_pipe(reader) ; break;
|
---|
774 | case 2: casc_do_sock(reader, 0) ; break;
|
---|
775 | case 3: casc_do_sock_log(reader); break;
|
---|
776 | }
|
---|
777 | }
|
---|
778 | }
|
---|
779 |
|
---|
780 | #pragma GCC diagnostic ignored "-Wempty-body"
|
---|
781 | void * start_cardreader(void * rdr)
|
---|
782 | {
|
---|
783 | struct s_reader * reader = (struct s_reader *) rdr;
|
---|
784 | struct s_client * client = reader->client;
|
---|
785 | if(!client) cs_exit(1);
|
---|
786 | #ifndef NO_PTHREAD_CLEANUP_PUSH
|
---|
787 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
---|
788 | pthread_cleanup_push(cleanup_thread, (void *)client);
|
---|
789 | #endif
|
---|
790 |
|
---|
791 | client->thread=pthread_self();
|
---|
792 | pthread_setspecific(getclient, client);
|
---|
793 |
|
---|
794 | if (reader->typ & R_IS_CASCADING)
|
---|
795 | {
|
---|
796 | client->typ='p';
|
---|
797 | client->port=reader->r_port;
|
---|
798 | cs_log("proxy thread started (thread=%8X, label=%s, server=%s)",pthread_self(), reader->label, reader->device);
|
---|
799 |
|
---|
800 | if (!(reader->ph.c_init)) {
|
---|
801 | cs_log("FATAL: %s-protocol not supporting cascading", reader->ph.desc);
|
---|
802 | cs_sleepms(1000);
|
---|
803 | cs_exit(1);
|
---|
804 | }
|
---|
805 |
|
---|
806 | if (reader->ph.c_init(client)) {
|
---|
807 | //proxy reader start failed
|
---|
808 | cs_exit(1);
|
---|
809 | }
|
---|
810 |
|
---|
811 | if ((reader->log_port) && (reader->ph.c_init_log))
|
---|
812 | reader->ph.c_init_log();
|
---|
813 | }
|
---|
814 | #ifdef WITH_CARDREADER
|
---|
815 | else
|
---|
816 | {
|
---|
817 | client->ip=cs_inet_addr("127.0.0.1");
|
---|
818 | cs_log("reader thread started (thread=%8X, label=%s, device=%s, detect=%s%s, mhz=%d, cardmhz=%d)", pthread_self(), reader->label,
|
---|
819 | reader->device, reader->detect&0x80 ? "!" : "",RDR_CD_TXT[reader->detect&0x7f], reader->mhz,reader->cardmhz);
|
---|
820 | while (reader_device_init(reader)==2)
|
---|
821 | cs_sleepms(60000); // wait 60 secs and try again
|
---|
822 | }
|
---|
823 |
|
---|
824 | #endif
|
---|
825 | cs_malloc(&client->emmcache,CS_EMMCACHESIZE*(sizeof(struct s_emm)), 1);
|
---|
826 | cs_malloc(&client->ecmtask,CS_MAXPENDING*(sizeof(ECM_REQUEST)), 1);
|
---|
827 |
|
---|
828 | reader_main(reader);
|
---|
829 | #ifndef NO_PTHREAD_CLEANUP_PUSH
|
---|
830 | pthread_cleanup_pop(1);
|
---|
831 | #else
|
---|
832 | cs_exit(0);
|
---|
833 | #endif
|
---|
834 | return NULL; //dummy to prevent compiler error
|
---|
835 | }
|
---|
836 | #pragma GCC diagnostic warning "-Wempty-body"
|
---|
837 |
|
---|