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