1 | #include "globals.h"
|
---|
2 |
|
---|
3 | int logfd=0;
|
---|
4 | extern struct s_reader * reader;
|
---|
5 |
|
---|
6 | static int proxy;
|
---|
7 | static struct s_emm *emmcache;
|
---|
8 | static int last_idx=1;
|
---|
9 | static ushort idx=1;
|
---|
10 |
|
---|
11 | void reader_do_idle(struct s_reader * reader);
|
---|
12 |
|
---|
13 | void cs_ri_brk(struct s_reader * reader, int flag)
|
---|
14 | {
|
---|
15 | static int brk_pos=0;
|
---|
16 | if (flag)
|
---|
17 | brk_pos=reader->init_history_pos;
|
---|
18 | else
|
---|
19 | reader->init_history_pos=brk_pos;
|
---|
20 | }
|
---|
21 |
|
---|
22 | void cs_ri_log(struct s_reader * reader, char *fmt,...)
|
---|
23 | {
|
---|
24 | char txt[256];
|
---|
25 |
|
---|
26 | va_list params;
|
---|
27 | va_start(params, fmt);
|
---|
28 | vsprintf(txt, fmt, params);
|
---|
29 | va_end(params);
|
---|
30 | cs_log("%s", txt);
|
---|
31 | #ifdef CS_RDR_INIT_HIST
|
---|
32 | int val;
|
---|
33 | val=sizeof(reader->init_history)-reader->init_history_pos-1;
|
---|
34 | if (val>0)
|
---|
35 | snprintf((char *) reader->init_history+reader->init_history_pos, val, "%s", txt);
|
---|
36 | #endif
|
---|
37 | if (cfg->saveinithistory) {
|
---|
38 | FILE *fp;
|
---|
39 | char filename[32];
|
---|
40 | char *buffer;
|
---|
41 | sprintf(filename, "%s/reader%d", get_tmp_dir(), reader->ridx);
|
---|
42 | int size = reader->init_history_pos+strlen(txt)+1;
|
---|
43 | buffer = malloc(size+1);
|
---|
44 |
|
---|
45 | if (buffer == NULL)
|
---|
46 | return;
|
---|
47 |
|
---|
48 | memset(buffer, 32, size);
|
---|
49 |
|
---|
50 | fp = fopen(filename, "r");
|
---|
51 |
|
---|
52 | if (fp) {
|
---|
53 | fread(buffer, 1, reader->init_history_pos, fp);
|
---|
54 | fclose(fp);
|
---|
55 | }
|
---|
56 |
|
---|
57 | sprintf(buffer+reader->init_history_pos, "%s\n", txt);
|
---|
58 |
|
---|
59 | fp = fopen(filename, "w");
|
---|
60 | fwrite(buffer, 1, reader->init_history_pos+strlen(txt)+1, fp);
|
---|
61 | fclose(fp);
|
---|
62 |
|
---|
63 | free(buffer);
|
---|
64 | }
|
---|
65 | reader->init_history_pos+=strlen(txt)+1;
|
---|
66 | }
|
---|
67 |
|
---|
68 | static void casc_check_dcw(struct s_reader * reader, int idx, int rc, uchar *cw)
|
---|
69 | {
|
---|
70 | int i;
|
---|
71 | for (i=1; i<CS_MAXPENDING; i++)
|
---|
72 | {
|
---|
73 | if ((ecmtask[i].rc>=10) &&
|
---|
74 | (!memcmp(ecmtask[i].ecmd5, ecmtask[idx].ecmd5, CS_ECMSTORESIZE)))
|
---|
75 | {
|
---|
76 | if (rc)
|
---|
77 | {
|
---|
78 | ecmtask[i].rc=(i==idx) ? 1 : 2;
|
---|
79 | #ifdef CS_WITH_GBOX
|
---|
80 | if(ecmtask[i].gbxRidx)ecmtask[i].rc=0;
|
---|
81 | #endif
|
---|
82 | memcpy(ecmtask[i].cw, cw, 16);
|
---|
83 | }
|
---|
84 | else
|
---|
85 | ecmtask[i].rc=0;
|
---|
86 | write_ecm_answer(reader, fd_c2m, &ecmtask[i]);
|
---|
87 | ecmtask[i].idx=0;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | static int casc_recv_timer(struct s_reader * reader, uchar *buf, int l, int msec)
|
---|
93 | {
|
---|
94 | struct timeval tv;
|
---|
95 | fd_set fds;
|
---|
96 | int rc;
|
---|
97 |
|
---|
98 | if (!pfd) return(-1);
|
---|
99 | tv.tv_sec = msec/1000;
|
---|
100 | tv.tv_usec = (msec%1000)*1000;
|
---|
101 | FD_ZERO(&fds);
|
---|
102 | FD_SET(pfd, &fds);
|
---|
103 | select(pfd+1, &fds, 0, 0, &tv);
|
---|
104 | rc=0;
|
---|
105 | if (FD_ISSET(pfd, &fds))
|
---|
106 | if (!(rc=reader->ph.recv(buf, l)))
|
---|
107 | rc=-1;
|
---|
108 |
|
---|
109 | return(rc);
|
---|
110 | }
|
---|
111 |
|
---|
112 | #define MSTIMEOUT 0x800000
|
---|
113 | #define DEFAULT_CONNECT_TIMEOUT 500
|
---|
114 |
|
---|
115 | int network_select(int forRead, int timeout)
|
---|
116 | {
|
---|
117 | int sd = client[cs_idx].udp_fd;
|
---|
118 | if(sd>=0) {
|
---|
119 | fd_set fds;
|
---|
120 | FD_ZERO(&fds); FD_SET(sd,&fds);
|
---|
121 | struct timeval tv;
|
---|
122 | if(timeout&MSTIMEOUT) { tv.tv_sec=0; tv.tv_usec=(timeout&~MSTIMEOUT)*1000; }
|
---|
123 | else { tv.tv_sec=timeout; tv.tv_usec=0; }
|
---|
124 | int r=select(sd+1,forRead ? &fds:0,forRead ? 0:&fds,0,&tv);
|
---|
125 | if(r>0) return 1;
|
---|
126 | else if(r<0) {
|
---|
127 | cs_debug("socket: select failed: %s",strerror(errno));
|
---|
128 | return -1;
|
---|
129 | }
|
---|
130 | else {
|
---|
131 | if(timeout>0)
|
---|
132 | cs_debug("socket: select timed out (%d %s)",timeout&~MSTIMEOUT,(timeout&MSTIMEOUT)?"ms":"secs");
|
---|
133 | errno=ETIMEDOUT;
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | return -1;
|
---|
138 | }
|
---|
139 |
|
---|
140 | // according to documentation getaddrinfo() is thread safe
|
---|
141 | int hostResolve(int ridx)
|
---|
142 | {
|
---|
143 | struct addrinfo hints, *res = NULL;
|
---|
144 |
|
---|
145 | int cs_idx = reader[ridx].cs_idx;
|
---|
146 |
|
---|
147 | memset(&hints, 0, sizeof(hints));
|
---|
148 | hints.ai_socktype = SOCK_STREAM;
|
---|
149 | hints.ai_family = client[cs_idx].udp_sa.sin_family;
|
---|
150 | hints.ai_protocol = IPPROTO_TCP;
|
---|
151 |
|
---|
152 | pthread_mutex_lock(&gethostbyname_lock);
|
---|
153 | int err = getaddrinfo(reader[ridx].device, NULL, &hints, &res);
|
---|
154 | if (err != 0 || !res || !res->ai_addr) {
|
---|
155 | client[cs_idx].udp_sa.sin_addr.s_addr = 0;
|
---|
156 | cs_log("can't resolve %s, error: %s", reader[ridx].device, err ? gai_strerror(err) : "unknown");
|
---|
157 | }
|
---|
158 | else {
|
---|
159 | in_addr_t last_ip = client[cs_idx].ip;
|
---|
160 | client[cs_idx].udp_sa.sin_addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
|
---|
161 | client[cs_idx].ip = cs_inet_order(client[cs_idx].udp_sa.sin_addr.s_addr);
|
---|
162 | if (client[cs_idx].ip != last_ip) {
|
---|
163 | uchar *ip = (uchar*) &client[cs_idx].ip;
|
---|
164 | cs_log("%s: resolved ip=%d.%d.%d.%d", reader[ridx].device, ip[3], ip[2], ip[1], ip[0]);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | if (res) freeaddrinfo(res);
|
---|
168 | pthread_mutex_unlock(&gethostbyname_lock);
|
---|
169 | return (client[cs_idx].udp_sa.sin_addr.s_addr) ? 1 : 0;
|
---|
170 | }
|
---|
171 |
|
---|
172 | int network_tcp_connection_open()
|
---|
173 | {
|
---|
174 | cs_log("connecting to %s", reader[ridx].device);
|
---|
175 |
|
---|
176 | if (!hostResolve(ridx))
|
---|
177 | return -1;
|
---|
178 |
|
---|
179 | int sd = client[cs_idx].udp_fd;
|
---|
180 | if (connect(sd, (struct sockaddr *)&client[cs_idx].udp_sa, sizeof(client[cs_idx].udp_sa)) == 0)
|
---|
181 | return sd;
|
---|
182 |
|
---|
183 | if (errno == EINPROGRESS || errno == EALREADY) {
|
---|
184 | if (network_select(0, DEFAULT_CONNECT_TIMEOUT) > 0) {
|
---|
185 | int r = -1;
|
---|
186 | uint l = sizeof(r);
|
---|
187 | if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &r, (socklen_t*)&l) == 0) {
|
---|
188 | if (r == 0) return sd;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | else if (errno == EBADF || errno == ENOTSOCK) {
|
---|
193 | cs_log("connect failed: bad socket/descriptor %d", sd);
|
---|
194 | return -1;
|
---|
195 | }
|
---|
196 | else if (errno == EISCONN) {
|
---|
197 | cs_log("already connected!");
|
---|
198 | return sd;
|
---|
199 | }
|
---|
200 | else if (errno == ETIMEDOUT) {
|
---|
201 | cs_log("connect failed: timeout");
|
---|
202 | return -1;
|
---|
203 | }
|
---|
204 | else if (errno == ECONNREFUSED) {
|
---|
205 | cs_log("connection refused");
|
---|
206 | return -1;
|
---|
207 | }
|
---|
208 | else if (errno == ENETUNREACH) {
|
---|
209 | cs_log("connect failed: network unreachable!");
|
---|
210 | return -1;
|
---|
211 | }
|
---|
212 | else if (errno == EADDRINUSE) {
|
---|
213 | cs_log("connect failed: address in use!");
|
---|
214 | return -1;
|
---|
215 | }
|
---|
216 |
|
---|
217 | cs_log("connect(fd=%d) failed: (errno=%d)", sd, errno);
|
---|
218 | return -1;
|
---|
219 | }
|
---|
220 |
|
---|
221 | void network_tcp_connection_close(struct s_reader * reader, int fd)
|
---|
222 | {
|
---|
223 | cs_debug("tcp_conn_close(): fd=%d, is_server=%d", fd, is_server);
|
---|
224 | close(fd);
|
---|
225 | client[cs_idx].udp_fd = 0;
|
---|
226 |
|
---|
227 | if (!is_server)
|
---|
228 | {
|
---|
229 | int i;
|
---|
230 | pfd = 0;
|
---|
231 | reader->tcp_connected = 0;
|
---|
232 |
|
---|
233 | if (ecmtask) {
|
---|
234 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
235 | ecmtask[i].idx = 0;
|
---|
236 | ecmtask[i].rc = 0;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | reader->ncd_msgid=0;
|
---|
241 | reader->last_s=reader->last_g=0;
|
---|
242 |
|
---|
243 | /* FIXME: this changes from r2318 only for CCcam */
|
---|
244 | if (reader->typ == R_CCCAM) {
|
---|
245 | if (reader->ph.cleanup) reader->ph.cleanup();
|
---|
246 | if (client[cs_idx].typ == 'p') return;
|
---|
247 | }
|
---|
248 | /* END */
|
---|
249 |
|
---|
250 | if (reader->ph.c_init()) {
|
---|
251 | cs_debug("network_tcp_connection_close() exit(1);");
|
---|
252 |
|
---|
253 | if (reader->ph.cleanup)
|
---|
254 | reader->ph.cleanup();
|
---|
255 |
|
---|
256 | cs_exit(1);
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | static void casc_do_sock_log(struct s_reader * reader)
|
---|
262 | {
|
---|
263 | int i, idx;
|
---|
264 | ushort caid, srvid;
|
---|
265 | ulong provid;
|
---|
266 |
|
---|
267 | idx=reader->ph.c_recv_log(&caid, &provid, &srvid);
|
---|
268 | client[cs_idx].last=time((time_t)0);
|
---|
269 | if (idx<0) return; // no dcw-msg received
|
---|
270 |
|
---|
271 | for (i=1; i<CS_MAXPENDING; i++)
|
---|
272 | {
|
---|
273 | if ( (ecmtask[i].rc>=10)
|
---|
274 | && (ecmtask[i].idx==idx)
|
---|
275 | && (ecmtask[i].caid==caid)
|
---|
276 | && (ecmtask[i].prid==provid)
|
---|
277 | && (ecmtask[i].srvid==srvid))
|
---|
278 | {
|
---|
279 | casc_check_dcw(reader, i, 0, ecmtask[i].cw); // send "not found"
|
---|
280 | break;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | static void casc_do_sock(struct s_reader * reader, int w)
|
---|
286 | {
|
---|
287 | int i, n, idx, rc, j;
|
---|
288 | uchar buf[1024];
|
---|
289 | uchar dcw[16];
|
---|
290 |
|
---|
291 | if ((n=casc_recv_timer(reader, buf, sizeof(buf), w))<=0)
|
---|
292 | {
|
---|
293 | if (reader->ph.type==MOD_CONN_TCP && reader->typ != R_RADEGAST)
|
---|
294 | {
|
---|
295 | if (reader->ph.c_idle)
|
---|
296 | reader_do_idle(reader);
|
---|
297 | else {
|
---|
298 | cs_debug("casc_do_sock: close connection");
|
---|
299 | network_tcp_connection_close(reader, client[cs_idx].udp_fd);
|
---|
300 | }
|
---|
301 | }
|
---|
302 | return;
|
---|
303 | }
|
---|
304 | client[cs_idx].last=time((time_t)0);
|
---|
305 | idx=reader->ph.c_recv_chk(dcw, &rc, buf, n);
|
---|
306 |
|
---|
307 | if (idx<0) return; // no dcw received
|
---|
308 | reader->last_g=time((time_t*)0); // for reconnect timeout
|
---|
309 | //cs_log("casc_do_sock: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
310 | if (!idx) idx=last_idx;
|
---|
311 | j=0;
|
---|
312 | for (i=1; i<CS_MAXPENDING; i++)
|
---|
313 | {
|
---|
314 |
|
---|
315 | if (ecmtask[i].idx==idx)
|
---|
316 | {
|
---|
317 | casc_check_dcw(reader, i, rc, dcw);
|
---|
318 | j=1;
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | static void casc_get_dcw(struct s_reader * reader, int n)
|
---|
325 | {
|
---|
326 | int w;
|
---|
327 | struct timeb tps, tpe;
|
---|
328 | tpe=ecmtask[n].tps;
|
---|
329 | //tpe.millitm+=1500; // TODO: timeout of 1500 should be config
|
---|
330 | tpe.millitm+=cfg->srtimeout;
|
---|
331 | tpe.time+=(tpe.millitm/1000);
|
---|
332 | tpe.millitm%=1000;
|
---|
333 |
|
---|
334 | cs_ftime(&tps);
|
---|
335 | while (((w=1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm)>0)
|
---|
336 | && (ecmtask[n].rc>=10))
|
---|
337 | {
|
---|
338 | casc_do_sock(reader, w);
|
---|
339 | cs_ftime(&tps);
|
---|
340 | }
|
---|
341 | if (ecmtask[n].rc>=10)
|
---|
342 | casc_check_dcw(reader, n, 0, ecmtask[n].cw); // simulate "not found"
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 |
|
---|
347 | int casc_process_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
348 | {
|
---|
349 | int rc, n, i, sflag;
|
---|
350 | time_t t;//, tls;
|
---|
351 |
|
---|
352 | uchar buf[512];
|
---|
353 |
|
---|
354 | t=time((time_t *)0);
|
---|
355 | for (n=0, i=sflag=1; i<CS_MAXPENDING; i++)
|
---|
356 | {
|
---|
357 | if ((t-(ulong)ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
358 | (ecmtask[i].rc>=10)) // drop timeouts
|
---|
359 | {
|
---|
360 | ecmtask[i].rc=0;
|
---|
361 | }
|
---|
362 | if ((!n) && (ecmtask[i].rc<10)) // free slot found
|
---|
363 | n=i;
|
---|
364 | if ((ecmtask[i].rc>=10) && // ecm already pending
|
---|
365 | (!memcmp(er->ecmd5, ecmtask[i].ecmd5, CS_ECMSTORESIZE)) &&
|
---|
366 | (er->level<=ecmtask[i].level)) // ... this level at least
|
---|
367 | sflag=0;
|
---|
368 | }
|
---|
369 | if (!n)
|
---|
370 | {
|
---|
371 | cs_log("WARNING: ecm pending table overflow !!");
|
---|
372 | return(-2);
|
---|
373 | }
|
---|
374 | memcpy(&ecmtask[n], er, sizeof(ECM_REQUEST));
|
---|
375 | if( reader->typ == R_NEWCAMD )
|
---|
376 | ecmtask[n].idx=(reader->ncd_msgid==0)?2:reader->ncd_msgid+1;
|
---|
377 | else
|
---|
378 | ecmtask[n].idx=idx++;
|
---|
379 | ecmtask[n].rc=10;
|
---|
380 | cs_debug("---- ecm_task %d, idx %d, sflag=%d, level=%d",
|
---|
381 | n, ecmtask[n].idx, sflag, er->level);
|
---|
382 |
|
---|
383 | if( reader->ph.type==MOD_CONN_TCP && reader->tcp_rto )
|
---|
384 | {
|
---|
385 | int rto = abs(reader->last_s - reader->last_g);
|
---|
386 | if (rto >= (reader->tcp_rto*60))
|
---|
387 | {
|
---|
388 | if (reader->ph.c_idle)
|
---|
389 | reader_do_idle(reader);
|
---|
390 | else {
|
---|
391 | cs_debug("rto=%d", rto);
|
---|
392 | network_tcp_connection_close(reader, client[cs_idx].udp_fd);
|
---|
393 | }
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | cs_ddump_mask(D_ATR, er->ecm, er->l, "casc ecm:");
|
---|
398 | rc=0;
|
---|
399 | if (sflag)
|
---|
400 | {
|
---|
401 | if ((rc=reader->ph.c_send_ecm(&ecmtask[n], buf)))
|
---|
402 | casc_check_dcw(reader, n, 0, ecmtask[n].cw); // simulate "not found"
|
---|
403 | else
|
---|
404 | last_idx = ecmtask[n].idx;
|
---|
405 | reader->last_s = t; // used for inactive_timeout and reconnect_timeout in TCP reader
|
---|
406 |
|
---|
407 | if (!reader->ph.c_multi)
|
---|
408 | casc_get_dcw(reader, n);
|
---|
409 | }
|
---|
410 |
|
---|
411 | //cs_log("casc_process_ecm 1: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
412 |
|
---|
413 | if (idx>0x1ffe) idx=1;
|
---|
414 | return(rc);
|
---|
415 | }
|
---|
416 |
|
---|
417 | static int reader_store_emm(uchar *emm, uchar type)
|
---|
418 | {
|
---|
419 | static int rotate=0;
|
---|
420 | int rc;
|
---|
421 | memcpy(emmcache[rotate].emm, emm, emm[2]);
|
---|
422 | emmcache[rotate].type=type;
|
---|
423 | emmcache[rotate].count=1;
|
---|
424 | // cs_debug("EMM stored (index %d)", rotate);
|
---|
425 | rc=rotate;
|
---|
426 | rotate=(rotate+1) % CS_EMMCACHESIZE;
|
---|
427 | return(rc);
|
---|
428 | }
|
---|
429 |
|
---|
430 | static void reader_get_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
431 | {
|
---|
432 | //cs_log("hallo idx:%d rc:%d caid:%04X",er->idx,er->rc,er->caid);
|
---|
433 | if ((er->rc<10) )
|
---|
434 | {
|
---|
435 | send_dcw(er);
|
---|
436 | return;
|
---|
437 | }
|
---|
438 | er->ocaid=er->caid;
|
---|
439 | if (!chk_bcaid(er, &reader->ctab))
|
---|
440 | {
|
---|
441 | cs_debug("caid %04X filtered", er->caid);
|
---|
442 | er->rcEx=E2_CAID;
|
---|
443 | er->rc=0;
|
---|
444 | write_ecm_answer(reader, fd_c2m, er);
|
---|
445 | return;
|
---|
446 | }
|
---|
447 | // cache2
|
---|
448 | if (check_ecmcache2(er, client[er->cidx].grp))
|
---|
449 | {
|
---|
450 | er->rc=2;
|
---|
451 | write_ecm_answer(reader, fd_c2m, er);
|
---|
452 | return;
|
---|
453 | }
|
---|
454 | if (proxy)
|
---|
455 | {
|
---|
456 | client[cs_idx].last_srvid=er->srvid;
|
---|
457 | client[cs_idx].last_caid=er->caid;
|
---|
458 | casc_process_ecm(reader, er);
|
---|
459 | return;
|
---|
460 | }
|
---|
461 | cs_ddump_mask(D_ATR, er->ecm, er->l, "ecm:");
|
---|
462 | er->rc=reader_ecm(reader, er);
|
---|
463 | write_ecm_answer(reader, fd_c2m, er);
|
---|
464 | reader_post_process(reader);
|
---|
465 | //fixme re-activated code for testing
|
---|
466 | if(reader->typ=='r') reader->qlen--;
|
---|
467 | //printf("queue: %d\n",reader->qlen);
|
---|
468 | }
|
---|
469 |
|
---|
470 | static void reader_send_DCW(ECM_REQUEST *er)
|
---|
471 | {
|
---|
472 | if ((er->rc<10) )
|
---|
473 | {
|
---|
474 | send_dcw(er);
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | static int reader_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
479 | {
|
---|
480 | int i, no, rc, ecs;
|
---|
481 | char *rtxt[] = { "error", proxy ? "sent" : "written", "skipped", "blocked" };
|
---|
482 | char *typedesc[]= { "unknown", "unique", "shared", "global" };
|
---|
483 | struct timeb tps, tpe;
|
---|
484 |
|
---|
485 | cs_ftime(&tps);
|
---|
486 |
|
---|
487 | no=0;
|
---|
488 | for (i=ecs=0; (i<CS_EMMCACHESIZE) && (!ecs); i++)
|
---|
489 | if (!memcmp(emmcache[i].emm, ep->emm, ep->emm[2]))
|
---|
490 | {
|
---|
491 | if (reader->cachemm)
|
---|
492 | ecs=(reader->rewritemm > emmcache[i].count) ? 1 : 2;
|
---|
493 | else
|
---|
494 | ecs=1;
|
---|
495 | no=++emmcache[i].count;
|
---|
496 | i--;
|
---|
497 | }
|
---|
498 |
|
---|
499 | if ((rc=ecs)<2)
|
---|
500 | {
|
---|
501 | if (proxy) {
|
---|
502 | cs_debug("network emm reader: %s" ,reader->label);
|
---|
503 |
|
---|
504 | if (reader->ph.c_send_emm) {
|
---|
505 | rc=reader->ph.c_send_emm(ep);
|
---|
506 | } else {
|
---|
507 | cs_debug("send_emm() support missing");
|
---|
508 | rc=0;
|
---|
509 | }
|
---|
510 | } else {
|
---|
511 | cs_debug("local emm reader: %s" ,reader->label);
|
---|
512 | rc=reader_emm(reader, ep);
|
---|
513 | }
|
---|
514 |
|
---|
515 | if (!ecs)
|
---|
516 | {
|
---|
517 | i=reader_store_emm(ep->emm, ep->type);
|
---|
518 | no=1;
|
---|
519 | }
|
---|
520 | }
|
---|
521 |
|
---|
522 | if (rc) client[cs_idx].lastemm=time((time_t)0);
|
---|
523 |
|
---|
524 | #ifdef CS_LED
|
---|
525 | if (rc) cs_switch_led(LED3, LED_BLINK_ON);
|
---|
526 | #endif
|
---|
527 |
|
---|
528 | if (reader->logemm & (1 << rc))
|
---|
529 | {
|
---|
530 | cs_ftime(&tpe);
|
---|
531 |
|
---|
532 | cs_log("%s emmtype=%s, len=%d, idx=%d, cnt=%d: %s (%d ms) by %s",
|
---|
533 | username(ep->cidx), typedesc[emmcache[i].type], ep->emm[2],
|
---|
534 | i, no, rtxt[rc], 1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm, reader->label);
|
---|
535 | }
|
---|
536 |
|
---|
537 | #ifdef WEBIF
|
---|
538 | //counting results
|
---|
539 | switch(rc){
|
---|
540 | case 0:
|
---|
541 | reader->emmerror[ep->type]++;
|
---|
542 | break;
|
---|
543 | case 1:
|
---|
544 | reader->emmwritten[ep->type]++;
|
---|
545 | break;
|
---|
546 | case 2:
|
---|
547 | reader->emmskipped[ep->type]++;
|
---|
548 | break;
|
---|
549 | case 3:
|
---|
550 | reader->emmblocked[ep->type]++;
|
---|
551 | break;
|
---|
552 | }
|
---|
553 | #endif
|
---|
554 |
|
---|
555 | return(rc);
|
---|
556 | }
|
---|
557 |
|
---|
558 | static int reader_listen(struct s_reader * reader, int fd1, int fd2)
|
---|
559 | {
|
---|
560 | int fdmax, tcp_toflag, use_tv=(!proxy);
|
---|
561 | int is_tcp=(reader->ph.type==MOD_CONN_TCP);
|
---|
562 | fd_set fds;
|
---|
563 | struct timeval tv;
|
---|
564 |
|
---|
565 | #ifdef CS_WITH_GBOX
|
---|
566 | if(reader->typ==R_GBOX) {
|
---|
567 | struct timeb tpe;
|
---|
568 | int x;
|
---|
569 | ulong ms;
|
---|
570 | cs_ftime(&tpe);
|
---|
571 | for(x=0;x<CS_MAXPENDING;x++){
|
---|
572 | ms=1000*(tpe.time-ecmtask[x].tps.time)+tpe.millitm-ecmtask[x].tps.millitm;
|
---|
573 | if(ecmtask[x].rc == 10 && ms > cfg->ctimeout && reader->ridx == ecmtask[x].gbxRidx) {
|
---|
574 | //cs_log("hello rc=%d idx:%d x:%d ridx%d ridx:%d",ecmtask[x].rc,ecmtask[x].idx,x,ridx,ecmtask[x].gbxRidx);
|
---|
575 | ecmtask[x].rc=5;
|
---|
576 | send_dcw(&ecmtask[x]);
|
---|
577 | }
|
---|
578 | }
|
---|
579 | }
|
---|
580 | #endif
|
---|
581 | if (reader->typ != R_SC8in1) {
|
---|
582 | if (master_pid!=getppid()) cs_exit(0);
|
---|
583 | }
|
---|
584 | tcp_toflag=(fd2 && is_tcp && reader->tcp_ito && reader->tcp_connected);
|
---|
585 | tv.tv_sec = 0;
|
---|
586 | tv.tv_usec = 100000L;
|
---|
587 | if (tcp_toflag)
|
---|
588 | {
|
---|
589 | tv.tv_sec = reader->tcp_ito*60;
|
---|
590 | tv.tv_usec = 0;
|
---|
591 | use_tv = 1;
|
---|
592 | }
|
---|
593 | FD_ZERO(&fds);
|
---|
594 | FD_SET(fd1, &fds);
|
---|
595 | if (fd2) FD_SET(fd2, &fds);
|
---|
596 | if (logfd) FD_SET(logfd, &fds);
|
---|
597 | fdmax=(fd1>fd2) ? fd1 : fd2;
|
---|
598 | fdmax=(fdmax>logfd) ? fdmax : logfd;
|
---|
599 | if (select(fdmax+1, &fds, 0, 0, (use_tv) ? &tv : 0)<0) return(0);
|
---|
600 |
|
---|
601 | if (reader->typ != R_SC8in1) {
|
---|
602 | if (master_pid!=getppid()) cs_exit(0);
|
---|
603 | }
|
---|
604 | if ((logfd) && (FD_ISSET(logfd, &fds)))
|
---|
605 | {
|
---|
606 | cs_debug("select: log-socket ist set");
|
---|
607 | return(3);
|
---|
608 | }
|
---|
609 |
|
---|
610 | if ((fd2) && (FD_ISSET(fd2, &fds)))
|
---|
611 | {
|
---|
612 | cs_debug("select: socket is set");
|
---|
613 | return(2);
|
---|
614 | }
|
---|
615 |
|
---|
616 | if (FD_ISSET(fd1, &fds))
|
---|
617 | {
|
---|
618 | if (tcp_toflag)
|
---|
619 | {
|
---|
620 | time_t now;
|
---|
621 | int time_diff;
|
---|
622 | time(&now);
|
---|
623 | time_diff = abs(now-reader->last_s);
|
---|
624 | if (time_diff>(reader->tcp_ito*60))
|
---|
625 | {
|
---|
626 | if (reader->ph.c_idle)
|
---|
627 | reader_do_idle(reader);
|
---|
628 | else {
|
---|
629 | cs_debug("%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
630 | reader->ph.desc, time_diff, fd2);
|
---|
631 | network_tcp_connection_close(reader, fd2);
|
---|
632 | }
|
---|
633 | }
|
---|
634 | }
|
---|
635 | cs_debug("select: pipe is set");
|
---|
636 | return(1);
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (tcp_toflag)
|
---|
640 | {
|
---|
641 | if (reader->ph.c_idle)
|
---|
642 | reader_do_idle(reader);
|
---|
643 | else {
|
---|
644 | cs_debug("%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
645 | reader->ph.desc, tv.tv_sec, fd2);
|
---|
646 | network_tcp_connection_close(reader, fd2);
|
---|
647 | }
|
---|
648 | return(0);
|
---|
649 | }
|
---|
650 |
|
---|
651 | if (!proxy) reader_checkhealth(reader);
|
---|
652 | return(0);
|
---|
653 | }
|
---|
654 |
|
---|
655 | static void reader_do_pipe(struct s_reader * reader)
|
---|
656 | {
|
---|
657 | uchar *ptr;
|
---|
658 | int pipeCmd = read_from_pipe(client[reader->cs_idx].fd_m2c_c, &ptr, 0);
|
---|
659 |
|
---|
660 | /* FIXME: this breaks camd35/newcamd cascading as this modules does not set tcp_connected = 2 */
|
---|
661 | if (reader->typ == R_CCCAM && reader->tcp_connected != 2 && client[cs_idx].typ == 'p')
|
---|
662 | return;
|
---|
663 |
|
---|
664 | switch(pipeCmd)
|
---|
665 | {
|
---|
666 | case PIP_ID_ECM:
|
---|
667 | reader_get_ecm(reader, (ECM_REQUEST *)ptr);
|
---|
668 | break;
|
---|
669 | case PIP_ID_DCW:
|
---|
670 | reader_send_DCW((ECM_REQUEST *)ptr);
|
---|
671 | break;
|
---|
672 | case PIP_ID_EMM:
|
---|
673 | reader_do_emm(reader, (EMM_PACKET *)ptr);
|
---|
674 | break;
|
---|
675 | case PIP_ID_CIN:
|
---|
676 | reader_card_info(reader);
|
---|
677 | break;
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | void reader_do_idle(struct s_reader * reader)
|
---|
682 | {
|
---|
683 | if (reader->ph.c_idle)
|
---|
684 | reader->ph.c_idle();
|
---|
685 | }
|
---|
686 |
|
---|
687 | static void reader_main(struct s_reader * reader)
|
---|
688 | {
|
---|
689 | while (1)
|
---|
690 | {
|
---|
691 | /* FIXME: this breaks newcamd/camd3 cascading as newcamd/camd35 only connects if server sends ecm/emm to client */
|
---|
692 | if (reader->typ == R_CCCAM && !reader->tcp_connected && client[cs_idx].typ == 'p') {
|
---|
693 | if (!cfg->reader_restart_seconds)
|
---|
694 | cs_exit(1);
|
---|
695 | cs_log("%s not connected! Reconection in %d sec", reader->label, cfg->reader_restart_seconds);
|
---|
696 | cs_sleepms(1000 * cfg->reader_restart_seconds);
|
---|
697 | if (reader->ph.c_init() && reader->ph.cleanup) {
|
---|
698 | reader->ph.cleanup();
|
---|
699 | continue;
|
---|
700 | }
|
---|
701 | }
|
---|
702 | switch(reader_listen(reader, client[reader->cs_idx].fd_m2c_c, pfd))
|
---|
703 | {
|
---|
704 | case 0: reader_do_idle(reader); break;
|
---|
705 | case 1: reader_do_pipe(reader) ; break;
|
---|
706 | case 2: casc_do_sock(reader, 0) ; break;
|
---|
707 | case 3: casc_do_sock_log(reader); break;
|
---|
708 | }
|
---|
709 | }
|
---|
710 | }
|
---|
711 |
|
---|
712 | void * start_cardreader(void * rdr)
|
---|
713 | {
|
---|
714 | struct s_reader * reader = (struct s_reader *) rdr; //FIXME can be made simpler
|
---|
715 | cs_ptyp=D_READER;
|
---|
716 |
|
---|
717 | if ((proxy=reader->typ & R_IS_CASCADING))
|
---|
718 | {
|
---|
719 | client[cs_idx].typ='p';
|
---|
720 | client[cs_idx].port=reader->r_port;
|
---|
721 | strcpy(client[cs_idx].usr, reader->r_usr);
|
---|
722 | switch(reader->typ)
|
---|
723 | {
|
---|
724 | case R_CAMD33 : module_camd33(&reader->ph); break;
|
---|
725 | case R_CAMD35 : module_camd35(&reader->ph); break;
|
---|
726 | case R_NEWCAMD : module_newcamd(&reader->ph); break;
|
---|
727 | case R_RADEGAST: module_radegast(&reader->ph); break;
|
---|
728 | case R_SERIAL : module_oscam_ser(&reader->ph); break;
|
---|
729 | case R_CS378X : module_camd35_tcp(&reader->ph); break;
|
---|
730 | case R_CCCAM : module_cccam(&reader->ph); break;
|
---|
731 | case R_CONSTCW : module_constcw(&reader->ph); break;
|
---|
732 | #ifdef CS_WITH_GBOX
|
---|
733 | case R_GBOX : module_gbox(&reader->ph);strcpy(client[cs_idx].usr, reader->label); break;
|
---|
734 | #endif
|
---|
735 | }
|
---|
736 |
|
---|
737 | if (!(reader->ph.c_init)) {
|
---|
738 | cs_log("FATAL: %s-protocol not supporting cascading", reader->ph.desc);
|
---|
739 | cs_sleepms(1000);
|
---|
740 | cs_exit(1);
|
---|
741 | }
|
---|
742 |
|
---|
743 | if (reader->ph.c_init()) {
|
---|
744 | if (reader->ph.cleanup)
|
---|
745 | reader->ph.cleanup();
|
---|
746 | if (client[cs_idx].typ != 'p')
|
---|
747 | cs_exit(1);
|
---|
748 | }
|
---|
749 |
|
---|
750 | if ((reader->log_port) && (reader->ph.c_init_log))
|
---|
751 | reader->ph.c_init_log();
|
---|
752 | }
|
---|
753 | else
|
---|
754 | {
|
---|
755 | client[cs_idx].ip=cs_inet_addr("127.0.0.1");
|
---|
756 | if (reader->typ != R_SC8in1)
|
---|
757 | while (reader_device_init(reader)==2)
|
---|
758 | cs_sleepms(60000); // wait 60 secs and try again
|
---|
759 | }
|
---|
760 |
|
---|
761 | emmcache=(struct s_emm *)malloc(CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
762 | if (!emmcache)
|
---|
763 | {
|
---|
764 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
765 | cs_exit(1);
|
---|
766 | }
|
---|
767 | memset(emmcache, 0, CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
768 |
|
---|
769 | ecmtask=(ECM_REQUEST *)malloc(CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
770 | if (!ecmtask)
|
---|
771 | {
|
---|
772 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
773 | cs_exit(1);
|
---|
774 | }
|
---|
775 | memset(ecmtask, 0, CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
776 | reader_main(reader);
|
---|
777 | cs_exit(0);
|
---|
778 | return NULL; //dummy to prevent compiler error
|
---|
779 | }
|
---|
780 |
|
---|