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