1 | #include "globals.h"
|
---|
2 |
|
---|
3 | int ridx=0, logfd=0;
|
---|
4 |
|
---|
5 | static int proxy;
|
---|
6 | static struct s_emm *emmcache;
|
---|
7 | static int last_idx=1;
|
---|
8 | static ushort idx=1;
|
---|
9 |
|
---|
10 | void cs_ri_brk(int flag)
|
---|
11 | {
|
---|
12 | #ifdef CS_RDR_INIT_HIST
|
---|
13 | static int brk_pos=0;
|
---|
14 | if (flag)
|
---|
15 | brk_pos=reader[ridx].init_history_pos;
|
---|
16 | else
|
---|
17 | reader[ridx].init_history_pos=brk_pos;
|
---|
18 | #endif
|
---|
19 | }
|
---|
20 |
|
---|
21 | void cs_ri_log(char *fmt,...)
|
---|
22 | {
|
---|
23 | char txt[256];
|
---|
24 |
|
---|
25 | va_list params;
|
---|
26 | va_start(params, fmt);
|
---|
27 | vsprintf(txt, fmt, params);
|
---|
28 | va_end(params);
|
---|
29 | cs_log("%s", txt);
|
---|
30 | #ifdef CS_RDR_INIT_HIST
|
---|
31 | int val;
|
---|
32 | val=sizeof(reader[ridx].init_history)-reader[ridx].init_history_pos-1;
|
---|
33 | if (val>0)
|
---|
34 | snprintf((char *) reader[ridx].init_history+reader[ridx].init_history_pos, val, "%s", txt);
|
---|
35 | reader[ridx].init_history_pos+=strlen(txt)+1;
|
---|
36 | #endif
|
---|
37 | }
|
---|
38 |
|
---|
39 | static void casc_check_dcw(int idx, int rc, uchar *cw)
|
---|
40 | {
|
---|
41 | int i;
|
---|
42 | for (i=1; i<CS_MAXPENDING; i++)
|
---|
43 | {
|
---|
44 | if ((ecmtask[i].rc>=10) &&
|
---|
45 | (!memcmp(ecmtask[i].ecmd5, ecmtask[idx].ecmd5, CS_ECMSTORESIZE)))
|
---|
46 | {
|
---|
47 | if (rc)
|
---|
48 | {
|
---|
49 | ecmtask[i].rc=(i==idx) ? 1 : 2;
|
---|
50 | #ifdef CS_WITH_GBOX
|
---|
51 | if(ecmtask[i].gbxRidx)ecmtask[i].rc=0;
|
---|
52 | #endif
|
---|
53 | memcpy(ecmtask[i].cw, cw, 16);
|
---|
54 | }
|
---|
55 | else
|
---|
56 | ecmtask[i].rc=0;
|
---|
57 | write_ecm_answer(fd_c2m, &ecmtask[i]);
|
---|
58 | ecmtask[i].idx=0;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int casc_recv_timer(uchar *buf, int l, int msec)
|
---|
64 | {
|
---|
65 | struct timeval tv;
|
---|
66 | fd_set fds;
|
---|
67 | int rc;
|
---|
68 |
|
---|
69 | if (!pfd) return(-1);
|
---|
70 | tv.tv_sec = msec/1000;
|
---|
71 | tv.tv_usec = (msec%1000)*1000;
|
---|
72 | FD_ZERO(&fds);
|
---|
73 | FD_SET(pfd, &fds);
|
---|
74 | select(pfd+1, &fds, 0, 0, &tv);
|
---|
75 | rc=0;
|
---|
76 | if (FD_ISSET(pfd, &fds))
|
---|
77 | if (!(rc=reader[ridx].ph.recv(buf, l)))
|
---|
78 | rc=-1;
|
---|
79 |
|
---|
80 | return(rc);
|
---|
81 | }
|
---|
82 |
|
---|
83 | static int connect_nonb(int sockfd, const struct sockaddr *saptr, socklen_t salen, int nsec)
|
---|
84 | {
|
---|
85 | int flags, n, error;
|
---|
86 | socklen_t len;
|
---|
87 | fd_set rset, wset;
|
---|
88 | struct timeval tval;
|
---|
89 |
|
---|
90 | flags = fcntl(sockfd, F_GETFL, 0);
|
---|
91 | fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
|
---|
92 |
|
---|
93 | error = 0;
|
---|
94 | cs_debug("conn_nb 1 (fd=%d)", sockfd);
|
---|
95 |
|
---|
96 | if ( (n = connect(sockfd, saptr, salen)) < 0) {
|
---|
97 | if( errno==EALREADY ) {
|
---|
98 | cs_debug("conn_nb in progress, errno=%d", errno);
|
---|
99 | return(-1);
|
---|
100 | }
|
---|
101 | else if( errno==EISCONN ) {
|
---|
102 | cs_debug("conn_nb already connected, errno=%d", errno);
|
---|
103 | goto done;
|
---|
104 | }
|
---|
105 | cs_debug("conn_nb 2 (fd=%d)", sockfd);
|
---|
106 | if (errno != EINPROGRESS) {
|
---|
107 | cs_debug("conn_nb 3 (fd=%d)", sockfd);
|
---|
108 | return(-1);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | cs_debug("n = %d\n", n);
|
---|
113 |
|
---|
114 | /* Do whatever we want while the connect is taking place. */
|
---|
115 | if (n == 0)
|
---|
116 | goto done; /* connect completed immediately */
|
---|
117 |
|
---|
118 | FD_ZERO(&rset);
|
---|
119 | FD_SET(sockfd, &rset);
|
---|
120 | wset = rset;
|
---|
121 | tval.tv_sec = nsec;
|
---|
122 | tval.tv_usec = 0;
|
---|
123 |
|
---|
124 | if ( (n = select(sockfd+1, &rset, &wset, 0, nsec ? &tval : 0)) == 0) {
|
---|
125 | //close(sockfd); // timeout
|
---|
126 | cs_debug("conn_nb 4 (fd=%d)", sockfd);
|
---|
127 | errno = ETIMEDOUT;
|
---|
128 | return(-1);
|
---|
129 | }
|
---|
130 |
|
---|
131 | cs_debug("conn_nb 5 (fd=%d)", sockfd);
|
---|
132 |
|
---|
133 | if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
|
---|
134 | cs_debug("conn_nb 6 (fd=%d)", sockfd);
|
---|
135 | len = sizeof(error);
|
---|
136 | if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
---|
137 | cs_debug("conn_nb 7 (fd=%d)", sockfd);
|
---|
138 | return(-1); // Solaris pending error
|
---|
139 | }
|
---|
140 | } else {
|
---|
141 | cs_debug("conn_nb 8 (fd=%d)", sockfd);
|
---|
142 | return -2;
|
---|
143 | }
|
---|
144 |
|
---|
145 | done:
|
---|
146 | cs_debug("conn_nb 9 (fd=%d)", sockfd);
|
---|
147 | fcntl(sockfd, F_SETFL, flags); /* restore file status flags */
|
---|
148 |
|
---|
149 | if (error) {
|
---|
150 | cs_debug("cccam: conn_nb 10 (fd=%d)", sockfd);
|
---|
151 | //close(sockfd); /* just in case */
|
---|
152 | errno = error;
|
---|
153 | return(-1);
|
---|
154 | }
|
---|
155 | return(0);
|
---|
156 | }
|
---|
157 |
|
---|
158 | int network_tcp_connection_open()
|
---|
159 | {
|
---|
160 | int flags;
|
---|
161 | if( connect_nonb(client[cs_idx].udp_fd,
|
---|
162 | (struct sockaddr *)&client[cs_idx].udp_sa,
|
---|
163 | sizeof(client[cs_idx].udp_sa), 5) < 0)
|
---|
164 | {
|
---|
165 | cs_log("connect(fd=%d) failed: (errno=%d)", client[cs_idx].udp_fd, errno);
|
---|
166 | return -1;
|
---|
167 | }
|
---|
168 | flags = fcntl(client[cs_idx].udp_fd, F_GETFL, 0);
|
---|
169 | flags &=~ O_NONBLOCK;
|
---|
170 | fcntl(client[cs_idx].udp_fd, F_SETFL, flags );
|
---|
171 |
|
---|
172 | return client[cs_idx].udp_fd;
|
---|
173 | }
|
---|
174 |
|
---|
175 | void network_tcp_connection_close(int fd)
|
---|
176 | {
|
---|
177 | cs_debug("tcp_conn_close(): fd=%d, is_server=%d", fd, is_server);
|
---|
178 | close(fd);
|
---|
179 | client[cs_idx].udp_fd = 0;
|
---|
180 |
|
---|
181 | if (!is_server)
|
---|
182 | {
|
---|
183 | int i;
|
---|
184 | pfd=0;
|
---|
185 | reader[ridx].tcp_connected = 0;
|
---|
186 |
|
---|
187 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
188 | {
|
---|
189 | ecmtask[i].idx=0;
|
---|
190 | ecmtask[i].rc=0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | reader[ridx].ncd_msgid=0;
|
---|
194 | reader[ridx].last_s=reader[ridx].last_g=0;
|
---|
195 |
|
---|
196 | if (reader[ridx].ph.c_init())
|
---|
197 | {
|
---|
198 | cs_debug("network_tcp_connection_close() exit(1);");
|
---|
199 | cs_exit(1);
|
---|
200 | }
|
---|
201 |
|
---|
202 | cs_resolve();
|
---|
203 | // cs_log("last_s=%d, last_g=%d", reader[ridx].last_s, reader[ridx].last_g);
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | static void casc_do_sock_log()
|
---|
208 | {
|
---|
209 | int i, idx;
|
---|
210 | ushort caid, srvid;
|
---|
211 | ulong provid;
|
---|
212 |
|
---|
213 | idx=reader[ridx].ph.c_recv_log(&caid, &provid, &srvid);
|
---|
214 | client[cs_idx].last=time((time_t)0);
|
---|
215 | if (idx<0) return; // no dcw-msg received
|
---|
216 |
|
---|
217 | for (i=1; i<CS_MAXPENDING; i++)
|
---|
218 | {
|
---|
219 | if ( (ecmtask[i].rc>=10)
|
---|
220 | && (ecmtask[i].idx==idx)
|
---|
221 | && (ecmtask[i].caid==caid)
|
---|
222 | && (ecmtask[i].prid==provid)
|
---|
223 | && (ecmtask[i].srvid==srvid))
|
---|
224 | {
|
---|
225 | casc_check_dcw(i, 0, ecmtask[i].cw); // send "not found"
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | static void casc_do_sock(int w)
|
---|
232 | {
|
---|
233 | int i, n, idx, rc, j;
|
---|
234 | uchar buf[1024];
|
---|
235 | uchar dcw[16];
|
---|
236 |
|
---|
237 | if ((n=casc_recv_timer(buf, sizeof(buf), w))<=0)
|
---|
238 | {
|
---|
239 | if (reader[ridx].ph.type==MOD_CONN_TCP && reader[ridx].typ != R_RADEGAST)
|
---|
240 | {
|
---|
241 | cs_debug("casc_do_sock: close connection");
|
---|
242 | network_tcp_connection_close(client[cs_idx].udp_fd);
|
---|
243 | }
|
---|
244 | return;
|
---|
245 | }
|
---|
246 | client[cs_idx].last=time((time_t)0);
|
---|
247 | idx=reader[ridx].ph.c_recv_chk(dcw, &rc, buf, n);
|
---|
248 |
|
---|
249 | if (idx<0) return; // no dcw received
|
---|
250 | reader[ridx].last_g=time((time_t*)0); // for reconnect timeout
|
---|
251 | //cs_log("casc_do_sock: last_s=%d, last_g=%d", reader[ridx].last_s, reader[ridx].last_g);
|
---|
252 | if (!idx) idx=last_idx;
|
---|
253 | j=0;
|
---|
254 | for (i=1; i<CS_MAXPENDING; i++)
|
---|
255 | {
|
---|
256 |
|
---|
257 | if (ecmtask[i].idx==idx)
|
---|
258 | {
|
---|
259 | casc_check_dcw(i, rc, dcw);
|
---|
260 | j=1;
|
---|
261 | break;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | static void casc_get_dcw(int n)
|
---|
267 | {
|
---|
268 | int w;
|
---|
269 | struct timeb tps, tpe;
|
---|
270 | tpe=ecmtask[n].tps;
|
---|
271 | //tpe.millitm+=1500; // TODO: timeout of 1500 should be config
|
---|
272 | tpe.millitm+=cfg->srtimeout;
|
---|
273 | tpe.time+=(tpe.millitm/1000);
|
---|
274 | tpe.millitm%=1000;
|
---|
275 |
|
---|
276 | cs_ftime(&tps);
|
---|
277 | while (((w=1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm)>0)
|
---|
278 | && (ecmtask[n].rc>=10))
|
---|
279 | {
|
---|
280 | casc_do_sock(w);
|
---|
281 | cs_ftime(&tps);
|
---|
282 | }
|
---|
283 | if (ecmtask[n].rc>=10)
|
---|
284 | casc_check_dcw(n, 0, ecmtask[n].cw); // simulate "not found"
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 |
|
---|
289 | int casc_process_ecm(ECM_REQUEST *er)
|
---|
290 | {
|
---|
291 | int rc, n, i, sflag;
|
---|
292 | time_t t;//, tls;
|
---|
293 |
|
---|
294 | uchar buf[512];
|
---|
295 |
|
---|
296 | t=time((time_t *)0);
|
---|
297 | for (n=0, i=sflag=1; i<CS_MAXPENDING; i++)
|
---|
298 | {
|
---|
299 | if ((t-(ulong)ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
300 | (ecmtask[i].rc>=10)) // drop timeouts
|
---|
301 | {
|
---|
302 | ecmtask[i].rc=0;
|
---|
303 | }
|
---|
304 | if ((!n) && (ecmtask[i].rc<10)) // free slot found
|
---|
305 | n=i;
|
---|
306 | if ((ecmtask[i].rc>=10) && // ecm already pending
|
---|
307 | (!memcmp(er->ecmd5, ecmtask[i].ecmd5, CS_ECMSTORESIZE)) &&
|
---|
308 | (er->level<=ecmtask[i].level)) // ... this level at least
|
---|
309 | sflag=0;
|
---|
310 | }
|
---|
311 | if (!n)
|
---|
312 | {
|
---|
313 | cs_log("WARNING: ecm pending table overflow !!");
|
---|
314 | return(-2);
|
---|
315 | }
|
---|
316 | memcpy(&ecmtask[n], er, sizeof(ECM_REQUEST));
|
---|
317 | if( reader[ridx].typ == R_NEWCAMD )
|
---|
318 | ecmtask[n].idx=(reader[ridx].ncd_msgid==0)?2:reader[ridx].ncd_msgid+1;
|
---|
319 | else
|
---|
320 | ecmtask[n].idx=idx++;
|
---|
321 | ecmtask[n].rc=10;
|
---|
322 | cs_debug("---- ecm_task %d, idx %d, sflag=%d, level=%d",
|
---|
323 | n, ecmtask[n].idx, sflag, er->level);
|
---|
324 |
|
---|
325 | if( reader[ridx].ph.type==MOD_CONN_TCP && reader[ridx].tcp_rto )
|
---|
326 | {
|
---|
327 | int rto = abs(reader[ridx].last_s - reader[ridx].last_g);
|
---|
328 | if (rto >= (reader[ridx].tcp_rto*60))
|
---|
329 | {
|
---|
330 | cs_debug("rto=%d", rto);
|
---|
331 | network_tcp_connection_close(client[cs_idx].udp_fd);
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | cs_ddump_mask(D_ATR, er->ecm, er->l, "casc ecm:");
|
---|
336 | rc=0;
|
---|
337 | if (sflag)
|
---|
338 | {
|
---|
339 | if (!client[cs_idx].udp_sa.sin_addr.s_addr) // once resolved at least
|
---|
340 | cs_resolve();
|
---|
341 |
|
---|
342 | if ((rc=reader[ridx].ph.c_send_ecm(&ecmtask[n], buf)))
|
---|
343 | casc_check_dcw(n, 0, ecmtask[n].cw); // simulate "not found"
|
---|
344 | else
|
---|
345 | last_idx = ecmtask[n].idx;
|
---|
346 | reader[ridx].last_s = t; // used for inactive_timeout and reconnect_timeout in TCP reader
|
---|
347 |
|
---|
348 | if (!reader[ridx].ph.c_multi)
|
---|
349 | casc_get_dcw(n);
|
---|
350 | }
|
---|
351 |
|
---|
352 | //cs_log("casc_process_ecm 1: last_s=%d, last_g=%d", reader[ridx].last_s, reader[ridx].last_g);
|
---|
353 |
|
---|
354 | if (idx>0x1ffe) idx=1;
|
---|
355 | return(rc);
|
---|
356 | }
|
---|
357 |
|
---|
358 | static int reader_store_emm(uchar *emm, uchar type)
|
---|
359 | {
|
---|
360 | static int rotate=0;
|
---|
361 | int rc;
|
---|
362 | memcpy(emmcache[rotate].emm, emm, emm[2]);
|
---|
363 | emmcache[rotate].type=type;
|
---|
364 | emmcache[rotate].count=1;
|
---|
365 | // cs_debug("EMM stored (index %d)", rotate);
|
---|
366 | rc=rotate;
|
---|
367 | rotate=(rotate+1) % CS_EMMCACHESIZE;
|
---|
368 | return(rc);
|
---|
369 | }
|
---|
370 |
|
---|
371 | static void reader_get_ecm(ECM_REQUEST *er)
|
---|
372 | {
|
---|
373 | //cs_log("hallo idx:%d rc:%d caid:%04X",er->idx,er->rc,er->caid);
|
---|
374 | if ((er->rc<10) )
|
---|
375 | {
|
---|
376 | send_dcw(er);
|
---|
377 | return;
|
---|
378 | }
|
---|
379 | er->ocaid=er->caid;
|
---|
380 | if (!chk_bcaid(er, &reader[ridx].ctab))
|
---|
381 | {
|
---|
382 | cs_debug("caid %04X filtered", er->caid);
|
---|
383 | er->rcEx=E2_CAID;
|
---|
384 | er->rc=0;
|
---|
385 | write_ecm_answer(fd_c2m, er);
|
---|
386 | return;
|
---|
387 | }
|
---|
388 | if (check_ecmcache(er, client[er->cidx].grp))
|
---|
389 | {
|
---|
390 | er->rc=2;
|
---|
391 | write_ecm_answer(fd_c2m, er);
|
---|
392 | return;
|
---|
393 | }
|
---|
394 | if (proxy)
|
---|
395 | {
|
---|
396 | client[cs_idx].last_srvid=er->srvid;
|
---|
397 | client[cs_idx].last_caid=er->caid;
|
---|
398 | casc_process_ecm(er);
|
---|
399 | return;
|
---|
400 | }
|
---|
401 | cs_ddump_mask(D_ATR, er->ecm, er->l, "ecm:");
|
---|
402 | er->rc=reader_ecm(er);
|
---|
403 | write_ecm_answer(fd_c2m, er);
|
---|
404 | reader_post_process();
|
---|
405 | //if(reader[ridx].typ=='r') reader[ridx].qlen--;
|
---|
406 | //printf("queue: %d\n",reader[ridx].qlen);
|
---|
407 | }
|
---|
408 |
|
---|
409 | static void reader_send_DCW(ECM_REQUEST *er)
|
---|
410 | {
|
---|
411 | if ((er->rc<10) )
|
---|
412 | {
|
---|
413 | send_dcw(er);
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | static int reader_do_emm(EMM_PACKET *ep)
|
---|
418 | {
|
---|
419 | int i, no, rc, ecs;
|
---|
420 | char *rtxt[] = { "error", "written", "skipped", "blocked" };
|
---|
421 | //char *typedesc[]= { "unknown", "unique", "shared", "global" };
|
---|
422 | struct timeb tps, tpe;
|
---|
423 |
|
---|
424 | cs_ftime(&tps);
|
---|
425 |
|
---|
426 | no=0;
|
---|
427 | for (i=ecs=0; (i<CS_EMMCACHESIZE) && (!ecs); i++)
|
---|
428 | if (!memcmp(emmcache[i].emm, ep->emm, ep->emm[2]))
|
---|
429 | {
|
---|
430 | if (reader[ridx].cachemm)
|
---|
431 | ecs=(reader[ridx].rewritemm > emmcache[i].count) ? 1 : 2;
|
---|
432 | else
|
---|
433 | ecs=1;
|
---|
434 | no=++emmcache[i].count;
|
---|
435 | i--;
|
---|
436 | }
|
---|
437 |
|
---|
438 | if ((rc=ecs)<2)
|
---|
439 | {
|
---|
440 | rc=(proxy) ? 0 : reader_emm(ep);
|
---|
441 | if (!ecs)
|
---|
442 | {
|
---|
443 | i=reader_store_emm(ep->emm, ep->type);
|
---|
444 | no=1;
|
---|
445 | }
|
---|
446 | }
|
---|
447 | if (rc) client[cs_idx].lastemm=time((time_t)0);
|
---|
448 |
|
---|
449 | if (reader[ridx].logemm>=rc)
|
---|
450 | {
|
---|
451 | cs_ftime(&tpe);
|
---|
452 |
|
---|
453 | //cs_log("%s emm: type=%s, len=%d, idx=%d, cnt=%d: %s (%d ms)",
|
---|
454 | cs_log("%s type=%s, len=%d, idx=%d, cnt=%d: %s (%d ms)",
|
---|
455 | //username(ep->cidx), typedesc[emmcache[i].type], ep->emm[2],
|
---|
456 | username(ep->cidx), emmcache[i].type, ep->emm[2],
|
---|
457 | i, no, rtxt[rc], 1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm);
|
---|
458 | }
|
---|
459 |
|
---|
460 | #ifdef WEBIF
|
---|
461 | //counting results
|
---|
462 | switch(rc){
|
---|
463 | case 0:
|
---|
464 | reader[ridx].emmerror++;
|
---|
465 | break;
|
---|
466 | case 1:
|
---|
467 | reader[ridx].emmwritten++;
|
---|
468 | break;
|
---|
469 | case 2:
|
---|
470 | reader[ridx].emmskipped++;
|
---|
471 | break;
|
---|
472 | case 3:
|
---|
473 | reader[ridx].emmblocked++;
|
---|
474 | break;
|
---|
475 | }
|
---|
476 | #endif
|
---|
477 |
|
---|
478 | return(rc);
|
---|
479 | }
|
---|
480 |
|
---|
481 | static int reader_listen(int fd1, int fd2)
|
---|
482 | {
|
---|
483 | int fdmax, tcp_toflag, use_tv=(!proxy);
|
---|
484 | int is_tcp=(reader[ridx].ph.type==MOD_CONN_TCP);
|
---|
485 | fd_set fds;
|
---|
486 | struct timeval tv;
|
---|
487 |
|
---|
488 | #ifdef CS_WITH_GBOX
|
---|
489 | if(reader[ridx].typ==R_GBOX) {
|
---|
490 | struct timeb tpe;
|
---|
491 | int x;
|
---|
492 | ulong ms;
|
---|
493 | cs_ftime(&tpe);
|
---|
494 | for(x=0;x<CS_MAXPENDING;x++){
|
---|
495 | ms=1000*(tpe.time-ecmtask[x].tps.time)+tpe.millitm-ecmtask[x].tps.millitm;
|
---|
496 | if(ecmtask[x].rc == 10 && ms > cfg->ctimeout && ridx == ecmtask[x].gbxRidx) {
|
---|
497 | //cs_log("hello rc=%d idx:%d x:%d ridx%d ridx:%d",ecmtask[x].rc,ecmtask[x].idx,x,ridx,ecmtask[x].gbxRidx);
|
---|
498 | ecmtask[x].rc=5;
|
---|
499 | send_dcw(&ecmtask[x]);
|
---|
500 | }
|
---|
501 | }
|
---|
502 | }
|
---|
503 | #endif
|
---|
504 |
|
---|
505 | if (master_pid!=getppid()) cs_exit(0);
|
---|
506 | tcp_toflag=(fd2 && is_tcp && reader[ridx].tcp_ito && reader[ridx].tcp_connected);
|
---|
507 | tv.tv_sec = 0;
|
---|
508 | tv.tv_usec = 100000L;
|
---|
509 | if (tcp_toflag)
|
---|
510 | {
|
---|
511 | tv.tv_sec = reader[ridx].tcp_ito*60;
|
---|
512 | tv.tv_usec = 0;
|
---|
513 | use_tv = 1;
|
---|
514 | }
|
---|
515 | FD_ZERO(&fds);
|
---|
516 | FD_SET(fd1, &fds);
|
---|
517 | if (fd2) FD_SET(fd2, &fds);
|
---|
518 | if (logfd) FD_SET(logfd, &fds);
|
---|
519 | fdmax=(fd1>fd2) ? fd1 : fd2;
|
---|
520 | fdmax=(fdmax>logfd) ? fdmax : logfd;
|
---|
521 | if (select(fdmax+1, &fds, 0, 0, (use_tv) ? &tv : 0)<0) return(0);
|
---|
522 | if (master_pid!=getppid()) cs_exit(0);
|
---|
523 |
|
---|
524 | if ((logfd) && (FD_ISSET(logfd, &fds)))
|
---|
525 | {
|
---|
526 | cs_debug("select: log-socket ist set");
|
---|
527 | return(3);
|
---|
528 | }
|
---|
529 |
|
---|
530 | if ((fd2) && (FD_ISSET(fd2, &fds)))
|
---|
531 | {
|
---|
532 | cs_debug("select: socket is set");
|
---|
533 | return(2);
|
---|
534 | }
|
---|
535 |
|
---|
536 | if (FD_ISSET(fd1, &fds))
|
---|
537 | {
|
---|
538 | if (tcp_toflag)
|
---|
539 | {
|
---|
540 | time_t now;
|
---|
541 | int time_diff;
|
---|
542 | time(&now);
|
---|
543 | time_diff = abs(now-reader[ridx].last_s);
|
---|
544 | if (time_diff>(reader[ridx].tcp_ito*60))
|
---|
545 | {
|
---|
546 | cs_debug("%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
547 | reader[ridx].ph.desc, time_diff, fd2);
|
---|
548 | network_tcp_connection_close(fd2);
|
---|
549 | }
|
---|
550 | }
|
---|
551 | cs_debug("select: pipe is set");
|
---|
552 | return(1);
|
---|
553 | }
|
---|
554 |
|
---|
555 | if (tcp_toflag)
|
---|
556 | {
|
---|
557 | cs_debug("%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
558 | reader[ridx].ph.desc, tv.tv_sec, fd2);
|
---|
559 | network_tcp_connection_close(fd2);
|
---|
560 | return(0);
|
---|
561 | }
|
---|
562 |
|
---|
563 | if (!proxy) reader_checkhealth();
|
---|
564 | return(0);
|
---|
565 | }
|
---|
566 |
|
---|
567 | static void reader_do_pipe()
|
---|
568 | {
|
---|
569 | uchar *ptr;
|
---|
570 | switch(read_from_pipe(fd_m2c, &ptr, 0))
|
---|
571 | {
|
---|
572 | case PIP_ID_ECM:
|
---|
573 | reader_get_ecm((ECM_REQUEST *)ptr);
|
---|
574 | break;
|
---|
575 | case PIP_ID_DCW:
|
---|
576 | reader_send_DCW((ECM_REQUEST *)ptr);
|
---|
577 | break;
|
---|
578 | case PIP_ID_EMM:
|
---|
579 | reader_do_emm((EMM_PACKET *)ptr);
|
---|
580 | break;
|
---|
581 | case PIP_ID_CIN:
|
---|
582 | reader_card_info();
|
---|
583 | break;
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | static void reader_main()
|
---|
588 | {
|
---|
589 | while (1)
|
---|
590 | {
|
---|
591 | switch(reader_listen(fd_m2c, pfd))
|
---|
592 | {
|
---|
593 | case 1: reader_do_pipe() ; break;
|
---|
594 | case 2: casc_do_sock(0) ; break;
|
---|
595 | case 3: casc_do_sock_log(); break;
|
---|
596 | }
|
---|
597 | }
|
---|
598 | }
|
---|
599 |
|
---|
600 | void start_cardreader()
|
---|
601 | {
|
---|
602 | cs_ptyp=D_READER;
|
---|
603 |
|
---|
604 | if ((proxy=reader[ridx].typ & R_IS_CASCADING))
|
---|
605 | {
|
---|
606 | client[cs_idx].typ='p';
|
---|
607 | client[cs_idx].port=reader[ridx].r_port;
|
---|
608 | strcpy(client[cs_idx].usr, reader[ridx].r_usr);
|
---|
609 | switch(reader[ridx].typ)
|
---|
610 | {
|
---|
611 | case R_CAMD33 : module_camd33(&reader[ridx].ph); break;
|
---|
612 | case R_CAMD35 : module_camd35(&reader[ridx].ph); break;
|
---|
613 | case R_NEWCAMD : module_newcamd(&reader[ridx].ph); break;
|
---|
614 | case R_RADEGAST: module_radegast(&reader[ridx].ph); break;
|
---|
615 | case R_SERIAL : module_oscam_ser(&reader[ridx].ph); break;
|
---|
616 | case R_CS378X : module_camd35_tcp(&reader[ridx].ph); break;
|
---|
617 | case R_CCCAM : module_cccam(&reader[ridx].ph); break;
|
---|
618 | #ifdef CS_WITH_GBOX
|
---|
619 | case R_GBOX : module_gbox(&reader[ridx].ph);strcpy(client[cs_idx].usr, reader[ridx].label); break;
|
---|
620 | #endif
|
---|
621 | }
|
---|
622 | if (!(reader[ridx].ph.c_init))
|
---|
623 | {
|
---|
624 | cs_log("FATAL: %s-protocol not supporting cascading", reader[ridx].ph.desc);
|
---|
625 | cs_sleepms(1000);
|
---|
626 | cs_exit(1);
|
---|
627 | }
|
---|
628 | if (reader[ridx].ph.c_init())
|
---|
629 | cs_exit(1);
|
---|
630 | if ((reader[ridx].log_port) && (reader[ridx].ph.c_init_log))
|
---|
631 | reader[ridx].ph.c_init_log();
|
---|
632 | }
|
---|
633 | else
|
---|
634 | {
|
---|
635 | client[cs_idx].ip=cs_inet_addr("127.0.0.1");
|
---|
636 | while (reader_device_init(reader[ridx].device)==2)
|
---|
637 | cs_sleepms(60000); // wait 60 secs and try again
|
---|
638 | }
|
---|
639 |
|
---|
640 | emmcache=(struct s_emm *)malloc(CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
641 | if (!emmcache)
|
---|
642 | {
|
---|
643 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
644 | cs_exit(1);
|
---|
645 | }
|
---|
646 | memset(emmcache, 0, CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
647 |
|
---|
648 | ecmtask=(ECM_REQUEST *)malloc(CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
649 | if (!ecmtask)
|
---|
650 | {
|
---|
651 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
652 | cs_exit(1);
|
---|
653 | }
|
---|
654 | memset(ecmtask, 0, CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
655 |
|
---|
656 | reader_main();
|
---|
657 | cs_exit(0);
|
---|
658 | }
|
---|