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