1 | #include "globals.h"
|
---|
2 |
|
---|
3 | int32_t logfd=0;
|
---|
4 |
|
---|
5 | void reader_do_idle(struct s_reader * reader);
|
---|
6 |
|
---|
7 | void cs_ri_brk(struct s_reader * reader, int32_t flag)
|
---|
8 | {
|
---|
9 | if (flag)
|
---|
10 | reader->brk_pos=reader->init_history_pos;
|
---|
11 | else
|
---|
12 | reader->init_history_pos=reader->brk_pos;
|
---|
13 | }
|
---|
14 |
|
---|
15 | void cs_ri_log(struct s_reader * reader, char *fmt,...)
|
---|
16 | {
|
---|
17 | char txt[256];
|
---|
18 |
|
---|
19 | va_list params;
|
---|
20 | va_start(params, fmt);
|
---|
21 | vsnprintf(txt, sizeof(txt), fmt, params);
|
---|
22 | va_end(params);
|
---|
23 | cs_log("%s", txt);
|
---|
24 |
|
---|
25 | if (cfg.saveinithistory) {
|
---|
26 | int32_t size = reader->init_history_pos+strlen(txt)+2;
|
---|
27 |
|
---|
28 | cs_realloc(&reader->init_history, size, -1);
|
---|
29 |
|
---|
30 | if (!reader->init_history)
|
---|
31 | return;
|
---|
32 |
|
---|
33 | snprintf(reader->init_history+reader->init_history_pos, strlen(txt)+2, "%s\n", txt);
|
---|
34 | reader->init_history_pos+=strlen(txt)+1;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | static void casc_check_dcw(struct s_reader * reader, int32_t idx, int32_t rc, uchar *cw)
|
---|
39 | {
|
---|
40 | int32_t i, pending=0;
|
---|
41 | struct s_client *cl = reader->client;
|
---|
42 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
43 | {
|
---|
44 | if ((cl->ecmtask[i].rc>=10) &&
|
---|
45 | (!memcmp(cl->ecmtask[i].ecmd5, cl->ecmtask[idx].ecmd5, CS_ECMSTORESIZE)))
|
---|
46 | {
|
---|
47 | if (rc)
|
---|
48 | {
|
---|
49 | cl->ecmtask[i].rc=(i==idx) ? 1 : 2;
|
---|
50 | memcpy(cl->ecmtask[i].cw, cw, 16);
|
---|
51 | }
|
---|
52 | else
|
---|
53 | cl->ecmtask[i].rc=0;
|
---|
54 | write_ecm_answer(reader, &cl->ecmtask[i]);
|
---|
55 | cl->ecmtask[i].idx=0;
|
---|
56 | }
|
---|
57 | if (cl->ecmtask[i].rc >= E_99)
|
---|
58 | pending++;
|
---|
59 | }
|
---|
60 | cl->pending=pending;
|
---|
61 | }
|
---|
62 |
|
---|
63 | int32_t casc_recv_timer(struct s_reader * reader, uchar *buf, int32_t l, int32_t msec)
|
---|
64 | {
|
---|
65 | struct timeval tv;
|
---|
66 | fd_set fds;
|
---|
67 | int32_t rc;
|
---|
68 | struct s_client *cl = reader->client;
|
---|
69 |
|
---|
70 | if (!cl->pfd) return(-1);
|
---|
71 |
|
---|
72 | if (!reader->ph.recv) {
|
---|
73 | cs_log("reader %s: unsupported protocol!", reader->label);
|
---|
74 | return(-1);
|
---|
75 | }
|
---|
76 |
|
---|
77 | tv.tv_sec = msec/1000;
|
---|
78 | tv.tv_usec = (msec%1000)*1000;
|
---|
79 | FD_ZERO(&fds);
|
---|
80 | FD_SET(cl->pfd, &fds);
|
---|
81 | select(cl->pfd+1, &fds, 0, 0, &tv);
|
---|
82 | rc=0;
|
---|
83 | if (FD_ISSET(cl->pfd, &fds))
|
---|
84 | if (!(rc=reader->ph.recv(cl, buf, l)))
|
---|
85 | rc=-1;
|
---|
86 |
|
---|
87 | return(rc);
|
---|
88 | }
|
---|
89 |
|
---|
90 | #define MSTIMEOUT 0x800000
|
---|
91 | #define DEFAULT_CONNECT_TIMEOUT 500
|
---|
92 |
|
---|
93 | int32_t network_select(int32_t forRead, int32_t timeout)
|
---|
94 | {
|
---|
95 | int32_t sd = cur_client()->udp_fd;
|
---|
96 | if(sd>=0) {
|
---|
97 | fd_set fds;
|
---|
98 | FD_ZERO(&fds); FD_SET(sd,&fds);
|
---|
99 | struct timeval tv;
|
---|
100 | if(timeout&MSTIMEOUT) { tv.tv_sec=0; tv.tv_usec=(timeout&~MSTIMEOUT)*1000; }
|
---|
101 | else { tv.tv_sec=0; tv.tv_usec=timeout*1000; }
|
---|
102 | int32_t r=select(sd+1,forRead ? &fds:0,forRead ? 0:&fds,0,&tv);
|
---|
103 | if(r>0) return 1;
|
---|
104 | else if(r<0) {
|
---|
105 | cs_debug_mask(D_READER, "socket: select failed (errno=%d %s)", errno, strerror(errno));
|
---|
106 | return -1;
|
---|
107 | }
|
---|
108 | else {
|
---|
109 | if(timeout>0) {
|
---|
110 | cs_debug_mask(D_READER, "socket: select timed out (%d %s)",timeout&~MSTIMEOUT,(timeout&MSTIMEOUT)?"ms":"secs");
|
---|
111 | }
|
---|
112 | errno=ETIMEDOUT;
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return -1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // according to documentation getaddrinfo() is thread safe
|
---|
120 | int32_t hostResolve(struct s_reader *rdr)
|
---|
121 | {
|
---|
122 | int32_t result = 0;
|
---|
123 | struct s_client *cl = rdr->client;
|
---|
124 |
|
---|
125 | pthread_mutex_lock(&gethostbyname_lock);
|
---|
126 |
|
---|
127 | in_addr_t last_ip = cl->ip;
|
---|
128 |
|
---|
129 | if (cfg.resolve_gethostbyname) { //Resolve with gethostbyname:
|
---|
130 | struct hostent *rht = gethostbyname(rdr->device);
|
---|
131 | if (!rht) {
|
---|
132 | cs_log("can't resolve %s", rdr->device);
|
---|
133 | result = 0;
|
---|
134 | } else {
|
---|
135 | memcpy(&cl->udp_sa.sin_addr, rht->h_addr, sizeof(cl->udp_sa.sin_addr));
|
---|
136 | cl->ip=cl->udp_sa.sin_addr.s_addr;
|
---|
137 | result = 1;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | else { //Resolve with getaddrinfo:
|
---|
141 | struct addrinfo hints, *res = NULL;
|
---|
142 | memset(&hints, 0, sizeof(hints));
|
---|
143 | hints.ai_socktype = SOCK_STREAM;
|
---|
144 | hints.ai_family = cl->udp_sa.sin_family;
|
---|
145 | hints.ai_protocol = IPPROTO_TCP;
|
---|
146 |
|
---|
147 | int32_t err = getaddrinfo(rdr->device, NULL, &hints, &res);
|
---|
148 | if (err != 0 || !res || !res->ai_addr) {
|
---|
149 | cs_log("can't resolve %s, error: %s", rdr->device, err ? gai_strerror(err) : "unknown");
|
---|
150 | result = 0;
|
---|
151 | } else {
|
---|
152 | cl->udp_sa.sin_addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
|
---|
153 | cl->ip = cl->udp_sa.sin_addr.s_addr;
|
---|
154 | result = 1;
|
---|
155 | }
|
---|
156 | if (res) freeaddrinfo(res);
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (!result) {
|
---|
160 | cl->udp_sa.sin_addr.s_addr = 0;
|
---|
161 | cl->ip = 0;
|
---|
162 | } else if (cl->ip != last_ip) {
|
---|
163 | cs_log("%s: resolved ip=%s", rdr->device, cs_inet_ntoa(cl->ip));
|
---|
164 | }
|
---|
165 |
|
---|
166 | pthread_mutex_unlock(&gethostbyname_lock);
|
---|
167 |
|
---|
168 | return result;
|
---|
169 | }
|
---|
170 |
|
---|
171 | void clear_block_delay(struct s_reader *rdr) {
|
---|
172 | rdr->tcp_block_delay = 0;
|
---|
173 | cs_ftime(&rdr->tcp_block_connect_till);
|
---|
174 | }
|
---|
175 |
|
---|
176 | void block_connect(struct s_reader *rdr) {
|
---|
177 | if (!rdr->tcp_block_delay)
|
---|
178 | rdr->tcp_block_delay = 100; //starting blocking time, 100ms
|
---|
179 | rdr->tcp_block_connect_till.time += rdr->tcp_block_delay / 1000;
|
---|
180 | rdr->tcp_block_connect_till.millitm += rdr->tcp_block_delay % 1000;
|
---|
181 | rdr->tcp_block_delay *= 2; //increment timeouts
|
---|
182 | if (rdr->tcp_block_delay >= 60*1000)
|
---|
183 | rdr->tcp_block_delay = 60*1000; //max 1min, todo config
|
---|
184 | cs_debug_mask(D_TRACE, "tcp connect blocking delay for %s set to %d", rdr->label, rdr->tcp_block_delay);
|
---|
185 | }
|
---|
186 |
|
---|
187 | int32_t is_connect_blocked(struct s_reader *rdr) {
|
---|
188 | struct timeb cur_time;
|
---|
189 | cs_ftime(&cur_time);
|
---|
190 | return (rdr->tcp_block_delay && comp_timeb(&cur_time, &rdr->tcp_block_connect_till) < 0);
|
---|
191 | }
|
---|
192 |
|
---|
193 | int32_t network_tcp_connection_open()
|
---|
194 | {
|
---|
195 | struct s_client *cl = cur_client();
|
---|
196 | struct s_reader *rdr = cl->reader;
|
---|
197 | cs_log("connecting to %s", rdr->device);
|
---|
198 |
|
---|
199 | in_addr_t last_ip = cl->ip;
|
---|
200 | if (!hostResolve(rdr))
|
---|
201 | return -1;
|
---|
202 |
|
---|
203 | if (last_ip != cl->ip) //clean blocking delay on ip change:
|
---|
204 | clear_block_delay(rdr);
|
---|
205 | if (is_connect_blocked(rdr)) { //inside of blocking delay, do not connect!
|
---|
206 | cs_log("tcp connect blocking delay asserted for %s", rdr->label);
|
---|
207 | return -1;
|
---|
208 | }
|
---|
209 |
|
---|
210 | int32_t flag = 1;
|
---|
211 | setsockopt(cl->udp_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
|
---|
212 |
|
---|
213 | int32_t sd = cl->udp_fd;
|
---|
214 | int32_t fl = fcntl(sd, F_GETFL);
|
---|
215 | fcntl(sd, F_SETFL, O_NONBLOCK); //set to nonblocking mode to avoid "endless" connecting loops and pipe-overflows:
|
---|
216 | int32_t res =connect(sd, (struct sockaddr *)&cl->udp_sa, sizeof(cl->udp_sa));
|
---|
217 | if (res == 0) {
|
---|
218 | fcntl(sd, F_SETFL, fl); //connect sucessfull, restore blocking mode
|
---|
219 | clear_block_delay(rdr);
|
---|
220 | return sd;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (errno == EINPROGRESS || errno == EALREADY) {
|
---|
224 | if (network_select(0, DEFAULT_CONNECT_TIMEOUT) > 0) { //if connect is in progress, wait apr. 500ms
|
---|
225 | int32_t r = -1;
|
---|
226 | uint32_t l = sizeof(r);
|
---|
227 | if (getsockopt(sd, SOL_SOCKET, SO_ERROR, &r, (socklen_t*)&l) == 0) {
|
---|
228 | if (r == 0) {
|
---|
229 | fcntl(sd, F_SETFL, fl);
|
---|
230 | return sd; //now we are connected
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|
235 | //else we are not connected - or already connected:
|
---|
236 | else if (errno == EISCONN) {
|
---|
237 | cs_log("already connected!");
|
---|
238 | fcntl(sd, F_SETFL, fl);
|
---|
239 | clear_block_delay(rdr);
|
---|
240 | return sd;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (errno == EBADF || errno == ENOTSOCK) {
|
---|
244 | cs_log("connect failed: bad socket/descriptor %d", sd);
|
---|
245 | }
|
---|
246 | else if (errno == ETIMEDOUT) {
|
---|
247 | cs_log("connect failed: timeout");
|
---|
248 | }
|
---|
249 | else if (errno == ECONNREFUSED) {
|
---|
250 | cs_log("connection refused");
|
---|
251 | }
|
---|
252 | else if (errno == ENETUNREACH) {
|
---|
253 | cs_log("connect failed: network unreachable!");
|
---|
254 | }
|
---|
255 | else if (errno == EADDRINUSE) {
|
---|
256 | cs_log("connect failed: address in use!");
|
---|
257 | }
|
---|
258 | else
|
---|
259 | cs_log("connect(fd=%d) failed: (errno=%d %s)", sd, errno, strerror(errno));
|
---|
260 |
|
---|
261 | fcntl(sd, F_SETFL, fl); //restore blocking mode
|
---|
262 |
|
---|
263 | //connect has failed. Block connect for a while:
|
---|
264 | block_connect(rdr);
|
---|
265 |
|
---|
266 | return -1;
|
---|
267 | }
|
---|
268 |
|
---|
269 | void network_tcp_connection_close(struct s_client *cl, int32_t fd)
|
---|
270 | {
|
---|
271 | if(!cl) return;
|
---|
272 | struct s_reader *reader = cl->reader;
|
---|
273 | cs_debug_mask(D_READER, "tcp_conn_close(): fd=%d, cl->typ == 'c'=%d", fd, cl->typ == 'c');
|
---|
274 |
|
---|
275 | if (fd) {
|
---|
276 | close(fd);
|
---|
277 | if (fd == cl->udp_fd)
|
---|
278 | cl->udp_fd = 0;
|
---|
279 | if (fd == cl->pfd)
|
---|
280 | cl->pfd = 0;
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | if (cl->typ != 'c')
|
---|
285 | {
|
---|
286 | int32_t i;
|
---|
287 | //cl->pfd = 0;
|
---|
288 | if(reader)
|
---|
289 | reader->tcp_connected = 0;
|
---|
290 |
|
---|
291 | if (cl->ecmtask) {
|
---|
292 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
293 | cl->ecmtask[i].idx = 0;
|
---|
294 | cl->ecmtask[i].rc = 0;
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | if(reader) {
|
---|
299 | reader->ncd_msgid=0;
|
---|
300 | reader->last_s=reader->last_g=0;
|
---|
301 |
|
---|
302 | if (reader->ph.c_init(cl)) {
|
---|
303 | cs_debug_mask(D_READER, "network_tcp_connection_close() exit(1);");
|
---|
304 | cs_exit(1);
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | static void casc_do_sock_log(struct s_reader * reader)
|
---|
311 | {
|
---|
312 | int32_t i, idx;
|
---|
313 | uint16_t caid, srvid;
|
---|
314 | uint32_t provid;
|
---|
315 | struct s_client *cl = reader->client;
|
---|
316 |
|
---|
317 | idx=reader->ph.c_recv_log(&caid, &provid, &srvid);
|
---|
318 | cl->last=time((time_t)0);
|
---|
319 | if (idx<0) return; // no dcw-msg received
|
---|
320 |
|
---|
321 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
322 | {
|
---|
323 | if ( (cl->ecmtask[i].rc>=10)
|
---|
324 | && (cl->ecmtask[i].idx==idx)
|
---|
325 | && (cl->ecmtask[i].caid==caid)
|
---|
326 | && (cl->ecmtask[i].prid==provid)
|
---|
327 | && (cl->ecmtask[i].srvid==srvid))
|
---|
328 | {
|
---|
329 | casc_check_dcw(reader, i, 0, cl->ecmtask[i].cw); // send "not found"
|
---|
330 | break;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | static void casc_do_sock(struct s_reader * reader, int32_t w)
|
---|
336 | {
|
---|
337 | int32_t i, n, idx, rc, j;
|
---|
338 | uchar buf[1024];
|
---|
339 | uchar dcw[16];
|
---|
340 | struct s_client *cl = reader->client;
|
---|
341 |
|
---|
342 | if ((n=casc_recv_timer(reader, buf, sizeof(buf), w))<=0)
|
---|
343 | {
|
---|
344 | if (reader->ph.type==MOD_CONN_TCP && reader->typ != R_RADEGAST)
|
---|
345 | {
|
---|
346 | if (reader->ph.c_idle)
|
---|
347 | reader_do_idle(reader);
|
---|
348 | else {
|
---|
349 | cs_debug_mask(D_READER, "casc_do_sock: closed connection by remote");
|
---|
350 | network_tcp_connection_close(reader->client, cl->udp_fd);
|
---|
351 | }
|
---|
352 | return;
|
---|
353 | }
|
---|
354 | }
|
---|
355 | cl->last=time((time_t)0);
|
---|
356 | idx=reader->ph.c_recv_chk(cl, dcw, &rc, buf, n);
|
---|
357 |
|
---|
358 | if (idx<0) return; // no dcw received
|
---|
359 | reader->last_g=time((time_t*)0); // for reconnect timeout
|
---|
360 | //cs_log("casc_do_sock: last_s=%d, last_g=%d", reader->last_s, reader->last_g);
|
---|
361 | if (!idx) idx=cl->last_idx;
|
---|
362 | j=0;
|
---|
363 | for (i=0; i<CS_MAXPENDING; i++)
|
---|
364 | {
|
---|
365 | if (cl->ecmtask[i].idx==idx)
|
---|
366 | {
|
---|
367 | cl->pending--;
|
---|
368 | casc_check_dcw(reader, i, rc, dcw);
|
---|
369 | j=1;
|
---|
370 | break;
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | static void casc_get_dcw(struct s_reader * reader, int32_t n)
|
---|
376 | {
|
---|
377 | int32_t w;
|
---|
378 | struct timeb tps, tpe;
|
---|
379 | struct s_client *cl = reader->client;
|
---|
380 | tpe=cl->ecmtask[n].tps;
|
---|
381 | //tpe.millitm+=1500; // TODO: timeout of 1500 should be config
|
---|
382 |
|
---|
383 | tpe.time += cfg.srtimeout/1000;
|
---|
384 | tpe.millitm += cfg.srtimeout%1000;
|
---|
385 |
|
---|
386 | cs_ftime(&tps);
|
---|
387 | while (((w=1000*(tpe.time-tps.time)+tpe.millitm-tps.millitm)>0)
|
---|
388 | && (cl->ecmtask[n].rc>=10))
|
---|
389 | {
|
---|
390 | casc_do_sock(reader, w);
|
---|
391 | cs_ftime(&tps);
|
---|
392 | }
|
---|
393 | if (cl->ecmtask[n].rc>=10)
|
---|
394 | casc_check_dcw(reader, n, 0, cl->ecmtask[n].cw); // simulate "not found"
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 |
|
---|
399 | int32_t casc_process_ecm(struct s_reader * reader, ECM_REQUEST *er)
|
---|
400 | {
|
---|
401 | int32_t rc, n, i, sflag, pending=0;
|
---|
402 | time_t t;//, tls;
|
---|
403 | struct s_client *cl = reader->client;
|
---|
404 |
|
---|
405 | uchar buf[512];
|
---|
406 |
|
---|
407 | t=time((time_t *)0);
|
---|
408 | ECM_REQUEST *ecm;
|
---|
409 | for (n=-1, i=0, sflag=1; i<CS_MAXPENDING; i++)
|
---|
410 | {
|
---|
411 | ecm = &cl->ecmtask[i];
|
---|
412 | if ((t-(uint32_t)ecm->tps.time > ((cfg.ctimeout + 500) / 1000) + 1) &&
|
---|
413 | (ecm->rc>=10)) // drop timeouts
|
---|
414 | {
|
---|
415 | ecm->rc=0;
|
---|
416 | send_reader_stat(reader, ecm, E_TIMEOUT);
|
---|
417 | }
|
---|
418 | if (n<0 && (ecm->rc<10)) // free slot found
|
---|
419 | n=i;
|
---|
420 | if ((ecm->rc>=10) && // ecm already pending
|
---|
421 | (!memcmp(er->ecmd5, ecm->ecmd5, CS_ECMSTORESIZE)) &&
|
---|
422 | (er->level<=ecm->level)) // ... this level at least
|
---|
423 | sflag=0;
|
---|
424 |
|
---|
425 | if (ecm->rc >=E_99)
|
---|
426 | pending++;
|
---|
427 | }
|
---|
428 | cl->pending=pending;
|
---|
429 | if (n<0)
|
---|
430 | {
|
---|
431 | cs_log("WARNING: ecm pending table overflow !!");
|
---|
432 | return(-2);
|
---|
433 | }
|
---|
434 | memcpy(&cl->ecmtask[n], er, sizeof(ECM_REQUEST));
|
---|
435 | cl->ecmtask[n].matching_rdr = NULL; //This avoids double free of matching_rdr!
|
---|
436 | if( reader->typ == R_NEWCAMD )
|
---|
437 | cl->ecmtask[n].idx=(reader->ncd_msgid==0)?2:reader->ncd_msgid+1;
|
---|
438 | else {
|
---|
439 | if (!cl->idx)
|
---|
440 | cl->idx = 1;
|
---|
441 | cl->ecmtask[n].idx=cl->idx++;
|
---|
442 | }
|
---|
443 | cl->ecmtask[n].rc=10;
|
---|
444 | cs_debug_mask(D_READER, "---- 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 | int32_t 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_mask(D_READER, "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 int32_t reader_store_emm(uchar *emm, uchar type)
|
---|
482 | {
|
---|
483 | int32_t 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_mask(D_READER, "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<=E_STOPPED)
|
---|
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_mask(D_READER, "caid %04X filtered", er->caid);
|
---|
507 | er->rcEx=E2_CAID;
|
---|
508 | er->rc = E_RDR_NOTFOUND;
|
---|
509 | write_ecm_answer(reader, er);
|
---|
510 | return;
|
---|
511 | }
|
---|
512 | // cache2
|
---|
513 | if (check_cwcache2(er, reader->grp))
|
---|
514 | {
|
---|
515 | er->rc = E_CACHE2;
|
---|
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_mask(D_READER, "ratelimit idx:%d rc:%d caid:%04X srvid:%04X",er->idx,er->rc,er->caid,er->srvid);
|
---|
530 | int32_t foundspace=-1;
|
---|
531 | int32_t h;
|
---|
532 | for (h=0;h<reader->ratelimitecm;h++) {
|
---|
533 | if (reader->rlecmh[h].srvid == er->srvid) {
|
---|
534 | foundspace=h;
|
---|
535 | cs_debug_mask(D_READER, "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_mask(D_READER, "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_mask(D_READER, "ratelimit could not find space for srvid %04X. Dropping.",er->srvid);
|
---|
551 | er->rcEx=32;
|
---|
552 | er->rc = E_RDR_NOTFOUND;
|
---|
553 | int32_t 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 | uint16_t lc, *lp;
|
---|
572 | for (lp=(uint16_t *)er->ecm+(er->l>>2), lc=0; lp>=(uint16_t *)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 | }
|
---|
580 |
|
---|
581 | static int32_t reader_do_emm(struct s_reader * reader, EMM_PACKET *ep)
|
---|
582 | {
|
---|
583 | int32_t i, no, rc, ecs;
|
---|
584 | char *rtxt[] = { "error", (reader->typ & R_IS_CASCADING) ? "sent" : "written", "skipped", "blocked" };
|
---|
585 | char *typedesc[]= { "unknown", "unique", "shared", "global" };
|
---|
586 | struct timeb tps, tpe;
|
---|
587 | struct s_client *cl = reader->client;
|
---|
588 |
|
---|
589 | cs_ftime(&tps);
|
---|
590 |
|
---|
591 | MD5(ep->emm, ep->emm[2], cl->dump);
|
---|
592 |
|
---|
593 | no=0;
|
---|
594 | for (i=ecs=0; (i<CS_EMMCACHESIZE) && (!ecs); i++) {
|
---|
595 | if (!memcmp(cl->emmcache[i].emmd5, cl->dump, CS_EMMSTORESIZE)) {
|
---|
596 | if (reader->cachemm)
|
---|
597 | ecs=(reader->rewritemm > cl->emmcache[i].count) ? 1 : 2;
|
---|
598 | else
|
---|
599 | ecs=1;
|
---|
600 | no=++cl->emmcache[i].count;
|
---|
601 | i--;
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | if ((rc=ecs)<2)
|
---|
606 | {
|
---|
607 | if (reader->typ & R_IS_CASCADING) {
|
---|
608 | cs_debug_mask(D_READER, "network emm reader: %s" ,reader->label);
|
---|
609 |
|
---|
610 | if (reader->ph.c_send_emm) {
|
---|
611 | rc=reader->ph.c_send_emm(ep);
|
---|
612 | } else {
|
---|
613 | cs_debug_mask(D_READER, "send_emm() support missing");
|
---|
614 | rc=0;
|
---|
615 | }
|
---|
616 | } else {
|
---|
617 | cs_debug_mask(D_READER, "local emm reader: %s" ,reader->label);
|
---|
618 | #ifdef WITH_CARDREADER
|
---|
619 | rc=reader_emm(reader, ep);
|
---|
620 | #else
|
---|
621 | rc=0;
|
---|
622 | #endif
|
---|
623 | }
|
---|
624 |
|
---|
625 | if (!ecs)
|
---|
626 | {
|
---|
627 | i=reader_store_emm(ep->emm, ep->type);
|
---|
628 | no=1;
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | if (rc) cl->lastemm=time((time_t)0);
|
---|
633 |
|
---|
634 | #ifdef CS_LED
|
---|
635 | if (rc) cs_switch_led(LED3, LED_BLINK_ON);
|
---|
636 | #endif
|
---|
637 |
|
---|
638 | if (reader->logemm & (1 << rc))
|
---|
639 | {
|
---|
640 | cs_ftime(&tpe);
|
---|
641 |
|
---|
642 | cs_log("%s emmtype=%s, len=%d, idx=%d, cnt=%d: %s (%d ms) by %s",
|
---|
643 | username(ep->client), typedesc[cl->emmcache[i].type], ep->emm[2],
|
---|
644 | 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
|
---|
645 | }
|
---|
646 |
|
---|
647 | #ifdef WEBIF
|
---|
648 | //counting results
|
---|
649 | switch(rc){
|
---|
650 | case 0:
|
---|
651 | reader->emmerror[ep->type]++;
|
---|
652 | break;
|
---|
653 | case 1:
|
---|
654 | reader->emmwritten[ep->type]++;
|
---|
655 | break;
|
---|
656 | case 2:
|
---|
657 | reader->emmskipped[ep->type]++;
|
---|
658 | break;
|
---|
659 | case 3:
|
---|
660 | reader->emmblocked[ep->type]++;
|
---|
661 | break;
|
---|
662 | }
|
---|
663 | #endif
|
---|
664 |
|
---|
665 | #ifdef QBOXHD_LED
|
---|
666 | if (rc) qboxhd_led_blink(QBOXHD_LED_COLOR_BLUE,QBOXHD_LED_BLINK_MEDIUM);
|
---|
667 | #endif
|
---|
668 |
|
---|
669 |
|
---|
670 | return(rc);
|
---|
671 | }
|
---|
672 |
|
---|
673 | static int32_t reader_listen(struct s_reader * reader, int32_t fd1, int32_t fd2)
|
---|
674 | {
|
---|
675 | int32_t fdmax, tcp_toflag, use_tv=(!(reader->typ & R_IS_CASCADING));
|
---|
676 | int32_t is_tcp=(reader->ph.type==MOD_CONN_TCP);
|
---|
677 | fd_set fds;
|
---|
678 | struct timeval tv;
|
---|
679 |
|
---|
680 | tcp_toflag=(fd2 && is_tcp && reader->tcp_ito && reader->tcp_connected);
|
---|
681 | tv.tv_sec = 0;
|
---|
682 | tv.tv_usec = 100000L;
|
---|
683 | if (tcp_toflag)
|
---|
684 | {
|
---|
685 | tv.tv_sec = reader->tcp_ito*60;
|
---|
686 | tv.tv_usec = 0;
|
---|
687 | use_tv = 1;
|
---|
688 | }
|
---|
689 | FD_ZERO(&fds);
|
---|
690 | FD_SET(fd1, &fds);
|
---|
691 | if (fd2) FD_SET(fd2, &fds);
|
---|
692 | if (logfd) FD_SET(logfd, &fds);
|
---|
693 | fdmax=(fd1>fd2) ? fd1 : fd2;
|
---|
694 | fdmax=(fdmax>logfd) ? fdmax : logfd;
|
---|
695 | if (select(fdmax+1, &fds, 0, 0, (use_tv) ? &tv : 0)<0) return(0);
|
---|
696 |
|
---|
697 | if ((logfd) && (FD_ISSET(logfd, &fds)))
|
---|
698 | {
|
---|
699 | cs_debug_mask(D_READER, "select: log-socket ist set");
|
---|
700 | return(3);
|
---|
701 | }
|
---|
702 |
|
---|
703 | if ((fd2) && (FD_ISSET(fd2, &fds)))
|
---|
704 | {
|
---|
705 | cs_debug_mask(D_READER, "select: socket is set");
|
---|
706 | return(2);
|
---|
707 | }
|
---|
708 |
|
---|
709 | if (FD_ISSET(fd1, &fds))
|
---|
710 | {
|
---|
711 | if (tcp_toflag)
|
---|
712 | {
|
---|
713 | time_t now;
|
---|
714 | int32_t time_diff;
|
---|
715 | time(&now);
|
---|
716 | time_diff = abs(now-reader->last_s);
|
---|
717 | if (time_diff>(reader->tcp_ito*60))
|
---|
718 | {
|
---|
719 | if (reader->ph.c_idle)
|
---|
720 | reader_do_idle(reader);
|
---|
721 | else {
|
---|
722 | cs_debug_mask(D_READER, "%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
723 | reader->ph.desc, time_diff, fd2);
|
---|
724 | network_tcp_connection_close(reader->client, fd2);
|
---|
725 | }
|
---|
726 | }
|
---|
727 | }
|
---|
728 | cs_debug_mask(D_READER, "select: pipe is set");
|
---|
729 | return(1);
|
---|
730 | }
|
---|
731 |
|
---|
732 | if (tcp_toflag)
|
---|
733 | {
|
---|
734 | if (reader->ph.c_idle)
|
---|
735 | reader_do_idle(reader);
|
---|
736 | else {
|
---|
737 | cs_debug_mask(D_READER, "%s inactive_timeout (%d), close connection (fd=%d)",
|
---|
738 | reader->ph.desc, tv.tv_sec, fd2);
|
---|
739 | network_tcp_connection_close(reader->client, fd2);
|
---|
740 | }
|
---|
741 | return(0);
|
---|
742 | }
|
---|
743 |
|
---|
744 | #ifdef WITH_CARDREADER
|
---|
745 | if (!(reader->typ & R_IS_CASCADING)) reader_checkhealth(reader);
|
---|
746 | #endif
|
---|
747 | return(0);
|
---|
748 | }
|
---|
749 |
|
---|
750 | void reader_do_card_info(struct s_reader * reader)
|
---|
751 | {
|
---|
752 | #ifdef WITH_CARDREADER
|
---|
753 | reader_card_info(reader);
|
---|
754 | #endif
|
---|
755 | if (reader->ph.c_card_info)
|
---|
756 | reader->ph.c_card_info();
|
---|
757 | }
|
---|
758 |
|
---|
759 | void clear_reader_pipe(struct s_reader * reader)
|
---|
760 | {
|
---|
761 | uchar *ptr;
|
---|
762 | int32_t pipeCmd;
|
---|
763 | while (reader && reader->client && reader->client->fd_m2c_c)
|
---|
764 | {
|
---|
765 | pipeCmd = read_from_pipe(reader->client->fd_m2c_c, &ptr, 0);
|
---|
766 | if (ptr) free(ptr);
|
---|
767 | if (pipeCmd==PIP_ID_ERR || pipeCmd==PIP_ID_NUL)
|
---|
768 | break;
|
---|
769 | }
|
---|
770 | }
|
---|
771 |
|
---|
772 | static void reader_do_pipe(struct s_reader * reader)
|
---|
773 | {
|
---|
774 | uchar *ptr;
|
---|
775 | int32_t pipeCmd = read_from_pipe(reader->client->fd_m2c_c, &ptr, 0);
|
---|
776 |
|
---|
777 | switch(pipeCmd)
|
---|
778 | {
|
---|
779 | case PIP_ID_ECM:
|
---|
780 | reader_get_ecm(reader, (ECM_REQUEST *)ptr);
|
---|
781 | break;
|
---|
782 | case PIP_ID_EMM:
|
---|
783 | reader_do_emm(reader, (EMM_PACKET *)ptr);
|
---|
784 | break;
|
---|
785 | case PIP_ID_CIN:
|
---|
786 | reader_do_card_info(reader);
|
---|
787 | break;
|
---|
788 | case PIP_ID_ERR:
|
---|
789 | cs_exit(1);
|
---|
790 | break;
|
---|
791 | default:
|
---|
792 | cs_log("unhandled pipe message %d (reader %s)", pipeCmd, reader->label);
|
---|
793 | break;
|
---|
794 | }
|
---|
795 | if (ptr) free(ptr);
|
---|
796 | }
|
---|
797 |
|
---|
798 | void reader_do_idle(struct s_reader * reader)
|
---|
799 | {
|
---|
800 | if (reader->ph.c_idle)
|
---|
801 | reader->ph.c_idle();
|
---|
802 | }
|
---|
803 |
|
---|
804 | static void reader_main(struct s_reader * reader)
|
---|
805 | {
|
---|
806 | while (1)
|
---|
807 | {
|
---|
808 | switch(reader_listen(reader, reader->client->fd_m2c_c, reader->client->pfd))
|
---|
809 | {
|
---|
810 | case 0: reader_do_idle(reader); break;
|
---|
811 | case 1: reader_do_pipe(reader) ; break;
|
---|
812 | case 2: casc_do_sock(reader, 0) ; break;
|
---|
813 | case 3: casc_do_sock_log(reader); break;
|
---|
814 | }
|
---|
815 | }
|
---|
816 | }
|
---|
817 |
|
---|
818 | #pragma GCC diagnostic ignored "-Wempty-body"
|
---|
819 | void * start_cardreader(void * rdr)
|
---|
820 | {
|
---|
821 | struct s_reader * reader = (struct s_reader *) rdr;
|
---|
822 | struct s_client * client = reader->client;
|
---|
823 | #ifndef NO_PTHREAD_CLEANUP_PUSH
|
---|
824 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
---|
825 | pthread_cleanup_push(cleanup_thread, (void *)client);
|
---|
826 | #endif
|
---|
827 |
|
---|
828 | client->thread=pthread_self();
|
---|
829 | pthread_setspecific(getclient, client);
|
---|
830 |
|
---|
831 | if (reader->typ & R_IS_CASCADING)
|
---|
832 | {
|
---|
833 | client->typ='p';
|
---|
834 | client->port=reader->r_port;
|
---|
835 | cs_log("proxy thread started (thread=%8X, label=%s, server=%s)",pthread_self(), reader->label, reader->device);
|
---|
836 |
|
---|
837 | if (!(reader->ph.c_init)) {
|
---|
838 | cs_log("FATAL: %s-protocol not supporting cascading", reader->ph.desc);
|
---|
839 | cs_sleepms(1000);
|
---|
840 | cs_exit(1);
|
---|
841 | }
|
---|
842 |
|
---|
843 | if (reader->ph.c_init(client)) {
|
---|
844 | //proxy reader start failed
|
---|
845 | cs_exit(1);
|
---|
846 | }
|
---|
847 |
|
---|
848 | if ((reader->log_port) && (reader->ph.c_init_log))
|
---|
849 | reader->ph.c_init_log();
|
---|
850 | }
|
---|
851 | #ifdef WITH_CARDREADER
|
---|
852 | else
|
---|
853 | {
|
---|
854 | client->ip=cs_inet_addr("127.0.0.1");
|
---|
855 | cs_log("reader thread started (thread=%8X, label=%s, device=%s, detect=%s%s, mhz=%d, cardmhz=%d)", pthread_self(), reader->label,
|
---|
856 | reader->device, reader->detect&0x80 ? "!" : "",RDR_CD_TXT[reader->detect&0x7f], reader->mhz,reader->cardmhz);
|
---|
857 | while (reader_device_init(reader)==2)
|
---|
858 | cs_sleepms(60000); // wait 60 secs and try again
|
---|
859 | }
|
---|
860 |
|
---|
861 | #endif
|
---|
862 | client->emmcache=(struct s_emm *)malloc(CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
863 | if (!client->emmcache)
|
---|
864 | {
|
---|
865 | cs_log("Cannot allocate memory (errno=%d %s)", errno, strerror(errno));
|
---|
866 | cs_exit(1);
|
---|
867 | }
|
---|
868 | memset(client->emmcache, 0, CS_EMMCACHESIZE*(sizeof(struct s_emm)));
|
---|
869 |
|
---|
870 | client->ecmtask=(ECM_REQUEST *)malloc(CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
871 | if (!client->ecmtask)
|
---|
872 | {
|
---|
873 | cs_log("Cannot allocate memory (errno=%d %s)", errno, strerror(errno));
|
---|
874 | cs_exit(1);
|
---|
875 | }
|
---|
876 | memset(client->ecmtask, 0, CS_MAXPENDING*(sizeof(ECM_REQUEST)));
|
---|
877 | reader_main(reader);
|
---|
878 | #ifndef NO_PTHREAD_CLEANUP_PUSH
|
---|
879 | pthread_cleanup_pop(1);
|
---|
880 | #else
|
---|
881 | cs_exit(0);
|
---|
882 | #endif
|
---|
883 | return NULL; //dummy to prevent compiler error
|
---|
884 | }
|
---|
885 | #pragma GCC diagnostic warning "-Wempty-body"
|
---|
886 |
|
---|