1 | #include "globals.h"
|
---|
2 | extern struct s_reader *reader;
|
---|
3 |
|
---|
4 | //CMD00 - ECM (request)
|
---|
5 | //CMD01 - ECM (response)
|
---|
6 | //CMD02 - EMM (in clientmode - set EMM, in server mode - EMM data) - obsolete
|
---|
7 | //CMD03 - ECM (cascading request)
|
---|
8 | //CMD04 - ECM (cascading response)
|
---|
9 | //CMD05 - EMM (emm request) send cardata/cardinfo to client
|
---|
10 | //CMD06 - EMM (incomming EMM in server mode)
|
---|
11 | //CMD19 - EMM (incomming EMM in server mode) only seen with caid 0x1830
|
---|
12 | //CMD08 - Stop sending requests to the server for current srvid,prvid,caid
|
---|
13 | //CMD44 - MPCS/OScam internal error notification
|
---|
14 |
|
---|
15 | #define REQ_SIZE 328 // 256 + 20 + 0x34
|
---|
16 | static uchar upwd[64]={0};
|
---|
17 | static uchar *req;
|
---|
18 | static int is_udp=1;
|
---|
19 | static int stopped;
|
---|
20 | static int lastcaid;
|
---|
21 | static int lastsrvid;
|
---|
22 | static int lastpid;
|
---|
23 |
|
---|
24 | static int camd35_send(uchar *buf)
|
---|
25 | {
|
---|
26 | int l;
|
---|
27 | unsigned char rbuf[REQ_SIZE+15+4], *sbuf = rbuf + 4;
|
---|
28 |
|
---|
29 | if (!client[cs_idx].udp_fd) return(-1);
|
---|
30 | l = 20 + buf[1] + (((buf[0] == 3) || (buf[0] == 4)) ? 0x34 : 0);
|
---|
31 | memcpy(rbuf, client[cs_idx].ucrc, 4);
|
---|
32 | memcpy(sbuf, buf, l);
|
---|
33 | memset(sbuf + l, 0xff, 15); // set unused space to 0xff for newer camd3's
|
---|
34 | memcpy(sbuf + 4, i2b(4, crc32(0L, sbuf+20, sbuf[1])), 4);
|
---|
35 | l = boundary(4, l);
|
---|
36 | cs_ddump(sbuf, l, "send %d bytes to %s", l, remote_txt());
|
---|
37 | aes_encrypt(sbuf, l);
|
---|
38 |
|
---|
39 | int status;
|
---|
40 | if (is_udp) {
|
---|
41 | status = sendto(client[cs_idx].udp_fd, rbuf, l+4, 0,
|
---|
42 | (struct sockaddr *)&client[cs_idx].udp_sa,
|
---|
43 | sizeof(client[cs_idx].udp_sa));
|
---|
44 | if (status == -1) client[cs_idx].udp_sa.sin_addr.s_addr = 0;
|
---|
45 | }
|
---|
46 | else {
|
---|
47 | status = send(client[cs_idx].udp_fd, rbuf, l + 4, 0);
|
---|
48 | if (status == -1) network_tcp_connection_close(&reader[ridx], pfd);
|
---|
49 | }
|
---|
50 | return status;
|
---|
51 | }
|
---|
52 |
|
---|
53 | static int camd35_auth_client(uchar *ucrc)
|
---|
54 | {
|
---|
55 | int rc=1;
|
---|
56 | ulong crc;
|
---|
57 | struct s_auth *account;
|
---|
58 |
|
---|
59 | if (upwd[0])
|
---|
60 | return(memcmp(client[cs_idx].ucrc, ucrc, 4) ? 1 : 0);
|
---|
61 | client[cs_idx].crypted=1;
|
---|
62 | crc=(((ucrc[0]<<24) | (ucrc[1]<<16) | (ucrc[2]<<8) | ucrc[3]) & 0xffffffffL);
|
---|
63 | for (account=cfg->account; (account) && (!upwd[0]); account=account->next)
|
---|
64 | if (crc==crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), NULL), 16))
|
---|
65 | {
|
---|
66 | memcpy(client[cs_idx].ucrc, ucrc, 4);
|
---|
67 | strcpy((char *)upwd, account->pwd);
|
---|
68 | aes_set_key((char *) MD5(upwd, strlen((char *)upwd), NULL));
|
---|
69 | rc=cs_auth_client(account, NULL);
|
---|
70 | }
|
---|
71 | return(rc);
|
---|
72 | }
|
---|
73 |
|
---|
74 | static int camd35_recv(uchar *buf, int l)
|
---|
75 | {
|
---|
76 | int rc, s, rs, n=0;
|
---|
77 | unsigned char recrc[4];
|
---|
78 | for (rc=rs=s=0; !rc; s++) switch(s)
|
---|
79 | {
|
---|
80 | case 0:
|
---|
81 | if (is_server)
|
---|
82 | {
|
---|
83 | if (!client[cs_idx].udp_fd) return(-9);
|
---|
84 | if (is_udp)
|
---|
85 | rs=recv_from_udpipe(buf);
|
---|
86 | else
|
---|
87 | rs=recv(client[cs_idx].udp_fd, buf, l, 0);
|
---|
88 | }
|
---|
89 | else
|
---|
90 | {
|
---|
91 | if (!client[cs_idx].udp_fd) return(-9);
|
---|
92 | rs = recv(client[cs_idx].udp_fd, buf, l, 0);
|
---|
93 | }
|
---|
94 | if (rs < 24) rc = -1;
|
---|
95 | break;
|
---|
96 | case 1:
|
---|
97 | memcpy(recrc, buf, 4);
|
---|
98 | memmove(buf, buf+4, rs-=4);
|
---|
99 | switch (camd35_auth_client(recrc))
|
---|
100 | {
|
---|
101 | case 0: break; // ok
|
---|
102 | case 1: rc=-2; break; // unknown user
|
---|
103 | default: rc=-9; break; // error's from cs_auth()
|
---|
104 | }
|
---|
105 | break;
|
---|
106 | case 2:
|
---|
107 | aes_decrypt(buf, rs);
|
---|
108 | cs_ddump(buf, rs, "received %d bytes from %s", rs, remote_txt());
|
---|
109 | if (rs!=boundary(4, rs))
|
---|
110 | cs_debug("WARNING: packet size has wrong decryption boundary");
|
---|
111 | //n=(buf[0]==3) ? n=0x34 : 0; this was original, but statement below seems more logical -- dingo35
|
---|
112 | n=(buf[0]==3) ? 0x34 : 0;
|
---|
113 | n=boundary(4, n+20+buf[1]);
|
---|
114 | if (n<rs)
|
---|
115 | cs_debug("ignoring %d bytes of garbage", rs-n);
|
---|
116 | else
|
---|
117 | if (n>rs) rc=-3;
|
---|
118 | break;
|
---|
119 | case 3:
|
---|
120 | if (crc32(0L, buf+20, buf[1])!=b2i(4, buf+4)) rc=-4;
|
---|
121 | if (!rc) rc=n;
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | if ((rs>0) && ((rc==-1)||(rc==-2)))
|
---|
125 | cs_ddump(buf, rs, "received %d bytes from %s (native)", rs, remote_txt);
|
---|
126 | client[cs_idx].last=time((time_t *) 0);
|
---|
127 | switch(rc)
|
---|
128 | {
|
---|
129 | case -1: cs_log("packet to small (%d bytes)", rs);
|
---|
130 | break;
|
---|
131 | case -2: cs_auth_client(0, "unknown user");
|
---|
132 | break;
|
---|
133 | case -3: cs_log("incomplete request !");
|
---|
134 | break;
|
---|
135 | case -4: cs_log("checksum error (wrong password ?)");
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | return(rc);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * server functions
|
---|
143 | */
|
---|
144 |
|
---|
145 | static void camd35_request_emm(ECM_REQUEST *er)
|
---|
146 | {
|
---|
147 | int i, au;
|
---|
148 | time_t now;
|
---|
149 | static time_t last = 0;
|
---|
150 | static int disable_counter = 0;
|
---|
151 | static uchar lastserial[8] = {0,0,0,0,0,0,0,0};
|
---|
152 |
|
---|
153 | au = client[cs_idx].au;
|
---|
154 | if ((au < 0) || (au > CS_MAXREADER))
|
---|
155 | return; // TODO
|
---|
156 |
|
---|
157 | time(&now);
|
---|
158 | if (!memcmp(lastserial, reader[au].hexserial, 8))
|
---|
159 | if (abs(now-last) < 180) return;
|
---|
160 |
|
---|
161 | memcpy(lastserial, reader[au].hexserial, 8);
|
---|
162 | last = now;
|
---|
163 |
|
---|
164 | if (reader[au].caid[0])
|
---|
165 | {
|
---|
166 | disable_counter = 0;
|
---|
167 | log_emm_request(au);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | if (disable_counter > 2)
|
---|
171 | return;
|
---|
172 | else
|
---|
173 | disable_counter++;
|
---|
174 |
|
---|
175 | memset(mbuf, 0, sizeof(mbuf));
|
---|
176 | mbuf[2] = mbuf[3] = 0xff; // must not be zero
|
---|
177 | memcpy(mbuf + 8, i2b(2, er->srvid), 2);
|
---|
178 |
|
---|
179 | //override request provid with auprovid if set in CMD05
|
---|
180 | if(reader[au].auprovid) {
|
---|
181 | if(reader[au].auprovid != er->prid)
|
---|
182 | memcpy(mbuf + 12, i2b(4, reader[au].auprovid), 4);
|
---|
183 | else
|
---|
184 | memcpy(mbuf + 12, i2b(4, er->prid), 4);
|
---|
185 | } else {
|
---|
186 | memcpy(mbuf + 12, i2b(4, er->prid), 4);
|
---|
187 | }
|
---|
188 |
|
---|
189 | memcpy(mbuf + 16, i2b(2, er->pid), 2);
|
---|
190 | mbuf[0] = 5;
|
---|
191 | mbuf[1] = 111;
|
---|
192 | if (reader[au].caid[0])
|
---|
193 | {
|
---|
194 | mbuf[39] = 1; // no. caids
|
---|
195 | mbuf[20] = reader[au].caid[0]>>8; // caid's (max 8)
|
---|
196 | mbuf[21] = reader[au].caid[0]&0xff;
|
---|
197 | memcpy(mbuf + 40, reader[au].hexserial, 6); // serial now 6 bytes
|
---|
198 | mbuf[47] = reader[au].nprov;
|
---|
199 | for (i = 0; i < reader[au].nprov; i++)
|
---|
200 | {
|
---|
201 | if (((reader[au].caid[0] >= 0x1700) && (reader[au].caid[0] <= 0x1799)) || // Betacrypt
|
---|
202 | ((reader[au].caid[0] >= 0x0600) && (reader[au].caid[0] <= 0x0699))) // Irdeto (don't know if this is correct, cause I don't own a IRDETO-Card)
|
---|
203 | {
|
---|
204 | mbuf[48 + (i*5)] = reader[au].prid[i][0];
|
---|
205 | memcpy(&mbuf[50 + (i*5)], &reader[au].prid[i][1], 3);
|
---|
206 | }
|
---|
207 | else
|
---|
208 | {
|
---|
209 | mbuf[48 + (i * 5)] = reader[au].prid[i][2];
|
---|
210 | mbuf[49 + (i * 5)] =reader[au].prid[i][3];
|
---|
211 | memcpy(&mbuf[50 + (i * 5)], &reader[au].sa[i][0],4); // for conax we need at least 4 Bytes
|
---|
212 | }
|
---|
213 | }
|
---|
214 | //we think client/server protocols should deliver all information, and only readers should discard EMM
|
---|
215 | mbuf[128] = (reader[au].blockemm_g == 1) ? 0: 1;
|
---|
216 | mbuf[129] = (reader[au].blockemm_s == 1) ? 0: 1;
|
---|
217 | mbuf[130] = (reader[au].blockemm_u == 1) ? 0: 1;
|
---|
218 | //mbuf[131] = reader[au].card_system; //Cardsystem for Oscam client
|
---|
219 | }
|
---|
220 | else // disable emm
|
---|
221 | mbuf[20] = mbuf[39] = mbuf[40] = mbuf[47] = mbuf[49] = 1;
|
---|
222 |
|
---|
223 | memcpy(mbuf + 10, mbuf + 20, 2);
|
---|
224 | camd35_send(mbuf); // send with data-len 111 for camd3 > 3.890
|
---|
225 | mbuf[1]++;
|
---|
226 | camd35_send(mbuf); // send with data-len 112 for camd3 < 3.890
|
---|
227 | }
|
---|
228 |
|
---|
229 | static void camd35_send_dcw(ECM_REQUEST *er)
|
---|
230 | {
|
---|
231 | uchar *buf;
|
---|
232 | buf = req + (er->cpti * REQ_SIZE); // get orig request
|
---|
233 |
|
---|
234 | if (((er->rcEx > 0) || (er->rc == 8)) && !client[cs_idx].c35_suppresscmd08)
|
---|
235 | {
|
---|
236 | buf[0] = 0x08;
|
---|
237 | buf[1] = 2;
|
---|
238 | memset(buf + 20, 0, buf[1]);
|
---|
239 | }
|
---|
240 | else if (er->rc == 13)
|
---|
241 | {
|
---|
242 | buf[0] = 0x08;
|
---|
243 | buf[1] = 2;
|
---|
244 | buf[20] = 0;
|
---|
245 | /*
|
---|
246 | * the second Databyte should be forseen for a sleeptime in minutes
|
---|
247 | * whoever knows the camd3 protocol related to CMD08 - please help!
|
---|
248 | * on tests this don't work with native camd3
|
---|
249 | */
|
---|
250 | buf[21] = client[cs_idx].c35_sleepsend;
|
---|
251 | cs_log("%s stop request send", client[cs_idx].usr);
|
---|
252 | }
|
---|
253 | else
|
---|
254 | {
|
---|
255 | // Send CW
|
---|
256 | if ((er->rc < 4) || (er->rc == 7))
|
---|
257 | {
|
---|
258 | if (buf[0]==3)
|
---|
259 | memmove(buf + 20 + 16, buf + 20 + buf[1], 0x34);
|
---|
260 | buf[0]++;
|
---|
261 | buf[1] = 16;
|
---|
262 | memcpy(buf+20, er->cw, buf[1]);
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | // Send old CMD44 to prevent cascading problems with older mpcs/oscam versions
|
---|
267 | buf[0] = 0x44;
|
---|
268 | buf[1] = 0;
|
---|
269 | }
|
---|
270 | }
|
---|
271 | camd35_send(buf);
|
---|
272 | camd35_request_emm(er);
|
---|
273 | }
|
---|
274 |
|
---|
275 | static void camd35_process_ecm(uchar *buf)
|
---|
276 | {
|
---|
277 | ECM_REQUEST *er;
|
---|
278 | if (!(er = get_ecmtask()))
|
---|
279 | return;
|
---|
280 | er->l = buf[1];
|
---|
281 | memcpy(req + (er->cpti*REQ_SIZE), buf, 0x34 + 20 + er->l); // save request
|
---|
282 | er->srvid = b2i(2, buf+ 8);
|
---|
283 | er->caid = b2i(2, buf+10);
|
---|
284 | er->prid = b2i(4, buf+12);
|
---|
285 | er->pid = b2i(2, buf+16);
|
---|
286 | memcpy(er->ecm, buf + 20, er->l);
|
---|
287 | get_cw(er);
|
---|
288 | }
|
---|
289 |
|
---|
290 | static void camd35_process_emm(uchar *buf)
|
---|
291 | {
|
---|
292 | int au;
|
---|
293 | EMM_PACKET epg;
|
---|
294 | memset(&epg, 0, sizeof(epg));
|
---|
295 | au = client[cs_idx].au;
|
---|
296 | if ((au < 0) || (au > CS_MAXREADER)) return; // TODO
|
---|
297 | epg.l = buf[1];
|
---|
298 | memcpy(epg.caid, buf + 10, 2);
|
---|
299 | memcpy(epg.provid, buf + 12 , 4);
|
---|
300 | memcpy(epg.emm, buf + 20, epg.l);
|
---|
301 | do_emm(&epg);
|
---|
302 | }
|
---|
303 |
|
---|
304 | static void camd35_server()
|
---|
305 | {
|
---|
306 | int n;
|
---|
307 |
|
---|
308 | req=(uchar *)malloc(CS_MAXPENDING*REQ_SIZE);
|
---|
309 | if (!req)
|
---|
310 | {
|
---|
311 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
312 | cs_exit(1);
|
---|
313 | }
|
---|
314 | memset(req, 0, CS_MAXPENDING*REQ_SIZE);
|
---|
315 |
|
---|
316 | is_udp = (ph[client[cs_idx].ctyp].type == MOD_CONN_UDP);
|
---|
317 |
|
---|
318 | while ((n=process_input(mbuf, sizeof(mbuf), cfg->cmaxidle))>0)
|
---|
319 | {
|
---|
320 | switch(mbuf[0])
|
---|
321 | {
|
---|
322 | case 0: // ECM
|
---|
323 | case 3: // ECM (cascading)
|
---|
324 | camd35_process_ecm(mbuf);
|
---|
325 | break;
|
---|
326 | case 6: // EMM
|
---|
327 | case 19: // EMM
|
---|
328 | camd35_process_emm(mbuf);
|
---|
329 | break;
|
---|
330 | default:
|
---|
331 | cs_log("unknown camd35 command! (%d)", mbuf[0]);
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | if(req) { free(req); req=0;}
|
---|
336 |
|
---|
337 | cs_disconnect_client();
|
---|
338 | }
|
---|
339 |
|
---|
340 | /*
|
---|
341 | * client functions
|
---|
342 | */
|
---|
343 |
|
---|
344 | static void casc_set_account()
|
---|
345 | {
|
---|
346 | strcpy((char *)upwd, reader[ridx].r_pwd);
|
---|
347 | memcpy(client[cs_idx].ucrc, i2b(4, crc32(0L, MD5((unsigned char *)reader[ridx].r_usr, strlen(reader[ridx].r_usr), NULL), 16)), 4);
|
---|
348 | aes_set_key((char *)MD5(upwd, strlen((char *)upwd), NULL));
|
---|
349 | client[cs_idx].crypted=1;
|
---|
350 | }
|
---|
351 |
|
---|
352 | int camd35_client_init()
|
---|
353 | {
|
---|
354 | static struct sockaddr_in loc_sa;
|
---|
355 | struct protoent *ptrp;
|
---|
356 | int p_proto;//, sock_type;
|
---|
357 | char ptxt[16];
|
---|
358 |
|
---|
359 | pfd=0;
|
---|
360 | if (reader[ridx].r_port<=0)
|
---|
361 | {
|
---|
362 | cs_log("invalid port %d for server %s", reader[ridx].r_port, reader[ridx].device);
|
---|
363 | return(1);
|
---|
364 | }
|
---|
365 | is_udp=(reader[ridx].typ==R_CAMD35);
|
---|
366 | if( (ptrp=getprotobyname(is_udp ? "udp" : "tcp")) )
|
---|
367 | p_proto=ptrp->p_proto;
|
---|
368 | else
|
---|
369 | p_proto=(is_udp) ? 17 : 6; // use defaults on error
|
---|
370 |
|
---|
371 | client[cs_idx].ip=0;
|
---|
372 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
373 | loc_sa.sin_family = AF_INET;
|
---|
374 | #ifdef LALL
|
---|
375 | if (cfg->serverip[0])
|
---|
376 | loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip);
|
---|
377 | else
|
---|
378 | #endif
|
---|
379 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
380 | loc_sa.sin_port = htons(reader[ridx].l_port);
|
---|
381 |
|
---|
382 | if ((client[cs_idx].udp_fd=socket(PF_INET, is_udp ? SOCK_DGRAM : SOCK_STREAM, p_proto))<0)
|
---|
383 | {
|
---|
384 | cs_log("Socket creation failed (errno=%d)", errno);
|
---|
385 | cs_exit(1);
|
---|
386 | }
|
---|
387 |
|
---|
388 | #ifdef SO_PRIORITY
|
---|
389 | if (cfg->netprio)
|
---|
390 | setsockopt(client[cs_idx].udp_fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg->netprio, sizeof(ulong));
|
---|
391 | #endif
|
---|
392 |
|
---|
393 | if (reader[ridx].l_port>0)
|
---|
394 | {
|
---|
395 | if (bind(client[cs_idx].udp_fd, (struct sockaddr *)&loc_sa, sizeof (loc_sa))<0)
|
---|
396 | {
|
---|
397 | cs_log("bind failed (errno=%d)", errno);
|
---|
398 | close(client[cs_idx].udp_fd);
|
---|
399 | return(1);
|
---|
400 | }
|
---|
401 | sprintf(ptxt, ", port=%d", reader[ridx].l_port);
|
---|
402 | }
|
---|
403 | else
|
---|
404 | ptxt[0]='\0';
|
---|
405 |
|
---|
406 | casc_set_account();
|
---|
407 | memset((char *)&client[cs_idx].udp_sa, 0, sizeof(client[cs_idx].udp_sa));
|
---|
408 | client[cs_idx].udp_sa.sin_family=AF_INET;
|
---|
409 | client[cs_idx].udp_sa.sin_port=htons((u_short)reader[ridx].r_port);
|
---|
410 |
|
---|
411 | cs_log("proxy %s:%d (fd=%d%s)",
|
---|
412 | reader[ridx].device, reader[ridx].r_port,
|
---|
413 | client[cs_idx].udp_fd, ptxt);
|
---|
414 |
|
---|
415 | if (is_udp) {
|
---|
416 | pfd=client[cs_idx].udp_fd;
|
---|
417 | }
|
---|
418 |
|
---|
419 | return(0);
|
---|
420 | }
|
---|
421 |
|
---|
422 | int camd35_client_init_log()
|
---|
423 | {
|
---|
424 | static struct sockaddr_in loc_sa;
|
---|
425 | struct protoent *ptrp;
|
---|
426 | int p_proto;
|
---|
427 |
|
---|
428 | if (reader[ridx].log_port<=0)
|
---|
429 | {
|
---|
430 | cs_log("invalid port %d for camd3-loghost", reader[ridx].log_port);
|
---|
431 | return(1);
|
---|
432 | }
|
---|
433 |
|
---|
434 | ptrp=getprotobyname("udp");
|
---|
435 | if (ptrp)
|
---|
436 | p_proto=ptrp->p_proto;
|
---|
437 | else
|
---|
438 | p_proto=17; // use defaults on error
|
---|
439 |
|
---|
440 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
441 | loc_sa.sin_family = AF_INET;
|
---|
442 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
443 | loc_sa.sin_port = htons(reader[ridx].log_port);
|
---|
444 |
|
---|
445 | if ((logfd=socket(PF_INET, SOCK_DGRAM, p_proto))<0)
|
---|
446 | {
|
---|
447 | cs_log("Socket creation failed (errno=%d)", errno);
|
---|
448 | return(1);
|
---|
449 | }
|
---|
450 |
|
---|
451 | if (bind(logfd, (struct sockaddr *)&loc_sa, sizeof(loc_sa))<0)
|
---|
452 | {
|
---|
453 | cs_log("bind failed (errno=%d)", errno);
|
---|
454 | close(logfd);
|
---|
455 | return(1);
|
---|
456 | }
|
---|
457 |
|
---|
458 | cs_log("camd3 loghost initialized (fd=%d, port=%d)",
|
---|
459 | logfd, reader[ridx].log_port);
|
---|
460 |
|
---|
461 | return(0);
|
---|
462 | }
|
---|
463 |
|
---|
464 | static int tcp_connect()
|
---|
465 | {
|
---|
466 | if (!reader[ridx].tcp_connected)
|
---|
467 | {
|
---|
468 | int handle=0;
|
---|
469 | handle = network_tcp_connection_open();
|
---|
470 | if (handle<0) return(0);
|
---|
471 |
|
---|
472 | reader[ridx].tcp_connected = 1;
|
---|
473 | reader[ridx].card_status = CARD_INSERTED;
|
---|
474 | reader[ridx].last_s = reader[ridx].last_g = time((time_t *)0);
|
---|
475 | pfd = client[cs_idx].udp_fd = handle;
|
---|
476 | }
|
---|
477 | if (!client[cs_idx].udp_fd) return(0);
|
---|
478 | return(1);
|
---|
479 | }
|
---|
480 |
|
---|
481 | static int camd35_send_ecm(ECM_REQUEST *er, uchar *buf)
|
---|
482 | {
|
---|
483 | char *typtext[]={"ok", "invalid", "sleeping"};
|
---|
484 |
|
---|
485 | if (stopped) {
|
---|
486 | if (er->srvid == lastsrvid && er->caid == lastcaid && er->pid == lastpid){
|
---|
487 | cs_log("%s is stopped - requested by server (%s)",
|
---|
488 | reader[ridx].label, typtext[stopped]);
|
---|
489 | return(-1);
|
---|
490 | }
|
---|
491 | else {
|
---|
492 | stopped = 0;
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | lastsrvid = er->srvid;
|
---|
497 | lastcaid = er->caid;
|
---|
498 | lastpid = er->pid;
|
---|
499 |
|
---|
500 | if (is_udp) {
|
---|
501 | if (!client[cs_idx].udp_sa.sin_addr.s_addr)
|
---|
502 | if (!hostResolve(ridx)) return -1;
|
---|
503 | }
|
---|
504 | else {
|
---|
505 | if (!tcp_connect()) return -1;
|
---|
506 | }
|
---|
507 |
|
---|
508 | reader[ridx].card_status = CARD_INSERTED; //for udp
|
---|
509 |
|
---|
510 | memset(buf, 0, 20);
|
---|
511 | memset(buf + 20, 0xff, er->l+15);
|
---|
512 | buf[1]=er->l;
|
---|
513 | memcpy(buf + 8, i2b(2, er->srvid), 2);
|
---|
514 | memcpy(buf + 10, i2b(2, er->caid ), 2);
|
---|
515 | memcpy(buf + 12, i2b(4, er->prid ), 4);
|
---|
516 | // memcpy(buf+16, i2b(2, er->pid ), 2);
|
---|
517 | // memcpy(buf+16, &er->idx , 2);
|
---|
518 | memcpy(buf + 16, i2b(2, er->idx ), 2);
|
---|
519 | buf[18] = 0xff;
|
---|
520 | buf[19] = 0xff;
|
---|
521 | memcpy(buf + 20, er->ecm , er->l);
|
---|
522 | return((camd35_send(buf) < 1) ? (-1) : 0);
|
---|
523 | }
|
---|
524 |
|
---|
525 | static int camd35_send_emm(EMM_PACKET *ep)
|
---|
526 | {
|
---|
527 | uchar buf[512];
|
---|
528 |
|
---|
529 | if (is_udp) {
|
---|
530 | if (!client[cs_idx].udp_sa.sin_addr.s_addr)
|
---|
531 | if (!hostResolve(ridx)) return -1;
|
---|
532 | }
|
---|
533 | else {
|
---|
534 | if (!tcp_connect()) return -1;
|
---|
535 | }
|
---|
536 |
|
---|
537 | memset(buf, 0, 20);
|
---|
538 | memset(buf+20, 0xff, ep->l+15);
|
---|
539 |
|
---|
540 | buf[0]=0x06;
|
---|
541 | buf[1]=ep->l;
|
---|
542 | memcpy(buf+10, ep->caid, 2);
|
---|
543 | memcpy(buf+12, ep->provid, 4);
|
---|
544 | memcpy(buf+20, ep->emm, ep->l);
|
---|
545 |
|
---|
546 | return((camd35_send(buf)<1) ? 0 : 1);
|
---|
547 | }
|
---|
548 |
|
---|
549 | static int camd35_recv_chk(uchar *dcw, int *rc, uchar *buf)
|
---|
550 | {
|
---|
551 | ushort idx;
|
---|
552 | char *typtext[]={"ok", "invalid", "sleeping"};
|
---|
553 |
|
---|
554 | // reading CMD05 Emm request and set serial
|
---|
555 | if (buf[0] == 0x05 && buf[1] == 111) {
|
---|
556 |
|
---|
557 | //cs_log("CMD05: %s", cs_hexdump(1, buf, buf[1]));
|
---|
558 | reader[ridx].nprov = 0; //reset if number changes on reader change
|
---|
559 | reader[ridx].nprov = buf[47];
|
---|
560 | reader[ridx].caid[0] = b2i(2, buf + 20);
|
---|
561 | reader[ridx].auprovid = b2i(4, buf + 12);
|
---|
562 |
|
---|
563 | int i;
|
---|
564 | for (i=0; i<reader[ridx].nprov; i++) {
|
---|
565 | if (((reader[ridx].caid[0] >= 0x1700) && (reader[ridx].caid[0] <= 0x1799)) || // Betacrypt
|
---|
566 | ((reader[ridx].caid[0] >= 0x0600) && (reader[ridx].caid[0] <= 0x0699))) // Irdeto (don't know if this is correct, cause I don't own a IRDETO-Card)
|
---|
567 | {
|
---|
568 | reader[ridx].prid[i][0] = buf[48 + (i*5)];
|
---|
569 | memcpy(&reader[ridx].prid[i][1], &buf[50 + (i * 5)], 3);
|
---|
570 | } else {
|
---|
571 | reader[ridx].prid[i][2] = buf[48 + (i * 5)];
|
---|
572 | reader[ridx].prid[i][3] = buf[49+ (i * 5)];
|
---|
573 | memcpy(&reader[ridx].sa[i][0], &buf[50 + (i * 5)], 4);
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | memcpy(reader[ridx].hexserial, buf + 40, 6);
|
---|
578 | reader[ridx].hexserial[6] = 0;
|
---|
579 | reader[ridx].hexserial[7] = 0;
|
---|
580 |
|
---|
581 | reader[ridx].blockemm_g = (buf[128]==1) ? 0: 1;
|
---|
582 | reader[ridx].blockemm_s = (buf[129]==1) ? 0: 1;
|
---|
583 | reader[ridx].blockemm_u = (buf[130]==1) ? 0: 1;
|
---|
584 | reader[ridx].card_system = get_cardsystem(reader[ridx].caid[0]);
|
---|
585 | cs_log("%s CMD05 AU request for caid: %04X auprovid: %06lX",
|
---|
586 | reader[ridx].label,
|
---|
587 | reader[ridx].caid[0],
|
---|
588 | reader[ridx].auprovid);
|
---|
589 | }
|
---|
590 |
|
---|
591 | if (buf[0] == 0x08) {
|
---|
592 | if(buf[21] == 0xFF) {
|
---|
593 | stopped = 2; // server says sleep
|
---|
594 | reader[ridx].card_status = NO_CARD;
|
---|
595 | } else {
|
---|
596 | stopped = 1; // server says invalid
|
---|
597 | reader[ridx].card_status = CARD_FAILURE;
|
---|
598 | }
|
---|
599 | cs_log("%s CMD08 (%02X - %d) stop request by server (%s)",
|
---|
600 | reader[ridx].label, buf[21], buf[21], typtext[stopped]);
|
---|
601 | }
|
---|
602 |
|
---|
603 | // CMD44: old reject command introduced in mpcs
|
---|
604 | // keeping this for backward compatibility
|
---|
605 | if ((buf[0] != 1) && (buf[0] != 0x44) && (buf[0] != 0x08))
|
---|
606 | return(-1);
|
---|
607 |
|
---|
608 | idx = b2i(2, buf+16);
|
---|
609 |
|
---|
610 | *rc = ((buf[0] != 0x44) && (buf[0] != 0x08));
|
---|
611 |
|
---|
612 | memcpy(dcw, buf+20, 16);
|
---|
613 | return(idx);
|
---|
614 | }
|
---|
615 |
|
---|
616 | static int camd35_recv_log(ushort *caid, ulong *provid, ushort *srvid)
|
---|
617 | {
|
---|
618 | int i;
|
---|
619 | uchar buf[512], *ptr, *ptr2;
|
---|
620 | ushort idx=0;
|
---|
621 | if (!logfd) return(-1);
|
---|
622 | if ((i=recv(logfd, buf, sizeof(buf), 0))<=0) return(-1);
|
---|
623 | buf[i]=0;
|
---|
624 |
|
---|
625 | if (!(ptr=(uchar *)strstr((char *)buf, " -> "))) return(-1);
|
---|
626 | ptr+=4;
|
---|
627 | if (strstr((char *)ptr, " decoded ")) return(-1); // skip "found"s
|
---|
628 | if (!(ptr2=(uchar *)strchr((char *)ptr, ' '))) return(-1); // corrupt
|
---|
629 | *ptr2=0;
|
---|
630 |
|
---|
631 | for (i=0, ptr2=(uchar *)strtok((char *)ptr, ":"); ptr2; i++, ptr2=(uchar *)strtok(NULL, ":"))
|
---|
632 | {
|
---|
633 | trim((char *)ptr2);
|
---|
634 | switch(i)
|
---|
635 | {
|
---|
636 | case 0: *caid =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
637 | case 1: *provid=cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
638 | case 2: *srvid =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
639 | case 3: idx =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
640 | }
|
---|
641 | if (errno) return(-1);
|
---|
642 | }
|
---|
643 | return(idx&0x1FFF);
|
---|
644 | }
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * module definitions
|
---|
648 | */
|
---|
649 |
|
---|
650 | void module_camd35(struct s_module *ph)
|
---|
651 | {
|
---|
652 | static PTAB ptab;
|
---|
653 | ptab.ports[0].s_port = cfg->c35_port;
|
---|
654 | ph->ptab = &ptab;
|
---|
655 | ph->ptab->nports = 1;
|
---|
656 |
|
---|
657 | strcpy(ph->desc, "camd 3.5x");
|
---|
658 | ph->type=MOD_CONN_UDP;
|
---|
659 | ph->multi=1;
|
---|
660 | ph->watchdog=1;
|
---|
661 | ph->s_ip=cfg->c35_srvip;
|
---|
662 | ph->s_handler=camd35_server;
|
---|
663 | ph->recv=camd35_recv;
|
---|
664 | ph->send_dcw=camd35_send_dcw;
|
---|
665 | ph->c_multi=1;
|
---|
666 | ph->c_init=camd35_client_init;
|
---|
667 | ph->c_recv_chk=camd35_recv_chk;
|
---|
668 | ph->c_send_ecm=camd35_send_ecm;
|
---|
669 | ph->c_send_emm=camd35_send_emm;
|
---|
670 | ph->c_init_log=camd35_client_init_log;
|
---|
671 | ph->c_recv_log=camd35_recv_log;
|
---|
672 | }
|
---|
673 |
|
---|
674 | void module_camd35_tcp(struct s_module *ph)
|
---|
675 | {
|
---|
676 | strcpy(ph->desc, "cs378x");
|
---|
677 | ph->type=MOD_CONN_TCP;
|
---|
678 | ph->multi=1;
|
---|
679 | ph->watchdog=1;
|
---|
680 | ph->ptab=&cfg->c35_tcp_ptab;
|
---|
681 | if (ph->ptab->nports==0)
|
---|
682 | ph->ptab->nports=1; // show disabled in log
|
---|
683 | ph->s_ip=cfg->c35_tcp_srvip;
|
---|
684 | ph->s_handler=camd35_server;
|
---|
685 | ph->recv=camd35_recv;
|
---|
686 | ph->send_dcw=camd35_send_dcw;
|
---|
687 | ph->c_multi=1;
|
---|
688 | ph->c_init=camd35_client_init;
|
---|
689 | ph->c_recv_chk=camd35_recv_chk;
|
---|
690 | ph->c_send_ecm=camd35_send_ecm;
|
---|
691 | ph->c_send_emm=camd35_send_emm;
|
---|
692 | ph->c_init_log=camd35_client_init_log;
|
---|
693 | ph->c_recv_log=camd35_recv_log;
|
---|
694 | }
|
---|