1 | #include "globals.h"
|
---|
2 | extern struct s_reader reader[CS_MAXREADER];
|
---|
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 |
|
---|
17 | static int camd35_send(uchar *buf)
|
---|
18 | {
|
---|
19 | int l;
|
---|
20 | unsigned char rbuf[REQ_SIZE+15+4], *sbuf = rbuf + 4;
|
---|
21 | struct s_client *cl = cur_client();
|
---|
22 |
|
---|
23 | if (!cl->udp_fd) return(-1);
|
---|
24 | l = 20 + buf[1] + (((buf[0] == 3) || (buf[0] == 4)) ? 0x34 : 0);
|
---|
25 | memcpy(rbuf, cl->ucrc, 4);
|
---|
26 | memcpy(sbuf, buf, l);
|
---|
27 | memset(sbuf + l, 0xff, 15); // set unused space to 0xff for newer camd3's
|
---|
28 | memcpy(sbuf + 4, i2b(4, crc32(0L, sbuf+20, sbuf[1])), 4);
|
---|
29 | l = boundary(4, l);
|
---|
30 | cs_ddump(sbuf, l, "send %d bytes to %s", l, remote_txt());
|
---|
31 | aes_encrypt(sbuf, l);
|
---|
32 |
|
---|
33 | int status;
|
---|
34 | if (cl->is_udp) {
|
---|
35 | status = sendto(cl->udp_fd, rbuf, l+4, 0,
|
---|
36 | (struct sockaddr *)&cl->udp_sa,
|
---|
37 | sizeof(cl->udp_sa));
|
---|
38 | if (status == -1) cl->udp_sa.sin_addr.s_addr = 0;
|
---|
39 | }
|
---|
40 | else {
|
---|
41 | status = send(cl->udp_fd, rbuf, l + 4, 0);
|
---|
42 | if (status == -1) network_tcp_connection_close(&reader[cl->ridx], cl->pfd);
|
---|
43 | }
|
---|
44 | return status;
|
---|
45 | }
|
---|
46 |
|
---|
47 | static int camd35_auth_client(uchar *ucrc)
|
---|
48 | {
|
---|
49 | int rc=1;
|
---|
50 | ulong crc;
|
---|
51 | struct s_auth *account;
|
---|
52 | struct s_client *cl = cur_client();
|
---|
53 |
|
---|
54 | if (cl->upwd[0])
|
---|
55 | return(memcmp(cl->ucrc, ucrc, 4) ? 1 : 0);
|
---|
56 | cl->crypted=1;
|
---|
57 | crc=(((ucrc[0]<<24) | (ucrc[1]<<16) | (ucrc[2]<<8) | ucrc[3]) & 0xffffffffL);
|
---|
58 | for (account=cfg->account; (account) && (!cl->upwd[0]); account=account->next)
|
---|
59 | if (crc==crc32(0L, MD5((unsigned char *)account->usr, strlen(account->usr), cl->dump), 16))
|
---|
60 | {
|
---|
61 | memcpy(cl->ucrc, ucrc, 4);
|
---|
62 | strcpy((char *)cl->upwd, account->pwd);
|
---|
63 | aes_set_key((char *) MD5(cl->upwd, strlen((char *)cl->upwd), cl->dump));
|
---|
64 | rc=cs_auth_client(cl, account, NULL);
|
---|
65 | }
|
---|
66 | return(rc);
|
---|
67 | }
|
---|
68 |
|
---|
69 | static int camd35_recv(struct s_client *client, uchar *buf, int l)
|
---|
70 | {
|
---|
71 | int rc, s, rs, n=0;
|
---|
72 | unsigned char recrc[4];
|
---|
73 | for (rc=rs=s=0; !rc; s++) switch(s)
|
---|
74 | {
|
---|
75 | case 0:
|
---|
76 | if (client->typ == 'c')
|
---|
77 | {
|
---|
78 | if (!client->udp_fd) return(-9);
|
---|
79 | if (client->is_udp)
|
---|
80 | rs=recv_from_udpipe(buf);
|
---|
81 | else
|
---|
82 | rs=recv(client->udp_fd, buf, l, 0);
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | if (!client->udp_fd) return(-9);
|
---|
87 | rs = recv(client->udp_fd, buf, l, 0);
|
---|
88 | }
|
---|
89 | if (rs < 24) rc = -1;
|
---|
90 | break;
|
---|
91 | case 1:
|
---|
92 | memcpy(recrc, buf, 4);
|
---|
93 | memmove(buf, buf+4, rs-=4);
|
---|
94 | switch (camd35_auth_client(recrc))
|
---|
95 | {
|
---|
96 | case 0: break; // ok
|
---|
97 | case 1: rc=-2; break; // unknown user
|
---|
98 | default: rc=-9; break; // error's from cs_auth()
|
---|
99 | }
|
---|
100 | break;
|
---|
101 | case 2:
|
---|
102 | aes_decrypt(buf, rs);
|
---|
103 | cs_ddump(buf, rs, "received %d bytes from %s", rs, remote_txt());
|
---|
104 | if (rs!=boundary(4, rs))
|
---|
105 | {
|
---|
106 | cs_debug("WARNING: packet size has wrong decryption boundary");
|
---|
107 | }
|
---|
108 | //n=(buf[0]==3) ? n=0x34 : 0; this was original, but statement below seems more logical -- dingo35
|
---|
109 | n=(buf[0]==3) ? 0x34 : 0;
|
---|
110 | n=boundary(4, n+20+buf[1]);
|
---|
111 | if (n<rs)
|
---|
112 | {
|
---|
113 | cs_debug("ignoring %d bytes of garbage", rs-n);
|
---|
114 | }
|
---|
115 | else
|
---|
116 | if (n>rs) rc=-3;
|
---|
117 | break;
|
---|
118 | case 3:
|
---|
119 | if (crc32(0L, buf+20, buf[1])!=b2i(4, buf+4)) rc=-4;
|
---|
120 | if (!rc) rc=n;
|
---|
121 | break;
|
---|
122 | }
|
---|
123 | if ((rs>0) && ((rc==-1)||(rc==-2)))
|
---|
124 | {
|
---|
125 | cs_ddump(buf, rs, "received %d bytes from %s (native)", rs, remote_txt);
|
---|
126 | }
|
---|
127 | client->last=time((time_t *) 0);
|
---|
128 | switch(rc)
|
---|
129 | {
|
---|
130 | case -1: cs_log("packet to small (%d bytes)", rs);
|
---|
131 | break;
|
---|
132 | case -2: cs_auth_client(client, 0, "unknown user");
|
---|
133 | break;
|
---|
134 | case -3: cs_log("incomplete request !");
|
---|
135 | break;
|
---|
136 | case -4: cs_log("checksum error (wrong password ?)");
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | return(rc);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * server functions
|
---|
144 | */
|
---|
145 |
|
---|
146 | static void camd35_request_emm(ECM_REQUEST *er)
|
---|
147 | {
|
---|
148 | int i, au;
|
---|
149 | time_t now;
|
---|
150 | uchar mbuf[1024];
|
---|
151 | struct s_client *cl = cur_client();
|
---|
152 |
|
---|
153 | au = cl->au;
|
---|
154 | if ((au < 0) || (au > CS_MAXREADER))
|
---|
155 | return; // TODO
|
---|
156 |
|
---|
157 | time(&now);
|
---|
158 | if (!memcmp(cl->lastserial, reader[au].hexserial, 8))
|
---|
159 | if (abs(now-cl->last) < 180) return;
|
---|
160 |
|
---|
161 | memcpy(cl->lastserial, reader[au].hexserial, 8);
|
---|
162 | cl->last = now;
|
---|
163 |
|
---|
164 | if (reader[au].caid[0])
|
---|
165 | {
|
---|
166 | cl->disable_counter = 0;
|
---|
167 | log_emm_request(au);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | if (cl->disable_counter > 2)
|
---|
171 | return;
|
---|
172 | else
|
---|
173 | cl->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(struct s_client *client, ECM_REQUEST *er)
|
---|
230 | {
|
---|
231 | uchar *buf;
|
---|
232 | buf = client->req + (er->cpti * REQ_SIZE); // get orig request
|
---|
233 |
|
---|
234 | if (((er->rcEx > 0) || (er->rc == 8)) && !client->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->c35_sleepsend;
|
---|
251 | cs_log("%s stop request send", client->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(cur_client()->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(cur_client(), 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 = cur_client()->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(cur_client(), &epg);
|
---|
302 | }
|
---|
303 |
|
---|
304 | static void * camd35_server(void *cli)
|
---|
305 | {
|
---|
306 | int n;
|
---|
307 | uchar mbuf[1024];
|
---|
308 |
|
---|
309 | struct s_client * client = (struct s_client *) cli;
|
---|
310 | client->thread=pthread_self();
|
---|
311 | pthread_setspecific(getclient, cli);
|
---|
312 |
|
---|
313 | client->req=(uchar *)malloc(CS_MAXPENDING*REQ_SIZE);
|
---|
314 | if (!client->req)
|
---|
315 | {
|
---|
316 | cs_log("Cannot allocate memory (errno=%d)", errno);
|
---|
317 | cs_exit(1);
|
---|
318 | }
|
---|
319 | memset(client->req, 0, CS_MAXPENDING*REQ_SIZE);
|
---|
320 |
|
---|
321 | client->is_udp = (ph[client->ctyp].type == MOD_CONN_UDP);
|
---|
322 |
|
---|
323 | while ((n=process_input(mbuf, sizeof(mbuf), cfg->cmaxidle))>0)
|
---|
324 | {
|
---|
325 | switch(mbuf[0])
|
---|
326 | {
|
---|
327 | case 0: // ECM
|
---|
328 | case 3: // ECM (cascading)
|
---|
329 | camd35_process_ecm(mbuf);
|
---|
330 | break;
|
---|
331 | case 6: // EMM
|
---|
332 | case 19: // EMM
|
---|
333 | camd35_process_emm(mbuf);
|
---|
334 | break;
|
---|
335 | default:
|
---|
336 | cs_log("unknown camd35 command! (%d)", mbuf[0]);
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | NULLFREE(client->req);
|
---|
341 |
|
---|
342 | cs_disconnect_client(client);
|
---|
343 | return NULL; //to prevent compiler message
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * client functions
|
---|
348 | */
|
---|
349 |
|
---|
350 | static void casc_set_account()
|
---|
351 | {
|
---|
352 | struct s_client *cl = cur_client();
|
---|
353 | strcpy((char *)cl->upwd, reader[cl->ridx].r_pwd);
|
---|
354 | memcpy(cl->ucrc, i2b(4, crc32(0L, MD5((unsigned char *)reader[cl->ridx].r_usr, strlen(reader[cl->ridx].r_usr), cl->dump), 16)), 4);
|
---|
355 | aes_set_key((char *)MD5(cl->upwd, strlen((char *)cl->upwd), cl->dump));
|
---|
356 | cl->crypted=1;
|
---|
357 | }
|
---|
358 |
|
---|
359 | int camd35_client_init(struct s_client *client)
|
---|
360 | {
|
---|
361 | struct sockaddr_in loc_sa;
|
---|
362 | struct protoent *ptrp;
|
---|
363 | int p_proto;//, sock_type;
|
---|
364 | char ptxt[16];
|
---|
365 |
|
---|
366 | client->pfd=0;
|
---|
367 | if (reader[client->ridx].r_port<=0)
|
---|
368 | {
|
---|
369 | cs_log("invalid port %d for server %s", reader[client->ridx].r_port, reader[client->ridx].device);
|
---|
370 | return(1);
|
---|
371 | }
|
---|
372 | client->is_udp=(reader[client->ridx].typ==R_CAMD35);
|
---|
373 | if( (ptrp=getprotobyname(client->is_udp ? "udp" : "tcp")) )
|
---|
374 | p_proto=ptrp->p_proto;
|
---|
375 | else
|
---|
376 | p_proto=(client->is_udp) ? 17 : 6; // use defaults on error
|
---|
377 |
|
---|
378 | client->ip=0;
|
---|
379 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
380 | loc_sa.sin_family = AF_INET;
|
---|
381 | #ifdef LALL
|
---|
382 | if (cfg->serverip[0])
|
---|
383 | loc_sa.sin_addr.s_addr = inet_addr(cfg->serverip);
|
---|
384 | else
|
---|
385 | #endif
|
---|
386 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
387 | loc_sa.sin_port = htons(reader[client->ridx].l_port);
|
---|
388 |
|
---|
389 | if ((client->udp_fd=socket(PF_INET, client->is_udp ? SOCK_DGRAM : SOCK_STREAM, p_proto))<0)
|
---|
390 | {
|
---|
391 | cs_log("Socket creation failed (errno=%d)", errno);
|
---|
392 | cs_exit(1);
|
---|
393 | }
|
---|
394 |
|
---|
395 | #ifdef SO_PRIORITY
|
---|
396 | if (cfg->netprio)
|
---|
397 | setsockopt(client->udp_fd, SOL_SOCKET, SO_PRIORITY, (void *)&cfg->netprio, sizeof(ulong));
|
---|
398 | #endif
|
---|
399 |
|
---|
400 | if (reader[client->ridx].l_port>0)
|
---|
401 | {
|
---|
402 | if (bind(client->udp_fd, (struct sockaddr *)&loc_sa, sizeof (loc_sa))<0)
|
---|
403 | {
|
---|
404 | cs_log("bind failed (errno=%d)", errno);
|
---|
405 | close(client->udp_fd);
|
---|
406 | return(1);
|
---|
407 | }
|
---|
408 | sprintf(ptxt, ", port=%d", reader[client->ridx].l_port);
|
---|
409 | }
|
---|
410 | else
|
---|
411 | ptxt[0]='\0';
|
---|
412 |
|
---|
413 | casc_set_account();
|
---|
414 | memset((char *)&client->udp_sa, 0, sizeof(client->udp_sa));
|
---|
415 | client->udp_sa.sin_family=AF_INET;
|
---|
416 | client->udp_sa.sin_port=htons((u_short)reader[client->ridx].r_port);
|
---|
417 |
|
---|
418 | cs_log("proxy %s:%d (fd=%d%s)",
|
---|
419 | reader[client->ridx].device, reader[client->ridx].r_port,
|
---|
420 | client->udp_fd, ptxt);
|
---|
421 |
|
---|
422 | if (client->is_udp) {
|
---|
423 | client->pfd=client->udp_fd;
|
---|
424 | }
|
---|
425 |
|
---|
426 | return(0);
|
---|
427 | }
|
---|
428 |
|
---|
429 | int camd35_client_init_log()
|
---|
430 | {
|
---|
431 | struct sockaddr_in loc_sa;
|
---|
432 | struct protoent *ptrp;
|
---|
433 | int p_proto;
|
---|
434 | struct s_client *cl = cur_client();
|
---|
435 |
|
---|
436 | if (reader[cl->ridx].log_port<=0)
|
---|
437 | {
|
---|
438 | cs_log("invalid port %d for camd3-loghost", reader[cl->ridx].log_port);
|
---|
439 | return(1);
|
---|
440 | }
|
---|
441 |
|
---|
442 | ptrp=getprotobyname("udp");
|
---|
443 | if (ptrp)
|
---|
444 | p_proto=ptrp->p_proto;
|
---|
445 | else
|
---|
446 | p_proto=17; // use defaults on error
|
---|
447 |
|
---|
448 | memset((char *)&loc_sa,0,sizeof(loc_sa));
|
---|
449 | loc_sa.sin_family = AF_INET;
|
---|
450 | loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
451 | loc_sa.sin_port = htons(reader[cl->ridx].log_port);
|
---|
452 |
|
---|
453 | if ((logfd=socket(PF_INET, SOCK_DGRAM, p_proto))<0)
|
---|
454 | {
|
---|
455 | cs_log("Socket creation failed (errno=%d)", errno);
|
---|
456 | return(1);
|
---|
457 | }
|
---|
458 |
|
---|
459 | if (bind(logfd, (struct sockaddr *)&loc_sa, sizeof(loc_sa))<0)
|
---|
460 | {
|
---|
461 | cs_log("bind failed (errno=%d)", errno);
|
---|
462 | close(logfd);
|
---|
463 | return(1);
|
---|
464 | }
|
---|
465 |
|
---|
466 | cs_log("camd3 loghost initialized (fd=%d, port=%d)",
|
---|
467 | logfd, reader[cl->ridx].log_port);
|
---|
468 |
|
---|
469 | return(0);
|
---|
470 | }
|
---|
471 |
|
---|
472 | static int tcp_connect()
|
---|
473 | {
|
---|
474 | struct s_client *cl = cur_client();
|
---|
475 | if (!reader[cl->ridx].tcp_connected)
|
---|
476 | {
|
---|
477 | int handle=0;
|
---|
478 | handle = network_tcp_connection_open();
|
---|
479 | if (handle<0) return(0);
|
---|
480 |
|
---|
481 | reader[cl->ridx].tcp_connected = 1;
|
---|
482 | reader[cl->ridx].card_status = CARD_INSERTED;
|
---|
483 | reader[cl->ridx].last_s = reader[cl->ridx].last_g = time((time_t *)0);
|
---|
484 | cl->pfd = cl->udp_fd = handle;
|
---|
485 | }
|
---|
486 | if (!cl->udp_fd) return(0);
|
---|
487 | return(1);
|
---|
488 | }
|
---|
489 |
|
---|
490 | static int camd35_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *buf)
|
---|
491 | {
|
---|
492 | static const char *typtext[]={"ok", "invalid", "sleeping"};
|
---|
493 |
|
---|
494 | if (client->stopped) {
|
---|
495 | if (er->srvid == client->lastsrvid && er->caid == client->lastcaid && er->pid == client->lastpid){
|
---|
496 | cs_log("%s is stopped - requested by server (%s)",
|
---|
497 | reader[client->ridx].label, typtext[client->stopped]);
|
---|
498 | return(-1);
|
---|
499 | }
|
---|
500 | else {
|
---|
501 | client->stopped = 0;
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 | client->lastsrvid = er->srvid;
|
---|
506 | client->lastcaid = er->caid;
|
---|
507 | client->lastpid = er->pid;
|
---|
508 |
|
---|
509 | if (client->is_udp) {
|
---|
510 | if (!client->udp_sa.sin_addr.s_addr || reader[client->ridx].last_s-reader[client->ridx].last_g > reader[client->ridx].tcp_rto)
|
---|
511 | if (!hostResolve(&reader[client->ridx])) return -1;
|
---|
512 | }
|
---|
513 | else {
|
---|
514 | if (!tcp_connect()) return -1;
|
---|
515 | }
|
---|
516 |
|
---|
517 | reader[client->ridx].card_status = CARD_INSERTED; //for udp
|
---|
518 |
|
---|
519 | memset(buf, 0, 20);
|
---|
520 | memset(buf + 20, 0xff, er->l+15);
|
---|
521 | buf[1]=er->l;
|
---|
522 | memcpy(buf + 8, i2b(2, er->srvid), 2);
|
---|
523 | memcpy(buf + 10, i2b(2, er->caid ), 2);
|
---|
524 | memcpy(buf + 12, i2b(4, er->prid ), 4);
|
---|
525 | // memcpy(buf+16, i2b(2, er->pid ), 2);
|
---|
526 | // memcpy(buf+16, &er->idx , 2);
|
---|
527 | memcpy(buf + 16, i2b(2, er->idx ), 2);
|
---|
528 | buf[18] = 0xff;
|
---|
529 | buf[19] = 0xff;
|
---|
530 | memcpy(buf + 20, er->ecm , er->l);
|
---|
531 | return((camd35_send(buf) < 1) ? (-1) : 0);
|
---|
532 | }
|
---|
533 |
|
---|
534 | static int camd35_send_emm(EMM_PACKET *ep)
|
---|
535 | {
|
---|
536 | uchar buf[512];
|
---|
537 | struct s_client *cl = cur_client();
|
---|
538 |
|
---|
539 | if (cl->is_udp) {
|
---|
540 | if (!cl->udp_sa.sin_addr.s_addr || reader[cl->ridx].last_s-reader[cl->ridx].last_g > reader[cl->ridx].tcp_rto)
|
---|
541 | if (!hostResolve(&reader[cl->ridx])) return -1;
|
---|
542 | }
|
---|
543 | else {
|
---|
544 | if (!tcp_connect()) return -1;
|
---|
545 | }
|
---|
546 |
|
---|
547 | memset(buf, 0, 20);
|
---|
548 | memset(buf+20, 0xff, ep->l+15);
|
---|
549 |
|
---|
550 | buf[0]=0x06;
|
---|
551 | buf[1]=ep->l;
|
---|
552 | memcpy(buf+10, ep->caid, 2);
|
---|
553 | memcpy(buf+12, ep->provid, 4);
|
---|
554 | memcpy(buf+20, ep->emm, ep->l);
|
---|
555 |
|
---|
556 | return((camd35_send(buf)<1) ? 0 : 1);
|
---|
557 | }
|
---|
558 |
|
---|
559 | static int camd35_recv_chk(struct s_client *client, uchar *dcw, int *rc, uchar *buf)
|
---|
560 | {
|
---|
561 | ushort idx;
|
---|
562 | static const char *typtext[]={"ok", "invalid", "sleeping"};
|
---|
563 | struct s_reader *rdr = &reader[client->ridx];
|
---|
564 |
|
---|
565 | // reading CMD05 Emm request and set serial
|
---|
566 | if (buf[0] == 0x05 && buf[1] == 111) {
|
---|
567 |
|
---|
568 | //cs_log("CMD05: %s", cs_hexdump(1, buf, buf[1]));
|
---|
569 | rdr->nprov = 0; //reset if number changes on reader change
|
---|
570 | rdr->nprov = buf[47];
|
---|
571 | rdr->caid[0] = b2i(2, buf + 20);
|
---|
572 | rdr->auprovid = b2i(4, buf + 12);
|
---|
573 |
|
---|
574 | int i;
|
---|
575 | for (i=0; i<rdr->nprov; i++) {
|
---|
576 | if (((rdr->caid[0] >= 0x1700) && (rdr->caid[0] <= 0x1799)) || // Betacrypt
|
---|
577 | ((rdr->caid[0] >= 0x0600) && (rdr->caid[0] <= 0x0699))) // Irdeto (don't know if this is correct, cause I don't own a IRDETO-Card)
|
---|
578 | {
|
---|
579 | rdr->prid[i][0] = buf[48 + (i*5)];
|
---|
580 | memcpy(&rdr->prid[i][1], &buf[50 + (i * 5)], 3);
|
---|
581 | } else {
|
---|
582 | rdr->prid[i][2] = buf[48 + (i * 5)];
|
---|
583 | rdr->prid[i][3] = buf[49+ (i * 5)];
|
---|
584 | memcpy(&rdr->sa[i][0], &buf[50 + (i * 5)], 4);
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | memcpy(rdr->hexserial, buf + 40, 6);
|
---|
589 | rdr->hexserial[6] = 0;
|
---|
590 | rdr->hexserial[7] = 0;
|
---|
591 |
|
---|
592 | rdr->blockemm_g = (buf[128]==1) ? 0: 1;
|
---|
593 | rdr->blockemm_s = (buf[129]==1) ? 0: 1;
|
---|
594 | rdr->blockemm_u = (buf[130]==1) ? 0: 1;
|
---|
595 | rdr->card_system = get_cardsystem(rdr->caid[0]);
|
---|
596 | cs_log("%s CMD05 AU request for caid: %04X auprovid: %06lX",
|
---|
597 | rdr->label,
|
---|
598 | rdr->caid[0],
|
---|
599 | rdr->auprovid);
|
---|
600 | }
|
---|
601 |
|
---|
602 | if (buf[0] == 0x08 && !cfg->c35_suppresscmd08) {
|
---|
603 | if(buf[21] == 0xFF) {
|
---|
604 | client->stopped = 2; // server says sleep
|
---|
605 | rdr->card_status = NO_CARD;
|
---|
606 | } else {
|
---|
607 | client->stopped = 1; // server says invalid
|
---|
608 | rdr->card_status = CARD_FAILURE;
|
---|
609 | }
|
---|
610 | cs_log("%s CMD08 (%02X - %d) stop request by server (%s)",
|
---|
611 | rdr->label, buf[21], buf[21], typtext[client->stopped]);
|
---|
612 | }
|
---|
613 |
|
---|
614 | // CMD44: old reject command introduced in mpcs
|
---|
615 | // keeping this for backward compatibility
|
---|
616 | if ((buf[0] != 1) && (buf[0] != 0x44) && (buf[0] != 0x08))
|
---|
617 | return(-1);
|
---|
618 |
|
---|
619 | idx = b2i(2, buf+16);
|
---|
620 |
|
---|
621 | *rc = ((buf[0] != 0x44) && (buf[0] != 0x08));
|
---|
622 |
|
---|
623 | memcpy(dcw, buf+20, 16);
|
---|
624 | return(idx);
|
---|
625 | }
|
---|
626 |
|
---|
627 | static int camd35_recv_log(ushort *caid, ulong *provid, ushort *srvid)
|
---|
628 | {
|
---|
629 | int i;
|
---|
630 | uchar buf[512], *ptr, *ptr2;
|
---|
631 | ushort idx=0;
|
---|
632 | if (!logfd) return(-1);
|
---|
633 | if ((i=recv(logfd, buf, sizeof(buf), 0))<=0) return(-1);
|
---|
634 | buf[i]=0;
|
---|
635 |
|
---|
636 | if (!(ptr=(uchar *)strstr((char *)buf, " -> "))) return(-1);
|
---|
637 | ptr+=4;
|
---|
638 | if (strstr((char *)ptr, " decoded ")) return(-1); // skip "found"s
|
---|
639 | if (!(ptr2=(uchar *)strchr((char *)ptr, ' '))) return(-1); // corrupt
|
---|
640 | *ptr2=0;
|
---|
641 |
|
---|
642 | for (i=0, ptr2=(uchar *)strtok((char *)ptr, ":"); ptr2; i++, ptr2=(uchar *)strtok(NULL, ":"))
|
---|
643 | {
|
---|
644 | trim((char *)ptr2);
|
---|
645 | switch(i)
|
---|
646 | {
|
---|
647 | case 0: *caid =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
648 | case 1: *provid=cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
649 | case 2: *srvid =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
650 | case 3: idx =cs_atoi((char *)ptr2, strlen((char *)ptr2)>>1, 0); break;
|
---|
651 | }
|
---|
652 | if (errno) return(-1);
|
---|
653 | }
|
---|
654 | return(idx&0x1FFF);
|
---|
655 | }
|
---|
656 |
|
---|
657 | /*
|
---|
658 | * module definitions
|
---|
659 | */
|
---|
660 |
|
---|
661 | void module_camd35(struct s_module *ph)
|
---|
662 | {
|
---|
663 | static PTAB ptab; //since there is always only 1 camd35 server running, this is threadsafe
|
---|
664 | ptab.ports[0].s_port = cfg->c35_port;
|
---|
665 | ph->ptab = &ptab;
|
---|
666 | ph->ptab->nports = 1;
|
---|
667 |
|
---|
668 | strcpy(ph->desc, "camd 3.5x");
|
---|
669 | ph->type=MOD_CONN_UDP;
|
---|
670 | ph->multi=1;
|
---|
671 | ph->watchdog=1;
|
---|
672 | ph->s_ip=cfg->c35_srvip;
|
---|
673 | ph->s_handler=camd35_server;
|
---|
674 | ph->recv=camd35_recv;
|
---|
675 | ph->send_dcw=camd35_send_dcw;
|
---|
676 | ph->c_multi=1;
|
---|
677 | ph->c_init=camd35_client_init;
|
---|
678 | ph->c_recv_chk=camd35_recv_chk;
|
---|
679 | ph->c_send_ecm=camd35_send_ecm;
|
---|
680 | ph->c_send_emm=camd35_send_emm;
|
---|
681 | ph->c_init_log=camd35_client_init_log;
|
---|
682 | ph->c_recv_log=camd35_recv_log;
|
---|
683 | ph->num=R_CAMD35;
|
---|
684 | }
|
---|
685 |
|
---|
686 | void module_camd35_tcp(struct s_module *ph)
|
---|
687 | {
|
---|
688 | strcpy(ph->desc, "cs378x");
|
---|
689 | ph->type=MOD_CONN_TCP;
|
---|
690 | ph->multi=1;
|
---|
691 | ph->watchdog=1;
|
---|
692 | ph->ptab=&cfg->c35_tcp_ptab;
|
---|
693 | if (ph->ptab->nports==0)
|
---|
694 | ph->ptab->nports=1; // show disabled in log
|
---|
695 | ph->s_ip=cfg->c35_tcp_srvip;
|
---|
696 | ph->s_handler=camd35_server;
|
---|
697 | ph->recv=camd35_recv;
|
---|
698 | ph->send_dcw=camd35_send_dcw;
|
---|
699 | ph->c_multi=1;
|
---|
700 | ph->c_init=camd35_client_init;
|
---|
701 | ph->c_recv_chk=camd35_recv_chk;
|
---|
702 | ph->c_send_ecm=camd35_send_ecm;
|
---|
703 | ph->c_send_emm=camd35_send_emm;
|
---|
704 | ph->c_init_log=camd35_client_init_log;
|
---|
705 | ph->c_recv_log=camd35_recv_log;
|
---|
706 | ph->num=R_CS378X;
|
---|
707 | }
|
---|