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(), client[cs_idx].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 ((client[cs_idx].ecmtask[i].rc>=10) &&
|
---|
66 | (!memcmp(client[cs_idx].ecmtask[i].ecmd5, client[cs_idx].ecmtask[idx].ecmd5, CS_ECMSTORESIZE)))
|
---|
67 | {
|
---|
68 | if (rc)
|
---|
69 | {
|
---|
70 | client[cs_idx].ecmtask[i].rc=(i==idx) ? 1 : 2;
|
---|
71 | #ifdef CS_WITH_GBOX
|
---|
72 | if(client[cs_idx].ecmtask[i].gbxRidx)client[cs_idx].ecmtask[i].rc=0;
|
---|
73 | #endif
|
---|
74 | memcpy(client[cs_idx].ecmtask[i].cw, cw, 16);
|
---|
75 | }
|
---|
76 | else
|
---|
77 | client[cs_idx].ecmtask[i].rc=0;
|
---|
78 | write_ecm_answer(reader, client[0].fd_m2c, &client[cs_idx].ecmtask[i]);
|
---|
79 | client[cs_idx].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 = &client[reader->cidx];
|
---|
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 = client[cs_idx].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(int ridx)
|
---|
136 | {
|
---|
137 | int result = 0;
|
---|
138 | //int cs_idx = reader[ridx].cs_idx;
|
---|
139 |
|
---|
140 | pthread_mutex_lock(&gethostbyname_lock);
|
---|
141 |
|
---|
142 | in_addr_t last_ip = client[cs_idx].ip;
|
---|
143 |
|
---|
144 | if (cfg->resolve_gethostbyname) { //Resolve with gethostbyname:
|
---|
145 | struct hostent *rht = gethostbyname(reader[ridx].device);
|
---|
146 | if (!rht) {
|
---|
147 | cs_log("can't resolve %s", reader[ridx].device);
|
---|
148 | result = 0;
|
---|
149 | } else {
|
---|
150 | memcpy(&client[cs_idx].udp_sa.sin_addr, rht->h_addr, sizeof(client[cs_idx].udp_sa.sin_addr));
|
---|
151 | client[cs_idx].ip=cs_inet_order(client[cs_idx].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 = client[cs_idx].udp_sa.sin_family;
|
---|
160 | hints.ai_protocol = IPPROTO_TCP;
|
---|
161 |
|
---|
162 | int err = getaddrinfo(reader[ridx].device, NULL, &hints, &res);
|
---|
163 | if (err != 0 || !res || !res->ai_addr) {
|
---|
164 | cs_log("can't resolve %s, error: %s", reader[ridx].device, err ? gai_strerror(err) : "unknown");
|
---|
165 | result = 0;
|
---|
166 | } else {
|
---|
167 | client[cs_idx].udp_sa.sin_addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
|
---|
168 | client[cs_idx].ip = cs_inet_order(client[cs_idx].udp_sa.sin_addr.s_addr);
|
---|
169 | result = 1;
|
---|
170 | }
|
---|
171 | if (res) freeaddrinfo(res);
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (!result) {
|
---|
175 | client[cs_idx].udp_sa.sin_addr.s_addr = 0;
|
---|
176 | client[cs_idx].ip = 0;
|
---|
177 | } else if (client[cs_idx].ip != last_ip) {
|
---|
178 | uchar *ip = (uchar*) &client[cs_idx].ip;
|
---|
179 | cs_log("%s: resolved ip=%d.%d.%d.%d", reader[ridx].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[client[cs_idx].ridx].device);
|
---|
190 |
|
---|
191 | if (!hostResolve(client[cs_idx].ridx))
|
---|
192 | return -1;
|
---|
193 |
|
---|
194 | int sd = client[cs_idx].udp_fd;
|
---|
195 | if (connect(sd, (struct sockaddr *)&client[cs_idx].udp_sa, sizeof(client[cs_idx].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, client[cs_idx].is_server=%d", fd, client[cs_idx].is_server);
|
---|
239 | close(fd);
|
---|
240 | client[cs_idx].udp_fd = 0;
|
---|
241 |
|
---|
242 | if (!client[cs_idx].is_server)
|
---|
243 | {
|
---|
244 | int i;
|
---|
245 | //client[cs_idx].pfd = 0;
|
---|
246 | reader->tcp_connected = 0;
|
---|
247 |
|
---|
248 | if (client[cs_idx].ecmtask) {
|
---|
249 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
250 | client[cs_idx].ecmtask[i].idx = 0;
|
---|
251 | client[cs_idx].ecmtask[i].rc = 0;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | reader->ncd_msgid=0;
|
---|
256 | reader->last_s=reader->last_g=0;
|
---|
257 |
|
---|
258 | if (reader->ph.c_init()) {
|
---|
259 | cs_debug("network_tcp_connection_close() exit(1);");
|
---|
260 |
|
---|
261 | if (reader->ph.cleanup)
|
---|
262 | reader->ph.cleanup();
|
---|
263 |
|
---|
264 | cs_exit(1);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | static void casc_do_sock_log(struct s_reader * reader)
|
---|
270 | {
|
---|
271 | int i, idx;
|
---|
272 | ushort caid, srvid;
|
---|
273 | ulong provid;
|
---|
274 |
|
---|
275 | idx=reader->ph.c_recv_log(&caid, &provid, &srvid);
|
---|
276 | client[cs_idx].last=time((time_t)0);
|
---|
277 | if (idx<0) return; // no dcw-msg received
|
---|
278 |
|
---|
279 | for (i=1; i<CS_MAXPENDING; i++)
|
---|
280 | {
|
---|
281 | if ( (client[cs_idx].ecmtask[i].rc>=10)
|
---|
282 | && (client[cs_idx].ecmtask[i].idx==idx)
|
---|
283 | && (client[cs_idx].ecmtask[i].caid==caid)
|
---|
284 | && (client[cs_idx].ecmtask[i].prid==provid)
|
---|
285 | && (client[cs_idx].ecmtask[i].srvid==srvid))
|
---|
286 | {
|
---|
287 | casc_check_dcw(reader, i, 0, client[cs_idx].ecmtask[i].cw); // send "not found"
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | static void casc_do_sock(struct s_reader * reader, int w)
|
---|
294 | {
|
---|
295 | int i, n, idx, rc, j;
|
---|
296 | uchar buf[1024];
|
---|
297 | uchar dcw[16];
|
---|
298 |
|
---|
299 | if ((n=casc_recv_timer(&client[reader->cidx], buf, sizeof(buf), w))<=0)
|
---|
300 | {
|
---|
301 | if (reader->ph.type==MOD_CONN_TCP && reader->typ != R_RADEGAST)
|
---|
302 | {
|
---|
303 | if (reader->ph.c_idle)
|
---|
304 | reader_do_idle(reader);
|
---|
305 | else {
|
---|
306 | cs_debug("casc_do_sock: close connection");
|
---|
307 | network_tcp_connection_close(reader, client[cs_idx].udp_fd);
|
---|
308 | }
|
---|
309 | return;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | client[cs_idx].last=time((time_t)0);
|
---|
313 | idx=reader->ph.c_recv_chk(dcw, &rc, buf, n);
|
---|
314 |
|
---|
315 | if (idx<0) return; // no dcw received
|
---|
316 | reader->last_g=time((time_t*)0); // for reconnect timeout
|
---|
317 | //cs_log("casc_do_sock: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
318 | if (!idx) idx=client[cs_idx].last_idx;
|
---|
319 | j=0;
|
---|
320 | for (i=1; i<CS_MAXPENDING; i++)
|
---|
321 | {
|
---|
322 |
|
---|
323 | if (client[cs_idx].ecmtask[i].idx==idx)
|
---|
324 | {
|
---|
325 | casc_check_dcw(reader, i, rc, dcw);
|
---|
326 | j=1;
|
---|
327 | break;
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | static void casc_get_dcw(struct s_reader * reader, int n)
|
---|
333 | {
|
---|
334 | int w;
|
---|
335 | struct timeb tps, tpe;
|
---|
336 | tpe=client[cs_idx].ecmtask[n].tps;
|
---|
337 | //tpe.millitm+=1500; // TODO: timeout of 1500 should be config
|
---|
338 |
|
---|
339 | tpe.time += cfg->srtimeout/1000;
|
---|
340 | tpe.millitm += cfg->srtimeout%1000;
|
---|
341 |
|
---|
342 | cs_ftime(&tps);
|
---|
343 | while (((w=1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm)>0)
|
---|
344 | && (client[cs_idx].ecmtask[n].rc>=10))
|
---|
345 | {
|
---|
346 | casc_do_sock(reader, w);
|
---|
347 | cs_ftime(&tps);
|
---|
348 | }
|
---|
349 | if (client[cs_idx].ecmtask[n].rc>=10)
|
---|
350 | casc_check_dcw(reader, n, 0, client[cs_idx].ecmtask[n].cw); // simulate "not found"
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 |
|
---|
355 | int casc_process_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
356 | {
|
---|
357 | int rc, n, i, sflag;
|
---|
358 | time_t t;//, tls;
|
---|
359 |
|
---|
360 | uchar buf[512];
|
---|
361 |
|
---|
362 | t=time((time_t *)0);
|
---|
363 | for (n=0, i=sflag=1; i<CS_MAXPENDING; i++)
|
---|
364 | {
|
---|
365 | if ((t-(ulong)client[cs_idx].ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
366 | (client[cs_idx].ecmtask[i].rc>=10)) // drop timeouts
|
---|
367 | {
|
---|
368 | client[cs_idx].ecmtask[i].rc=0;
|
---|
369 | }
|
---|
370 | if ((!n) && (client[cs_idx].ecmtask[i].rc<10)) // free slot found
|
---|
371 | n=i;
|
---|
372 | if ((client[cs_idx].ecmtask[i].rc>=10) && // ecm already pending
|
---|
373 | (!memcmp(er->ecmd5, client[cs_idx].ecmtask[i].ecmd5, CS_ECMSTORESIZE)) &&
|
---|
374 | (er->level<=client[cs_idx].ecmtask[i].level)) // ... this level at least
|
---|
375 | sflag=0;
|
---|
376 | }
|
---|
377 | if (!n)
|
---|
378 | {
|
---|
379 | cs_log("WARNING: ecm pending table overflow !!");
|
---|
380 | return(-2);
|
---|
381 | }
|
---|
382 | memcpy(&client[cs_idx].ecmtask[n], er, sizeof(ECM_REQUEST));
|
---|
383 | if( reader->typ == R_NEWCAMD )
|
---|
384 | client[cs_idx].ecmtask[n].idx=(reader->ncd_msgid==0)?2:reader->ncd_msgid+1;
|
---|
385 | else
|
---|
386 | client[cs_idx].ecmtask[n].idx=client[cs_idx].idx++;
|
---|
387 | client[cs_idx].ecmtask[n].rc=10;
|
---|
388 | cs_debug("---- ecm_task %d, idx %d, sflag=%d, level=%d",
|
---|
389 | n, client[cs_idx].ecmtask[n].idx, sflag, er->level);
|
---|
390 |
|
---|
391 | if( reader->ph.type==MOD_CONN_TCP && reader->tcp_rto )
|
---|
392 | {
|
---|
393 | int rto = abs(reader->last_s - reader->last_g);
|
---|
394 | if (rto >= (reader->tcp_rto*60))
|
---|
395 | {
|
---|
396 | if (reader->ph.c_idle)
|
---|
397 | reader_do_idle(reader);
|
---|
398 | else {
|
---|
399 | cs_debug("rto=%d", rto);
|
---|
400 | network_tcp_connection_close(reader, client[cs_idx].udp_fd);
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | cs_ddump_mask(D_ATR, er->ecm, er->l, "casc ecm:");
|
---|
406 | rc=0;
|
---|
407 | if (sflag)
|
---|
408 | {
|
---|
409 | if ((rc=reader->ph.c_send_ecm(&client[cs_idx], &client[cs_idx].ecmtask[n], buf)))
|
---|
410 | casc_check_dcw(reader, n, 0, client[cs_idx].ecmtask[n].cw); // simulate "not found"
|
---|
411 | else
|
---|
412 | client[cs_idx].last_idx = client[cs_idx].ecmtask[n].idx;
|
---|
413 | reader->last_s = t; // used for inactive_timeout and reconnect_timeout in TCP reader
|
---|
414 |
|
---|
415 | if (!reader->ph.c_multi)
|
---|
416 | casc_get_dcw(reader, n);
|
---|
417 | }
|
---|
418 |
|
---|
419 | //cs_log("casc_process_ecm 1: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
420 |
|
---|
421 | if (client[cs_idx].idx>0x1ffe) client[cs_idx].idx=1;
|
---|
422 | return(rc);
|
---|
423 | }
|
---|
424 |
|
---|
425 | static int reader_store_emm(uchar *emm, uchar type)
|
---|
426 | {
|
---|
427 | int rc;
|
---|
428 | memcpy(client[cs_idx].emmcache[client[cs_idx].rotate].emm, emm, emm[2]);
|
---|
429 | client[cs_idx].emmcache[client[cs_idx].rotate].type=type;
|
---|
430 | client[cs_idx].emmcache[client[cs_idx].rotate].count=1;
|
---|
431 | // cs_debug("EMM stored (index %d)", rotate);
|
---|
432 | rc=client[cs_idx].rotate;
|
---|
433 | client[cs_idx].rotate=(client[cs_idx].rotate+1) % CS_EMMCACHESIZE;
|
---|
434 | return(rc);
|
---|
435 | }
|
---|
436 |
|
---|
437 | static void reader_get_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
438 | {
|
---|
439 | //cs_log("hallo idx:%d rc:%d caid:%04X",er->idx,er->rc,er->caid);
|
---|
440 | if ((er->rc<10) )
|
---|
441 | {
|
---|
442 | send_dcw(&client[reader->cidx], er);
|
---|
443 | return;
|
---|
444 | }
|
---|
445 |
|
---|
446 | er->ocaid=er->caid;
|
---|
447 | if (!chk_bcaid(er, &reader->ctab))
|
---|
448 | {
|
---|
449 | cs_debug("caid %04X filtered", er->caid);
|
---|
450 | er->rcEx=E2_CAID;
|
---|
451 | er->rc=0;
|
---|
452 | write_ecm_answer(reader, client[0].fd_m2c, er);
|
---|
453 | return;
|
---|
454 | }
|
---|
455 | // cache2
|
---|
456 | if (check_ecmcache2(er, client[er->cidx].grp))
|
---|
457 | {
|
---|
458 | er->rc=2;
|
---|
459 | write_ecm_answer(reader, client[0].fd_m2c, er);
|
---|
460 | return;
|
---|
461 | }
|
---|
462 | if (reader->typ & R_IS_CASCADING)
|
---|
463 | {
|
---|
464 | client[cs_idx].last_srvid=er->srvid;
|
---|
465 | client[cs_idx].last_caid=er->caid;
|
---|
466 | casc_process_ecm(reader, er);
|
---|
467 | return;
|
---|
468 | }
|
---|
469 | #ifdef WITH_CARDREADER
|
---|
470 | if (reader->ratelimitecm) {
|
---|
471 | cs_debug("ratelimit idx:%d rc:%d caid:%04X srvid:%04X",er->idx,er->rc,er->caid,er->srvid);
|
---|
472 | int foundspace=-1;
|
---|
473 | int h;
|
---|
474 | for (h=0;h<reader->ratelimitecm;h++) {
|
---|
475 | if (reader->rlecmh[h].srvid == er->srvid) {
|
---|
476 | foundspace=h;
|
---|
477 | cs_debug("ratelimit found srvid in use at pos: %d",h);
|
---|
478 | break;
|
---|
479 | }
|
---|
480 | }
|
---|
481 | if (foundspace<0) {
|
---|
482 | for (h=0;h<reader->ratelimitecm;h++) {
|
---|
483 | if ((reader->rlecmh[h].last ==- 1) || ((time(NULL)-reader->rlecmh[h].last) > reader->ratelimitseconds)) {
|
---|
484 | foundspace=h;
|
---|
485 | cs_debug("ratelimit found space at pos: %d old seconds %d",h,reader->rlecmh[h].last);
|
---|
486 | break;
|
---|
487 | }
|
---|
488 | }
|
---|
489 | }
|
---|
490 | if (foundspace<0) {
|
---|
491 | //drop
|
---|
492 | cs_debug("ratelimit could not find space for srvid %04X. Dropping.",er->srvid);
|
---|
493 | er->rcEx=32;
|
---|
494 | er->rc=0;
|
---|
495 | int clcw;
|
---|
496 | for (clcw=0;clcw<16;clcw++) er->cw[clcw]=(uchar)0;
|
---|
497 | snprintf( er->msglog, MSGLOGSIZE, "ECMratelimit no space for srvid" );
|
---|
498 | write_ecm_answer(reader, client[0].fd_m2c, er);
|
---|
499 | return;
|
---|
500 | } else {
|
---|
501 | reader->rlecmh[foundspace].last=time(NULL);
|
---|
502 | reader->rlecmh[foundspace].srvid=er->srvid;
|
---|
503 | }
|
---|
504 |
|
---|
505 | }
|
---|
506 | cs_ddump_mask(D_ATR, er->ecm, er->l, "ecm:");
|
---|
507 | er->msglog[0] = 0;
|
---|
508 | er->rc=reader_ecm(reader, er);
|
---|
509 | write_ecm_answer(reader, client[0].fd_m2c, er);
|
---|
510 | reader_post_process(reader);
|
---|
511 | #endif
|
---|
512 | //fixme re-activated code for testing
|
---|
513 | if(reader->typ=='r') reader->qlen--;
|
---|
514 | //printf("queue: %d\n",reader->qlen);
|
---|
515 | }
|
---|
516 |
|
---|
517 | static int reader_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
518 | {
|
---|
519 | int i, no, rc, ecs;
|
---|
520 | char *rtxt[] = { "error", (reader->typ & R_IS_CASCADING) ? "sent" : "written", "skipped", "blocked" };
|
---|
521 | char *typedesc[]= { "unknown", "unique", "shared", "global" };
|
---|
522 | struct timeb tps, tpe;
|
---|
523 |
|
---|
524 | cs_ftime(&tps);
|
---|
525 |
|
---|
526 | no=0;
|
---|
527 | for (i=ecs=0; (i<CS_EMMCACHESIZE) && (!ecs); i++)
|
---|
528 | if (!memcmp(client[cs_idx].emmcache[i].emm, ep->emm, ep->emm[2]))
|
---|
529 | {
|
---|
530 | if (reader->cachemm)
|
---|
531 | ecs=(reader->rewritemm > client[cs_idx].emmcache[i].count) ? 1 : 2;
|
---|
532 | else
|
---|
533 | ecs=1;
|
---|
534 | no=++client[cs_idx].emmcache[i].count;
|
---|
535 | i--;
|
---|
536 | }
|
---|
537 |
|
---|
538 | if ((rc=ecs)<2)
|
---|
539 | {
|
---|
540 | if (reader->typ & R_IS_CASCADING) {
|
---|
541 | cs_debug("network emm reader: %s" ,reader->label);
|
---|
542 |
|
---|
543 | if (reader->ph.c_send_emm) {
|
---|
544 | rc=reader->ph.c_send_emm(ep);
|
---|
545 | } else {
|
---|
546 | cs_debug("send_emm() support missing");
|
---|
547 | rc=0;
|
---|
548 | }
|
---|
549 | } else {
|
---|
550 | cs_debug("local emm reader: %s" ,reader->label);
|
---|
551 | #ifdef WITH_CARDREADER
|
---|
552 | rc=reader_emm(reader, ep);
|
---|
553 | #else
|
---|
554 | rc=0;
|
---|
555 | #endif
|
---|
556 | }
|
---|
557 |
|
---|
558 | if (!ecs)
|
---|
559 | {
|
---|
560 | i=reader_store_emm(ep->emm, ep->type);
|
---|
561 | no=1;
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 | if (rc) client[cs_idx].lastemm=time((time_t)0);
|
---|
566 |
|
---|
567 | #ifdef CS_LED
|
---|
568 | if (rc) cs_switch_led(LED3, LED_BLINK_ON);
|
---|
569 | #endif
|
---|
570 |
|
---|
571 | if (reader->logemm & (1 << rc))
|
---|
572 | {
|
---|
573 | cs_ftime(&tpe);
|
---|
574 |
|
---|
575 | cs_log("%s emmtype=%s, len=%d, idx=%d, cnt=%d: %s (%d ms) by %s",
|
---|
576 | username(&client[ep->cidx]), typedesc[client[cs_idx].emmcache[i].type], ep->emm[2],
|
---|
577 | i, no, rtxt[rc], 1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm, reader->label);
|
---|
578 | }
|
---|
579 |
|
---|
580 | #ifdef WEBIF
|
---|
581 | //counting results
|
---|
582 | switch(rc){
|
---|
583 | case 0:
|
---|
584 | reader->emmerror[ep->type]++;
|
---|
585 | break;
|
---|
586 | case 1:
|
---|
587 | reader->emmwritten[ep->type]++;
|
---|
588 | break;
|
---|
589 | case 2:
|
---|
590 | reader->emmskipped[ep->type]++;
|
---|
591 | break;
|
---|
592 | case 3:
|
---|
593 | reader->emmblocked[ep->type]++;
|
---|
594 | break;
|
---|
595 | }
|
---|
596 | #endif
|
---|
597 |
|
---|
598 | return(rc);
|
---|
599 | }
|
---|
600 |
|
---|
601 | static int reader_listen(struct s_reader * reader, int fd1, int fd2)
|
---|
602 | {
|
---|
603 | int fdmax, tcp_toflag, use_tv=(!(reader->typ & R_IS_CASCADING));
|
---|
604 | int is_tcp=(reader->ph.type==MOD_CONN_TCP);
|
---|
605 | fd_set fds;
|
---|
606 | struct timeval tv;
|
---|
607 |
|
---|
608 | #ifdef CS_WITH_GBOX
|
---|
609 | if(reader->typ==R_GBOX) {
|
---|
610 | struct timeb tpe;
|
---|
611 | int x;
|
---|
612 | ulong ms;
|
---|
613 | cs_ftime(&tpe);
|
---|
614 | for(x=0;x<CS_MAXPENDING;x++){
|
---|
615 | ms=1000*(tpe.time-client[cs_idx].ecmtask[x].tps.time)+tpe.millitm-client[cs_idx].ecmtask[x].tps.millitm;
|
---|
616 | if(client[cs_idx].ecmtask[x].rc == 10 && ms > cfg->ctimeout && client[cs_idx].ridx == client[cs_idx].ecmtask[x].gbxRidx) {
|
---|
617 | //cs_log("hello rc=%d idx:%d x:%d ridx%d ridx:%d",client[cs_idx].ecmtask[x].rc,client[cs_idx].ecmtask[x].idx,x,ridx,client[cs_idx].ecmtask[x].gbxRidx);
|
---|
618 | client[cs_idx].ecmtask[x].rc=5;
|
---|
619 | send_dcw(&client[cs_idx], &client[cs_idx].ecmtask[x]);
|
---|
620 | }
|
---|
621 | }
|
---|
622 | }
|
---|
623 | #endif
|
---|
624 |
|
---|
625 | tcp_toflag=(fd2 && is_tcp && reader->tcp_ito && reader->tcp_connected);
|
---|
626 | tv.tv_sec = 0;
|
---|
627 | tv.tv_usec = 100000L;
|
---|
628 | if (tcp_toflag)
|
---|
629 | {
|
---|
630 | tv.tv_sec = reader->tcp_ito*60;
|
---|
631 | tv.tv_usec = 0;
|
---|
632 | use_tv = 1;
|
---|
633 | }
|
---|
634 | FD_ZERO(&fds);
|
---|
635 | FD_SET(fd1, &fds);
|
---|
636 | if (fd2) FD_SET(fd2, &fds);
|
---|
637 | if (logfd) FD_SET(logfd, &fds);
|
---|
638 | fdmax=(fd1>fd2) ? fd1 : fd2;
|
---|
639 | fdmax=(fdmax>logfd) ? fdmax : logfd;
|
---|
640 | if (select(fdmax+1, &fds, 0, 0, (use_tv) ? &tv : 0)<0) return(0);
|
---|
641 |
|
---|
642 | if ((logfd) && (FD_ISSET(logfd, &fds)))
|
---|
643 | {
|
---|
644 | cs_debug("select: log-socket ist set");
|
---|
645 | return(3);
|
---|
646 | }
|
---|
647 |
|
---|
648 | if ((fd2) && (FD_ISSET(fd2, &fds)))
|
---|
649 | {
|
---|
650 | cs_debug("select: socket is set");
|
---|
651 | return(2);
|
---|
652 | }
|
---|
653 |
|
---|
654 | if (FD_ISSET(fd1, &fds))
|
---|
655 | {
|
---|
656 | if (tcp_toflag)
|
---|
657 | {
|
---|
658 | time_t now;
|
---|
659 | int time_diff;
|
---|
660 | time(&now);
|
---|
661 | time_diff = abs(now-reader->last_s);
|
---|
662 | if (time_diff>(reader->tcp_ito*60))
|
---|
663 | {
|
---|
664 | if (reader->ph.c_idle)
|
---|
665 | reader_do_idle(reader);
|
---|
666 | else {
|
---|
667 | cs_debug("%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
668 | reader->ph.desc, time_diff, fd2);
|
---|
669 | network_tcp_connection_close(reader, fd2);
|
---|
670 | }
|
---|
671 | }
|
---|
672 | }
|
---|
673 | cs_debug("select: pipe is set");
|
---|
674 | return(1);
|
---|
675 | }
|
---|
676 |
|
---|
677 | if (tcp_toflag)
|
---|
678 | {
|
---|
679 | if (reader->ph.c_idle)
|
---|
680 | reader_do_idle(reader);
|
---|
681 | else {
|
---|
682 | cs_debug("%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
683 | reader->ph.desc, tv.tv_sec, fd2);
|
---|
684 | network_tcp_connection_close(reader, fd2);
|
---|
685 | }
|
---|
686 | return(0);
|
---|
687 | }
|
---|
688 |
|
---|
689 | #ifdef WITH_CARDREADER
|
---|
690 | if (!(reader->typ & R_IS_CASCADING)) reader_checkhealth(reader);
|
---|
691 | #endif
|
---|
692 | return(0);
|
---|
693 | }
|
---|
694 |
|
---|
695 | void reader_do_card_info(struct s_reader * reader)
|
---|
696 | {
|
---|
697 | #ifdef WITH_CARDREADER
|
---|
698 | reader_card_info(reader);
|
---|
699 | #endif
|
---|
700 | if (reader->ph.c_card_info)
|
---|
701 | reader->ph.c_card_info();
|
---|
702 | }
|
---|
703 |
|
---|
704 | static void reader_do_pipe(struct s_reader * reader)
|
---|
705 | {
|
---|
706 | uchar *ptr;
|
---|
707 | int pipeCmd = read_from_pipe(client[cs_idx].fd_m2c_c, &ptr, 0);
|
---|
708 |
|
---|
709 | switch(pipeCmd)
|
---|
710 | {
|
---|
711 | case PIP_ID_ECM:
|
---|
712 | reader_get_ecm(reader, (ECM_REQUEST *)ptr);
|
---|
713 | break;
|
---|
714 | case PIP_ID_EMM:
|
---|
715 | reader_do_emm(reader, (EMM_PACKET *)ptr);
|
---|
716 | break;
|
---|
717 | case PIP_ID_CIN:
|
---|
718 | reader_do_card_info(reader);
|
---|
719 | break;
|
---|
720 | default:
|
---|
721 | cs_log("unhandled pipe message %d", pipeCmd);
|
---|
722 | break;
|
---|
723 | }
|
---|
724 | if (ptr) free(ptr);
|
---|
725 | }
|
---|
726 |
|
---|
727 | void reader_do_idle(struct s_reader * reader)
|
---|
728 | {
|
---|
729 | if (reader->ph.c_idle)
|
---|
730 | reader->ph.c_idle();
|
---|
731 | }
|
---|
732 |
|
---|
733 | static void reader_main(struct s_reader * reader)
|
---|
734 | {
|
---|
735 | while (1)
|
---|
736 | {
|
---|
737 | switch(reader_listen(reader, client[reader->cidx].fd_m2c_c, client[cs_idx].pfd))
|
---|
738 | {
|
---|
739 | case 0: reader_do_idle(reader); break;
|
---|
740 | case 1: reader_do_pipe(reader) ; break;
|
---|
741 | case 2: casc_do_sock(reader, 0) ; break;
|
---|
742 | case 3: casc_do_sock_log(reader); break;
|
---|
743 | }
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 | void * start_cardreader(void * rdr)
|
---|
748 | {
|
---|
749 | struct s_reader * reader = (struct s_reader *) rdr; //FIXME can be made simpler
|
---|
750 |
|
---|
751 | client[reader->cidx].thread=pthread_self();
|
---|
752 | client[cs_idx].cs_ptyp=D_READER;
|
---|
753 |
|
---|
754 | if (reader->typ & R_IS_CASCADING)
|
---|
755 | {
|
---|
756 | client[cs_idx].typ='p';
|
---|
757 | client[cs_idx].port=reader->r_port;
|
---|
758 | strcpy(client[cs_idx].usr, reader->r_usr);
|
---|
759 |
|
---|
760 | if (!(reader->ph.c_init)) {
|
---|
761 | cs_log("FATAL: %s-protocol not supporting cascading", reader->ph.desc);
|
---|
762 | cs_sleepms(1000);
|
---|
763 | cs_exit(1);
|
---|
764 | }
|
---|
765 |
|
---|
766 | if (reader->ph.c_init()) {
|
---|
767 | if (reader->ph.cleanup)
|
---|
768 | reader->ph.cleanup();
|
---|
769 | if (client[cs_idx].typ != 'p')
|
---|
770 | cs_exit(1);
|
---|
771 | }
|
---|
772 |
|
---|
773 | if ((reader->log_port) && (reader->ph.c_init_log))
|
---|
774 | reader->ph.c_init_log();
|
---|
775 | }
|
---|
776 | #ifdef WITH_CARDREADER
|
---|
777 | else
|
---|
778 | {
|
---|
779 | client[cs_idx].ip=cs_inet_addr("127.0.0.1");
|
---|
780 | if (reader->typ != R_SC8in1)
|
---|
781 | while (reader_device_init(reader)==2)
|
---|
782 | cs_sleepms(60000); // wait 60 secs and try again
|
---|
783 | }
|
---|
784 |
|
---|
785 | #endif
|
---|
786 | client[cs_idx].emmcache=(struct s_emm *)malloc(CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
787 | if (!client[cs_idx].emmcache)
|
---|
788 | {
|
---|
789 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
790 | cs_exit(1);
|
---|
791 | }
|
---|
792 | memset(client[cs_idx].emmcache, 0, CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
793 |
|
---|
794 | client[cs_idx].ecmtask=(ECM_REQUEST *)malloc(CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
795 | if (!client[cs_idx].ecmtask)
|
---|
796 | {
|
---|
797 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
798 | cs_exit(1);
|
---|
799 | }
|
---|
800 | memset(client[cs_idx].ecmtask, 0, CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
801 | reader_main(reader);
|
---|
802 | cs_exit(0);
|
---|
803 | return NULL; //dummy to prevent compiler error
|
---|
804 | }
|
---|
805 |
|
---|