1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
2 | #include "globals.h"
|
---|
3 |
|
---|
4 | int logfd=0;
|
---|
5 |
|
---|
6 | void reader_do_idle(struct s_reader * reader);
|
---|
7 |
|
---|
8 | void cs_ri_brk(struct s_reader * reader, int flag)
|
---|
9 | {
|
---|
10 | if (flag)
|
---|
11 | reader->brk_pos=reader->init_history_pos;
|
---|
12 | else
|
---|
13 | reader->init_history_pos=reader->brk_pos;
|
---|
14 | }
|
---|
15 |
|
---|
16 | void cs_ri_log(struct s_reader * reader, char *fmt,...)
|
---|
17 | {
|
---|
18 | char txt[256];
|
---|
19 |
|
---|
20 | va_list params;
|
---|
21 | va_start(params, fmt);
|
---|
22 | vsprintf(txt, fmt, params);
|
---|
23 | va_end(params);
|
---|
24 | cs_log("%s", txt);
|
---|
25 |
|
---|
26 | if (cfg->saveinithistory) {
|
---|
27 | FILE *fp;
|
---|
28 | char filename[256];
|
---|
29 | char *buffer;
|
---|
30 | sprintf(filename, "%s/reader%d", get_tmp_dir(), get_ridx(reader));
|
---|
31 | int size = reader->init_history_pos+strlen(txt)+1;
|
---|
32 | buffer = malloc(size+1);
|
---|
33 |
|
---|
34 | if (buffer == NULL)
|
---|
35 | return;
|
---|
36 |
|
---|
37 | memset(buffer, 32, size);
|
---|
38 |
|
---|
39 | fp = fopen(filename, "r");
|
---|
40 |
|
---|
41 | if (fp) {
|
---|
42 | fread(buffer, 1, reader->init_history_pos, fp);
|
---|
43 | fclose(fp);
|
---|
44 | }
|
---|
45 |
|
---|
46 | sprintf(buffer+reader->init_history_pos, "%s\n", txt);
|
---|
47 |
|
---|
48 | fp = fopen(filename, "w");
|
---|
49 | if (fp) {
|
---|
50 | fwrite(buffer, 1, reader->init_history_pos+strlen(txt)+1, fp);
|
---|
51 | fclose(fp);
|
---|
52 | }
|
---|
53 |
|
---|
54 | free(buffer);
|
---|
55 | }
|
---|
56 | reader->init_history_pos+=strlen(txt)+1;
|
---|
57 | }
|
---|
58 |
|
---|
59 | static void casc_check_dcw(struct s_reader * reader, int idx, int rc, uchar *cw)
|
---|
60 | {
|
---|
61 | int i;
|
---|
62 | struct s_client *cl = reader->client;
|
---|
63 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
64 | {
|
---|
65 | if ((cl->ecmtask[i].rc>=10) &&
|
---|
66 | (!memcmp(cl->ecmtask[i].ecmd5, cl->ecmtask[idx].ecmd5, CS_ECMSTORESIZE)))
|
---|
67 | {
|
---|
68 | if (rc)
|
---|
69 | {
|
---|
70 | cl->ecmtask[i].rc=(i==idx) ? 1 : 2;
|
---|
71 | #ifdef CS_WITH_GBOX
|
---|
72 | if(cl->ecmtask[i].gbxRidx)cl->ecmtask[i].rc=0;
|
---|
73 | #endif
|
---|
74 | memcpy(cl->ecmtask[i].cw, cw, 16);
|
---|
75 | }
|
---|
76 | else
|
---|
77 | cl->ecmtask[i].rc=0;
|
---|
78 | write_ecm_answer(reader, &cl->ecmtask[i]);
|
---|
79 | cl->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=0; tv.tv_usec=timeout*1000; }
|
---|
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 = rdr->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 | void clear_block_delay(struct s_reader *rdr) {
|
---|
188 | rdr->tcp_block_delay = 100;
|
---|
189 | cs_ftime(&rdr->tcp_block_connect_till);
|
---|
190 | }
|
---|
191 |
|
---|
192 | void block_connect(struct s_reader *rdr) {
|
---|
193 | if (!rdr->tcp_block_delay)
|
---|
194 | rdr->tcp_block_delay = 100; //starting blocking time, 100ms
|
---|
195 | rdr->tcp_block_connect_till.time += rdr->tcp_block_delay / 1000;
|
---|
196 | rdr->tcp_block_connect_till.millitm += rdr->tcp_block_delay % 1000;
|
---|
197 | rdr->tcp_block_delay *= 2; //increment timeouts
|
---|
198 | if (rdr->tcp_block_delay >= 60*1000)
|
---|
199 | rdr->tcp_block_delay = 60*1000; //max 1min, todo config
|
---|
200 | cs_debug_mask(D_TRACE, "tcp connect blocking delay for %s set to %d", rdr->label, rdr->tcp_block_delay);
|
---|
201 | }
|
---|
202 |
|
---|
203 | int is_connect_blocked(struct s_reader *rdr) {
|
---|
204 | struct timeb cur_time;
|
---|
205 | cs_ftime(&cur_time);
|
---|
206 | return (comp_timeb(&cur_time, &rdr->tcp_block_connect_till) < 0);
|
---|
207 | }
|
---|
208 |
|
---|
209 | int network_tcp_connection_open()
|
---|
210 | {
|
---|
211 | struct s_client *cl = cur_client();
|
---|
212 | struct s_reader *rdr = cl->reader;
|
---|
213 | cs_log("connecting to %s", rdr->device);
|
---|
214 |
|
---|
215 | in_addr_t last_ip = cl->ip;
|
---|
216 | if (!hostResolve(rdr))
|
---|
217 | return -1;
|
---|
218 |
|
---|
219 | if (last_ip != cl->ip) //clean blocking delay on ip change:
|
---|
220 | clear_block_delay(rdr);
|
---|
221 | if (is_connect_blocked(rdr)) { //inside of blocking delay, do not connect!
|
---|
222 | cs_log("tcp connect blocking delay asserted for %s", rdr->label);
|
---|
223 | return -1;
|
---|
224 | }
|
---|
225 |
|
---|
226 | int sd = cl->udp_fd;
|
---|
227 | int fl = fcntl(sd, F_GETFL);
|
---|
228 | fcntl(sd, F_SETFL, O_NONBLOCK); //set to nonblocking mode to avoid "endless" connecting loops and pipe-overflows:
|
---|
229 | int res =connect(sd, (struct sockaddr *)&cl->udp_sa, sizeof(cl->udp_sa));
|
---|
230 | if (res == 0) {
|
---|
231 | fcntl(sd, F_SETFL, fl); //connect sucessfull, restore blocking mode
|
---|
232 | clear_block_delay(rdr);
|
---|
233 | return sd;
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (errno == EINPROGRESS || errno == EALREADY) {
|
---|
237 | if (network_select(0, DEFAULT_CONNECT_TIMEOUT) > 0) { //if connect is in progress, wait apr. 500ms
|
---|
238 | int r = -1;
|
---|
239 | uint l = sizeof(r);
|
---|
240 | if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &r, (socklen_t*)&l) == 0) {
|
---|
241 | if (r == 0) {
|
---|
242 | fcntl(sd, F_SETFL, fl);
|
---|
243 | clear_block_delay(rdr);
|
---|
244 | return sd; //now we are connected
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 | //else we are not connected - or already connected:
|
---|
250 | else if (errno == EISCONN) {
|
---|
251 | cs_log("already connected!");
|
---|
252 | fcntl(sd, F_SETFL, fl);
|
---|
253 | clear_block_delay(rdr);
|
---|
254 | return sd;
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (errno == EBADF || errno == ENOTSOCK) {
|
---|
258 | cs_log("connect failed: bad socket/descriptor %d", sd);
|
---|
259 | }
|
---|
260 | else if (errno == ETIMEDOUT) {
|
---|
261 | cs_log("connect failed: timeout");
|
---|
262 | }
|
---|
263 | else if (errno == ECONNREFUSED) {
|
---|
264 | cs_log("connection refused");
|
---|
265 | }
|
---|
266 | else if (errno == ENETUNREACH) {
|
---|
267 | cs_log("connect failed: network unreachable!");
|
---|
268 | }
|
---|
269 | else if (errno == EADDRINUSE) {
|
---|
270 | cs_log("connect failed: address in use!");
|
---|
271 | }
|
---|
272 | else
|
---|
273 | cs_log("connect(fd=%d) failed: (errno=%d: %s)", sd, errno, strerror(errno));
|
---|
274 |
|
---|
275 | fcntl(sd, F_SETFL, fl); //restore blocking mode
|
---|
276 |
|
---|
277 | //connect has failed. Block connect for a while:
|
---|
278 | block_connect(rdr);
|
---|
279 |
|
---|
280 | return -1;
|
---|
281 | }
|
---|
282 |
|
---|
283 | void network_tcp_connection_close(struct s_client *cl, int fd)
|
---|
284 | {
|
---|
285 | if(!cl) return;
|
---|
286 | struct s_reader *reader = cl->reader;
|
---|
287 | cs_debug("tcp_conn_close(): fd=%d, cl->typ == 'c'=%d", fd, cl->typ == 'c');
|
---|
288 | if (fd) {
|
---|
289 | close(fd);
|
---|
290 | if(reader)
|
---|
291 | clear_block_delay(reader);
|
---|
292 | }
|
---|
293 | cl->udp_fd = 0;
|
---|
294 |
|
---|
295 | if (cl->typ != 'c')
|
---|
296 | {
|
---|
297 | int i;
|
---|
298 | //cl->pfd = 0;
|
---|
299 | if(reader)
|
---|
300 | reader->tcp_connected = 0;
|
---|
301 |
|
---|
302 | if (cl->ecmtask) {
|
---|
303 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
304 | cl->ecmtask[i].idx = 0;
|
---|
305 | cl->ecmtask[i].rc = 0;
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | if(reader) {
|
---|
310 | reader->ncd_msgid=0;
|
---|
311 | reader->last_s=reader->last_g=0;
|
---|
312 |
|
---|
313 | if (reader->ph.c_init(cl)) {
|
---|
314 | cs_debug("network_tcp_connection_close() exit(1);");
|
---|
315 | cs_exit(1);
|
---|
316 | }
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | static void casc_do_sock_log(struct s_reader * reader)
|
---|
322 | {
|
---|
323 | int i, idx;
|
---|
324 | ushort caid, srvid;
|
---|
325 | ulong provid;
|
---|
326 | struct s_client *cl = reader->client;
|
---|
327 |
|
---|
328 | idx=reader->ph.c_recv_log(&caid, &provid, &srvid);
|
---|
329 | cl->last=time((time_t)0);
|
---|
330 | if (idx<0) return; // no dcw-msg received
|
---|
331 |
|
---|
332 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
333 | {
|
---|
334 | if ( (cl->ecmtask[i].rc>=10)
|
---|
335 | && (cl->ecmtask[i].idx==idx)
|
---|
336 | && (cl->ecmtask[i].caid==caid)
|
---|
337 | && (cl->ecmtask[i].prid==provid)
|
---|
338 | && (cl->ecmtask[i].srvid==srvid))
|
---|
339 | {
|
---|
340 | casc_check_dcw(reader, i, 0, cl->ecmtask[i].cw); // send "not found"
|
---|
341 | break;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | static void casc_do_sock(struct s_reader * reader, int w)
|
---|
347 | {
|
---|
348 | int i, n, idx, rc, j;
|
---|
349 | uchar buf[1024];
|
---|
350 | uchar dcw[16];
|
---|
351 | struct s_client *cl = reader->client;
|
---|
352 |
|
---|
353 | if ((n=casc_recv_timer(reader, buf, sizeof(buf), w))<=0)
|
---|
354 | {
|
---|
355 | if (reader->ph.type==MOD_CONN_TCP && reader->typ != R_RADEGAST)
|
---|
356 | {
|
---|
357 | if (reader->ph.c_idle)
|
---|
358 | reader_do_idle(reader);
|
---|
359 | else {
|
---|
360 | cs_debug("casc_do_sock: close connection");
|
---|
361 | network_tcp_connection_close(reader->client, cl->udp_fd);
|
---|
362 | }
|
---|
363 | return;
|
---|
364 | }
|
---|
365 | }
|
---|
366 | cl->last=time((time_t)0);
|
---|
367 | idx=reader->ph.c_recv_chk(cl, dcw, &rc, buf, n);
|
---|
368 |
|
---|
369 | if (idx<0) return; // no dcw received
|
---|
370 | reader->last_g=time((time_t*)0); // for reconnect timeout
|
---|
371 | //cs_log("casc_do_sock: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
372 | if (!idx) idx=cl->last_idx;
|
---|
373 | j=0;
|
---|
374 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
375 | {
|
---|
376 |
|
---|
377 | if (cl->ecmtask[i].idx==idx)
|
---|
378 | {
|
---|
379 | casc_check_dcw(reader, i, rc, dcw);
|
---|
380 | j=1;
|
---|
381 | break;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | static void casc_get_dcw(struct s_reader * reader, int n)
|
---|
387 | {
|
---|
388 | int w;
|
---|
389 | struct timeb tps, tpe;
|
---|
390 | struct s_client *cl = reader->client;
|
---|
391 | tpe=cl->ecmtask[n].tps;
|
---|
392 | //tpe.millitm+=1500; // TODO: timeout of 1500 should be config
|
---|
393 |
|
---|
394 | tpe.time += cfg->srtimeout/1000;
|
---|
395 | tpe.millitm += cfg->srtimeout%1000;
|
---|
396 |
|
---|
397 | cs_ftime(&tps);
|
---|
398 | while (((w=1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm)>0)
|
---|
399 | && (cl->ecmtask[n].rc>=10))
|
---|
400 | {
|
---|
401 | casc_do_sock(reader, w);
|
---|
402 | cs_ftime(&tps);
|
---|
403 | }
|
---|
404 | if (cl->ecmtask[n].rc>=10)
|
---|
405 | casc_check_dcw(reader, n, 0, cl->ecmtask[n].cw); // simulate "not found"
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 |
|
---|
410 | int casc_process_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
411 | {
|
---|
412 | int rc, n, i, sflag;
|
---|
413 | time_t t;//, tls;
|
---|
414 | struct s_client *cl = reader->client;
|
---|
415 |
|
---|
416 | uchar buf[512];
|
---|
417 |
|
---|
418 | t=time((time_t *)0);
|
---|
419 | for (n=-1, i=0, sflag=1; i<CS_MAXPENDING; i++)
|
---|
420 | {
|
---|
421 | if ((t-(ulong)cl->ecmtask[i].tps.time > ((cfg->ctimeout + 500) / 1000) + 1) &&
|
---|
422 | (cl->ecmtask[i].rc>=10)) // drop timeouts
|
---|
423 | {
|
---|
424 | cl->ecmtask[i].rc=0;
|
---|
425 | }
|
---|
426 | if (n<0 && (cl->ecmtask[i].rc<10)) // free slot found
|
---|
427 | n=i;
|
---|
428 | if ((cl->ecmtask[i].rc>=10) && // ecm already pending
|
---|
429 | (!memcmp(er->ecmd5, cl->ecmtask[i].ecmd5, CS_ECMSTORESIZE)) &&
|
---|
430 | (er->level<=cl->ecmtask[i].level)) // ... this level at least
|
---|
431 | sflag=0;
|
---|
432 | }
|
---|
433 | if (n<0)
|
---|
434 | {
|
---|
435 | cs_log("WARNING: ecm pending table overflow !!");
|
---|
436 | return(-2);
|
---|
437 | }
|
---|
438 | memcpy(&cl->ecmtask[n], er, sizeof(ECM_REQUEST));
|
---|
439 | if( reader->typ == R_NEWCAMD )
|
---|
440 | cl->ecmtask[n].idx=(reader->ncd_msgid==0)?2:reader->ncd_msgid+1;
|
---|
441 | else
|
---|
442 | cl->ecmtask[n].idx=cl->idx++;
|
---|
443 | cl->ecmtask[n].rc=10;
|
---|
444 | cs_debug("---- ecm_task %d, idx %d, sflag=%d, level=%d",
|
---|
445 | n, cl->ecmtask[n].idx, sflag, er->level);
|
---|
446 |
|
---|
447 | if( reader->ph.type==MOD_CONN_TCP && reader->tcp_rto )
|
---|
448 | {
|
---|
449 | int rto = abs(reader->last_s - reader->last_g);
|
---|
450 | if (rto >= (reader->tcp_rto*60))
|
---|
451 | {
|
---|
452 | if (reader->ph.c_idle)
|
---|
453 | reader_do_idle(reader);
|
---|
454 | else {
|
---|
455 | cs_debug("rto=%d", rto);
|
---|
456 | network_tcp_connection_close(reader->client, cl->udp_fd);
|
---|
457 | }
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | cs_ddump_mask(D_ATR, er->ecm, er->l, "casc ecm:");
|
---|
462 | rc=0;
|
---|
463 | if (sflag)
|
---|
464 | {
|
---|
465 | if ((rc=reader->ph.c_send_ecm(cl, &cl->ecmtask[n], buf)))
|
---|
466 | casc_check_dcw(reader, n, 0, cl->ecmtask[n].cw); // simulate "not found"
|
---|
467 | else
|
---|
468 | cl->last_idx = cl->ecmtask[n].idx;
|
---|
469 | reader->last_s = t; // used for inactive_timeout and reconnect_timeout in TCP reader
|
---|
470 |
|
---|
471 | if (!reader->ph.c_multi)
|
---|
472 | casc_get_dcw(reader, n);
|
---|
473 | }
|
---|
474 |
|
---|
475 | //cs_log("casc_process_ecm 1: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
476 |
|
---|
477 | if (cl->idx>0x1ffe) cl->idx=1;
|
---|
478 | return(rc);
|
---|
479 | }
|
---|
480 |
|
---|
481 | static int reader_store_emm(uchar *emm, uchar type)
|
---|
482 | {
|
---|
483 | int rc;
|
---|
484 | struct s_client *cl = cur_client();
|
---|
485 | memcpy(cl->emmcache[cl->rotate].emmd5, MD5(emm, emm[2], cl->dump), CS_EMMSTORESIZE);
|
---|
486 | cl->emmcache[cl->rotate].type=type;
|
---|
487 | cl->emmcache[cl->rotate].count=1;
|
---|
488 | // cs_debug("EMM stored (index %d)", rotate);
|
---|
489 | rc=cl->rotate;
|
---|
490 | cl->rotate=(++cl->rotate < CS_EMMCACHESIZE)?cl->rotate:0;
|
---|
491 | return(rc);
|
---|
492 | }
|
---|
493 |
|
---|
494 | static void reader_get_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
495 | {
|
---|
496 | //cs_log("hallo idx:%d rc:%d caid:%04X",er->idx,er->rc,er->caid);
|
---|
497 | if ((er->rc<10) )
|
---|
498 | {
|
---|
499 | send_dcw(reader->client, er);
|
---|
500 | return;
|
---|
501 | }
|
---|
502 |
|
---|
503 | er->ocaid=er->caid;
|
---|
504 | if (!chk_bcaid(er, &reader->ctab))
|
---|
505 | {
|
---|
506 | cs_debug("caid %04X filtered", er->caid);
|
---|
507 | er->rcEx=E2_CAID;
|
---|
508 | er->rc=0;
|
---|
509 | write_ecm_answer(reader, er);
|
---|
510 | return;
|
---|
511 | }
|
---|
512 | // cache2
|
---|
513 | if (check_cwcache2(er, er->client->grp))
|
---|
514 | {
|
---|
515 | er->rc=2;
|
---|
516 | write_ecm_answer(reader, er);
|
---|
517 | return;
|
---|
518 | }
|
---|
519 | if (reader->typ & R_IS_CASCADING)
|
---|
520 | {
|
---|
521 | struct s_client *cl = reader->client;
|
---|
522 | cl->last_srvid=er->srvid;
|
---|
523 | cl->last_caid=er->caid;
|
---|
524 | casc_process_ecm(reader, er);
|
---|
525 | return;
|
---|
526 | }
|
---|
527 | #ifdef WITH_CARDREADER
|
---|
528 | if (reader->ratelimitecm) {
|
---|
529 | cs_debug("ratelimit idx:%d rc:%d caid:%04X srvid:%04X",er->idx,er->rc,er->caid,er->srvid);
|
---|
530 | int foundspace=-1;
|
---|
531 | int h;
|
---|
532 | for (h=0;h<reader->ratelimitecm;h++) {
|
---|
533 | if (reader->rlecmh[h].srvid == er->srvid) {
|
---|
534 | foundspace=h;
|
---|
535 | cs_debug("ratelimit found srvid in use at pos: %d",h);
|
---|
536 | break;
|
---|
537 | }
|
---|
538 | }
|
---|
539 | if (foundspace<0) {
|
---|
540 | for (h=0;h<reader->ratelimitecm;h++) {
|
---|
541 | if ((reader->rlecmh[h].last ==- 1) || ((time(NULL)-reader->rlecmh[h].last) > reader->ratelimitseconds)) {
|
---|
542 | foundspace=h;
|
---|
543 | cs_debug("ratelimit found space at pos: %d old seconds %d",h,reader->rlecmh[h].last);
|
---|
544 | break;
|
---|
545 | }
|
---|
546 | }
|
---|
547 | }
|
---|
548 | if (foundspace<0) {
|
---|
549 | //drop
|
---|
550 | cs_debug("ratelimit could not find space for srvid %04X. Dropping.",er->srvid);
|
---|
551 | er->rcEx=32;
|
---|
552 | er->rc=0;
|
---|
553 | int clcw;
|
---|
554 | for (clcw=0;clcw<16;clcw++) er->cw[clcw]=(uchar)0;
|
---|
555 | snprintf( er->msglog, MSGLOGSIZE, "ECMratelimit no space for srvid" );
|
---|
556 | write_ecm_answer(reader, er);
|
---|
557 | return;
|
---|
558 | } else {
|
---|
559 | reader->rlecmh[foundspace].last=time(NULL);
|
---|
560 | reader->rlecmh[foundspace].srvid=er->srvid;
|
---|
561 | }
|
---|
562 |
|
---|
563 | }
|
---|
564 | cs_ddump_mask(D_ATR, er->ecm, er->l, "ecm:");
|
---|
565 | er->msglog[0] = 0;
|
---|
566 | struct timeb tps, tpe;
|
---|
567 | cs_ftime(&tps);
|
---|
568 | er->rc=reader_ecm(reader, er);
|
---|
569 | cs_ftime(&tpe);
|
---|
570 | if (cs_dblevel) {
|
---|
571 | ushort lc, *lp;
|
---|
572 | for (lp=(ushort *)er->ecm+(er->l>>2), lc=0; lp>=(ushort *)er->ecm; lp--)
|
---|
573 | lc^=*lp;
|
---|
574 | cs_debug_mask(D_TRACE, "reader: %s ecm: %04X real time: %d ms", reader->label, lc, 1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm);
|
---|
575 | }
|
---|
576 | write_ecm_answer(reader, er);
|
---|
577 | reader_post_process(reader);
|
---|
578 | #endif
|
---|
579 | //fixme re-activated code for testing
|
---|
580 | if(reader->typ=='r') reader->qlen--;
|
---|
581 | //printf("queue: %d\n",reader->qlen);
|
---|
582 | }
|
---|
583 |
|
---|
584 | static int reader_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
585 | {
|
---|
586 | int i, no, rc, ecs;
|
---|
587 | char *rtxt[] = { "error", (reader->typ & R_IS_CASCADING) ? "sent" : "written", "skipped", "blocked" };
|
---|
588 | char *typedesc[]= { "unknown", "unique", "shared", "global" };
|
---|
589 | struct timeb tps, tpe;
|
---|
590 | struct s_client *cl = reader->client;
|
---|
591 |
|
---|
592 | cs_ftime(&tps);
|
---|
593 |
|
---|
594 | MD5(ep->emm, ep->emm[2], cl->dump);
|
---|
595 |
|
---|
596 | no=0;
|
---|
597 | for (i=ecs=0; (i<CS_EMMCACHESIZE) && (!ecs); i++) {
|
---|
598 | if (!memcmp(cl->emmcache[i].emmd5, cl->dump, CS_EMMSTORESIZE)) {
|
---|
599 | if (reader->cachemm)
|
---|
600 | ecs=(reader->rewritemm > cl->emmcache[i].count) ? 1 : 2;
|
---|
601 | else
|
---|
602 | ecs=1;
|
---|
603 | no=++cl->emmcache[i].count;
|
---|
604 | i--;
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | if ((rc=ecs)<2)
|
---|
609 | {
|
---|
610 | if (reader->typ & R_IS_CASCADING) {
|
---|
611 | cs_debug("network emm reader: %s" ,reader->label);
|
---|
612 |
|
---|
613 | if (reader->ph.c_send_emm) {
|
---|
614 | rc=reader->ph.c_send_emm(ep);
|
---|
615 | } else {
|
---|
616 | cs_debug("send_emm() support missing");
|
---|
617 | rc=0;
|
---|
618 | }
|
---|
619 | } else {
|
---|
620 | cs_debug("local emm reader: %s" ,reader->label);
|
---|
621 | #ifdef WITH_CARDREADER
|
---|
622 | rc=reader_emm(reader, ep);
|
---|
623 | #else
|
---|
624 | rc=0;
|
---|
625 | #endif
|
---|
626 | }
|
---|
627 |
|
---|
628 | if (!ecs)
|
---|
629 | {
|
---|
630 | i=reader_store_emm(ep->emm, ep->type);
|
---|
631 | no=1;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | if (rc) cl->lastemm=time((time_t)0);
|
---|
636 |
|
---|
637 | #ifdef CS_LED
|
---|
638 | if (rc) cs_switch_led(LED3, LED_BLINK_ON);
|
---|
639 | #endif
|
---|
640 |
|
---|
641 | if (reader->logemm & (1 << rc))
|
---|
642 | {
|
---|
643 | cs_ftime(&tpe);
|
---|
644 |
|
---|
645 | cs_log("%s emmtype=%s, len=%d, idx=%d, cnt=%d: %s (%d ms) by %s",
|
---|
646 | username(ep->client), typedesc[cl->emmcache[i].type], ep->emm[2],
|
---|
647 | i, no, rtxt[rc], 1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm, reader->label); //FIXME not sure why emmtyp must come from ep->client and typedesc can be of cur_client
|
---|
648 | }
|
---|
649 |
|
---|
650 | #ifdef WEBIF
|
---|
651 | //counting results
|
---|
652 | switch(rc){
|
---|
653 | case 0:
|
---|
654 | reader->emmerror[ep->type]++;
|
---|
655 | break;
|
---|
656 | case 1:
|
---|
657 | reader->emmwritten[ep->type]++;
|
---|
658 | break;
|
---|
659 | case 2:
|
---|
660 | reader->emmskipped[ep->type]++;
|
---|
661 | break;
|
---|
662 | case 3:
|
---|
663 | reader->emmblocked[ep->type]++;
|
---|
664 | break;
|
---|
665 | }
|
---|
666 | #endif
|
---|
667 |
|
---|
668 | #ifdef QBOXHD_LED
|
---|
669 | if (rc) qboxhd_led_blink(QBOXHD_LED_COLOR_BLUE,QBOXHD_LED_BLINK_MEDIUM);
|
---|
670 | #endif
|
---|
671 |
|
---|
672 |
|
---|
673 | return(rc);
|
---|
674 | }
|
---|
675 |
|
---|
676 | static int reader_listen(struct s_reader * reader, int fd1, int fd2)
|
---|
677 | {
|
---|
678 | int fdmax, tcp_toflag, use_tv=(!(reader->typ & R_IS_CASCADING));
|
---|
679 | int is_tcp=(reader->ph.type==MOD_CONN_TCP);
|
---|
680 | fd_set fds;
|
---|
681 | struct timeval tv;
|
---|
682 |
|
---|
683 | #ifdef CS_WITH_GBOX
|
---|
684 | if(reader->typ==R_GBOX) {
|
---|
685 | struct timeb tpe;
|
---|
686 | int x;
|
---|
687 | ulong ms;
|
---|
688 | cs_ftime(&tpe);
|
---|
689 | for(x=0;x<CS_MAXPENDING;x++){
|
---|
690 | ms=1000*(tpe.time-cl->ecmtask[x].tps.time)+tpe.millitm-cl->ecmtask[x].tps.millitm;
|
---|
691 | if(cl->ecmtask[x].rc == 10 && ms > cfg->ctimeout && cl->ridx == cl->ecmtask[x].gbxRidx) {
|
---|
692 | //cs_log("hello rc=%d idx:%d x:%d ridx%d ridx:%d",cl->ecmtask[x].rc,cl->ecmtask[x].idx,x,ridx,cl->ecmtask[x].gbxRidx);
|
---|
693 | cl->ecmtask[x].rc=5;
|
---|
694 | send_dcw(cl, &cl->ecmtask[x]);
|
---|
695 | }
|
---|
696 | }
|
---|
697 | }
|
---|
698 | #endif
|
---|
699 |
|
---|
700 | tcp_toflag=(fd2 && is_tcp && reader->tcp_ito && reader->tcp_connected);
|
---|
701 | tv.tv_sec = 0;
|
---|
702 | tv.tv_usec = 100000L;
|
---|
703 | if (tcp_toflag)
|
---|
704 | {
|
---|
705 | tv.tv_sec = reader->tcp_ito*60;
|
---|
706 | tv.tv_usec = 0;
|
---|
707 | use_tv = 1;
|
---|
708 | }
|
---|
709 | FD_ZERO(&fds);
|
---|
710 | FD_SET(fd1, &fds);
|
---|
711 | if (fd2) FD_SET(fd2, &fds);
|
---|
712 | if (logfd) FD_SET(logfd, &fds);
|
---|
713 | fdmax=(fd1>fd2) ? fd1 : fd2;
|
---|
714 | fdmax=(fdmax>logfd) ? fdmax : logfd;
|
---|
715 | if (select(fdmax+1, &fds, 0, 0, (use_tv) ? &tv : 0)<0) return(0);
|
---|
716 |
|
---|
717 | if ((logfd) && (FD_ISSET(logfd, &fds)))
|
---|
718 | {
|
---|
719 | cs_debug("select: log-socket ist set");
|
---|
720 | return(3);
|
---|
721 | }
|
---|
722 |
|
---|
723 | if ((fd2) && (FD_ISSET(fd2, &fds)))
|
---|
724 | {
|
---|
725 | cs_debug("select: socket is set");
|
---|
726 | return(2);
|
---|
727 | }
|
---|
728 |
|
---|
729 | if (FD_ISSET(fd1, &fds))
|
---|
730 | {
|
---|
731 | if (tcp_toflag)
|
---|
732 | {
|
---|
733 | time_t now;
|
---|
734 | int time_diff;
|
---|
735 | time(&now);
|
---|
736 | time_diff = abs(now-reader->last_s);
|
---|
737 | if (time_diff>(reader->tcp_ito*60))
|
---|
738 | {
|
---|
739 | if (reader->ph.c_idle)
|
---|
740 | reader_do_idle(reader);
|
---|
741 | else {
|
---|
742 | cs_debug("%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
743 | reader->ph.desc, time_diff, fd2);
|
---|
744 | network_tcp_connection_close(reader->client, fd2);
|
---|
745 | }
|
---|
746 | }
|
---|
747 | }
|
---|
748 | cs_debug("select: pipe is set");
|
---|
749 | return(1);
|
---|
750 | }
|
---|
751 |
|
---|
752 | if (tcp_toflag)
|
---|
753 | {
|
---|
754 | if (reader->ph.c_idle)
|
---|
755 | reader_do_idle(reader);
|
---|
756 | else {
|
---|
757 | cs_debug("%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
758 | reader->ph.desc, tv.tv_sec, fd2);
|
---|
759 | network_tcp_connection_close(reader->client, fd2);
|
---|
760 | }
|
---|
761 | return(0);
|
---|
762 | }
|
---|
763 |
|
---|
764 | #ifdef WITH_CARDREADER
|
---|
765 | if (!(reader->typ & R_IS_CASCADING)) reader_checkhealth(reader);
|
---|
766 | #endif
|
---|
767 | return(0);
|
---|
768 | }
|
---|
769 |
|
---|
770 | void reader_do_card_info(struct s_reader * reader)
|
---|
771 | {
|
---|
772 | #ifdef WITH_CARDREADER
|
---|
773 | reader_card_info(reader);
|
---|
774 | #endif
|
---|
775 | if (reader->ph.c_card_info)
|
---|
776 | reader->ph.c_card_info();
|
---|
777 | }
|
---|
778 |
|
---|
779 | void clear_reader_pipe(struct s_reader * reader)
|
---|
780 | {
|
---|
781 | uchar *ptr;
|
---|
782 | int pipeCmd;
|
---|
783 | while (reader && reader->client && reader->client->fd_m2c_c)
|
---|
784 | {
|
---|
785 | pipeCmd = read_from_pipe(reader->client->fd_m2c_c, &ptr, 0);
|
---|
786 | if (ptr) free(ptr);
|
---|
787 | if (pipeCmd==PIP_ID_ERR || pipeCmd==PIP_ID_NUL)
|
---|
788 | break;
|
---|
789 | }
|
---|
790 | }
|
---|
791 |
|
---|
792 | static void reader_do_pipe(struct s_reader * reader)
|
---|
793 | {
|
---|
794 | uchar *ptr;
|
---|
795 | int pipeCmd = read_from_pipe(reader->client->fd_m2c_c, &ptr, 0);
|
---|
796 |
|
---|
797 | switch(pipeCmd)
|
---|
798 | {
|
---|
799 | case PIP_ID_ECM:
|
---|
800 | reader_get_ecm(reader, (ECM_REQUEST *)ptr);
|
---|
801 | break;
|
---|
802 | case PIP_ID_EMM:
|
---|
803 | reader_do_emm(reader, (EMM_PACKET *)ptr);
|
---|
804 | break;
|
---|
805 | case PIP_ID_CIN:
|
---|
806 | reader_do_card_info(reader);
|
---|
807 | break;
|
---|
808 | case PIP_ID_ERR:
|
---|
809 | cs_exit(1);
|
---|
810 | break;
|
---|
811 | default:
|
---|
812 | cs_log("unhandled pipe message %d (reader %s)", pipeCmd, reader->label);
|
---|
813 | break;
|
---|
814 | }
|
---|
815 | if (ptr) free(ptr);
|
---|
816 | }
|
---|
817 |
|
---|
818 | void reader_do_idle(struct s_reader * reader)
|
---|
819 | {
|
---|
820 | if (reader->ph.c_idle)
|
---|
821 | reader->ph.c_idle();
|
---|
822 | }
|
---|
823 |
|
---|
824 | static void reader_main(struct s_reader * reader)
|
---|
825 | {
|
---|
826 | while (1)
|
---|
827 | {
|
---|
828 | switch(reader_listen(reader, reader->client->fd_m2c_c, reader->client->pfd))
|
---|
829 | {
|
---|
830 | case 0: reader_do_idle(reader); break;
|
---|
831 | case 1: reader_do_pipe(reader) ; break;
|
---|
832 | case 2: casc_do_sock(reader, 0) ; break;
|
---|
833 | case 3: casc_do_sock_log(reader); break;
|
---|
834 | }
|
---|
835 | }
|
---|
836 | }
|
---|
837 |
|
---|
838 | void * start_cardreader(void * rdr)
|
---|
839 | {
|
---|
840 | struct s_reader * reader = (struct s_reader *) rdr;
|
---|
841 |
|
---|
842 | reader->client->thread=pthread_self();
|
---|
843 | pthread_setspecific(getclient, reader->client);
|
---|
844 | reader->client->cs_ptyp=D_READER;
|
---|
845 | strcpy(reader->client->usr, first_client->usr);
|
---|
846 |
|
---|
847 | if (reader->typ & R_IS_CASCADING)
|
---|
848 | {
|
---|
849 | reader->client->typ='p';
|
---|
850 | reader->client->port=reader->r_port;
|
---|
851 | strcpy(reader->client->usr, reader->r_usr);
|
---|
852 | cs_log("proxy thread started (thread=%8X, label=%s, server=%s)",pthread_self(), reader->label, reader->device);
|
---|
853 |
|
---|
854 | if (!(reader->ph.c_init)) {
|
---|
855 | cs_log("FATAL: %s-protocol not supporting cascading", reader->ph.desc);
|
---|
856 | cs_sleepms(1000);
|
---|
857 | cs_exit(1);
|
---|
858 | }
|
---|
859 |
|
---|
860 | if (reader->ph.c_init(reader->client)) {
|
---|
861 | //proxy reader start failed
|
---|
862 | cs_exit(1);
|
---|
863 | }
|
---|
864 |
|
---|
865 | if ((reader->log_port) && (reader->ph.c_init_log))
|
---|
866 | reader->ph.c_init_log();
|
---|
867 | }
|
---|
868 | #ifdef WITH_CARDREADER
|
---|
869 | else
|
---|
870 | {
|
---|
871 | reader->client->ip=cs_inet_addr("127.0.0.1");
|
---|
872 | if (reader->typ != R_SC8in1) {
|
---|
873 | cs_log("reader thread started (thread=%8X, label=%s, device=%s, detect=%s%s, mhz=%d, cardmhz=%d)", pthread_self(), reader->label,
|
---|
874 | reader->device, reader->detect&0x80 ? "!" : "",RDR_CD_TXT[reader->detect&0x7f], reader->mhz,reader->cardmhz);
|
---|
875 | while (reader_device_init(reader)==2)
|
---|
876 | cs_sleepms(60000); // wait 60 secs and try again
|
---|
877 | }
|
---|
878 | else {
|
---|
879 | cs_log("reader thread started (thread=%8X, label=%s, device=%s:%i, detect=%s%s, mhz=%d, cardmhz=%d)", pthread_self(), reader->label,
|
---|
880 | reader->device, reader->slot,reader->detect&0x80 ? "!" : "", RDR_CD_TXT[reader->detect&0x7f], reader->mhz,reader->cardmhz);
|
---|
881 | }
|
---|
882 | }
|
---|
883 |
|
---|
884 | #endif
|
---|
885 | reader->client->emmcache=(struct s_emm *)malloc(CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
886 | if (!reader->client->emmcache)
|
---|
887 | {
|
---|
888 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
889 | cs_exit(1);
|
---|
890 | }
|
---|
891 | memset(reader->client->emmcache, 0, CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
892 |
|
---|
893 | reader->client->ecmtask=(ECM_REQUEST *)malloc(CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
894 | if (!reader->client->ecmtask)
|
---|
895 | {
|
---|
896 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
897 | cs_exit(1);
|
---|
898 | }
|
---|
899 | memset(reader->client->ecmtask, 0, CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
900 | reader_main(reader);
|
---|
901 | cs_exit(0);
|
---|
902 | return NULL; //dummy to prevent compiler error
|
---|
903 | }
|
---|
904 |
|
---|